]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/getifaddrs.3
man*/: srcfix (Use .P instead of .PP or .LP)
[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 .P
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 .P
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 .P
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 .P
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 .P
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 .P
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 .P
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 .P
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 .P
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 .P
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 .SH STANDARDS
161 None.
162 .SH HISTORY
163 This function first appeared in BSDi and is
164 present on the BSD systems, but with slightly different
165 semantics documented\[em]returning one entry per interface,
166 not per address.
167 This means
168 .I ifa_addr
169 and other fields can actually be NULL if the interface has no address,
170 and no link-level address is returned if the interface has an IP address
171 assigned.
172 Also, the way of choosing either
173 .I ifa_broadaddr
174 or
175 .I ifa_dstaddr
176 differs on various systems.
177 .\" , but the BSD-derived documentation generally
178 .\" appears to be confused and obsolete on this point.
179 .\" i.e., commonly it still says one of them will be NULL, even if
180 .\" the ifa_ifu union is already present
181 .P
182 .BR getifaddrs ()
183 first appeared in glibc 2.3, but before glibc 2.3.3,
184 the implementation supported only IPv4 addresses;
185 IPv6 support was added in glibc 2.3.3.
186 Support of address families other than IPv4 is available only
187 on kernels that support netlink.
188 .SH NOTES
189 The addresses returned on Linux will usually be the IPv4 and IPv6 addresses
190 assigned to the interface, but also one
191 .B AF_PACKET
192 address per interface containing lower-level details about the interface
193 and its physical layer.
194 In this case, the
195 .I ifa_data
196 field may contain a pointer to a
197 .IR "struct rtnl_link_stats" ,
198 defined in
199 .I <linux/if_link.h>
200 (in Linux 2.4 and earlier,
201 .IR "struct net_device_stats" ,
202 defined in
203 .IR <linux/netdevice.h> ),
204 which contains various interface attributes and statistics.
205 .SH EXAMPLES
206 The program below demonstrates the use of
207 .BR getifaddrs (),
208 .BR freeifaddrs (),
209 and
210 .BR getnameinfo (3).
211 Here is what we see when running this program on one system:
212 .P
213 .in +4n
214 .EX
215 $ \fB./a.out\fP
216 lo AF_PACKET (17)
217 tx_packets = 524; rx_packets = 524
218 tx_bytes = 38788; rx_bytes = 38788
219 wlp3s0 AF_PACKET (17)
220 tx_packets = 108391; rx_packets = 130245
221 tx_bytes = 30420659; rx_bytes = 94230014
222 em1 AF_PACKET (17)
223 tx_packets = 0; rx_packets = 0
224 tx_bytes = 0; rx_bytes = 0
225 lo AF_INET (2)
226 address: <127.0.0.1>
227 wlp3s0 AF_INET (2)
228 address: <192.168.235.137>
229 lo AF_INET6 (10)
230 address: <::1>
231 wlp3s0 AF_INET6 (10)
232 address: <fe80::7ee9:d3ff:fef5:1a91%wlp3s0>
233 .EE
234 .in
235 .SS Program source
236 \&
237 .EX
238 #define _GNU_SOURCE /* To get defns of NI_MAXSERV and NI_MAXHOST */
239 #include <arpa/inet.h>
240 #include <sys/socket.h>
241 #include <netdb.h>
242 #include <ifaddrs.h>
243 #include <stdio.h>
244 #include <stdlib.h>
245 #include <unistd.h>
246 #include <linux/if_link.h>
247 \&
248 int main(int argc, char *argv[])
249 {
250 struct ifaddrs *ifaddr;
251 int family, s;
252 char host[NI_MAXHOST];
253 \&
254 if (getifaddrs(&ifaddr) == \-1) {
255 perror("getifaddrs");
256 exit(EXIT_FAILURE);
257 }
258 \&
259 /* Walk through linked list, maintaining head pointer so we
260 can free list later. */
261 \&
262 for (struct ifaddrs *ifa = ifaddr; ifa != NULL;
263 ifa = ifa\->ifa_next) {
264 if (ifa\->ifa_addr == NULL)
265 continue;
266 \&
267 family = ifa\->ifa_addr\->sa_family;
268 \&
269 /* Display interface name and family (including symbolic
270 form of the latter for the common families). */
271 \&
272 printf("%\-8s %s (%d)\en",
273 ifa\->ifa_name,
274 (family == AF_PACKET) ? "AF_PACKET" :
275 (family == AF_INET) ? "AF_INET" :
276 (family == AF_INET6) ? "AF_INET6" : "???",
277 family);
278 \&
279 /* For an AF_INET* interface address, display the address. */
280 \&
281 if (family == AF_INET || family == AF_INET6) {
282 s = getnameinfo(ifa\->ifa_addr,
283 (family == AF_INET) ? sizeof(struct sockaddr_in) :
284 sizeof(struct sockaddr_in6),
285 host, NI_MAXHOST,
286 NULL, 0, NI_NUMERICHOST);
287 if (s != 0) {
288 printf("getnameinfo() failed: %s\en", gai_strerror(s));
289 exit(EXIT_FAILURE);
290 }
291 \&
292 printf("\et\etaddress: <%s>\en", host);
293 \&
294 } else if (family == AF_PACKET && ifa\->ifa_data != NULL) {
295 struct rtnl_link_stats *stats = ifa\->ifa_data;
296 \&
297 printf("\et\ettx_packets = %10u; rx_packets = %10u\en"
298 "\et\ettx_bytes = %10u; rx_bytes = %10u\en",
299 stats\->tx_packets, stats\->rx_packets,
300 stats\->tx_bytes, stats\->rx_bytes);
301 }
302 }
303 \&
304 freeifaddrs(ifaddr);
305 exit(EXIT_SUCCESS);
306 }
307 .EE
308 .SH SEE ALSO
309 .BR bind (2),
310 .BR getsockname (2),
311 .BR socket (2),
312 .BR packet (7),
313 .BR ifconfig (8)