class IpAddress
Defined at line 15 of file ../../src/lib/inet/ip_address.h
Represents a V4 or V6 IP address.
Public Members
static IpAddress kInvalid
static IpAddress kV4Loopback
static IpAddress kV6Loopback
Public Methods
bool is_valid ()
Indicates whether this address is valid.
Defined at line 104 of file ../../src/lib/inet/ip_address.h
sa_family_t family ()
Returns the family of this address: |AF_INET| for V4, |AF_INET6| for V6 and
|AF_UNSPEC| for an invalid address.
Defined at line 108 of file ../../src/lib/inet/ip_address.h
bool is_v4 ()
Indicates whether this address is a V4 address.
Defined at line 111 of file ../../src/lib/inet/ip_address.h
bool is_v6 ()
Indicates whether this address is a V6 address.
Defined at line 114 of file ../../src/lib/inet/ip_address.h
const in_addr & as_in_addr ()
Returns this address as an |in_addr|. Only defined for V4 addresses.
Defined at line 133 of file ../../src/lib/inet/ip_address.h
in_addr_t as_in_addr_t ()
Returns this address as an |in_addr_t|. Only defined for V4 addresses.
Defined at line 139 of file ../../src/lib/inet/ip_address.h
const in6_addr & as_in6_addr ()
Returns this address as an |in6_addr|. Only defined for V6 addresses.
Defined at line 145 of file ../../src/lib/inet/ip_address.h
const uint8_t * as_bytes ()
Returns a pointer to the bytes that make up this address. |byte_count|
indicates the byte count. Not defined for invalid addresses.
Defined at line 152 of file ../../src/lib/inet/ip_address.h
const uint16_t * as_v6_words ()
Returns a pointer to the network-order words (big-endian) that make up the
address. |word_count| indicates the byte count. This is only defined for
V6 addresses.
Defined at line 160 of file ../../src/lib/inet/ip_address.h
size_t byte_count ()
Returns the number of bytes that make up this address. A V4 address is
4 bytes, and a V6 address is 16 bytes. Not defined for invalid addresses.
Defined at line 167 of file ../../src/lib/inet/ip_address.h
size_t word_count ()
Returns the number of words that make up this address. A V4 address is
2 words, and a V6 address is 8 words. Not defined for invalid addresses.
Defined at line 174 of file ../../src/lib/inet/ip_address.h
bool operator bool ()
Defined at line 184 of file ../../src/lib/inet/ip_address.h
bool operator== (const IpAddress & other)
Defined at line 186 of file ../../src/lib/inet/ip_address.h
bool operator!= (const IpAddress & other)
Defined at line 191 of file ../../src/lib/inet/ip_address.h
IpAddress FromString (const std::string & address_string, sa_family_t family)
Creates an IpAddress from a string containing a numeric IP address. Returns |kInvalid| if
|address_string| cannot be converted into a valid IP address.
If |family| is |AF_UNSPEC|, this function accepts both IPv4 and IPv6 address strings. If
|family| is |AF_INET| only IPv4 strings are accepted, and if |family| is |AF_INET6|, only
IPv6 strings are accepted.
IPv6 address strings may be non-canonical in the following respects:
1) A pair of colons may be substituted for one or more zeros (rather than two or more).
2) The run of zeros replaced by a pair of colons need not be the longest run of zeros, and,
in the case of a tie, need not be the left-most run of zeros.
3) Zeros may occur adjacent to a pair of colons.
4) Upper-case hexadecimal digits are allowed.
5) Hexadecimal words may have leading zeros.
The following constraints apply to IPv6 address strings:
1) At most one pair of colons may appear in the string.
2) A string that has no colon pairs must contain exactly eight words.
3) Hexadecimal words may have at most 4 digits.
4) Mixed (IPv6 and IPv4) address strings are not supported.
Defined at line 233 of file ../../src/lib/inet/ip_address.cc
std::pair<IpAddress, size_t> FromStringView (std::string_view string_view, sa_family_t family)
Parses an IpAddress prefixing a string view. If the parse succeeds, this method returns the
valid IpAddress and the number of characters that were parsed to produce it. If the parse
fails, this method returns an invalid IpAddress and zero. See |FromString| for details about
how the string is parsed.
Note that this method looks for a valid address string in the initial position in the string
view. This means that inputs such as "::anything" will produce a successful result: in this
case, the IPv6 unspecified address "::" and 2 characters parsed. If the caller wants to
know whether the entire string view constitutes a valid address, it will be necessary to
compare the returned number of parsed characters to the length of the string view, e.g.
auto result = IpAddress::FromStringView(string_view);
if (result.first.is_valid()
&
&
result.second == string_view.size()) {
// Do something with result.first.
}
Defined at line 250 of file ../../src/lib/inet/ip_address.cc
void IpAddress ()
Creates an invalid IP address.
Defined at line 264 of file ../../src/lib/inet/ip_address.cc
void IpAddress (uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3)
Creates an IPV4 address from four address bytes.
Defined at line 266 of file ../../src/lib/inet/ip_address.cc
void IpAddress (in_addr_t addr)
Creates an IPV4 address from an in_addr_t.
Defined at line 274 of file ../../src/lib/inet/ip_address.cc
void IpAddress (const in_addr & addr)
Creates an IPV4 address from an in_addr struct.
Defined at line 276 of file ../../src/lib/inet/ip_address.cc
void IpAddress (uint16_t w0, uint16_t w1, uint16_t w2, uint16_t w3, uint16_t w4, uint16_t w5, uint16_t w6, uint16_t w7)
Creates an IPV6 address from eight address words.
Defined at line 278 of file ../../src/lib/inet/ip_address.cc
void IpAddress (uint16_t w0, uint16_t w7)
Creates an IPV6 address from two address words (first and last).
Defined at line 310 of file ../../src/lib/inet/ip_address.cc
void IpAddress (const std::vector<uint16_t> & source, size_t start)
Creates an IPV6 address from a vector of words. The address starts with |start| words of value
zero followed by the values from |source|, followed by zeros to fill the remainder.
Defined at line 292 of file ../../src/lib/inet/ip_address.cc
void IpAddress (const in6_addr & addr)
Creates an IPV6 address from an in6_addr struct.
Defined at line 317 of file ../../src/lib/inet/ip_address.cc
void IpAddress (const sockaddr & addr)
Creates an address from a sockaddr struct.
Defined at line 319 of file ../../src/lib/inet/ip_address.cc
void IpAddress (const sockaddr_storage & addr)
Creates an address from a sockaddr_storage struct.
Defined at line 336 of file ../../src/lib/inet/ip_address.cc
void IpAddress (const fuchsia::net::Ipv4Address & addr)
Creates an address from a fuchsia.net Ipv4Address struct.
Defined at line 355 of file ../../src/lib/inet/ip_address.cc
void IpAddress (const fuchsia::net::Ipv6Address & addr)
Creates an address from a fuchsia.net Ipv6Address struct.
Defined at line 359 of file ../../src/lib/inet/ip_address.cc
void IpAddress (const fuchsia::net::IpAddress & addr)
Creates an address from a fuchsia.net IpAddress struct.
Defined at line 363 of file ../../src/lib/inet/ip_address.cc
bool is_mapped_from_v4 ()
Indicates whether this address is a V6 address that is mapped from a V4 address.
Defined at line 381 of file ../../src/lib/inet/ip_address.cc
IpAddress mapped_v4_address ()
Returns the V4 address from a V6 address that is mapped from a V4 address. Calling this method
is only permitted if this address returns true from |is_mapped_from_v4|.
Defined at line 383 of file ../../src/lib/inet/ip_address.cc
IpAddress mapped_as_v6 ()
Returns the V6 address that is the mapping of this address, which must be a V4 address.
Defined at line 389 of file ../../src/lib/inet/ip_address.cc
bool is_loopback ()
Indicates whether this address is a loopback address.
Defined at line 397 of file ../../src/lib/inet/ip_address.cc
bool is_link_local ()
Indicates whether this address is link-local (IPv4: in 169.254.0.0/16, IPv6: in fe80::/10).
Defined at line 408 of file ../../src/lib/inet/ip_address.cc
std::string ToString ()
Returns a string representation of this address. V6 addresses are
represented as specified in RFC 5952. For invalid addresses, this method
returns "<invalid>".
Defined at line 425 of file ../../src/lib/inet/ip_address.cc
fuchsia::net::Ipv4Address operator Ipv4Address ()
Defined at line 496 of file ../../src/lib/inet/ip_address.cc
fuchsia::net::Ipv6Address operator Ipv6Address ()
Defined at line 504 of file ../../src/lib/inet/ip_address.cc
fuchsia::net::IpAddress operator IpAddress ()
Defined at line 512 of file ../../src/lib/inet/ip_address.cc