2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 * Support for splitting captures into multiple files with a maximum
25 * Seth Webster <swebster@sst.ll.mit.edu>
29 static const char copyright
[] _U_
=
30 "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
31 The Regents of the University of California. All rights reserved.\n";
32 static const char rcsid
[] _U_
=
33 "@(#) $Header: /tcpdump/master/tcpdump/tcpdump.c,v 1.283 2008-09-25 21:45:50 guy Exp $ (LBL)";
37 * tcpdump - monitor tcp/ip traffic on an ethernet.
39 * First written in 1987 by Van Jacobson, Lawrence Berkeley Laboratory.
40 * Mercilessly hacked and occasionally improved since then via the
41 * combined efforts of Van, Steve McCanne and Craig Leres of LBL.
48 #include <tcpdump-stdinc.h>
53 extern int strcasecmp (const char *__s1
, const char *__s2
);
71 #include <sys/resource.h>
77 /* capabilities convinience library */
80 #endif /* HAVE_CAP_NG_H */
82 #include "netdissect.h"
83 #include "interface.h"
84 #include "addrtoname.h"
86 #include "setsignal.h"
87 #include "gmt2local.h"
88 #include "pcap-missing.h"
95 #define SIGNAL_REQ_INFO SIGINFO
97 #define SIGNAL_REQ_INFO SIGUSR1
100 netdissect_options Gndo
;
101 netdissect_options
*gndo
= &Gndo
;
103 static int dflag
; /* print filter code */
104 static int Lflag
; /* list available data link types and exit */
105 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
106 static int Jflag
; /* list available time stamp types */
108 static char *zflag
= NULL
; /* compress each savefile using a specified command (like gzip or bzip2) */
110 static int infodelay
;
111 static int infoprint
;
115 int32_t thiszone
; /* seconds offset from gmt to local time */
118 static RETSIGTYPE
cleanup(int);
119 static RETSIGTYPE
child_cleanup(int);
120 static void usage(void) __attribute__((noreturn
));
121 static void show_dlts_and_exit(const char *device
, pcap_t
*pd
) __attribute__((noreturn
));
123 static void print_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
124 static void ndo_default_print(netdissect_options
*, const u_char
*, u_int
);
125 static void dump_packet_and_trunc(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
126 static void dump_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
127 static void droproot(const char *, const char *);
128 static void ndo_error(netdissect_options
*ndo
, const char *fmt
, ...)
129 __attribute__ ((noreturn
, format (printf
, 2, 3)));
130 static void ndo_warning(netdissect_options
*ndo
, const char *fmt
, ...);
132 #ifdef SIGNAL_REQ_INFO
133 RETSIGTYPE
requestinfo(int);
136 #if defined(USE_WIN32_MM_TIMER)
137 #include <MMsystem.h>
138 static UINT timer_id
;
139 static void CALLBACK
verbose_stats_dump(UINT
, UINT
, DWORD_PTR
, DWORD_PTR
, DWORD_PTR
);
140 #elif defined(HAVE_ALARM)
141 static void verbose_stats_dump(int sig
);
144 static void info(int);
145 static u_int packets_captured
;
159 static struct printer printers
[] = {
160 { arcnet_if_print
, DLT_ARCNET
},
161 #ifdef DLT_ARCNET_LINUX
162 { arcnet_linux_if_print
, DLT_ARCNET_LINUX
},
164 { token_if_print
, DLT_IEEE802
},
166 { lane_if_print
, DLT_LANE8023
},
169 { cip_if_print
, DLT_CIP
},
172 { cip_if_print
, DLT_ATM_CLIP
},
174 { sl_if_print
, DLT_SLIP
},
175 #ifdef DLT_SLIP_BSDOS
176 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
178 { ppp_if_print
, DLT_PPP
},
179 #ifdef DLT_PPP_WITHDIRECTION
180 { ppp_if_print
, DLT_PPP_WITHDIRECTION
},
183 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
185 { fddi_if_print
, DLT_FDDI
},
186 { null_if_print
, DLT_NULL
},
188 { null_if_print
, DLT_LOOP
},
190 { raw_if_print
, DLT_RAW
},
191 { atm_if_print
, DLT_ATM_RFC1483
},
193 { chdlc_if_print
, DLT_C_HDLC
},
196 { chdlc_if_print
, DLT_HDLC
},
198 #ifdef DLT_PPP_SERIAL
199 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
202 { pppoe_if_print
, DLT_PPP_ETHER
},
205 { sll_if_print
, DLT_LINUX_SLL
},
207 #ifdef DLT_IEEE802_11
208 { ieee802_11_if_print
, DLT_IEEE802_11
},
211 { ltalk_if_print
, DLT_LTALK
},
213 #if defined(DLT_PFLOG) && defined(HAVE_NET_PFVAR_H)
214 { pflog_if_print
, DLT_PFLOG
},
217 { fr_if_print
, DLT_FR
},
220 { fr_if_print
, DLT_FRELAY
},
223 { sunatm_if_print
, DLT_SUNATM
},
225 #ifdef DLT_IP_OVER_FC
226 { ipfc_if_print
, DLT_IP_OVER_FC
},
228 #ifdef DLT_PRISM_HEADER
229 { prism_if_print
, DLT_PRISM_HEADER
},
231 #ifdef DLT_IEEE802_11_RADIO
232 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
235 { enc_if_print
, DLT_ENC
},
237 #ifdef DLT_SYMANTEC_FIREWALL
238 { symantec_if_print
, DLT_SYMANTEC_FIREWALL
},
240 #ifdef DLT_APPLE_IP_OVER_IEEE1394
241 { ap1394_if_print
, DLT_APPLE_IP_OVER_IEEE1394
},
243 #ifdef DLT_IEEE802_11_RADIO_AVS
244 { ieee802_11_radio_avs_if_print
, DLT_IEEE802_11_RADIO_AVS
},
246 #ifdef DLT_JUNIPER_ATM1
247 { juniper_atm1_print
, DLT_JUNIPER_ATM1
},
249 #ifdef DLT_JUNIPER_ATM2
250 { juniper_atm2_print
, DLT_JUNIPER_ATM2
},
252 #ifdef DLT_JUNIPER_MFR
253 { juniper_mfr_print
, DLT_JUNIPER_MFR
},
255 #ifdef DLT_JUNIPER_MLFR
256 { juniper_mlfr_print
, DLT_JUNIPER_MLFR
},
258 #ifdef DLT_JUNIPER_MLPPP
259 { juniper_mlppp_print
, DLT_JUNIPER_MLPPP
},
261 #ifdef DLT_JUNIPER_PPPOE
262 { juniper_pppoe_print
, DLT_JUNIPER_PPPOE
},
264 #ifdef DLT_JUNIPER_PPPOE_ATM
265 { juniper_pppoe_atm_print
, DLT_JUNIPER_PPPOE_ATM
},
267 #ifdef DLT_JUNIPER_GGSN
268 { juniper_ggsn_print
, DLT_JUNIPER_GGSN
},
270 #ifdef DLT_JUNIPER_ES
271 { juniper_es_print
, DLT_JUNIPER_ES
},
273 #ifdef DLT_JUNIPER_MONITOR
274 { juniper_monitor_print
, DLT_JUNIPER_MONITOR
},
276 #ifdef DLT_JUNIPER_SERVICES
277 { juniper_services_print
, DLT_JUNIPER_SERVICES
},
279 #ifdef DLT_JUNIPER_ETHER
280 { juniper_ether_print
, DLT_JUNIPER_ETHER
},
282 #ifdef DLT_JUNIPER_PPP
283 { juniper_ppp_print
, DLT_JUNIPER_PPP
},
285 #ifdef DLT_JUNIPER_FRELAY
286 { juniper_frelay_print
, DLT_JUNIPER_FRELAY
},
288 #ifdef DLT_JUNIPER_CHDLC
289 { juniper_chdlc_print
, DLT_JUNIPER_CHDLC
},
292 { mfr_if_print
, DLT_MFR
},
294 #if defined(DLT_BLUETOOTH_HCI_H4_WITH_PHDR) && defined(HAVE_PCAP_BLUETOOTH_H)
295 { bt_if_print
, DLT_BLUETOOTH_HCI_H4_WITH_PHDR
},
297 #ifdef HAVE_PCAP_USB_H
299 { usb_linux_48_byte_print
, DLT_USB_LINUX
},
300 #endif /* DLT_USB_LINUX */
301 #ifdef DLT_USB_LINUX_MMAPPED
302 { usb_linux_64_byte_print
, DLT_USB_LINUX_MMAPPED
},
303 #endif /* DLT_USB_LINUX_MMAPPED */
304 #endif /* HAVE_PCAP_USB_H */
306 { raw_if_print
, DLT_IPV4
},
309 { raw_if_print
, DLT_IPV6
},
314 static struct ndo_printer ndo_printers
[] = {
316 #ifdef HAVE_LINUX_NETFILTER_NFNETLINK_LOG_H
317 { nflog_if_print
, DLT_NFLOG
},
320 { ether_if_print
, DLT_EN10MB
},
322 { ipnet_if_print
, DLT_IPNET
},
324 #ifdef DLT_IEEE802_15_4
325 { ieee802_15_4_if_print
, DLT_IEEE802_15_4
},
327 #ifdef DLT_IEEE802_15_4_NOFCS
328 { ieee802_15_4_if_print
, DLT_IEEE802_15_4_NOFCS
},
331 { ppi_if_print
, DLT_PPI
},
333 #ifdef DLT_NETANALYZER
334 { netanalyzer_if_print
, DLT_NETANALYZER
},
336 #ifdef DLT_NETANALYZER_TRANSPARENT
337 { netanalyzer_transparent_if_print
, DLT_NETANALYZER_TRANSPARENT
},
343 lookup_printer(int type
)
347 for (p
= printers
; p
->f
; ++p
)
356 lookup_ndo_printer(int type
)
358 struct ndo_printer
*p
;
360 for (p
= ndo_printers
; p
->f
; ++p
)
370 static int supports_monitor_mode
;
377 netdissect_options
*ndo
;
380 if_ndo_printer ndo_printer
;
387 char *CurrentFileName
;
392 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
394 show_tstamp_types_and_exit(const char *device
, pcap_t
*pd
)
397 int *tstamp_types
= 0;
398 const char *tstamp_type_name
;
401 n_tstamp_types
= pcap_list_tstamp_types(pd
, &tstamp_types
);
402 if (n_tstamp_types
< 0)
403 error("%s", pcap_geterr(pd
));
405 if (n_tstamp_types
== 0) {
406 fprintf(stderr
, "Time stamp type cannot be set for %s\n",
410 fprintf(stderr
, "Time stamp types for %s (use option -j to set):\n",
412 for (i
= 0; i
< n_tstamp_types
; i
++) {
413 tstamp_type_name
= pcap_tstamp_type_val_to_name(tstamp_types
[i
]);
414 if (tstamp_type_name
!= NULL
) {
415 (void) fprintf(stderr
, " %s (%s)\n", tstamp_type_name
,
416 pcap_tstamp_type_val_to_description(tstamp_types
[i
]));
418 (void) fprintf(stderr
, " %d\n", tstamp_types
[i
]);
421 pcap_free_tstamp_types(tstamp_types
);
427 show_dlts_and_exit(const char *device
, pcap_t
*pd
)
431 const char *dlt_name
;
433 n_dlts
= pcap_list_datalinks(pd
, &dlts
);
435 error("%s", pcap_geterr(pd
));
436 else if (n_dlts
== 0 || !dlts
)
437 error("No data link types.");
440 * If the interface is known to support monitor mode, indicate
441 * whether these are the data link types available when not in
442 * monitor mode, if -I wasn't specified, or when in monitor mode,
443 * when -I was specified (the link-layer types available in
444 * monitor mode might be different from the ones available when
445 * not in monitor mode).
447 if (supports_monitor_mode
)
448 (void) fprintf(stderr
, "Data link types for %s %s (use option -y to set):\n",
450 Iflag
? "when in monitor mode" : "when not in monitor mode");
452 (void) fprintf(stderr
, "Data link types for %s (use option -y to set):\n",
455 while (--n_dlts
>= 0) {
456 dlt_name
= pcap_datalink_val_to_name(dlts
[n_dlts
]);
457 if (dlt_name
!= NULL
) {
458 (void) fprintf(stderr
, " %s (%s)", dlt_name
,
459 pcap_datalink_val_to_description(dlts
[n_dlts
]));
462 * OK, does tcpdump handle that type?
464 if (lookup_printer(dlts
[n_dlts
]) == NULL
465 && lookup_ndo_printer(dlts
[n_dlts
]) == NULL
)
466 (void) fprintf(stderr
, " (printing not supported)");
467 fprintf(stderr
, "\n");
469 (void) fprintf(stderr
, " DLT %d (printing not supported)\n",
473 #ifdef HAVE_PCAP_FREE_DATALINKS
474 pcap_free_datalinks(dlts
);
480 * Set up flags that might or might not be supported depending on the
481 * version of libpcap we're using.
483 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
485 #define B_FLAG_USAGE " [ -B size ]"
486 #else /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
489 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
491 #ifdef HAVE_PCAP_CREATE
493 #else /* HAVE_PCAP_CREATE */
495 #endif /* HAVE_PCAP_CREATE */
497 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
499 #define j_FLAG_USAGE " [ -j tstamptype ]"
501 #else /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
505 #endif /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
507 #ifdef HAVE_PCAP_FINDALLDEVS
508 #ifndef HAVE_PCAP_IF_T
509 #undef HAVE_PCAP_FINDALLDEVS
513 #ifdef HAVE_PCAP_FINDALLDEVS
519 #ifdef HAVE_PCAP_DUMP_FLUSH
526 /* Drop root privileges and chroot if necessary */
528 droproot(const char *username
, const char *chroot_dir
)
530 struct passwd
*pw
= NULL
;
532 if (chroot_dir
&& !username
) {
533 fprintf(stderr
, "tcpdump: Chroot without dropping root is insecure\n");
537 pw
= getpwnam(username
);
540 if (chroot(chroot_dir
) != 0 || chdir ("/") != 0) {
541 fprintf(stderr
, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
542 chroot_dir
, pcap_strerror(errno
));
547 int ret
= capng_change_id(pw
->pw_uid
, pw
->pw_gid
, CAPNG_NO_FLAG
);
549 printf("error : ret %d\n", ret
);
551 /* We don't need CAP_SETUID and CAP_SETGID */
552 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_SETUID
);
553 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_SETUID
);
554 capng_update(CAPNG_DROP
, CAPNG_PERMITTED
, CAP_SETUID
);
555 capng_update(CAPNG_DROP
, CAPNG_PERMITTED
, CAP_SETUID
);
556 capng_apply(CAPNG_SELECT_BOTH
);
559 if (initgroups(pw
->pw_name
, pw
->pw_gid
) != 0 ||
560 setgid(pw
->pw_gid
) != 0 || setuid(pw
->pw_uid
) != 0) {
561 fprintf(stderr
, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
563 (unsigned long)pw
->pw_uid
,
564 (unsigned long)pw
->pw_gid
,
565 pcap_strerror(errno
));
568 #endif /* HAVE_CAP_NG_H */
571 fprintf(stderr
, "tcpdump: Couldn't find user '%.32s'\n",
594 MakeFilename(char *buffer
, char *orig_name
, int cnt
, int max_chars
)
596 char *filename
= malloc(PATH_MAX
+ 1);
597 if (filename
== NULL
)
598 error("Makefilename: malloc");
600 /* Process with strftime if Gflag is set. */
604 /* Convert Gflag_time to a usable format */
605 if ((local_tm
= localtime(&Gflag_time
)) == NULL
) {
606 error("MakeTimedFilename: localtime");
609 /* There's no good way to detect an error in strftime since a return
610 * value of 0 isn't necessarily failure.
612 strftime(filename
, PATH_MAX
, orig_name
, local_tm
);
614 strncpy(filename
, orig_name
, PATH_MAX
);
617 if (cnt
== 0 && max_chars
== 0)
618 strncpy(buffer
, filename
, PATH_MAX
+ 1);
620 if (snprintf(buffer
, PATH_MAX
+ 1, "%s%0*d", filename
, max_chars
, cnt
) > PATH_MAX
)
621 /* Report an error if the filename is too large */
622 error("too many output files or filename is too long (> %d)", PATH_MAX
);
626 static int tcpdump_printf(netdissect_options
*ndo _U_
,
627 const char *fmt
, ...)
634 ret
=vfprintf(stdout
, fmt
, args
);
640 static struct print_info
641 get_print_info(int type
)
643 struct print_info printinfo
;
645 printinfo
.ndo_type
= 1;
646 printinfo
.ndo
= gndo
;
647 printinfo
.p
.ndo_printer
= lookup_ndo_printer(type
);
648 if (printinfo
.p
.ndo_printer
== NULL
) {
649 printinfo
.p
.printer
= lookup_printer(type
);
650 printinfo
.ndo_type
= 0;
651 if (printinfo
.p
.printer
== NULL
) {
652 gndo
->ndo_dltname
= pcap_datalink_val_to_name(type
);
653 if (gndo
->ndo_dltname
!= NULL
)
654 error("packet printing is not supported for link type %s: use -w",
657 error("packet printing is not supported for link type %d: use -w", type
);
664 get_next_file(FILE *VFile
, char *ptr
)
668 ret
= fgets(ptr
, PATH_MAX
, VFile
);
672 if (ptr
[strlen(ptr
) - 1] == '\n')
673 ptr
[strlen(ptr
) - 1] = '\0';
679 main(int argc
, char **argv
)
681 register int cnt
, op
, i
;
682 bpf_u_int32 localnet
, netmask
;
683 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *VFileName
, *WFileName
;
684 pcap_handler callback
;
688 const char *dlt_name
;
689 struct bpf_program fcode
;
691 RETSIGTYPE (*oldhandler
)(int);
693 struct print_info printinfo
;
694 struct dump_info dumpinfo
;
695 u_char
*pcap_userdata
;
696 char ebuf
[PCAP_ERRBUF_SIZE
];
697 char VFileLine
[PATH_MAX
+ 1];
698 char *username
= NULL
;
699 char *chroot_dir
= NULL
;
702 #ifdef HAVE_PCAP_FINDALLDEVS
703 pcap_if_t
*devpointer
;
709 if(wsockinit() != 0) return 1;
712 jflag
=-1; /* not set */
716 gndo
->ndo_default_print
=ndo_default_print
;
717 gndo
->ndo_printf
=tcpdump_printf
;
718 gndo
->ndo_error
=ndo_error
;
719 gndo
->ndo_warning
=ndo_warning
;
720 gndo
->ndo_snaplen
= DEFAULT_SNAPLEN
;
730 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
731 program_name
= cp
+ 1;
733 program_name
= argv
[0];
735 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
743 (op
= getopt(argc
, argv
, "aAb" B_FLAG
"c:C:d" D_FLAG
"eE:fF:G:hHi:" I_FLAG j_FLAG J_FLAG
"KlLm:M:nNOpqr:Rs:StT:u" U_FLAG
"V:vw:W:xXy:Yz:Z:")) != -1)
747 /* compatibility for old -a */
758 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
760 Bflag
= atoi(optarg
)*1024;
762 error("invalid packet buffer size %s", optarg
);
764 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
769 error("invalid packet count %s", optarg
);
773 Cflag
= atoi(optarg
) * 1000000;
775 error("invalid file size %s", optarg
);
782 #ifdef HAVE_PCAP_FINDALLDEVS
784 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
787 for (i
= 0; devpointer
!= 0; i
++) {
788 printf("%d.%s", i
+1, devpointer
->name
);
789 if (devpointer
->description
!= NULL
)
790 printf(" (%s)", devpointer
->description
);
792 devpointer
= devpointer
->next
;
796 #endif /* HAVE_PCAP_FINDALLDEVS */
807 #ifndef HAVE_LIBCRYPTO
808 warning("crypto code not compiled in");
810 gndo
->ndo_espsecret
= optarg
;
822 Gflag
= atoi(optarg
);
824 error("invalid number of seconds %s", optarg
);
826 /* We will create one file initially. */
829 /* Grab the current time for rotation use. */
830 if ((Gflag_time
= time(NULL
)) == (time_t)-1) {
831 error("main: can't get current time: %s",
832 pcap_strerror(errno
));
845 if (optarg
[0] == '0' && optarg
[1] == 0)
846 error("Invalid adapter index");
848 #ifdef HAVE_PCAP_FINDALLDEVS
850 * If the argument is a number, treat it as
851 * an index into the list of adapters, as
852 * printed by "tcpdump -D".
854 * This should be OK on UNIX systems, as interfaces
855 * shouldn't have names that begin with digits.
856 * It can be useful on Windows, where more than
857 * one interface can have the same name.
859 devnum
= strtol(optarg
, &end
, 10);
860 if (optarg
!= end
&& *end
== '\0') {
862 error("Invalid adapter index");
864 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
868 * Look for the devnum-th entry
869 * in the list of devices
873 i
< devnum
-1 && devpointer
!= NULL
;
874 i
++, devpointer
= devpointer
->next
)
876 if (devpointer
== NULL
)
877 error("Invalid adapter index");
879 device
= devpointer
->name
;
882 #endif /* HAVE_PCAP_FINDALLDEVS */
886 #ifdef HAVE_PCAP_CREATE
890 #endif /* HAVE_PCAP_CREATE */
892 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
894 jflag
= pcap_tstamp_type_name_to_val(optarg
);
896 error("invalid time stamp type %s", optarg
);
907 * _IOLBF is the same as _IOFBF in Microsoft's C
908 * libraries; the only alternative they offer
911 * XXX - this should really be checking for MSVC++,
912 * not WIN32, if, for example, MinGW has its own
913 * C library that is more UNIX-compatible.
915 setvbuf(stdout
, NULL
, _IONBF
, 0);
917 #ifdef HAVE_SETLINEBUF
920 setvbuf(stdout
, NULL
, _IOLBF
, 0);
931 if (smiLoadModule(optarg
) == 0) {
932 error("could not load MIB module %s", optarg
);
936 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
937 program_name
, optarg
);
938 (void)fprintf(stderr
, "(no libsmi support)\n");
943 /* TCP-MD5 shared secret */
944 #ifndef HAVE_LIBCRYPTO
945 warning("crypto code not compiled in");
968 ++suppress_default_print
;
980 snaplen
= strtol(optarg
, &end
, 0);
981 if (optarg
== end
|| *end
!= '\0'
982 || snaplen
< 0 || snaplen
> MAXIMUM_SNAPLEN
)
983 error("invalid snaplen %s", optarg
);
984 else if (snaplen
== 0)
985 snaplen
= MAXIMUM_SNAPLEN
;
997 if (strcasecmp(optarg
, "vat") == 0)
999 else if (strcasecmp(optarg
, "wb") == 0)
1001 else if (strcasecmp(optarg
, "rpc") == 0)
1002 packettype
= PT_RPC
;
1003 else if (strcasecmp(optarg
, "rtp") == 0)
1004 packettype
= PT_RTP
;
1005 else if (strcasecmp(optarg
, "rtcp") == 0)
1006 packettype
= PT_RTCP
;
1007 else if (strcasecmp(optarg
, "snmp") == 0)
1008 packettype
= PT_SNMP
;
1009 else if (strcasecmp(optarg
, "cnfp") == 0)
1010 packettype
= PT_CNFP
;
1011 else if (strcasecmp(optarg
, "tftp") == 0)
1012 packettype
= PT_TFTP
;
1013 else if (strcasecmp(optarg
, "aodv") == 0)
1014 packettype
= PT_AODV
;
1015 else if (strcasecmp(optarg
, "carp") == 0)
1016 packettype
= PT_CARP
;
1017 else if (strcasecmp(optarg
, "radius") == 0)
1018 packettype
= PT_RADIUS
;
1019 else if (strcasecmp(optarg
, "zmtp1") == 0)
1020 packettype
= PT_ZMTP1
;
1021 else if (strcasecmp(optarg
, "vxlan") == 0)
1022 packettype
= PT_VXLAN
;
1023 else if (strcasecmp(optarg
, "pgm") == 0)
1024 packettype
= PT_PGM
;
1025 else if (strcasecmp(optarg
, "pgm_zmtp1") == 0)
1026 packettype
= PT_PGM_ZMTP1
;
1028 error("unknown packet type `%s'", optarg
);
1035 #ifdef HAVE_PCAP_DUMP_FLUSH
1054 Wflag
= atoi(optarg
);
1056 error("invalid number of output files %s", optarg
);
1057 WflagChars
= getWflagChars(Wflag
);
1062 ++suppress_default_print
;
1067 ++suppress_default_print
;
1071 gndo
->ndo_dltname
= optarg
;
1073 pcap_datalink_name_to_val(gndo
->ndo_dltname
);
1074 if (gndo
->ndo_dlt
< 0)
1075 error("invalid data link type %s", gndo
->ndo_dltname
);
1078 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
1081 /* Undocumented flag */
1082 #ifdef HAVE_PCAP_DEBUG
1083 extern int pcap_debug
;
1094 zflag
= strdup(optarg
);
1103 username
= strdup(optarg
);
1118 case 0: /* Default */
1119 case 4: /* Default + Date*/
1120 thiszone
= gmt2local(0);
1123 case 1: /* No time stamp */
1124 case 2: /* Unix timeval style */
1125 case 3: /* Microseconds since previous packet */
1126 case 5: /* Microseconds since first packet */
1129 default: /* Not supported */
1130 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
1134 if (fflag
!= 0 && (VFileName
!= NULL
|| RFileName
!= NULL
))
1135 error("-f can not be used with -V or -r");
1137 if (VFileName
!= NULL
&& RFileName
!= NULL
)
1138 error("-V and -r are mutually exclusive.");
1141 /* if run as root, prepare for chrooting */
1142 if (getuid() == 0 || geteuid() == 0) {
1143 /* future extensibility for cmd-line arguments */
1145 chroot_dir
= WITH_CHROOT
;
1150 /* if run as root, prepare for dropping root privileges */
1151 if (getuid() == 0 || geteuid() == 0) {
1152 /* Run with '-Z root' to restore old behaviour */
1154 username
= WITH_USER
;
1158 if (RFileName
!= NULL
|| VFileName
!= NULL
) {
1160 * If RFileName is non-null, it's the pathname of a
1161 * savefile to read. If VFileName is non-null, it's
1162 * the pathname of a file containing a list of pathnames
1163 * (one per line) of savefiles to read.
1165 * In either case, we're reading a savefile, not doing
1170 * We don't need network access, so relinquish any set-UID
1171 * or set-GID privileges we have (if any).
1173 * We do *not* want set-UID privileges when opening a
1174 * trace file, as that might let the user read other
1175 * people's trace files (especially if we're set-UID
1178 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
1179 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1181 if (VFileName
!= NULL
) {
1182 if (VFileName
[0] == '-' && VFileName
[1] == '\0')
1185 VFile
= fopen(VFileName
, "r");
1188 error("Unable to open file: %s\n", strerror(errno
));
1190 ret
= get_next_file(VFile
, VFileLine
);
1192 error("Nothing in %s\n", VFileName
);
1193 RFileName
= VFileLine
;
1196 pd
= pcap_open_offline(RFileName
, ebuf
);
1199 dlt
= pcap_datalink(pd
);
1200 dlt_name
= pcap_datalink_val_to_name(dlt
);
1201 if (dlt_name
== NULL
) {
1202 fprintf(stderr
, "reading from file %s, link-type %u\n",
1206 "reading from file %s, link-type %s (%s)\n",
1207 RFileName
, dlt_name
,
1208 pcap_datalink_val_to_description(dlt
));
1214 * We're doing a live capture.
1216 if (device
== NULL
) {
1217 device
= pcap_lookupdev(ebuf
);
1223 * Print a message to the standard error on Windows.
1224 * XXX - why do it here, with a different message?
1226 if(strlen(device
) == 1) /* we assume that an ASCII string is always longer than 1 char */
1227 { /* a Unicode string has a \0 as second byte (so strlen() is 1) */
1228 fprintf(stderr
, "%s: listening on %ws\n", program_name
, device
);
1232 fprintf(stderr
, "%s: listening on %s\n", program_name
, device
);
1237 #ifdef HAVE_PCAP_CREATE
1238 pd
= pcap_create(device
, ebuf
);
1241 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1243 show_tstamp_types_and_exit(device
, pd
);
1246 * Is this an interface that supports monitor mode?
1248 if (pcap_can_set_rfmon(pd
) == 1)
1249 supports_monitor_mode
= 1;
1251 supports_monitor_mode
= 0;
1252 status
= pcap_set_snaplen(pd
, snaplen
);
1254 error("%s: Can't set snapshot length: %s",
1255 device
, pcap_statustostr(status
));
1256 status
= pcap_set_promisc(pd
, !pflag
);
1258 error("%s: Can't set promiscuous mode: %s",
1259 device
, pcap_statustostr(status
));
1261 status
= pcap_set_rfmon(pd
, 1);
1263 error("%s: Can't set monitor mode: %s",
1264 device
, pcap_statustostr(status
));
1266 status
= pcap_set_timeout(pd
, 1000);
1268 error("%s: pcap_set_timeout failed: %s",
1269 device
, pcap_statustostr(status
));
1271 status
= pcap_set_buffer_size(pd
, Bflag
);
1273 error("%s: Can't set buffer size: %s",
1274 device
, pcap_statustostr(status
));
1276 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1278 status
= pcap_set_tstamp_type(pd
, jflag
);
1280 error("%s: Can't set time stamp type: %s",
1281 device
, pcap_statustostr(status
));
1284 status
= pcap_activate(pd
);
1287 * pcap_activate() failed.
1289 cp
= pcap_geterr(pd
);
1290 if (status
== PCAP_ERROR
)
1292 else if ((status
== PCAP_ERROR_NO_SUCH_DEVICE
||
1293 status
== PCAP_ERROR_PERM_DENIED
) &&
1295 error("%s: %s\n(%s)", device
,
1296 pcap_statustostr(status
), cp
);
1298 error("%s: %s", device
,
1299 pcap_statustostr(status
));
1300 } else if (status
> 0) {
1302 * pcap_activate() succeeded, but it's warning us
1303 * of a problem it had.
1305 cp
= pcap_geterr(pd
);
1306 if (status
== PCAP_WARNING
)
1308 else if (status
== PCAP_WARNING_PROMISC_NOTSUP
&&
1310 warning("%s: %s\n(%s)", device
,
1311 pcap_statustostr(status
), cp
);
1313 warning("%s: %s", device
,
1314 pcap_statustostr(status
));
1318 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
1322 warning("%s", ebuf
);
1323 #endif /* HAVE_PCAP_CREATE */
1325 * Let user own process after socket has been opened.
1328 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
1329 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1331 #if !defined(HAVE_PCAP_CREATE) && defined(WIN32)
1333 if(pcap_setbuff(pd
, Bflag
)==-1){
1334 error("%s", pcap_geterr(pd
));
1336 #endif /* !defined(HAVE_PCAP_CREATE) && defined(WIN32) */
1338 show_dlts_and_exit(device
, pd
);
1339 if (gndo
->ndo_dlt
>= 0) {
1340 #ifdef HAVE_PCAP_SET_DATALINK
1341 if (pcap_set_datalink(pd
, gndo
->ndo_dlt
) < 0)
1342 error("%s", pcap_geterr(pd
));
1345 * We don't actually support changing the
1346 * data link type, so we only let them
1347 * set it to what it already is.
1349 if (gndo
->ndo_dlt
!= pcap_datalink(pd
)) {
1350 error("%s is not one of the DLTs supported by this device\n",
1354 (void)fprintf(stderr
, "%s: data link type %s\n",
1355 program_name
, gndo
->ndo_dltname
);
1356 (void)fflush(stderr
);
1358 i
= pcap_snapshot(pd
);
1360 warning("snaplen raised from %d to %d", snaplen
, i
);
1363 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
1366 warning("%s", ebuf
);
1370 cmdbuf
= read_infile(infile
);
1372 cmdbuf
= copy_argv(&argv
[optind
]);
1374 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
1375 error("%s", pcap_geterr(pd
));
1377 bpf_dump(&fcode
, dflag
);
1382 init_addrtoname(localnet
, netmask
);
1386 (void)setsignal(SIGPIPE
, cleanup
);
1387 (void)setsignal(SIGTERM
, cleanup
);
1388 (void)setsignal(SIGINT
, cleanup
);
1390 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1391 (void)setsignal(SIGCHLD
, child_cleanup
);
1393 /* Cooperate with nohup(1) */
1395 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
1396 (void)setsignal(SIGHUP
, oldhandler
);
1401 * If a user name was specified with "-Z", attempt to switch to
1402 * that user's UID. This would probably be used with sudo,
1403 * to allow tcpdump to be run in a special restricted
1404 * account (if you just want to allow users to open capture
1405 * devices, and can't just give users that permission,
1406 * you'd make tcpdump set-UID or set-GID).
1408 * Tcpdump doesn't necessarily write only to one savefile;
1409 * the general only way to allow a -Z instance to write to
1410 * savefiles as the user under whose UID it's run, rather
1411 * than as the user specified with -Z, would thus be to switch
1412 * to the original user ID before opening a capture file and
1413 * then switch back to the -Z user ID after opening the savefile.
1414 * Switching to the -Z user ID only after opening the first
1415 * savefile doesn't handle the general case.
1418 #ifdef HAVE_CAP_NG_H
1419 /* We are running as root and we will be writing to savefile */
1420 if ((getuid() == 0 || geteuid() == 0) && WFileName
) {
1422 /* Drop all capabilities from effective set */
1423 capng_clear(CAPNG_EFFECTIVE
);
1424 /* Add capabilities we will need*/
1425 capng_update(CAPNG_ADD
, CAPNG_PERMITTED
, CAP_SETUID
);
1426 capng_update(CAPNG_ADD
, CAPNG_PERMITTED
, CAP_SETGID
);
1427 capng_update(CAPNG_ADD
, CAPNG_PERMITTED
, CAP_DAC_OVERRIDE
);
1429 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_SETUID
);
1430 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_SETGID
);
1431 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
1433 capng_apply(CAPNG_SELECT_BOTH
);
1436 #endif /* HAVE_CAP_NG_H */
1438 if (getuid() == 0 || geteuid() == 0) {
1439 if (username
|| chroot_dir
)
1440 droproot(username
, chroot_dir
);
1445 if (pcap_setfilter(pd
, &fcode
) < 0)
1446 error("%s", pcap_geterr(pd
));
1449 /* Do not exceed the default PATH_MAX for files. */
1450 dumpinfo
.CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
1452 if (dumpinfo
.CurrentFileName
== NULL
)
1453 error("malloc of dumpinfo.CurrentFileName");
1455 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1457 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, WflagChars
);
1459 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, 0);
1461 p
= pcap_dump_open(pd
, dumpinfo
.CurrentFileName
);
1462 #ifdef HAVE_CAP_NG_H
1463 /* Give up capabilities, clear Effective set */
1464 capng_clear(CAPNG_EFFECTIVE
);
1467 error("%s", pcap_geterr(pd
));
1468 if (Cflag
!= 0 || Gflag
!= 0) {
1469 callback
= dump_packet_and_trunc
;
1470 dumpinfo
.WFileName
= WFileName
;
1473 pcap_userdata
= (u_char
*)&dumpinfo
;
1475 callback
= dump_packet
;
1476 pcap_userdata
= (u_char
*)p
;
1478 #ifdef HAVE_PCAP_DUMP_FLUSH
1483 type
= pcap_datalink(pd
);
1484 printinfo
= get_print_info(type
);
1485 callback
= print_packet
;
1486 pcap_userdata
= (u_char
*)&printinfo
;
1489 #ifdef SIGNAL_REQ_INFO
1491 * We can't get statistics when reading from a file rather
1492 * than capturing from a device.
1494 if (RFileName
== NULL
)
1495 (void)setsignal(SIGNAL_REQ_INFO
, requestinfo
);
1498 if (vflag
> 0 && WFileName
) {
1500 * When capturing to a file, "-v" means tcpdump should,
1501 * every 10 secodns, "v"erbosely report the number of
1504 #ifdef USE_WIN32_MM_TIMER
1505 /* call verbose_stats_dump() each 1000 +/-100msec */
1506 timer_id
= timeSetEvent(1000, 100, verbose_stats_dump
, 0, TIME_PERIODIC
);
1507 setvbuf(stderr
, NULL
, _IONBF
, 0);
1508 #elif defined(HAVE_ALARM)
1509 (void)setsignal(SIGALRM
, verbose_stats_dump
);
1515 if (RFileName
== NULL
) {
1517 * Live capture (if -V was specified, we set RFileName
1518 * to a file from the -V file). Print a message to
1519 * the standard error on UN*X.
1521 if (!vflag
&& !WFileName
) {
1522 (void)fprintf(stderr
,
1523 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1526 (void)fprintf(stderr
, "%s: ", program_name
);
1527 dlt
= pcap_datalink(pd
);
1528 dlt_name
= pcap_datalink_val_to_name(dlt
);
1529 if (dlt_name
== NULL
) {
1530 (void)fprintf(stderr
, "listening on %s, link-type %u, capture size %u bytes\n",
1531 device
, dlt
, snaplen
);
1533 (void)fprintf(stderr
, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1535 pcap_datalink_val_to_description(dlt
), snaplen
);
1537 (void)fflush(stderr
);
1541 status
= pcap_loop(pd
, cnt
, callback
, pcap_userdata
);
1542 if (WFileName
== NULL
) {
1544 * We're printing packets. Flush the printed output,
1545 * so it doesn't get intermingled with error output.
1549 * We got interrupted, so perhaps we didn't
1550 * manage to finish a line we were printing.
1551 * Print an extra newline, just in case.
1555 (void)fflush(stdout
);
1559 * We got interrupted. If we are reading multiple
1560 * files (via -V) set these so that we stop.
1569 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
1570 program_name
, pcap_geterr(pd
));
1572 if (RFileName
== NULL
) {
1574 * We're doing a live capture. Report the capture
1580 if (VFileName
!= NULL
) {
1581 ret
= get_next_file(VFile
, VFileLine
);
1583 RFileName
= VFileLine
;
1584 pd
= pcap_open_offline(RFileName
, ebuf
);
1587 new_dlt
= pcap_datalink(pd
);
1588 if (WFileName
&& new_dlt
!= dlt
)
1589 error("%s: new dlt does not match original", RFileName
);
1590 printinfo
= get_print_info(new_dlt
);
1591 dlt_name
= pcap_datalink_val_to_name(new_dlt
);
1592 if (dlt_name
== NULL
) {
1593 fprintf(stderr
, "reading from file %s, link-type %u\n",
1594 RFileName
, new_dlt
);
1597 "reading from file %s, link-type %s (%s)\n",
1598 RFileName
, dlt_name
,
1599 pcap_datalink_val_to_description(new_dlt
));
1601 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
1602 error("%s", pcap_geterr(pd
));
1603 if (pcap_setfilter(pd
, &fcode
) < 0)
1604 error("%s", pcap_geterr(pd
));
1608 while (ret
!= NULL
);
1611 exit(status
== -1 ? 1 : 0);
1614 /* make a clean exit on interrupts */
1616 cleanup(int signo _U_
)
1618 #ifdef USE_WIN32_MM_TIMER
1620 timeKillEvent(timer_id
);
1622 #elif defined(HAVE_ALARM)
1626 #ifdef HAVE_PCAP_BREAKLOOP
1628 * We have "pcap_breakloop()"; use it, so that we do as little
1629 * as possible in the signal handler (it's probably not safe
1630 * to do anything with standard I/O streams in a signal handler -
1631 * the ANSI C standard doesn't say it is).
1636 * We don't have "pcap_breakloop()"; this isn't safe, but
1637 * it's the best we can do. Print the summary if we're
1638 * not reading from a savefile - i.e., if we're doing a
1639 * live capture - and exit.
1641 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
1643 * We got interrupted, so perhaps we didn't
1644 * manage to finish a line we were printing.
1645 * Print an extra newline, just in case.
1648 (void)fflush(stdout
);
1656 On windows, we do not use a fork, so we do not care less about
1657 waiting a child processes to die
1659 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1661 child_cleanup(int signo _U_
)
1665 #endif /* HAVE_FORK && HAVE_VFORK */
1668 info(register int verbose
)
1670 struct pcap_stat stat
;
1673 * Older versions of libpcap didn't set ps_ifdrop on some
1674 * platforms; initialize it to 0 to handle that.
1677 if (pcap_stats(pd
, &stat
) < 0) {
1678 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
1684 fprintf(stderr
, "%s: ", program_name
);
1686 (void)fprintf(stderr
, "%u packet%s captured", packets_captured
,
1687 PLURAL_SUFFIX(packets_captured
));
1689 fputs(", ", stderr
);
1692 (void)fprintf(stderr
, "%u packet%s received by filter", stat
.ps_recv
,
1693 PLURAL_SUFFIX(stat
.ps_recv
));
1695 fputs(", ", stderr
);
1698 (void)fprintf(stderr
, "%u packet%s dropped by kernel", stat
.ps_drop
,
1699 PLURAL_SUFFIX(stat
.ps_drop
));
1700 if (stat
.ps_ifdrop
!= 0) {
1702 fputs(", ", stderr
);
1705 (void)fprintf(stderr
, "%u packet%s dropped by interface\n",
1706 stat
.ps_ifdrop
, PLURAL_SUFFIX(stat
.ps_ifdrop
));
1712 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1714 compress_savefile(const char *filename
)
1723 * Set to lowest priority so that this doesn't disturb the capture
1726 setpriority(PRIO_PROCESS
, 0, NZERO
- 1);
1728 setpriority(PRIO_PROCESS
, 0, 19);
1730 if (execlp(zflag
, zflag
, filename
, (char *)NULL
) == -1)
1732 "compress_savefile:execlp(%s, %s): %s\n",
1742 #else /* HAVE_FORK && HAVE_VFORK */
1744 compress_savefile(const char *filename
)
1747 "compress_savefile failed. Functionality not implemented under your system\n");
1749 #endif /* HAVE_FORK && HAVE_VFORK */
1752 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1754 struct dump_info
*dump_info
;
1760 dump_info
= (struct dump_info
*)user
;
1763 * XXX - this won't force the file to rotate on the specified time
1764 * boundary, but it will rotate on the first packet received after the
1765 * specified Gflag number of seconds. Note: if a Gflag time boundary
1766 * and a Cflag size boundary coincide, the time rotation will occur
1767 * first thereby cancelling the Cflag boundary (since the file should
1771 /* Check if it is time to rotate */
1774 /* Get the current time */
1775 if ((t
= time(NULL
)) == (time_t)-1) {
1776 error("dump_and_trunc_packet: can't get current_time: %s",
1777 pcap_strerror(errno
));
1781 /* If the time is greater than the specified window, rotate */
1782 if (t
- Gflag_time
>= Gflag
) {
1783 /* Update the Gflag_time */
1785 /* Update Gflag_count */
1788 * Close the current file and open a new one.
1790 pcap_dump_close(dump_info
->p
);
1793 * Compress the file we just closed, if the user asked for it
1796 compress_savefile(dump_info
->CurrentFileName
);
1799 * Check to see if we've exceeded the Wflag (when
1802 if (Cflag
== 0 && Wflag
> 0 && Gflag_count
>= Wflag
) {
1803 (void)fprintf(stderr
, "Maximum file limit reached: %d\n",
1808 if (dump_info
->CurrentFileName
!= NULL
)
1809 free(dump_info
->CurrentFileName
);
1810 /* Allocate space for max filename + \0. */
1811 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
1812 if (dump_info
->CurrentFileName
== NULL
)
1813 error("dump_packet_and_trunc: malloc");
1815 * This is always the first file in the Cflag
1817 * We also don't need numbering if Cflag is not set.
1820 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0,
1823 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0, 0);
1825 #ifdef HAVE_CAP_NG_H
1826 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
1827 capng_apply(CAPNG_EFFECTIVE
);
1828 #endif /* HAVE_CAP_NG_H */
1829 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
1830 #ifdef HAVE_CAP_NG_H
1831 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
1832 capng_apply(CAPNG_EFFECTIVE
);
1833 #endif /* HAVE_CAP_NG_H */
1834 if (dump_info
->p
== NULL
)
1835 error("%s", pcap_geterr(pd
));
1840 * XXX - this won't prevent capture files from getting
1841 * larger than Cflag - the last packet written to the
1842 * file could put it over Cflag.
1844 if (Cflag
!= 0 && pcap_dump_ftell(dump_info
->p
) > Cflag
) {
1846 * Close the current file and open a new one.
1848 pcap_dump_close(dump_info
->p
);
1851 * Compress the file we just closed, if the user asked for it
1854 compress_savefile(dump_info
->CurrentFileName
);
1858 if (Cflag_count
>= Wflag
)
1861 if (dump_info
->CurrentFileName
!= NULL
)
1862 free(dump_info
->CurrentFileName
);
1863 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
1864 if (dump_info
->CurrentFileName
== NULL
)
1865 error("dump_packet_and_trunc: malloc");
1866 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, Cflag_count
, WflagChars
);
1867 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
1868 if (dump_info
->p
== NULL
)
1869 error("%s", pcap_geterr(pd
));
1872 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
1873 #ifdef HAVE_PCAP_DUMP_FLUSH
1875 pcap_dump_flush(dump_info
->p
);
1884 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1890 pcap_dump(user
, h
, sp
);
1891 #ifdef HAVE_PCAP_DUMP_FLUSH
1893 pcap_dump_flush((pcap_dumper_t
*)user
);
1902 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1904 struct print_info
*print_info
;
1912 print_info
= (struct print_info
*)user
;
1915 * Some printers want to check that they're not walking off the
1916 * end of the packet.
1917 * Rather than pass it all the way down, we set this global.
1919 snapend
= sp
+ h
->caplen
;
1921 if(print_info
->ndo_type
) {
1922 hdrlen
= (*print_info
->p
.ndo_printer
)(print_info
->ndo
, h
, sp
);
1924 hdrlen
= (*print_info
->p
.printer
)(h
, sp
);
1929 * Print the raw packet data in hex and ASCII.
1933 * Include the link-layer header.
1935 hex_and_ascii_print("\n\t", sp
, h
->caplen
);
1938 * Don't include the link-layer header - and if
1939 * we have nothing past the link-layer header,
1942 if (h
->caplen
> hdrlen
)
1943 hex_and_ascii_print("\n\t", sp
+ hdrlen
,
1944 h
->caplen
- hdrlen
);
1948 * Print the raw packet data in hex.
1952 * Include the link-layer header.
1954 hex_print("\n\t", sp
, h
->caplen
);
1957 * Don't include the link-layer header - and if
1958 * we have nothing past the link-layer header,
1961 if (h
->caplen
> hdrlen
)
1962 hex_print("\n\t", sp
+ hdrlen
,
1963 h
->caplen
- hdrlen
);
1967 * Print the raw packet data in ASCII.
1971 * Include the link-layer header.
1973 ascii_print(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 ascii_print(sp
+ hdrlen
, h
->caplen
- hdrlen
);
1994 * XXX - there should really be libpcap calls to get the version
1995 * number as a string (the string would be generated from #defines
1996 * at run time, so that it's not generated from string constants
1997 * in the library, as, on many UNIX systems, those constants would
1998 * be statically linked into the application executable image, and
1999 * would thus reflect the version of libpcap on the system on
2000 * which the application was *linked*, not the system on which it's
2003 * That routine should be documented, unlike the "version[]"
2004 * string, so that UNIX vendors providing their own libpcaps
2005 * don't omit it (as a couple of vendors have...).
2007 * Packet.dll should perhaps also export a routine to return the
2008 * version number of the Packet.dll code, to supply the
2009 * "Wpcap_version" information on Windows.
2011 char WDversion
[]="current-cvs.tcpdump.org";
2012 #if !defined(HAVE_GENERATED_VERSION)
2013 char version
[]="current-cvs.tcpdump.org";
2015 char pcap_version
[]="current-cvs.tcpdump.org";
2016 char Wpcap_version
[]="3.1";
2020 * By default, print the specified data out in hex and ASCII.
2023 ndo_default_print(netdissect_options
*ndo _U_
, const u_char
*bp
, u_int length
)
2025 hex_and_ascii_print("\n\t", bp
, length
); /* pass on lf and identation string */
2029 default_print(const u_char
*bp
, u_int length
)
2031 ndo_default_print(gndo
, bp
, length
);
2034 #ifdef SIGNAL_REQ_INFO
2035 RETSIGTYPE
requestinfo(int signo _U_
)
2045 * Called once each second in verbose mode while dumping to file
2047 #ifdef USE_WIN32_MM_TIMER
2048 void CALLBACK
verbose_stats_dump (UINT timer_id _U_
, UINT msg _U_
, DWORD_PTR arg _U_
,
2049 DWORD_PTR dw1 _U_
, DWORD_PTR dw2 _U_
)
2051 struct pcap_stat stat
;
2053 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
2054 fprintf(stderr
, "Got %u\r", packets_captured
);
2056 #elif defined(HAVE_ALARM)
2057 static void verbose_stats_dump(int sig _U_
)
2059 struct pcap_stat stat
;
2061 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
2062 fprintf(stderr
, "Got %u\r", packets_captured
);
2070 extern char version
[];
2071 #ifndef HAVE_PCAP_LIB_VERSION
2072 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
2073 extern char pcap_version
[];
2074 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2075 static char pcap_version
[] = "unknown";
2076 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2077 #endif /* HAVE_PCAP_LIB_VERSION */
2079 #ifdef HAVE_PCAP_LIB_VERSION
2081 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2083 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2085 (void)fprintf(stderr
, "%s\n",pcap_lib_version());
2086 #else /* HAVE_PCAP_LIB_VERSION */
2088 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2089 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
2091 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2092 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
2094 #endif /* HAVE_PCAP_LIB_VERSION */
2095 (void)fprintf(stderr
,
2096 "Usage: %s [-aAbd" D_FLAG
"efhH" I_FLAG J_FLAG
"KlLnNOpqRStu" U_FLAG
"vxX]" B_FLAG_USAGE
" [ -c count ]\n", program_name
);
2097 (void)fprintf(stderr
,
2098 "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n");
2099 (void)fprintf(stderr
,
2100 "\t\t[ -i interface ]" j_FLAG_USAGE
" [ -M secret ]\n");
2101 (void)fprintf(stderr
,
2102 "\t\t[ -r file ] [ -s snaplen ] [ -T type ] [ -V file ] [ -w file ]\n");
2103 (void)fprintf(stderr
,
2104 "\t\t[ -W filecount ] [ -y datalinktype ] [ -z command ]\n");
2105 (void)fprintf(stderr
,
2106 "\t\t[ -Z user ] [ expression ]\n");
2114 ndo_error(netdissect_options
*ndo _U_
, const char *fmt
, ...)
2118 (void)fprintf(stderr
, "%s: ", program_name
);
2120 (void)vfprintf(stderr
, fmt
, ap
);
2124 if (fmt
[-1] != '\n')
2125 (void)fputc('\n', stderr
);
2133 ndo_warning(netdissect_options
*ndo _U_
, const char *fmt
, ...)
2137 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
2139 (void)vfprintf(stderr
, fmt
, ap
);
2143 if (fmt
[-1] != '\n')
2144 (void)fputc('\n', stderr
);