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