1GETNAMEINFO(3)             Linux Programmer's Manual            GETNAMEINFO(3)
2
3
4

NAME

6       getnameinfo  - address-to-name translation in protocol-independent man‐
7       ner
8

SYNOPSIS

10       #include <sys/socket.h>
11       #include <netdb.h>
12
13       int getnameinfo(const struct sockaddr *restrict addr, socklen_t addrlen,
14                       char *restrict host, socklen_t hostlen,
15                       char *restrict serv, socklen_t servlen, int flags);
16
17   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
18
19       getnameinfo():
20           Since glibc 2.22:
21               _POSIX_C_SOURCE >= 200112L
22           Glibc 2.21 and earlier:
23               _POSIX_C_SOURCE
24

DESCRIPTION

26       The getnameinfo() function is the inverse of  getaddrinfo(3):  it  con‐
27       verts a socket address to a corresponding host and service, in a proto‐
28       col-independent manner.  It  combines  the  functionality  of  gethost‐
29       byaddr(3)  and  getservbyport(3),  but unlike those functions, getname‐
30       info() is reentrant and allows programs to  eliminate  IPv4-versus-IPv6
31       dependencies.
32
33       The  addr  argument  is a pointer to a generic socket address structure
34       (of type sockaddr_in or sockaddr_in6) of size addrlen  that  holds  the
35       input  IP  address  and  port  number.  The arguments host and serv are
36       pointers to caller-allocated buffers (of size hostlen and  servlen  re‐
37       spectively)  into  which  getnameinfo()  places null-terminated strings
38       containing the host and service names respectively.
39
40       The caller can specify that no hostname (or no  service  name)  is  re‐
41       quired  by  providing  a NULL host (or serv) argument or a zero hostlen
42       (or servlen) argument.  However, at least one of  hostname  or  service
43       name must be requested.
44
45       The flags argument modifies the behavior of getnameinfo() as follows:
46
47       NI_NAMEREQD
48              If  set, then an error is returned if the hostname cannot be de‐
49              termined.
50
51       NI_DGRAM
52              If set, then the service is datagram  (UDP)  based  rather  than
53              stream  (TCP)  based.   This  is  required  for  the  few  ports
54              (512–514) that have different services for UDP and TCP.
55
56       NI_NOFQDN
57              If set, return only the hostname part of the fully qualified do‐
58              main name for local hosts.
59
60       NI_NUMERICHOST
61              If  set,  then  the  numeric  form  of the hostname is returned.
62              (When not set, this will still happen in case  the  node's  name
63              cannot be determined.)
64
65       NI_NUMERICSERV
66              If  set,  then  the  numeric  form of the service address is re‐
67              turned.  (When not set, this will still happen in case the  ser‐
68              vice's name cannot be determined.)
69
70   Extensions to getnameinfo() for Internationalized Domain Names
71       Starting  with  glibc  2.3.4, getnameinfo() has been extended to selec‐
72       tively allow hostnames to be transparently converted to  and  from  the
73       Internationalized  Domain Name (IDN) format (see RFC 3490, Internation‐
74       alizing Domain Names in Applications (IDNA)).  Three new flags are  de‐
75       fined:
76
77       NI_IDN If  this flag is used, then the name found in the lookup process
78              is converted from IDN format to the locale's encoding if  neces‐
79              sary.   ASCII-only  names  are  not  affected by the conversion,
80              which makes this flag usable in existing programs  and  environ‐
81              ments.
82
83       NI_IDN_ALLOW_UNASSIGNED, NI_IDN_USE_STD3_ASCII_RULES
84              Setting these flags will enable the IDNA_ALLOW_UNASSIGNED (allow
85              unassigned Unicode code  points)  and  IDNA_USE_STD3_ASCII_RULES
86              (check  output  to  make  sure it is a STD3 conforming hostname)
87              flags respectively to be used in the IDNA handling.
88

RETURN VALUE

90       On success, 0 is returned, and node and service  names,  if  requested,
91       are  filled with null-terminated strings, possibly truncated to fit the
92       specified buffer lengths.  On error, one of the following nonzero error
93       codes is returned:
94
95       EAI_AGAIN
96              The name could not be resolved at this time.  Try again later.
97
98       EAI_BADFLAGS
99              The flags argument has an invalid value.
100
101       EAI_FAIL
102              A nonrecoverable error occurred.
103
104       EAI_FAMILY
105              The address family was not recognized, or the address length was
106              invalid for the specified family.
107
108       EAI_MEMORY
109              Out of memory.
110
111       EAI_NONAME
112              The  name  does  not  resolve  for   the   supplied   arguments.
113              NI_NAMEREQD  is  set  and  the host's name cannot be located, or
114              neither hostname nor service name were requested.
115
116       EAI_OVERFLOW
117              The buffer pointed to by host or serv was too small.
118
119       EAI_SYSTEM
120              A system error occurred.  The error code can be found in errno.
121
122       The gai_strerror(3) function translates these error codes  to  a  human
123       readable string, suitable for error reporting.
124

FILES

126       /etc/hosts
127       /etc/nsswitch.conf
128       /etc/resolv.conf
129

VERSIONS

131       getnameinfo() is provided in glibc since version 2.1.
132

ATTRIBUTES

134       For  an  explanation  of  the  terms  used  in  this  section,  see at‐
135       tributes(7).
136
137       ┌─────────────────────────────────┬───────────────┬────────────────────┐
138Interface                        Attribute     Value              
139       ├─────────────────────────────────┼───────────────┼────────────────────┤
140getnameinfo()                    │ Thread safety │ MT-Safe env locale │
141       └─────────────────────────────────┴───────────────┴────────────────────┘
142

CONFORMING TO

144       POSIX.1-2001, POSIX.1-2008, RFC 2553.
145

NOTES

147       In order to assist the programmer in choosing reasonable sizes for  the
148       supplied buffers, <netdb.h> defines the constants
149
150           #define NI_MAXHOST      1025
151           #define NI_MAXSERV      32
152
153       Since glibc 2.8, these definitions are exposed only if suitable feature
154       test macros are defined, namely:  _GNU_SOURCE,  _DEFAULT_SOURCE  (since
155       glibc   2.19),  or  (in  glibc  versions  up  to  and  including  2.19)
156       _BSD_SOURCE or _SVID_SOURCE.
157
158       The former is the  constant  MAXDNAME  in  recent  versions  of  BIND's
159       <arpa/nameser.h>  header file.  The latter is a guess based on the ser‐
160       vices listed in the current Assigned Numbers RFC.
161
162       Before glibc version 2.2, the hostlen and servlen arguments were  typed
163       as size_t.
164

EXAMPLES

166       The  following code tries to get the numeric hostname and service name,
167       for a given socket address.  Note that there is no hardcoded  reference
168       to a particular address family.
169
170           struct sockaddr *addr;     /* input */
171           socklen_t addrlen;         /* input */
172           char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
173
174           if (getnameinfo(addr, addrlen, hbuf, sizeof(hbuf), sbuf,
175                       sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0)
176               printf("host=%s, serv=%s\n", hbuf, sbuf);
177
178       The  following  version  checks if the socket address has a reverse ad‐
179       dress mapping.
180
181           struct sockaddr *addr;     /* input */
182           socklen_t addrlen;         /* input */
183           char hbuf[NI_MAXHOST];
184
185           if (getnameinfo(addr, addrlen, hbuf, sizeof(hbuf),
186                       NULL, 0, NI_NAMEREQD))
187               printf("could not resolve hostname");
188           else
189               printf("host=%s\n", hbuf);
190
191       An example program using getnameinfo() can be found in getaddrinfo(3).
192

SEE ALSO

194       accept(2),  getpeername(2),  getsockname(2),  recvfrom(2),   socket(2),
195       getaddrinfo(3),  gethostbyaddr(3),  getservbyname(3), getservbyport(3),
196       inet_ntop(3), hosts(5), services(5), hostname(7), named(8)
197
198       R. Gilligan, S. Thomson, J. Bound and W. Stevens, Basic  Socket  Inter‐
199       face Extensions for IPv6, RFC 2553, March 1999.
200
201       Tatsuya Jinmei and Atsushi Onoe, An Extension of Format for IPv6 Scoped
202       Addresses,  internet  draft,  work  in   progress   ⟨ftp://ftp.ietf.org
203       /internet-drafts/draft-ietf-ipngwg-scopedaddr-format-02.txt⟩.
204
205       Craig Metz, Protocol Independence Using the Sockets API, Proceedings of
206       the freenix track: 2000 USENIX annual technical conference, June 2000
207http://www.usenix.org/publications/library/proceedings/usenix2000
208       /freenix/metzprotocol.html⟩.
209

COLOPHON

211       This page is part of release 5.12 of the Linux man-pages project.  A
212       description of the project, information about reporting bugs, and the
213       latest version of this page, can be found at
214       https://www.kernel.org/doc/man-pages/.
215
216
217
218GNU                               2021-03-22                    GETNAMEINFO(3)
Impressum