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: --- 20 unchanged lines hidden (view full) --- 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 36static const char rcsid[] _U_ = |
37 "@(#) $Header: /tcpdump/master/libpcap/inet.c,v 1.66.2.6 2007/06/11 09:52:04 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> --- 84 unchanged lines hidden (view full) --- 130add_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; --- 154 unchanged lines hidden (view full) --- 406 } else 407 curaddr->addr = NULL; 408 409 if (netmask != NULL) { 410 curaddr->netmask = dup_sockaddr(netmask, netmask_size); 411 if (curaddr->netmask == NULL) { 412 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 413 "malloc: %s", pcap_strerror(errno)); |
414 if (curaddr->addr != NULL) 415 free(curaddr->addr); |
416 free(curaddr); 417 return (-1); 418 } 419 } else 420 curaddr->netmask = NULL; 421 422 if (broadaddr != NULL) { 423 curaddr->broadaddr = dup_sockaddr(broadaddr, broadaddr_size); 424 if (curaddr->broadaddr == NULL) { 425 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 426 "malloc: %s", pcap_strerror(errno)); |
427 if (curaddr->netmask != NULL) 428 free(curaddr->netmask); 429 if (curaddr->addr != NULL) 430 free(curaddr->addr); |
431 free(curaddr); 432 return (-1); 433 } 434 } else 435 curaddr->broadaddr = NULL; 436 437 if (dstaddr != NULL) { 438 curaddr->dstaddr = dup_sockaddr(dstaddr, dstaddr_size); 439 if (curaddr->dstaddr == NULL) { 440 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 441 "malloc: %s", pcap_strerror(errno)); |
442 if (curaddr->broadaddr != NULL) 443 free(curaddr->broadaddr); 444 if (curaddr->netmask != NULL) 445 free(curaddr->netmask); 446 if (curaddr->addr != NULL) 447 free(curaddr->addr); |
448 free(curaddr); 449 return (-1); 450 } 451 } else 452 curaddr->dstaddr = NULL; 453 454 /* 455 * Find the end of the list of addresses. --- 132 unchanged lines hidden (view full) --- 588 589int 590pcap_lookupnet(device, netp, maskp, errbuf) 591 register const char *device; 592 register bpf_u_int32 *netp, *maskp; 593 register char *errbuf; 594{ 595 register int fd; |
596 register struct sockaddr_in *sin4; |
597 struct ifreq ifr; 598 599 /* 600 * The pseudo-device "any" listens on all interfaces and therefore 601 * has the network address and -mask "0.0.0.0" therefore catching 602 * all traffic. Using NULL for the interface is the same as "any". 603 */ 604 if (!device || strcmp(device, "any") == 0 --- 27 unchanged lines hidden (view full) --- 632 } else { 633 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 634 "SIOCGIFADDR: %s: %s", 635 device, pcap_strerror(errno)); 636 } 637 (void)close(fd); 638 return (-1); 639 } |
640 sin4 = (struct sockaddr_in *)&ifr.ifr_addr; 641 *netp = sin4->sin_addr.s_addr; |
642 if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifr) < 0) { 643 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 644 "SIOCGIFNETMASK: %s: %s", device, pcap_strerror(errno)); 645 (void)close(fd); 646 return (-1); 647 } 648 (void)close(fd); |
649 *maskp = sin4->sin_addr.s_addr; |
650 if (*maskp == 0) { 651 if (IN_CLASSA(*netp)) 652 *maskp = IN_CLASSA_NET; 653 else if (IN_CLASSB(*netp)) 654 *maskp = IN_CLASSB_NET; 655 else if (IN_CLASSC(*netp)) 656 *maskp = IN_CLASSC_NET; 657 else { --- 136 unchanged lines hidden --- |