2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 * Support for splitting captures into multiple files with a maximum
25 * Seth Webster <swebster@sst.ll.mit.edu>
29 static const char copyright[] _U_ =
30 "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
31 The Regents of the University of California. All rights reserved.\n";
37 * tcpdump - monitor tcp/ip traffic on an ethernet.
39 * First written in 1987 by Van Jacobson, Lawrence Berkeley Laboratory.
40 * Mercilessly hacked and occasionally improved since then via the
41 * combined efforts of Van, Steve McCanne and Craig Leres of LBL.
49 * Mac OS X may ship pcap.h from libpcap 0.6 with a libpcap based on
50 * 0.8. That means it has pcap_findalldevs() but the header doesn't
51 * define pcap_if_t, meaning that we can't actually *use* pcap_findalldevs().
53 #ifdef HAVE_PCAP_FINDALLDEVS
54 #ifndef HAVE_PCAP_IF_T
55 #undef HAVE_PCAP_FINDALLDEVS
59 #include <tcpdump-stdinc.h>
63 extern int strcasecmp (const char *__s1, const char *__s2);
74 #include <openssl/crypto.h>
77 #ifdef HAVE_GETOPT_LONG
80 #include "getopt_long.h"
82 /* Capsicum-specific code requires macros from <net/bpf.h>, which will fail
83 * to compile if <pcap.h> has already been included; including the headers
84 * in the opposite order works fine.
87 #include <sys/capsicum.h>
88 #include <sys/sysctl.h>
89 #endif /* __FreeBSD__ */
91 #include <libcasper.h>
92 #include <casper/cap_dns.h>
94 #include <sys/capability.h>
95 #include <sys/ioccom.h>
99 #endif /* HAVE_CAPSPER */
107 #include <sys/wait.h>
108 #include <sys/resource.h>
113 /* capabilities convenience library */
114 /* If a code depends on HAVE_LIBCAP_NG, it depends also on HAVE_CAP_NG_H.
115 * If HAVE_CAP_NG_H is not defined, undefine HAVE_LIBCAP_NG.
116 * Thus, the later tests are done only on HAVE_LIBCAP_NG.
118 #ifdef HAVE_LIBCAP_NG
122 #undef HAVE_LIBCAP_NG
123 #endif /* HAVE_CAP_NG_H */
124 #endif /* HAVE_LIBCAP_NG */
126 #include "netdissect.h"
127 #include "interface.h"
128 #include "addrtoname.h"
130 #include "setsignal.h"
131 #include "gmt2local.h"
132 #include "pcap-missing.h"
135 #define PATH_MAX 1024
139 #define SIGNAL_REQ_INFO SIGINFO
141 #define SIGNAL_REQ_INFO SIGUSR1
144 netdissect_options Gndo;
145 netdissect_options *gndo = &Gndo;
147 static int Dflag; /* list available devices and exit */
148 static int dflag; /* print filter code */
149 static int Lflag; /* list available data link types and exit */
150 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
151 static int Jflag; /* list available time stamp types */
153 #ifdef HAVE_PCAP_SETDIRECTION
154 int Qflag = -1; /* restrict captured packet by send/receive direction */
156 static char *zflag = NULL; /* compress each savefile using a specified command (like gzip or bzip2) */
158 static int infodelay;
159 static int infoprint;
164 cap_channel_t *capdns;
167 int32_t thiszone; /* seconds offset from gmt to local time */
170 static RETSIGTYPE cleanup(int);
171 static RETSIGTYPE child_cleanup(int);
172 static void print_version(void);
173 static void print_usage(void);
174 static void show_dlts_and_exit(const char *device, pcap_t *pd) __attribute__((noreturn));
176 static void print_packet(u_char *, const struct pcap_pkthdr *, const u_char *);
177 static void ndo_default_print(netdissect_options *, const u_char *, u_int);
178 static void dump_packet_and_trunc(u_char *, const struct pcap_pkthdr *, const u_char *);
179 static void dump_packet(u_char *, const struct pcap_pkthdr *, const u_char *);
180 static void droproot(const char *, const char *);
181 static void ndo_error(netdissect_options *ndo, const char *fmt, ...)
182 __attribute__((noreturn))
183 #ifdef __ATTRIBUTE___FORMAT_OK
184 __attribute__((format (printf, 2, 3)))
185 #endif /* __ATTRIBUTE___FORMAT_OK */
187 static void ndo_warning(netdissect_options *ndo, const char *fmt, ...)
188 #ifdef __ATTRIBUTE___FORMAT_OK
189 __attribute__((format (printf, 2, 3)))
190 #endif /* __ATTRIBUTE___FORMAT_OK */
193 #ifdef SIGNAL_REQ_INFO
194 RETSIGTYPE requestinfo(int);
197 #if defined(USE_WIN32_MM_TIMER)
198 #include <MMsystem.h>
199 static UINT timer_id;
200 static void CALLBACK verbose_stats_dump(UINT, UINT, DWORD_PTR, DWORD_PTR, DWORD_PTR);
201 #elif defined(HAVE_ALARM)
202 static void verbose_stats_dump(int sig);
205 static void info(int);
206 static u_int packets_captured;
220 static const struct printer printers[] = {
224 static const struct ndo_printer ndo_printers[] = {
225 { ether_if_print, DLT_EN10MB },
227 { ipnet_if_print, DLT_IPNET },
229 #ifdef DLT_IEEE802_15_4
230 { ieee802_15_4_if_print, DLT_IEEE802_15_4 },
232 #ifdef DLT_IEEE802_15_4_NOFCS
233 { ieee802_15_4_if_print, DLT_IEEE802_15_4_NOFCS },
236 { ppi_if_print, DLT_PPI },
238 #ifdef DLT_NETANALYZER
239 { netanalyzer_if_print, DLT_NETANALYZER },
241 #ifdef DLT_NETANALYZER_TRANSPARENT
242 { netanalyzer_transparent_if_print, DLT_NETANALYZER_TRANSPARENT },
244 #if defined(DLT_NFLOG) && defined(HAVE_PCAP_NFLOG_H)
245 { nflog_if_print, DLT_NFLOG},
248 { cip_if_print, DLT_CIP },
251 { cip_if_print, DLT_ATM_CLIP },
253 #ifdef DLT_IP_OVER_FC
254 { ipfc_if_print, DLT_IP_OVER_FC },
256 { null_if_print, DLT_NULL },
258 { null_if_print, DLT_LOOP },
260 #ifdef DLT_APPLE_IP_OVER_IEEE1394
261 { ap1394_if_print, DLT_APPLE_IP_OVER_IEEE1394 },
263 #if defined(DLT_BLUETOOTH_HCI_H4_WITH_PHDR) && defined(HAVE_PCAP_BLUETOOTH_H)
264 { bt_if_print, DLT_BLUETOOTH_HCI_H4_WITH_PHDR},
267 { lane_if_print, DLT_LANE8023 },
269 { arcnet_if_print, DLT_ARCNET },
270 #ifdef DLT_ARCNET_LINUX
271 { arcnet_linux_if_print, DLT_ARCNET_LINUX },
273 { raw_if_print, DLT_RAW },
275 { raw_if_print, DLT_IPV4 },
278 { raw_if_print, DLT_IPV6 },
280 #ifdef HAVE_PCAP_USB_H
282 { usb_linux_48_byte_print, DLT_USB_LINUX},
283 #endif /* DLT_USB_LINUX */
284 #ifdef DLT_USB_LINUX_MMAPPED
285 { usb_linux_64_byte_print, DLT_USB_LINUX_MMAPPED},
286 #endif /* DLT_USB_LINUX_MMAPPED */
287 #endif /* HAVE_PCAP_USB_H */
288 #ifdef DLT_SYMANTEC_FIREWALL
289 { symantec_if_print, DLT_SYMANTEC_FIREWALL },
292 { chdlc_if_print, DLT_C_HDLC },
295 { chdlc_if_print, DLT_HDLC },
298 { pppoe_if_print, DLT_PPP_ETHER },
300 #if defined(DLT_PFLOG) && defined(HAVE_NET_PFVAR_H)
301 { pflog_if_print, DLT_PFLOG },
303 { token_if_print, DLT_IEEE802 },
304 { fddi_if_print, DLT_FDDI },
306 { sll_if_print, DLT_LINUX_SLL },
309 { fr_if_print, DLT_FR },
312 { fr_if_print, DLT_FRELAY },
315 { mfr_if_print, DLT_MFR },
317 { atm_if_print, DLT_ATM_RFC1483 },
319 { sunatm_if_print, DLT_SUNATM },
322 { enc_if_print, DLT_ENC },
324 { sl_if_print, DLT_SLIP },
325 #ifdef DLT_SLIP_BSDOS
326 { sl_bsdos_if_print, DLT_SLIP_BSDOS },
329 { ltalk_if_print, DLT_LTALK },
331 #ifdef DLT_JUNIPER_ATM1
332 { juniper_atm1_print, DLT_JUNIPER_ATM1 },
334 #ifdef DLT_JUNIPER_ATM2
335 { juniper_atm2_print, DLT_JUNIPER_ATM2 },
337 #ifdef DLT_JUNIPER_MFR
338 { juniper_mfr_print, DLT_JUNIPER_MFR },
340 #ifdef DLT_JUNIPER_MLFR
341 { juniper_mlfr_print, DLT_JUNIPER_MLFR },
343 #ifdef DLT_JUNIPER_MLPPP
344 { juniper_mlppp_print, DLT_JUNIPER_MLPPP },
346 #ifdef DLT_JUNIPER_PPPOE
347 { juniper_pppoe_print, DLT_JUNIPER_PPPOE },
349 #ifdef DLT_JUNIPER_PPPOE_ATM
350 { juniper_pppoe_atm_print, DLT_JUNIPER_PPPOE_ATM },
352 #ifdef DLT_JUNIPER_GGSN
353 { juniper_ggsn_print, DLT_JUNIPER_GGSN },
355 #ifdef DLT_JUNIPER_ES
356 { juniper_es_print, DLT_JUNIPER_ES },
358 #ifdef DLT_JUNIPER_MONITOR
359 { juniper_monitor_print, DLT_JUNIPER_MONITOR },
361 #ifdef DLT_JUNIPER_SERVICES
362 { juniper_services_print, DLT_JUNIPER_SERVICES },
364 #ifdef DLT_JUNIPER_ETHER
365 { juniper_ether_print, DLT_JUNIPER_ETHER },
367 #ifdef DLT_JUNIPER_PPP
368 { juniper_ppp_print, DLT_JUNIPER_PPP },
370 #ifdef DLT_JUNIPER_FRELAY
371 { juniper_frelay_print, DLT_JUNIPER_FRELAY },
373 #ifdef DLT_JUNIPER_CHDLC
374 { juniper_chdlc_print, DLT_JUNIPER_CHDLC },
377 { pktap_if_print, DLT_PKTAP },
379 #ifdef DLT_IEEE802_11_RADIO
380 { ieee802_11_radio_if_print, DLT_IEEE802_11_RADIO },
382 #ifdef DLT_IEEE802_11
383 { ieee802_11_if_print, DLT_IEEE802_11},
385 #ifdef DLT_IEEE802_11_RADIO_AVS
386 { ieee802_11_radio_avs_if_print, DLT_IEEE802_11_RADIO_AVS },
388 #ifdef DLT_PRISM_HEADER
389 { prism_if_print, DLT_PRISM_HEADER },
391 { ppp_if_print, DLT_PPP },
392 #ifdef DLT_PPP_WITHDIRECTION
393 { ppp_if_print, DLT_PPP_WITHDIRECTION },
396 { ppp_bsdos_if_print, DLT_PPP_BSDOS },
398 #ifdef DLT_PPP_SERIAL
399 { ppp_hdlc_if_print, DLT_PPP_SERIAL },
404 static const struct tok status_flags[] = {
406 { PCAP_IF_UP, "Up" },
408 #ifdef PCAP_IF_RUNNING
409 { PCAP_IF_RUNNING, "Running" },
411 { PCAP_IF_LOOPBACK, "Loopback" },
416 lookup_printer(int type)
418 const struct printer *p;
420 for (p = printers; p->f; ++p)
429 lookup_ndo_printer(int type)
431 const struct ndo_printer *p;
433 for (p = ndo_printers; p->f; ++p)
437 #if defined(DLT_USER2) && defined(DLT_PKTAP)
439 * Apple incorrectly chose to use DLT_USER2 for their PKTAP
442 * We map DLT_PKTAP, whether it's DLT_USER2 as it is on Darwin-
443 * based OSes or the same value as LINKTYPE_PKTAP as it is on
444 * other OSes, to LINKTYPE_PKTAP, so files written with
445 * this version of libpcap for a DLT_PKTAP capture have a link-
446 * layer header type of LINKTYPE_PKTAP.
448 * However, files written on OS X Mavericks for a DLT_PKTAP
449 * capture have a link-layer header type of LINKTYPE_USER2.
450 * If we don't have a printer for DLT_USER2, and type is
451 * DLT_USER2, we look up the printer for DLT_PKTAP and use
454 if (type == DLT_USER2) {
455 for (p = ndo_printers; p->f; ++p)
456 if (DLT_PKTAP == p->type)
467 static int supports_monitor_mode;
474 netdissect_options *ndo;
477 if_ndo_printer ndo_printer;
484 char *CurrentFileName;
492 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
494 show_tstamp_types_and_exit(const char *device, pcap_t *pd)
497 int *tstamp_types = 0;
498 const char *tstamp_type_name;
501 n_tstamp_types = pcap_list_tstamp_types(pd, &tstamp_types);
502 if (n_tstamp_types < 0)
503 error("%s", pcap_geterr(pd));
505 if (n_tstamp_types == 0) {
506 fprintf(stderr, "Time stamp type cannot be set for %s\n",
510 fprintf(stderr, "Time stamp types for %s (use option -j to set):\n",
512 for (i = 0; i < n_tstamp_types; i++) {
513 tstamp_type_name = pcap_tstamp_type_val_to_name(tstamp_types[i]);
514 if (tstamp_type_name != NULL) {
515 (void) fprintf(stderr, " %s (%s)\n", tstamp_type_name,
516 pcap_tstamp_type_val_to_description(tstamp_types[i]));
518 (void) fprintf(stderr, " %d\n", tstamp_types[i]);
521 pcap_free_tstamp_types(tstamp_types);
527 show_dlts_and_exit(const char *device, pcap_t *pd)
531 const char *dlt_name;
533 n_dlts = pcap_list_datalinks(pd, &dlts);
535 error("%s", pcap_geterr(pd));
536 else if (n_dlts == 0 || !dlts)
537 error("No data link types.");
540 * If the interface is known to support monitor mode, indicate
541 * whether these are the data link types available when not in
542 * monitor mode, if -I wasn't specified, or when in monitor mode,
543 * when -I was specified (the link-layer types available in
544 * monitor mode might be different from the ones available when
545 * not in monitor mode).
547 if (supports_monitor_mode)
548 (void) fprintf(stderr, "Data link types for %s %s (use option -y to set):\n",
550 Iflag ? "when in monitor mode" : "when not in monitor mode");
552 (void) fprintf(stderr, "Data link types for %s (use option -y to set):\n",
555 while (--n_dlts >= 0) {
556 dlt_name = pcap_datalink_val_to_name(dlts[n_dlts]);
557 if (dlt_name != NULL) {
558 (void) fprintf(stderr, " %s (%s)", dlt_name,
559 pcap_datalink_val_to_description(dlts[n_dlts]));
562 * OK, does tcpdump handle that type?
564 if (lookup_printer(dlts[n_dlts]) == NULL
565 && lookup_ndo_printer(dlts[n_dlts]) == NULL)
566 (void) fprintf(stderr, " (printing not supported)");
567 fprintf(stderr, "\n");
569 (void) fprintf(stderr, " DLT %d (printing not supported)\n",
573 #ifdef HAVE_PCAP_FREE_DATALINKS
574 pcap_free_datalinks(dlts);
579 #ifdef HAVE_PCAP_FINDALLDEVS
581 show_devices_and_exit (void)
583 pcap_if_t *devpointer;
584 char ebuf[PCAP_ERRBUF_SIZE];
587 if (pcap_findalldevs(&devpointer, ebuf) < 0)
590 for (i = 0; devpointer != NULL; i++) {
591 printf("%d.%s", i+1, devpointer->name);
592 if (devpointer->description != NULL)
593 printf(" (%s)", devpointer->description);
594 if (devpointer->flags != 0)
595 printf(" [%s]", bittok2str(status_flags, "none", devpointer->flags));
597 devpointer = devpointer->next;
602 #endif /* HAVE_PCAP_FINDALLDEVS */
607 * Note that there we use all letters for short options except for g, k,
608 * o, and P, and those are used by other versions of tcpdump, and we should
609 * only use them for the same purposes that the other versions of tcpdump
612 * OS X tcpdump uses -g to force non--v output for IP to be on one
613 * line, making it more "g"repable;
615 * OS X tcpdump uses -k tospecify that packet comments in pcap-ng files
618 * OpenBSD tcpdump uses -o to indicate that OS fingerprinting should be done
619 * for hosts sending TCP SYN packets;
621 * OS X tcpdump uses -P to indicate that -w should write pcap-ng rather
624 * OS X tcpdump also uses -Q to specify expressions that match packet
625 * metadata, including but not limited to the packet direction.
626 * The expression syntax is different from a simple "in|out|inout",
627 * and those expressions aren't accepted by OS X tcpdump, but the
628 * equivalents would be "in" = "dir=in", "out" = "dir=out", and
629 * "inout" = "dir=in or dir=out", and the parser could conceivably
630 * special-case "in", "out", and "inout" as expressions for backwards
631 * compatibility, so all is not (yet) lost.
635 * Set up flags that might or might not be supported depending on the
636 * version of libpcap we're using.
638 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
640 #define B_FLAG_USAGE " [ -B size ]"
641 #else /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
644 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
646 #ifdef HAVE_PCAP_CREATE
648 #else /* HAVE_PCAP_CREATE */
650 #endif /* HAVE_PCAP_CREATE */
652 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
654 #define j_FLAG_USAGE " [ -j tstamptype ]"
656 #else /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
660 #endif /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
662 #ifdef HAVE_PCAP_FINDALLDEVS
668 #ifdef HAVE_PCAP_DUMP_FLUSH
674 #ifdef HAVE_PCAP_SETDIRECTION
680 #define SHORTOPTS "aAb" B_FLAG "c:C:d" D_FLAG "eE:fF:G:hHi:" I_FLAG j_FLAG J_FLAG "KlLm:M:nNOpq" Q_FLAG "r:Rs:StT:u" U_FLAG "vV:w:W:xXy:Yz:Z:#"
685 * We do not currently have long options corresponding to all short
686 * options; we should probably pick appropriate option names for them.
688 * However, the short options where the number of times the option is
689 * specified matters, such as -v and -d and -t, should probably not
690 * just map to a long option, as saying
692 * tcpdump --verbose --verbose
694 * doesn't make sense; it should be --verbosity={N} or something such
697 * For long options with no corresponding short options, we define values
698 * outside the range of ASCII graphic characters, make that the last
699 * component of the entry for the long option, and have a case for that
700 * option in the switch statement.
702 #define OPTION_VERSION 128
703 #define OPTION_TSTAMP_PRECISION 129
704 #define OPTION_IMMEDIATE_MODE 130
706 static const struct option longopts[] = {
707 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
708 { "buffer-size", required_argument, NULL, 'B' },
710 { "list-interfaces", no_argument, NULL, 'D' },
711 { "help", no_argument, NULL, 'h' },
712 { "interface", required_argument, NULL, 'i' },
713 #ifdef HAVE_PCAP_CREATE
714 { "monitor-mode", no_argument, NULL, 'I' },
716 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
717 { "time-stamp-type", required_argument, NULL, 'j' },
718 { "list-time-stamp-types", no_argument, NULL, 'J' },
720 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
721 { "time-stamp-precision", required_argument, NULL, OPTION_TSTAMP_PRECISION},
723 { "dont-verify-checksums", no_argument, NULL, 'K' },
724 { "list-data-link-types", no_argument, NULL, 'L' },
725 { "no-optimize", no_argument, NULL, 'O' },
726 { "no-promiscuous-mode", no_argument, NULL, 'p' },
727 #ifdef HAVE_PCAP_SETDIRECTION
728 { "direction", required_argument, NULL, 'Q' },
730 { "snapshot-length", required_argument, NULL, 's' },
731 { "absolute-tcp-sequence-numbers", no_argument, NULL, 'S' },
732 #ifdef HAVE_PCAP_DUMP_FLUSH
733 { "packet-buffered", no_argument, NULL, 'U' },
735 { "linktype", required_argument, NULL, 'y' },
736 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
737 { "immediate-mode", no_argument, NULL, OPTION_IMMEDIATE_MODE },
739 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
740 { "debug-filter-parser", no_argument, NULL, 'Y' },
742 { "relinquish-privileges", required_argument, NULL, 'Z' },
743 { "number", no_argument, NULL, '#' },
744 { "version", no_argument, NULL, OPTION_VERSION },
749 /* Drop root privileges and chroot if necessary */
751 droproot(const char *username, const char *chroot_dir)
753 struct passwd *pw = NULL;
755 if (chroot_dir && !username) {
756 fprintf(stderr, "tcpdump: Chroot without dropping root is insecure\n");
760 pw = getpwnam(username);
763 if (chroot(chroot_dir) != 0 || chdir ("/") != 0) {
764 fprintf(stderr, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
765 chroot_dir, pcap_strerror(errno));
769 #ifdef HAVE_LIBCAP_NG
770 int ret = capng_change_id(pw->pw_uid, pw->pw_gid, CAPNG_NO_FLAG);
772 fprintf(stderr, "error : ret %d\n", ret);
775 fprintf(stderr, "dropped privs to %s\n", username);
778 if (initgroups(pw->pw_name, pw->pw_gid) != 0 ||
779 setgid(pw->pw_gid) != 0 || setuid(pw->pw_uid) != 0) {
780 fprintf(stderr, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
782 (unsigned long)pw->pw_uid,
783 (unsigned long)pw->pw_gid,
784 pcap_strerror(errno));
788 fprintf(stderr, "dropped privs to %s\n", username);
790 #endif /* HAVE_LIBCAP_NG */
793 fprintf(stderr, "tcpdump: Couldn't find user '%.32s'\n",
797 #ifdef HAVE_LIBCAP_NG
798 /* We don't need CAP_SETUID and CAP_SETGID any more. */
801 CAPNG_EFFECTIVE | CAPNG_PERMITTED,
805 capng_apply(CAPNG_SELECT_BOTH);
806 #endif /* HAVE_LIBCAP_NG */
827 MakeFilename(char *buffer, char *orig_name, int cnt, int max_chars)
829 char *filename = malloc(PATH_MAX + 1);
830 if (filename == NULL)
831 error("Makefilename: malloc");
833 /* Process with strftime if Gflag is set. */
837 /* Convert Gflag_time to a usable format */
838 if ((local_tm = localtime(&Gflag_time)) == NULL) {
839 error("MakeTimedFilename: localtime");
842 /* There's no good way to detect an error in strftime since a return
843 * value of 0 isn't necessarily failure.
845 strftime(filename, PATH_MAX, orig_name, local_tm);
847 strncpy(filename, orig_name, PATH_MAX);
850 if (cnt == 0 && max_chars == 0)
851 strncpy(buffer, filename, PATH_MAX + 1);
853 if (snprintf(buffer, PATH_MAX + 1, "%s%0*d", filename, max_chars, cnt) > PATH_MAX)
854 /* Report an error if the filename is too large */
855 error("too many output files or filename is too long (> %d)", PATH_MAX);
859 static int tcpdump_printf(netdissect_options *ndo _U_,
860 const char *fmt, ...)
867 ret=vfprintf(stdout, fmt, args);
873 static struct print_info
874 get_print_info(int type)
876 struct print_info printinfo;
878 printinfo.ndo_type = 1;
879 printinfo.ndo = gndo;
880 printinfo.p.ndo_printer = lookup_ndo_printer(type);
881 if (printinfo.p.ndo_printer == NULL) {
882 printinfo.p.printer = lookup_printer(type);
883 printinfo.ndo_type = 0;
884 if (printinfo.p.printer == NULL) {
885 gndo->ndo_dltname = pcap_datalink_val_to_name(type);
886 if (gndo->ndo_dltname != NULL)
887 error("packet printing is not supported for link type %s: use -w",
890 error("packet printing is not supported for link type %d: use -w", type);
897 get_next_file(FILE *VFile, char *ptr)
901 ret = fgets(ptr, PATH_MAX, VFile);
905 if (ptr[strlen(ptr) - 1] == '\n')
906 ptr[strlen(ptr) - 1] = '\0';
912 static cap_channel_t *
915 cap_channel_t *capcas, *capdnsloc;
916 const char *types[1];
921 error("unable to create casper process");
922 capdnsloc = cap_service_open(capcas, "system.dns");
923 /* Casper capability no longer needed. */
925 if (capdnsloc == NULL)
926 error("unable to open system.dns service");
927 /* Limit system.dns to reverse DNS lookups. */
929 if (cap_dns_type_limit(capdnsloc, types, 1) < 0)
930 error("unable to limit access to system.dns service");
931 families[0] = AF_INET;
932 families[1] = AF_INET6;
933 if (cap_dns_family_limit(capdnsloc, families, 2) < 0)
934 error("unable to limit access to system.dns service");
938 #endif /* HAVE_CAPSPER */
940 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
942 tstamp_precision_from_string(const char *precision)
944 if (strncmp(precision, "nano", strlen("nano")) == 0)
945 return PCAP_TSTAMP_PRECISION_NANO;
947 if (strncmp(precision, "micro", strlen("micro")) == 0)
948 return PCAP_TSTAMP_PRECISION_MICRO;
954 tstamp_precision_to_string(int precision)
958 case PCAP_TSTAMP_PRECISION_MICRO:
961 case PCAP_TSTAMP_PRECISION_NANO:
972 * Ensure that, on a dump file's descriptor, we have all the rights
973 * necessary to make the standard I/O library work with an fdopen()ed
974 * FILE * from that descriptor.
976 * A long time ago, in a galaxy far far away, AT&T decided that, instead
977 * of providing separate APIs for getting and setting the FD_ flags on a
978 * descriptor, getting and setting the O_ flags on a descriptor, and
979 * locking files, they'd throw them all into a kitchen-sink fcntl() call
980 * along the lines of ioctl(), the fact that ioctl() operations are
981 * largely specific to particular character devices but fcntl() operations
982 * are either generic to all descriptors or generic to all descriptors for
983 * regular files nonwithstanding.
985 * The Capsicum people decided that fine-grained control of descriptor
986 * operations was required, so that you need to grant permission for
987 * reading, writing, seeking, and fcntl-ing. The latter, courtesy of
988 * AT&T's decision, means that "fcntl-ing" isn't a thing, but a motley
989 * collection of things, so there are *individual* fcntls for which
990 * permission needs to be granted.
992 * The FreeBSD standard I/O people implemented some optimizations that
993 * requires that the standard I/O routines be able to determine whether
994 * the descriptor for the FILE * is open append-only or not; as that
995 * descriptor could have come from an open() rather than an fopen(),
996 * that requires that it be able to do an F_GETFL fcntl() to read
999 * Tcpdump uses ftell() to determine how much data has been written
1000 * to a file in order to, when used with -C, determine when it's time
1001 * to rotate capture files. ftell() therefore needs to do an lseek()
1002 * to find out the file offset and must, thanks to the aforementioned
1003 * optimization, also know whether the descriptor is open append-only
1006 * The net result of all the above is that we need to grant CAP_SEEK,
1007 * CAP_WRITE, and CAP_FCNTL with the CAP_FCNTL_GETFL subcapability.
1009 * Perhaps this is the universe's way of saying that either
1011 * 1) there needs to be an fopenat() call and a pcap_dump_openat() call
1012 * using it, so that Capsicum-capable tcpdump wouldn't need to do
1017 * 2) there needs to be a cap_fdopen() call in the FreeBSD standard
1018 * I/O library that knows what rights are needed by the standard
1019 * I/O library, based on the open mode, and assigns them, perhaps
1020 * with an additional argument indicating, for example, whether
1021 * seeking should be allowed, so that tcpdump doesn't need to know
1022 * what the standard I/O library happens to require this week.
1025 set_dumper_capsicum_rights(pcap_dumper_t *p)
1027 int fd = fileno(pcap_dump_file(p));
1028 cap_rights_t rights;
1030 cap_rights_init(&rights, CAP_SEEK, CAP_WRITE, CAP_FCNTL);
1031 if (cap_rights_limit(fd, &rights) < 0 && errno != ENOSYS) {
1032 error("unable to limit dump descriptor");
1034 if (cap_fcntls_limit(fd, CAP_FCNTL_GETFL) < 0 && errno != ENOSYS) {
1035 error("unable to limit dump descriptor fcntls");
1041 main(int argc, char **argv)
1043 register int cnt, op, i;
1044 bpf_u_int32 localnet =0 , netmask = 0;
1045 register char *cp, *infile, *cmdbuf, *device, *RFileName, *VFileName, *WFileName;
1046 pcap_handler callback;
1050 const char *dlt_name;
1051 struct bpf_program fcode;
1053 RETSIGTYPE (*oldhandler)(int);
1055 struct print_info printinfo;
1056 struct dump_info dumpinfo;
1057 u_char *pcap_userdata;
1058 char ebuf[PCAP_ERRBUF_SIZE];
1059 char VFileLine[PATH_MAX + 1];
1060 char *username = NULL;
1061 char *chroot_dir = NULL;
1064 #ifdef HAVE_PCAP_FINDALLDEVS
1065 pcap_if_t *devpointer;
1071 cap_rights_t rights;
1072 #endif /* HAVE_CAPSPER */
1076 if(wsockinit() != 0) return 1;
1079 jflag=-1; /* not set */
1083 gndo->ndo_default_print=ndo_default_print;
1084 gndo->ndo_printf=tcpdump_printf;
1085 gndo->ndo_error=ndo_error;
1086 gndo->ndo_warning=ndo_warning;
1087 gndo->ndo_snaplen = DEFAULT_SNAPLEN;
1088 gndo->ndo_immediate = 0;
1098 if ((cp = strrchr(argv[0], '/')) != NULL)
1099 program_name = cp + 1;
1101 program_name = argv[0];
1104 * On platforms where the CPU doesn't support unaligned loads,
1105 * force unaligned accesses to abort with SIGBUS, rather than
1106 * being fixed up (slowly) by the OS kernel; on those platforms,
1107 * misaligned accesses are bugs, and we want tcpdump to crash so
1108 * that the bugs are reported.
1110 if (abort_on_misalignment(ebuf, sizeof(ebuf)) < 0)
1118 (op = getopt_long(argc, argv, SHORTOPTS, longopts, NULL)) != -1)
1122 /* compatibility for old -a */
1133 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
1135 Bflag = atoi(optarg)*1024;
1137 error("invalid packet buffer size %s", optarg);
1139 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
1144 error("invalid packet count %s", optarg);
1148 Cflag = atoi(optarg) * 1000000;
1150 error("invalid file size %s", optarg);
1170 #ifndef HAVE_LIBCRYPTO
1171 warning("crypto code not compiled in");
1173 gndo->ndo_espsecret = optarg;
1185 Gflag = atoi(optarg);
1187 error("invalid number of seconds %s", optarg);
1189 /* We will create one file initially. */
1192 /* Grab the current time for rotation use. */
1193 if ((Gflag_time = time(NULL)) == (time_t)-1) {
1194 error("main: can't get current time: %s",
1195 pcap_strerror(errno));
1209 if (optarg[0] == '0' && optarg[1] == 0)
1210 error("Invalid adapter index");
1212 #ifdef HAVE_PCAP_FINDALLDEVS
1214 * If the argument is a number, treat it as
1215 * an index into the list of adapters, as
1216 * printed by "tcpdump -D".
1218 * This should be OK on UNIX systems, as interfaces
1219 * shouldn't have names that begin with digits.
1220 * It can be useful on Windows, where more than
1221 * one interface can have the same name.
1223 devnum = strtol(optarg, &end, 10);
1224 if (optarg != end && *end == '\0') {
1226 error("Invalid adapter index");
1228 if (pcap_findalldevs(&devpointer, ebuf) < 0)
1232 * Look for the devnum-th entry
1233 * in the list of devices
1237 i < devnum-1 && devpointer != NULL;
1238 i++, devpointer = devpointer->next)
1240 if (devpointer == NULL)
1241 error("Invalid adapter index");
1243 device = devpointer->name;
1246 #endif /* HAVE_PCAP_FINDALLDEVS */
1250 #ifdef HAVE_PCAP_CREATE
1254 #endif /* HAVE_PCAP_CREATE */
1256 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1258 jflag = pcap_tstamp_type_name_to_val(optarg);
1260 error("invalid time stamp type %s", optarg);
1271 * _IOLBF is the same as _IOFBF in Microsoft's C
1272 * libraries; the only alternative they offer
1275 * XXX - this should really be checking for MSVC++,
1276 * not WIN32, if, for example, MinGW has its own
1277 * C library that is more UNIX-compatible.
1279 setvbuf(stdout, NULL, _IONBF, 0);
1281 #ifdef HAVE_SETLINEBUF
1284 setvbuf(stdout, NULL, _IOLBF, 0);
1295 if (smiLoadModule(optarg) == 0) {
1296 error("could not load MIB module %s", optarg);
1300 (void)fprintf(stderr, "%s: ignoring option `-m %s' ",
1301 program_name, optarg);
1302 (void)fprintf(stderr, "(no libsmi support)\n");
1307 /* TCP-MD5 shared secret */
1308 #ifndef HAVE_LIBCRYPTO
1309 warning("crypto code not compiled in");
1332 ++suppress_default_print;
1335 #ifdef HAVE_PCAP_SETDIRECTION
1337 if (strcasecmp(optarg, "in") == 0)
1339 else if (strcasecmp(optarg, "out") == 0)
1341 else if (strcasecmp(optarg, "inout") == 0)
1342 Qflag = PCAP_D_INOUT;
1344 error("unknown capture direction `%s'", optarg);
1346 #endif /* HAVE_PCAP_SETDIRECTION */
1357 snaplen = strtol(optarg, &end, 0);
1358 if (optarg == end || *end != '\0'
1359 || snaplen < 0 || snaplen > MAXIMUM_SNAPLEN)
1360 error("invalid snaplen %s", optarg);
1361 else if (snaplen == 0)
1362 snaplen = MAXIMUM_SNAPLEN;
1374 if (strcasecmp(optarg, "vat") == 0)
1375 packettype = PT_VAT;
1376 else if (strcasecmp(optarg, "wb") == 0)
1378 else if (strcasecmp(optarg, "rpc") == 0)
1379 packettype = PT_RPC;
1380 else if (strcasecmp(optarg, "rtp") == 0)
1381 packettype = PT_RTP;
1382 else if (strcasecmp(optarg, "rtcp") == 0)
1383 packettype = PT_RTCP;
1384 else if (strcasecmp(optarg, "snmp") == 0)
1385 packettype = PT_SNMP;
1386 else if (strcasecmp(optarg, "cnfp") == 0)
1387 packettype = PT_CNFP;
1388 else if (strcasecmp(optarg, "tftp") == 0)
1389 packettype = PT_TFTP;
1390 else if (strcasecmp(optarg, "aodv") == 0)
1391 packettype = PT_AODV;
1392 else if (strcasecmp(optarg, "carp") == 0)
1393 packettype = PT_CARP;
1394 else if (strcasecmp(optarg, "radius") == 0)
1395 packettype = PT_RADIUS;
1396 else if (strcasecmp(optarg, "zmtp1") == 0)
1397 packettype = PT_ZMTP1;
1398 else if (strcasecmp(optarg, "vxlan") == 0)
1399 packettype = PT_VXLAN;
1400 else if (strcasecmp(optarg, "pgm") == 0)
1401 packettype = PT_PGM;
1402 else if (strcasecmp(optarg, "pgm_zmtp1") == 0)
1403 packettype = PT_PGM_ZMTP1;
1404 else if (strcasecmp(optarg, "lmp") == 0)
1405 packettype = PT_LMP;
1407 error("unknown packet type `%s'", optarg);
1414 #ifdef HAVE_PCAP_DUMP_FLUSH
1433 Wflag = atoi(optarg);
1435 error("invalid number of output files %s", optarg);
1436 WflagChars = getWflagChars(Wflag);
1441 ++suppress_default_print;
1446 ++suppress_default_print;
1450 gndo->ndo_dltname = optarg;
1452 pcap_datalink_name_to_val(gndo->ndo_dltname);
1453 if (gndo->ndo_dlt < 0)
1454 error("invalid data link type %s", gndo->ndo_dltname);
1457 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
1460 /* Undocumented flag */
1461 #ifdef HAVE_PCAP_DEBUG
1462 extern int pcap_debug;
1472 zflag = strdup(optarg);
1476 username = strdup(optarg);
1480 gndo->ndo_packet_number = 1;
1483 case OPTION_VERSION:
1488 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1489 case OPTION_TSTAMP_PRECISION:
1490 gndo->ndo_tstamp_precision = tstamp_precision_from_string(optarg);
1491 if (gndo->ndo_tstamp_precision < 0)
1492 error("unsupported time stamp precision");
1496 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1497 case OPTION_IMMEDIATE_MODE:
1498 gndo->ndo_immediate = 1;
1508 #ifdef HAVE_PCAP_FINDALLDEVS
1510 show_devices_and_exit();
1515 case 0: /* Default */
1516 case 4: /* Default + Date*/
1517 thiszone = gmt2local(0);
1520 case 1: /* No time stamp */
1521 case 2: /* Unix timeval style */
1522 case 3: /* Microseconds since previous packet */
1523 case 5: /* Microseconds since first packet */
1526 default: /* Not supported */
1527 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
1531 if (fflag != 0 && (VFileName != NULL || RFileName != NULL))
1532 error("-f can not be used with -V or -r");
1534 if (VFileName != NULL && RFileName != NULL)
1535 error("-V and -r are mutually exclusive.");
1537 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1539 * If we're printing dissected packets to the standard output
1540 * rather than saving raw packets to a file, and the standard
1541 * output is a terminal, use immediate mode, as the user's
1542 * probably expecting to see packets pop up immediately.
1544 if (WFileName == NULL && isatty(1))
1545 gndo->ndo_immediate = 1;
1549 /* if run as root, prepare for chrooting */
1550 if (getuid() == 0 || geteuid() == 0) {
1551 /* future extensibility for cmd-line arguments */
1553 chroot_dir = WITH_CHROOT;
1558 /* if run as root, prepare for dropping root privileges */
1559 if (getuid() == 0 || geteuid() == 0) {
1560 /* Run with '-Z root' to restore old behaviour */
1562 username = WITH_USER;
1566 if (RFileName != NULL || VFileName != NULL) {
1568 * If RFileName is non-null, it's the pathname of a
1569 * savefile to read. If VFileName is non-null, it's
1570 * the pathname of a file containing a list of pathnames
1571 * (one per line) of savefiles to read.
1573 * In either case, we're reading a savefile, not doing
1578 * We don't need network access, so relinquish any set-UID
1579 * or set-GID privileges we have (if any).
1581 * We do *not* want set-UID privileges when opening a
1582 * trace file, as that might let the user read other
1583 * people's trace files (especially if we're set-UID
1586 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
1587 fprintf(stderr, "Warning: setgid/setuid failed !\n");
1589 if (VFileName != NULL) {
1590 if (VFileName[0] == '-' && VFileName[1] == '\0')
1593 VFile = fopen(VFileName, "r");
1596 error("Unable to open file: %s\n", strerror(errno));
1598 ret = get_next_file(VFile, VFileLine);
1600 error("Nothing in %s\n", VFileName);
1601 RFileName = VFileLine;
1604 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1605 pd = pcap_open_offline_with_tstamp_precision(RFileName,
1606 gndo->ndo_tstamp_precision, ebuf);
1608 pd = pcap_open_offline(RFileName, ebuf);
1614 cap_rights_init(&rights, CAP_READ);
1615 if (cap_rights_limit(fileno(pcap_file(pd)), &rights) < 0 &&
1617 error("unable to limit pcap descriptor");
1620 dlt = pcap_datalink(pd);
1621 dlt_name = pcap_datalink_val_to_name(dlt);
1622 if (dlt_name == NULL) {
1623 fprintf(stderr, "reading from file %s, link-type %u\n",
1627 "reading from file %s, link-type %s (%s)\n",
1628 RFileName, dlt_name,
1629 pcap_datalink_val_to_description(dlt));
1633 * We're doing a live capture.
1635 if (device == NULL) {
1636 device = pcap_lookupdev(ebuf);
1642 * Print a message to the standard error on Windows.
1643 * XXX - why do it here, with a different message?
1645 if(strlen(device) == 1) /* we assume that an ASCII string is always longer than 1 char */
1646 { /* a Unicode string has a \0 as second byte (so strlen() is 1) */
1647 fprintf(stderr, "%s: listening on %ws\n", program_name, device);
1651 fprintf(stderr, "%s: listening on %s\n", program_name, device);
1656 #ifdef HAVE_PCAP_CREATE
1657 pd = pcap_create(device, ebuf);
1660 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1662 show_tstamp_types_and_exit(device, pd);
1664 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1665 status = pcap_set_tstamp_precision(pd, gndo->ndo_tstamp_precision);
1667 error("%s: Can't set %ssecond time stamp precision: %s",
1669 tstamp_precision_to_string(gndo->ndo_tstamp_precision),
1670 pcap_statustostr(status));
1673 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1674 if (gndo->ndo_immediate) {
1675 status = pcap_set_immediate_mode(pd, 1);
1677 error("%s: Can't set immediate mode: %s",
1679 pcap_statustostr(status));
1683 * Is this an interface that supports monitor mode?
1685 if (pcap_can_set_rfmon(pd) == 1)
1686 supports_monitor_mode = 1;
1688 supports_monitor_mode = 0;
1689 status = pcap_set_snaplen(pd, snaplen);
1691 error("%s: Can't set snapshot length: %s",
1692 device, pcap_statustostr(status));
1693 status = pcap_set_promisc(pd, !pflag);
1695 error("%s: Can't set promiscuous mode: %s",
1696 device, pcap_statustostr(status));
1698 status = pcap_set_rfmon(pd, 1);
1700 error("%s: Can't set monitor mode: %s",
1701 device, pcap_statustostr(status));
1703 status = pcap_set_timeout(pd, 1000);
1705 error("%s: pcap_set_timeout failed: %s",
1706 device, pcap_statustostr(status));
1708 status = pcap_set_buffer_size(pd, Bflag);
1710 error("%s: Can't set buffer size: %s",
1711 device, pcap_statustostr(status));
1713 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1715 status = pcap_set_tstamp_type(pd, jflag);
1717 error("%s: Can't set time stamp type: %s",
1718 device, pcap_statustostr(status));
1721 status = pcap_activate(pd);
1724 * pcap_activate() failed.
1726 cp = pcap_geterr(pd);
1727 if (status == PCAP_ERROR)
1729 else if ((status == PCAP_ERROR_NO_SUCH_DEVICE ||
1730 status == PCAP_ERROR_PERM_DENIED) &&
1732 error("%s: %s\n(%s)", device,
1733 pcap_statustostr(status), cp);
1735 else if (status == PCAP_ERROR_RFMON_NOTSUP &&
1736 strncmp(device, "wlan", 4) == 0) {
1737 char parent[8], newdev[8];
1739 size_t s = sizeof(parent);
1741 snprintf(sysctl, sizeof(sysctl),
1742 "net.wlan.%d.%%parent", atoi(device + 4));
1743 sysctlbyname(sysctl, parent, &s, NULL, 0);
1744 strlcpy(newdev, device, sizeof(newdev));
1745 /* Suggest a new wlan device. */
1746 newdev[strlen(newdev)-1]++;
1747 error("%s is not a monitor mode VAP\n"
1748 "To create a new monitor mode VAP use:\n"
1749 " ifconfig %s create wlandev %s wlanmode "
1750 "monitor\nand use %s as the tcpdump "
1751 "interface", device, newdev, parent,
1756 error("%s: %s", device,
1757 pcap_statustostr(status));
1758 } else if (status > 0) {
1760 * pcap_activate() succeeded, but it's warning us
1761 * of a problem it had.
1763 cp = pcap_geterr(pd);
1764 if (status == PCAP_WARNING)
1766 else if (status == PCAP_WARNING_PROMISC_NOTSUP &&
1768 warning("%s: %s\n(%s)", device,
1769 pcap_statustostr(status), cp);
1771 warning("%s: %s", device,
1772 pcap_statustostr(status));
1774 #ifdef HAVE_PCAP_SETDIRECTION
1776 status = pcap_setdirection(pd, Qflag);
1778 error("%s: pcap_setdirection() failed: %s",
1779 device, pcap_geterr(pd));
1781 #endif /* HAVE_PCAP_SETDIRECTION */
1784 pd = pcap_open_live(device, snaplen, !pflag, 1000, ebuf);
1788 warning("%s", ebuf);
1789 #endif /* HAVE_PCAP_CREATE */
1791 * Let user own process after socket has been opened.
1794 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
1795 fprintf(stderr, "Warning: setgid/setuid failed !\n");
1797 #if !defined(HAVE_PCAP_CREATE) && defined(WIN32)
1799 if(pcap_setbuff(pd, Bflag)==-1){
1800 error("%s", pcap_geterr(pd));
1802 #endif /* !defined(HAVE_PCAP_CREATE) && defined(WIN32) */
1804 show_dlts_and_exit(device, pd);
1805 if (gndo->ndo_dlt >= 0) {
1806 #ifdef HAVE_PCAP_SET_DATALINK
1807 if (pcap_set_datalink(pd, gndo->ndo_dlt) < 0)
1808 error("%s", pcap_geterr(pd));
1811 * We don't actually support changing the
1812 * data link type, so we only let them
1813 * set it to what it already is.
1815 if (gndo->ndo_dlt != pcap_datalink(pd)) {
1816 error("%s is not one of the DLTs supported by this device\n",
1820 (void)fprintf(stderr, "%s: data link type %s\n",
1821 program_name, gndo->ndo_dltname);
1822 (void)fflush(stderr);
1824 i = pcap_snapshot(pd);
1826 warning("snaplen raised from %d to %d", snaplen, i);
1830 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
1831 warning("foreign (-f) flag used but: %s", ebuf);
1837 cmdbuf = read_infile(infile);
1839 cmdbuf = copy_argv(&argv[optind]);
1841 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
1842 error("%s", pcap_geterr(pd));
1844 bpf_dump(&fcode, dflag);
1852 capdns = capdns_setup();
1853 #endif /* HAVE_CAPSPER */
1855 init_addrtoname(gndo, localnet, netmask);
1859 (void)setsignal(SIGPIPE, cleanup);
1860 (void)setsignal(SIGTERM, cleanup);
1861 (void)setsignal(SIGINT, cleanup);
1863 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1864 (void)setsignal(SIGCHLD, child_cleanup);
1866 /* Cooperate with nohup(1) */
1868 if ((oldhandler = setsignal(SIGHUP, cleanup)) != SIG_DFL)
1869 (void)setsignal(SIGHUP, oldhandler);
1874 * If a user name was specified with "-Z", attempt to switch to
1875 * that user's UID. This would probably be used with sudo,
1876 * to allow tcpdump to be run in a special restricted
1877 * account (if you just want to allow users to open capture
1878 * devices, and can't just give users that permission,
1879 * you'd make tcpdump set-UID or set-GID).
1881 * Tcpdump doesn't necessarily write only to one savefile;
1882 * the general only way to allow a -Z instance to write to
1883 * savefiles as the user under whose UID it's run, rather
1884 * than as the user specified with -Z, would thus be to switch
1885 * to the original user ID before opening a capture file and
1886 * then switch back to the -Z user ID after opening the savefile.
1887 * Switching to the -Z user ID only after opening the first
1888 * savefile doesn't handle the general case.
1891 if (getuid() == 0 || geteuid() == 0) {
1892 #ifdef HAVE_LIBCAP_NG
1893 /* Initialize capng */
1894 capng_clear(CAPNG_SELECT_BOTH);
1898 CAPNG_PERMITTED | CAPNG_EFFECTIVE,
1907 CAPNG_PERMITTED | CAPNG_EFFECTIVE,
1911 capng_apply(CAPNG_SELECT_BOTH);
1912 #endif /* HAVE_LIBCAP_NG */
1913 if (username || chroot_dir)
1914 droproot(username, chroot_dir);
1919 if (pcap_setfilter(pd, &fcode) < 0)
1920 error("%s", pcap_geterr(pd));
1922 if (RFileName == NULL && VFileName == NULL) {
1923 static const unsigned long cmds[] = { BIOCGSTATS };
1926 * The various libpcap devices use a combination of
1927 * read (bpf), ioctl (bpf, netmap), poll (netmap).
1928 * Grant the relevant access rights, sorted by name.
1930 cap_rights_init(&rights, CAP_EVENT, CAP_IOCTL, CAP_READ);
1931 if (cap_rights_limit(pcap_fileno(pd), &rights) < 0 &&
1933 error("unable to limit pcap descriptor");
1935 if (cap_ioctls_limit(pcap_fileno(pd), cmds,
1936 sizeof(cmds) / sizeof(cmds[0])) < 0 && errno != ENOSYS) {
1937 error("unable to limit ioctls on pcap descriptor");
1943 /* Do not exceed the default PATH_MAX for files. */
1944 dumpinfo.CurrentFileName = (char *)malloc(PATH_MAX + 1);
1946 if (dumpinfo.CurrentFileName == NULL)
1947 error("malloc of dumpinfo.CurrentFileName");
1949 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1951 MakeFilename(dumpinfo.CurrentFileName, WFileName, 0, WflagChars);
1953 MakeFilename(dumpinfo.CurrentFileName, WFileName, 0, 0);
1955 p = pcap_dump_open(pd, dumpinfo.CurrentFileName);
1956 #ifdef HAVE_LIBCAP_NG
1957 /* Give up CAP_DAC_OVERRIDE capability.
1958 * Only allow it to be restored if the -C or -G flag have been
1959 * set since we may need to create more files later on.
1963 (Cflag || Gflag ? 0 : CAPNG_PERMITTED)
1967 capng_apply(CAPNG_SELECT_BOTH);
1968 #endif /* HAVE_LIBCAP_NG */
1970 error("%s", pcap_geterr(pd));
1972 set_dumper_capsicum_rights(p);
1974 if (Cflag != 0 || Gflag != 0) {
1976 dumpinfo.WFileName = strdup(basename(WFileName));
1977 dumpinfo.dirfd = open(dirname(WFileName),
1978 O_DIRECTORY | O_RDONLY);
1979 if (dumpinfo.dirfd < 0) {
1980 error("unable to open directory %s",
1981 dirname(WFileName));
1983 cap_rights_init(&rights, CAP_CREATE, CAP_FCNTL,
1984 CAP_FTRUNCATE, CAP_LOOKUP, CAP_SEEK, CAP_WRITE);
1985 if (cap_rights_limit(dumpinfo.dirfd, &rights) < 0 &&
1987 error("unable to limit directory rights");
1989 if (cap_fcntls_limit(dumpinfo.dirfd, CAP_FCNTL_GETFL) < 0 &&
1991 error("unable to limit dump descriptor fcntls");
1993 #else /* !HAVE_CAPSPER */
1994 dumpinfo.WFileName = WFileName;
1996 callback = dump_packet_and_trunc;
1999 pcap_userdata = (u_char *)&dumpinfo;
2001 callback = dump_packet;
2002 pcap_userdata = (u_char *)p;
2004 #ifdef HAVE_PCAP_DUMP_FLUSH
2009 type = pcap_datalink(pd);
2010 printinfo = get_print_info(type);
2011 callback = print_packet;
2012 pcap_userdata = (u_char *)&printinfo;
2015 #ifdef SIGNAL_REQ_INFO
2017 * We can't get statistics when reading from a file rather
2018 * than capturing from a device.
2020 if (RFileName == NULL)
2021 (void)setsignal(SIGNAL_REQ_INFO, requestinfo);
2024 if (vflag > 0 && WFileName) {
2026 * When capturing to a file, "-v" means tcpdump should,
2027 * every 10 secodns, "v"erbosely report the number of
2030 #ifdef USE_WIN32_MM_TIMER
2031 /* call verbose_stats_dump() each 1000 +/-100msec */
2032 timer_id = timeSetEvent(1000, 100, verbose_stats_dump, 0, TIME_PERIODIC);
2033 setvbuf(stderr, NULL, _IONBF, 0);
2034 #elif defined(HAVE_ALARM)
2035 (void)setsignal(SIGALRM, verbose_stats_dump);
2041 if (RFileName == NULL) {
2043 * Live capture (if -V was specified, we set RFileName
2044 * to a file from the -V file). Print a message to
2045 * the standard error on UN*X.
2047 if (!vflag && !WFileName) {
2048 (void)fprintf(stderr,
2049 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
2052 (void)fprintf(stderr, "%s: ", program_name);
2053 dlt = pcap_datalink(pd);
2054 dlt_name = pcap_datalink_val_to_name(dlt);
2055 if (dlt_name == NULL) {
2056 (void)fprintf(stderr, "listening on %s, link-type %u, capture size %u bytes\n",
2057 device, dlt, snaplen);
2059 (void)fprintf(stderr, "listening on %s, link-type %s (%s), capture size %u bytes\n",
2061 pcap_datalink_val_to_description(dlt), snaplen);
2063 (void)fflush(stderr);
2068 cansandbox = (VFileName == NULL && zflag == NULL);
2070 cansandbox = (cansandbox && (nflag || capdns != NULL));
2072 cansandbox = (cansandbox && nflag);
2074 if (cansandbox && cap_enter() < 0 && errno != ENOSYS)
2075 error("unable to enter the capability mode");
2076 #endif /* __FreeBSD__ */
2079 status = pcap_loop(pd, cnt, callback, pcap_userdata);
2080 if (WFileName == NULL) {
2082 * We're printing packets. Flush the printed output,
2083 * so it doesn't get intermingled with error output.
2087 * We got interrupted, so perhaps we didn't
2088 * manage to finish a line we were printing.
2089 * Print an extra newline, just in case.
2093 (void)fflush(stdout);
2097 * We got interrupted. If we are reading multiple
2098 * files (via -V) set these so that we stop.
2107 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
2108 program_name, pcap_geterr(pd));
2110 if (RFileName == NULL) {
2112 * We're doing a live capture. Report the capture
2118 if (VFileName != NULL) {
2119 ret = get_next_file(VFile, VFileLine);
2121 RFileName = VFileLine;
2122 pd = pcap_open_offline(RFileName, ebuf);
2126 cap_rights_init(&rights, CAP_READ);
2127 if (cap_rights_limit(fileno(pcap_file(pd)),
2128 &rights) < 0 && errno != ENOSYS) {
2129 error("unable to limit pcap descriptor");
2132 new_dlt = pcap_datalink(pd);
2133 if (WFileName && new_dlt != dlt)
2134 error("%s: new dlt does not match original", RFileName);
2135 printinfo = get_print_info(new_dlt);
2136 dlt_name = pcap_datalink_val_to_name(new_dlt);
2137 if (dlt_name == NULL) {
2138 fprintf(stderr, "reading from file %s, link-type %u\n",
2139 RFileName, new_dlt);
2142 "reading from file %s, link-type %s (%s)\n",
2143 RFileName, dlt_name,
2144 pcap_datalink_val_to_description(new_dlt));
2146 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
2147 error("%s", pcap_geterr(pd));
2148 if (pcap_setfilter(pd, &fcode) < 0)
2149 error("%s", pcap_geterr(pd));
2153 while (ret != NULL);
2156 exit(status == -1 ? 1 : 0);
2159 /* make a clean exit on interrupts */
2161 cleanup(int signo _U_)
2163 #ifdef USE_WIN32_MM_TIMER
2165 timeKillEvent(timer_id);
2167 #elif defined(HAVE_ALARM)
2171 #ifdef HAVE_PCAP_BREAKLOOP
2173 * We have "pcap_breakloop()"; use it, so that we do as little
2174 * as possible in the signal handler (it's probably not safe
2175 * to do anything with standard I/O streams in a signal handler -
2176 * the ANSI C standard doesn't say it is).
2181 * We don't have "pcap_breakloop()"; this isn't safe, but
2182 * it's the best we can do. Print the summary if we're
2183 * not reading from a savefile - i.e., if we're doing a
2184 * live capture - and exit.
2186 if (pd != NULL && pcap_file(pd) == NULL) {
2188 * We got interrupted, so perhaps we didn't
2189 * manage to finish a line we were printing.
2190 * Print an extra newline, just in case.
2193 (void)fflush(stdout);
2201 On windows, we do not use a fork, so we do not care less about
2202 waiting a child processes to die
2204 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
2206 child_cleanup(int signo _U_)
2210 #endif /* HAVE_FORK && HAVE_VFORK */
2213 info(register int verbose)
2215 struct pcap_stat stat;
2218 * Older versions of libpcap didn't set ps_ifdrop on some
2219 * platforms; initialize it to 0 to handle that.
2222 if (pcap_stats(pd, &stat) < 0) {
2223 (void)fprintf(stderr, "pcap_stats: %s\n", pcap_geterr(pd));
2229 fprintf(stderr, "%s: ", program_name);
2231 (void)fprintf(stderr, "%u packet%s captured", packets_captured,
2232 PLURAL_SUFFIX(packets_captured));
2234 fputs(", ", stderr);
2237 (void)fprintf(stderr, "%u packet%s received by filter", stat.ps_recv,
2238 PLURAL_SUFFIX(stat.ps_recv));
2240 fputs(", ", stderr);
2243 (void)fprintf(stderr, "%u packet%s dropped by kernel", stat.ps_drop,
2244 PLURAL_SUFFIX(stat.ps_drop));
2245 if (stat.ps_ifdrop != 0) {
2247 fputs(", ", stderr);
2250 (void)fprintf(stderr, "%u packet%s dropped by interface\n",
2251 stat.ps_ifdrop, PLURAL_SUFFIX(stat.ps_ifdrop));
2257 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
2259 compress_savefile(const char *filename)
2268 * Set to lowest priority so that this doesn't disturb the capture
2271 setpriority(PRIO_PROCESS, 0, NZERO - 1);
2273 setpriority(PRIO_PROCESS, 0, 19);
2275 if (execlp(zflag, zflag, filename, (char *)NULL) == -1)
2277 "compress_savefile:execlp(%s, %s): %s\n",
2287 #else /* HAVE_FORK && HAVE_VFORK */
2289 compress_savefile(const char *filename)
2292 "compress_savefile failed. Functionality not implemented under your system\n");
2294 #endif /* HAVE_FORK && HAVE_VFORK */
2297 dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
2299 struct dump_info *dump_info;
2305 dump_info = (struct dump_info *)user;
2308 * XXX - this won't force the file to rotate on the specified time
2309 * boundary, but it will rotate on the first packet received after the
2310 * specified Gflag number of seconds. Note: if a Gflag time boundary
2311 * and a Cflag size boundary coincide, the time rotation will occur
2312 * first thereby cancelling the Cflag boundary (since the file should
2316 /* Check if it is time to rotate */
2319 /* Get the current time */
2320 if ((t = time(NULL)) == (time_t)-1) {
2321 error("dump_and_trunc_packet: can't get current_time: %s",
2322 pcap_strerror(errno));
2326 /* If the time is greater than the specified window, rotate */
2327 if (t - Gflag_time >= Gflag) {
2333 /* Update the Gflag_time */
2335 /* Update Gflag_count */
2338 * Close the current file and open a new one.
2340 pcap_dump_close(dump_info->p);
2343 * Compress the file we just closed, if the user asked for it
2346 compress_savefile(dump_info->CurrentFileName);
2349 * Check to see if we've exceeded the Wflag (when
2352 if (Cflag == 0 && Wflag > 0 && Gflag_count >= Wflag) {
2353 (void)fprintf(stderr, "Maximum file limit reached: %d\n",
2358 if (dump_info->CurrentFileName != NULL)
2359 free(dump_info->CurrentFileName);
2360 /* Allocate space for max filename + \0. */
2361 dump_info->CurrentFileName = (char *)malloc(PATH_MAX + 1);
2362 if (dump_info->CurrentFileName == NULL)
2363 error("dump_packet_and_trunc: malloc");
2365 * Gflag was set otherwise we wouldn't be here. Reset the count
2366 * so multiple files would end with 1,2,3 in the filename.
2367 * The counting is handled with the -C flow after this.
2372 * This is always the first file in the Cflag
2374 * We also don't need numbering if Cflag is not set.
2377 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, 0,
2380 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, 0, 0);
2382 #ifdef HAVE_LIBCAP_NG
2383 capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
2384 capng_apply(CAPNG_SELECT_BOTH);
2385 #endif /* HAVE_LIBCAP_NG */
2387 fd = openat(dump_info->dirfd,
2388 dump_info->CurrentFileName,
2389 O_CREAT | O_WRONLY | O_TRUNC, 0644);
2391 error("unable to open file %s",
2392 dump_info->CurrentFileName);
2394 fp = fdopen(fd, "w");
2396 error("unable to fdopen file %s",
2397 dump_info->CurrentFileName);
2399 dump_info->p = pcap_dump_fopen(dump_info->pd, fp);
2400 #else /* !HAVE_CAPSPER */
2401 dump_info->p = pcap_dump_open(dump_info->pd, dump_info->CurrentFileName);
2403 #ifdef HAVE_LIBCAP_NG
2404 capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
2405 capng_apply(CAPNG_SELECT_BOTH);
2406 #endif /* HAVE_LIBCAP_NG */
2407 if (dump_info->p == NULL)
2408 error("%s", pcap_geterr(pd));
2410 set_dumper_capsicum_rights(dump_info->p);
2416 * XXX - this won't prevent capture files from getting
2417 * larger than Cflag - the last packet written to the
2418 * file could put it over Cflag.
2421 long size = pcap_dump_ftell(dump_info->p);
2424 error("ftell fails on output file");
2432 * Close the current file and open a new one.
2434 pcap_dump_close(dump_info->p);
2437 * Compress the file we just closed, if the user
2441 compress_savefile(dump_info->CurrentFileName);
2445 if (Cflag_count >= Wflag)
2448 if (dump_info->CurrentFileName != NULL)
2449 free(dump_info->CurrentFileName);
2450 dump_info->CurrentFileName = (char *)malloc(PATH_MAX + 1);
2451 if (dump_info->CurrentFileName == NULL)
2452 error("dump_packet_and_trunc: malloc");
2453 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, Cflag_count, WflagChars);
2454 #ifdef HAVE_LIBCAP_NG
2455 capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
2456 capng_apply(CAPNG_SELECT_BOTH);
2457 #endif /* HAVE_LIBCAP_NG */
2459 fd = openat(dump_info->dirfd, dump_info->CurrentFileName,
2460 O_CREAT | O_WRONLY | O_TRUNC, 0644);
2462 error("unable to open file %s",
2463 dump_info->CurrentFileName);
2465 fp = fdopen(fd, "w");
2467 error("unable to fdopen file %s",
2468 dump_info->CurrentFileName);
2470 dump_info->p = pcap_dump_fopen(dump_info->pd, fp);
2471 #else /* !HAVE_CAPSPER */
2472 dump_info->p = pcap_dump_open(dump_info->pd, dump_info->CurrentFileName);
2474 #ifdef HAVE_LIBCAP_NG
2475 capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
2476 capng_apply(CAPNG_SELECT_BOTH);
2477 #endif /* HAVE_LIBCAP_NG */
2478 if (dump_info->p == NULL)
2479 error("%s", pcap_geterr(pd));
2481 set_dumper_capsicum_rights(dump_info->p);
2486 pcap_dump((u_char *)dump_info->p, h, sp);
2487 #ifdef HAVE_PCAP_DUMP_FLUSH
2489 pcap_dump_flush(dump_info->p);
2498 dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
2504 pcap_dump(user, h, sp);
2505 #ifdef HAVE_PCAP_DUMP_FLUSH
2507 pcap_dump_flush((pcap_dumper_t *)user);
2516 print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
2518 struct print_info *print_info;
2520 netdissect_options *ndo;
2526 print_info = (struct print_info *)user;
2527 ndo = print_info->ndo;
2529 if(ndo->ndo_packet_number)
2530 ND_PRINT((ndo, "%5u ", packets_captured));
2532 ts_print(ndo, &h->ts);
2535 * Some printers want to check that they're not walking off the
2536 * end of the packet.
2537 * Rather than pass it all the way down, we set this member
2538 * of the netdissect_options structure.
2540 ndo->ndo_snapend = sp + h->caplen;
2542 if(print_info->ndo_type) {
2543 hdrlen = (*print_info->p.ndo_printer)(print_info->ndo, h, sp);
2545 hdrlen = (*print_info->p.printer)(h, sp);
2549 * Restore the original snapend, as a printer might have
2552 ndo->ndo_snapend = sp + h->caplen;
2553 if (ndo->ndo_Xflag) {
2555 * Print the raw packet data in hex and ASCII.
2557 if (ndo->ndo_Xflag > 1) {
2559 * Include the link-layer header.
2561 hex_and_ascii_print(ndo, "\n\t", sp, h->caplen);
2564 * Don't include the link-layer header - and if
2565 * we have nothing past the link-layer header,
2568 if (h->caplen > hdrlen)
2569 hex_and_ascii_print(ndo, "\n\t", sp + hdrlen,
2570 h->caplen - hdrlen);
2572 } else if (ndo->ndo_xflag) {
2574 * Print the raw packet data in hex.
2576 if (ndo->ndo_xflag > 1) {
2578 * Include the link-layer header.
2580 hex_print(ndo, "\n\t", sp, h->caplen);
2583 * Don't include the link-layer header - and if
2584 * we have nothing past the link-layer header,
2587 if (h->caplen > hdrlen)
2588 hex_print(ndo, "\n\t", sp + hdrlen,
2589 h->caplen - hdrlen);
2591 } else if (ndo->ndo_Aflag) {
2593 * Print the raw packet data in ASCII.
2595 if (ndo->ndo_Aflag > 1) {
2597 * Include the link-layer header.
2599 ascii_print(ndo, sp, h->caplen);
2602 * Don't include the link-layer header - and if
2603 * we have nothing past the link-layer header,
2606 if (h->caplen > hdrlen)
2607 ascii_print(ndo, sp + hdrlen, h->caplen - hdrlen);
2620 * XXX - there should really be libpcap calls to get the version
2621 * number as a string (the string would be generated from #defines
2622 * at run time, so that it's not generated from string constants
2623 * in the library, as, on many UNIX systems, those constants would
2624 * be statically linked into the application executable image, and
2625 * would thus reflect the version of libpcap on the system on
2626 * which the application was *linked*, not the system on which it's
2629 * That routine should be documented, unlike the "version[]"
2630 * string, so that UNIX vendors providing their own libpcaps
2631 * don't omit it (as a couple of vendors have...).
2633 * Packet.dll should perhaps also export a routine to return the
2634 * version number of the Packet.dll code, to supply the
2635 * "Wpcap_version" information on Windows.
2637 char WDversion[]="current-git.tcpdump.org";
2638 #if !defined(HAVE_GENERATED_VERSION)
2639 char version[]="current-git.tcpdump.org";
2641 char pcap_version[]="current-git.tcpdump.org";
2642 char Wpcap_version[]="3.1";
2646 * By default, print the specified data out in hex and ASCII.
2649 ndo_default_print(netdissect_options *ndo, const u_char *bp, u_int length)
2651 hex_and_ascii_print(ndo, "\n\t", bp, length); /* pass on lf and indentation string */
2655 default_print(const u_char *bp, u_int length)
2657 ndo_default_print(gndo, bp, length);
2660 #ifdef SIGNAL_REQ_INFO
2661 RETSIGTYPE requestinfo(int signo _U_)
2671 * Called once each second in verbose mode while dumping to file
2673 #ifdef USE_WIN32_MM_TIMER
2674 void CALLBACK verbose_stats_dump (UINT timer_id _U_, UINT msg _U_, DWORD_PTR arg _U_,
2675 DWORD_PTR dw1 _U_, DWORD_PTR dw2 _U_)
2677 struct pcap_stat stat;
2679 if (infodelay == 0 && pcap_stats(pd, &stat) >= 0)
2680 fprintf(stderr, "Got %u\r", packets_captured);
2682 #elif defined(HAVE_ALARM)
2683 static void verbose_stats_dump(int sig _U_)
2685 struct pcap_stat stat;
2687 if (infodelay == 0 && pcap_stats(pd, &stat) >= 0)
2688 fprintf(stderr, "Got %u\r", packets_captured);
2693 USES_APPLE_DEPRECATED_API
2697 extern char version[];
2698 #ifndef HAVE_PCAP_LIB_VERSION
2699 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
2700 extern char pcap_version[];
2701 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2702 static char pcap_version[] = "unknown";
2703 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2704 #endif /* HAVE_PCAP_LIB_VERSION */
2706 #ifdef HAVE_PCAP_LIB_VERSION
2708 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
2710 (void)fprintf(stderr, "%s version %s\n", program_name, version);
2712 (void)fprintf(stderr, "%s\n",pcap_lib_version());
2713 #else /* HAVE_PCAP_LIB_VERSION */
2715 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
2716 (void)fprintf(stderr, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version, pcap_version);
2718 (void)fprintf(stderr, "%s version %s\n", program_name, version);
2719 (void)fprintf(stderr, "libpcap version %s\n", pcap_version);
2721 #endif /* HAVE_PCAP_LIB_VERSION */
2723 #if defined(HAVE_LIBCRYPTO) && defined(SSLEAY_VERSION)
2724 (void)fprintf (stderr, "%s\n", SSLeay_version(SSLEAY_VERSION));
2728 (void)fprintf (stderr, "SMI-library: %s\n", smi_version_string);
2737 (void)fprintf(stderr,
2738 "Usage: %s [-aAbd" D_FLAG "efhH" I_FLAG J_FLAG "KlLnNOpqRStu" U_FLAG "vxX#]" B_FLAG_USAGE " [ -c count ]\n", program_name);
2739 (void)fprintf(stderr,
2740 "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n");
2741 (void)fprintf(stderr,
2742 "\t\t[ -i interface ]" j_FLAG_USAGE " [ -M secret ] [ --number ]\n");
2743 #ifdef HAVE_PCAP_SETDIRECTION
2744 (void)fprintf(stderr,
2745 "\t\t[ -Q in|out|inout ]\n");
2747 (void)fprintf(stderr,
2748 "\t\t[ -r file ] [ -s snaplen ] ");
2749 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
2750 (void)fprintf(stderr, "[ --time-stamp-precision precision ]\n");
2751 (void)fprintf(stderr,
2754 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
2755 (void)fprintf(stderr, "[ --immediate-mode ] ");
2757 (void)fprintf(stderr, "[ -T type ] [ --version ] [ -V file ]\n");
2758 (void)fprintf(stderr,
2759 "\t\t[ -w file ] [ -W filecount ] [ -y datalinktype ] [ -z command ]\n");
2760 (void)fprintf(stderr,
2761 "\t\t[ -Z user ] [ expression ]\n");
2768 ndo_error(netdissect_options *ndo _U_, const char *fmt, ...)
2772 (void)fprintf(stderr, "%s: ", program_name);
2774 (void)vfprintf(stderr, fmt, ap);
2778 if (fmt[-1] != '\n')
2779 (void)fputc('\n', stderr);
2787 ndo_warning(netdissect_options *ndo _U_, const char *fmt, ...)
2791 (void)fprintf(stderr, "%s: WARNING: ", program_name);
2793 (void)vfprintf(stderr, fmt, ap);
2797 if (fmt[-1] != '\n')
2798 (void)fputc('\n', stderr);
2803 * c-style: whitesmith