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