1 .\" %%%LICENSE_START(PUBLIC_DOMAIN)
2 .\" This page is in the public domain.
5 .\" Almost all details are from RFC 2553.
7 .\" 2004-12-14, mtk, Added EAI_OVERFLOW error
8 .\" 2004-12-14 Fixed description of error return
10 .TH GETNAMEINFO 3 2017-09-15 "GNU" "Linux Programmer's Manual"
12 getnameinfo \- address-to-name translation in protocol-independent manner
15 .B #include <sys/socket.h>
18 .BI "int getnameinfo(const struct sockaddr *" "addr" ", socklen_t " "addrlen" ,
19 .BI " char *" "host" ", socklen_t " "hostlen" ,
20 .BI " char *" "serv" ", socklen_t " "servlen" ", int " "flags" );
24 Feature Test Macro Requirements for glibc (see
25 .BR feature_test_macros (7)):
30 Since glibc 2.22: _POSIX_C_SOURCE >= 201112L
31 Glibc 2.21 and earlier: _POSIX_C_SOURCE
36 function is the inverse of
38 it converts a socket address to a corresponding host and service,
39 in a protocol-independent manner.
40 It combines the functionality of
43 .BR getservbyport (3),
44 but unlike those functions,
46 is reentrant and allows programs to eliminate
47 IPv4-versus-IPv6 dependencies.
51 argument is a pointer to a generic socket address structure
58 that holds the input IP address and port number.
63 are pointers to caller-allocated buffers (of size
67 respectively) into which
69 places null-terminated strings containing the host and
70 service names respectively.
72 The caller can specify that no hostname (or no service name)
73 is required by providing a NULL
82 However, at least one of hostname or service name
87 argument modifies the behavior of
92 If set, then an error is returned if the hostname cannot be determined.
95 If set, then the service is datagram (UDP) based rather than
97 This is required for the few ports (512\(en514)
98 that have different services for UDP and TCP.
101 If set, return only the hostname part of the fully qualified domain name
105 If set, then the numeric form of the hostname is returned.
106 .\" For example, by calling
109 .\" .BR gethostbyaddr ().
110 (When not set, this will still happen in case the node's name
111 cannot be determined.)
112 .\" POSIX.1-2003 has NI_NUMERICSCOPE, but glibc doesn't have it.
115 If set, then the numeric form of the service address is returned.
116 (When not set, this will still happen in case the service's name
117 cannot be determined.)
118 .SS Extensions to getnameinfo() for Internationalized Domain Names
120 Starting with glibc 2.3.4,
122 has been extended to selectively allow
123 hostnames to be transparently converted to and from the
124 Internationalized Domain Name (IDN) format (see RFC 3490,
125 .IR "Internationalizing Domain Names in Applications (IDNA)" ).
126 Three new flags are defined:
129 If this flag is used, then the name found in the lookup process is
130 converted from IDN format to the locale's encoding if necessary.
131 ASCII-only names are not affected by the conversion, which
132 makes this flag usable in existing programs and environments.
134 .BR NI_IDN_ALLOW_UNASSIGNED ", " NI_IDN_USE_STD3_ASCII_RULES
135 Setting these flags will enable the
136 IDNA_ALLOW_UNASSIGNED (allow unassigned Unicode code points) and
137 IDNA_USE_STD3_ASCII_RULES (check output to make sure it is a STD3
139 flags respectively to be used in the IDNA handling.
141 .\" FIXME glibc defines the following additional errors, some which
142 .\" can probably be returned by getnameinfo(); they need to
146 .\" #define EAI_INPROGRESS -100 /* Processing request in progress. */
147 .\" #define EAI_CANCELED -101 /* Request canceled. */
148 .\" #define EAI_NOTCANCELED -102 /* Request not canceled. */
149 .\" #define EAI_ALLDONE -103 /* All requests done. */
150 .\" #define EAI_INTR -104 /* Interrupted by a signal. */
151 .\" #define EAI_IDN_ENCODE -105 /* IDN encoding failed. */
153 On success, 0 is returned, and node and service names, if requested,
154 are filled with null-terminated strings, possibly truncated to fit
155 the specified buffer lengths.
156 On error, one of the following nonzero error codes is returned:
159 The name could not be resolved at this time.
165 argument has an invalid value.
168 A nonrecoverable error occurred.
171 The address family was not recognized,
172 or the address length was invalid for the specified family.
178 The name does not resolve for the supplied arguments.
180 is set and the host's name cannot be located,
181 or neither hostname nor service name were requested.
184 The buffer pointed to by
191 A system error occurred.
192 The error code can be found in
197 function translates these error codes to a human readable string,
198 suitable for error reporting.
202 .I /etc/nsswitch.conf
207 is provided in glibc since version 2.1.
209 For an explanation of the terms used in this section, see
215 Interface Attribute Value
218 T} Thread safety MT-Safe env locale
222 POSIX.1-2001, POSIX.1-2008, RFC\ 2553.
224 In order to assist the programmer in choosing reasonable sizes
225 for the supplied buffers,
227 defines the constants
231 #define NI_MAXHOST 1025
232 #define NI_MAXSERV 32
237 these definitions are exposed only if suitable
238 feature test macros are defined, namely:
242 or (in glibc versions up to and including 2.19)
247 The former is the constant
249 in recent versions of BIND's
252 The latter is a guess based on the services listed
253 in the current Assigned Numbers RFC.
255 Before glibc version 2.2, the
259 arguments were typed as
262 The following code tries to get the numeric hostname and service name,
263 for a given socket address.
264 Note that there is no hardcoded reference to
265 a particular address family.
269 struct sockaddr *addr; /* input */
270 socklen_t addrlen; /* input */
271 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
273 if (getnameinfo(addr, addrlen, hbuf, sizeof(hbuf), sbuf,
274 sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0)
275 printf("host=%s, serv=%s\en", hbuf, sbuf);
279 The following version checks if the socket address has a
280 reverse address mapping.
284 struct sockaddr *addr; /* input */
285 socklen_t addrlen; /* input */
286 char hbuf[NI_MAXHOST];
288 if (getnameinfo(addr, addrlen, hbuf, sizeof(hbuf),
289 NULL, 0, NI_NAMEREQD))
290 printf("could not resolve hostname");
292 printf("host=%s\en", hbuf);
296 An example program using
307 .BR gethostbyaddr (3),
308 .BR getservbyname (3),
309 .BR getservbyport (3),
316 R. Gilligan, S. Thomson, J. Bound and W. Stevens,
317 .IR "Basic Socket Interface Extensions for IPv6" ,
318 RFC\ 2553, March 1999.
320 Tatsuya Jinmei and Atsushi Onoe,
321 .IR "An Extension of Format for IPv6 Scoped Addresses" ,
322 internet draft, work in progress
323 .UR ftp://ftp.ietf.org\:/internet\-drafts\:/draft\-ietf\-ipngwg\-scopedaddr\-format\-02.txt
327 .IR "Protocol Independence Using the Sockets API" ,
328 Proceedings of the freenix track:
329 2000 USENIX annual technical conference, June 2000
331 .UR http://www.usenix.org\:/publications\:/library\:/proceedings\:/usenix2000\:/freenix\:/metzprotocol.html