xref: /netbsd/usr.sbin/bootp/common/getif.c (revision bf9ec67e)
1 /*	$NetBSD: getif.c,v 1.4 1998/03/14 04:39:54 lukem Exp $	*/
2 
3 #include <sys/cdefs.h>
4 #ifndef lint
5 __RCSID("$NetBSD: getif.c,v 1.4 1998/03/14 04:39:54 lukem 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 #ifdef	__STDC__
45 #define P(args) args
46 #else
47 #define P(args) ()
48 #endif
49 
50 static int nmatch P((u_char *, u_char *));
51 
52 #undef P
53 
54 /* Return a pointer to the interface struct for the passed address. */
55 struct ifreq *
56 getif(s, addrp)
57 	int s;						/* socket file descriptor */
58 	struct in_addr *addrp;		/* destination address on interface */
59 {
60 	int maxmatch;
61 	int len, m, incr;
62 	struct ifreq *ifrq, *ifrmax;
63 	struct sockaddr_in *sip;
64 	char *p;
65 
66 	/* If no address was supplied, just return NULL. */
67 	if (!addrp)
68 		return (struct ifreq *) 0;
69 
70 	/* Get the interface config if not done already. */
71 	if (ifconf.ifc_len == 0) {
72 #ifdef	SVR4
73 		/*
74 		 * SysVr4 returns garbage if you do this the obvious way!
75 		 * This one took a while to figure out... -gwr
76 		 */
77 		struct strioctl ioc;
78 		ioc.ic_cmd = SIOCGIFCONF;
79 		ioc.ic_timout = 0;
80 		ioc.ic_len = sizeof(ifreq);
81 		ioc.ic_dp = (char *) ifreq;
82 		m = ioctl(s, I_STR, (char *) &ioc);
83 		ifconf.ifc_len = ioc.ic_len;
84 		ifconf.ifc_req = ifreq;
85 #else	/* SVR4 */
86 		ifconf.ifc_len = sizeof(ifreq);
87 		ifconf.ifc_req = ifreq;
88 		m = ioctl(s, SIOCGIFCONF, (caddr_t) & ifconf);
89 #endif	/* SVR4 */
90 		if ((m < 0) || (ifconf.ifc_len <= 0)) {
91 			report(LOG_ERR, "ioctl SIOCGIFCONF");
92 			return (struct ifreq *) 0;
93 		}
94 	}
95 	maxmatch = 7;				/* this many bits or less... */
96 	ifrmax = (struct ifreq *) 0;/* ... is not a valid match  */
97 	p = (char *) ifreq;
98 	len = ifconf.ifc_len;
99 	while (len > 0) {
100 		ifrq = (struct ifreq *) p;
101 		sip = (struct sockaddr_in *) &ifrq->ifr_addr;
102 		m = nmatch((u_char *)addrp, (u_char *)&(sip->sin_addr));
103 		if (m > maxmatch) {
104 			maxmatch = m;
105 			ifrmax = ifrq;
106 		}
107 		/* XXX - Could this be just #ifndef IFNAMSIZ instead? -gwr */
108 #if (BSD - 0) < 43
109 		/* BSD not defined or earlier than 4.3 */
110 		incr = sizeof(*ifrq);
111 #else /* NetBSD */
112 		incr = ifrq->ifr_addr.sa_len + IFNAMSIZ;
113 #endif /* NetBSD */
114 
115 		p += incr;
116 		len -= incr;
117 	}
118 
119 	return ifrmax;
120 }
121 
122 /*
123  * Return the number of leading bits matching in the
124  * internet addresses supplied.
125  */
126 static int
127 nmatch(ca, cb)
128 	u_char *ca, *cb;			/* ptrs to IP address, network order */
129 {
130 	u_int m = 0;				/* count of matching bits */
131 	u_int n = 4;				/* bytes left, then bitmask */
132 
133 	/* Count matching bytes. */
134 	while (n && (*ca == *cb)) {
135 		ca++;
136 		cb++;
137 		m += 8;
138 		n--;
139 	}
140 	/* Now count matching bits. */
141 	if (n) {
142 		n = 0x80;
143 		while (n && ((*ca & n) == (*cb & n))) {
144 			m++;
145 			n >>= 1;
146 		}
147 	}
148 	return (m);
149 }
150 
151 /*
152  * Local Variables:
153  * tab-width: 4
154  * c-indent-level: 4
155  * c-argdecl-indent: 4
156  * c-continued-statement-offset: 4
157  * c-continued-brace-offset: -4
158  * c-label-offset: -4
159  * c-brace-offset: 0
160  * End:
161  */
162