1 .\" This page is in the public domain.
2 .\" Almost all details are from RFC 2553.
4 .\" 2004-12-14, mtk, Added EAI_OVERFLOW error
5 .\" 2004-12-14 Fixed description of error return
7 .TH GETNAMEINFO 3 2009-12-03 "GNU" "Linux Programmer's Manual"
9 getnameinfo \- address-to-name translation in protocol-independent manner
12 .B #include <sys/socket.h>
15 .BI "int getnameinfo(const struct sockaddr *" "sa" ", socklen_t " "salen" ,
16 .BI " char *" "host" ", size_t " "hostlen" ,
17 .BI " char *" "serv" ", size_t " "servlen" ", int " "flags" );
21 Feature Test Macro Requirements for glibc (see
22 .BR feature_test_macros (7)):
27 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _POSIX_SOURCE
32 function is the inverse of
34 it converts a socket address to a corresponding host and service,
35 in a protocol-independent manner.
36 It combines the functionality of
39 .BR getservbyport (3),
40 but unlike those functions,
42 is reentrant and allows programs to eliminate
43 IPv4-versus-IPv6 dependencies.
47 argument is a pointer to a generic socket address structure
54 that holds the input IP address and port number.
59 are pointers to caller-allocated buffers (of size
63 respectively) into which
65 places null-terminated strings containing the host and
66 service names respectively.
68 The caller can specify that no hostname (or no service name)
69 is required by providing a NULL
78 However, at least one of hostname or service name
83 argument modifies the behavior of
88 If set, then an error is returned if the hostname cannot be determined.
91 If set, then the service is datagram (UDP) based rather than
93 This is required for the few ports (512-514)
94 that have different services for UDP and TCP.
97 If set, return only the hostname part of the fully qualified domain name
101 If set, then the numeric form of the hostname is returned.
102 .\" For example, by calling
105 .\" .BR gethostbyaddr ().
106 (When not set, this will still happen in case the node's name
107 cannot be determined.)
108 .\" POSIX.1-2003 has NI_NUMERICSCOPE, but glibc doesn't have it.
111 If set, then the numeric form of the service address is returned.
112 (When not set, this will still happen in case the service's name
113 cannot be determined.)
114 .SS "Extensions to getaddrinfo() for Internationalized Domain Names"
116 Starting with glibc 2.3.4,
118 has been extended to selectively allow
119 hostnames to be transparently converted to and from the
120 Internationalized Domain Name (IDN) format (see RFC 3490,
121 .IR "Internationalizing Domain Names in Applications (IDNA)" ).
122 Three new flags are defined:
125 If this flag is used, then the name found in the lookup process is
126 converted from IDN format to the locale's encoding if necessary.
127 ASCII-only names are not affected by the conversion, which
128 makes this flag usable in existing programs and environments.
130 .BR NI_IDN_ALLOW_UNASSIGNED ", " NI_IDN_USE_STD3_ASCII_RULES
131 Setting these flags will enable the
132 IDNA_ALLOW_UNASSIGNED (allow unassigned Unicode code points) and
133 IDNA_USE_STD3_ASCII_RULES (check output to make sure it is a STD3
135 flags respectively to be used in the IDNA handling.
137 .\" FIXME glibc defines the following additional errors, some which
138 .\" can probably be returned by getnameinfo(); they need to
141 .\" #define EAI_INPROGRESS -100 /* Processing request in progress. */
142 .\" #define EAI_CANCELED -101 /* Request canceled. */
143 .\" #define EAI_NOTCANCELED -102 /* Request not canceled. */
144 .\" #define EAI_ALLDONE -103 /* All requests done. */
145 .\" #define EAI_INTR -104 /* Interrupted by a signal. */
146 .\" #define EAI_IDN_ENCODE -105 /* IDN encoding failed. */
148 On success 0 is returned, and node and service names, if requested,
149 are filled with null-terminated strings, possibly truncated to fit
150 the specified buffer lengths.
151 On error one of the following nonzero error codes is returned:
154 The name could not be resolved at this time.
160 argument has an invalid value.
163 A nonrecoverable error occurred.
166 The address family was not recognized,
167 or the address length was invalid for the specified family.
173 The name does not resolve for the supplied arguments.
175 is set and the host's name cannot be located,
176 or neither hostname nor service name were requested.
179 The buffer pointed to by
186 A system error occurred.
187 The error code can be found in
192 function translates these error codes to a human readable string,
193 suitable for error reporting.
202 is provided in glibc since version 2.1.
204 RFC\ 2553, POSIX.1-2001.
206 In order to assist the programmer in choosing reasonable sizes
207 for the supplied buffers,
209 defines the constants
213 #define NI_MAXHOST 1025
214 #define NI_MAXSERV 32
219 these definitions are exposed only if one of the feature test macros
226 The former is the constant
228 in recent versions of BIND's
231 The latter is a guess based on the services listed
232 in the current Assigned Numbers RFC.
234 The following code tries to get the numeric hostname and service name,
235 for a given socket address.
236 Note that there is no hardcoded reference to
237 a particular address family.
241 struct sockaddr *sa; /* input */
242 socklen_t len; /* input */
243 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
245 if (getnameinfo(sa, len, hbuf, sizeof(hbuf), sbuf,
246 sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0)
247 printf("host=%s, serv=%s\en", hbuf, sbuf);
251 The following version checks if the socket address has a
252 reverse address mapping.
256 struct sockaddr *sa; /* input */
257 socklen_t len; /* input */
258 char hbuf[NI_MAXHOST];
260 if (getnameinfo(sa, len, hbuf, sizeof(hbuf),
261 NULL, 0, NI_NAMEREQD))
262 printf("could not resolve hostname");
264 printf("host=%s\en", hbuf);
268 An example program using
279 .BR gethostbyaddr (3),
280 .BR getservbyname (3),
281 .BR getservbyport (3),
288 R. Gilligan, S. Thomson, J. Bound and W. Stevens,
289 .IR "Basic Socket Interface Extensions for IPv6" ,
290 RFC\ 2553, March 1999.
292 Tatsuya Jinmei and Atsushi Onoe,
293 .IR "An Extension of Format for IPv6 Scoped Addresses" ,
294 internet draft, work in progress.
295 ftp://ftp.ietf.org/internet\-drafts/draft\-ietf\-ipngwg\-scopedaddr\-format\-02.txt
298 .IR "Protocol Independence Using the Sockets API" ,
299 Proceedings of the freenix track:
300 2000 USENIX annual technical conference, June 2000.
302 http://www.usenix.org/publications/library/proceedings/usenix2000/freenix/metzprotocol.html