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