inet_pton()--Convert IPv4 and IPv6 Addresses Between Text and Binary Form


  Syntax
  #include  <sys/socket.h>
  #include  <arpa/inet.h>

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

  Service Program Name: QSOSRV2

  Default Public Authority: *USE

  Threadsafe: Yes

The inet_pton() function converts an address in its standard text presentation form into its numeric binary form.

Parameters

af
(Input) Specifies the family of the address to be converted. Currently the AF_INET and AF_INET6 address families are supported.

src
(Input) The pointer to the null-terminated character string that contains the text presentation form of an IPv4 address if the af parameter is AF_INET, or the text presentation form of an IPv6 address if the af parameter is AF_INET6. See usage notes for the supported formats.

dst
(Output) The pointer to a buffer into which the function stores the numeric address. The calling application must ensure that the buffer referred to by dst is large enough to hold the numeric address (4 bytes for AF_INET or 16 bytes for AF_INET6).

Authorities

No authorization is required.


Return Value

inet_pton() returns an integer. Possible values are:

If successful, the buffer pointed at by dst will be updated with the numeric address.


Error Conditions

When inet_pton() fails with a -1, errno will be set to:

[EAFNOSUPPORT]

The address family is not supported.

[EINVAL]

Parameter is not valid.

[EFAULT]

The system detected an address which was not valid while attempting to access the src or dst parameter.


Usage Notes

  1. If the af parameter of inet_pton() is AF_INET, the src string must 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 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).

  2. If the af parameter of inet_pton is AF_INET6, the src string must be in one of the following 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 must be at least one value 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).


  3. Start of change The above IPv6 text forms may include an appended zone indicator (if preceded by a % character) and/or an appended prefix length (if preceded by a / character). In these cases, the % or / will be treated the same as a null terminator.

  4. A trailing space will be treated the same as a null terminator.

  5. The default coded character set identifier (CCSID) currently in effect for the job End of change will be used to convert the characters found at src (to allow the hexadecimal values to be entered in lower case).


Related Information




API introduced: V5R2
Top | UNIX-Type APIs | APIs by category