Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

inet_ntop(), inet_pton() - manipulate Internet addresses

&pagelevel(3)&pagelevel

#include <sys.socket.h>
#include <netinet.in.h>
#include <arpa.inet.h>


Kernighan-Ritchie-C:
char *inet_ntop(af, addr, dst, size);

int af;
char *addr;
char *dst;
int size;


int inet_pton(af, addr, dst);

int af;
char *addr;
char *dst;

ANSI-C:
char* inet_ntop(int af, char* addr, char* dst, int size);
int inet_pton(int af, char* addr, char* dst);


Description

The use of the inet_ntop() and inet_pton() functions only makes sense in the AF_INET and AF_INET6 address families.

The inet_ntop() function converts the binary IP address to which the addr parameter is pointing to printable notation. The value passed in the af parameter indicates whether the address involved is an IPv4 address or an IPv6 address:

  • If the value AF_INET is passed in af, a binary IPv4 address is converted to printable decimal dotted notation.

  • If the value AF_INET6 is passed in af, a binary IPv6 address is converted to printable hexadecimal colon notation.

inet_ntop() returns the printable address in the buffer of the length size to which the pointer dst is pointing. You can ensure that the buffer is big enough by using the integer constant INET_ADDRSTRLEN (for IPv4 addresses) or INET6_ADDRSTRLEN (for IPv6 addresses) as the current value for size when you call inet_ntop(). Both constants are defined in <netinet.in.h>.

The inet_pton() function converts an IPv4 address in decimal dotted notation or an IPv6 address in hexadecimal colon notation to a binary address. The value passed in the af parameter indicates whether the address involved is an IPv4 address or an IPv6 address:

  • If the value AF_INET is passed in af, an IPv4 address is converted.

  • If the value AF_INET6 is passed in af, an IPv6 address is converted.

inet_pton() returns the binary address to the buffer to which the pointer dst is pointing. The buffer must be sufficiently large: 4 bytes for AF_INET and 16 bytes for AF_INET6. The conversion of IPv6 addresses in abbreviated notation with two colons (“::”) is not supported.

Note

If the output of inet_pton() is used as the input for a new function, make sure that the starting address of the destination area dst has doubleword alignment.

Return value

If the inet_pton() function is executed successfully, it returns a pointer to the buffer in which the text string is stored. The null pointer is returned if an error occurs.

inet_ntop() returns the following values:

1:

If conversion is successful.

0:

If the input is an invalid address string.

-1:

If a parameter is invalid.

Errors indicated by errno

EAFNOSUPPORT

Illegal operand.

ENOSPC

The result buffer is too small.