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 2009-09-03 "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)
89 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _POSIX_SOURCE
97 .\"O which identify an Internet host and a service,
98 .\"O .BR getaddrinfo ()
99 .\"O returns one or more
101 .\"O structures, each of which contains an Internet address
102 .\"O that can be specified in a call to
105 .\"O .BR connect (2).
107 ¤Ï¡¢(¥¤¥ó¥¿¡¼¥Í¥Ã¥È¤Î¥Û¥¹¥È¤È¥µ¡¼¥Ó¥¹¤ò¼±Ê̤¹¤ë)
113 ¹½Â¤ÂΤòÊÖ¤¹¡£¤½¤ì¤¾¤ì¤Î
119 ¤ò¸Æ¤Ó½Ð¤¹ºÝ¤Ë»ØÄê¤Ç¤¤ë¥¤¥ó¥¿¡¼¥Í¥Ã¥È¥¢¥É¥ì¥¹¤¬³ÊǼ¤µ¤ì¤Æ¤¤¤ë¡£
121 .\"O .BR getaddrinfo ()
122 .\"O function combines the functionality provided by the
123 .\"O .\" .BR getipnodebyname (3),
124 .\"O .\" .BR getipnodebyaddr (3),
125 .\"O .BR getservbyname (3)
127 .\"O .BR getservbyport (3)
128 .\"O functions into a single interface, but unlike the latter functions,
129 .\"O .BR getaddrinfo ()
130 .\"O is reentrant and allows programs to eliminate IPv4-versus-IPv6 dependencies.
133 .\" .BR getipnodebyname (3),
134 .\" .BR getipnodebyaddr (3),
135 .BR getservbyname (3)
137 .BR getservbyport (3)
138 ¤Îµ¡Ç½¤ò¤Þ¤È¤á¤Æ°ì¤Ä¤Î¥¤¥ó¥¿¡¼¥Õ¥§¡¼¥¹¤Ë¤·¤¿¤â¤Î¤Ç¤¢¤ë¤¬¡¢
141 ¤Ï¥ê¥¨¥ó¥È¥é¥ó¥È¤Ç¤¢¤ê¡¢
143 ¤ò»È¤¦¤³¤È¤Ç¥×¥í¥°¥é¥à¤Ï IPv4 ¤È IPv6 ¤Î°ã¤¤¤Ë´Ø¤¹¤ë°Í¸´Ø·¸¤ò
148 .\"O structure used by
149 .\"O .BR getaddrinfo ()
150 .\"O contains the following fields:
154 ¹½Â¤ÂΤϰʲ¼¤Î¥Õ¥£¡¼¥ë¥É¤ò´Þ¤à¡£
164 struct sockaddr *ai_addr;
166 struct addrinfo *ai_next;
173 .\"O argument points to an
175 .\"O structure that specifies criteria for selecting the socket address
176 .\"O structures returned in the list pointed to by
180 .\"O is not NULL it points to an
184 .\"O .IR ai_socktype ,
187 .\"O specify criteria that limit the set of socket addresses returned by
188 .\"O .BR getaddrinfo (),
193 ¹½Â¤ÂΤò»Ø¤·¼¨¤·¡¢¤³¤Î¹½Â¤ÂΤòÍѤ¤¤Æ
195 ¤¬»Ø¤¹¥ê¥¹¥È¤ËÆþ¤ì¤ÆÊÖ¤¹¥½¥±¥Ã¥È¥¢¥É¥ì¥¹¹½Â¤ÂΤòÁªÂò¤¹¤ë¤¿¤á¤Î´ð½à¤ò»ØÄꤹ¤ë¡£
201 ¹½Â¤ÂΤò»Ø¤·¼¨¤·¡¢¤½¤Î¹½Â¤ÂΤΥե£¡¼¥ë¥É
207 ¤¬ÊÖ¤¹¥½¥±¥Ã¥È¥¢¥É¥ì¥¹½¸¹ç¤ËÂФ¹¤ë´ð½à¤ò»ØÄꤹ¤ë¡£
210 .\"O This field specifies the desired address family for the returned addresses.
211 .\"O Valid values for this field include
218 .\"O .BR getaddrinfo ()
219 .\"O should return socket addresses for any address family
220 .\"O (either IPv4 or IPv6, for example) that can be used with
224 ¤³¤Î¥Õ¥£¡¼¥ë¥É¤ÏÊÖ¤µ¤ì¤ë¥¢¥É¥ì¥¹¤Î´õ˾¤Î¥¢¥É¥ì¥¹¥Õ¥¡¥ß¥ê¡¼¤ò»ØÄꤹ¤ë¡£
225 ¤³¤Î¥Õ¥£¡¼¥ë¥É¤Ë»ØÄê¤Ç¤¤ë͸ú¤ÊÃͤȤ·¤Æ¤Ï
238 ¤Ç»ÈÍѤǤ¤ë¤¤¤º¤ì¤«¤Î¥¢¥É¥ì¥¹¥Õ¥¡¥ß¥ê¡¼ (Î㤨¤Ð IPv4 ¤« IPv6) ¤Î
239 ¥½¥±¥Ã¥È¥¢¥É¥ì¥¹¤òÊÖ¤¹¤³¤È¤òµá¤á¤é¤ì¤ë¡£
242 .\"O This field specifies the preferred socket type, for example
245 .\"O .BR SOCK_DGRAM .
246 .\"O Specifying 0 in this field indicates that socket addresses of any type
247 .\"O can be returned by
248 .\"O .BR getaddrinfo ().
249 ¤³¤Î¥Õ¥£¡¼¥ë¥É¤Ï¿ä¾©¤Î¥½¥±¥Ã¥È·¿ (Î㤨¤Ð
254 ¤³¤Î¥Õ¥£¡¼¥ë¥É¤Ë 0 ¤ò»ØÄꤹ¤ë¤È¡¢Ç¤°Õ¤Î¥½¥±¥Ã¥È·¿¤Î¥½¥±¥Ã¥È¥¢¥É¥ì¥¹¤ò
256 ¤¬ÊÖ¤·¤Æ¤è¤¤¤³¤È¤ò°ÕÌ£¤¹¤ë¡£
259 .\"O This field specifies the protocol for the returned socket addresses.
260 .\"O Specifying 0 in this field indicates that socket addresses with
261 .\"O any protocol can be returned by
262 .\"O .BR getaddrinfo ().
263 ¤³¤Î¥Õ¥£¡¼¥ë¥É¤ÏÊÖ¤µ¤ì¤ë¥½¥±¥Ã¥È¥¢¥É¥ì¥¹¤Î¥×¥í¥È¥³¥ë¤ò»ØÄꤹ¤ë¡£
264 ¤³¤Î¥Õ¥£¡¼¥ë¥É¤Ë 0 ¤ò»ØÄꤹ¤ë¤È¡¢Ç¤°Õ¤Î¥×¥í¥È¥³¥ë¤Î¤Î¥½¥±¥Ã¥È¥¢¥É¥ì¥¹¤ò
266 ¤¬ÊÖ¤·¤Æ¤è¤¤¤³¤È¤ò°ÕÌ£¤¹¤ë¡£
269 .\"O This field specifies additional options, described below.
270 .\"O Multiple flags are specified by logically OR-ing them together.
271 ¤³¤Î¥Õ¥£¡¼¥ë¥É¤Ï¡¢ÄɲäΥª¥×¥·¥ç¥ó (²¼µ) ¤ò»ØÄꤹ¤ë¡£
272 Ê£¿ô¤Î¥Õ¥é¥°¤ò»ØÄꤹ¤ëºÝ¤Ë¤Ï¡¢¤½¤ì¤é¤ÎÏÀÍý OR ¤ò¤È¤Ã¤Æ»ØÄꤹ¤ë¡£
274 .\"O All the other fields in the structure pointed to by
276 .\"O must contain either 0 or a null pointer, as appropriate.
278 ¤¬»Ø¤·¼¨¤¹¹½Â¤ÂΤξ¤Î¤¹¤Ù¤Æ¤Î¥Õ¥£¡¼¥ë¥É¤Ë¤Ï
279 0 ¤« NULL ¥Ý¥¤¥ó¥¿¤òŬÀÚ¤ËÆþ¤ì¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
282 .\"O as NULL is equivalent to setting
293 .\"O .BR "(AI_V4MAPPED\ |\ AI_ADDRCONFIG)" .
295 ¤Ë NULL ¤ò»ØÄꤹ¤ë¤Î¤Ï¡¢
306 .B "(AI_V4MAPPED\ |\ AI_ADDRCONFIG)"
307 ¤òÀßÄꤹ¤ë¤Î¤ÈÅù²Á¤Ç¤¢¤ë¡£
310 .\"O specifies either a numerical network address
311 .\"O (for IPv4, numbers-and-dots notation as supported by
312 .\"O .BR inet_aton (3);
313 .\"O for IPv6, hexadecimal string format as supported by
314 .\"O .BR inet_pton (3)),
315 .\"O or a network hostname, whose network addresses are looked up and resolved.
317 .\"O .I hints.ai_flags
319 .\"O .B AI_NUMERICHOST
322 .\"O must be a numerical network address.
324 .\"O .B AI_NUMERICHOST
325 .\"O flag suppresses any potentially lengthy network host address lookups.
327 ¤Ë¤Ï¡¢¿ôÃÍ·Á¼°¤Î¥Í¥Ã¥È¥ï¡¼¥¯¥¢¥É¥ì¥¹
330 ¤Ç¥µ¥Ý¡¼¥È¤µ¤ì¤Æ¤¤¤ë¥É¥Ã¥È¶èÀÚ¤ê¤Î¿ô»ú¤Ë¤è¤ëɽµ¡¢
333 ¤Ç¥µ¥Ý¡¼¥È¤µ¤ì¤Æ¤¤¤ë 16 ¿Ê¿ô¤Îʸ»úÎó·Á¼°) ¤â¤·¤¯¤Ï
334 ¥Í¥Ã¥È¥ï¡¼¥¯¥Û¥¹¥È̾¤ò»ØÄꤹ¤ë¡£
335 ¥Í¥Ã¥È¥ï¡¼¥¯¥Û¥¹¥È̾¤ò»ØÄꤷ¤¿¾ì¹ç¤Ë¤Ï¡¢¤½¤Î¥Í¥Ã¥È¥ï¡¼¥¯¥¢¥É¥ì¥¹¤¬¸¡º÷¤µ¤ì¡¢
336 ̾Á°²ò·è¤¬¹Ô¤Ê¤ï¤ì¤ë¡£
340 ¥Õ¥é¥°¤¬´Þ¤Þ¤ì¤Æ¤¤¤ë¾ì¹ç¤Ï¡¢
342 ¤Ï¿ôÃÍ·Á¼°¤Î¥Í¥Ã¥È¥ï¡¼¥¯¥¢¥É¥ì¥¹¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
344 ¥Õ¥é¥°¤ò»È¤¦¤È¡¢»þ´Ö¤Î³Ý¤«¤ë²ÄǽÀ¤Î¤¢¤ë¥Í¥Ã¥È¥ï¡¼¥¯¥Û¥¹¥È¥¢¥É¥ì¥¹¤Î¸¡º÷¤Ï
349 .\"O flag is specified in
350 .\"O .IR hints.ai_flags ,
354 .\"O then the returned socket addresses will be suitable for
356 .\"O a socket that will
359 .\"O The returned socket address will contain the "wildcard address"
360 .\"O .RB ( INADDR_ANY
361 .\"O for IPv4 addresses,
362 .\"O .BR IN6ADDR_ANY_INIT
363 .\"O for IPv6 address).
364 .\"O The wildcard address is used by applications (typically servers)
365 .\"O that intend to accept connections on any of the hosts's network addresses.
368 .\"O is not NULL, then the
370 .\"O flag is ignored.
374 ¥Õ¥é¥°¤¬»ØÄꤵ¤ì¡¢¤«¤Ä
377 ÊÖ¤µ¤ì¤ë¥½¥±¥Ã¥È¥¢¥É¥ì¥¹¤Ï
382 ¤¹¤ë¤Î¤ËŬ¤·¤¿¤â¤Î¤È¤Ê¤ë¡£
383 ÊÖ¤µ¤ì¤ë¥½¥±¥Ã¥È¥¢¥É¥ì¥¹¤Ë¤Ï¡Ö¥ï¥¤¥ë¥É¥«¡¼¥É¡¦¥¢¥É¥ì¥¹¡×
384 (IPv4 ¥¢¥É¥ì¥¹¤Î¾ì¹ç¤Ï
386 IPv6 ¥¢¥É¥ì¥¹¤Î¾ì¹ç¤Ï
387 .BR IN6ADDR_ANY_INIT )
389 ¥ï¥¤¥ë¥É¥«¡¼¥É¡¦¥¢¥É¥ì¥¹¤Ï¡¢Ç¤°Õ¤Î¥Û¥¹¥È¤Î¥Í¥Ã¥È¥ï¡¼¥¯¥¢¥É¥ì¥¹¤ÇÀܳ¤ò
390 ¼õ¤±ÉÕ¤±¤è¤¦¤È¤¹¤ë¥¢¥×¥ê¥±¡¼¥·¥ç¥ó (Ä̾ï¤Ï¥µ¡¼¥Ð¡¼) ¤ÇÍѤ¤¤é¤ì¤ë¡£
398 .\"O flag is not set in
399 .\"O .IR hints.ai_flags ,
400 .\"O then the returned socket addresses will be suitable for use with
401 .\"O .BR connect (2),
404 .\"O .BR sendmsg (2).
408 .\"O then the network address will be set to the loopback interface address
409 .\"O .RB ( INADDR_LOOPBACK
410 .\"O for IPv4 addresses,
411 .\"O .BR IN6ADDR_LOOPBACK_INIT
412 .\"O for IPv6 address);
413 .\"O this is used by applications that intend to communicate
414 .\"O with peers running on the same host.
418 ¥Õ¥é¥°¤¬¥»¥Ã¥È¤µ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¡¢
419 ÊÖ¤µ¤ì¤ë¥½¥±¥Ã¥È¥¢¥É¥ì¥¹¤Ï
423 ¤Ç¤Î»ÈÍѤËŬ¤·¤¿¤â¤Î¤È¤Ê¤ë¡£
425 ¤¬ NULL ¤Î¾ì¹ç¡¢¥Í¥Ã¥È¥ï¡¼¥¯¥¢¥É¥ì¥¹¤Ë¤Ï¥ë¡¼¥×¥Ð¥Ã¥¯¡¦¥¤¥ó¥¿¡¼¥Õ¥§¥¤¥¹¤Î
426 ¥¢¥É¥ì¥¹ (IPv4 ¥¢¥É¥ì¥¹¤Î¾ì¹ç¤Ï
428 IPv6 ¥¢¥É¥ì¥¹¤Î¾ì¹ç¤Ï
429 .BR IN6ADDR_LOOPBACK_INIT ) ¤¬ÀßÄꤵ¤ì¤ë¡£
430 ¤³¤ì¤ÏƱ¤¸¥Û¥¹¥È¾å¤ÇÆ°ºî¤·¤Æ¤¤¤ëÀܳÁê¼ê¤ÈÄÌ¿®¤¹¤ë¤è¤¦¤Ê
431 ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤ÇÍѤ¤¤é¤ì¤ë¡£
434 .\"O sets the port in each returned address structure.
435 .\"O If this argument is a service name (see
436 .\"O .BR services (5)),
437 .\"O it is translated to the corresponding port number.
438 .\"O This argument can also be specified as a decimal number,
439 .\"O which is simply converted to binary.
441 ¤Ë¤è¤ê¡¢ÊÖ¤µ¤ì¤ë³Æ¥¢¥É¥ì¥¹¹½Â¤ÂΤΥݡ¼¥ÈÈֹ椬·è¤Þ¤ë¡£
442 ¤³¤Î°ú¤¿ô¤¬¥µ¡¼¥Ó¥¹Ì¾
444 »²¾È) ¤Î¾ì¹ç¡¢Âбþ¤¹¤ë¥Ý¡¼¥ÈÈÖ¹æ¤ËËÝÌõ¤µ¤ì¤ë¡£
445 ¤³¤Î°ú¤¿ô¤Ë¤Ï 10 ¿Ê¿ô¤â»ØÄꤹ¤ë¤³¤È¤¬¤Ç¤¡¢
446 ¤³¤Î¾ì¹ç¤Ë¤Ï¥Ð¥¤¥Ê¥ê¤Ø¤ÎÊÑ´¹¤À¤±¤¬¹Ô¤ï¤ì¤ë¡£
449 .\"O is NULL, then the port number of the returned socket addresses
450 .\"O will be left uninitialized.
452 ¤¬ NULL ¤Î¾ì¹ç¡¢ÊÖ¤µ¤ì¤ë¥½¥±¥Ã¥È¥¢¥É¥ì¥¹¤Î¥Ý¡¼¥ÈÈÖ¹æ¤Ï
453 ½é´ü²½¤µ¤ì¤Ê¤¤¤Þ¤Þ¤È¤Ê¤ë¡£
455 .\"O .B AI_NUMERICSERV
457 .\"O .I hints.ai_flags
460 .\"O is not NULL, then
462 .\"O must point to a string containing a numeric port number.
470 ¤Ï¿ôÃͤΥݡ¼¥ÈÈÖ¹æ¤ò´Þ¤àʸ»úÎó¤ò»Ø¤·¼¨¤µ¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
471 .\"O This flag is used to inhibit the invocation of a name resolution service
472 .\"O in cases where it is known not to be required.
473 ¤³¤Î¥Õ¥é¥°¤Ï¡¢Ì¾Á°²ò·è¥µ¡¼¥Ó¥¹¤¬ÉÔÍפǤ¢¤ë¤³¤È¤¬Ê¬¤«¤Ã¤Æ¤¤¤ë¾ì¹ç¤Ë¡¢
474 ¥µ¡¼¥Ó¥¹¤Îµ¯Æ°¤òÍÞÀ©¤¹¤ë¤¿¤á¤ËÍѤ¤¤é¤ì¤ë¡£
480 .\"O but not both, may be NULL.
484 ¤Î¤É¤Á¤é¤«¤Ï NULL ¤Ë¤·¤Æ¤è¤¤¤¬¡¢Î¾ÊýƱ»þ¤Ë NULL ¤Ë¤·¤Æ¤Ï¤Ê¤é¤Ê¤¤¡£
487 .\"O .BR getaddrinfo ()
488 .\"O function allocates and initializes a linked list of
490 .\"O structures, one for each network address that matches
494 .\"O subject to any restrictions imposed by
496 .\"O and returns a pointer to the start of the list in
498 .\"O The items in the linked list are linked by the
504 ¹½Â¤ÂΤΥá¥â¥ê³ÎÊݤò¹Ô¤¤¡¢
506 ¹½Â¤ÂΤΥê¥ó¥¯¥ê¥¹¥È¤ò½é´ü²½¤·¡¢
508 ¤Ë¥ê¥¹¥È¤ÎÀèƬ¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÆþ¤ì¤ÆÊÖ¤¹¡£
509 ¤³¤Î¤È¤¡¢³Æ¹½Â¤ÂΤΥͥåȥ¥¯¥¢¥É¥ì¥¹¤Ï
515 ¤Ç²Ý¤µ¤ì¤¿¤¹¤Ù¤Æ¤ÎÀ©¸Â¤òËþ¤¿¤¹¤â¤Î¤È¤Ê¤ë¡£
518 ¥Õ¥£¡¼¥ë¥É¤Ë¤è¤êÏ¢·ë¤µ¤ì¤ë¡£
520 .\"O There are several reasons why
521 .\"O the linked list may have more than one
523 .\"O structure, including: the network host is multihomed, accessible
524 .\"O over multiple protocols (e.g. both
528 .\"O or the same service is available from multiple socket types (one
530 .\"O address and another
532 .\"O address, for example).
535 ¹½Â¤ÂΤÏÊ£¿ô¸Ä¤Ë¤Ê¤ë¤³¤È¤â¤¢¤ê¡¢¤½¤ÎÍýͳ¤Ï¤¤¤¯¤Ä¤«¤¢¤ë¡£
536 ¥Í¥Ã¥È¥ï¡¼¥¯¥Û¥¹¥È¤¬¥Þ¥ë¥Á¥Û¡¼¥à¤Ç¤¢¤ë¡¢
537 Ê£¿ô¤Î¥×¥í¥È¥³¥ë¤Ç¥¢¥¯¥»¥¹¤Ç¤¤ë (Î㤨¤Ð
542 Ê£¿ô¤Î¥½¥±¥Ã¥È¼ïÊ̤ÇƱ¤¸¥µ¡¼¥Ó¥¹¤¬ÍøÍѤǤ¤ë
545 ¥¢¥É¥ì¥¹¤Ç¡¢¤â¤¦¤Ò¤È¤Ä¤¬
547 ¥¢¥É¥ì¥¹¤Ç¤¢¤ë)¡¢¤¬¤¢¤ë¡£
548 .\"O Normally, the application should try
549 .\"O using the addresses in the order in which they are returned.
550 .\"O The sorting function used within
551 .\"O .BR getaddrinfo ()
552 .\"O is defined in RFC\ 3484; the order can be tweaked for a particular
553 .\"O system by editing
554 .\"O .IR /etc/gai.conf
555 .\"O (available since glibc 2.5).
556 Ä̾ï¤Ï¡¢¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤ÏÊÖ¤µ¤ì¤¿½ç½ø¤Ç¥¢¥É¥ì¥¹¤ò»î¤¹¤Ù¤¤Ç¤¢¤ë¡£
558 ¤ÎÃæ¤Ç»ÈÍѤµ¤ì¤ëʤÙÂؤ¨´Ø¿ô¤Ï RFC\ 3484 ¤ÇÄêµÁ¤µ¤ì¤Æ¤¤¤ë¡£
561 ¤òÊÔ½¸¤¹¤ë¤³¤È¤Ç¡¢¤³¤Î½ç½ø¤òÈùÄ´À°¤¹¤ë¤³¤È¤¬¤Ç¤¤ë
563 ¤Ï glibc 2.5 °Ê¹ß¤ÇÍøÍѤǤ¤ë)¡£
566 .\"O .I hints.ai_flags
571 .\"O field of the first of the
573 .\"O structures in the returned list is set to point to the
574 .\"O official name of the host.
578 ¥Õ¥é¥°¤¬´Þ¤Þ¤ì¤Æ¤¤¤ë¾ì¹ç¡¢ÊÖ¤µ¤ì¤ë¥ê¥¹¥È¤ÎºÇ½é¤Î
582 ¥Õ¥£¡¼¥ë¥É¤Ï¥Û¥¹¥È¤Î¸ø¼°¤Ê̾Á°¤ò»Ø¤¹¤è¤¦¤ËÀßÄꤵ¤ì¤ë¡£
583 .\" In glibc prior to 2.3.4, the ai_canonname of each addrinfo
584 .\" structure was set pointing to the canonical name; that was
585 .\" more than POSIX.1-2001 specified, or other implementations provided.
588 .\"O The remaining fields of each returned
590 .\"O structure are initialized as follows:
593 ¹½Â¤ÂΤλĤê¤Î¥Õ¥£¡¼¥ë¥É¤Ï°Ê²¼¤Î¤è¤¦¤Ë½é´ü²½¤µ¤ì¤ë¡£
597 .\"O .IR ai_socktype ,
600 .\"O fields return the socket creation parameters (i.e., these fields have
601 .\"O the same meaning as the corresponding arguments of
602 .\"O .BR socket (2)).
606 ¥Õ¥£¡¼¥ë¥É¤Ï¥½¥±¥Ã¥ÈÀ¸À®¥Ñ¥é¥á¡¼¥¿¤òÊÖ¤¹
607 (¤³¤ì¤é¤Î¥Õ¥£¡¼¥ë¥É¤Î°ÕÌ£¤Ï
609 ¤ÎƱ¤¸Ì¾Á°¤Î°ú¤¿ô¤ÈƱ¤¸¤Ç¤¢¤ë)¡£
620 .\"O .BR SOCK_STREAM ;
623 .\"O returns the protocol for the socket.
638 ¤Ï¤½¤Î¥½¥±¥Ã¥È¤Î¥×¥í¥È¥³¥ë¤òÊÖ¤¹¡£
640 .\"O A pointer to the socket address is placed in the
642 .\"O field, and the length of the socket address, in bytes,
643 .\"O is placed in the
647 ¥Õ¥£¡¼¥ë¥É¤Ë¤Ï¥½¥±¥Ã¥È¥¢¥É¥ì¥¹¤Ø¤Î¥Ý¥¤¥ó¥¿¤¬½ñ¤¹þ¤Þ¤ì¡¢
649 ¥Õ¥£¡¼¥ë¥É¤Ë¤Ï¥½¥±¥Ã¥È¥¢¥É¥ì¥¹¤ÎŤµ¤¬¥Ð¥¤¥Èñ°Ì¤Ç½ñ¤¹þ¤Þ¤ì¤ë¡£
652 .\"O .I hints.ai_flags
654 .\"O .B AI_ADDRCONFIG
655 .\"O flag, then IPv4 addresses are returned in the list pointed to by
657 .\"O only if the local system has at least one
658 .\"O IPv4 address configured, and IPv6 addresses are only returned
659 .\"O if the local system has at least one IPv6 address configured.
666 ¥í¡¼¥«¥ë¥·¥¹¥Æ¥à¤ËºÇÄã°ì¤Ä¤Î IPv4 ¥¢¥É¥ì¥¹¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¾ì¹ç¤Ï
667 IPv4 ¥¢¥É¥ì¥¹¤¬ÊÖ¤µ¤ì¡¢
668 ¥í¡¼¥«¥ë¥·¥¹¥Æ¥à¤ËºÇÄã°ì¤Ä¤Î IPv6 ¥¢¥É¥ì¥¹¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¾ì¹ç¤Ï
669 IPv6 ¥¢¥É¥ì¥¹¤¬ÊÖ¤µ¤ì¤ë¡£
672 .\"O .I hint.ai_flags
676 .\"O .I hints.ai_family
677 .\"O was specified as
679 .\"O and no matching IPv6 addresses could be found,
680 .\"O then return IPv4-mapped IPv6 addresses in the list pointed to by
690 ¥Þ¥Ã¥Á¤¹¤ë IPv6 ¥¢¥É¥ì¥¹¤¬¸«¤Ä¤«¤é¤Ê¤«¤Ã¤¿¾ì¹ç¡¢
692 ¤¬»Ø¤¹¥ê¥¹¥È¤Ë¤Ï IPv4-mapped IPv6 ¥¢¥É¥ì¥¹¤¬ÊÖ¤µ¤ì¤ë¡£
697 .\"O are specified in
698 .\"O .IR hints.ai_family ,
699 .\"O then return both IPv6 and IPv4-mapped IPv6 addresses
700 .\"O in the list pointed to by
707 ¤ÎξÊý¤¬»ØÄꤵ¤ì¤Æ¤¤¤ë¾ì¹ç¡¢
709 ¤¬»Ø¤¹¥ê¥¹¥È¤Ë¤Ï IPv6 ¥¢¥É¥ì¥¹¤È IPv4-mapped IPv6 ¥¢¥É¥ì¥¹¤Î
714 .\"O is not also specified.
716 ¤¬»ØÄꤵ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¡¢
721 .\"O .BR freeaddrinfo ()
722 .\"O function frees the memory that was allocated
723 .\"O for the dynamically allocated linked list
729 ¤ËÂФ·¤ÆưŪ¤Ë³ä¤êÅö¤Æ¤é¤ì¤¿¥á¥â¥ê¤ò²òÊü¤¹¤ë¡£
730 .\"O .SS "Extensions to getaddrinfo() for Internationalized Domain Names"
731 .SS "¹ñºÝ²½¥É¥á¥¤¥ó̾¤Î¤¿¤á¤Î getaddrinfo() ¤Î³ÈÄ¥"
733 .\"O Starting with glibc 2.3.4,
734 .\"O .BR getaddrinfo ()
735 .\"O has been extended to selectively allow the incoming and outgoing
736 .\"O hostnames to be transparently converted to and from the
737 .\"O Internationalized Domain Name (IDN) format (see RFC 3490,
738 .\"O .IR "Internationalizing Domain Names in Applications (IDNA)" ).
739 .\"O Four new flags are defined:
742 ¤ÏÆþ½ÐÎϤ¹¤ë¥Û¥¹¥È̾¤òÆ©²áŪ¤Ë¹ñºÝ²½¥É¥á¥¤¥ó̾ (IDN) ·Á¼° (RFC 3490 ¤Î
743 .I "Internationalizing Domain Names in Applications (IDNA)"
744 ¤ò»²¾È¤Î¤³¤È) ¤ÈÊÑ´¹¤¹¤ë¤³¤È¤òÁªÂòŪ¤Ëǧ¤á¤ë¤è¤¦¤Ë³ÈÄ¥¤µ¤ì¤Æ¤¤¤ë¡£
745 4 ¤Ä¤Î¿·¤·¤¤¥Õ¥é¥°¤¬ÄêµÁ¤µ¤ì¤Æ¤¤¤ë:
748 .\"O If this flag is specified, then the node name given in
750 .\"O is converted to IDN format if necessary.
751 .\"O The source encoding is that of the current locale.
752 ¤³¤Î¥Õ¥é¥°¤¬»ØÄꤵ¤ì¤ë¤È¡¢
754 ¤ÇÍ¿¤¨¤é¤ì¤¿¥Î¡¼¥É̾¤ÏɬÍפ¬¤¢¤ì¤Ð IDN ·Á¼°¤ËÊÑ´¹¤µ¤ì¤ë¡£
755 ¥½¡¼¥¹Éä¹æ²½·Á¼°¤Ï¸½ºß¤Î¥í¥±¡¼¥ë¤Î¤â¤Î¤Ç¤¢¤ë¡£
757 .\"O If the input name contains non-ASCII characters, then the IDN encoding
759 .\"O Those parts of the node name (delimited by dots) that contain
760 .\"O non-ASCII characters are encoded using ASCII Compatible Encoding (ACE)
761 .\"O before being passed to the name resolution functions.
762 ÆþÎÏ̾¤ËÈó ASCII ʸ»ú¤¬´Þ¤Þ¤ì¤Æ¤¤¤ë¾ì¹ç¡¢
763 IDN Éä¹æ²½·Á¼°¤¬»È¤ï¤ì¤ë¡£
764 Èó ASCII ʸ»ú¤¬´Þ¤Þ¤ì¤Æ¤¤¤ë(¥Ô¥ê¥ª¥É¤Ç¶èÀÚ¤é¤ì¤ë)Éôʬ¥Î¡¼¥É̾¤Ï¡¢
765 ̾Á°²ò·èµ¡Ç½¤ËÅϤµ¤ì¤ëÁ°¤Ë ASCII ¸ß´¹Éä¹æ²½·Á¼° (ACE) ¤ò»È¤Ã¤Æ
767 .\" Implementation Detail:
768 .\" To minimize effects on system performance the implementation might
769 .\" want to check whether the input string contains any non-ASCII
770 .\" characters. If there are none the IDN step can be skipped completely.
771 .\" On systems which allow not-ASCII safe encodings for a locale this
772 .\" might be a problem.
775 .\"O After a successful name lookup, and if the
777 .\"O flag was specified,
778 .\"O .BR getaddrinfo ()
779 .\"O will return the canonical name of the
780 .\"O node corresponding to the
782 .\"O structure value passed back.
783 .\"O The return value is an exact copy of the value returned by the name
784 .\"O resolution function.
786 ¤¬»ØÄꤵ¤ì¤Æ¤¤¤ë¾ì¹ç¡¢
788 ¤Ï̾Á°¤Î¸¡º÷¤ËÀ®¸ù¤·¤¿¸å¡¢
791 ¹½Â¤ÂΤËÂбþ¤¹¤ë¥Î¡¼¥É¤ÎÀµµ¬Ì¾¤òÊÖ¤¹¡£
792 ÊÖ¤êÃͤÏ̾Á°²ò·èµ¡Ç½¤«¤éÊÖ¤µ¤ì¤¿ÃͤÎÀµ³Î¤Ê¥³¥Ô¡¼¤Ç¤¢¤ë¡£
794 .\"O If the name is encoded using ACE, then it will contain the
796 .\"O prefix for one or more components of the name.
797 .\"O To convert these components into a readable form the
799 .\"O flag can be passed in addition to
800 .\"O .BR AI_CANONNAME .
801 .\"O The resulting string is encoded using the current locale's encoding.
802 ̾Á°¤¬ ACE ¤ÇÉä¹æ²½¤µ¤ì¤Æ¤¤¤ë¾ì¹ç¡¢°ì¤Ä¤Þ¤¿¤ÏÊ£¿ô¤Î̾Á°¤Î¹½À®Í×ÁǤÎÀèƬ¤Ë
805 ¤³¤ì¤é¤Î¹½À®Í×ÁǤòÆɤ߹þ¤ß²Äǽ¤Ê·Á¤ËÊÑ´¹¤¹¤ë¤¿¤á¤Ë¡¢
809 ¥Õ¥é¥°¤òÅϤ¹¤³¤È¤â½ÐÍè¤ë¡£
810 ÊÖ¤µ¤ì¤ëʸ»úÎó¤Ï¸½ºß¤Î¥í¥±¡¼¥ë¤ÎÉä¹æ²½·Á¼°¤ÇÉä¹æ²½¤µ¤ì¤Æ¤¤¤ë¡£
812 .\"Implementation Detail:
813 .\"If no component of the returned name starts with xn\-\- the IDN
814 .\"step can be skipped, therefore avoiding unnecessary slowdowns.
816 .BR AI_IDN_ALLOW_UNASSIGNED ", " AI_IDN_USE_STD3_ASCII_RULES
817 .\"O Setting these flags will enable the
818 .\"O IDNA_ALLOW_UNASSIGNED (allow unassigned Unicode code points) and
819 .\"O IDNA_USE_STD3_ASCII_RULES (check output to make sure it is a STD3
820 .\"O conforming hostname)
821 .\"O flags respectively to be used in the IDNA handling.
822 ¤³¤ì¤é¤Î¥Õ¥é¥°¤¬ÀßÄꤵ¤ì¤ë¤È¡¢
823 IDNA ¥Ï¥ó¥É¥ê¥ó¥°¤ò»È¤¦¤È¤¤Ë¤½¤ì¤¾¤ì
824 IDNA_ALLOW_UNASSIGNED (Èó³ä¤êÅö¤Æ Unicode ¥³¡¼¥É¥Ý¥¤¥ó¥È¤òµö²Ä¤¹¤ë) ¤È
825 IDNA_USE_STD3_ASCII_RULES (½ÐÎϤ¬ STD3 ½àµò¥Û¥¹¥È̾¤Ç¤¢¤ë¤³¤È¤ò³Îǧ¤¹¤ë)
828 .\"O .SH "RETURN VALUE"
830 .\" FIXME glibc defines the following additional errors, some which
831 .\" can probably be returned by getaddrinfo(); they need to
834 .\" #define EAI_INPROGRESS -100 /* Processing request in progress. */
835 .\" #define EAI_CANCELED -101 /* Request canceled. */
836 .\" #define EAI_NOTCANCELED -102 /* Request not canceled. */
837 .\" #define EAI_ALLDONE -103 /* All requests done. */
838 .\" #define EAI_INTR -104 /* Interrupted by a signal. */
839 .\" #define EAI_IDN_ENCODE -105 /* IDN encoding failed. */
841 .\"O .BR getaddrinfo ()
842 .\"O returns 0 if it succeeds, or one of the following nonzero error codes:
844 ¤ÏÀ®¸ù¤¹¤ë¤È 0 ¤òÊÖ¤·¡¢¼ºÇÔ¤¹¤ë¤È°Ê²¼¤ÎÈó 0 ¤Î¥¨¥é¡¼¥³¡¼¥É¤Î¤¤¤º¤ì¤«¤òÊÖ¤¹¡£
848 .\"O The specified network host does not have any network addresses in the
849 .\"O requested address family.
850 »ØÄꤵ¤ì¤¿¥Í¥Ã¥È¥ï¡¼¥¯¥Û¥¹¥È¤Ë¤Ï¡¢
851 Í׵ᤵ¤ì¤¿¥¢¥É¥ì¥¹¥Õ¥¡¥ß¥ê¡¼¤Î¥Í¥Ã¥È¥ï¡¼¥¯¥¢¥É¥ì¥¹¤¬¤Ê¤¤¡£
854 .\"O The name server returned a temporary failure indication.
855 .\"O Try again later.
856 ¥Í¡¼¥à¥µ¡¼¥Ð¡¼¤«¤é°ì»þŪ¤Ê¼ºÇÔ (temporary failure)
857 ¤ò°ÕÌ£¤¹¤ëÊÖ»ö¤¬ÊÖ¤µ¤ì¤¿¡£¸å¤Ç¤â¤¦°ìÅٻ¤Æ¤ß¤è¡£
860 .\"O .I hints.ai_flags
861 .\"O contains invalid flags; or,
862 .\"O .I hints.ai_flags
869 ¤Î¥Õ¥é¥°¤ËÉÔÀµ¤Ê¥Õ¥é¥°¤¬´Þ¤Þ¤ì¤Æ¤¤¤ë¡£¤Þ¤¿¤Ï¡¢
878 .\"O The name server returned a permanent failure indication.
879 ¥Í¡¼¥à¥µ¡¼¥Ð¡¼¤«¤é¹±µ×Ū¤Ê¼ºÇÔ (permanent failure)
880 ¤ò°ÕÌ£¤¹¤ëÊÖ»ö¤¬ÊÖ¤µ¤ì¤¿¡£
883 .\"O The requested address family is not supported.
884 Í׵ᤵ¤ì¤¿¥¢¥É¥ì¥¹¥Õ¥¡¥ß¥ê¡¼¤¬¥µ¥Ý¡¼¥È¤µ¤ì¤Æ¤¤¤Ê¤¤¡£
892 .\"O The specified network host exists, but does not have any
893 .\"O network addresses defined.
894 »ØÄꤵ¤ì¤¿¥Í¥Ã¥È¥ï¡¼¥¯¥Û¥¹¥È¤Ï¸ºß¤¹¤ë¤¬¡¢
895 ¥Í¥Ã¥È¥ï¡¼¥¯¥¢¥É¥ì¥¹¤¬¤Ò¤È¤Ä¤âÄêµÁ¤µ¤ì¤Æ¤¤¤Ê¤¤¡£
902 .\"O is not known; or both
907 .\"O .B AI_NUMERICSERV
908 .\"O was specified in
909 .\"O .I hints.ai_flags
912 .\"O was not a numeric port-number string.
916 ¤Î¤É¤Á¤é¤«¤¬ÉÔÌÀ¡¢¤Þ¤¿¤Ï
920 ¤ÎξÊý¤¬ NULL ¤À¤Ã¤¿¾ì¹ç¡¢¤Þ¤¿¤Ï
928 ¤¬¿ôÃͤΥݡ¼¥ÈÈÖ¹æ¤Îʸ»úÎó¤Ç¤Ê¤¤¡£
931 .\"O The requested service is not available for the requested socket type.
932 .\"O It may be available through another socket type.
933 Í׵ᤵ¤ì¤¿¥µ¡¼¥Ó¥¹¤Ï¡¢Í׵ᤵ¤ì¤¿¥½¥±¥Ã¥È¥¿¥¤¥×¤Ç¤ÏÍøÍѤǤ¤Ê¤¤¡£
934 ¾¤Î¥½¥±¥Ã¥È¥¿¥¤¥×¤Ç¤Ê¤éÍøÍѲÄǽ¤«¤â¤·¤ì¤Ê¤¤¡£
935 .\"O For example, this error could occur if
937 .\"O was "shell" (a service only available on stream sockets), and either
938 .\"O .I hints.ai_protocol
940 .\"O .BR IPPROTO_UDP ,
942 .\"O .I hints.ai_socktype
944 .\"O .BR SOCK_DGRAM ;
945 .\"O or the error could occur if
947 .\"O was not NULL, and
948 .\"O .I hints.ai_socktype
951 .\"O (a socket type that does not support the concept of services).
952 ¤³¤Î¥¨¥é¡¼¤¬È¯À¸¤¹¤ëÎã¤È¤·¤Æ¤Ï¡¢
954 ¤¬ "shell" (¥¹¥È¥ê¡¼¥à¡¦¥½¥±¥Ã¥È¤Ç¤Î¤ßÍøÍѤǤ¤ë¥µ¡¼¥Ó¥¹) ¤Ç¡¢
962 ¤¬»ØÄꤵ¤ì¤¿¤ê¤·¤¿¾ì¹ç¤¬¤¢¤ë¡£
969 (¥µ¡¼¥Ó¥¹¤Î¹Í¤¨Êý¤ò¥µ¥Ý¡¼¥È¤·¤Æ¤¤¤Ê¤¤¥½¥±¥Ã¥È¼ïÊÌ)
970 ¤¬»ØÄꤵ¤ì¤¿¾ì¹ç¤Ë¤â¡¢¤³¤Î¥¨¥é¡¼¤¬È¯À¸¤¹¤ë¡£
973 .\"O The requested socket type is not supported.
974 Í׵ᤵ¤ì¤¿¥½¥±¥Ã¥È¥¿¥¤¥×¤¬¥µ¥Ý¡¼¥È¤µ¤ì¤Æ¤¤¤Ê¤¤¡£
975 .\"O This could occur, for example, if
976 .\"O .I hints.ai_socktype
978 .\"O .I hints.ai_protocol
979 .\"O are inconsistent (e.g.,
982 .\"O .BR IPPROTO_TCP ,
984 ¤³¤Î¥¨¥é¡¼¤¬È¯À¸¤¹¤ëÎã¤È¤·¤Æ¤Ï¡¢
988 ¤¬Ì·½â¤·¤Æ¤¤¤ë¾ì¹ç (Î㤨¤Ð
999 .\"O Other system error, check
1002 ¤½¤Î¾¤Î¥·¥¹¥Æ¥à¥¨¥é¡¼¡£¾Ü¤·¤¯¤Ï
1007 .\"O .BR gai_strerror ()
1008 .\"O function translates these error codes to a human readable string,
1009 .\"O suitable for error reporting.
1011 ´Ø¿ô¤òÍѤ¤¤ë¤È¡¢¤³¤ì¤é¤Î¥¨¥é¡¼¥³¡¼¥É¤ò¿Í´Ö¤Ë²ÄÆɤÊʸ»úÎó¤ËÊÑ´¹¤Ç¤¤ë¤Î¤Ç¡¢
1012 ¥¨¥é¡¼Êó¹ð¤ËŬ¤¹¤ë¤À¤í¤¦¡£
1016 .\"O .SH "CONFORMING TO"
1020 .\"O .BR getaddrinfo ()
1021 .\"O function is documented in RFC\ 2553.
1024 ´Ø¿ô¤Ï RFC 2553 ¤ËµºÜ¤µ¤ì¤Æ¤¤¤ë¡£
1027 .\"O .BR getaddrinfo ()
1029 .\"O .IB address % scope-id
1030 .\"O notation for specifying the IPv6 scope-ID.
1032 ¤Ï¡¢IPv6 scope-ID ¤ò»ØÄꤹ¤ë¤¿¤á¤Ë
1033 .IB address % scope-id
1034 µË¡¤ò¥µ¥Ý¡¼¥È¤·¤Æ¤¤¤ë¡£
1036 .\"O .BR AI_ADDRCONFIG ,
1040 .\"O are available since glibc 2.3.3.
1041 .\"O .B AI_NUMERICSERV
1042 .\"O is available since glibc 2.3.4.
1046 ¤Ï glibc 2.3.3 °Ê¹ß¤ÇÍøÍѲÄǽ¤Ç¤¢¤ë¡£
1048 ¤Ï glibc 2.3.4 °Ê¹ß¤ÇÍøÍѲÄǽ¤Ç¤¢¤ë¡£
1050 .\"O According to POSIX.1-2001, specifying
1052 .\"O as NULL should cause
1054 .\"O to be assumed as 0.
1055 .\"O The GNU C library instead assumes a value of
1056 .\"O .BR "(AI_V4MAPPED\ |\ AI_ADDRCONFIG)"
1058 .\"O since this value is considered an improvement on the specification.
1059 POSIX.1-2001 ¤Ë¤è¤ë¤È¡¢
1061 ¤Ë NULL ¤¬»ØÄꤵ¤ì¤¿¾ì¹ç¡¢
1063 ¤ò 0 ¤È¤ß¤Ê¤¹¤Ù¤¤È¤µ¤ì¤Æ¤¤¤ë¡£
1064 GNU C ¥é¥¤¥Ö¥é¥ê¤Ç¤Ï¡¢¤³¤Î¾ì¹ç¤Ë¡¢Âå¤ï¤ê¤Ë
1067 .BR "(AI_V4MAPPED\ |\ AI_ADDRCONFIG)"
1068 ¤È¤ß¤Ê¤¹¤è¤¦¤Ë¤Ê¤Ã¤Æ¤¤¤ë¡£
1069 ¤³¤ÎÃͤÎÊý¤¬É¸½àµ¬³Ê¤Î²þÁ±¤Ë¤Ê¤ë¤È¹Í¤¨¤é¤ì¤Æ¤¤¤ë¤«¤é¤Ç¤¢¤ë¡£
1072 .\" getnameinfo.3 refers to this example
1073 .\" socket.2 refers to this example
1074 .\" bind.2 refers to this example
1075 .\" connect.2 refers to this example
1076 .\" recvfrom.2 refers to this example
1077 .\" sendto.2 refers to this example
1078 .\"O The following programs demonstrate the use of
1079 .\"O .BR getaddrinfo (),
1080 .\"O .BR gai_strerror (),
1081 .\"O .BR freeaddrinfo (),
1083 .\"O .BR getnameinfo (3).
1084 .\"O The programs are an echo server and client for UDP datagrams.
1085 °Ê²¼¤Î¥×¥í¥°¥é¥à¤Ï¡¢
1087 .BR gai_strerror (),
1088 .BR freeaddrinfo (),
1090 ¤Î»È¤¤Êý¤ò¼¨¤·¤¿¤â¤Î¤Ç¤¢¤ë¡£
1091 ¥×¥í¥°¥é¥à¤Ï UDP ¥Ç¡¼¥¿¥°¥é¥à¤Î echo ¥µ¡¼¥Ð¤È¥¯¥é¥¤¥¢¥ó¥È¤Ç¤¢¤ë¡£
1092 .\"O .SS Server program
1093 .SS ¥µ¡¼¥Ð¤Î¥×¥í¥°¥é¥à
1096 #include <sys/types.h>
1101 #include <sys/socket.h>
1104 #define BUF_SIZE 500
1107 main(int argc, char *argv[])
1109 struct addrinfo hints;
1110 struct addrinfo *result, *rp;
1112 struct sockaddr_storage peer_addr;
1113 socklen_t peer_addr_len;
1118 fprintf(stderr, "Usage: %s port\\n", argv[0]);
1122 memset(&hints, 0, sizeof(struct addrinfo));
1123 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
1124 hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
1125 hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
1126 hints.ai_protocol = 0; /* Any protocol */
1127 hints.ai_canonname = NULL;
1128 hints.ai_addr = NULL;
1129 hints.ai_next = NULL;
1131 s = getaddrinfo(NULL, argv[1], &hints, &result);
1133 fprintf(stderr, "getaddrinfo: %s\\n", gai_strerror(s));
1137 /* getaddrinfo() returns a list of address structures.
1138 Try each address until we successfully bind(2).
1139 If socket(2) (or bind(2)) fails, we (close the socket
1140 and) try the next address. */
1142 for (rp = result; rp != NULL; rp = rp\->ai_next) {
1143 sfd = socket(rp\->ai_family, rp\->ai_socktype,
1148 if (bind(sfd, rp\->ai_addr, rp\->ai_addrlen) == 0)
1149 break; /* Success */
1154 if (rp == NULL) { /* No address succeeded */
1155 fprintf(stderr, "Could not bind\\n");
1159 freeaddrinfo(result); /* No longer needed */
1161 /* Read datagrams and echo them back to sender */
1164 peer_addr_len = sizeof(struct sockaddr_storage);
1165 nread = recvfrom(sfd, buf, BUF_SIZE, 0,
1166 (struct sockaddr *) &peer_addr, &peer_addr_len);
1168 continue; /* Ignore failed request */
1170 char host[NI_MAXHOST], service[NI_MAXSERV];
1172 s = getnameinfo((struct sockaddr *) &peer_addr,
1173 peer_addr_len, host, NI_MAXHOST,
1174 service, NI_MAXSERV, NI_NUMERICSERV);
1176 printf("Received %ld bytes from %s:%s\\n",
1177 (long) nread, host, service);
1179 fprintf(stderr, "getnameinfo: %s\\n", gai_strerror(s));
1181 if (sendto(sfd, buf, nread, 0,
1182 (struct sockaddr *) &peer_addr,
1183 peer_addr_len) != nread)
1184 fprintf(stderr, "Error sending response\\n");
1188 .\"O .SS Client program
1189 .SS ¥¯¥é¥¤¥¢¥ó¥È¤Î¥×¥í¥°¥é¥à
1192 #include <sys/types.h>
1193 #include <sys/socket.h>
1200 #define BUF_SIZE 500
1203 main(int argc, char *argv[])
1205 struct addrinfo hints;
1206 struct addrinfo *result, *rp;
1213 fprintf(stderr, "Usage: %s host port msg...\\n", argv[0]);
1217 /* Obtain address(es) matching host/port */
1219 memset(&hints, 0, sizeof(struct addrinfo));
1220 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
1221 hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
1223 hints.ai_protocol = 0; /* Any protocol */
1225 s = getaddrinfo(argv[1], argv[2], &hints, &result);
1227 fprintf(stderr, "getaddrinfo: %s\\n", gai_strerror(s));
1231 /* getaddrinfo() returns a list of address structures.
1232 Try each address until we successfully connect(2).
1233 If socket(2) (or connect(2)) fails, we (close the socket
1234 and) try the next address. */
1236 for (rp = result; rp != NULL; rp = rp\->ai_next) {
1237 sfd = socket(rp\->ai_family, rp\->ai_socktype,
1242 if (connect(sfd, rp\->ai_addr, rp\->ai_addrlen) != \-1)
1243 break; /* Success */
1248 if (rp == NULL) { /* No address succeeded */
1249 fprintf(stderr, "Could not connect\\n");
1253 freeaddrinfo(result); /* No longer needed */
1255 /* Send remaining command\-line arguments as separate
1256 datagrams, and read responses from server */
1258 for (j = 3; j < argc; j++) {
1259 len = strlen(argv[j]) + 1;
1260 /* +1 for terminating null byte */
1262 if (len + 1 > BUF_SIZE) {
1264 "Ignoring long message in argument %d\\n", j);
1268 if (write(sfd, argv[j], len) != len) {
1269 fprintf(stderr, "partial/failed write\\n");
1273 nread = read(sfd, buf, BUF_SIZE);
1279 printf("Received %ld bytes: %s\\n", (long) nread, buf);
1287 .\" .BR getipnodebyaddr (3),
1288 .\" .BR getipnodebyname (3)
1289 .BR gethostbyname (3),
1290 .BR getnameinfo (3),