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";
35 * tcpdump - monitor tcp/ip traffic on an ethernet.
37 * First written in 1987 by Van Jacobson, Lawrence Berkeley Laboratory.
38 * Mercilessly hacked and occasionally improved since then via the
39 * combined efforts of Van, Steve McCanne and Craig Leres of LBL.
47 * Mac OS X may ship pcap.h from libpcap 0.6 with a libpcap based on
48 * 0.8. That means it has pcap_findalldevs() but the header doesn't
49 * define pcap_if_t, meaning that we can't actually *use* pcap_findalldevs().
51 #ifdef HAVE_PCAP_FINDALLDEVS
52 #ifndef HAVE_PCAP_IF_T
53 #undef HAVE_PCAP_FINDALLDEVS
57 #include <tcpdump-stdinc.h>
61 extern int strcasecmp (const char *__s1
, const char *__s2
);
72 #include <openssl/crypto.h>
75 #ifdef HAVE_GETOPT_LONG
78 #include "getopt_long.h"
80 /* Capsicum-specific code requires macros from <net/bpf.h>, which will fail
81 * to compile if <pcap.h> has already been included; including the headers
82 * in the opposite order works fine.
85 #include <sys/capability.h>
86 #include <sys/ioccom.h>
90 #endif /* HAVE_CAPSICUM */
99 #include <sys/resource.h>
104 /* capabilities convenience library */
105 /* If a code depends on HAVE_LIBCAP_NG, it depends also on HAVE_CAP_NG_H.
106 * If HAVE_CAP_NG_H is not defined, undefine HAVE_LIBCAP_NG.
107 * Thus, the later tests are done only on HAVE_LIBCAP_NG.
109 #ifdef HAVE_LIBCAP_NG
113 #undef HAVE_LIBCAP_NG
114 #endif /* HAVE_CAP_NG_H */
115 #endif /* HAVE_LIBCAP_NG */
117 #include "netdissect.h"
118 #include "interface.h"
119 #include "addrtoname.h"
121 #include "setsignal.h"
122 #include "gmt2local.h"
123 #include "pcap-missing.h"
126 #define PATH_MAX 1024
130 #define SIGNAL_REQ_INFO SIGINFO
132 #define SIGNAL_REQ_INFO SIGUSR1
135 netdissect_options Gndo
;
136 netdissect_options
*gndo
= &Gndo
;
138 static int Dflag
; /* list available devices and exit */
139 static int dflag
; /* print filter code */
140 static int Lflag
; /* list available data link types and exit */
141 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
142 static int Jflag
; /* list available time stamp types */
144 #ifdef HAVE_PCAP_SETDIRECTION
145 int Qflag
= -1; /* restrict captured packet by send/receive direction */
147 static char *zflag
= NULL
; /* compress each savefile using a specified command (like gzip or bzip2) */
149 static int infodelay
;
150 static int infoprint
;
154 int32_t thiszone
; /* seconds offset from gmt to local time */
157 static RETSIGTYPE
cleanup(int);
158 static RETSIGTYPE
child_cleanup(int);
159 static void print_version(void);
160 static void print_usage(void);
161 static void show_dlts_and_exit(const char *device
, pcap_t
*pd
) __attribute__((noreturn
));
163 static void print_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
164 static void ndo_default_print(netdissect_options
*, const u_char
*, u_int
);
165 static void dump_packet_and_trunc(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
166 static void dump_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
167 static void droproot(const char *, const char *);
168 static void ndo_error(netdissect_options
*ndo
, const char *fmt
, ...)
169 __attribute__((noreturn
))
170 #ifdef __ATTRIBUTE___FORMAT_OK
171 __attribute__((format (printf
, 2, 3)))
172 #endif /* __ATTRIBUTE___FORMAT_OK */
174 static void ndo_warning(netdissect_options
*ndo
, const char *fmt
, ...)
175 #ifdef __ATTRIBUTE___FORMAT_OK
176 __attribute__((format (printf
, 2, 3)))
177 #endif /* __ATTRIBUTE___FORMAT_OK */
180 #ifdef SIGNAL_REQ_INFO
181 RETSIGTYPE
requestinfo(int);
184 #if defined(USE_WIN32_MM_TIMER)
185 #include <MMsystem.h>
186 static UINT timer_id
;
187 static void CALLBACK
verbose_stats_dump(UINT
, UINT
, DWORD_PTR
, DWORD_PTR
, DWORD_PTR
);
188 #elif defined(HAVE_ALARM)
189 static void verbose_stats_dump(int sig
);
192 static void info(int);
193 static u_int packets_captured
;
201 static const struct printer printers
[] = {
202 { ether_if_print
, DLT_EN10MB
},
204 { ipnet_if_print
, DLT_IPNET
},
206 #ifdef DLT_IEEE802_15_4
207 { ieee802_15_4_if_print
, DLT_IEEE802_15_4
},
209 #ifdef DLT_IEEE802_15_4_NOFCS
210 { ieee802_15_4_if_print
, DLT_IEEE802_15_4_NOFCS
},
213 { ppi_if_print
, DLT_PPI
},
215 #ifdef DLT_NETANALYZER
216 { netanalyzer_if_print
, DLT_NETANALYZER
},
218 #ifdef DLT_NETANALYZER_TRANSPARENT
219 { netanalyzer_transparent_if_print
, DLT_NETANALYZER_TRANSPARENT
},
221 #if defined(DLT_NFLOG) && defined(HAVE_PCAP_NFLOG_H)
222 { nflog_if_print
, DLT_NFLOG
},
225 { cip_if_print
, DLT_CIP
},
228 { cip_if_print
, DLT_ATM_CLIP
},
230 #ifdef DLT_IP_OVER_FC
231 { ipfc_if_print
, DLT_IP_OVER_FC
},
233 { null_if_print
, DLT_NULL
},
235 { null_if_print
, DLT_LOOP
},
237 #ifdef DLT_APPLE_IP_OVER_IEEE1394
238 { ap1394_if_print
, DLT_APPLE_IP_OVER_IEEE1394
},
240 #if defined(DLT_BLUETOOTH_HCI_H4_WITH_PHDR) && defined(HAVE_PCAP_BLUETOOTH_H)
241 { bt_if_print
, DLT_BLUETOOTH_HCI_H4_WITH_PHDR
},
244 { lane_if_print
, DLT_LANE8023
},
246 { arcnet_if_print
, DLT_ARCNET
},
247 #ifdef DLT_ARCNET_LINUX
248 { arcnet_linux_if_print
, DLT_ARCNET_LINUX
},
250 { raw_if_print
, DLT_RAW
},
252 { raw_if_print
, DLT_IPV4
},
255 { raw_if_print
, DLT_IPV6
},
257 #ifdef HAVE_PCAP_USB_H
259 { usb_linux_48_byte_print
, DLT_USB_LINUX
},
260 #endif /* DLT_USB_LINUX */
261 #ifdef DLT_USB_LINUX_MMAPPED
262 { usb_linux_64_byte_print
, DLT_USB_LINUX_MMAPPED
},
263 #endif /* DLT_USB_LINUX_MMAPPED */
264 #endif /* HAVE_PCAP_USB_H */
265 #ifdef DLT_SYMANTEC_FIREWALL
266 { symantec_if_print
, DLT_SYMANTEC_FIREWALL
},
269 { chdlc_if_print
, DLT_C_HDLC
},
272 { chdlc_if_print
, DLT_HDLC
},
275 { pppoe_if_print
, DLT_PPP_ETHER
},
277 #if defined(DLT_PFLOG) && defined(HAVE_NET_PFVAR_H)
278 { pflog_if_print
, DLT_PFLOG
},
280 { token_if_print
, DLT_IEEE802
},
281 { fddi_if_print
, DLT_FDDI
},
283 { sll_if_print
, DLT_LINUX_SLL
},
286 { fr_if_print
, DLT_FR
},
289 { fr_if_print
, DLT_FRELAY
},
292 { mfr_if_print
, DLT_MFR
},
294 { atm_if_print
, DLT_ATM_RFC1483
},
296 { sunatm_if_print
, DLT_SUNATM
},
299 { enc_if_print
, DLT_ENC
},
301 { sl_if_print
, DLT_SLIP
},
302 #ifdef DLT_SLIP_BSDOS
303 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
306 { ltalk_if_print
, DLT_LTALK
},
308 #ifdef DLT_JUNIPER_ATM1
309 { juniper_atm1_print
, DLT_JUNIPER_ATM1
},
311 #ifdef DLT_JUNIPER_ATM2
312 { juniper_atm2_print
, DLT_JUNIPER_ATM2
},
314 #ifdef DLT_JUNIPER_MFR
315 { juniper_mfr_print
, DLT_JUNIPER_MFR
},
317 #ifdef DLT_JUNIPER_MLFR
318 { juniper_mlfr_print
, DLT_JUNIPER_MLFR
},
320 #ifdef DLT_JUNIPER_MLPPP
321 { juniper_mlppp_print
, DLT_JUNIPER_MLPPP
},
323 #ifdef DLT_JUNIPER_PPPOE
324 { juniper_pppoe_print
, DLT_JUNIPER_PPPOE
},
326 #ifdef DLT_JUNIPER_PPPOE_ATM
327 { juniper_pppoe_atm_print
, DLT_JUNIPER_PPPOE_ATM
},
329 #ifdef DLT_JUNIPER_GGSN
330 { juniper_ggsn_print
, DLT_JUNIPER_GGSN
},
332 #ifdef DLT_JUNIPER_ES
333 { juniper_es_print
, DLT_JUNIPER_ES
},
335 #ifdef DLT_JUNIPER_MONITOR
336 { juniper_monitor_print
, DLT_JUNIPER_MONITOR
},
338 #ifdef DLT_JUNIPER_SERVICES
339 { juniper_services_print
, DLT_JUNIPER_SERVICES
},
341 #ifdef DLT_JUNIPER_ETHER
342 { juniper_ether_print
, DLT_JUNIPER_ETHER
},
344 #ifdef DLT_JUNIPER_PPP
345 { juniper_ppp_print
, DLT_JUNIPER_PPP
},
347 #ifdef DLT_JUNIPER_FRELAY
348 { juniper_frelay_print
, DLT_JUNIPER_FRELAY
},
350 #ifdef DLT_JUNIPER_CHDLC
351 { juniper_chdlc_print
, DLT_JUNIPER_CHDLC
},
354 { pktap_if_print
, DLT_PKTAP
},
356 #ifdef DLT_IEEE802_11_RADIO
357 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
359 #ifdef DLT_IEEE802_11
360 { ieee802_11_if_print
, DLT_IEEE802_11
},
362 #ifdef DLT_IEEE802_11_RADIO_AVS
363 { ieee802_11_radio_avs_if_print
, DLT_IEEE802_11_RADIO_AVS
},
365 #ifdef DLT_PRISM_HEADER
366 { prism_if_print
, DLT_PRISM_HEADER
},
368 { ppp_if_print
, DLT_PPP
},
369 #ifdef DLT_PPP_WITHDIRECTION
370 { ppp_if_print
, DLT_PPP_WITHDIRECTION
},
373 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
375 #ifdef DLT_PPP_SERIAL
376 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
381 static const struct tok status_flags
[] = {
383 { PCAP_IF_UP
, "Up" },
385 #ifdef PCAP_IF_RUNNING
386 { PCAP_IF_RUNNING
, "Running" },
388 { PCAP_IF_LOOPBACK
, "Loopback" },
393 lookup_printer(int type
)
395 const struct printer
*p
;
397 for (p
= printers
; p
->f
; ++p
)
401 #if defined(DLT_USER2) && defined(DLT_PKTAP)
403 * Apple incorrectly chose to use DLT_USER2 for their PKTAP
406 * We map DLT_PKTAP, whether it's DLT_USER2 as it is on Darwin-
407 * based OSes or the same value as LINKTYPE_PKTAP as it is on
408 * other OSes, to LINKTYPE_PKTAP, so files written with
409 * this version of libpcap for a DLT_PKTAP capture have a link-
410 * layer header type of LINKTYPE_PKTAP.
412 * However, files written on OS X Mavericks for a DLT_PKTAP
413 * capture have a link-layer header type of LINKTYPE_USER2.
414 * If we don't have a printer for DLT_USER2, and type is
415 * DLT_USER2, we look up the printer for DLT_PKTAP and use
418 if (type
== DLT_USER2
) {
419 for (p
= printers
; p
->f
; ++p
)
420 if (DLT_PKTAP
== p
->type
)
431 static int supports_monitor_mode
;
438 netdissect_options
*ndo
;
444 char *CurrentFileName
;
452 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
454 show_tstamp_types_and_exit(const char *device
, pcap_t
*pd
)
457 int *tstamp_types
= 0;
458 const char *tstamp_type_name
;
461 n_tstamp_types
= pcap_list_tstamp_types(pd
, &tstamp_types
);
462 if (n_tstamp_types
< 0)
463 error("%s", pcap_geterr(pd
));
465 if (n_tstamp_types
== 0) {
466 fprintf(stderr
, "Time stamp type cannot be set for %s\n",
470 fprintf(stderr
, "Time stamp types for %s (use option -j to set):\n",
472 for (i
= 0; i
< n_tstamp_types
; i
++) {
473 tstamp_type_name
= pcap_tstamp_type_val_to_name(tstamp_types
[i
]);
474 if (tstamp_type_name
!= NULL
) {
475 (void) fprintf(stderr
, " %s (%s)\n", tstamp_type_name
,
476 pcap_tstamp_type_val_to_description(tstamp_types
[i
]));
478 (void) fprintf(stderr
, " %d\n", tstamp_types
[i
]);
481 pcap_free_tstamp_types(tstamp_types
);
487 show_dlts_and_exit(const char *device
, pcap_t
*pd
)
491 const char *dlt_name
;
493 n_dlts
= pcap_list_datalinks(pd
, &dlts
);
495 error("%s", pcap_geterr(pd
));
496 else if (n_dlts
== 0 || !dlts
)
497 error("No data link types.");
500 * If the interface is known to support monitor mode, indicate
501 * whether these are the data link types available when not in
502 * monitor mode, if -I wasn't specified, or when in monitor mode,
503 * when -I was specified (the link-layer types available in
504 * monitor mode might be different from the ones available when
505 * not in monitor mode).
507 if (supports_monitor_mode
)
508 (void) fprintf(stderr
, "Data link types for %s %s (use option -y to set):\n",
510 Iflag
? "when in monitor mode" : "when not in monitor mode");
512 (void) fprintf(stderr
, "Data link types for %s (use option -y to set):\n",
515 while (--n_dlts
>= 0) {
516 dlt_name
= pcap_datalink_val_to_name(dlts
[n_dlts
]);
517 if (dlt_name
!= NULL
) {
518 (void) fprintf(stderr
, " %s (%s)", dlt_name
,
519 pcap_datalink_val_to_description(dlts
[n_dlts
]));
522 * OK, does tcpdump handle that type?
524 if (lookup_printer(dlts
[n_dlts
]) == NULL
)
525 (void) fprintf(stderr
, " (printing not supported)");
526 fprintf(stderr
, "\n");
528 (void) fprintf(stderr
, " DLT %d (printing not supported)\n",
532 #ifdef HAVE_PCAP_FREE_DATALINKS
533 pcap_free_datalinks(dlts
);
538 #ifdef HAVE_PCAP_FINDALLDEVS
540 show_devices_and_exit (void)
542 pcap_if_t
*devpointer
;
543 char ebuf
[PCAP_ERRBUF_SIZE
];
546 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
549 for (i
= 0; devpointer
!= NULL
; i
++) {
550 printf("%d.%s", i
+1, devpointer
->name
);
551 if (devpointer
->description
!= NULL
)
552 printf(" (%s)", devpointer
->description
);
553 if (devpointer
->flags
!= 0)
554 printf(" [%s]", bittok2str(status_flags
, "none", devpointer
->flags
));
556 devpointer
= devpointer
->next
;
561 #endif /* HAVE_PCAP_FINDALLDEVS */
566 * Note that there we use all letters for short options except for g, k,
567 * o, and P, and those are used by other versions of tcpdump, and we should
568 * only use them for the same purposes that the other versions of tcpdump
571 * OS X tcpdump uses -g to force non--v output for IP to be on one
572 * line, making it more "g"repable;
574 * OS X tcpdump uses -k tospecify that packet comments in pcap-ng files
577 * OpenBSD tcpdump uses -o to indicate that OS fingerprinting should be done
578 * for hosts sending TCP SYN packets;
580 * OS X tcpdump uses -P to indicate that -w should write pcap-ng rather
583 * OS X tcpdump also uses -Q to specify expressions that match packet
584 * metadata, including but not limited to the packet direction.
585 * The expression syntax is different from a simple "in|out|inout",
586 * and those expressions aren't accepted by OS X tcpdump, but the
587 * equivalents would be "in" = "dir=in", "out" = "dir=out", and
588 * "inout" = "dir=in or dir=out", and the parser could conceivably
589 * special-case "in", "out", and "inout" as expressions for backwards
590 * compatibility, so all is not (yet) lost.
594 * Set up flags that might or might not be supported depending on the
595 * version of libpcap we're using.
597 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
599 #define B_FLAG_USAGE " [ -B size ]"
600 #else /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
603 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
605 #ifdef HAVE_PCAP_CREATE
607 #else /* HAVE_PCAP_CREATE */
609 #endif /* HAVE_PCAP_CREATE */
611 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
613 #define j_FLAG_USAGE " [ -j tstamptype ]"
615 #else /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
619 #endif /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
621 #ifdef HAVE_PCAP_FINDALLDEVS
627 #ifdef HAVE_PCAP_DUMP_FLUSH
633 #ifdef HAVE_PCAP_SETDIRECTION
639 #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:#"
644 * We do not currently have long options corresponding to all short
645 * options; we should probably pick appropriate option names for them.
647 * However, the short options where the number of times the option is
648 * specified matters, such as -v and -d and -t, should probably not
649 * just map to a long option, as saying
651 * tcpdump --verbose --verbose
653 * doesn't make sense; it should be --verbosity={N} or something such
656 * For long options with no corresponding short options, we define values
657 * outside the range of ASCII graphic characters, make that the last
658 * component of the entry for the long option, and have a case for that
659 * option in the switch statement.
661 #define OPTION_VERSION 128
662 #define OPTION_TSTAMP_PRECISION 129
663 #define OPTION_IMMEDIATE_MODE 130
665 static const struct option longopts
[] = {
666 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
667 { "buffer-size", required_argument
, NULL
, 'B' },
669 { "list-interfaces", no_argument
, NULL
, 'D' },
670 { "help", no_argument
, NULL
, 'h' },
671 { "interface", required_argument
, NULL
, 'i' },
672 #ifdef HAVE_PCAP_CREATE
673 { "monitor-mode", no_argument
, NULL
, 'I' },
675 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
676 { "time-stamp-type", required_argument
, NULL
, 'j' },
677 { "list-time-stamp-types", no_argument
, NULL
, 'J' },
679 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
680 { "time-stamp-precision", required_argument
, NULL
, OPTION_TSTAMP_PRECISION
},
682 { "dont-verify-checksums", no_argument
, NULL
, 'K' },
683 { "list-data-link-types", no_argument
, NULL
, 'L' },
684 { "no-optimize", no_argument
, NULL
, 'O' },
685 { "no-promiscuous-mode", no_argument
, NULL
, 'p' },
686 #ifdef HAVE_PCAP_SETDIRECTION
687 { "direction", required_argument
, NULL
, 'Q' },
689 { "snapshot-length", required_argument
, NULL
, 's' },
690 { "absolute-tcp-sequence-numbers", no_argument
, NULL
, 'S' },
691 #ifdef HAVE_PCAP_DUMP_FLUSH
692 { "packet-buffered", no_argument
, NULL
, 'U' },
694 { "linktype", required_argument
, NULL
, 'y' },
695 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
696 { "immediate-mode", no_argument
, NULL
, OPTION_IMMEDIATE_MODE
},
698 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
699 { "debug-filter-parser", no_argument
, NULL
, 'Y' },
701 { "relinquish-privileges", required_argument
, NULL
, 'Z' },
702 { "number", no_argument
, NULL
, '#' },
703 { "version", no_argument
, NULL
, OPTION_VERSION
},
708 /* Drop root privileges and chroot if necessary */
710 droproot(const char *username
, const char *chroot_dir
)
712 struct passwd
*pw
= NULL
;
714 if (chroot_dir
&& !username
) {
715 fprintf(stderr
, "tcpdump: Chroot without dropping root is insecure\n");
719 pw
= getpwnam(username
);
722 if (chroot(chroot_dir
) != 0 || chdir ("/") != 0) {
723 fprintf(stderr
, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
724 chroot_dir
, pcap_strerror(errno
));
728 #ifdef HAVE_LIBCAP_NG
729 int ret
= capng_change_id(pw
->pw_uid
, pw
->pw_gid
, CAPNG_NO_FLAG
);
731 fprintf(stderr
, "error : ret %d\n", ret
);
734 fprintf(stderr
, "dropped privs to %s\n", username
);
737 if (initgroups(pw
->pw_name
, pw
->pw_gid
) != 0 ||
738 setgid(pw
->pw_gid
) != 0 || setuid(pw
->pw_uid
) != 0) {
739 fprintf(stderr
, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
741 (unsigned long)pw
->pw_uid
,
742 (unsigned long)pw
->pw_gid
,
743 pcap_strerror(errno
));
747 fprintf(stderr
, "dropped privs to %s\n", username
);
749 #endif /* HAVE_LIBCAP_NG */
752 fprintf(stderr
, "tcpdump: Couldn't find user '%.32s'\n",
756 #ifdef HAVE_LIBCAP_NG
757 /* We don't need CAP_SETUID and CAP_SETGID any more. */
760 CAPNG_EFFECTIVE
| CAPNG_PERMITTED
,
764 capng_apply(CAPNG_SELECT_BOTH
);
765 #endif /* HAVE_LIBCAP_NG */
786 MakeFilename(char *buffer
, char *orig_name
, int cnt
, int max_chars
)
788 char *filename
= malloc(PATH_MAX
+ 1);
789 if (filename
== NULL
)
790 error("Makefilename: malloc");
792 /* Process with strftime if Gflag is set. */
796 /* Convert Gflag_time to a usable format */
797 if ((local_tm
= localtime(&Gflag_time
)) == NULL
) {
798 error("MakeTimedFilename: localtime");
801 /* There's no good way to detect an error in strftime since a return
802 * value of 0 isn't necessarily failure.
804 strftime(filename
, PATH_MAX
, orig_name
, local_tm
);
806 strncpy(filename
, orig_name
, PATH_MAX
);
809 if (cnt
== 0 && max_chars
== 0)
810 strncpy(buffer
, filename
, PATH_MAX
+ 1);
812 if (snprintf(buffer
, PATH_MAX
+ 1, "%s%0*d", filename
, max_chars
, cnt
) > PATH_MAX
)
813 /* Report an error if the filename is too large */
814 error("too many output files or filename is too long (> %d)", PATH_MAX
);
818 static int tcpdump_printf(netdissect_options
*ndo _U_
,
819 const char *fmt
, ...)
826 ret
=vfprintf(stdout
, fmt
, args
);
832 static struct print_info
833 get_print_info(int type
)
835 struct print_info printinfo
;
837 printinfo
.ndo
= gndo
;
838 printinfo
.printer
= lookup_printer(type
);
839 if (printinfo
.printer
== NULL
) {
840 gndo
->ndo_dltname
= pcap_datalink_val_to_name(type
);
841 if (gndo
->ndo_dltname
!= NULL
)
842 error("packet printing is not supported for link type %s: use -w",
845 error("packet printing is not supported for link type %d: use -w", type
);
851 get_next_file(FILE *VFile
, char *ptr
)
855 ret
= fgets(ptr
, PATH_MAX
, VFile
);
859 if (ptr
[strlen(ptr
) - 1] == '\n')
860 ptr
[strlen(ptr
) - 1] = '\0';
865 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
867 tstamp_precision_from_string(const char *precision
)
869 if (strncmp(precision
, "nano", strlen("nano")) == 0)
870 return PCAP_TSTAMP_PRECISION_NANO
;
872 if (strncmp(precision
, "micro", strlen("micro")) == 0)
873 return PCAP_TSTAMP_PRECISION_MICRO
;
879 tstamp_precision_to_string(int precision
)
883 case PCAP_TSTAMP_PRECISION_MICRO
:
886 case PCAP_TSTAMP_PRECISION_NANO
:
897 * Ensure that, on a dump file's descriptor, we have all the rights
898 * necessary to make the standard I/O library work with an fdopen()ed
899 * FILE * from that descriptor.
901 * A long time ago, in a galaxy far far away, AT&T decided that, instead
902 * of providing separate APIs for getting and setting the FD_ flags on a
903 * descriptor, getting and setting the O_ flags on a descriptor, and
904 * locking files, they'd throw them all into a kitchen-sink fcntl() call
905 * along the lines of ioctl(), the fact that ioctl() operations are
906 * largely specific to particular character devices but fcntl() operations
907 * are either generic to all descriptors or generic to all descriptors for
908 * regular files nonwithstanding.
910 * The Capsicum people decided that fine-grained control of descriptor
911 * operations was required, so that you need to grant permission for
912 * reading, writing, seeking, and fcntl-ing. The latter, courtesy of
913 * AT&T's decision, means that "fcntl-ing" isn't a thing, but a motley
914 * collection of things, so there are *individual* fcntls for which
915 * permission needs to be granted.
917 * The FreeBSD standard I/O people implemented some optimizations that
918 * requires that the standard I/O routines be able to determine whether
919 * the descriptor for the FILE * is open append-only or not; as that
920 * descriptor could have come from an open() rather than an fopen(),
921 * that requires that it be able to do an F_GETFL fcntl() to read
924 * Tcpdump uses ftell() to determine how much data has been written
925 * to a file in order to, when used with -C, determine when it's time
926 * to rotate capture files. ftell() therefore needs to do an lseek()
927 * to find out the file offset and must, thanks to the aforementioned
928 * optimization, also know whether the descriptor is open append-only
931 * The net result of all the above is that we need to grant CAP_SEEK,
932 * CAP_WRITE, and CAP_FCNTL with the CAP_FCNTL_GETFL subcapability.
934 * Perhaps this is the universe's way of saying that either
936 * 1) there needs to be an fopenat() call and a pcap_dump_openat() call
937 * using it, so that Capsicum-capable tcpdump wouldn't need to do
942 * 2) there needs to be a cap_fdopen() call in the FreeBSD standard
943 * I/O library that knows what rights are needed by the standard
944 * I/O library, based on the open mode, and assigns them, perhaps
945 * with an additional argument indicating, for example, whether
946 * seeking should be allowed, so that tcpdump doesn't need to know
947 * what the standard I/O library happens to require this week.
950 set_dumper_capsicum_rights(pcap_dumper_t
*p
)
952 int fd
= fileno(pcap_dump_file(p
));
955 cap_rights_init(&rights
, CAP_SEEK
, CAP_WRITE
, CAP_FCNTL
);
956 if (cap_rights_limit(fd
, &rights
) < 0 && errno
!= ENOSYS
) {
957 error("unable to limit dump descriptor");
959 if (cap_fcntls_limit(fd
, CAP_FCNTL_GETFL
) < 0 && errno
!= ENOSYS
) {
960 error("unable to limit dump descriptor fcntls");
966 main(int argc
, char **argv
)
968 register int cnt
, op
, i
;
969 bpf_u_int32 localnet
=0 , netmask
= 0;
970 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *VFileName
, *WFileName
;
971 pcap_handler callback
;
975 const char *dlt_name
;
976 struct bpf_program fcode
;
978 RETSIGTYPE (*oldhandler
)(int);
980 struct print_info printinfo
;
981 struct dump_info dumpinfo
;
982 u_char
*pcap_userdata
;
983 char ebuf
[PCAP_ERRBUF_SIZE
];
984 char VFileLine
[PATH_MAX
+ 1];
985 char *username
= NULL
;
986 char *chroot_dir
= NULL
;
989 #ifdef HAVE_PCAP_FINDALLDEVS
990 pcap_if_t
*devpointer
;
998 #endif /* HAVE_CAPSICUM */
1001 if(wsockinit() != 0) return 1;
1004 jflag
=-1; /* not set */
1008 gndo
->ndo_default_print
=ndo_default_print
;
1009 gndo
->ndo_printf
=tcpdump_printf
;
1010 gndo
->ndo_error
=ndo_error
;
1011 gndo
->ndo_warning
=ndo_warning
;
1012 gndo
->ndo_snaplen
= DEFAULT_SNAPLEN
;
1013 gndo
->ndo_immediate
= 0;
1023 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
1024 program_name
= cp
+ 1;
1026 program_name
= argv
[0];
1029 * On platforms where the CPU doesn't support unaligned loads,
1030 * force unaligned accesses to abort with SIGBUS, rather than
1031 * being fixed up (slowly) by the OS kernel; on those platforms,
1032 * misaligned accesses are bugs, and we want tcpdump to crash so
1033 * that the bugs are reported.
1035 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
1043 (op
= getopt_long(argc
, argv
, SHORTOPTS
, longopts
, NULL
)) != -1)
1047 /* compatibility for old -a */
1058 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
1060 Bflag
= atoi(optarg
)*1024;
1062 error("invalid packet buffer size %s", optarg
);
1064 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
1069 error("invalid packet count %s", optarg
);
1073 Cflag
= atoi(optarg
) * 1000000;
1075 error("invalid file size %s", optarg
);
1095 #ifndef HAVE_LIBCRYPTO
1096 warning("crypto code not compiled in");
1098 gndo
->ndo_espsecret
= optarg
;
1110 Gflag
= atoi(optarg
);
1112 error("invalid number of seconds %s", optarg
);
1114 /* We will create one file initially. */
1117 /* Grab the current time for rotation use. */
1118 if ((Gflag_time
= time(NULL
)) == (time_t)-1) {
1119 error("main: can't get current time: %s",
1120 pcap_strerror(errno
));
1134 if (optarg
[0] == '0' && optarg
[1] == 0)
1135 error("Invalid adapter index");
1137 #ifdef HAVE_PCAP_FINDALLDEVS
1139 * If the argument is a number, treat it as
1140 * an index into the list of adapters, as
1141 * printed by "tcpdump -D".
1143 * This should be OK on UNIX systems, as interfaces
1144 * shouldn't have names that begin with digits.
1145 * It can be useful on Windows, where more than
1146 * one interface can have the same name.
1148 devnum
= strtol(optarg
, &end
, 10);
1149 if (optarg
!= end
&& *end
== '\0') {
1151 error("Invalid adapter index");
1153 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
1157 * Look for the devnum-th entry
1158 * in the list of devices
1162 i
< devnum
-1 && devpointer
!= NULL
;
1163 i
++, devpointer
= devpointer
->next
)
1165 if (devpointer
== NULL
)
1166 error("Invalid adapter index");
1168 device
= devpointer
->name
;
1171 #endif /* HAVE_PCAP_FINDALLDEVS */
1175 #ifdef HAVE_PCAP_CREATE
1179 #endif /* HAVE_PCAP_CREATE */
1181 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1183 jflag
= pcap_tstamp_type_name_to_val(optarg
);
1185 error("invalid time stamp type %s", optarg
);
1196 * _IOLBF is the same as _IOFBF in Microsoft's C
1197 * libraries; the only alternative they offer
1200 * XXX - this should really be checking for MSVC++,
1201 * not WIN32, if, for example, MinGW has its own
1202 * C library that is more UNIX-compatible.
1204 setvbuf(stdout
, NULL
, _IONBF
, 0);
1206 #ifdef HAVE_SETLINEBUF
1209 setvbuf(stdout
, NULL
, _IOLBF
, 0);
1220 if (smiLoadModule(optarg
) == 0) {
1221 error("could not load MIB module %s", optarg
);
1225 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
1226 program_name
, optarg
);
1227 (void)fprintf(stderr
, "(no libsmi support)\n");
1232 /* TCP-MD5 shared secret */
1233 #ifndef HAVE_LIBCRYPTO
1234 warning("crypto code not compiled in");
1257 ++suppress_default_print
;
1260 #ifdef HAVE_PCAP_SETDIRECTION
1262 if (strcasecmp(optarg
, "in") == 0)
1264 else if (strcasecmp(optarg
, "out") == 0)
1266 else if (strcasecmp(optarg
, "inout") == 0)
1267 Qflag
= PCAP_D_INOUT
;
1269 error("unknown capture direction `%s'", optarg
);
1271 #endif /* HAVE_PCAP_SETDIRECTION */
1282 snaplen
= strtol(optarg
, &end
, 0);
1283 if (optarg
== end
|| *end
!= '\0'
1284 || snaplen
< 0 || snaplen
> MAXIMUM_SNAPLEN
)
1285 error("invalid snaplen %s", optarg
);
1286 else if (snaplen
== 0)
1287 snaplen
= MAXIMUM_SNAPLEN
;
1299 if (strcasecmp(optarg
, "vat") == 0)
1300 packettype
= PT_VAT
;
1301 else if (strcasecmp(optarg
, "wb") == 0)
1303 else if (strcasecmp(optarg
, "rpc") == 0)
1304 packettype
= PT_RPC
;
1305 else if (strcasecmp(optarg
, "rtp") == 0)
1306 packettype
= PT_RTP
;
1307 else if (strcasecmp(optarg
, "rtcp") == 0)
1308 packettype
= PT_RTCP
;
1309 else if (strcasecmp(optarg
, "snmp") == 0)
1310 packettype
= PT_SNMP
;
1311 else if (strcasecmp(optarg
, "cnfp") == 0)
1312 packettype
= PT_CNFP
;
1313 else if (strcasecmp(optarg
, "tftp") == 0)
1314 packettype
= PT_TFTP
;
1315 else if (strcasecmp(optarg
, "aodv") == 0)
1316 packettype
= PT_AODV
;
1317 else if (strcasecmp(optarg
, "carp") == 0)
1318 packettype
= PT_CARP
;
1319 else if (strcasecmp(optarg
, "radius") == 0)
1320 packettype
= PT_RADIUS
;
1321 else if (strcasecmp(optarg
, "zmtp1") == 0)
1322 packettype
= PT_ZMTP1
;
1323 else if (strcasecmp(optarg
, "vxlan") == 0)
1324 packettype
= PT_VXLAN
;
1325 else if (strcasecmp(optarg
, "pgm") == 0)
1326 packettype
= PT_PGM
;
1327 else if (strcasecmp(optarg
, "pgm_zmtp1") == 0)
1328 packettype
= PT_PGM_ZMTP1
;
1329 else if (strcasecmp(optarg
, "lmp") == 0)
1330 packettype
= PT_LMP
;
1332 error("unknown packet type `%s'", optarg
);
1339 #ifdef HAVE_PCAP_DUMP_FLUSH
1358 Wflag
= atoi(optarg
);
1360 error("invalid number of output files %s", optarg
);
1361 WflagChars
= getWflagChars(Wflag
);
1366 ++suppress_default_print
;
1371 ++suppress_default_print
;
1375 gndo
->ndo_dltname
= optarg
;
1377 pcap_datalink_name_to_val(gndo
->ndo_dltname
);
1378 if (gndo
->ndo_dlt
< 0)
1379 error("invalid data link type %s", gndo
->ndo_dltname
);
1382 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
1385 /* Undocumented flag */
1386 #ifdef HAVE_PCAP_DEBUG
1387 extern int pcap_debug
;
1397 zflag
= strdup(optarg
);
1401 username
= strdup(optarg
);
1405 gndo
->ndo_packet_number
= 1;
1408 case OPTION_VERSION
:
1413 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1414 case OPTION_TSTAMP_PRECISION
:
1415 gndo
->ndo_tstamp_precision
= tstamp_precision_from_string(optarg
);
1416 if (gndo
->ndo_tstamp_precision
< 0)
1417 error("unsupported time stamp precision");
1421 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1422 case OPTION_IMMEDIATE_MODE
:
1423 gndo
->ndo_immediate
= 1;
1433 #ifdef HAVE_PCAP_FINDALLDEVS
1435 show_devices_and_exit();
1440 case 0: /* Default */
1441 case 4: /* Default + Date*/
1442 thiszone
= gmt2local(0);
1445 case 1: /* No time stamp */
1446 case 2: /* Unix timeval style */
1447 case 3: /* Microseconds since previous packet */
1448 case 5: /* Microseconds since first packet */
1451 default: /* Not supported */
1452 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
1456 if (fflag
!= 0 && (VFileName
!= NULL
|| RFileName
!= NULL
))
1457 error("-f can not be used with -V or -r");
1459 if (VFileName
!= NULL
&& RFileName
!= NULL
)
1460 error("-V and -r are mutually exclusive.");
1462 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1464 * If we're printing dissected packets to the standard output
1465 * rather than saving raw packets to a file, and the standard
1466 * output is a terminal, use immediate mode, as the user's
1467 * probably expecting to see packets pop up immediately.
1469 if (WFileName
== NULL
&& isatty(1))
1470 gndo
->ndo_immediate
= 1;
1474 /* if run as root, prepare for chrooting */
1475 if (getuid() == 0 || geteuid() == 0) {
1476 /* future extensibility for cmd-line arguments */
1478 chroot_dir
= WITH_CHROOT
;
1483 /* if run as root, prepare for dropping root privileges */
1484 if (getuid() == 0 || geteuid() == 0) {
1485 /* Run with '-Z root' to restore old behaviour */
1487 username
= WITH_USER
;
1491 if (RFileName
!= NULL
|| VFileName
!= NULL
) {
1493 * If RFileName is non-null, it's the pathname of a
1494 * savefile to read. If VFileName is non-null, it's
1495 * the pathname of a file containing a list of pathnames
1496 * (one per line) of savefiles to read.
1498 * In either case, we're reading a savefile, not doing
1503 * We don't need network access, so relinquish any set-UID
1504 * or set-GID privileges we have (if any).
1506 * We do *not* want set-UID privileges when opening a
1507 * trace file, as that might let the user read other
1508 * people's trace files (especially if we're set-UID
1511 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
1512 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1514 if (VFileName
!= NULL
) {
1515 if (VFileName
[0] == '-' && VFileName
[1] == '\0')
1518 VFile
= fopen(VFileName
, "r");
1521 error("Unable to open file: %s\n", strerror(errno
));
1523 ret
= get_next_file(VFile
, VFileLine
);
1525 error("Nothing in %s\n", VFileName
);
1526 RFileName
= VFileLine
;
1529 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1530 pd
= pcap_open_offline_with_tstamp_precision(RFileName
,
1531 gndo
->ndo_tstamp_precision
, ebuf
);
1533 pd
= pcap_open_offline(RFileName
, ebuf
);
1538 #ifdef HAVE_CAPSICUM
1539 cap_rights_init(&rights
, CAP_READ
);
1540 if (cap_rights_limit(fileno(pcap_file(pd
)), &rights
) < 0 &&
1542 error("unable to limit pcap descriptor");
1545 dlt
= pcap_datalink(pd
);
1546 dlt_name
= pcap_datalink_val_to_name(dlt
);
1547 if (dlt_name
== NULL
) {
1548 fprintf(stderr
, "reading from file %s, link-type %u\n",
1552 "reading from file %s, link-type %s (%s)\n",
1553 RFileName
, dlt_name
,
1554 pcap_datalink_val_to_description(dlt
));
1558 * We're doing a live capture.
1560 if (device
== NULL
) {
1561 device
= pcap_lookupdev(ebuf
);
1567 * Print a message to the standard error on Windows.
1568 * XXX - why do it here, with a different message?
1570 if(strlen(device
) == 1) /* we assume that an ASCII string is always longer than 1 char */
1571 { /* a Unicode string has a \0 as second byte (so strlen() is 1) */
1572 fprintf(stderr
, "%s: listening on %ws\n", program_name
, device
);
1576 fprintf(stderr
, "%s: listening on %s\n", program_name
, device
);
1581 #ifdef HAVE_PCAP_CREATE
1582 pd
= pcap_create(device
, ebuf
);
1585 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1587 show_tstamp_types_and_exit(device
, pd
);
1589 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1590 status
= pcap_set_tstamp_precision(pd
, gndo
->ndo_tstamp_precision
);
1592 error("%s: Can't set %ssecond time stamp precision: %s",
1594 tstamp_precision_to_string(gndo
->ndo_tstamp_precision
),
1595 pcap_statustostr(status
));
1598 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1599 if (gndo
->ndo_immediate
) {
1600 status
= pcap_set_immediate_mode(pd
, 1);
1602 error("%s: Can't set immediate mode: %s",
1604 pcap_statustostr(status
));
1608 * Is this an interface that supports monitor mode?
1610 if (pcap_can_set_rfmon(pd
) == 1)
1611 supports_monitor_mode
= 1;
1613 supports_monitor_mode
= 0;
1614 status
= pcap_set_snaplen(pd
, snaplen
);
1616 error("%s: Can't set snapshot length: %s",
1617 device
, pcap_statustostr(status
));
1618 status
= pcap_set_promisc(pd
, !pflag
);
1620 error("%s: Can't set promiscuous mode: %s",
1621 device
, pcap_statustostr(status
));
1623 status
= pcap_set_rfmon(pd
, 1);
1625 error("%s: Can't set monitor mode: %s",
1626 device
, pcap_statustostr(status
));
1628 status
= pcap_set_timeout(pd
, 1000);
1630 error("%s: pcap_set_timeout failed: %s",
1631 device
, pcap_statustostr(status
));
1633 status
= pcap_set_buffer_size(pd
, Bflag
);
1635 error("%s: Can't set buffer size: %s",
1636 device
, pcap_statustostr(status
));
1638 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1640 status
= pcap_set_tstamp_type(pd
, jflag
);
1642 error("%s: Can't set time stamp type: %s",
1643 device
, pcap_statustostr(status
));
1646 status
= pcap_activate(pd
);
1649 * pcap_activate() failed.
1651 cp
= pcap_geterr(pd
);
1652 if (status
== PCAP_ERROR
)
1654 else if ((status
== PCAP_ERROR_NO_SUCH_DEVICE
||
1655 status
== PCAP_ERROR_PERM_DENIED
) &&
1657 error("%s: %s\n(%s)", device
,
1658 pcap_statustostr(status
), cp
);
1660 error("%s: %s", device
,
1661 pcap_statustostr(status
));
1662 } else if (status
> 0) {
1664 * pcap_activate() succeeded, but it's warning us
1665 * of a problem it had.
1667 cp
= pcap_geterr(pd
);
1668 if (status
== PCAP_WARNING
)
1670 else if (status
== PCAP_WARNING_PROMISC_NOTSUP
&&
1672 warning("%s: %s\n(%s)", device
,
1673 pcap_statustostr(status
), cp
);
1675 warning("%s: %s", device
,
1676 pcap_statustostr(status
));
1678 #ifdef HAVE_PCAP_SETDIRECTION
1680 status
= pcap_setdirection(pd
, Qflag
);
1682 error("%s: pcap_setdirection() failed: %s",
1683 device
, pcap_geterr(pd
));
1685 #endif /* HAVE_PCAP_SETDIRECTION */
1688 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
1692 warning("%s", ebuf
);
1693 #endif /* HAVE_PCAP_CREATE */
1695 * Let user own process after socket has been opened.
1698 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
1699 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1701 #if !defined(HAVE_PCAP_CREATE) && defined(WIN32)
1703 if(pcap_setbuff(pd
, Bflag
)==-1){
1704 error("%s", pcap_geterr(pd
));
1706 #endif /* !defined(HAVE_PCAP_CREATE) && defined(WIN32) */
1708 show_dlts_and_exit(device
, pd
);
1709 if (gndo
->ndo_dlt
>= 0) {
1710 #ifdef HAVE_PCAP_SET_DATALINK
1711 if (pcap_set_datalink(pd
, gndo
->ndo_dlt
) < 0)
1712 error("%s", pcap_geterr(pd
));
1715 * We don't actually support changing the
1716 * data link type, so we only let them
1717 * set it to what it already is.
1719 if (gndo
->ndo_dlt
!= pcap_datalink(pd
)) {
1720 error("%s is not one of the DLTs supported by this device\n",
1724 (void)fprintf(stderr
, "%s: data link type %s\n",
1725 program_name
, gndo
->ndo_dltname
);
1726 (void)fflush(stderr
);
1728 i
= pcap_snapshot(pd
);
1730 warning("snaplen raised from %d to %d", snaplen
, i
);
1734 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
1735 warning("foreign (-f) flag used but: %s", ebuf
);
1741 cmdbuf
= read_infile(infile
);
1743 cmdbuf
= copy_argv(&argv
[optind
]);
1745 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
1746 error("%s", pcap_geterr(pd
));
1748 bpf_dump(&fcode
, dflag
);
1753 init_addrtoname(gndo
, localnet
, netmask
);
1757 (void)setsignal(SIGPIPE
, cleanup
);
1758 (void)setsignal(SIGTERM
, cleanup
);
1759 (void)setsignal(SIGINT
, cleanup
);
1761 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1762 (void)setsignal(SIGCHLD
, child_cleanup
);
1764 /* Cooperate with nohup(1) */
1766 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
1767 (void)setsignal(SIGHUP
, oldhandler
);
1772 * If a user name was specified with "-Z", attempt to switch to
1773 * that user's UID. This would probably be used with sudo,
1774 * to allow tcpdump to be run in a special restricted
1775 * account (if you just want to allow users to open capture
1776 * devices, and can't just give users that permission,
1777 * you'd make tcpdump set-UID or set-GID).
1779 * Tcpdump doesn't necessarily write only to one savefile;
1780 * the general only way to allow a -Z instance to write to
1781 * savefiles as the user under whose UID it's run, rather
1782 * than as the user specified with -Z, would thus be to switch
1783 * to the original user ID before opening a capture file and
1784 * then switch back to the -Z user ID after opening the savefile.
1785 * Switching to the -Z user ID only after opening the first
1786 * savefile doesn't handle the general case.
1789 if (getuid() == 0 || geteuid() == 0) {
1790 #ifdef HAVE_LIBCAP_NG
1791 /* Initialize capng */
1792 capng_clear(CAPNG_SELECT_BOTH
);
1796 CAPNG_PERMITTED
| CAPNG_EFFECTIVE
,
1805 CAPNG_PERMITTED
| CAPNG_EFFECTIVE
,
1809 capng_apply(CAPNG_SELECT_BOTH
);
1810 #endif /* HAVE_LIBCAP_NG */
1811 if (username
|| chroot_dir
)
1812 droproot(username
, chroot_dir
);
1817 if (pcap_setfilter(pd
, &fcode
) < 0)
1818 error("%s", pcap_geterr(pd
));
1819 #ifdef HAVE_CAPSICUM
1820 if (RFileName
== NULL
&& VFileName
== NULL
) {
1821 static const unsigned long cmds
[] = { BIOCGSTATS
};
1823 cap_rights_init(&rights
, CAP_IOCTL
, CAP_READ
);
1824 if (cap_rights_limit(pcap_fileno(pd
), &rights
) < 0 &&
1826 error("unable to limit pcap descriptor");
1828 if (cap_ioctls_limit(pcap_fileno(pd
), cmds
,
1829 sizeof(cmds
) / sizeof(cmds
[0])) < 0 && errno
!= ENOSYS
) {
1830 error("unable to limit ioctls on pcap descriptor");
1836 /* Do not exceed the default PATH_MAX for files. */
1837 dumpinfo
.CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
1839 if (dumpinfo
.CurrentFileName
== NULL
)
1840 error("malloc of dumpinfo.CurrentFileName");
1842 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1844 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, WflagChars
);
1846 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, 0);
1848 p
= pcap_dump_open(pd
, dumpinfo
.CurrentFileName
);
1849 #ifdef HAVE_LIBCAP_NG
1850 /* Give up CAP_DAC_OVERRIDE capability.
1851 * Only allow it to be restored if the -C or -G flag have been
1852 * set since we may need to create more files later on.
1856 (Cflag
|| Gflag
? 0 : CAPNG_PERMITTED
)
1860 capng_apply(CAPNG_SELECT_BOTH
);
1861 #endif /* HAVE_LIBCAP_NG */
1863 error("%s", pcap_geterr(pd
));
1864 #ifdef HAVE_CAPSICUM
1865 set_dumper_capsicum_rights(p
);
1867 if (Cflag
!= 0 || Gflag
!= 0) {
1868 #ifdef HAVE_CAPSICUM
1869 dumpinfo
.WFileName
= strdup(basename(WFileName
));
1870 dumpinfo
.dirfd
= open(dirname(WFileName
),
1871 O_DIRECTORY
| O_RDONLY
);
1872 if (dumpinfo
.dirfd
< 0) {
1873 error("unable to open directory %s",
1874 dirname(WFileName
));
1876 cap_rights_init(&rights
, CAP_CREATE
, CAP_FCNTL
,
1877 CAP_FTRUNCATE
, CAP_LOOKUP
, CAP_SEEK
, CAP_WRITE
);
1878 if (cap_rights_limit(dumpinfo
.dirfd
, &rights
) < 0 &&
1880 error("unable to limit directory rights");
1882 if (cap_fcntls_limit(dumpinfo
.dirfd
, CAP_FCNTL_GETFL
) < 0 &&
1884 error("unable to limit dump descriptor fcntls");
1886 #else /* !HAVE_CAPSICUM */
1887 dumpinfo
.WFileName
= WFileName
;
1889 callback
= dump_packet_and_trunc
;
1892 pcap_userdata
= (u_char
*)&dumpinfo
;
1894 callback
= dump_packet
;
1895 pcap_userdata
= (u_char
*)p
;
1897 #ifdef HAVE_PCAP_DUMP_FLUSH
1902 type
= pcap_datalink(pd
);
1903 printinfo
= get_print_info(type
);
1904 callback
= print_packet
;
1905 pcap_userdata
= (u_char
*)&printinfo
;
1908 #ifdef SIGNAL_REQ_INFO
1910 * We can't get statistics when reading from a file rather
1911 * than capturing from a device.
1913 if (RFileName
== NULL
)
1914 (void)setsignal(SIGNAL_REQ_INFO
, requestinfo
);
1917 if (vflag
> 0 && WFileName
) {
1919 * When capturing to a file, "-v" means tcpdump should,
1920 * every 10 secodns, "v"erbosely report the number of
1923 #ifdef USE_WIN32_MM_TIMER
1924 /* call verbose_stats_dump() each 1000 +/-100msec */
1925 timer_id
= timeSetEvent(1000, 100, verbose_stats_dump
, 0, TIME_PERIODIC
);
1926 setvbuf(stderr
, NULL
, _IONBF
, 0);
1927 #elif defined(HAVE_ALARM)
1928 (void)setsignal(SIGALRM
, verbose_stats_dump
);
1934 if (RFileName
== NULL
) {
1936 * Live capture (if -V was specified, we set RFileName
1937 * to a file from the -V file). Print a message to
1938 * the standard error on UN*X.
1940 if (!vflag
&& !WFileName
) {
1941 (void)fprintf(stderr
,
1942 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1945 (void)fprintf(stderr
, "%s: ", program_name
);
1946 dlt
= pcap_datalink(pd
);
1947 dlt_name
= pcap_datalink_val_to_name(dlt
);
1948 if (dlt_name
== NULL
) {
1949 (void)fprintf(stderr
, "listening on %s, link-type %u, capture size %u bytes\n",
1950 device
, dlt
, snaplen
);
1952 (void)fprintf(stderr
, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1954 pcap_datalink_val_to_description(dlt
), snaplen
);
1956 (void)fflush(stderr
);
1960 #ifdef HAVE_CAPSICUM
1961 cansandbox
= (nflag
&& VFileName
== NULL
&& zflag
== NULL
);
1962 if (cansandbox
&& cap_enter() < 0 && errno
!= ENOSYS
)
1963 error("unable to enter the capability mode");
1964 if (cap_sandboxed())
1965 fprintf(stderr
, "capability mode sandbox enabled\n");
1966 #endif /* HAVE_CAPSICUM */
1969 status
= pcap_loop(pd
, cnt
, callback
, pcap_userdata
);
1970 if (WFileName
== NULL
) {
1972 * We're printing packets. Flush the printed output,
1973 * so it doesn't get intermingled with error output.
1977 * We got interrupted, so perhaps we didn't
1978 * manage to finish a line we were printing.
1979 * Print an extra newline, just in case.
1983 (void)fflush(stdout
);
1987 * We got interrupted. If we are reading multiple
1988 * files (via -V) set these so that we stop.
1997 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
1998 program_name
, pcap_geterr(pd
));
2000 if (RFileName
== NULL
) {
2002 * We're doing a live capture. Report the capture
2008 if (VFileName
!= NULL
) {
2009 ret
= get_next_file(VFile
, VFileLine
);
2011 RFileName
= VFileLine
;
2012 pd
= pcap_open_offline(RFileName
, ebuf
);
2015 #ifdef HAVE_CAPSICUM
2016 cap_rights_init(&rights
, CAP_READ
);
2017 if (cap_rights_limit(fileno(pcap_file(pd
)),
2018 &rights
) < 0 && errno
!= ENOSYS
) {
2019 error("unable to limit pcap descriptor");
2022 new_dlt
= pcap_datalink(pd
);
2023 if (WFileName
&& new_dlt
!= dlt
)
2024 error("%s: new dlt does not match original", RFileName
);
2025 printinfo
= get_print_info(new_dlt
);
2026 dlt_name
= pcap_datalink_val_to_name(new_dlt
);
2027 if (dlt_name
== NULL
) {
2028 fprintf(stderr
, "reading from file %s, link-type %u\n",
2029 RFileName
, new_dlt
);
2032 "reading from file %s, link-type %s (%s)\n",
2033 RFileName
, dlt_name
,
2034 pcap_datalink_val_to_description(new_dlt
));
2036 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
2037 error("%s", pcap_geterr(pd
));
2038 if (pcap_setfilter(pd
, &fcode
) < 0)
2039 error("%s", pcap_geterr(pd
));
2043 while (ret
!= NULL
);
2046 exit(status
== -1 ? 1 : 0);
2049 /* make a clean exit on interrupts */
2051 cleanup(int signo _U_
)
2053 #ifdef USE_WIN32_MM_TIMER
2055 timeKillEvent(timer_id
);
2057 #elif defined(HAVE_ALARM)
2061 #ifdef HAVE_PCAP_BREAKLOOP
2063 * We have "pcap_breakloop()"; use it, so that we do as little
2064 * as possible in the signal handler (it's probably not safe
2065 * to do anything with standard I/O streams in a signal handler -
2066 * the ANSI C standard doesn't say it is).
2071 * We don't have "pcap_breakloop()"; this isn't safe, but
2072 * it's the best we can do. Print the summary if we're
2073 * not reading from a savefile - i.e., if we're doing a
2074 * live capture - and exit.
2076 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
2078 * We got interrupted, so perhaps we didn't
2079 * manage to finish a line we were printing.
2080 * Print an extra newline, just in case.
2083 (void)fflush(stdout
);
2091 On windows, we do not use a fork, so we do not care less about
2092 waiting a child processes to die
2094 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
2096 child_cleanup(int signo _U_
)
2100 #endif /* HAVE_FORK && HAVE_VFORK */
2103 info(register int verbose
)
2105 struct pcap_stat stat
;
2108 * Older versions of libpcap didn't set ps_ifdrop on some
2109 * platforms; initialize it to 0 to handle that.
2112 if (pcap_stats(pd
, &stat
) < 0) {
2113 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
2119 fprintf(stderr
, "%s: ", program_name
);
2121 (void)fprintf(stderr
, "%u packet%s captured", packets_captured
,
2122 PLURAL_SUFFIX(packets_captured
));
2124 fputs(", ", stderr
);
2127 (void)fprintf(stderr
, "%u packet%s received by filter", stat
.ps_recv
,
2128 PLURAL_SUFFIX(stat
.ps_recv
));
2130 fputs(", ", stderr
);
2133 (void)fprintf(stderr
, "%u packet%s dropped by kernel", stat
.ps_drop
,
2134 PLURAL_SUFFIX(stat
.ps_drop
));
2135 if (stat
.ps_ifdrop
!= 0) {
2137 fputs(", ", stderr
);
2140 (void)fprintf(stderr
, "%u packet%s dropped by interface\n",
2141 stat
.ps_ifdrop
, PLURAL_SUFFIX(stat
.ps_ifdrop
));
2147 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
2149 compress_savefile(const char *filename
)
2158 * Set to lowest priority so that this doesn't disturb the capture
2161 setpriority(PRIO_PROCESS
, 0, NZERO
- 1);
2163 setpriority(PRIO_PROCESS
, 0, 19);
2165 if (execlp(zflag
, zflag
, filename
, (char *)NULL
) == -1)
2167 "compress_savefile:execlp(%s, %s): %s\n",
2177 #else /* HAVE_FORK && HAVE_VFORK */
2179 compress_savefile(const char *filename
)
2182 "compress_savefile failed. Functionality not implemented under your system\n");
2184 #endif /* HAVE_FORK && HAVE_VFORK */
2187 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
2189 struct dump_info
*dump_info
;
2190 #ifdef HAVE_CAPSICUM
2191 cap_rights_t rights
;
2198 dump_info
= (struct dump_info
*)user
;
2201 * XXX - this won't force the file to rotate on the specified time
2202 * boundary, but it will rotate on the first packet received after the
2203 * specified Gflag number of seconds. Note: if a Gflag time boundary
2204 * and a Cflag size boundary coincide, the time rotation will occur
2205 * first thereby cancelling the Cflag boundary (since the file should
2209 /* Check if it is time to rotate */
2212 /* Get the current time */
2213 if ((t
= time(NULL
)) == (time_t)-1) {
2214 error("dump_and_trunc_packet: can't get current_time: %s",
2215 pcap_strerror(errno
));
2219 /* If the time is greater than the specified window, rotate */
2220 if (t
- Gflag_time
>= Gflag
) {
2221 #ifdef HAVE_CAPSICUM
2226 /* Update the Gflag_time */
2228 /* Update Gflag_count */
2231 * Close the current file and open a new one.
2233 pcap_dump_close(dump_info
->p
);
2236 * Compress the file we just closed, if the user asked for it
2239 compress_savefile(dump_info
->CurrentFileName
);
2242 * Check to see if we've exceeded the Wflag (when
2245 if (Cflag
== 0 && Wflag
> 0 && Gflag_count
>= Wflag
) {
2246 (void)fprintf(stderr
, "Maximum file limit reached: %d\n",
2251 if (dump_info
->CurrentFileName
!= NULL
)
2252 free(dump_info
->CurrentFileName
);
2253 /* Allocate space for max filename + \0. */
2254 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
2255 if (dump_info
->CurrentFileName
== NULL
)
2256 error("dump_packet_and_trunc: malloc");
2258 * Gflag was set otherwise we wouldn't be here. Reset the count
2259 * so multiple files would end with 1,2,3 in the filename.
2260 * The counting is handled with the -C flow after this.
2265 * This is always the first file in the Cflag
2267 * We also don't need numbering if Cflag is not set.
2270 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0,
2273 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0, 0);
2275 #ifdef HAVE_LIBCAP_NG
2276 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
2277 capng_apply(CAPNG_SELECT_BOTH
);
2278 #endif /* HAVE_LIBCAP_NG */
2279 #ifdef HAVE_CAPSICUM
2280 fd
= openat(dump_info
->dirfd
,
2281 dump_info
->CurrentFileName
,
2282 O_CREAT
| O_WRONLY
| O_TRUNC
, 0644);
2284 error("unable to open file %s",
2285 dump_info
->CurrentFileName
);
2287 fp
= fdopen(fd
, "w");
2289 error("unable to fdopen file %s",
2290 dump_info
->CurrentFileName
);
2292 dump_info
->p
= pcap_dump_fopen(dump_info
->pd
, fp
);
2293 #else /* !HAVE_CAPSICUM */
2294 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
2296 #ifdef HAVE_LIBCAP_NG
2297 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
2298 capng_apply(CAPNG_SELECT_BOTH
);
2299 #endif /* HAVE_LIBCAP_NG */
2300 if (dump_info
->p
== NULL
)
2301 error("%s", pcap_geterr(pd
));
2302 #ifdef HAVE_CAPSICUM
2303 set_dumper_capsicum_rights(dump_info
->p
);
2309 * XXX - this won't prevent capture files from getting
2310 * larger than Cflag - the last packet written to the
2311 * file could put it over Cflag.
2314 long size
= pcap_dump_ftell(dump_info
->p
);
2317 error("ftell fails on output file");
2319 #ifdef HAVE_CAPSICUM
2325 * Close the current file and open a new one.
2327 pcap_dump_close(dump_info
->p
);
2330 * Compress the file we just closed, if the user
2334 compress_savefile(dump_info
->CurrentFileName
);
2338 if (Cflag_count
>= Wflag
)
2341 if (dump_info
->CurrentFileName
!= NULL
)
2342 free(dump_info
->CurrentFileName
);
2343 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
2344 if (dump_info
->CurrentFileName
== NULL
)
2345 error("dump_packet_and_trunc: malloc");
2346 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, Cflag_count
, WflagChars
);
2347 #ifdef HAVE_LIBCAP_NG
2348 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
2349 capng_apply(CAPNG_SELECT_BOTH
);
2350 #endif /* HAVE_LIBCAP_NG */
2351 #ifdef HAVE_CAPSICUM
2352 fd
= openat(dump_info
->dirfd
, dump_info
->CurrentFileName
,
2353 O_CREAT
| O_WRONLY
| O_TRUNC
, 0644);
2355 error("unable to open file %s",
2356 dump_info
->CurrentFileName
);
2358 fp
= fdopen(fd
, "w");
2360 error("unable to fdopen file %s",
2361 dump_info
->CurrentFileName
);
2363 dump_info
->p
= pcap_dump_fopen(dump_info
->pd
, fp
);
2364 #else /* !HAVE_CAPSICUM */
2365 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
2367 #ifdef HAVE_LIBCAP_NG
2368 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
2369 capng_apply(CAPNG_SELECT_BOTH
);
2370 #endif /* HAVE_LIBCAP_NG */
2371 if (dump_info
->p
== NULL
)
2372 error("%s", pcap_geterr(pd
));
2373 #ifdef HAVE_CAPSICUM
2374 set_dumper_capsicum_rights(dump_info
->p
);
2379 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
2380 #ifdef HAVE_PCAP_DUMP_FLUSH
2382 pcap_dump_flush(dump_info
->p
);
2391 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
2397 pcap_dump(user
, h
, sp
);
2398 #ifdef HAVE_PCAP_DUMP_FLUSH
2400 pcap_dump_flush((pcap_dumper_t
*)user
);
2409 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
2411 struct print_info
*print_info
;
2413 netdissect_options
*ndo
;
2419 print_info
= (struct print_info
*)user
;
2420 ndo
= print_info
->ndo
;
2422 if(ndo
->ndo_packet_number
)
2423 ND_PRINT((ndo
, "%5u ", packets_captured
));
2425 ts_print(ndo
, &h
->ts
);
2428 * Some printers want to check that they're not walking off the
2429 * end of the packet.
2430 * Rather than pass it all the way down, we set this member
2431 * of the netdissect_options structure.
2433 ndo
->ndo_snapend
= sp
+ h
->caplen
;
2435 hdrlen
= (*print_info
->printer
)(print_info
->ndo
, h
, sp
);
2438 * Restore the original snapend, as a printer might have
2441 ndo
->ndo_snapend
= sp
+ h
->caplen
;
2442 if (ndo
->ndo_Xflag
) {
2444 * Print the raw packet data in hex and ASCII.
2446 if (ndo
->ndo_Xflag
> 1) {
2448 * Include the link-layer header.
2450 hex_and_ascii_print(ndo
, "\n\t", sp
, h
->caplen
);
2453 * Don't include the link-layer header - and if
2454 * we have nothing past the link-layer header,
2457 if (h
->caplen
> hdrlen
)
2458 hex_and_ascii_print(ndo
, "\n\t", sp
+ hdrlen
,
2459 h
->caplen
- hdrlen
);
2461 } else if (ndo
->ndo_xflag
) {
2463 * Print the raw packet data in hex.
2465 if (ndo
->ndo_xflag
> 1) {
2467 * Include the link-layer header.
2469 hex_print(ndo
, "\n\t", sp
, h
->caplen
);
2472 * Don't include the link-layer header - and if
2473 * we have nothing past the link-layer header,
2476 if (h
->caplen
> hdrlen
)
2477 hex_print(ndo
, "\n\t", sp
+ hdrlen
,
2478 h
->caplen
- hdrlen
);
2480 } else if (ndo
->ndo_Aflag
) {
2482 * Print the raw packet data in ASCII.
2484 if (ndo
->ndo_Aflag
> 1) {
2486 * Include the link-layer header.
2488 ascii_print(ndo
, sp
, h
->caplen
);
2491 * Don't include the link-layer header - and if
2492 * we have nothing past the link-layer header,
2495 if (h
->caplen
> hdrlen
)
2496 ascii_print(ndo
, sp
+ hdrlen
, h
->caplen
- hdrlen
);
2509 * XXX - there should really be libpcap calls to get the version
2510 * number as a string (the string would be generated from #defines
2511 * at run time, so that it's not generated from string constants
2512 * in the library, as, on many UNIX systems, those constants would
2513 * be statically linked into the application executable image, and
2514 * would thus reflect the version of libpcap on the system on
2515 * which the application was *linked*, not the system on which it's
2518 * That routine should be documented, unlike the "version[]"
2519 * string, so that UNIX vendors providing their own libpcaps
2520 * don't omit it (as a couple of vendors have...).
2522 * Packet.dll should perhaps also export a routine to return the
2523 * version number of the Packet.dll code, to supply the
2524 * "Wpcap_version" information on Windows.
2526 char WDversion
[]="current-git.tcpdump.org";
2527 #if !defined(HAVE_GENERATED_VERSION)
2528 char version
[]="current-git.tcpdump.org";
2530 char pcap_version
[]="current-git.tcpdump.org";
2531 char Wpcap_version
[]="3.1";
2535 * By default, print the specified data out in hex and ASCII.
2538 ndo_default_print(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
2540 hex_and_ascii_print(ndo
, "\n\t", bp
, length
); /* pass on lf and indentation string */
2544 default_print(const u_char
*bp
, u_int length
)
2546 ndo_default_print(gndo
, bp
, length
);
2549 #ifdef SIGNAL_REQ_INFO
2550 RETSIGTYPE
requestinfo(int signo _U_
)
2560 * Called once each second in verbose mode while dumping to file
2562 #ifdef USE_WIN32_MM_TIMER
2563 void CALLBACK
verbose_stats_dump (UINT timer_id _U_
, UINT msg _U_
, DWORD_PTR arg _U_
,
2564 DWORD_PTR dw1 _U_
, DWORD_PTR dw2 _U_
)
2566 struct pcap_stat stat
;
2568 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
2569 fprintf(stderr
, "Got %u\r", packets_captured
);
2571 #elif defined(HAVE_ALARM)
2572 static void verbose_stats_dump(int sig _U_
)
2574 struct pcap_stat stat
;
2576 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
2577 fprintf(stderr
, "Got %u\r", packets_captured
);
2582 USES_APPLE_DEPRECATED_API
2586 extern char version
[];
2587 #ifndef HAVE_PCAP_LIB_VERSION
2588 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
2589 extern char pcap_version
[];
2590 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2591 static char pcap_version
[] = "unknown";
2592 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2593 #endif /* HAVE_PCAP_LIB_VERSION */
2595 #ifdef HAVE_PCAP_LIB_VERSION
2597 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2599 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2601 (void)fprintf(stderr
, "%s\n",pcap_lib_version());
2602 #else /* HAVE_PCAP_LIB_VERSION */
2604 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2605 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
2607 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2608 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
2610 #endif /* HAVE_PCAP_LIB_VERSION */
2612 #if defined(HAVE_LIBCRYPTO) && defined(SSLEAY_VERSION)
2613 (void)fprintf (stderr
, "%s\n", SSLeay_version(SSLEAY_VERSION
));
2617 (void)fprintf (stderr
, "SMI-library: %s\n", smi_version_string
);
2626 (void)fprintf(stderr
,
2627 "Usage: %s [-aAbd" D_FLAG
"efhH" I_FLAG J_FLAG
"KlLnNOpqRStu" U_FLAG
"vxX#]" B_FLAG_USAGE
" [ -c count ]\n", program_name
);
2628 (void)fprintf(stderr
,
2629 "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n");
2630 (void)fprintf(stderr
,
2631 "\t\t[ -i interface ]" j_FLAG_USAGE
" [ -M secret ] [ --number ]\n");
2632 #ifdef HAVE_PCAP_SETDIRECTION
2633 (void)fprintf(stderr
,
2634 "\t\t[ -Q in|out|inout ]\n");
2636 (void)fprintf(stderr
,
2637 "\t\t[ -r file ] [ -s snaplen ] ");
2638 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
2639 (void)fprintf(stderr
, "[ --time-stamp-precision precision ]\n");
2640 (void)fprintf(stderr
,
2643 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
2644 (void)fprintf(stderr
, "[ --immediate-mode ] ");
2646 (void)fprintf(stderr
, "[ -T type ] [ --version ] [ -V file ]\n");
2647 (void)fprintf(stderr
,
2648 "\t\t[ -w file ] [ -W filecount ] [ -y datalinktype ] [ -z command ]\n");
2649 (void)fprintf(stderr
,
2650 "\t\t[ -Z user ] [ expression ]\n");
2657 ndo_error(netdissect_options
*ndo _U_
, const char *fmt
, ...)
2661 (void)fprintf(stderr
, "%s: ", program_name
);
2663 (void)vfprintf(stderr
, fmt
, ap
);
2667 if (fmt
[-1] != '\n')
2668 (void)fputc('\n', stderr
);
2676 ndo_warning(netdissect_options
*ndo _U_
, const char *fmt
, ...)
2680 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
2682 (void)vfprintf(stderr
, fmt
, ap
);
2686 if (fmt
[-1] != '\n')
2687 (void)fputc('\n', stderr
);
2692 * c-style: whitesmith