The Open Group Base Specifications Issue 7, 2018 edition
IEEE Std 1003.1-2017 (Revision of IEEE Std 1003.1-2008)
Copyright © 2001-2018 IEEE and The Open Group
A newer edition of this document exists here

NAME

inet_ntop, inet_pton - convert IPv4 and IPv6 addresses between binary and text form

SYNOPSIS

#include <arpa/inet.h>

const char *inet_ntop(int
af, const void *restrict src,
       char *restrict
dst, socklen_t size);
int inet_pton(int
af, const char *restrict src, void *restrict dst);

DESCRIPTION

The inet_ntop() function shall convert a numeric address into a text string suitable for presentation. The af argument shall specify the family of the address. This can be AF_INET [IP6] [Option Start]  or AF_INET6. [Option End] The src argument points to a buffer holding an IPv4 address if the af argument is AF_INET, [IP6] [Option Start]  or an IPv6 address if the af argument is AF_INET6; [Option End] the address must be in network byte order. The dst argument points to a buffer where the function stores the resulting text string; it shall not be NULL. The size argument specifies the size of this buffer, which shall be large enough to hold the text string (INET_ADDRSTRLEN characters for IPv4, [IP6] [Option Start]  INET6_ADDRSTRLEN characters for IPv6). [Option End]

The inet_pton() function shall convert an address in its standard text presentation form into its numeric binary form. The af argument shall specify the family of the address. The AF_INET [IP6] [Option Start]  and AF_INET6 [Option End] address families shall be 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 shall be large enough to hold the numeric address (32 bits for AF_INET, [IP6] [Option Start]  128 bits for AF_INET6). [Option End]

If the af argument of inet_pton() is AF_INET, the src string shall be in the standard IPv4 dotted-decimal form:

ddd.ddd.ddd.ddd

where "ddd" is a one to three digit decimal number between 0 and 255 (see inet_addr). 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).

[IP6] [Option Start] If the af argument of inet_pton() is AF_INET6, the src string shall be in one of the following standard IPv6 text forms:

  1. 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 shall be one to four hexadecimal digits in every field.

  2. 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 "::".

  3. 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).

Note:
A more extensive description of the standard representations of IPv6 addresses can be found in RFC 2373.
[Option End]

RETURN VALUE

The inet_ntop() function shall return a pointer to the buffer containing the text string if the conversion succeeds, and NULL otherwise, and set errno to indicate the error.

The inet_pton() function shall return 1 if the conversion succeeds, with the address pointed to by dst in network byte order. It shall return 0 if the input is not a valid IPv4 dotted-decimal string [IP6] [Option Start]  or a valid IPv6 address string, [Option End] or -1 with errno set to [EAFNOSUPPORT] if the af argument is unknown.

ERRORS

The inet_ntop() and inet_pton() functions shall fail if:

[EAFNOSUPPORT]
The af argument is invalid.
[ENOSPC]
The size of the inet_ntop() result buffer is inadequate.

The following sections are informative.

EXAMPLES

None.

APPLICATION USAGE

None.

RATIONALE

None.

FUTURE DIRECTIONS

None.

SEE ALSO

XBD <arpa/inet.h>

CHANGE HISTORY

First released in Issue 6. Derived from the XNS, Issue 5.2 specification.

IPv6 extensions are marked.

The restrict keyword is added to the inet_ntop() and inet_pton() prototypes for alignment with the ISO/IEC 9899:1999 standard.

IEEE Std 1003.1-2001/Cor 1-2002, item XSH/TC1/D6/29 is applied, adding ``the address must be in network byte order'' to the end of the fourth sentence of the first paragraph in the DESCRIPTION.

Issue 7

POSIX.1-2008, Technical Corrigendum 2, XSH/TC2-2008/0178 [777] is applied.

End of informative text.

 

return to top of page

UNIX ® is a registered Trademark of The Open Group.
POSIX ™ is a Trademark of The IEEE.
Copyright © 2001-2018 IEEE and The Open Group, All Rights Reserved
[ Main Index | XBD | XSH | XCU | XRAT ]