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
#define DEFAULT_TEXT_DOMAIN "sudo"
90
/* Minix apparently lacks IFF_LOOPBACK */
92
# define IFF_LOOPBACK 0
95
#ifndef INET6_ADDRSTRLEN
96
# define INET6_ADDRSTRLEN 46
99
#ifdef HAVE_GETIFADDRS
102
* Fill in the interfaces string with the machine's ip addresses and netmasks
103
* and return the number of interfaces found.
106
get_net_ifs(char **addrinfo)
108
struct ifaddrs *ifa, *ifaddrs;
109
struct sockaddr_in *sin;
111
struct sockaddr_in6 *sin6;
112
char addrbuf[INET6_ADDRSTRLEN];
114
int ailen, i, len, num_interfaces = 0;
117
if (getifaddrs(&ifaddrs))
120
/* Allocate space for the interfaces info string. */
121
for (ifa = ifaddrs; ifa != NULL; ifa = ifa -> ifa_next) {
122
/* Skip interfaces marked "down" and "loopback". */
123
if (ifa->ifa_addr == NULL || ifa->ifa_netmask == NULL ||
124
!ISSET(ifa->ifa_flags, IFF_UP) || ISSET(ifa->ifa_flags, IFF_LOOPBACK))
127
switch (ifa->ifa_addr->sa_family) {
136
if (num_interfaces == 0)
138
ailen = num_interfaces * 2 * INET6_ADDRSTRLEN;
139
*addrinfo = cp = emalloc(ailen);
141
/* Store the IP addr/netmask pairs. */
142
for (ifa = ifaddrs, i = 0; ifa != NULL; ifa = ifa -> ifa_next) {
143
/* Skip interfaces marked "down" and "loopback". */
144
if (ifa->ifa_addr == NULL || ifa->ifa_netmask == NULL ||
145
!ISSET(ifa->ifa_flags, IFF_UP) || ISSET(ifa->ifa_flags, IFF_LOOPBACK))
148
switch (ifa->ifa_addr->sa_family) {
150
sin = (struct sockaddr_in *)ifa->ifa_addr;
151
len = snprintf(cp, ailen - (*addrinfo - cp),
152
"%s%s/", cp == *addrinfo ? "" : " ",
153
inet_ntoa(sin->sin_addr));
154
if (len <= 0 || len >= ailen - (*addrinfo - cp)) {
155
warningx(_("load_interfaces: overflow detected"));
160
sin = (struct sockaddr_in *)ifa->ifa_netmask;
161
len = snprintf(cp, ailen - (*addrinfo - cp),
162
"%s", inet_ntoa(sin->sin_addr));
163
if (len <= 0 || len >= ailen - (*addrinfo - cp)) {
164
warningx(_("load_interfaces: overflow detected"));
171
sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
172
inet_ntop(AF_INET6, &sin6->sin6_addr, addrbuf, sizeof(addrbuf));
173
len = snprintf(cp, ailen - (*addrinfo - cp),
174
"%s%s/", cp == *addrinfo ? "" : " ", addrbuf);
175
if (len <= 0 || len >= ailen - (*addrinfo - cp)) {
176
warningx(_("load_interfaces: overflow detected"));
181
sin6 = (struct sockaddr_in6 *)ifa->ifa_netmask;
182
inet_ntop(AF_INET6, &sin6->sin6_addr, addrbuf, sizeof(addrbuf));
183
len = snprintf(cp, ailen - (*addrinfo - cp), "%s", addrbuf);
184
if (len <= 0 || len >= ailen - (*addrinfo - cp)) {
185
warningx(_("load_interfaces: overflow detected"));
190
#endif /* HAVE_IN6_ADDR */
195
#ifdef HAVE_FREEIFADDRS
196
freeifaddrs(ifaddrs);
200
return num_interfaces;
203
#elif defined(SIOCGIFCONF) && !defined(STUB_LOAD_INTERFACES)
206
* Allocate and fill in the interfaces global variable with the
207
* machine's ip addresses and netmasks.
210
get_net_ifs(char **addrinfo)
212
struct ifconf *ifconf;
213
struct ifreq *ifr, ifr_tmp;
214
struct sockaddr_in *sin;
215
int ailen, i, len, n, sock, num_interfaces = 0;
216
size_t buflen = sizeof(struct ifconf) + BUFSIZ;
217
char *cp, *previfname = "", *ifconf_buf = NULL;
219
struct strioctl strioctl;
222
sock = socket(AF_INET, SOCK_DGRAM, 0);
224
error(1, _("unable to open socket"));
227
* Get interface configuration or return.
230
ifconf_buf = emalloc(buflen);
231
ifconf = (struct ifconf *) ifconf_buf;
232
ifconf->ifc_len = buflen - sizeof(struct ifconf);
233
ifconf->ifc_buf = (caddr_t) (ifconf_buf + sizeof(struct ifconf));
236
STRSET(SIOCGIFCONF, (caddr_t) ifconf, buflen);
237
if (ioctl(sock, I_STR, (caddr_t) &strioctl) < 0)
239
/* Note that some kernels return EINVAL if the buffer is too small */
240
if (ioctl(sock, SIOCGIFCONF, (caddr_t) ifconf) < 0 && errno != EINVAL)
244
/* Break out of loop if we have a big enough buffer. */
245
if (ifconf->ifc_len + sizeof(struct ifreq) < buflen)
251
/* Allocate space for the maximum number of interfaces that could exist. */
252
if ((n = ifconf->ifc_len / sizeof(struct ifreq)) == 0)
254
ailen = n * 2 * INET6_ADDRSTRLEN;
255
*addrinfo = cp = emalloc(ailen);
257
/* For each interface, store the ip address and netmask. */
258
for (i = 0; i < ifconf->ifc_len; ) {
259
/* Get a pointer to the current interface. */
260
ifr = (struct ifreq *) &ifconf->ifc_buf[i];
262
/* Set i to the subscript of the next interface. */
263
i += sizeof(struct ifreq);
265
if (ifr->ifr_addr.sa_len > sizeof(ifr->ifr_addr))
266
i += ifr->ifr_addr.sa_len - sizeof(struct sockaddr);
267
#endif /* HAVE_SA_LEN */
269
/* Skip duplicates and interfaces with NULL addresses. */
270
sin = (struct sockaddr_in *) &ifr->ifr_addr;
271
if (sin->sin_addr.s_addr == 0 ||
272
strncmp(previfname, ifr->ifr_name, sizeof(ifr->ifr_name) - 1) == 0)
275
if (ifr->ifr_addr.sa_family != AF_INET)
279
memset(&ifr_tmp, 0, sizeof(ifr_tmp));
280
strncpy(ifr_tmp.ifr_name, ifr->ifr_name, sizeof(ifr_tmp.ifr_name) - 1);
281
if (ioctl(sock, SIOCGIFFLAGS, (caddr_t) &ifr_tmp) < 0)
285
/* Skip interfaces marked "down" and "loopback". */
286
if (!ISSET(ifr_tmp.ifr_flags, IFF_UP) ||
287
ISSET(ifr_tmp.ifr_flags, IFF_LOOPBACK))
290
sin = (struct sockaddr_in *) &ifr->ifr_addr;
291
len = snprintf(cp, ailen - (*addrinfo - cp),
292
"%s%s/", cp == *addrinfo ? "" : " ",
293
inet_ntoa(sin->sin_addr));
294
if (len <= 0 || len >= ailen - (*addrinfo - cp)) {
295
warningx(_("load_interfaces: overflow detected"));
300
/* Stash the name of the interface we saved. */
301
previfname = ifr->ifr_name;
303
/* Get the netmask. */
304
memset(&ifr_tmp, 0, sizeof(ifr_tmp));
305
strncpy(ifr_tmp.ifr_name, ifr->ifr_name, sizeof(ifr_tmp.ifr_name) - 1);
307
STRSET(SIOCGIFNETMASK, (caddr_t) &ifr_tmp, sizeof(ifr_tmp));
308
if (ioctl(sock, I_STR, (caddr_t) &strioctl) < 0) {
310
if (ioctl(sock, SIOCGIFNETMASK, (caddr_t) &ifr_tmp) < 0) {
312
sin = (struct sockaddr_in *) &ifr_tmp.ifr_addr;
313
sin->sin_addr.s_addr = htonl(IN_CLASSC_NET);
315
sin = (struct sockaddr_in *) &ifr_tmp.ifr_addr;
316
len = snprintf(cp, ailen - (*addrinfo - cp),
317
"%s", inet_ntoa(sin->sin_addr));
318
if (len <= 0 || len >= ailen - (*addrinfo - cp)) {
319
warningx(_("load_interfaces: overflow detected"));
330
return num_interfaces;
333
#else /* !SIOCGIFCONF || STUB_LOAD_INTERFACES */
336
* Stub function for those without SIOCGIFCONF or getifaddrs()
339
get_net_ifs(char **addrinfo)
344
#endif /* SIOCGIFCONF && !STUB_LOAD_INTERFACES */