inet_pton

Converts an IPv4 or IPv6 network address from string format to its numeric binary format.

Library:LibC
Standard:BSD
Service:Networking

Syntax

  #include <arpa/inet.h> 
   
  int inet_pton (
     long          af,
     const char   *cp,
     void         *ap);
  

Parameters

af

(IN) Specifies the address family. Currently, AF_INET and AF_INET6 are supported.

cp

(IN) Points to the address in standard text presentation. For IPv4, the string is in dotted decimal notation. For IPv6, it is in the string format appropriate for the address.

ap

(OUT) Points to memory where inet_pton can copy the converted address in binary format.

Return Values

If successful, returns 1. If the address is not parsable in the specified address family, returns 0. Otherwise, returns -1 and sets errno to the following:

Decimal

Constant

Description

47

EAFNOSUPPORT

The address family parameter is invalid or unsupported.

Remarks

The binary address is in network byte order.

The calling application must ensure that the buffer referred to by dst is large enough to hold the numeric address, for example:

  • 4 bytes for IPv4 address (struct in_addr)

  • 16 bytes for IPv6 address (struct in6_addr)

If the af argument is AF_INET, the function accepts an address string in the standard IPv4 dotted decimal format where ddd is a 1 to 3 digit decimal number between 0 and 255:

     		ddd.ddd.ddd.ddd
  

The inet_pton function does not accept the other formats (octal numbers, hexadecimal numbers, or fewer than 4 numbers) that inet_addr accepts.

If the af argument is AF_INET6, the function accepts a string in one of the standard IPv6 text forms defined in section 2.2 of RFC 2373: IP Version 6 Addressing Architecture.