2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 * Support for splitting captures into multiple files with a maximum
25 * Seth Webster <swebster@sst.ll.mit.edu>
29 static const char copyright
[] _U_
=
30 "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
31 The Regents of the University of California. All rights reserved.\n";
35 * tcpdump - monitor tcp/ip traffic on an ethernet.
37 * First written in 1987 by Van Jacobson, Lawrence Berkeley Laboratory.
38 * Mercilessly hacked and occasionally improved since then via the
39 * combined efforts of Van, Steve McCanne and Craig Leres of LBL.
46 #include <tcpdump-stdinc.h>
51 extern int strcasecmp (const char *__s1
, const char *__s2
);
69 #include <sys/resource.h>
75 /* capabilities convinience library */
78 #endif /* HAVE_CAP_NG_H */
80 #include "netdissect.h"
81 #include "interface.h"
82 #include "addrtoname.h"
84 #include "setsignal.h"
85 #include "gmt2local.h"
86 #include "pcap-missing.h"
93 #define SIGNAL_REQ_INFO SIGINFO
95 #define SIGNAL_REQ_INFO SIGUSR1
98 netdissect_options Gndo
;
99 netdissect_options
*gndo
= &Gndo
;
101 static int dflag
; /* print filter code */
102 static int Lflag
; /* list available data link types and exit */
103 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
104 static int Jflag
; /* list available time stamp types */
106 #ifdef HAVE_PCAP_SETDIRECTION
107 int Qflag
= -1; /* restrict captured packet by send/receive direction */
109 static char *zflag
= NULL
; /* compress each savefile using a specified command (like gzip or bzip2) */
111 static int infodelay
;
112 static int infoprint
;
116 int32_t thiszone
; /* seconds offset from gmt to local time */
119 static RETSIGTYPE
cleanup(int);
120 static RETSIGTYPE
child_cleanup(int);
121 static void usage(void) __attribute__((noreturn
));
122 static void show_dlts_and_exit(const char *device
, pcap_t
*pd
) __attribute__((noreturn
));
124 static void print_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
125 static void ndo_default_print(netdissect_options
*, const u_char
*, u_int
);
126 static void dump_packet_and_trunc(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
127 static void dump_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
128 static void droproot(const char *, const char *);
129 static void ndo_error(netdissect_options
*ndo
, const char *fmt
, ...)
130 __attribute__((noreturn
))
131 #ifdef __ATTRIBUTE___FORMAT_OK
132 __attribute__((format (printf
, 2, 3)))
133 #endif /* __ATTRIBUTE___FORMAT_OK */
135 static void ndo_warning(netdissect_options
*ndo
, const char *fmt
, ...)
136 #ifdef __ATTRIBUTE___FORMAT_OK
137 __attribute__((format (printf
, 2, 3)))
138 #endif /* __ATTRIBUTE___FORMAT_OK */
141 #ifdef SIGNAL_REQ_INFO
142 RETSIGTYPE
requestinfo(int);
145 #if defined(USE_WIN32_MM_TIMER)
146 #include <MMsystem.h>
147 static UINT timer_id
;
148 static void CALLBACK
verbose_stats_dump(UINT
, UINT
, DWORD_PTR
, DWORD_PTR
, DWORD_PTR
);
149 #elif defined(HAVE_ALARM)
150 static void verbose_stats_dump(int sig
);
153 static void info(int);
154 static u_int packets_captured
;
168 static const struct printer printers
[] = {
169 { arcnet_if_print
, DLT_ARCNET
},
170 #ifdef DLT_ARCNET_LINUX
171 { arcnet_linux_if_print
, DLT_ARCNET_LINUX
},
173 { token_if_print
, DLT_IEEE802
},
175 { lane_if_print
, DLT_LANE8023
},
178 { cip_if_print
, DLT_CIP
},
181 { cip_if_print
, DLT_ATM_CLIP
},
183 { sl_if_print
, DLT_SLIP
},
184 #ifdef DLT_SLIP_BSDOS
185 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
187 { ppp_if_print
, DLT_PPP
},
188 #ifdef DLT_PPP_WITHDIRECTION
189 { ppp_if_print
, DLT_PPP_WITHDIRECTION
},
192 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
194 { fddi_if_print
, DLT_FDDI
},
195 { null_if_print
, DLT_NULL
},
197 { null_if_print
, DLT_LOOP
},
199 { raw_if_print
, DLT_RAW
},
200 { atm_if_print
, DLT_ATM_RFC1483
},
202 { chdlc_if_print
, DLT_C_HDLC
},
205 { chdlc_if_print
, DLT_HDLC
},
207 #ifdef DLT_PPP_SERIAL
208 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
211 { pppoe_if_print
, DLT_PPP_ETHER
},
214 { sll_if_print
, DLT_LINUX_SLL
},
216 #ifdef DLT_IEEE802_11
217 { ieee802_11_if_print
, DLT_IEEE802_11
},
220 { ltalk_if_print
, DLT_LTALK
},
222 #if defined(DLT_PFLOG) && defined(HAVE_NET_PFVAR_H)
223 { pflog_if_print
, DLT_PFLOG
},
226 { fr_if_print
, DLT_FR
},
229 { fr_if_print
, DLT_FRELAY
},
232 { sunatm_if_print
, DLT_SUNATM
},
234 #ifdef DLT_IP_OVER_FC
235 { ipfc_if_print
, DLT_IP_OVER_FC
},
237 #ifdef DLT_PRISM_HEADER
238 { prism_if_print
, DLT_PRISM_HEADER
},
240 #ifdef DLT_IEEE802_11_RADIO
241 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
244 { enc_if_print
, DLT_ENC
},
246 #ifdef DLT_SYMANTEC_FIREWALL
247 { symantec_if_print
, DLT_SYMANTEC_FIREWALL
},
249 #ifdef DLT_APPLE_IP_OVER_IEEE1394
250 { ap1394_if_print
, DLT_APPLE_IP_OVER_IEEE1394
},
252 #ifdef DLT_IEEE802_11_RADIO_AVS
253 { ieee802_11_radio_avs_if_print
, DLT_IEEE802_11_RADIO_AVS
},
255 #ifdef DLT_JUNIPER_ATM1
256 { juniper_atm1_print
, DLT_JUNIPER_ATM1
},
258 #ifdef DLT_JUNIPER_ATM2
259 { juniper_atm2_print
, DLT_JUNIPER_ATM2
},
261 #ifdef DLT_JUNIPER_MFR
262 { juniper_mfr_print
, DLT_JUNIPER_MFR
},
264 #ifdef DLT_JUNIPER_MLFR
265 { juniper_mlfr_print
, DLT_JUNIPER_MLFR
},
267 #ifdef DLT_JUNIPER_MLPPP
268 { juniper_mlppp_print
, DLT_JUNIPER_MLPPP
},
270 #ifdef DLT_JUNIPER_PPPOE
271 { juniper_pppoe_print
, DLT_JUNIPER_PPPOE
},
273 #ifdef DLT_JUNIPER_PPPOE_ATM
274 { juniper_pppoe_atm_print
, DLT_JUNIPER_PPPOE_ATM
},
276 #ifdef DLT_JUNIPER_GGSN
277 { juniper_ggsn_print
, DLT_JUNIPER_GGSN
},
279 #ifdef DLT_JUNIPER_ES
280 { juniper_es_print
, DLT_JUNIPER_ES
},
282 #ifdef DLT_JUNIPER_MONITOR
283 { juniper_monitor_print
, DLT_JUNIPER_MONITOR
},
285 #ifdef DLT_JUNIPER_SERVICES
286 { juniper_services_print
, DLT_JUNIPER_SERVICES
},
288 #ifdef DLT_JUNIPER_ETHER
289 { juniper_ether_print
, DLT_JUNIPER_ETHER
},
291 #ifdef DLT_JUNIPER_PPP
292 { juniper_ppp_print
, DLT_JUNIPER_PPP
},
294 #ifdef DLT_JUNIPER_FRELAY
295 { juniper_frelay_print
, DLT_JUNIPER_FRELAY
},
297 #ifdef DLT_JUNIPER_CHDLC
298 { juniper_chdlc_print
, DLT_JUNIPER_CHDLC
},
301 { mfr_if_print
, DLT_MFR
},
303 #if defined(DLT_BLUETOOTH_HCI_H4_WITH_PHDR) && defined(HAVE_PCAP_BLUETOOTH_H)
304 { bt_if_print
, DLT_BLUETOOTH_HCI_H4_WITH_PHDR
},
306 #ifdef HAVE_PCAP_USB_H
308 { usb_linux_48_byte_print
, DLT_USB_LINUX
},
309 #endif /* DLT_USB_LINUX */
310 #ifdef DLT_USB_LINUX_MMAPPED
311 { usb_linux_64_byte_print
, DLT_USB_LINUX_MMAPPED
},
312 #endif /* DLT_USB_LINUX_MMAPPED */
313 #endif /* HAVE_PCAP_USB_H */
315 { raw_if_print
, DLT_IPV4
},
318 { raw_if_print
, DLT_IPV6
},
323 static const struct ndo_printer ndo_printers
[] = {
324 { ether_if_print
, DLT_EN10MB
},
326 { ipnet_if_print
, DLT_IPNET
},
328 #ifdef DLT_IEEE802_15_4
329 { ieee802_15_4_if_print
, DLT_IEEE802_15_4
},
331 #ifdef DLT_IEEE802_15_4_NOFCS
332 { ieee802_15_4_if_print
, DLT_IEEE802_15_4_NOFCS
},
335 { ppi_if_print
, DLT_PPI
},
337 #ifdef DLT_NETANALYZER
338 { netanalyzer_if_print
, DLT_NETANALYZER
},
340 #ifdef DLT_NETANALYZER_TRANSPARENT
341 { netanalyzer_transparent_if_print
, DLT_NETANALYZER_TRANSPARENT
},
344 { nflog_if_print
, DLT_NFLOG
},
350 lookup_printer(int type
)
352 const struct printer
*p
;
354 for (p
= printers
; p
->f
; ++p
)
363 lookup_ndo_printer(int type
)
365 const struct ndo_printer
*p
;
367 for (p
= ndo_printers
; p
->f
; ++p
)
377 static int supports_monitor_mode
;
384 netdissect_options
*ndo
;
387 if_ndo_printer ndo_printer
;
394 char *CurrentFileName
;
399 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
401 show_tstamp_types_and_exit(const char *device
, pcap_t
*pd
)
404 int *tstamp_types
= 0;
405 const char *tstamp_type_name
;
408 n_tstamp_types
= pcap_list_tstamp_types(pd
, &tstamp_types
);
409 if (n_tstamp_types
< 0)
410 error("%s", pcap_geterr(pd
));
412 if (n_tstamp_types
== 0) {
413 fprintf(stderr
, "Time stamp type cannot be set for %s\n",
417 fprintf(stderr
, "Time stamp types for %s (use option -j to set):\n",
419 for (i
= 0; i
< n_tstamp_types
; i
++) {
420 tstamp_type_name
= pcap_tstamp_type_val_to_name(tstamp_types
[i
]);
421 if (tstamp_type_name
!= NULL
) {
422 (void) fprintf(stderr
, " %s (%s)\n", tstamp_type_name
,
423 pcap_tstamp_type_val_to_description(tstamp_types
[i
]));
425 (void) fprintf(stderr
, " %d\n", tstamp_types
[i
]);
428 pcap_free_tstamp_types(tstamp_types
);
434 show_dlts_and_exit(const char *device
, pcap_t
*pd
)
438 const char *dlt_name
;
440 n_dlts
= pcap_list_datalinks(pd
, &dlts
);
442 error("%s", pcap_geterr(pd
));
443 else if (n_dlts
== 0 || !dlts
)
444 error("No data link types.");
447 * If the interface is known to support monitor mode, indicate
448 * whether these are the data link types available when not in
449 * monitor mode, if -I wasn't specified, or when in monitor mode,
450 * when -I was specified (the link-layer types available in
451 * monitor mode might be different from the ones available when
452 * not in monitor mode).
454 if (supports_monitor_mode
)
455 (void) fprintf(stderr
, "Data link types for %s %s (use option -y to set):\n",
457 Iflag
? "when in monitor mode" : "when not in monitor mode");
459 (void) fprintf(stderr
, "Data link types for %s (use option -y to set):\n",
462 while (--n_dlts
>= 0) {
463 dlt_name
= pcap_datalink_val_to_name(dlts
[n_dlts
]);
464 if (dlt_name
!= NULL
) {
465 (void) fprintf(stderr
, " %s (%s)", dlt_name
,
466 pcap_datalink_val_to_description(dlts
[n_dlts
]));
469 * OK, does tcpdump handle that type?
471 if (lookup_printer(dlts
[n_dlts
]) == NULL
472 && lookup_ndo_printer(dlts
[n_dlts
]) == NULL
)
473 (void) fprintf(stderr
, " (printing not supported)");
474 fprintf(stderr
, "\n");
476 (void) fprintf(stderr
, " DLT %d (printing not supported)\n",
480 #ifdef HAVE_PCAP_FREE_DATALINKS
481 pcap_free_datalinks(dlts
);
487 * Set up flags that might or might not be supported depending on the
488 * version of libpcap we're using.
490 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
492 #define B_FLAG_USAGE " [ -B size ]"
493 #else /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
496 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
498 #ifdef HAVE_PCAP_CREATE
500 #else /* HAVE_PCAP_CREATE */
502 #endif /* HAVE_PCAP_CREATE */
504 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
506 #define j_FLAG_USAGE " [ -j tstamptype ]"
508 #else /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
512 #endif /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
514 #ifdef HAVE_PCAP_FINDALLDEVS
515 #ifndef HAVE_PCAP_IF_T
516 #undef HAVE_PCAP_FINDALLDEVS
520 #ifdef HAVE_PCAP_FINDALLDEVS
526 #ifdef HAVE_PCAP_DUMP_FLUSH
532 #ifdef HAVE_PCAP_SETDIRECTION
539 /* Drop root privileges and chroot if necessary */
541 droproot(const char *username
, const char *chroot_dir
)
543 struct passwd
*pw
= NULL
;
545 if (chroot_dir
&& !username
) {
546 fprintf(stderr
, "tcpdump: Chroot without dropping root is insecure\n");
550 pw
= getpwnam(username
);
553 if (chroot(chroot_dir
) != 0 || chdir ("/") != 0) {
554 fprintf(stderr
, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
555 chroot_dir
, pcap_strerror(errno
));
560 int ret
= capng_change_id(pw
->pw_uid
, pw
->pw_gid
, CAPNG_NO_FLAG
);
562 printf("error : ret %d\n", ret
);
564 /* We don't need CAP_SETUID and CAP_SETGID */
565 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_SETUID
);
566 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_SETUID
);
567 capng_update(CAPNG_DROP
, CAPNG_PERMITTED
, CAP_SETUID
);
568 capng_update(CAPNG_DROP
, CAPNG_PERMITTED
, CAP_SETUID
);
569 capng_apply(CAPNG_SELECT_BOTH
);
572 if (initgroups(pw
->pw_name
, pw
->pw_gid
) != 0 ||
573 setgid(pw
->pw_gid
) != 0 || setuid(pw
->pw_uid
) != 0) {
574 fprintf(stderr
, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
576 (unsigned long)pw
->pw_uid
,
577 (unsigned long)pw
->pw_gid
,
578 pcap_strerror(errno
));
581 #endif /* HAVE_CAP_NG_H */
584 fprintf(stderr
, "tcpdump: Couldn't find user '%.32s'\n",
607 MakeFilename(char *buffer
, char *orig_name
, int cnt
, int max_chars
)
609 char *filename
= malloc(PATH_MAX
+ 1);
610 if (filename
== NULL
)
611 error("Makefilename: malloc");
613 /* Process with strftime if Gflag is set. */
617 /* Convert Gflag_time to a usable format */
618 if ((local_tm
= localtime(&Gflag_time
)) == NULL
) {
619 error("MakeTimedFilename: localtime");
622 /* There's no good way to detect an error in strftime since a return
623 * value of 0 isn't necessarily failure.
625 strftime(filename
, PATH_MAX
, orig_name
, local_tm
);
627 strncpy(filename
, orig_name
, PATH_MAX
);
630 if (cnt
== 0 && max_chars
== 0)
631 strncpy(buffer
, filename
, PATH_MAX
+ 1);
633 if (snprintf(buffer
, PATH_MAX
+ 1, "%s%0*d", filename
, max_chars
, cnt
) > PATH_MAX
)
634 /* Report an error if the filename is too large */
635 error("too many output files or filename is too long (> %d)", PATH_MAX
);
639 static int tcpdump_printf(netdissect_options
*ndo _U_
,
640 const char *fmt
, ...)
647 ret
=vfprintf(stdout
, fmt
, args
);
653 static struct print_info
654 get_print_info(int type
)
656 struct print_info printinfo
;
658 printinfo
.ndo_type
= 1;
659 printinfo
.ndo
= gndo
;
660 printinfo
.p
.ndo_printer
= lookup_ndo_printer(type
);
661 if (printinfo
.p
.ndo_printer
== NULL
) {
662 printinfo
.p
.printer
= lookup_printer(type
);
663 printinfo
.ndo_type
= 0;
664 if (printinfo
.p
.printer
== NULL
) {
665 gndo
->ndo_dltname
= pcap_datalink_val_to_name(type
);
666 if (gndo
->ndo_dltname
!= NULL
)
667 error("packet printing is not supported for link type %s: use -w",
670 error("packet printing is not supported for link type %d: use -w", type
);
677 get_next_file(FILE *VFile
, char *ptr
)
681 ret
= fgets(ptr
, PATH_MAX
, VFile
);
685 if (ptr
[strlen(ptr
) - 1] == '\n')
686 ptr
[strlen(ptr
) - 1] = '\0';
692 main(int argc
, char **argv
)
694 register int cnt
, op
, i
;
695 bpf_u_int32 localnet
, netmask
;
696 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *VFileName
, *WFileName
;
697 pcap_handler callback
;
701 const char *dlt_name
;
702 struct bpf_program fcode
;
704 RETSIGTYPE (*oldhandler
)(int);
706 struct print_info printinfo
;
707 struct dump_info dumpinfo
;
708 u_char
*pcap_userdata
;
709 char ebuf
[PCAP_ERRBUF_SIZE
];
710 char VFileLine
[PATH_MAX
+ 1];
711 char *username
= NULL
;
712 char *chroot_dir
= NULL
;
715 #ifdef HAVE_PCAP_FINDALLDEVS
716 pcap_if_t
*devpointer
;
722 if(wsockinit() != 0) return 1;
725 jflag
=-1; /* not set */
729 gndo
->ndo_default_print
=ndo_default_print
;
730 gndo
->ndo_printf
=tcpdump_printf
;
731 gndo
->ndo_error
=ndo_error
;
732 gndo
->ndo_warning
=ndo_warning
;
733 gndo
->ndo_snaplen
= DEFAULT_SNAPLEN
;
743 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
744 program_name
= cp
+ 1;
746 program_name
= argv
[0];
748 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
756 (op
= getopt(argc
, argv
, "aAb" B_FLAG
"c:C:d" D_FLAG
"eE:fF:G:hHi:" I_FLAG j_FLAG J_FLAG
"KlLm:M:nNOpq" Q_FLAG
"r:Rs:StT:u" U_FLAG
"vV:w:W:xXy:Yz:Z:")) != -1)
760 /* compatibility for old -a */
771 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
773 Bflag
= atoi(optarg
)*1024;
775 error("invalid packet buffer size %s", optarg
);
777 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
782 error("invalid packet count %s", optarg
);
786 Cflag
= atoi(optarg
) * 1000000;
788 error("invalid file size %s", optarg
);
795 #ifdef HAVE_PCAP_FINDALLDEVS
797 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
800 for (i
= 0; devpointer
!= 0; i
++) {
801 printf("%d.%s", i
+1, devpointer
->name
);
802 if (devpointer
->description
!= NULL
)
803 printf(" (%s)", devpointer
->description
);
805 devpointer
= devpointer
->next
;
809 #endif /* HAVE_PCAP_FINDALLDEVS */
820 #ifndef HAVE_LIBCRYPTO
821 warning("crypto code not compiled in");
823 gndo
->ndo_espsecret
= optarg
;
835 Gflag
= atoi(optarg
);
837 error("invalid number of seconds %s", optarg
);
839 /* We will create one file initially. */
842 /* Grab the current time for rotation use. */
843 if ((Gflag_time
= time(NULL
)) == (time_t)-1) {
844 error("main: can't get current time: %s",
845 pcap_strerror(errno
));
858 if (optarg
[0] == '0' && optarg
[1] == 0)
859 error("Invalid adapter index");
861 #ifdef HAVE_PCAP_FINDALLDEVS
863 * If the argument is a number, treat it as
864 * an index into the list of adapters, as
865 * printed by "tcpdump -D".
867 * This should be OK on UNIX systems, as interfaces
868 * shouldn't have names that begin with digits.
869 * It can be useful on Windows, where more than
870 * one interface can have the same name.
872 devnum
= strtol(optarg
, &end
, 10);
873 if (optarg
!= end
&& *end
== '\0') {
875 error("Invalid adapter index");
877 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
881 * Look for the devnum-th entry
882 * in the list of devices
886 i
< devnum
-1 && devpointer
!= NULL
;
887 i
++, devpointer
= devpointer
->next
)
889 if (devpointer
== NULL
)
890 error("Invalid adapter index");
892 device
= devpointer
->name
;
895 #endif /* HAVE_PCAP_FINDALLDEVS */
899 #ifdef HAVE_PCAP_CREATE
903 #endif /* HAVE_PCAP_CREATE */
905 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
907 jflag
= pcap_tstamp_type_name_to_val(optarg
);
909 error("invalid time stamp type %s", optarg
);
920 * _IOLBF is the same as _IOFBF in Microsoft's C
921 * libraries; the only alternative they offer
924 * XXX - this should really be checking for MSVC++,
925 * not WIN32, if, for example, MinGW has its own
926 * C library that is more UNIX-compatible.
928 setvbuf(stdout
, NULL
, _IONBF
, 0);
930 #ifdef HAVE_SETLINEBUF
933 setvbuf(stdout
, NULL
, _IOLBF
, 0);
944 if (smiLoadModule(optarg
) == 0) {
945 error("could not load MIB module %s", optarg
);
949 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
950 program_name
, optarg
);
951 (void)fprintf(stderr
, "(no libsmi support)\n");
956 /* TCP-MD5 shared secret */
957 #ifndef HAVE_LIBCRYPTO
958 warning("crypto code not compiled in");
981 ++suppress_default_print
;
984 #ifdef HAVE_PCAP_SETDIRECTION
986 if (strcasecmp(optarg
, "in") == 0)
988 else if (strcasecmp(optarg
, "out") == 0)
990 else if (strcasecmp(optarg
, "inout") == 0)
991 Qflag
= PCAP_D_INOUT
;
993 error("unknown capture direction `%s'", optarg
);
995 #endif /* HAVE_PCAP_SETDIRECTION */
1006 snaplen
= strtol(optarg
, &end
, 0);
1007 if (optarg
== end
|| *end
!= '\0'
1008 || snaplen
< 0 || snaplen
> MAXIMUM_SNAPLEN
)
1009 error("invalid snaplen %s", optarg
);
1010 else if (snaplen
== 0)
1011 snaplen
= MAXIMUM_SNAPLEN
;
1023 if (strcasecmp(optarg
, "vat") == 0)
1024 packettype
= PT_VAT
;
1025 else if (strcasecmp(optarg
, "wb") == 0)
1027 else if (strcasecmp(optarg
, "rpc") == 0)
1028 packettype
= PT_RPC
;
1029 else if (strcasecmp(optarg
, "rtp") == 0)
1030 packettype
= PT_RTP
;
1031 else if (strcasecmp(optarg
, "rtcp") == 0)
1032 packettype
= PT_RTCP
;
1033 else if (strcasecmp(optarg
, "snmp") == 0)
1034 packettype
= PT_SNMP
;
1035 else if (strcasecmp(optarg
, "cnfp") == 0)
1036 packettype
= PT_CNFP
;
1037 else if (strcasecmp(optarg
, "tftp") == 0)
1038 packettype
= PT_TFTP
;
1039 else if (strcasecmp(optarg
, "aodv") == 0)
1040 packettype
= PT_AODV
;
1041 else if (strcasecmp(optarg
, "carp") == 0)
1042 packettype
= PT_CARP
;
1043 else if (strcasecmp(optarg
, "radius") == 0)
1044 packettype
= PT_RADIUS
;
1045 else if (strcasecmp(optarg
, "zmtp1") == 0)
1046 packettype
= PT_ZMTP1
;
1047 else if (strcasecmp(optarg
, "vxlan") == 0)
1048 packettype
= PT_VXLAN
;
1049 else if (strcasecmp(optarg
, "pgm") == 0)
1050 packettype
= PT_PGM
;
1051 else if (strcasecmp(optarg
, "pgm_zmtp1") == 0)
1052 packettype
= PT_PGM_ZMTP1
;
1053 else if (strcasecmp(optarg
, "lmp") == 0)
1054 packettype
= PT_LMP
;
1056 error("unknown packet type `%s'", optarg
);
1063 #ifdef HAVE_PCAP_DUMP_FLUSH
1082 Wflag
= atoi(optarg
);
1084 error("invalid number of output files %s", optarg
);
1085 WflagChars
= getWflagChars(Wflag
);
1090 ++suppress_default_print
;
1095 ++suppress_default_print
;
1099 gndo
->ndo_dltname
= optarg
;
1101 pcap_datalink_name_to_val(gndo
->ndo_dltname
);
1102 if (gndo
->ndo_dlt
< 0)
1103 error("invalid data link type %s", gndo
->ndo_dltname
);
1106 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
1109 /* Undocumented flag */
1110 #ifdef HAVE_PCAP_DEBUG
1111 extern int pcap_debug
;
1122 zflag
= strdup(optarg
);
1131 username
= strdup(optarg
);
1146 case 0: /* Default */
1147 case 4: /* Default + Date*/
1148 thiszone
= gmt2local(0);
1151 case 1: /* No time stamp */
1152 case 2: /* Unix timeval style */
1153 case 3: /* Microseconds since previous packet */
1154 case 5: /* Microseconds since first packet */
1157 default: /* Not supported */
1158 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
1162 if (fflag
!= 0 && (VFileName
!= NULL
|| RFileName
!= NULL
))
1163 error("-f can not be used with -V or -r");
1165 if (VFileName
!= NULL
&& RFileName
!= NULL
)
1166 error("-V and -r are mutually exclusive.");
1169 /* if run as root, prepare for chrooting */
1170 if (getuid() == 0 || geteuid() == 0) {
1171 /* future extensibility for cmd-line arguments */
1173 chroot_dir
= WITH_CHROOT
;
1178 /* if run as root, prepare for dropping root privileges */
1179 if (getuid() == 0 || geteuid() == 0) {
1180 /* Run with '-Z root' to restore old behaviour */
1182 username
= WITH_USER
;
1186 if (RFileName
!= NULL
|| VFileName
!= NULL
) {
1188 * If RFileName is non-null, it's the pathname of a
1189 * savefile to read. If VFileName is non-null, it's
1190 * the pathname of a file containing a list of pathnames
1191 * (one per line) of savefiles to read.
1193 * In either case, we're reading a savefile, not doing
1198 * We don't need network access, so relinquish any set-UID
1199 * or set-GID privileges we have (if any).
1201 * We do *not* want set-UID privileges when opening a
1202 * trace file, as that might let the user read other
1203 * people's trace files (especially if we're set-UID
1206 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
1207 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1209 if (VFileName
!= NULL
) {
1210 if (VFileName
[0] == '-' && VFileName
[1] == '\0')
1213 VFile
= fopen(VFileName
, "r");
1216 error("Unable to open file: %s\n", strerror(errno
));
1218 ret
= get_next_file(VFile
, VFileLine
);
1220 error("Nothing in %s\n", VFileName
);
1221 RFileName
= VFileLine
;
1224 pd
= pcap_open_offline(RFileName
, ebuf
);
1227 dlt
= pcap_datalink(pd
);
1228 dlt_name
= pcap_datalink_val_to_name(dlt
);
1229 if (dlt_name
== NULL
) {
1230 fprintf(stderr
, "reading from file %s, link-type %u\n",
1234 "reading from file %s, link-type %s (%s)\n",
1235 RFileName
, dlt_name
,
1236 pcap_datalink_val_to_description(dlt
));
1242 * We're doing a live capture.
1244 if (device
== NULL
) {
1245 device
= pcap_lookupdev(ebuf
);
1251 * Print a message to the standard error on Windows.
1252 * XXX - why do it here, with a different message?
1254 if(strlen(device
) == 1) /* we assume that an ASCII string is always longer than 1 char */
1255 { /* a Unicode string has a \0 as second byte (so strlen() is 1) */
1256 fprintf(stderr
, "%s: listening on %ws\n", program_name
, device
);
1260 fprintf(stderr
, "%s: listening on %s\n", program_name
, device
);
1265 #ifdef HAVE_PCAP_CREATE
1266 pd
= pcap_create(device
, ebuf
);
1269 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1271 show_tstamp_types_and_exit(device
, pd
);
1274 * Is this an interface that supports monitor mode?
1276 if (pcap_can_set_rfmon(pd
) == 1)
1277 supports_monitor_mode
= 1;
1279 supports_monitor_mode
= 0;
1280 status
= pcap_set_snaplen(pd
, snaplen
);
1282 error("%s: Can't set snapshot length: %s",
1283 device
, pcap_statustostr(status
));
1284 status
= pcap_set_promisc(pd
, !pflag
);
1286 error("%s: Can't set promiscuous mode: %s",
1287 device
, pcap_statustostr(status
));
1289 status
= pcap_set_rfmon(pd
, 1);
1291 error("%s: Can't set monitor mode: %s",
1292 device
, pcap_statustostr(status
));
1294 status
= pcap_set_timeout(pd
, 1000);
1296 error("%s: pcap_set_timeout failed: %s",
1297 device
, pcap_statustostr(status
));
1299 status
= pcap_set_buffer_size(pd
, Bflag
);
1301 error("%s: Can't set buffer size: %s",
1302 device
, pcap_statustostr(status
));
1304 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1306 status
= pcap_set_tstamp_type(pd
, jflag
);
1308 error("%s: Can't set time stamp type: %s",
1309 device
, pcap_statustostr(status
));
1312 status
= pcap_activate(pd
);
1315 * pcap_activate() failed.
1317 cp
= pcap_geterr(pd
);
1318 if (status
== PCAP_ERROR
)
1320 else if ((status
== PCAP_ERROR_NO_SUCH_DEVICE
||
1321 status
== PCAP_ERROR_PERM_DENIED
) &&
1323 error("%s: %s\n(%s)", device
,
1324 pcap_statustostr(status
), cp
);
1326 error("%s: %s", device
,
1327 pcap_statustostr(status
));
1328 } else if (status
> 0) {
1330 * pcap_activate() succeeded, but it's warning us
1331 * of a problem it had.
1333 cp
= pcap_geterr(pd
);
1334 if (status
== PCAP_WARNING
)
1336 else if (status
== PCAP_WARNING_PROMISC_NOTSUP
&&
1338 warning("%s: %s\n(%s)", device
,
1339 pcap_statustostr(status
), cp
);
1341 warning("%s: %s", device
,
1342 pcap_statustostr(status
));
1344 #ifdef HAVE_PCAP_SETDIRECTION
1346 status
= pcap_setdirection(pd
, Qflag
);
1348 error("%s: pcap_setdirection() failed: %s",
1349 device
, pcap_geterr(pd
));
1351 #endif /* HAVE_PCAP_SETDIRECTION */
1354 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
1358 warning("%s", ebuf
);
1359 #endif /* HAVE_PCAP_CREATE */
1361 * Let user own process after socket has been opened.
1364 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
1365 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1367 #if !defined(HAVE_PCAP_CREATE) && defined(WIN32)
1369 if(pcap_setbuff(pd
, Bflag
)==-1){
1370 error("%s", pcap_geterr(pd
));
1372 #endif /* !defined(HAVE_PCAP_CREATE) && defined(WIN32) */
1374 show_dlts_and_exit(device
, pd
);
1375 if (gndo
->ndo_dlt
>= 0) {
1376 #ifdef HAVE_PCAP_SET_DATALINK
1377 if (pcap_set_datalink(pd
, gndo
->ndo_dlt
) < 0)
1378 error("%s", pcap_geterr(pd
));
1381 * We don't actually support changing the
1382 * data link type, so we only let them
1383 * set it to what it already is.
1385 if (gndo
->ndo_dlt
!= pcap_datalink(pd
)) {
1386 error("%s is not one of the DLTs supported by this device\n",
1390 (void)fprintf(stderr
, "%s: data link type %s\n",
1391 program_name
, gndo
->ndo_dltname
);
1392 (void)fflush(stderr
);
1394 i
= pcap_snapshot(pd
);
1396 warning("snaplen raised from %d to %d", snaplen
, i
);
1399 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
1402 warning("%s", ebuf
);
1406 cmdbuf
= read_infile(infile
);
1408 cmdbuf
= copy_argv(&argv
[optind
]);
1410 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
1411 error("%s", pcap_geterr(pd
));
1413 bpf_dump(&fcode
, dflag
);
1418 init_addrtoname(localnet
, netmask
);
1422 (void)setsignal(SIGPIPE
, cleanup
);
1423 (void)setsignal(SIGTERM
, cleanup
);
1424 (void)setsignal(SIGINT
, cleanup
);
1426 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1427 (void)setsignal(SIGCHLD
, child_cleanup
);
1429 /* Cooperate with nohup(1) */
1431 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
1432 (void)setsignal(SIGHUP
, oldhandler
);
1437 * If a user name was specified with "-Z", attempt to switch to
1438 * that user's UID. This would probably be used with sudo,
1439 * to allow tcpdump to be run in a special restricted
1440 * account (if you just want to allow users to open capture
1441 * devices, and can't just give users that permission,
1442 * you'd make tcpdump set-UID or set-GID).
1444 * Tcpdump doesn't necessarily write only to one savefile;
1445 * the general only way to allow a -Z instance to write to
1446 * savefiles as the user under whose UID it's run, rather
1447 * than as the user specified with -Z, would thus be to switch
1448 * to the original user ID before opening a capture file and
1449 * then switch back to the -Z user ID after opening the savefile.
1450 * Switching to the -Z user ID only after opening the first
1451 * savefile doesn't handle the general case.
1454 #ifdef HAVE_CAP_NG_H
1455 /* We are running as root and we will be writing to savefile */
1456 if ((getuid() == 0 || geteuid() == 0) && WFileName
) {
1458 /* Drop all capabilities from effective set */
1459 capng_clear(CAPNG_EFFECTIVE
);
1460 /* Add capabilities we will need*/
1461 capng_update(CAPNG_ADD
, CAPNG_PERMITTED
, CAP_SETUID
);
1462 capng_update(CAPNG_ADD
, CAPNG_PERMITTED
, CAP_SETGID
);
1463 capng_update(CAPNG_ADD
, CAPNG_PERMITTED
, CAP_DAC_OVERRIDE
);
1465 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_SETUID
);
1466 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_SETGID
);
1467 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
1469 capng_apply(CAPNG_SELECT_BOTH
);
1472 #endif /* HAVE_CAP_NG_H */
1474 if (getuid() == 0 || geteuid() == 0) {
1475 if (username
|| chroot_dir
)
1476 droproot(username
, chroot_dir
);
1481 if (pcap_setfilter(pd
, &fcode
) < 0)
1482 error("%s", pcap_geterr(pd
));
1485 /* Do not exceed the default PATH_MAX for files. */
1486 dumpinfo
.CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
1488 if (dumpinfo
.CurrentFileName
== NULL
)
1489 error("malloc of dumpinfo.CurrentFileName");
1491 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1493 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, WflagChars
);
1495 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, 0);
1497 p
= pcap_dump_open(pd
, dumpinfo
.CurrentFileName
);
1498 #ifdef HAVE_CAP_NG_H
1499 /* Give up capabilities, clear Effective set */
1500 capng_clear(CAPNG_EFFECTIVE
);
1503 error("%s", pcap_geterr(pd
));
1504 if (Cflag
!= 0 || Gflag
!= 0) {
1505 callback
= dump_packet_and_trunc
;
1506 dumpinfo
.WFileName
= WFileName
;
1509 pcap_userdata
= (u_char
*)&dumpinfo
;
1511 callback
= dump_packet
;
1512 pcap_userdata
= (u_char
*)p
;
1514 #ifdef HAVE_PCAP_DUMP_FLUSH
1519 type
= pcap_datalink(pd
);
1520 printinfo
= get_print_info(type
);
1521 callback
= print_packet
;
1522 pcap_userdata
= (u_char
*)&printinfo
;
1525 #ifdef SIGNAL_REQ_INFO
1527 * We can't get statistics when reading from a file rather
1528 * than capturing from a device.
1530 if (RFileName
== NULL
)
1531 (void)setsignal(SIGNAL_REQ_INFO
, requestinfo
);
1534 if (vflag
> 0 && WFileName
) {
1536 * When capturing to a file, "-v" means tcpdump should,
1537 * every 10 secodns, "v"erbosely report the number of
1540 #ifdef USE_WIN32_MM_TIMER
1541 /* call verbose_stats_dump() each 1000 +/-100msec */
1542 timer_id
= timeSetEvent(1000, 100, verbose_stats_dump
, 0, TIME_PERIODIC
);
1543 setvbuf(stderr
, NULL
, _IONBF
, 0);
1544 #elif defined(HAVE_ALARM)
1545 (void)setsignal(SIGALRM
, verbose_stats_dump
);
1551 if (RFileName
== NULL
) {
1553 * Live capture (if -V was specified, we set RFileName
1554 * to a file from the -V file). Print a message to
1555 * the standard error on UN*X.
1557 if (!vflag
&& !WFileName
) {
1558 (void)fprintf(stderr
,
1559 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1562 (void)fprintf(stderr
, "%s: ", program_name
);
1563 dlt
= pcap_datalink(pd
);
1564 dlt_name
= pcap_datalink_val_to_name(dlt
);
1565 if (dlt_name
== NULL
) {
1566 (void)fprintf(stderr
, "listening on %s, link-type %u, capture size %u bytes\n",
1567 device
, dlt
, snaplen
);
1569 (void)fprintf(stderr
, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1571 pcap_datalink_val_to_description(dlt
), snaplen
);
1573 (void)fflush(stderr
);
1577 status
= pcap_loop(pd
, cnt
, callback
, pcap_userdata
);
1578 if (WFileName
== NULL
) {
1580 * We're printing packets. Flush the printed output,
1581 * so it doesn't get intermingled with error output.
1585 * We got interrupted, so perhaps we didn't
1586 * manage to finish a line we were printing.
1587 * Print an extra newline, just in case.
1591 (void)fflush(stdout
);
1595 * We got interrupted. If we are reading multiple
1596 * files (via -V) set these so that we stop.
1605 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
1606 program_name
, pcap_geterr(pd
));
1608 if (RFileName
== NULL
) {
1610 * We're doing a live capture. Report the capture
1616 if (VFileName
!= NULL
) {
1617 ret
= get_next_file(VFile
, VFileLine
);
1619 RFileName
= VFileLine
;
1620 pd
= pcap_open_offline(RFileName
, ebuf
);
1623 new_dlt
= pcap_datalink(pd
);
1624 if (WFileName
&& new_dlt
!= dlt
)
1625 error("%s: new dlt does not match original", RFileName
);
1626 printinfo
= get_print_info(new_dlt
);
1627 dlt_name
= pcap_datalink_val_to_name(new_dlt
);
1628 if (dlt_name
== NULL
) {
1629 fprintf(stderr
, "reading from file %s, link-type %u\n",
1630 RFileName
, new_dlt
);
1633 "reading from file %s, link-type %s (%s)\n",
1634 RFileName
, dlt_name
,
1635 pcap_datalink_val_to_description(new_dlt
));
1637 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
1638 error("%s", pcap_geterr(pd
));
1639 if (pcap_setfilter(pd
, &fcode
) < 0)
1640 error("%s", pcap_geterr(pd
));
1644 while (ret
!= NULL
);
1647 exit(status
== -1 ? 1 : 0);
1650 /* make a clean exit on interrupts */
1652 cleanup(int signo _U_
)
1654 #ifdef USE_WIN32_MM_TIMER
1656 timeKillEvent(timer_id
);
1658 #elif defined(HAVE_ALARM)
1662 #ifdef HAVE_PCAP_BREAKLOOP
1664 * We have "pcap_breakloop()"; use it, so that we do as little
1665 * as possible in the signal handler (it's probably not safe
1666 * to do anything with standard I/O streams in a signal handler -
1667 * the ANSI C standard doesn't say it is).
1672 * We don't have "pcap_breakloop()"; this isn't safe, but
1673 * it's the best we can do. Print the summary if we're
1674 * not reading from a savefile - i.e., if we're doing a
1675 * live capture - and exit.
1677 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
1679 * We got interrupted, so perhaps we didn't
1680 * manage to finish a line we were printing.
1681 * Print an extra newline, just in case.
1684 (void)fflush(stdout
);
1692 On windows, we do not use a fork, so we do not care less about
1693 waiting a child processes to die
1695 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1697 child_cleanup(int signo _U_
)
1701 #endif /* HAVE_FORK && HAVE_VFORK */
1704 info(register int verbose
)
1706 struct pcap_stat stat
;
1709 * Older versions of libpcap didn't set ps_ifdrop on some
1710 * platforms; initialize it to 0 to handle that.
1713 if (pcap_stats(pd
, &stat
) < 0) {
1714 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
1720 fprintf(stderr
, "%s: ", program_name
);
1722 (void)fprintf(stderr
, "%u packet%s captured", packets_captured
,
1723 PLURAL_SUFFIX(packets_captured
));
1725 fputs(", ", stderr
);
1728 (void)fprintf(stderr
, "%u packet%s received by filter", stat
.ps_recv
,
1729 PLURAL_SUFFIX(stat
.ps_recv
));
1731 fputs(", ", stderr
);
1734 (void)fprintf(stderr
, "%u packet%s dropped by kernel", stat
.ps_drop
,
1735 PLURAL_SUFFIX(stat
.ps_drop
));
1736 if (stat
.ps_ifdrop
!= 0) {
1738 fputs(", ", stderr
);
1741 (void)fprintf(stderr
, "%u packet%s dropped by interface\n",
1742 stat
.ps_ifdrop
, PLURAL_SUFFIX(stat
.ps_ifdrop
));
1748 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1750 compress_savefile(const char *filename
)
1759 * Set to lowest priority so that this doesn't disturb the capture
1762 setpriority(PRIO_PROCESS
, 0, NZERO
- 1);
1764 setpriority(PRIO_PROCESS
, 0, 19);
1766 if (execlp(zflag
, zflag
, filename
, (char *)NULL
) == -1)
1768 "compress_savefile:execlp(%s, %s): %s\n",
1778 #else /* HAVE_FORK && HAVE_VFORK */
1780 compress_savefile(const char *filename
)
1783 "compress_savefile failed. Functionality not implemented under your system\n");
1785 #endif /* HAVE_FORK && HAVE_VFORK */
1788 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1790 struct dump_info
*dump_info
;
1796 dump_info
= (struct dump_info
*)user
;
1799 * XXX - this won't force the file to rotate on the specified time
1800 * boundary, but it will rotate on the first packet received after the
1801 * specified Gflag number of seconds. Note: if a Gflag time boundary
1802 * and a Cflag size boundary coincide, the time rotation will occur
1803 * first thereby cancelling the Cflag boundary (since the file should
1807 /* Check if it is time to rotate */
1810 /* Get the current time */
1811 if ((t
= time(NULL
)) == (time_t)-1) {
1812 error("dump_and_trunc_packet: can't get current_time: %s",
1813 pcap_strerror(errno
));
1817 /* If the time is greater than the specified window, rotate */
1818 if (t
- Gflag_time
>= Gflag
) {
1819 /* Update the Gflag_time */
1821 /* Update Gflag_count */
1824 * Close the current file and open a new one.
1826 pcap_dump_close(dump_info
->p
);
1829 * Compress the file we just closed, if the user asked for it
1832 compress_savefile(dump_info
->CurrentFileName
);
1835 * Check to see if we've exceeded the Wflag (when
1838 if (Cflag
== 0 && Wflag
> 0 && Gflag_count
>= Wflag
) {
1839 (void)fprintf(stderr
, "Maximum file limit reached: %d\n",
1844 if (dump_info
->CurrentFileName
!= NULL
)
1845 free(dump_info
->CurrentFileName
);
1846 /* Allocate space for max filename + \0. */
1847 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
1848 if (dump_info
->CurrentFileName
== NULL
)
1849 error("dump_packet_and_trunc: malloc");
1851 * This is always the first file in the Cflag
1853 * We also don't need numbering if Cflag is not set.
1856 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0,
1859 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0, 0);
1861 #ifdef HAVE_CAP_NG_H
1862 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
1863 capng_apply(CAPNG_EFFECTIVE
);
1864 #endif /* HAVE_CAP_NG_H */
1865 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
1866 #ifdef HAVE_CAP_NG_H
1867 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
1868 capng_apply(CAPNG_EFFECTIVE
);
1869 #endif /* HAVE_CAP_NG_H */
1870 if (dump_info
->p
== NULL
)
1871 error("%s", pcap_geterr(pd
));
1876 * XXX - this won't prevent capture files from getting
1877 * larger than Cflag - the last packet written to the
1878 * file could put it over Cflag.
1880 if (Cflag
!= 0 && pcap_dump_ftell(dump_info
->p
) > Cflag
) {
1882 * Close the current file and open a new one.
1884 pcap_dump_close(dump_info
->p
);
1887 * Compress the file we just closed, if the user asked for it
1890 compress_savefile(dump_info
->CurrentFileName
);
1894 if (Cflag_count
>= Wflag
)
1897 if (dump_info
->CurrentFileName
!= NULL
)
1898 free(dump_info
->CurrentFileName
);
1899 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
1900 if (dump_info
->CurrentFileName
== NULL
)
1901 error("dump_packet_and_trunc: malloc");
1902 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, Cflag_count
, WflagChars
);
1903 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
1904 if (dump_info
->p
== NULL
)
1905 error("%s", pcap_geterr(pd
));
1908 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
1909 #ifdef HAVE_PCAP_DUMP_FLUSH
1911 pcap_dump_flush(dump_info
->p
);
1920 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1926 pcap_dump(user
, h
, sp
);
1927 #ifdef HAVE_PCAP_DUMP_FLUSH
1929 pcap_dump_flush((pcap_dumper_t
*)user
);
1938 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1940 struct print_info
*print_info
;
1942 netdissect_options
*ndo
;
1949 print_info
= (struct print_info
*)user
;
1950 ndo
= print_info
->ndo
;
1953 * Some printers want to check that they're not walking off the
1954 * end of the packet.
1955 * Rather than pass it all the way down, we set this global.
1957 ndo
->ndo_snapend
= sp
+ h
->caplen
;
1959 if(print_info
->ndo_type
) {
1960 hdrlen
= (*print_info
->p
.ndo_printer
)(print_info
->ndo
, h
, sp
);
1962 hdrlen
= (*print_info
->p
.printer
)(h
, sp
);
1965 if (ndo
->ndo_Xflag
) {
1967 * Print the raw packet data in hex and ASCII.
1969 if (ndo
->ndo_Xflag
> 1) {
1971 * Include the link-layer header.
1973 hex_and_ascii_print("\n\t", sp
, h
->caplen
);
1976 * Don't include the link-layer header - and if
1977 * we have nothing past the link-layer header,
1980 if (h
->caplen
> hdrlen
)
1981 hex_and_ascii_print("\n\t", sp
+ hdrlen
,
1982 h
->caplen
- hdrlen
);
1984 } else if (ndo
->ndo_xflag
) {
1986 * Print the raw packet data in hex.
1988 if (ndo
->ndo_xflag
> 1) {
1990 * Include the link-layer header.
1992 hex_print(ndo
, "\n\t", sp
, h
->caplen
);
1995 * Don't include the link-layer header - and if
1996 * we have nothing past the link-layer header,
1999 if (h
->caplen
> hdrlen
)
2000 hex_print(ndo
, "\n\t", sp
+ hdrlen
,
2001 h
->caplen
- hdrlen
);
2003 } else if (ndo
->ndo_Aflag
) {
2005 * Print the raw packet data in ASCII.
2007 if (ndo
->ndo_Aflag
> 1) {
2009 * Include the link-layer header.
2011 ascii_print(sp
, h
->caplen
);
2014 * Don't include the link-layer header - and if
2015 * we have nothing past the link-layer header,
2018 if (h
->caplen
> hdrlen
)
2019 ascii_print(sp
+ hdrlen
, h
->caplen
- hdrlen
);
2032 * XXX - there should really be libpcap calls to get the version
2033 * number as a string (the string would be generated from #defines
2034 * at run time, so that it's not generated from string constants
2035 * in the library, as, on many UNIX systems, those constants would
2036 * be statically linked into the application executable image, and
2037 * would thus reflect the version of libpcap on the system on
2038 * which the application was *linked*, not the system on which it's
2041 * That routine should be documented, unlike the "version[]"
2042 * string, so that UNIX vendors providing their own libpcaps
2043 * don't omit it (as a couple of vendors have...).
2045 * Packet.dll should perhaps also export a routine to return the
2046 * version number of the Packet.dll code, to supply the
2047 * "Wpcap_version" information on Windows.
2049 char WDversion
[]="current-git.tcpdump.org";
2050 #if !defined(HAVE_GENERATED_VERSION)
2051 char version
[]="current-git.tcpdump.org";
2053 char pcap_version
[]="current-git.tcpdump.org";
2054 char Wpcap_version
[]="3.1";
2058 * By default, print the specified data out in hex and ASCII.
2061 ndo_default_print(netdissect_options
*ndo _U_
, const u_char
*bp
, u_int length
)
2063 hex_and_ascii_print("\n\t", bp
, length
); /* pass on lf and identation string */
2067 default_print(const u_char
*bp
, u_int length
)
2069 ndo_default_print(gndo
, bp
, length
);
2072 #ifdef SIGNAL_REQ_INFO
2073 RETSIGTYPE
requestinfo(int signo _U_
)
2083 * Called once each second in verbose mode while dumping to file
2085 #ifdef USE_WIN32_MM_TIMER
2086 void CALLBACK
verbose_stats_dump (UINT timer_id _U_
, UINT msg _U_
, DWORD_PTR arg _U_
,
2087 DWORD_PTR dw1 _U_
, DWORD_PTR dw2 _U_
)
2089 struct pcap_stat stat
;
2091 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
2092 fprintf(stderr
, "Got %u\r", packets_captured
);
2094 #elif defined(HAVE_ALARM)
2095 static void verbose_stats_dump(int sig _U_
)
2097 struct pcap_stat stat
;
2099 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
2100 fprintf(stderr
, "Got %u\r", packets_captured
);
2108 extern char version
[];
2109 #ifndef HAVE_PCAP_LIB_VERSION
2110 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
2111 extern char pcap_version
[];
2112 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2113 static char pcap_version
[] = "unknown";
2114 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2115 #endif /* HAVE_PCAP_LIB_VERSION */
2117 #ifdef HAVE_PCAP_LIB_VERSION
2119 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2121 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2123 (void)fprintf(stderr
, "%s\n",pcap_lib_version());
2124 #else /* HAVE_PCAP_LIB_VERSION */
2126 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2127 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
2129 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2130 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
2132 #endif /* HAVE_PCAP_LIB_VERSION */
2133 (void)fprintf(stderr
,
2134 "Usage: %s [-aAbd" D_FLAG
"efhH" I_FLAG J_FLAG
"KlLnNOpqRStu" U_FLAG
"vxX]" B_FLAG_USAGE
" [ -c count ]\n", program_name
);
2135 (void)fprintf(stderr
,
2136 "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n");
2137 (void)fprintf(stderr
,
2138 "\t\t[ -i interface ]" j_FLAG_USAGE
" [ -M secret ]\n");
2139 #ifdef HAVE_PCAP_SETDIRECTION
2140 (void)fprintf(stderr
,
2141 "\t\t[ -Q in|out|inout ]\n");
2143 (void)fprintf(stderr
,
2144 "\t\t[ -r file ] [ -s snaplen ] [ -T type ] [ -V file ] [ -w file ]\n");
2145 (void)fprintf(stderr
,
2146 "\t\t[ -W filecount ] [ -y datalinktype ] [ -z command ]\n");
2147 (void)fprintf(stderr
,
2148 "\t\t[ -Z user ] [ expression ]\n");
2156 ndo_error(netdissect_options
*ndo _U_
, const char *fmt
, ...)
2160 (void)fprintf(stderr
, "%s: ", program_name
);
2162 (void)vfprintf(stderr
, fmt
, ap
);
2166 if (fmt
[-1] != '\n')
2167 (void)fputc('\n', stderr
);
2175 ndo_warning(netdissect_options
*ndo _U_
, const char *fmt
, ...)
2179 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
2181 (void)vfprintf(stderr
, fmt
, ap
);
2185 if (fmt
[-1] != '\n')
2186 (void)fputc('\n', stderr
);
2191 * c-style: whitesmith