|HP-UX Reference > I
inet6(3N)HP-UX 11i Version 3: February 2007
inet_pton(), inet_ntop() — Internet address manipulation routines for IP Version 4 and later
#include <sys/socket.h> #include <arpa/inet.h> int inet_pton(int af, const char *src, void *dst); const char *inet_ntop(int af, const void *src, char *dst, size_t size);
The functions inet_pton() and inet_ntop() are new with IP Version 6 (IPv6) and work with both IP Version 4 (IPv4) and IPv6 addresses. The letters "p" and "n" stand for presentation and numeric. The presentation format for an address is often an ASCII string and the numeric format is the binary value that goes into a socket address structure.
The ASCII string is either the IPv4 address in dot notation or an IPv6 address in colon notation. An example of an ASCII string is 22.214.171.124 or 1080::2538:400:25:800:200C:417A.
The binary value is the hex representation of the IPv4/IPv6 address. This binary value resides in the in_addr structure for IPv4 address and in the in6_addr structure for IPv6 address. (Refer to the inet(3N) manpage for more details.)
The functions inet_pton() and inet_ntop() are opposite of each other. The function inet_pton() converts an ASCII address string to binary address, and the function inet_ntop() converts a binary address into ASCII address string. Also, these are equivalent to inet_addr() and inet_ntoa(), respectively.
The inet_pton() Function
The 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. Currently, the AF_INET and AF_INET6 address families are supported.
The src argument points to the IPv6/IPv4 address string being passed in.
The dst argument points to a buffer in which the function stores the numeric address. The address is returned in network byte order.
inet_pton() returns 1 if the conversion succeeds, 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 calling application must ensure that the buffer referred to by dst is large enough to hold the numeric address (e.g., 4 bytes for AF_INET or 16 bytes for AF_INET6).
If the af argument is AF_INET, the function accepts a string in the standard IPv4 dotted-decimal form:
where ddd is a one to three digit decimal number between 0 and 255. Note that many implementations of the existing inet_addr() and inet_aton() functions accept nonstandard input: octal numbers, hexadecimal numbers, and fewer than four numbers. inet_pton() does not accept these formats.
If the af argument is AF_INET6, then the function accepts a string in one of the standard IPv6 text forms. (Refer to the IPv6 address notation below for more details).
The inet_ntop() Function
The 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.
The size argument specifies the size of this buffer. The application must specify a non-NULL dst argument. For IPv6 addresses, the buffer must be at least 46-octets. For IPv4 addresses, the buffer must be at least 16-octets. In order to allow applications to easily declare buffers of the proper size to store IPv4 and IPv6 addresses in string form, the following two constants are defined in <netinet/in.h>:
#define INET_ADDRSTRLEN 16 #define INET6_ADDRSTRLEN 46
The inet_ntop() function returns a pointer to the buffer containing the text string if the conversion succeeds, and NULL otherwise. Upon failure, errno is set to EAFNOSUPPORT if the af argument is invalid, or ENOSPC if the size of the result buffer is inadequate.
IPv6 Address Notation
The IPv6 address is 128 bytes long. Example of an IPv6 address is as shown below:
The 128 bits are written as eight 16-bit integers separated by colons. Each integer is represented by four hexadecimal digits.
In the initial stages all the 128 bits will not be used, hence, it is very likely that there will be many zeros. Hence the IP address in such a scenario will look like this:
The above address can be represented in a compact fashion, by replacing a set of consecutive null 16-bit numbers by two colons. The above address can now be re-written as follows:
Expanding the abbreviation is very simple. Align whatever is at the left of the double colon to the left of the address: these are the leading 16-bit words. Then align whatever is at the right of the colons to the right of the address and fill up with zeros.
The double-colon convention can be used only once inside an address.
To support the transition from IPv4, two special IPv6 addresses are supported. They are IPv4-compatible addresses and IPv4-mapped addresses.
IPv4-Compatible Addresses Description and Example
IPv4-compatible addresses can be converted to and from the older IPv4 network address format. They are used when IPv6 systems need to communicate with each other, but are separated by an IPv4 network.
IPv4-compatible addresses are formed by prepending 96 bits of zero, to a valid, 32-bit IPv4 address. For example, the IPv4 address of
can be converted to the IPv6 address by first converting each decimal number separated by dots (".") to a 2-digit hexadecimal value, and concatenate into 4-digit hex values between colons (":") as listed below.
In addition, leading zeros may be omitted for each hex number between the colons. Therefore, this may be written as the following IPv6 address:
We can retain the dot-decimal format and re-write the above address as
The above address is also a valid IPv4-compatible IPv6 address.
IPv4-Mapped Addresses Description and Example
IPv4-mapped addresses indicate systems that do not support IPv6. They are limited to IPv4. An IPv6 host can communicate with an IPv4-only host using the IPv4-mapped IPv6 address.
IPv4-mapped addresses are formed by prepending 80 bits of zero, and 16 bits of one's, to a valid 32 bit IPv4 address. An IPv4 address of
when mapped to IPv6, it becomes:
gethostent(3N), getnetent(3N), inet(3N), hosts(4), networks(4).