inet.c (147894) | inet.c (172677) |
---|---|
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_ = | 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.1 2005/06/20 21:30:17 guy Exp $ (LBL)"; | 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 /* | 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 * Can we open this interface for live capture? 139 * 140 * We do this check so that interfaces that ae supplied 141 * by the interface enumeration mechanism we're using 142 * but that don't support packet capture aren't included 143 * in the list. An example of this is loopback interfaces 144 * on Solaris; we don't just omit loopback interfaces 145 * becaue you *can* capture on loopback interfaces on some 146 * OSes. 147 */ 148 p = pcap_open_live(name, 68, 0, 0, errbuf); 149 if (p == NULL) { 150 /* 151 * No. Don't bother including it. 152 * Don't treat this as an error, though. 153 */ 154 *curdev_ret = NULL; 155 return (0); 156 } 157 pcap_close(p); 158 159 /* | |
160 * Is there already an entry in the list for this interface? 161 */ 162 for (curdev = *alldevs; curdev != NULL; curdev = curdev->next) { 163 if (strcmp(name, curdev->name) == 0) 164 break; /* yes, we found it */ 165 } | 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 |
|
166 if (curdev == NULL) { 167 /* 168 * No, we didn't find it. | 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. |
|
169 * Allocate a new entry. 170 */ 171 curdev = malloc(sizeof(pcap_if_t)); 172 if (curdev == NULL) { 173 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 174 "malloc: %s", pcap_strerror(errno)); 175 return (-1); 176 } 177 178 /* 179 * Fill in the entry. 180 */ 181 curdev->next = NULL; | 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; |
182 curdev->name = malloc(strlen(name) + 1); 183 strcpy(curdev->name, name); | 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 } |
184 if (description != NULL) { 185 /* 186 * We have a description for this interface. 187 */ | 232 if (description != NULL) { 233 /* 234 * We have a description for this interface. 235 */ |
188 curdev->description = malloc(strlen(description) + 1); 189 strcpy(curdev->description, description); | 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 } |
190 } else { 191 /* 192 * We don't. 193 */ 194 curdev->description = NULL; 195 } 196 curdev->addresses = NULL; /* list starts out as empty */ 197 curdev->flags = 0; --- 154 unchanged lines hidden (view full) --- 352 } else 353 curaddr->addr = NULL; 354 355 if (netmask != NULL) { 356 curaddr->netmask = dup_sockaddr(netmask, netmask_size); 357 if (curaddr->netmask == NULL) { 358 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 359 "malloc: %s", pcap_strerror(errno)); | 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); |
|
360 free(curaddr); 361 return (-1); 362 } 363 } else 364 curaddr->netmask = NULL; 365 366 if (broadaddr != NULL) { 367 curaddr->broadaddr = dup_sockaddr(broadaddr, broadaddr_size); 368 if (curaddr->broadaddr == NULL) { 369 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 370 "malloc: %s", pcap_strerror(errno)); | 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); |
|
371 free(curaddr); 372 return (-1); 373 } 374 } else 375 curaddr->broadaddr = NULL; 376 377 if (dstaddr != NULL) { 378 curaddr->dstaddr = dup_sockaddr(dstaddr, dstaddr_size); 379 if (curaddr->dstaddr == NULL) { 380 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 381 "malloc: %s", pcap_strerror(errno)); | 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); |
|
382 free(curaddr); 383 return (-1); 384 } 385 } else 386 curaddr->dstaddr = NULL; 387 388 /* 389 * Find the end of the list of addresses. --- 132 unchanged lines hidden (view full) --- 522 523int 524pcap_lookupnet(device, netp, maskp, errbuf) 525 register const char *device; 526 register bpf_u_int32 *netp, *maskp; 527 register char *errbuf; 528{ 529 register int fd; | 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; |
530 register struct sockaddr_in *sin; | 596 register struct sockaddr_in *sin4; |
531 struct ifreq ifr; 532 533 /* 534 * The pseudo-device "any" listens on all interfaces and therefore 535 * has the network address and -mask "0.0.0.0" therefore catching 536 * all traffic. Using NULL for the interface is the same as "any". 537 */ 538 if (!device || strcmp(device, "any") == 0 --- 27 unchanged lines hidden (view full) --- 566 } else { 567 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 568 "SIOCGIFADDR: %s: %s", 569 device, pcap_strerror(errno)); 570 } 571 (void)close(fd); 572 return (-1); 573 } | 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 } |
574 sin = (struct sockaddr_in *)&ifr.ifr_addr; 575 *netp = sin->sin_addr.s_addr; | 640 sin4 = (struct sockaddr_in *)&ifr.ifr_addr; 641 *netp = sin4->sin_addr.s_addr; |
576 if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifr) < 0) { 577 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 578 "SIOCGIFNETMASK: %s: %s", device, pcap_strerror(errno)); 579 (void)close(fd); 580 return (-1); 581 } 582 (void)close(fd); | 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); |
583 *maskp = sin->sin_addr.s_addr; | 649 *maskp = sin4->sin_addr.s_addr; |
584 if (*maskp == 0) { 585 if (IN_CLASSA(*netp)) 586 *maskp = IN_CLASSA_NET; 587 else if (IN_CLASSB(*netp)) 588 *maskp = IN_CLASSB_NET; 589 else if (IN_CLASSC(*netp)) 590 *maskp = IN_CLASSC_NET; 591 else { --- 136 unchanged lines hidden --- | 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 --- |