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