]> The Tcpdump Group git mirrors - libpcap/blob - inet.c
088cfdcdba919accccddcc40ee1d6ca8cddf6fde
[libpcap] / inet.c
1 /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
2 /*
3 * Copyright (c) 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the Computer Systems
17 * Engineering Group at Lawrence Berkeley Laboratory.
18 * 4. Neither the name of the University nor of the Laboratory may be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #ifndef lint
36 static const char rcsid[] =
37 "@(#) $Header: /tcpdump/master/libpcap/inet.c,v 1.51 2002-08-01 08:33:02 risso Exp $ (LBL)";
38 #endif
39
40 #ifdef HAVE_CONFIG_H
41 #include "config.h"
42 #endif
43
44 #include <pcap-stdinc.h>
45 #ifdef HAVE_SYS_SOCKIO_H
46 #include <sys/sockio.h>
47 #endif
48
49 struct mbuf; /* Squelch compiler warnings on some platforms for */
50 struct rtentry; /* declarations in <net/if.h> */
51 #include <net/if.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 #ifdef HAVE_LIMITS_H
60 #include <limits.h>
61 #else
62 #define INT_MAX 2147483647
63 #endif
64 #ifdef HAVE_IFADDRS_H
65 #include <ifaddrs.h>
66 #endif
67
68 #include "pcap-int.h"
69
70 #ifdef HAVE_OS_PROTO_H
71 #include "os-proto.h"
72 #endif
73
74 /* Not all systems have IFF_LOOPBACK */
75 #ifdef IFF_LOOPBACK
76 #define ISLOOPBACK(name, flags) ((flags) & IFF_LOOPBACK)
77 #else
78 #define ISLOOPBACK(name, flags) ((name)[0] == 'l' && (name)[1] == 'o' && \
79 (isdigit((unsigned char)((name)[2])) || (name)[2] == '\0'))
80 #endif
81
82 struct sockaddr *
83 dup_sockaddr(struct sockaddr *sa, size_t sa_len)
84 {
85 struct sockaddr *newsa;
86
87 if ((newsa = malloc(sa_len)) == NULL)
88 return (NULL);
89 return (memcpy(newsa, sa, sa_len));
90 }
91
92 static int
93 get_instance(char *name)
94 {
95 char *cp, *endcp;
96 int n;
97
98 if (strcmp(name, "any") == 0) {
99 /*
100 * Give the "any" device an artificially high instance
101 * number, so it shows up after all other non-loopback
102 * interfaces.
103 */
104 return INT_MAX;
105 }
106
107 endcp = name + strlen(name);
108 for (cp = name; cp < endcp && !isdigit((unsigned char)*cp); ++cp)
109 continue;
110
111 if (isdigit((unsigned char)*cp))
112 n = atoi(cp);
113 else
114 n = 0;
115 return (n);
116 }
117
118 int
119 add_or_find_if(pcap_if_t **curdev_ret, pcap_if_t **alldevs, char *name,
120 u_int flags, const char *description, char *errbuf)
121 {
122 pcap_t *p;
123 pcap_if_t *curdev, *prevdev, *nextdev;
124 int this_instance;
125
126 /*
127 * Can we open this interface for live capture?
128 */
129 p = pcap_open_live(name, 68, 0, 0, errbuf);
130 if (p == NULL) {
131 /*
132 * No. Don't bother including it.
133 * Don't treat this as an error, though.
134 */
135 *curdev_ret = NULL;
136 return (0);
137 }
138 pcap_close(p);
139
140 /*
141 * Is there already an entry in the list for this interface?
142 */
143 for (curdev = *alldevs; curdev != NULL; curdev = curdev->next) {
144 if (strcmp(name, curdev->name) == 0)
145 break; /* yes, we found it */
146 }
147 if (curdev == NULL) {
148 /*
149 * No, we didn't find it.
150 * Allocate a new entry.
151 */
152 curdev = malloc(sizeof(pcap_if_t));
153 if (curdev == NULL) {
154 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
155 "malloc: %s", pcap_strerror(errno));
156 return (-1);
157 }
158
159 /*
160 * Fill in the entry.
161 */
162 curdev->next = NULL;
163 curdev->name = malloc(strlen(name) + 1);
164 strcpy(curdev->name, name);
165 if (description != NULL) {
166 /*
167 * We have a description for this interface.
168 */
169 curdev->description = malloc(strlen(description) + 1);
170 strcpy(curdev->description, description);
171 } else {
172 /*
173 * We don't.
174 */
175 curdev->description = NULL;
176 }
177 curdev->addresses = NULL; /* list starts out as empty */
178 curdev->flags = 0;
179 if (ISLOOPBACK(name, flags))
180 curdev->flags |= PCAP_IF_LOOPBACK;
181
182 /*
183 * Add it to the list, in the appropriate location.
184 * First, get the instance number of this interface.
185 */
186 this_instance = get_instance(name);
187
188 /*
189 * Now look for the last interface with an instance number
190 * less than or equal to the new interface's instance
191 * number - except that non-loopback interfaces are
192 * arbitrarily treated as having interface numbers less
193 * than those of loopback interfaces, so the loopback
194 * interfaces are put at the end of the list.
195 *
196 * We start with "prevdev" being NULL, meaning we're before
197 * the first element in the list.
198 */
199 prevdev = NULL;
200 for (;;) {
201 /*
202 * Get the interface after this one.
203 */
204 if (prevdev == NULL) {
205 /*
206 * The next element is the first element.
207 */
208 nextdev = *alldevs;
209 } else
210 nextdev = prevdev->next;
211
212 /*
213 * Are we at the end of the list?
214 */
215 if (nextdev == NULL) {
216 /*
217 * Yes - we have to put the new entry
218 * after "prevdev".
219 */
220 break;
221 }
222
223 /*
224 * Is the new interface a non-loopback interface
225 * and the next interface a loopback interface?
226 */
227 if (!(curdev->flags & PCAP_IF_LOOPBACK) &&
228 (nextdev->flags & PCAP_IF_LOOPBACK)) {
229 /*
230 * Yes, we should put the new entry
231 * before "nextdev", i.e. after "prevdev".
232 */
233 break;
234 }
235
236 /*
237 * Is the new interface's instance number less
238 * than the next interface's instance number,
239 * and is it the case that the new interface is a
240 * non-loopback interface or the next interface is
241 * a loopback interface?
242 *
243 * (The goal of both loopback tests is to make
244 * sure that we never put a loopback interface
245 * before any non-loopback interface and that we
246 * always put a non-loopback interface before all
247 * loopback interfaces.)
248 */
249 if (this_instance < get_instance(nextdev->name) &&
250 (!(curdev->flags & PCAP_IF_LOOPBACK) ||
251 (nextdev->flags & PCAP_IF_LOOPBACK))) {
252 /*
253 * Yes - we should put the new entry
254 * before "nextdev", i.e. after "prevdev".
255 */
256 break;
257 }
258
259 prevdev = nextdev;
260 }
261
262 /*
263 * Insert before "nextdev".
264 */
265 curdev->next = nextdev;
266
267 /*
268 * Insert after "prevdev" - unless "prevdev" is null,
269 * in which case this is the first interface.
270 */
271 if (prevdev == NULL) {
272 /*
273 * This is the first interface. Pass back a
274 * pointer to it, and put "curdev" before
275 * "nextdev".
276 */
277 *alldevs = curdev;
278 } else
279 prevdev->next = curdev;
280 }
281
282 *curdev_ret = curdev;
283 return (0);
284 }
285
286 int
287 add_addr_to_iflist(pcap_if_t **alldevs, char *name, u_int flags,
288 struct sockaddr *addr, size_t addr_size,
289 struct sockaddr *netmask, size_t netmask_size,
290 struct sockaddr *broadaddr, size_t broadaddr_size,
291 struct sockaddr *dstaddr, size_t dstaddr_size,
292 char *errbuf)
293 {
294 pcap_if_t *curdev;
295 pcap_addr_t *curaddr, *prevaddr, *nextaddr;
296
297 if (add_or_find_if(&curdev, alldevs, name, flags, NULL, errbuf) == -1) {
298 /*
299 * Error - give up.
300 */
301 return (-1);
302 }
303 if (curdev == NULL) {
304 /*
305 * Device wasn't added because it can't be opened.
306 * Not a fatal error.
307 */
308 return (0);
309 }
310
311 /*
312 * "curdev" is an entry for this interface; add an entry for this
313 * address to its list of addresses.
314 *
315 * Allocate the new entry and fill it in.
316 */
317 curaddr = malloc(sizeof(pcap_addr_t));
318 if (curaddr == NULL) {
319 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
320 "malloc: %s", pcap_strerror(errno));
321 return (-1);
322 }
323
324 curaddr->next = NULL;
325 if (addr != NULL) {
326 curaddr->addr = dup_sockaddr(addr, addr_size);
327 if (curaddr->addr == NULL) {
328 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
329 "malloc: %s", pcap_strerror(errno));
330 free(curaddr);
331 return (-1);
332 }
333 } else
334 curaddr->addr = NULL;
335
336 if (netmask != NULL) {
337 curaddr->netmask = dup_sockaddr(netmask, netmask_size);
338 if (curaddr->netmask == NULL) {
339 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
340 "malloc: %s", pcap_strerror(errno));
341 free(curaddr);
342 return (-1);
343 }
344 } else
345 curaddr->netmask = NULL;
346
347 if (broadaddr != NULL) {
348 curaddr->broadaddr = dup_sockaddr(broadaddr, broadaddr_size);
349 if (curaddr->broadaddr == NULL) {
350 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
351 "malloc: %s", pcap_strerror(errno));
352 free(curaddr);
353 return (-1);
354 }
355 } else
356 curaddr->broadaddr = NULL;
357
358 if (dstaddr != NULL) {
359 curaddr->dstaddr = dup_sockaddr(dstaddr, dstaddr_size);
360 if (curaddr->dstaddr == NULL) {
361 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
362 "malloc: %s", pcap_strerror(errno));
363 free(curaddr);
364 return (-1);
365 }
366 } else
367 curaddr->dstaddr = NULL;
368
369 /*
370 * Find the end of the list of addresses.
371 */
372 for (prevaddr = curdev->addresses; prevaddr != NULL; prevaddr = nextaddr) {
373 nextaddr = prevaddr->next;
374 if (nextaddr == NULL) {
375 /*
376 * This is the end of the list.
377 */
378 break;
379 }
380 }
381
382 if (prevaddr == NULL) {
383 /*
384 * The list was empty; this is the first member.
385 */
386 curdev->addresses = curaddr;
387 } else {
388 /*
389 * "prevaddr" is the last member of the list; append
390 * this member to it.
391 */
392 prevaddr->next = curaddr;
393 }
394
395 return (0);
396 }
397
398 int
399 pcap_add_if(pcap_if_t **devlist, char *name, u_int flags,
400 const char *description, char *errbuf)
401 {
402 pcap_if_t *curdev;
403
404 return (add_or_find_if(&curdev, devlist, name, flags, description,
405 errbuf));
406 }
407
408
409 /*
410 * Free a list of interfaces.
411 */
412 void
413 pcap_freealldevs(pcap_if_t *alldevs)
414 {
415 pcap_if_t *curdev, *nextdev;
416 pcap_addr_t *curaddr, *nextaddr;
417
418 for (curdev = alldevs; curdev != NULL; curdev = nextdev) {
419 nextdev = curdev->next;
420
421 /*
422 * Free all addresses.
423 */
424 for (curaddr = curdev->addresses; curaddr != NULL; curaddr = nextaddr) {
425 nextaddr = curaddr->next;
426 if (curaddr->addr)
427 free(curaddr->addr);
428 if (curaddr->netmask)
429 free(curaddr->netmask);
430 if (curaddr->broadaddr)
431 free(curaddr->broadaddr);
432 if (curaddr->dstaddr)
433 free(curaddr->dstaddr);
434 free(curaddr);
435 }
436
437 /*
438 * Free the name string.
439 */
440 free(curdev->name);
441
442 /*
443 * Free the description string, if any.
444 */
445 if (curdev->description != NULL)
446 free(curdev->description);
447
448 /*
449 * Free the interface.
450 */
451 free(curdev);
452 }
453 }
454
455 #ifndef WIN32
456
457 /*
458 * Return the name of a network interface attached to the system, or NULL
459 * if none can be found. The interface must be configured up; the
460 * lowest unit number is preferred; loopback is ignored.
461 */
462 char *
463 pcap_lookupdev(errbuf)
464 register char *errbuf;
465 {
466 pcap_if_t *alldevs;
467 /* for old BSD systems, including bsdi3 */
468 #ifndef IF_NAMESIZE
469 #define IF_NAMESIZE IFNAMSIZ
470 #endif
471 static char device[IF_NAMESIZE + 1];
472 char *ret;
473
474 if (pcap_findalldevs(&alldevs, errbuf) == -1)
475 return (NULL);
476
477 if (alldevs == NULL || (alldevs->flags & PCAP_IF_LOOPBACK)) {
478 /*
479 * There are no devices on the list, or the first device
480 * on the list is a loopback device, which means there
481 * are no non-loopback devices on the list. This means
482 * we can't return any device.
483 *
484 * XXX - why not return a loopback device? If we can't
485 * capture on it, it won't be on the list, and if it's
486 * on the list, there aren't any non-loopback devices,
487 * so why not just supply it as the default device?
488 */
489 (void)strlcpy(errbuf, "no suitable device found",
490 PCAP_ERRBUF_SIZE);
491 ret = NULL;
492 } else {
493 /*
494 * Return the name of the first device on the list.
495 */
496 (void)strlcpy(device, alldevs->name, sizeof(device));
497 ret = device;
498 }
499
500 pcap_freealldevs(alldevs);
501 return (ret);
502 }
503
504 int
505 pcap_lookupnet(device, netp, maskp, errbuf)
506 register char *device;
507 register bpf_u_int32 *netp, *maskp;
508 register char *errbuf;
509 {
510 register int fd;
511 register struct sockaddr_in *sin;
512 struct ifreq ifr;
513
514 /*
515 * The pseudo-device "any" listens on all interfaces and therefore
516 * has the network address and -mask "0.0.0.0" therefore catching
517 * all traffic. Using NULL for the interface is the same as "any".
518 */
519 if (!device || strcmp(device, "any") == 0) {
520 *netp = *maskp = 0;
521 return 0;
522 }
523
524 fd = socket(AF_INET, SOCK_DGRAM, 0);
525 if (fd < 0) {
526 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "socket: %s",
527 pcap_strerror(errno));
528 return (-1);
529 }
530 memset(&ifr, 0, sizeof(ifr));
531 #ifdef linux
532 /* XXX Work around Linux kernel bug */
533 ifr.ifr_addr.sa_family = AF_INET;
534 #endif
535 (void)strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
536 if (ioctl(fd, SIOCGIFADDR, (char *)&ifr) < 0) {
537 if (errno == EADDRNOTAVAIL) {
538 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
539 "%s: no IPv4 address assigned", device);
540 } else {
541 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
542 "SIOCGIFADDR: %s: %s",
543 device, pcap_strerror(errno));
544 }
545 (void)close(fd);
546 return (-1);
547 }
548 sin = (struct sockaddr_in *)&ifr.ifr_addr;
549 *netp = sin->sin_addr.s_addr;
550 if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifr) < 0) {
551 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
552 "SIOCGIFNETMASK: %s: %s", device, pcap_strerror(errno));
553 (void)close(fd);
554 return (-1);
555 }
556 (void)close(fd);
557 *maskp = sin->sin_addr.s_addr;
558 if (*maskp == 0) {
559 if (IN_CLASSA(*netp))
560 *maskp = IN_CLASSA_NET;
561 else if (IN_CLASSB(*netp))
562 *maskp = IN_CLASSB_NET;
563 else if (IN_CLASSC(*netp))
564 *maskp = IN_CLASSC_NET;
565 else {
566 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
567 "inet class for 0x%x unknown", *netp);
568 return (-1);
569 }
570 }
571 *netp &= *maskp;
572 return (0);
573 }
574
575 #else /* WIN32 */
576
577 /*
578 * Return the name of a network interface attached to the system, or NULL
579 * if none can be found. The interface must be configured up; the
580 * lowest unit number is preferred; loopback is ignored.
581 */
582 char *
583 pcap_lookupdev(errbuf)
584 register char *errbuf;
585 {
586 DWORD dwVersion;
587 DWORD dwWindowsMajorVersion;
588 dwVersion = GetVersion(); /* get the OS version */
589 dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
590
591 if (dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4) {
592 /*
593 * Windows 95, 98, ME.
594 */
595 ULONG NameLength = 8192;
596 static char AdaptersName[8192];
597
598 PacketGetAdapterNames(AdaptersName,&NameLength);
599
600 return (AdaptersName);
601 } else {
602 /*
603 * Windows NT (NT 4.0, W2K, WXP).
604 */
605 ULONG NameLength = 8192;
606 static WCHAR AdaptersName[8192];
607
608 PacketGetAdapterNames((PTSTR)AdaptersName,&NameLength);
609
610 return (char *)(AdaptersName);
611 }
612 }
613
614
615 int
616 pcap_lookupnet(device, netp, maskp, errbuf)
617 register char *device;
618 register bpf_u_int32 *netp, *maskp;
619 register char *errbuf;
620 {
621 /*
622 * We need only the first address, so we allocate a single
623 * npf_if_addr structure and we set if_addr_size to 1.
624 */
625 npf_if_addr if_addrs;
626 LONG if_addr_size = 1;
627 struct sockaddr_in *t_addr;
628
629 if (!PacketGetNetInfoEx((void *)device, &if_addrs, &if_addr_size)) {
630 *netp = *maskp = 0;
631 return (0);
632 }
633
634 t_addr = (struct sockaddr_in *) &(if_addrs.IPAddress);
635 *netp = t_addr->sin_addr.S_un.S_addr;
636 t_addr = (struct sockaddr_in *) &(if_addrs.SubnetMask);
637 *maskp = t_addr->sin_addr.S_un.S_addr;
638
639 /*
640 * XXX - will we ever get back a 0 netmask?
641 * If so, we should presumably make the "if (*maskp == 0)" code
642 * above common, rather than non-Win32-specific.
643 */
644
645 *netp &= *maskp;
646 return (0);
647 }
648
649 #endif /* WIN32 */