2 * Copyright (c) 1997-2014 Erez Zadok
3 * Copyright (c) 1990 Jan-Simon Pendry
4 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
5 * Copyright (c) 1990 The Regents of the University of California.
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry at Imperial College, London.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * File: am-utils/libamu/wire.c
41 * This function returns the subnet (address&netmask) for the primary network
42 * interface. If the resulting address has an entry in the hosts file, the
43 * corresponding name is returned, otherwise the address is returned in
44 * standard internet format.
45 * As a side-effect, a list of local IP/net address is recorded for use
46 * by the islocalnet() function.
48 * Derived from original by Paul Anderson (23/4/90)
49 * Updates from Dirk Grunwald (11/11/91)
54 #endif /* HAVE_CONFIG_H */
61 #endif /* HAVE_IFADDRS_H */
65 #endif /* HAVE_IRS_H */
68 * List of locally connected networks
70 typedef struct addrlist addrlist;
73 u_long ip_addr; /* address of network */
75 char *ip_net_num; /* number of network */
76 char *ip_net_name; /* name of network */
78 static addrlist *localnets = NULL;
80 #if defined(IFF_LOCAL_LOOPBACK) && !defined(IFF_LOOPBACK)
81 # define IFF_LOOPBACK IFF_LOCAL_LOOPBACK
82 #endif /* defined(IFF_LOCAL_LOOPBACK) && !defined(IFF_LOOPBACK) */
84 #define C(x) ((x) & 0xff)
86 #define S2IN(s) (((struct sockaddr_in *)(s))->sin_addr.s_addr)
89 /* return malloc'ed buffer. caller must free it */
100 buf = SALLOC(buf_size); /* initial allocation (may grow!) */
105 xstrlcpy(buf, "No networks\n", buf_size);
108 /* check if there's more than one network */
109 if (!localnets->ip_next) {
110 /* use buf_size for sizeof(buf) because of the realloc() below */
111 xsnprintf(buf, buf_size,
112 "Network: wire=\"%s\" (netnumber=%s).\n",
113 localnets->ip_net_name, localnets->ip_net_num);
116 buf[0] = '\0'; /* null out buffer before appending */
117 for (i = 1, al = localnets; al; al = al->ip_next, i++) {
118 xsnprintf(s, sizeof(s), "Network %d: wire=\"%s\" (netnumber=%s).\n",
119 i, al->ip_net_name, al->ip_net_num);
120 bufcount += strlen(s);
121 if (bufcount > buf_size) {
123 buf = xrealloc(buf, buf_size);
125 xstrlcat(buf, s, buf_size);
131 static struct addrlist *
132 getwire_lookup(u_long address, u_long netmask, int ishost)
136 char netNumberBuf[64];
137 char buf[GFBUFLEN], *s;
140 #else /* not HAVE_IRS_H */
142 #endif /* not HAVE_IRS_H */
145 * Add interface to local network singly linked list
147 al = ALLOC(struct addrlist);
148 al->ip_addr = address;
149 al->ip_mask = netmask;
150 al->ip_net_name = NO_SUBNET; /* fill in a bit later */
151 al->ip_net_num = "0.0.0.0"; /* fill in a bit later */
154 subnet = ntohl(address) & ntohl(netmask);
160 u_long mask = ntohl(netmask);
161 static struct irs_acc *irs_gen;
162 static struct irs_nw *irs_nw;
170 * bsdi4 added another argument to this function, without changing
171 * its name. The irs_irp_acc is the one (hacky) distinguishing
172 * feature found in <irs.h> that can differentiate between bsdi3 and
175 irs_gen = irs_gen_acc("", NULL);
176 #else /* not irs_irp_acc */
177 irs_gen = irs_gen_acc("");
178 #endif /* not irs_irp_acc */
179 if (irs_gen && irs_nw == NULL)
180 irs_nw = (*irs_gen->nw_map)(irs_gen);
181 net = ntohl(address) & (mask = ntohl(netmask));
182 addr[0] = (0xFF000000 & net) >> 24;
183 addr[1] = (0x00FF0000 & net) >> 16;
184 addr[2] = (0x0000FF00 & net) >> 8;
185 addr[3] = (0x000000FF & net);
186 for (maskbits = 32; !(mask & 1); mask >>= 1)
188 np = (*irs_nw->byaddr)(irs_nw, addr, maskbits, AF_INET);
189 #else /* not HAVE_IRS_H */
190 np = getnetbyaddr(subnet, AF_INET);
192 * Some systems (IRIX 6.4) cannot getnetbyaddr on networks such as
193 * "128.59.16.0". Instead, they need to look for the short form of
194 * the network, "128.59.16". So if the first getnetbyaddr failed, we
195 * shift the subnet way from zeros and try again.
198 u_long short_subnet = subnet;
199 while (short_subnet && (short_subnet & 0x000000ff) == 0)
201 np = getnetbyaddr(short_subnet, AF_INET);
203 plog(XLOG_WARNING, "getnetbyaddr failed on 0x%x, succeeded on 0x%x",
204 (u_int) subnet, (u_int) short_subnet);
206 #endif /* not HAVE_IRS_H */
209 if ((subnet & 0xffffff) == 0) {
210 xsnprintf(netNumberBuf, sizeof(netNumberBuf), "%lu", C(subnet >> 24));
211 } else if ((subnet & 0xffff) == 0) {
212 xsnprintf(netNumberBuf, sizeof(netNumberBuf), "%lu.%lu",
213 C(subnet >> 24), C(subnet >> 16));
214 } else if ((subnet & 0xff) == 0) {
215 xsnprintf(netNumberBuf, sizeof(netNumberBuf), "%lu.%lu.%lu",
216 C(subnet >> 24), C(subnet >> 16),
219 xsnprintf(netNumberBuf, sizeof(netNumberBuf), "%lu.%lu.%lu.%lu",
220 C(subnet >> 24), C(subnet >> 16),
221 C(subnet >> 8), C(subnet));
224 /* fill in network number (string) */
225 al->ip_net_num = xstrdup(netNumberBuf);
232 subnet = address & netmask;
233 hp = gethostbyaddr((char *) &subnet, 4, AF_INET);
235 s = (char *) hp->h_name;
237 s = inet_dquad(buf, sizeof(buf), subnet);
240 /* fill in network name (string) */
241 al->ip_net_name = xstrdup(s);
242 /* Let's be cautious here about buffer overflows -Ion */
243 if (strlen(s) > MAXHOSTNAMELEN) {
244 al->ip_net_name[MAXHOSTNAMELEN] = '\0';
245 plog(XLOG_WARNING, "Long hostname %s truncated to %d characters",
254 * Make a dotted quad from a 32bit IP address
255 * addr is in network byte order.
256 * sizeof(buf) needs to be at least 16.
259 inet_dquad(char *buf, size_t l, u_long addr)
262 xsnprintf(buf, l, "%ld.%ld.%ld.%ld",
263 ((addr >> 24) & 0xff),
264 ((addr >> 16) & 0xff),
265 ((addr >> 8) & 0xff),
266 ((addr >> 0) & 0xff));
272 * Determine whether a network is on a local network
273 * (addr) is in network byte order.
276 islocalnet(u_long addr)
280 for (al = localnets; al; al = al->ip_next)
281 if (((addr ^ al->ip_addr) & al->ip_mask) == 0)
287 plog(XLOG_INFO, "%s is on a remote network",
288 inet_dquad(buf, sizeof(buf), addr));
297 * Determine whether a network name is one of the local networks
301 is_network_member(const char *net)
306 * If the network name string does not contain a '/', use old behavior.
307 * If it does contain a '/' then interpret the string as a network/netmask
308 * pair. If "netmask" doesn't exist, use the interface's own netmask.
309 * Also support fully explicit netmasks such as 255.255.255.0 as well as
310 * bit-length netmask such as /24 (hex formats such 0xffffff00 work too).
312 if (strchr(net, '/') == NULL) {
313 for (al = localnets; al; al = al->ip_next)
314 if (STREQ(net, al->ip_net_name) || STREQ(net, al->ip_net_num))
317 char *netstr = xstrdup(net), *maskstr;
318 u_long netnum, masknum = 0;
319 maskstr = strchr(netstr, '/');
320 if (maskstr == NULL) {
321 plog(XLOG_ERROR, "%s: netstr %s does not have a `/'", __func__, netstr);
325 maskstr[0] = '\0'; /* null terminate netstr */
327 if (*maskstr == '\0') /* if empty string, make it NULL */
329 /* check if netmask uses a dotted-quad or bit-length, or not defined at all */
331 if (strchr(maskstr, '.')) {
332 /* XXX: inet_addr is obsolste, convert to inet_aton() */
333 masknum = inet_addr(maskstr);
334 if (masknum == INADDR_NONE) /* can be invalid (-1) or all-1s */
335 masknum = 0xffffffff;
336 } else if (NSTRCEQ(maskstr, "0x", 2)) {
337 masknum = strtoul(maskstr, NULL, 16);
339 int bits = atoi(maskstr);
344 masknum = 0xffffffff << (32-bits);
347 netnum = inet_addr(netstr); /* not checking return value, b/c -1 (0xffffffff) is valid */
348 XFREE(netstr); /* netstr not needed any longer */
350 /* now check against each local interface */
351 for (al = localnets; al; al = al->ip_next) {
352 if ((al->ip_addr & (maskstr ? masknum : al->ip_mask)) == netnum)
362 * Determine whether a IP address (netnum) is one of the local interfaces,
363 * returns TRUE/FALSE.
364 * Does not include the loopback interface: caller needs to check that.
367 is_interface_local(u_long netnum)
371 for (al = localnets; al; al = al->ip_next) {
372 if (al->ip_addr == netnum)
379 #ifdef HAVE_GETIFADDRS
381 getwire(char **name1, char **number1)
383 addrlist *al = NULL, *tail = NULL;
384 struct ifaddrs *ifaddrs, *ifap;
385 #ifndef HAVE_STRUCT_IFADDRS_IFA_NEXT
387 #endif /* not HAVE_STRUCT_IFADDRS_IFA_NEXT */
390 #ifdef HAVE_STRUCT_IFADDRS_IFA_NEXT
391 if (getifaddrs(&ifaddrs) < 0)
394 for (ifap = ifaddrs; ifap != NULL; ifap = ifap->ifa_next) {
395 #else /* not HAVE_STRUCT_IFADDRS_IFA_NEXT */
396 if (getifaddrs(&ifaddrs, &count) < 0)
399 for (i = 0,ifap = ifaddrs; i < count; ifap++, i++) {
400 #endif /* HAVE_STRUCT_IFADDRS_IFA_NEXT */
402 if (!ifap || !ifap->ifa_addr || ifap->ifa_addr->sa_family != AF_INET)
406 * If the interface is the loopback, or it's not running,
409 if (S2IN(ifap->ifa_addr) == htonl(INADDR_LOOPBACK))
411 if ((ifap->ifa_flags & IFF_RUNNING) == 0)
414 if ((ifap->ifa_flags & IFF_POINTOPOINT) == 0)
415 al = getwire_lookup(S2IN(ifap->ifa_addr), S2IN(ifap->ifa_netmask), 0);
417 al = getwire_lookup(S2IN(ifap->ifa_dstaddr), 0xffffffff, 1);
419 /* append to the end of the list */
420 if (!localnets || tail == NULL) {
421 localnets = tail = al;
422 tail->ip_next = NULL;
434 *name1 = localnets->ip_net_name;
435 *number1 = localnets->ip_net_num;
438 *number1 = "0.0.0.0";
442 #else /* not HAVE_GETIFADDRS */
444 #if defined(HAVE_STRUCT_IFREQ_IFR_ADDR) && defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
445 # define SIZE(ifr) (MAX((ifr)->ifr_addr.sa_len, sizeof((ifr)->ifr_addr)) + sizeof(ifr->ifr_name))
446 #else /* not defined(HAVE_STRUCT_IFREQ_IFR_ADDR) && defined(HAVE_STRUCT_SOCKADDR_SA_LEN) */
447 # define SIZE(ifr) sizeof(struct ifreq)
448 #endif /* not defined(HAVE_STRUCT_IFREQ_IFR_ADDR) && defined(HAVE_STRUCT_SOCKADDR_SA_LEN) */
450 #define clist (ifc.ifc_ifcu.ifcu_req)
451 #define count (ifc.ifc_len/sizeof(struct ifreq))
455 getwire(char **name1, char **number1)
458 struct ifreq *ifr, ifrpool;
462 addrlist *al = NULL, *tail = NULL;
466 /* if cannot get interface flags, return nothing */
467 plog(XLOG_ERROR, "getwire unable to get interface flags");
470 #endif /* not SIOCGIFFLAGS */
473 * Get suitable socket
475 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
479 * Fill in ifconf details
481 memset(&buf[0], 0, GFBUFLEN);
482 ifc.ifc_len = sizeof(buf);
486 * Get network interface configurations
488 if (ioctl(fd, SIOCGIFCONF, (caddr_t) & ifc) < 0)
492 * Upper bound on array
494 cplim = buf + ifc.ifc_len;
497 * This is some magic to cope with both "traditional" and the
498 * new 4.4BSD-style struct sockaddrs. The new structure has
499 * variable length and a size field to support longer addresses.
500 * AF_LINK is a new definition for 4.4BSD.
504 * Scan the list looking for a suitable interface
506 for (cp = buf; cp < cplim; /* increment in the loop body */) {
507 memcpy(&ifrpool, cp, sizeof(ifrpool));
511 if (ifr->ifr_addr.sa_family != AF_INET)
514 address = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
517 * Get interface flags
519 if (ioctl(fd, SIOCGIFFLAGS, (caddr_t) ifr) < 0)
523 * If the interface is the loopback, or it's not running,
526 if (address == htonl(INADDR_LOOPBACK))
529 * Fix for 0.0.0.0 loopback on SunOS 3.X which defines IFF_ROUTE
530 * instead of IFF_LOOPBACK.
533 if (ifr->ifr_flags == (IFF_UP|IFF_RUNNING))
535 #endif /* IFF_ROUTE */
537 /* if the interface is not UP or not RUNNING, skip it */
538 if ((ifr->ifr_flags & IFF_RUNNING) == 0 ||
539 (ifr->ifr_flags & IFF_UP) == 0)
542 if ((ifr->ifr_flags & IFF_POINTOPOINT) == 0) {
544 * Get the netmask of this interface
546 if (ioctl(fd, SIOCGIFNETMASK, (caddr_t) ifr) < 0)
549 al = getwire_lookup(address, S2IN(&ifr->ifr_addr), 0);
551 al = getwire_lookup(address, 0xffffffff, 1);
553 /* append to the end of the list */
555 localnets = tail = al;
556 tail->ip_next = NULL;
567 *name1 = localnets->ip_net_name;
568 *number1 = localnets->ip_net_num;
571 *number1 = "0.0.0.0";
574 #endif /* not HAVE_GETIFADDRS */