function is the inverse of
it converts a socket address to a corresponding host and service,
in a protocol-independent manner.
It combines the functionality of
but unlike those functions,
is reentrant and allows programs to eliminate
argument is a pointer to a generic socket address structure
that holds the input IP address and port number.
are pointers to caller-allocated buffers (of size
respectively) into which
places null-terminated strings containing the host and
service names respectively.
The caller can specify that no hostname (or no service name)
is required by providing a NULL
argument or a zero
However, at least one of hostname or service name
must be requested.
argument modifies the behavior of
If set, then an error is returned if the hostname cannot be determined.
If set, then the service is datagram (UDP) based rather than
stream (TCP) based.
This is required for the few ports (512-514)
that have different services for UDP and TCP.
If set, return only the hostname part of the fully qualified domain name
for local hosts.
If set, then the numeric form of the hostname is returned.
(When not set, this will still happen in case the node's name
cannot be determined.)
If set, then the numeric form of the service address is returned.
(When not set, this will still happen in case the service's name
cannot be determined.)
Extensions to getaddrinfo() for Internationalized Domain Names
Starting with glibc 2.3.4,
has been extended to selectively allow
hostnames to be transparently converted to and from the
Internationalized Domain Name (IDN) format (see RFC 3490,
Internationalizing Domain Names in Applications (IDNA)).
Three new flags are defined:
If this flag is used, then the name found in the lookup process is
converted from IDN format to the locale's encoding if necessary.
ASCII-only names are not affected by the conversion, which
makes this flag usable in existing programs and environments.
Setting these flags will enable the
IDNA_ALLOW_UNASSIGNED (allow unassigned Unicode code points) and
IDNA_USE_STD3_ASCII_RULES (check output to make sure it is a STD3
flags respectively to be used in the IDNA handling.
On success 0 is returned, and node and service names, if requested,
are filled with null-terminated strings, possibly truncated to fit
the specified buffer lengths.
On error one of the following nonzero error codes is returned:
The name could not be resolved at this time.
Try again later.
argument has an invalid value.
A nonrecoverable error occurred.
The address family was not recognized,
or the address length was invalid for the specified family.
Out of memory.
The name does not resolve for the supplied arguments.
is set and the host's name cannot be located,
or neither hostname nor service name were requested.
The buffer pointed to by
was too small.
A system error occurred.
The error code can be found in
function translates these error codes to a human readable string,
suitable for error reporting.
is provided in glibc since version 2.1.
RFC 2553, POSIX.1-2001.
In order to assist the programmer in choosing reasonable sizes
for the supplied buffers,
defines the constants
#define NI_MAXHOST 1025
#define NI_MAXSERV 32
Since glibc 2.8,
these definitions are exposed only if one of the feature test macros
The former is the constant
in recent versions of BIND's
The latter is a guess based on the services listed
in the current Assigned Numbers RFC.
The following code tries to get the numeric hostname and service name,
for a given socket address.
Note that there is no hardcoded reference to
a particular address family.