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.268 2006-03-23 17:33:01 hannes 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:lLm: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);
681 if (smiLoadModule(optarg
) == 0) {
682 error("could not load MIB module %s", optarg
);
686 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
687 program_name
, optarg
);
688 (void)fprintf(stderr
, "(no libsmi support)\n");
693 /* TCP-MD5 shared secret */
694 #ifndef HAVE_LIBCRYPTO
695 warning("crypto code not compiled in");
697 tcpmd5secret
= optarg
;
710 ++suppress_default_print
;
724 snaplen
= strtol(optarg
, &end
, 0);
725 if (optarg
== end
|| *end
!= '\0'
726 || snaplen
< 0 || snaplen
> 65535)
727 error("invalid snaplen %s", optarg
);
728 else if (snaplen
== 0)
742 if (strcasecmp(optarg
, "vat") == 0)
744 else if (strcasecmp(optarg
, "wb") == 0)
746 else if (strcasecmp(optarg
, "rpc") == 0)
748 else if (strcasecmp(optarg
, "rtp") == 0)
750 else if (strcasecmp(optarg
, "rtcp") == 0)
751 packettype
= PT_RTCP
;
752 else if (strcasecmp(optarg
, "snmp") == 0)
753 packettype
= PT_SNMP
;
754 else if (strcasecmp(optarg
, "cnfp") == 0)
755 packettype
= PT_CNFP
;
756 else if (strcasecmp(optarg
, "tftp") == 0)
757 packettype
= PT_TFTP
;
758 else if (strcasecmp(optarg
, "aodv") == 0)
759 packettype
= PT_AODV
;
761 error("unknown packet type `%s'", optarg
);
768 #ifdef HAVE_PCAP_DUMP_FLUSH
783 Wflag
= atoi(optarg
);
785 error("invalid number of output files %s", optarg
);
786 WflagChars
= getWflagChars(Wflag
);
791 ++suppress_default_print
;
796 ++suppress_default_print
;
800 gndo
->ndo_dltname
= optarg
;
802 pcap_datalink_name_to_val(gndo
->ndo_dltname
);
803 if (gndo
->ndo_dlt
< 0)
804 error("invalid data link type %s", gndo
->ndo_dltname
);
807 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
810 /* Undocumented flag */
811 #ifdef HAVE_PCAP_DEBUG
812 extern int pcap_debug
;
823 zflag
= strdup(optarg
);
832 username
= strdup(optarg
);
847 case 0: /* Default */
848 case 4: /* Default + Date*/
849 thiszone
= gmt2local(0);
852 case 1: /* No time stamp */
853 case 2: /* Unix timeval style */
854 case 3: /* Microseconds since previous packet */
855 case 5: /* Microseconds since first packet */
858 default: /* Not supported */
859 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
864 /* if run as root, prepare for chrooting */
865 if (getuid() == 0 || geteuid() == 0) {
866 /* future extensibility for cmd-line arguments */
868 chroot_dir
= WITH_CHROOT
;
873 /* if run as root, prepare for dropping root privileges */
874 if (getuid() == 0 || geteuid() == 0) {
875 /* Run with '-Z root' to restore old behaviour */
877 username
= WITH_USER
;
881 if (RFileName
!= NULL
) {
883 const char *dlt_name
;
887 * We don't need network access, so relinquish any set-UID
888 * or set-GID privileges we have (if any).
890 * We do *not* want set-UID privileges when opening a
891 * trace file, as that might let the user read other
892 * people's trace files (especially if we're set-UID
895 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
896 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
898 pd
= pcap_open_offline(RFileName
, ebuf
);
901 dlt
= pcap_datalink(pd
);
902 dlt_name
= pcap_datalink_val_to_name(dlt
);
903 if (dlt_name
== NULL
) {
904 fprintf(stderr
, "reading from file %s, link-type %u\n",
908 "reading from file %s, link-type %s (%s)\n",
910 pcap_datalink_val_to_description(dlt
));
915 error("-f and -r options are incompatible");
917 if (device
== NULL
) {
918 device
= pcap_lookupdev(ebuf
);
923 if(strlen(device
) == 1) //we assume that an ASCII string is always longer than 1 char
924 { //a Unicode string has a \0 as second byte (so strlen() is 1)
925 fprintf(stderr
, "%s: listening on %ws\n", program_name
, device
);
929 fprintf(stderr
, "%s: listening on %s\n", program_name
, device
);
935 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
941 * Let user own process after socket has been opened.
944 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
945 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
948 if(UserBufferSize
!= 1000000)
949 if(pcap_setbuff(pd
, UserBufferSize
)==-1){
950 error("%s", pcap_geterr(pd
));
954 show_dlts_and_exit(pd
);
955 if (gndo
->ndo_dlt
>= 0) {
956 #ifdef HAVE_PCAP_SET_DATALINK
957 if (pcap_set_datalink(pd
, gndo
->ndo_dlt
) < 0)
958 error("%s", pcap_geterr(pd
));
961 * We don't actually support changing the
962 * data link type, so we only let them
963 * set it to what it already is.
965 if (gndo
->ndo_dlt
!= pcap_datalink(pd
)) {
966 error("%s is not one of the DLTs supported by this device\n",
970 (void)fprintf(stderr
, "%s: data link type %s\n",
971 program_name
, gndo
->ndo_dltname
);
972 (void)fflush(stderr
);
974 i
= pcap_snapshot(pd
);
976 warning("snaplen raised from %d to %d", snaplen
, i
);
979 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
986 cmdbuf
= read_infile(infile
);
988 cmdbuf
= copy_argv(&argv
[optind
]);
990 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
991 error("%s", pcap_geterr(pd
));
993 bpf_dump(&fcode
, dflag
);
997 init_addrtoname(localnet
, netmask
);
1001 (void)setsignal(SIGPIPE
, cleanup
);
1003 (void)setsignal(SIGTERM
, cleanup
);
1004 (void)setsignal(SIGINT
, cleanup
);
1005 (void)setsignal(SIGCHLD
, child_cleanup
);
1006 /* Cooperate with nohup(1) */
1008 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
1009 (void)setsignal(SIGHUP
, oldhandler
);
1012 if (pcap_setfilter(pd
, &fcode
) < 0)
1013 error("%s", pcap_geterr(pd
));
1016 /* Do not exceed the default NAME_MAX for files. */
1017 dumpinfo
.CurrentFileName
= (char *)malloc(NAME_MAX
+ 1);
1019 if (dumpinfo
.CurrentFileName
== NULL
)
1020 error("malloc of dumpinfo.CurrentFileName");
1022 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1024 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, WflagChars
);
1026 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, 0);
1028 p
= pcap_dump_open(pd
, dumpinfo
.CurrentFileName
);
1030 error("%s", pcap_geterr(pd
));
1031 if (Cflag
!= 0 || Gflag
!= 0) {
1032 callback
= dump_packet_and_trunc
;
1033 dumpinfo
.WFileName
= WFileName
;
1036 pcap_userdata
= (u_char
*)&dumpinfo
;
1038 callback
= dump_packet
;
1039 pcap_userdata
= (u_char
*)p
;
1042 type
= pcap_datalink(pd
);
1043 printinfo
.printer
= lookup_printer(type
);
1044 if (printinfo
.printer
== NULL
) {
1045 gndo
->ndo_dltname
= pcap_datalink_val_to_name(type
);
1046 if (gndo
->ndo_dltname
!= NULL
)
1047 error("unsupported data link type %s",
1050 error("unsupported data link type %d", type
);
1052 callback
= print_packet
;
1053 pcap_userdata
= (u_char
*)&printinfo
;
1057 * We cannot do this earlier, because we want to be able to open
1058 * the file (if done) for writing before giving up permissions.
1060 if (getuid() == 0 || geteuid() == 0) {
1061 if (username
|| chroot_dir
)
1062 droproot(username
, chroot_dir
);
1066 (void)setsignal(SIGINFO
, requestinfo
);
1069 if (vflag
> 0 && WFileName
) {
1071 * When capturing to a file, "-v" means tcpdump should,
1072 * every 10 secodns, "v"erbosely report the number of
1075 #ifdef USE_WIN32_MM_TIMER
1076 /* call verbose_stats_dump() each 1000 +/-100msec */
1077 timer_id
= timeSetEvent(1000, 100, verbose_stats_dump
, 0, TIME_PERIODIC
);
1078 setvbuf(stderr
, NULL
, _IONBF
, 0);
1079 #elif defined(HAVE_ALARM)
1080 (void)setsignal(SIGALRM
, verbose_stats_dump
);
1086 if (RFileName
== NULL
) {
1088 const char *dlt_name
;
1090 if (!vflag
&& !WFileName
) {
1091 (void)fprintf(stderr
,
1092 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1095 (void)fprintf(stderr
, "%s: ", program_name
);
1096 dlt
= pcap_datalink(pd
);
1097 dlt_name
= pcap_datalink_val_to_name(dlt
);
1098 if (dlt_name
== NULL
) {
1099 (void)fprintf(stderr
, "listening on %s, link-type %u, capture size %u bytes\n",
1100 device
, dlt
, snaplen
);
1102 (void)fprintf(stderr
, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1104 pcap_datalink_val_to_description(dlt
), snaplen
);
1106 (void)fflush(stderr
);
1109 status
= pcap_loop(pd
, cnt
, callback
, pcap_userdata
);
1110 if (WFileName
== NULL
) {
1112 * We're printing packets. Flush the printed output,
1113 * so it doesn't get intermingled with error output.
1117 * We got interrupted, so perhaps we didn't
1118 * manage to finish a line we were printing.
1119 * Print an extra newline, just in case.
1123 (void)fflush(stdout
);
1129 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
1130 program_name
, pcap_geterr(pd
));
1132 if (RFileName
== NULL
) {
1134 * We're doing a live capture. Report the capture
1140 exit(status
== -1 ? 1 : 0);
1143 /* make a clean exit on interrupts */
1145 cleanup(int signo _U_
)
1147 #ifdef USE_WIN32_MM_TIMER
1149 timeKillEvent(timer_id
);
1151 #elif defined(HAVE_ALARM)
1155 #ifdef HAVE_PCAP_BREAKLOOP
1157 * We have "pcap_breakloop()"; use it, so that we do as little
1158 * as possible in the signal handler (it's probably not safe
1159 * to do anything with standard I/O streams in a signal handler -
1160 * the ANSI C standard doesn't say it is).
1165 * We don't have "pcap_breakloop()"; this isn't safe, but
1166 * it's the best we can do. Print the summary if we're
1167 * not reading from a savefile - i.e., if we're doing a
1168 * live capture - and exit.
1170 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
1172 * We got interrupted, so perhaps we didn't
1173 * manage to finish a line we were printing.
1174 * Print an extra newline, just in case.
1177 (void)fflush(stdout
);
1185 child_cleanup(int signo _U_
)
1191 info(register int verbose
)
1193 struct pcap_stat stat
;
1195 if (pcap_stats(pd
, &stat
) < 0) {
1196 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
1201 fprintf(stderr
, "%s: ", program_name
);
1203 (void)fprintf(stderr
, "%u packets captured", packets_captured
);
1205 fputs(", ", stderr
);
1208 (void)fprintf(stderr
, "%d packets received by filter", stat
.ps_recv
);
1210 fputs(", ", stderr
);
1213 (void)fprintf(stderr
, "%d packets dropped by kernel\n", stat
.ps_drop
);
1218 compress_savefile(const char *filename
)
1223 * Set to lowest priority so that this doesn't disturb the capture
1226 setpriority(PRIO_PROCESS
, 0, NZERO
- 1);
1228 setpriority(PRIO_PROCESS
, 0, 19);
1230 if (execlp(zflag
, zflag
, filename
, NULL
) == -1)
1232 "compress_savefile:execlp(%s, %s): %s\n",
1239 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1241 struct dump_info
*dump_info
;
1247 dump_info
= (struct dump_info
*)user
;
1250 * XXX - this won't force the file to rotate on the specified time
1251 * boundary, but it will rotate on the first packet received after the
1252 * specified Gflag number of seconds. Note: if a Gflag time boundary
1253 * and a Cflag size boundary coincide, the time rotation will occur
1254 * first thereby cancelling the Cflag boundary (since the file should
1258 /* Check if it is time to rotate */
1261 /* Get the current time */
1262 if ((t
= time(NULL
)) == (time_t)-1) {
1263 error("dump_and_trunc_packet: can't get current_time: %s",
1264 pcap_strerror(errno
));
1268 /* If the time is greater than the specified window, rotate */
1269 if (t
- Gflag_time
>= Gflag
) {
1270 /* Update the Gflag_time */
1272 /* Update Gflag_count */
1275 * Close the current file and open a new one.
1277 pcap_dump_close(dump_info
->p
);
1280 * Compress the file we just closed, if the user asked for it
1283 compress_savefile(dump_info
->CurrentFileName
);
1286 * Check to see if we've exceeded the Wflag (when
1289 if (Cflag
== 0 && Wflag
> 0 && Gflag_count
>= Wflag
) {
1290 (void)fprintf(stderr
, "Maximum file limit reached: %d\n",
1295 if (dump_info
->CurrentFileName
!= NULL
)
1296 free(dump_info
->CurrentFileName
);
1297 /* Allocate space for max filename + \0. */
1298 dump_info
->CurrentFileName
= (char *)malloc(NAME_MAX
+ 1);
1299 if (dump_info
->CurrentFileName
== NULL
)
1300 error("dump_packet_and_trunc: malloc");
1302 * This is always the first file in the Cflag
1304 * We also don't need numbering if Cflag is not set.
1307 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0,
1310 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0, 0);
1312 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
1313 if (dump_info
->p
== NULL
)
1314 error("%s", pcap_geterr(pd
));
1319 * XXX - this won't prevent capture files from getting
1320 * larger than Cflag - the last packet written to the
1321 * file could put it over Cflag.
1323 if (Cflag
!= 0 && pcap_dump_ftell(dump_info
->p
) > Cflag
) {
1325 * Close the current file and open a new one.
1327 pcap_dump_close(dump_info
->p
);
1330 * Compress the file we just closed, if the user asked for it
1333 compress_savefile(dump_info
->CurrentFileName
);
1337 if (Cflag_count
>= Wflag
)
1340 if (dump_info
->CurrentFileName
!= NULL
)
1341 free(dump_info
->CurrentFileName
);
1342 dump_info
->CurrentFileName
= (char *)malloc(NAME_MAX
+ 1);
1343 if (dump_info
->CurrentFileName
== NULL
)
1344 error("dump_packet_and_trunc: malloc");
1345 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, Cflag_count
, WflagChars
);
1346 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
1347 if (dump_info
->p
== NULL
)
1348 error("%s", pcap_geterr(pd
));
1351 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
1352 #ifdef HAVE_PCAP_DUMP_FLUSH
1354 pcap_dump_flush(dump_info
->p
);
1363 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1369 pcap_dump(user
, h
, sp
);
1370 #ifdef HAVE_PCAP_DUMP_FLUSH
1372 pcap_dump_flush((pcap_dumper_t
*)user
);
1381 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1383 struct print_info
*print_info
;
1391 print_info
= (struct print_info
*)user
;
1394 * Some printers want to check that they're not walking off the
1395 * end of the packet.
1396 * Rather than pass it all the way down, we set this global.
1398 snapend
= sp
+ h
->caplen
;
1400 hdrlen
= (*print_info
->printer
)(h
, sp
);
1403 * Print the raw packet data in hex and ASCII.
1407 * Include the link-layer header.
1409 hex_and_ascii_print("\n\t", sp
, h
->caplen
);
1412 * Don't include the link-layer header - and if
1413 * we have nothing past the link-layer header,
1416 if (h
->caplen
> hdrlen
)
1417 hex_and_ascii_print("\n\t", sp
+ hdrlen
,
1418 h
->caplen
- hdrlen
);
1422 * Print the raw packet data in hex.
1426 * Include the link-layer header.
1428 hex_print("\n\t", sp
, h
->caplen
);
1431 * Don't include the link-layer header - and if
1432 * we have nothing past the link-layer header,
1435 if (h
->caplen
> hdrlen
)
1436 hex_print("\n\t", sp
+ hdrlen
,
1437 h
->caplen
- hdrlen
);
1441 * Print the raw packet data in ASCII.
1445 * Include the link-layer header.
1447 ascii_print(sp
, h
->caplen
);
1450 * Don't include the link-layer header - and if
1451 * we have nothing past the link-layer header,
1454 if (h
->caplen
> hdrlen
)
1455 ascii_print(sp
+ hdrlen
, h
->caplen
- hdrlen
);
1468 * XXX - there should really be libpcap calls to get the version
1469 * number as a string (the string would be generated from #defines
1470 * at run time, so that it's not generated from string constants
1471 * in the library, as, on many UNIX systems, those constants would
1472 * be statically linked into the application executable image, and
1473 * would thus reflect the version of libpcap on the system on
1474 * which the application was *linked*, not the system on which it's
1477 * That routine should be documented, unlike the "version[]"
1478 * string, so that UNIX vendors providing their own libpcaps
1479 * don't omit it (as a couple of vendors have...).
1481 * Packet.dll should perhaps also export a routine to return the
1482 * version number of the Packet.dll code, to supply the
1483 * "Wpcap_version" information on Windows.
1485 char WDversion
[]="current-cvs.tcpdump.org";
1486 #if !defined(HAVE_GENERATED_VERSION)
1487 char version
[]="current-cvs.tcpdump.org";
1489 char pcap_version
[]="current-cvs.tcpdump.org";
1490 char Wpcap_version
[]="3.1";
1494 * By default, print the specified data out in hex and ASCII.
1497 ndo_default_print(netdissect_options
*ndo _U_
, const u_char
*bp
, u_int length
)
1499 hex_and_ascii_print("\n\t", bp
, length
); /* pass on lf and identation string */
1503 default_print(const u_char
*bp
, u_int length
)
1505 ndo_default_print(gndo
, bp
, length
);
1509 RETSIGTYPE
requestinfo(int signo _U_
)
1519 * Called once each second in verbose mode while dumping to file
1521 #ifdef USE_WIN32_MM_TIMER
1522 void CALLBACK
verbose_stats_dump (UINT timer_id _U_
, UINT msg _U_
, DWORD_PTR arg _U_
,
1523 DWORD_PTR dw1 _U_
, DWORD_PTR dw2 _U_
)
1525 struct pcap_stat stat
;
1527 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
1528 fprintf(stderr
, "Got %u\r", packets_captured
);
1530 #elif defined(HAVE_ALARM)
1531 static void verbose_stats_dump(int sig _U_
)
1533 struct pcap_stat stat
;
1535 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
1536 fprintf(stderr
, "Got %u\r", packets_captured
);
1544 extern char version
[];
1545 #ifndef HAVE_PCAP_LIB_VERSION
1546 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
1547 extern char pcap_version
[];
1548 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1549 static char pcap_version
[] = "unknown";
1550 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1551 #endif /* HAVE_PCAP_LIB_VERSION */
1553 #ifdef HAVE_PCAP_LIB_VERSION
1555 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
1557 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1559 (void)fprintf(stderr
, "%s\n",pcap_lib_version());
1560 #else /* HAVE_PCAP_LIB_VERSION */
1562 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
1563 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
1565 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1566 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
1568 #endif /* HAVE_PCAP_LIB_VERSION */
1569 (void)fprintf(stderr
,
1570 "Usage: %s [-aAd" D_FLAG
"eflLnNOpqRStu" U_FLAG
"vxX]" B_FLAG_USAGE
" [-c count] [ -C file_size ]\n", program_name
);
1571 (void)fprintf(stderr
,
1572 "\t\t[ -E algo:secret ] [ -F file ] [ -G seconds ] [ -i interface ]\n");
1573 (void)fprintf(stderr
,
1574 "\t\t[ -M secret ] [ -r file ] [ -s snaplen ] [ -T type ] [ -w file ]\n");
1575 (void)fprintf(stderr
,
1576 "\t\t[ -W filecount ] [ -y datalinktype ] [ -z command ] [ -Z user ]\n");
1577 (void)fprintf(stderr
,
1578 "\t\t[ expression ]\n");
1586 ndo_error(netdissect_options
*ndo _U_
, const char *fmt
, ...)
1590 (void)fprintf(stderr
, "%s: ", program_name
);
1592 (void)vfprintf(stderr
, fmt
, ap
);
1596 if (fmt
[-1] != '\n')
1597 (void)fputc('\n', stderr
);
1605 ndo_warning(netdissect_options
*ndo _U_
, const char *fmt
, ...)
1609 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
1611 (void)vfprintf(stderr
, fmt
, ap
);
1615 if (fmt
[-1] != '\n')
1616 (void)fputc('\n', stderr
);