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
63 #include "getopt_long.h"
73 #include <sys/resource.h>
78 /* capabilities convinience library */
81 #endif /* HAVE_CAP_NG_H */
83 #include "netdissect.h"
84 #include "interface.h"
85 #include "addrtoname.h"
87 #include "setsignal.h"
88 #include "gmt2local.h"
89 #include "pcap-missing.h"
96 #define SIGNAL_REQ_INFO SIGINFO
98 #define SIGNAL_REQ_INFO SIGUSR1
101 netdissect_options Gndo
;
102 netdissect_options
*gndo
= &Gndo
;
104 static int Dflag
; /* list available devices and exit */
105 static int dflag
; /* print filter code */
106 static int Lflag
; /* list available data link types and exit */
107 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
108 static int Jflag
; /* list available time stamp types */
110 #ifdef HAVE_PCAP_SETDIRECTION
111 int Qflag
= -1; /* restrict captured packet by send/receive direction */
113 static char *zflag
= NULL
; /* compress each savefile using a specified command (like gzip or bzip2) */
115 static int infodelay
;
116 static int infoprint
;
120 int32_t thiszone
; /* seconds offset from gmt to local time */
123 static RETSIGTYPE
cleanup(int);
124 static RETSIGTYPE
child_cleanup(int);
125 static void print_version(void);
126 static void print_usage(void);
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 */
557 * Note that there we use all letters for short options except for g, k,
558 * o, and P, and those are used by other versions of tcpdump, and we should
559 * only use them for the same purposes that the other versions of tcpdump
562 * OS X tcpdump uses -g to force non--v output for IP to be on one
563 * line, making it more "g"repable;
565 * OS X tcpdump uses -k tospecify that packet comments in pcap-ng files
568 * OpenBSD tcpdump uses -o to indicate that OS fingerprinting should be done
569 * for hosts sending TCP SYN packets;
571 * OS X tcpdump uses -P to indicate that -w should write pcap-ng rather
576 * Set up flags that might or might not be supported depending on the
577 * version of libpcap we're using.
579 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
581 #define B_FLAG_USAGE " [ -B size ]"
582 #else /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
585 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
587 #ifdef HAVE_PCAP_CREATE
589 #else /* HAVE_PCAP_CREATE */
591 #endif /* HAVE_PCAP_CREATE */
593 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
595 #define j_FLAG_USAGE " [ -j tstamptype ]"
597 #else /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
601 #endif /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
603 #ifdef HAVE_PCAP_FINDALLDEVS
604 #ifndef HAVE_PCAP_IF_T
605 #undef HAVE_PCAP_FINDALLDEVS
609 #ifdef HAVE_PCAP_FINDALLDEVS
615 #ifdef HAVE_PCAP_DUMP_FLUSH
621 #ifdef HAVE_PCAP_SETDIRECTION
630 * We do not currently have long options corresponding to all short
631 * options; we should probably pick appropriate option names for them.
633 * However, the short options where the number of times the option is
634 * specified matters, such as -v and -d and -t, should probably not
635 * just map to a long option, as saying
637 * tcpdump --verbose --verbose
639 * doesn't make sense; it should be --verbosity={N} or something such
642 * For long options with no corresponding short options, we define values
643 * outside the range of ASCII graphic characters, make that the last
644 * component of the entry for the long option, and have a case for that
645 * option in the switch statement.
647 #define OPTION_NUMBER 128
648 #define OPTION_VERSION 129
650 static struct option longopts
[] = {
651 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
652 { "buffer-size", required_argument
, NULL
, 'B' },
654 { "list-interfaces", no_argument
, NULL
, 'D' },
655 { "help", no_argument
, NULL
, 'h' },
656 { "interface", required_argument
, NULL
, 'i' },
657 #ifdef HAVE_PCAP_CREATE
658 { "monitor-mode", no_argument
, NULL
, 'I' },
660 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
661 { "time-stamp-type", required_argument
, NULL
, 'j' },
662 { "list-time-stamp-types", no_argument
, NULL
, 'J' },
664 { "dont-verify-checksums", no_argument
, NULL
, 'K' },
665 { "list-data-link-types", no_argument
, NULL
, 'L' },
666 { "no-optimize", no_argument
, NULL
, 'O' },
667 { "no-promiscuous-mode", no_argument
, NULL
, 'p' },
668 #ifdef HAVE_PCAP_SETDIRECTION
669 { "direction", required_argument
, NULL
, 'Q' },
671 { "snapshot-length", required_argument
, NULL
, 's' },
672 { "absolute-tcp-sequence-numbers", no_argument
, NULL
, 'S' },
673 #ifdef HAVE_PCAP_DUMP_FLUSH
674 { "packet-buffered", no_argument
, NULL
, 'U' },
676 { "linktype", required_argument
, NULL
, 'y' },
677 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
678 { "debug-filter-parser", no_argument
, NULL
, 'Y' },
680 { "relinquish-privileges", required_argument
, NULL
, 'Z' },
681 { "number", no_argument
, NULL
, OPTION_NUMBER
},
682 { "version", no_argument
, NULL
, OPTION_VERSION
},
687 /* Drop root privileges and chroot if necessary */
689 droproot(const char *username
, const char *chroot_dir
)
691 struct passwd
*pw
= NULL
;
693 if (chroot_dir
&& !username
) {
694 fprintf(stderr
, "tcpdump: Chroot without dropping root is insecure\n");
698 pw
= getpwnam(username
);
701 if (chroot(chroot_dir
) != 0 || chdir ("/") != 0) {
702 fprintf(stderr
, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
703 chroot_dir
, pcap_strerror(errno
));
708 int ret
= capng_change_id(pw
->pw_uid
, pw
->pw_gid
, CAPNG_NO_FLAG
);
710 printf("error : ret %d\n", ret
);
712 /* We don't need CAP_SETUID and CAP_SETGID */
713 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_SETUID
);
714 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_SETUID
);
715 capng_update(CAPNG_DROP
, CAPNG_PERMITTED
, CAP_SETUID
);
716 capng_update(CAPNG_DROP
, CAPNG_PERMITTED
, CAP_SETUID
);
717 capng_apply(CAPNG_SELECT_BOTH
);
720 if (initgroups(pw
->pw_name
, pw
->pw_gid
) != 0 ||
721 setgid(pw
->pw_gid
) != 0 || setuid(pw
->pw_uid
) != 0) {
722 fprintf(stderr
, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
724 (unsigned long)pw
->pw_uid
,
725 (unsigned long)pw
->pw_gid
,
726 pcap_strerror(errno
));
729 #endif /* HAVE_CAP_NG_H */
732 fprintf(stderr
, "tcpdump: Couldn't find user '%.32s'\n",
755 MakeFilename(char *buffer
, char *orig_name
, int cnt
, int max_chars
)
757 char *filename
= malloc(PATH_MAX
+ 1);
758 if (filename
== NULL
)
759 error("Makefilename: malloc");
761 /* Process with strftime if Gflag is set. */
765 /* Convert Gflag_time to a usable format */
766 if ((local_tm
= localtime(&Gflag_time
)) == NULL
) {
767 error("MakeTimedFilename: localtime");
770 /* There's no good way to detect an error in strftime since a return
771 * value of 0 isn't necessarily failure.
773 strftime(filename
, PATH_MAX
, orig_name
, local_tm
);
775 strncpy(filename
, orig_name
, PATH_MAX
);
778 if (cnt
== 0 && max_chars
== 0)
779 strncpy(buffer
, filename
, PATH_MAX
+ 1);
781 if (snprintf(buffer
, PATH_MAX
+ 1, "%s%0*d", filename
, max_chars
, cnt
) > PATH_MAX
)
782 /* Report an error if the filename is too large */
783 error("too many output files or filename is too long (> %d)", PATH_MAX
);
787 static int tcpdump_printf(netdissect_options
*ndo _U_
,
788 const char *fmt
, ...)
795 ret
=vfprintf(stdout
, fmt
, args
);
801 static struct print_info
802 get_print_info(int type
)
804 struct print_info printinfo
;
806 printinfo
.ndo_type
= 1;
807 printinfo
.ndo
= gndo
;
808 printinfo
.p
.ndo_printer
= lookup_ndo_printer(type
);
809 if (printinfo
.p
.ndo_printer
== NULL
) {
810 printinfo
.p
.printer
= lookup_printer(type
);
811 printinfo
.ndo_type
= 0;
812 if (printinfo
.p
.printer
== NULL
) {
813 gndo
->ndo_dltname
= pcap_datalink_val_to_name(type
);
814 if (gndo
->ndo_dltname
!= NULL
)
815 error("packet printing is not supported for link type %s: use -w",
818 error("packet printing is not supported for link type %d: use -w", type
);
825 get_next_file(FILE *VFile
, char *ptr
)
829 ret
= fgets(ptr
, PATH_MAX
, VFile
);
833 if (ptr
[strlen(ptr
) - 1] == '\n')
834 ptr
[strlen(ptr
) - 1] = '\0';
840 main(int argc
, char **argv
)
842 register int cnt
, op
, i
;
843 bpf_u_int32 localnet
=0 , netmask
= 0;
844 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *VFileName
, *WFileName
;
845 pcap_handler callback
;
849 const char *dlt_name
;
850 struct bpf_program fcode
;
852 RETSIGTYPE (*oldhandler
)(int);
854 struct print_info printinfo
;
855 struct dump_info dumpinfo
;
856 u_char
*pcap_userdata
;
857 char ebuf
[PCAP_ERRBUF_SIZE
];
858 char VFileLine
[PATH_MAX
+ 1];
859 char *username
= NULL
;
860 char *chroot_dir
= NULL
;
863 #ifdef HAVE_PCAP_FINDALLDEVS
864 pcap_if_t
*devpointer
;
870 if(wsockinit() != 0) return 1;
873 jflag
=-1; /* not set */
877 gndo
->ndo_default_print
=ndo_default_print
;
878 gndo
->ndo_printf
=tcpdump_printf
;
879 gndo
->ndo_error
=ndo_error
;
880 gndo
->ndo_warning
=ndo_warning
;
881 gndo
->ndo_snaplen
= DEFAULT_SNAPLEN
;
891 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
892 program_name
= cp
+ 1;
894 program_name
= argv
[0];
896 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
904 (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)
908 /* compatibility for old -a */
919 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
921 Bflag
= atoi(optarg
)*1024;
923 error("invalid packet buffer size %s", optarg
);
925 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
930 error("invalid packet count %s", optarg
);
934 Cflag
= atoi(optarg
) * 1000000;
936 error("invalid file size %s", optarg
);
956 #ifndef HAVE_LIBCRYPTO
957 warning("crypto code not compiled in");
959 gndo
->ndo_espsecret
= optarg
;
971 Gflag
= atoi(optarg
);
973 error("invalid number of seconds %s", optarg
);
975 /* We will create one file initially. */
978 /* Grab the current time for rotation use. */
979 if ((Gflag_time
= time(NULL
)) == (time_t)-1) {
980 error("main: can't get current time: %s",
981 pcap_strerror(errno
));
995 if (optarg
[0] == '0' && optarg
[1] == 0)
996 error("Invalid adapter index");
998 #ifdef HAVE_PCAP_FINDALLDEVS
1000 * If the argument is a number, treat it as
1001 * an index into the list of adapters, as
1002 * printed by "tcpdump -D".
1004 * This should be OK on UNIX systems, as interfaces
1005 * shouldn't have names that begin with digits.
1006 * It can be useful on Windows, where more than
1007 * one interface can have the same name.
1009 devnum
= strtol(optarg
, &end
, 10);
1010 if (optarg
!= end
&& *end
== '\0') {
1012 error("Invalid adapter index");
1014 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
1018 * Look for the devnum-th entry
1019 * in the list of devices
1023 i
< devnum
-1 && devpointer
!= NULL
;
1024 i
++, devpointer
= devpointer
->next
)
1026 if (devpointer
== NULL
)
1027 error("Invalid adapter index");
1029 device
= devpointer
->name
;
1032 #endif /* HAVE_PCAP_FINDALLDEVS */
1036 #ifdef HAVE_PCAP_CREATE
1040 #endif /* HAVE_PCAP_CREATE */
1042 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1044 jflag
= pcap_tstamp_type_name_to_val(optarg
);
1046 error("invalid time stamp type %s", optarg
);
1057 * _IOLBF is the same as _IOFBF in Microsoft's C
1058 * libraries; the only alternative they offer
1061 * XXX - this should really be checking for MSVC++,
1062 * not WIN32, if, for example, MinGW has its own
1063 * C library that is more UNIX-compatible.
1065 setvbuf(stdout
, NULL
, _IONBF
, 0);
1067 #ifdef HAVE_SETLINEBUF
1070 setvbuf(stdout
, NULL
, _IOLBF
, 0);
1081 if (smiLoadModule(optarg
) == 0) {
1082 error("could not load MIB module %s", optarg
);
1086 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
1087 program_name
, optarg
);
1088 (void)fprintf(stderr
, "(no libsmi support)\n");
1093 /* TCP-MD5 shared secret */
1094 #ifndef HAVE_LIBCRYPTO
1095 warning("crypto code not compiled in");
1118 ++suppress_default_print
;
1121 #ifdef HAVE_PCAP_SETDIRECTION
1123 if (strcasecmp(optarg
, "in") == 0)
1125 else if (strcasecmp(optarg
, "out") == 0)
1127 else if (strcasecmp(optarg
, "inout") == 0)
1128 Qflag
= PCAP_D_INOUT
;
1130 error("unknown capture direction `%s'", optarg
);
1132 #endif /* HAVE_PCAP_SETDIRECTION */
1143 snaplen
= strtol(optarg
, &end
, 0);
1144 if (optarg
== end
|| *end
!= '\0'
1145 || snaplen
< 0 || snaplen
> MAXIMUM_SNAPLEN
)
1146 error("invalid snaplen %s", optarg
);
1147 else if (snaplen
== 0)
1148 snaplen
= MAXIMUM_SNAPLEN
;
1160 if (strcasecmp(optarg
, "vat") == 0)
1161 packettype
= PT_VAT
;
1162 else if (strcasecmp(optarg
, "wb") == 0)
1164 else if (strcasecmp(optarg
, "rpc") == 0)
1165 packettype
= PT_RPC
;
1166 else if (strcasecmp(optarg
, "rtp") == 0)
1167 packettype
= PT_RTP
;
1168 else if (strcasecmp(optarg
, "rtcp") == 0)
1169 packettype
= PT_RTCP
;
1170 else if (strcasecmp(optarg
, "snmp") == 0)
1171 packettype
= PT_SNMP
;
1172 else if (strcasecmp(optarg
, "cnfp") == 0)
1173 packettype
= PT_CNFP
;
1174 else if (strcasecmp(optarg
, "tftp") == 0)
1175 packettype
= PT_TFTP
;
1176 else if (strcasecmp(optarg
, "aodv") == 0)
1177 packettype
= PT_AODV
;
1178 else if (strcasecmp(optarg
, "carp") == 0)
1179 packettype
= PT_CARP
;
1180 else if (strcasecmp(optarg
, "radius") == 0)
1181 packettype
= PT_RADIUS
;
1182 else if (strcasecmp(optarg
, "zmtp1") == 0)
1183 packettype
= PT_ZMTP1
;
1184 else if (strcasecmp(optarg
, "vxlan") == 0)
1185 packettype
= PT_VXLAN
;
1186 else if (strcasecmp(optarg
, "pgm") == 0)
1187 packettype
= PT_PGM
;
1188 else if (strcasecmp(optarg
, "pgm_zmtp1") == 0)
1189 packettype
= PT_PGM_ZMTP1
;
1190 else if (strcasecmp(optarg
, "lmp") == 0)
1191 packettype
= PT_LMP
;
1193 error("unknown packet type `%s'", optarg
);
1200 #ifdef HAVE_PCAP_DUMP_FLUSH
1219 Wflag
= atoi(optarg
);
1221 error("invalid number of output files %s", optarg
);
1222 WflagChars
= getWflagChars(Wflag
);
1227 ++suppress_default_print
;
1232 ++suppress_default_print
;
1236 gndo
->ndo_dltname
= optarg
;
1238 pcap_datalink_name_to_val(gndo
->ndo_dltname
);
1239 if (gndo
->ndo_dlt
< 0)
1240 error("invalid data link type %s", gndo
->ndo_dltname
);
1243 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
1246 /* Undocumented flag */
1247 #ifdef HAVE_PCAP_DEBUG
1248 extern int pcap_debug
;
1258 zflag
= strdup(optarg
);
1262 username
= strdup(optarg
);
1266 gndo
->ndo_packet_number
= 1;
1269 case OPTION_VERSION
:
1280 #ifdef HAVE_PCAP_FINDALLDEVS
1282 show_devices_and_exit();
1287 case 0: /* Default */
1288 case 4: /* Default + Date*/
1289 thiszone
= gmt2local(0);
1292 case 1: /* No time stamp */
1293 case 2: /* Unix timeval style */
1294 case 3: /* Microseconds since previous packet */
1295 case 5: /* Microseconds since first packet */
1298 default: /* Not supported */
1299 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
1303 if (fflag
!= 0 && (VFileName
!= NULL
|| RFileName
!= NULL
))
1304 error("-f can not be used with -V or -r");
1306 if (VFileName
!= NULL
&& RFileName
!= NULL
)
1307 error("-V and -r are mutually exclusive.");
1310 /* if run as root, prepare for chrooting */
1311 if (getuid() == 0 || geteuid() == 0) {
1312 /* future extensibility for cmd-line arguments */
1314 chroot_dir
= WITH_CHROOT
;
1319 /* if run as root, prepare for dropping root privileges */
1320 if (getuid() == 0 || geteuid() == 0) {
1321 /* Run with '-Z root' to restore old behaviour */
1323 username
= WITH_USER
;
1327 if (RFileName
!= NULL
|| VFileName
!= NULL
) {
1329 * If RFileName is non-null, it's the pathname of a
1330 * savefile to read. If VFileName is non-null, it's
1331 * the pathname of a file containing a list of pathnames
1332 * (one per line) of savefiles to read.
1334 * In either case, we're reading a savefile, not doing
1339 * We don't need network access, so relinquish any set-UID
1340 * or set-GID privileges we have (if any).
1342 * We do *not* want set-UID privileges when opening a
1343 * trace file, as that might let the user read other
1344 * people's trace files (especially if we're set-UID
1347 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
1348 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1350 if (VFileName
!= NULL
) {
1351 if (VFileName
[0] == '-' && VFileName
[1] == '\0')
1354 VFile
= fopen(VFileName
, "r");
1357 error("Unable to open file: %s\n", strerror(errno
));
1359 ret
= get_next_file(VFile
, VFileLine
);
1361 error("Nothing in %s\n", VFileName
);
1362 RFileName
= VFileLine
;
1365 pd
= pcap_open_offline(RFileName
, ebuf
);
1368 dlt
= pcap_datalink(pd
);
1369 dlt_name
= pcap_datalink_val_to_name(dlt
);
1370 if (dlt_name
== NULL
) {
1371 fprintf(stderr
, "reading from file %s, link-type %u\n",
1375 "reading from file %s, link-type %s (%s)\n",
1376 RFileName
, dlt_name
,
1377 pcap_datalink_val_to_description(dlt
));
1381 * We're doing a live capture.
1383 if (device
== NULL
) {
1384 device
= pcap_lookupdev(ebuf
);
1390 * Print a message to the standard error on Windows.
1391 * XXX - why do it here, with a different message?
1393 if(strlen(device
) == 1) /* we assume that an ASCII string is always longer than 1 char */
1394 { /* a Unicode string has a \0 as second byte (so strlen() is 1) */
1395 fprintf(stderr
, "%s: listening on %ws\n", program_name
, device
);
1399 fprintf(stderr
, "%s: listening on %s\n", program_name
, device
);
1404 #ifdef HAVE_PCAP_CREATE
1405 pd
= pcap_create(device
, ebuf
);
1408 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1410 show_tstamp_types_and_exit(device
, pd
);
1413 * Is this an interface that supports monitor mode?
1415 if (pcap_can_set_rfmon(pd
) == 1)
1416 supports_monitor_mode
= 1;
1418 supports_monitor_mode
= 0;
1419 status
= pcap_set_snaplen(pd
, snaplen
);
1421 error("%s: Can't set snapshot length: %s",
1422 device
, pcap_statustostr(status
));
1423 status
= pcap_set_promisc(pd
, !pflag
);
1425 error("%s: Can't set promiscuous mode: %s",
1426 device
, pcap_statustostr(status
));
1428 status
= pcap_set_rfmon(pd
, 1);
1430 error("%s: Can't set monitor mode: %s",
1431 device
, pcap_statustostr(status
));
1433 status
= pcap_set_timeout(pd
, 1000);
1435 error("%s: pcap_set_timeout failed: %s",
1436 device
, pcap_statustostr(status
));
1438 status
= pcap_set_buffer_size(pd
, Bflag
);
1440 error("%s: Can't set buffer size: %s",
1441 device
, pcap_statustostr(status
));
1443 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1445 status
= pcap_set_tstamp_type(pd
, jflag
);
1447 error("%s: Can't set time stamp type: %s",
1448 device
, pcap_statustostr(status
));
1451 status
= pcap_activate(pd
);
1454 * pcap_activate() failed.
1456 cp
= pcap_geterr(pd
);
1457 if (status
== PCAP_ERROR
)
1459 else if ((status
== PCAP_ERROR_NO_SUCH_DEVICE
||
1460 status
== PCAP_ERROR_PERM_DENIED
) &&
1462 error("%s: %s\n(%s)", device
,
1463 pcap_statustostr(status
), cp
);
1465 error("%s: %s", device
,
1466 pcap_statustostr(status
));
1467 } else if (status
> 0) {
1469 * pcap_activate() succeeded, but it's warning us
1470 * of a problem it had.
1472 cp
= pcap_geterr(pd
);
1473 if (status
== PCAP_WARNING
)
1475 else if (status
== PCAP_WARNING_PROMISC_NOTSUP
&&
1477 warning("%s: %s\n(%s)", device
,
1478 pcap_statustostr(status
), cp
);
1480 warning("%s: %s", device
,
1481 pcap_statustostr(status
));
1483 #ifdef HAVE_PCAP_SETDIRECTION
1485 status
= pcap_setdirection(pd
, Qflag
);
1487 error("%s: pcap_setdirection() failed: %s",
1488 device
, pcap_geterr(pd
));
1490 #endif /* HAVE_PCAP_SETDIRECTION */
1493 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
1497 warning("%s", ebuf
);
1498 #endif /* HAVE_PCAP_CREATE */
1500 * Let user own process after socket has been opened.
1503 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
1504 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1506 #if !defined(HAVE_PCAP_CREATE) && defined(WIN32)
1508 if(pcap_setbuff(pd
, Bflag
)==-1){
1509 error("%s", pcap_geterr(pd
));
1511 #endif /* !defined(HAVE_PCAP_CREATE) && defined(WIN32) */
1513 show_dlts_and_exit(device
, pd
);
1514 if (gndo
->ndo_dlt
>= 0) {
1515 #ifdef HAVE_PCAP_SET_DATALINK
1516 if (pcap_set_datalink(pd
, gndo
->ndo_dlt
) < 0)
1517 error("%s", pcap_geterr(pd
));
1520 * We don't actually support changing the
1521 * data link type, so we only let them
1522 * set it to what it already is.
1524 if (gndo
->ndo_dlt
!= pcap_datalink(pd
)) {
1525 error("%s is not one of the DLTs supported by this device\n",
1529 (void)fprintf(stderr
, "%s: data link type %s\n",
1530 program_name
, gndo
->ndo_dltname
);
1531 (void)fflush(stderr
);
1533 i
= pcap_snapshot(pd
);
1535 warning("snaplen raised from %d to %d", snaplen
, i
);
1539 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
1540 warning("foreign (-f) flag used but: %s", ebuf
);
1546 cmdbuf
= read_infile(infile
);
1548 cmdbuf
= copy_argv(&argv
[optind
]);
1550 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
1551 error("%s", pcap_geterr(pd
));
1553 bpf_dump(&fcode
, dflag
);
1558 init_addrtoname(gndo
, localnet
, netmask
);
1562 (void)setsignal(SIGPIPE
, cleanup
);
1563 (void)setsignal(SIGTERM
, cleanup
);
1564 (void)setsignal(SIGINT
, cleanup
);
1566 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1567 (void)setsignal(SIGCHLD
, child_cleanup
);
1569 /* Cooperate with nohup(1) */
1571 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
1572 (void)setsignal(SIGHUP
, oldhandler
);
1577 * If a user name was specified with "-Z", attempt to switch to
1578 * that user's UID. This would probably be used with sudo,
1579 * to allow tcpdump to be run in a special restricted
1580 * account (if you just want to allow users to open capture
1581 * devices, and can't just give users that permission,
1582 * you'd make tcpdump set-UID or set-GID).
1584 * Tcpdump doesn't necessarily write only to one savefile;
1585 * the general only way to allow a -Z instance to write to
1586 * savefiles as the user under whose UID it's run, rather
1587 * than as the user specified with -Z, would thus be to switch
1588 * to the original user ID before opening a capture file and
1589 * then switch back to the -Z user ID after opening the savefile.
1590 * Switching to the -Z user ID only after opening the first
1591 * savefile doesn't handle the general case.
1594 #ifdef HAVE_CAP_NG_H
1595 /* We are running as root and we will be writing to savefile */
1596 if ((getuid() == 0 || geteuid() == 0) && WFileName
) {
1598 /* Drop all capabilities from effective set */
1599 capng_clear(CAPNG_EFFECTIVE
);
1600 /* Add capabilities we will need*/
1601 capng_update(CAPNG_ADD
, CAPNG_PERMITTED
, CAP_SETUID
);
1602 capng_update(CAPNG_ADD
, CAPNG_PERMITTED
, CAP_SETGID
);
1603 capng_update(CAPNG_ADD
, CAPNG_PERMITTED
, CAP_DAC_OVERRIDE
);
1605 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_SETUID
);
1606 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_SETGID
);
1607 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
1609 capng_apply(CAPNG_SELECT_BOTH
);
1612 #endif /* HAVE_CAP_NG_H */
1614 if (getuid() == 0 || geteuid() == 0) {
1615 if (username
|| chroot_dir
)
1616 droproot(username
, chroot_dir
);
1621 if (pcap_setfilter(pd
, &fcode
) < 0)
1622 error("%s", pcap_geterr(pd
));
1625 /* Do not exceed the default PATH_MAX for files. */
1626 dumpinfo
.CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
1628 if (dumpinfo
.CurrentFileName
== NULL
)
1629 error("malloc of dumpinfo.CurrentFileName");
1631 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1633 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, WflagChars
);
1635 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, 0);
1637 p
= pcap_dump_open(pd
, dumpinfo
.CurrentFileName
);
1638 #ifdef HAVE_CAP_NG_H
1639 /* Give up capabilities, clear Effective set */
1640 capng_clear(CAPNG_EFFECTIVE
);
1643 error("%s", pcap_geterr(pd
));
1644 if (Cflag
!= 0 || Gflag
!= 0) {
1645 callback
= dump_packet_and_trunc
;
1646 dumpinfo
.WFileName
= WFileName
;
1649 pcap_userdata
= (u_char
*)&dumpinfo
;
1651 callback
= dump_packet
;
1652 pcap_userdata
= (u_char
*)p
;
1654 #ifdef HAVE_PCAP_DUMP_FLUSH
1659 type
= pcap_datalink(pd
);
1660 printinfo
= get_print_info(type
);
1661 callback
= print_packet
;
1662 pcap_userdata
= (u_char
*)&printinfo
;
1665 #ifdef SIGNAL_REQ_INFO
1667 * We can't get statistics when reading from a file rather
1668 * than capturing from a device.
1670 if (RFileName
== NULL
)
1671 (void)setsignal(SIGNAL_REQ_INFO
, requestinfo
);
1674 if (vflag
> 0 && WFileName
) {
1676 * When capturing to a file, "-v" means tcpdump should,
1677 * every 10 secodns, "v"erbosely report the number of
1680 #ifdef USE_WIN32_MM_TIMER
1681 /* call verbose_stats_dump() each 1000 +/-100msec */
1682 timer_id
= timeSetEvent(1000, 100, verbose_stats_dump
, 0, TIME_PERIODIC
);
1683 setvbuf(stderr
, NULL
, _IONBF
, 0);
1684 #elif defined(HAVE_ALARM)
1685 (void)setsignal(SIGALRM
, verbose_stats_dump
);
1691 if (RFileName
== NULL
) {
1693 * Live capture (if -V was specified, we set RFileName
1694 * to a file from the -V file). Print a message to
1695 * the standard error on UN*X.
1697 if (!vflag
&& !WFileName
) {
1698 (void)fprintf(stderr
,
1699 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1702 (void)fprintf(stderr
, "%s: ", program_name
);
1703 dlt
= pcap_datalink(pd
);
1704 dlt_name
= pcap_datalink_val_to_name(dlt
);
1705 if (dlt_name
== NULL
) {
1706 (void)fprintf(stderr
, "listening on %s, link-type %u, capture size %u bytes\n",
1707 device
, dlt
, snaplen
);
1709 (void)fprintf(stderr
, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1711 pcap_datalink_val_to_description(dlt
), snaplen
);
1713 (void)fflush(stderr
);
1717 status
= pcap_loop(pd
, cnt
, callback
, pcap_userdata
);
1718 if (WFileName
== NULL
) {
1720 * We're printing packets. Flush the printed output,
1721 * so it doesn't get intermingled with error output.
1725 * We got interrupted, so perhaps we didn't
1726 * manage to finish a line we were printing.
1727 * Print an extra newline, just in case.
1731 (void)fflush(stdout
);
1735 * We got interrupted. If we are reading multiple
1736 * files (via -V) set these so that we stop.
1745 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
1746 program_name
, pcap_geterr(pd
));
1748 if (RFileName
== NULL
) {
1750 * We're doing a live capture. Report the capture
1756 if (VFileName
!= NULL
) {
1757 ret
= get_next_file(VFile
, VFileLine
);
1759 RFileName
= VFileLine
;
1760 pd
= pcap_open_offline(RFileName
, ebuf
);
1763 new_dlt
= pcap_datalink(pd
);
1764 if (WFileName
&& new_dlt
!= dlt
)
1765 error("%s: new dlt does not match original", RFileName
);
1766 printinfo
= get_print_info(new_dlt
);
1767 dlt_name
= pcap_datalink_val_to_name(new_dlt
);
1768 if (dlt_name
== NULL
) {
1769 fprintf(stderr
, "reading from file %s, link-type %u\n",
1770 RFileName
, new_dlt
);
1773 "reading from file %s, link-type %s (%s)\n",
1774 RFileName
, dlt_name
,
1775 pcap_datalink_val_to_description(new_dlt
));
1777 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
1778 error("%s", pcap_geterr(pd
));
1779 if (pcap_setfilter(pd
, &fcode
) < 0)
1780 error("%s", pcap_geterr(pd
));
1784 while (ret
!= NULL
);
1787 exit(status
== -1 ? 1 : 0);
1790 /* make a clean exit on interrupts */
1792 cleanup(int signo _U_
)
1794 #ifdef USE_WIN32_MM_TIMER
1796 timeKillEvent(timer_id
);
1798 #elif defined(HAVE_ALARM)
1802 #ifdef HAVE_PCAP_BREAKLOOP
1804 * We have "pcap_breakloop()"; use it, so that we do as little
1805 * as possible in the signal handler (it's probably not safe
1806 * to do anything with standard I/O streams in a signal handler -
1807 * the ANSI C standard doesn't say it is).
1812 * We don't have "pcap_breakloop()"; this isn't safe, but
1813 * it's the best we can do. Print the summary if we're
1814 * not reading from a savefile - i.e., if we're doing a
1815 * live capture - and exit.
1817 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
1819 * We got interrupted, so perhaps we didn't
1820 * manage to finish a line we were printing.
1821 * Print an extra newline, just in case.
1824 (void)fflush(stdout
);
1832 On windows, we do not use a fork, so we do not care less about
1833 waiting a child processes to die
1835 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1837 child_cleanup(int signo _U_
)
1841 #endif /* HAVE_FORK && HAVE_VFORK */
1844 info(register int verbose
)
1846 struct pcap_stat stat
;
1849 * Older versions of libpcap didn't set ps_ifdrop on some
1850 * platforms; initialize it to 0 to handle that.
1853 if (pcap_stats(pd
, &stat
) < 0) {
1854 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
1860 fprintf(stderr
, "%s: ", program_name
);
1862 (void)fprintf(stderr
, "%u packet%s captured", packets_captured
,
1863 PLURAL_SUFFIX(packets_captured
));
1865 fputs(", ", stderr
);
1868 (void)fprintf(stderr
, "%u packet%s received by filter", stat
.ps_recv
,
1869 PLURAL_SUFFIX(stat
.ps_recv
));
1871 fputs(", ", stderr
);
1874 (void)fprintf(stderr
, "%u packet%s dropped by kernel", stat
.ps_drop
,
1875 PLURAL_SUFFIX(stat
.ps_drop
));
1876 if (stat
.ps_ifdrop
!= 0) {
1878 fputs(", ", stderr
);
1881 (void)fprintf(stderr
, "%u packet%s dropped by interface\n",
1882 stat
.ps_ifdrop
, PLURAL_SUFFIX(stat
.ps_ifdrop
));
1888 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1890 compress_savefile(const char *filename
)
1899 * Set to lowest priority so that this doesn't disturb the capture
1902 setpriority(PRIO_PROCESS
, 0, NZERO
- 1);
1904 setpriority(PRIO_PROCESS
, 0, 19);
1906 if (execlp(zflag
, zflag
, filename
, (char *)NULL
) == -1)
1908 "compress_savefile:execlp(%s, %s): %s\n",
1918 #else /* HAVE_FORK && HAVE_VFORK */
1920 compress_savefile(const char *filename
)
1923 "compress_savefile failed. Functionality not implemented under your system\n");
1925 #endif /* HAVE_FORK && HAVE_VFORK */
1928 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1930 struct dump_info
*dump_info
;
1936 dump_info
= (struct dump_info
*)user
;
1939 * XXX - this won't force the file to rotate on the specified time
1940 * boundary, but it will rotate on the first packet received after the
1941 * specified Gflag number of seconds. Note: if a Gflag time boundary
1942 * and a Cflag size boundary coincide, the time rotation will occur
1943 * first thereby cancelling the Cflag boundary (since the file should
1947 /* Check if it is time to rotate */
1950 /* Get the current time */
1951 if ((t
= time(NULL
)) == (time_t)-1) {
1952 error("dump_and_trunc_packet: can't get current_time: %s",
1953 pcap_strerror(errno
));
1957 /* If the time is greater than the specified window, rotate */
1958 if (t
- Gflag_time
>= Gflag
) {
1959 /* Update the Gflag_time */
1961 /* Update Gflag_count */
1964 * Close the current file and open a new one.
1966 pcap_dump_close(dump_info
->p
);
1969 * Compress the file we just closed, if the user asked for it
1972 compress_savefile(dump_info
->CurrentFileName
);
1975 * Check to see if we've exceeded the Wflag (when
1978 if (Cflag
== 0 && Wflag
> 0 && Gflag_count
>= Wflag
) {
1979 (void)fprintf(stderr
, "Maximum file limit reached: %d\n",
1984 if (dump_info
->CurrentFileName
!= NULL
)
1985 free(dump_info
->CurrentFileName
);
1986 /* Allocate space for max filename + \0. */
1987 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
1988 if (dump_info
->CurrentFileName
== NULL
)
1989 error("dump_packet_and_trunc: malloc");
1991 * Gflag was set otherwise we wouldn't be here. Reset the count
1992 * so multiple files would end with 1,2,3 in the filename.
1993 * The counting is handled with the -C flow after this.
1998 * This is always the first file in the Cflag
2000 * We also don't need numbering if Cflag is not set.
2003 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0,
2006 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0, 0);
2008 #ifdef HAVE_CAP_NG_H
2009 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
2010 capng_apply(CAPNG_EFFECTIVE
);
2011 #endif /* HAVE_CAP_NG_H */
2012 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
2013 #ifdef HAVE_CAP_NG_H
2014 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
2015 capng_apply(CAPNG_EFFECTIVE
);
2016 #endif /* HAVE_CAP_NG_H */
2017 if (dump_info
->p
== NULL
)
2018 error("%s", pcap_geterr(pd
));
2023 * XXX - this won't prevent capture files from getting
2024 * larger than Cflag - the last packet written to the
2025 * file could put it over Cflag.
2027 if (Cflag
!= 0 && pcap_dump_ftell(dump_info
->p
) > Cflag
) {
2029 * Close the current file and open a new one.
2031 pcap_dump_close(dump_info
->p
);
2034 * Compress the file we just closed, if the user asked for it
2037 compress_savefile(dump_info
->CurrentFileName
);
2041 if (Cflag_count
>= Wflag
)
2044 if (dump_info
->CurrentFileName
!= NULL
)
2045 free(dump_info
->CurrentFileName
);
2046 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
2047 if (dump_info
->CurrentFileName
== NULL
)
2048 error("dump_packet_and_trunc: malloc");
2049 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, Cflag_count
, WflagChars
);
2050 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
2051 if (dump_info
->p
== NULL
)
2052 error("%s", pcap_geterr(pd
));
2055 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
2056 #ifdef HAVE_PCAP_DUMP_FLUSH
2058 pcap_dump_flush(dump_info
->p
);
2067 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
2073 pcap_dump(user
, h
, sp
);
2074 #ifdef HAVE_PCAP_DUMP_FLUSH
2076 pcap_dump_flush((pcap_dumper_t
*)user
);
2085 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
2087 struct print_info
*print_info
;
2089 netdissect_options
*ndo
;
2095 print_info
= (struct print_info
*)user
;
2096 ndo
= print_info
->ndo
;
2098 if(ndo
->ndo_packet_number
)
2099 ND_PRINT((ndo
, "%5u ", packets_captured
));
2101 ts_print(ndo
, &h
->ts
);
2104 * Some printers want to check that they're not walking off the
2105 * end of the packet.
2106 * Rather than pass it all the way down, we set this global.
2108 ndo
->ndo_snapend
= sp
+ h
->caplen
;
2110 if(print_info
->ndo_type
) {
2111 hdrlen
= (*print_info
->p
.ndo_printer
)(print_info
->ndo
, h
, sp
);
2113 hdrlen
= (*print_info
->p
.printer
)(h
, sp
);
2116 if (ndo
->ndo_Xflag
) {
2118 * Print the raw packet data in hex and ASCII.
2120 if (ndo
->ndo_Xflag
> 1) {
2122 * Include the link-layer header.
2124 hex_and_ascii_print(ndo
, "\n\t", sp
, h
->caplen
);
2127 * Don't include the link-layer header - and if
2128 * we have nothing past the link-layer header,
2131 if (h
->caplen
> hdrlen
)
2132 hex_and_ascii_print(ndo
, "\n\t", sp
+ hdrlen
,
2133 h
->caplen
- hdrlen
);
2135 } else if (ndo
->ndo_xflag
) {
2137 * Print the raw packet data in hex.
2139 if (ndo
->ndo_xflag
> 1) {
2141 * Include the link-layer header.
2143 hex_print(ndo
, "\n\t", sp
, h
->caplen
);
2146 * Don't include the link-layer header - and if
2147 * we have nothing past the link-layer header,
2150 if (h
->caplen
> hdrlen
)
2151 hex_print(ndo
, "\n\t", sp
+ hdrlen
,
2152 h
->caplen
- hdrlen
);
2154 } else if (ndo
->ndo_Aflag
) {
2156 * Print the raw packet data in ASCII.
2158 if (ndo
->ndo_Aflag
> 1) {
2160 * Include the link-layer header.
2162 ascii_print(ndo
, sp
, h
->caplen
);
2165 * Don't include the link-layer header - and if
2166 * we have nothing past the link-layer header,
2169 if (h
->caplen
> hdrlen
)
2170 ascii_print(ndo
, sp
+ hdrlen
, h
->caplen
- hdrlen
);
2183 * XXX - there should really be libpcap calls to get the version
2184 * number as a string (the string would be generated from #defines
2185 * at run time, so that it's not generated from string constants
2186 * in the library, as, on many UNIX systems, those constants would
2187 * be statically linked into the application executable image, and
2188 * would thus reflect the version of libpcap on the system on
2189 * which the application was *linked*, not the system on which it's
2192 * That routine should be documented, unlike the "version[]"
2193 * string, so that UNIX vendors providing their own libpcaps
2194 * don't omit it (as a couple of vendors have...).
2196 * Packet.dll should perhaps also export a routine to return the
2197 * version number of the Packet.dll code, to supply the
2198 * "Wpcap_version" information on Windows.
2200 char WDversion
[]="current-git.tcpdump.org";
2201 #if !defined(HAVE_GENERATED_VERSION)
2202 char version
[]="current-git.tcpdump.org";
2204 char pcap_version
[]="current-git.tcpdump.org";
2205 char Wpcap_version
[]="3.1";
2209 * By default, print the specified data out in hex and ASCII.
2212 ndo_default_print(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
2214 hex_and_ascii_print(ndo
, "\n\t", bp
, length
); /* pass on lf and identation string */
2218 default_print(const u_char
*bp
, u_int length
)
2220 ndo_default_print(gndo
, bp
, length
);
2223 #ifdef SIGNAL_REQ_INFO
2224 RETSIGTYPE
requestinfo(int signo _U_
)
2234 * Called once each second in verbose mode while dumping to file
2236 #ifdef USE_WIN32_MM_TIMER
2237 void CALLBACK
verbose_stats_dump (UINT timer_id _U_
, UINT msg _U_
, DWORD_PTR arg _U_
,
2238 DWORD_PTR dw1 _U_
, DWORD_PTR dw2 _U_
)
2240 struct pcap_stat stat
;
2242 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
2243 fprintf(stderr
, "Got %u\r", packets_captured
);
2245 #elif defined(HAVE_ALARM)
2246 static void verbose_stats_dump(int sig _U_
)
2248 struct pcap_stat stat
;
2250 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
2251 fprintf(stderr
, "Got %u\r", packets_captured
);
2259 extern char version
[];
2260 #ifndef HAVE_PCAP_LIB_VERSION
2261 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
2262 extern char pcap_version
[];
2263 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2264 static char pcap_version
[] = "unknown";
2265 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2266 #endif /* HAVE_PCAP_LIB_VERSION */
2268 #ifdef HAVE_PCAP_LIB_VERSION
2270 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2272 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2274 (void)fprintf(stderr
, "%s\n",pcap_lib_version());
2275 #else /* HAVE_PCAP_LIB_VERSION */
2277 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2278 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
2280 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2281 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
2283 #endif /* HAVE_PCAP_LIB_VERSION */
2290 (void)fprintf(stderr
,
2291 "Usage: %s [-aAbd" D_FLAG
"efhH" I_FLAG J_FLAG
"KlLnNOpqRStu" U_FLAG
"vxX]" B_FLAG_USAGE
" [ -c count ]\n", program_name
);
2292 (void)fprintf(stderr
,
2293 "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n");
2294 (void)fprintf(stderr
,
2295 "\t\t[ -i interface ]" j_FLAG_USAGE
" [ -M secret ] [ --number ]\n");
2296 #ifdef HAVE_PCAP_SETDIRECTION
2297 (void)fprintf(stderr
,
2298 "\t\t[ -Q in|out|inout ]\n");
2300 (void)fprintf(stderr
,
2301 "\t\t[ -r file ] [ -s snaplen ] [ -T type ] [ -V file ] [ -w file ]\n");
2302 (void)fprintf(stderr
,
2303 "\t\t[ -W filecount ] [ -y datalinktype ] [ -z command ]\n");
2304 (void)fprintf(stderr
,
2305 "\t\t[ -Z user ] [ expression ]\n");
2312 ndo_error(netdissect_options
*ndo _U_
, const char *fmt
, ...)
2316 (void)fprintf(stderr
, "%s: ", program_name
);
2318 (void)vfprintf(stderr
, fmt
, ap
);
2322 if (fmt
[-1] != '\n')
2323 (void)fputc('\n', stderr
);
2331 ndo_warning(netdissect_options
*ndo _U_
, const char *fmt
, ...)
2335 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
2337 (void)vfprintf(stderr
, fmt
, ap
);
2341 if (fmt
[-1] != '\n')
2342 (void)fputc('\n', stderr
);
2347 * c-style: whitesmith