7.2.11.1 Network Address Conversion

This section describes procedures which convert internet addresses between numeric and string formats.

IPv4 Address Conversion

An IPv4 Internet address is a 4-byte value, represented in Guile as an integer in host byte order, so that say “0.0.0.1” is 1, or “1.0.0.0” is 16777216.

Some underlying C functions use network byte order for addresses, Guile converts as necessary so that at the Scheme level its host byte order everywhere.

Variable: INADDR_ANY

For a server, this can be used with bind (see Network Sockets and Communication) to allow connections from any interface on the machine.

Variable: INADDR_BROADCAST

The broadcast address on the local network.

Variable: INADDR_LOOPBACK

The address of the local host using the loopback device, ie. ‘127.0.0.1’.

Scheme Procedure: inet-netof address
C Function: scm_inet_netof (address)

Return the network number part of the given IPv4 Internet address. E.g.,

(inet-netof 2130706433) ⇒ 127
Scheme Procedure: inet-lnaof address
C Function: scm_lnaof (address)

Return the local-address-with-network part of the given IPv4 Internet address, using the obsolete class A/B/C system. E.g.,

(inet-lnaof 2130706433) ⇒ 1
Scheme Procedure: inet-makeaddr net lna
C Function: scm_inet_makeaddr (net, lna)

Make an IPv4 Internet address by combining the network number net with the local-address-within-network number lna. E.g.,

(inet-makeaddr 127 1) ⇒ 2130706433

IPv6 Address Conversion

An IPv6 Internet address is a 16-byte value, represented in Guile as an integer in host byte order, so that say “::1” is 1. The following constants are defined for convenience.

Variable: IN6ADDR_ANY

For a server, this can be used with bind (see Network Sockets and Communication) to allow connections from any IPv6 interface on the machine.

Variable: IN6ADDR_LOOPBACK

The address of the local host using the loopback device, ie. ‘::1’.

The procedures below convert an IPv6 or an IPv4 address to and from its textual representation.

Scheme Procedure: inet-ntop family address
C Function: scm_inet_ntop (family, address)

Convert a network address from an integer to a printable string. family can be AF_INET or AF_INET6. E.g.,

(inet-ntop AF_INET 2130706433) ⇒ "127.0.0.1"
(inet-ntop AF_INET6 (- (expt 2 128) 1))
  ⇒ "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
Scheme Procedure: inet-pton family address
C Function: scm_inet_pton (family, address)

Convert a string containing a printable network address to an integer address. family can be AF_INET or AF_INET6. E.g.,

(inet-pton AF_INET "127.0.0.1") ⇒ 2130706433
(inet-pton AF_INET6 "::1") ⇒ 1