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>
50 extern int strcasecmp (const char *__s1
, const char *__s2
);
60 #ifdef HAVE_GETOPT_LONG
73 #include <sys/resource.h>
79 /* capabilities convinience library */
82 #endif /* HAVE_CAP_NG_H */
84 #include "netdissect.h"
85 #include "interface.h"
86 #include "addrtoname.h"
88 #include "setsignal.h"
89 #include "gmt2local.h"
90 #include "pcap-missing.h"
97 #define SIGNAL_REQ_INFO SIGINFO
99 #define SIGNAL_REQ_INFO SIGUSR1
102 netdissect_options Gndo
;
103 netdissect_options
*gndo
= &Gndo
;
105 static int Dflag
; /* list available devices and exit */
106 static int dflag
; /* print filter code */
107 static int Lflag
; /* list available data link types and exit */
108 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
109 static int Jflag
; /* list available time stamp types */
111 #ifdef HAVE_PCAP_SETDIRECTION
112 int Qflag
= -1; /* restrict captured packet by send/receive direction */
114 static char *zflag
= NULL
; /* compress each savefile using a specified command (like gzip or bzip2) */
116 static int infodelay
;
117 static int infoprint
;
121 int32_t thiszone
; /* seconds offset from gmt to local time */
124 static RETSIGTYPE
cleanup(int);
125 static RETSIGTYPE
child_cleanup(int);
126 static void usage(void) __attribute__((noreturn
));
127 static void show_dlts_and_exit(const char *device
, pcap_t
*pd
) __attribute__((noreturn
));
129 static void print_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
130 static void ndo_default_print(netdissect_options
*, const u_char
*, u_int
);
131 static void dump_packet_and_trunc(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
132 static void dump_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
133 static void droproot(const char *, const char *);
134 static void ndo_error(netdissect_options
*ndo
, const char *fmt
, ...)
135 __attribute__((noreturn
))
136 #ifdef __ATTRIBUTE___FORMAT_OK
137 __attribute__((format (printf
, 2, 3)))
138 #endif /* __ATTRIBUTE___FORMAT_OK */
140 static void ndo_warning(netdissect_options
*ndo
, const char *fmt
, ...)
141 #ifdef __ATTRIBUTE___FORMAT_OK
142 __attribute__((format (printf
, 2, 3)))
143 #endif /* __ATTRIBUTE___FORMAT_OK */
146 #ifdef SIGNAL_REQ_INFO
147 RETSIGTYPE
requestinfo(int);
150 #if defined(USE_WIN32_MM_TIMER)
151 #include <MMsystem.h>
152 static UINT timer_id
;
153 static void CALLBACK
verbose_stats_dump(UINT
, UINT
, DWORD_PTR
, DWORD_PTR
, DWORD_PTR
);
154 #elif defined(HAVE_ALARM)
155 static void verbose_stats_dump(int sig
);
158 static void info(int);
159 static u_int packets_captured
;
173 static const struct printer printers
[] = {
177 static const struct ndo_printer ndo_printers
[] = {
178 { ether_if_print
, DLT_EN10MB
},
180 { ipnet_if_print
, DLT_IPNET
},
182 #ifdef DLT_IEEE802_15_4
183 { ieee802_15_4_if_print
, DLT_IEEE802_15_4
},
185 #ifdef DLT_IEEE802_15_4_NOFCS
186 { ieee802_15_4_if_print
, DLT_IEEE802_15_4_NOFCS
},
189 { ppi_if_print
, DLT_PPI
},
191 #ifdef DLT_NETANALYZER
192 { netanalyzer_if_print
, DLT_NETANALYZER
},
194 #ifdef DLT_NETANALYZER_TRANSPARENT
195 { netanalyzer_transparent_if_print
, DLT_NETANALYZER_TRANSPARENT
},
197 #if defined(DLT_NFLOG) && defined(HAVE_PCAP_NFLOG_H)
198 { nflog_if_print
, DLT_NFLOG
},
201 { cip_if_print
, DLT_CIP
},
204 { cip_if_print
, DLT_ATM_CLIP
},
206 #ifdef DLT_IP_OVER_FC
207 { ipfc_if_print
, DLT_IP_OVER_FC
},
209 { null_if_print
, DLT_NULL
},
211 { null_if_print
, DLT_LOOP
},
213 #ifdef DLT_APPLE_IP_OVER_IEEE1394
214 { ap1394_if_print
, DLT_APPLE_IP_OVER_IEEE1394
},
216 #if defined(DLT_BLUETOOTH_HCI_H4_WITH_PHDR) && defined(HAVE_PCAP_BLUETOOTH_H)
217 { bt_if_print
, DLT_BLUETOOTH_HCI_H4_WITH_PHDR
},
220 { lane_if_print
, DLT_LANE8023
},
222 { arcnet_if_print
, DLT_ARCNET
},
223 #ifdef DLT_ARCNET_LINUX
224 { arcnet_linux_if_print
, DLT_ARCNET_LINUX
},
226 { raw_if_print
, DLT_RAW
},
228 { raw_if_print
, DLT_IPV4
},
231 { raw_if_print
, DLT_IPV6
},
233 #ifdef HAVE_PCAP_USB_H
235 { usb_linux_48_byte_print
, DLT_USB_LINUX
},
236 #endif /* DLT_USB_LINUX */
237 #ifdef DLT_USB_LINUX_MMAPPED
238 { usb_linux_64_byte_print
, DLT_USB_LINUX_MMAPPED
},
239 #endif /* DLT_USB_LINUX_MMAPPED */
240 #endif /* HAVE_PCAP_USB_H */
241 #ifdef DLT_SYMANTEC_FIREWALL
242 { symantec_if_print
, DLT_SYMANTEC_FIREWALL
},
245 { chdlc_if_print
, DLT_C_HDLC
},
248 { chdlc_if_print
, DLT_HDLC
},
251 { pppoe_if_print
, DLT_PPP_ETHER
},
253 #if defined(DLT_PFLOG) && defined(HAVE_NET_PFVAR_H)
254 { pflog_if_print
, DLT_PFLOG
},
256 { token_if_print
, DLT_IEEE802
},
257 { fddi_if_print
, DLT_FDDI
},
259 { sll_if_print
, DLT_LINUX_SLL
},
262 { fr_if_print
, DLT_FR
},
265 { fr_if_print
, DLT_FRELAY
},
268 { mfr_if_print
, DLT_MFR
},
270 { atm_if_print
, DLT_ATM_RFC1483
},
272 { sunatm_if_print
, DLT_SUNATM
},
275 { enc_if_print
, DLT_ENC
},
277 { sl_if_print
, DLT_SLIP
},
278 #ifdef DLT_SLIP_BSDOS
279 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
282 { ltalk_if_print
, DLT_LTALK
},
284 #ifdef DLT_JUNIPER_ATM1
285 { juniper_atm1_print
, DLT_JUNIPER_ATM1
},
287 #ifdef DLT_JUNIPER_ATM2
288 { juniper_atm2_print
, DLT_JUNIPER_ATM2
},
290 #ifdef DLT_JUNIPER_MFR
291 { juniper_mfr_print
, DLT_JUNIPER_MFR
},
293 #ifdef DLT_JUNIPER_MLFR
294 { juniper_mlfr_print
, DLT_JUNIPER_MLFR
},
296 #ifdef DLT_JUNIPER_MLPPP
297 { juniper_mlppp_print
, DLT_JUNIPER_MLPPP
},
299 #ifdef DLT_JUNIPER_PPPOE
300 { juniper_pppoe_print
, DLT_JUNIPER_PPPOE
},
302 #ifdef DLT_JUNIPER_PPPOE_ATM
303 { juniper_pppoe_atm_print
, DLT_JUNIPER_PPPOE_ATM
},
305 #ifdef DLT_JUNIPER_GGSN
306 { juniper_ggsn_print
, DLT_JUNIPER_GGSN
},
308 #ifdef DLT_JUNIPER_ES
309 { juniper_es_print
, DLT_JUNIPER_ES
},
311 #ifdef DLT_JUNIPER_MONITOR
312 { juniper_monitor_print
, DLT_JUNIPER_MONITOR
},
314 #ifdef DLT_JUNIPER_SERVICES
315 { juniper_services_print
, DLT_JUNIPER_SERVICES
},
317 #ifdef DLT_JUNIPER_ETHER
318 { juniper_ether_print
, DLT_JUNIPER_ETHER
},
320 #ifdef DLT_JUNIPER_PPP
321 { juniper_ppp_print
, DLT_JUNIPER_PPP
},
323 #ifdef DLT_JUNIPER_FRELAY
324 { juniper_frelay_print
, DLT_JUNIPER_FRELAY
},
326 #ifdef DLT_JUNIPER_CHDLC
327 { juniper_chdlc_print
, DLT_JUNIPER_CHDLC
},
330 { pktap_if_print
, DLT_PKTAP
},
332 #ifdef DLT_IEEE802_11_RADIO
333 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
335 #ifdef DLT_IEEE802_11
336 { ieee802_11_if_print
, DLT_IEEE802_11
},
338 #ifdef DLT_IEEE802_11_RADIO_AVS
339 { ieee802_11_radio_avs_if_print
, DLT_IEEE802_11_RADIO_AVS
},
341 #ifdef DLT_PRISM_HEADER
342 { prism_if_print
, DLT_PRISM_HEADER
},
344 { ppp_if_print
, DLT_PPP
},
345 #ifdef DLT_PPP_WITHDIRECTION
346 { ppp_if_print
, DLT_PPP_WITHDIRECTION
},
349 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
351 #ifdef DLT_PPP_SERIAL
352 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
357 static const struct tok status_flags
[] = {
359 { PCAP_IF_UP
, "Up" },
361 #ifdef PCAP_IF_RUNNING
362 { PCAP_IF_RUNNING
, "Running" },
364 { PCAP_IF_LOOPBACK
, "Loopback" },
369 lookup_printer(int type
)
371 const struct printer
*p
;
373 for (p
= printers
; p
->f
; ++p
)
382 lookup_ndo_printer(int type
)
384 const struct ndo_printer
*p
;
386 for (p
= ndo_printers
; p
->f
; ++p
)
390 #if defined(DLT_USER2) && defined(DLT_PKTAP)
392 * Apple incorrectly chose to use DLT_USER2 for their PKTAP
395 * We map DLT_PKTAP, whether it's DLT_USER2 as it is on Darwin-
396 * based OSes or the same value as LINKTYPE_PKTAP as it is on
397 * other OSes, to LINKTYPE_PKTAP, so files written with
398 * this version of libpcap for a DLT_PKTAP capture have a link-
399 * layer header type of LINKTYPE_PKTAP.
401 * However, files written on OS X Mavericks for a DLT_PKTAP
402 * capture have a link-layer header type of LINKTYPE_USER2.
403 * If we don't have a printer for DLT_USER2, and type is
404 * DLT_USER2, we look up the printer for DLT_PKTAP and use
407 if (type
== DLT_USER2
) {
408 for (p
= ndo_printers
; p
->f
; ++p
)
409 if (DLT_PKTAP
== p
->type
)
420 static int supports_monitor_mode
;
427 netdissect_options
*ndo
;
430 if_ndo_printer ndo_printer
;
437 char *CurrentFileName
;
442 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
444 show_tstamp_types_and_exit(const char *device
, pcap_t
*pd
)
447 int *tstamp_types
= 0;
448 const char *tstamp_type_name
;
451 n_tstamp_types
= pcap_list_tstamp_types(pd
, &tstamp_types
);
452 if (n_tstamp_types
< 0)
453 error("%s", pcap_geterr(pd
));
455 if (n_tstamp_types
== 0) {
456 fprintf(stderr
, "Time stamp type cannot be set for %s\n",
460 fprintf(stderr
, "Time stamp types for %s (use option -j to set):\n",
462 for (i
= 0; i
< n_tstamp_types
; i
++) {
463 tstamp_type_name
= pcap_tstamp_type_val_to_name(tstamp_types
[i
]);
464 if (tstamp_type_name
!= NULL
) {
465 (void) fprintf(stderr
, " %s (%s)\n", tstamp_type_name
,
466 pcap_tstamp_type_val_to_description(tstamp_types
[i
]));
468 (void) fprintf(stderr
, " %d\n", tstamp_types
[i
]);
471 pcap_free_tstamp_types(tstamp_types
);
477 show_dlts_and_exit(const char *device
, pcap_t
*pd
)
481 const char *dlt_name
;
483 n_dlts
= pcap_list_datalinks(pd
, &dlts
);
485 error("%s", pcap_geterr(pd
));
486 else if (n_dlts
== 0 || !dlts
)
487 error("No data link types.");
490 * If the interface is known to support monitor mode, indicate
491 * whether these are the data link types available when not in
492 * monitor mode, if -I wasn't specified, or when in monitor mode,
493 * when -I was specified (the link-layer types available in
494 * monitor mode might be different from the ones available when
495 * not in monitor mode).
497 if (supports_monitor_mode
)
498 (void) fprintf(stderr
, "Data link types for %s %s (use option -y to set):\n",
500 Iflag
? "when in monitor mode" : "when not in monitor mode");
502 (void) fprintf(stderr
, "Data link types for %s (use option -y to set):\n",
505 while (--n_dlts
>= 0) {
506 dlt_name
= pcap_datalink_val_to_name(dlts
[n_dlts
]);
507 if (dlt_name
!= NULL
) {
508 (void) fprintf(stderr
, " %s (%s)", dlt_name
,
509 pcap_datalink_val_to_description(dlts
[n_dlts
]));
512 * OK, does tcpdump handle that type?
514 if (lookup_printer(dlts
[n_dlts
]) == NULL
515 && lookup_ndo_printer(dlts
[n_dlts
]) == NULL
)
516 (void) fprintf(stderr
, " (printing not supported)");
517 fprintf(stderr
, "\n");
519 (void) fprintf(stderr
, " DLT %d (printing not supported)\n",
523 #ifdef HAVE_PCAP_FREE_DATALINKS
524 pcap_free_datalinks(dlts
);
529 #ifdef HAVE_PCAP_FINDALLDEVS
531 show_devices_and_exit (void)
533 pcap_if_t
*devpointer
;
534 char ebuf
[PCAP_ERRBUF_SIZE
];
537 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
540 for (i
= 0; devpointer
!= NULL
; i
++) {
541 printf("%d.%s", i
+1, devpointer
->name
);
542 if (devpointer
->description
!= NULL
)
543 printf(" (%s)", devpointer
->description
);
544 if (devpointer
->flags
!= 0)
545 printf(" [%s]", bittok2str(status_flags
, "none", devpointer
->flags
));
547 devpointer
= devpointer
->next
;
552 #endif /* HAVE_PCAP_FINDALLDEVS */
555 * Set up flags that might or might not be supported depending on the
556 * version of libpcap we're using.
558 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
560 #define B_FLAG_USAGE " [ -B size ]"
561 #else /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
564 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
566 #ifdef HAVE_PCAP_CREATE
568 #else /* HAVE_PCAP_CREATE */
570 #endif /* HAVE_PCAP_CREATE */
572 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
574 #define j_FLAG_USAGE " [ -j tstamptype ]"
576 #else /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
580 #endif /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
582 #ifdef HAVE_PCAP_FINDALLDEVS
583 #ifndef HAVE_PCAP_IF_T
584 #undef HAVE_PCAP_FINDALLDEVS
588 #ifdef HAVE_PCAP_FINDALLDEVS
594 #ifdef HAVE_PCAP_DUMP_FLUSH
600 #ifdef HAVE_PCAP_SETDIRECTION
606 static struct option longopts
[] = {
607 { NULL
, no_argument
, NULL
, 'a' },
608 { NULL
, no_argument
, NULL
, 'A' },
609 { NULL
, no_argument
, NULL
, 'b' },
610 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
611 { NULL
, required_argument
, NULL
, 'B' },
613 { NULL
, required_argument
, NULL
, 'c' },
614 { NULL
, required_argument
, NULL
, 'C' },
615 { NULL
, no_argument
, NULL
, 'd' },
616 { NULL
, no_argument
, NULL
, 'D' },
617 { NULL
, no_argument
, NULL
, 'L' },
618 { NULL
, no_argument
, NULL
, 'e' },
619 { NULL
, required_argument
, NULL
, 'E' },
620 { NULL
, no_argument
, NULL
, 'f' },
621 { NULL
, required_argument
, NULL
, 'F' },
622 { NULL
, required_argument
, NULL
, 'G' },
623 { NULL
, no_argument
, NULL
, 'h' },
624 { NULL
, no_argument
, NULL
, 'H' },
625 { NULL
, required_argument
, NULL
, 'i' },
626 #ifdef HAVE_PCAP_CREATE
627 { NULL
, no_argument
, NULL
, 'I' },
629 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
630 { NULL
, required_argument
, NULL
, 'j' },
631 { NULL
, no_argument
, NULL
, 'J' },
633 { NULL
, no_argument
, NULL
, 'l' },
634 { NULL
, no_argument
, NULL
, 'K' },
635 { NULL
, required_argument
, NULL
, 'm' },
636 { NULL
, required_argument
, NULL
, 'M' },
637 { NULL
, no_argument
, NULL
, 'n' },
638 { NULL
, no_argument
, NULL
, 'N' },
639 { NULL
, no_argument
, NULL
, 'O' },
640 { NULL
, no_argument
, NULL
, 'p' },
641 { NULL
, no_argument
, NULL
, 'q' },
642 #ifdef HAVE_PCAP_SETDIRECTION
643 { NULL
, required_argument
, NULL
, 'Q' },
645 { NULL
, required_argument
, NULL
, 'r' },
646 { NULL
, no_argument
, NULL
, 'R' },
647 { NULL
, required_argument
, NULL
, 's' },
648 { NULL
, no_argument
, NULL
, 'S' },
649 { NULL
, no_argument
, NULL
, 't' },
650 { NULL
, required_argument
, NULL
, 'T' },
651 { NULL
, no_argument
, NULL
, 'u' },
652 #ifdef HAVE_PCAP_DUMP_FLUSH
653 { NULL
, no_argument
, NULL
, 'U' },
655 { NULL
, no_argument
, NULL
, 'v' },
656 { NULL
, required_argument
, NULL
, 'V' },
657 { NULL
, required_argument
, NULL
, 'w' },
658 { NULL
, required_argument
, NULL
, 'W' },
659 { NULL
, no_argument
, NULL
, 'x' },
660 { NULL
, no_argument
, NULL
, 'X' },
661 { NULL
, required_argument
, NULL
, 'y' },
662 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
663 { NULL
, no_argument
, NULL
, 'Y' },
665 { NULL
, required_argument
, NULL
, 'z' },
666 { NULL
, required_argument
, NULL
, 'Z' },
671 /* Drop root privileges and chroot if necessary */
673 droproot(const char *username
, const char *chroot_dir
)
675 struct passwd
*pw
= NULL
;
677 if (chroot_dir
&& !username
) {
678 fprintf(stderr
, "tcpdump: Chroot without dropping root is insecure\n");
682 pw
= getpwnam(username
);
685 if (chroot(chroot_dir
) != 0 || chdir ("/") != 0) {
686 fprintf(stderr
, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
687 chroot_dir
, pcap_strerror(errno
));
692 int ret
= capng_change_id(pw
->pw_uid
, pw
->pw_gid
, CAPNG_NO_FLAG
);
694 printf("error : ret %d\n", ret
);
696 /* We don't need CAP_SETUID and CAP_SETGID */
697 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_SETUID
);
698 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_SETUID
);
699 capng_update(CAPNG_DROP
, CAPNG_PERMITTED
, CAP_SETUID
);
700 capng_update(CAPNG_DROP
, CAPNG_PERMITTED
, CAP_SETUID
);
701 capng_apply(CAPNG_SELECT_BOTH
);
704 if (initgroups(pw
->pw_name
, pw
->pw_gid
) != 0 ||
705 setgid(pw
->pw_gid
) != 0 || setuid(pw
->pw_uid
) != 0) {
706 fprintf(stderr
, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
708 (unsigned long)pw
->pw_uid
,
709 (unsigned long)pw
->pw_gid
,
710 pcap_strerror(errno
));
713 #endif /* HAVE_CAP_NG_H */
716 fprintf(stderr
, "tcpdump: Couldn't find user '%.32s'\n",
739 MakeFilename(char *buffer
, char *orig_name
, int cnt
, int max_chars
)
741 char *filename
= malloc(PATH_MAX
+ 1);
742 if (filename
== NULL
)
743 error("Makefilename: malloc");
745 /* Process with strftime if Gflag is set. */
749 /* Convert Gflag_time to a usable format */
750 if ((local_tm
= localtime(&Gflag_time
)) == NULL
) {
751 error("MakeTimedFilename: localtime");
754 /* There's no good way to detect an error in strftime since a return
755 * value of 0 isn't necessarily failure.
757 strftime(filename
, PATH_MAX
, orig_name
, local_tm
);
759 strncpy(filename
, orig_name
, PATH_MAX
);
762 if (cnt
== 0 && max_chars
== 0)
763 strncpy(buffer
, filename
, PATH_MAX
+ 1);
765 if (snprintf(buffer
, PATH_MAX
+ 1, "%s%0*d", filename
, max_chars
, cnt
) > PATH_MAX
)
766 /* Report an error if the filename is too large */
767 error("too many output files or filename is too long (> %d)", PATH_MAX
);
771 static int tcpdump_printf(netdissect_options
*ndo _U_
,
772 const char *fmt
, ...)
779 ret
=vfprintf(stdout
, fmt
, args
);
785 static struct print_info
786 get_print_info(int type
)
788 struct print_info printinfo
;
790 printinfo
.ndo_type
= 1;
791 printinfo
.ndo
= gndo
;
792 printinfo
.p
.ndo_printer
= lookup_ndo_printer(type
);
793 if (printinfo
.p
.ndo_printer
== NULL
) {
794 printinfo
.p
.printer
= lookup_printer(type
);
795 printinfo
.ndo_type
= 0;
796 if (printinfo
.p
.printer
== NULL
) {
797 gndo
->ndo_dltname
= pcap_datalink_val_to_name(type
);
798 if (gndo
->ndo_dltname
!= NULL
)
799 error("packet printing is not supported for link type %s: use -w",
802 error("packet printing is not supported for link type %d: use -w", type
);
809 get_next_file(FILE *VFile
, char *ptr
)
813 ret
= fgets(ptr
, PATH_MAX
, VFile
);
817 if (ptr
[strlen(ptr
) - 1] == '\n')
818 ptr
[strlen(ptr
) - 1] = '\0';
824 main(int argc
, char **argv
)
826 register int cnt
, op
, i
;
827 bpf_u_int32 localnet
=0 , netmask
= 0;
828 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *VFileName
, *WFileName
;
829 pcap_handler callback
;
833 const char *dlt_name
;
834 struct bpf_program fcode
;
836 RETSIGTYPE (*oldhandler
)(int);
838 struct print_info printinfo
;
839 struct dump_info dumpinfo
;
840 u_char
*pcap_userdata
;
841 char ebuf
[PCAP_ERRBUF_SIZE
];
842 char VFileLine
[PATH_MAX
+ 1];
843 char *username
= NULL
;
844 char *chroot_dir
= NULL
;
847 #ifdef HAVE_PCAP_FINDALLDEVS
848 pcap_if_t
*devpointer
;
854 if(wsockinit() != 0) return 1;
857 jflag
=-1; /* not set */
861 gndo
->ndo_default_print
=ndo_default_print
;
862 gndo
->ndo_printf
=tcpdump_printf
;
863 gndo
->ndo_error
=ndo_error
;
864 gndo
->ndo_warning
=ndo_warning
;
865 gndo
->ndo_snaplen
= DEFAULT_SNAPLEN
;
875 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
876 program_name
= cp
+ 1;
878 program_name
= argv
[0];
880 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
888 (op
= getopt_long(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:", longopts
, NULL
)) != -1)
892 /* compatibility for old -a */
903 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
905 Bflag
= atoi(optarg
)*1024;
907 error("invalid packet buffer size %s", optarg
);
909 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
914 error("invalid packet count %s", optarg
);
918 Cflag
= atoi(optarg
) * 1000000;
920 error("invalid file size %s", optarg
);
940 #ifndef HAVE_LIBCRYPTO
941 warning("crypto code not compiled in");
943 gndo
->ndo_espsecret
= optarg
;
955 Gflag
= atoi(optarg
);
957 error("invalid number of seconds %s", optarg
);
959 /* We will create one file initially. */
962 /* Grab the current time for rotation use. */
963 if ((Gflag_time
= time(NULL
)) == (time_t)-1) {
964 error("main: can't get current time: %s",
965 pcap_strerror(errno
));
978 if (optarg
[0] == '0' && optarg
[1] == 0)
979 error("Invalid adapter index");
981 #ifdef HAVE_PCAP_FINDALLDEVS
983 * If the argument is a number, treat it as
984 * an index into the list of adapters, as
985 * printed by "tcpdump -D".
987 * This should be OK on UNIX systems, as interfaces
988 * shouldn't have names that begin with digits.
989 * It can be useful on Windows, where more than
990 * one interface can have the same name.
992 devnum
= strtol(optarg
, &end
, 10);
993 if (optarg
!= end
&& *end
== '\0') {
995 error("Invalid adapter index");
997 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
1001 * Look for the devnum-th entry
1002 * in the list of devices
1006 i
< devnum
-1 && devpointer
!= NULL
;
1007 i
++, devpointer
= devpointer
->next
)
1009 if (devpointer
== NULL
)
1010 error("Invalid adapter index");
1012 device
= devpointer
->name
;
1015 #endif /* HAVE_PCAP_FINDALLDEVS */
1019 #ifdef HAVE_PCAP_CREATE
1023 #endif /* HAVE_PCAP_CREATE */
1025 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1027 jflag
= pcap_tstamp_type_name_to_val(optarg
);
1029 error("invalid time stamp type %s", optarg
);
1040 * _IOLBF is the same as _IOFBF in Microsoft's C
1041 * libraries; the only alternative they offer
1044 * XXX - this should really be checking for MSVC++,
1045 * not WIN32, if, for example, MinGW has its own
1046 * C library that is more UNIX-compatible.
1048 setvbuf(stdout
, NULL
, _IONBF
, 0);
1050 #ifdef HAVE_SETLINEBUF
1053 setvbuf(stdout
, NULL
, _IOLBF
, 0);
1064 if (smiLoadModule(optarg
) == 0) {
1065 error("could not load MIB module %s", optarg
);
1069 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
1070 program_name
, optarg
);
1071 (void)fprintf(stderr
, "(no libsmi support)\n");
1076 /* TCP-MD5 shared secret */
1077 #ifndef HAVE_LIBCRYPTO
1078 warning("crypto code not compiled in");
1101 ++suppress_default_print
;
1104 #ifdef HAVE_PCAP_SETDIRECTION
1106 if (strcasecmp(optarg
, "in") == 0)
1108 else if (strcasecmp(optarg
, "out") == 0)
1110 else if (strcasecmp(optarg
, "inout") == 0)
1111 Qflag
= PCAP_D_INOUT
;
1113 error("unknown capture direction `%s'", optarg
);
1115 #endif /* HAVE_PCAP_SETDIRECTION */
1126 snaplen
= strtol(optarg
, &end
, 0);
1127 if (optarg
== end
|| *end
!= '\0'
1128 || snaplen
< 0 || snaplen
> MAXIMUM_SNAPLEN
)
1129 error("invalid snaplen %s", optarg
);
1130 else if (snaplen
== 0)
1131 snaplen
= MAXIMUM_SNAPLEN
;
1143 if (strcasecmp(optarg
, "vat") == 0)
1144 packettype
= PT_VAT
;
1145 else if (strcasecmp(optarg
, "wb") == 0)
1147 else if (strcasecmp(optarg
, "rpc") == 0)
1148 packettype
= PT_RPC
;
1149 else if (strcasecmp(optarg
, "rtp") == 0)
1150 packettype
= PT_RTP
;
1151 else if (strcasecmp(optarg
, "rtcp") == 0)
1152 packettype
= PT_RTCP
;
1153 else if (strcasecmp(optarg
, "snmp") == 0)
1154 packettype
= PT_SNMP
;
1155 else if (strcasecmp(optarg
, "cnfp") == 0)
1156 packettype
= PT_CNFP
;
1157 else if (strcasecmp(optarg
, "tftp") == 0)
1158 packettype
= PT_TFTP
;
1159 else if (strcasecmp(optarg
, "aodv") == 0)
1160 packettype
= PT_AODV
;
1161 else if (strcasecmp(optarg
, "carp") == 0)
1162 packettype
= PT_CARP
;
1163 else if (strcasecmp(optarg
, "radius") == 0)
1164 packettype
= PT_RADIUS
;
1165 else if (strcasecmp(optarg
, "zmtp1") == 0)
1166 packettype
= PT_ZMTP1
;
1167 else if (strcasecmp(optarg
, "vxlan") == 0)
1168 packettype
= PT_VXLAN
;
1169 else if (strcasecmp(optarg
, "pgm") == 0)
1170 packettype
= PT_PGM
;
1171 else if (strcasecmp(optarg
, "pgm_zmtp1") == 0)
1172 packettype
= PT_PGM_ZMTP1
;
1173 else if (strcasecmp(optarg
, "lmp") == 0)
1174 packettype
= PT_LMP
;
1176 error("unknown packet type `%s'", optarg
);
1183 #ifdef HAVE_PCAP_DUMP_FLUSH
1202 Wflag
= atoi(optarg
);
1204 error("invalid number of output files %s", optarg
);
1205 WflagChars
= getWflagChars(Wflag
);
1210 ++suppress_default_print
;
1215 ++suppress_default_print
;
1219 gndo
->ndo_dltname
= optarg
;
1221 pcap_datalink_name_to_val(gndo
->ndo_dltname
);
1222 if (gndo
->ndo_dlt
< 0)
1223 error("invalid data link type %s", gndo
->ndo_dltname
);
1226 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
1229 /* Undocumented flag */
1230 #ifdef HAVE_PCAP_DEBUG
1231 extern int pcap_debug
;
1241 zflag
= strdup(optarg
);
1245 username
= strdup(optarg
);
1253 #ifdef HAVE_PCAP_FINDALLDEVS
1255 show_devices_and_exit();
1260 case 0: /* Default */
1261 case 4: /* Default + Date*/
1262 thiszone
= gmt2local(0);
1265 case 1: /* No time stamp */
1266 case 2: /* Unix timeval style */
1267 case 3: /* Microseconds since previous packet */
1268 case 5: /* Microseconds since first packet */
1271 default: /* Not supported */
1272 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
1276 if (fflag
!= 0 && (VFileName
!= NULL
|| RFileName
!= NULL
))
1277 error("-f can not be used with -V or -r");
1279 if (VFileName
!= NULL
&& RFileName
!= NULL
)
1280 error("-V and -r are mutually exclusive.");
1283 /* if run as root, prepare for chrooting */
1284 if (getuid() == 0 || geteuid() == 0) {
1285 /* future extensibility for cmd-line arguments */
1287 chroot_dir
= WITH_CHROOT
;
1292 /* if run as root, prepare for dropping root privileges */
1293 if (getuid() == 0 || geteuid() == 0) {
1294 /* Run with '-Z root' to restore old behaviour */
1296 username
= WITH_USER
;
1300 if (RFileName
!= NULL
|| VFileName
!= NULL
) {
1302 * If RFileName is non-null, it's the pathname of a
1303 * savefile to read. If VFileName is non-null, it's
1304 * the pathname of a file containing a list of pathnames
1305 * (one per line) of savefiles to read.
1307 * In either case, we're reading a savefile, not doing
1312 * We don't need network access, so relinquish any set-UID
1313 * or set-GID privileges we have (if any).
1315 * We do *not* want set-UID privileges when opening a
1316 * trace file, as that might let the user read other
1317 * people's trace files (especially if we're set-UID
1320 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
1321 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1323 if (VFileName
!= NULL
) {
1324 if (VFileName
[0] == '-' && VFileName
[1] == '\0')
1327 VFile
= fopen(VFileName
, "r");
1330 error("Unable to open file: %s\n", strerror(errno
));
1332 ret
= get_next_file(VFile
, VFileLine
);
1334 error("Nothing in %s\n", VFileName
);
1335 RFileName
= VFileLine
;
1338 pd
= pcap_open_offline(RFileName
, ebuf
);
1341 dlt
= pcap_datalink(pd
);
1342 dlt_name
= pcap_datalink_val_to_name(dlt
);
1343 if (dlt_name
== NULL
) {
1344 fprintf(stderr
, "reading from file %s, link-type %u\n",
1348 "reading from file %s, link-type %s (%s)\n",
1349 RFileName
, dlt_name
,
1350 pcap_datalink_val_to_description(dlt
));
1354 * We're doing a live capture.
1356 if (device
== NULL
) {
1357 device
= pcap_lookupdev(ebuf
);
1363 * Print a message to the standard error on Windows.
1364 * XXX - why do it here, with a different message?
1366 if(strlen(device
) == 1) /* we assume that an ASCII string is always longer than 1 char */
1367 { /* a Unicode string has a \0 as second byte (so strlen() is 1) */
1368 fprintf(stderr
, "%s: listening on %ws\n", program_name
, device
);
1372 fprintf(stderr
, "%s: listening on %s\n", program_name
, device
);
1377 #ifdef HAVE_PCAP_CREATE
1378 pd
= pcap_create(device
, ebuf
);
1381 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1383 show_tstamp_types_and_exit(device
, pd
);
1386 * Is this an interface that supports monitor mode?
1388 if (pcap_can_set_rfmon(pd
) == 1)
1389 supports_monitor_mode
= 1;
1391 supports_monitor_mode
= 0;
1392 status
= pcap_set_snaplen(pd
, snaplen
);
1394 error("%s: Can't set snapshot length: %s",
1395 device
, pcap_statustostr(status
));
1396 status
= pcap_set_promisc(pd
, !pflag
);
1398 error("%s: Can't set promiscuous mode: %s",
1399 device
, pcap_statustostr(status
));
1401 status
= pcap_set_rfmon(pd
, 1);
1403 error("%s: Can't set monitor mode: %s",
1404 device
, pcap_statustostr(status
));
1406 status
= pcap_set_timeout(pd
, 1000);
1408 error("%s: pcap_set_timeout failed: %s",
1409 device
, pcap_statustostr(status
));
1411 status
= pcap_set_buffer_size(pd
, Bflag
);
1413 error("%s: Can't set buffer size: %s",
1414 device
, pcap_statustostr(status
));
1416 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1418 status
= pcap_set_tstamp_type(pd
, jflag
);
1420 error("%s: Can't set time stamp type: %s",
1421 device
, pcap_statustostr(status
));
1424 status
= pcap_activate(pd
);
1427 * pcap_activate() failed.
1429 cp
= pcap_geterr(pd
);
1430 if (status
== PCAP_ERROR
)
1432 else if ((status
== PCAP_ERROR_NO_SUCH_DEVICE
||
1433 status
== PCAP_ERROR_PERM_DENIED
) &&
1435 error("%s: %s\n(%s)", device
,
1436 pcap_statustostr(status
), cp
);
1438 error("%s: %s", device
,
1439 pcap_statustostr(status
));
1440 } else if (status
> 0) {
1442 * pcap_activate() succeeded, but it's warning us
1443 * of a problem it had.
1445 cp
= pcap_geterr(pd
);
1446 if (status
== PCAP_WARNING
)
1448 else if (status
== PCAP_WARNING_PROMISC_NOTSUP
&&
1450 warning("%s: %s\n(%s)", device
,
1451 pcap_statustostr(status
), cp
);
1453 warning("%s: %s", device
,
1454 pcap_statustostr(status
));
1456 #ifdef HAVE_PCAP_SETDIRECTION
1458 status
= pcap_setdirection(pd
, Qflag
);
1460 error("%s: pcap_setdirection() failed: %s",
1461 device
, pcap_geterr(pd
));
1463 #endif /* HAVE_PCAP_SETDIRECTION */
1466 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
1470 warning("%s", ebuf
);
1471 #endif /* HAVE_PCAP_CREATE */
1473 * Let user own process after socket has been opened.
1476 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
1477 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1479 #if !defined(HAVE_PCAP_CREATE) && defined(WIN32)
1481 if(pcap_setbuff(pd
, Bflag
)==-1){
1482 error("%s", pcap_geterr(pd
));
1484 #endif /* !defined(HAVE_PCAP_CREATE) && defined(WIN32) */
1486 show_dlts_and_exit(device
, pd
);
1487 if (gndo
->ndo_dlt
>= 0) {
1488 #ifdef HAVE_PCAP_SET_DATALINK
1489 if (pcap_set_datalink(pd
, gndo
->ndo_dlt
) < 0)
1490 error("%s", pcap_geterr(pd
));
1493 * We don't actually support changing the
1494 * data link type, so we only let them
1495 * set it to what it already is.
1497 if (gndo
->ndo_dlt
!= pcap_datalink(pd
)) {
1498 error("%s is not one of the DLTs supported by this device\n",
1502 (void)fprintf(stderr
, "%s: data link type %s\n",
1503 program_name
, gndo
->ndo_dltname
);
1504 (void)fflush(stderr
);
1506 i
= pcap_snapshot(pd
);
1508 warning("snaplen raised from %d to %d", snaplen
, i
);
1512 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
1513 warning("foreign (-f) flag used but: %s", ebuf
);
1519 cmdbuf
= read_infile(infile
);
1521 cmdbuf
= copy_argv(&argv
[optind
]);
1523 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
1524 error("%s", pcap_geterr(pd
));
1526 bpf_dump(&fcode
, dflag
);
1531 init_addrtoname(gndo
, localnet
, netmask
);
1535 (void)setsignal(SIGPIPE
, cleanup
);
1536 (void)setsignal(SIGTERM
, cleanup
);
1537 (void)setsignal(SIGINT
, cleanup
);
1539 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1540 (void)setsignal(SIGCHLD
, child_cleanup
);
1542 /* Cooperate with nohup(1) */
1544 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
1545 (void)setsignal(SIGHUP
, oldhandler
);
1550 * If a user name was specified with "-Z", attempt to switch to
1551 * that user's UID. This would probably be used with sudo,
1552 * to allow tcpdump to be run in a special restricted
1553 * account (if you just want to allow users to open capture
1554 * devices, and can't just give users that permission,
1555 * you'd make tcpdump set-UID or set-GID).
1557 * Tcpdump doesn't necessarily write only to one savefile;
1558 * the general only way to allow a -Z instance to write to
1559 * savefiles as the user under whose UID it's run, rather
1560 * than as the user specified with -Z, would thus be to switch
1561 * to the original user ID before opening a capture file and
1562 * then switch back to the -Z user ID after opening the savefile.
1563 * Switching to the -Z user ID only after opening the first
1564 * savefile doesn't handle the general case.
1567 #ifdef HAVE_CAP_NG_H
1568 /* We are running as root and we will be writing to savefile */
1569 if ((getuid() == 0 || geteuid() == 0) && WFileName
) {
1571 /* Drop all capabilities from effective set */
1572 capng_clear(CAPNG_EFFECTIVE
);
1573 /* Add capabilities we will need*/
1574 capng_update(CAPNG_ADD
, CAPNG_PERMITTED
, CAP_SETUID
);
1575 capng_update(CAPNG_ADD
, CAPNG_PERMITTED
, CAP_SETGID
);
1576 capng_update(CAPNG_ADD
, CAPNG_PERMITTED
, CAP_DAC_OVERRIDE
);
1578 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_SETUID
);
1579 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_SETGID
);
1580 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
1582 capng_apply(CAPNG_SELECT_BOTH
);
1585 #endif /* HAVE_CAP_NG_H */
1587 if (getuid() == 0 || geteuid() == 0) {
1588 if (username
|| chroot_dir
)
1589 droproot(username
, chroot_dir
);
1594 if (pcap_setfilter(pd
, &fcode
) < 0)
1595 error("%s", pcap_geterr(pd
));
1598 /* Do not exceed the default PATH_MAX for files. */
1599 dumpinfo
.CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
1601 if (dumpinfo
.CurrentFileName
== NULL
)
1602 error("malloc of dumpinfo.CurrentFileName");
1604 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1606 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, WflagChars
);
1608 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, 0);
1610 p
= pcap_dump_open(pd
, dumpinfo
.CurrentFileName
);
1611 #ifdef HAVE_CAP_NG_H
1612 /* Give up capabilities, clear Effective set */
1613 capng_clear(CAPNG_EFFECTIVE
);
1616 error("%s", pcap_geterr(pd
));
1617 if (Cflag
!= 0 || Gflag
!= 0) {
1618 callback
= dump_packet_and_trunc
;
1619 dumpinfo
.WFileName
= WFileName
;
1622 pcap_userdata
= (u_char
*)&dumpinfo
;
1624 callback
= dump_packet
;
1625 pcap_userdata
= (u_char
*)p
;
1627 #ifdef HAVE_PCAP_DUMP_FLUSH
1632 type
= pcap_datalink(pd
);
1633 printinfo
= get_print_info(type
);
1634 callback
= print_packet
;
1635 pcap_userdata
= (u_char
*)&printinfo
;
1638 #ifdef SIGNAL_REQ_INFO
1640 * We can't get statistics when reading from a file rather
1641 * than capturing from a device.
1643 if (RFileName
== NULL
)
1644 (void)setsignal(SIGNAL_REQ_INFO
, requestinfo
);
1647 if (vflag
> 0 && WFileName
) {
1649 * When capturing to a file, "-v" means tcpdump should,
1650 * every 10 secodns, "v"erbosely report the number of
1653 #ifdef USE_WIN32_MM_TIMER
1654 /* call verbose_stats_dump() each 1000 +/-100msec */
1655 timer_id
= timeSetEvent(1000, 100, verbose_stats_dump
, 0, TIME_PERIODIC
);
1656 setvbuf(stderr
, NULL
, _IONBF
, 0);
1657 #elif defined(HAVE_ALARM)
1658 (void)setsignal(SIGALRM
, verbose_stats_dump
);
1664 if (RFileName
== NULL
) {
1666 * Live capture (if -V was specified, we set RFileName
1667 * to a file from the -V file). Print a message to
1668 * the standard error on UN*X.
1670 if (!vflag
&& !WFileName
) {
1671 (void)fprintf(stderr
,
1672 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1675 (void)fprintf(stderr
, "%s: ", program_name
);
1676 dlt
= pcap_datalink(pd
);
1677 dlt_name
= pcap_datalink_val_to_name(dlt
);
1678 if (dlt_name
== NULL
) {
1679 (void)fprintf(stderr
, "listening on %s, link-type %u, capture size %u bytes\n",
1680 device
, dlt
, snaplen
);
1682 (void)fprintf(stderr
, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1684 pcap_datalink_val_to_description(dlt
), snaplen
);
1686 (void)fflush(stderr
);
1690 status
= pcap_loop(pd
, cnt
, callback
, pcap_userdata
);
1691 if (WFileName
== NULL
) {
1693 * We're printing packets. Flush the printed output,
1694 * so it doesn't get intermingled with error output.
1698 * We got interrupted, so perhaps we didn't
1699 * manage to finish a line we were printing.
1700 * Print an extra newline, just in case.
1704 (void)fflush(stdout
);
1708 * We got interrupted. If we are reading multiple
1709 * files (via -V) set these so that we stop.
1718 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
1719 program_name
, pcap_geterr(pd
));
1721 if (RFileName
== NULL
) {
1723 * We're doing a live capture. Report the capture
1729 if (VFileName
!= NULL
) {
1730 ret
= get_next_file(VFile
, VFileLine
);
1732 RFileName
= VFileLine
;
1733 pd
= pcap_open_offline(RFileName
, ebuf
);
1736 new_dlt
= pcap_datalink(pd
);
1737 if (WFileName
&& new_dlt
!= dlt
)
1738 error("%s: new dlt does not match original", RFileName
);
1739 printinfo
= get_print_info(new_dlt
);
1740 dlt_name
= pcap_datalink_val_to_name(new_dlt
);
1741 if (dlt_name
== NULL
) {
1742 fprintf(stderr
, "reading from file %s, link-type %u\n",
1743 RFileName
, new_dlt
);
1746 "reading from file %s, link-type %s (%s)\n",
1747 RFileName
, dlt_name
,
1748 pcap_datalink_val_to_description(new_dlt
));
1750 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
1751 error("%s", pcap_geterr(pd
));
1752 if (pcap_setfilter(pd
, &fcode
) < 0)
1753 error("%s", pcap_geterr(pd
));
1757 while (ret
!= NULL
);
1760 exit(status
== -1 ? 1 : 0);
1763 /* make a clean exit on interrupts */
1765 cleanup(int signo _U_
)
1767 #ifdef USE_WIN32_MM_TIMER
1769 timeKillEvent(timer_id
);
1771 #elif defined(HAVE_ALARM)
1775 #ifdef HAVE_PCAP_BREAKLOOP
1777 * We have "pcap_breakloop()"; use it, so that we do as little
1778 * as possible in the signal handler (it's probably not safe
1779 * to do anything with standard I/O streams in a signal handler -
1780 * the ANSI C standard doesn't say it is).
1785 * We don't have "pcap_breakloop()"; this isn't safe, but
1786 * it's the best we can do. Print the summary if we're
1787 * not reading from a savefile - i.e., if we're doing a
1788 * live capture - and exit.
1790 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
1792 * We got interrupted, so perhaps we didn't
1793 * manage to finish a line we were printing.
1794 * Print an extra newline, just in case.
1797 (void)fflush(stdout
);
1805 On windows, we do not use a fork, so we do not care less about
1806 waiting a child processes to die
1808 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1810 child_cleanup(int signo _U_
)
1814 #endif /* HAVE_FORK && HAVE_VFORK */
1817 info(register int verbose
)
1819 struct pcap_stat stat
;
1822 * Older versions of libpcap didn't set ps_ifdrop on some
1823 * platforms; initialize it to 0 to handle that.
1826 if (pcap_stats(pd
, &stat
) < 0) {
1827 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
1833 fprintf(stderr
, "%s: ", program_name
);
1835 (void)fprintf(stderr
, "%u packet%s captured", packets_captured
,
1836 PLURAL_SUFFIX(packets_captured
));
1838 fputs(", ", stderr
);
1841 (void)fprintf(stderr
, "%u packet%s received by filter", stat
.ps_recv
,
1842 PLURAL_SUFFIX(stat
.ps_recv
));
1844 fputs(", ", stderr
);
1847 (void)fprintf(stderr
, "%u packet%s dropped by kernel", stat
.ps_drop
,
1848 PLURAL_SUFFIX(stat
.ps_drop
));
1849 if (stat
.ps_ifdrop
!= 0) {
1851 fputs(", ", stderr
);
1854 (void)fprintf(stderr
, "%u packet%s dropped by interface\n",
1855 stat
.ps_ifdrop
, PLURAL_SUFFIX(stat
.ps_ifdrop
));
1861 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1863 compress_savefile(const char *filename
)
1872 * Set to lowest priority so that this doesn't disturb the capture
1875 setpriority(PRIO_PROCESS
, 0, NZERO
- 1);
1877 setpriority(PRIO_PROCESS
, 0, 19);
1879 if (execlp(zflag
, zflag
, filename
, (char *)NULL
) == -1)
1881 "compress_savefile:execlp(%s, %s): %s\n",
1891 #else /* HAVE_FORK && HAVE_VFORK */
1893 compress_savefile(const char *filename
)
1896 "compress_savefile failed. Functionality not implemented under your system\n");
1898 #endif /* HAVE_FORK && HAVE_VFORK */
1901 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1903 struct dump_info
*dump_info
;
1909 dump_info
= (struct dump_info
*)user
;
1912 * XXX - this won't force the file to rotate on the specified time
1913 * boundary, but it will rotate on the first packet received after the
1914 * specified Gflag number of seconds. Note: if a Gflag time boundary
1915 * and a Cflag size boundary coincide, the time rotation will occur
1916 * first thereby cancelling the Cflag boundary (since the file should
1920 /* Check if it is time to rotate */
1923 /* Get the current time */
1924 if ((t
= time(NULL
)) == (time_t)-1) {
1925 error("dump_and_trunc_packet: can't get current_time: %s",
1926 pcap_strerror(errno
));
1930 /* If the time is greater than the specified window, rotate */
1931 if (t
- Gflag_time
>= Gflag
) {
1932 /* Update the Gflag_time */
1934 /* Update Gflag_count */
1937 * Close the current file and open a new one.
1939 pcap_dump_close(dump_info
->p
);
1942 * Compress the file we just closed, if the user asked for it
1945 compress_savefile(dump_info
->CurrentFileName
);
1948 * Check to see if we've exceeded the Wflag (when
1951 if (Cflag
== 0 && Wflag
> 0 && Gflag_count
>= Wflag
) {
1952 (void)fprintf(stderr
, "Maximum file limit reached: %d\n",
1957 if (dump_info
->CurrentFileName
!= NULL
)
1958 free(dump_info
->CurrentFileName
);
1959 /* Allocate space for max filename + \0. */
1960 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
1961 if (dump_info
->CurrentFileName
== NULL
)
1962 error("dump_packet_and_trunc: malloc");
1964 * Gflag was set otherwise we wouldn't be here. Reset the count
1965 * so multiple files would end with 1,2,3 in the filename.
1966 * The counting is handled with the -C flow after this.
1971 * This is always the first file in the Cflag
1973 * We also don't need numbering if Cflag is not set.
1976 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0,
1979 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0, 0);
1981 #ifdef HAVE_CAP_NG_H
1982 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
1983 capng_apply(CAPNG_EFFECTIVE
);
1984 #endif /* HAVE_CAP_NG_H */
1985 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
1986 #ifdef HAVE_CAP_NG_H
1987 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
1988 capng_apply(CAPNG_EFFECTIVE
);
1989 #endif /* HAVE_CAP_NG_H */
1990 if (dump_info
->p
== NULL
)
1991 error("%s", pcap_geterr(pd
));
1996 * XXX - this won't prevent capture files from getting
1997 * larger than Cflag - the last packet written to the
1998 * file could put it over Cflag.
2000 if (Cflag
!= 0 && pcap_dump_ftell(dump_info
->p
) > Cflag
) {
2002 * Close the current file and open a new one.
2004 pcap_dump_close(dump_info
->p
);
2007 * Compress the file we just closed, if the user asked for it
2010 compress_savefile(dump_info
->CurrentFileName
);
2014 if (Cflag_count
>= Wflag
)
2017 if (dump_info
->CurrentFileName
!= NULL
)
2018 free(dump_info
->CurrentFileName
);
2019 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
2020 if (dump_info
->CurrentFileName
== NULL
)
2021 error("dump_packet_and_trunc: malloc");
2022 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, Cflag_count
, WflagChars
);
2023 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
2024 if (dump_info
->p
== NULL
)
2025 error("%s", pcap_geterr(pd
));
2028 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
2029 #ifdef HAVE_PCAP_DUMP_FLUSH
2031 pcap_dump_flush(dump_info
->p
);
2040 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
2046 pcap_dump(user
, h
, sp
);
2047 #ifdef HAVE_PCAP_DUMP_FLUSH
2049 pcap_dump_flush((pcap_dumper_t
*)user
);
2058 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
2060 struct print_info
*print_info
;
2062 netdissect_options
*ndo
;
2068 print_info
= (struct print_info
*)user
;
2069 ndo
= print_info
->ndo
;
2070 ts_print(ndo
, &h
->ts
);
2073 * Some printers want to check that they're not walking off the
2074 * end of the packet.
2075 * Rather than pass it all the way down, we set this global.
2077 ndo
->ndo_snapend
= sp
+ h
->caplen
;
2079 if(print_info
->ndo_type
) {
2080 hdrlen
= (*print_info
->p
.ndo_printer
)(print_info
->ndo
, h
, sp
);
2082 hdrlen
= (*print_info
->p
.printer
)(h
, sp
);
2085 if (ndo
->ndo_Xflag
) {
2087 * Print the raw packet data in hex and ASCII.
2089 if (ndo
->ndo_Xflag
> 1) {
2091 * Include the link-layer header.
2093 hex_and_ascii_print(ndo
, "\n\t", sp
, h
->caplen
);
2096 * Don't include the link-layer header - and if
2097 * we have nothing past the link-layer header,
2100 if (h
->caplen
> hdrlen
)
2101 hex_and_ascii_print(ndo
, "\n\t", sp
+ hdrlen
,
2102 h
->caplen
- hdrlen
);
2104 } else if (ndo
->ndo_xflag
) {
2106 * Print the raw packet data in hex.
2108 if (ndo
->ndo_xflag
> 1) {
2110 * Include the link-layer header.
2112 hex_print(ndo
, "\n\t", sp
, h
->caplen
);
2115 * Don't include the link-layer header - and if
2116 * we have nothing past the link-layer header,
2119 if (h
->caplen
> hdrlen
)
2120 hex_print(ndo
, "\n\t", sp
+ hdrlen
,
2121 h
->caplen
- hdrlen
);
2123 } else if (ndo
->ndo_Aflag
) {
2125 * Print the raw packet data in ASCII.
2127 if (ndo
->ndo_Aflag
> 1) {
2129 * Include the link-layer header.
2131 ascii_print(ndo
, sp
, h
->caplen
);
2134 * Don't include the link-layer header - and if
2135 * we have nothing past the link-layer header,
2138 if (h
->caplen
> hdrlen
)
2139 ascii_print(ndo
, sp
+ hdrlen
, h
->caplen
- hdrlen
);
2152 * XXX - there should really be libpcap calls to get the version
2153 * number as a string (the string would be generated from #defines
2154 * at run time, so that it's not generated from string constants
2155 * in the library, as, on many UNIX systems, those constants would
2156 * be statically linked into the application executable image, and
2157 * would thus reflect the version of libpcap on the system on
2158 * which the application was *linked*, not the system on which it's
2161 * That routine should be documented, unlike the "version[]"
2162 * string, so that UNIX vendors providing their own libpcaps
2163 * don't omit it (as a couple of vendors have...).
2165 * Packet.dll should perhaps also export a routine to return the
2166 * version number of the Packet.dll code, to supply the
2167 * "Wpcap_version" information on Windows.
2169 char WDversion
[]="current-git.tcpdump.org";
2170 #if !defined(HAVE_GENERATED_VERSION)
2171 char version
[]="current-git.tcpdump.org";
2173 char pcap_version
[]="current-git.tcpdump.org";
2174 char Wpcap_version
[]="3.1";
2178 * By default, print the specified data out in hex and ASCII.
2181 ndo_default_print(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
2183 hex_and_ascii_print(ndo
, "\n\t", bp
, length
); /* pass on lf and identation string */
2187 default_print(const u_char
*bp
, u_int length
)
2189 ndo_default_print(gndo
, bp
, length
);
2192 #ifdef SIGNAL_REQ_INFO
2193 RETSIGTYPE
requestinfo(int signo _U_
)
2203 * Called once each second in verbose mode while dumping to file
2205 #ifdef USE_WIN32_MM_TIMER
2206 void CALLBACK
verbose_stats_dump (UINT timer_id _U_
, UINT msg _U_
, DWORD_PTR arg _U_
,
2207 DWORD_PTR dw1 _U_
, DWORD_PTR dw2 _U_
)
2209 struct pcap_stat stat
;
2211 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
2212 fprintf(stderr
, "Got %u\r", packets_captured
);
2214 #elif defined(HAVE_ALARM)
2215 static void verbose_stats_dump(int sig _U_
)
2217 struct pcap_stat stat
;
2219 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
2220 fprintf(stderr
, "Got %u\r", packets_captured
);
2228 extern char version
[];
2229 #ifndef HAVE_PCAP_LIB_VERSION
2230 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
2231 extern char pcap_version
[];
2232 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2233 static char pcap_version
[] = "unknown";
2234 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2235 #endif /* HAVE_PCAP_LIB_VERSION */
2237 #ifdef HAVE_PCAP_LIB_VERSION
2239 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2241 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2243 (void)fprintf(stderr
, "%s\n",pcap_lib_version());
2244 #else /* HAVE_PCAP_LIB_VERSION */
2246 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2247 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
2249 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2250 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
2252 #endif /* HAVE_PCAP_LIB_VERSION */
2253 (void)fprintf(stderr
,
2254 "Usage: %s [-aAbd" D_FLAG
"efhH" I_FLAG J_FLAG
"KlLnNOpqRStu" U_FLAG
"vxX]" B_FLAG_USAGE
" [ -c count ]\n", program_name
);
2255 (void)fprintf(stderr
,
2256 "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n");
2257 (void)fprintf(stderr
,
2258 "\t\t[ -i interface ]" j_FLAG_USAGE
" [ -M secret ]\n");
2259 #ifdef HAVE_PCAP_SETDIRECTION
2260 (void)fprintf(stderr
,
2261 "\t\t[ -Q in|out|inout ]\n");
2263 (void)fprintf(stderr
,
2264 "\t\t[ -r file ] [ -s snaplen ] [ -T type ] [ -V file ] [ -w file ]\n");
2265 (void)fprintf(stderr
,
2266 "\t\t[ -W filecount ] [ -y datalinktype ] [ -z command ]\n");
2267 (void)fprintf(stderr
,
2268 "\t\t[ -Z user ] [ expression ]\n");
2276 ndo_error(netdissect_options
*ndo _U_
, const char *fmt
, ...)
2280 (void)fprintf(stderr
, "%s: ", program_name
);
2282 (void)vfprintf(stderr
, fmt
, ap
);
2286 if (fmt
[-1] != '\n')
2287 (void)fputc('\n', stderr
);
2295 ndo_warning(netdissect_options
*ndo _U_
, const char *fmt
, ...)
2299 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
2301 (void)vfprintf(stderr
, fmt
, ap
);
2305 if (fmt
[-1] != '\n')
2306 (void)fputc('\n', stderr
);
2311 * c-style: whitesmith