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