getif.c revision 1.5 1 /* $NetBSD: getif.c,v 1.5 2002/07/14 00:26:17 wiz Exp $ */
2
3 #include <sys/cdefs.h>
4 #ifndef lint
5 __RCSID("$NetBSD: getif.c,v 1.5 2002/07/14 00:26:17 wiz 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 sip = (struct sockaddr_in *) &ifrq->ifr_addr;
94 m = nmatch((u_char *)addrp, (u_char *)&(sip->sin_addr));
95 if (m > maxmatch) {
96 maxmatch = m;
97 ifrmax = ifrq;
98 }
99 /* XXX - Could this be just #ifndef IFNAMSIZ instead? -gwr */
100 #if (BSD - 0) < 43
101 /* BSD not defined or earlier than 4.3 */
102 incr = sizeof(*ifrq);
103 #else /* NetBSD */
104 incr = ifrq->ifr_addr.sa_len + IFNAMSIZ;
105 #endif /* NetBSD */
106
107 p += incr;
108 len -= incr;
109 }
110
111 return ifrmax;
112 }
113
114 /*
115 * Return the number of leading bits matching in the
116 * internet addresses supplied.
117 */
118 static int
119 nmatch(u_char *ca, u_char *cb)
120 /* ptrs to IP address, network order */
121 {
122 u_int m = 0; /* count of matching bits */
123 u_int n = 4; /* bytes left, then bitmask */
124
125 /* Count matching bytes. */
126 while (n && (*ca == *cb)) {
127 ca++;
128 cb++;
129 m += 8;
130 n--;
131 }
132 /* Now count matching bits. */
133 if (n) {
134 n = 0x80;
135 while (n && ((*ca & n) == (*cb & n))) {
136 m++;
137 n >>= 1;
138 }
139 }
140 return (m);
141 }
142
143 /*
144 * Local Variables:
145 * tab-width: 4
146 * c-indent-level: 4
147 * c-argdecl-indent: 4
148 * c-continued-statement-offset: 4
149 * c-continued-brace-offset: -4
150 * c-label-offset: -4
151 * c-brace-offset: 0
152 * End:
153 */
154