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 2007-06-08 "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" );
22 function is defined for protocol-independent address-to-nodename translation.
23 It combines the functionality of
31 argument is a pointer to a generic socket address structure
38 that holds the input IP address and port number.
43 are pointers to buffers (of size
47 respectively) to hold the return values.
49 The caller can specify that no hostname (or no service name)
50 is required by providing a NULL
59 However, at least one of hostname or service name
64 argument modifies the behavior of
69 If set, return only the hostname part of the FQDN for local hosts.
72 If set, then the numeric form of the hostname is returned.
73 .\" For example, by calling
76 .\" .IR gethostbyaddr() .
77 (When not set, this will still happen in case the node's name
81 If set, then a error is returned if the hostname cannot be looked up.
84 If set, then the service address is returned in numeric form,
85 for example by its port number.
88 If set, then the service is datagram (UDP) based rather than
90 This is required for the few ports (512-514)
91 that have different services for UDP and TCP.
92 .SS "Extensions to getaddrinfo() for Internationalized Domain Names"
94 Starting with glibc 2.3.4,
96 has been extended to selectively allow
97 host names to be transparently converted to and from the
98 Internationalized Domain Name (IDN) format (see RFC 3490,
99 .IR "Internationalizing Domain Names in Applications (IDNA)" ).
100 Three new flags are defined:
103 If this flag is used, then the name found in the lookup process is
104 converted from IDN format to the locale's encoding if necessary.
105 ASCII-only names are not affected by the conversion, which
106 makes this flag usable in existing programs and environments.
108 .BR NI_IDN_ALLOW_UNASSIGNED ", " NI_IDN_USE_STD3_ASCII_RULES
109 Setting these flags will enable the
110 IDNA_ALLOW_UNASSIGNED (allow unassigned Unicode code points) and
111 IDNA_USE_STD3_ASCII_RULES (check output to make sure it is a STD3
112 conforming host name)
113 flags respectively to be used in the IDNA handling.
115 .\" FIXME glibc defines the following additional errors, some which
116 .\" can probably be returned by getnameinfo(); they need to
118 .\" # ifdef __USE_GNU
119 .\" # define EAI_INPROGRESS -100 /* Processing request in progress. */
120 .\" # define EAI_CANCELED -101 /* Request canceled. */
121 .\" # define EAI_NOTCANCELED -102 /* Request not canceled. */
122 .\" # define EAI_ALLDONE -103 /* All requests done. */
123 .\" # define EAI_INTR -104 /* Interrupted by a signal. */
124 .\" # define EAI_IDN_ENCODE -105 /* IDN encoding failed. */
126 On success 0 is returned, and node and service names, if requested,
127 are filled with null-terminated strings, possibly truncated to fit
128 the specified buffer lengths.
129 On error one of the following non-zero error codes is returned:
132 The name could not be resolved at this time.
138 parameter has an invalid value.
141 A non-recoverable error occurred.
144 The address family was not recognized,
145 or the address length was invalid for the specified family.
151 The name does not resolve for the supplied parameters.
153 is set and the host's name cannot be located,
154 or neither hostname nor service name were requested.
157 The buffer pointed to by
164 A system error occurred.
165 The error code can be found in
170 function translates these error codes to a human readable string,
171 suitable for error reporting.
179 RFC\ 2553, POSIX.1-2001.
181 In order to assist the programmer in choosing reasonable sizes
182 for the supplied buffers,
184 defines the constants
188 # define NI_MAXHOST 1025
189 # define NI_MAXSERV 32
193 The former is the constant
195 in recent versions of BIND's
198 The latter is a guess based on the services listed
199 in the current Assigned Numbers RFC.
201 The following code tries to get the numeric hostname and service name,
202 for a given socket address.
203 Note that there is no hardcoded reference to
204 a particular address family.
208 struct sockaddr *sa; /* input */
209 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
211 if (getnameinfo(sa, sa\->sa_len, hbuf, sizeof(hbuf), sbuf,
212 sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0)
213 printf("host=%s, serv=%s\en", hbuf, sbuf);
217 The following version checks if the socket address has a
218 reverse address mapping.
222 struct sockaddr *sa; /* input */
223 char hbuf[NI_MAXHOST];
225 if (getnameinfo(sa, sa\->sa_len, hbuf, sizeof(hbuf),
226 NULL, 0, NI_NAMEREQD))
227 printf("could not resolve hostname");
229 printf("host=%s\en", hbuf);
233 An example program using
240 .BR gethostbyaddr (3),
241 .BR getservbyname (3),
242 .BR getservbyport (3),
249 R. Gilligan, S. Thomson, J. Bound and W. Stevens,
250 .IR "Basic Socket Interface Extensions for IPv6" ,
251 RFC\ 2553, March 1999.
253 Tatsuya Jinmei and Atsushi Onoe,
254 .IR "An Extension of Format for IPv6 Scoped Addresses" ,
255 internet draft, work in progress.
256 ftp://ftp.ietf.org/internet\-drafts/draft\-ietf\-ipngwg\-scopedaddr\-format\-02.txt
259 .IR "Protocol Independence Using the Sockets API" ,
260 Proceedings of the freenix track:
261 2000 USENIX annual technical conference, June 2000.
262 http://www.usenix.org/publications/library/proceedings/usenix2000/freenix/metzprotocol.html