2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 * Support for splitting captures into multiple files with a maximum
25 * Seth Webster <swebster@sst.ll.mit.edu>
29 static const char copyright
[] _U_
=
30 "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
31 The Regents of the University of California. All rights reserved.\n";
32 static const char rcsid
[] _U_
=
33 "@(#) $Header: /tcpdump/master/tcpdump/tcpdump.c,v 1.269 2006-05-05 23:13:01 guy Exp $ (LBL)";
37 * tcpdump - monitor tcp/ip traffic on an ethernet.
39 * First written in 1987 by Van Jacobson, Lawrence Berkeley Laboratory.
40 * Mercilessly hacked and occasionally improved since then via the
41 * combined efforts of Van, Steve McCanne and Craig Leres of LBL.
48 #include <tcpdump-stdinc.h>
53 extern int strcasecmp (const char *__s1
, const char *__s2
);
69 #include <sys/resource.h>
77 #include "netdissect.h"
78 #include "interface.h"
79 #include "addrtoname.h"
81 #include "setsignal.h"
82 #include "gmt2local.h"
83 #include "pcap-missing.h"
89 netdissect_options Gndo
;
90 netdissect_options
*gndo
= &Gndo
;
92 int dflag
; /* print filter code */
93 int Lflag
; /* list available data link types and exit */
94 char *zflag
= NULL
; /* compress each savefile using a specified command (like gzip or bzip2) */
101 int32_t thiszone
; /* seconds offset from gmt to local time */
104 static RETSIGTYPE
cleanup(int);
105 static RETSIGTYPE
child_cleanup(int);
106 static void usage(void) __attribute__((noreturn
));
107 static void show_dlts_and_exit(pcap_t
*pd
) __attribute__((noreturn
));
109 static void print_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
110 static void ndo_default_print(netdissect_options
*, const u_char
*, u_int
);
111 static void dump_packet_and_trunc(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
112 static void dump_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
113 static void droproot(const char *, const char *);
114 static void ndo_error(netdissect_options
*ndo
, const char *fmt
, ...);
115 static void ndo_warning(netdissect_options
*ndo
, const char *fmt
, ...);
118 RETSIGTYPE
requestinfo(int);
121 #if defined(USE_WIN32_MM_TIMER)
122 #include <MMsystem.h>
123 static UINT timer_id
;
124 static void CALLBACK
verbose_stats_dump(UINT
, UINT
, DWORD_PTR
, DWORD_PTR
, DWORD_PTR
);
125 #elif defined(HAVE_ALARM)
126 static void verbose_stats_dump(int sig
);
129 static void info(int);
130 static u_int packets_captured
;
132 typedef u_int (*if_printer
)(const struct pcap_pkthdr
*, const u_char
*);
139 static struct printer printers
[] = {
140 { arcnet_if_print
, DLT_ARCNET
},
141 #ifdef DLT_ARCNET_LINUX
142 { arcnet_linux_if_print
, DLT_ARCNET_LINUX
},
144 { ether_if_print
, DLT_EN10MB
},
145 { token_if_print
, DLT_IEEE802
},
147 { lane_if_print
, DLT_LANE8023
},
150 { cip_if_print
, DLT_CIP
},
153 { cip_if_print
, DLT_ATM_CLIP
},
155 { sl_if_print
, DLT_SLIP
},
156 #ifdef DLT_SLIP_BSDOS
157 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
159 { ppp_if_print
, DLT_PPP
},
160 #ifdef DLT_PPP_WITHDIRECTION
161 { ppp_if_print
, DLT_PPP_WITHDIRECTION
},
164 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
166 { fddi_if_print
, DLT_FDDI
},
167 { null_if_print
, DLT_NULL
},
169 { null_if_print
, DLT_LOOP
},
171 { raw_if_print
, DLT_RAW
},
172 { atm_if_print
, DLT_ATM_RFC1483
},
174 { chdlc_if_print
, DLT_C_HDLC
},
177 { chdlc_if_print
, DLT_HDLC
},
179 #ifdef DLT_PPP_SERIAL
180 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
183 { pppoe_if_print
, DLT_PPP_ETHER
},
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 { pflog_if_print
, DLT_PFLOG
},
198 { fr_if_print
, DLT_FR
},
201 { fr_if_print
, DLT_FRELAY
},
204 { sunatm_if_print
, DLT_SUNATM
},
206 #ifdef DLT_IP_OVER_FC
207 { ipfc_if_print
, DLT_IP_OVER_FC
},
209 #ifdef DLT_PRISM_HEADER
210 { prism_if_print
, DLT_PRISM_HEADER
},
212 #ifdef DLT_IEEE802_11_RADIO
213 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
216 { enc_if_print
, DLT_ENC
},
218 #ifdef DLT_SYMANTEC_FIREWALL
219 { symantec_if_print
, DLT_SYMANTEC_FIREWALL
},
221 #ifdef DLT_APPLE_IP_OVER_IEEE1394
222 { ap1394_if_print
, DLT_APPLE_IP_OVER_IEEE1394
},
224 #ifdef DLT_JUNIPER_ATM1
225 { juniper_atm1_print
, DLT_JUNIPER_ATM1
},
227 #ifdef DLT_JUNIPER_ATM2
228 { juniper_atm2_print
, DLT_JUNIPER_ATM2
},
230 #ifdef DLT_JUNIPER_MFR
231 { juniper_mfr_print
, DLT_JUNIPER_MFR
},
233 #ifdef DLT_JUNIPER_MLFR
234 { juniper_mlfr_print
, DLT_JUNIPER_MLFR
},
236 #ifdef DLT_JUNIPER_MLPPP
237 { juniper_mlppp_print
, DLT_JUNIPER_MLPPP
},
239 #ifdef DLT_JUNIPER_PPPOE
240 { juniper_pppoe_print
, DLT_JUNIPER_PPPOE
},
242 #ifdef DLT_JUNIPER_PPPOE_ATM
243 { juniper_pppoe_atm_print
, DLT_JUNIPER_PPPOE_ATM
},
245 #ifdef DLT_JUNIPER_GGSN
246 { juniper_ggsn_print
, DLT_JUNIPER_GGSN
},
248 #ifdef DLT_JUNIPER_ES
249 { juniper_es_print
, DLT_JUNIPER_ES
},
251 #ifdef DLT_JUNIPER_MONITOR
252 { juniper_monitor_print
, DLT_JUNIPER_MONITOR
},
254 #ifdef DLT_JUNIPER_SERVICES
255 { juniper_services_print
, DLT_JUNIPER_SERVICES
},
257 #ifdef DLT_JUNIPER_ETHER
258 { juniper_ether_print
, DLT_JUNIPER_ETHER
},
260 #ifdef DLT_JUNIPER_PPP
261 { juniper_ppp_print
, DLT_JUNIPER_PPP
},
263 #ifdef DLT_JUNIPER_FRELAY
264 { juniper_frelay_print
, DLT_JUNIPER_FRELAY
},
266 #ifdef DLT_JUNIPER_CHDLC
267 { juniper_chdlc_print
, DLT_JUNIPER_CHDLC
},
270 { mfr_if_print
, DLT_MFR
},
276 lookup_printer(int type
)
280 for (p
= printers
; p
->f
; ++p
)
300 char *CurrentFileName
;
306 show_dlts_and_exit(pcap_t
*pd
)
310 const char *dlt_name
;
312 n_dlts
= pcap_list_datalinks(pd
, &dlts
);
314 error("%s", pcap_geterr(pd
));
315 else if (n_dlts
== 0 || !dlts
)
316 error("No data link types.");
318 (void) fprintf(stderr
, "Data link types (use option -y to set):\n");
320 while (--n_dlts
>= 0) {
321 dlt_name
= pcap_datalink_val_to_name(dlts
[n_dlts
]);
322 if (dlt_name
!= NULL
) {
323 (void) fprintf(stderr
, " %s (%s)", dlt_name
,
324 pcap_datalink_val_to_description(dlts
[n_dlts
]));
327 * OK, does tcpdump handle that type?
329 if (lookup_printer(dlts
[n_dlts
]) == NULL
)
330 (void) fprintf(stderr
, " (not supported)");
333 (void) fprintf(stderr
, " DLT %d (not supported)\n",
342 * Set up flags that might or might not be supported depending on the
343 * version of libpcap we're using.
347 #define B_FLAG_USAGE " [ -B size ]"
353 #ifdef HAVE_PCAP_FINDALLDEVS
354 #ifndef HAVE_PCAP_IF_T
355 #undef HAVE_PCAP_FINDALLDEVS
359 #ifdef HAVE_PCAP_FINDALLDEVS
365 #ifdef HAVE_PCAP_DUMP_FLUSH
372 /* Drop root privileges and chroot if necessary */
374 droproot(const char *username
, const char *chroot_dir
)
376 struct passwd
*pw
= NULL
;
378 if (chroot_dir
&& !username
) {
379 fprintf(stderr
, "tcpdump: Chroot without dropping root is insecure\n");
383 pw
= getpwnam(username
);
386 if (chroot(chroot_dir
) != 0 || chdir ("/") != 0) {
387 fprintf(stderr
, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
388 chroot_dir
, pcap_strerror(errno
));
392 if (initgroups(pw
->pw_name
, pw
->pw_gid
) != 0 ||
393 setgid(pw
->pw_gid
) != 0 || setuid(pw
->pw_uid
) != 0) {
394 fprintf(stderr
, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
396 (unsigned long)pw
->pw_uid
,
397 (unsigned long)pw
->pw_gid
,
398 pcap_strerror(errno
));
403 fprintf(stderr
, "tcpdump: Couldn't find user '%.32s'\n",
426 MakeFilename(char *buffer
, char *orig_name
, int cnt
, int max_chars
)
428 char *filename
= malloc(NAME_MAX
+ 1);
430 /* Process with strftime if Gflag is set. */
434 /* Convert Gflag_time to a usable format */
435 if ((local_tm
= localtime(&Gflag_time
)) == NULL
) {
436 error("MakeTimedFilename: localtime");
439 /* There's no good way to detect an error in strftime since a return
440 * value of 0 isn't necessarily failure.
442 strftime(filename
, NAME_MAX
, orig_name
, local_tm
);
444 strncpy(filename
, orig_name
, NAME_MAX
);
447 if (cnt
== 0 && max_chars
== 0)
448 strncpy(buffer
, filename
, NAME_MAX
+ 1);
450 if (snprintf(buffer
, NAME_MAX
+ 1, "%s%0*d", filename
, max_chars
, cnt
) > NAME_MAX
)
451 /* Report an error if the filename is too large */
452 error("too many output files or filename is too long (> %d)", NAME_MAX
);
456 static int tcpdump_printf(netdissect_options
*ndo _U_
,
457 const char *fmt
, ...)
464 ret
=vfprintf(stdout
, fmt
, args
);
471 main(int argc
, char **argv
)
473 register int cnt
, op
, i
;
474 bpf_u_int32 localnet
, netmask
;
475 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *WFileName
;
476 pcap_handler callback
;
478 struct bpf_program fcode
;
480 RETSIGTYPE (*oldhandler
)(int);
482 struct print_info printinfo
;
483 struct dump_info dumpinfo
;
484 u_char
*pcap_userdata
;
485 char ebuf
[PCAP_ERRBUF_SIZE
];
486 char *username
= NULL
;
487 char *chroot_dir
= NULL
;
488 #ifdef HAVE_PCAP_FINDALLDEVS
489 pcap_if_t
*devpointer
;
494 u_int UserBufferSize
= 1000000;
495 if(wsockinit() != 0) return 1;
501 gndo
->ndo_default_print
=ndo_default_print
;
502 gndo
->ndo_printf
=tcpdump_printf
;
503 gndo
->ndo_error
=ndo_error
;
504 gndo
->ndo_warning
=ndo_warning
;
505 gndo
->ndo_snaplen
= DEFAULT_SNAPLEN
;
512 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
513 program_name
= cp
+ 1;
515 program_name
= argv
[0];
517 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
526 (op
= getopt(argc
, argv
, "aA" B_FLAG
"c:C:d" D_FLAG
"eE:fF:G:i:KlLm:M:nNOpqr:Rs:StT:u" U_FLAG
"vw:W:xXy:Yz:Z:")) != -1)
530 /* compatibility for old -a */
539 UserBufferSize
= atoi(optarg
)*1024;
540 if (UserBufferSize
< 0)
541 error("invalid packet buffer size %s", optarg
);
548 error("invalid packet count %s", optarg
);
552 Cflag
= atoi(optarg
) * 1000000;
554 error("invalid file size %s", optarg
);
561 #ifdef HAVE_PCAP_FINDALLDEVS
563 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
566 for (i
= 0; devpointer
!= 0; i
++) {
567 printf("%d.%s", i
+1, devpointer
->name
);
568 if (devpointer
->description
!= NULL
)
569 printf(" (%s)", devpointer
->description
);
571 devpointer
= devpointer
->next
;
575 #endif /* HAVE_PCAP_FINDALLDEVS */
586 #ifndef HAVE_LIBCRYPTO
587 warning("crypto code not compiled in");
589 gndo
->ndo_espsecret
= optarg
;
601 Gflag
= atoi(optarg
);
603 error("invalid number of seconds %s", optarg
);
605 /* We will create one file initially. */
608 /* Grab the current time for rotation use. */
609 if ((Gflag_time
= time(NULL
)) == (time_t)-1) {
610 error("main: can't get current time: %s",
611 pcap_strerror(errno
));
616 if (optarg
[0] == '0' && optarg
[1] == 0)
617 error("Invalid adapter index");
619 #ifdef HAVE_PCAP_FINDALLDEVS
621 * If the argument is a number, treat it as
622 * an index into the list of adapters, as
623 * printed by "tcpdump -D".
625 * This should be OK on UNIX systems, as interfaces
626 * shouldn't have names that begin with digits.
627 * It can be useful on Windows, where more than
628 * one interface can have the same name.
630 if ((devnum
= atoi(optarg
)) != 0) {
632 error("Invalid adapter index");
634 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
637 for (i
= 0; i
< devnum
-1; i
++){
638 devpointer
= devpointer
->next
;
639 if (devpointer
== NULL
)
640 error("Invalid adapter index");
643 device
= devpointer
->name
;
646 #endif /* HAVE_PCAP_FINDALLDEVS */
653 * _IOLBF is the same as _IOFBF in Microsoft's C
654 * libraries; the only alternative they offer
657 * XXX - this should really be checking for MSVC++,
658 * not WIN32, if, for example, MinGW has its own
659 * C library that is more UNIX-compatible.
661 setvbuf(stdout
, NULL
, _IONBF
, 0);
663 #ifdef HAVE_SETLINEBUF
666 setvbuf(stdout
, NULL
, _IOLBF
, 0);
677 if (smiLoadModule(optarg
) == 0) {
678 error("could not load MIB module %s", optarg
);
682 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
683 program_name
, optarg
);
684 (void)fprintf(stderr
, "(no libsmi support)\n");
689 /* TCP-MD5 shared secret */
690 #ifndef HAVE_LIBCRYPTO
691 warning("crypto code not compiled in");
693 tcpmd5secret
= optarg
;
714 ++suppress_default_print
;
728 snaplen
= strtol(optarg
, &end
, 0);
729 if (optarg
== end
|| *end
!= '\0'
730 || snaplen
< 0 || snaplen
> 65535)
731 error("invalid snaplen %s", optarg
);
732 else if (snaplen
== 0)
746 if (strcasecmp(optarg
, "vat") == 0)
748 else if (strcasecmp(optarg
, "wb") == 0)
750 else if (strcasecmp(optarg
, "rpc") == 0)
752 else if (strcasecmp(optarg
, "rtp") == 0)
754 else if (strcasecmp(optarg
, "rtcp") == 0)
755 packettype
= PT_RTCP
;
756 else if (strcasecmp(optarg
, "snmp") == 0)
757 packettype
= PT_SNMP
;
758 else if (strcasecmp(optarg
, "cnfp") == 0)
759 packettype
= PT_CNFP
;
760 else if (strcasecmp(optarg
, "tftp") == 0)
761 packettype
= PT_TFTP
;
762 else if (strcasecmp(optarg
, "aodv") == 0)
763 packettype
= PT_AODV
;
765 error("unknown packet type `%s'", optarg
);
772 #ifdef HAVE_PCAP_DUMP_FLUSH
787 Wflag
= atoi(optarg
);
789 error("invalid number of output files %s", optarg
);
790 WflagChars
= getWflagChars(Wflag
);
795 ++suppress_default_print
;
800 ++suppress_default_print
;
804 gndo
->ndo_dltname
= optarg
;
806 pcap_datalink_name_to_val(gndo
->ndo_dltname
);
807 if (gndo
->ndo_dlt
< 0)
808 error("invalid data link type %s", gndo
->ndo_dltname
);
811 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
814 /* Undocumented flag */
815 #ifdef HAVE_PCAP_DEBUG
816 extern int pcap_debug
;
827 zflag
= strdup(optarg
);
836 username
= strdup(optarg
);
851 case 0: /* Default */
852 case 4: /* Default + Date*/
853 thiszone
= gmt2local(0);
856 case 1: /* No time stamp */
857 case 2: /* Unix timeval style */
858 case 3: /* Microseconds since previous packet */
859 case 5: /* Microseconds since first packet */
862 default: /* Not supported */
863 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
868 /* if run as root, prepare for chrooting */
869 if (getuid() == 0 || geteuid() == 0) {
870 /* future extensibility for cmd-line arguments */
872 chroot_dir
= WITH_CHROOT
;
877 /* if run as root, prepare for dropping root privileges */
878 if (getuid() == 0 || geteuid() == 0) {
879 /* Run with '-Z root' to restore old behaviour */
881 username
= WITH_USER
;
885 if (RFileName
!= NULL
) {
887 const char *dlt_name
;
891 * We don't need network access, so relinquish any set-UID
892 * or set-GID privileges we have (if any).
894 * We do *not* want set-UID privileges when opening a
895 * trace file, as that might let the user read other
896 * people's trace files (especially if we're set-UID
899 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
900 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
902 pd
= pcap_open_offline(RFileName
, ebuf
);
905 dlt
= pcap_datalink(pd
);
906 dlt_name
= pcap_datalink_val_to_name(dlt
);
907 if (dlt_name
== NULL
) {
908 fprintf(stderr
, "reading from file %s, link-type %u\n",
912 "reading from file %s, link-type %s (%s)\n",
914 pcap_datalink_val_to_description(dlt
));
919 error("-f and -r options are incompatible");
921 if (device
== NULL
) {
922 device
= pcap_lookupdev(ebuf
);
927 if(strlen(device
) == 1) //we assume that an ASCII string is always longer than 1 char
928 { //a Unicode string has a \0 as second byte (so strlen() is 1)
929 fprintf(stderr
, "%s: listening on %ws\n", program_name
, device
);
933 fprintf(stderr
, "%s: listening on %s\n", program_name
, device
);
939 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
945 * Let user own process after socket has been opened.
948 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
949 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
952 if(UserBufferSize
!= 1000000)
953 if(pcap_setbuff(pd
, UserBufferSize
)==-1){
954 error("%s", pcap_geterr(pd
));
958 show_dlts_and_exit(pd
);
959 if (gndo
->ndo_dlt
>= 0) {
960 #ifdef HAVE_PCAP_SET_DATALINK
961 if (pcap_set_datalink(pd
, gndo
->ndo_dlt
) < 0)
962 error("%s", pcap_geterr(pd
));
965 * We don't actually support changing the
966 * data link type, so we only let them
967 * set it to what it already is.
969 if (gndo
->ndo_dlt
!= pcap_datalink(pd
)) {
970 error("%s is not one of the DLTs supported by this device\n",
974 (void)fprintf(stderr
, "%s: data link type %s\n",
975 program_name
, gndo
->ndo_dltname
);
976 (void)fflush(stderr
);
978 i
= pcap_snapshot(pd
);
980 warning("snaplen raised from %d to %d", snaplen
, i
);
983 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
990 cmdbuf
= read_infile(infile
);
992 cmdbuf
= copy_argv(&argv
[optind
]);
994 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
995 error("%s", pcap_geterr(pd
));
997 bpf_dump(&fcode
, dflag
);
1001 init_addrtoname(localnet
, netmask
);
1005 (void)setsignal(SIGPIPE
, cleanup
);
1007 (void)setsignal(SIGTERM
, cleanup
);
1008 (void)setsignal(SIGINT
, cleanup
);
1009 (void)setsignal(SIGCHLD
, child_cleanup
);
1010 /* Cooperate with nohup(1) */
1012 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
1013 (void)setsignal(SIGHUP
, oldhandler
);
1016 if (pcap_setfilter(pd
, &fcode
) < 0)
1017 error("%s", pcap_geterr(pd
));
1020 /* Do not exceed the default NAME_MAX for files. */
1021 dumpinfo
.CurrentFileName
= (char *)malloc(NAME_MAX
+ 1);
1023 if (dumpinfo
.CurrentFileName
== NULL
)
1024 error("malloc of dumpinfo.CurrentFileName");
1026 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1028 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, WflagChars
);
1030 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, 0);
1032 p
= pcap_dump_open(pd
, dumpinfo
.CurrentFileName
);
1034 error("%s", pcap_geterr(pd
));
1035 if (Cflag
!= 0 || Gflag
!= 0) {
1036 callback
= dump_packet_and_trunc
;
1037 dumpinfo
.WFileName
= WFileName
;
1040 pcap_userdata
= (u_char
*)&dumpinfo
;
1042 callback
= dump_packet
;
1043 pcap_userdata
= (u_char
*)p
;
1046 type
= pcap_datalink(pd
);
1047 printinfo
.printer
= lookup_printer(type
);
1048 if (printinfo
.printer
== NULL
) {
1049 gndo
->ndo_dltname
= pcap_datalink_val_to_name(type
);
1050 if (gndo
->ndo_dltname
!= NULL
)
1051 error("unsupported data link type %s",
1054 error("unsupported data link type %d", type
);
1056 callback
= print_packet
;
1057 pcap_userdata
= (u_char
*)&printinfo
;
1061 * We cannot do this earlier, because we want to be able to open
1062 * the file (if done) for writing before giving up permissions.
1064 if (getuid() == 0 || geteuid() == 0) {
1065 if (username
|| chroot_dir
)
1066 droproot(username
, chroot_dir
);
1070 (void)setsignal(SIGINFO
, requestinfo
);
1073 if (vflag
> 0 && WFileName
) {
1075 * When capturing to a file, "-v" means tcpdump should,
1076 * every 10 secodns, "v"erbosely report the number of
1079 #ifdef USE_WIN32_MM_TIMER
1080 /* call verbose_stats_dump() each 1000 +/-100msec */
1081 timer_id
= timeSetEvent(1000, 100, verbose_stats_dump
, 0, TIME_PERIODIC
);
1082 setvbuf(stderr
, NULL
, _IONBF
, 0);
1083 #elif defined(HAVE_ALARM)
1084 (void)setsignal(SIGALRM
, verbose_stats_dump
);
1090 if (RFileName
== NULL
) {
1092 const char *dlt_name
;
1094 if (!vflag
&& !WFileName
) {
1095 (void)fprintf(stderr
,
1096 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1099 (void)fprintf(stderr
, "%s: ", program_name
);
1100 dlt
= pcap_datalink(pd
);
1101 dlt_name
= pcap_datalink_val_to_name(dlt
);
1102 if (dlt_name
== NULL
) {
1103 (void)fprintf(stderr
, "listening on %s, link-type %u, capture size %u bytes\n",
1104 device
, dlt
, snaplen
);
1106 (void)fprintf(stderr
, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1108 pcap_datalink_val_to_description(dlt
), snaplen
);
1110 (void)fflush(stderr
);
1113 status
= pcap_loop(pd
, cnt
, callback
, pcap_userdata
);
1114 if (WFileName
== NULL
) {
1116 * We're printing packets. Flush the printed output,
1117 * so it doesn't get intermingled with error output.
1121 * We got interrupted, so perhaps we didn't
1122 * manage to finish a line we were printing.
1123 * Print an extra newline, just in case.
1127 (void)fflush(stdout
);
1133 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
1134 program_name
, pcap_geterr(pd
));
1136 if (RFileName
== NULL
) {
1138 * We're doing a live capture. Report the capture
1144 exit(status
== -1 ? 1 : 0);
1147 /* make a clean exit on interrupts */
1149 cleanup(int signo _U_
)
1151 #ifdef USE_WIN32_MM_TIMER
1153 timeKillEvent(timer_id
);
1155 #elif defined(HAVE_ALARM)
1159 #ifdef HAVE_PCAP_BREAKLOOP
1161 * We have "pcap_breakloop()"; use it, so that we do as little
1162 * as possible in the signal handler (it's probably not safe
1163 * to do anything with standard I/O streams in a signal handler -
1164 * the ANSI C standard doesn't say it is).
1169 * We don't have "pcap_breakloop()"; this isn't safe, but
1170 * it's the best we can do. Print the summary if we're
1171 * not reading from a savefile - i.e., if we're doing a
1172 * live capture - and exit.
1174 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
1176 * We got interrupted, so perhaps we didn't
1177 * manage to finish a line we were printing.
1178 * Print an extra newline, just in case.
1181 (void)fflush(stdout
);
1189 child_cleanup(int signo _U_
)
1195 info(register int verbose
)
1197 struct pcap_stat stat
;
1199 if (pcap_stats(pd
, &stat
) < 0) {
1200 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
1205 fprintf(stderr
, "%s: ", program_name
);
1207 (void)fprintf(stderr
, "%u packets captured", packets_captured
);
1209 fputs(", ", stderr
);
1212 (void)fprintf(stderr
, "%d packets received by filter", stat
.ps_recv
);
1214 fputs(", ", stderr
);
1217 (void)fprintf(stderr
, "%d packets dropped by kernel\n", stat
.ps_drop
);
1222 compress_savefile(const char *filename
)
1227 * Set to lowest priority so that this doesn't disturb the capture
1230 setpriority(PRIO_PROCESS
, 0, NZERO
- 1);
1232 setpriority(PRIO_PROCESS
, 0, 19);
1234 if (execlp(zflag
, zflag
, filename
, NULL
) == -1)
1236 "compress_savefile:execlp(%s, %s): %s\n",
1243 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1245 struct dump_info
*dump_info
;
1251 dump_info
= (struct dump_info
*)user
;
1254 * XXX - this won't force the file to rotate on the specified time
1255 * boundary, but it will rotate on the first packet received after the
1256 * specified Gflag number of seconds. Note: if a Gflag time boundary
1257 * and a Cflag size boundary coincide, the time rotation will occur
1258 * first thereby cancelling the Cflag boundary (since the file should
1262 /* Check if it is time to rotate */
1265 /* Get the current time */
1266 if ((t
= time(NULL
)) == (time_t)-1) {
1267 error("dump_and_trunc_packet: can't get current_time: %s",
1268 pcap_strerror(errno
));
1272 /* If the time is greater than the specified window, rotate */
1273 if (t
- Gflag_time
>= Gflag
) {
1274 /* Update the Gflag_time */
1276 /* Update Gflag_count */
1279 * Close the current file and open a new one.
1281 pcap_dump_close(dump_info
->p
);
1284 * Compress the file we just closed, if the user asked for it
1287 compress_savefile(dump_info
->CurrentFileName
);
1290 * Check to see if we've exceeded the Wflag (when
1293 if (Cflag
== 0 && Wflag
> 0 && Gflag_count
>= Wflag
) {
1294 (void)fprintf(stderr
, "Maximum file limit reached: %d\n",
1299 if (dump_info
->CurrentFileName
!= NULL
)
1300 free(dump_info
->CurrentFileName
);
1301 /* Allocate space for max filename + \0. */
1302 dump_info
->CurrentFileName
= (char *)malloc(NAME_MAX
+ 1);
1303 if (dump_info
->CurrentFileName
== NULL
)
1304 error("dump_packet_and_trunc: malloc");
1306 * This is always the first file in the Cflag
1308 * We also don't need numbering if Cflag is not set.
1311 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0,
1314 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0, 0);
1316 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
1317 if (dump_info
->p
== NULL
)
1318 error("%s", pcap_geterr(pd
));
1323 * XXX - this won't prevent capture files from getting
1324 * larger than Cflag - the last packet written to the
1325 * file could put it over Cflag.
1327 if (Cflag
!= 0 && pcap_dump_ftell(dump_info
->p
) > Cflag
) {
1329 * Close the current file and open a new one.
1331 pcap_dump_close(dump_info
->p
);
1334 * Compress the file we just closed, if the user asked for it
1337 compress_savefile(dump_info
->CurrentFileName
);
1341 if (Cflag_count
>= Wflag
)
1344 if (dump_info
->CurrentFileName
!= NULL
)
1345 free(dump_info
->CurrentFileName
);
1346 dump_info
->CurrentFileName
= (char *)malloc(NAME_MAX
+ 1);
1347 if (dump_info
->CurrentFileName
== NULL
)
1348 error("dump_packet_and_trunc: malloc");
1349 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, Cflag_count
, WflagChars
);
1350 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
1351 if (dump_info
->p
== NULL
)
1352 error("%s", pcap_geterr(pd
));
1355 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
1356 #ifdef HAVE_PCAP_DUMP_FLUSH
1358 pcap_dump_flush(dump_info
->p
);
1367 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1373 pcap_dump(user
, h
, sp
);
1374 #ifdef HAVE_PCAP_DUMP_FLUSH
1376 pcap_dump_flush((pcap_dumper_t
*)user
);
1385 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1387 struct print_info
*print_info
;
1395 print_info
= (struct print_info
*)user
;
1398 * Some printers want to check that they're not walking off the
1399 * end of the packet.
1400 * Rather than pass it all the way down, we set this global.
1402 snapend
= sp
+ h
->caplen
;
1404 hdrlen
= (*print_info
->printer
)(h
, sp
);
1407 * Print the raw packet data in hex and ASCII.
1411 * Include the link-layer header.
1413 hex_and_ascii_print("\n\t", sp
, h
->caplen
);
1416 * Don't include the link-layer header - and if
1417 * we have nothing past the link-layer header,
1420 if (h
->caplen
> hdrlen
)
1421 hex_and_ascii_print("\n\t", sp
+ hdrlen
,
1422 h
->caplen
- hdrlen
);
1426 * Print the raw packet data in hex.
1430 * Include the link-layer header.
1432 hex_print("\n\t", sp
, h
->caplen
);
1435 * Don't include the link-layer header - and if
1436 * we have nothing past the link-layer header,
1439 if (h
->caplen
> hdrlen
)
1440 hex_print("\n\t", sp
+ hdrlen
,
1441 h
->caplen
- hdrlen
);
1445 * Print the raw packet data in ASCII.
1449 * Include the link-layer header.
1451 ascii_print(sp
, h
->caplen
);
1454 * Don't include the link-layer header - and if
1455 * we have nothing past the link-layer header,
1458 if (h
->caplen
> hdrlen
)
1459 ascii_print(sp
+ hdrlen
, h
->caplen
- hdrlen
);
1472 * XXX - there should really be libpcap calls to get the version
1473 * number as a string (the string would be generated from #defines
1474 * at run time, so that it's not generated from string constants
1475 * in the library, as, on many UNIX systems, those constants would
1476 * be statically linked into the application executable image, and
1477 * would thus reflect the version of libpcap on the system on
1478 * which the application was *linked*, not the system on which it's
1481 * That routine should be documented, unlike the "version[]"
1482 * string, so that UNIX vendors providing their own libpcaps
1483 * don't omit it (as a couple of vendors have...).
1485 * Packet.dll should perhaps also export a routine to return the
1486 * version number of the Packet.dll code, to supply the
1487 * "Wpcap_version" information on Windows.
1489 char WDversion
[]="current-cvs.tcpdump.org";
1490 #if !defined(HAVE_GENERATED_VERSION)
1491 char version
[]="current-cvs.tcpdump.org";
1493 char pcap_version
[]="current-cvs.tcpdump.org";
1494 char Wpcap_version
[]="3.1";
1498 * By default, print the specified data out in hex and ASCII.
1501 ndo_default_print(netdissect_options
*ndo _U_
, const u_char
*bp
, u_int length
)
1503 hex_and_ascii_print("\n\t", bp
, length
); /* pass on lf and identation string */
1507 default_print(const u_char
*bp
, u_int length
)
1509 ndo_default_print(gndo
, bp
, length
);
1513 RETSIGTYPE
requestinfo(int signo _U_
)
1523 * Called once each second in verbose mode while dumping to file
1525 #ifdef USE_WIN32_MM_TIMER
1526 void CALLBACK
verbose_stats_dump (UINT timer_id _U_
, UINT msg _U_
, DWORD_PTR arg _U_
,
1527 DWORD_PTR dw1 _U_
, DWORD_PTR dw2 _U_
)
1529 struct pcap_stat stat
;
1531 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
1532 fprintf(stderr
, "Got %u\r", packets_captured
);
1534 #elif defined(HAVE_ALARM)
1535 static void verbose_stats_dump(int sig _U_
)
1537 struct pcap_stat stat
;
1539 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
1540 fprintf(stderr
, "Got %u\r", packets_captured
);
1548 extern char version
[];
1549 #ifndef HAVE_PCAP_LIB_VERSION
1550 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
1551 extern char pcap_version
[];
1552 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1553 static char pcap_version
[] = "unknown";
1554 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1555 #endif /* HAVE_PCAP_LIB_VERSION */
1557 #ifdef HAVE_PCAP_LIB_VERSION
1559 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
1561 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1563 (void)fprintf(stderr
, "%s\n",pcap_lib_version());
1564 #else /* HAVE_PCAP_LIB_VERSION */
1566 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
1567 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
1569 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1570 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
1572 #endif /* HAVE_PCAP_LIB_VERSION */
1573 (void)fprintf(stderr
,
1574 "Usage: %s [-aAd" D_FLAG
"efKlLnNOpqRStu" U_FLAG
"vxX]" B_FLAG_USAGE
" [-c count] [ -C file_size ]\n", program_name
);
1575 (void)fprintf(stderr
,
1576 "\t\t[ -E algo:secret ] [ -F file ] [ -G seconds ] [ -i interface ]\n");
1577 (void)fprintf(stderr
,
1578 "\t\t[ -M secret ] [ -r file ] [ -s snaplen ] [ -T type ] [ -w file ]\n");
1579 (void)fprintf(stderr
,
1580 "\t\t[ -W filecount ] [ -y datalinktype ] [ -z command ] [ -Z user ]\n");
1581 (void)fprintf(stderr
,
1582 "\t\t[ expression ]\n");
1590 ndo_error(netdissect_options
*ndo _U_
, const char *fmt
, ...)
1594 (void)fprintf(stderr
, "%s: ", program_name
);
1596 (void)vfprintf(stderr
, fmt
, ap
);
1600 if (fmt
[-1] != '\n')
1601 (void)fputc('\n', stderr
);
1609 ndo_warning(netdissect_options
*ndo _U_
, const char *fmt
, ...)
1613 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
1615 (void)vfprintf(stderr
, fmt
, ap
);
1619 if (fmt
[-1] != '\n')
1620 (void)fputc('\n', stderr
);