.\" Copyright 2000 Sam Varshavchik .\" and Copyright (c) 2008 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: RFC 2553 .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 2001 NAKANO Takeo all rights reserved. .\" Translated 2001-01-14, NAKANO Takeo .\" Updated 2008-08-08, Akihiro MOTOKI, LDP v3.05 .\" .TH INET_PTON 3 2008\-06\-18 Linux "Linux Programmer's Manual" .SH 名前 inet_pton \- IPv4/IPv6 アドレスをテキスト形式からバイナリ形式に変換する .SH 書式 .nf \fB#include \fP \fBint inet_pton(int \fP\fIaf\fP\fB, const char *\fP\fIsrc\fP\fB, void *\fP\fIdst\fP\fB);\fP .fi .SH 説明 この関数は文字列 \fIsrc\fP を、アドレスファミリー \fIaf\fP のネットワークアドレス構造体に変換し、 \fIdst\fP にコピーする。 \fIaf\fP 引き数は \fBAF_INET\fP か \fBAF_INET6\fP のどちらかでなければならない。 .PP 現在サポートされているアドレスファミリーは以下の通りである。 .TP \fBAF_INET\fP \fIsrc\fP はドット区切りの 10 進数形式 "\fIddd.ddd.ddd.ddd\fP" の IPv4 ネットワークアドレス文字列へのポインターである。 \fIddd\fP は 0 から 255 までの範囲の最大 3 桁の 10 進数である。 このアドレスは \fIstruct in_addr\fP に変換されて \fIdst\fP にコピーされる。 \fIdst\fP の長さは \fIsizeof(struct in_addr)\fP (4) バイト (32ビット) でなければならない。 .TP \fBAF_INET6\fP \fIsrc\fP は IPv6 ネットワークアドレスが格納された文字列へのポインターである。 このアドレスは \fIstruct in6_addr\fP に変換されて \fIdst\fP にコピーされる。 \fIdst\fP の長さは \fIsizeof(struct in6_addr)\fP (16) バイト (128 ビット) でなければならない。 以下の 3 つのルールにしたがった形式が IPv6 アドレスとして入力できる。 .RS .IP 1. 3 推奨形式は \fIx:x:x:x:x:x:x:x\fP である。この形式は 8 個の 16 進数から構成され、 各々の 16 進数は 16 ビット値を表す (\fIx\fP は最大 4 桁の 16 進数である)。 .IP 2. 推奨形式の中の連続する 0 の列は \fI::\fP に短縮できる。アドレス中で使用できる \fI::\fP は 1 個だけである。 例えば、ループバックアドレス \fI0:0:0:0:0:0:0:1\fP は \fI::1\fP と短縮できる。 全ビットが 0 で構成されるワイルドカードアドレスは \fI::\fP と記載できる。 .IP 3. IPv4 をマッピングした IPv6 アドレスを表記するには別の形式が便利である。 この別の形式は \fIx:x:x:x:x:x:d.d.d.d\fP と書くことができる。 最初の 6 個の \fIx\fP はアドレスを 16 ビット単位に区切ったときの上位側 6 個分 (つまり 96 ビット分) を定義する 16 進数であり、 \fId\fP の部分はアドレスの下位 32 ビットをドット区切りの 10 進数表記で表したものである。 \fI::FFFF:204.152.189.116\fP はこの形式の例である。 .RE .IP IPv6 アドレスの表現方法の詳細については RFC 2373 を参照のこと。 .SH 返り値 成功する (ネットワークアドレスが正常に変換される) と、 \fBinet_pton\fP() は 1 を返す。 \fIsrc\fP が指定されたアドレスファミリーに対する 正しいネットワークアドレス表記でない場合には、 0 を返す。 \fIaf\fP がサポートされているアドレスファミリーでない場合には、 \-1 を返し、 \fIerrno\fP に \fBEAFNOSUPPORT\fP を設定する。 .SH 準拠 POSIX.1\-2001. .SH 注意 \fBinet_aton\fP(3) や \fBinet_addr\fP(3) と異なり、 \fBinet_pton\fP() は IPv6 アドレスに対応している。 一方で、 \fBinet_pton\fP() が受け付ける IPv4 アドレスはドット区切りの 10 進数表記だけである。 これに対し、 \fBinet_aton\fP(3) や \fBinet_addr\fP(3) ではもっと一般的なドット区切りの数字表記 (16 進数や 8 進数の形式や、 4 バイト全てを明示的に書かなくてもよい形式) が使用できる。 ドット区切りの数字表記で IPv6 アドレスと IPv4 アドレスの両方を扱える インターフェイスについては、 \fBgetaddrinfo\fP(3) を参照のこと。 .SH バグ \fBAF_INET6\fP は IPv4 アドレスを認識しない。 代わりに IPv4 アドレスをマッピングした IPv6 アドレスを \fIsrc\fP に与えなければならない。 .SH 例 以下のプログラムは \fBinet_pton\fP() と \fBinet_ntop\fP(3) の使用例を示すものである。 実行すると以下のようになる。 .in +4n .nf $\fB ./a.out i6 0:0:0:0:0:0:0:0\fP :: $\fB ./a.out i6 1:0:0:0:0:0:0:8\fP 1::8 $\fB ./a.out i6 0:0:0:0:0:FFFF:204.152.189.116\fP ::ffff:204.152.189.116 .fi .in .SS プログラムのソース \& .nf #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\en", 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\en", str); exit(EXIT_SUCCESS); } .fi .SH 関連項目 \fBgetaddrinfo\fP(3), \fBinet\fP(3), \fBinet_ntop\fP(3) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.79 の一部 である。プロジェクトの説明とバグ報告に関する情報は http://www.kernel.org/doc/man\-pages/ に書かれている。