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";
32 static const char rcsid
[] _U_
=
33 "@(#) $Header: /tcpdump/master/tcpdump/tcpdump.c,v 1.283 2008-09-25 21:45:50 guy Exp $ (LBL)";
37 * tcpdump - monitor tcp/ip traffic on an ethernet.
39 * First written in 1987 by Van Jacobson, Lawrence Berkeley Laboratory.
40 * Mercilessly hacked and occasionally improved since then via the
41 * combined efforts of Van, Steve McCanne and Craig Leres of LBL.
48 #include <tcpdump-stdinc.h>
53 extern int strcasecmp (const char *__s1
, const char *__s2
);
71 #include <sys/resource.h>
77 /* capabilities convinience library */
80 #endif /* HAVE_CAP_NG_H */
82 #include "netdissect.h"
83 #include "interface.h"
84 #include "addrtoname.h"
86 #include "setsignal.h"
87 #include "gmt2local.h"
88 #include "pcap-missing.h"
95 #define SIGNAL_REQ_INFO SIGINFO
97 #define SIGNAL_REQ_INFO SIGUSR1
100 netdissect_options Gndo
;
101 netdissect_options
*gndo
= &Gndo
;
103 static int dflag
; /* print filter code */
104 static int Lflag
; /* list available data link types and exit */
105 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
106 static int Jflag
; /* list available time stamp types */
108 static char *zflag
= NULL
; /* compress each savefile using a specified command (like gzip or bzip2) */
110 static int infodelay
;
111 static int infoprint
;
115 int32_t thiszone
; /* seconds offset from gmt to local time */
118 static RETSIGTYPE
cleanup(int);
119 static RETSIGTYPE
child_cleanup(int);
120 static void usage(void) __attribute__((noreturn
));
121 static void show_dlts_and_exit(const char *device
, pcap_t
*pd
) __attribute__((noreturn
));
123 static void print_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
124 static void ndo_default_print(netdissect_options
*, const u_char
*, u_int
);
125 static void dump_packet_and_trunc(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
126 static void dump_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
127 static void droproot(const char *, const char *);
128 static void ndo_error(netdissect_options
*ndo
, const char *fmt
, ...)
129 __attribute__ ((noreturn
, format (printf
, 2, 3)));
130 static void ndo_warning(netdissect_options
*ndo
, const char *fmt
, ...);
132 #ifdef SIGNAL_REQ_INFO
133 RETSIGTYPE
requestinfo(int);
136 #if defined(USE_WIN32_MM_TIMER)
137 #include <MMsystem.h>
138 static UINT timer_id
;
139 static void CALLBACK
verbose_stats_dump(UINT
, UINT
, DWORD_PTR
, DWORD_PTR
, DWORD_PTR
);
140 #elif defined(HAVE_ALARM)
141 static void verbose_stats_dump(int sig
);
144 static void info(int);
145 static u_int packets_captured
;
159 static struct printer printers
[] = {
160 { arcnet_if_print
, DLT_ARCNET
},
161 #ifdef DLT_ARCNET_LINUX
162 { arcnet_linux_if_print
, DLT_ARCNET_LINUX
},
164 { token_if_print
, DLT_IEEE802
},
166 { lane_if_print
, DLT_LANE8023
},
169 { cip_if_print
, DLT_CIP
},
172 { cip_if_print
, DLT_ATM_CLIP
},
174 { sl_if_print
, DLT_SLIP
},
175 #ifdef DLT_SLIP_BSDOS
176 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
178 { ppp_if_print
, DLT_PPP
},
179 #ifdef DLT_PPP_WITHDIRECTION
180 { ppp_if_print
, DLT_PPP_WITHDIRECTION
},
183 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
185 { fddi_if_print
, DLT_FDDI
},
186 { null_if_print
, DLT_NULL
},
188 { null_if_print
, DLT_LOOP
},
190 { raw_if_print
, DLT_RAW
},
191 { atm_if_print
, DLT_ATM_RFC1483
},
193 { chdlc_if_print
, DLT_C_HDLC
},
196 { chdlc_if_print
, DLT_HDLC
},
198 #ifdef DLT_PPP_SERIAL
199 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
202 { pppoe_if_print
, DLT_PPP_ETHER
},
205 { sll_if_print
, DLT_LINUX_SLL
},
207 #ifdef DLT_IEEE802_11
208 { ieee802_11_if_print
, DLT_IEEE802_11
},
211 { ltalk_if_print
, DLT_LTALK
},
213 #if defined(DLT_PFLOG) && defined(HAVE_NET_PFVAR_H)
214 { pflog_if_print
, DLT_PFLOG
},
217 { fr_if_print
, DLT_FR
},
220 { fr_if_print
, DLT_FRELAY
},
223 { sunatm_if_print
, DLT_SUNATM
},
225 #ifdef DLT_IP_OVER_FC
226 { ipfc_if_print
, DLT_IP_OVER_FC
},
228 #ifdef DLT_PRISM_HEADER
229 { prism_if_print
, DLT_PRISM_HEADER
},
231 #ifdef DLT_IEEE802_11_RADIO
232 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
235 { enc_if_print
, DLT_ENC
},
237 #ifdef DLT_SYMANTEC_FIREWALL
238 { symantec_if_print
, DLT_SYMANTEC_FIREWALL
},
240 #ifdef DLT_APPLE_IP_OVER_IEEE1394
241 { ap1394_if_print
, DLT_APPLE_IP_OVER_IEEE1394
},
243 #ifdef DLT_IEEE802_11_RADIO_AVS
244 { ieee802_11_radio_avs_if_print
, DLT_IEEE802_11_RADIO_AVS
},
246 #ifdef DLT_JUNIPER_ATM1
247 { juniper_atm1_print
, DLT_JUNIPER_ATM1
},
249 #ifdef DLT_JUNIPER_ATM2
250 { juniper_atm2_print
, DLT_JUNIPER_ATM2
},
252 #ifdef DLT_JUNIPER_MFR
253 { juniper_mfr_print
, DLT_JUNIPER_MFR
},
255 #ifdef DLT_JUNIPER_MLFR
256 { juniper_mlfr_print
, DLT_JUNIPER_MLFR
},
258 #ifdef DLT_JUNIPER_MLPPP
259 { juniper_mlppp_print
, DLT_JUNIPER_MLPPP
},
261 #ifdef DLT_JUNIPER_PPPOE
262 { juniper_pppoe_print
, DLT_JUNIPER_PPPOE
},
264 #ifdef DLT_JUNIPER_PPPOE_ATM
265 { juniper_pppoe_atm_print
, DLT_JUNIPER_PPPOE_ATM
},
267 #ifdef DLT_JUNIPER_GGSN
268 { juniper_ggsn_print
, DLT_JUNIPER_GGSN
},
270 #ifdef DLT_JUNIPER_ES
271 { juniper_es_print
, DLT_JUNIPER_ES
},
273 #ifdef DLT_JUNIPER_MONITOR
274 { juniper_monitor_print
, DLT_JUNIPER_MONITOR
},
276 #ifdef DLT_JUNIPER_SERVICES
277 { juniper_services_print
, DLT_JUNIPER_SERVICES
},
279 #ifdef DLT_JUNIPER_ETHER
280 { juniper_ether_print
, DLT_JUNIPER_ETHER
},
282 #ifdef DLT_JUNIPER_PPP
283 { juniper_ppp_print
, DLT_JUNIPER_PPP
},
285 #ifdef DLT_JUNIPER_FRELAY
286 { juniper_frelay_print
, DLT_JUNIPER_FRELAY
},
288 #ifdef DLT_JUNIPER_CHDLC
289 { juniper_chdlc_print
, DLT_JUNIPER_CHDLC
},
292 { mfr_if_print
, DLT_MFR
},
294 #if defined(DLT_BLUETOOTH_HCI_H4_WITH_PHDR) && defined(HAVE_PCAP_BLUETOOTH_H)
295 { bt_if_print
, DLT_BLUETOOTH_HCI_H4_WITH_PHDR
},
297 #ifdef HAVE_PCAP_USB_H
299 { usb_linux_48_byte_print
, DLT_USB_LINUX
},
300 #endif /* DLT_USB_LINUX */
301 #ifdef DLT_USB_LINUX_MMAPPED
302 { usb_linux_64_byte_print
, DLT_USB_LINUX_MMAPPED
},
303 #endif /* DLT_USB_LINUX_MMAPPED */
304 #endif /* HAVE_PCAP_USB_H */
306 { raw_if_print
, DLT_IPV4
},
309 { raw_if_print
, DLT_IPV6
},
314 static struct ndo_printer ndo_printers
[] = {
315 { ether_if_print
, DLT_EN10MB
},
317 { ipnet_if_print
, DLT_IPNET
},
319 #ifdef DLT_IEEE802_15_4
320 { ieee802_15_4_if_print
, DLT_IEEE802_15_4
},
322 #ifdef DLT_IEEE802_15_4_NOFCS
323 { ieee802_15_4_if_print
, DLT_IEEE802_15_4_NOFCS
},
326 { ppi_if_print
, DLT_PPI
},
328 #ifdef DLT_NETANALYZER
329 { netanalyzer_if_print
, DLT_NETANALYZER
},
331 #ifdef DLT_NETANALYZER_TRANSPARENT
332 { netanalyzer_transparent_if_print
, DLT_NETANALYZER_TRANSPARENT
},
338 lookup_printer(int type
)
342 for (p
= printers
; p
->f
; ++p
)
351 lookup_ndo_printer(int type
)
353 struct ndo_printer
*p
;
355 for (p
= ndo_printers
; p
->f
; ++p
)
365 static int supports_monitor_mode
;
372 netdissect_options
*ndo
;
375 if_ndo_printer ndo_printer
;
382 char *CurrentFileName
;
387 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
389 show_tstamp_types_and_exit(const char *device
, pcap_t
*pd
)
392 int *tstamp_types
= 0;
393 const char *tstamp_type_name
;
396 n_tstamp_types
= pcap_list_tstamp_types(pd
, &tstamp_types
);
397 if (n_tstamp_types
< 0)
398 error("%s", pcap_geterr(pd
));
400 if (n_tstamp_types
== 0) {
401 fprintf(stderr
, "Time stamp type cannot be set for %s\n",
405 fprintf(stderr
, "Time stamp types for %s (use option -j to set):\n",
407 for (i
= 0; i
< n_tstamp_types
; i
++) {
408 tstamp_type_name
= pcap_tstamp_type_val_to_name(tstamp_types
[i
]);
409 if (tstamp_type_name
!= NULL
) {
410 (void) fprintf(stderr
, " %s (%s)\n", tstamp_type_name
,
411 pcap_tstamp_type_val_to_description(tstamp_types
[i
]));
413 (void) fprintf(stderr
, " %d\n", tstamp_types
[i
]);
416 pcap_free_tstamp_types(tstamp_types
);
422 show_dlts_and_exit(const char *device
, pcap_t
*pd
)
426 const char *dlt_name
;
428 n_dlts
= pcap_list_datalinks(pd
, &dlts
);
430 error("%s", pcap_geterr(pd
));
431 else if (n_dlts
== 0 || !dlts
)
432 error("No data link types.");
435 * If the interface is known to support monitor mode, indicate
436 * whether these are the data link types available when not in
437 * monitor mode, if -I wasn't specified, or when in monitor mode,
438 * when -I was specified (the link-layer types available in
439 * monitor mode might be different from the ones available when
440 * not in monitor mode).
442 if (supports_monitor_mode
)
443 (void) fprintf(stderr
, "Data link types for %s %s (use option -y to set):\n",
445 Iflag
? "when in monitor mode" : "when not in monitor mode");
447 (void) fprintf(stderr
, "Data link types for %s (use option -y to set):\n",
450 while (--n_dlts
>= 0) {
451 dlt_name
= pcap_datalink_val_to_name(dlts
[n_dlts
]);
452 if (dlt_name
!= NULL
) {
453 (void) fprintf(stderr
, " %s (%s)", dlt_name
,
454 pcap_datalink_val_to_description(dlts
[n_dlts
]));
457 * OK, does tcpdump handle that type?
459 if (lookup_printer(dlts
[n_dlts
]) == NULL
460 && lookup_ndo_printer(dlts
[n_dlts
]) == NULL
)
461 (void) fprintf(stderr
, " (printing not supported)");
462 fprintf(stderr
, "\n");
464 (void) fprintf(stderr
, " DLT %d (printing not supported)\n",
468 #ifdef HAVE_PCAP_FREE_DATALINKS
469 pcap_free_datalinks(dlts
);
475 * Set up flags that might or might not be supported depending on the
476 * version of libpcap we're using.
478 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
480 #define B_FLAG_USAGE " [ -B size ]"
481 #else /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
484 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
486 #ifdef HAVE_PCAP_CREATE
488 #else /* HAVE_PCAP_CREATE */
490 #endif /* HAVE_PCAP_CREATE */
492 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
494 #define j_FLAG_USAGE " [ -j tstamptype ]"
496 #else /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
500 #endif /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
502 #ifdef HAVE_PCAP_FINDALLDEVS
503 #ifndef HAVE_PCAP_IF_T
504 #undef HAVE_PCAP_FINDALLDEVS
508 #ifdef HAVE_PCAP_FINDALLDEVS
514 #ifdef HAVE_PCAP_DUMP_FLUSH
521 /* Drop root privileges and chroot if necessary */
523 droproot(const char *username
, const char *chroot_dir
)
525 struct passwd
*pw
= NULL
;
527 if (chroot_dir
&& !username
) {
528 fprintf(stderr
, "tcpdump: Chroot without dropping root is insecure\n");
532 pw
= getpwnam(username
);
535 if (chroot(chroot_dir
) != 0 || chdir ("/") != 0) {
536 fprintf(stderr
, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
537 chroot_dir
, pcap_strerror(errno
));
542 int ret
= capng_change_id(pw
->pw_uid
, pw
->pw_gid
, CAPNG_NO_FLAG
);
544 printf("error : ret %d\n", ret
);
546 /* We don't need CAP_SETUID and CAP_SETGID */
547 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_SETUID
);
548 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_SETUID
);
549 capng_update(CAPNG_DROP
, CAPNG_PERMITTED
, CAP_SETUID
);
550 capng_update(CAPNG_DROP
, CAPNG_PERMITTED
, CAP_SETUID
);
551 capng_apply(CAPNG_SELECT_BOTH
);
554 if (initgroups(pw
->pw_name
, pw
->pw_gid
) != 0 ||
555 setgid(pw
->pw_gid
) != 0 || setuid(pw
->pw_uid
) != 0) {
556 fprintf(stderr
, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
558 (unsigned long)pw
->pw_uid
,
559 (unsigned long)pw
->pw_gid
,
560 pcap_strerror(errno
));
563 #endif /* HAVE_CAP_NG_H */
566 fprintf(stderr
, "tcpdump: Couldn't find user '%.32s'\n",
589 MakeFilename(char *buffer
, char *orig_name
, int cnt
, int max_chars
)
591 char *filename
= malloc(NAME_MAX
+ 1);
592 if (filename
== NULL
)
593 error("Makefilename: malloc);
595 /* Process with strftime if Gflag is set. */
599 /* Convert Gflag_time to a usable format */
600 if ((local_tm = localtime(&Gflag_time)) == NULL) {
601 error("MakeTimedFilename
: localtime
");
604 /* There's no good way to detect an error in strftime since a return
605 * value of 0 isn't necessarily failure.
607 strftime(filename, NAME_MAX, orig_name, local_tm);
609 strncpy(filename, orig_name, NAME_MAX);
612 if (cnt == 0 && max_chars == 0)
613 strncpy(buffer, filename, NAME_MAX + 1);
615 if (snprintf(buffer, NAME_MAX + 1, "%s
%0*d
", filename, max_chars, cnt) > NAME_MAX)
616 /* Report an error if the filename is too large */
617 error("too many output files
or filename is too
long (> %d
)", NAME_MAX);
621 static int tcpdump_printf(netdissect_options *ndo _U_,
622 const char *fmt, ...)
629 ret=vfprintf(stdout, fmt, args);
635 struct print_info get_print_info(int type) {
636 struct print_info printinfo;
638 printinfo.ndo_type = 1;
639 printinfo.ndo = gndo;
640 printinfo.p.ndo_printer = lookup_ndo_printer(type);
641 if (printinfo.p.ndo_printer == NULL) {
642 printinfo.p.printer = lookup_printer(type);
643 printinfo.ndo_type = 0;
644 if (printinfo.p.printer == NULL) {
645 gndo->ndo_dltname = pcap_datalink_val_to_name(type);
646 if (gndo->ndo_dltname != NULL)
647 error("packet printing is
not supported
for link type
%s
: use
-w
",
650 error("packet printing is
not supported
for link type
%d
: use
-w
", type);
656 char *get_next_file(FILE *VFile, char *ptr) {
659 ret = fgets(ptr, NAME_MAX, VFile);
663 if (ptr[strlen(ptr) - 1] == '\n')
664 ptr[strlen(ptr) - 1] = '\0';
670 main(int argc, char **argv)
672 register int cnt, op, i;
673 bpf_u_int32 localnet, netmask;
674 register char *cp, *infile, *cmdbuf, *device, *RFileName, *VFileName, *WFileName;
675 pcap_handler callback;
679 const char *dlt_name;
680 struct bpf_program fcode;
682 RETSIGTYPE (*oldhandler)(int);
684 struct print_info printinfo;
685 struct dump_info dumpinfo;
686 u_char *pcap_userdata;
687 char ebuf[PCAP_ERRBUF_SIZE];
688 char VFileLine[NAME_MAX + 1];
689 char *username = NULL;
690 char *chroot_dir = NULL;
692 #ifdef HAVE_PCAP_FINDALLDEVS
693 pcap_if_t *devpointer;
699 if(wsockinit() != 0) return 1;
702 jflag=-1; /* not set */
706 gndo->ndo_default_print=ndo_default_print;
707 gndo->ndo_printf=tcpdump_printf;
708 gndo->ndo_error=ndo_error;
709 gndo->ndo_warning=ndo_warning;
710 gndo->ndo_snaplen = DEFAULT_SNAPLEN;
718 if ((cp = strrchr(argv[0], '/')) != NULL)
719 program_name = cp + 1;
721 program_name = argv[0];
723 if (abort_on_misalignment(ebuf, sizeof(ebuf)) < 0)
731 (op = getopt(argc, argv, "aAb
" B_FLAG "c
:C
:d
" D_FLAG "eE
:fF
:G
:hHi
:" I_FLAG j_FLAG J_FLAG "KlLm
:M
:nNOpqr
:Rs
:StT
:u
" U_FLAG "V
:vw
:W
:xXy
:Yz
:Z
:")) != -1)
735 /* compatibility for old -a */
746 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
748 Bflag = atoi(optarg)*1024;
750 error("invalid packet buffer size
%s
", optarg);
752 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
757 error("invalid packet count
%s
", optarg);
761 Cflag = atoi(optarg) * 1000000;
763 error("invalid file size
%s
", optarg);
770 #ifdef HAVE_PCAP_FINDALLDEVS
772 if (pcap_findalldevs(&devpointer, ebuf) < 0)
775 for (i = 0; devpointer != 0; i++) {
776 printf("%d
.%s
", i+1, devpointer->name);
777 if (devpointer->description != NULL)
778 printf(" (%s
)", devpointer->description);
780 devpointer = devpointer->next;
784 #endif /* HAVE_PCAP_FINDALLDEVS */
795 #ifndef HAVE_LIBCRYPTO
796 warning("crypto code
not compiled in
");
798 gndo->ndo_espsecret = optarg;
810 Gflag = atoi(optarg);
812 error("invalid number of seconds
%s
", optarg);
814 /* We will create one file initially. */
817 /* Grab the current time for rotation use. */
818 if ((Gflag_time = time(NULL)) == (time_t)-1) {
819 error("main
: can
't get current time: %s",
820 pcap_strerror(errno));
833 if (optarg[0] == '0' && optarg
[1] == 0)
834 error("Invalid adapter index");
836 #ifdef HAVE_PCAP_FINDALLDEVS
838 * If the argument is a number, treat it as
839 * an index into the list of adapters, as
840 * printed by "tcpdump -D".
842 * This should be OK on UNIX systems, as interfaces
843 * shouldn't have names that begin with digits.
844 * It can be useful on Windows, where more than
845 * one interface can have the same name.
847 if ((devnum
= atoi(optarg
)) != 0) {
849 error("Invalid adapter index");
851 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
855 * Look for the devnum-th entry
856 * in the list of devices
860 i
< devnum
-1 && devpointer
!= NULL
;
861 i
++, devpointer
= devpointer
->next
)
863 if (devpointer
== NULL
)
864 error("Invalid adapter index");
866 device
= devpointer
->name
;
869 #endif /* HAVE_PCAP_FINDALLDEVS */
873 #ifdef HAVE_PCAP_CREATE
877 #endif /* HAVE_PCAP_CREATE */
879 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
881 jflag
= pcap_tstamp_type_name_to_val(optarg
);
883 error("invalid time stamp type %s", optarg
);
894 * _IOLBF is the same as _IOFBF in Microsoft's C
895 * libraries; the only alternative they offer
898 * XXX - this should really be checking for MSVC++,
899 * not WIN32, if, for example, MinGW has its own
900 * C library that is more UNIX-compatible.
902 setvbuf(stdout
, NULL
, _IONBF
, 0);
904 #ifdef HAVE_SETLINEBUF
907 setvbuf(stdout
, NULL
, _IOLBF
, 0);
918 if (smiLoadModule(optarg
) == 0) {
919 error("could not load MIB module %s", optarg
);
923 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
924 program_name
, optarg
);
925 (void)fprintf(stderr
, "(no libsmi support)\n");
930 /* TCP-MD5 shared secret */
931 #ifndef HAVE_LIBCRYPTO
932 warning("crypto code not compiled in");
955 ++suppress_default_print
;
969 snaplen
= strtol(optarg
, &end
, 0);
970 if (optarg
== end
|| *end
!= '\0'
971 || snaplen
< 0 || snaplen
> MAXIMUM_SNAPLEN
)
972 error("invalid snaplen %s", optarg
);
973 else if (snaplen
== 0)
974 snaplen
= MAXIMUM_SNAPLEN
;
987 if (strcasecmp(optarg
, "vat") == 0)
989 else if (strcasecmp(optarg
, "wb") == 0)
991 else if (strcasecmp(optarg
, "rpc") == 0)
993 else if (strcasecmp(optarg
, "rtp") == 0)
995 else if (strcasecmp(optarg
, "rtcp") == 0)
996 packettype
= PT_RTCP
;
997 else if (strcasecmp(optarg
, "snmp") == 0)
998 packettype
= PT_SNMP
;
999 else if (strcasecmp(optarg
, "cnfp") == 0)
1000 packettype
= PT_CNFP
;
1001 else if (strcasecmp(optarg
, "tftp") == 0)
1002 packettype
= PT_TFTP
;
1003 else if (strcasecmp(optarg
, "aodv") == 0)
1004 packettype
= PT_AODV
;
1005 else if (strcasecmp(optarg
, "carp") == 0)
1006 packettype
= PT_CARP
;
1007 else if (strcasecmp(optarg
, "radius") == 0)
1008 packettype
= PT_RADIUS
;
1010 error("unknown packet type `%s'", optarg
);
1017 #ifdef HAVE_PCAP_DUMP_FLUSH
1036 Wflag
= atoi(optarg
);
1038 error("invalid number of output files %s", optarg
);
1039 WflagChars
= getWflagChars(Wflag
);
1044 ++suppress_default_print
;
1049 ++suppress_default_print
;
1053 gndo
->ndo_dltname
= optarg
;
1055 pcap_datalink_name_to_val(gndo
->ndo_dltname
);
1056 if (gndo
->ndo_dlt
< 0)
1057 error("invalid data link type %s", gndo
->ndo_dltname
);
1060 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
1063 /* Undocumented flag */
1064 #ifdef HAVE_PCAP_DEBUG
1065 extern int pcap_debug
;
1076 zflag
= strdup(optarg
);
1085 username
= strdup(optarg
);
1100 case 0: /* Default */
1101 case 4: /* Default + Date*/
1102 thiszone
= gmt2local(0);
1105 case 1: /* No time stamp */
1106 case 2: /* Unix timeval style */
1107 case 3: /* Microseconds since previous packet */
1108 case 5: /* Microseconds since first packet */
1111 default: /* Not supported */
1112 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
1116 if (fflag
!= 0 && (VFileName
!= NULL
|| RFileName
!= NULL
))
1117 error("-f can not be used with -V or -r");
1119 if (VFileName
!= NULL
&& RFileName
!= NULL
)
1120 error("-V and -r are mutually exclusive.");
1123 /* if run as root, prepare for chrooting */
1124 if (getuid() == 0 || geteuid() == 0) {
1125 /* future extensibility for cmd-line arguments */
1127 chroot_dir
= WITH_CHROOT
;
1132 /* if run as root, prepare for dropping root privileges */
1133 if (getuid() == 0 || geteuid() == 0) {
1134 /* Run with '-Z root' to restore old behaviour */
1136 username
= WITH_USER
;
1140 if (RFileName
!= NULL
|| VFileName
!= NULL
) {
1143 * We don't need network access, so relinquish any set-UID
1144 * or set-GID privileges we have (if any).
1146 * We do *not* want set-UID privileges when opening a
1147 * trace file, as that might let the user read other
1148 * people's trace files (especially if we're set-UID
1151 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
1152 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1154 if (VFileName
!= NULL
) {
1155 if (VFileName
[0] == '-' && VFileName
[1] == '\0')
1158 VFile
= fopen(VFileName
, "r");
1161 error("Unable to open file: %s\n", strerror(errno
));
1163 ret
= get_next_file(VFile
, VFileLine
);
1165 error("Nothing in %s\n", VFileName
);
1166 RFileName
= VFileLine
;
1169 pd
= pcap_open_offline(RFileName
, ebuf
);
1172 dlt
= pcap_datalink(pd
);
1173 dlt_name
= pcap_datalink_val_to_name(dlt
);
1174 if (dlt_name
== NULL
) {
1175 fprintf(stderr
, "reading from file %s, link-type %u\n",
1179 "reading from file %s, link-type %s (%s)\n",
1180 RFileName
, dlt_name
,
1181 pcap_datalink_val_to_description(dlt
));
1186 if (device
== NULL
) {
1187 device
= pcap_lookupdev(ebuf
);
1192 if(strlen(device
) == 1) //we assume that an ASCII string is always longer than 1 char
1193 { //a Unicode string has a \0 as second byte (so strlen() is 1)
1194 fprintf(stderr
, "%s: listening on %ws\n", program_name
, device
);
1198 fprintf(stderr
, "%s: listening on %s\n", program_name
, device
);
1203 #ifdef HAVE_PCAP_CREATE
1204 pd
= pcap_create(device
, ebuf
);
1207 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1209 show_tstamp_types_and_exit(device
, pd
);
1212 * Is this an interface that supports monitor mode?
1214 if (pcap_can_set_rfmon(pd
) == 1)
1215 supports_monitor_mode
= 1;
1217 supports_monitor_mode
= 0;
1218 status
= pcap_set_snaplen(pd
, snaplen
);
1220 error("%s: Can't set snapshot length: %s",
1221 device
, pcap_statustostr(status
));
1222 status
= pcap_set_promisc(pd
, !pflag
);
1224 error("%s: Can't set promiscuous mode: %s",
1225 device
, pcap_statustostr(status
));
1227 status
= pcap_set_rfmon(pd
, 1);
1229 error("%s: Can't set monitor mode: %s",
1230 device
, pcap_statustostr(status
));
1232 status
= pcap_set_timeout(pd
, 1000);
1234 error("%s: pcap_set_timeout failed: %s",
1235 device
, pcap_statustostr(status
));
1237 status
= pcap_set_buffer_size(pd
, Bflag
);
1239 error("%s: Can't set buffer size: %s",
1240 device
, pcap_statustostr(status
));
1242 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1244 status
= pcap_set_tstamp_type(pd
, jflag
);
1246 error("%s: Can't set time stamp type: %s",
1247 device
, pcap_statustostr(status
));
1250 status
= pcap_activate(pd
);
1253 * pcap_activate() failed.
1255 cp
= pcap_geterr(pd
);
1256 if (status
== PCAP_ERROR
)
1258 else if ((status
== PCAP_ERROR_NO_SUCH_DEVICE
||
1259 status
== PCAP_ERROR_PERM_DENIED
) &&
1261 error("%s: %s\n(%s)", device
,
1262 pcap_statustostr(status
), cp
);
1264 error("%s: %s", device
,
1265 pcap_statustostr(status
));
1266 } else if (status
> 0) {
1268 * pcap_activate() succeeded, but it's warning us
1269 * of a problem it had.
1271 cp
= pcap_geterr(pd
);
1272 if (status
== PCAP_WARNING
)
1274 else if (status
== PCAP_WARNING_PROMISC_NOTSUP
&&
1276 warning("%s: %s\n(%s)", device
,
1277 pcap_statustostr(status
), cp
);
1279 warning("%s: %s", device
,
1280 pcap_statustostr(status
));
1284 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
1288 warning("%s", ebuf
);
1289 #endif /* HAVE_PCAP_CREATE */
1291 * Let user own process after socket has been opened.
1294 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
1295 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1297 #if !defined(HAVE_PCAP_CREATE) && defined(WIN32)
1299 if(pcap_setbuff(pd
, Bflag
)==-1){
1300 error("%s", pcap_geterr(pd
));
1302 #endif /* !defined(HAVE_PCAP_CREATE) && defined(WIN32) */
1304 show_dlts_and_exit(device
, pd
);
1305 if (gndo
->ndo_dlt
>= 0) {
1306 #ifdef HAVE_PCAP_SET_DATALINK
1307 if (pcap_set_datalink(pd
, gndo
->ndo_dlt
) < 0)
1308 error("%s", pcap_geterr(pd
));
1311 * We don't actually support changing the
1312 * data link type, so we only let them
1313 * set it to what it already is.
1315 if (gndo
->ndo_dlt
!= pcap_datalink(pd
)) {
1316 error("%s is not one of the DLTs supported by this device\n",
1320 (void)fprintf(stderr
, "%s: data link type %s\n",
1321 program_name
, gndo
->ndo_dltname
);
1322 (void)fflush(stderr
);
1324 i
= pcap_snapshot(pd
);
1326 warning("snaplen raised from %d to %d", snaplen
, i
);
1329 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
1332 warning("%s", ebuf
);
1336 cmdbuf
= read_infile(infile
);
1338 cmdbuf
= copy_argv(&argv
[optind
]);
1340 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
1341 error("%s", pcap_geterr(pd
));
1343 bpf_dump(&fcode
, dflag
);
1348 init_addrtoname(localnet
, netmask
);
1352 (void)setsignal(SIGPIPE
, cleanup
);
1353 (void)setsignal(SIGTERM
, cleanup
);
1354 (void)setsignal(SIGINT
, cleanup
);
1356 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1357 (void)setsignal(SIGCHLD
, child_cleanup
);
1359 /* Cooperate with nohup(1) */
1361 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
1362 (void)setsignal(SIGHUP
, oldhandler
);
1367 * If a user name was specified with "-Z", attempt to switch to
1368 * that user's UID. This would probably be used with sudo,
1369 * to allow tcpdump to be run in a special restricted
1370 * account (if you just want to allow users to open capture
1371 * devices, and can't just give users that permission,
1372 * you'd make tcpdump set-UID or set-GID).
1374 * Tcpdump doesn't necessarily write only to one savefile;
1375 * the general only way to allow a -Z instance to write to
1376 * savefiles as the user under whose UID it's run, rather
1377 * than as the user specified with -Z, would thus be to switch
1378 * to the original user ID before opening a capture file and
1379 * then switch back to the -Z user ID after opening the savefile.
1380 * Switching to the -Z user ID only after opening the first
1381 * savefile doesn't handle the general case.
1384 #ifdef HAVE_CAP_NG_H
1385 /* We are running as root and we will be writing to savefile */
1386 if ((getuid() == 0 || geteuid() == 0) && WFileName
) {
1388 /* Drop all capabilities from effective set */
1389 capng_clear(CAPNG_EFFECTIVE
);
1390 /* Add capabilities we will need*/
1391 capng_update(CAPNG_ADD
, CAPNG_PERMITTED
, CAP_SETUID
);
1392 capng_update(CAPNG_ADD
, CAPNG_PERMITTED
, CAP_SETGID
);
1393 capng_update(CAPNG_ADD
, CAPNG_PERMITTED
, CAP_DAC_OVERRIDE
);
1395 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_SETUID
);
1396 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_SETGID
);
1397 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
1399 capng_apply(CAPNG_SELECT_BOTH
);
1402 #endif /* HAVE_CAP_NG_H */
1404 if (getuid() == 0 || geteuid() == 0) {
1405 if (username
|| chroot_dir
)
1406 droproot(username
, chroot_dir
);
1411 if (pcap_setfilter(pd
, &fcode
) < 0)
1412 error("%s", pcap_geterr(pd
));
1415 /* Do not exceed the default NAME_MAX for files. */
1416 dumpinfo
.CurrentFileName
= (char *)malloc(NAME_MAX
+ 1);
1418 if (dumpinfo
.CurrentFileName
== NULL
)
1419 error("malloc of dumpinfo.CurrentFileName");
1421 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1423 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, WflagChars
);
1425 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, 0);
1427 p
= pcap_dump_open(pd
, dumpinfo
.CurrentFileName
);
1428 #ifdef HAVE_CAP_NG_H
1429 /* Give up capabilities, clear Effective set */
1430 capng_clear(CAPNG_EFFECTIVE
);
1433 error("%s", pcap_geterr(pd
));
1434 if (Cflag
!= 0 || Gflag
!= 0) {
1435 callback
= dump_packet_and_trunc
;
1436 dumpinfo
.WFileName
= WFileName
;
1439 pcap_userdata
= (u_char
*)&dumpinfo
;
1441 callback
= dump_packet
;
1442 pcap_userdata
= (u_char
*)p
;
1444 #ifdef HAVE_PCAP_DUMP_FLUSH
1449 type
= pcap_datalink(pd
);
1450 printinfo
= get_print_info(type
);
1451 callback
= print_packet
;
1452 pcap_userdata
= (u_char
*)&printinfo
;
1455 #ifdef SIGNAL_REQ_INFO
1457 * We can't get statistics when reading from a file rather
1458 * than capturing from a device.
1460 if (RFileName
== NULL
)
1461 (void)setsignal(SIGNAL_REQ_INFO
, requestinfo
);
1464 if (vflag
> 0 && WFileName
) {
1466 * When capturing to a file, "-v" means tcpdump should,
1467 * every 10 secodns, "v"erbosely report the number of
1470 #ifdef USE_WIN32_MM_TIMER
1471 /* call verbose_stats_dump() each 1000 +/-100msec */
1472 timer_id
= timeSetEvent(1000, 100, verbose_stats_dump
, 0, TIME_PERIODIC
);
1473 setvbuf(stderr
, NULL
, _IONBF
, 0);
1474 #elif defined(HAVE_ALARM)
1475 (void)setsignal(SIGALRM
, verbose_stats_dump
);
1481 if (RFileName
== NULL
) {
1482 if (!vflag
&& !WFileName
) {
1483 (void)fprintf(stderr
,
1484 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1487 (void)fprintf(stderr
, "%s: ", program_name
);
1488 dlt
= pcap_datalink(pd
);
1489 dlt_name
= pcap_datalink_val_to_name(dlt
);
1490 if (dlt_name
== NULL
) {
1491 (void)fprintf(stderr
, "listening on %s, link-type %u, capture size %u bytes\n",
1492 device
, dlt
, snaplen
);
1494 (void)fprintf(stderr
, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1496 pcap_datalink_val_to_description(dlt
), snaplen
);
1498 (void)fflush(stderr
);
1502 status
= pcap_loop(pd
, cnt
, callback
, pcap_userdata
);
1503 if (WFileName
== NULL
) {
1505 * We're printing packets. Flush the printed output,
1506 * so it doesn't get intermingled with error output.
1510 * We got interrupted, so perhaps we didn't
1511 * manage to finish a line we were printing.
1512 * Print an extra newline, just in case.
1516 (void)fflush(stdout
);
1522 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
1523 program_name
, pcap_geterr(pd
));
1525 if (RFileName
== NULL
) {
1527 * We're doing a live capture. Report the capture
1533 if (VFileName
!= NULL
) {
1534 ret
= get_next_file(VFile
, VFileLine
);
1536 RFileName
= VFileLine
;
1537 pd
= pcap_open_offline(RFileName
, ebuf
);
1540 new_dlt
= pcap_datalink(pd
);
1541 if (WFileName
&& new_dlt
!= dlt
)
1542 error("%s: new dlt does not match original", RFileName
);
1543 printinfo
= get_print_info(new_dlt
);
1544 dlt_name
= pcap_datalink_val_to_name(new_dlt
);
1545 if (dlt_name
== NULL
) {
1546 fprintf(stderr
, "reading from file %s, link-type %u\n",
1547 RFileName
, new_dlt
);
1550 "reading from file %s, link-type %s (%s)\n",
1551 RFileName
, dlt_name
,
1552 pcap_datalink_val_to_description(new_dlt
));
1554 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
1555 error("%s", pcap_geterr(pd
));
1556 if (pcap_setfilter(pd
, &fcode
) < 0)
1557 error("%s", pcap_geterr(pd
));
1561 while (ret
!= NULL
);
1564 exit(status
== -1 ? 1 : 0);
1567 /* make a clean exit on interrupts */
1569 cleanup(int signo _U_
)
1571 #ifdef USE_WIN32_MM_TIMER
1573 timeKillEvent(timer_id
);
1575 #elif defined(HAVE_ALARM)
1579 #ifdef HAVE_PCAP_BREAKLOOP
1581 * We have "pcap_breakloop()"; use it, so that we do as little
1582 * as possible in the signal handler (it's probably not safe
1583 * to do anything with standard I/O streams in a signal handler -
1584 * the ANSI C standard doesn't say it is).
1589 * We don't have "pcap_breakloop()"; this isn't safe, but
1590 * it's the best we can do. Print the summary if we're
1591 * not reading from a savefile - i.e., if we're doing a
1592 * live capture - and exit.
1594 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
1596 * We got interrupted, so perhaps we didn't
1597 * manage to finish a line we were printing.
1598 * Print an extra newline, just in case.
1601 (void)fflush(stdout
);
1609 On windows, we do not use a fork, so we do not care less about
1610 waiting a child processes to die
1612 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1614 child_cleanup(int signo _U_
)
1618 #endif /* HAVE_FORK && HAVE_VFORK */
1621 info(register int verbose
)
1623 struct pcap_stat stat
;
1626 * Older versions of libpcap didn't set ps_ifdrop on some
1627 * platforms; initialize it to 0 to handle that.
1630 if (pcap_stats(pd
, &stat
) < 0) {
1631 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
1637 fprintf(stderr
, "%s: ", program_name
);
1639 (void)fprintf(stderr
, "%u packet%s captured", packets_captured
,
1640 PLURAL_SUFFIX(packets_captured
));
1642 fputs(", ", stderr
);
1645 (void)fprintf(stderr
, "%u packet%s received by filter", stat
.ps_recv
,
1646 PLURAL_SUFFIX(stat
.ps_recv
));
1648 fputs(", ", stderr
);
1651 (void)fprintf(stderr
, "%u packet%s dropped by kernel", stat
.ps_drop
,
1652 PLURAL_SUFFIX(stat
.ps_drop
));
1653 if (stat
.ps_ifdrop
!= 0) {
1655 fputs(", ", stderr
);
1658 (void)fprintf(stderr
, "%u packet%s dropped by interface\n",
1659 stat
.ps_ifdrop
, PLURAL_SUFFIX(stat
.ps_ifdrop
));
1665 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1667 compress_savefile(const char *filename
)
1676 * Set to lowest priority so that this doesn't disturb the capture
1679 setpriority(PRIO_PROCESS
, 0, NZERO
- 1);
1681 setpriority(PRIO_PROCESS
, 0, 19);
1683 if (execlp(zflag
, zflag
, filename
, (char *)NULL
) == -1)
1685 "compress_savefile:execlp(%s, %s): %s\n",
1695 #else /* HAVE_FORK && HAVE_VFORK */
1697 compress_savefile(const char *filename
)
1700 "compress_savefile failed. Functionality not implemented under your system\n");
1702 #endif /* HAVE_FORK && HAVE_VFORK */
1705 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1707 struct dump_info
*dump_info
;
1713 dump_info
= (struct dump_info
*)user
;
1716 * XXX - this won't force the file to rotate on the specified time
1717 * boundary, but it will rotate on the first packet received after the
1718 * specified Gflag number of seconds. Note: if a Gflag time boundary
1719 * and a Cflag size boundary coincide, the time rotation will occur
1720 * first thereby cancelling the Cflag boundary (since the file should
1724 /* Check if it is time to rotate */
1727 /* Get the current time */
1728 if ((t
= time(NULL
)) == (time_t)-1) {
1729 error("dump_and_trunc_packet: can't get current_time: %s",
1730 pcap_strerror(errno
));
1734 /* If the time is greater than the specified window, rotate */
1735 if (t
- Gflag_time
>= Gflag
) {
1736 /* Update the Gflag_time */
1738 /* Update Gflag_count */
1741 * Close the current file and open a new one.
1743 pcap_dump_close(dump_info
->p
);
1746 * Compress the file we just closed, if the user asked for it
1749 compress_savefile(dump_info
->CurrentFileName
);
1752 * Check to see if we've exceeded the Wflag (when
1755 if (Cflag
== 0 && Wflag
> 0 && Gflag_count
>= Wflag
) {
1756 (void)fprintf(stderr
, "Maximum file limit reached: %d\n",
1761 if (dump_info
->CurrentFileName
!= NULL
)
1762 free(dump_info
->CurrentFileName
);
1763 /* Allocate space for max filename + \0. */
1764 dump_info
->CurrentFileName
= (char *)malloc(NAME_MAX
+ 1);
1765 if (dump_info
->CurrentFileName
== NULL
)
1766 error("dump_packet_and_trunc: malloc");
1768 * This is always the first file in the Cflag
1770 * We also don't need numbering if Cflag is not set.
1773 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0,
1776 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0, 0);
1778 #ifdef HAVE_CAP_NG_H
1779 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
1780 capng_apply(CAPNG_EFFECTIVE
);
1781 #endif /* HAVE_CAP_NG_H */
1782 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
1783 #ifdef HAVE_CAP_NG_H
1784 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
1785 capng_apply(CAPNG_EFFECTIVE
);
1786 #endif /* HAVE_CAP_NG_H */
1787 if (dump_info
->p
== NULL
)
1788 error("%s", pcap_geterr(pd
));
1793 * XXX - this won't prevent capture files from getting
1794 * larger than Cflag - the last packet written to the
1795 * file could put it over Cflag.
1797 if (Cflag
!= 0 && pcap_dump_ftell(dump_info
->p
) > Cflag
) {
1799 * Close the current file and open a new one.
1801 pcap_dump_close(dump_info
->p
);
1804 * Compress the file we just closed, if the user asked for it
1807 compress_savefile(dump_info
->CurrentFileName
);
1811 if (Cflag_count
>= Wflag
)
1814 if (dump_info
->CurrentFileName
!= NULL
)
1815 free(dump_info
->CurrentFileName
);
1816 dump_info
->CurrentFileName
= (char *)malloc(NAME_MAX
+ 1);
1817 if (dump_info
->CurrentFileName
== NULL
)
1818 error("dump_packet_and_trunc: malloc");
1819 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, Cflag_count
, WflagChars
);
1820 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
1821 if (dump_info
->p
== NULL
)
1822 error("%s", pcap_geterr(pd
));
1825 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
1826 #ifdef HAVE_PCAP_DUMP_FLUSH
1828 pcap_dump_flush(dump_info
->p
);
1837 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1843 pcap_dump(user
, h
, sp
);
1844 #ifdef HAVE_PCAP_DUMP_FLUSH
1846 pcap_dump_flush((pcap_dumper_t
*)user
);
1855 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1857 struct print_info
*print_info
;
1865 print_info
= (struct print_info
*)user
;
1868 * Some printers want to check that they're not walking off the
1869 * end of the packet.
1870 * Rather than pass it all the way down, we set this global.
1872 snapend
= sp
+ h
->caplen
;
1874 if(print_info
->ndo_type
) {
1875 hdrlen
= (*print_info
->p
.ndo_printer
)(print_info
->ndo
, h
, sp
);
1877 hdrlen
= (*print_info
->p
.printer
)(h
, sp
);
1882 * Print the raw packet data in hex and ASCII.
1886 * Include the link-layer header.
1888 hex_and_ascii_print("\n\t", sp
, h
->caplen
);
1891 * Don't include the link-layer header - and if
1892 * we have nothing past the link-layer header,
1895 if (h
->caplen
> hdrlen
)
1896 hex_and_ascii_print("\n\t", sp
+ hdrlen
,
1897 h
->caplen
- hdrlen
);
1901 * Print the raw packet data in hex.
1905 * Include the link-layer header.
1907 hex_print("\n\t", sp
, h
->caplen
);
1910 * Don't include the link-layer header - and if
1911 * we have nothing past the link-layer header,
1914 if (h
->caplen
> hdrlen
)
1915 hex_print("\n\t", sp
+ hdrlen
,
1916 h
->caplen
- hdrlen
);
1920 * Print the raw packet data in ASCII.
1924 * Include the link-layer header.
1926 ascii_print(sp
, h
->caplen
);
1929 * Don't include the link-layer header - and if
1930 * we have nothing past the link-layer header,
1933 if (h
->caplen
> hdrlen
)
1934 ascii_print(sp
+ hdrlen
, h
->caplen
- hdrlen
);
1947 * XXX - there should really be libpcap calls to get the version
1948 * number as a string (the string would be generated from #defines
1949 * at run time, so that it's not generated from string constants
1950 * in the library, as, on many UNIX systems, those constants would
1951 * be statically linked into the application executable image, and
1952 * would thus reflect the version of libpcap on the system on
1953 * which the application was *linked*, not the system on which it's
1956 * That routine should be documented, unlike the "version[]"
1957 * string, so that UNIX vendors providing their own libpcaps
1958 * don't omit it (as a couple of vendors have...).
1960 * Packet.dll should perhaps also export a routine to return the
1961 * version number of the Packet.dll code, to supply the
1962 * "Wpcap_version" information on Windows.
1964 char WDversion
[]="current-cvs.tcpdump.org";
1965 #if !defined(HAVE_GENERATED_VERSION)
1966 char version
[]="current-cvs.tcpdump.org";
1968 char pcap_version
[]="current-cvs.tcpdump.org";
1969 char Wpcap_version
[]="3.1";
1973 * By default, print the specified data out in hex and ASCII.
1976 ndo_default_print(netdissect_options
*ndo _U_
, const u_char
*bp
, u_int length
)
1978 hex_and_ascii_print("\n\t", bp
, length
); /* pass on lf and identation string */
1982 default_print(const u_char
*bp
, u_int length
)
1984 ndo_default_print(gndo
, bp
, length
);
1987 #ifdef SIGNAL_REQ_INFO
1988 RETSIGTYPE
requestinfo(int signo _U_
)
1998 * Called once each second in verbose mode while dumping to file
2000 #ifdef USE_WIN32_MM_TIMER
2001 void CALLBACK
verbose_stats_dump (UINT timer_id _U_
, UINT msg _U_
, DWORD_PTR arg _U_
,
2002 DWORD_PTR dw1 _U_
, DWORD_PTR dw2 _U_
)
2004 struct pcap_stat stat
;
2006 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
2007 fprintf(stderr
, "Got %u\r", packets_captured
);
2009 #elif defined(HAVE_ALARM)
2010 static void verbose_stats_dump(int sig _U_
)
2012 struct pcap_stat stat
;
2014 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
2015 fprintf(stderr
, "Got %u\r", packets_captured
);
2023 extern char version
[];
2024 #ifndef HAVE_PCAP_LIB_VERSION
2025 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
2026 extern char pcap_version
[];
2027 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2028 static char pcap_version
[] = "unknown";
2029 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2030 #endif /* HAVE_PCAP_LIB_VERSION */
2032 #ifdef HAVE_PCAP_LIB_VERSION
2034 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2036 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2038 (void)fprintf(stderr
, "%s\n",pcap_lib_version());
2039 #else /* HAVE_PCAP_LIB_VERSION */
2041 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2042 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
2044 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2045 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
2047 #endif /* HAVE_PCAP_LIB_VERSION */
2048 (void)fprintf(stderr
,
2049 "Usage: %s [-aAbd" D_FLAG
"efhH" I_FLAG J_FLAG
"KlLnNOpqRStu" U_FLAG
"vxX]" B_FLAG_USAGE
" [ -c count ]\n", program_name
);
2050 (void)fprintf(stderr
,
2051 "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n");
2052 (void)fprintf(stderr
,
2053 "\t\t[ -i interface ]" j_FLAG_USAGE
" [ -M secret ]\n");
2054 (void)fprintf(stderr
,
2055 "\t\t[ -r file ] [ -s snaplen ] [ -T type ] [ -V file ] [ -w file ]\n");
2056 (void)fprintf(stderr
,
2057 "\t\t[ -W filecount ] [ -y datalinktype ] [ -z command ]\n");
2058 (void)fprintf(stderr
,
2059 "\t\t[ -Z user ] [ expression ]\n");
2067 ndo_error(netdissect_options
*ndo _U_
, const char *fmt
, ...)
2071 (void)fprintf(stderr
, "%s: ", program_name
);
2073 (void)vfprintf(stderr
, fmt
, ap
);
2077 if (fmt
[-1] != '\n')
2078 (void)fputc('\n', stderr
);
2086 ndo_warning(netdissect_options
*ndo _U_
, const char *fmt
, ...)
2090 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
2092 (void)vfprintf(stderr
, fmt
, ap
);
2096 if (fmt
[-1] != '\n')
2097 (void)fputc('\n', stderr
);