inetのヘルプ・マニュアル
日本語 英語
inet --help
man inet
INET(3) Linux Programmer’s Manual INET(3)
名前
inet_aton, inet_addr, inet_network, inet_ntoa, inet_makeaddr,
inet_lnaof, inet_netof - インターネットアドレス操作ルーチン
書式
#include
#include
#include
int inet_aton(const char *cp, struct in_addr *inp);
in_addr_t inet_addr(const char *cp);
in_addr_t inet_network(const char *cp);
char *inet_ntoa(struct in_addr in);
struct in_addr inet_makeaddr(int net, int host);
in_addr_t inet_lnaof(struct in_addr in);
in_addr_t inet_netof(struct in_addr in);
glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):
inet_aton(), inet_ntoa(): _BSD_SOURCE || _SVID_SOURCE
説明
inet_aton() は、インターネットホストのアドレス cp を、 IPv4 の数値と ド
ッ トによる表記から (ネットワークバイトオーダの) バイナリ値へ変換し、変
換結果を inp が指している構造体に格納する。アドレスが有効な場合 0 以 外
を返し、そうでない場合は 0 を返す。 cp で渡すアドレスとして、以下の形式
を用いることができる。
a.b.c.d 4 つの数字のそれぞれはアドレスの各バイトを示す。これらのバ イ
トは左から右の順序でバイナリアドレスに割り当てられる。
a.b.c a と b はバイナリアドレスの最初の 2 バイトを示す。 c は 16 ビ
ット値と解釈され、バイナリアドレスの右側の 2 バイトを表す。こ
の 表記は、(過去のものとなった) クラス B ネットワークアドレス
を指定するのに適している。
a.b a はバイナリアドレスの最初のバイトを示す。 b は 24 ビット値と
解釈され、バイナリアドレスの右側の 3 バイトを表す。この表記は
、(過去のものとなった) クラス C ネットワークアドレスを指定 す
るのに適している。
a 値 a は 32 ビット値と解釈され、バイトの再配置は行われず、その
ままバイナリアドレスとして格納される。
上記の全ての形式で、ドット区切りのアドレスの各要素は、10 進数、 8 進 数
(先頭に 0 を付ける)、 16 進数 (先頭に 0X を付ける) で指定できる。これら
の形式のアドレスをまとめて IPv4 の数値とドットによる表記 (IPv4 numbers-
and-dots notation) と呼ぶ。また、10 進数 4 つだけを使った形式を IPv4 の
ドット区切りの 10 進数表記 (IPv4 dotted-decimal notation) と呼ぶ (IPv4
のドット区切り 4 分割表記 (IPv4 dotted-decimal notation) と呼ぶこともあ
る)。
inet_addr() 関数は、インターネットホストのアドレス cp を、 IPv4 の数 値
と ドットによる表記からネットワークバイトオーダでのバイナリ値へ変換して
返す。入力が不正な場合、 INADDR_NONE (普通は -1) を返す。 -1 は有効なア
ドレス (255.255.255.255) なので、この関数を使うと問題になるかもしれない
。この関数を使うのは避け、代わりに inet_aton(), inet_pton(3), getad-
drinfo(3) を使うのがよい。これらの関数の方が、エラーの通知がよりきれい
な方法で行われる。
inet_network() 関数は、 IPv4 の数値とドットによる表記の文字列 cp を、イ
ン ターネットアドレスとしての使用に適したホストバイトオーダの数値に変換
する。成功すると、変換されたアドレスを返す。入力が不正な場合は -1 を 返
す。
inet_ntoa() 関数は、ネットワークバイトオーダで渡されたインターネットホ
ストアドレス in を、 IPv4 のドット区切りの 10 進数表記の文字列に変換 す
る 。文字列は静的に割当てられたバッファに格納されて返されるので、この後
でこの関数を再度呼び出すと文字列は上書きされる。
inet_lnaof() 関数は、インターネットアドレス in のローカルネットワーク部
分を返す。この返り値はホストバイトオーダである。
inet_netof() 関数は、インターネットアドレス in のネットワーク部分を返す
。この返り値はホストバイトオーダである。
inet_makeaddr() 関数は inet_netof() と inet_lnaof() の逆の機能を持つ 。
ネットワーク番号 net と、ローカルアドレス host を組み合わせて生成した、
インターネットホストアドレスをネットワークバイトオーダで返 す 。 host,
net はともにホストバイトオーダである。
inet_ntoa(), inet_makeaddr(), inet_lnaof(), inet_netof() で使用する構造
体 in_addr は で次のように定義されている:
typedef uint32_t in_addr_t;
struct in_addr {
in_addr_t s_addr;
};
準拠
4.3BSD. inet_addr(), inet_ntoa() は POSIX.1-2001 で規定されて い る 。
inet_aton() は POSIX.1-2001 で規定されていないが、ほとんどのシステムで
利用可能である。
注意
i386 ではホストバイトオーダは Least Significant Byte (LSB) first (リ ト
ル エンディアン) だが、インターネットで使われるネットワークバイトオーダ
は Most Significant Byte (MSB) first (ビッグエンディアン) である点に 注
意すること。
inet_lnaof(), inet_netof(), inet_makeaddr() は過去の名残であり、渡され
たアドレスが クラスフル・ネットワ ー ク ア ド レ ス (classful network
addresses) であると仮定して処理を行う。クラスフル・ネットワークアドレス
では、以下にあるように、 IPv4 ネットワークアドレスをバイト境界でネッ ト
ワーク部とホスト部に分割する。
Class A ( ネットワークバイトオーダの) アドレスの最上位ビットが 0 の場
合、このアドレス種別となる。このアドレス種別では、最上位バ イ
トがネットワークアドレスを表し、残りの 3 バイトがホストアドレ
スを表す。
Class B (ネットワークバイトオーダの) アドレスの上位側 2 ビットがバ イ
ナ リ値で 10 の場合、このアドレス種別となる。このアドレス種別
では、上位 2 バイトがネットワークアドレスを表し、残りの 2 バ
イトがホストアドレスを表す。
Class C ( ネットワークバイトオーダの) アドレスの上位側 3 ビットがバイ
ナリ値で 110 の場合、このアドレス種別となる。このアドレス種別
で は、上位 3 バイトがネットワークアドレスを表し、残りの 1 バ
イトがホストアドレスを表す。
クラスフル・ネットワークアドレスは現在では廃止され、クラスレス・ドメ イ
ン 間ルーチン (CIDR) に取って代わられた。 CIDR では、アドレスを任意のビ
ット境界 (バイト境界ではない) でネットワーク部とホスト部に分割する。
例
以下は inet_aton() と inet_ntoa() の使用例である。このように実行する。
$ ./a.out 226.000.000.037 # Last byte is in octal
226.0.0.31
$ ./a.out 0x7f.1 # First byte is in hex
127.0.0.1
プログラムのソース
#define _BSD_SOURCE
#include
#include
#include
int
main(int argc, char *argv[])
{
struct in_addr addr;
if (argc != 2) {
fprintf(stderr, "%s \n", argv[0]);
exit(EXIT_FAILURE);
}
if (inet_aton(argv[1], &addr) == 0) {
perror("inet_aton");
exit(EXIT_FAILURE);
}
printf("%s\n", inet_ntoa(addr));
exit(EXIT_SUCCESS);
}
関連項目
byteorder(3), getaddrinfo(3), gethostbyname(3), getnameinfo(3), getne-
tent(3), inet_ntop(3), inet_pton(3), hosts(5), networks(5)
GNU 2008-06-19 INET(3)
INET(3) Linux Programmer’s Manual INET(3)
NAME
inet_aton, inet_addr, inet_network, inet_ntoa, inet_makeaddr,
inet_lnaof, inet_netof - Internet address manipulation routines
SYNOPSIS
#include
#include
#include
int inet_aton(const char *cp, struct in_addr *inp);
in_addr_t inet_addr(const char *cp);
in_addr_t inet_network(const char *cp);
char *inet_ntoa(struct in_addr in);
struct in_addr inet_makeaddr(int net, int host);
in_addr_t inet_lnaof(struct in_addr in);
in_addr_t inet_netof(struct in_addr in);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
inet_aton(), inet_ntoa(): _BSD_SOURCE || _SVID_SOURCE
DESCRIPTION
inet_aton() converts the Internet host address cp from the IPv4 num-
bers-and-dots notation into binary form (in network byte order) and
stores it in the structure that inp points to. inet_aton() returns
non-zero if the address is valid, zero if not. The address supplied in
cp can have one of the following forms:
a.b.c.d Each of the four numeric parts specifies a byte of the
address; the bytes are assigned in left-to-right order to
produce the binary address.
a.b.c Parts a and b specify the first two bytes of the binary
address. Part c is interpreted as a 16-bit value that
defines the rightmost two bytes of the binary address. This
notation is suitable for specifying (outmoded) Class B net-
work addresses.
a.b Part a specifies the first byte of the binary address. Part
b is interpreted as a 24-bit value that defines the rightmost
three bytes of the binary address. This notation is suitable
for specifying (outmoded) Class C network addresses.
a The value a is interpreted as a 32-bit value that is stored
directly into the binary address without any byte rearrange-
ment.
In all of the above forms, components of the dotted address can be
specified in decimal, octal (with a leading 0), or hexadecimal, with a
leading 0X). Addresses in any of these forms are collectively termed
IPV4 numbers-and-dots notation. The form that uses exactly four deci-
mal numbers is referred to as IPv4 dotted-decimal notation (or some-
times: IPv4 dotted-quad notation).
The inet_addr() function converts the Internet host address cp from
IPv4 numbers-and-dots notation into binary data in network byte order.
If the input is invalid, INADDR_NONE (usually -1) is returned. Use of
this function is problematic because -1 is a valid address
(255.255.255.255). Avoid its use in favor of inet_aton(),
inet_pton(3), or getaddrinfo(3) which provide a cleaner way to indicate
error return.
The inet_network() function converts cp, a string in IPv4 numbers-and-
dots notation, into a number in host byte order suitable for use as an
Internet network address. On success, the converted address is
returned. If the input is invalid, -1 is returned.
The inet_ntoa() function converts the Internet host address in, given
in network byte order, to a string in IPv4 dotted-decimal notation.
The string is returned in a statically allocated buffer, which subse-
quent calls will overwrite.
The inet_lnaof() function returns the local network address part of the
Internet address in. The returned value is in host byte order.
The inet_netof() function returns the network number part of the Inter-
net address in. The returned value is in host byte order.
The inet_makeaddr() function is the converse of inet_netof() and
inet_lnaof(). It returns an Internet host address in network byte
order, created by combining the network number net with the local
address host, both in host byte order.
The structure in_addr as used in inet_ntoa(), inet_makeaddr(),
inet_lnaof() and inet_netof() is defined in as:
typedef uint32_t in_addr_t;
struct in_addr {
in_addr_t s_addr;
};
CONFORMING TO
4.3BSD. inet_addr() and inet_ntoa() are specified in POSIX.1-2001.
inet_aton() is not specified in POSIX.1-2001, but is available on most
systems.
NOTES
On the i386 the host byte order is Least Significant Byte first (little
endian), whereas the network byte order, as used on the Internet, is
Most Significant Byte first (big endian).
inet_lnaof(), inet_netof(), and inet_makeaddr() are legacy functions
that assume they are dealing with classful network addresses. Classful
networking divides IPv4 network addresses into host and network compo-
nents at byte boundaries, as follows:
Class A This address type is indicated by the value 0 in the most
significant bit of the (network byte ordered) address. The
network address is contained in the most significant byte,
and the host address occupies the remaining three bytes.
Class B This address type is indicated by the binary value 10 in the
most significant two bits of the address. The network
address is contained in the two most significant bytes, and
the host address occupies the remaining two bytes.
Class C This address type is indicated by the binary value 110 in the
most significant three bits of the address. The network
address is contained in the three most significant bytes, and
the host address occupies the remaining byte.
Classful network addresses are now obsolete, having been superseded by
Classless Inter-Domain Routing (CIDR), which divides addresses into
network and host components at arbitrary bit (rather than byte) bound-
aries.
EXAMPLE
An example of the use of inet_aton() and inet_ntoa() is shown below.
Here are some example runs:
$ ./a.out 226.000.000.037 # Last byte is in octal
226.0.0.31
$ ./a.out 0x7f.1 # First byte is in hex
127.0.0.1
Program source
#define _BSD_SOURCE
#include
#include
#include
int
main(int argc, char *argv[])
{
struct in_addr addr;
if (argc != 2) {
fprintf(stderr, "%s \n", argv[0]);
exit(EXIT_FAILURE);
}
if (inet_aton(argv[1], &addr) == 0) {
perror("inet_aton");
exit(EXIT_FAILURE);
}
printf("%s\n", inet_ntoa(addr));
exit(EXIT_SUCCESS);
}
SEE ALSO
byteorder(3), getaddrinfo(3), gethostbyname(3), getnameinfo(3), getne-
tent(3), inet_ntop(3), inet_pton(3), hosts(5), networks(5)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
GNU 2008-06-19 INET(3)