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