]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/getaddrinfo.3
ffix
[thirdparty/man-pages.git] / man3 / getaddrinfo.3
CommitLineData
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
39getaddrinfo, freeaddrinfo, gai_strerror \- network address and
40service 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
56Given
57.I node
58and
59.IR service ,
a3edd71d 60which identify an Internet host and a service,
0af804ac 61.BR getaddrinfo ()
a3edd71d
MK
62returns one or more
63.I addrinfo
64structures, each of which contains an Internet address
65that can be specified in a call to
0af804ac 66.BR bind (2)
a3edd71d
MK
67or
68.BR connect (2).
fea681da 69The
2777b1ca 70.BR getaddrinfo ()
fea681da 71function combines the functionality provided by the
823ecd0c
MK
72.\" .BR getipnodebyname (3),
73.\" .BR getipnodebyaddr (3),
38f76cd2 74.BR getservbyname (3)
fea681da
MK
75and
76.BR getservbyport (3)
0af804ac 77functions into a single interface, but unlike the latter functions,
2777b1ca 78.BR getaddrinfo ()
a3edd71d 79is reentrant and allows programs to eliminate IPv4-versus-IPv6 dependencies.
fea681da
MK
80.PP
81The
8478ee02 82.I addrinfo
a3edd71d
MK
83structure used by
84.BR getaddrinfo ()
85contains the following fields:
fea681da 86.sp
3ad4ddcd 87.in +4n
fea681da 88.nf
89f9f5b2
MK
89struct 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 102The
fea681da 103.I hints
a3edd71d 104argument points to an
0af804ac
MK
105.I addrinfo
106structure that specifies criteria for selecting the socket address
107structures returned in the list pointed to by
108.IR res .
a3edd71d
MK
109If
110.I hints
111is not NULL it points to an
8478ee02 112.I addrinfo
a3edd71d 113structure whose
fea681da
MK
114.IR ai_family ,
115.IR ai_socktype ,
116and
117.I ai_protocol
a3edd71d
MK
118specify criteria that limit the set of socket addresses returned by
119.BR getaddrinfo (),
120as follows:
121.TP 12
fea681da 122.I ai_family
a3edd71d
MK
123This field specifies the desired address family for the returned addresses.
124Valid values for this field include
125.BR AF_INET
126and
127.BR AF_INET6 .
128The value
129.B AF_UNSPEC
130indicates that
131.BR getaddrinfo ()
132should return socket addresses for any address family
133(either IPv4 or IPv6, for example) that can be used with
134.I node
135and
136.IR service .
137.TP
fea681da 138.I ai_socktype
a3edd71d
MK
139This field specifies the preferred socket type, for example
140.BR SOCK_STREAM
fea681da 141or
a3edd71d
MK
142.BR SOCK_DGRAM .
143Specifying 0 in this field indicates that socket addresses of any type
144can be returned by
145.BR getaddrinfo ().
146.TP
fea681da 147.I ai_protocol
a3edd71d
MK
148This field specifies the protocol for the returned socket addresses.
149Specifying 0 in this field indicates that socket addresses with
150any protocol can be returned by
151.BR getaddrinfo ().
152.TP
fea681da 153.I ai_flags
a3edd71d 154This field specifies additional options, described below.
fea681da 155Multiple flags are specified by logically OR-ing them together.
a3edd71d
MK
156.PP
157All the other fields in the structure pointed to by
fea681da 158.I hints
a3edd71d 159must contain either 0 or a null pointer, as appropriate.
0af804ac
MK
160Specifying
161.I hints
162as NULL is equivalent to setting
163.I ai_socktype
164and
165.I ai_protocol
166to 0;
167.I ai_family
168to
169.BR AF_UNSPEC ;
170and
171.I ai_flags
172to
173.BR "(AI_V4MAPPED\ |\ AI_ADDRCONFIG)" .
a3edd71d 174
fea681da
MK
175.I node
176specifies either a numerical network address
a3edd71d
MK
177(for IPv4, numbers-and-dots notation as supported by
178.BR inet_aton (3);
179for IPv6, hexadecimal string format as supported by
180.BR inet_pton (3)),
fea681da 181or a network hostname, whose network addresses are looked up and resolved.
8d18d6ed
MK
182If
183.I hints.ai_flags
184contains the
fea681da 185.B AI_NUMERICHOST
a3edd71d 186flag then
fea681da 187.I node
a3edd71d 188must be a numerical network address.
fea681da
MK
189The
190.B AI_NUMERICHOST
191flag suppresses any potentially lengthy network host address lookups.
192.PP
a3edd71d
MK
193If the
194.B AI_PASSIVE
195flag is specified in
196.IR hints.ai_flags ,
197and
198.I node
199is NULL,
200then the returned socket addresses will be suitable for
201.BR bind (2)ing
202a socket that will
203.BR accept (2)
204connections.
205The returned socket address will contain the "wildcard address"
206.RB ( INADDR_ANY
207for IPv4 addresses,
208.BR IN6ADDR_ANY_INIT
209for IPv6 address).
210The wildcard address is used by applications (typically servers)
211that intend to accept connections on any of the hosts's network addresses.
212If
213.I node
214is not NULL, then the
20f81e4b 215.B AI_PASSIVE
a3edd71d
MK
216flag is ignored.
217.PP
218If the
219.B AI_PASSIVE
220flag is not set in
221.IR hints.ai_flags ,
222then the returned socket addresses will be suitable for use with
223.BR connect (2),
224.BR sendto (2),
225or
226.BR sendmsg (2).
227If
228.I node
229is NULL,
230then the network address will be set to the loopback interface address
231.RB ( INADDR_LOOPBACK
232for IPv4 addresses,
233.BR IN6ADDR_LOOPBACK_INIT
234for IPv6 address);
235this is used by applications that intend to communicate
236with peers running on the same host.
237.PP
238.I service
239sets the port in each returned address structure.
240If this argument is a service name (see
241.BR services (5)),
242it is translated to the corresponding port number.
243This argument can also be specified as a decimal number,
244which is simply converted to binary.
245If
246.I service
247is NULL, then the port number of the returned socket addresses
248will be left uninitialized.
249If
250.B AI_NUMERICSERV
251is specified in
252.I hints.ai_flags
253and
254.I service
255is not NULL, then
256.I service
257must point to a string containing a numeric port number.
258This flag is used to inhibit the invocation of a name resolution service
259in cases where it is known not to be required.
260.PP
261Either
262.I node
263or
264.IR service ,
265but not both, may be NULL.
266.PP
fea681da 267The
2777b1ca 268.BR getaddrinfo ()
a3edd71d 269function allocates and initializes a linked list of
8478ee02 270.I addrinfo
a3edd71d
MK
271structures, one for each network address that matches
272.I node
273and
274.IR service ,
275subject to any restrictions imposed by
276.IR hints ,
277and returns a pointer to the start of the list in
278.IR res .
279The items in the linked list are linked by the
280.I ai_next
281field.
282There are several reasons why
283the linked list may have more than one
c13182ef 284.I addrinfo
a3edd71d
MK
285structure, including: the network host is multi-homed; or the same service
286is available from multiple socket protocols (one
287.B SOCK_STREAM
288address and another
289.B SOCK_DGRAM
290address, for example).
291.PP
292If
8d18d6ed 293.I hints.ai_flags
fea681da
MK
294includes the
295.B AI_CANONNAME
a3edd71d
MK
296flag, then the
297.I ai_canonname
298field of the first of the
299.I addrinfo
300structures in the returned list is set to point to the
301official 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
307The remaining fields of each returned
308.I addrinfo
309structure are initialized as follows:
310.IP * 2
311The
312.I ai_family
fea681da
MK
313.IR ai_socktype ,
314and
315.I ai_protocol
a3edd71d
MK
316fields return the socket creation parameters (i.e., these fields have
317the same meaning as the corresponding arguments of
0af804ac 318.BR socket (2)).
a3edd71d
MK
319For example,
320.I ai_family
321might return
322.B AF_INET
323or
324.BR AF_INET6 ;
325.I ai_socktype
326might return
327.B SOCK_DGRAM
328or
329.BR SOCK_STREAM ;
330and
331.I ai_protocol
332returns the protocol for the socket.
333.IP *
fea681da
MK
334A pointer to the socket address is placed in the
335.I ai_addr
a3edd71d 336field, and the length of the socket address, in bytes,
fea681da
MK
337is placed in the
338.I ai_addrlen
a3edd71d 339field.
fea681da 340.PP
8d18d6ed
MK
341If
342.I hints.ai_flags
343includes the
344.B AI_ADDRCONFIG
345flag, then IPv4 addresses are returned in the list pointed to by
346.I result
c13182ef
MK
347only if the local system has at least one
348IPv4 address configured, and IPv6 addresses are only returned
8d18d6ed
MK
349if the local system has at least one IPv6 address configured.
350.PP
351If
352.I hint.ai_flags
353specifies the
354.B AI_V4MAPPED
355flag, and
356.I hints.ai_family
357was specified as
358.BR AF_INET6 ,
359and no matching IPv6 addresses could be found,
360then return IPv4-mapped IPv6 addresses in the list pointed to by
361.IR result .
362If both
363.B AI_V4MAPPED
364and
365.B AI_ALL
366are specified in
367.IR hints.ai_family ,
c13182ef 368then return both IPv6 and IPv4-mapped IPv6 addresses
8d18d6ed
MK
369in the list pointed to by
370.IR result .
371.B AI_ALL
372is ignored if
373.B AI_V4MAPPED
374is not also specified.
375.PP
fea681da 376The
2777b1ca 377.BR freeaddrinfo ()
fea681da 378function frees the memory that was allocated
8194de33 379for the dynamically allocated linked list
fea681da 380.IR res .
22135cad
MK
381.SS "Extensions to getaddrinfo() for Internationalized Domain Names"
382.PP
383Starting with glibc 2.3.4,
c13182ef
MK
384.BR getaddrinfo ()
385has been extended to selectively allow the incoming and outgoing
ddaec46d 386hostnames to be transparently converted to and from the
22135cad
MK
387Internationalized Domain Name (IDN) format (see RFC 3490,
388.IR "Internationalizing Domain Names in Applications (IDNA)" ).
389Four new flags are defined:
390.TP
391.B AI_IDN
392If this flag is specified, then the node name given in
393.I node
394is converted to IDN format if necessary.
395The source encoding is that of the current locale.
396
c13182ef
MK
397If the input name contains non-ASCII characters, then the IDN encoding
398is used.
22135cad 399Those parts of the node name (delimited by dots) that contain
c13182ef 400non-ASCII characters are encoded using ASCII Compatible Encoding (ACE)
22135cad
MK
401before 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
410After a successful name lookup, and if the
411.B AI_CANONNAME
22135cad
MK
412flag was specified,
413.BR getaddrinfo ()
414will return the canonical name of the
c13182ef
MK
415node corresponding to the
416.I addrinfo
22135cad
MK
417structure value passed back.
418The return value is an exact copy of the value returned by the name
419resolution function.
420
c13182ef 421If the name is encoded using ACE, then it will contain the
94e9d9fe 422.I xn\-\-
c13182ef
MK
423prefix for one or more components of the name.
424To convert these components into a readable form the
425.B AI_CANONIDN
426flag can be passed in addition to
427.BR AI_CANONNAME .
22135cad
MK
428The 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 435Setting these flags will enable the
22135cad 436IDNA_ALLOW_UNASSIGNED (allow unassigned Unicode code points) and
c13182ef 437IDNA_USE_STD3_ASCII_RULES (check output to make sure it is a STD3
ddaec46d 438conforming hostname)
22135cad 439flags 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 453returns 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
457The specified network host does not have any network addresses in the
458requested address family.
fea681da 459.TP
a0aa388d
MK
460.B EAI_AGAIN
461The name server returned a temporary failure indication.
462Try again later.
fea681da
MK
463.TP
464.B EAI_BADFLAGS
465.I ai_flags
466contains invalid flags.
467.TP
a0aa388d
MK
468.B EAI_FAIL
469The name server returned a permanent failure indication.
470.TP
471.B EAI_FAMILY
a3edd71d 472The requested address family is not supported.
a0aa388d
MK
473.TP
474.B EAI_MEMORY
475Out of memory.
476.TP
477.B EAI_NODATA
a3edd71d 478.\" Not in SUSv3
a0aa388d
MK
479The specified network host exists, but does not have any
480network addresses defined.
481.TP
fea681da
MK
482.B EAI_NONAME
483The
484.I node
485or
486.I service
8d18d6ed 487is not known; or both
fea681da
MK
488.I node
489and
490.I service
8d18d6ed
MK
491are NULL; or
492.B AI_NUMERICSERV
493was specified in
494.I hints.ai_flags
c13182ef 495and
8d18d6ed
MK
496.I service
497was not a numeric port-number string.
fea681da
MK
498.TP
499.B EAI_SERVICE
500The requested service is not available for the requested socket type.
501It may be available through another socket type.
502.TP
a0aa388d 503.B EAI_SOCKTYPE
a3edd71d 504The requested socket type is not supported.
fea681da
MK
505.TP
506.B EAI_SYSTEM
507Other system error, check
508.I errno
509for details.
510.PP
511The
2777b1ca 512.BR gai_strerror ()
fea681da
MK
513function translates these error codes to a human readable string,
514suitable for error reporting.
515.SH "CONFORMING TO"
68e1685c 516POSIX.1-2001.
fea681da 517The
63aa9df0 518.BR getaddrinfo ()
331da7c3 519function is documented in RFC\ 2553.
8d18d6ed 520.SH "NOTES"
c783f237
MK
521.BR getaddrinfo ()
522supports the
523.IB address % scope-id
524notation for specifying the IPv6 scope-ID.
525
8d18d6ed 526.BR AI_ADDRCONFIG ,
d63f46e1 527.BR AI_ALL ,
8d18d6ed 528and
0daa9e92 529.B AI_V4MAPPED
8d18d6ed 530are available since glibc 2.3.3.
0daa9e92 531.B AI_NUMERICSERV
8d18d6ed 532is 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
540The following programs demonstrate the use of
541.BR getaddrinfo (),
542.BR gai_strerror (),
543.BR freeaddrinfo (),
544and
545.BR getnameinfo (3).
546The programs are an echo server and client for UDP datagrams.
547
548This 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
562int
563main(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
646This 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
660int
661main(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)