X-Git-Url: https://git.tcpdump.org/tcpdump/blobdiff_plain/f0ff49cc4da980f56cc08211c03b9260e6c5400f..refs/pull/482/head:/tcpdump.c diff --git a/tcpdump.c b/tcpdump.c index eb1241ad..a0c627c6 100644 --- a/tcpdump.c +++ b/tcpdump.c @@ -56,17 +56,6 @@ The Regents of the University of California. All rights reserved.\n"; #include -#ifdef _WIN32 -#ifndef _WINSOCKAPI_ -#define _WINSOCKAPI_ /* Prevent inclusion of winsock.h in windows.h */ -#endif /* _WINSOCKAPI_ */ -#include -#include -extern int SIZE_BUF; -#define off_t long -#define uint UINT -#endif /* _WIN32 */ - #ifdef USE_LIBSMI #include #endif @@ -736,18 +725,20 @@ main(int argc, char **argv) cap_rights_t rights; int cansandbox; #endif /* HAVE_CAPSICUM */ - int Bflag=0; /* buffer size */ - int jflag=-1; /* packet time stamp source */ - int Oflag=1; /* run filter code optimizer */ - int pflag=0; /* don't go promiscuous */ + int Bflag = 0; /* buffer size */ + int jflag = -1; /* packet time stamp source */ + int Oflag = 1; /* run filter code optimizer */ + int pflag = 0; /* don't go promiscuous */ + int yflag_dlt = -1; + const char *yflag_dlt_name = NULL; + netdissect_options Ndo; netdissect_options *ndo = &Ndo; + int immediate_mode = 0; memset(ndo, 0, sizeof(*ndo)); - ndo->ndo_dlt=-1; ndo_set_function_pointers(ndo); ndo->ndo_snaplen = DEFAULT_SNAPLEN; - ndo->ndo_immediate = 0; cnt = -1; device = NULL; @@ -1110,11 +1101,11 @@ main(int argc, char **argv) break; case 'y': - ndo->ndo_dltname = optarg; - ndo->ndo_dlt = - pcap_datalink_name_to_val(ndo->ndo_dltname); - if (ndo->ndo_dlt < 0) - error("invalid data link type %s", ndo->ndo_dltname); + yflag_dlt_name = optarg; + yflag_dlt = + pcap_datalink_name_to_val(yflag_dlt_name); + if (yflag_dlt < 0) + error("invalid data link type %s", yflag_dlt_name); break; #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG) @@ -1162,7 +1153,7 @@ main(int argc, char **argv) #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE case OPTION_IMMEDIATE_MODE: - ndo->ndo_immediate = 1; + immediate_mode = 1; break; #endif @@ -1209,7 +1200,7 @@ main(int argc, char **argv) * probably expecting to see packets pop up immediately. */ if (WFileName == NULL && isatty(1)) - ndo->ndo_immediate = 1; + immediate_mode = 1; #endif #ifdef WITH_CHROOT @@ -1300,7 +1291,12 @@ main(int argc, char **argv) * We're doing a live capture. */ if (device == NULL) { +#ifdef HAVE_PCAP_FINDALLDEVS + if (pcap_findalldevs(&devpointer, ebuf) >= 0 && devpointer != NULL) + device = devpointer->name; +#else /* HAVE_PCAP_FINDALLDEVS */ device = pcap_lookupdev(ebuf); +#endif if (device == NULL) error("%s", ebuf); } @@ -1338,7 +1334,7 @@ main(int argc, char **argv) #endif #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE - if (ndo->ndo_immediate) { + if (immediate_mode) { status = pcap_set_immediate_mode(pd, 1); if (status != 0) error("%s: Can't set immediate mode: %s", @@ -1427,7 +1423,8 @@ main(int argc, char **argv) #endif /* HAVE_PCAP_SETDIRECTION */ #else *ebuf = '\0'; - pd = pcap_open_live(device, snaplen, !pflag, 1000, ebuf); + pd = pcap_open_live(device, ndo->ndo_snaplen, !pflag, 1000, + ebuf); if (pd == NULL) error("%s", ebuf); else if (*ebuf) @@ -1448,9 +1445,9 @@ main(int argc, char **argv) #endif /* !defined(HAVE_PCAP_CREATE) && defined(_WIN32) */ if (Lflag) show_dlts_and_exit(device); - if (ndo->ndo_dlt >= 0) { + if (yflag_dlt >= 0) { #ifdef HAVE_PCAP_SET_DATALINK - if (pcap_set_datalink(pd, ndo->ndo_dlt) < 0) + if (pcap_set_datalink(pd, yflag_dlt) < 0) error("%s", pcap_geterr(pd)); #else /* @@ -1458,13 +1455,13 @@ main(int argc, char **argv) * data link type, so we only let them * set it to what it already is. */ - if (ndo->ndo_dlt != pcap_datalink(pd)) { + if (yflag_dlt != pcap_datalink(pd)) { error("%s is not one of the DLTs supported by this device\n", - ndo->ndo_dltname); + yflag_dlt_name); } #endif (void)fprintf(stderr, "%s: data link type %s\n", - program_name, ndo->ndo_dltname); + program_name, yflag_dlt_name); (void)fflush(stderr); } i = pcap_snapshot(pd); @@ -1845,14 +1842,14 @@ child_cleanup(int signo _U_) static void info(register int verbose) { - struct pcap_stat stat; + struct pcap_stat stats; /* * Older versions of libpcap didn't set ps_ifdrop on some * platforms; initialize it to 0 to handle that. */ - stat.ps_ifdrop = 0; - if (pcap_stats(pd, &stat) < 0) { + stats.ps_ifdrop = 0; + if (pcap_stats(pd, &stats) < 0) { (void)fprintf(stderr, "pcap_stats: %s\n", pcap_geterr(pd)); infoprint = 0; return; @@ -1867,21 +1864,21 @@ info(register int verbose) fputs(", ", stderr); else putc('\n', stderr); - (void)fprintf(stderr, "%u packet%s received by filter", stat.ps_recv, - PLURAL_SUFFIX(stat.ps_recv)); + (void)fprintf(stderr, "%u packet%s received by filter", stats.ps_recv, + PLURAL_SUFFIX(stats.ps_recv)); if (!verbose) fputs(", ", stderr); else putc('\n', stderr); - (void)fprintf(stderr, "%u packet%s dropped by kernel", stat.ps_drop, - PLURAL_SUFFIX(stat.ps_drop)); - if (stat.ps_ifdrop != 0) { + (void)fprintf(stderr, "%u packet%s dropped by kernel", stats.ps_drop, + PLURAL_SUFFIX(stats.ps_drop)); + if (stats.ps_ifdrop != 0) { if (!verbose) fputs(", ", stderr); else putc('\n', stderr); (void)fprintf(stderr, "%u packet%s dropped by interface\n", - stat.ps_ifdrop, PLURAL_SUFFIX(stat.ps_ifdrop)); + stats.ps_ifdrop, PLURAL_SUFFIX(stats.ps_ifdrop)); } else putc('\n', stderr); infoprint = 0; @@ -2203,17 +2200,13 @@ RETSIGTYPE requestinfo(int signo _U_) void CALLBACK verbose_stats_dump (UINT timer_id _U_, UINT msg _U_, DWORD_PTR arg _U_, DWORD_PTR dw1 _U_, DWORD_PTR dw2 _U_) { - struct pcap_stat stat; - - if (infodelay == 0 && pcap_stats(pd, &stat) >= 0) + if (infodelay == 0) fprintf(stderr, "Got %u\r", packets_captured); } #elif defined(HAVE_ALARM) static void verbose_stats_dump(int sig _U_) { - struct pcap_stat stat; - - if (infodelay == 0 && pcap_stats(pd, &stat) >= 0) + if (infodelay == 0) fprintf(stderr, "Got %u\r", packets_captured); alarm(1); }