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 \-
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)):
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 は、(インターネットのホストとサービスを識別する)
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 の機能をまとめて一つのインターフェースにしたものであるが、
145 を使うことでプログラムは IPv4 と IPv6 の違いに関する依存関係を
150 .\"O structure used by
151 .\"O .BR getaddrinfo ()
152 .\"O contains the following fields:
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 (),
197 が指すリストに入れて返すソケットアドレス構造体を選択するための基準を指定する。
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) の
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 を指定すると、任意のソケット型のソケットアドレスを
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 を指定すると、任意のプロトコルののソケットアドレスを
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)" .
308 .B "(AI_V4MAPPED\ |\ AI_ADDRCONFIG)"
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.
332 でサポートされているドット区切りの数字による表記、
335 でサポートされている 16 進数の文字列形式) もしくは
337 ネットワークホスト名を指定した場合には、そのネットワークアドレスが検索され、
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.
385 返されるソケットアドレスには「ワイルドカード・アドレス」
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.
427 が NULL の場合、ネットワークアドレスにはループバック・インターフェイスの
431 .BR IN6ADDR_LOOPBACK_INIT ) が設定される。
432 これは同じホスト上で動作している接続相手と通信するような
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 により、返される各アドレス構造体のポート番号が決まる。
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 の場合、返されるソケットアドレスのポート番号は
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
511 このとき、各構造体のネットワークアドレスは
517 で課されたすべての制限を満たすものとなる。
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 構造体は複数個になることもあり、その理由はいくつかある。
539 複数のプロトコルでアクセスできる (例えば
544 複数のソケット種別で同じサービスが利用できる
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)).
622 .\"O .BR SOCK_STREAM ;
625 .\"O returns the protocol for the socket.
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 アドレスが設定されている場合は
670 ローカルシステムに最低一つの 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
711 が指すリストには IPv6 アドレスと IPv4-mapped IPv6 アドレスの
716 .\"O is not also specified.
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 を参照のこと) と変換することを選択的に認めるように拡張されている。
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.
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 文字が含まれている場合、
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.
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 これらの構成要素を読み込み可能な形に変換するために、
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.
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.
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)
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.
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).
956 が "shell" (ストリーム・ソケットでのみ利用できるサービス) で、
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 ,
1001 .\"O Other system error, check
1009 .\"O .BR gai_strerror ()
1010 .\"O function translates these error codes to a human readable string,
1011 .\"O suitable for error reporting.
1013 関数を用いると、これらのエラーコードを人間に可読な文字列に変換できるので、
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
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.
1066 GNU C ライブラリでは、この場合に、代わりに
1069 .BR "(AI_V4MAPPED\ |\ AI_ADDRCONFIG)"
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.
1089 .BR gai_strerror (),
1090 .BR freeaddrinfo (),
1093 プログラムは UDP データグラムの echo サーバとクライアントである。
1094 .\"O .SS Server program
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
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),