2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 * Support for splitting captures into multiple files with a maximum
25 * Seth Webster <swebster@sst.ll.mit.edu>
29 static const char copyright
[] _U_
=
30 "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
31 The Regents of the University of California. All rights reserved.\n";
35 * tcpdump - monitor tcp/ip traffic on an ethernet.
37 * First written in 1987 by Van Jacobson, Lawrence Berkeley Laboratory.
38 * Mercilessly hacked and occasionally improved since then via the
39 * combined efforts of Van, Steve McCanne and Craig Leres of LBL.
46 #include <tcpdump-stdinc.h>
51 extern int strcasecmp (const char *__s1
, const char *__s2
);
69 #include <sys/resource.h>
75 /* capabilities convinience library */
78 #endif /* HAVE_CAP_NG_H */
80 #include "netdissect.h"
81 #include "interface.h"
82 #include "addrtoname.h"
84 #include "setsignal.h"
85 #include "gmt2local.h"
86 #include "pcap-missing.h"
93 #define SIGNAL_REQ_INFO SIGINFO
95 #define SIGNAL_REQ_INFO SIGUSR1
98 netdissect_options Gndo
;
99 netdissect_options
*gndo
= &Gndo
;
101 static int Dflag
; /* list available devices and exit */
102 static int dflag
; /* print filter code */
103 static int Lflag
; /* list available data link types and exit */
104 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
105 static int Jflag
; /* list available time stamp types */
107 #ifdef HAVE_PCAP_SETDIRECTION
108 int Qflag
= -1; /* restrict captured packet by send/receive direction */
110 static char *zflag
= NULL
; /* compress each savefile using a specified command (like gzip or bzip2) */
112 static int infodelay
;
113 static int infoprint
;
117 int32_t thiszone
; /* seconds offset from gmt to local time */
120 static RETSIGTYPE
cleanup(int);
121 static RETSIGTYPE
child_cleanup(int);
122 static void usage(void) __attribute__((noreturn
));
123 static void show_dlts_and_exit(const char *device
, pcap_t
*pd
) __attribute__((noreturn
));
125 static void print_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
126 static void ndo_default_print(netdissect_options
*, const u_char
*, u_int
);
127 static void dump_packet_and_trunc(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
128 static void dump_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
129 static void droproot(const char *, const char *);
130 static void ndo_error(netdissect_options
*ndo
, const char *fmt
, ...)
131 __attribute__((noreturn
))
132 #ifdef __ATTRIBUTE___FORMAT_OK
133 __attribute__((format (printf
, 2, 3)))
134 #endif /* __ATTRIBUTE___FORMAT_OK */
136 static void ndo_warning(netdissect_options
*ndo
, const char *fmt
, ...)
137 #ifdef __ATTRIBUTE___FORMAT_OK
138 __attribute__((format (printf
, 2, 3)))
139 #endif /* __ATTRIBUTE___FORMAT_OK */
142 #ifdef SIGNAL_REQ_INFO
143 RETSIGTYPE
requestinfo(int);
146 #if defined(USE_WIN32_MM_TIMER)
147 #include <MMsystem.h>
148 static UINT timer_id
;
149 static void CALLBACK
verbose_stats_dump(UINT
, UINT
, DWORD_PTR
, DWORD_PTR
, DWORD_PTR
);
150 #elif defined(HAVE_ALARM)
151 static void verbose_stats_dump(int sig
);
154 static void info(int);
155 static u_int packets_captured
;
169 static const struct printer printers
[] = {
170 { sl_if_print
, DLT_SLIP
},
171 #ifdef DLT_SLIP_BSDOS
172 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
174 { ppp_if_print
, DLT_PPP
},
175 #ifdef DLT_PPP_WITHDIRECTION
176 { ppp_if_print
, DLT_PPP_WITHDIRECTION
},
179 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
181 { atm_if_print
, DLT_ATM_RFC1483
},
182 #ifdef DLT_PPP_SERIAL
183 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
186 { sll_if_print
, DLT_LINUX_SLL
},
188 #ifdef DLT_IEEE802_11
189 { ieee802_11_if_print
, DLT_IEEE802_11
},
192 { ltalk_if_print
, DLT_LTALK
},
195 { fr_if_print
, DLT_FR
},
198 { fr_if_print
, DLT_FRELAY
},
201 { sunatm_if_print
, DLT_SUNATM
},
203 #ifdef DLT_PRISM_HEADER
204 { prism_if_print
, DLT_PRISM_HEADER
},
206 #ifdef DLT_IEEE802_11_RADIO
207 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
210 { enc_if_print
, DLT_ENC
},
212 #ifdef DLT_IEEE802_11_RADIO_AVS
213 { ieee802_11_radio_avs_if_print
, DLT_IEEE802_11_RADIO_AVS
},
215 #ifdef DLT_JUNIPER_ATM1
216 { juniper_atm1_print
, DLT_JUNIPER_ATM1
},
218 #ifdef DLT_JUNIPER_ATM2
219 { juniper_atm2_print
, DLT_JUNIPER_ATM2
},
221 #ifdef DLT_JUNIPER_MFR
222 { juniper_mfr_print
, DLT_JUNIPER_MFR
},
224 #ifdef DLT_JUNIPER_MLFR
225 { juniper_mlfr_print
, DLT_JUNIPER_MLFR
},
227 #ifdef DLT_JUNIPER_MLPPP
228 { juniper_mlppp_print
, DLT_JUNIPER_MLPPP
},
230 #ifdef DLT_JUNIPER_PPPOE
231 { juniper_pppoe_print
, DLT_JUNIPER_PPPOE
},
233 #ifdef DLT_JUNIPER_PPPOE_ATM
234 { juniper_pppoe_atm_print
, DLT_JUNIPER_PPPOE_ATM
},
236 #ifdef DLT_JUNIPER_GGSN
237 { juniper_ggsn_print
, DLT_JUNIPER_GGSN
},
239 #ifdef DLT_JUNIPER_ES
240 { juniper_es_print
, DLT_JUNIPER_ES
},
242 #ifdef DLT_JUNIPER_MONITOR
243 { juniper_monitor_print
, DLT_JUNIPER_MONITOR
},
245 #ifdef DLT_JUNIPER_SERVICES
246 { juniper_services_print
, DLT_JUNIPER_SERVICES
},
248 #ifdef DLT_JUNIPER_ETHER
249 { juniper_ether_print
, DLT_JUNIPER_ETHER
},
251 #ifdef DLT_JUNIPER_PPP
252 { juniper_ppp_print
, DLT_JUNIPER_PPP
},
254 #ifdef DLT_JUNIPER_FRELAY
255 { juniper_frelay_print
, DLT_JUNIPER_FRELAY
},
257 #ifdef DLT_JUNIPER_CHDLC
258 { juniper_chdlc_print
, DLT_JUNIPER_CHDLC
},
261 { mfr_if_print
, DLT_MFR
},
266 static const struct ndo_printer ndo_printers
[] = {
267 { ether_if_print
, DLT_EN10MB
},
269 { ipnet_if_print
, DLT_IPNET
},
271 #ifdef DLT_IEEE802_15_4
272 { ieee802_15_4_if_print
, DLT_IEEE802_15_4
},
274 #ifdef DLT_IEEE802_15_4_NOFCS
275 { ieee802_15_4_if_print
, DLT_IEEE802_15_4_NOFCS
},
278 { ppi_if_print
, DLT_PPI
},
280 #ifdef DLT_NETANALYZER
281 { netanalyzer_if_print
, DLT_NETANALYZER
},
283 #ifdef DLT_NETANALYZER_TRANSPARENT
284 { netanalyzer_transparent_if_print
, DLT_NETANALYZER_TRANSPARENT
},
286 #if defined(DLT_NFLOG) && defined(HAVE_PCAP_NFLOG_H)
287 { nflog_if_print
, DLT_NFLOG
},
290 { cip_if_print
, DLT_CIP
},
293 { cip_if_print
, DLT_ATM_CLIP
},
295 #ifdef DLT_IP_OVER_FC
296 { ipfc_if_print
, DLT_IP_OVER_FC
},
298 { null_if_print
, DLT_NULL
},
300 { null_if_print
, DLT_LOOP
},
302 #ifdef DLT_APPLE_IP_OVER_IEEE1394
303 { ap1394_if_print
, DLT_APPLE_IP_OVER_IEEE1394
},
305 #if defined(DLT_BLUETOOTH_HCI_H4_WITH_PHDR) && defined(HAVE_PCAP_BLUETOOTH_H)
306 { bt_if_print
, DLT_BLUETOOTH_HCI_H4_WITH_PHDR
},
309 { lane_if_print
, DLT_LANE8023
},
311 { arcnet_if_print
, DLT_ARCNET
},
312 #ifdef DLT_ARCNET_LINUX
313 { arcnet_linux_if_print
, DLT_ARCNET_LINUX
},
315 { raw_if_print
, DLT_RAW
},
317 { raw_if_print
, DLT_IPV4
},
320 { raw_if_print
, DLT_IPV6
},
322 #ifdef HAVE_PCAP_USB_H
324 { usb_linux_48_byte_print
, DLT_USB_LINUX
},
325 #endif /* DLT_USB_LINUX */
326 #ifdef DLT_USB_LINUX_MMAPPED
327 { usb_linux_64_byte_print
, DLT_USB_LINUX_MMAPPED
},
328 #endif /* DLT_USB_LINUX_MMAPPED */
329 #endif /* HAVE_PCAP_USB_H */
330 #ifdef DLT_SYMANTEC_FIREWALL
331 { symantec_if_print
, DLT_SYMANTEC_FIREWALL
},
334 { chdlc_if_print
, DLT_C_HDLC
},
337 { chdlc_if_print
, DLT_HDLC
},
340 { pppoe_if_print
, DLT_PPP_ETHER
},
342 #if defined(DLT_PFLOG) && defined(HAVE_NET_PFVAR_H)
343 { pflog_if_print
, DLT_PFLOG
},
345 { token_if_print
, DLT_IEEE802
},
346 { fddi_if_print
, DLT_FDDI
},
350 static const struct tok status_flags
[] = {
352 { PCAP_IF_UP
, "Up" },
354 #ifdef PCAP_IF_RUNNING
355 { PCAP_IF_RUNNING
, "Running" },
357 { PCAP_IF_LOOPBACK
, "Loopback" },
362 lookup_printer(int type
)
364 const struct printer
*p
;
366 for (p
= printers
; p
->f
; ++p
)
375 lookup_ndo_printer(int type
)
377 const struct ndo_printer
*p
;
379 for (p
= ndo_printers
; p
->f
; ++p
)
389 static int supports_monitor_mode
;
396 netdissect_options
*ndo
;
399 if_ndo_printer ndo_printer
;
406 char *CurrentFileName
;
411 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
413 show_tstamp_types_and_exit(const char *device
, pcap_t
*pd
)
416 int *tstamp_types
= 0;
417 const char *tstamp_type_name
;
420 n_tstamp_types
= pcap_list_tstamp_types(pd
, &tstamp_types
);
421 if (n_tstamp_types
< 0)
422 error("%s", pcap_geterr(pd
));
424 if (n_tstamp_types
== 0) {
425 fprintf(stderr
, "Time stamp type cannot be set for %s\n",
429 fprintf(stderr
, "Time stamp types for %s (use option -j to set):\n",
431 for (i
= 0; i
< n_tstamp_types
; i
++) {
432 tstamp_type_name
= pcap_tstamp_type_val_to_name(tstamp_types
[i
]);
433 if (tstamp_type_name
!= NULL
) {
434 (void) fprintf(stderr
, " %s (%s)\n", tstamp_type_name
,
435 pcap_tstamp_type_val_to_description(tstamp_types
[i
]));
437 (void) fprintf(stderr
, " %d\n", tstamp_types
[i
]);
440 pcap_free_tstamp_types(tstamp_types
);
446 show_dlts_and_exit(const char *device
, pcap_t
*pd
)
450 const char *dlt_name
;
452 n_dlts
= pcap_list_datalinks(pd
, &dlts
);
454 error("%s", pcap_geterr(pd
));
455 else if (n_dlts
== 0 || !dlts
)
456 error("No data link types.");
459 * If the interface is known to support monitor mode, indicate
460 * whether these are the data link types available when not in
461 * monitor mode, if -I wasn't specified, or when in monitor mode,
462 * when -I was specified (the link-layer types available in
463 * monitor mode might be different from the ones available when
464 * not in monitor mode).
466 if (supports_monitor_mode
)
467 (void) fprintf(stderr
, "Data link types for %s %s (use option -y to set):\n",
469 Iflag
? "when in monitor mode" : "when not in monitor mode");
471 (void) fprintf(stderr
, "Data link types for %s (use option -y to set):\n",
474 while (--n_dlts
>= 0) {
475 dlt_name
= pcap_datalink_val_to_name(dlts
[n_dlts
]);
476 if (dlt_name
!= NULL
) {
477 (void) fprintf(stderr
, " %s (%s)", dlt_name
,
478 pcap_datalink_val_to_description(dlts
[n_dlts
]));
481 * OK, does tcpdump handle that type?
483 if (lookup_printer(dlts
[n_dlts
]) == NULL
484 && lookup_ndo_printer(dlts
[n_dlts
]) == NULL
)
485 (void) fprintf(stderr
, " (printing not supported)");
486 fprintf(stderr
, "\n");
488 (void) fprintf(stderr
, " DLT %d (printing not supported)\n",
492 #ifdef HAVE_PCAP_FREE_DATALINKS
493 pcap_free_datalinks(dlts
);
498 #ifdef HAVE_PCAP_FINDALLDEVS
500 show_devices_and_exit (void)
502 pcap_if_t
*devpointer
;
503 char ebuf
[PCAP_ERRBUF_SIZE
];
506 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
509 for (i
= 0; devpointer
!= NULL
; i
++) {
510 printf("%d.%s", i
+1, devpointer
->name
);
511 if (devpointer
->description
!= NULL
)
512 printf(" (%s)", devpointer
->description
);
513 if (devpointer
->flags
!= 0)
514 printf(" [%s]", bittok2str(status_flags
, "none", devpointer
->flags
));
516 devpointer
= devpointer
->next
;
521 #endif /* HAVE_PCAP_FINDALLDEVS */
524 * Set up flags that might or might not be supported depending on the
525 * version of libpcap we're using.
527 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
529 #define B_FLAG_USAGE " [ -B size ]"
530 #else /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
533 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
535 #ifdef HAVE_PCAP_CREATE
537 #else /* HAVE_PCAP_CREATE */
539 #endif /* HAVE_PCAP_CREATE */
541 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
543 #define j_FLAG_USAGE " [ -j tstamptype ]"
545 #else /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
549 #endif /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
551 #ifdef HAVE_PCAP_FINDALLDEVS
552 #ifndef HAVE_PCAP_IF_T
553 #undef HAVE_PCAP_FINDALLDEVS
557 #ifdef HAVE_PCAP_FINDALLDEVS
563 #ifdef HAVE_PCAP_DUMP_FLUSH
569 #ifdef HAVE_PCAP_SETDIRECTION
576 /* Drop root privileges and chroot if necessary */
578 droproot(const char *username
, const char *chroot_dir
)
580 struct passwd
*pw
= NULL
;
582 if (chroot_dir
&& !username
) {
583 fprintf(stderr
, "tcpdump: Chroot without dropping root is insecure\n");
587 pw
= getpwnam(username
);
590 if (chroot(chroot_dir
) != 0 || chdir ("/") != 0) {
591 fprintf(stderr
, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
592 chroot_dir
, pcap_strerror(errno
));
597 int ret
= capng_change_id(pw
->pw_uid
, pw
->pw_gid
, CAPNG_NO_FLAG
);
599 printf("error : ret %d\n", ret
);
601 /* We don't need CAP_SETUID and CAP_SETGID */
602 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_SETUID
);
603 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_SETUID
);
604 capng_update(CAPNG_DROP
, CAPNG_PERMITTED
, CAP_SETUID
);
605 capng_update(CAPNG_DROP
, CAPNG_PERMITTED
, CAP_SETUID
);
606 capng_apply(CAPNG_SELECT_BOTH
);
609 if (initgroups(pw
->pw_name
, pw
->pw_gid
) != 0 ||
610 setgid(pw
->pw_gid
) != 0 || setuid(pw
->pw_uid
) != 0) {
611 fprintf(stderr
, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
613 (unsigned long)pw
->pw_uid
,
614 (unsigned long)pw
->pw_gid
,
615 pcap_strerror(errno
));
618 #endif /* HAVE_CAP_NG_H */
621 fprintf(stderr
, "tcpdump: Couldn't find user '%.32s'\n",
644 MakeFilename(char *buffer
, char *orig_name
, int cnt
, int max_chars
)
646 char *filename
= malloc(PATH_MAX
+ 1);
647 if (filename
== NULL
)
648 error("Makefilename: malloc");
650 /* Process with strftime if Gflag is set. */
654 /* Convert Gflag_time to a usable format */
655 if ((local_tm
= localtime(&Gflag_time
)) == NULL
) {
656 error("MakeTimedFilename: localtime");
659 /* There's no good way to detect an error in strftime since a return
660 * value of 0 isn't necessarily failure.
662 strftime(filename
, PATH_MAX
, orig_name
, local_tm
);
664 strncpy(filename
, orig_name
, PATH_MAX
);
667 if (cnt
== 0 && max_chars
== 0)
668 strncpy(buffer
, filename
, PATH_MAX
+ 1);
670 if (snprintf(buffer
, PATH_MAX
+ 1, "%s%0*d", filename
, max_chars
, cnt
) > PATH_MAX
)
671 /* Report an error if the filename is too large */
672 error("too many output files or filename is too long (> %d)", PATH_MAX
);
676 static int tcpdump_printf(netdissect_options
*ndo _U_
,
677 const char *fmt
, ...)
684 ret
=vfprintf(stdout
, fmt
, args
);
690 static struct print_info
691 get_print_info(int type
)
693 struct print_info printinfo
;
695 printinfo
.ndo_type
= 1;
696 printinfo
.ndo
= gndo
;
697 printinfo
.p
.ndo_printer
= lookup_ndo_printer(type
);
698 if (printinfo
.p
.ndo_printer
== NULL
) {
699 printinfo
.p
.printer
= lookup_printer(type
);
700 printinfo
.ndo_type
= 0;
701 if (printinfo
.p
.printer
== NULL
) {
702 gndo
->ndo_dltname
= pcap_datalink_val_to_name(type
);
703 if (gndo
->ndo_dltname
!= NULL
)
704 error("packet printing is not supported for link type %s: use -w",
707 error("packet printing is not supported for link type %d: use -w", type
);
714 get_next_file(FILE *VFile
, char *ptr
)
718 ret
= fgets(ptr
, PATH_MAX
, VFile
);
722 if (ptr
[strlen(ptr
) - 1] == '\n')
723 ptr
[strlen(ptr
) - 1] = '\0';
729 main(int argc
, char **argv
)
731 register int cnt
, op
, i
;
732 bpf_u_int32 localnet
=0 , netmask
= 0;
733 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *VFileName
, *WFileName
;
734 pcap_handler callback
;
738 const char *dlt_name
;
739 struct bpf_program fcode
;
741 RETSIGTYPE (*oldhandler
)(int);
743 struct print_info printinfo
;
744 struct dump_info dumpinfo
;
745 u_char
*pcap_userdata
;
746 char ebuf
[PCAP_ERRBUF_SIZE
];
747 char VFileLine
[PATH_MAX
+ 1];
748 char *username
= NULL
;
749 char *chroot_dir
= NULL
;
752 #ifdef HAVE_PCAP_FINDALLDEVS
753 pcap_if_t
*devpointer
;
759 if(wsockinit() != 0) return 1;
762 jflag
=-1; /* not set */
766 gndo
->ndo_default_print
=ndo_default_print
;
767 gndo
->ndo_printf
=tcpdump_printf
;
768 gndo
->ndo_error
=ndo_error
;
769 gndo
->ndo_warning
=ndo_warning
;
770 gndo
->ndo_snaplen
= DEFAULT_SNAPLEN
;
780 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
781 program_name
= cp
+ 1;
783 program_name
= argv
[0];
785 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
793 (op
= getopt(argc
, argv
, "aAb" B_FLAG
"c:C:d" D_FLAG
"eE:fF:G:hHi:" I_FLAG j_FLAG J_FLAG
"KlLm:M:nNOpq" Q_FLAG
"r:Rs:StT:u" U_FLAG
"vV:w:W:xXy:Yz:Z:")) != -1)
797 /* compatibility for old -a */
808 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
810 Bflag
= atoi(optarg
)*1024;
812 error("invalid packet buffer size %s", optarg
);
814 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
819 error("invalid packet count %s", optarg
);
823 Cflag
= atoi(optarg
) * 1000000;
825 error("invalid file size %s", optarg
);
845 #ifndef HAVE_LIBCRYPTO
846 warning("crypto code not compiled in");
848 gndo
->ndo_espsecret
= optarg
;
860 Gflag
= atoi(optarg
);
862 error("invalid number of seconds %s", optarg
);
864 /* We will create one file initially. */
867 /* Grab the current time for rotation use. */
868 if ((Gflag_time
= time(NULL
)) == (time_t)-1) {
869 error("main: can't get current time: %s",
870 pcap_strerror(errno
));
883 if (optarg
[0] == '0' && optarg
[1] == 0)
884 error("Invalid adapter index");
886 #ifdef HAVE_PCAP_FINDALLDEVS
888 * If the argument is a number, treat it as
889 * an index into the list of adapters, as
890 * printed by "tcpdump -D".
892 * This should be OK on UNIX systems, as interfaces
893 * shouldn't have names that begin with digits.
894 * It can be useful on Windows, where more than
895 * one interface can have the same name.
897 devnum
= strtol(optarg
, &end
, 10);
898 if (optarg
!= end
&& *end
== '\0') {
900 error("Invalid adapter index");
902 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
906 * Look for the devnum-th entry
907 * in the list of devices
911 i
< devnum
-1 && devpointer
!= NULL
;
912 i
++, devpointer
= devpointer
->next
)
914 if (devpointer
== NULL
)
915 error("Invalid adapter index");
917 device
= devpointer
->name
;
920 #endif /* HAVE_PCAP_FINDALLDEVS */
924 #ifdef HAVE_PCAP_CREATE
928 #endif /* HAVE_PCAP_CREATE */
930 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
932 jflag
= pcap_tstamp_type_name_to_val(optarg
);
934 error("invalid time stamp type %s", optarg
);
945 * _IOLBF is the same as _IOFBF in Microsoft's C
946 * libraries; the only alternative they offer
949 * XXX - this should really be checking for MSVC++,
950 * not WIN32, if, for example, MinGW has its own
951 * C library that is more UNIX-compatible.
953 setvbuf(stdout
, NULL
, _IONBF
, 0);
955 #ifdef HAVE_SETLINEBUF
958 setvbuf(stdout
, NULL
, _IOLBF
, 0);
969 if (smiLoadModule(optarg
) == 0) {
970 error("could not load MIB module %s", optarg
);
974 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
975 program_name
, optarg
);
976 (void)fprintf(stderr
, "(no libsmi support)\n");
981 /* TCP-MD5 shared secret */
982 #ifndef HAVE_LIBCRYPTO
983 warning("crypto code not compiled in");
1006 ++suppress_default_print
;
1009 #ifdef HAVE_PCAP_SETDIRECTION
1011 if (strcasecmp(optarg
, "in") == 0)
1013 else if (strcasecmp(optarg
, "out") == 0)
1015 else if (strcasecmp(optarg
, "inout") == 0)
1016 Qflag
= PCAP_D_INOUT
;
1018 error("unknown capture direction `%s'", optarg
);
1020 #endif /* HAVE_PCAP_SETDIRECTION */
1031 snaplen
= strtol(optarg
, &end
, 0);
1032 if (optarg
== end
|| *end
!= '\0'
1033 || snaplen
< 0 || snaplen
> MAXIMUM_SNAPLEN
)
1034 error("invalid snaplen %s", optarg
);
1035 else if (snaplen
== 0)
1036 snaplen
= MAXIMUM_SNAPLEN
;
1048 if (strcasecmp(optarg
, "vat") == 0)
1049 packettype
= PT_VAT
;
1050 else if (strcasecmp(optarg
, "wb") == 0)
1052 else if (strcasecmp(optarg
, "rpc") == 0)
1053 packettype
= PT_RPC
;
1054 else if (strcasecmp(optarg
, "rtp") == 0)
1055 packettype
= PT_RTP
;
1056 else if (strcasecmp(optarg
, "rtcp") == 0)
1057 packettype
= PT_RTCP
;
1058 else if (strcasecmp(optarg
, "snmp") == 0)
1059 packettype
= PT_SNMP
;
1060 else if (strcasecmp(optarg
, "cnfp") == 0)
1061 packettype
= PT_CNFP
;
1062 else if (strcasecmp(optarg
, "tftp") == 0)
1063 packettype
= PT_TFTP
;
1064 else if (strcasecmp(optarg
, "aodv") == 0)
1065 packettype
= PT_AODV
;
1066 else if (strcasecmp(optarg
, "carp") == 0)
1067 packettype
= PT_CARP
;
1068 else if (strcasecmp(optarg
, "radius") == 0)
1069 packettype
= PT_RADIUS
;
1070 else if (strcasecmp(optarg
, "zmtp1") == 0)
1071 packettype
= PT_ZMTP1
;
1072 else if (strcasecmp(optarg
, "vxlan") == 0)
1073 packettype
= PT_VXLAN
;
1074 else if (strcasecmp(optarg
, "pgm") == 0)
1075 packettype
= PT_PGM
;
1076 else if (strcasecmp(optarg
, "pgm_zmtp1") == 0)
1077 packettype
= PT_PGM_ZMTP1
;
1078 else if (strcasecmp(optarg
, "lmp") == 0)
1079 packettype
= PT_LMP
;
1081 error("unknown packet type `%s'", optarg
);
1088 #ifdef HAVE_PCAP_DUMP_FLUSH
1107 Wflag
= atoi(optarg
);
1109 error("invalid number of output files %s", optarg
);
1110 WflagChars
= getWflagChars(Wflag
);
1115 ++suppress_default_print
;
1120 ++suppress_default_print
;
1124 gndo
->ndo_dltname
= optarg
;
1126 pcap_datalink_name_to_val(gndo
->ndo_dltname
);
1127 if (gndo
->ndo_dlt
< 0)
1128 error("invalid data link type %s", gndo
->ndo_dltname
);
1131 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
1134 /* Undocumented flag */
1135 #ifdef HAVE_PCAP_DEBUG
1136 extern int pcap_debug
;
1146 zflag
= strdup(optarg
);
1150 username
= strdup(optarg
);
1158 #ifdef HAVE_PCAP_FINDALLDEVS
1160 show_devices_and_exit();
1165 case 0: /* Default */
1166 case 4: /* Default + Date*/
1167 thiszone
= gmt2local(0);
1170 case 1: /* No time stamp */
1171 case 2: /* Unix timeval style */
1172 case 3: /* Microseconds since previous packet */
1173 case 5: /* Microseconds since first packet */
1176 default: /* Not supported */
1177 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
1181 if (fflag
!= 0 && (VFileName
!= NULL
|| RFileName
!= NULL
))
1182 error("-f can not be used with -V or -r");
1184 if (VFileName
!= NULL
&& RFileName
!= NULL
)
1185 error("-V and -r are mutually exclusive.");
1188 /* if run as root, prepare for chrooting */
1189 if (getuid() == 0 || geteuid() == 0) {
1190 /* future extensibility for cmd-line arguments */
1192 chroot_dir
= WITH_CHROOT
;
1197 /* if run as root, prepare for dropping root privileges */
1198 if (getuid() == 0 || geteuid() == 0) {
1199 /* Run with '-Z root' to restore old behaviour */
1201 username
= WITH_USER
;
1205 if (RFileName
!= NULL
|| VFileName
!= NULL
) {
1207 * If RFileName is non-null, it's the pathname of a
1208 * savefile to read. If VFileName is non-null, it's
1209 * the pathname of a file containing a list of pathnames
1210 * (one per line) of savefiles to read.
1212 * In either case, we're reading a savefile, not doing
1217 * We don't need network access, so relinquish any set-UID
1218 * or set-GID privileges we have (if any).
1220 * We do *not* want set-UID privileges when opening a
1221 * trace file, as that might let the user read other
1222 * people's trace files (especially if we're set-UID
1225 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
1226 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1228 if (VFileName
!= NULL
) {
1229 if (VFileName
[0] == '-' && VFileName
[1] == '\0')
1232 VFile
= fopen(VFileName
, "r");
1235 error("Unable to open file: %s\n", strerror(errno
));
1237 ret
= get_next_file(VFile
, VFileLine
);
1239 error("Nothing in %s\n", VFileName
);
1240 RFileName
= VFileLine
;
1243 pd
= pcap_open_offline(RFileName
, ebuf
);
1246 dlt
= pcap_datalink(pd
);
1247 dlt_name
= pcap_datalink_val_to_name(dlt
);
1248 if (dlt_name
== NULL
) {
1249 fprintf(stderr
, "reading from file %s, link-type %u\n",
1253 "reading from file %s, link-type %s (%s)\n",
1254 RFileName
, dlt_name
,
1255 pcap_datalink_val_to_description(dlt
));
1259 * We're doing a live capture.
1261 if (device
== NULL
) {
1262 device
= pcap_lookupdev(ebuf
);
1268 * Print a message to the standard error on Windows.
1269 * XXX - why do it here, with a different message?
1271 if(strlen(device
) == 1) /* we assume that an ASCII string is always longer than 1 char */
1272 { /* a Unicode string has a \0 as second byte (so strlen() is 1) */
1273 fprintf(stderr
, "%s: listening on %ws\n", program_name
, device
);
1277 fprintf(stderr
, "%s: listening on %s\n", program_name
, device
);
1282 #ifdef HAVE_PCAP_CREATE
1283 pd
= pcap_create(device
, ebuf
);
1286 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1288 show_tstamp_types_and_exit(device
, pd
);
1291 * Is this an interface that supports monitor mode?
1293 if (pcap_can_set_rfmon(pd
) == 1)
1294 supports_monitor_mode
= 1;
1296 supports_monitor_mode
= 0;
1297 status
= pcap_set_snaplen(pd
, snaplen
);
1299 error("%s: Can't set snapshot length: %s",
1300 device
, pcap_statustostr(status
));
1301 status
= pcap_set_promisc(pd
, !pflag
);
1303 error("%s: Can't set promiscuous mode: %s",
1304 device
, pcap_statustostr(status
));
1306 status
= pcap_set_rfmon(pd
, 1);
1308 error("%s: Can't set monitor mode: %s",
1309 device
, pcap_statustostr(status
));
1311 status
= pcap_set_timeout(pd
, 1000);
1313 error("%s: pcap_set_timeout failed: %s",
1314 device
, pcap_statustostr(status
));
1316 status
= pcap_set_buffer_size(pd
, Bflag
);
1318 error("%s: Can't set buffer size: %s",
1319 device
, pcap_statustostr(status
));
1321 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1323 status
= pcap_set_tstamp_type(pd
, jflag
);
1325 error("%s: Can't set time stamp type: %s",
1326 device
, pcap_statustostr(status
));
1329 status
= pcap_activate(pd
);
1332 * pcap_activate() failed.
1334 cp
= pcap_geterr(pd
);
1335 if (status
== PCAP_ERROR
)
1337 else if ((status
== PCAP_ERROR_NO_SUCH_DEVICE
||
1338 status
== PCAP_ERROR_PERM_DENIED
) &&
1340 error("%s: %s\n(%s)", device
,
1341 pcap_statustostr(status
), cp
);
1343 error("%s: %s", device
,
1344 pcap_statustostr(status
));
1345 } else if (status
> 0) {
1347 * pcap_activate() succeeded, but it's warning us
1348 * of a problem it had.
1350 cp
= pcap_geterr(pd
);
1351 if (status
== PCAP_WARNING
)
1353 else if (status
== PCAP_WARNING_PROMISC_NOTSUP
&&
1355 warning("%s: %s\n(%s)", device
,
1356 pcap_statustostr(status
), cp
);
1358 warning("%s: %s", device
,
1359 pcap_statustostr(status
));
1361 #ifdef HAVE_PCAP_SETDIRECTION
1363 status
= pcap_setdirection(pd
, Qflag
);
1365 error("%s: pcap_setdirection() failed: %s",
1366 device
, pcap_geterr(pd
));
1368 #endif /* HAVE_PCAP_SETDIRECTION */
1371 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
1375 warning("%s", ebuf
);
1376 #endif /* HAVE_PCAP_CREATE */
1378 * Let user own process after socket has been opened.
1381 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
1382 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1384 #if !defined(HAVE_PCAP_CREATE) && defined(WIN32)
1386 if(pcap_setbuff(pd
, Bflag
)==-1){
1387 error("%s", pcap_geterr(pd
));
1389 #endif /* !defined(HAVE_PCAP_CREATE) && defined(WIN32) */
1391 show_dlts_and_exit(device
, pd
);
1392 if (gndo
->ndo_dlt
>= 0) {
1393 #ifdef HAVE_PCAP_SET_DATALINK
1394 if (pcap_set_datalink(pd
, gndo
->ndo_dlt
) < 0)
1395 error("%s", pcap_geterr(pd
));
1398 * We don't actually support changing the
1399 * data link type, so we only let them
1400 * set it to what it already is.
1402 if (gndo
->ndo_dlt
!= pcap_datalink(pd
)) {
1403 error("%s is not one of the DLTs supported by this device\n",
1407 (void)fprintf(stderr
, "%s: data link type %s\n",
1408 program_name
, gndo
->ndo_dltname
);
1409 (void)fflush(stderr
);
1411 i
= pcap_snapshot(pd
);
1413 warning("snaplen raised from %d to %d", snaplen
, i
);
1417 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
1418 warning("foreign (-f) flag used but: %s", ebuf
);
1424 cmdbuf
= read_infile(infile
);
1426 cmdbuf
= copy_argv(&argv
[optind
]);
1428 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
1429 error("%s", pcap_geterr(pd
));
1431 bpf_dump(&fcode
, dflag
);
1436 init_addrtoname(localnet
, netmask
);
1440 (void)setsignal(SIGPIPE
, cleanup
);
1441 (void)setsignal(SIGTERM
, cleanup
);
1442 (void)setsignal(SIGINT
, cleanup
);
1444 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1445 (void)setsignal(SIGCHLD
, child_cleanup
);
1447 /* Cooperate with nohup(1) */
1449 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
1450 (void)setsignal(SIGHUP
, oldhandler
);
1455 * If a user name was specified with "-Z", attempt to switch to
1456 * that user's UID. This would probably be used with sudo,
1457 * to allow tcpdump to be run in a special restricted
1458 * account (if you just want to allow users to open capture
1459 * devices, and can't just give users that permission,
1460 * you'd make tcpdump set-UID or set-GID).
1462 * Tcpdump doesn't necessarily write only to one savefile;
1463 * the general only way to allow a -Z instance to write to
1464 * savefiles as the user under whose UID it's run, rather
1465 * than as the user specified with -Z, would thus be to switch
1466 * to the original user ID before opening a capture file and
1467 * then switch back to the -Z user ID after opening the savefile.
1468 * Switching to the -Z user ID only after opening the first
1469 * savefile doesn't handle the general case.
1472 #ifdef HAVE_CAP_NG_H
1473 /* We are running as root and we will be writing to savefile */
1474 if ((getuid() == 0 || geteuid() == 0) && WFileName
) {
1476 /* Drop all capabilities from effective set */
1477 capng_clear(CAPNG_EFFECTIVE
);
1478 /* Add capabilities we will need*/
1479 capng_update(CAPNG_ADD
, CAPNG_PERMITTED
, CAP_SETUID
);
1480 capng_update(CAPNG_ADD
, CAPNG_PERMITTED
, CAP_SETGID
);
1481 capng_update(CAPNG_ADD
, CAPNG_PERMITTED
, CAP_DAC_OVERRIDE
);
1483 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_SETUID
);
1484 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_SETGID
);
1485 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
1487 capng_apply(CAPNG_SELECT_BOTH
);
1490 #endif /* HAVE_CAP_NG_H */
1492 if (getuid() == 0 || geteuid() == 0) {
1493 if (username
|| chroot_dir
)
1494 droproot(username
, chroot_dir
);
1499 if (pcap_setfilter(pd
, &fcode
) < 0)
1500 error("%s", pcap_geterr(pd
));
1503 /* Do not exceed the default PATH_MAX for files. */
1504 dumpinfo
.CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
1506 if (dumpinfo
.CurrentFileName
== NULL
)
1507 error("malloc of dumpinfo.CurrentFileName");
1509 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1511 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, WflagChars
);
1513 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, 0);
1515 p
= pcap_dump_open(pd
, dumpinfo
.CurrentFileName
);
1516 #ifdef HAVE_CAP_NG_H
1517 /* Give up capabilities, clear Effective set */
1518 capng_clear(CAPNG_EFFECTIVE
);
1521 error("%s", pcap_geterr(pd
));
1522 if (Cflag
!= 0 || Gflag
!= 0) {
1523 callback
= dump_packet_and_trunc
;
1524 dumpinfo
.WFileName
= WFileName
;
1527 pcap_userdata
= (u_char
*)&dumpinfo
;
1529 callback
= dump_packet
;
1530 pcap_userdata
= (u_char
*)p
;
1532 #ifdef HAVE_PCAP_DUMP_FLUSH
1537 type
= pcap_datalink(pd
);
1538 printinfo
= get_print_info(type
);
1539 callback
= print_packet
;
1540 pcap_userdata
= (u_char
*)&printinfo
;
1543 #ifdef SIGNAL_REQ_INFO
1545 * We can't get statistics when reading from a file rather
1546 * than capturing from a device.
1548 if (RFileName
== NULL
)
1549 (void)setsignal(SIGNAL_REQ_INFO
, requestinfo
);
1552 if (vflag
> 0 && WFileName
) {
1554 * When capturing to a file, "-v" means tcpdump should,
1555 * every 10 secodns, "v"erbosely report the number of
1558 #ifdef USE_WIN32_MM_TIMER
1559 /* call verbose_stats_dump() each 1000 +/-100msec */
1560 timer_id
= timeSetEvent(1000, 100, verbose_stats_dump
, 0, TIME_PERIODIC
);
1561 setvbuf(stderr
, NULL
, _IONBF
, 0);
1562 #elif defined(HAVE_ALARM)
1563 (void)setsignal(SIGALRM
, verbose_stats_dump
);
1569 if (RFileName
== NULL
) {
1571 * Live capture (if -V was specified, we set RFileName
1572 * to a file from the -V file). Print a message to
1573 * the standard error on UN*X.
1575 if (!vflag
&& !WFileName
) {
1576 (void)fprintf(stderr
,
1577 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1580 (void)fprintf(stderr
, "%s: ", program_name
);
1581 dlt
= pcap_datalink(pd
);
1582 dlt_name
= pcap_datalink_val_to_name(dlt
);
1583 if (dlt_name
== NULL
) {
1584 (void)fprintf(stderr
, "listening on %s, link-type %u, capture size %u bytes\n",
1585 device
, dlt
, snaplen
);
1587 (void)fprintf(stderr
, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1589 pcap_datalink_val_to_description(dlt
), snaplen
);
1591 (void)fflush(stderr
);
1595 status
= pcap_loop(pd
, cnt
, callback
, pcap_userdata
);
1596 if (WFileName
== NULL
) {
1598 * We're printing packets. Flush the printed output,
1599 * so it doesn't get intermingled with error output.
1603 * We got interrupted, so perhaps we didn't
1604 * manage to finish a line we were printing.
1605 * Print an extra newline, just in case.
1609 (void)fflush(stdout
);
1613 * We got interrupted. If we are reading multiple
1614 * files (via -V) set these so that we stop.
1623 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
1624 program_name
, pcap_geterr(pd
));
1626 if (RFileName
== NULL
) {
1628 * We're doing a live capture. Report the capture
1634 if (VFileName
!= NULL
) {
1635 ret
= get_next_file(VFile
, VFileLine
);
1637 RFileName
= VFileLine
;
1638 pd
= pcap_open_offline(RFileName
, ebuf
);
1641 new_dlt
= pcap_datalink(pd
);
1642 if (WFileName
&& new_dlt
!= dlt
)
1643 error("%s: new dlt does not match original", RFileName
);
1644 printinfo
= get_print_info(new_dlt
);
1645 dlt_name
= pcap_datalink_val_to_name(new_dlt
);
1646 if (dlt_name
== NULL
) {
1647 fprintf(stderr
, "reading from file %s, link-type %u\n",
1648 RFileName
, new_dlt
);
1651 "reading from file %s, link-type %s (%s)\n",
1652 RFileName
, dlt_name
,
1653 pcap_datalink_val_to_description(new_dlt
));
1655 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
1656 error("%s", pcap_geterr(pd
));
1657 if (pcap_setfilter(pd
, &fcode
) < 0)
1658 error("%s", pcap_geterr(pd
));
1662 while (ret
!= NULL
);
1665 exit(status
== -1 ? 1 : 0);
1668 /* make a clean exit on interrupts */
1670 cleanup(int signo _U_
)
1672 #ifdef USE_WIN32_MM_TIMER
1674 timeKillEvent(timer_id
);
1676 #elif defined(HAVE_ALARM)
1680 #ifdef HAVE_PCAP_BREAKLOOP
1682 * We have "pcap_breakloop()"; use it, so that we do as little
1683 * as possible in the signal handler (it's probably not safe
1684 * to do anything with standard I/O streams in a signal handler -
1685 * the ANSI C standard doesn't say it is).
1690 * We don't have "pcap_breakloop()"; this isn't safe, but
1691 * it's the best we can do. Print the summary if we're
1692 * not reading from a savefile - i.e., if we're doing a
1693 * live capture - and exit.
1695 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
1697 * We got interrupted, so perhaps we didn't
1698 * manage to finish a line we were printing.
1699 * Print an extra newline, just in case.
1702 (void)fflush(stdout
);
1710 On windows, we do not use a fork, so we do not care less about
1711 waiting a child processes to die
1713 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1715 child_cleanup(int signo _U_
)
1719 #endif /* HAVE_FORK && HAVE_VFORK */
1722 info(register int verbose
)
1724 struct pcap_stat stat
;
1727 * Older versions of libpcap didn't set ps_ifdrop on some
1728 * platforms; initialize it to 0 to handle that.
1731 if (pcap_stats(pd
, &stat
) < 0) {
1732 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
1738 fprintf(stderr
, "%s: ", program_name
);
1740 (void)fprintf(stderr
, "%u packet%s captured", packets_captured
,
1741 PLURAL_SUFFIX(packets_captured
));
1743 fputs(", ", stderr
);
1746 (void)fprintf(stderr
, "%u packet%s received by filter", stat
.ps_recv
,
1747 PLURAL_SUFFIX(stat
.ps_recv
));
1749 fputs(", ", stderr
);
1752 (void)fprintf(stderr
, "%u packet%s dropped by kernel", stat
.ps_drop
,
1753 PLURAL_SUFFIX(stat
.ps_drop
));
1754 if (stat
.ps_ifdrop
!= 0) {
1756 fputs(", ", stderr
);
1759 (void)fprintf(stderr
, "%u packet%s dropped by interface\n",
1760 stat
.ps_ifdrop
, PLURAL_SUFFIX(stat
.ps_ifdrop
));
1766 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1768 compress_savefile(const char *filename
)
1777 * Set to lowest priority so that this doesn't disturb the capture
1780 setpriority(PRIO_PROCESS
, 0, NZERO
- 1);
1782 setpriority(PRIO_PROCESS
, 0, 19);
1784 if (execlp(zflag
, zflag
, filename
, (char *)NULL
) == -1)
1786 "compress_savefile:execlp(%s, %s): %s\n",
1796 #else /* HAVE_FORK && HAVE_VFORK */
1798 compress_savefile(const char *filename
)
1801 "compress_savefile failed. Functionality not implemented under your system\n");
1803 #endif /* HAVE_FORK && HAVE_VFORK */
1806 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1808 struct dump_info
*dump_info
;
1814 dump_info
= (struct dump_info
*)user
;
1817 * XXX - this won't force the file to rotate on the specified time
1818 * boundary, but it will rotate on the first packet received after the
1819 * specified Gflag number of seconds. Note: if a Gflag time boundary
1820 * and a Cflag size boundary coincide, the time rotation will occur
1821 * first thereby cancelling the Cflag boundary (since the file should
1825 /* Check if it is time to rotate */
1828 /* Get the current time */
1829 if ((t
= time(NULL
)) == (time_t)-1) {
1830 error("dump_and_trunc_packet: can't get current_time: %s",
1831 pcap_strerror(errno
));
1835 /* If the time is greater than the specified window, rotate */
1836 if (t
- Gflag_time
>= Gflag
) {
1837 /* Update the Gflag_time */
1839 /* Update Gflag_count */
1842 * Close the current file and open a new one.
1844 pcap_dump_close(dump_info
->p
);
1847 * Compress the file we just closed, if the user asked for it
1850 compress_savefile(dump_info
->CurrentFileName
);
1853 * Check to see if we've exceeded the Wflag (when
1856 if (Cflag
== 0 && Wflag
> 0 && Gflag_count
>= Wflag
) {
1857 (void)fprintf(stderr
, "Maximum file limit reached: %d\n",
1862 if (dump_info
->CurrentFileName
!= NULL
)
1863 free(dump_info
->CurrentFileName
);
1864 /* Allocate space for max filename + \0. */
1865 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
1866 if (dump_info
->CurrentFileName
== NULL
)
1867 error("dump_packet_and_trunc: malloc");
1869 * Gflag was set otherwise we wouldn't be here. Reset the count
1870 * so multiple files would end with 1,2,3 in the filename.
1871 * The counting is handled with the -C flow after this.
1876 * This is always the first file in the Cflag
1878 * We also don't need numbering if Cflag is not set.
1881 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0,
1884 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0, 0);
1886 #ifdef HAVE_CAP_NG_H
1887 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
1888 capng_apply(CAPNG_EFFECTIVE
);
1889 #endif /* HAVE_CAP_NG_H */
1890 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
1891 #ifdef HAVE_CAP_NG_H
1892 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
1893 capng_apply(CAPNG_EFFECTIVE
);
1894 #endif /* HAVE_CAP_NG_H */
1895 if (dump_info
->p
== NULL
)
1896 error("%s", pcap_geterr(pd
));
1901 * XXX - this won't prevent capture files from getting
1902 * larger than Cflag - the last packet written to the
1903 * file could put it over Cflag.
1905 if (Cflag
!= 0 && pcap_dump_ftell(dump_info
->p
) > Cflag
) {
1907 * Close the current file and open a new one.
1909 pcap_dump_close(dump_info
->p
);
1912 * Compress the file we just closed, if the user asked for it
1915 compress_savefile(dump_info
->CurrentFileName
);
1919 if (Cflag_count
>= Wflag
)
1922 if (dump_info
->CurrentFileName
!= NULL
)
1923 free(dump_info
->CurrentFileName
);
1924 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
1925 if (dump_info
->CurrentFileName
== NULL
)
1926 error("dump_packet_and_trunc: malloc");
1927 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, Cflag_count
, WflagChars
);
1928 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
1929 if (dump_info
->p
== NULL
)
1930 error("%s", pcap_geterr(pd
));
1933 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
1934 #ifdef HAVE_PCAP_DUMP_FLUSH
1936 pcap_dump_flush(dump_info
->p
);
1945 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1951 pcap_dump(user
, h
, sp
);
1952 #ifdef HAVE_PCAP_DUMP_FLUSH
1954 pcap_dump_flush((pcap_dumper_t
*)user
);
1963 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1965 struct print_info
*print_info
;
1967 netdissect_options
*ndo
;
1974 print_info
= (struct print_info
*)user
;
1975 ndo
= print_info
->ndo
;
1978 * Some printers want to check that they're not walking off the
1979 * end of the packet.
1980 * Rather than pass it all the way down, we set this global.
1982 ndo
->ndo_snapend
= sp
+ h
->caplen
;
1984 if(print_info
->ndo_type
) {
1985 hdrlen
= (*print_info
->p
.ndo_printer
)(print_info
->ndo
, h
, sp
);
1987 hdrlen
= (*print_info
->p
.printer
)(h
, sp
);
1990 if (ndo
->ndo_Xflag
) {
1992 * Print the raw packet data in hex and ASCII.
1994 if (ndo
->ndo_Xflag
> 1) {
1996 * Include the link-layer header.
1998 hex_and_ascii_print(ndo
, "\n\t", sp
, h
->caplen
);
2001 * Don't include the link-layer header - and if
2002 * we have nothing past the link-layer header,
2005 if (h
->caplen
> hdrlen
)
2006 hex_and_ascii_print(ndo
, "\n\t", sp
+ hdrlen
,
2007 h
->caplen
- hdrlen
);
2009 } else if (ndo
->ndo_xflag
) {
2011 * Print the raw packet data in hex.
2013 if (ndo
->ndo_xflag
> 1) {
2015 * Include the link-layer header.
2017 hex_print(ndo
, "\n\t", sp
, h
->caplen
);
2020 * Don't include the link-layer header - and if
2021 * we have nothing past the link-layer header,
2024 if (h
->caplen
> hdrlen
)
2025 hex_print(ndo
, "\n\t", sp
+ hdrlen
,
2026 h
->caplen
- hdrlen
);
2028 } else if (ndo
->ndo_Aflag
) {
2030 * Print the raw packet data in ASCII.
2032 if (ndo
->ndo_Aflag
> 1) {
2034 * Include the link-layer header.
2036 ascii_print(sp
, h
->caplen
);
2039 * Don't include the link-layer header - and if
2040 * we have nothing past the link-layer header,
2043 if (h
->caplen
> hdrlen
)
2044 ascii_print(sp
+ hdrlen
, h
->caplen
- hdrlen
);
2057 * XXX - there should really be libpcap calls to get the version
2058 * number as a string (the string would be generated from #defines
2059 * at run time, so that it's not generated from string constants
2060 * in the library, as, on many UNIX systems, those constants would
2061 * be statically linked into the application executable image, and
2062 * would thus reflect the version of libpcap on the system on
2063 * which the application was *linked*, not the system on which it's
2066 * That routine should be documented, unlike the "version[]"
2067 * string, so that UNIX vendors providing their own libpcaps
2068 * don't omit it (as a couple of vendors have...).
2070 * Packet.dll should perhaps also export a routine to return the
2071 * version number of the Packet.dll code, to supply the
2072 * "Wpcap_version" information on Windows.
2074 char WDversion
[]="current-git.tcpdump.org";
2075 #if !defined(HAVE_GENERATED_VERSION)
2076 char version
[]="current-git.tcpdump.org";
2078 char pcap_version
[]="current-git.tcpdump.org";
2079 char Wpcap_version
[]="3.1";
2083 * By default, print the specified data out in hex and ASCII.
2086 ndo_default_print(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
2088 hex_and_ascii_print(ndo
, "\n\t", bp
, length
); /* pass on lf and identation string */
2092 default_print(const u_char
*bp
, u_int length
)
2094 ndo_default_print(gndo
, bp
, length
);
2097 #ifdef SIGNAL_REQ_INFO
2098 RETSIGTYPE
requestinfo(int signo _U_
)
2108 * Called once each second in verbose mode while dumping to file
2110 #ifdef USE_WIN32_MM_TIMER
2111 void CALLBACK
verbose_stats_dump (UINT timer_id _U_
, UINT msg _U_
, DWORD_PTR arg _U_
,
2112 DWORD_PTR dw1 _U_
, DWORD_PTR dw2 _U_
)
2114 struct pcap_stat stat
;
2116 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
2117 fprintf(stderr
, "Got %u\r", packets_captured
);
2119 #elif defined(HAVE_ALARM)
2120 static void verbose_stats_dump(int sig _U_
)
2122 struct pcap_stat stat
;
2124 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
2125 fprintf(stderr
, "Got %u\r", packets_captured
);
2133 extern char version
[];
2134 #ifndef HAVE_PCAP_LIB_VERSION
2135 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
2136 extern char pcap_version
[];
2137 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2138 static char pcap_version
[] = "unknown";
2139 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2140 #endif /* HAVE_PCAP_LIB_VERSION */
2142 #ifdef HAVE_PCAP_LIB_VERSION
2144 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2146 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2148 (void)fprintf(stderr
, "%s\n",pcap_lib_version());
2149 #else /* HAVE_PCAP_LIB_VERSION */
2151 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2152 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
2154 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2155 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
2157 #endif /* HAVE_PCAP_LIB_VERSION */
2158 (void)fprintf(stderr
,
2159 "Usage: %s [-aAbd" D_FLAG
"efhH" I_FLAG J_FLAG
"KlLnNOpqRStu" U_FLAG
"vxX]" B_FLAG_USAGE
" [ -c count ]\n", program_name
);
2160 (void)fprintf(stderr
,
2161 "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n");
2162 (void)fprintf(stderr
,
2163 "\t\t[ -i interface ]" j_FLAG_USAGE
" [ -M secret ]\n");
2164 #ifdef HAVE_PCAP_SETDIRECTION
2165 (void)fprintf(stderr
,
2166 "\t\t[ -Q in|out|inout ]\n");
2168 (void)fprintf(stderr
,
2169 "\t\t[ -r file ] [ -s snaplen ] [ -T type ] [ -V file ] [ -w file ]\n");
2170 (void)fprintf(stderr
,
2171 "\t\t[ -W filecount ] [ -y datalinktype ] [ -z command ]\n");
2172 (void)fprintf(stderr
,
2173 "\t\t[ -Z user ] [ expression ]\n");
2181 ndo_error(netdissect_options
*ndo _U_
, const char *fmt
, ...)
2185 (void)fprintf(stderr
, "%s: ", program_name
);
2187 (void)vfprintf(stderr
, fmt
, ap
);
2191 if (fmt
[-1] != '\n')
2192 (void)fputc('\n', stderr
);
2200 ndo_warning(netdissect_options
*ndo _U_
, const char *fmt
, ...)
2204 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
2206 (void)vfprintf(stderr
, fmt
, ap
);
2210 if (fmt
[-1] != '\n')
2211 (void)fputc('\n', stderr
);
2216 * c-style: whitesmith