OSDN Git Service

Convert release and draft pages to UTF-8.
[linuxjm/jm.git] / manual / LDP_man-pages / release / man3 / inet.3
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\"
3 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
4 .\" and Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
5 .\"     <mtk.manpages@gmail.com>
6 .\"
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.
15 .\"
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.
23 .\"
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
26 .\"
27 .\" References consulted:
28 .\"     Linux libc source code
29 .\"     Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
30 .\"     386BSD man pages
31 .\"     libc.info (from glibc distribution)
32 .\" Modified Sat Jul 24 19:12:00 1993 by Rik Faith <faith@cs.unc.edu>
33 .\" Modified Sun Sep  3 20:29:36 1995 by Jim Van Zandt <jrv@vanzandt.mv.com>
34 .\" Changed network into host byte order (for inet_network),
35 .\"     Andreas Jaeger <aj@arthur.rhein-neckar.de>, 980130.
36 .\" 2008-06-19, mtk
37 .\"     Describe the various address forms supported by inet_aton().
38 .\"     Clarify discussion of inet_lnaof(), inet_netof(), and inet_makeaddr().
39 .\"     Add discussion of Classful Addressing, noting that it is obsolete.
40 .\"     Added an EXAMPLE program.
41 .\"
42 .\" Japanese Version Copyright (c) 1998 Ueyama Rui
43 .\"         all rights reserved.
44 .\" Translated 1998-05-23, Ueyama Rui <rui@linux.or.jp>
45 .\" Updated & Modefied 1999-02-26, Shouichi Saito
46 .\" Updated 1999-12-26, Kentaro Shirakata <argrath@ub32.org>
47 .\" Updated 2005-10-04, Kentaro Shirakata <argrath@ub32.org>
48 .\" Updated 2007-05-01, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.46
49 .\" Updated 2008-08-13, Akihiro MOTOKI, LDP v3.05
50 .\"
51 .\"WORD:        numbers-and-dots notation       数値とドットによる記法
52 .\"WORD:        dotted-decimal notation ドット区切りの 10 進数記法
53 .\"WORD:        feature test macro      機能検査マクロ
54 .\"
55 .TH INET 3  2008-06-19 "GNU" "Linux Programmer's Manual"
56 .SH 名前
57 inet_aton, inet_addr, inet_network, inet_ntoa, inet_makeaddr, inet_lnaof,
58 inet_netof \- インターネットアドレス操作ルーチン
59 .SH 書式
60 .nf
61 .B #include <sys/socket.h>
62 .B #include <netinet/in.h>
63 .B #include <arpa/inet.h>
64 .sp
65 .BI "int inet_aton(const char *" cp ", struct in_addr *" inp );
66 .sp
67 .BI "in_addr_t inet_addr(const char *" cp );
68 .sp
69 .BI "in_addr_t inet_network(const char *" cp );
70 .sp
71 .BI "char *inet_ntoa(struct in_addr " in );
72 .sp
73 .BI "struct in_addr inet_makeaddr(int " net ", int " host );
74 .sp
75 .BI "in_addr_t inet_lnaof(struct in_addr " in );
76 .sp
77 .BI "in_addr_t inet_netof(struct in_addr " in );
78 .fi
79 .sp
80 .in -4n
81 glibc 向けの機能検査マクロの要件
82 .RB ( feature_test_macros (7)
83 参照):
84 .in
85 .sp
86 .BR inet_aton (),
87 .BR inet_ntoa ():
88 _BSD_SOURCE || _SVID_SOURCE
89 .SH 説明
90 .BR inet_aton ()
91 は、インターネットホストのアドレス \fIcp\fP を、
92 IPv4 の数値とドットによる表記から (ネットワークバイトオーダの) バイナリ値へ
93 変換し、変換結果を \fIinp\fP が指している構造体に格納する。
94 アドレスが有効な場合 0 以外を返し、そうでない場合は 0 を返す。
95 .I cp
96 で渡すアドレスとして、以下の形式を用いることができる。
97 .TP 10
98 .I a.b.c.d
99 4 つの数字のそれぞれはアドレスの各バイトを示す。
100 これらのバイトは左から右の順序でバイナリアドレスに割り当てられる。
101 .TP
102 .I a.b.c
103 .I a
104
105 .I b
106 はバイナリアドレスの最初の 2 バイトを示す。
107 .I c
108 は 16 ビット値と解釈され、バイナリアドレスの右側の 2 バイトを表す。
109 この表記は、(過去のものとなった) クラス B ネットワークアドレスを
110 指定するのに適している。
111 .TP
112 .I a.b
113 .I a
114 はバイナリアドレスの最初のバイトを示す。
115 .I b
116 は 24 ビット値と解釈され、バイナリアドレスの右側の 3 バイトを表す。
117 この表記は、(過去のものとなった) クラス C ネットワークアドレスを
118 指定するのに適している。
119 .TP
120 .I a
121
122 .I a
123 は 32 ビット値と解釈され、バイトの再配置は行われず、
124 そのままバイナリアドレスとして格納される。
125 .PP
126 上記の全ての形式で、ドット区切りのアドレスの各要素は、10 進数、
127 8 進数 (先頭に
128 .I 0
129 を付ける)、
130 16 進数 (先頭に
131 .I 0X
132 を付ける) で指定できる。
133 これらの形式のアドレスをまとめて
134 .I "IPv4 の数値とドットによる表記 (IPv4 numbers-and-dots notation)"
135 と呼ぶ。
136 また、10 進数 4 つだけを使った形式を
137 .I "IPv4 のドット区切りの 10 進数表記 (IPv4 dotted-decimal notation)"
138 と呼ぶ
139 .RI ( "IPv4 のドット区切り 4 分割表記 (IPv4 dotted-decimal notation)"
140 と呼ぶこともある)。
141 .PP
142 .BR inet_addr ()
143 関数は、インターネットホストのアドレス \fIcp\fP を、
144 IPv4 の数値とドットによる表記からネットワークバイトオーダでの
145 バイナリ値へ変換して返す。
146 入力が不正な場合、
147 .B INADDR_NONE
148 (普通は \-1) を返す。
149 \-1 は有効なアドレス (255.255.255.255) なので、この関数を使うと
150 問題になるかもしれない。
151 この関数を使うのは避け、代わりに
152 .BR inet_aton (),
153 .BR inet_pton (3),
154 .BR getaddrinfo (3)
155 を使うのがよい。
156 これらの関数の方が、エラーの通知がよりきれいな方法で行われる。
157 .PP
158 .BR inet_network ()
159 関数は、
160 IPv4 の数値とドットによる表記の文字列 \fIcp\fP を、
161 インターネットアドレスとしての使用に適した
162 ホストバイトオーダの数値に変換する。
163 成功すると、変換されたアドレスを返す。
164 入力が不正な場合は \-1 を返す。
165 .PP
166 .BR inet_ntoa ()
167 関数は、ネットワークバイトオーダで渡されたインターネットホストアドレス
168 \fIin\fP を、 IPv4 のドット区切りの 10 進数表記の文字列に変換する。
169 文字列は静的に割当てられたバッファに格納されて返されるので、
170 この後でこの関数を再度呼び出すと文字列は上書きされる。
171 .PP
172 .BR inet_lnaof ()
173 関数は、インターネットアドレス \fIin\fP のローカルネットワーク部分を返す。
174 この返り値はホストバイトオーダである。
175 .PP
176 .BR inet_netof ()
177 関数は、インターネットアドレス \fIin\fP のネットワーク部分を返す。
178 この返り値はホストバイトオーダである。
179 .PP
180 .BR inet_makeaddr ()
181 関数は
182 .BR inet_netof ()
183
184 .BR inet_lnaof ()
185 の逆の機能を持つ。
186 ネットワーク番号 \fInet\fP と、ローカルアドレス \fIhost\fP を
187 組み合わせて生成した、インターネットホストアドレスを
188 ネットワークバイトオーダで返す。
189 \fIhost\fP, \fInet\fP はともにホストバイトオーダである。
190 .PP
191 .BR inet_ntoa (),
192 .BR inet_makeaddr (),
193 .BR inet_lnaof (),
194 .BR inet_netof ()
195 で使用する構造体 \fIin_addr\fP は
196 .I <netinet/in.h>
197 で次のように定義されている:
198 .sp
199 .in +4n
200 .nf
201 typedef uint32_t in_addr_t;
202
203 struct in_addr {
204     in_addr_t s_addr;
205 };
206 .fi
207 .in
208 .SH 準拠
209 4.3BSD.
210 .BR inet_addr (),
211 .BR inet_ntoa ()
212 は POSIX.1-2001 で規定されている。
213 .BR inet_aton ()
214 は POSIX.1-2001 で規定されていないが、ほとんどのシステムで利用可能である。
215 .SH 注意
216 i386 ではホストバイトオーダは Least Significant Byte (LSB) first
217 (リトルエンディアン) だが、
218 インターネットで使われるネットワークバイトオーダは
219 Most Significant Byte (MSB) first (ビッグエンディアン)
220 である点に注意すること。
221
222 .BR inet_lnaof (),
223 .BR inet_netof (),
224 .BR inet_makeaddr ()
225 は過去の名残であり、渡されたアドレスが
226 .I "クラスフル・ネットワークアドレス (classful network addresses)"
227 であると仮定して処理を行う。
228 クラスフル・ネットワークアドレスでは、以下にあるように、
229 IPv4 ネットワークアドレスをバイト境界でネットワーク部とホスト部に分割する。
230 .TP 10
231 Class A
232 (ネットワークバイトオーダの) アドレスの最上位ビットが 0 の場合、
233 このアドレス種別となる。このアドレス種別では、
234 最上位バイトがネットワークアドレスを表し、
235 残りの 3 バイトがホストアドレスを表す。
236 .TP
237 Class B
238 (ネットワークバイトオーダの) アドレスの上位側 2 ビットがバイナリ値で
239 10 の場合、このアドレス種別となる。このアドレス種別では、
240 上位 2 バイトがネットワークアドレスを表し、
241 残りの 2 バイトがホストアドレスを表す。
242 .TP
243 Class C
244 (ネットワークバイトオーダの) アドレスの上位側 3 ビットがバイナリ値で
245 110 の場合、このアドレス種別となる。このアドレス種別では、
246 上位 3 バイトがネットワークアドレスを表し、
247 残りの 1 バイトがホストアドレスを表す。
248 .PP
249 クラスフル・ネットワークアドレスは現在では廃止され、
250 クラスレス・ドメイン間ルーチン (CIDR) に取って代わられた。
251 CIDR では、アドレスを任意のビット境界 (バイト境界ではない) で
252 ネットワーク部とホスト部に分割する。
253 .SH 例
254 以下は
255 .BR inet_aton ()
256
257 .BR inet_ntoa ()
258 の使用例である。このように実行する。
259 .in +4n
260 .nf
261
262 .RB "$" " ./a.out 226.000.000.037" "      # Last byte is in octal"
263 226.0.0.31
264 .RB "$" " ./a.out 0x7f.1         " "      # First byte is in hex"
265 127.0.0.1
266 .fi
267 .in
268 .SS プログラムのソース
269 \&
270 .nf
271 #define _BSD_SOURCE
272 #include <arpa/inet.h>
273 #include <stdio.h>
274 #include <stdlib.h>
275
276 int
277 main(int argc, char *argv[])
278 {
279     struct in_addr addr;
280
281     if (argc != 2) {
282         fprintf(stderr, "%s <dotted\-address>\\n", argv[0]);
283         exit(EXIT_FAILURE);
284     }
285
286     if (inet_aton(argv[1], &addr) == 0) {
287         perror("inet_aton");
288         exit(EXIT_FAILURE);
289     }
290
291     printf("%s\\n", inet_ntoa(addr));
292     exit(EXIT_SUCCESS);
293 }
294 .fi
295 .SH 関連項目
296 .BR byteorder (3),
297 .BR getaddrinfo (3),
298 .BR gethostbyname (3),
299 .BR getnameinfo (3),
300 .BR getnetent (3),
301 .BR inet_ntop (3),
302 .BR inet_pton (3),
303 .BR hosts (5),
304 .BR networks (5)