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>
90 #endif /* __FreeBSD__ */
92 #include <libcasper.h>
93 #include <casper/cap_dns.h>
95 #include <sys/ioccom.h>
98 #endif /* HAVE_CASPER */
106 #include <sys/wait.h>
107 #include <sys/resource.h>
112 /* capabilities convenience library */
113 /* If a code depends on HAVE_LIBCAP_NG, it depends also on HAVE_CAP_NG_H.
114 * If HAVE_CAP_NG_H is not defined, undefine HAVE_LIBCAP_NG.
115 * Thus, the later tests are done only on HAVE_LIBCAP_NG.
117 #ifdef HAVE_LIBCAP_NG
121 #undef HAVE_LIBCAP_NG
122 #endif /* HAVE_CAP_NG_H */
123 #endif /* HAVE_LIBCAP_NG */
125 #include "netdissect.h"
126 #include "interface.h"
127 #include "addrtoname.h"
129 #include "setsignal.h"
130 #include "gmt2local.h"
131 #include "pcap-missing.h"
134 #define PATH_MAX 1024
138 #define SIGNAL_REQ_INFO SIGINFO
140 #define SIGNAL_REQ_INFO SIGUSR1
143 netdissect_options Gndo;
144 netdissect_options *gndo = &Gndo;
146 static int Dflag; /* list available devices and exit */
147 static int dflag; /* print filter code */
148 static int Lflag; /* list available data link types and exit */
149 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
150 static int Jflag; /* list available time stamp types */
152 #ifdef HAVE_PCAP_SETDIRECTION
153 int Qflag = -1; /* restrict captured packet by send/receive direction */
155 static char *zflag = NULL; /* compress each savefile using a specified command (like gzip or bzip2) */
157 static int infodelay;
158 static int infoprint;
163 cap_channel_t *capdns;
166 int32_t thiszone; /* seconds offset from gmt to local time */
169 static RETSIGTYPE cleanup(int);
170 static RETSIGTYPE child_cleanup(int);
171 static void print_version(void);
172 static void print_usage(void);
173 static void show_dlts_and_exit(const char *device, pcap_t *pd) __attribute__((noreturn));
175 static void print_packet(u_char *, const struct pcap_pkthdr *, const u_char *);
176 static void ndo_default_print(netdissect_options *, const u_char *, u_int);
177 static void dump_packet_and_trunc(u_char *, const struct pcap_pkthdr *, const u_char *);
178 static void dump_packet(u_char *, const struct pcap_pkthdr *, const u_char *);
179 static void droproot(const char *, const char *);
180 static void ndo_error(netdissect_options *ndo, const char *fmt, ...)
181 __attribute__((noreturn))
182 #ifdef __ATTRIBUTE___FORMAT_OK
183 __attribute__((format (printf, 2, 3)))
184 #endif /* __ATTRIBUTE___FORMAT_OK */
186 static void ndo_warning(netdissect_options *ndo, const char *fmt, ...)
187 #ifdef __ATTRIBUTE___FORMAT_OK
188 __attribute__((format (printf, 2, 3)))
189 #endif /* __ATTRIBUTE___FORMAT_OK */
192 #ifdef SIGNAL_REQ_INFO
193 RETSIGTYPE requestinfo(int);
196 #if defined(USE_WIN32_MM_TIMER)
197 #include <MMsystem.h>
198 static UINT timer_id;
199 static void CALLBACK verbose_stats_dump(UINT, UINT, DWORD_PTR, DWORD_PTR, DWORD_PTR);
200 #elif defined(HAVE_ALARM)
201 static void verbose_stats_dump(int sig);
204 static void info(int);
205 static u_int packets_captured;
219 static const struct printer printers[] = {
223 static const struct ndo_printer ndo_printers[] = {
224 { ether_if_print, DLT_EN10MB },
226 { ipnet_if_print, DLT_IPNET },
228 #ifdef DLT_IEEE802_15_4
229 { ieee802_15_4_if_print, DLT_IEEE802_15_4 },
231 #ifdef DLT_IEEE802_15_4_NOFCS
232 { ieee802_15_4_if_print, DLT_IEEE802_15_4_NOFCS },
235 { ppi_if_print, DLT_PPI },
237 #ifdef DLT_NETANALYZER
238 { netanalyzer_if_print, DLT_NETANALYZER },
240 #ifdef DLT_NETANALYZER_TRANSPARENT
241 { netanalyzer_transparent_if_print, DLT_NETANALYZER_TRANSPARENT },
243 #if defined(DLT_NFLOG) && defined(HAVE_PCAP_NFLOG_H)
244 { nflog_if_print, DLT_NFLOG},
247 { cip_if_print, DLT_CIP },
250 { cip_if_print, DLT_ATM_CLIP },
252 #ifdef DLT_IP_OVER_FC
253 { ipfc_if_print, DLT_IP_OVER_FC },
255 { null_if_print, DLT_NULL },
257 { null_if_print, DLT_LOOP },
259 #ifdef DLT_APPLE_IP_OVER_IEEE1394
260 { ap1394_if_print, DLT_APPLE_IP_OVER_IEEE1394 },
262 #if defined(DLT_BLUETOOTH_HCI_H4_WITH_PHDR) && defined(HAVE_PCAP_BLUETOOTH_H)
263 { bt_if_print, DLT_BLUETOOTH_HCI_H4_WITH_PHDR},
266 { lane_if_print, DLT_LANE8023 },
268 { arcnet_if_print, DLT_ARCNET },
269 #ifdef DLT_ARCNET_LINUX
270 { arcnet_linux_if_print, DLT_ARCNET_LINUX },
272 { raw_if_print, DLT_RAW },
274 { raw_if_print, DLT_IPV4 },
277 { raw_if_print, DLT_IPV6 },
279 #ifdef HAVE_PCAP_USB_H
281 { usb_linux_48_byte_print, DLT_USB_LINUX},
282 #endif /* DLT_USB_LINUX */
283 #ifdef DLT_USB_LINUX_MMAPPED
284 { usb_linux_64_byte_print, DLT_USB_LINUX_MMAPPED},
285 #endif /* DLT_USB_LINUX_MMAPPED */
286 #endif /* HAVE_PCAP_USB_H */
287 #ifdef DLT_SYMANTEC_FIREWALL
288 { symantec_if_print, DLT_SYMANTEC_FIREWALL },
291 { chdlc_if_print, DLT_C_HDLC },
294 { chdlc_if_print, DLT_HDLC },
297 { pppoe_if_print, DLT_PPP_ETHER },
299 #if defined(DLT_PFLOG) && defined(HAVE_NET_PFVAR_H)
300 { pflog_if_print, DLT_PFLOG },
302 { token_if_print, DLT_IEEE802 },
303 { fddi_if_print, DLT_FDDI },
305 { sll_if_print, DLT_LINUX_SLL },
308 { fr_if_print, DLT_FR },
311 { fr_if_print, DLT_FRELAY },
314 { mfr_if_print, DLT_MFR },
316 { atm_if_print, DLT_ATM_RFC1483 },
318 { sunatm_if_print, DLT_SUNATM },
321 { enc_if_print, DLT_ENC },
323 { sl_if_print, DLT_SLIP },
324 #ifdef DLT_SLIP_BSDOS
325 { sl_bsdos_if_print, DLT_SLIP_BSDOS },
328 { ltalk_if_print, DLT_LTALK },
330 #ifdef DLT_JUNIPER_ATM1
331 { juniper_atm1_print, DLT_JUNIPER_ATM1 },
333 #ifdef DLT_JUNIPER_ATM2
334 { juniper_atm2_print, DLT_JUNIPER_ATM2 },
336 #ifdef DLT_JUNIPER_MFR
337 { juniper_mfr_print, DLT_JUNIPER_MFR },
339 #ifdef DLT_JUNIPER_MLFR
340 { juniper_mlfr_print, DLT_JUNIPER_MLFR },
342 #ifdef DLT_JUNIPER_MLPPP
343 { juniper_mlppp_print, DLT_JUNIPER_MLPPP },
345 #ifdef DLT_JUNIPER_PPPOE
346 { juniper_pppoe_print, DLT_JUNIPER_PPPOE },
348 #ifdef DLT_JUNIPER_PPPOE_ATM
349 { juniper_pppoe_atm_print, DLT_JUNIPER_PPPOE_ATM },
351 #ifdef DLT_JUNIPER_GGSN
352 { juniper_ggsn_print, DLT_JUNIPER_GGSN },
354 #ifdef DLT_JUNIPER_ES
355 { juniper_es_print, DLT_JUNIPER_ES },
357 #ifdef DLT_JUNIPER_MONITOR
358 { juniper_monitor_print, DLT_JUNIPER_MONITOR },
360 #ifdef DLT_JUNIPER_SERVICES
361 { juniper_services_print, DLT_JUNIPER_SERVICES },
363 #ifdef DLT_JUNIPER_ETHER
364 { juniper_ether_print, DLT_JUNIPER_ETHER },
366 #ifdef DLT_JUNIPER_PPP
367 { juniper_ppp_print, DLT_JUNIPER_PPP },
369 #ifdef DLT_JUNIPER_FRELAY
370 { juniper_frelay_print, DLT_JUNIPER_FRELAY },
372 #ifdef DLT_JUNIPER_CHDLC
373 { juniper_chdlc_print, DLT_JUNIPER_CHDLC },
376 { pktap_if_print, DLT_PKTAP },
378 #ifdef DLT_IEEE802_11_RADIO
379 { ieee802_11_radio_if_print, DLT_IEEE802_11_RADIO },
381 #ifdef DLT_IEEE802_11
382 { ieee802_11_if_print, DLT_IEEE802_11},
384 #ifdef DLT_IEEE802_11_RADIO_AVS
385 { ieee802_11_radio_avs_if_print, DLT_IEEE802_11_RADIO_AVS },
387 #ifdef DLT_PRISM_HEADER
388 { prism_if_print, DLT_PRISM_HEADER },
390 { ppp_if_print, DLT_PPP },
391 #ifdef DLT_PPP_WITHDIRECTION
392 { ppp_if_print, DLT_PPP_WITHDIRECTION },
395 { ppp_bsdos_if_print, DLT_PPP_BSDOS },
397 #ifdef DLT_PPP_SERIAL
398 { ppp_hdlc_if_print, DLT_PPP_SERIAL },
403 static const struct tok status_flags[] = {
405 { PCAP_IF_UP, "Up" },
407 #ifdef PCAP_IF_RUNNING
408 { PCAP_IF_RUNNING, "Running" },
410 { PCAP_IF_LOOPBACK, "Loopback" },
415 lookup_printer(int type)
417 const struct printer *p;
419 for (p = printers; p->f; ++p)
428 lookup_ndo_printer(int type)
430 const struct ndo_printer *p;
432 for (p = ndo_printers; p->f; ++p)
436 #if defined(DLT_USER2) && defined(DLT_PKTAP)
438 * Apple incorrectly chose to use DLT_USER2 for their PKTAP
441 * We map DLT_PKTAP, whether it's DLT_USER2 as it is on Darwin-
442 * based OSes or the same value as LINKTYPE_PKTAP as it is on
443 * other OSes, to LINKTYPE_PKTAP, so files written with
444 * this version of libpcap for a DLT_PKTAP capture have a link-
445 * layer header type of LINKTYPE_PKTAP.
447 * However, files written on OS X Mavericks for a DLT_PKTAP
448 * capture have a link-layer header type of LINKTYPE_USER2.
449 * If we don't have a printer for DLT_USER2, and type is
450 * DLT_USER2, we look up the printer for DLT_PKTAP and use
453 if (type == DLT_USER2) {
454 for (p = ndo_printers; p->f; ++p)
455 if (DLT_PKTAP == p->type)
466 static int supports_monitor_mode;
473 netdissect_options *ndo;
476 if_ndo_printer ndo_printer;
483 char *CurrentFileName;
491 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
493 show_tstamp_types_and_exit(const char *device, pcap_t *pd)
496 int *tstamp_types = 0;
497 const char *tstamp_type_name;
500 n_tstamp_types = pcap_list_tstamp_types(pd, &tstamp_types);
501 if (n_tstamp_types < 0)
502 error("%s", pcap_geterr(pd));
504 if (n_tstamp_types == 0) {
505 fprintf(stderr, "Time stamp type cannot be set for %s\n",
509 fprintf(stderr, "Time stamp types for %s (use option -j to set):\n",
511 for (i = 0; i < n_tstamp_types; i++) {
512 tstamp_type_name = pcap_tstamp_type_val_to_name(tstamp_types[i]);
513 if (tstamp_type_name != NULL) {
514 (void) fprintf(stderr, " %s (%s)\n", tstamp_type_name,
515 pcap_tstamp_type_val_to_description(tstamp_types[i]));
517 (void) fprintf(stderr, " %d\n", tstamp_types[i]);
520 pcap_free_tstamp_types(tstamp_types);
526 show_dlts_and_exit(const char *device, pcap_t *pd)
530 const char *dlt_name;
532 n_dlts = pcap_list_datalinks(pd, &dlts);
534 error("%s", pcap_geterr(pd));
535 else if (n_dlts == 0 || !dlts)
536 error("No data link types.");
539 * If the interface is known to support monitor mode, indicate
540 * whether these are the data link types available when not in
541 * monitor mode, if -I wasn't specified, or when in monitor mode,
542 * when -I was specified (the link-layer types available in
543 * monitor mode might be different from the ones available when
544 * not in monitor mode).
546 if (supports_monitor_mode)
547 (void) fprintf(stderr, "Data link types for %s %s (use option -y to set):\n",
549 Iflag ? "when in monitor mode" : "when not in monitor mode");
551 (void) fprintf(stderr, "Data link types for %s (use option -y to set):\n",
554 while (--n_dlts >= 0) {
555 dlt_name = pcap_datalink_val_to_name(dlts[n_dlts]);
556 if (dlt_name != NULL) {
557 (void) fprintf(stderr, " %s (%s)", dlt_name,
558 pcap_datalink_val_to_description(dlts[n_dlts]));
561 * OK, does tcpdump handle that type?
563 if (lookup_printer(dlts[n_dlts]) == NULL
564 && lookup_ndo_printer(dlts[n_dlts]) == NULL)
565 (void) fprintf(stderr, " (printing not supported)");
566 fprintf(stderr, "\n");
568 (void) fprintf(stderr, " DLT %d (printing not supported)\n",
572 #ifdef HAVE_PCAP_FREE_DATALINKS
573 pcap_free_datalinks(dlts);
578 #ifdef HAVE_PCAP_FINDALLDEVS
580 show_devices_and_exit (void)
582 pcap_if_t *devpointer;
583 char ebuf[PCAP_ERRBUF_SIZE];
586 if (pcap_findalldevs(&devpointer, ebuf) < 0)
589 for (i = 0; devpointer != NULL; i++) {
590 printf("%d.%s", i+1, devpointer->name);
591 if (devpointer->description != NULL)
592 printf(" (%s)", devpointer->description);
593 if (devpointer->flags != 0)
594 printf(" [%s]", bittok2str(status_flags, "none", devpointer->flags));
596 devpointer = devpointer->next;
601 #endif /* HAVE_PCAP_FINDALLDEVS */
606 * Note that there we use all letters for short options except for g, k,
607 * o, and P, and those are used by other versions of tcpdump, and we should
608 * only use them for the same purposes that the other versions of tcpdump
611 * OS X tcpdump uses -g to force non--v output for IP to be on one
612 * line, making it more "g"repable;
614 * OS X tcpdump uses -k tospecify that packet comments in pcap-ng files
617 * OpenBSD tcpdump uses -o to indicate that OS fingerprinting should be done
618 * for hosts sending TCP SYN packets;
620 * OS X tcpdump uses -P to indicate that -w should write pcap-ng rather
623 * OS X tcpdump also uses -Q to specify expressions that match packet
624 * metadata, including but not limited to the packet direction.
625 * The expression syntax is different from a simple "in|out|inout",
626 * and those expressions aren't accepted by OS X tcpdump, but the
627 * equivalents would be "in" = "dir=in", "out" = "dir=out", and
628 * "inout" = "dir=in or dir=out", and the parser could conceivably
629 * special-case "in", "out", and "inout" as expressions for backwards
630 * compatibility, so all is not (yet) lost.
634 * Set up flags that might or might not be supported depending on the
635 * version of libpcap we're using.
637 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
639 #define B_FLAG_USAGE " [ -B size ]"
640 #else /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
643 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
645 #ifdef HAVE_PCAP_CREATE
647 #else /* HAVE_PCAP_CREATE */
649 #endif /* HAVE_PCAP_CREATE */
651 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
653 #define j_FLAG_USAGE " [ -j tstamptype ]"
655 #else /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
659 #endif /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
661 #ifdef HAVE_PCAP_FINDALLDEVS
667 #ifdef HAVE_PCAP_DUMP_FLUSH
673 #ifdef HAVE_PCAP_SETDIRECTION
679 #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:#"
684 * We do not currently have long options corresponding to all short
685 * options; we should probably pick appropriate option names for them.
687 * However, the short options where the number of times the option is
688 * specified matters, such as -v and -d and -t, should probably not
689 * just map to a long option, as saying
691 * tcpdump --verbose --verbose
693 * doesn't make sense; it should be --verbosity={N} or something such
696 * For long options with no corresponding short options, we define values
697 * outside the range of ASCII graphic characters, make that the last
698 * component of the entry for the long option, and have a case for that
699 * option in the switch statement.
701 #define OPTION_VERSION 128
702 #define OPTION_TSTAMP_PRECISION 129
703 #define OPTION_IMMEDIATE_MODE 130
705 static const struct option longopts[] = {
706 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
707 { "buffer-size", required_argument, NULL, 'B' },
709 { "list-interfaces", no_argument, NULL, 'D' },
710 { "help", no_argument, NULL, 'h' },
711 { "interface", required_argument, NULL, 'i' },
712 #ifdef HAVE_PCAP_CREATE
713 { "monitor-mode", no_argument, NULL, 'I' },
715 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
716 { "time-stamp-type", required_argument, NULL, 'j' },
717 { "list-time-stamp-types", no_argument, NULL, 'J' },
719 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
720 { "time-stamp-precision", required_argument, NULL, OPTION_TSTAMP_PRECISION},
722 { "dont-verify-checksums", no_argument, NULL, 'K' },
723 { "list-data-link-types", no_argument, NULL, 'L' },
724 { "no-optimize", no_argument, NULL, 'O' },
725 { "no-promiscuous-mode", no_argument, NULL, 'p' },
726 #ifdef HAVE_PCAP_SETDIRECTION
727 { "direction", required_argument, NULL, 'Q' },
729 { "snapshot-length", required_argument, NULL, 's' },
730 { "absolute-tcp-sequence-numbers", no_argument, NULL, 'S' },
731 #ifdef HAVE_PCAP_DUMP_FLUSH
732 { "packet-buffered", no_argument, NULL, 'U' },
734 { "linktype", required_argument, NULL, 'y' },
735 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
736 { "immediate-mode", no_argument, NULL, OPTION_IMMEDIATE_MODE },
738 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
739 { "debug-filter-parser", no_argument, NULL, 'Y' },
741 { "relinquish-privileges", required_argument, NULL, 'Z' },
742 { "number", no_argument, NULL, '#' },
743 { "version", no_argument, NULL, OPTION_VERSION },
748 /* Drop root privileges and chroot if necessary */
750 droproot(const char *username, const char *chroot_dir)
752 struct passwd *pw = NULL;
754 if (chroot_dir && !username) {
755 fprintf(stderr, "tcpdump: Chroot without dropping root is insecure\n");
759 pw = getpwnam(username);
762 if (chroot(chroot_dir) != 0 || chdir ("/") != 0) {
763 fprintf(stderr, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
764 chroot_dir, pcap_strerror(errno));
768 #ifdef HAVE_LIBCAP_NG
769 int ret = capng_change_id(pw->pw_uid, pw->pw_gid, CAPNG_NO_FLAG);
771 fprintf(stderr, "error : ret %d\n", ret);
774 fprintf(stderr, "dropped privs to %s\n", username);
777 if (initgroups(pw->pw_name, pw->pw_gid) != 0 ||
778 setgid(pw->pw_gid) != 0 || setuid(pw->pw_uid) != 0) {
779 fprintf(stderr, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
781 (unsigned long)pw->pw_uid,
782 (unsigned long)pw->pw_gid,
783 pcap_strerror(errno));
787 fprintf(stderr, "dropped privs to %s\n", username);
789 #endif /* HAVE_LIBCAP_NG */
792 fprintf(stderr, "tcpdump: Couldn't find user '%.32s'\n",
796 #ifdef HAVE_LIBCAP_NG
797 /* We don't need CAP_SETUID and CAP_SETGID any more. */
800 CAPNG_EFFECTIVE | CAPNG_PERMITTED,
804 capng_apply(CAPNG_SELECT_BOTH);
805 #endif /* HAVE_LIBCAP_NG */
826 MakeFilename(char *buffer, char *orig_name, int cnt, int max_chars)
828 char *filename = malloc(PATH_MAX + 1);
829 if (filename == NULL)
830 error("Makefilename: malloc");
832 /* Process with strftime if Gflag is set. */
836 /* Convert Gflag_time to a usable format */
837 if ((local_tm = localtime(&Gflag_time)) == NULL) {
838 error("MakeTimedFilename: localtime");
841 /* There's no good way to detect an error in strftime since a return
842 * value of 0 isn't necessarily failure.
844 strftime(filename, PATH_MAX, orig_name, local_tm);
846 strncpy(filename, orig_name, PATH_MAX);
849 if (cnt == 0 && max_chars == 0)
850 strncpy(buffer, filename, PATH_MAX + 1);
852 if (snprintf(buffer, PATH_MAX + 1, "%s%0*d", filename, max_chars, cnt) > PATH_MAX)
853 /* Report an error if the filename is too large */
854 error("too many output files or filename is too long (> %d)", PATH_MAX);
858 static int tcpdump_printf(netdissect_options *ndo _U_,
859 const char *fmt, ...)
866 ret=vfprintf(stdout, fmt, args);
872 static struct print_info
873 get_print_info(int type)
875 struct print_info printinfo;
877 printinfo.ndo_type = 1;
878 printinfo.ndo = gndo;
879 printinfo.p.ndo_printer = lookup_ndo_printer(type);
880 if (printinfo.p.ndo_printer == NULL) {
881 printinfo.p.printer = lookup_printer(type);
882 printinfo.ndo_type = 0;
883 if (printinfo.p.printer == NULL) {
884 gndo->ndo_dltname = pcap_datalink_val_to_name(type);
885 if (gndo->ndo_dltname != NULL)
886 error("packet printing is not supported for link type %s: use -w",
889 error("packet printing is not supported for link type %d: use -w", type);
896 get_next_file(FILE *VFile, char *ptr)
900 ret = fgets(ptr, PATH_MAX, VFile);
904 if (ptr[strlen(ptr) - 1] == '\n')
905 ptr[strlen(ptr) - 1] = '\0';
911 static cap_channel_t *
914 cap_channel_t *capcas, *capdnsloc;
915 const char *types[1];
920 error("unable to create casper process");
921 capdnsloc = cap_service_open(capcas, "system.dns");
922 /* Casper capability no longer needed. */
924 if (capdnsloc == NULL)
925 error("unable to open system.dns service");
926 /* Limit system.dns to reverse DNS lookups. */
928 if (cap_dns_type_limit(capdnsloc, types, 1) < 0)
929 error("unable to limit access to system.dns service");
930 families[0] = AF_INET;
931 families[1] = AF_INET6;
932 if (cap_dns_family_limit(capdnsloc, families, 2) < 0)
933 error("unable to limit access to system.dns service");
937 #endif /* HAVE_CASPER */
939 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
941 tstamp_precision_from_string(const char *precision)
943 if (strncmp(precision, "nano", strlen("nano")) == 0)
944 return PCAP_TSTAMP_PRECISION_NANO;
946 if (strncmp(precision, "micro", strlen("micro")) == 0)
947 return PCAP_TSTAMP_PRECISION_MICRO;
953 tstamp_precision_to_string(int precision)
957 case PCAP_TSTAMP_PRECISION_MICRO:
960 case PCAP_TSTAMP_PRECISION_NANO:
971 * Ensure that, on a dump file's descriptor, we have all the rights
972 * necessary to make the standard I/O library work with an fdopen()ed
973 * FILE * from that descriptor.
975 * A long time ago, in a galaxy far far away, AT&T decided that, instead
976 * of providing separate APIs for getting and setting the FD_ flags on a
977 * descriptor, getting and setting the O_ flags on a descriptor, and
978 * locking files, they'd throw them all into a kitchen-sink fcntl() call
979 * along the lines of ioctl(), the fact that ioctl() operations are
980 * largely specific to particular character devices but fcntl() operations
981 * are either generic to all descriptors or generic to all descriptors for
982 * regular files nonwithstanding.
984 * The Capsicum people decided that fine-grained control of descriptor
985 * operations was required, so that you need to grant permission for
986 * reading, writing, seeking, and fcntl-ing. The latter, courtesy of
987 * AT&T's decision, means that "fcntl-ing" isn't a thing, but a motley
988 * collection of things, so there are *individual* fcntls for which
989 * permission needs to be granted.
991 * The FreeBSD standard I/O people implemented some optimizations that
992 * requires that the standard I/O routines be able to determine whether
993 * the descriptor for the FILE * is open append-only or not; as that
994 * descriptor could have come from an open() rather than an fopen(),
995 * that requires that it be able to do an F_GETFL fcntl() to read
998 * Tcpdump uses ftell() to determine how much data has been written
999 * to a file in order to, when used with -C, determine when it's time
1000 * to rotate capture files. ftell() therefore needs to do an lseek()
1001 * to find out the file offset and must, thanks to the aforementioned
1002 * optimization, also know whether the descriptor is open append-only
1005 * The net result of all the above is that we need to grant CAP_SEEK,
1006 * CAP_WRITE, and CAP_FCNTL with the CAP_FCNTL_GETFL subcapability.
1008 * Perhaps this is the universe's way of saying that either
1010 * 1) there needs to be an fopenat() call and a pcap_dump_openat() call
1011 * using it, so that Capsicum-capable tcpdump wouldn't need to do
1016 * 2) there needs to be a cap_fdopen() call in the FreeBSD standard
1017 * I/O library that knows what rights are needed by the standard
1018 * I/O library, based on the open mode, and assigns them, perhaps
1019 * with an additional argument indicating, for example, whether
1020 * seeking should be allowed, so that tcpdump doesn't need to know
1021 * what the standard I/O library happens to require this week.
1024 set_dumper_capsicum_rights(pcap_dumper_t *p)
1026 int fd = fileno(pcap_dump_file(p));
1027 cap_rights_t rights;
1029 cap_rights_init(&rights, CAP_SEEK, CAP_WRITE, CAP_FCNTL);
1030 if (cap_rights_limit(fd, &rights) < 0 && errno != ENOSYS) {
1031 error("unable to limit dump descriptor");
1033 if (cap_fcntls_limit(fd, CAP_FCNTL_GETFL) < 0 && errno != ENOSYS) {
1034 error("unable to limit dump descriptor fcntls");
1040 main(int argc, char **argv)
1042 register int cnt, op, i;
1043 bpf_u_int32 localnet =0 , netmask = 0;
1044 register char *cp, *infile, *cmdbuf, *device, *RFileName, *VFileName, *WFileName;
1045 pcap_handler callback;
1049 const char *dlt_name;
1050 struct bpf_program fcode;
1052 RETSIGTYPE (*oldhandler)(int);
1054 struct print_info printinfo;
1055 struct dump_info dumpinfo;
1056 u_char *pcap_userdata;
1057 char ebuf[PCAP_ERRBUF_SIZE];
1058 char VFileLine[PATH_MAX + 1];
1059 char *username = NULL;
1060 char *chroot_dir = NULL;
1063 #ifdef HAVE_PCAP_FINDALLDEVS
1064 pcap_if_t *devpointer;
1070 cap_rights_t rights;
1071 #endif /* !__FreeBSD__ */
1075 if(wsockinit() != 0) return 1;
1078 jflag=-1; /* not set */
1082 gndo->ndo_default_print=ndo_default_print;
1083 gndo->ndo_printf=tcpdump_printf;
1084 gndo->ndo_error=ndo_error;
1085 gndo->ndo_warning=ndo_warning;
1086 gndo->ndo_snaplen = DEFAULT_SNAPLEN;
1087 gndo->ndo_immediate = 0;
1097 if ((cp = strrchr(argv[0], '/')) != NULL)
1098 program_name = cp + 1;
1100 program_name = argv[0];
1103 * On platforms where the CPU doesn't support unaligned loads,
1104 * force unaligned accesses to abort with SIGBUS, rather than
1105 * being fixed up (slowly) by the OS kernel; on those platforms,
1106 * misaligned accesses are bugs, and we want tcpdump to crash so
1107 * that the bugs are reported.
1109 if (abort_on_misalignment(ebuf, sizeof(ebuf)) < 0)
1117 (op = getopt_long(argc, argv, SHORTOPTS, longopts, NULL)) != -1)
1121 /* compatibility for old -a */
1132 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
1134 Bflag = atoi(optarg)*1024;
1136 error("invalid packet buffer size %s", optarg);
1138 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
1143 error("invalid packet count %s", optarg);
1147 Cflag = atoi(optarg) * 1000000;
1149 error("invalid file size %s", optarg);
1169 #ifndef HAVE_LIBCRYPTO
1170 warning("crypto code not compiled in");
1172 gndo->ndo_espsecret = optarg;
1184 Gflag = atoi(optarg);
1186 error("invalid number of seconds %s", optarg);
1188 /* We will create one file initially. */
1191 /* Grab the current time for rotation use. */
1192 if ((Gflag_time = time(NULL)) == (time_t)-1) {
1193 error("main: can't get current time: %s",
1194 pcap_strerror(errno));
1208 if (optarg[0] == '0' && optarg[1] == 0)
1209 error("Invalid adapter index");
1211 #ifdef HAVE_PCAP_FINDALLDEVS
1213 * If the argument is a number, treat it as
1214 * an index into the list of adapters, as
1215 * printed by "tcpdump -D".
1217 * This should be OK on UNIX systems, as interfaces
1218 * shouldn't have names that begin with digits.
1219 * It can be useful on Windows, where more than
1220 * one interface can have the same name.
1222 devnum = strtol(optarg, &end, 10);
1223 if (optarg != end && *end == '\0') {
1225 error("Invalid adapter index");
1227 if (pcap_findalldevs(&devpointer, ebuf) < 0)
1231 * Look for the devnum-th entry
1232 * in the list of devices
1236 i < devnum-1 && devpointer != NULL;
1237 i++, devpointer = devpointer->next)
1239 if (devpointer == NULL)
1240 error("Invalid adapter index");
1242 device = devpointer->name;
1245 #endif /* HAVE_PCAP_FINDALLDEVS */
1249 #ifdef HAVE_PCAP_CREATE
1253 #endif /* HAVE_PCAP_CREATE */
1255 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1257 jflag = pcap_tstamp_type_name_to_val(optarg);
1259 error("invalid time stamp type %s", optarg);
1270 * _IOLBF is the same as _IOFBF in Microsoft's C
1271 * libraries; the only alternative they offer
1274 * XXX - this should really be checking for MSVC++,
1275 * not WIN32, if, for example, MinGW has its own
1276 * C library that is more UNIX-compatible.
1278 setvbuf(stdout, NULL, _IONBF, 0);
1280 #ifdef HAVE_SETLINEBUF
1283 setvbuf(stdout, NULL, _IOLBF, 0);
1294 if (smiLoadModule(optarg) == 0) {
1295 error("could not load MIB module %s", optarg);
1299 (void)fprintf(stderr, "%s: ignoring option `-m %s' ",
1300 program_name, optarg);
1301 (void)fprintf(stderr, "(no libsmi support)\n");
1306 /* TCP-MD5 shared secret */
1307 #ifndef HAVE_LIBCRYPTO
1308 warning("crypto code not compiled in");
1331 ++suppress_default_print;
1334 #ifdef HAVE_PCAP_SETDIRECTION
1336 if (strcasecmp(optarg, "in") == 0)
1338 else if (strcasecmp(optarg, "out") == 0)
1340 else if (strcasecmp(optarg, "inout") == 0)
1341 Qflag = PCAP_D_INOUT;
1343 error("unknown capture direction `%s'", optarg);
1345 #endif /* HAVE_PCAP_SETDIRECTION */
1356 snaplen = strtol(optarg, &end, 0);
1357 if (optarg == end || *end != '\0'
1358 || snaplen < 0 || snaplen > MAXIMUM_SNAPLEN)
1359 error("invalid snaplen %s", optarg);
1360 else if (snaplen == 0)
1361 snaplen = MAXIMUM_SNAPLEN;
1373 if (strcasecmp(optarg, "vat") == 0)
1374 packettype = PT_VAT;
1375 else if (strcasecmp(optarg, "wb") == 0)
1377 else if (strcasecmp(optarg, "rpc") == 0)
1378 packettype = PT_RPC;
1379 else if (strcasecmp(optarg, "rtp") == 0)
1380 packettype = PT_RTP;
1381 else if (strcasecmp(optarg, "rtcp") == 0)
1382 packettype = PT_RTCP;
1383 else if (strcasecmp(optarg, "snmp") == 0)
1384 packettype = PT_SNMP;
1385 else if (strcasecmp(optarg, "cnfp") == 0)
1386 packettype = PT_CNFP;
1387 else if (strcasecmp(optarg, "tftp") == 0)
1388 packettype = PT_TFTP;
1389 else if (strcasecmp(optarg, "aodv") == 0)
1390 packettype = PT_AODV;
1391 else if (strcasecmp(optarg, "carp") == 0)
1392 packettype = PT_CARP;
1393 else if (strcasecmp(optarg, "radius") == 0)
1394 packettype = PT_RADIUS;
1395 else if (strcasecmp(optarg, "zmtp1") == 0)
1396 packettype = PT_ZMTP1;
1397 else if (strcasecmp(optarg, "vxlan") == 0)
1398 packettype = PT_VXLAN;
1399 else if (strcasecmp(optarg, "pgm") == 0)
1400 packettype = PT_PGM;
1401 else if (strcasecmp(optarg, "pgm_zmtp1") == 0)
1402 packettype = PT_PGM_ZMTP1;
1403 else if (strcasecmp(optarg, "lmp") == 0)
1404 packettype = PT_LMP;
1406 error("unknown packet type `%s'", optarg);
1413 #ifdef HAVE_PCAP_DUMP_FLUSH
1432 Wflag = atoi(optarg);
1434 error("invalid number of output files %s", optarg);
1435 WflagChars = getWflagChars(Wflag);
1440 ++suppress_default_print;
1445 ++suppress_default_print;
1449 gndo->ndo_dltname = optarg;
1451 pcap_datalink_name_to_val(gndo->ndo_dltname);
1452 if (gndo->ndo_dlt < 0)
1453 error("invalid data link type %s", gndo->ndo_dltname);
1456 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
1459 /* Undocumented flag */
1460 #ifdef HAVE_PCAP_DEBUG
1461 extern int pcap_debug;
1471 zflag = strdup(optarg);
1475 username = strdup(optarg);
1479 gndo->ndo_packet_number = 1;
1482 case OPTION_VERSION:
1487 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1488 case OPTION_TSTAMP_PRECISION:
1489 gndo->ndo_tstamp_precision = tstamp_precision_from_string(optarg);
1490 if (gndo->ndo_tstamp_precision < 0)
1491 error("unsupported time stamp precision");
1495 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1496 case OPTION_IMMEDIATE_MODE:
1497 gndo->ndo_immediate = 1;
1507 #ifdef HAVE_PCAP_FINDALLDEVS
1509 show_devices_and_exit();
1514 case 0: /* Default */
1515 case 4: /* Default + Date*/
1516 thiszone = gmt2local(0);
1519 case 1: /* No time stamp */
1520 case 2: /* Unix timeval style */
1521 case 3: /* Microseconds since previous packet */
1522 case 5: /* Microseconds since first packet */
1525 default: /* Not supported */
1526 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
1530 if (fflag != 0 && (VFileName != NULL || RFileName != NULL))
1531 error("-f can not be used with -V or -r");
1533 if (VFileName != NULL && RFileName != NULL)
1534 error("-V and -r are mutually exclusive.");
1536 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1538 * If we're printing dissected packets to the standard output
1539 * rather than saving raw packets to a file, and the standard
1540 * output is a terminal, use immediate mode, as the user's
1541 * probably expecting to see packets pop up immediately.
1543 if (WFileName == NULL && isatty(1))
1544 gndo->ndo_immediate = 1;
1548 /* if run as root, prepare for chrooting */
1549 if (getuid() == 0 || geteuid() == 0) {
1550 /* future extensibility for cmd-line arguments */
1552 chroot_dir = WITH_CHROOT;
1557 /* if run as root, prepare for dropping root privileges */
1558 if (getuid() == 0 || geteuid() == 0) {
1559 /* Run with '-Z root' to restore old behaviour */
1561 username = WITH_USER;
1565 if (RFileName != NULL || VFileName != NULL) {
1567 * If RFileName is non-null, it's the pathname of a
1568 * savefile to read. If VFileName is non-null, it's
1569 * the pathname of a file containing a list of pathnames
1570 * (one per line) of savefiles to read.
1572 * In either case, we're reading a savefile, not doing
1577 * We don't need network access, so relinquish any set-UID
1578 * or set-GID privileges we have (if any).
1580 * We do *not* want set-UID privileges when opening a
1581 * trace file, as that might let the user read other
1582 * people's trace files (especially if we're set-UID
1585 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
1586 fprintf(stderr, "Warning: setgid/setuid failed !\n");
1588 if (VFileName != NULL) {
1589 if (VFileName[0] == '-' && VFileName[1] == '\0')
1592 VFile = fopen(VFileName, "r");
1595 error("Unable to open file: %s\n", strerror(errno));
1597 ret = get_next_file(VFile, VFileLine);
1599 error("Nothing in %s\n", VFileName);
1600 RFileName = VFileLine;
1603 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1604 pd = pcap_open_offline_with_tstamp_precision(RFileName,
1605 gndo->ndo_tstamp_precision, ebuf);
1607 pd = pcap_open_offline(RFileName, ebuf);
1613 cap_rights_init(&rights, CAP_READ);
1614 if (cap_rights_limit(fileno(pcap_file(pd)), &rights) < 0 &&
1616 error("unable to limit pcap descriptor");
1619 dlt = pcap_datalink(pd);
1620 dlt_name = pcap_datalink_val_to_name(dlt);
1621 if (dlt_name == NULL) {
1622 fprintf(stderr, "reading from file %s, link-type %u\n",
1626 "reading from file %s, link-type %s (%s)\n",
1627 RFileName, dlt_name,
1628 pcap_datalink_val_to_description(dlt));
1632 * We're doing a live capture.
1634 if (device == NULL) {
1635 device = pcap_lookupdev(ebuf);
1641 * Print a message to the standard error on Windows.
1642 * XXX - why do it here, with a different message?
1644 if(strlen(device) == 1) /* we assume that an ASCII string is always longer than 1 char */
1645 { /* a Unicode string has a \0 as second byte (so strlen() is 1) */
1646 fprintf(stderr, "%s: listening on %ws\n", program_name, device);
1650 fprintf(stderr, "%s: listening on %s\n", program_name, device);
1655 #ifdef HAVE_PCAP_CREATE
1656 pd = pcap_create(device, ebuf);
1659 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1661 show_tstamp_types_and_exit(device, pd);
1663 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1664 status = pcap_set_tstamp_precision(pd, gndo->ndo_tstamp_precision);
1666 error("%s: Can't set %ssecond time stamp precision: %s",
1668 tstamp_precision_to_string(gndo->ndo_tstamp_precision),
1669 pcap_statustostr(status));
1672 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1673 if (gndo->ndo_immediate) {
1674 status = pcap_set_immediate_mode(pd, 1);
1676 error("%s: Can't set immediate mode: %s",
1678 pcap_statustostr(status));
1682 * Is this an interface that supports monitor mode?
1684 if (pcap_can_set_rfmon(pd) == 1)
1685 supports_monitor_mode = 1;
1687 supports_monitor_mode = 0;
1688 status = pcap_set_snaplen(pd, snaplen);
1690 error("%s: Can't set snapshot length: %s",
1691 device, pcap_statustostr(status));
1692 status = pcap_set_promisc(pd, !pflag);
1694 error("%s: Can't set promiscuous mode: %s",
1695 device, pcap_statustostr(status));
1697 status = pcap_set_rfmon(pd, 1);
1699 error("%s: Can't set monitor mode: %s",
1700 device, pcap_statustostr(status));
1702 status = pcap_set_timeout(pd, 1000);
1704 error("%s: pcap_set_timeout failed: %s",
1705 device, pcap_statustostr(status));
1707 status = pcap_set_buffer_size(pd, Bflag);
1709 error("%s: Can't set buffer size: %s",
1710 device, pcap_statustostr(status));
1712 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1714 status = pcap_set_tstamp_type(pd, jflag);
1716 error("%s: Can't set time stamp type: %s",
1717 device, pcap_statustostr(status));
1720 status = pcap_activate(pd);
1723 * pcap_activate() failed.
1725 cp = pcap_geterr(pd);
1726 if (status == PCAP_ERROR)
1728 else if ((status == PCAP_ERROR_NO_SUCH_DEVICE ||
1729 status == PCAP_ERROR_PERM_DENIED) &&
1731 error("%s: %s\n(%s)", device,
1732 pcap_statustostr(status), cp);
1734 else if (status == PCAP_ERROR_RFMON_NOTSUP &&
1735 strncmp(device, "wlan", 4) == 0) {
1736 char parent[8], newdev[8];
1738 size_t s = sizeof(parent);
1740 snprintf(sysctl, sizeof(sysctl),
1741 "net.wlan.%d.%%parent", atoi(device + 4));
1742 sysctlbyname(sysctl, parent, &s, NULL, 0);
1743 strlcpy(newdev, device, sizeof(newdev));
1744 /* Suggest a new wlan device. */
1745 newdev[strlen(newdev)-1]++;
1746 error("%s is not a monitor mode VAP\n"
1747 "To create a new monitor mode VAP use:\n"
1748 " ifconfig %s create wlandev %s wlanmode "
1749 "monitor\nand use %s as the tcpdump "
1750 "interface", device, newdev, parent,
1755 error("%s: %s", device,
1756 pcap_statustostr(status));
1757 } else if (status > 0) {
1759 * pcap_activate() succeeded, but it's warning us
1760 * of a problem it had.
1762 cp = pcap_geterr(pd);
1763 if (status == PCAP_WARNING)
1765 else if (status == PCAP_WARNING_PROMISC_NOTSUP &&
1767 warning("%s: %s\n(%s)", device,
1768 pcap_statustostr(status), cp);
1770 warning("%s: %s", device,
1771 pcap_statustostr(status));
1773 #ifdef HAVE_PCAP_SETDIRECTION
1775 status = pcap_setdirection(pd, Qflag);
1777 error("%s: pcap_setdirection() failed: %s",
1778 device, pcap_geterr(pd));
1780 #endif /* HAVE_PCAP_SETDIRECTION */
1783 pd = pcap_open_live(device, snaplen, !pflag, 1000, ebuf);
1787 warning("%s", ebuf);
1788 #endif /* HAVE_PCAP_CREATE */
1790 * Let user own process after socket has been opened.
1793 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
1794 fprintf(stderr, "Warning: setgid/setuid failed !\n");
1796 #if !defined(HAVE_PCAP_CREATE) && defined(WIN32)
1798 if(pcap_setbuff(pd, Bflag)==-1){
1799 error("%s", pcap_geterr(pd));
1801 #endif /* !defined(HAVE_PCAP_CREATE) && defined(WIN32) */
1803 show_dlts_and_exit(device, pd);
1804 if (gndo->ndo_dlt >= 0) {
1805 #ifdef HAVE_PCAP_SET_DATALINK
1806 if (pcap_set_datalink(pd, gndo->ndo_dlt) < 0)
1807 error("%s", pcap_geterr(pd));
1810 * We don't actually support changing the
1811 * data link type, so we only let them
1812 * set it to what it already is.
1814 if (gndo->ndo_dlt != pcap_datalink(pd)) {
1815 error("%s is not one of the DLTs supported by this device\n",
1819 (void)fprintf(stderr, "%s: data link type %s\n",
1820 program_name, gndo->ndo_dltname);
1821 (void)fflush(stderr);
1823 i = pcap_snapshot(pd);
1825 warning("snaplen raised from %d to %d", snaplen, i);
1829 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
1830 warning("foreign (-f) flag used but: %s", ebuf);
1836 cmdbuf = read_infile(infile);
1838 cmdbuf = copy_argv(&argv[optind]);
1840 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
1841 error("%s", pcap_geterr(pd));
1843 bpf_dump(&fcode, dflag);
1851 capdns = capdns_setup();
1852 #endif /* HAVE_CASPER */
1854 init_addrtoname(gndo, localnet, netmask);
1858 (void)setsignal(SIGPIPE, cleanup);
1859 (void)setsignal(SIGTERM, cleanup);
1860 (void)setsignal(SIGINT, cleanup);
1862 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1863 (void)setsignal(SIGCHLD, child_cleanup);
1865 /* Cooperate with nohup(1) */
1867 if ((oldhandler = setsignal(SIGHUP, cleanup)) != SIG_DFL)
1868 (void)setsignal(SIGHUP, oldhandler);
1873 * If a user name was specified with "-Z", attempt to switch to
1874 * that user's UID. This would probably be used with sudo,
1875 * to allow tcpdump to be run in a special restricted
1876 * account (if you just want to allow users to open capture
1877 * devices, and can't just give users that permission,
1878 * you'd make tcpdump set-UID or set-GID).
1880 * Tcpdump doesn't necessarily write only to one savefile;
1881 * the general only way to allow a -Z instance to write to
1882 * savefiles as the user under whose UID it's run, rather
1883 * than as the user specified with -Z, would thus be to switch
1884 * to the original user ID before opening a capture file and
1885 * then switch back to the -Z user ID after opening the savefile.
1886 * Switching to the -Z user ID only after opening the first
1887 * savefile doesn't handle the general case.
1890 if (getuid() == 0 || geteuid() == 0) {
1891 #ifdef HAVE_LIBCAP_NG
1892 /* Initialize capng */
1893 capng_clear(CAPNG_SELECT_BOTH);
1897 CAPNG_PERMITTED | CAPNG_EFFECTIVE,
1906 CAPNG_PERMITTED | CAPNG_EFFECTIVE,
1910 capng_apply(CAPNG_SELECT_BOTH);
1911 #endif /* HAVE_LIBCAP_NG */
1912 if (username || chroot_dir)
1913 droproot(username, chroot_dir);
1918 if (pcap_setfilter(pd, &fcode) < 0)
1919 error("%s", pcap_geterr(pd));
1921 if (RFileName == NULL && VFileName == NULL) {
1922 static const unsigned long cmds[] = { BIOCGSTATS, BIOCROTZBUF };
1925 * The various libpcap devices use a combination of
1926 * read (bpf), ioctl (bpf, netmap), poll (netmap).
1927 * Grant the relevant access rights, sorted by name.
1929 cap_rights_init(&rights, CAP_EVENT, CAP_IOCTL, CAP_READ);
1930 if (cap_rights_limit(pcap_fileno(pd), &rights) < 0 &&
1932 error("unable to limit pcap descriptor");
1934 if (cap_ioctls_limit(pcap_fileno(pd), cmds,
1935 sizeof(cmds) / sizeof(cmds[0])) < 0 && errno != ENOSYS) {
1936 error("unable to limit ioctls on pcap descriptor");
1942 /* Do not exceed the default PATH_MAX for files. */
1943 dumpinfo.CurrentFileName = (char *)malloc(PATH_MAX + 1);
1945 if (dumpinfo.CurrentFileName == NULL)
1946 error("malloc of dumpinfo.CurrentFileName");
1948 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1950 MakeFilename(dumpinfo.CurrentFileName, WFileName, 0, WflagChars);
1952 MakeFilename(dumpinfo.CurrentFileName, WFileName, 0, 0);
1954 p = pcap_dump_open(pd, dumpinfo.CurrentFileName);
1955 #ifdef HAVE_LIBCAP_NG
1956 /* Give up CAP_DAC_OVERRIDE capability.
1957 * Only allow it to be restored if the -C or -G flag have been
1958 * set since we may need to create more files later on.
1962 (Cflag || Gflag ? 0 : CAPNG_PERMITTED)
1966 capng_apply(CAPNG_SELECT_BOTH);
1967 #endif /* HAVE_LIBCAP_NG */
1969 error("%s", pcap_geterr(pd));
1971 set_dumper_capsicum_rights(p);
1973 if (Cflag != 0 || Gflag != 0) {
1975 dumpinfo.WFileName = strdup(basename(WFileName));
1976 dumpinfo.dirfd = open(dirname(WFileName),
1977 O_DIRECTORY | O_RDONLY);
1978 if (dumpinfo.dirfd < 0) {
1979 error("unable to open directory %s",
1980 dirname(WFileName));
1982 cap_rights_init(&rights, CAP_CREATE, CAP_FCNTL,
1983 CAP_FTRUNCATE, CAP_LOOKUP, CAP_SEEK, CAP_WRITE);
1984 if (cap_rights_limit(dumpinfo.dirfd, &rights) < 0 &&
1986 error("unable to limit directory rights");
1988 if (cap_fcntls_limit(dumpinfo.dirfd, CAP_FCNTL_GETFL) < 0 &&
1990 error("unable to limit dump descriptor fcntls");
1992 #else /* !__FreeBSD__ */
1993 dumpinfo.WFileName = WFileName;
1995 callback = dump_packet_and_trunc;
1998 pcap_userdata = (u_char *)&dumpinfo;
2000 callback = dump_packet;
2001 pcap_userdata = (u_char *)p;
2003 #ifdef HAVE_PCAP_DUMP_FLUSH
2008 type = pcap_datalink(pd);
2009 printinfo = get_print_info(type);
2010 callback = print_packet;
2011 pcap_userdata = (u_char *)&printinfo;
2014 #ifdef SIGNAL_REQ_INFO
2016 * We can't get statistics when reading from a file rather
2017 * than capturing from a device.
2019 if (RFileName == NULL)
2020 (void)setsignal(SIGNAL_REQ_INFO, requestinfo);
2023 if (vflag > 0 && WFileName) {
2025 * When capturing to a file, "-v" means tcpdump should,
2026 * every 10 secodns, "v"erbosely report the number of
2029 #ifdef USE_WIN32_MM_TIMER
2030 /* call verbose_stats_dump() each 1000 +/-100msec */
2031 timer_id = timeSetEvent(1000, 100, verbose_stats_dump, 0, TIME_PERIODIC);
2032 setvbuf(stderr, NULL, _IONBF, 0);
2033 #elif defined(HAVE_ALARM)
2034 (void)setsignal(SIGALRM, verbose_stats_dump);
2040 if (RFileName == NULL) {
2042 * Live capture (if -V was specified, we set RFileName
2043 * to a file from the -V file). Print a message to
2044 * the standard error on UN*X.
2046 if (!vflag && !WFileName) {
2047 (void)fprintf(stderr,
2048 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
2051 (void)fprintf(stderr, "%s: ", program_name);
2052 dlt = pcap_datalink(pd);
2053 dlt_name = pcap_datalink_val_to_name(dlt);
2054 if (dlt_name == NULL) {
2055 (void)fprintf(stderr, "listening on %s, link-type %u, capture size %u bytes\n",
2056 device, dlt, snaplen);
2058 (void)fprintf(stderr, "listening on %s, link-type %s (%s), capture size %u bytes\n",
2060 pcap_datalink_val_to_description(dlt), snaplen);
2062 (void)fflush(stderr);
2067 cansandbox = (VFileName == NULL && zflag == NULL);
2069 cansandbox = (cansandbox && (nflag || capdns != NULL));
2071 cansandbox = (cansandbox && nflag);
2073 if (cansandbox && cap_enter() < 0 && errno != ENOSYS)
2074 error("unable to enter the capability mode");
2075 #endif /* __FreeBSD__ */
2078 status = pcap_loop(pd, cnt, callback, pcap_userdata);
2079 if (WFileName == NULL) {
2081 * We're printing packets. Flush the printed output,
2082 * so it doesn't get intermingled with error output.
2086 * We got interrupted, so perhaps we didn't
2087 * manage to finish a line we were printing.
2088 * Print an extra newline, just in case.
2092 (void)fflush(stdout);
2096 * We got interrupted. If we are reading multiple
2097 * files (via -V) set these so that we stop.
2106 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
2107 program_name, pcap_geterr(pd));
2109 if (RFileName == NULL) {
2111 * We're doing a live capture. Report the capture
2117 if (VFileName != NULL) {
2118 ret = get_next_file(VFile, VFileLine);
2120 RFileName = VFileLine;
2121 pd = pcap_open_offline(RFileName, ebuf);
2125 cap_rights_init(&rights, CAP_READ);
2126 if (cap_rights_limit(fileno(pcap_file(pd)),
2127 &rights) < 0 && errno != ENOSYS) {
2128 error("unable to limit pcap descriptor");
2131 new_dlt = pcap_datalink(pd);
2132 if (WFileName && new_dlt != dlt)
2133 error("%s: new dlt does not match original", RFileName);
2134 printinfo = get_print_info(new_dlt);
2135 dlt_name = pcap_datalink_val_to_name(new_dlt);
2136 if (dlt_name == NULL) {
2137 fprintf(stderr, "reading from file %s, link-type %u\n",
2138 RFileName, new_dlt);
2141 "reading from file %s, link-type %s (%s)\n",
2142 RFileName, dlt_name,
2143 pcap_datalink_val_to_description(new_dlt));
2145 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
2146 error("%s", pcap_geterr(pd));
2147 if (pcap_setfilter(pd, &fcode) < 0)
2148 error("%s", pcap_geterr(pd));
2152 while (ret != NULL);
2155 exit(status == -1 ? 1 : 0);
2158 /* make a clean exit on interrupts */
2160 cleanup(int signo _U_)
2162 #ifdef USE_WIN32_MM_TIMER
2164 timeKillEvent(timer_id);
2166 #elif defined(HAVE_ALARM)
2170 #ifdef HAVE_PCAP_BREAKLOOP
2172 * We have "pcap_breakloop()"; use it, so that we do as little
2173 * as possible in the signal handler (it's probably not safe
2174 * to do anything with standard I/O streams in a signal handler -
2175 * the ANSI C standard doesn't say it is).
2180 * We don't have "pcap_breakloop()"; this isn't safe, but
2181 * it's the best we can do. Print the summary if we're
2182 * not reading from a savefile - i.e., if we're doing a
2183 * live capture - and exit.
2185 if (pd != NULL && pcap_file(pd) == NULL) {
2187 * We got interrupted, so perhaps we didn't
2188 * manage to finish a line we were printing.
2189 * Print an extra newline, just in case.
2192 (void)fflush(stdout);
2200 On windows, we do not use a fork, so we do not care less about
2201 waiting a child processes to die
2203 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
2205 child_cleanup(int signo _U_)
2209 #endif /* HAVE_FORK && HAVE_VFORK */
2212 info(register int verbose)
2214 struct pcap_stat stat;
2217 * Older versions of libpcap didn't set ps_ifdrop on some
2218 * platforms; initialize it to 0 to handle that.
2221 if (pcap_stats(pd, &stat) < 0) {
2222 (void)fprintf(stderr, "pcap_stats: %s\n", pcap_geterr(pd));
2228 fprintf(stderr, "%s: ", program_name);
2230 (void)fprintf(stderr, "%u packet%s captured", packets_captured,
2231 PLURAL_SUFFIX(packets_captured));
2233 fputs(", ", stderr);
2236 (void)fprintf(stderr, "%u packet%s received by filter", stat.ps_recv,
2237 PLURAL_SUFFIX(stat.ps_recv));
2239 fputs(", ", stderr);
2242 (void)fprintf(stderr, "%u packet%s dropped by kernel", stat.ps_drop,
2243 PLURAL_SUFFIX(stat.ps_drop));
2244 if (stat.ps_ifdrop != 0) {
2246 fputs(", ", stderr);
2249 (void)fprintf(stderr, "%u packet%s dropped by interface\n",
2250 stat.ps_ifdrop, PLURAL_SUFFIX(stat.ps_ifdrop));
2256 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
2258 compress_savefile(const char *filename)
2267 * Set to lowest priority so that this doesn't disturb the capture
2270 setpriority(PRIO_PROCESS, 0, NZERO - 1);
2272 setpriority(PRIO_PROCESS, 0, 19);
2274 if (execlp(zflag, zflag, filename, (char *)NULL) == -1)
2276 "compress_savefile:execlp(%s, %s): %s\n",
2286 #else /* HAVE_FORK && HAVE_VFORK */
2288 compress_savefile(const char *filename)
2291 "compress_savefile failed. Functionality not implemented under your system\n");
2293 #endif /* HAVE_FORK && HAVE_VFORK */
2296 dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
2298 struct dump_info *dump_info;
2304 dump_info = (struct dump_info *)user;
2307 * XXX - this won't force the file to rotate on the specified time
2308 * boundary, but it will rotate on the first packet received after the
2309 * specified Gflag number of seconds. Note: if a Gflag time boundary
2310 * and a Cflag size boundary coincide, the time rotation will occur
2311 * first thereby cancelling the Cflag boundary (since the file should
2315 /* Check if it is time to rotate */
2318 /* Get the current time */
2319 if ((t = time(NULL)) == (time_t)-1) {
2320 error("dump_and_trunc_packet: can't get current_time: %s",
2321 pcap_strerror(errno));
2325 /* If the time is greater than the specified window, rotate */
2326 if (t - Gflag_time >= Gflag) {
2332 /* Update the Gflag_time */
2334 /* Update Gflag_count */
2337 * Close the current file and open a new one.
2339 pcap_dump_close(dump_info->p);
2342 * Compress the file we just closed, if the user asked for it
2345 compress_savefile(dump_info->CurrentFileName);
2348 * Check to see if we've exceeded the Wflag (when
2351 if (Cflag == 0 && Wflag > 0 && Gflag_count >= Wflag) {
2352 (void)fprintf(stderr, "Maximum file limit reached: %d\n",
2357 if (dump_info->CurrentFileName != NULL)
2358 free(dump_info->CurrentFileName);
2359 /* Allocate space for max filename + \0. */
2360 dump_info->CurrentFileName = (char *)malloc(PATH_MAX + 1);
2361 if (dump_info->CurrentFileName == NULL)
2362 error("dump_packet_and_trunc: malloc");
2364 * Gflag was set otherwise we wouldn't be here. Reset the count
2365 * so multiple files would end with 1,2,3 in the filename.
2366 * The counting is handled with the -C flow after this.
2371 * This is always the first file in the Cflag
2373 * We also don't need numbering if Cflag is not set.
2376 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, 0,
2379 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, 0, 0);
2381 #ifdef HAVE_LIBCAP_NG
2382 capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
2383 capng_apply(CAPNG_SELECT_BOTH);
2384 #endif /* HAVE_LIBCAP_NG */
2386 fd = openat(dump_info->dirfd,
2387 dump_info->CurrentFileName,
2388 O_CREAT | O_WRONLY | O_TRUNC, 0644);
2390 error("unable to open file %s",
2391 dump_info->CurrentFileName);
2393 fp = fdopen(fd, "w");
2395 error("unable to fdopen file %s",
2396 dump_info->CurrentFileName);
2398 dump_info->p = pcap_dump_fopen(dump_info->pd, fp);
2399 #else /* !__FreeBSD__ */
2400 dump_info->p = pcap_dump_open(dump_info->pd, dump_info->CurrentFileName);
2402 #ifdef HAVE_LIBCAP_NG
2403 capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
2404 capng_apply(CAPNG_SELECT_BOTH);
2405 #endif /* HAVE_LIBCAP_NG */
2406 if (dump_info->p == NULL)
2407 error("%s", pcap_geterr(pd));
2409 set_dumper_capsicum_rights(dump_info->p);
2415 * XXX - this won't prevent capture files from getting
2416 * larger than Cflag - the last packet written to the
2417 * file could put it over Cflag.
2420 long size = pcap_dump_ftell(dump_info->p);
2423 error("ftell fails on output file");
2431 * Close the current file and open a new one.
2433 pcap_dump_close(dump_info->p);
2436 * Compress the file we just closed, if the user
2440 compress_savefile(dump_info->CurrentFileName);
2444 if (Cflag_count >= Wflag)
2447 if (dump_info->CurrentFileName != NULL)
2448 free(dump_info->CurrentFileName);
2449 dump_info->CurrentFileName = (char *)malloc(PATH_MAX + 1);
2450 if (dump_info->CurrentFileName == NULL)
2451 error("dump_packet_and_trunc: malloc");
2452 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, Cflag_count, WflagChars);
2453 #ifdef HAVE_LIBCAP_NG
2454 capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
2455 capng_apply(CAPNG_SELECT_BOTH);
2456 #endif /* HAVE_LIBCAP_NG */
2458 fd = openat(dump_info->dirfd, dump_info->CurrentFileName,
2459 O_CREAT | O_WRONLY | O_TRUNC, 0644);
2461 error("unable to open file %s",
2462 dump_info->CurrentFileName);
2464 fp = fdopen(fd, "w");
2466 error("unable to fdopen file %s",
2467 dump_info->CurrentFileName);
2469 dump_info->p = pcap_dump_fopen(dump_info->pd, fp);
2470 #else /* !__FreeBSD__ */
2471 dump_info->p = pcap_dump_open(dump_info->pd, dump_info->CurrentFileName);
2473 #ifdef HAVE_LIBCAP_NG
2474 capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
2475 capng_apply(CAPNG_SELECT_BOTH);
2476 #endif /* HAVE_LIBCAP_NG */
2477 if (dump_info->p == NULL)
2478 error("%s", pcap_geterr(pd));
2480 set_dumper_capsicum_rights(dump_info->p);
2485 pcap_dump((u_char *)dump_info->p, h, sp);
2486 #ifdef HAVE_PCAP_DUMP_FLUSH
2488 pcap_dump_flush(dump_info->p);
2497 dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
2503 pcap_dump(user, h, sp);
2504 #ifdef HAVE_PCAP_DUMP_FLUSH
2506 pcap_dump_flush((pcap_dumper_t *)user);
2515 print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
2517 struct print_info *print_info;
2519 netdissect_options *ndo;
2525 print_info = (struct print_info *)user;
2526 ndo = print_info->ndo;
2528 if(ndo->ndo_packet_number)
2529 ND_PRINT((ndo, "%5u ", packets_captured));
2531 ts_print(ndo, &h->ts);
2534 * Some printers want to check that they're not walking off the
2535 * end of the packet.
2536 * Rather than pass it all the way down, we set this member
2537 * of the netdissect_options structure.
2539 ndo->ndo_snapend = sp + h->caplen;
2541 if(print_info->ndo_type) {
2542 hdrlen = (*print_info->p.ndo_printer)(print_info->ndo, h, sp);
2544 hdrlen = (*print_info->p.printer)(h, sp);
2548 * Restore the original snapend, as a printer might have
2551 ndo->ndo_snapend = sp + h->caplen;
2552 if (ndo->ndo_Xflag) {
2554 * Print the raw packet data in hex and ASCII.
2556 if (ndo->ndo_Xflag > 1) {
2558 * Include the link-layer header.
2560 hex_and_ascii_print(ndo, "\n\t", sp, h->caplen);
2563 * Don't include the link-layer header - and if
2564 * we have nothing past the link-layer header,
2567 if (h->caplen > hdrlen)
2568 hex_and_ascii_print(ndo, "\n\t", sp + hdrlen,
2569 h->caplen - hdrlen);
2571 } else if (ndo->ndo_xflag) {
2573 * Print the raw packet data in hex.
2575 if (ndo->ndo_xflag > 1) {
2577 * Include the link-layer header.
2579 hex_print(ndo, "\n\t", sp, h->caplen);
2582 * Don't include the link-layer header - and if
2583 * we have nothing past the link-layer header,
2586 if (h->caplen > hdrlen)
2587 hex_print(ndo, "\n\t", sp + hdrlen,
2588 h->caplen - hdrlen);
2590 } else if (ndo->ndo_Aflag) {
2592 * Print the raw packet data in ASCII.
2594 if (ndo->ndo_Aflag > 1) {
2596 * Include the link-layer header.
2598 ascii_print(ndo, sp, h->caplen);
2601 * Don't include the link-layer header - and if
2602 * we have nothing past the link-layer header,
2605 if (h->caplen > hdrlen)
2606 ascii_print(ndo, sp + hdrlen, h->caplen - hdrlen);
2619 * XXX - there should really be libpcap calls to get the version
2620 * number as a string (the string would be generated from #defines
2621 * at run time, so that it's not generated from string constants
2622 * in the library, as, on many UNIX systems, those constants would
2623 * be statically linked into the application executable image, and
2624 * would thus reflect the version of libpcap on the system on
2625 * which the application was *linked*, not the system on which it's
2628 * That routine should be documented, unlike the "version[]"
2629 * string, so that UNIX vendors providing their own libpcaps
2630 * don't omit it (as a couple of vendors have...).
2632 * Packet.dll should perhaps also export a routine to return the
2633 * version number of the Packet.dll code, to supply the
2634 * "Wpcap_version" information on Windows.
2636 char WDversion[]="current-git.tcpdump.org";
2637 #if !defined(HAVE_GENERATED_VERSION)
2638 char version[]="current-git.tcpdump.org";
2640 char pcap_version[]="current-git.tcpdump.org";
2641 char Wpcap_version[]="3.1";
2645 * By default, print the specified data out in hex and ASCII.
2648 ndo_default_print(netdissect_options *ndo, const u_char *bp, u_int length)
2650 hex_and_ascii_print(ndo, "\n\t", bp, length); /* pass on lf and indentation string */
2654 default_print(const u_char *bp, u_int length)
2656 ndo_default_print(gndo, bp, length);
2659 #ifdef SIGNAL_REQ_INFO
2660 RETSIGTYPE requestinfo(int signo _U_)
2670 * Called once each second in verbose mode while dumping to file
2672 #ifdef USE_WIN32_MM_TIMER
2673 void CALLBACK verbose_stats_dump (UINT timer_id _U_, UINT msg _U_, DWORD_PTR arg _U_,
2674 DWORD_PTR dw1 _U_, DWORD_PTR dw2 _U_)
2676 struct pcap_stat stat;
2678 if (infodelay == 0 && pcap_stats(pd, &stat) >= 0)
2679 fprintf(stderr, "Got %u\r", packets_captured);
2681 #elif defined(HAVE_ALARM)
2682 static void verbose_stats_dump(int sig _U_)
2684 struct pcap_stat stat;
2686 if (infodelay == 0 && pcap_stats(pd, &stat) >= 0)
2687 fprintf(stderr, "Got %u\r", packets_captured);
2692 USES_APPLE_DEPRECATED_API
2696 extern char version[];
2697 #ifndef HAVE_PCAP_LIB_VERSION
2698 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
2699 extern char pcap_version[];
2700 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2701 static char pcap_version[] = "unknown";
2702 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2703 #endif /* HAVE_PCAP_LIB_VERSION */
2705 #ifdef HAVE_PCAP_LIB_VERSION
2707 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
2709 (void)fprintf(stderr, "%s version %s\n", program_name, version);
2711 (void)fprintf(stderr, "%s\n",pcap_lib_version());
2712 #else /* HAVE_PCAP_LIB_VERSION */
2714 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
2715 (void)fprintf(stderr, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version, pcap_version);
2717 (void)fprintf(stderr, "%s version %s\n", program_name, version);
2718 (void)fprintf(stderr, "libpcap version %s\n", pcap_version);
2720 #endif /* HAVE_PCAP_LIB_VERSION */
2722 #if defined(HAVE_LIBCRYPTO) && defined(SSLEAY_VERSION)
2723 (void)fprintf (stderr, "%s\n", SSLeay_version(SSLEAY_VERSION));
2727 (void)fprintf (stderr, "SMI-library: %s\n", smi_version_string);
2736 (void)fprintf(stderr,
2737 "Usage: %s [-aAbd" D_FLAG "efhH" I_FLAG J_FLAG "KlLnNOpqRStu" U_FLAG "vxX#]" B_FLAG_USAGE " [ -c count ]\n", program_name);
2738 (void)fprintf(stderr,
2739 "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n");
2740 (void)fprintf(stderr,
2741 "\t\t[ -i interface ]" j_FLAG_USAGE " [ -M secret ] [ --number ]\n");
2742 #ifdef HAVE_PCAP_SETDIRECTION
2743 (void)fprintf(stderr,
2744 "\t\t[ -Q in|out|inout ]\n");
2746 (void)fprintf(stderr,
2747 "\t\t[ -r file ] [ -s snaplen ] ");
2748 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
2749 (void)fprintf(stderr, "[ --time-stamp-precision precision ]\n");
2750 (void)fprintf(stderr,
2753 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
2754 (void)fprintf(stderr, "[ --immediate-mode ] ");
2756 (void)fprintf(stderr, "[ -T type ] [ --version ] [ -V file ]\n");
2757 (void)fprintf(stderr,
2758 "\t\t[ -w file ] [ -W filecount ] [ -y datalinktype ] [ -z command ]\n");
2759 (void)fprintf(stderr,
2760 "\t\t[ -Z user ] [ expression ]\n");
2767 ndo_error(netdissect_options *ndo _U_, const char *fmt, ...)
2771 (void)fprintf(stderr, "%s: ", program_name);
2773 (void)vfprintf(stderr, fmt, ap);
2777 if (fmt[-1] != '\n')
2778 (void)fputc('\n', stderr);
2786 ndo_warning(netdissect_options *ndo _U_, const char *fmt, ...)
2790 (void)fprintf(stderr, "%s: WARNING: ", program_name);
2792 (void)vfprintf(stderr, fmt, ap);
2796 if (fmt[-1] != '\n')
2797 (void)fputc('\n', stderr);
2802 * c-style: whitesmith