inet_pton() — Convert Internet address format from text to binary

Standards

Standards / Extensions C or C++ Dependencies

RFC2553
Single UNIX Specification, Version 3

both z/OS® V1R2

Format

#define _OPEN_SYS_SOCK_IPV6
#include <arpa/inet.h>

int inet_pton(int af, const char *src, void *dst);
SUSV3:
#define _POSIX_C_SOURCE 200112L
#include <arpa/inet.h>

int inet_pton(int af, const char *__restrict__ src, void *__restrict__ dst);

General description

The inet_pton() function converts an Internet address in its standard text format into its numeric binary form. The argument af specifies the family of the address.
Note: AF_INET and AF_INET6 address families are currently supported.

The input argument src is a null terminated string. It points to the string being passed in. The argument dst points to a buffer into which inet_pton() stores the numeric address. The address is returned in network byte order. The caller must ensure that the buffer pointed to by dst is large enough to hold the numeric address.

If the af argument is AF_INET, inet_pton() accepts a string in the standard IPv4 dotted-decimal form:
ddd.ddd.ddd.ddd
where ddd is a 1 to 3 digit decimal number between 0 and 255.
If the af argument is AF_INET6, the src string must be in one of the following standard IPv6 text forms:
  1. The preferred form is x: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 should be at least one numeral 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 is x:x:x:x:x:x:d.d.d.d., where 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).
Notes:
  1. A more extensive description of the IPv6 standard representations can be found in RFC2373.
  2. The inet_pton() function has a dependency on the level of the Enhanced ASCII Extensions. See Enhanced ASCII support for details.

Returned value

If successful, inet_pton() returns 1 and stores the binary form of the Internet address in the buffer pointed to by dst.

If unsuccessful because the input buffer pointed to by src is not a valid string, inet_pton() returns 0.

If unsuccessful because the af argument is unknown, inet_pton() returns -1 and sets errno to one of the following values:
Error Code
Description
EAFNOSUPPORT
The address family specified in af is unsupported.

Related information