IP Addresses
(require net/ip) | package: net-ip-lib |
1 Introduction
This package provides utilities for working with IP addresses and networks in Racket.
2 Generic IP Addresses and Networks
All IP addresses and network versions support the operations that follow.
procedure
(make-ip-address ip) → ip-address?
ip : string?
procedure
(make-ip-address ip) → ip-address?
ip : bytes?
procedure
(make-ip-address ip version) → ip-address?
ip : exact-nonnegative-integer? version : (or/c 4 16)
> (require net/ip) > (make-ip-address "127.0.0.1") (make-ip-address "127.0.0.1")
> (make-ip-address #"\177\0\0\1") (make-ip-address "127.0.0.1")
> (make-ip-address 127 4) (make-ip-address "0.0.0.127")
> (require net/ip) > (make-ip-address "::1") (make-ip-address "0:0:0:0:0:0:0:1")
> (make-ip-address #"\377\377\0\0\0\0\0\0\0\0\0\0\0\0\0\0") (make-ip-address "ffff:0:0:0:0:0:0:0")
> (make-ip-address "2001:db8::1") (make-ip-address "2001:db8:0:0:0:0:0:1")
procedure
(make-network ip prefix) → network?
ip : (or/c ip-address? string?) prefix : exact-nonnegative-integer?
procedure
(make-network ip mask) → network?
ip : (or/c ip-address? string?) mask : (or/c ip-address? string?)
procedure
(make-network cidr) → network?
cidr : string?
> (require net/ip) > (make-network "192.168.1.0/24") (make-network "192.168.1.0/24")
> (make-network "192.168.1.0" 24) (make-network "192.168.1.0/24")
> (make-network "192.168.1.0" "255.255.255.0") (make-network "192.168.1.0/24")
> (make-network (make-ip-address "192.168.1.0") 24) (make-network "192.168.1.0/24")
> (require net/ip) > (make-network "::1/128") (make-network "0:0:0:0:0:0:0:1/128")
> (make-network "::1" 128) (make-network "0:0:0:0:0:0:0:1/128")
> (make-network "::1" "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff") (make-network "0:0:0:0:0:0:0:1/128")
> (make-network (make-ip-address "::1") 128) (make-network "0:0:0:0:0:0:0:1/128")
2.1 IP Address Operations
procedure
(ip-address? addr) → boolean?
addr : any/c
procedure
(ip-address=? addr other) → boolean?
addr : ip-address? other : ip-address?
procedure
(ip-address<? addr other) → boolean?
addr : ip-address? other : ip-address?
procedure
(ip-address<=? addr other) → boolean?
addr : ip-address? other : ip-address?
procedure
(ip-address>? addr other) → boolean?
addr : ip-address? other : ip-address?
procedure
(ip-address>=? addr other) → boolean?
addr : ip-address? other : ip-address?
procedure
addr : ip-address?
> (require net/ip) > (ip-address-bitcount (make-ip-address "0.0.0.0")) 0
> (ip-address-bitcount (make-ip-address "255.255.255.0")) 24
> (ip-address-bitcount (make-ip-address "255.255.255.255")) 32
> (ip-address-bitcount (make-ip-address "ffff::")) 16
procedure
(ip-address-dec addr [n]) → ip-address?
addr : ip-address? n : exact-nonnegative-integer? = 1
procedure
(ip-address-inc addr [n]) → ip-address?
addr : ip-address? n : exact-nonnegative-integer? = 1
procedure
(ip-address-size addr) → (or/c 32 128)
addr : ip-address?
procedure
(ip-address-version addr) → (or/c 4 6)
addr : ip-address?
procedure
(ip-address->bytes addr) → bytes?
addr : ip-address?
procedure
addr : ip-address?
procedure
(ip-address->string addr) → string?
addr : ip-address?
2.2 Network Operations
procedure
addr : ip-address? prefix : exact-nonnegative-integer?
procedure
(network-address net) → ip-address?
net : network?
procedure
net : network?
procedure
(network-hostmask net) → ip-address?
net : network?
procedure
(network-netmask net) → ip-address?
net : network?
procedure
net : network?
procedure
(network-member net addr) → boolean?
net : network? addr : ip-address?
procedure
(network-version net) → (or/c 4 6)
net : network?
procedure
(network->string net) → string?
net : network?
procedure
(network->stream net) → (stream/c ip-address?)
net : network?
3 IPv4
These functions are specific to IPv4 addresses.
3.1 IPv4 Addresses
procedure
(ipv4-address? addr) → boolean?
addr : any/c
procedure
(bytes->ipv4-address bs) → ipv4-address?
bs : bytes?
procedure
(number->ipv4-address value) → ipv4-address?
value : exact-nonnegative-integer?
procedure
ip : string?
> (require net/ip) > (string->ipv4-address "127.0.0.1") (make-ip-address "127.0.0.1")
> (string->ipv4-address "127.1") (make-ip-address "127.0.0.1")
> (string->ipv4-address "192.168.1") (make-ip-address "192.168.0.1")
4 IPv6
These functions are specific to IPv6 addresses.
4.1 IPv6 Addresses
procedure
(ipv6-address? addr) → boolean?
addr : any/c
procedure
(bytes->ipv6-address ip) → ipv6-address?
ip : bytes?
procedure
(number->ipv6-address value) → ipv6-address?
value : exact-nonnegative-integer?
procedure
ip : string?
> (require net/ip) > (string->ipv6-address "::1") (make-ip-address "0:0:0:0:0:0:0:1")
> (string->ipv6-address "ff:ab:cd::ff") (make-ip-address "ff:ab:cd:0:0:0:0:ff")
> (string->ipv6-address "201:db:ee::1") (make-ip-address "201:db:ee:0:0:0:0:1")
> (string->ipv6-address "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff") (make-ip-address "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")