2 * Copyright (c) 2003 Bruce M. Simpson.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bruce M. Simpson.
16 * 4. Neither the name of Bruce M. Simpson nor the names of other
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY BRUCE M. SIMPSON AND AFFILIATES
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BRUCE M. SIMPSON OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include "namespace.h"
37 #include <sys/param.h>
38 #include <sys/sysctl.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
42 #include <net/if_dl.h>
43 #include <net/route.h>
49 #include "un-namespace.h"
51 #define SALIGN (sizeof(long) - 1)
52 #define SA_RLEN(sa) ((sa)->sa_len ? (((sa)->sa_len + SALIGN) & ~SALIGN) : \
54 #define MAX_SYSCTL_TRY 5
55 #define RTA_MASKS (RTA_GATEWAY | RTA_IFP | RTA_IFA)
58 getifmaddrs(struct ifmaddrs **pif)
71 struct ifma_msghdr *ifmam;
72 struct ifmaddrs *ifa, *ift;
73 struct rt_msghdr *rtm;
78 mib[2] = 0; /* protocol */
79 mib[3] = 0; /* wildcard address family */
80 mib[4] = NET_RT_IFMALIST;
81 mib[5] = 0; /* no flags */
83 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
85 if ((buf = malloc(needed)) == NULL)
87 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
88 if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
95 } while (buf == NULL);
97 for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
98 rtm = (struct rt_msghdr *)(void *)next;
99 if (rtm->rtm_version != RTM_VERSION)
101 switch (rtm->rtm_type) {
103 ifmam = (struct ifma_msghdr *)(void *)rtm;
104 if ((ifmam->ifmam_addrs & RTA_IFA) == 0)
107 p = (char *)(ifmam + 1);
108 for (i = 0; i < RTAX_MAX; i++) {
109 if ((RTA_MASKS & ifmam->ifmam_addrs &
112 sa = (struct sockaddr *)(void *)p;
121 data = malloc(sizeof(struct ifmaddrs) * icnt + dcnt);
127 ifa = (struct ifmaddrs *)(void *)data;
128 data += sizeof(struct ifmaddrs) * icnt;
130 memset(ifa, 0, sizeof(struct ifmaddrs) * icnt);
133 for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
134 rtm = (struct rt_msghdr *)(void *)next;
135 if (rtm->rtm_version != RTM_VERSION)
138 switch (rtm->rtm_type) {
140 ifmam = (struct ifma_msghdr *)(void *)rtm;
141 if ((ifmam->ifmam_addrs & RTA_IFA) == 0)
144 p = (char *)(ifmam + 1);
145 for (i = 0; i < RTAX_MAX; i++) {
146 if ((RTA_MASKS & ifmam->ifmam_addrs &
149 sa = (struct sockaddr *)(void *)p;
154 (struct sockaddr *)(void *)data;
155 memcpy(data, p, len);
161 (struct sockaddr *)(void *)data;
162 memcpy(data, p, len);
168 (struct sockaddr *)(void *)data;
169 memcpy(data, p, len);
179 ift->ifma_next = ift + 1;
180 ift = ift->ifma_next;
189 ift->ifma_next = NULL;
199 freeifmaddrs(struct ifmaddrs *ifmp)