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 #ifdef HAVE_PCAP_SETDIRECTION
109 int Pflag
= -1; /* Restrict captured packet by sent/receive direction */
111 static char *zflag
= NULL
; /* compress each savefile using a specified command (like gzip or bzip2) */
113 static int infodelay
;
114 static int infoprint
;
118 int32_t thiszone
; /* seconds offset from gmt to local time */
121 static RETSIGTYPE
cleanup(int);
122 static RETSIGTYPE
child_cleanup(int);
123 static void usage(void) __attribute__((noreturn
));
124 static void show_dlts_and_exit(const char *device
, pcap_t
*pd
) __attribute__((noreturn
));
126 static void print_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
127 static void ndo_default_print(netdissect_options
*, const u_char
*, u_int
);
128 static void dump_packet_and_trunc(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
129 static void dump_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
130 static void droproot(const char *, const char *);
131 static void ndo_error(netdissect_options
*ndo
, const char *fmt
, ...)
132 __attribute__ ((noreturn
, format (printf
, 2, 3)));
133 static void ndo_warning(netdissect_options
*ndo
, const char *fmt
, ...);
135 #ifdef SIGNAL_REQ_INFO
136 RETSIGTYPE
requestinfo(int);
139 #if defined(USE_WIN32_MM_TIMER)
140 #include <MMsystem.h>
141 static UINT timer_id
;
142 static void CALLBACK
verbose_stats_dump(UINT
, UINT
, DWORD_PTR
, DWORD_PTR
, DWORD_PTR
);
143 #elif defined(HAVE_ALARM)
144 static void verbose_stats_dump(int sig
);
147 static void info(int);
148 static u_int packets_captured
;
162 static struct printer printers
[] = {
163 { arcnet_if_print
, DLT_ARCNET
},
164 #ifdef DLT_ARCNET_LINUX
165 { arcnet_linux_if_print
, DLT_ARCNET_LINUX
},
167 { token_if_print
, DLT_IEEE802
},
169 { lane_if_print
, DLT_LANE8023
},
172 { cip_if_print
, DLT_CIP
},
175 { cip_if_print
, DLT_ATM_CLIP
},
177 { sl_if_print
, DLT_SLIP
},
178 #ifdef DLT_SLIP_BSDOS
179 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
181 { ppp_if_print
, DLT_PPP
},
182 #ifdef DLT_PPP_WITHDIRECTION
183 { ppp_if_print
, DLT_PPP_WITHDIRECTION
},
186 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
188 { fddi_if_print
, DLT_FDDI
},
189 { null_if_print
, DLT_NULL
},
191 { null_if_print
, DLT_LOOP
},
193 { raw_if_print
, DLT_RAW
},
194 { atm_if_print
, DLT_ATM_RFC1483
},
196 { chdlc_if_print
, DLT_C_HDLC
},
199 { chdlc_if_print
, DLT_HDLC
},
201 #ifdef DLT_PPP_SERIAL
202 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
205 { pppoe_if_print
, DLT_PPP_ETHER
},
208 { sll_if_print
, DLT_LINUX_SLL
},
210 #ifdef DLT_IEEE802_11
211 { ieee802_11_if_print
, DLT_IEEE802_11
},
214 { ltalk_if_print
, DLT_LTALK
},
216 #if defined(DLT_PFLOG) && defined(HAVE_NET_PFVAR_H)
217 { pflog_if_print
, DLT_PFLOG
},
220 { fr_if_print
, DLT_FR
},
223 { fr_if_print
, DLT_FRELAY
},
226 { sunatm_if_print
, DLT_SUNATM
},
228 #ifdef DLT_IP_OVER_FC
229 { ipfc_if_print
, DLT_IP_OVER_FC
},
231 #ifdef DLT_PRISM_HEADER
232 { prism_if_print
, DLT_PRISM_HEADER
},
234 #ifdef DLT_IEEE802_11_RADIO
235 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
238 { enc_if_print
, DLT_ENC
},
240 #ifdef DLT_SYMANTEC_FIREWALL
241 { symantec_if_print
, DLT_SYMANTEC_FIREWALL
},
243 #ifdef DLT_APPLE_IP_OVER_IEEE1394
244 { ap1394_if_print
, DLT_APPLE_IP_OVER_IEEE1394
},
246 #ifdef DLT_IEEE802_11_RADIO_AVS
247 { ieee802_11_radio_avs_if_print
, DLT_IEEE802_11_RADIO_AVS
},
249 #ifdef DLT_JUNIPER_ATM1
250 { juniper_atm1_print
, DLT_JUNIPER_ATM1
},
252 #ifdef DLT_JUNIPER_ATM2
253 { juniper_atm2_print
, DLT_JUNIPER_ATM2
},
255 #ifdef DLT_JUNIPER_MFR
256 { juniper_mfr_print
, DLT_JUNIPER_MFR
},
258 #ifdef DLT_JUNIPER_MLFR
259 { juniper_mlfr_print
, DLT_JUNIPER_MLFR
},
261 #ifdef DLT_JUNIPER_MLPPP
262 { juniper_mlppp_print
, DLT_JUNIPER_MLPPP
},
264 #ifdef DLT_JUNIPER_PPPOE
265 { juniper_pppoe_print
, DLT_JUNIPER_PPPOE
},
267 #ifdef DLT_JUNIPER_PPPOE_ATM
268 { juniper_pppoe_atm_print
, DLT_JUNIPER_PPPOE_ATM
},
270 #ifdef DLT_JUNIPER_GGSN
271 { juniper_ggsn_print
, DLT_JUNIPER_GGSN
},
273 #ifdef DLT_JUNIPER_ES
274 { juniper_es_print
, DLT_JUNIPER_ES
},
276 #ifdef DLT_JUNIPER_MONITOR
277 { juniper_monitor_print
, DLT_JUNIPER_MONITOR
},
279 #ifdef DLT_JUNIPER_SERVICES
280 { juniper_services_print
, DLT_JUNIPER_SERVICES
},
282 #ifdef DLT_JUNIPER_ETHER
283 { juniper_ether_print
, DLT_JUNIPER_ETHER
},
285 #ifdef DLT_JUNIPER_PPP
286 { juniper_ppp_print
, DLT_JUNIPER_PPP
},
288 #ifdef DLT_JUNIPER_FRELAY
289 { juniper_frelay_print
, DLT_JUNIPER_FRELAY
},
291 #ifdef DLT_JUNIPER_CHDLC
292 { juniper_chdlc_print
, DLT_JUNIPER_CHDLC
},
295 { mfr_if_print
, DLT_MFR
},
297 #if defined(DLT_BLUETOOTH_HCI_H4_WITH_PHDR) && defined(HAVE_PCAP_BLUETOOTH_H)
298 { bt_if_print
, DLT_BLUETOOTH_HCI_H4_WITH_PHDR
},
300 #ifdef HAVE_PCAP_USB_H
302 { usb_linux_48_byte_print
, DLT_USB_LINUX
},
303 #endif /* DLT_USB_LINUX */
304 #ifdef DLT_USB_LINUX_MMAPPED
305 { usb_linux_64_byte_print
, DLT_USB_LINUX_MMAPPED
},
306 #endif /* DLT_USB_LINUX_MMAPPED */
307 #endif /* HAVE_PCAP_USB_H */
309 { raw_if_print
, DLT_IPV4
},
312 { raw_if_print
, DLT_IPV6
},
317 static struct ndo_printer ndo_printers
[] = {
318 { ether_if_print
, DLT_EN10MB
},
320 { ipnet_if_print
, DLT_IPNET
},
322 #ifdef DLT_IEEE802_15_4
323 { ieee802_15_4_if_print
, DLT_IEEE802_15_4
},
325 #ifdef DLT_IEEE802_15_4_NOFCS
326 { ieee802_15_4_if_print
, DLT_IEEE802_15_4_NOFCS
},
329 { ppi_if_print
, DLT_PPI
},
331 #ifdef DLT_NETANALYZER
332 { netanalyzer_if_print
, DLT_NETANALYZER
},
334 #ifdef DLT_NETANALYZER_TRANSPARENT
335 { netanalyzer_transparent_if_print
, DLT_NETANALYZER_TRANSPARENT
},
338 { nflog_if_print
, DLT_NFLOG
},
344 lookup_printer(int type
)
348 for (p
= printers
; p
->f
; ++p
)
357 lookup_ndo_printer(int type
)
359 struct ndo_printer
*p
;
361 for (p
= ndo_printers
; p
->f
; ++p
)
371 static int supports_monitor_mode
;
378 netdissect_options
*ndo
;
381 if_ndo_printer ndo_printer
;
388 char *CurrentFileName
;
393 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
395 show_tstamp_types_and_exit(const char *device
, pcap_t
*pd
)
398 int *tstamp_types
= 0;
399 const char *tstamp_type_name
;
402 n_tstamp_types
= pcap_list_tstamp_types(pd
, &tstamp_types
);
403 if (n_tstamp_types
< 0)
404 error("%s", pcap_geterr(pd
));
406 if (n_tstamp_types
== 0) {
407 fprintf(stderr
, "Time stamp type cannot be set for %s\n",
411 fprintf(stderr
, "Time stamp types for %s (use option -j to set):\n",
413 for (i
= 0; i
< n_tstamp_types
; i
++) {
414 tstamp_type_name
= pcap_tstamp_type_val_to_name(tstamp_types
[i
]);
415 if (tstamp_type_name
!= NULL
) {
416 (void) fprintf(stderr
, " %s (%s)\n", tstamp_type_name
,
417 pcap_tstamp_type_val_to_description(tstamp_types
[i
]));
419 (void) fprintf(stderr
, " %d\n", tstamp_types
[i
]);
422 pcap_free_tstamp_types(tstamp_types
);
428 show_dlts_and_exit(const char *device
, pcap_t
*pd
)
432 const char *dlt_name
;
434 n_dlts
= pcap_list_datalinks(pd
, &dlts
);
436 error("%s", pcap_geterr(pd
));
437 else if (n_dlts
== 0 || !dlts
)
438 error("No data link types.");
441 * If the interface is known to support monitor mode, indicate
442 * whether these are the data link types available when not in
443 * monitor mode, if -I wasn't specified, or when in monitor mode,
444 * when -I was specified (the link-layer types available in
445 * monitor mode might be different from the ones available when
446 * not in monitor mode).
448 if (supports_monitor_mode
)
449 (void) fprintf(stderr
, "Data link types for %s %s (use option -y to set):\n",
451 Iflag
? "when in monitor mode" : "when not in monitor mode");
453 (void) fprintf(stderr
, "Data link types for %s (use option -y to set):\n",
456 while (--n_dlts
>= 0) {
457 dlt_name
= pcap_datalink_val_to_name(dlts
[n_dlts
]);
458 if (dlt_name
!= NULL
) {
459 (void) fprintf(stderr
, " %s (%s)", dlt_name
,
460 pcap_datalink_val_to_description(dlts
[n_dlts
]));
463 * OK, does tcpdump handle that type?
465 if (lookup_printer(dlts
[n_dlts
]) == NULL
466 && lookup_ndo_printer(dlts
[n_dlts
]) == NULL
)
467 (void) fprintf(stderr
, " (printing not supported)");
468 fprintf(stderr
, "\n");
470 (void) fprintf(stderr
, " DLT %d (printing not supported)\n",
474 #ifdef HAVE_PCAP_FREE_DATALINKS
475 pcap_free_datalinks(dlts
);
481 * Set up flags that might or might not be supported depending on the
482 * version of libpcap we're using.
484 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
486 #define B_FLAG_USAGE " [ -B size ]"
487 #else /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
490 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
492 #ifdef HAVE_PCAP_CREATE
494 #else /* HAVE_PCAP_CREATE */
496 #endif /* HAVE_PCAP_CREATE */
498 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
500 #define j_FLAG_USAGE " [ -j tstamptype ]"
502 #else /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
506 #endif /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
508 #ifdef HAVE_PCAP_FINDALLDEVS
509 #ifndef HAVE_PCAP_IF_T
510 #undef HAVE_PCAP_FINDALLDEVS
514 #ifdef HAVE_PCAP_FINDALLDEVS
520 #ifdef HAVE_PCAP_DUMP_FLUSH
526 #ifdef HAVE_PCAP_SETDIRECTION
533 /* Drop root privileges and chroot if necessary */
535 droproot(const char *username
, const char *chroot_dir
)
537 struct passwd
*pw
= NULL
;
539 if (chroot_dir
&& !username
) {
540 fprintf(stderr
, "tcpdump: Chroot without dropping root is insecure\n");
544 pw
= getpwnam(username
);
547 if (chroot(chroot_dir
) != 0 || chdir ("/") != 0) {
548 fprintf(stderr
, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
549 chroot_dir
, pcap_strerror(errno
));
554 int ret
= capng_change_id(pw
->pw_uid
, pw
->pw_gid
, CAPNG_NO_FLAG
);
556 printf("error : ret %d\n", ret
);
558 /* We don't need CAP_SETUID and CAP_SETGID */
559 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_SETUID
);
560 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_SETUID
);
561 capng_update(CAPNG_DROP
, CAPNG_PERMITTED
, CAP_SETUID
);
562 capng_update(CAPNG_DROP
, CAPNG_PERMITTED
, CAP_SETUID
);
563 capng_apply(CAPNG_SELECT_BOTH
);
566 if (initgroups(pw
->pw_name
, pw
->pw_gid
) != 0 ||
567 setgid(pw
->pw_gid
) != 0 || setuid(pw
->pw_uid
) != 0) {
568 fprintf(stderr
, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
570 (unsigned long)pw
->pw_uid
,
571 (unsigned long)pw
->pw_gid
,
572 pcap_strerror(errno
));
575 #endif /* HAVE_CAP_NG_H */
578 fprintf(stderr
, "tcpdump: Couldn't find user '%.32s'\n",
601 MakeFilename(char *buffer
, char *orig_name
, int cnt
, int max_chars
)
603 char *filename
= malloc(PATH_MAX
+ 1);
604 if (filename
== NULL
)
605 error("Makefilename: malloc");
607 /* Process with strftime if Gflag is set. */
611 /* Convert Gflag_time to a usable format */
612 if ((local_tm
= localtime(&Gflag_time
)) == NULL
) {
613 error("MakeTimedFilename: localtime");
616 /* There's no good way to detect an error in strftime since a return
617 * value of 0 isn't necessarily failure.
619 strftime(filename
, PATH_MAX
, orig_name
, local_tm
);
621 strncpy(filename
, orig_name
, PATH_MAX
);
624 if (cnt
== 0 && max_chars
== 0)
625 strncpy(buffer
, filename
, PATH_MAX
+ 1);
627 if (snprintf(buffer
, PATH_MAX
+ 1, "%s%0*d", filename
, max_chars
, cnt
) > PATH_MAX
)
628 /* Report an error if the filename is too large */
629 error("too many output files or filename is too long (> %d)", PATH_MAX
);
633 static int tcpdump_printf(netdissect_options
*ndo _U_
,
634 const char *fmt
, ...)
641 ret
=vfprintf(stdout
, fmt
, args
);
647 static struct print_info
648 get_print_info(int type
)
650 struct print_info printinfo
;
652 printinfo
.ndo_type
= 1;
653 printinfo
.ndo
= gndo
;
654 printinfo
.p
.ndo_printer
= lookup_ndo_printer(type
);
655 if (printinfo
.p
.ndo_printer
== NULL
) {
656 printinfo
.p
.printer
= lookup_printer(type
);
657 printinfo
.ndo_type
= 0;
658 if (printinfo
.p
.printer
== NULL
) {
659 gndo
->ndo_dltname
= pcap_datalink_val_to_name(type
);
660 if (gndo
->ndo_dltname
!= NULL
)
661 error("packet printing is not supported for link type %s: use -w",
664 error("packet printing is not supported for link type %d: use -w", type
);
671 get_next_file(FILE *VFile
, char *ptr
)
675 ret
= fgets(ptr
, PATH_MAX
, VFile
);
679 if (ptr
[strlen(ptr
) - 1] == '\n')
680 ptr
[strlen(ptr
) - 1] = '\0';
686 main(int argc
, char **argv
)
688 register int cnt
, op
, i
;
689 bpf_u_int32 localnet
, netmask
;
690 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *VFileName
, *WFileName
;
691 pcap_handler callback
;
695 const char *dlt_name
;
696 struct bpf_program fcode
;
698 RETSIGTYPE (*oldhandler
)(int);
700 struct print_info printinfo
;
701 struct dump_info dumpinfo
;
702 u_char
*pcap_userdata
;
703 char ebuf
[PCAP_ERRBUF_SIZE
];
704 char VFileLine
[PATH_MAX
+ 1];
705 char *username
= NULL
;
706 char *chroot_dir
= NULL
;
709 #ifdef HAVE_PCAP_FINDALLDEVS
710 pcap_if_t
*devpointer
;
716 if(wsockinit() != 0) return 1;
719 jflag
=-1; /* not set */
723 gndo
->ndo_default_print
=ndo_default_print
;
724 gndo
->ndo_printf
=tcpdump_printf
;
725 gndo
->ndo_error
=ndo_error
;
726 gndo
->ndo_warning
=ndo_warning
;
727 gndo
->ndo_snaplen
= DEFAULT_SNAPLEN
;
737 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
738 program_name
= cp
+ 1;
740 program_name
= argv
[0];
742 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
750 (op
= getopt(argc
, argv
, "aAb" B_FLAG
"c:C:d" D_FLAG
"eE:fF:G:hHi:" I_FLAG j_FLAG J_FLAG
"KlLm:M:nNOp" P_FLAG
"qr:Rs:StT:u" U_FLAG
"V:vw:W:xXy:Yz:Z:")) != -1)
754 /* compatibility for old -a */
765 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
767 Bflag
= atoi(optarg
)*1024;
769 error("invalid packet buffer size %s", optarg
);
771 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
776 error("invalid packet count %s", optarg
);
780 Cflag
= atoi(optarg
) * 1000000;
782 error("invalid file size %s", optarg
);
789 #ifdef HAVE_PCAP_FINDALLDEVS
791 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
794 for (i
= 0; devpointer
!= 0; i
++) {
795 printf("%d.%s", i
+1, devpointer
->name
);
796 if (devpointer
->description
!= NULL
)
797 printf(" (%s)", devpointer
->description
);
799 devpointer
= devpointer
->next
;
803 #endif /* HAVE_PCAP_FINDALLDEVS */
814 #ifndef HAVE_LIBCRYPTO
815 warning("crypto code not compiled in");
817 gndo
->ndo_espsecret
= optarg
;
829 Gflag
= atoi(optarg
);
831 error("invalid number of seconds %s", optarg
);
833 /* We will create one file initially. */
836 /* Grab the current time for rotation use. */
837 if ((Gflag_time
= time(NULL
)) == (time_t)-1) {
838 error("main: can't get current time: %s",
839 pcap_strerror(errno
));
852 if (optarg
[0] == '0' && optarg
[1] == 0)
853 error("Invalid adapter index");
855 #ifdef HAVE_PCAP_FINDALLDEVS
857 * If the argument is a number, treat it as
858 * an index into the list of adapters, as
859 * printed by "tcpdump -D".
861 * This should be OK on UNIX systems, as interfaces
862 * shouldn't have names that begin with digits.
863 * It can be useful on Windows, where more than
864 * one interface can have the same name.
866 devnum
= strtol(optarg
, &end
, 10);
867 if (optarg
!= end
&& *end
== '\0') {
869 error("Invalid adapter index");
871 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
875 * Look for the devnum-th entry
876 * in the list of devices
880 i
< devnum
-1 && devpointer
!= NULL
;
881 i
++, devpointer
= devpointer
->next
)
883 if (devpointer
== NULL
)
884 error("Invalid adapter index");
886 device
= devpointer
->name
;
889 #endif /* HAVE_PCAP_FINDALLDEVS */
893 #ifdef HAVE_PCAP_CREATE
897 #endif /* HAVE_PCAP_CREATE */
899 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
901 jflag
= pcap_tstamp_type_name_to_val(optarg
);
903 error("invalid time stamp type %s", optarg
);
914 * _IOLBF is the same as _IOFBF in Microsoft's C
915 * libraries; the only alternative they offer
918 * XXX - this should really be checking for MSVC++,
919 * not WIN32, if, for example, MinGW has its own
920 * C library that is more UNIX-compatible.
922 setvbuf(stdout
, NULL
, _IONBF
, 0);
924 #ifdef HAVE_SETLINEBUF
927 setvbuf(stdout
, NULL
, _IOLBF
, 0);
938 if (smiLoadModule(optarg
) == 0) {
939 error("could not load MIB module %s", optarg
);
943 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
944 program_name
, optarg
);
945 (void)fprintf(stderr
, "(no libsmi support)\n");
950 /* TCP-MD5 shared secret */
951 #ifndef HAVE_LIBCRYPTO
952 warning("crypto code not compiled in");
972 #ifdef HAVE_PCAP_SETDIRECTION
974 if (strcasecmp(optarg
, "in") == 0)
976 else if (strcasecmp(optarg
, "out") == 0)
978 else if (strcasecmp(optarg
, "inout") == 0)
979 Pflag
= PCAP_D_INOUT
;
981 error("unknown capture direction `%s'", optarg
);
983 #endif /* HAVE_PCAP_SETDIRECTION */
986 ++suppress_default_print
;
998 snaplen
= strtol(optarg
, &end
, 0);
999 if (optarg
== end
|| *end
!= '\0'
1000 || snaplen
< 0 || snaplen
> MAXIMUM_SNAPLEN
)
1001 error("invalid snaplen %s", optarg
);
1002 else if (snaplen
== 0)
1003 snaplen
= MAXIMUM_SNAPLEN
;
1015 if (strcasecmp(optarg
, "vat") == 0)
1016 packettype
= PT_VAT
;
1017 else if (strcasecmp(optarg
, "wb") == 0)
1019 else if (strcasecmp(optarg
, "rpc") == 0)
1020 packettype
= PT_RPC
;
1021 else if (strcasecmp(optarg
, "rtp") == 0)
1022 packettype
= PT_RTP
;
1023 else if (strcasecmp(optarg
, "rtcp") == 0)
1024 packettype
= PT_RTCP
;
1025 else if (strcasecmp(optarg
, "snmp") == 0)
1026 packettype
= PT_SNMP
;
1027 else if (strcasecmp(optarg
, "cnfp") == 0)
1028 packettype
= PT_CNFP
;
1029 else if (strcasecmp(optarg
, "tftp") == 0)
1030 packettype
= PT_TFTP
;
1031 else if (strcasecmp(optarg
, "aodv") == 0)
1032 packettype
= PT_AODV
;
1033 else if (strcasecmp(optarg
, "carp") == 0)
1034 packettype
= PT_CARP
;
1035 else if (strcasecmp(optarg
, "radius") == 0)
1036 packettype
= PT_RADIUS
;
1037 else if (strcasecmp(optarg
, "zmtp1") == 0)
1038 packettype
= PT_ZMTP1
;
1039 else if (strcasecmp(optarg
, "vxlan") == 0)
1040 packettype
= PT_VXLAN
;
1041 else if (strcasecmp(optarg
, "pgm") == 0)
1042 packettype
= PT_PGM
;
1043 else if (strcasecmp(optarg
, "pgm_zmtp1") == 0)
1044 packettype
= PT_PGM_ZMTP1
;
1045 else if (strcasecmp(optarg
, "lmp") == 0)
1046 packettype
= PT_LMP
;
1048 error("unknown packet type `%s'", optarg
);
1055 #ifdef HAVE_PCAP_DUMP_FLUSH
1074 Wflag
= atoi(optarg
);
1076 error("invalid number of output files %s", optarg
);
1077 WflagChars
= getWflagChars(Wflag
);
1082 ++suppress_default_print
;
1087 ++suppress_default_print
;
1091 gndo
->ndo_dltname
= optarg
;
1093 pcap_datalink_name_to_val(gndo
->ndo_dltname
);
1094 if (gndo
->ndo_dlt
< 0)
1095 error("invalid data link type %s", gndo
->ndo_dltname
);
1098 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
1101 /* Undocumented flag */
1102 #ifdef HAVE_PCAP_DEBUG
1103 extern int pcap_debug
;
1114 zflag
= strdup(optarg
);
1123 username
= strdup(optarg
);
1138 case 0: /* Default */
1139 case 4: /* Default + Date*/
1140 thiszone
= gmt2local(0);
1143 case 1: /* No time stamp */
1144 case 2: /* Unix timeval style */
1145 case 3: /* Microseconds since previous packet */
1146 case 5: /* Microseconds since first packet */
1149 default: /* Not supported */
1150 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
1154 if (fflag
!= 0 && (VFileName
!= NULL
|| RFileName
!= NULL
))
1155 error("-f can not be used with -V or -r");
1157 if (VFileName
!= NULL
&& RFileName
!= NULL
)
1158 error("-V and -r are mutually exclusive.");
1161 /* if run as root, prepare for chrooting */
1162 if (getuid() == 0 || geteuid() == 0) {
1163 /* future extensibility for cmd-line arguments */
1165 chroot_dir
= WITH_CHROOT
;
1170 /* if run as root, prepare for dropping root privileges */
1171 if (getuid() == 0 || geteuid() == 0) {
1172 /* Run with '-Z root' to restore old behaviour */
1174 username
= WITH_USER
;
1178 if (RFileName
!= NULL
|| VFileName
!= NULL
) {
1180 * If RFileName is non-null, it's the pathname of a
1181 * savefile to read. If VFileName is non-null, it's
1182 * the pathname of a file containing a list of pathnames
1183 * (one per line) of savefiles to read.
1185 * In either case, we're reading a savefile, not doing
1190 * We don't need network access, so relinquish any set-UID
1191 * or set-GID privileges we have (if any).
1193 * We do *not* want set-UID privileges when opening a
1194 * trace file, as that might let the user read other
1195 * people's trace files (especially if we're set-UID
1198 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
1199 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1201 if (VFileName
!= NULL
) {
1202 if (VFileName
[0] == '-' && VFileName
[1] == '\0')
1205 VFile
= fopen(VFileName
, "r");
1208 error("Unable to open file: %s\n", strerror(errno
));
1210 ret
= get_next_file(VFile
, VFileLine
);
1212 error("Nothing in %s\n", VFileName
);
1213 RFileName
= VFileLine
;
1216 pd
= pcap_open_offline(RFileName
, ebuf
);
1219 dlt
= pcap_datalink(pd
);
1220 dlt_name
= pcap_datalink_val_to_name(dlt
);
1221 if (dlt_name
== NULL
) {
1222 fprintf(stderr
, "reading from file %s, link-type %u\n",
1226 "reading from file %s, link-type %s (%s)\n",
1227 RFileName
, dlt_name
,
1228 pcap_datalink_val_to_description(dlt
));
1234 * We're doing a live capture.
1236 if (device
== NULL
) {
1237 device
= pcap_lookupdev(ebuf
);
1243 * Print a message to the standard error on Windows.
1244 * XXX - why do it here, with a different message?
1246 if(strlen(device
) == 1) /* we assume that an ASCII string is always longer than 1 char */
1247 { /* a Unicode string has a \0 as second byte (so strlen() is 1) */
1248 fprintf(stderr
, "%s: listening on %ws\n", program_name
, device
);
1252 fprintf(stderr
, "%s: listening on %s\n", program_name
, device
);
1257 #ifdef HAVE_PCAP_CREATE
1258 pd
= pcap_create(device
, ebuf
);
1261 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1263 show_tstamp_types_and_exit(device
, pd
);
1266 * Is this an interface that supports monitor mode?
1268 if (pcap_can_set_rfmon(pd
) == 1)
1269 supports_monitor_mode
= 1;
1271 supports_monitor_mode
= 0;
1272 status
= pcap_set_snaplen(pd
, snaplen
);
1274 error("%s: Can't set snapshot length: %s",
1275 device
, pcap_statustostr(status
));
1276 status
= pcap_set_promisc(pd
, !pflag
);
1278 error("%s: Can't set promiscuous mode: %s",
1279 device
, pcap_statustostr(status
));
1281 status
= pcap_set_rfmon(pd
, 1);
1283 error("%s: Can't set monitor mode: %s",
1284 device
, pcap_statustostr(status
));
1286 status
= pcap_set_timeout(pd
, 1000);
1288 error("%s: pcap_set_timeout failed: %s",
1289 device
, pcap_statustostr(status
));
1291 status
= pcap_set_buffer_size(pd
, Bflag
);
1293 error("%s: Can't set buffer size: %s",
1294 device
, pcap_statustostr(status
));
1296 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1298 status
= pcap_set_tstamp_type(pd
, jflag
);
1300 error("%s: Can't set time stamp type: %s",
1301 device
, pcap_statustostr(status
));
1304 status
= pcap_activate(pd
);
1307 * pcap_activate() failed.
1309 cp
= pcap_geterr(pd
);
1310 if (status
== PCAP_ERROR
)
1312 else if ((status
== PCAP_ERROR_NO_SUCH_DEVICE
||
1313 status
== PCAP_ERROR_PERM_DENIED
) &&
1315 error("%s: %s\n(%s)", device
,
1316 pcap_statustostr(status
), cp
);
1318 error("%s: %s", device
,
1319 pcap_statustostr(status
));
1320 } else if (status
> 0) {
1322 * pcap_activate() succeeded, but it's warning us
1323 * of a problem it had.
1325 cp
= pcap_geterr(pd
);
1326 if (status
== PCAP_WARNING
)
1328 else if (status
== PCAP_WARNING_PROMISC_NOTSUP
&&
1330 warning("%s: %s\n(%s)", device
,
1331 pcap_statustostr(status
), cp
);
1333 warning("%s: %s", device
,
1334 pcap_statustostr(status
));
1336 #ifdef HAVE_PCAP_SETDIRECTION
1338 status
= pcap_setdirection(pd
, Pflag
);
1340 error("%s: pcap_setdirection() failed: %s",
1341 device
, pcap_geterr(pd
));
1346 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
1350 warning("%s", ebuf
);
1351 #endif /* HAVE_PCAP_CREATE */
1353 * Let user own process after socket has been opened.
1356 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
1357 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1359 #if !defined(HAVE_PCAP_CREATE) && defined(WIN32)
1361 if(pcap_setbuff(pd
, Bflag
)==-1){
1362 error("%s", pcap_geterr(pd
));
1364 #endif /* !defined(HAVE_PCAP_CREATE) && defined(WIN32) */
1366 show_dlts_and_exit(device
, pd
);
1367 if (gndo
->ndo_dlt
>= 0) {
1368 #ifdef HAVE_PCAP_SET_DATALINK
1369 if (pcap_set_datalink(pd
, gndo
->ndo_dlt
) < 0)
1370 error("%s", pcap_geterr(pd
));
1373 * We don't actually support changing the
1374 * data link type, so we only let them
1375 * set it to what it already is.
1377 if (gndo
->ndo_dlt
!= pcap_datalink(pd
)) {
1378 error("%s is not one of the DLTs supported by this device\n",
1382 (void)fprintf(stderr
, "%s: data link type %s\n",
1383 program_name
, gndo
->ndo_dltname
);
1384 (void)fflush(stderr
);
1386 i
= pcap_snapshot(pd
);
1388 warning("snaplen raised from %d to %d", snaplen
, i
);
1391 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
1394 warning("%s", ebuf
);
1398 cmdbuf
= read_infile(infile
);
1400 cmdbuf
= copy_argv(&argv
[optind
]);
1402 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
1403 error("%s", pcap_geterr(pd
));
1405 bpf_dump(&fcode
, dflag
);
1410 init_addrtoname(localnet
, netmask
);
1414 (void)setsignal(SIGPIPE
, cleanup
);
1415 (void)setsignal(SIGTERM
, cleanup
);
1416 (void)setsignal(SIGINT
, cleanup
);
1418 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1419 (void)setsignal(SIGCHLD
, child_cleanup
);
1421 /* Cooperate with nohup(1) */
1423 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
1424 (void)setsignal(SIGHUP
, oldhandler
);
1429 * If a user name was specified with "-Z", attempt to switch to
1430 * that user's UID. This would probably be used with sudo,
1431 * to allow tcpdump to be run in a special restricted
1432 * account (if you just want to allow users to open capture
1433 * devices, and can't just give users that permission,
1434 * you'd make tcpdump set-UID or set-GID).
1436 * Tcpdump doesn't necessarily write only to one savefile;
1437 * the general only way to allow a -Z instance to write to
1438 * savefiles as the user under whose UID it's run, rather
1439 * than as the user specified with -Z, would thus be to switch
1440 * to the original user ID before opening a capture file and
1441 * then switch back to the -Z user ID after opening the savefile.
1442 * Switching to the -Z user ID only after opening the first
1443 * savefile doesn't handle the general case.
1446 #ifdef HAVE_CAP_NG_H
1447 /* We are running as root and we will be writing to savefile */
1448 if ((getuid() == 0 || geteuid() == 0) && WFileName
) {
1450 /* Drop all capabilities from effective set */
1451 capng_clear(CAPNG_EFFECTIVE
);
1452 /* Add capabilities we will need*/
1453 capng_update(CAPNG_ADD
, CAPNG_PERMITTED
, CAP_SETUID
);
1454 capng_update(CAPNG_ADD
, CAPNG_PERMITTED
, CAP_SETGID
);
1455 capng_update(CAPNG_ADD
, CAPNG_PERMITTED
, CAP_DAC_OVERRIDE
);
1457 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_SETUID
);
1458 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_SETGID
);
1459 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
1461 capng_apply(CAPNG_SELECT_BOTH
);
1464 #endif /* HAVE_CAP_NG_H */
1466 if (getuid() == 0 || geteuid() == 0) {
1467 if (username
|| chroot_dir
)
1468 droproot(username
, chroot_dir
);
1473 if (pcap_setfilter(pd
, &fcode
) < 0)
1474 error("%s", pcap_geterr(pd
));
1477 /* Do not exceed the default PATH_MAX for files. */
1478 dumpinfo
.CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
1480 if (dumpinfo
.CurrentFileName
== NULL
)
1481 error("malloc of dumpinfo.CurrentFileName");
1483 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1485 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, WflagChars
);
1487 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, 0);
1489 p
= pcap_dump_open(pd
, dumpinfo
.CurrentFileName
);
1490 #ifdef HAVE_CAP_NG_H
1491 /* Give up capabilities, clear Effective set */
1492 capng_clear(CAPNG_EFFECTIVE
);
1495 error("%s", pcap_geterr(pd
));
1496 if (Cflag
!= 0 || Gflag
!= 0) {
1497 callback
= dump_packet_and_trunc
;
1498 dumpinfo
.WFileName
= WFileName
;
1501 pcap_userdata
= (u_char
*)&dumpinfo
;
1503 callback
= dump_packet
;
1504 pcap_userdata
= (u_char
*)p
;
1506 #ifdef HAVE_PCAP_DUMP_FLUSH
1511 type
= pcap_datalink(pd
);
1512 printinfo
= get_print_info(type
);
1513 callback
= print_packet
;
1514 pcap_userdata
= (u_char
*)&printinfo
;
1517 #ifdef SIGNAL_REQ_INFO
1519 * We can't get statistics when reading from a file rather
1520 * than capturing from a device.
1522 if (RFileName
== NULL
)
1523 (void)setsignal(SIGNAL_REQ_INFO
, requestinfo
);
1526 if (vflag
> 0 && WFileName
) {
1528 * When capturing to a file, "-v" means tcpdump should,
1529 * every 10 secodns, "v"erbosely report the number of
1532 #ifdef USE_WIN32_MM_TIMER
1533 /* call verbose_stats_dump() each 1000 +/-100msec */
1534 timer_id
= timeSetEvent(1000, 100, verbose_stats_dump
, 0, TIME_PERIODIC
);
1535 setvbuf(stderr
, NULL
, _IONBF
, 0);
1536 #elif defined(HAVE_ALARM)
1537 (void)setsignal(SIGALRM
, verbose_stats_dump
);
1543 if (RFileName
== NULL
) {
1545 * Live capture (if -V was specified, we set RFileName
1546 * to a file from the -V file). Print a message to
1547 * the standard error on UN*X.
1549 if (!vflag
&& !WFileName
) {
1550 (void)fprintf(stderr
,
1551 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1554 (void)fprintf(stderr
, "%s: ", program_name
);
1555 dlt
= pcap_datalink(pd
);
1556 dlt_name
= pcap_datalink_val_to_name(dlt
);
1557 if (dlt_name
== NULL
) {
1558 (void)fprintf(stderr
, "listening on %s, link-type %u, capture size %u bytes\n",
1559 device
, dlt
, snaplen
);
1561 (void)fprintf(stderr
, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1563 pcap_datalink_val_to_description(dlt
), snaplen
);
1565 (void)fflush(stderr
);
1569 status
= pcap_loop(pd
, cnt
, callback
, pcap_userdata
);
1570 if (WFileName
== NULL
) {
1572 * We're printing packets. Flush the printed output,
1573 * so it doesn't get intermingled with error output.
1577 * We got interrupted, so perhaps we didn't
1578 * manage to finish a line we were printing.
1579 * Print an extra newline, just in case.
1583 (void)fflush(stdout
);
1587 * We got interrupted. If we are reading multiple
1588 * files (via -V) set these so that we stop.
1597 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
1598 program_name
, pcap_geterr(pd
));
1600 if (RFileName
== NULL
) {
1602 * We're doing a live capture. Report the capture
1608 if (VFileName
!= NULL
) {
1609 ret
= get_next_file(VFile
, VFileLine
);
1611 RFileName
= VFileLine
;
1612 pd
= pcap_open_offline(RFileName
, ebuf
);
1615 new_dlt
= pcap_datalink(pd
);
1616 if (WFileName
&& new_dlt
!= dlt
)
1617 error("%s: new dlt does not match original", RFileName
);
1618 printinfo
= get_print_info(new_dlt
);
1619 dlt_name
= pcap_datalink_val_to_name(new_dlt
);
1620 if (dlt_name
== NULL
) {
1621 fprintf(stderr
, "reading from file %s, link-type %u\n",
1622 RFileName
, new_dlt
);
1625 "reading from file %s, link-type %s (%s)\n",
1626 RFileName
, dlt_name
,
1627 pcap_datalink_val_to_description(new_dlt
));
1629 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
1630 error("%s", pcap_geterr(pd
));
1631 if (pcap_setfilter(pd
, &fcode
) < 0)
1632 error("%s", pcap_geterr(pd
));
1636 while (ret
!= NULL
);
1639 exit(status
== -1 ? 1 : 0);
1642 /* make a clean exit on interrupts */
1644 cleanup(int signo _U_
)
1646 #ifdef USE_WIN32_MM_TIMER
1648 timeKillEvent(timer_id
);
1650 #elif defined(HAVE_ALARM)
1654 #ifdef HAVE_PCAP_BREAKLOOP
1656 * We have "pcap_breakloop()"; use it, so that we do as little
1657 * as possible in the signal handler (it's probably not safe
1658 * to do anything with standard I/O streams in a signal handler -
1659 * the ANSI C standard doesn't say it is).
1664 * We don't have "pcap_breakloop()"; this isn't safe, but
1665 * it's the best we can do. Print the summary if we're
1666 * not reading from a savefile - i.e., if we're doing a
1667 * live capture - and exit.
1669 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
1671 * We got interrupted, so perhaps we didn't
1672 * manage to finish a line we were printing.
1673 * Print an extra newline, just in case.
1676 (void)fflush(stdout
);
1684 On windows, we do not use a fork, so we do not care less about
1685 waiting a child processes to die
1687 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1689 child_cleanup(int signo _U_
)
1693 #endif /* HAVE_FORK && HAVE_VFORK */
1696 info(register int verbose
)
1698 struct pcap_stat stat
;
1701 * Older versions of libpcap didn't set ps_ifdrop on some
1702 * platforms; initialize it to 0 to handle that.
1705 if (pcap_stats(pd
, &stat
) < 0) {
1706 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
1712 fprintf(stderr
, "%s: ", program_name
);
1714 (void)fprintf(stderr
, "%u packet%s captured", packets_captured
,
1715 PLURAL_SUFFIX(packets_captured
));
1717 fputs(", ", stderr
);
1720 (void)fprintf(stderr
, "%u packet%s received by filter", stat
.ps_recv
,
1721 PLURAL_SUFFIX(stat
.ps_recv
));
1723 fputs(", ", stderr
);
1726 (void)fprintf(stderr
, "%u packet%s dropped by kernel", stat
.ps_drop
,
1727 PLURAL_SUFFIX(stat
.ps_drop
));
1728 if (stat
.ps_ifdrop
!= 0) {
1730 fputs(", ", stderr
);
1733 (void)fprintf(stderr
, "%u packet%s dropped by interface\n",
1734 stat
.ps_ifdrop
, PLURAL_SUFFIX(stat
.ps_ifdrop
));
1740 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1742 compress_savefile(const char *filename
)
1751 * Set to lowest priority so that this doesn't disturb the capture
1754 setpriority(PRIO_PROCESS
, 0, NZERO
- 1);
1756 setpriority(PRIO_PROCESS
, 0, 19);
1758 if (execlp(zflag
, zflag
, filename
, (char *)NULL
) == -1)
1760 "compress_savefile:execlp(%s, %s): %s\n",
1770 #else /* HAVE_FORK && HAVE_VFORK */
1772 compress_savefile(const char *filename
)
1775 "compress_savefile failed. Functionality not implemented under your system\n");
1777 #endif /* HAVE_FORK && HAVE_VFORK */
1780 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1782 struct dump_info
*dump_info
;
1788 dump_info
= (struct dump_info
*)user
;
1791 * XXX - this won't force the file to rotate on the specified time
1792 * boundary, but it will rotate on the first packet received after the
1793 * specified Gflag number of seconds. Note: if a Gflag time boundary
1794 * and a Cflag size boundary coincide, the time rotation will occur
1795 * first thereby cancelling the Cflag boundary (since the file should
1799 /* Check if it is time to rotate */
1802 /* Get the current time */
1803 if ((t
= time(NULL
)) == (time_t)-1) {
1804 error("dump_and_trunc_packet: can't get current_time: %s",
1805 pcap_strerror(errno
));
1809 /* If the time is greater than the specified window, rotate */
1810 if (t
- Gflag_time
>= Gflag
) {
1811 /* Update the Gflag_time */
1813 /* Update Gflag_count */
1816 * Close the current file and open a new one.
1818 pcap_dump_close(dump_info
->p
);
1821 * Compress the file we just closed, if the user asked for it
1824 compress_savefile(dump_info
->CurrentFileName
);
1827 * Check to see if we've exceeded the Wflag (when
1830 if (Cflag
== 0 && Wflag
> 0 && Gflag_count
>= Wflag
) {
1831 (void)fprintf(stderr
, "Maximum file limit reached: %d\n",
1836 if (dump_info
->CurrentFileName
!= NULL
)
1837 free(dump_info
->CurrentFileName
);
1838 /* Allocate space for max filename + \0. */
1839 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
1840 if (dump_info
->CurrentFileName
== NULL
)
1841 error("dump_packet_and_trunc: malloc");
1843 * This is always the first file in the Cflag
1845 * We also don't need numbering if Cflag is not set.
1848 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0,
1851 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0, 0);
1853 #ifdef HAVE_CAP_NG_H
1854 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
1855 capng_apply(CAPNG_EFFECTIVE
);
1856 #endif /* HAVE_CAP_NG_H */
1857 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
1858 #ifdef HAVE_CAP_NG_H
1859 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
1860 capng_apply(CAPNG_EFFECTIVE
);
1861 #endif /* HAVE_CAP_NG_H */
1862 if (dump_info
->p
== NULL
)
1863 error("%s", pcap_geterr(pd
));
1868 * XXX - this won't prevent capture files from getting
1869 * larger than Cflag - the last packet written to the
1870 * file could put it over Cflag.
1872 if (Cflag
!= 0 && pcap_dump_ftell(dump_info
->p
) > Cflag
) {
1874 * Close the current file and open a new one.
1876 pcap_dump_close(dump_info
->p
);
1879 * Compress the file we just closed, if the user asked for it
1882 compress_savefile(dump_info
->CurrentFileName
);
1886 if (Cflag_count
>= Wflag
)
1889 if (dump_info
->CurrentFileName
!= NULL
)
1890 free(dump_info
->CurrentFileName
);
1891 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
1892 if (dump_info
->CurrentFileName
== NULL
)
1893 error("dump_packet_and_trunc: malloc");
1894 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, Cflag_count
, WflagChars
);
1895 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
1896 if (dump_info
->p
== NULL
)
1897 error("%s", pcap_geterr(pd
));
1900 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
1901 #ifdef HAVE_PCAP_DUMP_FLUSH
1903 pcap_dump_flush(dump_info
->p
);
1912 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1918 pcap_dump(user
, h
, sp
);
1919 #ifdef HAVE_PCAP_DUMP_FLUSH
1921 pcap_dump_flush((pcap_dumper_t
*)user
);
1930 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1932 struct print_info
*print_info
;
1940 print_info
= (struct print_info
*)user
;
1943 * Some printers want to check that they're not walking off the
1944 * end of the packet.
1945 * Rather than pass it all the way down, we set this global.
1947 snapend
= sp
+ h
->caplen
;
1949 if(print_info
->ndo_type
) {
1950 hdrlen
= (*print_info
->p
.ndo_printer
)(print_info
->ndo
, h
, sp
);
1952 hdrlen
= (*print_info
->p
.printer
)(h
, sp
);
1957 * Print the raw packet data in hex and ASCII.
1961 * Include the link-layer header.
1963 hex_and_ascii_print("\n\t", sp
, h
->caplen
);
1966 * Don't include the link-layer header - and if
1967 * we have nothing past the link-layer header,
1970 if (h
->caplen
> hdrlen
)
1971 hex_and_ascii_print("\n\t", sp
+ hdrlen
,
1972 h
->caplen
- hdrlen
);
1976 * Print the raw packet data in hex.
1980 * Include the link-layer header.
1982 hex_print("\n\t", sp
, h
->caplen
);
1985 * Don't include the link-layer header - and if
1986 * we have nothing past the link-layer header,
1989 if (h
->caplen
> hdrlen
)
1990 hex_print("\n\t", sp
+ hdrlen
,
1991 h
->caplen
- hdrlen
);
1995 * Print the raw packet data in ASCII.
1999 * Include the link-layer header.
2001 ascii_print(sp
, h
->caplen
);
2004 * Don't include the link-layer header - and if
2005 * we have nothing past the link-layer header,
2008 if (h
->caplen
> hdrlen
)
2009 ascii_print(sp
+ hdrlen
, h
->caplen
- hdrlen
);
2022 * XXX - there should really be libpcap calls to get the version
2023 * number as a string (the string would be generated from #defines
2024 * at run time, so that it's not generated from string constants
2025 * in the library, as, on many UNIX systems, those constants would
2026 * be statically linked into the application executable image, and
2027 * would thus reflect the version of libpcap on the system on
2028 * which the application was *linked*, not the system on which it's
2031 * That routine should be documented, unlike the "version[]"
2032 * string, so that UNIX vendors providing their own libpcaps
2033 * don't omit it (as a couple of vendors have...).
2035 * Packet.dll should perhaps also export a routine to return the
2036 * version number of the Packet.dll code, to supply the
2037 * "Wpcap_version" information on Windows.
2039 char WDversion
[]="current-cvs.tcpdump.org";
2040 #if !defined(HAVE_GENERATED_VERSION)
2041 char version
[]="current-cvs.tcpdump.org";
2043 char pcap_version
[]="current-cvs.tcpdump.org";
2044 char Wpcap_version
[]="3.1";
2048 * By default, print the specified data out in hex and ASCII.
2051 ndo_default_print(netdissect_options
*ndo _U_
, const u_char
*bp
, u_int length
)
2053 hex_and_ascii_print("\n\t", bp
, length
); /* pass on lf and identation string */
2057 default_print(const u_char
*bp
, u_int length
)
2059 ndo_default_print(gndo
, bp
, length
);
2062 #ifdef SIGNAL_REQ_INFO
2063 RETSIGTYPE
requestinfo(int signo _U_
)
2073 * Called once each second in verbose mode while dumping to file
2075 #ifdef USE_WIN32_MM_TIMER
2076 void CALLBACK
verbose_stats_dump (UINT timer_id _U_
, UINT msg _U_
, DWORD_PTR arg _U_
,
2077 DWORD_PTR dw1 _U_
, DWORD_PTR dw2 _U_
)
2079 struct pcap_stat stat
;
2081 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
2082 fprintf(stderr
, "Got %u\r", packets_captured
);
2084 #elif defined(HAVE_ALARM)
2085 static void verbose_stats_dump(int sig _U_
)
2087 struct pcap_stat stat
;
2089 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
2090 fprintf(stderr
, "Got %u\r", packets_captured
);
2098 extern char version
[];
2099 #ifndef HAVE_PCAP_LIB_VERSION
2100 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
2101 extern char pcap_version
[];
2102 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2103 static char pcap_version
[] = "unknown";
2104 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2105 #endif /* HAVE_PCAP_LIB_VERSION */
2107 #ifdef HAVE_PCAP_LIB_VERSION
2109 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2111 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2113 (void)fprintf(stderr
, "%s\n",pcap_lib_version());
2114 #else /* HAVE_PCAP_LIB_VERSION */
2116 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2117 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
2119 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2120 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
2122 #endif /* HAVE_PCAP_LIB_VERSION */
2123 (void)fprintf(stderr
,
2124 "Usage: %s [-aAbd" D_FLAG
"efhH" I_FLAG J_FLAG
"KlLnNOpqRStu" U_FLAG
"vxX]" B_FLAG_USAGE
" [ -c count ]\n", program_name
);
2125 (void)fprintf(stderr
,
2126 "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n");
2127 (void)fprintf(stderr
,
2128 "\t\t[ -i interface ]" j_FLAG_USAGE
" [ -M secret ]\n");
2129 #ifdef HAVE_PCAP_SETDIRECTION
2130 (void)fprintf(stderr
,
2131 "\t\t[ -P in|out|inout ]\n");
2133 (void)fprintf(stderr
,
2134 "\t\t[ -r file ] [ -s snaplen ] [ -T type ] [ -V file ] [ -w file ]\n");
2135 (void)fprintf(stderr
,
2136 "\t\t[ -W filecount ] [ -y datalinktype ] [ -z command ]\n");
2137 (void)fprintf(stderr
,
2138 "\t\t[ -Z user ] [ expression ]\n");
2146 ndo_error(netdissect_options
*ndo _U_
, const char *fmt
, ...)
2150 (void)fprintf(stderr
, "%s: ", program_name
);
2152 (void)vfprintf(stderr
, fmt
, ap
);
2156 if (fmt
[-1] != '\n')
2157 (void)fputc('\n', stderr
);
2165 ndo_warning(netdissect_options
*ndo _U_
, const char *fmt
, ...)
2169 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
2171 (void)vfprintf(stderr
, fmt
, ap
);
2175 if (fmt
[-1] != '\n')
2176 (void)fputc('\n', stderr
);