+.\" Copyright (c) 2008 Petr Baudis <pasky@suse.cz>
+.\" and copyright (c) 2009, Linux Foundation, written by Michael Kerrisk
+.\" <mtk.manpages@gmail.com>
+.\"
+.\" %%%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
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\"
+.\" 2008-12-08 Petr Baudis <pasky@suse.cz>
+.\" Rewrite the BSD manpage in the Linux man pages style and account
+.\" for glibc specificities, provide an example.
+.\" 2009-01-14 mtk, many edits and changes, rewrote example program.
+.\"
+.\"*******************************************************************
+.\"
+.\" This file was generated with po4a. Translate the source file.
+.\"
+.\"*******************************************************************
+.TH GETIFADDRS 3 2012\-11\-11 GNU "Linux Programmer's Manual"
+.SH 名前
+getifaddrs, freeifaddrs \- インターフェースのアドレスを取得する
+.SH 書式
+.nf
+\fB#include <sys/types.h>\fP
+\fB#include <ifaddrs.h>\fP
+.sp
+\fBint getifaddrs(struct ifaddrs **\fP\fIifap\fP\fB);\fP
+.sp
+\fBvoid freeifaddrs(struct ifaddrs *\fP\fIifa\fP\fB);\fP
+.fi
+.SH 説明
+\fBgetifaddrs\fP() 関数は、ローカルシステムのネットワークインターフェース情報を表す構造体の連結リストを作成し、
+リストの先頭の要素のアドレスを \fI*ifap\fP に格納する。 リストは \fIifaddrs\fP 構造体で構成される。 \fIifaddrs\fP
+構造体は以下のように定義されている。
+.sp
+.in +4n
+.nf
+struct ifaddrs {
+ struct ifaddrs *ifa_next; /* Next item in list */
+ char *ifa_name; /* Name of interface */
+ unsigned int ifa_flags; /* Flags from SIOCGIFFLAGS */
+ struct sockaddr *ifa_addr; /* Address of interface */
+ struct sockaddr *ifa_netmask; /* Netmask of interface */
+ union {
+ struct sockaddr *ifu_broadaddr;
+ /* Broadcast address of interface */
+ struct sockaddr *ifu_dstaddr;
+ /* Point\-to\-point destination address */
+ } ifa_ifu;
+#define ifa_broadaddr ifa_ifu.ifu_broadaddr
+#define ifa_dstaddr ifa_ifu.ifu_dstaddr
+ void *ifa_data; /* Address\-specific data */
+};
+.fi
+.in
+.PP
+\fIifa_next\fP フィールドにはリストの次の構造体へのポインタが格納される。 この要素がリストの最後の場合には NULL が入る。
+.PP
+.\" The constant
+.\" .B IF NAMESIZE
+.\" indicates the maximum length of this field.
+\fIifa_name\fP は NULL 終端されたインターフェース名を指す。
+.PP
+\fIifa_flags\fP フィールドには、 \fBSIOCGIFFLAGS\fP \fBioctl\fP(2) 操作で返されるインターフェースのフラグが格納される
+(これらのフラグのリストについては \fBnetdevice\fP(7) を参照)。
+.PP
+\fIifa_addr\fP フィールドは、インターフェースのアドレスを格納した構造体へのポインタである (\fIsa_family\fP
+サブフィールドを参照して、アドレス構造体の形式を判別すべきである)。 このフィールドは NULL ポインタとなる場合がある。
+.PP
+\fIifa_netmask\fP フィールドには、 ネットマスクがあるアドレスファミリーであれば、 \fIifa_addr\fP
+に関連付けられたネットマスクを格納した構造体へのポインタが入る。 このフィールドは NULL ポインタとなる場合がある。
+.PP
+\fIifa_flags\fP にビット \fBIFF_BROADCAST\fP か \fBIFF_POINTOPOINT\fP のどちらが設定されているかにより
+(同時にはこれらのどちらか一方だけが設定される)、 \fIifa_broadaddr\fP に \fIifa_addr\fP に対応するブロードキャストが入るか
+(そのアドレスファミリーでブロードキャストがある場合)、 \fIifa_dstaddr\fP に point\-to\-point
+インターフェースの宛先アドレスが入るかが決まる。
+.PP
+\fIifa_data\fP フィールドは、 アドレスファミリー固有のデータが入ったバッファへのポインタである。
+そのインターフェースでこのようなデータがない場合には、 このフィールドは NULL となる。
+.PP
+\fBgetifaddrs\fP() が返すデータは動的に確保される。 必要なくなった際には \fBfreeifaddrs\fP() を使って解放すべきである。
+.SH 返り値
+成功すると、 \fBgetifaddrs\fP は 0 を返す。 エラーの場合、 \-1 が返り、 \fIerrno\fP が適切に設定される。
+.SH エラー
+\fBgetifaddrs\fP() は失敗する場合があり、その場合には \fIerrno\fP には \fBsocket\fP(2), \fBbind\fP(2),
+\fBgetsockname\fP(2), \fBrecvmsg\fP(2), \fBsendto\fP(2), \fBmalloc\fP(3), \fBrealloc\fP(3)
+に対して規定されているエラーのいずれかが設定される。
+.SH バージョン
+\fBgetifaddrs\fP() 関数は glibc 2.3 で初めて登場したが、 glibc 2.3.3 より前のバージョンの実装では IPv4
+アドレスだけがサポートされていた。 IPv6 のサポートは glibc 2.3.3 で追加された。 IPv4 以外のアドレスファミリーが
+\fBgetifaddrs\fP で利用できるのは、 netlink をサポートするカーネルの場合だけである。
+.SH 準拠
+.\" , but the BSD-derived documentation generally
+.\" appears to be confused and obsolete on this point.
+.\" i.e., commonly it still says one of them will be NULL, even if
+.\" the ifa_ifu union is already present
+POSIX.1\-2001 にはない。 この関数は BSDi で初めて登場し、 BSD 系のシステムに存在するが、
+ドキュメント上はかなり動作が異なり、アドレス毎ではなくインターフェース毎に 1 エントリを返す。 このことは、
+インターフェースがアドレスを持たない場合には \fIifa_addr\fP や他のフィールドは実際に NULL になり、 インターフェースに IP
+アドレスが割り当てられている場合には リンクレベルのアドレスは返されない、ということを意味する。 また、 \fIifa_broadaddr\fP と
+\fIifa_dstaddr\fP のどちらを選択するかを決める方法は、 システムにより異なる。
+.SH 注意
+Linux では、 返されるアドレスは通常インターフェースに割り当てられた IPv4 アドレスと IPv6 アドレスになるが、
+これ以外にインターフェース毎に一つ \fBAF_PACKET\fP アドレスも返される。 \fBAF_PACKET\fP アドレスには、
+インターフェースとその物理層に関する低レベルの詳細が格納される。 この場合、 \fIifa_data\fP フィールドには、
+\fI<linux/if_link.h>\fP で定義される \fIstruct rtnl_link_stats\fP (Linux 2.4
+以前では \fI<linux/netdevice.h>\fP で定義される \fIstruct net_device_stats\fP)
+へのポインタが格納される。 この構造体には、インターフェースの様々な属性や統計情報が入る。
+.SH 例
+以下のプログラムは \fBgetifaddrs\fP(), \fBfreeifaddrs\fP(), \fBgetnameinfo\fP(3) の使用例である。
+以下はこのプログラムをあるシステムで実行した際の出力である。
+.in +4n
+.nf
+
+$ \fB./a.out\fP
+lo address family: 17 (AF_PACKET)
+eth0 address family: 17 (AF_PACKET)
+lo address family: 2 (AF_INET)
+ address: <127.0.0.1>
+eth0 address family: 2 (AF_INET)
+ address: <10.1.1.4>
+lo address family: 10 (AF_INET6)
+ address: <::1>
+eth0 address family: 10 (AF_INET6)
+ address: <fe80::2d0:59ff:feda:eb51%eth0>
+.fi
+.in
+.SS プログラムのソース
+\&
+.nf
+#include <arpa/inet.h>
+#include <sys/socket.h>
+#include <netdb.h>
+#include <ifaddrs.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int
+main(int argc, char *argv[])
+{
+ struct ifaddrs *ifaddr, *ifa;
+ int family, s;
+ char host[NI_MAXHOST];
+
+ if (getifaddrs(&ifaddr) == \-1) {
+ perror("getifaddrs");
+ exit(EXIT_FAILURE);
+ }
+
+ /* Walk through linked list, maintaining head pointer so we
+ can free list later */
+
+ for (ifa = ifaddr; ifa != NULL; ifa = ifa\->ifa_next) {
+ if (ifa\->ifa_addr == NULL)
+ continue;
+
+ family = ifa\->ifa_addr\->sa_family;
+
+ /* Display interface name and family (including symbolic
+ form of the latter for the common families) */
+
+ printf("%s\t address family: %d%s\en",
+ ifa\->ifa_name, family,
+ (family == AF_PACKET) ? " (AF_PACKET)" :
+ (family == AF_INET) ? " (AF_INET)" :
+ (family == AF_INET6) ? " (AF_INET6)" : "");
+
+ /* For an AF_INET* interface address, display the address */
+
+ if (family == AF_INET || family == AF_INET6) {
+ s = getnameinfo(ifa\->ifa_addr,
+ (family == AF_INET) ? sizeof(struct sockaddr_in) :
+ sizeof(struct sockaddr_in6),
+ host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
+ if (s != 0) {
+ printf("getnameinfo() failed: %s\en", gai_strerror(s));
+ exit(EXIT_FAILURE);
+ }
+ printf("\etaddress: <%s>\en", host);
+ }
+ }
+
+ freeifaddrs(ifaddr);
+ exit(EXIT_SUCCESS);
+}
+.fi
+.SH 関連項目
+\fBbind\fP(2), \fBgetsockname\fP(2), \fBsocket\fP(2), \fBpacket\fP(7), \fBifconfig\fP(8)
+.SH この文書について
+この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.52 の一部
+である。プロジェクトの説明とバグ報告に関する情報は
+http://www.kernel.org/doc/man\-pages/ に書かれている。