3IPv4
4IPv6
9.0
top
← prev up next →

IP AddressesπŸ”— i

Bogdan Popa <bogdan@defn.io>

(require net/ip ) package: net-ip-lib

1IntroductionπŸ”— i

This package provides utilities for working with IP addresses and networks in Racket.

2Generic IP Addresses and NetworksπŸ”— i

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 ipversion)ip-address?

version:(or/c 46)
Parse an IP address.

IPv4 examples:
> (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 1274)

(make-ip-address "0.0.0.127")

IPv6 examples:
> (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 cidr)network?

cidr:string?
Parse a network.

IPv4 examples:
> (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")

IPv6 examples:
> (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 "0:0:0:0:0:0:0:1/128")

2.1IP Address OperationsπŸ”— i

procedure

( ip-address? addr)boolean?

addr:any/c
Return #t when addr is an IP address.

procedure

( ip-address=? addrother)boolean?

other:ip-address?

procedure

( ip-address<? addrother)boolean?

other:ip-address?

procedure

( ip-address<=? addrother)boolean?

other:ip-address?

procedure

( ip-address>? addrother)boolean?

other:ip-address?

procedure

( ip-address>=? addrother)boolean?

other:ip-address?
Compare addr and other. These functions raise exn:fail:contract? when the two addresses are of different versions.

Count the number of set high bits in addr.

Examples:
> (require net/ip)

0

> (ip-address-bitcount (make-ip-address "255.255.255.0"))

24

> (ip-address-bitcount (make-ip-address "255.255.255.255"))

32

16

Return an ip-address? smaller than addr by n. Raises exn:fail:contract? if the resulting address would be smaller than 0.

Return an ip-address? greater than addr by n. Raises exn:fail:contract? if the resulting address would be greater than the maximum address for that particular version.

procedure

( ip-address-size addr)(or/c 32128)

Return 32 or 128 depending on the version of addr. This number represents the size in bits of each type of address.

procedure

( ip-address-version addr)(or/c 46)

Return 4 or 6 depending on the version of addr.

procedure

( ip-address->bytes addr)bytes?

Pack addr into an array of bytes in network order (big endian).

Convert addr to a number.

procedure

( ip-address->string addr)string?

Convert addr to a string.

2.2Network OperationsπŸ”— i

procedure

( network? net)boolean?

net:any/c
Return #t when net is a network.

procedure

( network addrprefix)network?

Construct a network from addr and prefix. Raises exn:fail:contract? if prefix is too large for addr.

procedure

( network-address net)ip-address?

net:network?
Return the network address for net.

procedure

( network-last-address net)ip-address?

net:network?
Return the last ip address in net.

Return the prefix for net.

procedure

( network-hostmask net)ip-address?

net:network?
Return the host mask for net.

procedure

( network-netmask net)ip-address?

net:network?
Return the net mask for net.

Return the broadcast address for net.

Added in version 0.2 of package net-ip-lib.

Return the total number of ip addresses in net.

procedure

( network-member netaddr)boolean?

net:network?
Return #t if addr is a member of net.

procedure

( network-version net)(or/c 46)

net:network?
Return 4 or 6 depending on the version of net.

procedure

( network->string net)string?

net:network?
Convert net to a string.

procedure

( network->stream net)(stream/c ip-address? )

net:network?
Convert net to a stream of all the ip addresses contained inside it.

3IPv4πŸ”— i

These functions are specific to IPv4 addresses.

3.1IPv4 AddressesπŸ”— i

procedure

( ipv4-address? addr)boolean?

addr:any/c
Return #t when addr is an IPv4 address.

Unpack a byte array in network order (big endian) into an IPv4 address. Must be exactly four bytes long,otherwise an exn:fail:contract? is raised.

Return the IPv4 address represented by value. value must be between 0 and 2^32-1, otherwise an exn:fail:contract? is raised.

Parse an IPv4 address. Includes support for compressed notation, similar to what is available on most UNIX systems.

Examples:
> (require net/ip)
> (string->ipv4-address "127.0.0.1")

(make-ip-address "127.0.0.1")

(make-ip-address "127.0.0.1")

> (string->ipv4-address "192.168.1")

(make-ip-address "192.168.0.1")

4IPv6πŸ”— i

These functions are specific to IPv6 addresses.

4.1IPv6 AddressesπŸ”— i

procedure

( ipv6-address? addr)boolean?

addr:any/c
Return #t when addr is an IPv6 address.

Unpack a byte array in network order (big endian) into an IPv6 address. Must be exactly sixteen bytes long, otherwise an exn:fail:contract? is raised.

Return the IPv6 address represented by value. value must be between 0 and 2^128-1, otherwise an exn:fail:contract? is raised.

Parse an IPv6 address.

Examples:
> (require net/ip)

(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")

top
← prev up next →

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /