getnameinfo (3) - Linux Manuals
getnameinfo: address-to-name translation in protocol-independent manner
NAME
getnameinfo - address-to-name translation in protocol-independent manner
SYNOPSIS
#include <sys/socket.h> #include <netdb.h> int getnameinfo(const struct sockaddr *addr, socklen_t addrlen, char *host, socklen_t hostlen, char *serv, socklen_t servlen, int flags);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
getnameinfo():
The
addr
argument is a pointer to a generic socket address structure
(of type
sockaddr_in
or
sockaddr_in6)
of size
addrlen
that holds the input IP address and port number.
The arguments
host
and
serv
are pointers to caller-allocated buffers (of size
hostlen
and
servlen
respectively) into which
getnameinfo()
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
host
(or
serv)
argument or a zero
hostlen
(or
servlen)
argument.
However, at least one of hostname or service name
must be requested.
The
flags
argument modifies the behavior of
getnameinfo()
as follows:
The
gai_strerror(3)
function translates these error codes to a human readable string,
suitable for error reporting.
#define NI_MAXHOST 1025
#define NI_MAXSERV 32
Since glibc 2.8,
these definitions are exposed only if suitable
feature test macros are defined, namely:
_GNU_SOURCE,
_DEFAULT_SOURCE
(since glibc 2.19),
or (in glibc versions up to and including 2.19)
_BSD_SOURCE
or
_SVID_SOURCE.
The former is the constant
MAXDNAME
in recent versions of BIND's
<arpa/nameser.h>
header file.
The latter is a guess based on the services listed
in the current Assigned Numbers RFC.
Before glibc version 2.2, the
hostlen
and
servlen
arguments were typed as
size_t.
struct sockaddr *addr; /* input */
socklen_t addrlen; /* input */
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
if (getnameinfo(addr, addrlen, hbuf, sizeof(hbuf), sbuf,
The following version checks if the socket address has a
reverse address mapping.
struct sockaddr *addr; /* input */
socklen_t addrlen; /* input */
char hbuf[NI_MAXHOST];
if (getnameinfo(addr, addrlen, hbuf, sizeof(hbuf),
An example program using
getnameinfo()
can be found in
getaddrinfo(3).
R. Gilligan, S. Thomson, J. Bound and W. Stevens,
Basic Socket Interface Extensions for IPv6,
RFC 2553, March 1999.
Tatsuya Jinmei and Atsushi Onoe,
An Extension of Format for IPv6 Scoped Addresses,
internet draft, work in progress
Craig Metz,
Protocol Independence Using the Sockets API,
Proceedings of the freenix track:
2000 USENIX annual technical conference, June 2000
DESCRIPTION
The
getnameinfo()
function is the inverse of
getaddrinfo(3):
it converts a socket address to a corresponding host and service,
in a protocol-independent manner.
It combines the functionality of
gethostbyaddr(3)
and
getservbyport(3),
but unlike those functions,
getnameinfo()
is reentrant and allows programs to eliminate
IPv4-versus-IPv6 dependencies.
Extensions to getnameinfo() for Internationalized Domain Names
Starting with glibc 2.3.4,
getnameinfo()
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:
RETURN VALUE
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:
FILES
/etc/hosts
/etc/nsswitch.conf
/etc/resolv.conf
VERSIONS
getnameinfo()
is provided in glibc since version 2.1.
ATTRIBUTES
For an explanation of the terms used in this section, see
attributes(7).
Interface Attribute Value
getnameinfo()
Thread safety MT-Safe env locale CONFORMING TO
POSIX.1-2001, POSIX.1-2008, RFC 2553.
NOTES
In order to assist the programmer in choosing reasonable sizes
for the supplied buffers,
<netdb.h>
defines the constants
EXAMPLES
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.
COLOPHON
This page is part of release 5.10 of the Linux
man-pages
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
https://www.kernel.org/doc/man-pages/.
SEE ALSO
accept(2),
getpeername(2),
getsockname(2),
recvfrom(2),
socket(2),
getaddrinfo(3),
gethostbyaddr(3),
getservbyname(3),
getservbyport(3),
inet_ntop(3),
hosts(5),
services(5),
hostname(7),
named(8)