]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/getifaddrs.3
man*/: ffix (un-bracket tables)
[thirdparty/man-pages.git] / man3 / getifaddrs.3
1 '\" t
2 .\" Copyright (c) 2008 Petr Baudis <pasky@suse.cz>
3 .\" and copyright (c) 2009, Linux Foundation, written by Michael Kerrisk
4 .\" <mtk.manpages@gmail.com>
5 .\"
6 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .\"
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
10 .\" are met:
11 .\"
12 .\" 2008-12-08 Petr Baudis <pasky@suse.cz>
13 .\" Rewrite the BSD manpage in the Linux man pages style and account
14 .\" for glibc specificities, provide an example.
15 .\" 2009-01-14 mtk, many edits and changes, rewrote example program.
16 .\"
17 .TH getifaddrs 3 (date) "Linux man-pages (unreleased)"
18 .SH NAME
19 getifaddrs, freeifaddrs \- get interface addresses
20 .SH LIBRARY
21 Standard C library
22 .RI ( libc ", " \-lc )
23 .SH SYNOPSIS
24 .nf
25 .B #include <sys/types.h>
26 .B #include <ifaddrs.h>
27 .PP
28 .BI "int getifaddrs(struct ifaddrs **" "ifap" );
29 .BI "void freeifaddrs(struct ifaddrs *" "ifa" );
30 .fi
31 .SH DESCRIPTION
32 The
33 .BR getifaddrs ()
34 function creates a linked list of structures describing
35 the network interfaces of the local system,
36 and stores the address of the first item of the list in
37 .IR *ifap .
38 The list consists of
39 .I ifaddrs
40 structures, defined as follows:
41 .PP
42 .in +4n
43 .EX
44 struct ifaddrs {
45 struct ifaddrs *ifa_next; /* Next item in list */
46 char *ifa_name; /* Name of interface */
47 unsigned int ifa_flags; /* Flags from SIOCGIFFLAGS */
48 struct sockaddr *ifa_addr; /* Address of interface */
49 struct sockaddr *ifa_netmask; /* Netmask of interface */
50 union {
51 struct sockaddr *ifu_broadaddr;
52 /* Broadcast address of interface */
53 struct sockaddr *ifu_dstaddr;
54 /* Point\-to\-point destination address */
55 } ifa_ifu;
56 #define ifa_broadaddr ifa_ifu.ifu_broadaddr
57 #define ifa_dstaddr ifa_ifu.ifu_dstaddr
58 void *ifa_data; /* Address\-specific data */
59 };
60 .EE
61 .in
62 .PP
63 The
64 .I ifa_next
65 field contains a pointer to the next structure on the list,
66 or NULL if this is the last item of the list.
67 .PP
68 The
69 .I ifa_name
70 points to the null-terminated interface name.
71 .\" The constant
72 .\" .B IF NAMESIZE
73 .\" indicates the maximum length of this field.
74 .PP
75 The
76 .I ifa_flags
77 field contains the interface flags, as returned by the
78 .B SIOCGIFFLAGS
79 .BR ioctl (2)
80 operation (see
81 .BR netdevice (7)
82 for a list of these flags).
83 .PP
84 The
85 .I ifa_addr
86 field points to a structure containing the interface address.
87 (The
88 .I sa_family
89 subfield should be consulted to determine the format of the
90 address structure.)
91 This field may contain a null pointer.
92 .PP
93 The
94 .I ifa_netmask
95 field points to a structure containing the netmask associated with
96 .IR ifa_addr ,
97 if applicable for the address family.
98 This field may contain a null pointer.
99 .PP
100 Depending on whether the bit
101 .B IFF_BROADCAST
102 or
103 .B IFF_POINTOPOINT
104 is set in
105 .I ifa_flags
106 (only one can be set at a time),
107 either
108 .I ifa_broadaddr
109 will contain the broadcast address associated with
110 .I ifa_addr
111 (if applicable for the address family) or
112 .I ifa_dstaddr
113 will contain the destination address of the point-to-point interface.
114 .PP
115 The
116 .I ifa_data
117 field points to a buffer containing address-family-specific data;
118 this field may be NULL if there is no such data for this interface.
119 .PP
120 The data returned by
121 .BR getifaddrs ()
122 is dynamically allocated and should be freed using
123 .BR freeifaddrs ()
124 when no longer needed.
125 .SH RETURN VALUE
126 On success,
127 .BR getifaddrs ()
128 returns zero;
129 on error, \-1 is returned, and
130 .I errno
131 is set to indicate the error.
132 .SH ERRORS
133 .BR getifaddrs ()
134 may fail and set
135 .I errno
136 for any of the errors specified for
137 .BR socket (2),
138 .BR bind (2),
139 .BR getsockname (2),
140 .BR recvmsg (2),
141 .BR sendto (2),
142 .BR malloc (3),
143 or
144 .BR realloc (3).
145 .SH ATTRIBUTES
146 For an explanation of the terms used in this section, see
147 .BR attributes (7).
148 .TS
149 allbox;
150 lbx lb lb
151 l l l.
152 Interface Attribute Value
153 T{
154 .na
155 .nh
156 .BR getifaddrs (),
157 .BR freeifaddrs ()
158 T} Thread safety MT-Safe
159 .TE
160 .sp 1
161 .SH STANDARDS
162 None.
163 .SH HISTORY
164 This function first appeared in BSDi and is
165 present on the BSD systems, but with slightly different
166 semantics documented\[em]returning one entry per interface,
167 not per address.
168 This means
169 .I ifa_addr
170 and other fields can actually be NULL if the interface has no address,
171 and no link-level address is returned if the interface has an IP address
172 assigned.
173 Also, the way of choosing either
174 .I ifa_broadaddr
175 or
176 .I ifa_dstaddr
177 differs on various systems.
178 .\" , but the BSD-derived documentation generally
179 .\" appears to be confused and obsolete on this point.
180 .\" i.e., commonly it still says one of them will be NULL, even if
181 .\" the ifa_ifu union is already present
182 .PP
183 .BR getifaddrs ()
184 first appeared in glibc 2.3, but before glibc 2.3.3,
185 the implementation supported only IPv4 addresses;
186 IPv6 support was added in glibc 2.3.3.
187 Support of address families other than IPv4 is available only
188 on kernels that support netlink.
189 .SH NOTES
190 The addresses returned on Linux will usually be the IPv4 and IPv6 addresses
191 assigned to the interface, but also one
192 .B AF_PACKET
193 address per interface containing lower-level details about the interface
194 and its physical layer.
195 In this case, the
196 .I ifa_data
197 field may contain a pointer to a
198 .IR "struct rtnl_link_stats" ,
199 defined in
200 .I <linux/if_link.h>
201 (in Linux 2.4 and earlier,
202 .IR "struct net_device_stats" ,
203 defined in
204 .IR <linux/netdevice.h> ),
205 which contains various interface attributes and statistics.
206 .SH EXAMPLES
207 The program below demonstrates the use of
208 .BR getifaddrs (),
209 .BR freeifaddrs (),
210 and
211 .BR getnameinfo (3).
212 Here is what we see when running this program on one system:
213 .PP
214 .in +4n
215 .EX
216 $ \fB./a.out\fP
217 lo AF_PACKET (17)
218 tx_packets = 524; rx_packets = 524
219 tx_bytes = 38788; rx_bytes = 38788
220 wlp3s0 AF_PACKET (17)
221 tx_packets = 108391; rx_packets = 130245
222 tx_bytes = 30420659; rx_bytes = 94230014
223 em1 AF_PACKET (17)
224 tx_packets = 0; rx_packets = 0
225 tx_bytes = 0; rx_bytes = 0
226 lo AF_INET (2)
227 address: <127.0.0.1>
228 wlp3s0 AF_INET (2)
229 address: <192.168.235.137>
230 lo AF_INET6 (10)
231 address: <::1>
232 wlp3s0 AF_INET6 (10)
233 address: <fe80::7ee9:d3ff:fef5:1a91%wlp3s0>
234 .EE
235 .in
236 .SS Program source
237 \&
238 .EX
239 #define _GNU_SOURCE /* To get defns of NI_MAXSERV and NI_MAXHOST */
240 #include <arpa/inet.h>
241 #include <sys/socket.h>
242 #include <netdb.h>
243 #include <ifaddrs.h>
244 #include <stdio.h>
245 #include <stdlib.h>
246 #include <unistd.h>
247 #include <linux/if_link.h>
248 \&
249 int main(int argc, char *argv[])
250 {
251 struct ifaddrs *ifaddr;
252 int family, s;
253 char host[NI_MAXHOST];
254 \&
255 if (getifaddrs(&ifaddr) == \-1) {
256 perror("getifaddrs");
257 exit(EXIT_FAILURE);
258 }
259 \&
260 /* Walk through linked list, maintaining head pointer so we
261 can free list later. */
262 \&
263 for (struct ifaddrs *ifa = ifaddr; ifa != NULL;
264 ifa = ifa\->ifa_next) {
265 if (ifa\->ifa_addr == NULL)
266 continue;
267 \&
268 family = ifa\->ifa_addr\->sa_family;
269 \&
270 /* Display interface name and family (including symbolic
271 form of the latter for the common families). */
272 \&
273 printf("%\-8s %s (%d)\en",
274 ifa\->ifa_name,
275 (family == AF_PACKET) ? "AF_PACKET" :
276 (family == AF_INET) ? "AF_INET" :
277 (family == AF_INET6) ? "AF_INET6" : "???",
278 family);
279 \&
280 /* For an AF_INET* interface address, display the address. */
281 \&
282 if (family == AF_INET || family == AF_INET6) {
283 s = getnameinfo(ifa\->ifa_addr,
284 (family == AF_INET) ? sizeof(struct sockaddr_in) :
285 sizeof(struct sockaddr_in6),
286 host, NI_MAXHOST,
287 NULL, 0, NI_NUMERICHOST);
288 if (s != 0) {
289 printf("getnameinfo() failed: %s\en", gai_strerror(s));
290 exit(EXIT_FAILURE);
291 }
292 \&
293 printf("\et\etaddress: <%s>\en", host);
294 \&
295 } else if (family == AF_PACKET && ifa\->ifa_data != NULL) {
296 struct rtnl_link_stats *stats = ifa\->ifa_data;
297 \&
298 printf("\et\ettx_packets = %10u; rx_packets = %10u\en"
299 "\et\ettx_bytes = %10u; rx_bytes = %10u\en",
300 stats\->tx_packets, stats\->rx_packets,
301 stats\->tx_bytes, stats\->rx_bytes);
302 }
303 }
304 \&
305 freeifaddrs(ifaddr);
306 exit(EXIT_SUCCESS);
307 }
308 .EE
309 .SH SEE ALSO
310 .BR bind (2),
311 .BR getsockname (2),
312 .BR socket (2),
313 .BR packet (7),
314 .BR ifconfig (8)