Home | History | Annotate | Line # | Download | only in common
      1 /*	$NetBSD: getif.c,v 1.6 2003/08/18 05:39:52 itojun Exp $	*/
      2 
      3 #include <sys/cdefs.h>
      4 #ifndef lint
      5 __RCSID("$NetBSD: getif.c,v 1.6 2003/08/18 05:39:52 itojun Exp $");
      6 #endif
      7 
      8 /*
      9  * getif.c : get an interface structure
     10  */
     11 
     12 #include <sys/types.h>
     13 #include <sys/socket.h>
     14 #include <sys/ioctl.h>
     15 #include <sys/param.h>
     16 
     17 #if defined(SUNOS) || defined(SVR4)
     18 #include <sys/sockio.h>
     19 #endif
     20 #ifdef	SVR4
     21 #include <sys/stropts.h>
     22 #endif
     23 
     24 #include <net/if.h>				/* for struct ifreq */
     25 #include <netinet/in.h>
     26 
     27 #ifndef	NO_UNISTD
     28 #include <unistd.h>
     29 #endif
     30 #include <syslog.h>
     31 #include <errno.h>
     32 #include <assert.h>
     33 
     34 #include "getif.h"
     35 #include "report.h"
     36 
     37 #ifdef	__bsdi__
     38 #define BSD 43
     39 #endif
     40 
     41 static struct ifreq ifreq[10];	/* Holds interface configuration */
     42 static struct ifconf ifconf;	/* points to ifreq */
     43 
     44 static int nmatch(u_char *, u_char *);
     45 
     46 /* Return a pointer to the interface struct for the passed address. */
     47 struct ifreq *
     48 getif(int s, struct in_addr *addrp)
     49 	      						/* socket file descriptor */
     50 	                      		/* destination address on interface */
     51 {
     52 	int maxmatch;
     53 	int len, m, incr;
     54 	struct ifreq *ifrq, *ifrmax;
     55 	struct sockaddr_in *sip;
     56 	char *p;
     57 
     58 	/* If no address was supplied, just return NULL. */
     59 	if (!addrp)
     60 		return (struct ifreq *) 0;
     61 
     62 	/* Get the interface config if not done already. */
     63 	if (ifconf.ifc_len == 0) {
     64 #ifdef	SVR4
     65 		/*
     66 		 * SysVr4 returns garbage if you do this the obvious way!
     67 		 * This one took a while to figure out... -gwr
     68 		 */
     69 		struct strioctl ioc;
     70 		ioc.ic_cmd = SIOCGIFCONF;
     71 		ioc.ic_timout = 0;
     72 		ioc.ic_len = sizeof(ifreq);
     73 		ioc.ic_dp = (char *) ifreq;
     74 		m = ioctl(s, I_STR, (char *) &ioc);
     75 		ifconf.ifc_len = ioc.ic_len;
     76 		ifconf.ifc_req = ifreq;
     77 #else	/* SVR4 */
     78 		ifconf.ifc_len = sizeof(ifreq);
     79 		ifconf.ifc_req = ifreq;
     80 		m = ioctl(s, SIOCGIFCONF, (caddr_t) & ifconf);
     81 #endif	/* SVR4 */
     82 		if ((m < 0) || (ifconf.ifc_len <= 0)) {
     83 			report(LOG_ERR, "ioctl SIOCGIFCONF");
     84 			return (struct ifreq *) 0;
     85 		}
     86 	}
     87 	maxmatch = 7;				/* this many bits or less... */
     88 	ifrmax = (struct ifreq *) 0;/* ... is not a valid match  */
     89 	p = (char *) ifreq;
     90 	len = ifconf.ifc_len;
     91 	while (len > 0) {
     92 		ifrq = (struct ifreq *) p;
     93 		if (ifrq->ifr_addr.sa_family == AF_INET) {
     94 			sip = (struct sockaddr_in *) &ifrq->ifr_addr;
     95 			m = nmatch((u_char *)addrp, (u_char *)&(sip->sin_addr));
     96 			if (m > maxmatch) {
     97 				maxmatch = m;
     98 				ifrmax = ifrq;
     99 			}
    100 		}
    101 		/* XXX - Could this be just #ifndef IFNAMSIZ instead? -gwr */
    102 #if (BSD - 0) < 43
    103 		/* BSD not defined or earlier than 4.3 */
    104 		incr = sizeof(*ifrq);
    105 #else /* NetBSD */
    106 		incr = ifrq->ifr_addr.sa_len + IFNAMSIZ;
    107 #endif /* NetBSD */
    108 
    109 		p += incr;
    110 		len -= incr;
    111 	}
    112 
    113 	return ifrmax;
    114 }
    115 
    116 /*
    117  * Return the number of leading bits matching in the
    118  * internet addresses supplied.
    119  */
    120 static int
    121 nmatch(u_char *ca, u_char *cb)
    122 	                			/* ptrs to IP address, network order */
    123 {
    124 	u_int m = 0;				/* count of matching bits */
    125 	u_int n = 4;				/* bytes left, then bitmask */
    126 
    127 	/* Count matching bytes. */
    128 	while (n && (*ca == *cb)) {
    129 		ca++;
    130 		cb++;
    131 		m += 8;
    132 		n--;
    133 	}
    134 	/* Now count matching bits. */
    135 	if (n) {
    136 		n = 0x80;
    137 		while (n && ((*ca & n) == (*cb & n))) {
    138 			m++;
    139 			n >>= 1;
    140 		}
    141 	}
    142 	return (m);
    143 }
    144 
    145 /*
    146  * Local Variables:
    147  * tab-width: 4
    148  * c-indent-level: 4
    149  * c-argdecl-indent: 4
    150  * c-continued-statement-offset: 4
    151  * c-continued-brace-offset: -4
    152  * c-label-offset: -4
    153  * c-brace-offset: 0
    154  * End:
    155  */
    156