X-Git-Url: https://git.tcpdump.org/tcpdump/blobdiff_plain/a022ed3a9d66f8e6280629787fa93c04282f333b..cfbae609a18fa6d61d09a8b263ceaf389f510e9b:/tcpdump.c diff --git a/tcpdump.c b/tcpdump.c index cd277042..1aa285e3 100644 --- a/tcpdump.c +++ b/tcpdump.c @@ -124,6 +124,16 @@ The Regents of the University of California. All rights reserved.\n"; #include #endif /* _WIN32 */ +/* + * Pathname separator. + * Use this in pathnames, but do *not* use it in URLs. + */ +#ifdef _WIN32 +#define PATH_SEPARATOR '\\' +#else +#define PATH_SEPARATOR '/' +#endif + /* capabilities convenience library */ /* If a code depends on HAVE_LIBCAP_NG, it depends also on HAVE_CAP_NG_H. * If HAVE_CAP_NG_H is not defined, undefine HAVE_LIBCAP_NG. @@ -220,6 +230,7 @@ static int timeout = 1000; /* default timeout = 1000 ms = 1 s */ #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE static int immediate_mode; #endif +static int count_mode; static int infodelay; static int infoprint; @@ -690,6 +701,7 @@ show_remote_devices_and_exit(void) #define OPTION_TSTAMP_MICRO 133 #define OPTION_TSTAMP_NANO 134 #define OPTION_FP_TYPE 135 +#define OPTION_COUNT 136 static const struct option longopts[] = { #if defined(HAVE_PCAP_CREATE) || defined(_WIN32) @@ -733,6 +745,7 @@ static const struct option longopts[] = { { "debug-filter-parser", no_argument, NULL, 'Y' }, #endif { "relinquish-privileges", required_argument, NULL, 'Z' }, + { "count", no_argument, NULL, OPTION_COUNT }, { "fp-type", no_argument, NULL, OPTION_FP_TYPE }, { "number", no_argument, NULL, '#' }, { "print", no_argument, NULL, OPTION_PRINT }, @@ -938,7 +951,7 @@ tstamp_precision_to_string(int precision) * necessary to make the standard I/O library work with an fdopen()ed * FILE * from that descriptor. * - * A long time ago, in a galaxy far far away, AT&T decided that, instead + * A long time ago in a galaxy far, far away, AT&T decided that, instead * of providing separate APIs for getting and setting the FD_ flags on a * descriptor, getting and setting the O_ flags on a descriptor, and * locking files, they'd throw them all into a kitchen-sink fcntl() call @@ -1495,7 +1508,7 @@ main(int argc, char **argv) VFile = NULL; WFileName = NULL; dlt = -1; - if ((cp = strrchr(argv[0], '/')) != NULL) + if ((cp = strrchr(argv[0], PATH_SEPARATOR)) != NULL) ndo->program_name = program_name = cp + 1; else ndo->program_name = program_name = argv[0]; @@ -1793,6 +1806,8 @@ main(int argc, char **argv) ndo->ndo_packettype = PT_PTP; else if (ascii_strcasecmp(optarg, "someip") == 0) ndo->ndo_packettype = PT_SOMEIP; + else if (ascii_strcasecmp(optarg, "domain") == 0) + ndo->ndo_packettype = PT_DOMAIN; else error("unknown packet type `%s'", optarg); break; @@ -1908,6 +1923,10 @@ main(int argc, char **argv) float_type_check(0x4e93312d); return 0; + case OPTION_COUNT: + count_mode = 1; + break; + default: print_usage(); exit_tcpdump(S_ERR_HOST_PROGRAM); @@ -1923,6 +1942,14 @@ main(int argc, char **argv) show_remote_devices_and_exit(); #endif +#if defined(DLT_LINUX_SLL2) && defined(HAVE_PCAP_SET_DATALINK) +/* Set default linktype DLT_LINUX_SLL2 when capturing on the "any" device */ + if (device != NULL && + strncmp (device, "any", strlen("any")) == 0 + && yflag_dlt == -1) + yflag_dlt = DLT_LINUX_SLL2; +#endif + switch (ndo->ndo_tflag) { case 0: /* Default */ @@ -2171,7 +2198,8 @@ main(int argc, char **argv) } #endif (void)fprintf(stderr, "%s: data link type %s\n", - program_name, yflag_dlt_name); + program_name, + pcap_datalink_val_to_name(yflag_dlt)); (void)fflush(stderr); } i = pcap_snapshot(pd); @@ -2452,7 +2480,7 @@ DIAG_ON_CLANG(assign-enum) */ if (!ndo->ndo_vflag && !WFileName) { (void)fprintf(stderr, - "%s: verbose output suppressed, use -v or -vv for full protocol decode\n", + "%s: verbose output suppressed, use -v[v]... for full protocol decode\n", program_name); } else (void)fprintf(stderr, "%s: ", program_name); @@ -2595,6 +2623,10 @@ DIAG_ON_CLANG(assign-enum) } while (ret != NULL); + if (count_mode && RFileName != NULL) + fprintf(stderr, "%u packet%s\n", packets_captured, + PLURAL_SUFFIX(packets_captured)); + free(cmdbuf); pcap_freecode(&fcode); exit_tcpdump(status == -1 ? 1 : 0); @@ -3027,7 +3059,8 @@ print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp) ++infodelay; - pretty_print_packet((netdissect_options *)user, h, sp, packets_captured); + if (!count_mode) + pretty_print_packet((netdissect_options *)user, h, sp, packets_captured); --infodelay; if (infoprint) @@ -3114,7 +3147,9 @@ print_version(void) (void)fprintf (stderr, "Compiled with AddressSanitizer/GCC.\n"); #elif defined(__has_feature) # if __has_feature(address_sanitizer) - (void)fprintf (stderr, "Compiled with AddressSanitizer/CLang.\n"); + (void)fprintf (stderr, "Compiled with AddressSanitizer/Clang.\n"); +# elif __has_feature(memory_sanitizer) + (void)fprintf (stderr, "Compiled with MemorySanitizer/Clang.\n"); # endif #endif /* __SANITIZE_ADDRESS__ or __has_feature */ } @@ -3125,7 +3160,7 @@ print_usage(void) { print_version(); (void)fprintf(stderr, -"Usage: %s [-aAbd" D_FLAG "efhH" I_FLAG J_FLAG "KlLnNOpqStu" U_FLAG "vxX#]" B_FLAG_USAGE " [ -c count ]\n", program_name); +"Usage: %s [-Abd" D_FLAG "efhH" I_FLAG J_FLAG "KlLnNOpqStu" U_FLAG "vxX#]" B_FLAG_USAGE " [ -c count ] [--count]\n", program_name); (void)fprintf(stderr, "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n"); (void)fprintf(stderr,