Debian Codegraph @

upstream/debian-trixie/man3/getaddrinfo.3

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