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