4.83. kink/socket/IP_ADDRESS¶
Provides ip addresses and hostname resolution of both IPv4 and IPv6.
4.83.1. type ip_address¶
`ip_address` represents an IPv4 or IPv6 address in the binary form.
4.83.1.1. A.bin¶
`bin` returns the ip address as a bin.
When `A` represents an IPv4 address, `bin` returns a bin of size 4.
When `A` represents an IPv6 address, `bin` returns a bin of size 16.
4.83.1.2. A.ipv4?¶
`ipv4?` returns if and only if `A` is an IPv4 address.
4.83.1.3. A.ipv6?¶
`ipv6?` returns if and only if `A` is an IPv6 address.
4.83.1.4. A.text¶
`text` returns a text representation of the ip address `A` as a str.
When `A` is an IPv4 address, `text` returns a str of the dotted decimal format.
When `A` is an IPv6 address, `text` returns a str of the format defined in RFC 2732, with small case letters, with no leading zeros omission, with no zero hexet elision.
Example
:IP_ADDRESS.require_from('kink/socket/')
stdout.print_line(IP_ADDRESS.for_hostname('127.0.0.1').text)
# => 127.0.0.1
stdout.print_line(IP_ADDRESS.for_hostname('ff02::1').text)
# => ff02:0000:0000:0000:0000:0000:0000:0001
4.83.1.5. A.repr¶
`repr` returns a str representation of `A` as debugging purpose, such as "(ip_address 127.0.0.1)" or "(ip_address ff02:0000:0000:0000:0000:0000:0000:0001)".
4.83.1.6. X == Y¶
Two `ip_address` values are equal when the address bins are equal.
4.83.2. IP_ADDRESS.new(Bin)¶
`new` makes an `ip_address` val of the IPv4 or IPv6 address represented by a bin `Bin`.
When the size of `Bin` is 4, the created `ip_address` is an IPv4 address. When the size of `Bin` is 16, the created `ip_address` is anIPv6 address.
Preconditions
• `Bin` must be a bin of size 4 or 16,
4.83.3. IP_ADDRESS.is?(Val)¶
`is?` returns whether `Val` is an `ip_address` val'.
4.83.4. IP_ADDRESS.for_hostname(Hostname ...[$config={}])¶
`for_hostname` resolves the `Hostname` to an ip address.
Name resolution
`Hostname` is resolved in the following ways.
• IPv4 address in dotted decimal format. Ex. 127.0.0.1
• IP6 address in the Text Representation specified in RFC 2373. Ex. ff06::c3
• IP6 address in the IPv6 literal address format specified in RFC 3986. Ex. [ff06::c3]
• Host name. Ex. localhost, www.example.org
A single host name can be resolved to multipe ip addresses. For example, when the DNS server returns multiple A or AAAA records. In that case, `for_hostname` uses one of those ip addresses as the result.
Preconditions
• `Hostname` must be a str.
• $config must be a fun which takes a config val.
Config val
A config val has following methods.
• C.on_success($success): Uses $success as the success cont. $success must be a fun which takes an `ip_address` as the only parameter.
• C.on_error($error): Uses $success as the error cont. $error must be a fun which takes a str of the error message as the only parameter.
Success result
When `Hostname` is resolved to an ip address, `for_hostname` tail-calls the success cont with an `ip_address` val which represents the ip address.
When `C.on_success` is not called, VAL$identity is used as the default success cont. It means that, if `Hostname` is resolved to an ip address, `for_hostname` returns the `ip_address` val as the result.
Error result
When `Hostname` is not resolved to an ip address, `for_hostname` tail-calls the error cont with an error message as a str.
When `C.on_error` is not called, a fun which raises an exception is used as the default error cont. It means that, if `Hostname` is not resolved to an ip address, `for_hostname` raises an exception.
Examples
:IP_ADDRESS.require_from('kink/socket/')
# IPv4 address
stdout.print_line(IP_ADDRESS.for_hostname('127.0.0.1').repr)
# => (ip_address 127.0.0.1)
# IPv6 address
stdout.print_line(IP_ADDRESS.for_hostname('ff06::c3').repr)
# => (ip_address ff06:0000:0000:0000:0000:0000:0000:00c3)
# IPv6 literal address format
stdout.print_line(IP_ADDRESS.for_hostname('[ff06::c3]').repr)
# => (ip_address ff06:0000:0000:0000:0000:0000:0000:00c3)
# Host name
stdout.print_line(IP_ADDRESS.for_hostname('www.example.com').repr)
# => (ip_address 93.184.215.14)
4.83.5. IP_ADDRESS.all_for_hostname(Hostname)¶
`all_for_hostname` returns a vec of `ip_address` vals corresponding to the `Hostname`, with the same name resolution mechanism with `for_hostname`.
Result
If `Hostname` is resolved to no ip address, `all_for_hostname` returns an empty vec.
If `Hostname` is resolved to a single ip address, `all_for_hostname` returns a vec of size 1 which contains the `ip_address` val.
If `Hostname` is resolved to multiple ip addresses, `all_for_hostname` returns a vec of multiple `ip_address` vals.
Preconditions
• `Hostname` must be a str.
Examples
:IP_ADDRESS.require_from('kink/socket/')
stdout.print_line(IP_ADDRESS.all_for_hostname('127.0.0.1').repr)
# => [(ip_address 127.0.0.1)]
stdout.print_line(IP_ADDRESS.all_for_hostname('www.example.com').repr)
# => [(ip_address 93.184.215.14) (ip_address 2606:2800:021f:cb07:6820:80da:af6b:8b2c)]
4.83.6. IP_ADDRESS.any¶
`any` returns in6addr_any address, which is the IPv6 address `::`.
This address can be used to accept connections, or receive datagram packets, from any IPv4 or IPv6 hosts.
4.83.7. IP_ADDRESS.any_ipv4¶
`any` returns INADDR_ANY address, which is the IPv4 address `0.0.0.0`.
This address can be used to accept connections, or receive datagram packets, from any IPv4 hosts.