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