]> The Tcpdump Group git mirrors - libpcap/blob - inet.c
Merge branch 'master' of git://github.com/mcr/libpcap
[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[] _U_ =
37 "@(#) $Header: /tcpdump/master/libpcap/inet.c,v 1.79 2008-04-20 18:19:02 guy Exp $ (LBL)";
38 #endif
39
40 #ifdef HAVE_CONFIG_H
41 #include "config.h"
42 #endif
43
44 #ifdef WIN32
45 #include <pcap-stdinc.h>
46 #else /* WIN32 */
47
48 #include <sys/param.h>
49 #ifndef MSDOS
50 #include <sys/file.h>
51 #endif
52 #include <sys/ioctl.h>
53 #include <sys/socket.h>
54 #ifdef HAVE_SYS_SOCKIO_H
55 #include <sys/sockio.h>
56 #endif
57
58 struct mbuf; /* Squelch compiler warnings on some platforms for */
59 struct rtentry; /* declarations in <net/if.h> */
60 #include <net/if.h>
61 #include <netinet/in.h>
62 #endif /* WIN32 */
63
64 #include <ctype.h>
65 #include <errno.h>
66 #include <memory.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #if !defined(WIN32) && !defined(__BORLANDC__)
71 #include <unistd.h>
72 #endif /* !WIN32 && !__BORLANDC__ */
73 #ifdef HAVE_LIMITS_H
74 #include <limits.h>
75 #else
76 #define INT_MAX 2147483647
77 #endif
78
79 #include "pcap-int.h"
80
81 #ifdef HAVE_OS_PROTO_H
82 #include "os-proto.h"
83 #endif
84
85 /* Not all systems have IFF_LOOPBACK */
86 #ifdef IFF_LOOPBACK
87 #define ISLOOPBACK(name, flags) ((flags) & IFF_LOOPBACK)
88 #else
89 #define ISLOOPBACK(name, flags) ((name)[0] == 'l' && (name)[1] == 'o' && \
90 (isdigit((unsigned char)((name)[2])) || (name)[2] == '\0'))
91 #endif
92
93 struct sockaddr *
94 dup_sockaddr(struct sockaddr *sa, size_t sa_length)
95 {
96 struct sockaddr *newsa;
97
98 if ((newsa = malloc(sa_length)) == NULL)
99 return (NULL);
100 return (memcpy(newsa, sa, sa_length));
101 }
102
103 static int
104 get_instance(const char *name)
105 {
106 const char *cp, *endcp;
107 int n;
108
109 if (strcmp(name, "any") == 0) {
110 /*
111 * Give the "any" device an artificially high instance
112 * number, so it shows up after all other non-loopback
113 * interfaces.
114 */
115 return INT_MAX;
116 }
117
118 endcp = name + strlen(name);
119 for (cp = name; cp < endcp && !isdigit((unsigned char)*cp); ++cp)
120 continue;
121
122 if (isdigit((unsigned char)*cp))
123 n = atoi(cp);
124 else
125 n = 0;
126 return (n);
127 }
128
129 int
130 add_or_find_if(pcap_if_t **curdev_ret, pcap_if_t **alldevs, const char *name,
131 u_int flags, const char *description, char *errbuf)
132 {
133 pcap_t *p;
134 pcap_if_t *curdev, *prevdev, *nextdev;
135 int this_instance;
136
137 /*
138 * Is there already an entry in the list for this interface?
139 */
140 for (curdev = *alldevs; curdev != NULL; curdev = curdev->next) {
141 if (strcmp(name, curdev->name) == 0)
142 break; /* yes, we found it */
143 }
144
145 if (curdev == NULL) {
146 /*
147 * No, we didn't find it.
148 *
149 * Can we open this interface for live capture?
150 *
151 * We do this check so that interfaces that are
152 * supplied by the interface enumeration mechanism
153 * we're using but that don't support packet capture
154 * aren't included in the list. Loopback interfaces
155 * on Solaris are an example of this; we don't just
156 * omit loopback interfaces on all platforms because
157 * you *can* capture on loopback interfaces on some
158 * OSes.
159 *
160 * On OS X, we don't do this check if the device
161 * name begins with "wlt"; at least some versions
162 * of OS X offer monitor mode capturing by having
163 * a separate "monitor mode" device for each wireless
164 * adapter, rather than by implementing the ioctls
165 * that {Free,Net,Open,DragonFly}BSD provide.
166 * Opening that device puts the adapter into monitor
167 * mode, which, at least for some adapters, causes
168 * them to deassociate from the network with which
169 * they're associated.
170 *
171 * Instead, we try to open the corresponding "en"
172 * device (so that we don't end up with, for users
173 * without sufficient privilege to open capture
174 * devices, a list of adapters that only includes
175 * the wlt devices).
176 */
177 #ifdef __APPLE__
178 if (strncmp(name, "wlt", 3) == 0) {
179 char *en_name;
180 size_t en_name_len;
181
182 /*
183 * Try to allocate a buffer for the "en"
184 * device's name.
185 */
186 en_name_len = strlen(name) - 1;
187 en_name = malloc(en_name_len + 1);
188 if (en_name == NULL) {
189 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
190 "malloc: %s", pcap_strerror(errno));
191 return (-1);
192 }
193 strcpy(en_name, "en");
194 strcat(en_name, name + 3);
195 p = pcap_open_live(en_name, 68, 0, 0, errbuf);
196 free(en_name);
197 } else
198 #endif /* __APPLE */
199 p = pcap_open_live(name, 68, 0, 0, errbuf);
200 if (p == NULL) {
201 /*
202 * No. Don't bother including it.
203 * Don't treat this as an error, though.
204 */
205 *curdev_ret = NULL;
206 return (0);
207 }
208 pcap_close(p);
209
210 /*
211 * Yes, we can open it.
212 * Allocate a new entry.
213 */
214 curdev = malloc(sizeof(pcap_if_t));
215 if (curdev == NULL) {
216 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
217 "malloc: %s", pcap_strerror(errno));
218 return (-1);
219 }
220
221 /*
222 * Fill in the entry.
223 */
224 curdev->next = NULL;
225 curdev->name = strdup(name);
226 if (curdev->name == NULL) {
227 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
228 "malloc: %s", pcap_strerror(errno));
229 free(curdev);
230 return (-1);
231 }
232 if (description != NULL) {
233 /*
234 * We have a description for this interface.
235 */
236 curdev->description = strdup(description);
237 if (curdev->description == NULL) {
238 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
239 "malloc: %s", pcap_strerror(errno));
240 free(curdev->name);
241 free(curdev);
242 return (-1);
243 }
244 } else {
245 /*
246 * We don't.
247 */
248 curdev->description = NULL;
249 }
250 curdev->addresses = NULL; /* list starts out as empty */
251 curdev->flags = 0;
252 if (ISLOOPBACK(name, flags))
253 curdev->flags |= PCAP_IF_LOOPBACK;
254
255 /*
256 * Add it to the list, in the appropriate location.
257 * First, get the instance number of this interface.
258 */
259 this_instance = get_instance(name);
260
261 /*
262 * Now look for the last interface with an instance number
263 * less than or equal to the new interface's instance
264 * number - except that non-loopback interfaces are
265 * arbitrarily treated as having interface numbers less
266 * than those of loopback interfaces, so the loopback
267 * interfaces are put at the end of the list.
268 *
269 * We start with "prevdev" being NULL, meaning we're before
270 * the first element in the list.
271 */
272 prevdev = NULL;
273 for (;;) {
274 /*
275 * Get the interface after this one.
276 */
277 if (prevdev == NULL) {
278 /*
279 * The next element is the first element.
280 */
281 nextdev = *alldevs;
282 } else
283 nextdev = prevdev->next;
284
285 /*
286 * Are we at the end of the list?
287 */
288 if (nextdev == NULL) {
289 /*
290 * Yes - we have to put the new entry
291 * after "prevdev".
292 */
293 break;
294 }
295
296 /*
297 * Is the new interface a non-loopback interface
298 * and the next interface a loopback interface?
299 */
300 if (!(curdev->flags & PCAP_IF_LOOPBACK) &&
301 (nextdev->flags & PCAP_IF_LOOPBACK)) {
302 /*
303 * Yes, we should put the new entry
304 * before "nextdev", i.e. after "prevdev".
305 */
306 break;
307 }
308
309 /*
310 * Is the new interface's instance number less
311 * than the next interface's instance number,
312 * and is it the case that the new interface is a
313 * non-loopback interface or the next interface is
314 * a loopback interface?
315 *
316 * (The goal of both loopback tests is to make
317 * sure that we never put a loopback interface
318 * before any non-loopback interface and that we
319 * always put a non-loopback interface before all
320 * loopback interfaces.)
321 */
322 if (this_instance < get_instance(nextdev->name) &&
323 (!(curdev->flags & PCAP_IF_LOOPBACK) ||
324 (nextdev->flags & PCAP_IF_LOOPBACK))) {
325 /*
326 * Yes - we should put the new entry
327 * before "nextdev", i.e. after "prevdev".
328 */
329 break;
330 }
331
332 prevdev = nextdev;
333 }
334
335 /*
336 * Insert before "nextdev".
337 */
338 curdev->next = nextdev;
339
340 /*
341 * Insert after "prevdev" - unless "prevdev" is null,
342 * in which case this is the first interface.
343 */
344 if (prevdev == NULL) {
345 /*
346 * This is the first interface. Pass back a
347 * pointer to it, and put "curdev" before
348 * "nextdev".
349 */
350 *alldevs = curdev;
351 } else
352 prevdev->next = curdev;
353 }
354
355 *curdev_ret = curdev;
356 return (0);
357 }
358
359 /*
360 * XXX - on FreeBSDs that support it, should it get the sysctl named
361 * "dev.{adapter family name}.{adapter unit}.%desc" to get a description
362 * of the adapter? Note that "dev.an.0.%desc" is "Aironet PC4500/PC4800"
363 * with my Cisco 350 card, so the name isn't entirely descriptive. The
364 * "dev.an.0.%pnpinfo" has a better description, although one might argue
365 * that the problem is really a driver bug - if it can find out that it's
366 * a Cisco 340 or 350, rather than an old Aironet card, it should use
367 * that in the description.
368 *
369 * Do NetBSD, DragonflyBSD, or OpenBSD support this as well? FreeBSD
370 * and OpenBSD let you get a description, but it's not generated by the OS,
371 * it's set with another ioctl that ifconfig supports; we use that to get
372 * a description in FreeBSD and OpenBSD, but if there is no such
373 * description available, it still might be nice to get some description
374 * string based on the device type or something such as that.
375 *
376 * In OS X, the System Configuration framework can apparently return
377 * names in 10.4 and later.
378 *
379 * It also appears that freedesktop.org's HAL offers an "info.product"
380 * string, but the HAL specification says it "should not be used in any
381 * UI" and "subsystem/capability specific properties" should be used
382 * instead and, in any case, I think HAL is being deprecated in
383 * favor of other stuff such as DeviceKit. DeviceKit doesn't appear
384 * to have any obvious product information for devices, but maybe
385 * I haven't looked hard enough.
386 *
387 * Using the System Configuration framework, or HAL, or DeviceKit, or
388 * whatever, would require that libpcap applications be linked with
389 * the frameworks/libraries in question. That shouldn't be a problem
390 * for programs linking with the shared version of libpcap (unless
391 * you're running on AIX - which I think is the only UN*X that doesn't
392 * support linking a shared library with other libraries on which it
393 * depends, and having an executable linked only with the first shared
394 * library automatically pick up the other libraries when started -
395 * and using HAL or whatever). Programs linked with the static
396 * version of libpcap would have to use pcap-config with the --static
397 * flag in order to get the right linker flags in order to pick up
398 * the additional libraries/frameworks; those programs need that anyway
399 * for libpcap 1.1 and beyond on Linux, as, by default, it requires
400 * -lnl.
401 *
402 * Do any other UN*Xes, or desktop environments support getting a
403 * description?
404 */
405 int
406 add_addr_to_iflist(pcap_if_t **alldevs, const char *name, u_int flags,
407 struct sockaddr *addr, size_t addr_size,
408 struct sockaddr *netmask, size_t netmask_size,
409 struct sockaddr *broadaddr, size_t broadaddr_size,
410 struct sockaddr *dstaddr, size_t dstaddr_size,
411 char *errbuf)
412 {
413 pcap_if_t *curdev;
414 char *description = NULL;
415 pcap_addr_t *curaddr, *prevaddr, *nextaddr;
416 #ifdef SIOCGIFDESCR
417 int s;
418 struct ifreq ifrdesc;
419 #ifndef IFDESCRSIZE
420 size_t descrlen = 64;
421 #else
422 size_t descrlen = IFDESCRSIZE;
423 #endif /* IFDESCRSIZE */
424 #endif /* SIOCGIFDESCR */
425
426 #ifdef SIOCGIFDESCR
427 /*
428 * Get the description for the interface.
429 */
430 memset(&ifrdesc, 0, sizeof ifrdesc);
431 strlcpy(ifrdesc.ifr_name, name, sizeof ifrdesc.ifr_name);
432 s = socket(AF_INET, SOCK_DGRAM, 0);
433 if (s >= 0) {
434 #ifdef __FreeBSD__
435 /*
436 * On FreeBSD, if the buffer isn't big enough for the
437 * description, the ioctl succeeds, but the description
438 * isn't copied, ifr_buffer.length is set to the description
439 * length, and ifr_buffer.buffer is set to NULL.
440 */
441 for (;;) {
442 free(description);
443 if ((description = malloc(descrlen)) != NULL) {
444 ifrdesc.ifr_buffer.buffer = description;
445 ifrdesc.ifr_buffer.length = descrlen;
446 if (ioctl(s, SIOCGIFDESCR, &ifrdesc) == 0) {
447 if (ifrdesc.ifr_buffer.buffer ==
448 description)
449 break;
450 else
451 descrlen = ifrdesc.ifr_buffer.length;
452 } else {
453 /*
454 * Failed to get interface description.
455 */
456 free(description);
457 description = NULL;
458 break;
459 }
460 } else
461 break;
462 }
463 #else /* __FreeBSD__ */
464 /*
465 * The only other OS that currently supports
466 * SIOCGIFDESCR is OpenBSD, and it has no way
467 * to get the description length - it's clamped
468 * to a maximum of IFDESCRSIZE.
469 */
470 if ((description = malloc(descrlen)) != NULL) {
471 ifrdesc.ifr_data = (caddr_t)description;
472 if (ioctl(s, SIOCGIFDESCR, &ifrdesc) != 0) {
473 /*
474 * Failed to get interface description.
475 */
476 free(description);
477 description = NULL;
478 }
479 }
480 #endif /* __FreeBSD__ */
481 close(s);
482 if (description != NULL && strlen(description) == 0) {
483 free(description);
484 description = NULL;
485 }
486 }
487 #endif /* SIOCGIFDESCR */
488
489 if (add_or_find_if(&curdev, alldevs, name, flags, description,
490 errbuf) == -1) {
491 free(description);
492 /*
493 * Error - give up.
494 */
495 return (-1);
496 }
497 free(description);
498 if (curdev == NULL) {
499 /*
500 * Device wasn't added because it can't be opened.
501 * Not a fatal error.
502 */
503 return (0);
504 }
505
506 /*
507 * "curdev" is an entry for this interface; add an entry for this
508 * address to its list of addresses.
509 *
510 * Allocate the new entry and fill it in.
511 */
512 curaddr = malloc(sizeof(pcap_addr_t));
513 if (curaddr == NULL) {
514 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
515 "malloc: %s", pcap_strerror(errno));
516 return (-1);
517 }
518
519 curaddr->next = NULL;
520 if (addr != NULL) {
521 curaddr->addr = dup_sockaddr(addr, addr_size);
522 if (curaddr->addr == NULL) {
523 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
524 "malloc: %s", pcap_strerror(errno));
525 free(curaddr);
526 return (-1);
527 }
528 } else
529 curaddr->addr = NULL;
530
531 if (netmask != NULL) {
532 curaddr->netmask = dup_sockaddr(netmask, netmask_size);
533 if (curaddr->netmask == NULL) {
534 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
535 "malloc: %s", pcap_strerror(errno));
536 if (curaddr->addr != NULL)
537 free(curaddr->addr);
538 free(curaddr);
539 return (-1);
540 }
541 } else
542 curaddr->netmask = NULL;
543
544 if (broadaddr != NULL) {
545 curaddr->broadaddr = dup_sockaddr(broadaddr, broadaddr_size);
546 if (curaddr->broadaddr == NULL) {
547 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
548 "malloc: %s", pcap_strerror(errno));
549 if (curaddr->netmask != NULL)
550 free(curaddr->netmask);
551 if (curaddr->addr != NULL)
552 free(curaddr->addr);
553 free(curaddr);
554 return (-1);
555 }
556 } else
557 curaddr->broadaddr = NULL;
558
559 if (dstaddr != NULL) {
560 curaddr->dstaddr = dup_sockaddr(dstaddr, dstaddr_size);
561 if (curaddr->dstaddr == NULL) {
562 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
563 "malloc: %s", pcap_strerror(errno));
564 if (curaddr->broadaddr != NULL)
565 free(curaddr->broadaddr);
566 if (curaddr->netmask != NULL)
567 free(curaddr->netmask);
568 if (curaddr->addr != NULL)
569 free(curaddr->addr);
570 free(curaddr);
571 return (-1);
572 }
573 } else
574 curaddr->dstaddr = NULL;
575
576 /*
577 * Find the end of the list of addresses.
578 */
579 for (prevaddr = curdev->addresses; prevaddr != NULL; prevaddr = nextaddr) {
580 nextaddr = prevaddr->next;
581 if (nextaddr == NULL) {
582 /*
583 * This is the end of the list.
584 */
585 break;
586 }
587 }
588
589 if (prevaddr == NULL) {
590 /*
591 * The list was empty; this is the first member.
592 */
593 curdev->addresses = curaddr;
594 } else {
595 /*
596 * "prevaddr" is the last member of the list; append
597 * this member to it.
598 */
599 prevaddr->next = curaddr;
600 }
601
602 return (0);
603 }
604
605 int
606 pcap_add_if(pcap_if_t **devlist, const char *name, u_int flags,
607 const char *description, char *errbuf)
608 {
609 pcap_if_t *curdev;
610
611 return (add_or_find_if(&curdev, devlist, name, flags, description,
612 errbuf));
613 }
614
615
616 /*
617 * Free a list of interfaces.
618 */
619 void
620 pcap_freealldevs(pcap_if_t *alldevs)
621 {
622 pcap_if_t *curdev, *nextdev;
623 pcap_addr_t *curaddr, *nextaddr;
624
625 for (curdev = alldevs; curdev != NULL; curdev = nextdev) {
626 nextdev = curdev->next;
627
628 /*
629 * Free all addresses.
630 */
631 for (curaddr = curdev->addresses; curaddr != NULL; curaddr = nextaddr) {
632 nextaddr = curaddr->next;
633 if (curaddr->addr)
634 free(curaddr->addr);
635 if (curaddr->netmask)
636 free(curaddr->netmask);
637 if (curaddr->broadaddr)
638 free(curaddr->broadaddr);
639 if (curaddr->dstaddr)
640 free(curaddr->dstaddr);
641 free(curaddr);
642 }
643
644 /*
645 * Free the name string.
646 */
647 free(curdev->name);
648
649 /*
650 * Free the description string, if any.
651 */
652 if (curdev->description != NULL)
653 free(curdev->description);
654
655 /*
656 * Free the interface.
657 */
658 free(curdev);
659 }
660 }
661
662 #if !defined(WIN32) && !defined(MSDOS)
663
664 /*
665 * Return the name of a network interface attached to the system, or NULL
666 * if none can be found. The interface must be configured up; the
667 * lowest unit number is preferred; loopback is ignored.
668 */
669 char *
670 pcap_lookupdev(errbuf)
671 register char *errbuf;
672 {
673 pcap_if_t *alldevs;
674 /* for old BSD systems, including bsdi3 */
675 #ifndef IF_NAMESIZE
676 #define IF_NAMESIZE IFNAMSIZ
677 #endif
678 static char device[IF_NAMESIZE + 1];
679 char *ret;
680
681 if (pcap_findalldevs(&alldevs, errbuf) == -1)
682 return (NULL);
683
684 if (alldevs == NULL || (alldevs->flags & PCAP_IF_LOOPBACK)) {
685 /*
686 * There are no devices on the list, or the first device
687 * on the list is a loopback device, which means there
688 * are no non-loopback devices on the list. This means
689 * we can't return any device.
690 *
691 * XXX - why not return a loopback device? If we can't
692 * capture on it, it won't be on the list, and if it's
693 * on the list, there aren't any non-loopback devices,
694 * so why not just supply it as the default device?
695 */
696 (void)strlcpy(errbuf, "no suitable device found",
697 PCAP_ERRBUF_SIZE);
698 ret = NULL;
699 } else {
700 /*
701 * Return the name of the first device on the list.
702 */
703 (void)strlcpy(device, alldevs->name, sizeof(device));
704 ret = device;
705 }
706
707 pcap_freealldevs(alldevs);
708 return (ret);
709 }
710
711 int
712 pcap_lookupnet(device, netp, maskp, errbuf)
713 register const char *device;
714 register bpf_u_int32 *netp, *maskp;
715 register char *errbuf;
716 {
717 register int fd;
718 register struct sockaddr_in *sin4;
719 struct ifreq ifr;
720
721 /*
722 * The pseudo-device "any" listens on all interfaces and therefore
723 * has the network address and -mask "0.0.0.0" therefore catching
724 * all traffic. Using NULL for the interface is the same as "any".
725 */
726 if (!device || strcmp(device, "any") == 0
727 #ifdef HAVE_DAG_API
728 || strstr(device, "dag") != NULL
729 #endif
730 #ifdef HAVE_SEPTEL_API
731 || strstr(device, "septel") != NULL
732 #endif
733 #ifdef PCAP_SUPPORT_BT
734 || strstr(device, "bluetooth") != NULL
735 #endif
736 #ifdef PCAP_SUPPORT_USB
737 || strstr(device, "usbmon") != NULL
738 #endif
739 #ifdef HAVE_SNF_API
740 || strstr(device, "snf") != NULL
741 #endif
742 ) {
743 *netp = *maskp = 0;
744 return 0;
745 }
746
747 fd = socket(AF_INET, SOCK_DGRAM, 0);
748 if (fd < 0) {
749 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "socket: %s",
750 pcap_strerror(errno));
751 return (-1);
752 }
753 memset(&ifr, 0, sizeof(ifr));
754 #ifdef linux
755 /* XXX Work around Linux kernel bug */
756 ifr.ifr_addr.sa_family = AF_INET;
757 #endif
758 (void)strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
759 if (ioctl(fd, SIOCGIFADDR, (char *)&ifr) < 0) {
760 if (errno == EADDRNOTAVAIL) {
761 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
762 "%s: no IPv4 address assigned", device);
763 } else {
764 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
765 "SIOCGIFADDR: %s: %s",
766 device, pcap_strerror(errno));
767 }
768 (void)close(fd);
769 return (-1);
770 }
771 sin4 = (struct sockaddr_in *)&ifr.ifr_addr;
772 *netp = sin4->sin_addr.s_addr;
773 memset(&ifr, 0, sizeof(ifr));
774 #ifdef linux
775 /* XXX Work around Linux kernel bug */
776 ifr.ifr_addr.sa_family = AF_INET;
777 #endif
778 (void)strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
779 if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifr) < 0) {
780 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
781 "SIOCGIFNETMASK: %s: %s", device, pcap_strerror(errno));
782 (void)close(fd);
783 return (-1);
784 }
785 (void)close(fd);
786 *maskp = sin4->sin_addr.s_addr;
787 if (*maskp == 0) {
788 if (IN_CLASSA(*netp))
789 *maskp = IN_CLASSA_NET;
790 else if (IN_CLASSB(*netp))
791 *maskp = IN_CLASSB_NET;
792 else if (IN_CLASSC(*netp))
793 *maskp = IN_CLASSC_NET;
794 else {
795 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
796 "inet class for 0x%x unknown", *netp);
797 return (-1);
798 }
799 }
800 *netp &= *maskp;
801 return (0);
802 }
803
804 #elif defined(WIN32)
805
806 /*
807 * Return the name of a network interface attached to the system, or NULL
808 * if none can be found. The interface must be configured up; the
809 * lowest unit number is preferred; loopback is ignored.
810 */
811 char *
812 pcap_lookupdev(errbuf)
813 register char *errbuf;
814 {
815 DWORD dwVersion;
816 DWORD dwWindowsMajorVersion;
817 dwVersion = GetVersion(); /* get the OS version */
818 dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
819
820 if (dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4) {
821 /*
822 * Windows 95, 98, ME.
823 */
824 ULONG NameLength = 8192;
825 static char AdaptersName[8192];
826
827 if (PacketGetAdapterNames(AdaptersName,&NameLength) )
828 return (AdaptersName);
829 else
830 return NULL;
831 } else {
832 /*
833 * Windows NT (NT 4.0, W2K, WXP). Convert the names to UNICODE for backward compatibility
834 */
835 ULONG NameLength = 8192;
836 static WCHAR AdaptersName[8192];
837 char *tAstr;
838 WCHAR *tUstr;
839 WCHAR *TAdaptersName = (WCHAR*)malloc(8192 * sizeof(WCHAR));
840 int NAdapts = 0;
841
842 if(TAdaptersName == NULL)
843 {
844 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "memory allocation failure");
845 return NULL;
846 }
847
848 if ( !PacketGetAdapterNames((PTSTR)TAdaptersName,&NameLength) )
849 {
850 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
851 "PacketGetAdapterNames: %s",
852 pcap_win32strerror());
853 free(TAdaptersName);
854 return NULL;
855 }
856
857
858 tAstr = (char*)TAdaptersName;
859 tUstr = (WCHAR*)AdaptersName;
860
861 /*
862 * Convert and copy the device names
863 */
864 while(sscanf(tAstr, "%S", tUstr) > 0)
865 {
866 tAstr += strlen(tAstr) + 1;
867 tUstr += wcslen(tUstr) + 1;
868 NAdapts ++;
869 }
870
871 tAstr++;
872 *tUstr = 0;
873 tUstr++;
874
875 /*
876 * Copy the descriptions
877 */
878 while(NAdapts--)
879 {
880 char* tmp = (char*)tUstr;
881 strcpy(tmp, tAstr);
882 tmp += strlen(tAstr) + 1;
883 tUstr = (WCHAR*)tmp;
884 tAstr += strlen(tAstr) + 1;
885 }
886
887 free(TAdaptersName);
888 return (char *)(AdaptersName);
889 }
890 }
891
892
893 int
894 pcap_lookupnet(device, netp, maskp, errbuf)
895 register const char *device;
896 register bpf_u_int32 *netp, *maskp;
897 register char *errbuf;
898 {
899 /*
900 * We need only the first IPv4 address, so we must scan the array returned by PacketGetNetInfo()
901 * in order to skip non IPv4 (i.e. IPv6 addresses)
902 */
903 npf_if_addr if_addrs[MAX_NETWORK_ADDRESSES];
904 LONG if_addr_size = 1;
905 struct sockaddr_in *t_addr;
906 unsigned int i;
907
908 if (!PacketGetNetInfoEx((void *)device, if_addrs, &if_addr_size)) {
909 *netp = *maskp = 0;
910 return (0);
911 }
912
913 for(i=0; i<MAX_NETWORK_ADDRESSES; i++)
914 {
915 if(if_addrs[i].IPAddress.ss_family == AF_INET)
916 {
917 t_addr = (struct sockaddr_in *) &(if_addrs[i].IPAddress);
918 *netp = t_addr->sin_addr.S_un.S_addr;
919 t_addr = (struct sockaddr_in *) &(if_addrs[i].SubnetMask);
920 *maskp = t_addr->sin_addr.S_un.S_addr;
921
922 *netp &= *maskp;
923 return (0);
924 }
925
926 }
927
928 *netp = *maskp = 0;
929 return (0);
930 }
931
932 #endif /* !WIN32 && !MSDOS */