Skip to content

Ip Address

Spice
import "std/net/ip-address";

IpAddress struct

An IPv4 or IPv6 address. The bytes are stored in network byte order. An IPv4 address only uses the first four of them, the others stay zero.

Methods

isIpv4

Spice
public inline f<bool> IpAddress.isIpv4()

Checks if this is an IPv4 address

Returns: bool — IPv4 address or not

isIpv6

Spice
public inline f<bool> IpAddress.isIpv6()

Checks if this is an IPv6 address

Returns: bool — IPv6 address or not

getLength

Spice
public inline f<unsigned int> IpAddress.getLength()

Returns the number of bytes, that this address consists of

Returns: unsigned int — 4 for an IPv4 address, 16 for an IPv6 address

getByte

Spice
public f<unsigned byte> IpAddress.getByte(unsigned int index)

Returns the address byte at the given index, with index 0 being the most significant one

Parameters

Name Type Description
index unsigned int Index of the byte, smaller than getLength()

Returns: unsigned byte — Address byte

toInAddr

Spice
public f<unsigned int> IpAddress.toInAddr()

Returns this IPv4 address in the form, that the socket structs carry it (in_addr_t): the four address bytes in network byte order, read as an unsigned int.

Returns: unsigned int — IPv4 address in network byte order

toString

Spice
public f<String> IpAddress.toString()

Formats this address in its textual representation: an IPv4 address in dotted decimal notation, an IPv6 address in the canonical form of RFC 5952 (lowercase hex groups without leading zeros, the longest run of at least two zero groups compressed to "::" and IPv4-mapped addresses with their IPv4 part in dotted decimal notation).

Returns: String — Textual representation of the address

Functions

createIpv4Address

Spice
public f<IpAddress> createIpv4Address(unsigned byte b0, unsigned byte b1, unsigned byte b2, unsigned byte b3)

Creates an IPv4 address from its four bytes, most significant first

Parameters

Name Type Description
b0 unsigned byte First byte, e.g. 127 for 127.0.0.1
b1 unsigned byte Second byte
b2 unsigned byte Third byte
b3 unsigned byte Fourth byte

Returns: IpAddress — IPv4 address

createIpv4AddressFromInAddr

Spice
public f<IpAddress> createIpv4AddressFromInAddr(unsigned int inAddr)

Creates an IPv4 address from the form, that the socket structs carry it in (in_addr_t)

Parameters

Name Type Description
inAddr unsigned int IPv4 address in network byte order

Returns: IpAddress — IPv4 address

createIpv6Address

Spice
public f<IpAddress> createIpv6Address(const unsigned short[8] groups)

Creates an IPv6 address from its eight 16 bit groups, most significant first

Parameters

Name Type Description
groups const unsigned short[8] Address groups

Returns: IpAddress — IPv6 address

parseIpAddress

Spice
public f<Result<IpAddress>> parseIpAddress(const String& text)

Parses an IPv4 address in dotted decimal notation (e.g. "192.168.0.1") or an IPv6 address in one of the textual forms of RFC 4291 (e.g. "2001:db8::1" or "::ffff:192.168.0.1").

Parameters

Name Type Description
text const String& Textual representation of the address

Returns: Result<IpAddress> — Parsed address

parseIpv4Address

Spice
public f<Result<IpAddress>> parseIpv4Address(const String& text)

Parses an IPv4 address in dotted decimal notation, e.g. "192.168.0.1". Only the strict form with four decimal parts is accepted. Shortened forms like "127.1" and parts with leading zeros, that the C function inet_aton reads as octal numbers, are rejected, so that no text is read ambiguously.

Parameters

Name Type Description
text const String& Textual representation of the address

Returns: Result<IpAddress> — Parsed address

parseIpv6Address

Spice
public f<Result<IpAddress>> parseIpv6Address(const String& text)

Parses an IPv6 address in one of the textual forms of RFC 4291: eight hex groups separated by colons, optionally with one run of zero groups compressed to "::" and optionally with the last two groups written as an IPv4 address in dotted decimal notation (e.g. "::ffff:192.168.0.1"). Zone identifiers (e.g. "fe80::1%eth0") are not supported.

Parameters

Name Type Description
text const String& Textual representation of the address

Returns: Result<IpAddress> — Parsed address

Operators

operator==

Spice
public f<bool> operator==(const IpAddress& lhs, const IpAddress& rhs)

Checks if two addresses are equal, i.e. are of the same family and carry the same bytes

Parameters

Name Type Description
lhs const IpAddress& First address
rhs const IpAddress& Second address

Returns: bool — Equal or not

operator!=

Spice
public f<bool> operator!=(const IpAddress& lhs, const IpAddress& rhs)

Checks if two addresses differ

Parameters

Name Type Description
lhs const IpAddress& First address
rhs const IpAddress& Second address

Returns: bool — Not equal or equal