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
611 * OS X tcpdump also uses -Q to specify expressions that match packet
612 * metadata, including but not limited to the packet direction.
613 * The expression syntax is different from a simple "in|out|inout",
614 * and those expressions aren't accepted by OS X tcpdump, but the
615 * equivalents would be "in" = "dir=in", "out" = "dir=out", and
616 * "inout" = "dir=in or dir=out", and the parser could conceivably
617 * special-case "in", "out", and "inout" as expressions for backwards
618 * compatibility, so all is not (yet) lost.
622 * Set up flags that might or might not be supported depending on the
623 * version of libpcap we're using.
625 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
627 #define B_FLAG_USAGE " [ -B size ]"
628 #else /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
631 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
633 #ifdef HAVE_PCAP_CREATE
635 #else /* HAVE_PCAP_CREATE */
637 #endif /* HAVE_PCAP_CREATE */
639 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
641 #define j_FLAG_USAGE " [ -j tstamptype ]"
643 #else /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
647 #endif /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
649 #ifdef HAVE_PCAP_FINDALLDEVS
655 #ifdef HAVE_PCAP_DUMP_FLUSH
661 #ifdef HAVE_PCAP_SETDIRECTION
667 #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:#"
672 * We do not currently have long options corresponding to all short
673 * options; we should probably pick appropriate option names for them.
675 * However, the short options where the number of times the option is
676 * specified matters, such as -v and -d and -t, should probably not
677 * just map to a long option, as saying
679 * tcpdump --verbose --verbose
681 * doesn't make sense; it should be --verbosity={N} or something such
684 * For long options with no corresponding short options, we define values
685 * outside the range of ASCII graphic characters, make that the last
686 * component of the entry for the long option, and have a case for that
687 * option in the switch statement.
689 #define OPTION_VERSION 128
690 #define OPTION_TSTAMP_PRECISION 129
691 #define OPTION_IMMEDIATE_MODE 130
693 static const struct option longopts
[] = {
694 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
695 { "buffer-size", required_argument
, NULL
, 'B' },
697 { "list-interfaces", no_argument
, NULL
, 'D' },
698 { "help", no_argument
, NULL
, 'h' },
699 { "interface", required_argument
, NULL
, 'i' },
700 #ifdef HAVE_PCAP_CREATE
701 { "monitor-mode", no_argument
, NULL
, 'I' },
703 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
704 { "time-stamp-type", required_argument
, NULL
, 'j' },
705 { "list-time-stamp-types", no_argument
, NULL
, 'J' },
707 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
708 { "time-stamp-precision", required_argument
, NULL
, OPTION_TSTAMP_PRECISION
},
710 { "dont-verify-checksums", no_argument
, NULL
, 'K' },
711 { "list-data-link-types", no_argument
, NULL
, 'L' },
712 { "no-optimize", no_argument
, NULL
, 'O' },
713 { "no-promiscuous-mode", no_argument
, NULL
, 'p' },
714 #ifdef HAVE_PCAP_SETDIRECTION
715 { "direction", required_argument
, NULL
, 'Q' },
717 { "snapshot-length", required_argument
, NULL
, 's' },
718 { "absolute-tcp-sequence-numbers", no_argument
, NULL
, 'S' },
719 #ifdef HAVE_PCAP_DUMP_FLUSH
720 { "packet-buffered", no_argument
, NULL
, 'U' },
722 { "linktype", required_argument
, NULL
, 'y' },
723 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
724 { "immediate-mode", no_argument
, NULL
, OPTION_IMMEDIATE_MODE
},
726 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
727 { "debug-filter-parser", no_argument
, NULL
, 'Y' },
729 { "relinquish-privileges", required_argument
, NULL
, 'Z' },
730 { "number", no_argument
, NULL
, '#' },
731 { "version", no_argument
, NULL
, OPTION_VERSION
},
736 /* Drop root privileges and chroot if necessary */
738 droproot(const char *username
, const char *chroot_dir
)
740 struct passwd
*pw
= NULL
;
742 if (chroot_dir
&& !username
) {
743 fprintf(stderr
, "tcpdump: Chroot without dropping root is insecure\n");
747 pw
= getpwnam(username
);
750 if (chroot(chroot_dir
) != 0 || chdir ("/") != 0) {
751 fprintf(stderr
, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
752 chroot_dir
, pcap_strerror(errno
));
756 #ifdef HAVE_LIBCAP_NG
757 int ret
= capng_change_id(pw
->pw_uid
, pw
->pw_gid
, CAPNG_NO_FLAG
);
759 fprintf(stderr
, "error : ret %d\n", ret
);
762 fprintf(stderr
, "dropped privs to %s\n", username
);
765 if (initgroups(pw
->pw_name
, pw
->pw_gid
) != 0 ||
766 setgid(pw
->pw_gid
) != 0 || setuid(pw
->pw_uid
) != 0) {
767 fprintf(stderr
, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
769 (unsigned long)pw
->pw_uid
,
770 (unsigned long)pw
->pw_gid
,
771 pcap_strerror(errno
));
775 fprintf(stderr
, "dropped privs to %s\n", username
);
777 #endif /* HAVE_LIBCAP_NG */
780 fprintf(stderr
, "tcpdump: Couldn't find user '%.32s'\n",
784 #ifdef HAVE_LIBCAP_NG
785 /* We don't need CAP_SETUID and CAP_SETGID any more. */
788 CAPNG_EFFECTIVE
| CAPNG_PERMITTED
,
792 capng_apply(CAPNG_SELECT_BOTH
);
793 #endif /* HAVE_LIBCAP_NG */
814 MakeFilename(char *buffer
, char *orig_name
, int cnt
, int max_chars
)
816 char *filename
= malloc(PATH_MAX
+ 1);
817 if (filename
== NULL
)
818 error("Makefilename: malloc");
820 /* Process with strftime if Gflag is set. */
824 /* Convert Gflag_time to a usable format */
825 if ((local_tm
= localtime(&Gflag_time
)) == NULL
) {
826 error("MakeTimedFilename: localtime");
829 /* There's no good way to detect an error in strftime since a return
830 * value of 0 isn't necessarily failure.
832 strftime(filename
, PATH_MAX
, orig_name
, local_tm
);
834 strncpy(filename
, orig_name
, PATH_MAX
);
837 if (cnt
== 0 && max_chars
== 0)
838 strncpy(buffer
, filename
, PATH_MAX
+ 1);
840 if (snprintf(buffer
, PATH_MAX
+ 1, "%s%0*d", filename
, max_chars
, cnt
) > PATH_MAX
)
841 /* Report an error if the filename is too large */
842 error("too many output files or filename is too long (> %d)", PATH_MAX
);
846 static int tcpdump_printf(netdissect_options
*ndo _U_
,
847 const char *fmt
, ...)
854 ret
=vfprintf(stdout
, fmt
, args
);
860 static struct print_info
861 get_print_info(int type
)
863 struct print_info printinfo
;
865 printinfo
.ndo_type
= 1;
866 printinfo
.ndo
= gndo
;
867 printinfo
.p
.ndo_printer
= lookup_ndo_printer(type
);
868 if (printinfo
.p
.ndo_printer
== NULL
) {
869 printinfo
.p
.printer
= lookup_printer(type
);
870 printinfo
.ndo_type
= 0;
871 if (printinfo
.p
.printer
== NULL
) {
872 gndo
->ndo_dltname
= pcap_datalink_val_to_name(type
);
873 if (gndo
->ndo_dltname
!= NULL
)
874 error("packet printing is not supported for link type %s: use -w",
877 error("packet printing is not supported for link type %d: use -w", type
);
884 get_next_file(FILE *VFile
, char *ptr
)
888 ret
= fgets(ptr
, PATH_MAX
, VFile
);
892 if (ptr
[strlen(ptr
) - 1] == '\n')
893 ptr
[strlen(ptr
) - 1] = '\0';
898 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
900 tstamp_precision_from_string(const char *precision
)
902 if (strncmp(precision
, "nano", strlen("nano")) == 0)
903 return PCAP_TSTAMP_PRECISION_NANO
;
905 if (strncmp(precision
, "micro", strlen("micro")) == 0)
906 return PCAP_TSTAMP_PRECISION_MICRO
;
912 tstamp_precision_to_string(int precision
)
916 case PCAP_TSTAMP_PRECISION_MICRO
:
919 case PCAP_TSTAMP_PRECISION_NANO
:
930 * Ensure that, on a dump file's descriptor, we have all the rights
931 * necessary to make the standard I/O library work with an fdopen()ed
932 * FILE * from that descriptor.
934 * A long time ago, in a galaxy far far away, AT&T decided that, instead
935 * of providing separate APIs for getting and setting the FD_ flags on a
936 * descriptor, getting and setting the O_ flags on a descriptor, and
937 * locking files, they'd throw them all into a kitchen-sink fcntl() call
938 * along the lines of ioctl(), the fact that ioctl() operations are
939 * largely specific to particular character devices but fcntl() operations
940 * are either generic to all descriptors or generic to all descriptors for
941 * regular files nonwithstanding.
943 * The Capsicum people decided that fine-grained control of descriptor
944 * operations was required, so that you need to grant permission for
945 * reading, writing, seeking, and fcntl-ing. The latter, courtesy of
946 * AT&T's decision, means that "fcntl-ing" isn't a thing, but a motley
947 * collection of things, so there are *individual* fcntls for which
948 * permission needs to be granted.
950 * The FreeBSD standard I/O people implemented some optimizations that
951 * requires that the standard I/O routines be able to determine whether
952 * the descriptor for the FILE * is open append-only or not; as that
953 * descriptor could have come from an open() rather than an fopen(),
954 * that requires that it be able to do an F_GETFL fcntl() to read
957 * Tcpdump uses ftell() to determine how much data has been written
958 * to a file in order to, when used with -C, determine when it's time
959 * to rotate capture files. ftell() therefore needs to do an lseek()
960 * to find out the file offset and must, thanks to the aforementioned
961 * optimization, also know whether the descriptor is open append-only
964 * The net result of all the above is that we need to grant CAP_SEEK,
965 * CAP_WRITE, and CAP_FCNTL with the CAP_FCNTL_GETFL subcapability.
967 * Perhaps this is the universe's way of saying that either
969 * 1) there needs to be an fopenat() call and a pcap_dump_openat() call
970 * using it, so that Capsicum-capable tcpdump wouldn't need to do
975 * 2) there needs to be a cap_fdopen() call in the FreeBSD standard
976 * I/O library that knows what rights are needed by the standard
977 * I/O library, based on the open mode, and assigns them, perhaps
978 * with an additional argument indicating, for example, whether
979 * seeking should be allowed, so that tcpdump doesn't need to know
980 * what the standard I/O library happens to require this week.
983 set_dumper_capsicum_rights(pcap_dumper_t
*p
)
985 int fd
= fileno(pcap_dump_file(p
));
988 cap_rights_init(&rights
, CAP_SEEK
, CAP_WRITE
, CAP_FCNTL
);
989 if (cap_rights_limit(fd
, &rights
) < 0 && errno
!= ENOSYS
) {
990 error("unable to limit dump descriptor");
992 if (cap_fcntls_limit(fd
, CAP_FCNTL_GETFL
) < 0 && errno
!= ENOSYS
) {
993 error("unable to limit dump descriptor fcntls");
999 main(int argc
, char **argv
)
1001 register int cnt
, op
, i
;
1002 bpf_u_int32 localnet
=0 , netmask
= 0;
1003 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *VFileName
, *WFileName
;
1004 pcap_handler callback
;
1008 const char *dlt_name
;
1009 struct bpf_program fcode
;
1011 RETSIGTYPE (*oldhandler
)(int);
1013 struct print_info printinfo
;
1014 struct dump_info dumpinfo
;
1015 u_char
*pcap_userdata
;
1016 char ebuf
[PCAP_ERRBUF_SIZE
];
1017 char VFileLine
[PATH_MAX
+ 1];
1018 char *username
= NULL
;
1019 char *chroot_dir
= NULL
;
1022 #ifdef HAVE_PCAP_FINDALLDEVS
1023 pcap_if_t
*devpointer
;
1028 #ifdef HAVE_CAPSICUM
1029 cap_rights_t rights
;
1031 #endif /* HAVE_CAPSICUM */
1034 if(wsockinit() != 0) return 1;
1037 jflag
=-1; /* not set */
1041 gndo
->ndo_default_print
=ndo_default_print
;
1042 gndo
->ndo_printf
=tcpdump_printf
;
1043 gndo
->ndo_error
=ndo_error
;
1044 gndo
->ndo_warning
=ndo_warning
;
1045 gndo
->ndo_snaplen
= DEFAULT_SNAPLEN
;
1046 gndo
->ndo_immediate
= 0;
1056 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
1057 program_name
= cp
+ 1;
1059 program_name
= argv
[0];
1062 * On platforms where the CPU doesn't support unaligned loads,
1063 * force unaligned accesses to abort with SIGBUS, rather than
1064 * being fixed up (slowly) by the OS kernel; on those platforms,
1065 * misaligned accesses are bugs, and we want tcpdump to crash so
1066 * that the bugs are reported.
1068 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
1076 (op
= getopt_long(argc
, argv
, SHORTOPTS
, longopts
, NULL
)) != -1)
1080 /* compatibility for old -a */
1091 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
1093 Bflag
= atoi(optarg
)*1024;
1095 error("invalid packet buffer size %s", optarg
);
1097 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
1102 error("invalid packet count %s", optarg
);
1106 Cflag
= atoi(optarg
) * 1000000;
1108 error("invalid file size %s", optarg
);
1128 #ifndef HAVE_LIBCRYPTO
1129 warning("crypto code not compiled in");
1131 gndo
->ndo_espsecret
= optarg
;
1143 Gflag
= atoi(optarg
);
1145 error("invalid number of seconds %s", optarg
);
1147 /* We will create one file initially. */
1150 /* Grab the current time for rotation use. */
1151 if ((Gflag_time
= time(NULL
)) == (time_t)-1) {
1152 error("main: can't get current time: %s",
1153 pcap_strerror(errno
));
1167 if (optarg
[0] == '0' && optarg
[1] == 0)
1168 error("Invalid adapter index");
1170 #ifdef HAVE_PCAP_FINDALLDEVS
1172 * If the argument is a number, treat it as
1173 * an index into the list of adapters, as
1174 * printed by "tcpdump -D".
1176 * This should be OK on UNIX systems, as interfaces
1177 * shouldn't have names that begin with digits.
1178 * It can be useful on Windows, where more than
1179 * one interface can have the same name.
1181 devnum
= strtol(optarg
, &end
, 10);
1182 if (optarg
!= end
&& *end
== '\0') {
1184 error("Invalid adapter index");
1186 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
1190 * Look for the devnum-th entry
1191 * in the list of devices
1195 i
< devnum
-1 && devpointer
!= NULL
;
1196 i
++, devpointer
= devpointer
->next
)
1198 if (devpointer
== NULL
)
1199 error("Invalid adapter index");
1201 device
= devpointer
->name
;
1204 #endif /* HAVE_PCAP_FINDALLDEVS */
1208 #ifdef HAVE_PCAP_CREATE
1212 #endif /* HAVE_PCAP_CREATE */
1214 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1216 jflag
= pcap_tstamp_type_name_to_val(optarg
);
1218 error("invalid time stamp type %s", optarg
);
1229 * _IOLBF is the same as _IOFBF in Microsoft's C
1230 * libraries; the only alternative they offer
1233 * XXX - this should really be checking for MSVC++,
1234 * not WIN32, if, for example, MinGW has its own
1235 * C library that is more UNIX-compatible.
1237 setvbuf(stdout
, NULL
, _IONBF
, 0);
1239 #ifdef HAVE_SETLINEBUF
1242 setvbuf(stdout
, NULL
, _IOLBF
, 0);
1253 if (smiLoadModule(optarg
) == 0) {
1254 error("could not load MIB module %s", optarg
);
1258 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
1259 program_name
, optarg
);
1260 (void)fprintf(stderr
, "(no libsmi support)\n");
1265 /* TCP-MD5 shared secret */
1266 #ifndef HAVE_LIBCRYPTO
1267 warning("crypto code not compiled in");
1290 ++suppress_default_print
;
1293 #ifdef HAVE_PCAP_SETDIRECTION
1295 if (strcasecmp(optarg
, "in") == 0)
1297 else if (strcasecmp(optarg
, "out") == 0)
1299 else if (strcasecmp(optarg
, "inout") == 0)
1300 Qflag
= PCAP_D_INOUT
;
1302 error("unknown capture direction `%s'", optarg
);
1304 #endif /* HAVE_PCAP_SETDIRECTION */
1315 snaplen
= strtol(optarg
, &end
, 0);
1316 if (optarg
== end
|| *end
!= '\0'
1317 || snaplen
< 0 || snaplen
> MAXIMUM_SNAPLEN
)
1318 error("invalid snaplen %s", optarg
);
1319 else if (snaplen
== 0)
1320 snaplen
= MAXIMUM_SNAPLEN
;
1332 if (strcasecmp(optarg
, "vat") == 0)
1333 packettype
= PT_VAT
;
1334 else if (strcasecmp(optarg
, "wb") == 0)
1336 else if (strcasecmp(optarg
, "rpc") == 0)
1337 packettype
= PT_RPC
;
1338 else if (strcasecmp(optarg
, "rtp") == 0)
1339 packettype
= PT_RTP
;
1340 else if (strcasecmp(optarg
, "rtcp") == 0)
1341 packettype
= PT_RTCP
;
1342 else if (strcasecmp(optarg
, "snmp") == 0)
1343 packettype
= PT_SNMP
;
1344 else if (strcasecmp(optarg
, "cnfp") == 0)
1345 packettype
= PT_CNFP
;
1346 else if (strcasecmp(optarg
, "tftp") == 0)
1347 packettype
= PT_TFTP
;
1348 else if (strcasecmp(optarg
, "aodv") == 0)
1349 packettype
= PT_AODV
;
1350 else if (strcasecmp(optarg
, "carp") == 0)
1351 packettype
= PT_CARP
;
1352 else if (strcasecmp(optarg
, "radius") == 0)
1353 packettype
= PT_RADIUS
;
1354 else if (strcasecmp(optarg
, "zmtp1") == 0)
1355 packettype
= PT_ZMTP1
;
1356 else if (strcasecmp(optarg
, "vxlan") == 0)
1357 packettype
= PT_VXLAN
;
1358 else if (strcasecmp(optarg
, "pgm") == 0)
1359 packettype
= PT_PGM
;
1360 else if (strcasecmp(optarg
, "pgm_zmtp1") == 0)
1361 packettype
= PT_PGM_ZMTP1
;
1362 else if (strcasecmp(optarg
, "lmp") == 0)
1363 packettype
= PT_LMP
;
1365 error("unknown packet type `%s'", optarg
);
1372 #ifdef HAVE_PCAP_DUMP_FLUSH
1391 Wflag
= atoi(optarg
);
1393 error("invalid number of output files %s", optarg
);
1394 WflagChars
= getWflagChars(Wflag
);
1399 ++suppress_default_print
;
1404 ++suppress_default_print
;
1408 gndo
->ndo_dltname
= optarg
;
1410 pcap_datalink_name_to_val(gndo
->ndo_dltname
);
1411 if (gndo
->ndo_dlt
< 0)
1412 error("invalid data link type %s", gndo
->ndo_dltname
);
1415 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
1418 /* Undocumented flag */
1419 #ifdef HAVE_PCAP_DEBUG
1420 extern int pcap_debug
;
1430 zflag
= strdup(optarg
);
1434 username
= strdup(optarg
);
1438 gndo
->ndo_packet_number
= 1;
1441 case OPTION_VERSION
:
1446 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1447 case OPTION_TSTAMP_PRECISION
:
1448 gndo
->ndo_tstamp_precision
= tstamp_precision_from_string(optarg
);
1449 if (gndo
->ndo_tstamp_precision
< 0)
1450 error("unsupported time stamp precision");
1454 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1455 case OPTION_IMMEDIATE_MODE
:
1456 gndo
->ndo_immediate
= 1;
1466 #ifdef HAVE_PCAP_FINDALLDEVS
1468 show_devices_and_exit();
1473 case 0: /* Default */
1474 case 4: /* Default + Date*/
1475 thiszone
= gmt2local(0);
1478 case 1: /* No time stamp */
1479 case 2: /* Unix timeval style */
1480 case 3: /* Microseconds since previous packet */
1481 case 5: /* Microseconds since first packet */
1484 default: /* Not supported */
1485 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
1489 if (fflag
!= 0 && (VFileName
!= NULL
|| RFileName
!= NULL
))
1490 error("-f can not be used with -V or -r");
1492 if (VFileName
!= NULL
&& RFileName
!= NULL
)
1493 error("-V and -r are mutually exclusive.");
1495 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1497 * If we're printing dissected packets to the standard output
1498 * rather than saving raw packets to a file, and the standard
1499 * output is a terminal, use immediate mode, as the user's
1500 * probably expecting to see packets pop up immediately.
1502 if (WFileName
== NULL
&& isatty(1))
1503 gndo
->ndo_immediate
= 1;
1507 /* if run as root, prepare for chrooting */
1508 if (getuid() == 0 || geteuid() == 0) {
1509 /* future extensibility for cmd-line arguments */
1511 chroot_dir
= WITH_CHROOT
;
1516 /* if run as root, prepare for dropping root privileges */
1517 if (getuid() == 0 || geteuid() == 0) {
1518 /* Run with '-Z root' to restore old behaviour */
1520 username
= WITH_USER
;
1524 if (RFileName
!= NULL
|| VFileName
!= NULL
) {
1526 * If RFileName is non-null, it's the pathname of a
1527 * savefile to read. If VFileName is non-null, it's
1528 * the pathname of a file containing a list of pathnames
1529 * (one per line) of savefiles to read.
1531 * In either case, we're reading a savefile, not doing
1536 * We don't need network access, so relinquish any set-UID
1537 * or set-GID privileges we have (if any).
1539 * We do *not* want set-UID privileges when opening a
1540 * trace file, as that might let the user read other
1541 * people's trace files (especially if we're set-UID
1544 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
1545 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1547 if (VFileName
!= NULL
) {
1548 if (VFileName
[0] == '-' && VFileName
[1] == '\0')
1551 VFile
= fopen(VFileName
, "r");
1554 error("Unable to open file: %s\n", strerror(errno
));
1556 ret
= get_next_file(VFile
, VFileLine
);
1558 error("Nothing in %s\n", VFileName
);
1559 RFileName
= VFileLine
;
1562 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1563 pd
= pcap_open_offline_with_tstamp_precision(RFileName
,
1564 gndo
->ndo_tstamp_precision
, ebuf
);
1566 pd
= pcap_open_offline(RFileName
, ebuf
);
1571 #ifdef HAVE_CAPSICUM
1572 cap_rights_init(&rights
, CAP_READ
);
1573 if (cap_rights_limit(fileno(pcap_file(pd
)), &rights
) < 0 &&
1575 error("unable to limit pcap descriptor");
1578 dlt
= pcap_datalink(pd
);
1579 dlt_name
= pcap_datalink_val_to_name(dlt
);
1580 if (dlt_name
== NULL
) {
1581 fprintf(stderr
, "reading from file %s, link-type %u\n",
1585 "reading from file %s, link-type %s (%s)\n",
1586 RFileName
, dlt_name
,
1587 pcap_datalink_val_to_description(dlt
));
1591 * We're doing a live capture.
1593 if (device
== NULL
) {
1594 device
= pcap_lookupdev(ebuf
);
1600 * Print a message to the standard error on Windows.
1601 * XXX - why do it here, with a different message?
1603 if(strlen(device
) == 1) /* we assume that an ASCII string is always longer than 1 char */
1604 { /* a Unicode string has a \0 as second byte (so strlen() is 1) */
1605 fprintf(stderr
, "%s: listening on %ws\n", program_name
, device
);
1609 fprintf(stderr
, "%s: listening on %s\n", program_name
, device
);
1614 #ifdef HAVE_PCAP_CREATE
1615 pd
= pcap_create(device
, ebuf
);
1618 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1620 show_tstamp_types_and_exit(device
, pd
);
1622 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1623 status
= pcap_set_tstamp_precision(pd
, gndo
->ndo_tstamp_precision
);
1625 error("%s: Can't set %ssecond time stamp precision: %s",
1627 tstamp_precision_to_string(gndo
->ndo_tstamp_precision
),
1628 pcap_statustostr(status
));
1631 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1632 if (gndo
->ndo_immediate
) {
1633 status
= pcap_set_immediate_mode(pd
, 1);
1635 error("%s: Can't set immediate mode: %s",
1637 pcap_statustostr(status
));
1641 * Is this an interface that supports monitor mode?
1643 if (pcap_can_set_rfmon(pd
) == 1)
1644 supports_monitor_mode
= 1;
1646 supports_monitor_mode
= 0;
1647 status
= pcap_set_snaplen(pd
, snaplen
);
1649 error("%s: Can't set snapshot length: %s",
1650 device
, pcap_statustostr(status
));
1651 status
= pcap_set_promisc(pd
, !pflag
);
1653 error("%s: Can't set promiscuous mode: %s",
1654 device
, pcap_statustostr(status
));
1656 status
= pcap_set_rfmon(pd
, 1);
1658 error("%s: Can't set monitor mode: %s",
1659 device
, pcap_statustostr(status
));
1661 status
= pcap_set_timeout(pd
, 1000);
1663 error("%s: pcap_set_timeout failed: %s",
1664 device
, pcap_statustostr(status
));
1666 status
= pcap_set_buffer_size(pd
, Bflag
);
1668 error("%s: Can't set buffer size: %s",
1669 device
, pcap_statustostr(status
));
1671 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1673 status
= pcap_set_tstamp_type(pd
, jflag
);
1675 error("%s: Can't set time stamp type: %s",
1676 device
, pcap_statustostr(status
));
1679 status
= pcap_activate(pd
);
1682 * pcap_activate() failed.
1684 cp
= pcap_geterr(pd
);
1685 if (status
== PCAP_ERROR
)
1687 else if ((status
== PCAP_ERROR_NO_SUCH_DEVICE
||
1688 status
== PCAP_ERROR_PERM_DENIED
) &&
1690 error("%s: %s\n(%s)", device
,
1691 pcap_statustostr(status
), cp
);
1693 error("%s: %s", device
,
1694 pcap_statustostr(status
));
1695 } else if (status
> 0) {
1697 * pcap_activate() succeeded, but it's warning us
1698 * of a problem it had.
1700 cp
= pcap_geterr(pd
);
1701 if (status
== PCAP_WARNING
)
1703 else if (status
== PCAP_WARNING_PROMISC_NOTSUP
&&
1705 warning("%s: %s\n(%s)", device
,
1706 pcap_statustostr(status
), cp
);
1708 warning("%s: %s", device
,
1709 pcap_statustostr(status
));
1711 #ifdef HAVE_PCAP_SETDIRECTION
1713 status
= pcap_setdirection(pd
, Qflag
);
1715 error("%s: pcap_setdirection() failed: %s",
1716 device
, pcap_geterr(pd
));
1718 #endif /* HAVE_PCAP_SETDIRECTION */
1721 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
1725 warning("%s", ebuf
);
1726 #endif /* HAVE_PCAP_CREATE */
1728 * Let user own process after socket has been opened.
1731 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
1732 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1734 #if !defined(HAVE_PCAP_CREATE) && defined(WIN32)
1736 if(pcap_setbuff(pd
, Bflag
)==-1){
1737 error("%s", pcap_geterr(pd
));
1739 #endif /* !defined(HAVE_PCAP_CREATE) && defined(WIN32) */
1741 show_dlts_and_exit(device
, pd
);
1742 if (gndo
->ndo_dlt
>= 0) {
1743 #ifdef HAVE_PCAP_SET_DATALINK
1744 if (pcap_set_datalink(pd
, gndo
->ndo_dlt
) < 0)
1745 error("%s", pcap_geterr(pd
));
1748 * We don't actually support changing the
1749 * data link type, so we only let them
1750 * set it to what it already is.
1752 if (gndo
->ndo_dlt
!= pcap_datalink(pd
)) {
1753 error("%s is not one of the DLTs supported by this device\n",
1757 (void)fprintf(stderr
, "%s: data link type %s\n",
1758 program_name
, gndo
->ndo_dltname
);
1759 (void)fflush(stderr
);
1761 i
= pcap_snapshot(pd
);
1763 warning("snaplen raised from %d to %d", snaplen
, i
);
1767 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
1768 warning("foreign (-f) flag used but: %s", ebuf
);
1774 cmdbuf
= read_infile(infile
);
1776 cmdbuf
= copy_argv(&argv
[optind
]);
1778 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
1779 error("%s", pcap_geterr(pd
));
1781 bpf_dump(&fcode
, dflag
);
1786 init_addrtoname(gndo
, localnet
, netmask
);
1790 (void)setsignal(SIGPIPE
, cleanup
);
1791 (void)setsignal(SIGTERM
, cleanup
);
1792 (void)setsignal(SIGINT
, cleanup
);
1794 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1795 (void)setsignal(SIGCHLD
, child_cleanup
);
1797 /* Cooperate with nohup(1) */
1799 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
1800 (void)setsignal(SIGHUP
, oldhandler
);
1805 * If a user name was specified with "-Z", attempt to switch to
1806 * that user's UID. This would probably be used with sudo,
1807 * to allow tcpdump to be run in a special restricted
1808 * account (if you just want to allow users to open capture
1809 * devices, and can't just give users that permission,
1810 * you'd make tcpdump set-UID or set-GID).
1812 * Tcpdump doesn't necessarily write only to one savefile;
1813 * the general only way to allow a -Z instance to write to
1814 * savefiles as the user under whose UID it's run, rather
1815 * than as the user specified with -Z, would thus be to switch
1816 * to the original user ID before opening a capture file and
1817 * then switch back to the -Z user ID after opening the savefile.
1818 * Switching to the -Z user ID only after opening the first
1819 * savefile doesn't handle the general case.
1822 if (getuid() == 0 || geteuid() == 0) {
1823 #ifdef HAVE_LIBCAP_NG
1824 /* Initialize capng */
1825 capng_clear(CAPNG_SELECT_BOTH
);
1829 CAPNG_PERMITTED
| CAPNG_EFFECTIVE
,
1838 CAPNG_PERMITTED
| CAPNG_EFFECTIVE
,
1842 capng_apply(CAPNG_SELECT_BOTH
);
1843 #endif /* HAVE_LIBCAP_NG */
1844 if (username
|| chroot_dir
)
1845 droproot(username
, chroot_dir
);
1850 if (pcap_setfilter(pd
, &fcode
) < 0)
1851 error("%s", pcap_geterr(pd
));
1852 #ifdef HAVE_CAPSICUM
1853 if (RFileName
== NULL
&& VFileName
== NULL
) {
1854 static const unsigned long cmds
[] = { BIOCGSTATS
};
1856 cap_rights_init(&rights
, CAP_IOCTL
, CAP_READ
);
1857 if (cap_rights_limit(pcap_fileno(pd
), &rights
) < 0 &&
1859 error("unable to limit pcap descriptor");
1861 if (cap_ioctls_limit(pcap_fileno(pd
), cmds
,
1862 sizeof(cmds
) / sizeof(cmds
[0])) < 0 && errno
!= ENOSYS
) {
1863 error("unable to limit ioctls on pcap descriptor");
1869 /* Do not exceed the default PATH_MAX for files. */
1870 dumpinfo
.CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
1872 if (dumpinfo
.CurrentFileName
== NULL
)
1873 error("malloc of dumpinfo.CurrentFileName");
1875 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1877 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, WflagChars
);
1879 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, 0);
1881 p
= pcap_dump_open(pd
, dumpinfo
.CurrentFileName
);
1882 #ifdef HAVE_LIBCAP_NG
1883 /* Give up CAP_DAC_OVERRIDE capability.
1884 * Only allow it to be restored if the -C or -G flag have been
1885 * set since we may need to create more files later on.
1889 (Cflag
|| Gflag
? 0 : CAPNG_PERMITTED
)
1893 capng_apply(CAPNG_SELECT_BOTH
);
1894 #endif /* HAVE_LIBCAP_NG */
1896 error("%s", pcap_geterr(pd
));
1897 #ifdef HAVE_CAPSICUM
1898 set_dumper_capsicum_rights(p
);
1900 if (Cflag
!= 0 || Gflag
!= 0) {
1901 #ifdef HAVE_CAPSICUM
1902 dumpinfo
.WFileName
= strdup(basename(WFileName
));
1903 dumpinfo
.dirfd
= open(dirname(WFileName
),
1904 O_DIRECTORY
| O_RDONLY
);
1905 if (dumpinfo
.dirfd
< 0) {
1906 error("unable to open directory %s",
1907 dirname(WFileName
));
1909 cap_rights_init(&rights
, CAP_CREATE
, CAP_FCNTL
,
1910 CAP_FTRUNCATE
, CAP_LOOKUP
, CAP_SEEK
, CAP_WRITE
);
1911 if (cap_rights_limit(dumpinfo
.dirfd
, &rights
) < 0 &&
1913 error("unable to limit directory rights");
1915 if (cap_fcntls_limit(dumpinfo
.dirfd
, CAP_FCNTL_GETFL
) < 0 &&
1917 error("unable to limit dump descriptor fcntls");
1919 #else /* !HAVE_CAPSICUM */
1920 dumpinfo
.WFileName
= WFileName
;
1922 callback
= dump_packet_and_trunc
;
1925 pcap_userdata
= (u_char
*)&dumpinfo
;
1927 callback
= dump_packet
;
1928 pcap_userdata
= (u_char
*)p
;
1930 #ifdef HAVE_PCAP_DUMP_FLUSH
1935 type
= pcap_datalink(pd
);
1936 printinfo
= get_print_info(type
);
1937 callback
= print_packet
;
1938 pcap_userdata
= (u_char
*)&printinfo
;
1941 #ifdef SIGNAL_REQ_INFO
1943 * We can't get statistics when reading from a file rather
1944 * than capturing from a device.
1946 if (RFileName
== NULL
)
1947 (void)setsignal(SIGNAL_REQ_INFO
, requestinfo
);
1950 if (vflag
> 0 && WFileName
) {
1952 * When capturing to a file, "-v" means tcpdump should,
1953 * every 10 secodns, "v"erbosely report the number of
1956 #ifdef USE_WIN32_MM_TIMER
1957 /* call verbose_stats_dump() each 1000 +/-100msec */
1958 timer_id
= timeSetEvent(1000, 100, verbose_stats_dump
, 0, TIME_PERIODIC
);
1959 setvbuf(stderr
, NULL
, _IONBF
, 0);
1960 #elif defined(HAVE_ALARM)
1961 (void)setsignal(SIGALRM
, verbose_stats_dump
);
1967 if (RFileName
== NULL
) {
1969 * Live capture (if -V was specified, we set RFileName
1970 * to a file from the -V file). Print a message to
1971 * the standard error on UN*X.
1973 if (!vflag
&& !WFileName
) {
1974 (void)fprintf(stderr
,
1975 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1978 (void)fprintf(stderr
, "%s: ", program_name
);
1979 dlt
= pcap_datalink(pd
);
1980 dlt_name
= pcap_datalink_val_to_name(dlt
);
1981 if (dlt_name
== NULL
) {
1982 (void)fprintf(stderr
, "listening on %s, link-type %u, capture size %u bytes\n",
1983 device
, dlt
, snaplen
);
1985 (void)fprintf(stderr
, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1987 pcap_datalink_val_to_description(dlt
), snaplen
);
1989 (void)fflush(stderr
);
1993 #ifdef HAVE_CAPSICUM
1994 cansandbox
= (nflag
&& VFileName
== NULL
&& zflag
== NULL
);
1995 if (cansandbox
&& cap_enter() < 0 && errno
!= ENOSYS
)
1996 error("unable to enter the capability mode");
1997 if (cap_sandboxed())
1998 fprintf(stderr
, "capability mode sandbox enabled\n");
1999 #endif /* HAVE_CAPSICUM */
2002 status
= pcap_loop(pd
, cnt
, callback
, pcap_userdata
);
2003 if (WFileName
== NULL
) {
2005 * We're printing packets. Flush the printed output,
2006 * so it doesn't get intermingled with error output.
2010 * We got interrupted, so perhaps we didn't
2011 * manage to finish a line we were printing.
2012 * Print an extra newline, just in case.
2016 (void)fflush(stdout
);
2020 * We got interrupted. If we are reading multiple
2021 * files (via -V) set these so that we stop.
2030 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
2031 program_name
, pcap_geterr(pd
));
2033 if (RFileName
== NULL
) {
2035 * We're doing a live capture. Report the capture
2041 if (VFileName
!= NULL
) {
2042 ret
= get_next_file(VFile
, VFileLine
);
2044 RFileName
= VFileLine
;
2045 pd
= pcap_open_offline(RFileName
, ebuf
);
2048 #ifdef HAVE_CAPSICUM
2049 cap_rights_init(&rights
, CAP_READ
);
2050 if (cap_rights_limit(fileno(pcap_file(pd
)),
2051 &rights
) < 0 && errno
!= ENOSYS
) {
2052 error("unable to limit pcap descriptor");
2055 new_dlt
= pcap_datalink(pd
);
2056 if (WFileName
&& new_dlt
!= dlt
)
2057 error("%s: new dlt does not match original", RFileName
);
2058 printinfo
= get_print_info(new_dlt
);
2059 dlt_name
= pcap_datalink_val_to_name(new_dlt
);
2060 if (dlt_name
== NULL
) {
2061 fprintf(stderr
, "reading from file %s, link-type %u\n",
2062 RFileName
, new_dlt
);
2065 "reading from file %s, link-type %s (%s)\n",
2066 RFileName
, dlt_name
,
2067 pcap_datalink_val_to_description(new_dlt
));
2069 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
2070 error("%s", pcap_geterr(pd
));
2071 if (pcap_setfilter(pd
, &fcode
) < 0)
2072 error("%s", pcap_geterr(pd
));
2076 while (ret
!= NULL
);
2079 exit(status
== -1 ? 1 : 0);
2082 /* make a clean exit on interrupts */
2084 cleanup(int signo _U_
)
2086 #ifdef USE_WIN32_MM_TIMER
2088 timeKillEvent(timer_id
);
2090 #elif defined(HAVE_ALARM)
2094 #ifdef HAVE_PCAP_BREAKLOOP
2096 * We have "pcap_breakloop()"; use it, so that we do as little
2097 * as possible in the signal handler (it's probably not safe
2098 * to do anything with standard I/O streams in a signal handler -
2099 * the ANSI C standard doesn't say it is).
2104 * We don't have "pcap_breakloop()"; this isn't safe, but
2105 * it's the best we can do. Print the summary if we're
2106 * not reading from a savefile - i.e., if we're doing a
2107 * live capture - and exit.
2109 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
2111 * We got interrupted, so perhaps we didn't
2112 * manage to finish a line we were printing.
2113 * Print an extra newline, just in case.
2116 (void)fflush(stdout
);
2124 On windows, we do not use a fork, so we do not care less about
2125 waiting a child processes to die
2127 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
2129 child_cleanup(int signo _U_
)
2133 #endif /* HAVE_FORK && HAVE_VFORK */
2136 info(register int verbose
)
2138 struct pcap_stat stat
;
2141 * Older versions of libpcap didn't set ps_ifdrop on some
2142 * platforms; initialize it to 0 to handle that.
2145 if (pcap_stats(pd
, &stat
) < 0) {
2146 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
2152 fprintf(stderr
, "%s: ", program_name
);
2154 (void)fprintf(stderr
, "%u packet%s captured", packets_captured
,
2155 PLURAL_SUFFIX(packets_captured
));
2157 fputs(", ", stderr
);
2160 (void)fprintf(stderr
, "%u packet%s received by filter", stat
.ps_recv
,
2161 PLURAL_SUFFIX(stat
.ps_recv
));
2163 fputs(", ", stderr
);
2166 (void)fprintf(stderr
, "%u packet%s dropped by kernel", stat
.ps_drop
,
2167 PLURAL_SUFFIX(stat
.ps_drop
));
2168 if (stat
.ps_ifdrop
!= 0) {
2170 fputs(", ", stderr
);
2173 (void)fprintf(stderr
, "%u packet%s dropped by interface\n",
2174 stat
.ps_ifdrop
, PLURAL_SUFFIX(stat
.ps_ifdrop
));
2180 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
2182 compress_savefile(const char *filename
)
2191 * Set to lowest priority so that this doesn't disturb the capture
2194 setpriority(PRIO_PROCESS
, 0, NZERO
- 1);
2196 setpriority(PRIO_PROCESS
, 0, 19);
2198 if (execlp(zflag
, zflag
, filename
, (char *)NULL
) == -1)
2200 "compress_savefile:execlp(%s, %s): %s\n",
2210 #else /* HAVE_FORK && HAVE_VFORK */
2212 compress_savefile(const char *filename
)
2215 "compress_savefile failed. Functionality not implemented under your system\n");
2217 #endif /* HAVE_FORK && HAVE_VFORK */
2220 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
2222 struct dump_info
*dump_info
;
2228 dump_info
= (struct dump_info
*)user
;
2231 * XXX - this won't force the file to rotate on the specified time
2232 * boundary, but it will rotate on the first packet received after the
2233 * specified Gflag number of seconds. Note: if a Gflag time boundary
2234 * and a Cflag size boundary coincide, the time rotation will occur
2235 * first thereby cancelling the Cflag boundary (since the file should
2239 /* Check if it is time to rotate */
2242 /* Get the current time */
2243 if ((t
= time(NULL
)) == (time_t)-1) {
2244 error("dump_and_trunc_packet: can't get current_time: %s",
2245 pcap_strerror(errno
));
2249 /* If the time is greater than the specified window, rotate */
2250 if (t
- Gflag_time
>= Gflag
) {
2251 #ifdef HAVE_CAPSICUM
2256 /* Update the Gflag_time */
2258 /* Update Gflag_count */
2261 * Close the current file and open a new one.
2263 pcap_dump_close(dump_info
->p
);
2266 * Compress the file we just closed, if the user asked for it
2269 compress_savefile(dump_info
->CurrentFileName
);
2272 * Check to see if we've exceeded the Wflag (when
2275 if (Cflag
== 0 && Wflag
> 0 && Gflag_count
>= Wflag
) {
2276 (void)fprintf(stderr
, "Maximum file limit reached: %d\n",
2281 if (dump_info
->CurrentFileName
!= NULL
)
2282 free(dump_info
->CurrentFileName
);
2283 /* Allocate space for max filename + \0. */
2284 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
2285 if (dump_info
->CurrentFileName
== NULL
)
2286 error("dump_packet_and_trunc: malloc");
2288 * Gflag was set otherwise we wouldn't be here. Reset the count
2289 * so multiple files would end with 1,2,3 in the filename.
2290 * The counting is handled with the -C flow after this.
2295 * This is always the first file in the Cflag
2297 * We also don't need numbering if Cflag is not set.
2300 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0,
2303 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0, 0);
2305 #ifdef HAVE_LIBCAP_NG
2306 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
2307 capng_apply(CAPNG_SELECT_BOTH
);
2308 #endif /* HAVE_LIBCAP_NG */
2309 #ifdef HAVE_CAPSICUM
2310 fd
= openat(dump_info
->dirfd
,
2311 dump_info
->CurrentFileName
,
2312 O_CREAT
| O_WRONLY
| O_TRUNC
, 0644);
2314 error("unable to open file %s",
2315 dump_info
->CurrentFileName
);
2317 fp
= fdopen(fd
, "w");
2319 error("unable to fdopen file %s",
2320 dump_info
->CurrentFileName
);
2322 dump_info
->p
= pcap_dump_fopen(dump_info
->pd
, fp
);
2323 #else /* !HAVE_CAPSICUM */
2324 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
2326 #ifdef HAVE_LIBCAP_NG
2327 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
2328 capng_apply(CAPNG_SELECT_BOTH
);
2329 #endif /* HAVE_LIBCAP_NG */
2330 if (dump_info
->p
== NULL
)
2331 error("%s", pcap_geterr(pd
));
2332 #ifdef HAVE_CAPSICUM
2333 set_dumper_capsicum_rights(dump_info
->p
);
2339 * XXX - this won't prevent capture files from getting
2340 * larger than Cflag - the last packet written to the
2341 * file could put it over Cflag.
2344 long size
= pcap_dump_ftell(dump_info
->p
);
2347 error("ftell fails on output file");
2349 #ifdef HAVE_CAPSICUM
2355 * Close the current file and open a new one.
2357 pcap_dump_close(dump_info
->p
);
2360 * Compress the file we just closed, if the user
2364 compress_savefile(dump_info
->CurrentFileName
);
2368 if (Cflag_count
>= Wflag
)
2371 if (dump_info
->CurrentFileName
!= NULL
)
2372 free(dump_info
->CurrentFileName
);
2373 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
2374 if (dump_info
->CurrentFileName
== NULL
)
2375 error("dump_packet_and_trunc: malloc");
2376 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, Cflag_count
, WflagChars
);
2377 #ifdef HAVE_LIBCAP_NG
2378 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
2379 capng_apply(CAPNG_SELECT_BOTH
);
2380 #endif /* HAVE_LIBCAP_NG */
2381 #ifdef HAVE_CAPSICUM
2382 fd
= openat(dump_info
->dirfd
, dump_info
->CurrentFileName
,
2383 O_CREAT
| O_WRONLY
| O_TRUNC
, 0644);
2385 error("unable to open file %s",
2386 dump_info
->CurrentFileName
);
2388 fp
= fdopen(fd
, "w");
2390 error("unable to fdopen file %s",
2391 dump_info
->CurrentFileName
);
2393 dump_info
->p
= pcap_dump_fopen(dump_info
->pd
, fp
);
2394 #else /* !HAVE_CAPSICUM */
2395 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
2397 #ifdef HAVE_LIBCAP_NG
2398 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
2399 capng_apply(CAPNG_SELECT_BOTH
);
2400 #endif /* HAVE_LIBCAP_NG */
2401 if (dump_info
->p
== NULL
)
2402 error("%s", pcap_geterr(pd
));
2403 #ifdef HAVE_CAPSICUM
2404 set_dumper_capsicum_rights(dump_info
->p
);
2409 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
2410 #ifdef HAVE_PCAP_DUMP_FLUSH
2412 pcap_dump_flush(dump_info
->p
);
2421 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
2427 pcap_dump(user
, h
, sp
);
2428 #ifdef HAVE_PCAP_DUMP_FLUSH
2430 pcap_dump_flush((pcap_dumper_t
*)user
);
2439 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
2441 struct print_info
*print_info
;
2443 netdissect_options
*ndo
;
2449 print_info
= (struct print_info
*)user
;
2450 ndo
= print_info
->ndo
;
2452 if(ndo
->ndo_packet_number
)
2453 ND_PRINT((ndo
, "%5u ", packets_captured
));
2455 ts_print(ndo
, &h
->ts
);
2458 * Some printers want to check that they're not walking off the
2459 * end of the packet.
2460 * Rather than pass it all the way down, we set this member
2461 * of the netdissect_options structure.
2463 ndo
->ndo_snapend
= sp
+ h
->caplen
;
2465 if(print_info
->ndo_type
) {
2466 hdrlen
= (*print_info
->p
.ndo_printer
)(print_info
->ndo
, h
, sp
);
2468 hdrlen
= (*print_info
->p
.printer
)(h
, sp
);
2472 * Restore the original snapend, as a printer might have
2475 ndo
->ndo_snapend
= sp
+ h
->caplen
;
2476 if (ndo
->ndo_Xflag
) {
2478 * Print the raw packet data in hex and ASCII.
2480 if (ndo
->ndo_Xflag
> 1) {
2482 * Include the link-layer header.
2484 hex_and_ascii_print(ndo
, "\n\t", sp
, h
->caplen
);
2487 * Don't include the link-layer header - and if
2488 * we have nothing past the link-layer header,
2491 if (h
->caplen
> hdrlen
)
2492 hex_and_ascii_print(ndo
, "\n\t", sp
+ hdrlen
,
2493 h
->caplen
- hdrlen
);
2495 } else if (ndo
->ndo_xflag
) {
2497 * Print the raw packet data in hex.
2499 if (ndo
->ndo_xflag
> 1) {
2501 * Include the link-layer header.
2503 hex_print(ndo
, "\n\t", sp
, h
->caplen
);
2506 * Don't include the link-layer header - and if
2507 * we have nothing past the link-layer header,
2510 if (h
->caplen
> hdrlen
)
2511 hex_print(ndo
, "\n\t", sp
+ hdrlen
,
2512 h
->caplen
- hdrlen
);
2514 } else if (ndo
->ndo_Aflag
) {
2516 * Print the raw packet data in ASCII.
2518 if (ndo
->ndo_Aflag
> 1) {
2520 * Include the link-layer header.
2522 ascii_print(ndo
, sp
, h
->caplen
);
2525 * Don't include the link-layer header - and if
2526 * we have nothing past the link-layer header,
2529 if (h
->caplen
> hdrlen
)
2530 ascii_print(ndo
, sp
+ hdrlen
, h
->caplen
- hdrlen
);
2543 * XXX - there should really be libpcap calls to get the version
2544 * number as a string (the string would be generated from #defines
2545 * at run time, so that it's not generated from string constants
2546 * in the library, as, on many UNIX systems, those constants would
2547 * be statically linked into the application executable image, and
2548 * would thus reflect the version of libpcap on the system on
2549 * which the application was *linked*, not the system on which it's
2552 * That routine should be documented, unlike the "version[]"
2553 * string, so that UNIX vendors providing their own libpcaps
2554 * don't omit it (as a couple of vendors have...).
2556 * Packet.dll should perhaps also export a routine to return the
2557 * version number of the Packet.dll code, to supply the
2558 * "Wpcap_version" information on Windows.
2560 char WDversion
[]="current-git.tcpdump.org";
2561 #if !defined(HAVE_GENERATED_VERSION)
2562 char version
[]="current-git.tcpdump.org";
2564 char pcap_version
[]="current-git.tcpdump.org";
2565 char Wpcap_version
[]="3.1";
2569 * By default, print the specified data out in hex and ASCII.
2572 ndo_default_print(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
2574 hex_and_ascii_print(ndo
, "\n\t", bp
, length
); /* pass on lf and indentation string */
2578 default_print(const u_char
*bp
, u_int length
)
2580 ndo_default_print(gndo
, bp
, length
);
2583 #ifdef SIGNAL_REQ_INFO
2584 RETSIGTYPE
requestinfo(int signo _U_
)
2594 * Called once each second in verbose mode while dumping to file
2596 #ifdef USE_WIN32_MM_TIMER
2597 void CALLBACK
verbose_stats_dump (UINT timer_id _U_
, UINT msg _U_
, DWORD_PTR arg _U_
,
2598 DWORD_PTR dw1 _U_
, DWORD_PTR dw2 _U_
)
2600 struct pcap_stat stat
;
2602 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
2603 fprintf(stderr
, "Got %u\r", packets_captured
);
2605 #elif defined(HAVE_ALARM)
2606 static void verbose_stats_dump(int sig _U_
)
2608 struct pcap_stat stat
;
2610 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
2611 fprintf(stderr
, "Got %u\r", packets_captured
);
2616 USES_APPLE_DEPRECATED_API
2620 extern char version
[];
2621 #ifndef HAVE_PCAP_LIB_VERSION
2622 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
2623 extern char pcap_version
[];
2624 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2625 static char pcap_version
[] = "unknown";
2626 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2627 #endif /* HAVE_PCAP_LIB_VERSION */
2629 #ifdef HAVE_PCAP_LIB_VERSION
2631 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2633 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2635 (void)fprintf(stderr
, "%s\n",pcap_lib_version());
2636 #else /* HAVE_PCAP_LIB_VERSION */
2638 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2639 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
2641 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2642 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
2644 #endif /* HAVE_PCAP_LIB_VERSION */
2646 #if defined(HAVE_LIBCRYPTO) && defined(SSLEAY_VERSION)
2647 (void)fprintf (stderr
, "%s\n", SSLeay_version(SSLEAY_VERSION
));
2651 (void)fprintf (stderr
, "SMI-library: %s\n", smi_version_string
);
2660 (void)fprintf(stderr
,
2661 "Usage: %s [-aAbd" D_FLAG
"efhH" I_FLAG J_FLAG
"KlLnNOpqRStu" U_FLAG
"vxX#]" B_FLAG_USAGE
" [ -c count ]\n", program_name
);
2662 (void)fprintf(stderr
,
2663 "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n");
2664 (void)fprintf(stderr
,
2665 "\t\t[ -i interface ]" j_FLAG_USAGE
" [ -M secret ] [ --number ]\n");
2666 #ifdef HAVE_PCAP_SETDIRECTION
2667 (void)fprintf(stderr
,
2668 "\t\t[ -Q in|out|inout ]\n");
2670 (void)fprintf(stderr
,
2671 "\t\t[ -r file ] [ -s snaplen ] ");
2672 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
2673 (void)fprintf(stderr
, "[ --time-stamp-precision precision ]\n");
2674 (void)fprintf(stderr
,
2677 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
2678 (void)fprintf(stderr
, "[ --immediate-mode ] ");
2680 (void)fprintf(stderr
, "[ -T type ] [ --version ] [ -V file ]\n");
2681 (void)fprintf(stderr
,
2682 "\t\t[ -w file ] [ -W filecount ] [ -y datalinktype ] [ -z command ]\n");
2683 (void)fprintf(stderr
,
2684 "\t\t[ -Z user ] [ expression ]\n");
2691 ndo_error(netdissect_options
*ndo _U_
, const char *fmt
, ...)
2695 (void)fprintf(stderr
, "%s: ", program_name
);
2697 (void)vfprintf(stderr
, fmt
, ap
);
2701 if (fmt
[-1] != '\n')
2702 (void)fputc('\n', stderr
);
2710 ndo_warning(netdissect_options
*ndo _U_
, const char *fmt
, ...)
2714 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
2716 (void)vfprintf(stderr
, fmt
, ap
);
2720 if (fmt
[-1] != '\n')
2721 (void)fputc('\n', stderr
);
2726 * c-style: whitesmith