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