2 * Copyright (c) 1996, 1998-2003 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 * Sponsored in part by the Defense Advanced Research Projects
17 * Agency (DARPA) and Air Force Research Laboratory, Air Force
18 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
22 * Supress a warning w/ gcc on Digital UN*X.
23 * The system headers should really do this....
25 #if defined(__osf__) && !defined(__cplusplus)
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <sys/param.h>
36 #include <sys/ioctl.h>
37 #if defined(HAVE_SYS_SOCKIO_H) && !defined(SIOCGIFCONF)
38 # include <sys/sockio.h>
48 #endif /* STDC_HEADERS */
50 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
55 # ifdef HAVE_STRINGS_H
58 #endif /* HAVE_STRING_H */
61 #endif /* HAVE_UNISTD_H */
65 # include "emul/err.h"
66 #endif /* HAVE_ERR_H */
69 # include <sys/stream.h>
70 # include <sys/sioctl.h>
71 # include <sys/stropts.h>
72 # define STRSET(cmd, param, len) {strioctl.ic_cmd=(cmd);\
73 strioctl.ic_dp=(param);\
74 strioctl.ic_timout=0;\
75 strioctl.ic_len=(len);}
78 # include <net/soioctl.h>
80 #include <netinet/in.h>
81 #include <arpa/inet.h>
83 #ifdef HAVE_GETIFADDRS
88 #include "interfaces.h"
91 static const char rcsid[] = "$Sudo: interfaces.c,v 1.72 2004/02/13 21:36:43 millert Exp $";
95 #ifdef HAVE_GETIFADDRS
98 * Allocate and fill in the interfaces global variable with the
99 * machine's ip addresses and netmasks.
104 struct ifaddrs *ifa, *ifaddrs;
105 /* XXX - sockaddr_in6 sin6; */
106 struct sockaddr_in *sin;
109 if (getifaddrs(&ifaddrs))
112 /* Allocate space for the interfaces list. */
113 for (ifa = ifaddrs; ifa != NULL; ifa = ifa -> ifa_next) {
114 /* Skip interfaces marked "down" and "loopback". */
115 if (ifa->ifa_addr == NULL || !ISSET(ifa->ifa_flags, IFF_UP) ||
116 ISSET(ifa->ifa_flags, IFF_LOOPBACK))
119 switch(ifa->ifa_addr->sa_family) {
126 if (num_interfaces == 0)
129 (struct interface *) emalloc2(num_interfaces, sizeof(struct interface));
131 /* Store the ip addr / netmask pairs. */
132 for (ifa = ifaddrs, i = 0; ifa != NULL; ifa = ifa -> ifa_next) {
133 /* Skip interfaces marked "down" and "loopback". */
134 if (ifa->ifa_addr == NULL || !ISSET(ifa->ifa_flags, IFF_UP) ||
135 ISSET(ifa->ifa_flags, IFF_LOOPBACK))
138 switch(ifa->ifa_addr->sa_family) {
141 sin = (struct sockaddr_in *)ifa->ifa_addr;
142 memcpy(&interfaces[i].addr, &sin->sin_addr,
143 sizeof(struct in_addr));
144 sin = (struct sockaddr_in *)ifa->ifa_netmask;
145 memcpy(&interfaces[i].netmask, &sin->sin_addr,
146 sizeof(struct in_addr));
151 #ifdef HAVE_FREEIFADDRS
152 freeifaddrs(ifaddrs);
158 #elif defined(SIOCGIFCONF) && !defined(STUB_LOAD_INTERFACES)
161 * Allocate and fill in the interfaces global variable with the
162 * machine's ip addresses and netmasks.
167 struct ifconf *ifconf;
168 struct ifreq *ifr, ifr_tmp;
169 struct sockaddr_in *sin;
171 size_t len = sizeof(struct ifconf) + BUFSIZ;
172 char *previfname = "", *ifconf_buf = NULL;
174 struct strioctl strioctl;
177 sock = socket(AF_INET, SOCK_DGRAM, 0);
179 err(1, "cannot open socket");
182 * Get interface configuration or return (leaving num_interfaces == 0)
185 ifconf_buf = erealloc(ifconf_buf, len);
186 ifconf = (struct ifconf *) ifconf_buf;
187 ifconf->ifc_len = len - sizeof(struct ifconf);
188 ifconf->ifc_buf = (caddr_t) (ifconf_buf + sizeof(struct ifconf));
190 /* Networking may not be installed in kernel... */
192 STRSET(SIOCGIFCONF, (caddr_t) ifconf, len);
193 if (ioctl(sock, I_STR, (caddr_t) &strioctl) < 0) {
195 if (ioctl(sock, SIOCGIFCONF, (caddr_t) ifconf) < 0) {
202 /* Break out of loop if we have a big enough buffer. */
203 if (ifconf->ifc_len + sizeof(struct ifreq) < len)
208 /* Allocate space for the maximum number of interfaces that could exist. */
209 if ((n = ifconf->ifc_len / sizeof(struct ifreq)) == 0)
211 interfaces = (struct interface *) emalloc2(n, sizeof(struct interface));
213 /* For each interface, store the ip address and netmask. */
214 for (i = 0; i < ifconf->ifc_len; ) {
215 /* Get a pointer to the current interface. */
216 ifr = (struct ifreq *) &ifconf->ifc_buf[i];
218 /* Set i to the subscript of the next interface. */
219 i += sizeof(struct ifreq);
221 if (ifr->ifr_addr.sa_len > sizeof(ifr->ifr_addr))
222 i += ifr->ifr_addr.sa_len - sizeof(struct sockaddr);
223 #endif /* HAVE_SA_LEN */
225 /* Skip duplicates and interfaces with NULL addresses. */
226 sin = (struct sockaddr_in *) &ifr->ifr_addr;
227 if (sin->sin_addr.s_addr == 0 ||
228 strncmp(previfname, ifr->ifr_name, sizeof(ifr->ifr_name) - 1) == 0)
231 if (ifr->ifr_addr.sa_family != AF_INET)
235 memset(&ifr_tmp, 0, sizeof(ifr_tmp));
236 strncpy(ifr_tmp.ifr_name, ifr->ifr_name, sizeof(ifr_tmp.ifr_name) - 1);
237 if (ioctl(sock, SIOCGIFFLAGS, (caddr_t) &ifr_tmp) < 0)
241 /* Skip interfaces marked "down" and "loopback". */
242 if (!ISSET(ifr_tmp.ifr_flags, IFF_UP) ||
243 ISSET(ifr_tmp.ifr_flags, IFF_LOOPBACK))
246 sin = (struct sockaddr_in *) &ifr->ifr_addr;
247 interfaces[num_interfaces].addr.s_addr = sin->sin_addr.s_addr;
249 /* Stash the name of the interface we saved. */
250 previfname = ifr->ifr_name;
252 /* Get the netmask. */
253 (void) memset(&ifr_tmp, 0, sizeof(ifr_tmp));
254 strncpy(ifr_tmp.ifr_name, ifr->ifr_name, sizeof(ifr_tmp.ifr_name) - 1);
255 #ifdef SIOCGIFNETMASK
257 STRSET(SIOCGIFNETMASK, (caddr_t) &ifr_tmp, sizeof(ifr_tmp));
258 if (ioctl(sock, I_STR, (caddr_t) &strioctl) == 0) {
260 if (ioctl(sock, SIOCGIFNETMASK, (caddr_t) &ifr_tmp) == 0) {
262 sin = (struct sockaddr_in *) &ifr_tmp.ifr_addr;
264 interfaces[num_interfaces].netmask.s_addr = sin->sin_addr.s_addr;
268 #endif /* SIOCGIFNETMASK */
269 if (IN_CLASSC(interfaces[num_interfaces].addr.s_addr))
270 interfaces[num_interfaces].netmask.s_addr = htonl(IN_CLASSC_NET);
271 else if (IN_CLASSB(interfaces[num_interfaces].addr.s_addr))
272 interfaces[num_interfaces].netmask.s_addr = htonl(IN_CLASSB_NET);
274 interfaces[num_interfaces].netmask.s_addr = htonl(IN_CLASSA_NET);
277 /* Only now can we be sure it was a good/interesting interface. */
281 /* If the expected size < real size, realloc the array. */
282 if (n != num_interfaces) {
283 if (num_interfaces != 0)
284 interfaces = (struct interface *) erealloc3(interfaces,
285 num_interfaces, sizeof(struct interface));
293 #else /* !SIOCGIFCONF || STUB_LOAD_INTERFACES */
296 * Stub function for those without SIOCGIFCONF
304 #endif /* SIOCGIFCONF && !STUB_LOAD_INTERFACES */
311 puts("Local IP address and netmask pairs:");
312 for (i = 0; i < num_interfaces; i++)
313 printf("\t%s / 0x%x\n", inet_ntoa(interfaces[i].addr),
314 (unsigned int)ntohl(interfaces[i].netmask.s_addr));