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