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