xref: /minix3/lib/libc/net/getifaddrs.3 (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1.\"	$NetBSD: getifaddrs.3,v 1.14 2013/04/07 23:12:36 wiz Exp $
2.\"	BSDI	getifaddrs.3,v 2.5 2000/02/23 14:51:59 dab Exp
3.\"
4.\" Copyright (c) 1995, 1999
5.\"	Berkeley Software Design, Inc.  All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\"
13.\" THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
14.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16.\" ARE DISCLAIMED.  IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
17.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23.\" SUCH DAMAGE.
24.Dd April 7, 2013
25.Dt GETIFADDRS 3
26.Os
27.Sh NAME
28.Nm getifaddrs
29.Nd get interface addresses
30.Sh SYNOPSIS
31.In sys/types.h
32.In sys/socket.h
33.In ifaddrs.h
34.Ft int
35.Fn getifaddrs "struct ifaddrs **ifap"
36.Ft void
37.Fn freeifaddrs "struct ifaddrs *ifp"
38.Sh DESCRIPTION
39The
40.Fn getifaddrs
41function stores a reference to a linked list of the network interfaces
42on the local machine in the memory referenced by
43.Fa ifap .
44The list consists of
45.Nm ifaddrs
46structures, as defined in the include file
47.In ifaddrs.h .
48The
49.Nm ifaddrs
50structure contains at least the following entries:
51.Bd -literal
52    struct ifaddrs   *ifa_next;         /* Pointer to next struct */
53    char             *ifa_name;         /* Interface name */
54    u_int             ifa_flags;        /* Interface flags */
55    struct sockaddr  *ifa_addr;         /* Interface address */
56    struct sockaddr  *ifa_netmask;      /* Interface netmask */
57    struct sockaddr  *ifa_broadaddr;    /* Interface broadcast address */
58    struct sockaddr  *ifa_dstaddr;      /* P2P interface destination */
59    void             *ifa_data;		/* Address specific data */
60.Ed
61.Pp
62The
63.Li ifa_next
64field contains a pointer to the next structure on the list.
65This field is
66.Dv NULL
67in last structure on the list.
68.Pp
69The
70.Li ifa_name
71field contains the interface name.
72.Pp
73The
74.Li ifa_flags
75field contains the interface flags, as set by
76.Xr ifconfig 8
77utility.
78.Pp
79The
80.Li ifa_addr
81field references either the address of the interface or the link level
82address of the interface, if one exists, otherwise it is
83.Dv NULL .
84(The
85.Li sa_family
86field of the
87.Li ifa_addr
88field should be consulted to determine the format of the
89.Li ifa_addr
90address.)
91.Pp
92The
93.Li ifa_netmask
94field references the netmask associated with
95.Li ifa_addr ,
96if one is set, otherwise it is
97.Dv NULL .
98.Pp
99The
100.Li ifa_broadaddr
101field,
102which should only be referenced for non-P2P interfaces,
103references the broadcast address associated with
104.Li ifa_addr ,
105if one exists, otherwise it is
106.Dv NULL .
107.Pp
108The
109.Li ifa_dstaddr
110field references the destination address on a P2P interface,
111if one exists, otherwise it is
112.Dv NULL .
113.Pp
114The
115.Li ifa_data
116field references address family specific data.
117For
118.Dv AF_LINK
119addresses it contains a pointer to the
120.Fa struct if_data
121.Pq as defined in include file Aq Pa net/if.h
122which contains various interface attributes and statistics.
123For all other address families, it is
124.Dv NULL .
125.Pp
126The data returned by
127.Fn getifaddrs
128is dynamically allocated and should be freed using
129.Fn freeifaddrs
130when no longer needed.
131.Sh RETURN VALUES
132Upon successful completion, a value of 0 is returned.
133Otherwise, a value of -1 is returned and
134.Va errno
135is set to indicate the error.
136.Sh EXAMPLES
137The following example program prints a list of all addresses configured
138on the system.
139.Bd -literal -offset indent
140#include \*[Lt]sys/types.h\*[Gt]
141#include \*[Lt]sys/socket.h\*[Gt]
142#include \*[Lt]stdio.h\*[Gt]
143#include \*[Lt]ifaddrs.h\*[Gt]
144#include \*[Lt]util.h\*[Gt]
145#include \*[Lt]err.h\*[Gt]
146#include \*[Lt]stdlib.h\*[Gt]
147
148int
149main(int argc, char *argv[])
150{
151	struct ifaddrs *ifa, *a;
152
153	if (getifaddrs(\*[Am]ifa) == -1)
154		err(EXIT_FAILURE, "getifaddrs");
155
156	for (a = ifa; a; a = a->ifa_next) {
157		char buf[1024];
158		sockaddr_snprintf(buf, sizeof(buf), "%f %a",
159		    a->ifa_addr);
160		printf("%s %x %s\\n", a->ifa_name, a->ifa_flags, buf);
161	}
162	freeifaddrs(ifa);
163	return EXIT_SUCCESS;
164}
165.Ed
166.Sh ERRORS
167The
168.Fn getifaddrs
169may fail and set
170.Va errno
171for any of the errors specified for the library routines
172.Xr ioctl 2 ,
173.Xr socket 2 ,
174.Xr malloc 3
175or
176.Xr sysctl 3 .
177.Sh SEE ALSO
178.Xr ioctl 2 ,
179.Xr socket 2 ,
180.Xr sysctl 3 ,
181.Xr networking 4 ,
182.Xr ifconfig 8
183.Sh HISTORY
184The
185.Nm
186implementation first appeared in
187.Bsx .
188.Sh BUGS
189If both
190.In net/if.h
191and
192.In ifaddrs.h
193are being included,
194.In net/if.h
195.Em must
196be included before
197.In ifaddrs.h .
198