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
;
207 static const struct printer printers
[] = {
211 static const struct ndo_printer ndo_printers
[] = {
212 { ether_if_print
, DLT_EN10MB
},
214 { ipnet_if_print
, DLT_IPNET
},
216 #ifdef DLT_IEEE802_15_4
217 { ieee802_15_4_if_print
, DLT_IEEE802_15_4
},
219 #ifdef DLT_IEEE802_15_4_NOFCS
220 { ieee802_15_4_if_print
, DLT_IEEE802_15_4_NOFCS
},
223 { ppi_if_print
, DLT_PPI
},
225 #ifdef DLT_NETANALYZER
226 { netanalyzer_if_print
, DLT_NETANALYZER
},
228 #ifdef DLT_NETANALYZER_TRANSPARENT
229 { netanalyzer_transparent_if_print
, DLT_NETANALYZER_TRANSPARENT
},
231 #if defined(DLT_NFLOG) && defined(HAVE_PCAP_NFLOG_H)
232 { nflog_if_print
, DLT_NFLOG
},
235 { cip_if_print
, DLT_CIP
},
238 { cip_if_print
, DLT_ATM_CLIP
},
240 #ifdef DLT_IP_OVER_FC
241 { ipfc_if_print
, DLT_IP_OVER_FC
},
243 { null_if_print
, DLT_NULL
},
245 { null_if_print
, DLT_LOOP
},
247 #ifdef DLT_APPLE_IP_OVER_IEEE1394
248 { ap1394_if_print
, DLT_APPLE_IP_OVER_IEEE1394
},
250 #if defined(DLT_BLUETOOTH_HCI_H4_WITH_PHDR) && defined(HAVE_PCAP_BLUETOOTH_H)
251 { bt_if_print
, DLT_BLUETOOTH_HCI_H4_WITH_PHDR
},
254 { lane_if_print
, DLT_LANE8023
},
256 { arcnet_if_print
, DLT_ARCNET
},
257 #ifdef DLT_ARCNET_LINUX
258 { arcnet_linux_if_print
, DLT_ARCNET_LINUX
},
260 { raw_if_print
, DLT_RAW
},
262 { raw_if_print
, DLT_IPV4
},
265 { raw_if_print
, DLT_IPV6
},
267 #ifdef HAVE_PCAP_USB_H
269 { usb_linux_48_byte_print
, DLT_USB_LINUX
},
270 #endif /* DLT_USB_LINUX */
271 #ifdef DLT_USB_LINUX_MMAPPED
272 { usb_linux_64_byte_print
, DLT_USB_LINUX_MMAPPED
},
273 #endif /* DLT_USB_LINUX_MMAPPED */
274 #endif /* HAVE_PCAP_USB_H */
275 #ifdef DLT_SYMANTEC_FIREWALL
276 { symantec_if_print
, DLT_SYMANTEC_FIREWALL
},
279 { chdlc_if_print
, DLT_C_HDLC
},
282 { chdlc_if_print
, DLT_HDLC
},
285 { pppoe_if_print
, DLT_PPP_ETHER
},
287 #if defined(DLT_PFLOG) && defined(HAVE_NET_PFVAR_H)
288 { pflog_if_print
, DLT_PFLOG
},
290 { token_if_print
, DLT_IEEE802
},
291 { fddi_if_print
, DLT_FDDI
},
293 { sll_if_print
, DLT_LINUX_SLL
},
296 { fr_if_print
, DLT_FR
},
299 { fr_if_print
, DLT_FRELAY
},
302 { mfr_if_print
, DLT_MFR
},
304 { atm_if_print
, DLT_ATM_RFC1483
},
306 { sunatm_if_print
, DLT_SUNATM
},
309 { enc_if_print
, DLT_ENC
},
311 { sl_if_print
, DLT_SLIP
},
312 #ifdef DLT_SLIP_BSDOS
313 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
316 { ltalk_if_print
, DLT_LTALK
},
318 #ifdef DLT_JUNIPER_ATM1
319 { juniper_atm1_print
, DLT_JUNIPER_ATM1
},
321 #ifdef DLT_JUNIPER_ATM2
322 { juniper_atm2_print
, DLT_JUNIPER_ATM2
},
324 #ifdef DLT_JUNIPER_MFR
325 { juniper_mfr_print
, DLT_JUNIPER_MFR
},
327 #ifdef DLT_JUNIPER_MLFR
328 { juniper_mlfr_print
, DLT_JUNIPER_MLFR
},
330 #ifdef DLT_JUNIPER_MLPPP
331 { juniper_mlppp_print
, DLT_JUNIPER_MLPPP
},
333 #ifdef DLT_JUNIPER_PPPOE
334 { juniper_pppoe_print
, DLT_JUNIPER_PPPOE
},
336 #ifdef DLT_JUNIPER_PPPOE_ATM
337 { juniper_pppoe_atm_print
, DLT_JUNIPER_PPPOE_ATM
},
339 #ifdef DLT_JUNIPER_GGSN
340 { juniper_ggsn_print
, DLT_JUNIPER_GGSN
},
342 #ifdef DLT_JUNIPER_ES
343 { juniper_es_print
, DLT_JUNIPER_ES
},
345 #ifdef DLT_JUNIPER_MONITOR
346 { juniper_monitor_print
, DLT_JUNIPER_MONITOR
},
348 #ifdef DLT_JUNIPER_SERVICES
349 { juniper_services_print
, DLT_JUNIPER_SERVICES
},
351 #ifdef DLT_JUNIPER_ETHER
352 { juniper_ether_print
, DLT_JUNIPER_ETHER
},
354 #ifdef DLT_JUNIPER_PPP
355 { juniper_ppp_print
, DLT_JUNIPER_PPP
},
357 #ifdef DLT_JUNIPER_FRELAY
358 { juniper_frelay_print
, DLT_JUNIPER_FRELAY
},
360 #ifdef DLT_JUNIPER_CHDLC
361 { juniper_chdlc_print
, DLT_JUNIPER_CHDLC
},
364 { pktap_if_print
, DLT_PKTAP
},
366 #ifdef DLT_IEEE802_11_RADIO
367 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
369 #ifdef DLT_IEEE802_11
370 { ieee802_11_if_print
, DLT_IEEE802_11
},
372 #ifdef DLT_IEEE802_11_RADIO_AVS
373 { ieee802_11_radio_avs_if_print
, DLT_IEEE802_11_RADIO_AVS
},
375 #ifdef DLT_PRISM_HEADER
376 { prism_if_print
, DLT_PRISM_HEADER
},
378 { ppp_if_print
, DLT_PPP
},
379 #ifdef DLT_PPP_WITHDIRECTION
380 { ppp_if_print
, DLT_PPP_WITHDIRECTION
},
383 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
385 #ifdef DLT_PPP_SERIAL
386 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
391 static const struct tok status_flags
[] = {
393 { PCAP_IF_UP
, "Up" },
395 #ifdef PCAP_IF_RUNNING
396 { PCAP_IF_RUNNING
, "Running" },
398 { PCAP_IF_LOOPBACK
, "Loopback" },
403 lookup_printer(int type
)
405 const struct printer
*p
;
407 for (p
= printers
; p
->f
; ++p
)
416 lookup_ndo_printer(int type
)
418 const struct ndo_printer
*p
;
420 for (p
= ndo_printers
; p
->f
; ++p
)
424 #if defined(DLT_USER2) && defined(DLT_PKTAP)
426 * Apple incorrectly chose to use DLT_USER2 for their PKTAP
429 * We map DLT_PKTAP, whether it's DLT_USER2 as it is on Darwin-
430 * based OSes or the same value as LINKTYPE_PKTAP as it is on
431 * other OSes, to LINKTYPE_PKTAP, so files written with
432 * this version of libpcap for a DLT_PKTAP capture have a link-
433 * layer header type of LINKTYPE_PKTAP.
435 * However, files written on OS X Mavericks for a DLT_PKTAP
436 * capture have a link-layer header type of LINKTYPE_USER2.
437 * If we don't have a printer for DLT_USER2, and type is
438 * DLT_USER2, we look up the printer for DLT_PKTAP and use
441 if (type
== DLT_USER2
) {
442 for (p
= ndo_printers
; p
->f
; ++p
)
443 if (DLT_PKTAP
== p
->type
)
454 static int supports_monitor_mode
;
461 netdissect_options
*ndo
;
464 if_ndo_printer ndo_printer
;
471 char *CurrentFileName
;
479 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
481 show_tstamp_types_and_exit(const char *device
, pcap_t
*pd
)
484 int *tstamp_types
= 0;
485 const char *tstamp_type_name
;
488 n_tstamp_types
= pcap_list_tstamp_types(pd
, &tstamp_types
);
489 if (n_tstamp_types
< 0)
490 error("%s", pcap_geterr(pd
));
492 if (n_tstamp_types
== 0) {
493 fprintf(stderr
, "Time stamp type cannot be set for %s\n",
497 fprintf(stderr
, "Time stamp types for %s (use option -j to set):\n",
499 for (i
= 0; i
< n_tstamp_types
; i
++) {
500 tstamp_type_name
= pcap_tstamp_type_val_to_name(tstamp_types
[i
]);
501 if (tstamp_type_name
!= NULL
) {
502 (void) fprintf(stderr
, " %s (%s)\n", tstamp_type_name
,
503 pcap_tstamp_type_val_to_description(tstamp_types
[i
]));
505 (void) fprintf(stderr
, " %d\n", tstamp_types
[i
]);
508 pcap_free_tstamp_types(tstamp_types
);
514 show_dlts_and_exit(const char *device
, pcap_t
*pd
)
518 const char *dlt_name
;
520 n_dlts
= pcap_list_datalinks(pd
, &dlts
);
522 error("%s", pcap_geterr(pd
));
523 else if (n_dlts
== 0 || !dlts
)
524 error("No data link types.");
527 * If the interface is known to support monitor mode, indicate
528 * whether these are the data link types available when not in
529 * monitor mode, if -I wasn't specified, or when in monitor mode,
530 * when -I was specified (the link-layer types available in
531 * monitor mode might be different from the ones available when
532 * not in monitor mode).
534 if (supports_monitor_mode
)
535 (void) fprintf(stderr
, "Data link types for %s %s (use option -y to set):\n",
537 Iflag
? "when in monitor mode" : "when not in monitor mode");
539 (void) fprintf(stderr
, "Data link types for %s (use option -y to set):\n",
542 while (--n_dlts
>= 0) {
543 dlt_name
= pcap_datalink_val_to_name(dlts
[n_dlts
]);
544 if (dlt_name
!= NULL
) {
545 (void) fprintf(stderr
, " %s (%s)", dlt_name
,
546 pcap_datalink_val_to_description(dlts
[n_dlts
]));
549 * OK, does tcpdump handle that type?
551 if (lookup_printer(dlts
[n_dlts
]) == NULL
552 && lookup_ndo_printer(dlts
[n_dlts
]) == NULL
)
553 (void) fprintf(stderr
, " (printing not supported)");
554 fprintf(stderr
, "\n");
556 (void) fprintf(stderr
, " DLT %d (printing not supported)\n",
560 #ifdef HAVE_PCAP_FREE_DATALINKS
561 pcap_free_datalinks(dlts
);
566 #ifdef HAVE_PCAP_FINDALLDEVS
568 show_devices_and_exit (void)
570 pcap_if_t
*devpointer
;
571 char ebuf
[PCAP_ERRBUF_SIZE
];
574 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
577 for (i
= 0; devpointer
!= NULL
; i
++) {
578 printf("%d.%s", i
+1, devpointer
->name
);
579 if (devpointer
->description
!= NULL
)
580 printf(" (%s)", devpointer
->description
);
581 if (devpointer
->flags
!= 0)
582 printf(" [%s]", bittok2str(status_flags
, "none", devpointer
->flags
));
584 devpointer
= devpointer
->next
;
589 #endif /* HAVE_PCAP_FINDALLDEVS */
594 * Note that there we use all letters for short options except for g, k,
595 * o, and P, and those are used by other versions of tcpdump, and we should
596 * only use them for the same purposes that the other versions of tcpdump
599 * OS X tcpdump uses -g to force non--v output for IP to be on one
600 * line, making it more "g"repable;
602 * OS X tcpdump uses -k tospecify that packet comments in pcap-ng files
605 * OpenBSD tcpdump uses -o to indicate that OS fingerprinting should be done
606 * for hosts sending TCP SYN packets;
608 * OS X tcpdump uses -P to indicate that -w should write pcap-ng rather
613 * Set up flags that might or might not be supported depending on the
614 * version of libpcap we're using.
616 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
618 #define B_FLAG_USAGE " [ -B size ]"
619 #else /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
622 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
624 #ifdef HAVE_PCAP_CREATE
626 #else /* HAVE_PCAP_CREATE */
628 #endif /* HAVE_PCAP_CREATE */
630 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
632 #define j_FLAG_USAGE " [ -j tstamptype ]"
634 #else /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
638 #endif /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
640 #ifdef HAVE_PCAP_FINDALLDEVS
646 #ifdef HAVE_PCAP_DUMP_FLUSH
652 #ifdef HAVE_PCAP_SETDIRECTION
661 * We do not currently have long options corresponding to all short
662 * options; we should probably pick appropriate option names for them.
664 * However, the short options where the number of times the option is
665 * specified matters, such as -v and -d and -t, should probably not
666 * just map to a long option, as saying
668 * tcpdump --verbose --verbose
670 * doesn't make sense; it should be --verbosity={N} or something such
673 * For long options with no corresponding short options, we define values
674 * outside the range of ASCII graphic characters, make that the last
675 * component of the entry for the long option, and have a case for that
676 * option in the switch statement.
678 #define OPTION_VERSION 128
679 #define OPTION_TSTAMP_PRECISION 129
680 #define OPTION_IMMEDIATE_MODE 130
682 static const struct option longopts
[] = {
683 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
684 { "buffer-size", required_argument
, NULL
, 'B' },
686 { "list-interfaces", no_argument
, NULL
, 'D' },
687 { "help", no_argument
, NULL
, 'h' },
688 { "interface", required_argument
, NULL
, 'i' },
689 #ifdef HAVE_PCAP_CREATE
690 { "monitor-mode", no_argument
, NULL
, 'I' },
692 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
693 { "time-stamp-type", required_argument
, NULL
, 'j' },
694 { "list-time-stamp-types", no_argument
, NULL
, 'J' },
696 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
697 { "time-stamp-precision", required_argument
, NULL
, OPTION_TSTAMP_PRECISION
},
699 { "dont-verify-checksums", no_argument
, NULL
, 'K' },
700 { "list-data-link-types", no_argument
, NULL
, 'L' },
701 { "no-optimize", no_argument
, NULL
, 'O' },
702 { "no-promiscuous-mode", no_argument
, NULL
, 'p' },
703 #ifdef HAVE_PCAP_SETDIRECTION
704 { "direction", required_argument
, NULL
, 'Q' },
706 { "snapshot-length", required_argument
, NULL
, 's' },
707 { "absolute-tcp-sequence-numbers", no_argument
, NULL
, 'S' },
708 #ifdef HAVE_PCAP_DUMP_FLUSH
709 { "packet-buffered", no_argument
, NULL
, 'U' },
711 { "linktype", required_argument
, NULL
, 'y' },
712 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
713 { "immediate-mode", no_argument
, NULL
, OPTION_IMMEDIATE_MODE
},
715 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
716 { "debug-filter-parser", no_argument
, NULL
, 'Y' },
718 { "relinquish-privileges", required_argument
, NULL
, 'Z' },
719 { "number", no_argument
, NULL
, '#' },
720 { "version", no_argument
, NULL
, OPTION_VERSION
},
725 /* Drop root privileges and chroot if necessary */
727 droproot(const char *username
, const char *chroot_dir
)
729 struct passwd
*pw
= NULL
;
731 if (chroot_dir
&& !username
) {
732 fprintf(stderr
, "tcpdump: Chroot without dropping root is insecure\n");
736 pw
= getpwnam(username
);
739 if (chroot(chroot_dir
) != 0 || chdir ("/") != 0) {
740 fprintf(stderr
, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
741 chroot_dir
, pcap_strerror(errno
));
745 #ifdef HAVE_LIBCAP_NG
746 int ret
= capng_change_id(pw
->pw_uid
, pw
->pw_gid
, CAPNG_NO_FLAG
);
748 fprintf(stderr
, "error : ret %d\n", ret
);
751 fprintf(stderr
, "dropped privs to %s\n", username
);
754 if (initgroups(pw
->pw_name
, pw
->pw_gid
) != 0 ||
755 setgid(pw
->pw_gid
) != 0 || setuid(pw
->pw_uid
) != 0) {
756 fprintf(stderr
, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
758 (unsigned long)pw
->pw_uid
,
759 (unsigned long)pw
->pw_gid
,
760 pcap_strerror(errno
));
764 fprintf(stderr
, "dropped privs to %s\n", username
);
766 #endif /* HAVE_LIBCAP_NG */
769 fprintf(stderr
, "tcpdump: Couldn't find user '%.32s'\n",
773 #ifdef HAVE_LIBCAP_NG
774 /* We don't need CAP_SETUID and CAP_SETGID any more. */
777 CAPNG_EFFECTIVE
| CAPNG_PERMITTED
,
781 capng_apply(CAPNG_SELECT_BOTH
);
782 #endif /* HAVE_LIBCAP_NG */
803 MakeFilename(char *buffer
, char *orig_name
, int cnt
, int max_chars
)
805 char *filename
= malloc(PATH_MAX
+ 1);
806 if (filename
== NULL
)
807 error("Makefilename: malloc");
809 /* Process with strftime if Gflag is set. */
813 /* Convert Gflag_time to a usable format */
814 if ((local_tm
= localtime(&Gflag_time
)) == NULL
) {
815 error("MakeTimedFilename: localtime");
818 /* There's no good way to detect an error in strftime since a return
819 * value of 0 isn't necessarily failure.
821 strftime(filename
, PATH_MAX
, orig_name
, local_tm
);
823 strncpy(filename
, orig_name
, PATH_MAX
);
826 if (cnt
== 0 && max_chars
== 0)
827 strncpy(buffer
, filename
, PATH_MAX
+ 1);
829 if (snprintf(buffer
, PATH_MAX
+ 1, "%s%0*d", filename
, max_chars
, cnt
) > PATH_MAX
)
830 /* Report an error if the filename is too large */
831 error("too many output files or filename is too long (> %d)", PATH_MAX
);
835 static int tcpdump_printf(netdissect_options
*ndo _U_
,
836 const char *fmt
, ...)
843 ret
=vfprintf(stdout
, fmt
, args
);
849 static struct print_info
850 get_print_info(int type
)
852 struct print_info printinfo
;
854 printinfo
.ndo_type
= 1;
855 printinfo
.ndo
= gndo
;
856 printinfo
.p
.ndo_printer
= lookup_ndo_printer(type
);
857 if (printinfo
.p
.ndo_printer
== NULL
) {
858 printinfo
.p
.printer
= lookup_printer(type
);
859 printinfo
.ndo_type
= 0;
860 if (printinfo
.p
.printer
== NULL
) {
861 gndo
->ndo_dltname
= pcap_datalink_val_to_name(type
);
862 if (gndo
->ndo_dltname
!= NULL
)
863 error("packet printing is not supported for link type %s: use -w",
866 error("packet printing is not supported for link type %d: use -w", type
);
873 get_next_file(FILE *VFile
, char *ptr
)
877 ret
= fgets(ptr
, PATH_MAX
, VFile
);
881 if (ptr
[strlen(ptr
) - 1] == '\n')
882 ptr
[strlen(ptr
) - 1] = '\0';
887 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
889 tstamp_precision_from_string(const char *precision
)
891 if (strncmp(precision
, "nano", strlen("nano")) == 0)
892 return PCAP_TSTAMP_PRECISION_NANO
;
894 if (strncmp(precision
, "micro", strlen("micro")) == 0)
895 return PCAP_TSTAMP_PRECISION_MICRO
;
901 tstamp_precision_to_string(int precision
)
905 case PCAP_TSTAMP_PRECISION_MICRO
:
908 case PCAP_TSTAMP_PRECISION_NANO
:
919 * Ensure that, on a dump file's descriptor, we have all the rights
920 * necessary to make the standard I/O library work with an fdopen()ed
921 * FILE * from that descriptor.
923 * A long time ago, in a galaxy far far away, AT&T decided that, instead
924 * of providing separate APIs for getting and setting the FD_ flags on a
925 * descriptor, getting and setting the O_ flags on a descriptor, and
926 * locking files, they'd throw them all into a kitchen-sink fcntl() call
927 * along the lines of ioctl(), the fact that ioctl() operations are
928 * largely specific to particular character devices but fcntl() operations
929 * are either generic to all descriptors or generic to all descriptors for
930 * regular files nonwithstanding.
932 * The Capsicum people decided that fine-grained control of descriptor
933 * operations was required, so that you need to grant permission for
934 * reading, writing, seeking, and fcntl-ing. The latter, courtesy of
935 * AT&T's decision, means that "fcntl-ing" isn't a thing, but a motley
936 * collection of things, so there are *individual* fcntls for which
937 * permission needs to be granted.
939 * The FreeBSD standard I/O people implemented some optimizations that
940 * requires that the standard I/O routines be able to determine whether
941 * the descriptor for the FILE * is open append-only or not; as that
942 * descriptor could have come from an open() rather than an fopen(),
943 * that requires that it be able to do an F_GETFL fcntl() to read
946 * Tcpdump uses ftell() to determine how much data has been written
947 * to a file in order to, when used with -C, determine when it's time
948 * to rotate capture files. ftell() therefore needs to do an lseek()
949 * to find out the file offset and must, thanks to the aforementioned
950 * optimization, also know whether the descriptor is open append-only
953 * The net result of all the above is that we need to grant CAP_SEEK,
954 * CAP_WRITE, and CAP_FCNTL with the CAP_FCNTL_GETFL subcapability.
956 * Perhaps this is the universe's way of saying that either
958 * 1) there needs to be an fopenat() call and a pcap_dump_openat() call
959 * using it, so that Capsicum-capable tcpdump wouldn't need to do
964 * 2) there needs to be a cap_fdopen() call in the FreeBSD standard
965 * I/O library that knows what rights are needed by the standard
966 * I/O library, based on the open mode, and assigns them, perhaps
967 * with an additional argument indicating, for example, whether
968 * seeking should be allowed, so that tcpdump doesn't need to know
969 * what the standard I/O library happens to require this week.
972 set_dump_fd_capsicum_rights(int fd
)
976 cap_rights_init(&rights
, CAP_SEEK
, CAP_WRITE
, CAP_FCNTL
);
977 if (cap_rights_limit(fd
, &rights
) < 0 && errno
!= ENOSYS
) {
978 error("unable to limit dump descriptor");
980 if (cap_fcntls_limit(fd
, CAP_FCNTL_GETFL
) < 0 && errno
!= ENOSYS
) {
981 error("unable to limit dump descriptor fcntls");
987 main(int argc
, char **argv
)
989 register int cnt
, op
, i
;
990 bpf_u_int32 localnet
=0 , netmask
= 0;
991 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *VFileName
, *WFileName
;
992 pcap_handler callback
;
996 const char *dlt_name
;
997 struct bpf_program fcode
;
999 RETSIGTYPE (*oldhandler
)(int);
1001 struct print_info printinfo
;
1002 struct dump_info dumpinfo
;
1003 u_char
*pcap_userdata
;
1004 char ebuf
[PCAP_ERRBUF_SIZE
];
1005 char VFileLine
[PATH_MAX
+ 1];
1006 char *username
= NULL
;
1007 char *chroot_dir
= NULL
;
1010 #ifdef HAVE_PCAP_FINDALLDEVS
1011 pcap_if_t
*devpointer
;
1016 #ifdef HAVE_CAPSICUM
1017 cap_rights_t rights
;
1019 #endif /* HAVE_CAPSICUM */
1022 if(wsockinit() != 0) return 1;
1025 jflag
=-1; /* not set */
1029 gndo
->ndo_default_print
=ndo_default_print
;
1030 gndo
->ndo_printf
=tcpdump_printf
;
1031 gndo
->ndo_error
=ndo_error
;
1032 gndo
->ndo_warning
=ndo_warning
;
1033 gndo
->ndo_snaplen
= DEFAULT_SNAPLEN
;
1034 gndo
->ndo_immediate
= 0;
1044 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
1045 program_name
= cp
+ 1;
1047 program_name
= argv
[0];
1050 * On platforms where the CPU doesn't support unaligned loads,
1051 * force unaligned accesses to abort with SIGBUS, rather than
1052 * being fixed up (slowly) by the OS kernel; on those platforms,
1053 * misaligned accesses are bugs, and we want tcpdump to crash so
1054 * that the bugs are reported.
1056 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
1064 (op
= getopt_long(argc
, argv
, "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:#", longopts
, NULL
)) != -1)
1068 /* compatibility for old -a */
1079 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
1081 Bflag
= atoi(optarg
)*1024;
1083 error("invalid packet buffer size %s", optarg
);
1085 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
1090 error("invalid packet count %s", optarg
);
1094 Cflag
= atoi(optarg
) * 1000000;
1096 error("invalid file size %s", optarg
);
1116 #ifndef HAVE_LIBCRYPTO
1117 warning("crypto code not compiled in");
1119 gndo
->ndo_espsecret
= optarg
;
1131 Gflag
= atoi(optarg
);
1133 error("invalid number of seconds %s", optarg
);
1135 /* We will create one file initially. */
1138 /* Grab the current time for rotation use. */
1139 if ((Gflag_time
= time(NULL
)) == (time_t)-1) {
1140 error("main: can't get current time: %s",
1141 pcap_strerror(errno
));
1155 if (optarg
[0] == '0' && optarg
[1] == 0)
1156 error("Invalid adapter index");
1158 #ifdef HAVE_PCAP_FINDALLDEVS
1160 * If the argument is a number, treat it as
1161 * an index into the list of adapters, as
1162 * printed by "tcpdump -D".
1164 * This should be OK on UNIX systems, as interfaces
1165 * shouldn't have names that begin with digits.
1166 * It can be useful on Windows, where more than
1167 * one interface can have the same name.
1169 devnum
= strtol(optarg
, &end
, 10);
1170 if (optarg
!= end
&& *end
== '\0') {
1172 error("Invalid adapter index");
1174 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
1178 * Look for the devnum-th entry
1179 * in the list of devices
1183 i
< devnum
-1 && devpointer
!= NULL
;
1184 i
++, devpointer
= devpointer
->next
)
1186 if (devpointer
== NULL
)
1187 error("Invalid adapter index");
1189 device
= devpointer
->name
;
1192 #endif /* HAVE_PCAP_FINDALLDEVS */
1196 #ifdef HAVE_PCAP_CREATE
1200 #endif /* HAVE_PCAP_CREATE */
1202 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1204 jflag
= pcap_tstamp_type_name_to_val(optarg
);
1206 error("invalid time stamp type %s", optarg
);
1217 * _IOLBF is the same as _IOFBF in Microsoft's C
1218 * libraries; the only alternative they offer
1221 * XXX - this should really be checking for MSVC++,
1222 * not WIN32, if, for example, MinGW has its own
1223 * C library that is more UNIX-compatible.
1225 setvbuf(stdout
, NULL
, _IONBF
, 0);
1227 #ifdef HAVE_SETLINEBUF
1230 setvbuf(stdout
, NULL
, _IOLBF
, 0);
1241 if (smiLoadModule(optarg
) == 0) {
1242 error("could not load MIB module %s", optarg
);
1246 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
1247 program_name
, optarg
);
1248 (void)fprintf(stderr
, "(no libsmi support)\n");
1253 /* TCP-MD5 shared secret */
1254 #ifndef HAVE_LIBCRYPTO
1255 warning("crypto code not compiled in");
1278 ++suppress_default_print
;
1281 #ifdef HAVE_PCAP_SETDIRECTION
1283 if (strcasecmp(optarg
, "in") == 0)
1285 else if (strcasecmp(optarg
, "out") == 0)
1287 else if (strcasecmp(optarg
, "inout") == 0)
1288 Qflag
= PCAP_D_INOUT
;
1290 error("unknown capture direction `%s'", optarg
);
1292 #endif /* HAVE_PCAP_SETDIRECTION */
1303 snaplen
= strtol(optarg
, &end
, 0);
1304 if (optarg
== end
|| *end
!= '\0'
1305 || snaplen
< 0 || snaplen
> MAXIMUM_SNAPLEN
)
1306 error("invalid snaplen %s", optarg
);
1307 else if (snaplen
== 0)
1308 snaplen
= MAXIMUM_SNAPLEN
;
1320 if (strcasecmp(optarg
, "vat") == 0)
1321 packettype
= PT_VAT
;
1322 else if (strcasecmp(optarg
, "wb") == 0)
1324 else if (strcasecmp(optarg
, "rpc") == 0)
1325 packettype
= PT_RPC
;
1326 else if (strcasecmp(optarg
, "rtp") == 0)
1327 packettype
= PT_RTP
;
1328 else if (strcasecmp(optarg
, "rtcp") == 0)
1329 packettype
= PT_RTCP
;
1330 else if (strcasecmp(optarg
, "snmp") == 0)
1331 packettype
= PT_SNMP
;
1332 else if (strcasecmp(optarg
, "cnfp") == 0)
1333 packettype
= PT_CNFP
;
1334 else if (strcasecmp(optarg
, "tftp") == 0)
1335 packettype
= PT_TFTP
;
1336 else if (strcasecmp(optarg
, "aodv") == 0)
1337 packettype
= PT_AODV
;
1338 else if (strcasecmp(optarg
, "carp") == 0)
1339 packettype
= PT_CARP
;
1340 else if (strcasecmp(optarg
, "radius") == 0)
1341 packettype
= PT_RADIUS
;
1342 else if (strcasecmp(optarg
, "zmtp1") == 0)
1343 packettype
= PT_ZMTP1
;
1344 else if (strcasecmp(optarg
, "vxlan") == 0)
1345 packettype
= PT_VXLAN
;
1346 else if (strcasecmp(optarg
, "pgm") == 0)
1347 packettype
= PT_PGM
;
1348 else if (strcasecmp(optarg
, "pgm_zmtp1") == 0)
1349 packettype
= PT_PGM_ZMTP1
;
1350 else if (strcasecmp(optarg
, "lmp") == 0)
1351 packettype
= PT_LMP
;
1353 error("unknown packet type `%s'", optarg
);
1360 #ifdef HAVE_PCAP_DUMP_FLUSH
1379 Wflag
= atoi(optarg
);
1381 error("invalid number of output files %s", optarg
);
1382 WflagChars
= getWflagChars(Wflag
);
1387 ++suppress_default_print
;
1392 ++suppress_default_print
;
1396 gndo
->ndo_dltname
= optarg
;
1398 pcap_datalink_name_to_val(gndo
->ndo_dltname
);
1399 if (gndo
->ndo_dlt
< 0)
1400 error("invalid data link type %s", gndo
->ndo_dltname
);
1403 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
1406 /* Undocumented flag */
1407 #ifdef HAVE_PCAP_DEBUG
1408 extern int pcap_debug
;
1418 zflag
= strdup(optarg
);
1422 username
= strdup(optarg
);
1426 gndo
->ndo_packet_number
= 1;
1429 case OPTION_VERSION
:
1434 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1435 case OPTION_TSTAMP_PRECISION
:
1436 gndo
->ndo_tstamp_precision
= tstamp_precision_from_string(optarg
);
1437 if (gndo
->ndo_tstamp_precision
< 0)
1438 error("unsupported time stamp precision");
1442 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1443 case OPTION_IMMEDIATE_MODE
:
1444 gndo
->ndo_immediate
= 1;
1454 #ifdef HAVE_PCAP_FINDALLDEVS
1456 show_devices_and_exit();
1461 case 0: /* Default */
1462 case 4: /* Default + Date*/
1463 thiszone
= gmt2local(0);
1466 case 1: /* No time stamp */
1467 case 2: /* Unix timeval style */
1468 case 3: /* Microseconds since previous packet */
1469 case 5: /* Microseconds since first packet */
1472 default: /* Not supported */
1473 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
1477 if (fflag
!= 0 && (VFileName
!= NULL
|| RFileName
!= NULL
))
1478 error("-f can not be used with -V or -r");
1480 if (VFileName
!= NULL
&& RFileName
!= NULL
)
1481 error("-V and -r are mutually exclusive.");
1483 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1485 * If we're printing dissected packets to the standard output
1486 * rather than saving raw packets to a file, and the standard
1487 * output is a terminal, use immediate mode, as the user's
1488 * probably expecting to see packets pop up immediately.
1490 if (WFileName
== NULL
&& isatty(1))
1491 gndo
->ndo_immediate
= 1;
1495 /* if run as root, prepare for chrooting */
1496 if (getuid() == 0 || geteuid() == 0) {
1497 /* future extensibility for cmd-line arguments */
1499 chroot_dir
= WITH_CHROOT
;
1504 /* if run as root, prepare for dropping root privileges */
1505 if (getuid() == 0 || geteuid() == 0) {
1506 /* Run with '-Z root' to restore old behaviour */
1508 username
= WITH_USER
;
1512 if (RFileName
!= NULL
|| VFileName
!= NULL
) {
1514 * If RFileName is non-null, it's the pathname of a
1515 * savefile to read. If VFileName is non-null, it's
1516 * the pathname of a file containing a list of pathnames
1517 * (one per line) of savefiles to read.
1519 * In either case, we're reading a savefile, not doing
1524 * We don't need network access, so relinquish any set-UID
1525 * or set-GID privileges we have (if any).
1527 * We do *not* want set-UID privileges when opening a
1528 * trace file, as that might let the user read other
1529 * people's trace files (especially if we're set-UID
1532 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
1533 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1535 if (VFileName
!= NULL
) {
1536 if (VFileName
[0] == '-' && VFileName
[1] == '\0')
1539 VFile
= fopen(VFileName
, "r");
1542 error("Unable to open file: %s\n", strerror(errno
));
1544 ret
= get_next_file(VFile
, VFileLine
);
1546 error("Nothing in %s\n", VFileName
);
1547 RFileName
= VFileLine
;
1550 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1551 pd
= pcap_open_offline_with_tstamp_precision(RFileName
,
1552 gndo
->ndo_tstamp_precision
, ebuf
);
1554 pd
= pcap_open_offline(RFileName
, ebuf
);
1559 #ifdef HAVE_CAPSICUM
1560 cap_rights_init(&rights
, CAP_READ
);
1561 if (cap_rights_limit(fileno(pcap_file(pd
)), &rights
) < 0 &&
1563 error("unable to limit pcap descriptor");
1566 dlt
= pcap_datalink(pd
);
1567 dlt_name
= pcap_datalink_val_to_name(dlt
);
1568 if (dlt_name
== NULL
) {
1569 fprintf(stderr
, "reading from file %s, link-type %u\n",
1573 "reading from file %s, link-type %s (%s)\n",
1574 RFileName
, dlt_name
,
1575 pcap_datalink_val_to_description(dlt
));
1579 * We're doing a live capture.
1581 if (device
== NULL
) {
1582 device
= pcap_lookupdev(ebuf
);
1588 * Print a message to the standard error on Windows.
1589 * XXX - why do it here, with a different message?
1591 if(strlen(device
) == 1) /* we assume that an ASCII string is always longer than 1 char */
1592 { /* a Unicode string has a \0 as second byte (so strlen() is 1) */
1593 fprintf(stderr
, "%s: listening on %ws\n", program_name
, device
);
1597 fprintf(stderr
, "%s: listening on %s\n", program_name
, device
);
1602 #ifdef HAVE_PCAP_CREATE
1603 pd
= pcap_create(device
, ebuf
);
1606 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1608 show_tstamp_types_and_exit(device
, pd
);
1610 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1611 status
= pcap_set_tstamp_precision(pd
, gndo
->ndo_tstamp_precision
);
1613 error("%s: Can't set %ssecond time stamp precision: %s",
1615 tstamp_precision_to_string(gndo
->ndo_tstamp_precision
),
1616 pcap_statustostr(status
));
1619 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1620 if (gndo
->ndo_immediate
) {
1621 status
= pcap_set_immediate_mode(pd
, 1);
1623 error("%s: Can't set immediate mode: %s",
1625 pcap_statustostr(status
));
1629 * Is this an interface that supports monitor mode?
1631 if (pcap_can_set_rfmon(pd
) == 1)
1632 supports_monitor_mode
= 1;
1634 supports_monitor_mode
= 0;
1635 status
= pcap_set_snaplen(pd
, snaplen
);
1637 error("%s: Can't set snapshot length: %s",
1638 device
, pcap_statustostr(status
));
1639 status
= pcap_set_promisc(pd
, !pflag
);
1641 error("%s: Can't set promiscuous mode: %s",
1642 device
, pcap_statustostr(status
));
1644 status
= pcap_set_rfmon(pd
, 1);
1646 error("%s: Can't set monitor mode: %s",
1647 device
, pcap_statustostr(status
));
1649 status
= pcap_set_timeout(pd
, 1000);
1651 error("%s: pcap_set_timeout failed: %s",
1652 device
, pcap_statustostr(status
));
1654 status
= pcap_set_buffer_size(pd
, Bflag
);
1656 error("%s: Can't set buffer size: %s",
1657 device
, pcap_statustostr(status
));
1659 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1661 status
= pcap_set_tstamp_type(pd
, jflag
);
1663 error("%s: Can't set time stamp type: %s",
1664 device
, pcap_statustostr(status
));
1667 status
= pcap_activate(pd
);
1670 * pcap_activate() failed.
1672 cp
= pcap_geterr(pd
);
1673 if (status
== PCAP_ERROR
)
1675 else if ((status
== PCAP_ERROR_NO_SUCH_DEVICE
||
1676 status
== PCAP_ERROR_PERM_DENIED
) &&
1678 error("%s: %s\n(%s)", device
,
1679 pcap_statustostr(status
), cp
);
1681 error("%s: %s", device
,
1682 pcap_statustostr(status
));
1683 } else if (status
> 0) {
1685 * pcap_activate() succeeded, but it's warning us
1686 * of a problem it had.
1688 cp
= pcap_geterr(pd
);
1689 if (status
== PCAP_WARNING
)
1691 else if (status
== PCAP_WARNING_PROMISC_NOTSUP
&&
1693 warning("%s: %s\n(%s)", device
,
1694 pcap_statustostr(status
), cp
);
1696 warning("%s: %s", device
,
1697 pcap_statustostr(status
));
1699 #ifdef HAVE_PCAP_SETDIRECTION
1701 status
= pcap_setdirection(pd
, Qflag
);
1703 error("%s: pcap_setdirection() failed: %s",
1704 device
, pcap_geterr(pd
));
1706 #endif /* HAVE_PCAP_SETDIRECTION */
1709 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
1713 warning("%s", ebuf
);
1714 #endif /* HAVE_PCAP_CREATE */
1716 * Let user own process after socket has been opened.
1719 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
1720 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1722 #if !defined(HAVE_PCAP_CREATE) && defined(WIN32)
1724 if(pcap_setbuff(pd
, Bflag
)==-1){
1725 error("%s", pcap_geterr(pd
));
1727 #endif /* !defined(HAVE_PCAP_CREATE) && defined(WIN32) */
1729 show_dlts_and_exit(device
, pd
);
1730 if (gndo
->ndo_dlt
>= 0) {
1731 #ifdef HAVE_PCAP_SET_DATALINK
1732 if (pcap_set_datalink(pd
, gndo
->ndo_dlt
) < 0)
1733 error("%s", pcap_geterr(pd
));
1736 * We don't actually support changing the
1737 * data link type, so we only let them
1738 * set it to what it already is.
1740 if (gndo
->ndo_dlt
!= pcap_datalink(pd
)) {
1741 error("%s is not one of the DLTs supported by this device\n",
1745 (void)fprintf(stderr
, "%s: data link type %s\n",
1746 program_name
, gndo
->ndo_dltname
);
1747 (void)fflush(stderr
);
1749 i
= pcap_snapshot(pd
);
1751 warning("snaplen raised from %d to %d", snaplen
, i
);
1755 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
1756 warning("foreign (-f) flag used but: %s", ebuf
);
1762 cmdbuf
= read_infile(infile
);
1764 cmdbuf
= copy_argv(&argv
[optind
]);
1766 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
1767 error("%s", pcap_geterr(pd
));
1769 bpf_dump(&fcode
, dflag
);
1774 init_addrtoname(gndo
, localnet
, netmask
);
1778 (void)setsignal(SIGPIPE
, cleanup
);
1779 (void)setsignal(SIGTERM
, cleanup
);
1780 (void)setsignal(SIGINT
, cleanup
);
1782 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1783 (void)setsignal(SIGCHLD
, child_cleanup
);
1785 /* Cooperate with nohup(1) */
1787 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
1788 (void)setsignal(SIGHUP
, oldhandler
);
1793 * If a user name was specified with "-Z", attempt to switch to
1794 * that user's UID. This would probably be used with sudo,
1795 * to allow tcpdump to be run in a special restricted
1796 * account (if you just want to allow users to open capture
1797 * devices, and can't just give users that permission,
1798 * you'd make tcpdump set-UID or set-GID).
1800 * Tcpdump doesn't necessarily write only to one savefile;
1801 * the general only way to allow a -Z instance to write to
1802 * savefiles as the user under whose UID it's run, rather
1803 * than as the user specified with -Z, would thus be to switch
1804 * to the original user ID before opening a capture file and
1805 * then switch back to the -Z user ID after opening the savefile.
1806 * Switching to the -Z user ID only after opening the first
1807 * savefile doesn't handle the general case.
1810 if (getuid() == 0 || geteuid() == 0) {
1811 #ifdef HAVE_LIBCAP_NG
1812 /* Initialize capng */
1813 capng_clear(CAPNG_SELECT_BOTH
);
1817 CAPNG_PERMITTED
| CAPNG_EFFECTIVE
,
1826 CAPNG_PERMITTED
| CAPNG_EFFECTIVE
,
1830 capng_apply(CAPNG_SELECT_BOTH
);
1831 #endif /* HAVE_LIBCAP_NG */
1832 if (username
|| chroot_dir
)
1833 droproot(username
, chroot_dir
);
1838 if (pcap_setfilter(pd
, &fcode
) < 0)
1839 error("%s", pcap_geterr(pd
));
1840 #ifdef HAVE_CAPSICUM
1841 if (RFileName
== NULL
&& VFileName
== NULL
) {
1842 static const unsigned long cmds
[] = { BIOCGSTATS
};
1844 cap_rights_init(&rights
, CAP_IOCTL
, CAP_READ
);
1845 if (cap_rights_limit(pcap_fileno(pd
), &rights
) < 0 &&
1847 error("unable to limit pcap descriptor");
1849 if (cap_ioctls_limit(pcap_fileno(pd
), cmds
,
1850 sizeof(cmds
) / sizeof(cmds
[0])) < 0 && errno
!= ENOSYS
) {
1851 error("unable to limit ioctls on pcap descriptor");
1857 /* Do not exceed the default PATH_MAX for files. */
1858 dumpinfo
.CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
1860 if (dumpinfo
.CurrentFileName
== NULL
)
1861 error("malloc of dumpinfo.CurrentFileName");
1863 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1865 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, WflagChars
);
1867 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, 0);
1869 p
= pcap_dump_open(pd
, dumpinfo
.CurrentFileName
);
1870 #ifdef HAVE_LIBCAP_NG
1871 /* Give up CAP_DAC_OVERRIDE capability.
1872 * Only allow it to be restored if the -C or -G flag have been
1873 * set since we may need to create more files later on.
1877 (Cflag
|| Gflag
? 0 : CAPNG_PERMITTED
)
1881 capng_apply(CAPNG_SELECT_BOTH
);
1882 #endif /* HAVE_LIBCAP_NG */
1884 error("%s", pcap_geterr(pd
));
1885 #ifdef HAVE_CAPSICUM
1886 set_dump_fd_capsicum_rights(fileno(pcap_dump_file(p
)));
1888 if (Cflag
!= 0 || Gflag
!= 0) {
1889 #ifdef HAVE_CAPSICUM
1890 dumpinfo
.WFileName
= strdup(basename(WFileName
));
1891 dumpinfo
.dirfd
= open(dirname(WFileName
),
1892 O_DIRECTORY
| O_RDONLY
);
1893 if (dumpinfo
.dirfd
< 0) {
1894 error("unable to open directory %s",
1895 dirname(WFileName
));
1897 cap_rights_init(&rights
, CAP_CREATE
, CAP_FCNTL
,
1898 CAP_FTRUNCATE
, CAP_LOOKUP
, CAP_SEEK
, CAP_WRITE
);
1899 if (cap_rights_limit(dumpinfo
.dirfd
, &rights
) < 0 &&
1901 error("unable to limit directory rights");
1903 if (cap_fcntls_limit(dumpinfo
.dirfd
, CAP_FCNTL_GETFL
) < 0 &&
1905 error("unable to limit dump descriptor fcntls");
1907 #else /* !HAVE_CAPSICUM */
1908 dumpinfo
.WFileName
= WFileName
;
1910 callback
= dump_packet_and_trunc
;
1913 pcap_userdata
= (u_char
*)&dumpinfo
;
1915 callback
= dump_packet
;
1916 pcap_userdata
= (u_char
*)p
;
1918 #ifdef HAVE_PCAP_DUMP_FLUSH
1923 type
= pcap_datalink(pd
);
1924 printinfo
= get_print_info(type
);
1925 callback
= print_packet
;
1926 pcap_userdata
= (u_char
*)&printinfo
;
1929 #ifdef SIGNAL_REQ_INFO
1931 * We can't get statistics when reading from a file rather
1932 * than capturing from a device.
1934 if (RFileName
== NULL
)
1935 (void)setsignal(SIGNAL_REQ_INFO
, requestinfo
);
1938 if (vflag
> 0 && WFileName
) {
1940 * When capturing to a file, "-v" means tcpdump should,
1941 * every 10 secodns, "v"erbosely report the number of
1944 #ifdef USE_WIN32_MM_TIMER
1945 /* call verbose_stats_dump() each 1000 +/-100msec */
1946 timer_id
= timeSetEvent(1000, 100, verbose_stats_dump
, 0, TIME_PERIODIC
);
1947 setvbuf(stderr
, NULL
, _IONBF
, 0);
1948 #elif defined(HAVE_ALARM)
1949 (void)setsignal(SIGALRM
, verbose_stats_dump
);
1955 if (RFileName
== NULL
) {
1957 * Live capture (if -V was specified, we set RFileName
1958 * to a file from the -V file). Print a message to
1959 * the standard error on UN*X.
1961 if (!vflag
&& !WFileName
) {
1962 (void)fprintf(stderr
,
1963 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1966 (void)fprintf(stderr
, "%s: ", program_name
);
1967 dlt
= pcap_datalink(pd
);
1968 dlt_name
= pcap_datalink_val_to_name(dlt
);
1969 if (dlt_name
== NULL
) {
1970 (void)fprintf(stderr
, "listening on %s, link-type %u, capture size %u bytes\n",
1971 device
, dlt
, snaplen
);
1973 (void)fprintf(stderr
, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1975 pcap_datalink_val_to_description(dlt
), snaplen
);
1977 (void)fflush(stderr
);
1981 #ifdef HAVE_CAPSICUM
1982 cansandbox
= (nflag
&& VFileName
== NULL
&& zflag
== NULL
);
1983 if (cansandbox
&& cap_enter() < 0 && errno
!= ENOSYS
)
1984 error("unable to enter the capability mode");
1985 if (cap_sandboxed())
1986 fprintf(stderr
, "capability mode sandbox enabled\n");
1987 #endif /* HAVE_CAPSICUM */
1990 status
= pcap_loop(pd
, cnt
, callback
, pcap_userdata
);
1991 if (WFileName
== NULL
) {
1993 * We're printing packets. Flush the printed output,
1994 * so it doesn't get intermingled with error output.
1998 * We got interrupted, so perhaps we didn't
1999 * manage to finish a line we were printing.
2000 * Print an extra newline, just in case.
2004 (void)fflush(stdout
);
2008 * We got interrupted. If we are reading multiple
2009 * files (via -V) set these so that we stop.
2018 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
2019 program_name
, pcap_geterr(pd
));
2021 if (RFileName
== NULL
) {
2023 * We're doing a live capture. Report the capture
2029 if (VFileName
!= NULL
) {
2030 ret
= get_next_file(VFile
, VFileLine
);
2032 RFileName
= VFileLine
;
2033 pd
= pcap_open_offline(RFileName
, ebuf
);
2036 #ifdef HAVE_CAPSICUM
2037 cap_rights_init(&rights
, CAP_READ
);
2038 if (cap_rights_limit(fileno(pcap_file(pd
)),
2039 &rights
) < 0 && errno
!= ENOSYS
) {
2040 error("unable to limit pcap descriptor");
2043 new_dlt
= pcap_datalink(pd
);
2044 if (WFileName
&& new_dlt
!= dlt
)
2045 error("%s: new dlt does not match original", RFileName
);
2046 printinfo
= get_print_info(new_dlt
);
2047 dlt_name
= pcap_datalink_val_to_name(new_dlt
);
2048 if (dlt_name
== NULL
) {
2049 fprintf(stderr
, "reading from file %s, link-type %u\n",
2050 RFileName
, new_dlt
);
2053 "reading from file %s, link-type %s (%s)\n",
2054 RFileName
, dlt_name
,
2055 pcap_datalink_val_to_description(new_dlt
));
2057 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
2058 error("%s", pcap_geterr(pd
));
2059 if (pcap_setfilter(pd
, &fcode
) < 0)
2060 error("%s", pcap_geterr(pd
));
2064 while (ret
!= NULL
);
2067 exit(status
== -1 ? 1 : 0);
2070 /* make a clean exit on interrupts */
2072 cleanup(int signo _U_
)
2074 #ifdef USE_WIN32_MM_TIMER
2076 timeKillEvent(timer_id
);
2078 #elif defined(HAVE_ALARM)
2082 #ifdef HAVE_PCAP_BREAKLOOP
2084 * We have "pcap_breakloop()"; use it, so that we do as little
2085 * as possible in the signal handler (it's probably not safe
2086 * to do anything with standard I/O streams in a signal handler -
2087 * the ANSI C standard doesn't say it is).
2092 * We don't have "pcap_breakloop()"; this isn't safe, but
2093 * it's the best we can do. Print the summary if we're
2094 * not reading from a savefile - i.e., if we're doing a
2095 * live capture - and exit.
2097 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
2099 * We got interrupted, so perhaps we didn't
2100 * manage to finish a line we were printing.
2101 * Print an extra newline, just in case.
2104 (void)fflush(stdout
);
2112 On windows, we do not use a fork, so we do not care less about
2113 waiting a child processes to die
2115 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
2117 child_cleanup(int signo _U_
)
2121 #endif /* HAVE_FORK && HAVE_VFORK */
2124 info(register int verbose
)
2126 struct pcap_stat stat
;
2129 * Older versions of libpcap didn't set ps_ifdrop on some
2130 * platforms; initialize it to 0 to handle that.
2133 if (pcap_stats(pd
, &stat
) < 0) {
2134 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
2140 fprintf(stderr
, "%s: ", program_name
);
2142 (void)fprintf(stderr
, "%u packet%s captured", packets_captured
,
2143 PLURAL_SUFFIX(packets_captured
));
2145 fputs(", ", stderr
);
2148 (void)fprintf(stderr
, "%u packet%s received by filter", stat
.ps_recv
,
2149 PLURAL_SUFFIX(stat
.ps_recv
));
2151 fputs(", ", stderr
);
2154 (void)fprintf(stderr
, "%u packet%s dropped by kernel", stat
.ps_drop
,
2155 PLURAL_SUFFIX(stat
.ps_drop
));
2156 if (stat
.ps_ifdrop
!= 0) {
2158 fputs(", ", stderr
);
2161 (void)fprintf(stderr
, "%u packet%s dropped by interface\n",
2162 stat
.ps_ifdrop
, PLURAL_SUFFIX(stat
.ps_ifdrop
));
2168 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
2170 compress_savefile(const char *filename
)
2179 * Set to lowest priority so that this doesn't disturb the capture
2182 setpriority(PRIO_PROCESS
, 0, NZERO
- 1);
2184 setpriority(PRIO_PROCESS
, 0, 19);
2186 if (execlp(zflag
, zflag
, filename
, (char *)NULL
) == -1)
2188 "compress_savefile:execlp(%s, %s): %s\n",
2198 #else /* HAVE_FORK && HAVE_VFORK */
2200 compress_savefile(const char *filename
)
2203 "compress_savefile failed. Functionality not implemented under your system\n");
2205 #endif /* HAVE_FORK && HAVE_VFORK */
2208 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
2210 struct dump_info
*dump_info
;
2211 #ifdef HAVE_CAPSICUM
2212 cap_rights_t rights
;
2219 dump_info
= (struct dump_info
*)user
;
2222 * XXX - this won't force the file to rotate on the specified time
2223 * boundary, but it will rotate on the first packet received after the
2224 * specified Gflag number of seconds. Note: if a Gflag time boundary
2225 * and a Cflag size boundary coincide, the time rotation will occur
2226 * first thereby cancelling the Cflag boundary (since the file should
2230 /* Check if it is time to rotate */
2233 /* Get the current time */
2234 if ((t
= time(NULL
)) == (time_t)-1) {
2235 error("dump_and_trunc_packet: can't get current_time: %s",
2236 pcap_strerror(errno
));
2240 /* If the time is greater than the specified window, rotate */
2241 if (t
- Gflag_time
>= Gflag
) {
2242 #ifdef HAVE_CAPSICUM
2247 /* Update the Gflag_time */
2249 /* Update Gflag_count */
2252 * Close the current file and open a new one.
2254 pcap_dump_close(dump_info
->p
);
2257 * Compress the file we just closed, if the user asked for it
2260 compress_savefile(dump_info
->CurrentFileName
);
2263 * Check to see if we've exceeded the Wflag (when
2266 if (Cflag
== 0 && Wflag
> 0 && Gflag_count
>= Wflag
) {
2267 (void)fprintf(stderr
, "Maximum file limit reached: %d\n",
2272 if (dump_info
->CurrentFileName
!= NULL
)
2273 free(dump_info
->CurrentFileName
);
2274 /* Allocate space for max filename + \0. */
2275 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
2276 if (dump_info
->CurrentFileName
== NULL
)
2277 error("dump_packet_and_trunc: malloc");
2279 * Gflag was set otherwise we wouldn't be here. Reset the count
2280 * so multiple files would end with 1,2,3 in the filename.
2281 * The counting is handled with the -C flow after this.
2286 * This is always the first file in the Cflag
2288 * We also don't need numbering if Cflag is not set.
2291 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0,
2294 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0, 0);
2296 #ifdef HAVE_LIBCAP_NG
2297 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
2298 capng_apply(CAPNG_SELECT_BOTH
);
2299 #endif /* HAVE_LIBCAP_NG */
2300 #ifdef HAVE_CAPSICUM
2301 fd
= openat(dump_info
->dirfd
,
2302 dump_info
->CurrentFileName
,
2303 O_CREAT
| O_WRONLY
| O_TRUNC
, 0644);
2305 error("unable to open file %s",
2306 dump_info
->CurrentFileName
);
2308 fp
= fdopen(fd
, "w");
2310 error("unable to fdopen file %s",
2311 dump_info
->CurrentFileName
);
2313 dump_info
->p
= pcap_dump_fopen(dump_info
->pd
, fp
);
2314 #else /* !HAVE_CAPSICUM */
2315 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
2317 #ifdef HAVE_LIBCAP_NG
2318 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
2319 capng_apply(CAPNG_SELECT_BOTH
);
2320 #endif /* HAVE_LIBCAP_NG */
2321 if (dump_info
->p
== NULL
)
2322 error("%s", pcap_geterr(pd
));
2323 #ifdef HAVE_CAPSICUM
2324 set_dump_fd_capsicum_rights(pcap_dump_file(dump_info
->p
)));
2330 * XXX - this won't prevent capture files from getting
2331 * larger than Cflag - the last packet written to the
2332 * file could put it over Cflag.
2335 long size
= pcap_dump_ftell(dump_info
->p
);
2338 error("ftell fails on output file");
2340 #ifdef HAVE_CAPSICUM
2346 * Close the current file and open a new one.
2348 pcap_dump_close(dump_info
->p
);
2351 * Compress the file we just closed, if the user
2355 compress_savefile(dump_info
->CurrentFileName
);
2359 if (Cflag_count
>= Wflag
)
2362 if (dump_info
->CurrentFileName
!= NULL
)
2363 free(dump_info
->CurrentFileName
);
2364 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
2365 if (dump_info
->CurrentFileName
== NULL
)
2366 error("dump_packet_and_trunc: malloc");
2367 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, Cflag_count
, WflagChars
);
2368 #ifdef HAVE_LIBCAP_NG
2369 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
2370 capng_apply(CAPNG_SELECT_BOTH
);
2371 #endif /* HAVE_LIBCAP_NG */
2372 #ifdef HAVE_CAPSICUM
2373 fd
= openat(dump_info
->dirfd
, dump_info
->CurrentFileName
,
2374 O_CREAT
| O_WRONLY
| O_TRUNC
, 0644);
2376 error("unable to open file %s",
2377 dump_info
->CurrentFileName
);
2379 fp
= fdopen(fd
, "w");
2381 error("unable to fdopen file %s",
2382 dump_info
->CurrentFileName
);
2384 dump_info
->p
= pcap_dump_fopen(dump_info
->pd
, fp
);
2385 #else /* !HAVE_CAPSICUM */
2386 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
2388 #ifdef HAVE_LIBCAP_NG
2389 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
2390 capng_apply(CAPNG_SELECT_BOTH
);
2391 #endif /* HAVE_LIBCAP_NG */
2392 if (dump_info
->p
== NULL
)
2393 error("%s", pcap_geterr(pd
));
2394 #ifdef HAVE_CAPSICUM
2395 set_dump_fd_capsicum_rights(fileno(pcap_dump_file(dump_info
->p
)));
2400 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
2401 #ifdef HAVE_PCAP_DUMP_FLUSH
2403 pcap_dump_flush(dump_info
->p
);
2412 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
2418 pcap_dump(user
, h
, sp
);
2419 #ifdef HAVE_PCAP_DUMP_FLUSH
2421 pcap_dump_flush((pcap_dumper_t
*)user
);
2430 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
2432 struct print_info
*print_info
;
2434 netdissect_options
*ndo
;
2440 print_info
= (struct print_info
*)user
;
2441 ndo
= print_info
->ndo
;
2443 if(ndo
->ndo_packet_number
)
2444 ND_PRINT((ndo
, "%5u ", packets_captured
));
2446 ts_print(ndo
, &h
->ts
);
2449 * Some printers want to check that they're not walking off the
2450 * end of the packet.
2451 * Rather than pass it all the way down, we set this member
2452 * of the netdissect_options structure.
2454 ndo
->ndo_snapend
= sp
+ h
->caplen
;
2456 if(print_info
->ndo_type
) {
2457 hdrlen
= (*print_info
->p
.ndo_printer
)(print_info
->ndo
, h
, sp
);
2459 hdrlen
= (*print_info
->p
.printer
)(h
, sp
);
2463 * Restore the original snapend, as a printer might have
2466 ndo
->ndo_snapend
= sp
+ h
->caplen
;
2467 if (ndo
->ndo_Xflag
) {
2469 * Print the raw packet data in hex and ASCII.
2471 if (ndo
->ndo_Xflag
> 1) {
2473 * Include the link-layer header.
2475 hex_and_ascii_print(ndo
, "\n\t", sp
, h
->caplen
);
2478 * Don't include the link-layer header - and if
2479 * we have nothing past the link-layer header,
2482 if (h
->caplen
> hdrlen
)
2483 hex_and_ascii_print(ndo
, "\n\t", sp
+ hdrlen
,
2484 h
->caplen
- hdrlen
);
2486 } else if (ndo
->ndo_xflag
) {
2488 * Print the raw packet data in hex.
2490 if (ndo
->ndo_xflag
> 1) {
2492 * Include the link-layer header.
2494 hex_print(ndo
, "\n\t", sp
, h
->caplen
);
2497 * Don't include the link-layer header - and if
2498 * we have nothing past the link-layer header,
2501 if (h
->caplen
> hdrlen
)
2502 hex_print(ndo
, "\n\t", sp
+ hdrlen
,
2503 h
->caplen
- hdrlen
);
2505 } else if (ndo
->ndo_Aflag
) {
2507 * Print the raw packet data in ASCII.
2509 if (ndo
->ndo_Aflag
> 1) {
2511 * Include the link-layer header.
2513 ascii_print(ndo
, sp
, h
->caplen
);
2516 * Don't include the link-layer header - and if
2517 * we have nothing past the link-layer header,
2520 if (h
->caplen
> hdrlen
)
2521 ascii_print(ndo
, sp
+ hdrlen
, h
->caplen
- hdrlen
);
2534 * XXX - there should really be libpcap calls to get the version
2535 * number as a string (the string would be generated from #defines
2536 * at run time, so that it's not generated from string constants
2537 * in the library, as, on many UNIX systems, those constants would
2538 * be statically linked into the application executable image, and
2539 * would thus reflect the version of libpcap on the system on
2540 * which the application was *linked*, not the system on which it's
2543 * That routine should be documented, unlike the "version[]"
2544 * string, so that UNIX vendors providing their own libpcaps
2545 * don't omit it (as a couple of vendors have...).
2547 * Packet.dll should perhaps also export a routine to return the
2548 * version number of the Packet.dll code, to supply the
2549 * "Wpcap_version" information on Windows.
2551 char WDversion
[]="current-git.tcpdump.org";
2552 #if !defined(HAVE_GENERATED_VERSION)
2553 char version
[]="current-git.tcpdump.org";
2555 char pcap_version
[]="current-git.tcpdump.org";
2556 char Wpcap_version
[]="3.1";
2560 * By default, print the specified data out in hex and ASCII.
2563 ndo_default_print(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
2565 hex_and_ascii_print(ndo
, "\n\t", bp
, length
); /* pass on lf and indentation string */
2569 default_print(const u_char
*bp
, u_int length
)
2571 ndo_default_print(gndo
, bp
, length
);
2574 #ifdef SIGNAL_REQ_INFO
2575 RETSIGTYPE
requestinfo(int signo _U_
)
2585 * Called once each second in verbose mode while dumping to file
2587 #ifdef USE_WIN32_MM_TIMER
2588 void CALLBACK
verbose_stats_dump (UINT timer_id _U_
, UINT msg _U_
, DWORD_PTR arg _U_
,
2589 DWORD_PTR dw1 _U_
, DWORD_PTR dw2 _U_
)
2591 struct pcap_stat stat
;
2593 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
2594 fprintf(stderr
, "Got %u\r", packets_captured
);
2596 #elif defined(HAVE_ALARM)
2597 static void verbose_stats_dump(int sig _U_
)
2599 struct pcap_stat stat
;
2601 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
2602 fprintf(stderr
, "Got %u\r", packets_captured
);
2607 USES_APPLE_DEPRECATED_API
2611 extern char version
[];
2612 #ifndef HAVE_PCAP_LIB_VERSION
2613 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
2614 extern char pcap_version
[];
2615 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2616 static char pcap_version
[] = "unknown";
2617 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2618 #endif /* HAVE_PCAP_LIB_VERSION */
2620 #ifdef HAVE_PCAP_LIB_VERSION
2622 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2624 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2626 (void)fprintf(stderr
, "%s\n",pcap_lib_version());
2627 #else /* HAVE_PCAP_LIB_VERSION */
2629 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2630 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
2632 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2633 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
2635 #endif /* HAVE_PCAP_LIB_VERSION */
2637 #if defined(HAVE_LIBCRYPTO) && defined(SSLEAY_VERSION)
2638 (void)fprintf (stderr
, "%s\n", SSLeay_version(SSLEAY_VERSION
));
2642 (void)fprintf (stderr
, "SMI-library: %s\n", smi_version_string
);
2651 (void)fprintf(stderr
,
2652 "Usage: %s [-aAbd" D_FLAG
"efhH" I_FLAG J_FLAG
"KlLnNOpqRStu" U_FLAG
"vxX#]" B_FLAG_USAGE
" [ -c count ]\n", program_name
);
2653 (void)fprintf(stderr
,
2654 "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n");
2655 (void)fprintf(stderr
,
2656 "\t\t[ -i interface ]" j_FLAG_USAGE
" [ -M secret ] [ --number ]\n");
2657 #ifdef HAVE_PCAP_SETDIRECTION
2658 (void)fprintf(stderr
,
2659 "\t\t[ -Q in|out|inout ]\n");
2661 (void)fprintf(stderr
,
2662 "\t\t[ -r file ] [ -s snaplen ] ");
2663 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
2664 (void)fprintf(stderr
, "[ --time-stamp-precision precision ]\n");
2665 (void)fprintf(stderr
,
2668 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
2669 (void)fprintf(stderr
, "[ --immediate-mode ] ");
2671 (void)fprintf(stderr
, "[ -T type ] [ --version ] [ -V file ]\n");
2672 (void)fprintf(stderr
,
2673 "\t\t[ -w file ] [ -W filecount ] [ -y datalinktype ] [ -z command ]\n");
2674 (void)fprintf(stderr
,
2675 "\t\t[ -Z user ] [ expression ]\n");
2682 ndo_error(netdissect_options
*ndo _U_
, const char *fmt
, ...)
2686 (void)fprintf(stderr
, "%s: ", program_name
);
2688 (void)vfprintf(stderr
, fmt
, ap
);
2692 if (fmt
[-1] != '\n')
2693 (void)fputc('\n', stderr
);
2701 ndo_warning(netdissect_options
*ndo _U_
, const char *fmt
, ...)
2705 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
2707 (void)vfprintf(stderr
, fmt
, ap
);
2711 if (fmt
[-1] != '\n')
2712 (void)fputc('\n', stderr
);
2717 * c-style: whitesmith