xref: /openbsd-src/lib/libpcap/inet.c (revision e7beb4a7d58a6a0955c07ef9465f5caa3383f928)
1 /*	$OpenBSD: inet.c,v 1.14 2002/02/19 19:39:37 millert 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.14 2002/02/19 19:39:37 millert 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 struct mbuf;
51 struct rtentry;
52 
53 #include <net/if.h>
54 #include <netinet/in.h>
55 
56 #include <ctype.h>
57 #include <errno.h>
58 #include <memory.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63 #ifdef HAVE_IFADDRS_H
64 #include <ifaddrs.h>
65 #endif
66 
67 #include "pcap-int.h"
68 
69 #ifdef HAVE_OS_PROTO_H
70 #include "os-proto.h"
71 #endif
72 
73 /* Not all systems have IFF_LOOPBACK */
74 #ifdef IFF_LOOPBACK
75 #define ISLOOPBACK(p) ((p)->ifr_flags & IFF_LOOPBACK)
76 #else
77 #define ISLOOPBACK(p) ((p)->ifr_name[0] == 'l' && (p)->ifr_name[1] == 'o' && \
78     (isdigit((p)->ifr_name[2]) || (p)->ifr_name[2] == '\0'))
79 #endif
80 
81 /*
82  * Return the name of a network interface attached to the system, or NULL
83  * if none can be found.  The interface must be configured up; the
84  * lowest unit number is preferred; loopback is ignored.
85  */
86 char *
87 pcap_lookupdev(errbuf)
88 	register char *errbuf;
89 {
90 #ifdef HAVE_IFADDRS_H
91 	struct ifaddrs *ifap, *ifa, *mp;
92 	int n, minunit;
93 	char *cp;
94 	static char device[IF_NAMESIZE + 1];
95 
96 	if (getifaddrs(&ifap) != 0) {
97 		(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
98 		    "getifaddrs: %s", pcap_strerror(errno));
99 		return NULL;
100 	}
101 
102 	mp = NULL;
103 	minunit = 666;
104 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
105 		if ((ifa->ifa_flags & IFF_UP) == 0)
106 			continue;
107 #ifdef IFF_LOOPBACK
108 		if ((ifa->ifa_flags & IFF_LOOPBACK) != 0)
109 			continue;
110 #else
111 		if (strncmp(ifa->ifa_name, "lo", 2) == 0 &&
112 		    (ifa->ifa_name[2] == '\0' || isdigit(ifa->ifa_name[2]))) {
113 			continue;
114 		}
115 #endif
116 
117 		for (cp = ifa->ifa_name; !isdigit(*cp); ++cp)
118 			continue;
119 		n = atoi(cp);
120 		if (n < minunit) {
121 			minunit = n;
122 			mp = ifa;
123 		}
124 	}
125 	if (mp == NULL) {
126 		(void)strncpy(errbuf, "no suitable device found",
127 		    PCAP_ERRBUF_SIZE);
128 		freeifaddrs(ifap);
129 		return (NULL);
130 	}
131 
132 	(void)strncpy(device, mp->ifa_name, sizeof(device) - 1);
133 	device[sizeof(device) - 1] = '\0';
134 	freeifaddrs(ifap);
135 	return (device);
136 #else
137 	register int fd, minunit, n;
138 	register char *cp;
139 	register struct ifreq *ifrp, *ifend, *ifnext, *mp;
140 	struct ifconf ifc;
141 	struct ifreq ibuf[16], ifr;
142 	static char device[sizeof(ifrp->ifr_name) + 1];
143 
144 	fd = socket(AF_INET, SOCK_DGRAM, 0);
145 	if (fd < 0) {
146 		(void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "socket: %s",
147 		    pcap_strerror(errno));
148 		return (NULL);
149 	}
150 	ifc.ifc_len = sizeof ibuf;
151 	ifc.ifc_buf = (caddr_t)ibuf;
152 
153 	memset((char *)ibuf, 0, sizeof(ibuf));
154 	if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 ||
155 	    ifc.ifc_len < sizeof(struct ifreq)) {
156 		(void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "SIOCGIFCONF: %s",
157 		    pcap_strerror(errno));
158 		(void)close(fd);
159 		return (NULL);
160 	}
161 	ifrp = ibuf;
162 	ifend = (struct ifreq *)((char *)ibuf + ifc.ifc_len);
163 
164 	mp = NULL;
165 	minunit = 666;
166 	for (; ifrp < ifend; ifrp = ifnext) {
167 #ifdef HAVE_SOCKADDR_SA_LEN
168 		n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name);
169 		if (n < sizeof(*ifrp))
170 			ifnext = ifrp + 1;
171 		else
172 			ifnext = (struct ifreq *)((char *)ifrp + n);
173 		if (ifrp->ifr_addr.sa_family != AF_INET)
174 			continue;
175 #else
176 		ifnext = ifrp + 1;
177 #endif
178 		/*
179 		 * Need a template to preserve address info that is
180 		 * used below to locate the next entry.  (Otherwise,
181 		 * SIOCGIFFLAGS stomps over it because the requests
182 		 * are returned in a union.)
183 		 */
184 		strncpy(ifr.ifr_name, ifrp->ifr_name, sizeof(ifr.ifr_name));
185 		if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) < 0) {
186 			if (errno == ENXIO)
187 				continue;
188 			(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
189 			    "SIOCGIFFLAGS: %.*s: %s",
190 			    (int)sizeof(ifr.ifr_name), ifr.ifr_name,
191 			    pcap_strerror(errno));
192 			(void)close(fd);
193 			return (NULL);
194 		}
195 
196 		/* Must be up and not the loopback */
197 		if ((ifr.ifr_flags & IFF_UP) == 0 || ISLOOPBACK(&ifr))
198 			continue;
199 
200 		for (cp = ifrp->ifr_name; !isdigit(*cp); ++cp)
201 			continue;
202 		n = atoi(cp);
203 		if (n < minunit) {
204 			minunit = n;
205 			mp = ifrp;
206 		}
207 	}
208 	(void)close(fd);
209 	if (mp == NULL) {
210 		(void)strlcpy(errbuf, "no suitable device found",
211 		    PCAP_ERRBUF_SIZE);
212 		return (NULL);
213 	}
214 
215 	(void)strncpy(device, mp->ifr_name, sizeof(device) - 1);
216 	device[sizeof(device) - 1] = '\0';
217 	return (device);
218 #endif
219 }
220 
221 int
222 pcap_lookupnet(device, netp, maskp, errbuf)
223 	register char *device;
224 	register bpf_u_int32 *netp, *maskp;
225 	register char *errbuf;
226 {
227 	register int fd;
228 	register struct sockaddr_in *sin;
229 	struct ifreq ifr;
230 
231 	fd = socket(AF_INET, SOCK_DGRAM, 0);
232 	if (fd < 0) {
233 		(void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "socket: %s",
234 		    pcap_strerror(errno));
235 		return (-1);
236 	}
237 	memset(&ifr, 0, sizeof(ifr));
238 #ifdef linux
239 	/* XXX Work around Linux kernel bug */
240 	ifr.ifr_addr.sa_family = AF_INET;
241 #endif
242 	(void)strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
243 	if (ioctl(fd, SIOCGIFADDR, (char *)&ifr) < 0) {
244 		if (errno == EADDRNOTAVAIL) {
245 			(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
246 			    "%s: no IPv4 address assigned", device);
247 		} else {
248 			(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
249 			    "SIOCGIFADDR: %s: %s",
250 			    device, pcap_strerror(errno));
251 		}
252 		(void)close(fd);
253 		return (-1);
254 	}
255 	sin = (struct sockaddr_in *)&ifr.ifr_addr;
256 	*netp = sin->sin_addr.s_addr;
257 	if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifr) < 0) {
258 		(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
259 		    "SIOCGIFNETMASK: %s: %s", device, pcap_strerror(errno));
260 		(void)close(fd);
261 		return (-1);
262 	}
263 	(void)close(fd);
264 	*maskp = sin->sin_addr.s_addr;
265 	if (*maskp == 0) {
266 		if (IN_CLASSA(*netp))
267 			*maskp = IN_CLASSA_NET;
268 		else if (IN_CLASSB(*netp))
269 			*maskp = IN_CLASSB_NET;
270 		else if (IN_CLASSC(*netp))
271 			*maskp = IN_CLASSC_NET;
272 		else {
273 			(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
274 			    "inet class for 0x%x unknown", *netp);
275 			return (-1);
276 		}
277 	}
278 	*netp &= *maskp;
279 	return (0);
280 }
281