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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include "namespace.h"
31 #include <sys/param.h>
32 #include <sys/sysctl.h>
33 #include <sys/ioctl.h>
34 #include <sys/socket.h>
36 #include <net/if_dl.h>
37 #include <net/route.h>
43 #include "un-namespace.h"
45 #define SALIGN (sizeof(long) - 1)
46 #define SA_RLEN(sa) ((sa)->sa_len ? (((sa)->sa_len + SALIGN) & ~SALIGN) : \
48 #define MAX_SYSCTL_TRY 5
49 #define RTA_MASKS (RTA_GATEWAY | RTA_IFP | RTA_IFA)
52 getifmaddrs(struct ifmaddrs **pif)
65 struct ifma_msghdr *ifmam;
66 struct ifmaddrs *ifa, *ift;
67 struct rt_msghdr *rtm;
72 mib[2] = 0; /* protocol */
73 mib[3] = 0; /* wildcard address family */
74 mib[4] = NET_RT_IFMALIST;
75 mib[5] = 0; /* no flags */
77 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
79 if ((buf = malloc(needed)) == NULL)
81 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
82 if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
89 } while (buf == NULL);
91 for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
92 rtm = (struct rt_msghdr *)(void *)next;
93 if (rtm->rtm_version != RTM_VERSION)
95 switch (rtm->rtm_type) {
97 ifmam = (struct ifma_msghdr *)(void *)rtm;
98 if ((ifmam->ifmam_addrs & RTA_IFA) == 0)
101 p = (char *)(ifmam + 1);
102 for (i = 0; i < RTAX_MAX; i++) {
103 if ((RTA_MASKS & ifmam->ifmam_addrs &
106 sa = (struct sockaddr *)(void *)p;
115 data = malloc(sizeof(struct ifmaddrs) * icnt + dcnt);
121 ifa = (struct ifmaddrs *)(void *)data;
122 data += sizeof(struct ifmaddrs) * icnt;
124 memset(ifa, 0, sizeof(struct ifmaddrs) * icnt);
127 for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
128 rtm = (struct rt_msghdr *)(void *)next;
129 if (rtm->rtm_version != RTM_VERSION)
132 switch (rtm->rtm_type) {
134 ifmam = (struct ifma_msghdr *)(void *)rtm;
135 if ((ifmam->ifmam_addrs & RTA_IFA) == 0)
138 p = (char *)(ifmam + 1);
139 for (i = 0; i < RTAX_MAX; i++) {
140 if ((RTA_MASKS & ifmam->ifmam_addrs &
143 sa = (struct sockaddr *)(void *)p;
148 (struct sockaddr *)(void *)data;
149 memcpy(data, p, len);
155 (struct sockaddr *)(void *)data;
156 memcpy(data, p, len);
162 (struct sockaddr *)(void *)data;
163 memcpy(data, p, len);
173 ift->ifma_next = ift + 1;
174 ift = ift->ifma_next;
183 ift->ifma_next = NULL;
193 freeifmaddrs(struct ifmaddrs *ifmp)