inet_pton, inet_ntop - convert IPv4 and IPv6 addresses between binary and text form.
#include <arpa/inet.h> const char *inet_ntop(int af, const void *src, char *dst, socklen_t size); int inet_pton(int af, const char *src, void *dst);
inet_ntop()function converts a numeric address into a text string suitable for presentation. The af argument specifies the family of the address. This can be AF_INET or AF_INET6. The src argument points to a buffer holding an IPv4 address if the af argument is AF_INET, or an IPv6 address if the af argument is AF_INET6. The dst argument points to a buffer where the function will store the resulting text string; it must not be NULL. The size argument specifies the size of this buffer, which must be large enough to hold the text string (INET_ADDRSTRLEN characters for IPv4, INET6_ADDRSTRLEN characters for IPv6).
inet_pton()function converts an address in its standard text presentation form into its numeric binary form. The af argument specifies the family of the address. The AF_INET and AF_INET6 address families are supported. The src argument points to the string being passed in. The dst argument points to a buffer into which the function stores the numeric address; this must be large enough to hold the numeric address (32 bits for AF_INET, 128 bits for AF_INET6).
If the af argument of
inet_pton()is AF_INET, the src string must be in the standard IPv4 dotted-decimal form:
where ddd is a one to three digit decimal number between 0 and 255 (see the
inet_addr()definition). The inet_pton()function does not accept other formats (such as the octal numbers, hexadecimal numbers, and fewer than four numbers that inet_addr()accepts).
If the af argument of
inet_pton()is AF_INET6, the src string must be in one of the following standard IPv6 text forms:
A more extensive description of the standard representations of IPv6 addresses can be found in referenced document RFC 2373.
- The preferred form is x:x:x:x:x:x:x:x, where the 'x's are the hexadecimal values of the eight 16-bit pieces of the address. Leading zeros in individual fields can be omitted, but there must be at least one numeral in every field.
- A string of contiguous zero fields in the preferred form can be shown as "::". The "::" can only appear once in an address. Unspecified addresses (0:0:0:0:0:0:0:0) may be represented simply as "::".
- A third form that is sometimes more convenient when dealing with a mixed environment of IPv4 and IPv6 nodes is x:x:x:x:x:x:d.d.d.d, where the "x"s are the hexadecimal values of the six high-order 16-bit pieces of the address, and the "d"s are the decimal values of the four low-order 8-bit pieces of the address (standard IPv4 representation).
inet_ntop()function returns a pointer to the buffer containing the text string if the conversion succeeds, and NULL otherwise.
inet_pton()function returns 1 if the conversion succeeds, with the address pointed to by dst in network byte order. It returns 0 if the input is not a valid IPv4 dotted-decimal string or a valid IPv6 address string, or -1 with errno set to [EAFNOSUPPORT] if the af argument is unknown.
- the af argument is invalid
- the size of the
inet_ntop()result buffer is inadequate.
First released in Issue 5.1.