inet_ntop

Converts an IPv4 or IPv6 address in its binary format into a text string.

Library:LibC
Standard:BSD
Service:Networking

Syntax

  #include <arpa/inet.h> 
   
  const char *inet_ntop (
     int           af,
     const void   *src,
     char         *dst,
     size_t        size); 
  

Parameters

af

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

src

(IN) Points to the address, in network byte order, of the type specified in the af parameter. If af is set to AF_INET, src points to an in_addr structure; if set to AF_INET6, src points to an in6_addr structure.

dst

(OUT) Points to the character buffer where inet_ntop can copy the converted address in string format. It should be non-NULL. For AF_INET, the string is standard dotted decimal format. For AF_INET6, the string is the most appropriate format for the address.

size

(IN) Specifies the length of the character buffer dst. For AF_INTET, it must be at least INET_ADDRSTRLEN, and for AF_INET6, it must be at least INET6_ADDSTRLEN.

Return Values

If successful, returns the pointer to the converted address (same as dst). Otherwise, returns a NULL pointer and sets errno to one of the following:

Decimal

Constant

Description

12

ENOSPC

The buffer pointed to by the dst parameter is not large enough to contain the converted address.

47

EAFNOSUPPORT

The address family parameter is invalid or unsupported.

Remarks

The dst argument points to a buffer, owned by the application, where the function stores the resulting text string. This buffer must be at least

  • 46 octets for IPv6 addresses

  • 16 bytes for IPv4 addresses

Applications can use the following macros to size the buffer:

  	#define INET_ADDRSTRLEN 16
  	#define INET6_ADDRSTRLEN 46
  

If successful, the return value is a pointer to the buffer containing the text string, which is the same as dst.