Inet.data is a library for modeling various Internet-related conceptual entities as data, supporting applications which are about the modeled entities versus interfacing with them.
Inet.data is available on Clojars.
Leiningen (project.clj):
[net.clojars.savya/inet.data "1.0.0"]
Clojure CLI (deps.edn):
net.clojars.savya/inet.data {:mvn/version "1.0.0"}
The Java parsers are generated from the Ragel grammars under src/ragel.
Run clojure -T:build ragel to regenerate them. Ragel is only needed when
changing the grammars. Do not hand-edit the generated Java files.
Currently inet.data includes support for IP addresses and networks, DNS domain names, and reverse DNS domains. Example usage follows; detailed API documentation available.
The inet.data.ip namespace defines types for IP addresses and networks and
associated functions. All public functions work in terms of a protocol which
is also implemented for strings, byte arrays, and java.net.InetAddress.
(require '[inet.data.ip :as ip])
(ip/network-contains? "192.168.1.0/24" "192.168.1.1") ;;=> true
(ip/private? "192.168.1.1") ;;=> true
(ip/special-use "2001:db8::1") ;;=> :documentation
(ip/global? "8.8.8.8") ;;=> true
(ip/address? "600d::") ;;=> true
(ip/address? "::bad::") ;;=> false
(let [rfc1918 (ip/network-set "10.0.0.0/8" "172.16.0.0/12" "192.168.0.0/16")]
(get rfc1918 "10.31.33.7") ;;=> (#ip/network "10.0.0.0/8")
(get rfc1918 "8.8.8.8") ;;=> nil
)
(seq (ip/network "192.168.0.0/30"))
;;=> (#ip/address "192.168.0.0"
;; #ip/address "192.168.0.1"
;; #ip/address "192.168.0.2"
;; #ip/address "192.168.0.3")
(ip/network-nth "192.168.0.0/30" -1)
;;=> #ip/address "192.168.0.3"
(ip/address-networks "192.168.0.0" "192.168.0.4")
;;=> #{#ip/network "192.168.0.0/30"
;; #ip/network "192.168.0.4/32"}
(ip/aggregate-networks ["10.0.0.0/24" "10.0.1.0/24"])
;;=> #{#ip/network "10.0.0.0/23"}
Parsing has two deliberate modes. The address, network, and domain
constructors, along with their predicate forms, are lenient: malformed input
returns nil or false. Operations that need a parsed value use strict
coercion and throw IPAddressException, IPNetworkException, or
DNSDomainException when input cannot be interpreted. Use address?,
network?, or domain? when testing input instead of asserting that it is
valid. aggregate-networks and the ARPA conversion functions remain lenient
filtering and conversion APIs and return their documented empty or nil
results for malformed input.
The inet.data.dns namespace defines a type for representing DNS domain names
and associated functions. All public functions work in terms of a protocol
which is also implemented for strings and byte arrays.
(require '[inet.data.dns :as dns])
(dns/domain-contains? "com" "example.com") ;;=> true
(dns/domain? "example.com") ;;=> true
(dns/domain? "bad..com") ;;=> false
(dns/domain-parent "www.example.com") ;;=> #dns/domain "example.com"
(let [gtlds (dns/domain-set "com" "net" "org")]
(get gtlds "example.com") ;;=> (#dns/domain "com")
(get gtlds "does.not.exist") ;;=> nil
)
The inet.data.arpa namespace bridges IP addresses and networks with reverse
DNS domains. IPv4 zones use octet-aligned prefixes and IPv6 zones use
nibble-aligned prefixes.
(require '[inet.data.arpa :as arpa])
(arpa/ip->domain "10.0.2.1")
;;=> #dns/domain "1.2.0.10.in-addr.arpa"
(arpa/domain->ip "2.0.10.in-addr.arpa")
;;=> #ip/network "10.0.2.0/24"
The inet.data.format.psl namespace defines functions for working with files
in the Mozilla Public Suffix List format. It can automatically use the current
version of the list as maintained by the Mozilla project. The format is
generally useful for domain suffix applications, but most applications will
need to provide their own list(s) customized for their particular use cases.
(require '[inet.data.format.psl :as psl])
(psl/lookup "www.example.co.uk") ;;=> #dns/domain "example.co.uk"
Copyright © 2012-2015 Marshall Bockrath-Vandegrift & Damballa, Inc.
Maintenance fork (2026) by Savyasachi, original: https://github.com/damballa/inet.data. Distributed under the Eclipse Public License 1.0, preserving the original license.
Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.
Can you improve this documentation? These fine people already did:
Savyasachi, Marshall T. Vandegrift, Marshall Bockrath-Vandegrift & Dave YarwoodEdit on GitHub
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |