inet_ptonのヘルプ・マニュアル
日本語 英語
inet_pton --help
man inet_pton
INET_PTON(3) Linux Programmer’s Manual INET_PTON(3)
名前
inet_pton - IPv4/IPv6 アドレスをテキスト形式からバイナリ形式に変換する
書式
#include
int inet_pton(int af, const char *src, void *dst);
説明
この関数は文字列 src を、アドレスファミリー af のネットワークアドレス構
造体に変換し、 dst にコピーする。 af 引き数は AF_INET か AF_INET6 の ど
ちらかでなければならない。
現在サポートされているアドレスファミリーは以下の通りである。
AF_INET
src はドット区切りの 10 進数形式 "ddd.ddd.ddd.ddd" の IPv4 ネッ
トワークアドレス文字列へのポインタである。 ddd は 0 から 255 ま
で の 範 囲 の 最 大 3 桁の 10 進数である。このアドレスは struct
in_addr に変換されて dst にコピー さ れ る 。 dst の 長 さ は
sizeof(struct in_addr) (4) バイト (32ビット) でなければならない
。
AF_INET6
src は IPv6 ネットワークアドレスが格納された文字列へのポインタで
ある。このアドレスは struct in6_addr に変換されて dst にコピーさ
れる。 dst の長さは sizeof(struct in6_addr) (16) バイト (128 ビ
ッ ト) でなければならない。以下の 3 つのルールにしたがった形式が
IPv6 アドレスとして入力できる。
1. 推奨形式は x:x:x:x:x:x:x:x である。この形式は 8 個の 16 進 数
か ら構成され、各々の 16 進数は 16 ビット値を表す (x は最大 4
桁の 16 進数である)。
2. 推奨形式の中の連続する 0 の列は :: に短縮できる。アドレス中で
使用できる :: は 1 個だけである。例えば、ループバックアドレス
0:0:0:0:0:0:0:1 は ::1 と短縮できる。全ビットが 0 で構成さ れ
るワイルドカードアドレスは :: と記載できる。
3. IPv4 をマッピングした IPv6 アドレスを表記するには別の形式が便
利である。この別の形式は x:x:x:x:x:x:d.d.d.d と書くことができ
る 。最初の 6 個の x はアドレスを 16 ビット単位に区切ったとき
の上位側 6 個分 (つまり 96 ビット分) を定義する 16 進数であり
、 d の部分はアドレスの下位 32 ビットをドット区切りの 10 進数
表記で表したものである。 ::FFFF:204.152.189.116 はこの形式 の
例である。
IPv6 アドレスの表現方法の詳細については RFC 2373 を参照のこと。
返り値
成功する (ネットワークアドレスが正常に変換される) と、 inet_pton() は 1
を返す。 src が指定されたアドレスファミリーに対する正しいネットワークア
ドレス表記でない場合には、 0 を返す。 af がサポートされているアドレスフ
ァミリーでない場合には、 -1 を返し、 errno に EAFNOSUPPORT を設定する。
準拠
POSIX.1-2001.
注意
inet_aton(3) や inet_addr(3) と異なり、 inet_pton() は IPv6 アドレスに
対応している。一方で、 inet_pton() が受け付ける IPv4 アドレスはドット区
切りの 10 進数表記だけである。これに対し、 inet_aton(3) や inet_addr(3)
ではもっと一般的なドット区切りの数字表記 (16 進数や 8 進数の形式や、 4
バ イト全てを明示的に書かなくてもよい形式) が使用できる。ドット区切りの
数字表記で IPv6 アドレスと IPv4 アドレスの両方を扱えるインターフェイ ス
については、 getaddrinfo(3) を参照のこと。
バグ
AF_INET6 は IPv4 アドレスを認識しない。代わりに IPv4 アドレスをマッピン
グした IPv6 アドレスを src に与えなければならない。
例
以下のプログラムは inet_pton() と inet_ntop(3) の使用例を示すものである
。実行すると以下のようになる。
$ ./a.out i6 0:0:0:0:0:0:0:0
::
$ ./a.out i6 1:0:0:0:0:0:0:8
1::8
$ ./a.out i6 0:0:0:0:0:FFFF:204.152.189.116
::ffff:204.152.189.116
プログラムのソース
#include
#include
#include
#include
int
main(int argc, char *argv[])
{
unsigned char buf[sizeof(struct in6_addr)];
int domain, s;
char str[INET6_ADDRSTRLEN];
if (argc != 3) {
fprintf(stderr, "Usage: %s {i4|i6|} string\n", argv[0]);
exit(EXIT_FAILURE);
}
domain = (strcmp(argv[1], "i4") == 0) ? AF_INET :
(strcmp(argv[1], "i6") == 0) ? AF_INET6 : atoi(argv[1]);
s = inet_pton(domain, argv[2], buf);
if (s <= 0) {
if (s == 0)
fprintf(stderr, "Not in presentation format");
else
perror("inet_pton");
exit(EXIT_FAILURE);
}
if (inet_ntop(domain, buf, str, INET6_ADDRSTRLEN) == NULL) {
perror("inet_ntop");
exit(EXIT_FAILURE);
}
printf("%s\n", str);
exit(EXIT_SUCCESS);
}
関連項目
getaddrinfo(3), inet(3), inet_ntop(3)
Linux 2008-06-18 INET_PTON(3)
INET_PTON(3) Linux Programmer’s Manual INET_PTON(3)
NAME
inet_pton - convert IPv4 and IPv6 addresses from text to binary form
SYNOPSIS
#include
int inet_pton(int af, const char *src, void *dst);
DESCRIPTION
This function converts the character string src into a network address
structure in the af address family, then copies the network address
structure to dst. The af argument must be either AF_INET or AF_INET6.
The following address families are currently supported:
AF_INET
src points to a character string containing an IPv4 network
address in dotted-decimal format, "ddd.ddd.ddd.ddd", where ddd
is a decimal number of up to three digits in the range 0 to 255.
The address is converted to a struct in_addr and copied to dst,
which must be sizeof(struct in_addr) (4) bytes (32 bits) long.
AF_INET6
src points to a character string containing an IPv6 network
address. The address is converted to a struct in6_addr and
copied to dst, which must be sizeof(struct in6_addr) (16) bytes
(128 bits) long. The allowed formats for IPv6 addresses follow
these rules:
1. The preferred format is x:x:x:x:x:x:x:x. This form consists
of eight hexadecimal numbers, each of which expresses a
16-bit value (i.e., each x can be up to 4 hex digits).
2. A series of contiguous zero values in the preferred format
can be abbreviated to ::. Only one instance of :: can occur
in an address. For example, the loopback address
0:0:0:0:0:0:0:1 can be abbreviated as ::1. The wildcard
address, consisting of all zeroes, can be written as ::.
3. An alternate format is useful for expressing IPv4-mapped IPv6
addresses. This form is written as x:x:x:x:x:x:d.d.d.d,
where the six leading xs are hexadecimal values that define
the six most-significant 16-bit pieces of the address (i.e.,
96 bits), and the ds express a value in dotted-decimal nota-
tion that defines the least significant 32 bits of the
address. An example of such an address is
::FFFF:204.152.189.116.
See RFC 2373 for further details on the representation of IPv6
addresses.
RETURN VALUE
inet_pton() returns 1 on success (network address was successfully con-
verted). 0 is returned if src does not contain a character string rep-
resenting a valid network address in the specified address family. If
af does not contain a valid address family, -1 is returned and errno is
set to EAFNOSUPPORT.
CONFORMING TO
POSIX.1-2001.
NOTES
Unlike inet_aton(3) and inet_addr(3), inet_pton() supports IPv6
addresses. On the other hand, inet_pton() only accepts IPv4 addresses
in dotted-decimal notation, whereas inet_aton(3) and inet_addr(3) allow
the more general numbers-and-dots notation (hexadecimal and octal num-
ber formats, and formats that don’t require all four bytes to be
explicitly written). For an interface that handles both IPv6
addresses, and IPv4 addresses in numbers-and-dots notation, see getad-
drinfo(3).
BUGS
AF_INET6 does not recognize IPv4 addresses. An explicit IPv4-mapped
IPv6 address must be supplied in src instead.
EXAMPLE
The program below demonstrates the use of inet_pton() and inet_ntop(3).
Here are some example runs:
$ ./a.out i6 0:0:0:0:0:0:0:0
::
$ ./a.out i6 1:0:0:0:0:0:0:8
1::8
$ ./a.out i6 0:0:0:0:0:FFFF:204.152.189.116
::ffff:204.152.189.116
Program source
#include
#include
#include
#include
int
main(int argc, char *argv[])
{
unsigned char buf[sizeof(struct in6_addr)];
int domain, s;
char str[INET6_ADDRSTRLEN];
if (argc != 3) {
fprintf(stderr, "Usage: %s {i4|i6|} string\n", argv[0]);
exit(EXIT_FAILURE);
}
domain = (strcmp(argv[1], "i4") == 0) ? AF_INET :
(strcmp(argv[1], "i6") == 0) ? AF_INET6 : atoi(argv[1]);
s = inet_pton(domain, argv[2], buf);
if (s <= 0) {
if (s == 0)
fprintf(stderr, "Not in presentation format");
else
perror("inet_pton");
exit(EXIT_FAILURE);
}
if (inet_ntop(domain, buf, str, INET6_ADDRSTRLEN) == NULL) {
perror("inet_ntop");
exit(EXIT_FAILURE);
}
printf("%s\n", str);
exit(EXIT_SUCCESS);
}
SEE ALSO
getaddrinfo(3), inet(3), inet_ntop(3)
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/.
Linux 2008-06-18 INET_PTON(3)