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