.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk) .\" and Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk .\" .\" .\" %%%LICENSE_START(VERBATIM) .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one. .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" %%%LICENSE_END .\" .\" References consulted: .\" Linux libc source code .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991) .\" 386BSD man pages .\" libc.info (from glibc distribution) .\" Modified Sat Jul 24 19:12:00 1993 by Rik Faith .\" Modified Sun Sep 3 20:29:36 1995 by Jim Van Zandt .\" Changed network into host byte order (for inet_network), .\" Andreas Jaeger , 980130. .\" 2008-06-19, mtk .\" Describe the various address forms supported by inet_aton(). .\" Clarify discussion of inet_lnaof(), inet_netof(), and inet_makeaddr(). .\" Add discussion of Classful Addressing, noting that it is obsolete. .\" Added an EXAMPLE program. .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 1998 Ueyama Rui .\" all rights reserved. .\" Translated 1998-05-23, Ueyama Rui .\" Updated & Modefied 1999-02-26, Shouichi Saito .\" Updated 1999-12-26, Kentaro Shirakata .\" Updated 2005-10-04, Kentaro Shirakata .\" Updated 2007-05-01, Akihiro MOTOKI , LDP v2.46 .\" Updated 2008-08-13, Akihiro MOTOKI, LDP v3.05 .\" .TH INET 3 2014\-04\-19 GNU "Linux Programmer's Manual" .SH 名前 inet_aton, inet_addr, inet_network, inet_ntoa, inet_makeaddr, inet_lnaof, inet_netof \- インターネットアドレス操作ルーチン .SH 書式 .nf \fB#include \fP \fB#include \fP \fB#include \fP .sp \fBint inet_aton(const char *\fP\fIcp\fP\fB, struct in_addr *\fP\fIinp\fP\fB);\fP .sp \fBin_addr_t inet_addr(const char *\fP\fIcp\fP\fB);\fP .sp \fBin_addr_t inet_network(const char *\fP\fIcp\fP\fB);\fP .sp \fBchar *inet_ntoa(struct in_addr \fP\fIin\fP\fB);\fP .sp \fBstruct in_addr inet_makeaddr(int \fP\fInet\fP\fB, int \fP\fIhost\fP\fB);\fP .sp \fBin_addr_t inet_lnaof(struct in_addr \fP\fIin\fP\fB);\fP .sp \fBin_addr_t inet_netof(struct in_addr \fP\fIin\fP\fB);\fP .fi .sp .in -4n glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7) 参照): .in .sp \fBinet_aton\fP(), \fBinet_ntoa\fP(): _BSD_SOURCE || _SVID_SOURCE .SH 説明 \fBinet_aton\fP() は、インターネットホストのアドレス \fIcp\fP を、 IPv4 の数値とドットによる表記から (ネットワークバイトオーダの) バイナリ値へ 変換し、変換結果を \fIinp\fP が指している構造体に格納する。 アドレスが有効な場合 0 以外を返し、そうでない場合は 0 を返す。 \fIcp\fP で渡すアドレスとして、以下の形式を用いることができる。 .TP 10 \fIa.b.c.d\fP 4 つの数字のそれぞれはアドレスの各バイトを示す。 これらのバイトは左から右の順序でバイナリアドレスに割り当てられる。 .TP \fIa.b.c\fP \fIa\fP と \fIb\fP はバイナリアドレスの最初の 2 バイトを示す。 \fIc\fP は 16 ビット値と解釈され、バイナリアドレスの右側の 2 バイトを表す。 この表記は、(過去のものとなった) クラス B ネットワークアドレスを 指定するのに適している。 .TP \fIa.b\fP \fIa\fP はバイナリアドレスの最初のバイトを示す。 \fIb\fP は 24 ビット値と解釈され、バイナリアドレスの右側の 3 バイトを表す。 この表記は、(過去のものとなった) クラス A ネットワークアドレスを 指定するのに適している。 .TP \fIa\fP 値 \fIa\fP は 32 ビット値と解釈され、バイトの再配置は行われず、 そのままバイナリアドレスとして格納される。 .PP 上記の全ての形式で、ドット区切りのアドレスの各要素は、10 進数、 8 進数 (先頭に \fI0\fP を付ける)、 16 進数 (先頭に \fI0X\fP を付ける) で指定できる。 これらの形式のアドレスをまとめて \fIIPv4 の数値とドットによる表記 (IPv4 numbers\-and\-dots notation)\fP と呼ぶ。 また、10 進数 4 つだけを使った形式を \fIIPv4 のドット区切りの 10 進数表記 (IPv4 dotted\-decimal notation)\fP と呼ぶ (\fIIPv4 のドット区切り 4 分割表記 (IPv4 dotted\-decimal notation)\fP と呼ぶこともある)。 \fBinet_aton\fP() は渡された文字列が正常に解釈できた場合 1 を返し、 文字列が不正な場合 0 を返す (エラーの場合に \fBerrno\fP はセット\fIされない\fP)。 .PP \fBinet_addr\fP() 関数は、インターネットホストのアドレス \fIcp\fP を、 IPv4 の数値とドットによる表記からネットワークバイトオーダでの バイナリ値へ変換して返す。 入力が不正な場合、 \fBINADDR_NONE\fP (普通は \-1) を返す。 \-1 は有効なアドレス (255.255.255.255) なので、この関数を使うと 問題になるかもしれない。 この関数を使うのは避け、代わりに \fBinet_aton\fP(), \fBinet_pton\fP(3), \fBgetaddrinfo\fP(3) を使うのがよい。 これらの関数の方が、エラーの通知がよりきれいな方法で行われる。 .PP \fBinet_network\fP() 関数は、 IPv4 の数値とドットによる表記の文字列 \fIcp\fP を、 インターネットアドレスとしての使用に適した ホストバイトオーダの数値に変換する。 成功すると、変換されたアドレスを返す。 入力が不正な場合は \-1 を返す。 .PP \fBinet_ntoa\fP() 関数は、ネットワークバイトオーダで渡されたインターネットホストアドレス \fIin\fP を、 IPv4 のドット区切りの 10 進数表記の文字列に変換する。 文字列は静的に割当てられたバッファに格納されて返されるので、 この後でこの関数を再度呼び出すと文字列は上書きされる。 .PP \fBinet_lnaof\fP() 関数は、インターネットアドレス \fIin\fP のローカルネットワーク部分を返す。 この返り値はホストバイトオーダである。 .PP \fBinet_netof\fP() 関数は、インターネットアドレス \fIin\fP のネットワーク部分を返す。 この返り値はホストバイトオーダである。 .PP \fBinet_makeaddr\fP() 関数は \fBinet_netof\fP() と \fBinet_lnaof\fP() の逆の機能を持つ。 ネットワーク番号 \fInet\fP と、ローカルアドレス \fIhost\fP を 組み合わせて生成した、インターネットホストアドレスを ネットワークバイトオーダで返す。 \fIhost\fP, \fInet\fP はともにホストバイトオーダである。 .PP \fBinet_ntoa\fP(), \fBinet_makeaddr\fP(), \fBinet_lnaof\fP(), \fBinet_netof\fP() で使用する構造体 \fIin_addr\fP は \fI\fP で次のように定義されている: .sp .in +4n .nf typedef uint32_t in_addr_t; struct in_addr { in_addr_t s_addr; }; .fi .in .SH 準拠 4.3BSD. \fBinet_addr\fP(), \fBinet_ntoa\fP() は POSIX.1\-2001 で規定されている。 \fBinet_aton\fP() は POSIX.1\-2001 で規定されていないが、ほとんどのシステムで利用可能である。 .SH 注意 i386 ではホストバイトオーダは Least Significant Byte (LSB) first (リトルエンディアン) だが、 インターネットで使われるネットワークバイトオーダは Most Significant Byte (MSB) first (ビッグエンディアン) である点に注意すること。 \fBinet_lnaof\fP(), \fBinet_netof\fP(), \fBinet_makeaddr\fP() は過去の名残であり、渡されたアドレスが \fIクラスフル・ネットワークアドレス (classful network addresses)\fP であると仮定して処理を行う。 クラスフル・ネットワークアドレスでは、以下にあるように、 IPv4 ネットワークアドレスをバイト境界でネットワーク部とホスト部に分割する。 .TP 10 Class A (ネットワークバイトオーダの) アドレスの最上位ビットが 0 の場合、 このアドレス種別となる。このアドレス種別では、 最上位バイトがネットワークアドレスを表し、 残りの 3 バイトがホストアドレスを表す。 .TP Class B (ネットワークバイトオーダの) アドレスの上位側 2 ビットがバイナリ値で 10 の場合、このアドレス種別となる。このアドレス種別では、 上位 2 バイトがネットワークアドレスを表し、 残りの 2 バイトがホストアドレスを表す。 .TP Class C (ネットワークバイトオーダの) アドレスの上位側 3 ビットがバイナリ値で 110 の場合、このアドレス種別となる。このアドレス種別では、 上位 3 バイトがネットワークアドレスを表し、 残りの 1 バイトがホストアドレスを表す。 .PP クラスフル・ネットワークアドレスは現在では廃止され、 クラスレス・ドメイン間ルーチン (CIDR) に取って代わられた。 CIDR では、アドレスを任意のビット境界 (バイト境界ではない) で ネットワーク部とホスト部に分割する。 .SH 例 以下は \fBinet_aton\fP() と \fBinet_ntoa\fP() の使用例である。このように実行する。 .in +4n .nf $\fB ./a.out 226.000.000.037\fP # Last byte is in octal 226.0.0.31 $\fB ./a.out 0x7f.1 \fP # First byte is in hex 127.0.0.1 .fi .in .SS プログラムのソース \& .nf #define _BSD_SOURCE #include #include #include int main(int argc, char *argv[]) { struct in_addr addr; if (argc != 2) { fprintf(stderr, "%s \en", argv[0]); exit(EXIT_FAILURE); } if (inet_aton(argv[1], &addr) == 0) { fprintf(stderr, "Invalid address\en"); exit(EXIT_FAILURE); } printf("%s\en", inet_ntoa(addr)); exit(EXIT_SUCCESS); } .fi .SH 関連項目 \fBbyteorder\fP(3), \fBgetaddrinfo\fP(3), \fBgethostbyname\fP(3), \fBgetnameinfo\fP(3), \fBgetnetent\fP(3), \fBinet_net_pton\fP(3), \fBinet_ntop\fP(3), \fBinet_pton\fP(3), \fBhosts\fP(5), \fBnetworks\fP(5) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.65 の一部 である。プロジェクトの説明とバグ報告に関する情報は http://www.kernel.org/doc/man\-pages/ に書かれている。