netaddr
2.0.1A library for manipulating IP addresses, subnets, ranges, and sets.
netaddr
NETADDR is a zero dependency Common Lisp library for manipulating IP addresses, subnets, ranges, and sets. It is inspired by its namesake library in Python, netaddr. Confirmed to work on SBCL, ECL, ABCL, and LispWorks. NETADDR supports/provides:
- Datatypes for IPv4 and IPv6 addresses, subnets, and ranges.
- Shorthand syntax for the above with a reader macro
#i. See the IP Syntax section for details. - Helper lookup functions for RFC reserved space, e.g.,
PRIVATE?,RESERVED?, andPUBLIC?. - An
IP-SETdata structure for working with sets of addresses, subnets, and ranges. SeeMAKE-IP-SET. - Set operations on the above like union, intersection, difference, and symmetric difference.
- Membership checks of IPs against subnets, ranges, and sets using
CONTAINS?.
Class Hierarchy
┌ ─ ─ ─ ┐
┌─────── IP+ ────────┐
│ └ ─ ─ ─ ┘ │
│ │
│ │
│ │
▼ ▼
┌ ─ ─ ─ ┐ ┌──────────┐
IP-LIKE ◀─ ─ set of─ ─│ IP-SET │
└ ─ ─ ─ ┘ └──────────┘
│
├──────┐
┌────────────┘ ▼
│ ┌ ─ ─ ─ ┐
│ ┌─ IP-PAIR ──┐
▼ ▼ └ ─ ─ ─ ┘ ▼
┌──────────┐ ┌──────────┐ ┌──────────┐
│IP-ADDRESS│ │IP-NETWORK│ │ IP-RANGE │
└──────────┘ └──────────┘ └──────────┘ Users of this library will only instantiate the leaf classes in the tree above,
using their respective MAKE-* functions, or in the case of the three that
inherit from IP-LIKE, the short-hand #i notation. IP-SETs are comprised of
a set of IP-LIKEs. Most operations will expect either IP-LIKEs as arguments
and/or IP+s. For example, CONTAINS? takes an IP+ as its first argument and
an IP-LIKE as its second argument because:
- An
IP-ADDRESSCONTAINS?itself. - An
IP-NETWORKand anIP-RANGECONTAINS?themselves, any subset of those networks or ranges, and anyIP-ADDRESSthat is a member of the network or range. - An
IP-SETCONTAINS?any of its memberIP-LIKEs, and so on. When it does, the most specific member containing the argument is returned, soCONTAINS?on anIP-SETis also a longest prefix match;LONGEST-MATCHis the same operation under a name that makes that intent clear.
IP-SETs index their members lazily on first query, so membership tests and
longest prefix matches cost O(log n) regardless of how many networks a set
holds, and the set theoretic operations only examine members that actually
overlap. Mutating a set (ADD!, ADDNEW!, SUB!) maintains the index
incrementally.
Equality
There are two equality operators for IP+ subclasses:
Similar to Common Lisp's EQUAL and EQUALP, IP-EQUAL is more specific than IP-EQUALP. The former considers different classes to always be unequal, while
the latter allows comparisons across all leaf classes described in the Class
Hierarchy. For example:
NETADDR> (ip-equal #i1.1.1.1 #i1.1.1.1/32)
NIL
NETADDR> (ip-equalp #i1.1.1.1 #i1.1.1.1/32)
T
NETADDR> (ip-equalp #i1.1.1.1 #i1.1.1.1/31)
NIL
NETADDR> (ip-equal #i1.0.0.0/8 #i1.0.0.0-1.255.255.255)
NIL
NETADDR> (ip-equalp #i1.0.0.0/8 #i1.0.0.0-1.255.255.255)
T
NETADDR> (ip-equal (make-ip-set #i(1.1.1.1)) (make-ip-set #i(1.1.1.1/32)))
NIL
NETADDR> (ip-equalp (make-ip-set #i(1.1.1.1)) (make-ip-set #i(1.1.1.1/32)))
T IP-EQUAL always returns NIL if classes are different. However, IP-EQUALP returns T if the underlying object refers to the same set of IP addresses,
regardless of the concrete object type. In general, if you are comparing
individual IP-LIKEs, you'll want to use IP-EQUAL. If you are comparing IP-SETs, which may contain a mixture of classes internally, or IP-NETWORKs
and IP-RANGEs, you'll want to use IP-EQUALP.
IP Syntax
NETADDR provides a shorthand syntax for writing IP-LIKEs with the reader
macro #i, enabled by calling ENABLE-IP-SYNTAX. An address, network, or
range written directly after #i reads as a single object; a parenthesized,
whitespace-separated list of them reads as a list of objects. An element may
also be a string, or ,FORM to use the string that FORM evaluates to at run
time. Example usage is shown below:
NETADDR> #i1.2.3.4
#<IP-ADDRESS 1.2.3.4>
NETADDR> #i192.168.1.0/24
#<IP-NETWORK 192.168.1.0/24>
NETADDR> #i::-ffff::
#<IP-RANGE ::-ffff::>
NETADDR> #i(0.0.0.0 1.1.1.1)
(#<IP-ADDRESS 0.0.0.0> #<IP-ADDRESS 1.1.1.1>)
NETADDR> (multiple-value-bind (x y z) (values "1.1.1.1" "::/96" "10.20.30.40-11.20.30.40")
#i(,x ,y ,z))
(#<IP-ADDRESS 1.1.1.1> #<IP-NETWORK ::/96> #<IP-RANGE 10.20.30.40-11.20.30.40>)
NETADDR> (let ((prefix "10.0.0.0")) #i,(format nil "~a/8" prefix))
#<IP-NETWORK 10.0.0.0/8> ENABLE-IP-SYNTAX copies the current readtable and adds #i to it, so other
reader extensions you have enabled are kept; within a file being compiled or
loaded the change is local to that file, and DISABLE-IP-SYNTAX restores the
previous readtable. To use the syntax without changing *READTABLE*, bind it
to *IP-SYNTAX-READTABLE*, or install IP-READER as the #i dispatch macro
in a readtable of your own (for example with named-readtables' :dispatch-macro-char).
System Information
Definition Index
-
NETADDR
No documentation provided.-
EXTERNAL SPECIAL-VARIABLE *IP-SYNTAX-READTABLE*
The standard readtable plus the #i syntax. Bind *READTABLE* to it, or merge it into your own readtable.
-
EXTERNAL FUNCTION ADD
- SET
- &REST
- IP-LIKES
Creates a copy of SET with IP-LIKES prepended.
-
EXTERNAL FUNCTION ADD!
- SET
- &REST
- IP-LIKES
Prepend in place IP-LIKES to the IP-SET SET. Returns the modified IP-SET.
-
EXTERNAL FUNCTION ADDNEW
- SET
- &REST
- IP-LIKES
Creates a fresh IP-SET that contains the original contents of SET as well as the IP-LIKES that are not already a member or a subset of a member of SET. Returns the fresh IP-SET.
-
EXTERNAL FUNCTION ADDNEW!
- SET
- &REST
- IP-LIKES
Push IP-LIKES to IP-SET SET if they are not already a member or a subset of a member of SET. Members of SET that are subsets of an added IP-LIKE are removed. Returns the modified IP-SET.
-
EXTERNAL FUNCTION APPLY-MASK
- IP
- MASK
Make a fresh IP-NETWORK by applying MASK to IP-ADDRESS IP.
-
EXTERNAL FUNCTION IP-READER
- STREAM
- SUB-CHAR
- INFIX
The #i dispatch macro function; see ENABLE-IP-SYNTAX. Exported so the syntax can be installed into any readtable, e.g. a NAMED-READTABLES one.
-
EXTERNAL FUNCTION IP-SET-DIFFERENCE
- &REST
- IP-SETS
Returns a fresh IP-SET that is the set difference of (first IP-SETS) from (rest IP-SETS).
-
EXTERNAL FUNCTION IP-SET-INTERSECTION
- &REST
- IP-SETS
Returns a fresh IP-SET that is the set intersection of all IP-SETS.
-
EXTERNAL FUNCTION IP-SET-SYMMETRIC-DIFFERENCE
- &REST
- IP-SETS
Returns a fresh IP-SET that is the set symmetric difference of IP-SETS, i.e., the difference of the union and intersection of IP-SETS.
-
EXTERNAL FUNCTION IP-SET-UNION
- &REST
- IP-SETS
Returns a fresh IP-SET that is the set union of all IP-SETS.
-
EXTERNAL FUNCTION IP=
- IP+1
- IP+2
Synonym for IP-EQUAL.
-
EXTERNAL FUNCTION LONGEST-MATCH
- SET
- IP-LIKE
Returns the most specific member of IP-SET SET that contains IP-LIKE, i.e., the longest prefix match, or NIL if no member contains it.
-
EXTERNAL FUNCTION MAKE-IP-LIKE
- IP-LIKE-STR
Given a string for an IP-LIKE, infer the concrete type and return an object.
-
EXTERNAL FUNCTION MAKE-IP-NETWORK
- STR
Make an IP-NETWORK object from a string STR in CIDR notation, e.g., "10.20.30.40/24" or "ffff::/96".
-
EXTERNAL FUNCTION MAKE-IP-RANGE
- FIRST
- LAST
Make an IP-RANGE object given two STRINGs or INTEGERs that represent valid IP addresses as expected by MAKE-IP-ADDRESS. LAST must be greater than or equal to FIRST.
-
EXTERNAL FUNCTION MAKE-IP-SET
- SET
Make an IP-SET object given a list of IP-LIKEs.
-
EXTERNAL FUNCTION MULTICAST?
- IP
Returns T if IP is an IP designated for multicast, otherwise NIL.
-
EXTERNAL FUNCTION PRIVATE?
- IP
Returns T if IP is a private IP address, otherwise NIL.
-
EXTERNAL FUNCTION PUBLIC?
- IP
Returns T if IP is a public IP address, otherwise NIL.
-
EXTERNAL FUNCTION RESERVED?
- IP
Returns T if IP is a reserved IP address, otherwise NIL.
-
EXTERNAL FUNCTION ROUTE-TYPE
- IP
Returns one of (:PRIVATE :RESERVED :MULTICAST :PUBLIC :OTHER) based on RFC defined IP usage.
-
EXTERNAL FUNCTION SUB
- SET
- &REST
- IP-LIKES
Like SUB! but return a fresh IP-SET without modifying the argument SET in place.
-
EXTERNAL FUNCTION SUB!
- SET
- &REST
- IP-LIKES
Remove IP-LIKES from IP-SET SET in place. IP-LIKES that are a member or superset of a member of SET are removed. IP-LIKES that are a subset of a member of SET are SUBTRACTed in place.
-
EXTERNAL FUNCTION SUBSET?
- IP-LIKE-1
- IP-LIKE-2
Returns T or an IP-LIKE if IP-LIKE-1 is a subset of IP-LIKE-2. This is synonymous with (CONTAINS? IP-LIKE-2 IP-LIKE-1). Otherwise, returns NIL.
-
EXTERNAL FUNCTION SUPERSET?
- IP-LIKE-1
- IP-LIKE-2
Returns T or an IP-LIKE if IP-LIKE-1 is a superset of IP-LIKE-2. Otherwise, NIL.
-
EXTERNAL GENERIC-FUNCTION CONTAINS?
- IP+
- IP-LIKE
Returns T or an IP-LIKE if IP+ contains IP-LIKE, NIL otherwise.
-
EXTERNAL GENERIC-FUNCTION CONTIGUOUS?
- IP-LIKE-1
- IP-LIKE-2
Returns T if IP-LIKE-1 and IP-LIKE-2 are contiguous, NIL otherwise.
-
EXTERNAL GENERIC-FUNCTION DISJOINT?
- IP-LIKE-1
- IP-LIKE-2
Return T if IP-LIKE-1 and IP-LIKE-2 are disjoint, i.e., they share no IP addresses in common.
-
EXTERNAL GENERIC-FUNCTION FIRST-IP
- IP-PAIR
Returns the first IP-ADDRESS of an IP-NETWORK or IP-RANGE.
-
EXTERNAL GENERIC-FUNCTION (SETF FIRST-IP)
- NEW-VALUE
- OBJECT
No documentation provided. -
EXTERNAL GENERIC-FUNCTION INT
- IP-ADDRESS
Returns the integer representation of IP-ADDRESS.
-
EXTERNAL GENERIC-FUNCTION IP-EQUAL
- IP+1
- IP+2
Returns T if IP+1 and IP+2 represent the same underlying IP address(es), are the same version of IP, and are instances of the same class (one of IP-ADDRESS, IP-PAIR, or IP-SET), or otherwise NIL.
-
EXTERNAL GENERIC-FUNCTION IP-EQUALP
- IP+1
- IP+2
Returns T if IP+1 and IP+2 represent the same underlying IP address(es), and are the same version of IP, or otherwise NIL. IP-RANGEs or IP-NETWORKs that contain a single IP will be IP-EQUALP to the IP-ADDRESS. See Equality in the README for details.
-
EXTERNAL GENERIC-FUNCTION LAST-IP
- IP-PAIR
Returns the last IP-ADDRESS of an IP-NETWORK or IP-RANGE.
-
EXTERNAL GENERIC-FUNCTION (SETF LAST-IP)
- NEW-VALUE
- OBJECT
No documentation provided. -
EXTERNAL GENERIC-FUNCTION MAKE-IP-ADDRESS
- STR-OR-INT
- &KEY
- VERSION
- &ALLOW-OTHER-KEYS
Make an IP-ADDRESS object from a STRING or INTEGER representation.
-
EXTERNAL GENERIC-FUNCTION SIZE
- IP+
Returns an INTEGER of the number of IP addresses contained in IP+.
-
EXTERNAL GENERIC-FUNCTION STR
- IP-ADDRESS
Returns the string representation of IP-ADDRESS.
-
EXTERNAL GENERIC-FUNCTION VERSION
- OBJECT
No documentation provided. -
EXTERNAL MACRO DISABLE-IP-SYNTAX
Restores the readtable that was current before the matching ENABLE-IP-SYNTAX.
-
EXTERNAL MACRO ENABLE-IP-SYNTAX
Enables the #i reader macro for writing IP-LIKEs directly: #i192.168.0.0, #i10.0.0.0/8 and #i::-::ff each read as a single IP-LIKE, and a parenthesized list like #i(192.168.0.0 10.0.0.0/8 ::-::ff) reads as a list of IP-LIKEs. An element may also be a string, or ,FORM to use the string FORM evaluates to at run time, e.g. #i,prefix or #i(10.0.0.0/8 ,(format nil "~a/24" prefix)). The current readtable is copied and #i added to it, so other reader extensions already enabled are kept; DISABLE-IP-SYNTAX restores the previous readtable. Within a file being compiled or loaded the change is local to that file. To use the syntax without changing *READTABLE*, bind it to *IP-SYNTAX-READTABLE* or install IP-READER into a readtable of your own.
-