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>
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.
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.
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
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" References: RFC 2553
28 .\" 2005-08-09, mtk, added AI_ALL, AI_ADDRCONFIG, AI_V4MAPPED,
29 .\" and AI_NUMERICSERV.
30 .\" 2006-11-25, Ulrich Drepper <drepper@redhat.com>
31 .\" Add text describing Internationalized Domain Name extensions.
32 .\" 2007-06-08, mtk: added example programs
33 .\" 2008-02-26, mtk; clarify discussion of NULL 'hints' argument; other
35 .\" 2008-06-18, mtk: many parts rewritten
36 .\" 2008-12-04, Petr Baudis <pasky@suse.cz>
37 .\" Describe results ordering and reference /etc/gai.conf.
38 .\" FIXME . glibc's 2.9 NEWS file documents DCCP and UDP-lite support
39 .\" and is SCTP support now also there?
41 .\" Japanese Version Copyright (c) 2001 NAKANO Takeo all rights reserved.
42 .\" Translated Sun Jan 14 2001 by NAKANO Takeo <nakano@apm.seikei.ac.jp>
43 .\" Updated 2005-10-09 by Kentaro Shirakata <argrath@ub32.org>
44 .\" Updated 2007-01-01 by Kentaro Shirakata <argrath@ub32.org>
45 .\" Updated 2007-06-13, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.55
46 .\" Updated 2008-04-04, Akihiro MOTOKI, LDP v2.79
47 .\" Updated 2008-08-11, Akihiro MOTOKI, LDP v3.05
48 .\" Updated 2008-11-09, Akihiro MOTOKI, LDP v3.13
49 .\" Updated 2008-12-26, Akihiro MOTOKI, LDP v3.15
50 .\" Updated 2010-04-18, Akihiro MOTOKI, LDP v3.24
52 .\"WORD: null pointer ¥Ì¥ë¡¦¥Ý¥¤¥ó¥¿
54 .TH GETADDRINFO 3 2010-09-27 "GNU" "Linux Programmer's Manual"
56 .\"O getaddrinfo, freeaddrinfo, gai_strerror \- network address and
57 .\"O service translation
59 getaddrinfo, freeaddrinfo, gai_strerror \-
60 ¥Í¥Ã¥È¥ï¡¼¥¯¤Î¥¢¥É¥ì¥¹¤È¥µ¡¼¥Ó¥¹¤òÊÑ´¹¤¹¤ë
64 .B #include <sys/types.h>
65 .B #include <sys/socket.h>
68 .BI "int getaddrinfo(const char *" "node" ", const char *" "service" ,
69 .BI " const struct addrinfo *" "hints" ,
70 .BI " struct addrinfo **" "res" );
72 .BI "void freeaddrinfo(struct addrinfo *" "res" );
74 .BI "const char *gai_strerror(int " "errcode" );
78 .\"O Feature Test Macro Requirements for glibc (see
79 .\"O .BR feature_test_macros (7)):
80 glibc ¸þ¤±¤Îµ¡Ç½¸¡ºº¥Þ¥¯¥í¤ÎÍ×·ï
81 .RB ( feature_test_macros (7)
90 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _POSIX_SOURCE
99 .\"O which identify an Internet host and a service,
100 .\"O .BR getaddrinfo ()
101 .\"O returns one or more
103 .\"O structures, each of which contains an Internet address
104 .\"O that can be specified in a call to
107 .\"O .BR connect (2).
109 ¤Ï¡¢(¥¤¥ó¥¿¡¼¥Í¥Ã¥È¤Î¥Û¥¹¥È¤È¥µ¡¼¥Ó¥¹¤ò¼±Ê̤¹¤ë)
115 ¹½Â¤ÂΤòÊÖ¤¹¡£¤½¤ì¤¾¤ì¤Î
121 ¤ò¸Æ¤Ó½Ð¤¹ºÝ¤Ë»ØÄê¤Ç¤¤ë¥¤¥ó¥¿¡¼¥Í¥Ã¥È¥¢¥É¥ì¥¹¤¬³ÊǼ¤µ¤ì¤Æ¤¤¤ë¡£
123 .\"O .BR getaddrinfo ()
124 .\"O function combines the functionality provided by the
125 .\"O .\" .BR getipnodebyname (3),
126 .\"O .\" .BR getipnodebyaddr (3),
127 .\"O .BR getservbyname (3)
129 .\"O .BR getservbyport (3)
130 .\"O functions into a single interface, but unlike the latter functions,
131 .\"O .BR getaddrinfo ()
132 .\"O is reentrant and allows programs to eliminate IPv4-versus-IPv6 dependencies.
135 .\" .BR getipnodebyname (3),
136 .\" .BR getipnodebyaddr (3),
137 .BR getservbyname (3)
139 .BR getservbyport (3)
140 ¤Îµ¡Ç½¤ò¤Þ¤È¤á¤Æ°ì¤Ä¤Î¥¤¥ó¥¿¡¼¥Õ¥§¡¼¥¹¤Ë¤·¤¿¤â¤Î¤Ç¤¢¤ë¤¬¡¢
143 ¤Ï¥ê¥¨¥ó¥È¥é¥ó¥È¤Ç¤¢¤ê¡¢
145 ¤ò»È¤¦¤³¤È¤Ç¥×¥í¥°¥é¥à¤Ï IPv4 ¤È IPv6 ¤Î°ã¤¤¤Ë´Ø¤¹¤ë°Í¸´Ø·¸¤ò
150 .\"O structure used by
151 .\"O .BR getaddrinfo ()
152 .\"O contains the following fields:
156 ¹½Â¤ÂΤϰʲ¼¤Î¥Õ¥£¡¼¥ë¥É¤ò´Þ¤à¡£
166 struct sockaddr *ai_addr;
168 struct addrinfo *ai_next;
175 .\"O argument points to an
177 .\"O structure that specifies criteria for selecting the socket address
178 .\"O structures returned in the list pointed to by
182 .\"O is not NULL it points to an
186 .\"O .IR ai_socktype ,
189 .\"O specify criteria that limit the set of socket addresses returned by
190 .\"O .BR getaddrinfo (),
195 ¹½Â¤ÂΤò»Ø¤·¼¨¤·¡¢¤³¤Î¹½Â¤ÂΤòÍѤ¤¤Æ
197 ¤¬»Ø¤¹¥ê¥¹¥È¤ËÆþ¤ì¤ÆÊÖ¤¹¥½¥±¥Ã¥È¥¢¥É¥ì¥¹¹½Â¤ÂΤòÁªÂò¤¹¤ë¤¿¤á¤Î´ð½à¤ò»ØÄꤹ¤ë¡£
203 ¹½Â¤ÂΤò»Ø¤·¼¨¤·¡¢¤½¤Î¹½Â¤ÂΤΥե£¡¼¥ë¥É
209 ¤¬ÊÖ¤¹¥½¥±¥Ã¥È¥¢¥É¥ì¥¹½¸¹ç¤ËÂФ¹¤ë´ð½à¤ò»ØÄꤹ¤ë¡£
212 .\"O This field specifies the desired address family for the returned addresses.
213 .\"O Valid values for this field include
220 .\"O .BR getaddrinfo ()
221 .\"O should return socket addresses for any address family
222 .\"O (either IPv4 or IPv6, for example) that can be used with
226 ¤³¤Î¥Õ¥£¡¼¥ë¥É¤ÏÊÖ¤µ¤ì¤ë¥¢¥É¥ì¥¹¤Î´õ˾¤Î¥¢¥É¥ì¥¹¥Õ¥¡¥ß¥ê¡¼¤ò»ØÄꤹ¤ë¡£
227 ¤³¤Î¥Õ¥£¡¼¥ë¥É¤Ë»ØÄê¤Ç¤¤ë͸ú¤ÊÃͤȤ·¤Æ¤Ï
240 ¤Ç»ÈÍѤǤ¤ë¤¤¤º¤ì¤«¤Î¥¢¥É¥ì¥¹¥Õ¥¡¥ß¥ê¡¼ (Î㤨¤Ð IPv4 ¤« IPv6) ¤Î
241 ¥½¥±¥Ã¥È¥¢¥É¥ì¥¹¤òÊÖ¤¹¤³¤È¤òµá¤á¤é¤ì¤ë¡£
244 .\"O This field specifies the preferred socket type, for example
247 .\"O .BR SOCK_DGRAM .
248 .\"O Specifying 0 in this field indicates that socket addresses of any type
249 .\"O can be returned by
250 .\"O .BR getaddrinfo ().
251 ¤³¤Î¥Õ¥£¡¼¥ë¥É¤Ï¿ä¾©¤Î¥½¥±¥Ã¥È·¿ (Î㤨¤Ð
256 ¤³¤Î¥Õ¥£¡¼¥ë¥É¤Ë 0 ¤ò»ØÄꤹ¤ë¤È¡¢Ç¤°Õ¤Î¥½¥±¥Ã¥È·¿¤Î¥½¥±¥Ã¥È¥¢¥É¥ì¥¹¤ò
258 ¤¬ÊÖ¤·¤Æ¤è¤¤¤³¤È¤ò°ÕÌ£¤¹¤ë¡£
261 .\"O This field specifies the protocol for the returned socket addresses.
262 .\"O Specifying 0 in this field indicates that socket addresses with
263 .\"O any protocol can be returned by
264 .\"O .BR getaddrinfo ().
265 ¤³¤Î¥Õ¥£¡¼¥ë¥É¤ÏÊÖ¤µ¤ì¤ë¥½¥±¥Ã¥È¥¢¥É¥ì¥¹¤Î¥×¥í¥È¥³¥ë¤ò»ØÄꤹ¤ë¡£
266 ¤³¤Î¥Õ¥£¡¼¥ë¥É¤Ë 0 ¤ò»ØÄꤹ¤ë¤È¡¢Ç¤°Õ¤Î¥×¥í¥È¥³¥ë¤Î¤Î¥½¥±¥Ã¥È¥¢¥É¥ì¥¹¤ò
268 ¤¬ÊÖ¤·¤Æ¤è¤¤¤³¤È¤ò°ÕÌ£¤¹¤ë¡£
271 .\"O This field specifies additional options, described below.
272 .\"O Multiple flags are specified by bitwise OR-ing them together.
273 ¤³¤Î¥Õ¥£¡¼¥ë¥É¤Ï¡¢ÄɲäΥª¥×¥·¥ç¥ó (²¼µ) ¤ò»ØÄꤹ¤ë¡£
274 Ê£¿ô¤Î¥Õ¥é¥°¤ò»ØÄꤹ¤ëºÝ¤Ë¤Ï¡¢¤½¤ì¤é¤Î¥Ó¥Ã¥Èñ°Ì¤Î OR ¤ò¤È¤Ã¤Æ»ØÄꤹ¤ë¡£
276 .\"O All the other fields in the structure pointed to by
278 .\"O must contain either 0 or a NULL pointer, as appropriate.
280 ¤¬»Ø¤·¼¨¤¹¹½Â¤ÂΤξ¤Î¤¹¤Ù¤Æ¤Î¥Õ¥£¡¼¥ë¥É¤Ë¤Ï
281 0 ¤« NULL ¥Ý¥¤¥ó¥¿¤òŬÀÚ¤ËÆþ¤ì¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
284 .\"O as NULL is equivalent to setting
295 .\"O .BR "(AI_V4MAPPED\ |\ AI_ADDRCONFIG)" .
297 ¤Ë NULL ¤ò»ØÄꤹ¤ë¤Î¤Ï¡¢
308 .B "(AI_V4MAPPED\ |\ AI_ADDRCONFIG)"
309 ¤òÀßÄꤹ¤ë¤Î¤ÈÅù²Á¤Ç¤¢¤ë¡£
312 .\"O specifies either a numerical network address
313 .\"O (for IPv4, numbers-and-dots notation as supported by
314 .\"O .BR inet_aton (3);
315 .\"O for IPv6, hexadecimal string format as supported by
316 .\"O .BR inet_pton (3)),
317 .\"O or a network hostname, whose network addresses are looked up and resolved.
319 .\"O .I hints.ai_flags
321 .\"O .B AI_NUMERICHOST
324 .\"O must be a numerical network address.
326 .\"O .B AI_NUMERICHOST
327 .\"O flag suppresses any potentially lengthy network host address lookups.
329 ¤Ë¤Ï¡¢¿ôÃÍ·Á¼°¤Î¥Í¥Ã¥È¥ï¡¼¥¯¥¢¥É¥ì¥¹
332 ¤Ç¥µ¥Ý¡¼¥È¤µ¤ì¤Æ¤¤¤ë¥É¥Ã¥È¶èÀÚ¤ê¤Î¿ô»ú¤Ë¤è¤ëɽµ¡¢
335 ¤Ç¥µ¥Ý¡¼¥È¤µ¤ì¤Æ¤¤¤ë 16 ¿Ê¿ô¤Îʸ»úÎó·Á¼°) ¤â¤·¤¯¤Ï
336 ¥Í¥Ã¥È¥ï¡¼¥¯¥Û¥¹¥È̾¤ò»ØÄꤹ¤ë¡£
337 ¥Í¥Ã¥È¥ï¡¼¥¯¥Û¥¹¥È̾¤ò»ØÄꤷ¤¿¾ì¹ç¤Ë¤Ï¡¢¤½¤Î¥Í¥Ã¥È¥ï¡¼¥¯¥¢¥É¥ì¥¹¤¬¸¡º÷¤µ¤ì¡¢
338 ̾Á°²ò·è¤¬¹Ô¤Ê¤ï¤ì¤ë¡£
342 ¥Õ¥é¥°¤¬´Þ¤Þ¤ì¤Æ¤¤¤ë¾ì¹ç¤Ï¡¢
344 ¤Ï¿ôÃÍ·Á¼°¤Î¥Í¥Ã¥È¥ï¡¼¥¯¥¢¥É¥ì¥¹¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
346 ¥Õ¥é¥°¤ò»È¤¦¤È¡¢»þ´Ö¤Î³Ý¤«¤ë²ÄǽÀ¤Î¤¢¤ë¥Í¥Ã¥È¥ï¡¼¥¯¥Û¥¹¥È¥¢¥É¥ì¥¹¤Î¸¡º÷¤Ï
351 .\"O flag is specified in
352 .\"O .IR hints.ai_flags ,
356 .\"O then the returned socket addresses will be suitable for
358 .\"O a socket that will
361 .\"O The returned socket address will contain the "wildcard address"
362 .\"O .RB ( INADDR_ANY
363 .\"O for IPv4 addresses,
364 .\"O .BR IN6ADDR_ANY_INIT
365 .\"O for IPv6 address).
366 .\"O The wildcard address is used by applications (typically servers)
367 .\"O that intend to accept connections on any of the hosts's network addresses.
370 .\"O is not NULL, then the
372 .\"O flag is ignored.
376 ¥Õ¥é¥°¤¬»ØÄꤵ¤ì¡¢¤«¤Ä
379 ÊÖ¤µ¤ì¤ë¥½¥±¥Ã¥È¥¢¥É¥ì¥¹¤Ï
384 ¤¹¤ë¤Î¤ËŬ¤·¤¿¤â¤Î¤È¤Ê¤ë¡£
385 ÊÖ¤µ¤ì¤ë¥½¥±¥Ã¥È¥¢¥É¥ì¥¹¤Ë¤Ï¡Ö¥ï¥¤¥ë¥É¥«¡¼¥É¡¦¥¢¥É¥ì¥¹¡×
386 (IPv4 ¥¢¥É¥ì¥¹¤Î¾ì¹ç¤Ï
388 IPv6 ¥¢¥É¥ì¥¹¤Î¾ì¹ç¤Ï
389 .BR IN6ADDR_ANY_INIT )
391 ¥ï¥¤¥ë¥É¥«¡¼¥É¡¦¥¢¥É¥ì¥¹¤Ï¡¢Ç¤°Õ¤Î¥Û¥¹¥È¤Î¥Í¥Ã¥È¥ï¡¼¥¯¥¢¥É¥ì¥¹¤ÇÀܳ¤ò
392 ¼õ¤±ÉÕ¤±¤è¤¦¤È¤¹¤ë¥¢¥×¥ê¥±¡¼¥·¥ç¥ó (Ä̾ï¤Ï¥µ¡¼¥Ð¡¼) ¤ÇÍѤ¤¤é¤ì¤ë¡£
400 .\"O flag is not set in
401 .\"O .IR hints.ai_flags ,
402 .\"O then the returned socket addresses will be suitable for use with
403 .\"O .BR connect (2),
406 .\"O .BR sendmsg (2).
410 .\"O then the network address will be set to the loopback interface address
411 .\"O .RB ( INADDR_LOOPBACK
412 .\"O for IPv4 addresses,
413 .\"O .BR IN6ADDR_LOOPBACK_INIT
414 .\"O for IPv6 address);
415 .\"O this is used by applications that intend to communicate
416 .\"O with peers running on the same host.
420 ¥Õ¥é¥°¤¬¥»¥Ã¥È¤µ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¡¢
421 ÊÖ¤µ¤ì¤ë¥½¥±¥Ã¥È¥¢¥É¥ì¥¹¤Ï
425 ¤Ç¤Î»ÈÍѤËŬ¤·¤¿¤â¤Î¤È¤Ê¤ë¡£
427 ¤¬ NULL ¤Î¾ì¹ç¡¢¥Í¥Ã¥È¥ï¡¼¥¯¥¢¥É¥ì¥¹¤Ë¤Ï¥ë¡¼¥×¥Ð¥Ã¥¯¡¦¥¤¥ó¥¿¡¼¥Õ¥§¥¤¥¹¤Î
428 ¥¢¥É¥ì¥¹ (IPv4 ¥¢¥É¥ì¥¹¤Î¾ì¹ç¤Ï
430 IPv6 ¥¢¥É¥ì¥¹¤Î¾ì¹ç¤Ï
431 .BR IN6ADDR_LOOPBACK_INIT ) ¤¬ÀßÄꤵ¤ì¤ë¡£
432 ¤³¤ì¤ÏƱ¤¸¥Û¥¹¥È¾å¤ÇÆ°ºî¤·¤Æ¤¤¤ëÀܳÁê¼ê¤ÈÄÌ¿®¤¹¤ë¤è¤¦¤Ê
433 ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤ÇÍѤ¤¤é¤ì¤ë¡£
436 .\"O sets the port in each returned address structure.
437 .\"O If this argument is a service name (see
438 .\"O .BR services (5)),
439 .\"O it is translated to the corresponding port number.
440 .\"O This argument can also be specified as a decimal number,
441 .\"O which is simply converted to binary.
443 ¤Ë¤è¤ê¡¢ÊÖ¤µ¤ì¤ë³Æ¥¢¥É¥ì¥¹¹½Â¤ÂΤΥݡ¼¥ÈÈֹ椬·è¤Þ¤ë¡£
444 ¤³¤Î°ú¤¿ô¤¬¥µ¡¼¥Ó¥¹Ì¾
446 »²¾È) ¤Î¾ì¹ç¡¢Âбþ¤¹¤ë¥Ý¡¼¥ÈÈÖ¹æ¤ËËÝÌõ¤µ¤ì¤ë¡£
447 ¤³¤Î°ú¤¿ô¤Ë¤Ï 10 ¿Ê¿ô¤â»ØÄꤹ¤ë¤³¤È¤¬¤Ç¤¡¢
448 ¤³¤Î¾ì¹ç¤Ë¤Ï¥Ð¥¤¥Ê¥ê¤Ø¤ÎÊÑ´¹¤À¤±¤¬¹Ô¤ï¤ì¤ë¡£
451 .\"O is NULL, then the port number of the returned socket addresses
452 .\"O will be left uninitialized.
454 ¤¬ NULL ¤Î¾ì¹ç¡¢ÊÖ¤µ¤ì¤ë¥½¥±¥Ã¥È¥¢¥É¥ì¥¹¤Î¥Ý¡¼¥ÈÈÖ¹æ¤Ï
455 ½é´ü²½¤µ¤ì¤Ê¤¤¤Þ¤Þ¤È¤Ê¤ë¡£
457 .\"O .B AI_NUMERICSERV
459 .\"O .I hints.ai_flags
462 .\"O is not NULL, then
464 .\"O must point to a string containing a numeric port number.
472 ¤Ï¿ôÃͤΥݡ¼¥ÈÈÖ¹æ¤ò´Þ¤àʸ»úÎó¤ò»Ø¤·¼¨¤µ¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
473 .\"O This flag is used to inhibit the invocation of a name resolution service
474 .\"O in cases where it is known not to be required.
475 ¤³¤Î¥Õ¥é¥°¤Ï¡¢Ì¾Á°²ò·è¥µ¡¼¥Ó¥¹¤¬ÉÔÍפǤ¢¤ë¤³¤È¤¬Ê¬¤«¤Ã¤Æ¤¤¤ë¾ì¹ç¤Ë¡¢
476 ¥µ¡¼¥Ó¥¹¤Îµ¯Æ°¤òÍÞÀ©¤¹¤ë¤¿¤á¤ËÍѤ¤¤é¤ì¤ë¡£
482 .\"O but not both, may be NULL.
486 ¤Î¤É¤Á¤é¤«¤Ï NULL ¤Ë¤·¤Æ¤è¤¤¤¬¡¢Î¾ÊýƱ»þ¤Ë NULL ¤Ë¤·¤Æ¤Ï¤Ê¤é¤Ê¤¤¡£
489 .\"O .BR getaddrinfo ()
490 .\"O function allocates and initializes a linked list of
492 .\"O structures, one for each network address that matches
496 .\"O subject to any restrictions imposed by
498 .\"O and returns a pointer to the start of the list in
500 .\"O The items in the linked list are linked by the
506 ¹½Â¤ÂΤΥá¥â¥ê³ÎÊݤò¹Ô¤¤¡¢
508 ¹½Â¤ÂΤΥê¥ó¥¯¥ê¥¹¥È¤ò½é´ü²½¤·¡¢
510 ¤Ë¥ê¥¹¥È¤ÎÀèƬ¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÆþ¤ì¤ÆÊÖ¤¹¡£
511 ¤³¤Î¤È¤¡¢³Æ¹½Â¤ÂΤΥͥåȥ¥¯¥¢¥É¥ì¥¹¤Ï
517 ¤Ç²Ý¤µ¤ì¤¿¤¹¤Ù¤Æ¤ÎÀ©¸Â¤òËþ¤¿¤¹¤â¤Î¤È¤Ê¤ë¡£
520 ¥Õ¥£¡¼¥ë¥É¤Ë¤è¤êÏ¢·ë¤µ¤ì¤ë¡£
522 .\"O There are several reasons why
523 .\"O the linked list may have more than one
525 .\"O structure, including: the network host is multihomed, accessible
526 .\"O over multiple protocols (e.g. both
530 .\"O or the same service is available from multiple socket types (one
532 .\"O address and another
534 .\"O address, for example).
537 ¹½Â¤ÂΤÏÊ£¿ô¸Ä¤Ë¤Ê¤ë¤³¤È¤â¤¢¤ê¡¢¤½¤ÎÍýͳ¤Ï¤¤¤¯¤Ä¤«¤¢¤ë¡£
538 ¥Í¥Ã¥È¥ï¡¼¥¯¥Û¥¹¥È¤¬¥Þ¥ë¥Á¥Û¡¼¥à¤Ç¤¢¤ë¡¢
539 Ê£¿ô¤Î¥×¥í¥È¥³¥ë¤Ç¥¢¥¯¥»¥¹¤Ç¤¤ë (Î㤨¤Ð
544 Ê£¿ô¤Î¥½¥±¥Ã¥È¼ïÊ̤ÇƱ¤¸¥µ¡¼¥Ó¥¹¤¬ÍøÍѤǤ¤ë
547 ¥¢¥É¥ì¥¹¤Ç¡¢¤â¤¦¤Ò¤È¤Ä¤¬
549 ¥¢¥É¥ì¥¹¤Ç¤¢¤ë)¡¢¤¬¤¢¤ë¡£
550 .\"O Normally, the application should try
551 .\"O using the addresses in the order in which they are returned.
552 .\"O The sorting function used within
553 .\"O .BR getaddrinfo ()
554 .\"O is defined in RFC\ 3484; the order can be tweaked for a particular
555 .\"O system by editing
556 .\"O .IR /etc/gai.conf
557 .\"O (available since glibc 2.5).
558 Ä̾ï¤Ï¡¢¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤ÏÊÖ¤µ¤ì¤¿½ç½ø¤Ç¥¢¥É¥ì¥¹¤ò»î¤¹¤Ù¤¤Ç¤¢¤ë¡£
560 ¤ÎÃæ¤Ç»ÈÍѤµ¤ì¤ëʤÙÂؤ¨´Ø¿ô¤Ï RFC\ 3484 ¤ÇÄêµÁ¤µ¤ì¤Æ¤¤¤ë¡£
563 ¤òÊÔ½¸¤¹¤ë¤³¤È¤Ç¡¢¤³¤Î½ç½ø¤òÈùÄ´À°¤¹¤ë¤³¤È¤¬¤Ç¤¤ë
565 ¤Ï glibc 2.5 °Ê¹ß¤ÇÍøÍѤǤ¤ë)¡£
568 .\"O .I hints.ai_flags
573 .\"O field of the first of the
575 .\"O structures in the returned list is set to point to the
576 .\"O official name of the host.
580 ¥Õ¥é¥°¤¬´Þ¤Þ¤ì¤Æ¤¤¤ë¾ì¹ç¡¢ÊÖ¤µ¤ì¤ë¥ê¥¹¥È¤ÎºÇ½é¤Î
584 ¥Õ¥£¡¼¥ë¥É¤Ï¥Û¥¹¥È¤Î¸ø¼°¤Ê̾Á°¤ò»Ø¤¹¤è¤¦¤ËÀßÄꤵ¤ì¤ë¡£
585 .\" In glibc prior to 2.3.4, the ai_canonname of each addrinfo
586 .\" structure was set pointing to the canonical name; that was
587 .\" more than POSIX.1-2001 specified, or other implementations provided.
590 .\"O The remaining fields of each returned
592 .\"O structure are initialized as follows:
595 ¹½Â¤ÂΤλĤê¤Î¥Õ¥£¡¼¥ë¥É¤Ï°Ê²¼¤Î¤è¤¦¤Ë½é´ü²½¤µ¤ì¤ë¡£
599 .\"O .IR ai_socktype ,
602 .\"O fields return the socket creation parameters (i.e., these fields have
603 .\"O the same meaning as the corresponding arguments of
604 .\"O .BR socket (2)).
608 ¥Õ¥£¡¼¥ë¥É¤Ï¥½¥±¥Ã¥ÈÀ¸À®¥Ñ¥é¥á¡¼¥¿¤òÊÖ¤¹
609 (¤³¤ì¤é¤Î¥Õ¥£¡¼¥ë¥É¤Î°ÕÌ£¤Ï
611 ¤ÎƱ¤¸Ì¾Á°¤Î°ú¤¿ô¤ÈƱ¤¸¤Ç¤¢¤ë)¡£
622 .\"O .BR SOCK_STREAM ;
625 .\"O returns the protocol for the socket.
640 ¤Ï¤½¤Î¥½¥±¥Ã¥È¤Î¥×¥í¥È¥³¥ë¤òÊÖ¤¹¡£
642 .\"O A pointer to the socket address is placed in the
644 .\"O field, and the length of the socket address, in bytes,
645 .\"O is placed in the
649 ¥Õ¥£¡¼¥ë¥É¤Ë¤Ï¥½¥±¥Ã¥È¥¢¥É¥ì¥¹¤Ø¤Î¥Ý¥¤¥ó¥¿¤¬½ñ¤¹þ¤Þ¤ì¡¢
651 ¥Õ¥£¡¼¥ë¥É¤Ë¤Ï¥½¥±¥Ã¥È¥¢¥É¥ì¥¹¤ÎŤµ¤¬¥Ð¥¤¥Èñ°Ì¤Ç½ñ¤¹þ¤Þ¤ì¤ë¡£
654 .\"O .I hints.ai_flags
656 .\"O .B AI_ADDRCONFIG
657 .\"O flag, then IPv4 addresses are returned in the list pointed to by
659 .\"O only if the local system has at least one
660 .\"O IPv4 address configured, and IPv6 addresses are only returned
661 .\"O if the local system has at least one IPv6 address configured.
668 ¥í¡¼¥«¥ë¥·¥¹¥Æ¥à¤ËºÇÄã°ì¤Ä¤Î IPv4 ¥¢¥É¥ì¥¹¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¾ì¹ç¤Ï
669 IPv4 ¥¢¥É¥ì¥¹¤¬ÊÖ¤µ¤ì¡¢
670 ¥í¡¼¥«¥ë¥·¥¹¥Æ¥à¤ËºÇÄã°ì¤Ä¤Î IPv6 ¥¢¥É¥ì¥¹¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¾ì¹ç¤Ï
671 IPv6 ¥¢¥É¥ì¥¹¤¬ÊÖ¤µ¤ì¤ë¡£
674 .\"O .I hint.ai_flags
678 .\"O .I hints.ai_family
679 .\"O was specified as
681 .\"O and no matching IPv6 addresses could be found,
682 .\"O then return IPv4-mapped IPv6 addresses in the list pointed to by
692 ¥Þ¥Ã¥Á¤¹¤ë IPv6 ¥¢¥É¥ì¥¹¤¬¸«¤Ä¤«¤é¤Ê¤«¤Ã¤¿¾ì¹ç¡¢
694 ¤¬»Ø¤¹¥ê¥¹¥È¤Ë¤Ï IPv4-mapped IPv6 ¥¢¥É¥ì¥¹¤¬ÊÖ¤µ¤ì¤ë¡£
699 .\"O are specified in
700 .\"O .IR hints.ai_flags ,
701 .\"O then return both IPv6 and IPv4-mapped IPv6 addresses
702 .\"O in the list pointed to by
709 ¤ÎξÊý¤¬»ØÄꤵ¤ì¤Æ¤¤¤ë¾ì¹ç¡¢
711 ¤¬»Ø¤¹¥ê¥¹¥È¤Ë¤Ï IPv6 ¥¢¥É¥ì¥¹¤È IPv4-mapped IPv6 ¥¢¥É¥ì¥¹¤Î
716 .\"O is not also specified.
718 ¤¬»ØÄꤵ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¡¢
723 .\"O .BR freeaddrinfo ()
724 .\"O function frees the memory that was allocated
725 .\"O for the dynamically allocated linked list
731 ¤ËÂФ·¤ÆưŪ¤Ë³ä¤êÅö¤Æ¤é¤ì¤¿¥á¥â¥ê¤ò²òÊü¤¹¤ë¡£
732 .\"O .SS "Extensions to getaddrinfo() for Internationalized Domain Names"
733 .SS "¹ñºÝ²½¥É¥á¥¤¥ó̾¤Î¤¿¤á¤Î getaddrinfo() ¤Î³ÈÄ¥"
735 .\"O Starting with glibc 2.3.4,
736 .\"O .BR getaddrinfo ()
737 .\"O has been extended to selectively allow the incoming and outgoing
738 .\"O hostnames to be transparently converted to and from the
739 .\"O Internationalized Domain Name (IDN) format (see RFC 3490,
740 .\"O .IR "Internationalizing Domain Names in Applications (IDNA)" ).
741 .\"O Four new flags are defined:
744 ¤ÏÆþ½ÐÎϤ¹¤ë¥Û¥¹¥È̾¤òÆ©²áŪ¤Ë¹ñºÝ²½¥É¥á¥¤¥ó̾ (IDN) ·Á¼° (RFC 3490 ¤Î
745 .I "Internationalizing Domain Names in Applications (IDNA)"
746 ¤ò»²¾È¤Î¤³¤È) ¤ÈÊÑ´¹¤¹¤ë¤³¤È¤òÁªÂòŪ¤Ëǧ¤á¤ë¤è¤¦¤Ë³ÈÄ¥¤µ¤ì¤Æ¤¤¤ë¡£
747 4 ¤Ä¤Î¿·¤·¤¤¥Õ¥é¥°¤¬ÄêµÁ¤µ¤ì¤Æ¤¤¤ë:
750 .\"O If this flag is specified, then the node name given in
752 .\"O is converted to IDN format if necessary.
753 .\"O The source encoding is that of the current locale.
754 ¤³¤Î¥Õ¥é¥°¤¬»ØÄꤵ¤ì¤ë¤È¡¢
756 ¤ÇÍ¿¤¨¤é¤ì¤¿¥Î¡¼¥É̾¤ÏɬÍפ¬¤¢¤ì¤Ð IDN ·Á¼°¤ËÊÑ´¹¤µ¤ì¤ë¡£
757 ¥½¡¼¥¹Éä¹æ²½·Á¼°¤Ï¸½ºß¤Î¥í¥±¡¼¥ë¤Î¤â¤Î¤Ç¤¢¤ë¡£
759 .\"O If the input name contains non-ASCII characters, then the IDN encoding
761 .\"O Those parts of the node name (delimited by dots) that contain
762 .\"O non-ASCII characters are encoded using ASCII Compatible Encoding (ACE)
763 .\"O before being passed to the name resolution functions.
764 ÆþÎÏ̾¤ËÈó ASCII ʸ»ú¤¬´Þ¤Þ¤ì¤Æ¤¤¤ë¾ì¹ç¡¢
765 IDN Éä¹æ²½·Á¼°¤¬»È¤ï¤ì¤ë¡£
766 Èó ASCII ʸ»ú¤¬´Þ¤Þ¤ì¤Æ¤¤¤ë(¥Ô¥ê¥ª¥É¤Ç¶èÀÚ¤é¤ì¤ë)Éôʬ¥Î¡¼¥É̾¤Ï¡¢
767 ̾Á°²ò·èµ¡Ç½¤ËÅϤµ¤ì¤ëÁ°¤Ë ASCII ¸ß´¹Éä¹æ²½·Á¼° (ACE) ¤ò»È¤Ã¤Æ
769 .\" Implementation Detail:
770 .\" To minimize effects on system performance the implementation might
771 .\" want to check whether the input string contains any non-ASCII
772 .\" characters. If there are none the IDN step can be skipped completely.
773 .\" On systems which allow not-ASCII safe encodings for a locale this
774 .\" might be a problem.
777 .\"O After a successful name lookup, and if the
779 .\"O flag was specified,
780 .\"O .BR getaddrinfo ()
781 .\"O will return the canonical name of the
782 .\"O node corresponding to the
784 .\"O structure value passed back.
785 .\"O The return value is an exact copy of the value returned by the name
786 .\"O resolution function.
788 ¤¬»ØÄꤵ¤ì¤Æ¤¤¤ë¾ì¹ç¡¢
790 ¤Ï̾Á°¤Î¸¡º÷¤ËÀ®¸ù¤·¤¿¸å¡¢
793 ¹½Â¤ÂΤËÂбþ¤¹¤ë¥Î¡¼¥É¤ÎÀµµ¬Ì¾¤òÊÖ¤¹¡£
794 ÊÖ¤êÃͤÏ̾Á°²ò·èµ¡Ç½¤«¤éÊÖ¤µ¤ì¤¿ÃͤÎÀµ³Î¤Ê¥³¥Ô¡¼¤Ç¤¢¤ë¡£
796 .\"O If the name is encoded using ACE, then it will contain the
798 .\"O prefix for one or more components of the name.
799 .\"O To convert these components into a readable form the
801 .\"O flag can be passed in addition to
802 .\"O .BR AI_CANONNAME .
803 .\"O The resulting string is encoded using the current locale's encoding.
804 ̾Á°¤¬ ACE ¤ÇÉä¹æ²½¤µ¤ì¤Æ¤¤¤ë¾ì¹ç¡¢°ì¤Ä¤Þ¤¿¤ÏÊ£¿ô¤Î̾Á°¤Î¹½À®Í×ÁǤÎÀèƬ¤Ë
807 ¤³¤ì¤é¤Î¹½À®Í×ÁǤòÆɤ߹þ¤ß²Äǽ¤Ê·Á¤ËÊÑ´¹¤¹¤ë¤¿¤á¤Ë¡¢
811 ¥Õ¥é¥°¤òÅϤ¹¤³¤È¤â½ÐÍè¤ë¡£
812 ÊÖ¤µ¤ì¤ëʸ»úÎó¤Ï¸½ºß¤Î¥í¥±¡¼¥ë¤ÎÉä¹æ²½·Á¼°¤ÇÉä¹æ²½¤µ¤ì¤Æ¤¤¤ë¡£
814 .\"Implementation Detail:
815 .\"If no component of the returned name starts with xn\-\- the IDN
816 .\"step can be skipped, therefore avoiding unnecessary slowdowns.
818 .BR AI_IDN_ALLOW_UNASSIGNED ", " AI_IDN_USE_STD3_ASCII_RULES
819 .\"O Setting these flags will enable the
820 .\"O IDNA_ALLOW_UNASSIGNED (allow unassigned Unicode code points) and
821 .\"O IDNA_USE_STD3_ASCII_RULES (check output to make sure it is a STD3
822 .\"O conforming hostname)
823 .\"O flags respectively to be used in the IDNA handling.
824 ¤³¤ì¤é¤Î¥Õ¥é¥°¤¬ÀßÄꤵ¤ì¤ë¤È¡¢
825 IDNA ¥Ï¥ó¥É¥ê¥ó¥°¤ò»È¤¦¤È¤¤Ë¤½¤ì¤¾¤ì
826 IDNA_ALLOW_UNASSIGNED (Èó³ä¤êÅö¤Æ Unicode ¥³¡¼¥É¥Ý¥¤¥ó¥È¤òµö²Ä¤¹¤ë) ¤È
827 IDNA_USE_STD3_ASCII_RULES (½ÐÎϤ¬ STD3 ½àµò¥Û¥¹¥È̾¤Ç¤¢¤ë¤³¤È¤ò³Îǧ¤¹¤ë)
830 .\"O .SH "RETURN VALUE"
832 .\" FIXME glibc defines the following additional errors, some which
833 .\" can probably be returned by getaddrinfo(); they need to
836 .\" #define EAI_INPROGRESS -100 /* Processing request in progress. */
837 .\" #define EAI_CANCELED -101 /* Request canceled. */
838 .\" #define EAI_NOTCANCELED -102 /* Request not canceled. */
839 .\" #define EAI_ALLDONE -103 /* All requests done. */
840 .\" #define EAI_INTR -104 /* Interrupted by a signal. */
841 .\" #define EAI_IDN_ENCODE -105 /* IDN encoding failed. */
843 .\"O .BR getaddrinfo ()
844 .\"O returns 0 if it succeeds, or one of the following nonzero error codes:
846 ¤ÏÀ®¸ù¤¹¤ë¤È 0 ¤òÊÖ¤·¡¢¼ºÇÔ¤¹¤ë¤È°Ê²¼¤ÎÈó 0 ¤Î¥¨¥é¡¼¥³¡¼¥É¤Î¤¤¤º¤ì¤«¤òÊÖ¤¹¡£
850 .\"O The specified network host does not have any network addresses in the
851 .\"O requested address family.
852 »ØÄꤵ¤ì¤¿¥Í¥Ã¥È¥ï¡¼¥¯¥Û¥¹¥È¤Ë¤Ï¡¢
853 Í׵ᤵ¤ì¤¿¥¢¥É¥ì¥¹¥Õ¥¡¥ß¥ê¡¼¤Î¥Í¥Ã¥È¥ï¡¼¥¯¥¢¥É¥ì¥¹¤¬¤Ê¤¤¡£
856 .\"O The name server returned a temporary failure indication.
857 .\"O Try again later.
858 ¥Í¡¼¥à¥µ¡¼¥Ð¡¼¤«¤é°ì»þŪ¤Ê¼ºÇÔ (temporary failure)
859 ¤ò°ÕÌ£¤¹¤ëÊÖ»ö¤¬ÊÖ¤µ¤ì¤¿¡£¸å¤Ç¤â¤¦°ìÅٻ¤Æ¤ß¤è¡£
862 .\"O .I hints.ai_flags
863 .\"O contains invalid flags; or,
864 .\"O .I hints.ai_flags
871 ¤Î¥Õ¥é¥°¤ËÉÔÀµ¤Ê¥Õ¥é¥°¤¬´Þ¤Þ¤ì¤Æ¤¤¤ë¡£¤Þ¤¿¤Ï¡¢
880 .\"O The name server returned a permanent failure indication.
881 ¥Í¡¼¥à¥µ¡¼¥Ð¡¼¤«¤é¹±µ×Ū¤Ê¼ºÇÔ (permanent failure)
882 ¤ò°ÕÌ£¤¹¤ëÊÖ»ö¤¬ÊÖ¤µ¤ì¤¿¡£
885 .\"O The requested address family is not supported.
886 Í׵ᤵ¤ì¤¿¥¢¥É¥ì¥¹¥Õ¥¡¥ß¥ê¡¼¤¬¥µ¥Ý¡¼¥È¤µ¤ì¤Æ¤¤¤Ê¤¤¡£
894 .\"O The specified network host exists, but does not have any
895 .\"O network addresses defined.
896 »ØÄꤵ¤ì¤¿¥Í¥Ã¥È¥ï¡¼¥¯¥Û¥¹¥È¤Ï¸ºß¤¹¤ë¤¬¡¢
897 ¥Í¥Ã¥È¥ï¡¼¥¯¥¢¥É¥ì¥¹¤¬¤Ò¤È¤Ä¤âÄêµÁ¤µ¤ì¤Æ¤¤¤Ê¤¤¡£
904 .\"O is not known; or both
909 .\"O .B AI_NUMERICSERV
910 .\"O was specified in
911 .\"O .I hints.ai_flags
914 .\"O was not a numeric port-number string.
918 ¤Î¤É¤Á¤é¤«¤¬ÉÔÌÀ¡¢¤Þ¤¿¤Ï
922 ¤ÎξÊý¤¬ NULL ¤À¤Ã¤¿¾ì¹ç¡¢¤Þ¤¿¤Ï
930 ¤¬¿ôÃͤΥݡ¼¥ÈÈÖ¹æ¤Îʸ»úÎó¤Ç¤Ê¤¤¡£
933 .\"O The requested service is not available for the requested socket type.
934 .\"O It may be available through another socket type.
935 Í׵ᤵ¤ì¤¿¥µ¡¼¥Ó¥¹¤Ï¡¢Í׵ᤵ¤ì¤¿¥½¥±¥Ã¥È¥¿¥¤¥×¤Ç¤ÏÍøÍѤǤ¤Ê¤¤¡£
936 ¾¤Î¥½¥±¥Ã¥È¥¿¥¤¥×¤Ç¤Ê¤éÍøÍѲÄǽ¤«¤â¤·¤ì¤Ê¤¤¡£
937 .\"O For example, this error could occur if
939 .\"O was "shell" (a service only available on stream sockets), and either
940 .\"O .I hints.ai_protocol
942 .\"O .BR IPPROTO_UDP ,
944 .\"O .I hints.ai_socktype
946 .\"O .BR SOCK_DGRAM ;
947 .\"O or the error could occur if
949 .\"O was not NULL, and
950 .\"O .I hints.ai_socktype
953 .\"O (a socket type that does not support the concept of services).
954 ¤³¤Î¥¨¥é¡¼¤¬È¯À¸¤¹¤ëÎã¤È¤·¤Æ¤Ï¡¢
956 ¤¬ "shell" (¥¹¥È¥ê¡¼¥à¡¦¥½¥±¥Ã¥È¤Ç¤Î¤ßÍøÍѤǤ¤ë¥µ¡¼¥Ó¥¹) ¤Ç¡¢
964 ¤¬»ØÄꤵ¤ì¤¿¤ê¤·¤¿¾ì¹ç¤¬¤¢¤ë¡£
971 (¥µ¡¼¥Ó¥¹¤Î¹Í¤¨Êý¤ò¥µ¥Ý¡¼¥È¤·¤Æ¤¤¤Ê¤¤¥½¥±¥Ã¥È¼ïÊÌ)
972 ¤¬»ØÄꤵ¤ì¤¿¾ì¹ç¤Ë¤â¡¢¤³¤Î¥¨¥é¡¼¤¬È¯À¸¤¹¤ë¡£
975 .\"O The requested socket type is not supported.
976 Í׵ᤵ¤ì¤¿¥½¥±¥Ã¥È¥¿¥¤¥×¤¬¥µ¥Ý¡¼¥È¤µ¤ì¤Æ¤¤¤Ê¤¤¡£
977 .\"O This could occur, for example, if
978 .\"O .I hints.ai_socktype
980 .\"O .I hints.ai_protocol
981 .\"O are inconsistent (e.g.,
984 .\"O .BR IPPROTO_TCP ,
986 ¤³¤Î¥¨¥é¡¼¤¬È¯À¸¤¹¤ëÎã¤È¤·¤Æ¤Ï¡¢
990 ¤¬Ì·½â¤·¤Æ¤¤¤ë¾ì¹ç (Î㤨¤Ð
1001 .\"O Other system error, check
1004 ¤½¤Î¾¤Î¥·¥¹¥Æ¥à¥¨¥é¡¼¡£¾Ü¤·¤¯¤Ï
1009 .\"O .BR gai_strerror ()
1010 .\"O function translates these error codes to a human readable string,
1011 .\"O suitable for error reporting.
1013 ´Ø¿ô¤òÍѤ¤¤ë¤È¡¢¤³¤ì¤é¤Î¥¨¥é¡¼¥³¡¼¥É¤ò¿Í´Ö¤Ë²ÄÆɤÊʸ»úÎó¤ËÊÑ´¹¤Ç¤¤ë¤Î¤Ç¡¢
1014 ¥¨¥é¡¼Êó¹ð¤ËŬ¤¹¤ë¤À¤í¤¦¡£
1018 .\"O .SH "CONFORMING TO"
1022 .\"O .BR getaddrinfo ()
1023 .\"O function is documented in RFC\ 2553.
1026 ´Ø¿ô¤Ï RFC 2553 ¤ËµºÜ¤µ¤ì¤Æ¤¤¤ë¡£
1029 .\"O .BR getaddrinfo ()
1031 .\"O .IB address % scope-id
1032 .\"O notation for specifying the IPv6 scope-ID.
1034 ¤Ï¡¢IPv6 scope-ID ¤ò»ØÄꤹ¤ë¤¿¤á¤Ë
1035 .IB address % scope-id
1036 µË¡¤ò¥µ¥Ý¡¼¥È¤·¤Æ¤¤¤ë¡£
1038 .\"O .BR AI_ADDRCONFIG ,
1042 .\"O are available since glibc 2.3.3.
1043 .\"O .B AI_NUMERICSERV
1044 .\"O is available since glibc 2.3.4.
1048 ¤Ï glibc 2.3.3 °Ê¹ß¤ÇÍøÍѲÄǽ¤Ç¤¢¤ë¡£
1050 ¤Ï glibc 2.3.4 °Ê¹ß¤ÇÍøÍѲÄǽ¤Ç¤¢¤ë¡£
1052 .\"O According to POSIX.1-2001, specifying
1054 .\"O as NULL should cause
1056 .\"O to be assumed as 0.
1057 .\"O The GNU C library instead assumes a value of
1058 .\"O .BR "(AI_V4MAPPED\ |\ AI_ADDRCONFIG)"
1060 .\"O since this value is considered an improvement on the specification.
1061 POSIX.1-2001 ¤Ë¤è¤ë¤È¡¢
1063 ¤Ë NULL ¤¬»ØÄꤵ¤ì¤¿¾ì¹ç¡¢
1065 ¤ò 0 ¤È¤ß¤Ê¤¹¤Ù¤¤È¤µ¤ì¤Æ¤¤¤ë¡£
1066 GNU C ¥é¥¤¥Ö¥é¥ê¤Ç¤Ï¡¢¤³¤Î¾ì¹ç¤Ë¡¢Âå¤ï¤ê¤Ë
1069 .BR "(AI_V4MAPPED\ |\ AI_ADDRCONFIG)"
1070 ¤È¤ß¤Ê¤¹¤è¤¦¤Ë¤Ê¤Ã¤Æ¤¤¤ë¡£
1071 ¤³¤ÎÃͤÎÊý¤¬É¸½àµ¬³Ê¤Î²þÁ±¤Ë¤Ê¤ë¤È¹Í¤¨¤é¤ì¤Æ¤¤¤ë¤«¤é¤Ç¤¢¤ë¡£
1074 .\" getnameinfo.3 refers to this example
1075 .\" socket.2 refers to this example
1076 .\" bind.2 refers to this example
1077 .\" connect.2 refers to this example
1078 .\" recvfrom.2 refers to this example
1079 .\" sendto.2 refers to this example
1080 .\"O The following programs demonstrate the use of
1081 .\"O .BR getaddrinfo (),
1082 .\"O .BR gai_strerror (),
1083 .\"O .BR freeaddrinfo (),
1085 .\"O .BR getnameinfo (3).
1086 .\"O The programs are an echo server and client for UDP datagrams.
1087 °Ê²¼¤Î¥×¥í¥°¥é¥à¤Ï¡¢
1089 .BR gai_strerror (),
1090 .BR freeaddrinfo (),
1092 ¤Î»È¤¤Êý¤ò¼¨¤·¤¿¤â¤Î¤Ç¤¢¤ë¡£
1093 ¥×¥í¥°¥é¥à¤Ï UDP ¥Ç¡¼¥¿¥°¥é¥à¤Î echo ¥µ¡¼¥Ð¤È¥¯¥é¥¤¥¢¥ó¥È¤Ç¤¢¤ë¡£
1094 .\"O .SS Server program
1095 .SS ¥µ¡¼¥Ð¤Î¥×¥í¥°¥é¥à
1098 #include <sys/types.h>
1103 #include <sys/socket.h>
1106 #define BUF_SIZE 500
1109 main(int argc, char *argv[])
1111 struct addrinfo hints;
1112 struct addrinfo *result, *rp;
1114 struct sockaddr_storage peer_addr;
1115 socklen_t peer_addr_len;
1120 fprintf(stderr, "Usage: %s port\\n", argv[0]);
1124 memset(&hints, 0, sizeof(struct addrinfo));
1125 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
1126 hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
1127 hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
1128 hints.ai_protocol = 0; /* Any protocol */
1129 hints.ai_canonname = NULL;
1130 hints.ai_addr = NULL;
1131 hints.ai_next = NULL;
1133 s = getaddrinfo(NULL, argv[1], &hints, &result);
1135 fprintf(stderr, "getaddrinfo: %s\\n", gai_strerror(s));
1139 /* getaddrinfo() returns a list of address structures.
1140 Try each address until we successfully bind(2).
1141 If socket(2) (or bind(2)) fails, we (close the socket
1142 and) try the next address. */
1144 for (rp = result; rp != NULL; rp = rp\->ai_next) {
1145 sfd = socket(rp\->ai_family, rp\->ai_socktype,
1150 if (bind(sfd, rp\->ai_addr, rp\->ai_addrlen) == 0)
1151 break; /* Success */
1156 if (rp == NULL) { /* No address succeeded */
1157 fprintf(stderr, "Could not bind\\n");
1161 freeaddrinfo(result); /* No longer needed */
1163 /* Read datagrams and echo them back to sender */
1166 peer_addr_len = sizeof(struct sockaddr_storage);
1167 nread = recvfrom(sfd, buf, BUF_SIZE, 0,
1168 (struct sockaddr *) &peer_addr, &peer_addr_len);
1170 continue; /* Ignore failed request */
1172 char host[NI_MAXHOST], service[NI_MAXSERV];
1174 s = getnameinfo((struct sockaddr *) &peer_addr,
1175 peer_addr_len, host, NI_MAXHOST,
1176 service, NI_MAXSERV, NI_NUMERICSERV);
1178 printf("Received %ld bytes from %s:%s\\n",
1179 (long) nread, host, service);
1181 fprintf(stderr, "getnameinfo: %s\\n", gai_strerror(s));
1183 if (sendto(sfd, buf, nread, 0,
1184 (struct sockaddr *) &peer_addr,
1185 peer_addr_len) != nread)
1186 fprintf(stderr, "Error sending response\\n");
1190 .\"O .SS Client program
1191 .SS ¥¯¥é¥¤¥¢¥ó¥È¤Î¥×¥í¥°¥é¥à
1194 #include <sys/types.h>
1195 #include <sys/socket.h>
1202 #define BUF_SIZE 500
1205 main(int argc, char *argv[])
1207 struct addrinfo hints;
1208 struct addrinfo *result, *rp;
1215 fprintf(stderr, "Usage: %s host port msg...\\n", argv[0]);
1219 /* Obtain address(es) matching host/port */
1221 memset(&hints, 0, sizeof(struct addrinfo));
1222 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
1223 hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
1225 hints.ai_protocol = 0; /* Any protocol */
1227 s = getaddrinfo(argv[1], argv[2], &hints, &result);
1229 fprintf(stderr, "getaddrinfo: %s\\n", gai_strerror(s));
1233 /* getaddrinfo() returns a list of address structures.
1234 Try each address until we successfully connect(2).
1235 If socket(2) (or connect(2)) fails, we (close the socket
1236 and) try the next address. */
1238 for (rp = result; rp != NULL; rp = rp\->ai_next) {
1239 sfd = socket(rp\->ai_family, rp\->ai_socktype,
1244 if (connect(sfd, rp\->ai_addr, rp\->ai_addrlen) != \-1)
1245 break; /* Success */
1250 if (rp == NULL) { /* No address succeeded */
1251 fprintf(stderr, "Could not connect\\n");
1255 freeaddrinfo(result); /* No longer needed */
1257 /* Send remaining command\-line arguments as separate
1258 datagrams, and read responses from server */
1260 for (j = 3; j < argc; j++) {
1261 len = strlen(argv[j]) + 1;
1262 /* +1 for terminating null byte */
1264 if (len + 1 > BUF_SIZE) {
1266 "Ignoring long message in argument %d\\n", j);
1270 if (write(sfd, argv[j], len) != len) {
1271 fprintf(stderr, "partial/failed write\\n");
1275 nread = read(sfd, buf, BUF_SIZE);
1281 printf("Received %ld bytes: %s\\n", (long) nread, buf);
1289 .\" .BR getipnodebyaddr (3),
1290 .\" .BR getipnodebyname (3)
1291 .BR getaddrinfo_a (3),
1292 .BR gethostbyname (3),
1293 .BR getnameinfo (3),