]>
Commit | Line | Data |
---|---|---|
a3edd71d MK |
1 | .\" Copyright (c) 2007, 2008 Michael Kerrisk <mtk.manpages@gmail.com> |
2 | .\" and Copyright (c) 2006 Ulrich Drepper <drepper@redhat.com> | |
3 | .\" A few pieces of an earlier version remain: | |
4 | .\" Copyright 2000, Sam Varshavchik <mrsam@courier-mta.com> | |
fea681da MK |
5 | .\" |
6 | .\" Permission is granted to make and distribute verbatim copies of this | |
7 | .\" manual provided the copyright notice and this permission notice are | |
8 | .\" preserved on all copies. | |
9 | .\" | |
10 | .\" Permission is granted to copy and distribute modified versions of this | |
11 | .\" manual under the conditions for verbatim copying, provided that the | |
12 | .\" entire resulting derived work is distributed under the terms of a | |
13 | .\" permission notice identical to this one. | |
c13182ef | 14 | .\" |
fea681da MK |
15 | .\" Since the Linux kernel and libraries are constantly changing, this |
16 | .\" manual page may be incorrect or out-of-date. The author(s) assume no | |
17 | .\" responsibility for errors or omissions, or for damages resulting from | |
18 | .\" the use of the information contained herein. The author(s) may not | |
19 | .\" have taken the same level of care in the production of this manual, | |
20 | .\" which is licensed free of charge, as they might when working | |
21 | .\" professionally. | |
c13182ef | 22 | .\" |
fea681da MK |
23 | .\" Formatted or processed versions of this manual, if unaccompanied by |
24 | .\" the source, must acknowledge the copyright and authors of this work. | |
25 | .\" | |
26 | .\" References: RFC 2553 | |
8d18d6ed | 27 | .\" |
c13182ef | 28 | .\" 2005-08-09, mtk, added AI_ALL, AI_ADDRCONFIG, AI_V4MAPPED, |
8d18d6ed | 29 | .\" and AI_NUMERICSERV. |
a3edd71d MK |
30 | .\" 2006-11-25, Ulrich Drepper <drepper@redhat.com> |
31 | .\" Add text describing Internationalized Domain Name extensions. | |
35bf3cc8 | 32 | .\" 2007-06-08, mtk: added example programs |
0af804ac MK |
33 | .\" 2008-02-26, mtk; clarify discussion of NULL 'hints' argument; other |
34 | .\" minor rewrites. | |
a3edd71d | 35 | .\" 2008-06-18, mtk: many parts rewritten |
00269a5a PB |
36 | .\" 2008-12-04, Petr Baudis <pasky@suse.cz> |
37 | .\" Describe results ordering and reference /etc/gai.conf. | |
273f6680 MK |
38 | .\" FIXME . glibc's 2.9 NEWS file documents DCCP and UDP-lite support |
39 | .\" and is SCTP support now also there? | |
8d18d6ed | 40 | .\" |
f9d5775f | 41 | .TH GETADDRINFO 3 2009-09-03 "GNU" "Linux Programmer's Manual" |
fea681da | 42 | .SH NAME |
a3edd71d MK |
43 | getaddrinfo, freeaddrinfo, gai_strerror \- network address and |
44 | service translation | |
fea681da MK |
45 | .SH SYNOPSIS |
46 | .nf | |
47 | .B #include <sys/types.h> | |
48 | .B #include <sys/socket.h> | |
49 | .B #include <netdb.h> | |
50 | .sp | |
51 | .BI "int getaddrinfo(const char *" "node" ", const char *" "service" , | |
52 | .BI " const struct addrinfo *" "hints" , | |
53 | .BI " struct addrinfo **" "res" ); | |
54 | .sp | |
55 | .BI "void freeaddrinfo(struct addrinfo *" "res" ); | |
56 | .sp | |
57 | .BI "const char *gai_strerror(int " "errcode" ); | |
58 | .fi | |
0f200f07 MK |
59 | .sp |
60 | .in -4n | |
61 | Feature Test Macro Requirements for glibc (see | |
62 | .BR feature_test_macros (7)): | |
63 | .ad l | |
64 | .in | |
65 | .sp | |
66 | .BR getaddrinfo (), | |
67 | .BR freeaddrinfo (), | |
68 | .BR gai_strerror (): | |
69 | _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _POSIX_SOURCE | |
70 | .ad b | |
fea681da | 71 | .SH DESCRIPTION |
0af804ac MK |
72 | Given |
73 | .I node | |
74 | and | |
75 | .IR service , | |
a3edd71d | 76 | which identify an Internet host and a service, |
0af804ac | 77 | .BR getaddrinfo () |
a3edd71d MK |
78 | returns one or more |
79 | .I addrinfo | |
80 | structures, each of which contains an Internet address | |
81 | that can be specified in a call to | |
0af804ac | 82 | .BR bind (2) |
a3edd71d MK |
83 | or |
84 | .BR connect (2). | |
fea681da | 85 | The |
2777b1ca | 86 | .BR getaddrinfo () |
fea681da | 87 | function combines the functionality provided by the |
823ecd0c MK |
88 | .\" .BR getipnodebyname (3), |
89 | .\" .BR getipnodebyaddr (3), | |
38f76cd2 | 90 | .BR getservbyname (3) |
fea681da MK |
91 | and |
92 | .BR getservbyport (3) | |
0af804ac | 93 | functions into a single interface, but unlike the latter functions, |
2777b1ca | 94 | .BR getaddrinfo () |
a3edd71d | 95 | is reentrant and allows programs to eliminate IPv4-versus-IPv6 dependencies. |
fea681da MK |
96 | .PP |
97 | The | |
8478ee02 | 98 | .I addrinfo |
a3edd71d MK |
99 | structure used by |
100 | .BR getaddrinfo () | |
101 | contains the following fields: | |
fea681da | 102 | .sp |
3ad4ddcd | 103 | .in +4n |
fea681da | 104 | .nf |
89f9f5b2 MK |
105 | struct addrinfo { |
106 | int ai_flags; | |
107 | int ai_family; | |
108 | int ai_socktype; | |
109 | int ai_protocol; | |
110 | size_t ai_addrlen; | |
111 | struct sockaddr *ai_addr; | |
112 | char *ai_canonname; | |
113 | struct addrinfo *ai_next; | |
114 | }; | |
fea681da | 115 | .fi |
3ad4ddcd | 116 | .in |
fea681da | 117 | .PP |
fea681da | 118 | The |
fea681da | 119 | .I hints |
a3edd71d | 120 | argument points to an |
0af804ac MK |
121 | .I addrinfo |
122 | structure that specifies criteria for selecting the socket address | |
123 | structures returned in the list pointed to by | |
124 | .IR res . | |
a3edd71d MK |
125 | If |
126 | .I hints | |
127 | is not NULL it points to an | |
8478ee02 | 128 | .I addrinfo |
a3edd71d | 129 | structure whose |
fea681da MK |
130 | .IR ai_family , |
131 | .IR ai_socktype , | |
132 | and | |
133 | .I ai_protocol | |
a3edd71d MK |
134 | specify criteria that limit the set of socket addresses returned by |
135 | .BR getaddrinfo (), | |
136 | as follows: | |
137 | .TP 12 | |
fea681da | 138 | .I ai_family |
a3edd71d MK |
139 | This field specifies the desired address family for the returned addresses. |
140 | Valid values for this field include | |
141 | .BR AF_INET | |
142 | and | |
143 | .BR AF_INET6 . | |
144 | The value | |
145 | .B AF_UNSPEC | |
146 | indicates that | |
147 | .BR getaddrinfo () | |
148 | should return socket addresses for any address family | |
149 | (either IPv4 or IPv6, for example) that can be used with | |
150 | .I node | |
151 | and | |
152 | .IR service . | |
153 | .TP | |
fea681da | 154 | .I ai_socktype |
a3edd71d MK |
155 | This field specifies the preferred socket type, for example |
156 | .BR SOCK_STREAM | |
fea681da | 157 | or |
a3edd71d MK |
158 | .BR SOCK_DGRAM . |
159 | Specifying 0 in this field indicates that socket addresses of any type | |
160 | can be returned by | |
161 | .BR getaddrinfo (). | |
162 | .TP | |
fea681da | 163 | .I ai_protocol |
a3edd71d MK |
164 | This field specifies the protocol for the returned socket addresses. |
165 | Specifying 0 in this field indicates that socket addresses with | |
166 | any protocol can be returned by | |
167 | .BR getaddrinfo (). | |
168 | .TP | |
fea681da | 169 | .I ai_flags |
a3edd71d | 170 | This field specifies additional options, described below. |
fea681da | 171 | Multiple flags are specified by logically OR-ing them together. |
a3edd71d MK |
172 | .PP |
173 | All the other fields in the structure pointed to by | |
fea681da | 174 | .I hints |
a3edd71d | 175 | must contain either 0 or a null pointer, as appropriate. |
0af804ac MK |
176 | Specifying |
177 | .I hints | |
178 | as NULL is equivalent to setting | |
179 | .I ai_socktype | |
180 | and | |
181 | .I ai_protocol | |
182 | to 0; | |
183 | .I ai_family | |
184 | to | |
185 | .BR AF_UNSPEC ; | |
186 | and | |
187 | .I ai_flags | |
188 | to | |
189 | .BR "(AI_V4MAPPED\ |\ AI_ADDRCONFIG)" . | |
a3edd71d | 190 | |
fea681da MK |
191 | .I node |
192 | specifies either a numerical network address | |
a3edd71d MK |
193 | (for IPv4, numbers-and-dots notation as supported by |
194 | .BR inet_aton (3); | |
195 | for IPv6, hexadecimal string format as supported by | |
196 | .BR inet_pton (3)), | |
fea681da | 197 | or a network hostname, whose network addresses are looked up and resolved. |
8d18d6ed MK |
198 | If |
199 | .I hints.ai_flags | |
200 | contains the | |
fea681da | 201 | .B AI_NUMERICHOST |
a3edd71d | 202 | flag then |
fea681da | 203 | .I node |
a3edd71d | 204 | must be a numerical network address. |
fea681da MK |
205 | The |
206 | .B AI_NUMERICHOST | |
207 | flag suppresses any potentially lengthy network host address lookups. | |
208 | .PP | |
a3edd71d MK |
209 | If the |
210 | .B AI_PASSIVE | |
211 | flag is specified in | |
212 | .IR hints.ai_flags , | |
213 | and | |
214 | .I node | |
215 | is NULL, | |
216 | then the returned socket addresses will be suitable for | |
217 | .BR bind (2)ing | |
218 | a socket that will | |
219 | .BR accept (2) | |
220 | connections. | |
221 | The returned socket address will contain the "wildcard address" | |
222 | .RB ( INADDR_ANY | |
223 | for IPv4 addresses, | |
224 | .BR IN6ADDR_ANY_INIT | |
225 | for IPv6 address). | |
226 | The wildcard address is used by applications (typically servers) | |
227 | that intend to accept connections on any of the hosts's network addresses. | |
228 | If | |
229 | .I node | |
230 | is not NULL, then the | |
20f81e4b | 231 | .B AI_PASSIVE |
a3edd71d MK |
232 | flag is ignored. |
233 | .PP | |
234 | If the | |
235 | .B AI_PASSIVE | |
236 | flag is not set in | |
237 | .IR hints.ai_flags , | |
238 | then the returned socket addresses will be suitable for use with | |
239 | .BR connect (2), | |
240 | .BR sendto (2), | |
241 | or | |
242 | .BR sendmsg (2). | |
243 | If | |
244 | .I node | |
245 | is NULL, | |
246 | then the network address will be set to the loopback interface address | |
247 | .RB ( INADDR_LOOPBACK | |
248 | for IPv4 addresses, | |
249 | .BR IN6ADDR_LOOPBACK_INIT | |
250 | for IPv6 address); | |
251 | this is used by applications that intend to communicate | |
252 | with peers running on the same host. | |
253 | .PP | |
254 | .I service | |
255 | sets the port in each returned address structure. | |
256 | If this argument is a service name (see | |
257 | .BR services (5)), | |
258 | it is translated to the corresponding port number. | |
259 | This argument can also be specified as a decimal number, | |
260 | which is simply converted to binary. | |
261 | If | |
262 | .I service | |
263 | is NULL, then the port number of the returned socket addresses | |
264 | will be left uninitialized. | |
265 | If | |
266 | .B AI_NUMERICSERV | |
267 | is specified in | |
268 | .I hints.ai_flags | |
269 | and | |
270 | .I service | |
271 | is not NULL, then | |
272 | .I service | |
273 | must point to a string containing a numeric port number. | |
274 | This flag is used to inhibit the invocation of a name resolution service | |
275 | in cases where it is known not to be required. | |
276 | .PP | |
277 | Either | |
278 | .I node | |
279 | or | |
280 | .IR service , | |
281 | but not both, may be NULL. | |
282 | .PP | |
fea681da | 283 | The |
2777b1ca | 284 | .BR getaddrinfo () |
a3edd71d | 285 | function allocates and initializes a linked list of |
8478ee02 | 286 | .I addrinfo |
a3edd71d MK |
287 | structures, one for each network address that matches |
288 | .I node | |
289 | and | |
290 | .IR service , | |
291 | subject to any restrictions imposed by | |
292 | .IR hints , | |
293 | and returns a pointer to the start of the list in | |
3cf81850 | 294 | .IR res . |
a3edd71d MK |
295 | The items in the linked list are linked by the |
296 | .I ai_next | |
297 | field. | |
00269a5a | 298 | |
a3edd71d MK |
299 | There are several reasons why |
300 | the linked list may have more than one | |
c13182ef | 301 | .I addrinfo |
00269a5a PB |
302 | structure, including: the network host is multi-homed, accessible |
303 | over multiple protocols (e.g. both | |
304 | .BR AF_INET | |
305 | and | |
306 | .BR AF_INET6 ); | |
307 | or the same service is available from multiple socket types (one | |
a3edd71d MK |
308 | .B SOCK_STREAM |
309 | address and another | |
310 | .B SOCK_DGRAM | |
311 | address, for example). | |
00269a5a PB |
312 | Normally, the application should try |
313 | using the addresses in the order in which they are returned. | |
314 | The sorting function used within | |
315 | .BR getaddrinfo () | |
316 | is defined in RFC\ 3484; the order can be tweaked for a particular | |
317 | system by editing | |
8ccde1f0 | 318 | .IR /etc/gai.conf |
00269a5a | 319 | (available since glibc 2.5). |
a3edd71d MK |
320 | .PP |
321 | If | |
8d18d6ed | 322 | .I hints.ai_flags |
fea681da MK |
323 | includes the |
324 | .B AI_CANONNAME | |
a3edd71d MK |
325 | flag, then the |
326 | .I ai_canonname | |
327 | field of the first of the | |
328 | .I addrinfo | |
329 | structures in the returned list is set to point to the | |
330 | official name of the host. | |
c13182ef | 331 | .\" In glibc prior to 2.3.4, the ai_canonname of each addrinfo |
8194de33 | 332 | .\" structure was set pointing to the canonical name; that was |
c13182ef | 333 | .\" more than POSIX.1-2001 specified, or other implementations provided. |
8194de33 | 334 | .\" MTK, Aug 05 |
a3edd71d MK |
335 | |
336 | The remaining fields of each returned | |
337 | .I addrinfo | |
338 | structure are initialized as follows: | |
339 | .IP * 2 | |
340 | The | |
9d68eac8 | 341 | .IR ai_family , |
fea681da MK |
342 | .IR ai_socktype , |
343 | and | |
344 | .I ai_protocol | |
a3edd71d MK |
345 | fields return the socket creation parameters (i.e., these fields have |
346 | the same meaning as the corresponding arguments of | |
0af804ac | 347 | .BR socket (2)). |
a3edd71d MK |
348 | For example, |
349 | .I ai_family | |
350 | might return | |
351 | .B AF_INET | |
352 | or | |
353 | .BR AF_INET6 ; | |
354 | .I ai_socktype | |
355 | might return | |
356 | .B SOCK_DGRAM | |
357 | or | |
358 | .BR SOCK_STREAM ; | |
359 | and | |
360 | .I ai_protocol | |
361 | returns the protocol for the socket. | |
362 | .IP * | |
fea681da MK |
363 | A pointer to the socket address is placed in the |
364 | .I ai_addr | |
a3edd71d | 365 | field, and the length of the socket address, in bytes, |
fea681da MK |
366 | is placed in the |
367 | .I ai_addrlen | |
a3edd71d | 368 | field. |
fea681da | 369 | .PP |
8d18d6ed MK |
370 | If |
371 | .I hints.ai_flags | |
372 | includes the | |
373 | .B AI_ADDRCONFIG | |
374 | flag, then IPv4 addresses are returned in the list pointed to by | |
19f56c70 | 375 | .I res |
c13182ef MK |
376 | only if the local system has at least one |
377 | IPv4 address configured, and IPv6 addresses are only returned | |
8d18d6ed MK |
378 | if the local system has at least one IPv6 address configured. |
379 | .PP | |
380 | If | |
381 | .I hint.ai_flags | |
382 | specifies the | |
383 | .B AI_V4MAPPED | |
384 | flag, and | |
385 | .I hints.ai_family | |
386 | was specified as | |
387 | .BR AF_INET6 , | |
388 | and no matching IPv6 addresses could be found, | |
389 | then return IPv4-mapped IPv6 addresses in the list pointed to by | |
19f56c70 | 390 | .IR res . |
8d18d6ed MK |
391 | If both |
392 | .B AI_V4MAPPED | |
393 | and | |
394 | .B AI_ALL | |
395 | are specified in | |
396 | .IR hints.ai_family , | |
c13182ef | 397 | then return both IPv6 and IPv4-mapped IPv6 addresses |
8d18d6ed | 398 | in the list pointed to by |
19f56c70 | 399 | .IR res . |
8d18d6ed MK |
400 | .B AI_ALL |
401 | is ignored if | |
402 | .B AI_V4MAPPED | |
403 | is not also specified. | |
404 | .PP | |
fea681da | 405 | The |
2777b1ca | 406 | .BR freeaddrinfo () |
fea681da | 407 | function frees the memory that was allocated |
8194de33 | 408 | for the dynamically allocated linked list |
fea681da | 409 | .IR res . |
22135cad MK |
410 | .SS "Extensions to getaddrinfo() for Internationalized Domain Names" |
411 | .PP | |
412 | Starting with glibc 2.3.4, | |
c13182ef MK |
413 | .BR getaddrinfo () |
414 | has been extended to selectively allow the incoming and outgoing | |
ddaec46d | 415 | hostnames to be transparently converted to and from the |
22135cad MK |
416 | Internationalized Domain Name (IDN) format (see RFC 3490, |
417 | .IR "Internationalizing Domain Names in Applications (IDNA)" ). | |
418 | Four new flags are defined: | |
419 | .TP | |
420 | .B AI_IDN | |
421 | If this flag is specified, then the node name given in | |
422 | .I node | |
423 | is converted to IDN format if necessary. | |
424 | The source encoding is that of the current locale. | |
425 | ||
c13182ef MK |
426 | If the input name contains non-ASCII characters, then the IDN encoding |
427 | is used. | |
22135cad | 428 | Those parts of the node name (delimited by dots) that contain |
c13182ef | 429 | non-ASCII characters are encoded using ASCII Compatible Encoding (ACE) |
22135cad MK |
430 | before being passed to the name resolution functions. |
431 | .\" Implementation Detail: | |
432 | .\" To minimize effects on system performance the implementation might | |
433 | .\" want to check whether the input string contains any non-ASCII | |
434 | .\" characters. If there are none the IDN step can be skipped completely. | |
435 | .\" On systems which allow not-ASCII safe encodings for a locale this | |
436 | .\" might be a problem. | |
437 | .TP | |
438 | .B AI_CANONIDN | |
c13182ef MK |
439 | After a successful name lookup, and if the |
440 | .B AI_CANONNAME | |
22135cad MK |
441 | flag was specified, |
442 | .BR getaddrinfo () | |
443 | will return the canonical name of the | |
c13182ef MK |
444 | node corresponding to the |
445 | .I addrinfo | |
22135cad MK |
446 | structure value passed back. |
447 | The return value is an exact copy of the value returned by the name | |
448 | resolution function. | |
449 | ||
c13182ef | 450 | If the name is encoded using ACE, then it will contain the |
94e9d9fe | 451 | .I xn\-\- |
c13182ef MK |
452 | prefix for one or more components of the name. |
453 | To convert these components into a readable form the | |
454 | .B AI_CANONIDN | |
455 | flag can be passed in addition to | |
456 | .BR AI_CANONNAME . | |
22135cad MK |
457 | The resulting string is encoded using the current locale's encoding. |
458 | .\" | |
459 | .\"Implementation Detail: | |
94e9d9fe | 460 | .\"If no component of the returned name starts with xn\-\- the IDN |
22135cad MK |
461 | .\"step can be skipped, therefore avoiding unnecessary slowdowns. |
462 | .TP | |
463 | .BR AI_IDN_ALLOW_UNASSIGNED ", " AI_IDN_USE_STD3_ASCII_RULES | |
c13182ef | 464 | Setting these flags will enable the |
22135cad | 465 | IDNA_ALLOW_UNASSIGNED (allow unassigned Unicode code points) and |
c13182ef | 466 | IDNA_USE_STD3_ASCII_RULES (check output to make sure it is a STD3 |
ddaec46d | 467 | conforming hostname) |
22135cad | 468 | flags respectively to be used in the IDNA handling. |
fea681da | 469 | .SH "RETURN VALUE" |
d5da3c8d | 470 | .\" FIXME glibc defines the following additional errors, some which |
c13182ef | 471 | .\" can probably be returned by getaddrinfo(); they need to |
22135cad | 472 | .\" be documented. |
3d32fee8 MK |
473 | .\" #ifdef __USE_GNU |
474 | .\" #define EAI_INPROGRESS -100 /* Processing request in progress. */ | |
475 | .\" #define EAI_CANCELED -101 /* Request canceled. */ | |
476 | .\" #define EAI_NOTCANCELED -102 /* Request not canceled. */ | |
477 | .\" #define EAI_ALLDONE -103 /* All requests done. */ | |
478 | .\" #define EAI_INTR -104 /* Interrupted by a signal. */ | |
479 | .\" #define EAI_IDN_ENCODE -105 /* IDN encoding failed. */ | |
480 | .\" #endif | |
2777b1ca | 481 | .BR getaddrinfo () |
eba72288 | 482 | returns 0 if it succeeds, or one of the following non-zero error codes: |
fea681da | 483 | .TP |
a0aa388d | 484 | .B EAI_ADDRFAMILY |
a3edd71d | 485 | .\" Not in SUSv3 |
a0aa388d MK |
486 | The specified network host does not have any network addresses in the |
487 | requested address family. | |
fea681da | 488 | .TP |
a0aa388d MK |
489 | .B EAI_AGAIN |
490 | The name server returned a temporary failure indication. | |
491 | Try again later. | |
fea681da MK |
492 | .TP |
493 | .B EAI_BADFLAGS | |
a87c4a1d MK |
494 | .I hints.ai_flags |
495 | contains invalid flags; or, | |
496 | .I hints.ai_flags | |
497 | included | |
498 | .B AI_CANONNAME | |
499 | and | |
500 | .I name | |
501 | was NULL. | |
fea681da | 502 | .TP |
a0aa388d MK |
503 | .B EAI_FAIL |
504 | The name server returned a permanent failure indication. | |
505 | .TP | |
506 | .B EAI_FAMILY | |
a3edd71d | 507 | The requested address family is not supported. |
a0aa388d MK |
508 | .TP |
509 | .B EAI_MEMORY | |
510 | Out of memory. | |
511 | .TP | |
512 | .B EAI_NODATA | |
a3edd71d | 513 | .\" Not in SUSv3 |
a0aa388d MK |
514 | The specified network host exists, but does not have any |
515 | network addresses defined. | |
516 | .TP | |
fea681da MK |
517 | .B EAI_NONAME |
518 | The | |
519 | .I node | |
520 | or | |
521 | .I service | |
8d18d6ed | 522 | is not known; or both |
fea681da MK |
523 | .I node |
524 | and | |
525 | .I service | |
8d18d6ed MK |
526 | are NULL; or |
527 | .B AI_NUMERICSERV | |
528 | was specified in | |
529 | .I hints.ai_flags | |
c13182ef | 530 | and |
8d18d6ed MK |
531 | .I service |
532 | was not a numeric port-number string. | |
fea681da MK |
533 | .TP |
534 | .B EAI_SERVICE | |
535 | The requested service is not available for the requested socket type. | |
536 | It may be available through another socket type. | |
a87c4a1d MK |
537 | For example, this error could occur if |
538 | .I service | |
539 | was "shell" (a service only available on stream sockets), and either | |
540 | .I hints.ai_protocol | |
541 | was | |
542 | .BR IPPROTO_UDP , | |
543 | or | |
544 | .I hints.ai_socktype | |
545 | was | |
546 | .BR SOCK_DGRAM ; | |
547 | or the error could occur if | |
548 | .I service | |
549 | was not NULL, and | |
550 | .I hints.ai_socktype | |
551 | was | |
552 | .BR SOCK_RAW | |
553 | (a socket type that does not support the concept of services). | |
fea681da | 554 | .TP |
a0aa388d | 555 | .B EAI_SOCKTYPE |
a3edd71d | 556 | The requested socket type is not supported. |
a87c4a1d MK |
557 | This could occur, for example, if |
558 | .I hints.ai_socktype | |
559 | and | |
560 | .I hints.ai_protocol | |
c5571b61 | 561 | are inconsistent (e.g., |
a87c4a1d MK |
562 | .BR SOCK_DGRAM |
563 | and | |
564 | .BR IPPROTO_TCP , | |
add478c1 | 565 | respectively). |
fea681da MK |
566 | .TP |
567 | .B EAI_SYSTEM | |
568 | Other system error, check | |
569 | .I errno | |
570 | for details. | |
571 | .PP | |
572 | The | |
2777b1ca | 573 | .BR gai_strerror () |
fea681da MK |
574 | function translates these error codes to a human readable string, |
575 | suitable for error reporting. | |
00269a5a PB |
576 | .SH "FILES" |
577 | .I /etc/gai.conf | |
fea681da | 578 | .SH "CONFORMING TO" |
68e1685c | 579 | POSIX.1-2001. |
fea681da | 580 | The |
63aa9df0 | 581 | .BR getaddrinfo () |
331da7c3 | 582 | function is documented in RFC\ 2553. |
8d18d6ed | 583 | .SH "NOTES" |
c783f237 MK |
584 | .BR getaddrinfo () |
585 | supports the | |
586 | .IB address % scope-id | |
587 | notation for specifying the IPv6 scope-ID. | |
588 | ||
8d18d6ed | 589 | .BR AI_ADDRCONFIG , |
d63f46e1 | 590 | .BR AI_ALL , |
8d18d6ed | 591 | and |
0daa9e92 | 592 | .B AI_V4MAPPED |
8d18d6ed | 593 | are available since glibc 2.3.3. |
0daa9e92 | 594 | .B AI_NUMERICSERV |
8d18d6ed | 595 | is available since glibc 2.3.4. |
f9d5775f MK |
596 | |
597 | According to POSIX.1-2001, specifying | |
598 | .I hints | |
599 | as NULL should cause | |
600 | .I ai_flags | |
601 | to be assumed as 0. | |
602 | The GNU C library instead assumes a value of | |
17e5e677 | 603 | .BR "(AI_V4MAPPED\ |\ AI_ADDRCONFIG)" |
f9d5775f MK |
604 | for this case, |
605 | since this value is considered an improvement on the specification. | |
35bf3cc8 | 606 | .SH EXAMPLE |
88a180b7 MK |
607 | .\" getnameinfo.3 refers to this example |
608 | .\" socket.2 refers to this example | |
609 | .\" bind.2 refers to this example | |
610 | .\" connect.2 refers to this example | |
611 | .\" recvfrom.2 refers to this example | |
612 | .\" sendto.2 refers to this example | |
35bf3cc8 MK |
613 | The following programs demonstrate the use of |
614 | .BR getaddrinfo (), | |
615 | .BR gai_strerror (), | |
616 | .BR freeaddrinfo (), | |
617 | and | |
618 | .BR getnameinfo (3). | |
619 | The programs are an echo server and client for UDP datagrams. | |
213bea67 | 620 | .SS Server program |
d84d0300 | 621 | \& |
35bf3cc8 | 622 | .nf |
988db661 | 623 | #include <sys/types.h> |
35bf3cc8 | 624 | #include <stdio.h> |
988db661 MK |
625 | #include <stdlib.h> |
626 | #include <unistd.h> | |
627 | #include <string.h> | |
628 | #include <sys/socket.h> | |
35bf3cc8 MK |
629 | #include <netdb.h> |
630 | ||
631 | #define BUF_SIZE 500 | |
632 | ||
633 | int | |
634 | main(int argc, char *argv[]) | |
635 | { | |
636 | struct addrinfo hints; | |
637 | struct addrinfo *result, *rp; | |
638 | int sfd, s; | |
639 | struct sockaddr_storage peer_addr; | |
640 | socklen_t peer_addr_len; | |
641 | ssize_t nread; | |
642 | char buf[BUF_SIZE]; | |
643 | ||
644 | if (argc != 2) { | |
645 | fprintf(stderr, "Usage: %s port\\n", argv[0]); | |
646 | exit(EXIT_FAILURE); | |
647 | } | |
648 | ||
649 | memset(&hints, 0, sizeof(struct addrinfo)); | |
650 | hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ | |
651 | hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */ | |
652 | hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */ | |
653 | hints.ai_protocol = 0; /* Any protocol */ | |
59cef123 MK |
654 | hints.ai_canonname = NULL; |
655 | hints.ai_addr = NULL; | |
656 | hints.ai_next = NULL; | |
35bf3cc8 MK |
657 | |
658 | s = getaddrinfo(NULL, argv[1], &hints, &result); | |
659 | if (s != 0) { | |
660 | fprintf(stderr, "getaddrinfo: %s\\n", gai_strerror(s)); | |
661 | exit(EXIT_FAILURE); | |
662 | } | |
663 | ||
664 | /* getaddrinfo() returns a list of address structures. | |
7a056410 | 665 | Try each address until we successfully bind(2). |
988db661 | 666 | If socket(2) (or bind(2)) fails, we (close the socket |
35bf3cc8 MK |
667 | and) try the next address. */ |
668 | ||
29059a65 | 669 | for (rp = result; rp != NULL; rp = rp\->ai_next) { |
988db661 | 670 | sfd = socket(rp\->ai_family, rp\->ai_socktype, |
29059a65 MK |
671 | rp\->ai_protocol); |
672 | if (sfd == \-1) | |
35bf3cc8 MK |
673 | continue; |
674 | ||
29059a65 | 675 | if (bind(sfd, rp\->ai_addr, rp\->ai_addrlen) == 0) |
35bf3cc8 MK |
676 | break; /* Success */ |
677 | ||
678 | close(sfd); | |
679 | } | |
680 | ||
681 | if (rp == NULL) { /* No address succeeded */ | |
682 | fprintf(stderr, "Could not bind\\n"); | |
683 | exit(EXIT_FAILURE); | |
684 | } | |
685 | ||
686 | freeaddrinfo(result); /* No longer needed */ | |
687 | ||
688 | /* Read datagrams and echo them back to sender */ | |
689 | ||
690 | for (;;) { | |
691 | peer_addr_len = sizeof(struct sockaddr_storage); | |
988db661 | 692 | nread = recvfrom(sfd, buf, BUF_SIZE, 0, |
35bf3cc8 | 693 | (struct sockaddr *) &peer_addr, &peer_addr_len); |
29059a65 | 694 | if (nread == \-1) |
35bf3cc8 MK |
695 | continue; /* Ignore failed request */ |
696 | ||
697 | char host[NI_MAXHOST], service[NI_MAXSERV]; | |
988db661 MK |
698 | |
699 | s = getnameinfo((struct sockaddr *) &peer_addr, | |
35bf3cc8 MK |
700 | peer_addr_len, host, NI_MAXHOST, |
701 | service, NI_MAXSERV, NI_NUMERICSERV); | |
988db661 | 702 | if (s == 0) |
35bf3cc8 MK |
703 | printf("Received %ld bytes from %s:%s\\n", |
704 | (long) nread, host, service); | |
705 | else | |
706 | fprintf(stderr, "getnameinfo: %s\\n", gai_strerror(s)); | |
707 | ||
988db661 | 708 | if (sendto(sfd, buf, nread, 0, |
35bf3cc8 | 709 | (struct sockaddr *) &peer_addr, |
988db661 | 710 | peer_addr_len) != nread) |
35bf3cc8 MK |
711 | fprintf(stderr, "Error sending response\\n"); |
712 | } | |
713 | } | |
714 | .fi | |
213bea67 MK |
715 | .SS Client program |
716 | .R " " | |
35bf3cc8 | 717 | .nf |
988db661 | 718 | #include <sys/types.h> |
35bf3cc8 MK |
719 | #include <sys/socket.h> |
720 | #include <netdb.h> | |
721 | #include <stdio.h> | |
988db661 MK |
722 | #include <stdlib.h> |
723 | #include <unistd.h> | |
724 | #include <string.h> | |
35bf3cc8 MK |
725 | |
726 | #define BUF_SIZE 500 | |
727 | ||
728 | int | |
729 | main(int argc, char *argv[]) | |
730 | { | |
731 | struct addrinfo hints; | |
732 | struct addrinfo *result, *rp; | |
733 | int sfd, s, j; | |
734 | size_t len; | |
735 | ssize_t nread; | |
736 | char buf[BUF_SIZE]; | |
737 | ||
738 | if (argc < 3) { | |
739 | fprintf(stderr, "Usage: %s host port msg...\\n", argv[0]); | |
740 | exit(EXIT_FAILURE); | |
741 | } | |
742 | ||
743 | /* Obtain address(es) matching host/port */ | |
744 | ||
745 | memset(&hints, 0, sizeof(struct addrinfo)); | |
746 | hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ | |
747 | hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */ | |
748 | hints.ai_flags = 0; | |
749 | hints.ai_protocol = 0; /* Any protocol */ | |
750 | ||
751 | s = getaddrinfo(argv[1], argv[2], &hints, &result); | |
752 | if (s != 0) { | |
753 | fprintf(stderr, "getaddrinfo: %s\\n", gai_strerror(s)); | |
754 | exit(EXIT_FAILURE); | |
755 | } | |
756 | ||
757 | /* getaddrinfo() returns a list of address structures. | |
7a056410 MK |
758 | Try each address until we successfully connect(2). |
759 | If socket(2) (or connect(2)) fails, we (close the socket | |
35bf3cc8 MK |
760 | and) try the next address. */ |
761 | ||
29059a65 | 762 | for (rp = result; rp != NULL; rp = rp\->ai_next) { |
988db661 | 763 | sfd = socket(rp\->ai_family, rp\->ai_socktype, |
29059a65 MK |
764 | rp\->ai_protocol); |
765 | if (sfd == \-1) | |
35bf3cc8 | 766 | continue; |
988db661 | 767 | |
29059a65 | 768 | if (connect(sfd, rp\->ai_addr, rp\->ai_addrlen) != \-1) |
35bf3cc8 MK |
769 | break; /* Success */ |
770 | ||
771 | close(sfd); | |
772 | } | |
773 | ||
774 | if (rp == NULL) { /* No address succeeded */ | |
775 | fprintf(stderr, "Could not connect\\n"); | |
776 | exit(EXIT_FAILURE); | |
777 | } | |
778 | ||
779 | freeaddrinfo(result); /* No longer needed */ | |
780 | ||
988db661 | 781 | /* Send remaining command\-line arguments as separate |
35bf3cc8 MK |
782 | datagrams, and read responses from server */ |
783 | ||
784 | for (j = 3; j < argc; j++) { | |
988db661 | 785 | len = strlen(argv[j]) + 1; |
35bf3cc8 MK |
786 | /* +1 for terminating null byte */ |
787 | ||
788 | if (len + 1 > BUF_SIZE) { | |
988db661 | 789 | fprintf(stderr, |
35bf3cc8 MK |
790 | "Ignoring long message in argument %d\\n", j); |
791 | continue; | |
792 | } | |
793 | ||
794 | if (write(sfd, argv[j], len) != len) { | |
795 | fprintf(stderr, "partial/failed write\\n"); | |
796 | exit(EXIT_FAILURE); | |
797 | } | |
988db661 | 798 | |
35bf3cc8 | 799 | nread = read(sfd, buf, BUF_SIZE); |
29059a65 | 800 | if (nread == \-1) { |
35bf3cc8 MK |
801 | perror("read"); |
802 | exit(EXIT_FAILURE); | |
803 | } | |
804 | ||
805 | printf("Received %ld bytes: %s\\n", (long) nread, buf); | |
806 | } | |
807 | ||
808 | exit(EXIT_SUCCESS); | |
809 | } | |
810 | .fi | |
fea681da | 811 | .SH "SEE ALSO" |
823ecd0c | 812 | .\" .BR getipnodebyaddr (3), |
ac5e6a36 | 813 | .\" .BR getipnodebyname (3), |
a3edd71d | 814 | .BR gethostbyname (3), |
60435a75 | 815 | .BR getnameinfo (3), |
a3edd71d MK |
816 | .BR inet (3), |
817 | .BR hostname (7), | |
818 | .BR ip (7) |