2 * Copyright (c) 1996, 1998-2005, 2007-2010
3 * Todd C. Miller <Todd.Miller@courtesan.com>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * Sponsored in part by the Defense Advanced Research Projects
18 * Agency (DARPA) and Air Force Research Laboratory, Air Force
19 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
23 * Suppress a warning w/ gcc on Digital UN*X.
24 * The system headers should really do this....
26 #if defined(__osf__) && !defined(__cplusplus)
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <sys/param.h>
37 #include <sys/ioctl.h>
38 #if defined(HAVE_SYS_SOCKIO_H) && !defined(SIOCGIFCONF)
39 # include <sys/sockio.h>
49 #endif /* STDC_HEADERS */
51 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
55 #endif /* HAVE_STRING_H */
58 #endif /* HAVE_STRINGS_H */
61 #endif /* HAVE_UNISTD_H */
65 # include <sys/stream.h>
66 # include <sys/sioctl.h>
67 # include <sys/stropts.h>
68 # define STRSET(cmd, param, len) {strioctl.ic_cmd=(cmd);\
69 strioctl.ic_dp=(param);\
70 strioctl.ic_timout=0;\
71 strioctl.ic_len=(len);}
74 # include <net/soioctl.h>
76 #include <netinet/in.h>
77 #include <arpa/inet.h>
79 #ifdef HAVE_GETIFADDRS
87 /* Minix apparently lacks IFF_LOOPBACK */
89 # define IFF_LOOPBACK 0
92 #ifndef INET6_ADDRSTRLEN
93 # define INET6_ADDRSTRLEN 46
96 #ifdef HAVE_GETIFADDRS
99 * Fill in the interfaces string with the machine's ip addresses and netmasks
100 * and return the number of interfaces found.
103 get_net_ifs(char **addrinfo)
105 struct ifaddrs *ifa, *ifaddrs;
106 struct sockaddr_in *sin;
108 struct sockaddr_in6 *sin6;
109 char addrbuf[INET6_ADDRSTRLEN];
111 int ailen, i, len, num_interfaces = 0;
114 if (getifaddrs(&ifaddrs))
117 /* Allocate space for the interfaces info string. */
118 for (ifa = ifaddrs; ifa != NULL; ifa = ifa -> ifa_next) {
119 /* Skip interfaces marked "down" and "loopback". */
120 if (ifa->ifa_addr == NULL || ifa->ifa_netmask == NULL ||
121 !ISSET(ifa->ifa_flags, IFF_UP) || ISSET(ifa->ifa_flags, IFF_LOOPBACK))
124 switch (ifa->ifa_addr->sa_family) {
133 if (num_interfaces == 0)
135 ailen = num_interfaces * 2 * INET6_ADDRSTRLEN;
136 *addrinfo = cp = emalloc(ailen);
138 /* Store the IP addr/netmask pairs. */
139 for (ifa = ifaddrs, i = 0; ifa != NULL; ifa = ifa -> ifa_next) {
140 /* Skip interfaces marked "down" and "loopback". */
141 if (ifa->ifa_addr == NULL || ifa->ifa_netmask == NULL ||
142 !ISSET(ifa->ifa_flags, IFF_UP) || ISSET(ifa->ifa_flags, IFF_LOOPBACK))
145 switch (ifa->ifa_addr->sa_family) {
147 sin = (struct sockaddr_in *)ifa->ifa_addr;
148 len = snprintf(cp, ailen - (*addrinfo - cp),
149 "%s%s/", cp == *addrinfo ? "" : " ",
150 inet_ntoa(sin->sin_addr));
151 if (len <= 0 || len >= ailen - (*addrinfo - cp)) {
152 warningx("load_interfaces: overflow detected");
157 sin = (struct sockaddr_in *)ifa->ifa_netmask;
158 len = snprintf(cp, ailen - (*addrinfo - cp),
159 "%s", inet_ntoa(sin->sin_addr));
160 if (len <= 0 || len >= ailen - (*addrinfo - cp)) {
161 warningx("load_interfaces: overflow detected");
168 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
169 inet_ntop(AF_INET6, &sin6->sin6_addr, addrbuf, sizeof(addrbuf));
170 len = snprintf(cp, ailen - (*addrinfo - cp),
171 "%s%s/", cp == *addrinfo ? "" : " ", addrbuf);
172 if (len <= 0 || len >= ailen - (*addrinfo - cp)) {
173 warningx("load_interfaces: overflow detected");
178 sin6 = (struct sockaddr_in6 *)ifa->ifa_netmask;
179 inet_ntop(AF_INET6, &sin6->sin6_addr, addrbuf, sizeof(addrbuf));
180 len = snprintf(cp, ailen - (*addrinfo - cp), "%s", addrbuf);
181 if (len <= 0 || len >= ailen - (*addrinfo - cp)) {
182 warningx("load_interfaces: overflow detected");
187 #endif /* HAVE_IN6_ADDR */
192 #ifdef HAVE_FREEIFADDRS
193 freeifaddrs(ifaddrs);
197 return num_interfaces;
200 #elif defined(SIOCGIFCONF) && !defined(STUB_LOAD_INTERFACES)
203 * Allocate and fill in the interfaces global variable with the
204 * machine's ip addresses and netmasks.
207 get_net_ifs(char **addrinfo)
209 struct ifconf *ifconf;
210 struct ifreq *ifr, ifr_tmp;
211 struct sockaddr_in *sin;
212 int ailen, i, len, n, sock, num_interfaces = 0;
213 size_t buflen = sizeof(struct ifconf) + BUFSIZ;
214 char *cp, *previfname = "", *ifconf_buf = NULL;
216 struct strioctl strioctl;
219 sock = socket(AF_INET, SOCK_DGRAM, 0);
221 error(1, "cannot open socket");
224 * Get interface configuration or return.
227 ifconf_buf = emalloc(buflen);
228 ifconf = (struct ifconf *) ifconf_buf;
229 ifconf->ifc_len = buflen - sizeof(struct ifconf);
230 ifconf->ifc_buf = (caddr_t) (ifconf_buf + sizeof(struct ifconf));
233 STRSET(SIOCGIFCONF, (caddr_t) ifconf, buflen);
234 if (ioctl(sock, I_STR, (caddr_t) &strioctl) < 0)
236 /* Note that some kernels return EINVAL if the buffer is too small */
237 if (ioctl(sock, SIOCGIFCONF, (caddr_t) ifconf) < 0 && errno != EINVAL)
241 /* Break out of loop if we have a big enough buffer. */
242 if (ifconf->ifc_len + sizeof(struct ifreq) < buflen)
248 /* Allocate space for the maximum number of interfaces that could exist. */
249 if ((n = ifconf->ifc_len / sizeof(struct ifreq)) == 0)
251 ailen = n * 2 * INET6_ADDRSTRLEN;
252 *addrinfo = cp = emalloc(ailen);
254 /* For each interface, store the ip address and netmask. */
255 for (i = 0; i < ifconf->ifc_len; ) {
256 /* Get a pointer to the current interface. */
257 ifr = (struct ifreq *) &ifconf->ifc_buf[i];
259 /* Set i to the subscript of the next interface. */
260 i += sizeof(struct ifreq);
262 if (ifr->ifr_addr.sa_len > sizeof(ifr->ifr_addr))
263 i += ifr->ifr_addr.sa_len - sizeof(struct sockaddr);
264 #endif /* HAVE_SA_LEN */
266 /* Skip duplicates and interfaces with NULL addresses. */
267 sin = (struct sockaddr_in *) &ifr->ifr_addr;
268 if (sin->sin_addr.s_addr == 0 ||
269 strncmp(previfname, ifr->ifr_name, sizeof(ifr->ifr_name) - 1) == 0)
272 if (ifr->ifr_addr.sa_family != AF_INET)
276 memset(&ifr_tmp, 0, sizeof(ifr_tmp));
277 strncpy(ifr_tmp.ifr_name, ifr->ifr_name, sizeof(ifr_tmp.ifr_name) - 1);
278 if (ioctl(sock, SIOCGIFFLAGS, (caddr_t) &ifr_tmp) < 0)
282 /* Skip interfaces marked "down" and "loopback". */
283 if (!ISSET(ifr_tmp.ifr_flags, IFF_UP) ||
284 ISSET(ifr_tmp.ifr_flags, IFF_LOOPBACK))
287 sin = (struct sockaddr_in *) &ifr->ifr_addr;
288 len = snprintf(cp, ailen - (*addrinfo - cp),
289 "%s%s/", cp == *addrinfo ? "" : " ",
290 inet_ntoa(sin->sin_addr));
291 if (len <= 0 || len >= ailen - (*addrinfo - cp)) {
292 warningx("load_interfaces: overflow detected");
297 /* Stash the name of the interface we saved. */
298 previfname = ifr->ifr_name;
300 /* Get the netmask. */
301 memset(&ifr_tmp, 0, sizeof(ifr_tmp));
302 strncpy(ifr_tmp.ifr_name, ifr->ifr_name, sizeof(ifr_tmp.ifr_name) - 1);
304 STRSET(SIOCGIFNETMASK, (caddr_t) &ifr_tmp, sizeof(ifr_tmp));
305 if (ioctl(sock, I_STR, (caddr_t) &strioctl) < 0) {
307 if (ioctl(sock, SIOCGIFNETMASK, (caddr_t) &ifr_tmp) < 0) {
309 sin = (struct sockaddr_in *) &ifr_tmp.ifr_addr;
310 sin->sin_addr.s_addr = htonl(IN_CLASSC_NET);
312 sin = (struct sockaddr_in *) &ifr_tmp.ifr_addr;
313 len = snprintf(cp, ailen - (*addrinfo - cp),
314 "%s", inet_ntoa(sin->sin_addr));
315 if (len <= 0 || len >= ailen - (*addrinfo - cp)) {
316 warningx("load_interfaces: overflow detected");
327 return num_interfaces;
330 #else /* !SIOCGIFCONF || STUB_LOAD_INTERFACES */
333 * Stub function for those without SIOCGIFCONF or getifaddrs()
336 get_net_ifs(char **addrinfo)
341 #endif /* SIOCGIFCONF && !STUB_LOAD_INTERFACES */