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

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>

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

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

General description

The inet_ntop() function converts from an Internet address in binary format, specified by src, to standard text format, and places the result in dst, when size, the space available in dst, is sufficient. The argument af specifies the family of the Internet address. This can be AF_INET or AF_INET6.

The argument src points to a buffer holding an IPv4 Internet address if the af argument is AF_INET, or an IPv6 Internet address if the af argument is AF_INET6. The address must be in network byte order.

The argument dst points to a buffer where the function will store the resulting text string. The size argument specifies the size of this buffer. The application must specify a non-NULL dst argument. For IPv6 addresses, the buffer must be at least 46 bytes. For IPv4 addresses, the buffer must be at least 16 bytes.

In order to allow applications to easily declare buffers of the proper size to store IPv4 and IPv6 addresses in string form, the following two constants are defined in <netinet/in.h>:
#define INET_ADDRSTRLEN 16
#define INET6_ADDRSTRLEN 46
Note: The inet_ntop() function has a dependency on the level of the Enhanced ASCII Extensions. See Enhanced ASCII support for details.

Returned value

If successful, inet_ntop() returns a pointer to the buffer containing the converted address.

If unsuccessful, inet_ntop() returns NULL and sets errno to one of the following values:
Error Code
Description
EAFNOSUPPORT
The address family specified in af is unsupported.
ENOSPC
The destination buffer size is too small.
Note: For Enhanced ASCII usage, the inet_ntop() function has a dependency on the level of the Enhanced ASCII Extensions. See Enhanced ASCII support for details.

Related information