3729 getifaddrs must learn to stop worrying and love the other address families
[unleashed.git] / usr / src / man / man3socket / getifaddrs.3socket
blob4b9c40b7f5366a88c98bebf3cbc4b553fccf6ae7
1 '\" t
2 .\"
3 .\" This file and its contents are supplied under the terms of the
4 .\" Common Development and Distribution License ("CDDL"), version 1.0.
5 .\" You may only use this file in accordance with the terms of version
6 .\" 1.0 of the CDDL.
7 .\"
8 .\" A full copy of the text of the CDDL should have accompanied this
9 .\" source.  A copy of the CDDL is also available via the Internet at
10 .\" http://www.illumos.org/license/CDDL.
11 .\"
12 .\"
13 .\" Copyright (c) 2013, Joyent, Inc. All rights reserved.
14 .\"
15 .TH GETIFADDRS 3SOCKET "May 18, 2017"
16 .SH NAME
17 getifaddrs, freeifaddrs \- get interface addresses
18 .SH SYNOPSIS
19 .LP
20 .nf
21 \fBcc\fR [ \fIflag\fR ... ] \fIfile\fR ... \fB-lsocket\fR \fB-lnsl\fR \
22 [ \fIlibrary\fR ... ]
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <ifaddrs.h>
26 .fi
28 .LP
29 .nf
30 \fBint\fR \fBgetifaddrs\fR(\fBstruct ifaddrs **\fR\fIifap\fR);
31 .fi
33 .LP
34 .nf
35 \fBvoid\fR \fBfreeifaddrs\fR(\fBstruct ifaddrs *\fR\fIifp\fR);
36 .fi
38 .SH DESCRIPTION
39 .LP
40 The \fBgetifaddrs\fR() function is used to obtain the list of network
41 interfaces on the local machine.  A reference to a linked list of \fBifaddrs\fR
42 structures, as defined in \fB<ifaddrs.h>\fR, is stored in the memory referenced
43 by \fIifap\fR.  Each structure in the list describes one network interface
44 address, and is of the form:
46 .sp
47 .in +2
48 .nf
49 struct ifaddrs {
50     struct ifaddrs      *ifa_next;
51     char                *ifa_name;
52     uint64_t            ifa_flags;
53     struct sockaddr     *ifa_addr;
54     struct sockaddr     *ifa_netmask;
55     union {
56         struct sockaddr *ifu_broadaddr;
57         struct sockaddr *ifu_dstaddr;
58     } ifa_ifu;
59     void                *ifa_data;
61 #define ifa_broadaddr   ifa_ifu.ifu_broadaddr
62 #define ifa_dstaddr     ifa_ifu.ifu_dstaddr
63 .fi
64 .in -2
66 .sp
67 .LP
68 The list is traversed by following the \fIifa_next\fR pointer.  This member is
69 \fBNULL\fR on the last structure in the list.
71 .sp
72 .LP
73 The \fIifa_name\fR member contains the interface name.
75 .sp
76 .LP
77 The \fIifa_flags\fR member contains the interface flags.
79 .sp
80 .LP
81 The \fIifa_addr\fR member references the address of the interface.  Use the
82 \fIsa_family\fR member of this structure to determine the format of the
83 address, as described in \fBsocket.h\fR(3HEAD).
85 .sp
86 .LP
87 The \fIifa_netmask\fR member references the netmask associated with ifa_addr,
88 or \fBNULL\fR if one is not set.
90 .sp
91 .LP
92 If the \fBIFF_BROADCAST\fR bit is set in \fIifa_flags\fR, then
93 \fIifa_broadaddr\fR is valid, or \fBNULL\fR if not present.  If the
94 \fBIFF_POINTOPOINT\fR bit is set, then \fIifa_dstaddr\fR is valid, or \fBNULL\fR
95 if not present.  These two flags are mutually exclusive; see \fBif_tcp\fR(7P)
96 for more information.
98 .sp
99 .LP
100 The \fIifa_data\fR member is specific to the address family. It is currently
101 only available for AF_LINK entries where it contains a pointer to the
102 \fBstruct if_data\fR (as defined in \fBif.h\fR(3HEAD)).
106 The memory used by \fBgetifaddrs\fR() to back the list is dynamically allocated.
107 It should be freed using \fBfreeifaddrs\fR().
109 .SH RETURN VALUES
111 If successful, \fBgetifaddrs\fR() returns the value \fB0\fR; otherwise it
112 returns \fB\(mi1\fR and sets \fIerrno\fR to indicate the error.
114 .SH ERRORS
116 The \fBgetifaddrs\fR() function may fail and set \fIerrno\fR for any of the
117 errors specified for the library routines \fBioctl\fR(2),
118 \fBsocket\fR(3SOCKET), and \fBmalloc\fR(3C).
120 .SH ATTRIBUTES
122 box;
123 c | c
124 l | l .
125 ATTRIBUTE TYPE  ATTRIBUTE VALUE
127 Interface Stability     Committed
129 MT-Level        MT-Safe
132 .SH SEE ALSO
134 \fBipadm\fR(1M), \fBifconfig\fR(1M), \fBioctl\fR(2), \fBmalloc\fR(3C),
135 \fBsocket\fR(3SOCKET), \fBsockaddr\fR(3SOCKET), \fBsocket.h\fR(3HEAD),
136 \fBif_tcp\fR(7P), \fBattributes\fR(5)
138 .SH NOTES
140 This function lists interfaces of type AF_INET, AF_INET6, and AF_LINK.
141 For AF_INET and AF_INET6 only interfaces with the \fBIFF_UP\fR
142 flag set are listed; see \fBif_tcp\fR(7P) and \fBifconfig\fR(1M) for more
143 information. For AF_LINK entries the interface index is only available when the
144 link is plumbed.