xref: /openbsd-src/lib/libpcap/inet.c (revision b85261abcd57db7dac50c2f10dce1d48bae6f3ae)
1 /*	$OpenBSD: inet.c,v 1.12 2000/04/13 05:55:19 itojun Exp $	*/
2 
3 /*
4  * Copyright (c) 1994, 1995, 1996, 1997, 1998
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the Computer Systems
18  *	Engineering Group at Lawrence Berkeley Laboratory.
19  * 4. Neither the name of the University nor of the Laboratory may be used
20  *    to endorse or promote products derived from this software without
21  *    specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifndef lint
37 static const char rcsid[] =
38     "@(#) $Header: /home/cvs/src/lib/libpcap/inet.c,v 1.12 2000/04/13 05:55:19 itojun Exp $ (LBL)";
39 #endif
40 
41 #include <sys/param.h>
42 #include <sys/file.h>
43 #include <sys/ioctl.h>
44 #include <sys/socket.h>
45 #ifdef HAVE_SYS_SOCKIO_H
46 #include <sys/sockio.h>
47 #endif
48 #include <sys/time.h>				/* concession to AIX */
49 
50 #ifdef __STDC__
51 struct mbuf;
52 struct rtentry;
53 #endif
54 
55 #include <net/if.h>
56 #include <netinet/in.h>
57 
58 #include <ctype.h>
59 #include <errno.h>
60 #include <memory.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <unistd.h>
65 #ifdef HAVE_IFADDRS_H
66 #include <ifaddrs.h>
67 #endif
68 
69 #include "pcap-int.h"
70 
71 #ifdef HAVE_OS_PROTO_H
72 #include "os-proto.h"
73 #endif
74 
75 /* Not all systems have IFF_LOOPBACK */
76 #ifdef IFF_LOOPBACK
77 #define ISLOOPBACK(p) ((p)->ifr_flags & IFF_LOOPBACK)
78 #else
79 #define ISLOOPBACK(p) ((p)->ifr_name[0] == 'l' && (p)->ifr_name[1] == 'o' && \
80     (isdigit((p)->ifr_name[2]) || (p)->ifr_name[2] == '\0'))
81 #endif
82 
83 /*
84  * Return the name of a network interface attached to the system, or NULL
85  * if none can be found.  The interface must be configured up; the
86  * lowest unit number is preferred; loopback is ignored.
87  */
88 char *
89 pcap_lookupdev(errbuf)
90 	register char *errbuf;
91 {
92 #ifdef HAVE_IFADDRS_H
93 	struct ifaddrs *ifap, *ifa, *mp;
94 	int n, minunit;
95 	char *cp;
96 	static char device[IF_NAMESIZE + 1];
97 
98 	if (getifaddrs(&ifap) != 0) {
99 		(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
100 		    "getifaddrs: %s", pcap_strerror(errno));
101 		return NULL;
102 	}
103 
104 	mp = NULL;
105 	minunit = 666;
106 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
107 		if ((ifa->ifa_flags & IFF_UP) == 0)
108 			continue;
109 #ifdef IFF_LOOPBACK
110 		if ((ifa->ifa_flags & IFF_LOOPBACK) != 0)
111 			continue;
112 #else
113 		if (strncmp(ifa->ifa_name, "lo", 2) == 0 &&
114 		    (ifa->ifa_name[2] == '\0' || isdigit(ifa->ifa_name[2]))) {
115 			continue;
116 		}
117 #endif
118 
119 		for (cp = ifa->ifa_name; !isdigit(*cp); ++cp)
120 			continue;
121 		n = atoi(cp);
122 		if (n < minunit) {
123 			minunit = n;
124 			mp = ifa;
125 		}
126 	}
127 	if (mp == NULL) {
128 		(void)strncpy(errbuf, "no suitable device found",
129 		    PCAP_ERRBUF_SIZE);
130 		freeifaddrs(ifap);
131 		return (NULL);
132 	}
133 
134 	(void)strncpy(device, mp->ifa_name, sizeof(device) - 1);
135 	device[sizeof(device) - 1] = '\0';
136 	freeifaddrs(ifap);
137 	return (device);
138 #else
139 	register int fd, minunit, n;
140 	register char *cp, *ibuf = NULL, *nibuf;
141 	register struct ifreq *ifrp, *ifend, *ifnext, *mp;
142 	struct ifconf ifc;
143 	struct ifreq ifr;
144 	static char device[sizeof(ifrp->ifr_name) + 1];
145 	int len = 8192;
146 
147 	fd = socket(AF_INET, SOCK_DGRAM, 0);
148 	if (fd < 0) {
149 		(void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "socket: %s",
150 		    pcap_strerror(errno));
151 		return (NULL);
152 	}
153 	while (1) {
154 		ifc.ifc_len = len;
155 		nibuf = realloc(ibuf, len);
156 		if (nibuf == NULL) {
157 			if (ibuf)
158 				free(ibuf);
159 			close(fd);
160 			return (NULL);
161 		}
162 		ifc.ifc_buf = ibuf = nibuf;
163 		if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0) {
164 			(void)close(fd);
165 			free(ibuf);
166 			return (NULL);
167 		}
168 		if (ifc.ifc_len + sizeof(ifr) < len)
169 			break;
170 		len *= 2;
171 	}
172 
173 	ifrp = (struct ifreq *)ibuf;
174 	ifend = (struct ifreq *)((char *)ibuf + ifc.ifc_len);
175 
176 	mp = NULL;
177 	minunit = 666;
178 	for (; ifrp < ifend; ifrp = ifnext) {
179 #if BSD - 0 >= 199006
180 		n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name);
181 		if (n < sizeof(*ifrp))
182 			ifnext = ifrp + 1;
183 		else
184 			ifnext = (struct ifreq *)((char *)ifrp + n);
185 		if (ifrp->ifr_addr.sa_family != AF_INET)
186 			continue;
187 #else
188 		ifnext = ifrp + 1;
189 #endif
190 		/*
191 		 * Need a template to preserve address info that is
192 		 * used below to locate the next entry.  (Otherwise,
193 		 * SIOCGIFFLAGS stomps over it because the requests
194 		 * are returned in a union.)
195 		 */
196 		strncpy(ifr.ifr_name, ifrp->ifr_name, sizeof(ifr.ifr_name));
197 		if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) < 0) {
198 			if (errno == ENXIO)
199 				continue;
200 			(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
201 			    "SIOCGIFFLAGS: %.*s: %s",
202 			    (int)sizeof(ifr.ifr_name), ifr.ifr_name,
203 			    pcap_strerror(errno));
204 			(void)close(fd);
205 			free(ibuf);
206 			return (NULL);
207 		}
208 
209 		/* Must be up and not the loopback */
210 		if ((ifr.ifr_flags & IFF_UP) == 0 || ISLOOPBACK(&ifr))
211 			continue;
212 
213 		for (cp = ifrp->ifr_name; !isdigit(*cp); ++cp)
214 			continue;
215 		n = atoi(cp);
216 		if (n < minunit) {
217 			minunit = n;
218 			mp = ifrp;
219 		}
220 	}
221 	(void)close(fd);
222 	if (mp == NULL) {
223 		(void)strlcpy(errbuf, "no suitable device found",
224 		    PCAP_ERRBUF_SIZE);
225 		free(ibuf);
226 		return (NULL);
227 	}
228 
229 	(void)strncpy(device, mp->ifr_name, sizeof(device) - 1);
230 	device[sizeof(device) - 1] = '\0';
231 	free(ibuf);
232 	return (device);
233 #endif
234 }
235 
236 int
237 pcap_lookupnet(device, netp, maskp, errbuf)
238 	register char *device;
239 	register bpf_u_int32 *netp, *maskp;
240 	register char *errbuf;
241 {
242 	register int fd;
243 	register struct sockaddr_in *sin;
244 	struct ifreq ifr;
245 
246 	fd = socket(AF_INET, SOCK_DGRAM, 0);
247 	if (fd < 0) {
248 		(void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "socket: %s",
249 		    pcap_strerror(errno));
250 		return (-1);
251 	}
252 	memset(&ifr, 0, sizeof(ifr));
253 #ifdef linux
254 	/* XXX Work around Linux kernel bug */
255 	ifr.ifr_addr.sa_family = AF_INET;
256 #endif
257 	(void)strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
258 	if (ioctl(fd, SIOCGIFADDR, (char *)&ifr) < 0) {
259 		(void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "SIOCGIFADDR: %s: %s",
260 		    device, pcap_strerror(errno));
261 		(void)close(fd);
262 		return (-1);
263 	}
264 	sin = (struct sockaddr_in *)&ifr.ifr_addr;
265 	*netp = sin->sin_addr.s_addr;
266 	if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifr) < 0) {
267 		(void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "SIOCGIFNETMASK: %s: %s",
268 		    device, pcap_strerror(errno));
269 		(void)close(fd);
270 		return (-1);
271 	}
272 	(void)close(fd);
273 	*maskp = sin->sin_addr.s_addr;
274 	if (*maskp == 0) {
275 		if (IN_CLASSA(*netp))
276 			*maskp = IN_CLASSA_NET;
277 		else if (IN_CLASSB(*netp))
278 			*maskp = IN_CLASSB_NET;
279 		else if (IN_CLASSC(*netp))
280 			*maskp = IN_CLASSC_NET;
281 		else {
282 			(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
283 			    "inet class for 0x%x unknown", *netp);
284 			return (-1);
285 		}
286 	}
287 	*netp &= *maskp;
288 	return (0);
289 }
290