OSDN Git Service

LDP: Update original to LDP v3.68
[linuxjm/LDP_man-pages.git] / original / man3 / getnameinfo.3
1 .\" %%%LICENSE_START(PUBLIC_DOMAIN)
2 .\" This page is in the public domain.
3 .\" %%%LICENSE_END
4 .\"
5 .\" Almost all details are from RFC 2553.
6 .\"
7 .\" 2004-12-14, mtk, Added EAI_OVERFLOW error
8 .\" 2004-12-14 Fixed description of error return
9 .\"
10 .TH GETNAMEINFO 3 2014-05-28 "GNU" "Linux Programmer's Manual"
11 .SH NAME
12 getnameinfo \- address-to-name translation in protocol-independent manner
13 .SH SYNOPSIS
14 .nf
15 .B #include <sys/socket.h>
16 .B #include <netdb.h>
17 .sp
18 .BI "int getnameinfo(const struct sockaddr *" "sa" ", socklen_t " "salen" ,
19 .BI "                char *" "host" ", socklen_t " "hostlen" ,
20 .BI "                char *" "serv" ", socklen_t " "servlen" ", int " "flags" );
21 .fi
22 .sp
23 .in -4n
24 Feature Test Macro Requirements for glibc (see
25 .BR feature_test_macros (7)):
26 .ad l
27 .in
28 .sp
29 .BR getnameinfo ():
30 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _POSIX_SOURCE
31 .ad b
32 .SH DESCRIPTION
33 The
34 .BR getnameinfo ()
35 function is the inverse of
36 .BR getaddrinfo (3):
37 it converts a socket address to a corresponding host and service,
38 in a protocol-independent manner.
39 It combines the functionality of
40 .BR gethostbyaddr (3)
41 and
42 .BR getservbyport (3),
43 but unlike those functions,
44 .BR getnameinfo ()
45 is reentrant and allows programs to eliminate
46 IPv4-versus-IPv6 dependencies.
47
48 The
49 .I sa
50 argument is a pointer to a generic socket address structure
51 (of type
52 .I sockaddr_in
53 or
54 .IR sockaddr_in6 )
55 of size
56 .I salen
57 that holds the input IP address and port number.
58 The arguments
59 .I host
60 and
61 .I serv
62 are pointers to caller-allocated buffers (of size
63 .I hostlen
64 and
65 .I servlen
66 respectively) into which
67 .BR getnameinfo ()
68 places null-terminated strings containing the host and
69 service names respectively.
70
71 The caller can specify that no hostname (or no service name)
72 is required by providing a NULL
73 .I host
74 (or
75 .IR serv )
76 argument or a zero
77 .I hostlen
78 (or
79 .IR servlen )
80 argument.
81 However, at least one of hostname or service name
82 must be requested.
83
84 The
85 .I flags
86 argument modifies the behavior of
87 .BR getnameinfo ()
88 as follows:
89 .TP
90 .B NI_NAMEREQD
91 If set, then an error is returned if the hostname cannot be determined.
92 .TP
93 .B NI_DGRAM
94 If set, then the service is datagram (UDP) based rather than
95 stream (TCP) based.
96 This is required for the few ports (512-514)
97 that have different services for UDP and TCP.
98 .TP
99 .B NI_NOFQDN
100 If set, return only the hostname part of the fully qualified domain name
101 for local hosts.
102 .TP
103 .B NI_NUMERICHOST
104 If set, then the numeric form of the hostname is returned.
105 .\" For example, by calling
106 .\" .BR inet_ntop ()
107 .\" instead of
108 .\" .BR gethostbyaddr ().
109 (When not set, this will still happen in case the node's name
110 cannot be determined.)
111 .\" POSIX.1-2003 has NI_NUMERICSCOPE, but glibc doesn't have it.
112 .TP
113 .B NI_NUMERICSERV
114 If set, then the numeric form of the service address is returned.
115 (When not set, this will still happen in case the service's name
116 cannot be determined.)
117 .SS Extensions to getnameinfo() for Internationalized Domain Names
118 .PP
119 Starting with glibc 2.3.4,
120 .BR getnameinfo ()
121 has been extended to selectively allow
122 hostnames to be transparently converted to and from the
123 Internationalized Domain Name (IDN) format (see RFC 3490,
124 .IR "Internationalizing Domain Names in Applications (IDNA)" ).
125 Three new flags are defined:
126 .TP
127 .B NI_IDN
128 If this flag is used, then the name found in the lookup process is
129 converted from IDN format to the locale's encoding if necessary.
130 ASCII-only names are not affected by the conversion, which
131 makes this flag usable in existing programs and environments.
132 .TP
133 .BR NI_IDN_ALLOW_UNASSIGNED ", " NI_IDN_USE_STD3_ASCII_RULES
134 Setting these flags will enable the
135 IDNA_ALLOW_UNASSIGNED (allow unassigned Unicode code points) and
136 IDNA_USE_STD3_ASCII_RULES (check output to make sure it is a STD3
137 conforming hostname)
138 flags respectively to be used in the IDNA handling.
139 .SH RETURN VALUE
140 .\" FIXME glibc defines the following additional errors, some which
141 .\" can probably be returned by getnameinfo(); they need to
142 .\" be documented.
143 .\" #ifdef __USE_GNU
144 .\" #define EAI_INPROGRESS  -100  /* Processing request in progress.  */
145 .\" #define EAI_CANCELED    -101  /* Request canceled.  */
146 .\" #define EAI_NOTCANCELED -102  /* Request not canceled.  */
147 .\" #define EAI_ALLDONE     -103  /* All requests done.  */
148 .\" #define EAI_INTR        -104  /* Interrupted by a signal.  */
149 .\" #define EAI_IDN_ENCODE  -105  /* IDN encoding failed.  */
150 .\" #endif
151 On success 0 is returned, and node and service names, if requested,
152 are filled with null-terminated strings, possibly truncated to fit
153 the specified buffer lengths.
154 On error, one of the following nonzero error codes is returned:
155 .TP
156 .B EAI_AGAIN
157 The name could not be resolved at this time.
158 Try again later.
159 .TP
160 .B EAI_BADFLAGS
161 The
162 .I flags
163 argument has an invalid value.
164 .TP
165 .B EAI_FAIL
166 A nonrecoverable error occurred.
167 .TP
168 .B EAI_FAMILY
169 The address family was not recognized,
170 or the address length was invalid for the specified family.
171 .TP
172 .B EAI_MEMORY
173 Out of memory.
174 .TP
175 .B EAI_NONAME
176 The name does not resolve for the supplied arguments.
177 .B NI_NAMEREQD
178 is set and the host's name cannot be located,
179 or neither hostname nor service name were requested.
180 .TP
181 .B EAI_OVERFLOW
182 The buffer pointed to by
183 .I host
184 or
185 .I serv
186 was too small.
187 .TP
188 .B EAI_SYSTEM
189 A system error occurred.
190 The error code can be found in
191 .IR errno .
192 .PP
193 The
194 .BR gai_strerror (3)
195 function translates these error codes to a human readable string,
196 suitable for error reporting.
197 .SH FILES
198 /etc/hosts
199 .br
200 /etc/nsswitch.conf
201 .br
202 /etc/resolv.conf
203 .SH VERSIONS
204 .BR getnameinfo ()
205 is provided in glibc since version 2.1.
206 .SH CONFORMING TO
207 RFC\ 2553, POSIX.1-2001.
208 .SH NOTES
209 In order to assist the programmer in choosing reasonable sizes
210 for the supplied buffers,
211 .I <netdb.h>
212 defines the constants
213 .in +4n
214 .nf
215
216 #define NI_MAXHOST      1025
217 #define NI_MAXSERV      32
218 .fi
219 .in
220
221 Since glibc 2.8,
222 these definitions are exposed only if one of the feature test macros
223 .BR _BSD_SOURCE ,
224 .BR _SVID_SOURCE ,
225 or
226 .BR _GNU_SOURCE
227 is defined.
228 .PP
229 The former is the constant
230 .B MAXDNAME
231 in recent versions of BIND's
232 .I <arpa/nameser.h>
233 header file.
234 The latter is a guess based on the services listed
235 in the current Assigned Numbers RFC.
236
237 Before glibc version 2.2, the
238 .I hostlen
239 and
240 .I servlen
241 arguments were typed as
242 .IR size_t .
243 .SH EXAMPLE
244 The following code tries to get the numeric hostname and service name,
245 for a given socket address.
246 Note that there is no hardcoded reference to
247 a particular address family.
248
249 .in +4n
250 .nf
251 struct sockaddr *sa;    /* input */
252 socklen_t len;         /* input */
253 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
254
255 if (getnameinfo(sa, len, hbuf, sizeof(hbuf), sbuf,
256             sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0)
257     printf("host=%s, serv=%s\en", hbuf, sbuf);
258 .fi
259 .in
260
261 The following version checks if the socket address has a
262 reverse address mapping.
263
264 .in +4n
265 .nf
266 struct sockaddr *sa;    /* input */
267 socklen_t len;         /* input */
268 char hbuf[NI_MAXHOST];
269
270 if (getnameinfo(sa, len, hbuf, sizeof(hbuf),
271             NULL, 0, NI_NAMEREQD))
272     printf("could not resolve hostname");
273 else
274     printf("host=%s\en", hbuf);
275 .fi
276 .in
277 .PP
278 An example program using
279 .BR getnameinfo ()
280 can be found in
281 .BR getaddrinfo (3).
282 .SH SEE ALSO
283 .BR accept (2),
284 .BR getpeername (2),
285 .BR getsockname (2),
286 .BR recvfrom (2),
287 .BR socket (2),
288 .BR getaddrinfo (3),
289 .BR gethostbyaddr (3),
290 .BR getservbyname (3),
291 .BR getservbyport (3),
292 .BR inet_ntop (3),
293 .BR hosts (5),
294 .BR services (5),
295 .BR hostname (7),
296 .BR named (8)
297
298 R. Gilligan, S. Thomson, J. Bound and W. Stevens,
299 .IR "Basic Socket Interface Extensions for IPv6" ,
300 RFC\ 2553, March 1999.
301
302 Tatsuya Jinmei and Atsushi Onoe,
303 .IR "An Extension of Format for IPv6 Scoped Addresses" ,
304 internet draft, work in progress
305 .UR ftp://ftp.ietf.org\:/internet\-drafts\:/draft\-ietf\-ipngwg\-scopedaddr\-format\-02.txt
306 .UE .
307
308 Craig Metz,
309 .IR "Protocol Independence Using the Sockets API" ,
310 Proceedings of the freenix track:
311 2000 USENIX annual technical conference, June 2000
312 .ad l
313 .UR http://www.usenix.org\:/publications\:/library\:/proceedings\:/usenix2000\:/freenix\:/metzprotocol.html
314 .UE .
315 .SH COLOPHON
316 This page is part of release 3.68 of the Linux
317 .I man-pages
318 project.
319 A description of the project,
320 information about reporting bugs,
321 and the latest version of this page,
322 can be found at
323 \%http://www.kernel.org/doc/man\-pages/.