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
},
349 static const struct tok status_flags
[] = {
351 { PCAP_IF_UP
, "Up" },
353 #ifdef PCAP_IF_RUNNING
354 { PCAP_IF_RUNNING
, "Running" },
356 { PCAP_IF_LOOPBACK
, "Loopback" },
361 lookup_printer(int type
)
363 const struct printer
*p
;
365 for (p
= printers
; p
->f
; ++p
)
374 lookup_ndo_printer(int type
)
376 const struct ndo_printer
*p
;
378 for (p
= ndo_printers
; p
->f
; ++p
)
388 static int supports_monitor_mode
;
395 netdissect_options
*ndo
;
398 if_ndo_printer ndo_printer
;
405 char *CurrentFileName
;
410 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
412 show_tstamp_types_and_exit(const char *device
, pcap_t
*pd
)
415 int *tstamp_types
= 0;
416 const char *tstamp_type_name
;
419 n_tstamp_types
= pcap_list_tstamp_types(pd
, &tstamp_types
);
420 if (n_tstamp_types
< 0)
421 error("%s", pcap_geterr(pd
));
423 if (n_tstamp_types
== 0) {
424 fprintf(stderr
, "Time stamp type cannot be set for %s\n",
428 fprintf(stderr
, "Time stamp types for %s (use option -j to set):\n",
430 for (i
= 0; i
< n_tstamp_types
; i
++) {
431 tstamp_type_name
= pcap_tstamp_type_val_to_name(tstamp_types
[i
]);
432 if (tstamp_type_name
!= NULL
) {
433 (void) fprintf(stderr
, " %s (%s)\n", tstamp_type_name
,
434 pcap_tstamp_type_val_to_description(tstamp_types
[i
]));
436 (void) fprintf(stderr
, " %d\n", tstamp_types
[i
]);
439 pcap_free_tstamp_types(tstamp_types
);
445 show_dlts_and_exit(const char *device
, pcap_t
*pd
)
449 const char *dlt_name
;
451 n_dlts
= pcap_list_datalinks(pd
, &dlts
);
453 error("%s", pcap_geterr(pd
));
454 else if (n_dlts
== 0 || !dlts
)
455 error("No data link types.");
458 * If the interface is known to support monitor mode, indicate
459 * whether these are the data link types available when not in
460 * monitor mode, if -I wasn't specified, or when in monitor mode,
461 * when -I was specified (the link-layer types available in
462 * monitor mode might be different from the ones available when
463 * not in monitor mode).
465 if (supports_monitor_mode
)
466 (void) fprintf(stderr
, "Data link types for %s %s (use option -y to set):\n",
468 Iflag
? "when in monitor mode" : "when not in monitor mode");
470 (void) fprintf(stderr
, "Data link types for %s (use option -y to set):\n",
473 while (--n_dlts
>= 0) {
474 dlt_name
= pcap_datalink_val_to_name(dlts
[n_dlts
]);
475 if (dlt_name
!= NULL
) {
476 (void) fprintf(stderr
, " %s (%s)", dlt_name
,
477 pcap_datalink_val_to_description(dlts
[n_dlts
]));
480 * OK, does tcpdump handle that type?
482 if (lookup_printer(dlts
[n_dlts
]) == NULL
483 && lookup_ndo_printer(dlts
[n_dlts
]) == NULL
)
484 (void) fprintf(stderr
, " (printing not supported)");
485 fprintf(stderr
, "\n");
487 (void) fprintf(stderr
, " DLT %d (printing not supported)\n",
491 #ifdef HAVE_PCAP_FREE_DATALINKS
492 pcap_free_datalinks(dlts
);
498 * Set up flags that might or might not be supported depending on the
499 * version of libpcap we're using.
501 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
503 #define B_FLAG_USAGE " [ -B size ]"
504 #else /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
507 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
509 #ifdef HAVE_PCAP_CREATE
511 #else /* HAVE_PCAP_CREATE */
513 #endif /* HAVE_PCAP_CREATE */
515 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
517 #define j_FLAG_USAGE " [ -j tstamptype ]"
519 #else /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
523 #endif /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
525 #ifdef HAVE_PCAP_FINDALLDEVS
526 #ifndef HAVE_PCAP_IF_T
527 #undef HAVE_PCAP_FINDALLDEVS
531 #ifdef HAVE_PCAP_FINDALLDEVS
537 #ifdef HAVE_PCAP_DUMP_FLUSH
543 #ifdef HAVE_PCAP_SETDIRECTION
550 /* Drop root privileges and chroot if necessary */
552 droproot(const char *username
, const char *chroot_dir
)
554 struct passwd
*pw
= NULL
;
556 if (chroot_dir
&& !username
) {
557 fprintf(stderr
, "tcpdump: Chroot without dropping root is insecure\n");
561 pw
= getpwnam(username
);
564 if (chroot(chroot_dir
) != 0 || chdir ("/") != 0) {
565 fprintf(stderr
, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
566 chroot_dir
, pcap_strerror(errno
));
571 int ret
= capng_change_id(pw
->pw_uid
, pw
->pw_gid
, CAPNG_NO_FLAG
);
573 printf("error : ret %d\n", ret
);
575 /* We don't need CAP_SETUID and CAP_SETGID */
576 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_SETUID
);
577 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_SETUID
);
578 capng_update(CAPNG_DROP
, CAPNG_PERMITTED
, CAP_SETUID
);
579 capng_update(CAPNG_DROP
, CAPNG_PERMITTED
, CAP_SETUID
);
580 capng_apply(CAPNG_SELECT_BOTH
);
583 if (initgroups(pw
->pw_name
, pw
->pw_gid
) != 0 ||
584 setgid(pw
->pw_gid
) != 0 || setuid(pw
->pw_uid
) != 0) {
585 fprintf(stderr
, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
587 (unsigned long)pw
->pw_uid
,
588 (unsigned long)pw
->pw_gid
,
589 pcap_strerror(errno
));
592 #endif /* HAVE_CAP_NG_H */
595 fprintf(stderr
, "tcpdump: Couldn't find user '%.32s'\n",
618 MakeFilename(char *buffer
, char *orig_name
, int cnt
, int max_chars
)
620 char *filename
= malloc(PATH_MAX
+ 1);
621 if (filename
== NULL
)
622 error("Makefilename: malloc");
624 /* Process with strftime if Gflag is set. */
628 /* Convert Gflag_time to a usable format */
629 if ((local_tm
= localtime(&Gflag_time
)) == NULL
) {
630 error("MakeTimedFilename: localtime");
633 /* There's no good way to detect an error in strftime since a return
634 * value of 0 isn't necessarily failure.
636 strftime(filename
, PATH_MAX
, orig_name
, local_tm
);
638 strncpy(filename
, orig_name
, PATH_MAX
);
641 if (cnt
== 0 && max_chars
== 0)
642 strncpy(buffer
, filename
, PATH_MAX
+ 1);
644 if (snprintf(buffer
, PATH_MAX
+ 1, "%s%0*d", filename
, max_chars
, cnt
) > PATH_MAX
)
645 /* Report an error if the filename is too large */
646 error("too many output files or filename is too long (> %d)", PATH_MAX
);
650 static int tcpdump_printf(netdissect_options
*ndo _U_
,
651 const char *fmt
, ...)
658 ret
=vfprintf(stdout
, fmt
, args
);
664 static struct print_info
665 get_print_info(int type
)
667 struct print_info printinfo
;
669 printinfo
.ndo_type
= 1;
670 printinfo
.ndo
= gndo
;
671 printinfo
.p
.ndo_printer
= lookup_ndo_printer(type
);
672 if (printinfo
.p
.ndo_printer
== NULL
) {
673 printinfo
.p
.printer
= lookup_printer(type
);
674 printinfo
.ndo_type
= 0;
675 if (printinfo
.p
.printer
== NULL
) {
676 gndo
->ndo_dltname
= pcap_datalink_val_to_name(type
);
677 if (gndo
->ndo_dltname
!= NULL
)
678 error("packet printing is not supported for link type %s: use -w",
681 error("packet printing is not supported for link type %d: use -w", type
);
688 get_next_file(FILE *VFile
, char *ptr
)
692 ret
= fgets(ptr
, PATH_MAX
, VFile
);
696 if (ptr
[strlen(ptr
) - 1] == '\n')
697 ptr
[strlen(ptr
) - 1] = '\0';
703 main(int argc
, char **argv
)
705 register int cnt
, op
, i
;
706 bpf_u_int32 localnet
, netmask
;
707 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *VFileName
, *WFileName
;
708 pcap_handler callback
;
712 const char *dlt_name
;
713 struct bpf_program fcode
;
715 RETSIGTYPE (*oldhandler
)(int);
717 struct print_info printinfo
;
718 struct dump_info dumpinfo
;
719 u_char
*pcap_userdata
;
720 char ebuf
[PCAP_ERRBUF_SIZE
];
721 char VFileLine
[PATH_MAX
+ 1];
722 char *username
= NULL
;
723 char *chroot_dir
= NULL
;
726 #ifdef HAVE_PCAP_FINDALLDEVS
727 pcap_if_t
*devpointer
;
733 if(wsockinit() != 0) return 1;
736 jflag
=-1; /* not set */
740 gndo
->ndo_default_print
=ndo_default_print
;
741 gndo
->ndo_printf
=tcpdump_printf
;
742 gndo
->ndo_error
=ndo_error
;
743 gndo
->ndo_warning
=ndo_warning
;
744 gndo
->ndo_snaplen
= DEFAULT_SNAPLEN
;
754 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
755 program_name
= cp
+ 1;
757 program_name
= argv
[0];
759 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
767 (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)
771 /* compatibility for old -a */
782 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
784 Bflag
= atoi(optarg
)*1024;
786 error("invalid packet buffer size %s", optarg
);
788 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
793 error("invalid packet count %s", optarg
);
797 Cflag
= atoi(optarg
) * 1000000;
799 error("invalid file size %s", optarg
);
806 #ifdef HAVE_PCAP_FINDALLDEVS
808 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
811 for (i
= 0; devpointer
!= NULL
; i
++) {
812 printf("%d.%s", i
+1, devpointer
->name
);
813 if (devpointer
->description
!= NULL
)
814 printf(" (%s)", devpointer
->description
);
815 if (devpointer
->flags
!= 0)
816 printf(" [%s]", bittok2str(status_flags
, "none", devpointer
->flags
));
818 devpointer
= devpointer
->next
;
822 #endif /* HAVE_PCAP_FINDALLDEVS */
833 #ifndef HAVE_LIBCRYPTO
834 warning("crypto code not compiled in");
836 gndo
->ndo_espsecret
= optarg
;
848 Gflag
= atoi(optarg
);
850 error("invalid number of seconds %s", optarg
);
852 /* We will create one file initially. */
855 /* Grab the current time for rotation use. */
856 if ((Gflag_time
= time(NULL
)) == (time_t)-1) {
857 error("main: can't get current time: %s",
858 pcap_strerror(errno
));
871 if (optarg
[0] == '0' && optarg
[1] == 0)
872 error("Invalid adapter index");
874 #ifdef HAVE_PCAP_FINDALLDEVS
876 * If the argument is a number, treat it as
877 * an index into the list of adapters, as
878 * printed by "tcpdump -D".
880 * This should be OK on UNIX systems, as interfaces
881 * shouldn't have names that begin with digits.
882 * It can be useful on Windows, where more than
883 * one interface can have the same name.
885 devnum
= strtol(optarg
, &end
, 10);
886 if (optarg
!= end
&& *end
== '\0') {
888 error("Invalid adapter index");
890 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
894 * Look for the devnum-th entry
895 * in the list of devices
899 i
< devnum
-1 && devpointer
!= NULL
;
900 i
++, devpointer
= devpointer
->next
)
902 if (devpointer
== NULL
)
903 error("Invalid adapter index");
905 device
= devpointer
->name
;
908 #endif /* HAVE_PCAP_FINDALLDEVS */
912 #ifdef HAVE_PCAP_CREATE
916 #endif /* HAVE_PCAP_CREATE */
918 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
920 jflag
= pcap_tstamp_type_name_to_val(optarg
);
922 error("invalid time stamp type %s", optarg
);
933 * _IOLBF is the same as _IOFBF in Microsoft's C
934 * libraries; the only alternative they offer
937 * XXX - this should really be checking for MSVC++,
938 * not WIN32, if, for example, MinGW has its own
939 * C library that is more UNIX-compatible.
941 setvbuf(stdout
, NULL
, _IONBF
, 0);
943 #ifdef HAVE_SETLINEBUF
946 setvbuf(stdout
, NULL
, _IOLBF
, 0);
957 if (smiLoadModule(optarg
) == 0) {
958 error("could not load MIB module %s", optarg
);
962 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
963 program_name
, optarg
);
964 (void)fprintf(stderr
, "(no libsmi support)\n");
969 /* TCP-MD5 shared secret */
970 #ifndef HAVE_LIBCRYPTO
971 warning("crypto code not compiled in");
994 ++suppress_default_print
;
997 #ifdef HAVE_PCAP_SETDIRECTION
999 if (strcasecmp(optarg
, "in") == 0)
1001 else if (strcasecmp(optarg
, "out") == 0)
1003 else if (strcasecmp(optarg
, "inout") == 0)
1004 Qflag
= PCAP_D_INOUT
;
1006 error("unknown capture direction `%s'", optarg
);
1008 #endif /* HAVE_PCAP_SETDIRECTION */
1019 snaplen
= strtol(optarg
, &end
, 0);
1020 if (optarg
== end
|| *end
!= '\0'
1021 || snaplen
< 0 || snaplen
> MAXIMUM_SNAPLEN
)
1022 error("invalid snaplen %s", optarg
);
1023 else if (snaplen
== 0)
1024 snaplen
= MAXIMUM_SNAPLEN
;
1036 if (strcasecmp(optarg
, "vat") == 0)
1037 packettype
= PT_VAT
;
1038 else if (strcasecmp(optarg
, "wb") == 0)
1040 else if (strcasecmp(optarg
, "rpc") == 0)
1041 packettype
= PT_RPC
;
1042 else if (strcasecmp(optarg
, "rtp") == 0)
1043 packettype
= PT_RTP
;
1044 else if (strcasecmp(optarg
, "rtcp") == 0)
1045 packettype
= PT_RTCP
;
1046 else if (strcasecmp(optarg
, "snmp") == 0)
1047 packettype
= PT_SNMP
;
1048 else if (strcasecmp(optarg
, "cnfp") == 0)
1049 packettype
= PT_CNFP
;
1050 else if (strcasecmp(optarg
, "tftp") == 0)
1051 packettype
= PT_TFTP
;
1052 else if (strcasecmp(optarg
, "aodv") == 0)
1053 packettype
= PT_AODV
;
1054 else if (strcasecmp(optarg
, "carp") == 0)
1055 packettype
= PT_CARP
;
1056 else if (strcasecmp(optarg
, "radius") == 0)
1057 packettype
= PT_RADIUS
;
1058 else if (strcasecmp(optarg
, "zmtp1") == 0)
1059 packettype
= PT_ZMTP1
;
1060 else if (strcasecmp(optarg
, "vxlan") == 0)
1061 packettype
= PT_VXLAN
;
1062 else if (strcasecmp(optarg
, "pgm") == 0)
1063 packettype
= PT_PGM
;
1064 else if (strcasecmp(optarg
, "pgm_zmtp1") == 0)
1065 packettype
= PT_PGM_ZMTP1
;
1066 else if (strcasecmp(optarg
, "lmp") == 0)
1067 packettype
= PT_LMP
;
1069 error("unknown packet type `%s'", optarg
);
1076 #ifdef HAVE_PCAP_DUMP_FLUSH
1095 Wflag
= atoi(optarg
);
1097 error("invalid number of output files %s", optarg
);
1098 WflagChars
= getWflagChars(Wflag
);
1103 ++suppress_default_print
;
1108 ++suppress_default_print
;
1112 gndo
->ndo_dltname
= optarg
;
1114 pcap_datalink_name_to_val(gndo
->ndo_dltname
);
1115 if (gndo
->ndo_dlt
< 0)
1116 error("invalid data link type %s", gndo
->ndo_dltname
);
1119 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
1122 /* Undocumented flag */
1123 #ifdef HAVE_PCAP_DEBUG
1124 extern int pcap_debug
;
1135 zflag
= strdup(optarg
);
1144 username
= strdup(optarg
);
1159 case 0: /* Default */
1160 case 4: /* Default + Date*/
1161 thiszone
= gmt2local(0);
1164 case 1: /* No time stamp */
1165 case 2: /* Unix timeval style */
1166 case 3: /* Microseconds since previous packet */
1167 case 5: /* Microseconds since first packet */
1170 default: /* Not supported */
1171 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
1175 if (fflag
!= 0 && (VFileName
!= NULL
|| RFileName
!= NULL
))
1176 error("-f can not be used with -V or -r");
1178 if (VFileName
!= NULL
&& RFileName
!= NULL
)
1179 error("-V and -r are mutually exclusive.");
1182 /* if run as root, prepare for chrooting */
1183 if (getuid() == 0 || geteuid() == 0) {
1184 /* future extensibility for cmd-line arguments */
1186 chroot_dir
= WITH_CHROOT
;
1191 /* if run as root, prepare for dropping root privileges */
1192 if (getuid() == 0 || geteuid() == 0) {
1193 /* Run with '-Z root' to restore old behaviour */
1195 username
= WITH_USER
;
1199 if (RFileName
!= NULL
|| VFileName
!= NULL
) {
1201 * If RFileName is non-null, it's the pathname of a
1202 * savefile to read. If VFileName is non-null, it's
1203 * the pathname of a file containing a list of pathnames
1204 * (one per line) of savefiles to read.
1206 * In either case, we're reading a savefile, not doing
1211 * We don't need network access, so relinquish any set-UID
1212 * or set-GID privileges we have (if any).
1214 * We do *not* want set-UID privileges when opening a
1215 * trace file, as that might let the user read other
1216 * people's trace files (especially if we're set-UID
1219 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
1220 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1222 if (VFileName
!= NULL
) {
1223 if (VFileName
[0] == '-' && VFileName
[1] == '\0')
1226 VFile
= fopen(VFileName
, "r");
1229 error("Unable to open file: %s\n", strerror(errno
));
1231 ret
= get_next_file(VFile
, VFileLine
);
1233 error("Nothing in %s\n", VFileName
);
1234 RFileName
= VFileLine
;
1237 pd
= pcap_open_offline(RFileName
, ebuf
);
1240 dlt
= pcap_datalink(pd
);
1241 dlt_name
= pcap_datalink_val_to_name(dlt
);
1242 if (dlt_name
== NULL
) {
1243 fprintf(stderr
, "reading from file %s, link-type %u\n",
1247 "reading from file %s, link-type %s (%s)\n",
1248 RFileName
, dlt_name
,
1249 pcap_datalink_val_to_description(dlt
));
1255 * We're doing a live capture.
1257 if (device
== NULL
) {
1258 device
= pcap_lookupdev(ebuf
);
1264 * Print a message to the standard error on Windows.
1265 * XXX - why do it here, with a different message?
1267 if(strlen(device
) == 1) /* we assume that an ASCII string is always longer than 1 char */
1268 { /* a Unicode string has a \0 as second byte (so strlen() is 1) */
1269 fprintf(stderr
, "%s: listening on %ws\n", program_name
, device
);
1273 fprintf(stderr
, "%s: listening on %s\n", program_name
, device
);
1278 #ifdef HAVE_PCAP_CREATE
1279 pd
= pcap_create(device
, ebuf
);
1282 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1284 show_tstamp_types_and_exit(device
, pd
);
1287 * Is this an interface that supports monitor mode?
1289 if (pcap_can_set_rfmon(pd
) == 1)
1290 supports_monitor_mode
= 1;
1292 supports_monitor_mode
= 0;
1293 status
= pcap_set_snaplen(pd
, snaplen
);
1295 error("%s: Can't set snapshot length: %s",
1296 device
, pcap_statustostr(status
));
1297 status
= pcap_set_promisc(pd
, !pflag
);
1299 error("%s: Can't set promiscuous mode: %s",
1300 device
, pcap_statustostr(status
));
1302 status
= pcap_set_rfmon(pd
, 1);
1304 error("%s: Can't set monitor mode: %s",
1305 device
, pcap_statustostr(status
));
1307 status
= pcap_set_timeout(pd
, 1000);
1309 error("%s: pcap_set_timeout failed: %s",
1310 device
, pcap_statustostr(status
));
1312 status
= pcap_set_buffer_size(pd
, Bflag
);
1314 error("%s: Can't set buffer size: %s",
1315 device
, pcap_statustostr(status
));
1317 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1319 status
= pcap_set_tstamp_type(pd
, jflag
);
1321 error("%s: Can't set time stamp type: %s",
1322 device
, pcap_statustostr(status
));
1325 status
= pcap_activate(pd
);
1328 * pcap_activate() failed.
1330 cp
= pcap_geterr(pd
);
1331 if (status
== PCAP_ERROR
)
1333 else if ((status
== PCAP_ERROR_NO_SUCH_DEVICE
||
1334 status
== PCAP_ERROR_PERM_DENIED
) &&
1336 error("%s: %s\n(%s)", device
,
1337 pcap_statustostr(status
), cp
);
1339 error("%s: %s", device
,
1340 pcap_statustostr(status
));
1341 } else if (status
> 0) {
1343 * pcap_activate() succeeded, but it's warning us
1344 * of a problem it had.
1346 cp
= pcap_geterr(pd
);
1347 if (status
== PCAP_WARNING
)
1349 else if (status
== PCAP_WARNING_PROMISC_NOTSUP
&&
1351 warning("%s: %s\n(%s)", device
,
1352 pcap_statustostr(status
), cp
);
1354 warning("%s: %s", device
,
1355 pcap_statustostr(status
));
1357 #ifdef HAVE_PCAP_SETDIRECTION
1359 status
= pcap_setdirection(pd
, Qflag
);
1361 error("%s: pcap_setdirection() failed: %s",
1362 device
, pcap_geterr(pd
));
1364 #endif /* HAVE_PCAP_SETDIRECTION */
1367 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
1371 warning("%s", ebuf
);
1372 #endif /* HAVE_PCAP_CREATE */
1374 * Let user own process after socket has been opened.
1377 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
1378 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1380 #if !defined(HAVE_PCAP_CREATE) && defined(WIN32)
1382 if(pcap_setbuff(pd
, Bflag
)==-1){
1383 error("%s", pcap_geterr(pd
));
1385 #endif /* !defined(HAVE_PCAP_CREATE) && defined(WIN32) */
1387 show_dlts_and_exit(device
, pd
);
1388 if (gndo
->ndo_dlt
>= 0) {
1389 #ifdef HAVE_PCAP_SET_DATALINK
1390 if (pcap_set_datalink(pd
, gndo
->ndo_dlt
) < 0)
1391 error("%s", pcap_geterr(pd
));
1394 * We don't actually support changing the
1395 * data link type, so we only let them
1396 * set it to what it already is.
1398 if (gndo
->ndo_dlt
!= pcap_datalink(pd
)) {
1399 error("%s is not one of the DLTs supported by this device\n",
1403 (void)fprintf(stderr
, "%s: data link type %s\n",
1404 program_name
, gndo
->ndo_dltname
);
1405 (void)fflush(stderr
);
1407 i
= pcap_snapshot(pd
);
1409 warning("snaplen raised from %d to %d", snaplen
, i
);
1412 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
1415 warning("%s", ebuf
);
1419 cmdbuf
= read_infile(infile
);
1421 cmdbuf
= copy_argv(&argv
[optind
]);
1423 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
1424 error("%s", pcap_geterr(pd
));
1426 bpf_dump(&fcode
, dflag
);
1431 init_addrtoname(localnet
, netmask
);
1435 (void)setsignal(SIGPIPE
, cleanup
);
1436 (void)setsignal(SIGTERM
, cleanup
);
1437 (void)setsignal(SIGINT
, cleanup
);
1439 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1440 (void)setsignal(SIGCHLD
, child_cleanup
);
1442 /* Cooperate with nohup(1) */
1444 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
1445 (void)setsignal(SIGHUP
, oldhandler
);
1450 * If a user name was specified with "-Z", attempt to switch to
1451 * that user's UID. This would probably be used with sudo,
1452 * to allow tcpdump to be run in a special restricted
1453 * account (if you just want to allow users to open capture
1454 * devices, and can't just give users that permission,
1455 * you'd make tcpdump set-UID or set-GID).
1457 * Tcpdump doesn't necessarily write only to one savefile;
1458 * the general only way to allow a -Z instance to write to
1459 * savefiles as the user under whose UID it's run, rather
1460 * than as the user specified with -Z, would thus be to switch
1461 * to the original user ID before opening a capture file and
1462 * then switch back to the -Z user ID after opening the savefile.
1463 * Switching to the -Z user ID only after opening the first
1464 * savefile doesn't handle the general case.
1467 #ifdef HAVE_CAP_NG_H
1468 /* We are running as root and we will be writing to savefile */
1469 if ((getuid() == 0 || geteuid() == 0) && WFileName
) {
1471 /* Drop all capabilities from effective set */
1472 capng_clear(CAPNG_EFFECTIVE
);
1473 /* Add capabilities we will need*/
1474 capng_update(CAPNG_ADD
, CAPNG_PERMITTED
, CAP_SETUID
);
1475 capng_update(CAPNG_ADD
, CAPNG_PERMITTED
, CAP_SETGID
);
1476 capng_update(CAPNG_ADD
, CAPNG_PERMITTED
, CAP_DAC_OVERRIDE
);
1478 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_SETUID
);
1479 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_SETGID
);
1480 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
1482 capng_apply(CAPNG_SELECT_BOTH
);
1485 #endif /* HAVE_CAP_NG_H */
1487 if (getuid() == 0 || geteuid() == 0) {
1488 if (username
|| chroot_dir
)
1489 droproot(username
, chroot_dir
);
1494 if (pcap_setfilter(pd
, &fcode
) < 0)
1495 error("%s", pcap_geterr(pd
));
1498 /* Do not exceed the default PATH_MAX for files. */
1499 dumpinfo
.CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
1501 if (dumpinfo
.CurrentFileName
== NULL
)
1502 error("malloc of dumpinfo.CurrentFileName");
1504 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1506 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, WflagChars
);
1508 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, 0);
1510 p
= pcap_dump_open(pd
, dumpinfo
.CurrentFileName
);
1511 #ifdef HAVE_CAP_NG_H
1512 /* Give up capabilities, clear Effective set */
1513 capng_clear(CAPNG_EFFECTIVE
);
1516 error("%s", pcap_geterr(pd
));
1517 if (Cflag
!= 0 || Gflag
!= 0) {
1518 callback
= dump_packet_and_trunc
;
1519 dumpinfo
.WFileName
= WFileName
;
1522 pcap_userdata
= (u_char
*)&dumpinfo
;
1524 callback
= dump_packet
;
1525 pcap_userdata
= (u_char
*)p
;
1527 #ifdef HAVE_PCAP_DUMP_FLUSH
1532 type
= pcap_datalink(pd
);
1533 printinfo
= get_print_info(type
);
1534 callback
= print_packet
;
1535 pcap_userdata
= (u_char
*)&printinfo
;
1538 #ifdef SIGNAL_REQ_INFO
1540 * We can't get statistics when reading from a file rather
1541 * than capturing from a device.
1543 if (RFileName
== NULL
)
1544 (void)setsignal(SIGNAL_REQ_INFO
, requestinfo
);
1547 if (vflag
> 0 && WFileName
) {
1549 * When capturing to a file, "-v" means tcpdump should,
1550 * every 10 secodns, "v"erbosely report the number of
1553 #ifdef USE_WIN32_MM_TIMER
1554 /* call verbose_stats_dump() each 1000 +/-100msec */
1555 timer_id
= timeSetEvent(1000, 100, verbose_stats_dump
, 0, TIME_PERIODIC
);
1556 setvbuf(stderr
, NULL
, _IONBF
, 0);
1557 #elif defined(HAVE_ALARM)
1558 (void)setsignal(SIGALRM
, verbose_stats_dump
);
1564 if (RFileName
== NULL
) {
1566 * Live capture (if -V was specified, we set RFileName
1567 * to a file from the -V file). Print a message to
1568 * the standard error on UN*X.
1570 if (!vflag
&& !WFileName
) {
1571 (void)fprintf(stderr
,
1572 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1575 (void)fprintf(stderr
, "%s: ", program_name
);
1576 dlt
= pcap_datalink(pd
);
1577 dlt_name
= pcap_datalink_val_to_name(dlt
);
1578 if (dlt_name
== NULL
) {
1579 (void)fprintf(stderr
, "listening on %s, link-type %u, capture size %u bytes\n",
1580 device
, dlt
, snaplen
);
1582 (void)fprintf(stderr
, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1584 pcap_datalink_val_to_description(dlt
), snaplen
);
1586 (void)fflush(stderr
);
1590 status
= pcap_loop(pd
, cnt
, callback
, pcap_userdata
);
1591 if (WFileName
== NULL
) {
1593 * We're printing packets. Flush the printed output,
1594 * so it doesn't get intermingled with error output.
1598 * We got interrupted, so perhaps we didn't
1599 * manage to finish a line we were printing.
1600 * Print an extra newline, just in case.
1604 (void)fflush(stdout
);
1608 * We got interrupted. If we are reading multiple
1609 * files (via -V) set these so that we stop.
1618 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
1619 program_name
, pcap_geterr(pd
));
1621 if (RFileName
== NULL
) {
1623 * We're doing a live capture. Report the capture
1629 if (VFileName
!= NULL
) {
1630 ret
= get_next_file(VFile
, VFileLine
);
1632 RFileName
= VFileLine
;
1633 pd
= pcap_open_offline(RFileName
, ebuf
);
1636 new_dlt
= pcap_datalink(pd
);
1637 if (WFileName
&& new_dlt
!= dlt
)
1638 error("%s: new dlt does not match original", RFileName
);
1639 printinfo
= get_print_info(new_dlt
);
1640 dlt_name
= pcap_datalink_val_to_name(new_dlt
);
1641 if (dlt_name
== NULL
) {
1642 fprintf(stderr
, "reading from file %s, link-type %u\n",
1643 RFileName
, new_dlt
);
1646 "reading from file %s, link-type %s (%s)\n",
1647 RFileName
, dlt_name
,
1648 pcap_datalink_val_to_description(new_dlt
));
1650 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
1651 error("%s", pcap_geterr(pd
));
1652 if (pcap_setfilter(pd
, &fcode
) < 0)
1653 error("%s", pcap_geterr(pd
));
1657 while (ret
!= NULL
);
1660 exit(status
== -1 ? 1 : 0);
1663 /* make a clean exit on interrupts */
1665 cleanup(int signo _U_
)
1667 #ifdef USE_WIN32_MM_TIMER
1669 timeKillEvent(timer_id
);
1671 #elif defined(HAVE_ALARM)
1675 #ifdef HAVE_PCAP_BREAKLOOP
1677 * We have "pcap_breakloop()"; use it, so that we do as little
1678 * as possible in the signal handler (it's probably not safe
1679 * to do anything with standard I/O streams in a signal handler -
1680 * the ANSI C standard doesn't say it is).
1685 * We don't have "pcap_breakloop()"; this isn't safe, but
1686 * it's the best we can do. Print the summary if we're
1687 * not reading from a savefile - i.e., if we're doing a
1688 * live capture - and exit.
1690 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
1692 * We got interrupted, so perhaps we didn't
1693 * manage to finish a line we were printing.
1694 * Print an extra newline, just in case.
1697 (void)fflush(stdout
);
1705 On windows, we do not use a fork, so we do not care less about
1706 waiting a child processes to die
1708 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1710 child_cleanup(int signo _U_
)
1714 #endif /* HAVE_FORK && HAVE_VFORK */
1717 info(register int verbose
)
1719 struct pcap_stat stat
;
1722 * Older versions of libpcap didn't set ps_ifdrop on some
1723 * platforms; initialize it to 0 to handle that.
1726 if (pcap_stats(pd
, &stat
) < 0) {
1727 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
1733 fprintf(stderr
, "%s: ", program_name
);
1735 (void)fprintf(stderr
, "%u packet%s captured", packets_captured
,
1736 PLURAL_SUFFIX(packets_captured
));
1738 fputs(", ", stderr
);
1741 (void)fprintf(stderr
, "%u packet%s received by filter", stat
.ps_recv
,
1742 PLURAL_SUFFIX(stat
.ps_recv
));
1744 fputs(", ", stderr
);
1747 (void)fprintf(stderr
, "%u packet%s dropped by kernel", stat
.ps_drop
,
1748 PLURAL_SUFFIX(stat
.ps_drop
));
1749 if (stat
.ps_ifdrop
!= 0) {
1751 fputs(", ", stderr
);
1754 (void)fprintf(stderr
, "%u packet%s dropped by interface\n",
1755 stat
.ps_ifdrop
, PLURAL_SUFFIX(stat
.ps_ifdrop
));
1761 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1763 compress_savefile(const char *filename
)
1772 * Set to lowest priority so that this doesn't disturb the capture
1775 setpriority(PRIO_PROCESS
, 0, NZERO
- 1);
1777 setpriority(PRIO_PROCESS
, 0, 19);
1779 if (execlp(zflag
, zflag
, filename
, (char *)NULL
) == -1)
1781 "compress_savefile:execlp(%s, %s): %s\n",
1791 #else /* HAVE_FORK && HAVE_VFORK */
1793 compress_savefile(const char *filename
)
1796 "compress_savefile failed. Functionality not implemented under your system\n");
1798 #endif /* HAVE_FORK && HAVE_VFORK */
1801 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1803 struct dump_info
*dump_info
;
1809 dump_info
= (struct dump_info
*)user
;
1812 * XXX - this won't force the file to rotate on the specified time
1813 * boundary, but it will rotate on the first packet received after the
1814 * specified Gflag number of seconds. Note: if a Gflag time boundary
1815 * and a Cflag size boundary coincide, the time rotation will occur
1816 * first thereby cancelling the Cflag boundary (since the file should
1820 /* Check if it is time to rotate */
1823 /* Get the current time */
1824 if ((t
= time(NULL
)) == (time_t)-1) {
1825 error("dump_and_trunc_packet: can't get current_time: %s",
1826 pcap_strerror(errno
));
1830 /* If the time is greater than the specified window, rotate */
1831 if (t
- Gflag_time
>= Gflag
) {
1832 /* Update the Gflag_time */
1834 /* Update Gflag_count */
1837 * Close the current file and open a new one.
1839 pcap_dump_close(dump_info
->p
);
1842 * Compress the file we just closed, if the user asked for it
1845 compress_savefile(dump_info
->CurrentFileName
);
1848 * Check to see if we've exceeded the Wflag (when
1851 if (Cflag
== 0 && Wflag
> 0 && Gflag_count
>= Wflag
) {
1852 (void)fprintf(stderr
, "Maximum file limit reached: %d\n",
1857 if (dump_info
->CurrentFileName
!= NULL
)
1858 free(dump_info
->CurrentFileName
);
1859 /* Allocate space for max filename + \0. */
1860 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
1861 if (dump_info
->CurrentFileName
== NULL
)
1862 error("dump_packet_and_trunc: malloc");
1864 * Gflag was set otherwise we wouldn't be here. Reset the count
1865 * so multiple files would end with 1,2,3 in the filename.
1866 * The counting is handled with the -C flow after this.
1871 * This is always the first file in the Cflag
1873 * We also don't need numbering if Cflag is not set.
1876 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0,
1879 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0, 0);
1881 #ifdef HAVE_CAP_NG_H
1882 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
1883 capng_apply(CAPNG_EFFECTIVE
);
1884 #endif /* HAVE_CAP_NG_H */
1885 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
1886 #ifdef HAVE_CAP_NG_H
1887 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
1888 capng_apply(CAPNG_EFFECTIVE
);
1889 #endif /* HAVE_CAP_NG_H */
1890 if (dump_info
->p
== NULL
)
1891 error("%s", pcap_geterr(pd
));
1896 * XXX - this won't prevent capture files from getting
1897 * larger than Cflag - the last packet written to the
1898 * file could put it over Cflag.
1900 if (Cflag
!= 0 && pcap_dump_ftell(dump_info
->p
) > Cflag
) {
1902 * Close the current file and open a new one.
1904 pcap_dump_close(dump_info
->p
);
1907 * Compress the file we just closed, if the user asked for it
1910 compress_savefile(dump_info
->CurrentFileName
);
1914 if (Cflag_count
>= Wflag
)
1917 if (dump_info
->CurrentFileName
!= NULL
)
1918 free(dump_info
->CurrentFileName
);
1919 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
1920 if (dump_info
->CurrentFileName
== NULL
)
1921 error("dump_packet_and_trunc: malloc");
1922 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, Cflag_count
, WflagChars
);
1923 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
1924 if (dump_info
->p
== NULL
)
1925 error("%s", pcap_geterr(pd
));
1928 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
1929 #ifdef HAVE_PCAP_DUMP_FLUSH
1931 pcap_dump_flush(dump_info
->p
);
1940 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1946 pcap_dump(user
, h
, sp
);
1947 #ifdef HAVE_PCAP_DUMP_FLUSH
1949 pcap_dump_flush((pcap_dumper_t
*)user
);
1958 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1960 struct print_info
*print_info
;
1962 netdissect_options
*ndo
;
1969 print_info
= (struct print_info
*)user
;
1970 ndo
= print_info
->ndo
;
1973 * Some printers want to check that they're not walking off the
1974 * end of the packet.
1975 * Rather than pass it all the way down, we set this global.
1977 ndo
->ndo_snapend
= sp
+ h
->caplen
;
1979 if(print_info
->ndo_type
) {
1980 hdrlen
= (*print_info
->p
.ndo_printer
)(print_info
->ndo
, h
, sp
);
1982 hdrlen
= (*print_info
->p
.printer
)(h
, sp
);
1985 if (ndo
->ndo_Xflag
) {
1987 * Print the raw packet data in hex and ASCII.
1989 if (ndo
->ndo_Xflag
> 1) {
1991 * Include the link-layer header.
1993 hex_and_ascii_print("\n\t", sp
, h
->caplen
);
1996 * Don't include the link-layer header - and if
1997 * we have nothing past the link-layer header,
2000 if (h
->caplen
> hdrlen
)
2001 hex_and_ascii_print("\n\t", sp
+ hdrlen
,
2002 h
->caplen
- hdrlen
);
2004 } else if (ndo
->ndo_xflag
) {
2006 * Print the raw packet data in hex.
2008 if (ndo
->ndo_xflag
> 1) {
2010 * Include the link-layer header.
2012 hex_print(ndo
, "\n\t", sp
, h
->caplen
);
2015 * Don't include the link-layer header - and if
2016 * we have nothing past the link-layer header,
2019 if (h
->caplen
> hdrlen
)
2020 hex_print(ndo
, "\n\t", sp
+ hdrlen
,
2021 h
->caplen
- hdrlen
);
2023 } else if (ndo
->ndo_Aflag
) {
2025 * Print the raw packet data in ASCII.
2027 if (ndo
->ndo_Aflag
> 1) {
2029 * Include the link-layer header.
2031 ascii_print(sp
, h
->caplen
);
2034 * Don't include the link-layer header - and if
2035 * we have nothing past the link-layer header,
2038 if (h
->caplen
> hdrlen
)
2039 ascii_print(sp
+ hdrlen
, h
->caplen
- hdrlen
);
2052 * XXX - there should really be libpcap calls to get the version
2053 * number as a string (the string would be generated from #defines
2054 * at run time, so that it's not generated from string constants
2055 * in the library, as, on many UNIX systems, those constants would
2056 * be statically linked into the application executable image, and
2057 * would thus reflect the version of libpcap on the system on
2058 * which the application was *linked*, not the system on which it's
2061 * That routine should be documented, unlike the "version[]"
2062 * string, so that UNIX vendors providing their own libpcaps
2063 * don't omit it (as a couple of vendors have...).
2065 * Packet.dll should perhaps also export a routine to return the
2066 * version number of the Packet.dll code, to supply the
2067 * "Wpcap_version" information on Windows.
2069 char WDversion
[]="current-git.tcpdump.org";
2070 #if !defined(HAVE_GENERATED_VERSION)
2071 char version
[]="current-git.tcpdump.org";
2073 char pcap_version
[]="current-git.tcpdump.org";
2074 char Wpcap_version
[]="3.1";
2078 * By default, print the specified data out in hex and ASCII.
2081 ndo_default_print(netdissect_options
*ndo _U_
, const u_char
*bp
, u_int length
)
2083 hex_and_ascii_print("\n\t", bp
, length
); /* pass on lf and identation string */
2087 default_print(const u_char
*bp
, u_int length
)
2089 ndo_default_print(gndo
, bp
, length
);
2092 #ifdef SIGNAL_REQ_INFO
2093 RETSIGTYPE
requestinfo(int signo _U_
)
2103 * Called once each second in verbose mode while dumping to file
2105 #ifdef USE_WIN32_MM_TIMER
2106 void CALLBACK
verbose_stats_dump (UINT timer_id _U_
, UINT msg _U_
, DWORD_PTR arg _U_
,
2107 DWORD_PTR dw1 _U_
, DWORD_PTR dw2 _U_
)
2109 struct pcap_stat stat
;
2111 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
2112 fprintf(stderr
, "Got %u\r", packets_captured
);
2114 #elif defined(HAVE_ALARM)
2115 static void verbose_stats_dump(int sig _U_
)
2117 struct pcap_stat stat
;
2119 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
2120 fprintf(stderr
, "Got %u\r", packets_captured
);
2128 extern char version
[];
2129 #ifndef HAVE_PCAP_LIB_VERSION
2130 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
2131 extern char pcap_version
[];
2132 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2133 static char pcap_version
[] = "unknown";
2134 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2135 #endif /* HAVE_PCAP_LIB_VERSION */
2137 #ifdef HAVE_PCAP_LIB_VERSION
2139 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2141 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2143 (void)fprintf(stderr
, "%s\n",pcap_lib_version());
2144 #else /* HAVE_PCAP_LIB_VERSION */
2146 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2147 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
2149 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2150 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
2152 #endif /* HAVE_PCAP_LIB_VERSION */
2153 (void)fprintf(stderr
,
2154 "Usage: %s [-aAbd" D_FLAG
"efhH" I_FLAG J_FLAG
"KlLnNOpqRStu" U_FLAG
"vxX]" B_FLAG_USAGE
" [ -c count ]\n", program_name
);
2155 (void)fprintf(stderr
,
2156 "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n");
2157 (void)fprintf(stderr
,
2158 "\t\t[ -i interface ]" j_FLAG_USAGE
" [ -M secret ]\n");
2159 #ifdef HAVE_PCAP_SETDIRECTION
2160 (void)fprintf(stderr
,
2161 "\t\t[ -Q in|out|inout ]\n");
2163 (void)fprintf(stderr
,
2164 "\t\t[ -r file ] [ -s snaplen ] [ -T type ] [ -V file ] [ -w file ]\n");
2165 (void)fprintf(stderr
,
2166 "\t\t[ -W filecount ] [ -y datalinktype ] [ -z command ]\n");
2167 (void)fprintf(stderr
,
2168 "\t\t[ -Z user ] [ expression ]\n");
2176 ndo_error(netdissect_options
*ndo _U_
, const char *fmt
, ...)
2180 (void)fprintf(stderr
, "%s: ", program_name
);
2182 (void)vfprintf(stderr
, fmt
, ap
);
2186 if (fmt
[-1] != '\n')
2187 (void)fputc('\n', stderr
);
2195 ndo_warning(netdissect_options
*ndo _U_
, const char *fmt
, ...)
2199 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
2201 (void)vfprintf(stderr
, fmt
, ap
);
2205 if (fmt
[-1] != '\n')
2206 (void)fputc('\n', stderr
);
2211 * c-style: whitesmith