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
= PCAP_D_INOUT
; /* 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
1337 status
= pcap_setdirection(pd
, Pflag
);
1339 error("%s: pcap_set_direction failed: %s",
1340 device
, pcap_geterr(pd
));
1344 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
1348 warning("%s", ebuf
);
1349 #endif /* HAVE_PCAP_CREATE */
1351 * Let user own process after socket has been opened.
1354 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
1355 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1357 #if !defined(HAVE_PCAP_CREATE) && defined(WIN32)
1359 if(pcap_setbuff(pd
, Bflag
)==-1){
1360 error("%s", pcap_geterr(pd
));
1362 #endif /* !defined(HAVE_PCAP_CREATE) && defined(WIN32) */
1364 show_dlts_and_exit(device
, pd
);
1365 if (gndo
->ndo_dlt
>= 0) {
1366 #ifdef HAVE_PCAP_SET_DATALINK
1367 if (pcap_set_datalink(pd
, gndo
->ndo_dlt
) < 0)
1368 error("%s", pcap_geterr(pd
));
1371 * We don't actually support changing the
1372 * data link type, so we only let them
1373 * set it to what it already is.
1375 if (gndo
->ndo_dlt
!= pcap_datalink(pd
)) {
1376 error("%s is not one of the DLTs supported by this device\n",
1380 (void)fprintf(stderr
, "%s: data link type %s\n",
1381 program_name
, gndo
->ndo_dltname
);
1382 (void)fflush(stderr
);
1384 i
= pcap_snapshot(pd
);
1386 warning("snaplen raised from %d to %d", snaplen
, i
);
1389 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
1392 warning("%s", ebuf
);
1396 cmdbuf
= read_infile(infile
);
1398 cmdbuf
= copy_argv(&argv
[optind
]);
1400 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
1401 error("%s", pcap_geterr(pd
));
1403 bpf_dump(&fcode
, dflag
);
1408 init_addrtoname(localnet
, netmask
);
1412 (void)setsignal(SIGPIPE
, cleanup
);
1413 (void)setsignal(SIGTERM
, cleanup
);
1414 (void)setsignal(SIGINT
, cleanup
);
1416 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1417 (void)setsignal(SIGCHLD
, child_cleanup
);
1419 /* Cooperate with nohup(1) */
1421 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
1422 (void)setsignal(SIGHUP
, oldhandler
);
1427 * If a user name was specified with "-Z", attempt to switch to
1428 * that user's UID. This would probably be used with sudo,
1429 * to allow tcpdump to be run in a special restricted
1430 * account (if you just want to allow users to open capture
1431 * devices, and can't just give users that permission,
1432 * you'd make tcpdump set-UID or set-GID).
1434 * Tcpdump doesn't necessarily write only to one savefile;
1435 * the general only way to allow a -Z instance to write to
1436 * savefiles as the user under whose UID it's run, rather
1437 * than as the user specified with -Z, would thus be to switch
1438 * to the original user ID before opening a capture file and
1439 * then switch back to the -Z user ID after opening the savefile.
1440 * Switching to the -Z user ID only after opening the first
1441 * savefile doesn't handle the general case.
1444 #ifdef HAVE_CAP_NG_H
1445 /* We are running as root and we will be writing to savefile */
1446 if ((getuid() == 0 || geteuid() == 0) && WFileName
) {
1448 /* Drop all capabilities from effective set */
1449 capng_clear(CAPNG_EFFECTIVE
);
1450 /* Add capabilities we will need*/
1451 capng_update(CAPNG_ADD
, CAPNG_PERMITTED
, CAP_SETUID
);
1452 capng_update(CAPNG_ADD
, CAPNG_PERMITTED
, CAP_SETGID
);
1453 capng_update(CAPNG_ADD
, CAPNG_PERMITTED
, CAP_DAC_OVERRIDE
);
1455 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_SETUID
);
1456 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_SETGID
);
1457 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
1459 capng_apply(CAPNG_SELECT_BOTH
);
1462 #endif /* HAVE_CAP_NG_H */
1464 if (getuid() == 0 || geteuid() == 0) {
1465 if (username
|| chroot_dir
)
1466 droproot(username
, chroot_dir
);
1471 if (pcap_setfilter(pd
, &fcode
) < 0)
1472 error("%s", pcap_geterr(pd
));
1475 /* Do not exceed the default PATH_MAX for files. */
1476 dumpinfo
.CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
1478 if (dumpinfo
.CurrentFileName
== NULL
)
1479 error("malloc of dumpinfo.CurrentFileName");
1481 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1483 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, WflagChars
);
1485 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, 0);
1487 p
= pcap_dump_open(pd
, dumpinfo
.CurrentFileName
);
1488 #ifdef HAVE_CAP_NG_H
1489 /* Give up capabilities, clear Effective set */
1490 capng_clear(CAPNG_EFFECTIVE
);
1493 error("%s", pcap_geterr(pd
));
1494 if (Cflag
!= 0 || Gflag
!= 0) {
1495 callback
= dump_packet_and_trunc
;
1496 dumpinfo
.WFileName
= WFileName
;
1499 pcap_userdata
= (u_char
*)&dumpinfo
;
1501 callback
= dump_packet
;
1502 pcap_userdata
= (u_char
*)p
;
1504 #ifdef HAVE_PCAP_DUMP_FLUSH
1509 type
= pcap_datalink(pd
);
1510 printinfo
= get_print_info(type
);
1511 callback
= print_packet
;
1512 pcap_userdata
= (u_char
*)&printinfo
;
1515 #ifdef SIGNAL_REQ_INFO
1517 * We can't get statistics when reading from a file rather
1518 * than capturing from a device.
1520 if (RFileName
== NULL
)
1521 (void)setsignal(SIGNAL_REQ_INFO
, requestinfo
);
1524 if (vflag
> 0 && WFileName
) {
1526 * When capturing to a file, "-v" means tcpdump should,
1527 * every 10 secodns, "v"erbosely report the number of
1530 #ifdef USE_WIN32_MM_TIMER
1531 /* call verbose_stats_dump() each 1000 +/-100msec */
1532 timer_id
= timeSetEvent(1000, 100, verbose_stats_dump
, 0, TIME_PERIODIC
);
1533 setvbuf(stderr
, NULL
, _IONBF
, 0);
1534 #elif defined(HAVE_ALARM)
1535 (void)setsignal(SIGALRM
, verbose_stats_dump
);
1541 if (RFileName
== NULL
) {
1543 * Live capture (if -V was specified, we set RFileName
1544 * to a file from the -V file). Print a message to
1545 * the standard error on UN*X.
1547 if (!vflag
&& !WFileName
) {
1548 (void)fprintf(stderr
,
1549 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1552 (void)fprintf(stderr
, "%s: ", program_name
);
1553 dlt
= pcap_datalink(pd
);
1554 dlt_name
= pcap_datalink_val_to_name(dlt
);
1555 if (dlt_name
== NULL
) {
1556 (void)fprintf(stderr
, "listening on %s, link-type %u, capture size %u bytes\n",
1557 device
, dlt
, snaplen
);
1559 (void)fprintf(stderr
, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1561 pcap_datalink_val_to_description(dlt
), snaplen
);
1563 (void)fflush(stderr
);
1567 status
= pcap_loop(pd
, cnt
, callback
, pcap_userdata
);
1568 if (WFileName
== NULL
) {
1570 * We're printing packets. Flush the printed output,
1571 * so it doesn't get intermingled with error output.
1575 * We got interrupted, so perhaps we didn't
1576 * manage to finish a line we were printing.
1577 * Print an extra newline, just in case.
1581 (void)fflush(stdout
);
1585 * We got interrupted. If we are reading multiple
1586 * files (via -V) set these so that we stop.
1595 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
1596 program_name
, pcap_geterr(pd
));
1598 if (RFileName
== NULL
) {
1600 * We're doing a live capture. Report the capture
1606 if (VFileName
!= NULL
) {
1607 ret
= get_next_file(VFile
, VFileLine
);
1609 RFileName
= VFileLine
;
1610 pd
= pcap_open_offline(RFileName
, ebuf
);
1613 new_dlt
= pcap_datalink(pd
);
1614 if (WFileName
&& new_dlt
!= dlt
)
1615 error("%s: new dlt does not match original", RFileName
);
1616 printinfo
= get_print_info(new_dlt
);
1617 dlt_name
= pcap_datalink_val_to_name(new_dlt
);
1618 if (dlt_name
== NULL
) {
1619 fprintf(stderr
, "reading from file %s, link-type %u\n",
1620 RFileName
, new_dlt
);
1623 "reading from file %s, link-type %s (%s)\n",
1624 RFileName
, dlt_name
,
1625 pcap_datalink_val_to_description(new_dlt
));
1627 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
1628 error("%s", pcap_geterr(pd
));
1629 if (pcap_setfilter(pd
, &fcode
) < 0)
1630 error("%s", pcap_geterr(pd
));
1634 while (ret
!= NULL
);
1637 exit(status
== -1 ? 1 : 0);
1640 /* make a clean exit on interrupts */
1642 cleanup(int signo _U_
)
1644 #ifdef USE_WIN32_MM_TIMER
1646 timeKillEvent(timer_id
);
1648 #elif defined(HAVE_ALARM)
1652 #ifdef HAVE_PCAP_BREAKLOOP
1654 * We have "pcap_breakloop()"; use it, so that we do as little
1655 * as possible in the signal handler (it's probably not safe
1656 * to do anything with standard I/O streams in a signal handler -
1657 * the ANSI C standard doesn't say it is).
1662 * We don't have "pcap_breakloop()"; this isn't safe, but
1663 * it's the best we can do. Print the summary if we're
1664 * not reading from a savefile - i.e., if we're doing a
1665 * live capture - and exit.
1667 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
1669 * We got interrupted, so perhaps we didn't
1670 * manage to finish a line we were printing.
1671 * Print an extra newline, just in case.
1674 (void)fflush(stdout
);
1682 On windows, we do not use a fork, so we do not care less about
1683 waiting a child processes to die
1685 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1687 child_cleanup(int signo _U_
)
1691 #endif /* HAVE_FORK && HAVE_VFORK */
1694 info(register int verbose
)
1696 struct pcap_stat stat
;
1699 * Older versions of libpcap didn't set ps_ifdrop on some
1700 * platforms; initialize it to 0 to handle that.
1703 if (pcap_stats(pd
, &stat
) < 0) {
1704 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
1710 fprintf(stderr
, "%s: ", program_name
);
1712 (void)fprintf(stderr
, "%u packet%s captured", packets_captured
,
1713 PLURAL_SUFFIX(packets_captured
));
1715 fputs(", ", stderr
);
1718 (void)fprintf(stderr
, "%u packet%s received by filter", stat
.ps_recv
,
1719 PLURAL_SUFFIX(stat
.ps_recv
));
1721 fputs(", ", stderr
);
1724 (void)fprintf(stderr
, "%u packet%s dropped by kernel", stat
.ps_drop
,
1725 PLURAL_SUFFIX(stat
.ps_drop
));
1726 if (stat
.ps_ifdrop
!= 0) {
1728 fputs(", ", stderr
);
1731 (void)fprintf(stderr
, "%u packet%s dropped by interface\n",
1732 stat
.ps_ifdrop
, PLURAL_SUFFIX(stat
.ps_ifdrop
));
1738 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1740 compress_savefile(const char *filename
)
1749 * Set to lowest priority so that this doesn't disturb the capture
1752 setpriority(PRIO_PROCESS
, 0, NZERO
- 1);
1754 setpriority(PRIO_PROCESS
, 0, 19);
1756 if (execlp(zflag
, zflag
, filename
, (char *)NULL
) == -1)
1758 "compress_savefile:execlp(%s, %s): %s\n",
1768 #else /* HAVE_FORK && HAVE_VFORK */
1770 compress_savefile(const char *filename
)
1773 "compress_savefile failed. Functionality not implemented under your system\n");
1775 #endif /* HAVE_FORK && HAVE_VFORK */
1778 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1780 struct dump_info
*dump_info
;
1786 dump_info
= (struct dump_info
*)user
;
1789 * XXX - this won't force the file to rotate on the specified time
1790 * boundary, but it will rotate on the first packet received after the
1791 * specified Gflag number of seconds. Note: if a Gflag time boundary
1792 * and a Cflag size boundary coincide, the time rotation will occur
1793 * first thereby cancelling the Cflag boundary (since the file should
1797 /* Check if it is time to rotate */
1800 /* Get the current time */
1801 if ((t
= time(NULL
)) == (time_t)-1) {
1802 error("dump_and_trunc_packet: can't get current_time: %s",
1803 pcap_strerror(errno
));
1807 /* If the time is greater than the specified window, rotate */
1808 if (t
- Gflag_time
>= Gflag
) {
1809 /* Update the Gflag_time */
1811 /* Update Gflag_count */
1814 * Close the current file and open a new one.
1816 pcap_dump_close(dump_info
->p
);
1819 * Compress the file we just closed, if the user asked for it
1822 compress_savefile(dump_info
->CurrentFileName
);
1825 * Check to see if we've exceeded the Wflag (when
1828 if (Cflag
== 0 && Wflag
> 0 && Gflag_count
>= Wflag
) {
1829 (void)fprintf(stderr
, "Maximum file limit reached: %d\n",
1834 if (dump_info
->CurrentFileName
!= NULL
)
1835 free(dump_info
->CurrentFileName
);
1836 /* Allocate space for max filename + \0. */
1837 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
1838 if (dump_info
->CurrentFileName
== NULL
)
1839 error("dump_packet_and_trunc: malloc");
1841 * This is always the first file in the Cflag
1843 * We also don't need numbering if Cflag is not set.
1846 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0,
1849 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0, 0);
1851 #ifdef HAVE_CAP_NG_H
1852 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
1853 capng_apply(CAPNG_EFFECTIVE
);
1854 #endif /* HAVE_CAP_NG_H */
1855 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
1856 #ifdef HAVE_CAP_NG_H
1857 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
1858 capng_apply(CAPNG_EFFECTIVE
);
1859 #endif /* HAVE_CAP_NG_H */
1860 if (dump_info
->p
== NULL
)
1861 error("%s", pcap_geterr(pd
));
1866 * XXX - this won't prevent capture files from getting
1867 * larger than Cflag - the last packet written to the
1868 * file could put it over Cflag.
1870 if (Cflag
!= 0 && pcap_dump_ftell(dump_info
->p
) > Cflag
) {
1872 * Close the current file and open a new one.
1874 pcap_dump_close(dump_info
->p
);
1877 * Compress the file we just closed, if the user asked for it
1880 compress_savefile(dump_info
->CurrentFileName
);
1884 if (Cflag_count
>= Wflag
)
1887 if (dump_info
->CurrentFileName
!= NULL
)
1888 free(dump_info
->CurrentFileName
);
1889 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
1890 if (dump_info
->CurrentFileName
== NULL
)
1891 error("dump_packet_and_trunc: malloc");
1892 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, Cflag_count
, WflagChars
);
1893 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
1894 if (dump_info
->p
== NULL
)
1895 error("%s", pcap_geterr(pd
));
1898 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
1899 #ifdef HAVE_PCAP_DUMP_FLUSH
1901 pcap_dump_flush(dump_info
->p
);
1910 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1916 pcap_dump(user
, h
, sp
);
1917 #ifdef HAVE_PCAP_DUMP_FLUSH
1919 pcap_dump_flush((pcap_dumper_t
*)user
);
1928 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1930 struct print_info
*print_info
;
1938 print_info
= (struct print_info
*)user
;
1941 * Some printers want to check that they're not walking off the
1942 * end of the packet.
1943 * Rather than pass it all the way down, we set this global.
1945 snapend
= sp
+ h
->caplen
;
1947 if(print_info
->ndo_type
) {
1948 hdrlen
= (*print_info
->p
.ndo_printer
)(print_info
->ndo
, h
, sp
);
1950 hdrlen
= (*print_info
->p
.printer
)(h
, sp
);
1955 * Print the raw packet data in hex and ASCII.
1959 * Include the link-layer header.
1961 hex_and_ascii_print("\n\t", sp
, h
->caplen
);
1964 * Don't include the link-layer header - and if
1965 * we have nothing past the link-layer header,
1968 if (h
->caplen
> hdrlen
)
1969 hex_and_ascii_print("\n\t", sp
+ hdrlen
,
1970 h
->caplen
- hdrlen
);
1974 * Print the raw packet data in hex.
1978 * Include the link-layer header.
1980 hex_print("\n\t", sp
, h
->caplen
);
1983 * Don't include the link-layer header - and if
1984 * we have nothing past the link-layer header,
1987 if (h
->caplen
> hdrlen
)
1988 hex_print("\n\t", sp
+ hdrlen
,
1989 h
->caplen
- hdrlen
);
1993 * Print the raw packet data in ASCII.
1997 * Include the link-layer header.
1999 ascii_print(sp
, h
->caplen
);
2002 * Don't include the link-layer header - and if
2003 * we have nothing past the link-layer header,
2006 if (h
->caplen
> hdrlen
)
2007 ascii_print(sp
+ hdrlen
, h
->caplen
- hdrlen
);
2020 * XXX - there should really be libpcap calls to get the version
2021 * number as a string (the string would be generated from #defines
2022 * at run time, so that it's not generated from string constants
2023 * in the library, as, on many UNIX systems, those constants would
2024 * be statically linked into the application executable image, and
2025 * would thus reflect the version of libpcap on the system on
2026 * which the application was *linked*, not the system on which it's
2029 * That routine should be documented, unlike the "version[]"
2030 * string, so that UNIX vendors providing their own libpcaps
2031 * don't omit it (as a couple of vendors have...).
2033 * Packet.dll should perhaps also export a routine to return the
2034 * version number of the Packet.dll code, to supply the
2035 * "Wpcap_version" information on Windows.
2037 char WDversion
[]="current-cvs.tcpdump.org";
2038 #if !defined(HAVE_GENERATED_VERSION)
2039 char version
[]="current-cvs.tcpdump.org";
2041 char pcap_version
[]="current-cvs.tcpdump.org";
2042 char Wpcap_version
[]="3.1";
2046 * By default, print the specified data out in hex and ASCII.
2049 ndo_default_print(netdissect_options
*ndo _U_
, const u_char
*bp
, u_int length
)
2051 hex_and_ascii_print("\n\t", bp
, length
); /* pass on lf and identation string */
2055 default_print(const u_char
*bp
, u_int length
)
2057 ndo_default_print(gndo
, bp
, length
);
2060 #ifdef SIGNAL_REQ_INFO
2061 RETSIGTYPE
requestinfo(int signo _U_
)
2071 * Called once each second in verbose mode while dumping to file
2073 #ifdef USE_WIN32_MM_TIMER
2074 void CALLBACK
verbose_stats_dump (UINT timer_id _U_
, UINT msg _U_
, DWORD_PTR arg _U_
,
2075 DWORD_PTR dw1 _U_
, DWORD_PTR dw2 _U_
)
2077 struct pcap_stat stat
;
2079 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
2080 fprintf(stderr
, "Got %u\r", packets_captured
);
2082 #elif defined(HAVE_ALARM)
2083 static void verbose_stats_dump(int sig _U_
)
2085 struct pcap_stat stat
;
2087 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
2088 fprintf(stderr
, "Got %u\r", packets_captured
);
2096 extern char version
[];
2097 #ifndef HAVE_PCAP_LIB_VERSION
2098 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
2099 extern char pcap_version
[];
2100 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2101 static char pcap_version
[] = "unknown";
2102 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2103 #endif /* HAVE_PCAP_LIB_VERSION */
2105 #ifdef HAVE_PCAP_LIB_VERSION
2107 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2109 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2111 (void)fprintf(stderr
, "%s\n",pcap_lib_version());
2112 #else /* HAVE_PCAP_LIB_VERSION */
2114 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2115 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
2117 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2118 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
2120 #endif /* HAVE_PCAP_LIB_VERSION */
2121 (void)fprintf(stderr
,
2122 "Usage: %s [-aAbd" D_FLAG
"efhH" I_FLAG J_FLAG
"KlLnNOpqRStu" U_FLAG
"vxX]" B_FLAG_USAGE
" [ -c count ]\n", program_name
);
2123 (void)fprintf(stderr
,
2124 "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n");
2125 (void)fprintf(stderr
,
2126 "\t\t[ -i interface ]" j_FLAG_USAGE
" [ -M secret ]\n");
2127 #ifdef HAVE_PCAP_SETDIRECTION
2128 (void)fprintf(stderr
,
2129 "\t\t[ -P in|out|inout ]\n");
2131 (void)fprintf(stderr
,
2132 "\t\t[ -r file ] [ -s snaplen ] [ -T type ] [ -V file ] [ -w file ]\n");
2133 (void)fprintf(stderr
,
2134 "\t\t[ -W filecount ] [ -y datalinktype ] [ -z command ]\n");
2135 (void)fprintf(stderr
,
2136 "\t\t[ -Z user ] [ expression ]\n");
2144 ndo_error(netdissect_options
*ndo _U_
, const char *fmt
, ...)
2148 (void)fprintf(stderr
, "%s: ", program_name
);
2150 (void)vfprintf(stderr
, fmt
, ap
);
2154 if (fmt
[-1] != '\n')
2155 (void)fputc('\n', stderr
);
2163 ndo_warning(netdissect_options
*ndo _U_
, const char *fmt
, ...)
2167 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
2169 (void)vfprintf(stderr
, fmt
, ap
);
2173 if (fmt
[-1] != '\n')
2174 (void)fputc('\n', stderr
);