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.271 2007-09-24 23:46:27 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
},
194 #if defined(DLT_PFLOG) && defined(HAVE_NET_PFVAR_H)
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
},
272 #ifdef DLT_BLUETOOTH_HCI_H4_WITH_PHDR
273 { bt_if_print
, DLT_BLUETOOTH_HCI_H4_WITH_PHDR
},
279 lookup_printer(int type
)
283 for (p
= printers
; p
->f
; ++p
)
303 char *CurrentFileName
;
309 show_dlts_and_exit(pcap_t
*pd
)
313 const char *dlt_name
;
315 n_dlts
= pcap_list_datalinks(pd
, &dlts
);
317 error("%s", pcap_geterr(pd
));
318 else if (n_dlts
== 0 || !dlts
)
319 error("No data link types.");
321 (void) fprintf(stderr
, "Data link types (use option -y to set):\n");
323 while (--n_dlts
>= 0) {
324 dlt_name
= pcap_datalink_val_to_name(dlts
[n_dlts
]);
325 if (dlt_name
!= NULL
) {
326 (void) fprintf(stderr
, " %s (%s)", dlt_name
,
327 pcap_datalink_val_to_description(dlts
[n_dlts
]));
330 * OK, does tcpdump handle that type?
332 if (lookup_printer(dlts
[n_dlts
]) == NULL
)
333 (void) fprintf(stderr
, " (not supported)");
336 (void) fprintf(stderr
, " DLT %d (not supported)\n",
345 * Set up flags that might or might not be supported depending on the
346 * version of libpcap we're using.
350 #define B_FLAG_USAGE " [ -B size ]"
356 #ifdef HAVE_PCAP_FINDALLDEVS
357 #ifndef HAVE_PCAP_IF_T
358 #undef HAVE_PCAP_FINDALLDEVS
362 #ifdef HAVE_PCAP_FINDALLDEVS
368 #ifdef HAVE_PCAP_DUMP_FLUSH
375 /* Drop root privileges and chroot if necessary */
377 droproot(const char *username
, const char *chroot_dir
)
379 struct passwd
*pw
= NULL
;
381 if (chroot_dir
&& !username
) {
382 fprintf(stderr
, "tcpdump: Chroot without dropping root is insecure\n");
386 pw
= getpwnam(username
);
389 if (chroot(chroot_dir
) != 0 || chdir ("/") != 0) {
390 fprintf(stderr
, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
391 chroot_dir
, pcap_strerror(errno
));
395 if (initgroups(pw
->pw_name
, pw
->pw_gid
) != 0 ||
396 setgid(pw
->pw_gid
) != 0 || setuid(pw
->pw_uid
) != 0) {
397 fprintf(stderr
, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
399 (unsigned long)pw
->pw_uid
,
400 (unsigned long)pw
->pw_gid
,
401 pcap_strerror(errno
));
406 fprintf(stderr
, "tcpdump: Couldn't find user '%.32s'\n",
429 MakeFilename(char *buffer
, char *orig_name
, int cnt
, int max_chars
)
431 char *filename
= malloc(NAME_MAX
+ 1);
433 /* Process with strftime if Gflag is set. */
437 /* Convert Gflag_time to a usable format */
438 if ((local_tm
= localtime(&Gflag_time
)) == NULL
) {
439 error("MakeTimedFilename: localtime");
442 /* There's no good way to detect an error in strftime since a return
443 * value of 0 isn't necessarily failure.
445 strftime(filename
, NAME_MAX
, orig_name
, local_tm
);
447 strncpy(filename
, orig_name
, NAME_MAX
);
450 if (cnt
== 0 && max_chars
== 0)
451 strncpy(buffer
, filename
, NAME_MAX
+ 1);
453 if (snprintf(buffer
, NAME_MAX
+ 1, "%s%0*d", filename
, max_chars
, cnt
) > NAME_MAX
)
454 /* Report an error if the filename is too large */
455 error("too many output files or filename is too long (> %d)", NAME_MAX
);
459 static int tcpdump_printf(netdissect_options
*ndo _U_
,
460 const char *fmt
, ...)
467 ret
=vfprintf(stdout
, fmt
, args
);
474 main(int argc
, char **argv
)
476 register int cnt
, op
, i
;
477 bpf_u_int32 localnet
, netmask
;
478 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *WFileName
;
479 pcap_handler callback
;
481 struct bpf_program fcode
;
483 RETSIGTYPE (*oldhandler
)(int);
485 struct print_info printinfo
;
486 struct dump_info dumpinfo
;
487 u_char
*pcap_userdata
;
488 char ebuf
[PCAP_ERRBUF_SIZE
];
489 char *username
= NULL
;
490 char *chroot_dir
= NULL
;
491 #ifdef HAVE_PCAP_FINDALLDEVS
492 pcap_if_t
*devpointer
;
497 u_int UserBufferSize
= 1000000;
498 if(wsockinit() != 0) return 1;
504 gndo
->ndo_default_print
=ndo_default_print
;
505 gndo
->ndo_printf
=tcpdump_printf
;
506 gndo
->ndo_error
=ndo_error
;
507 gndo
->ndo_warning
=ndo_warning
;
508 gndo
->ndo_snaplen
= DEFAULT_SNAPLEN
;
515 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
516 program_name
= cp
+ 1;
518 program_name
= argv
[0];
520 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
529 (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)
533 /* compatibility for old -a */
542 UserBufferSize
= atoi(optarg
)*1024;
543 if (UserBufferSize
< 0)
544 error("invalid packet buffer size %s", optarg
);
551 error("invalid packet count %s", optarg
);
555 Cflag
= atoi(optarg
) * 1000000;
557 error("invalid file size %s", optarg
);
564 #ifdef HAVE_PCAP_FINDALLDEVS
566 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
569 for (i
= 0; devpointer
!= 0; i
++) {
570 printf("%d.%s", i
+1, devpointer
->name
);
571 if (devpointer
->description
!= NULL
)
572 printf(" (%s)", devpointer
->description
);
574 devpointer
= devpointer
->next
;
578 #endif /* HAVE_PCAP_FINDALLDEVS */
589 #ifndef HAVE_LIBCRYPTO
590 warning("crypto code not compiled in");
592 gndo
->ndo_espsecret
= optarg
;
604 Gflag
= atoi(optarg
);
606 error("invalid number of seconds %s", optarg
);
608 /* We will create one file initially. */
611 /* Grab the current time for rotation use. */
612 if ((Gflag_time
= time(NULL
)) == (time_t)-1) {
613 error("main: can't get current time: %s",
614 pcap_strerror(errno
));
619 if (optarg
[0] == '0' && optarg
[1] == 0)
620 error("Invalid adapter index");
622 #ifdef HAVE_PCAP_FINDALLDEVS
624 * If the argument is a number, treat it as
625 * an index into the list of adapters, as
626 * printed by "tcpdump -D".
628 * This should be OK on UNIX systems, as interfaces
629 * shouldn't have names that begin with digits.
630 * It can be useful on Windows, where more than
631 * one interface can have the same name.
633 if ((devnum
= atoi(optarg
)) != 0) {
635 error("Invalid adapter index");
637 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
640 for (i
= 0; i
< devnum
-1; i
++){
641 devpointer
= devpointer
->next
;
642 if (devpointer
== NULL
)
643 error("Invalid adapter index");
646 device
= devpointer
->name
;
649 #endif /* HAVE_PCAP_FINDALLDEVS */
656 * _IOLBF is the same as _IOFBF in Microsoft's C
657 * libraries; the only alternative they offer
660 * XXX - this should really be checking for MSVC++,
661 * not WIN32, if, for example, MinGW has its own
662 * C library that is more UNIX-compatible.
664 setvbuf(stdout
, NULL
, _IONBF
, 0);
666 #ifdef HAVE_SETLINEBUF
669 setvbuf(stdout
, NULL
, _IOLBF
, 0);
680 if (smiLoadModule(optarg
) == 0) {
681 error("could not load MIB module %s", optarg
);
685 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
686 program_name
, optarg
);
687 (void)fprintf(stderr
, "(no libsmi support)\n");
692 /* TCP-MD5 shared secret */
693 #ifndef HAVE_LIBCRYPTO
694 warning("crypto code not compiled in");
696 tcpmd5secret
= optarg
;
717 ++suppress_default_print
;
731 snaplen
= strtol(optarg
, &end
, 0);
732 if (optarg
== end
|| *end
!= '\0'
733 || snaplen
< 0 || snaplen
> 65535)
734 error("invalid snaplen %s", optarg
);
735 else if (snaplen
== 0)
749 if (strcasecmp(optarg
, "vat") == 0)
751 else if (strcasecmp(optarg
, "wb") == 0)
753 else if (strcasecmp(optarg
, "rpc") == 0)
755 else if (strcasecmp(optarg
, "rtp") == 0)
757 else if (strcasecmp(optarg
, "rtcp") == 0)
758 packettype
= PT_RTCP
;
759 else if (strcasecmp(optarg
, "snmp") == 0)
760 packettype
= PT_SNMP
;
761 else if (strcasecmp(optarg
, "cnfp") == 0)
762 packettype
= PT_CNFP
;
763 else if (strcasecmp(optarg
, "tftp") == 0)
764 packettype
= PT_TFTP
;
765 else if (strcasecmp(optarg
, "aodv") == 0)
766 packettype
= PT_AODV
;
768 error("unknown packet type `%s'", optarg
);
775 #ifdef HAVE_PCAP_DUMP_FLUSH
790 Wflag
= atoi(optarg
);
792 error("invalid number of output files %s", optarg
);
793 WflagChars
= getWflagChars(Wflag
);
798 ++suppress_default_print
;
803 ++suppress_default_print
;
807 gndo
->ndo_dltname
= optarg
;
809 pcap_datalink_name_to_val(gndo
->ndo_dltname
);
810 if (gndo
->ndo_dlt
< 0)
811 error("invalid data link type %s", gndo
->ndo_dltname
);
814 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
817 /* Undocumented flag */
818 #ifdef HAVE_PCAP_DEBUG
819 extern int pcap_debug
;
830 zflag
= strdup(optarg
);
839 username
= strdup(optarg
);
854 case 0: /* Default */
855 case 4: /* Default + Date*/
856 thiszone
= gmt2local(0);
859 case 1: /* No time stamp */
860 case 2: /* Unix timeval style */
861 case 3: /* Microseconds since previous packet */
862 case 5: /* Microseconds since first packet */
865 default: /* Not supported */
866 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
871 /* if run as root, prepare for chrooting */
872 if (getuid() == 0 || geteuid() == 0) {
873 /* future extensibility for cmd-line arguments */
875 chroot_dir
= WITH_CHROOT
;
880 /* if run as root, prepare for dropping root privileges */
881 if (getuid() == 0 || geteuid() == 0) {
882 /* Run with '-Z root' to restore old behaviour */
884 username
= WITH_USER
;
888 if (RFileName
!= NULL
) {
890 const char *dlt_name
;
894 * We don't need network access, so relinquish any set-UID
895 * or set-GID privileges we have (if any).
897 * We do *not* want set-UID privileges when opening a
898 * trace file, as that might let the user read other
899 * people's trace files (especially if we're set-UID
902 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
903 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
905 pd
= pcap_open_offline(RFileName
, ebuf
);
908 dlt
= pcap_datalink(pd
);
909 dlt_name
= pcap_datalink_val_to_name(dlt
);
910 if (dlt_name
== NULL
) {
911 fprintf(stderr
, "reading from file %s, link-type %u\n",
915 "reading from file %s, link-type %s (%s)\n",
917 pcap_datalink_val_to_description(dlt
));
922 error("-f and -r options are incompatible");
924 if (device
== NULL
) {
925 device
= pcap_lookupdev(ebuf
);
930 if(strlen(device
) == 1) //we assume that an ASCII string is always longer than 1 char
931 { //a Unicode string has a \0 as second byte (so strlen() is 1)
932 fprintf(stderr
, "%s: listening on %ws\n", program_name
, device
);
936 fprintf(stderr
, "%s: listening on %s\n", program_name
, device
);
942 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
948 * Let user own process after socket has been opened.
951 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
952 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
955 if(UserBufferSize
!= 1000000)
956 if(pcap_setbuff(pd
, UserBufferSize
)==-1){
957 error("%s", pcap_geterr(pd
));
961 show_dlts_and_exit(pd
);
962 if (gndo
->ndo_dlt
>= 0) {
963 #ifdef HAVE_PCAP_SET_DATALINK
964 if (pcap_set_datalink(pd
, gndo
->ndo_dlt
) < 0)
965 error("%s", pcap_geterr(pd
));
968 * We don't actually support changing the
969 * data link type, so we only let them
970 * set it to what it already is.
972 if (gndo
->ndo_dlt
!= pcap_datalink(pd
)) {
973 error("%s is not one of the DLTs supported by this device\n",
977 (void)fprintf(stderr
, "%s: data link type %s\n",
978 program_name
, gndo
->ndo_dltname
);
979 (void)fflush(stderr
);
981 i
= pcap_snapshot(pd
);
983 warning("snaplen raised from %d to %d", snaplen
, i
);
986 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
993 cmdbuf
= read_infile(infile
);
995 cmdbuf
= copy_argv(&argv
[optind
]);
997 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
998 error("%s", pcap_geterr(pd
));
1000 bpf_dump(&fcode
, dflag
);
1004 init_addrtoname(localnet
, netmask
);
1008 (void)setsignal(SIGPIPE
, cleanup
);
1010 (void)setsignal(SIGTERM
, cleanup
);
1011 (void)setsignal(SIGINT
, cleanup
);
1012 (void)setsignal(SIGCHLD
, child_cleanup
);
1013 /* Cooperate with nohup(1) */
1015 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
1016 (void)setsignal(SIGHUP
, oldhandler
);
1019 if (pcap_setfilter(pd
, &fcode
) < 0)
1020 error("%s", pcap_geterr(pd
));
1023 /* Do not exceed the default NAME_MAX for files. */
1024 dumpinfo
.CurrentFileName
= (char *)malloc(NAME_MAX
+ 1);
1026 if (dumpinfo
.CurrentFileName
== NULL
)
1027 error("malloc of dumpinfo.CurrentFileName");
1029 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1031 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, WflagChars
);
1033 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, 0);
1035 p
= pcap_dump_open(pd
, dumpinfo
.CurrentFileName
);
1037 error("%s", pcap_geterr(pd
));
1038 if (Cflag
!= 0 || Gflag
!= 0) {
1039 callback
= dump_packet_and_trunc
;
1040 dumpinfo
.WFileName
= WFileName
;
1043 pcap_userdata
= (u_char
*)&dumpinfo
;
1045 callback
= dump_packet
;
1046 pcap_userdata
= (u_char
*)p
;
1049 type
= pcap_datalink(pd
);
1050 printinfo
.printer
= lookup_printer(type
);
1051 if (printinfo
.printer
== NULL
) {
1052 gndo
->ndo_dltname
= pcap_datalink_val_to_name(type
);
1053 if (gndo
->ndo_dltname
!= NULL
)
1054 error("unsupported data link type %s",
1057 error("unsupported data link type %d", type
);
1059 callback
= print_packet
;
1060 pcap_userdata
= (u_char
*)&printinfo
;
1064 * We cannot do this earlier, because we want to be able to open
1065 * the file (if done) for writing before giving up permissions.
1067 if (getuid() == 0 || geteuid() == 0) {
1068 if (username
|| chroot_dir
)
1069 droproot(username
, chroot_dir
);
1073 (void)setsignal(SIGINFO
, requestinfo
);
1076 if (vflag
> 0 && WFileName
) {
1078 * When capturing to a file, "-v" means tcpdump should,
1079 * every 10 secodns, "v"erbosely report the number of
1082 #ifdef USE_WIN32_MM_TIMER
1083 /* call verbose_stats_dump() each 1000 +/-100msec */
1084 timer_id
= timeSetEvent(1000, 100, verbose_stats_dump
, 0, TIME_PERIODIC
);
1085 setvbuf(stderr
, NULL
, _IONBF
, 0);
1086 #elif defined(HAVE_ALARM)
1087 (void)setsignal(SIGALRM
, verbose_stats_dump
);
1093 if (RFileName
== NULL
) {
1095 const char *dlt_name
;
1097 if (!vflag
&& !WFileName
) {
1098 (void)fprintf(stderr
,
1099 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1102 (void)fprintf(stderr
, "%s: ", program_name
);
1103 dlt
= pcap_datalink(pd
);
1104 dlt_name
= pcap_datalink_val_to_name(dlt
);
1105 if (dlt_name
== NULL
) {
1106 (void)fprintf(stderr
, "listening on %s, link-type %u, capture size %u bytes\n",
1107 device
, dlt
, snaplen
);
1109 (void)fprintf(stderr
, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1111 pcap_datalink_val_to_description(dlt
), snaplen
);
1113 (void)fflush(stderr
);
1116 status
= pcap_loop(pd
, cnt
, callback
, pcap_userdata
);
1117 if (WFileName
== NULL
) {
1119 * We're printing packets. Flush the printed output,
1120 * so it doesn't get intermingled with error output.
1124 * We got interrupted, so perhaps we didn't
1125 * manage to finish a line we were printing.
1126 * Print an extra newline, just in case.
1130 (void)fflush(stdout
);
1136 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
1137 program_name
, pcap_geterr(pd
));
1139 if (RFileName
== NULL
) {
1141 * We're doing a live capture. Report the capture
1147 exit(status
== -1 ? 1 : 0);
1150 /* make a clean exit on interrupts */
1152 cleanup(int signo _U_
)
1154 #ifdef USE_WIN32_MM_TIMER
1156 timeKillEvent(timer_id
);
1158 #elif defined(HAVE_ALARM)
1162 #ifdef HAVE_PCAP_BREAKLOOP
1164 * We have "pcap_breakloop()"; use it, so that we do as little
1165 * as possible in the signal handler (it's probably not safe
1166 * to do anything with standard I/O streams in a signal handler -
1167 * the ANSI C standard doesn't say it is).
1172 * We don't have "pcap_breakloop()"; this isn't safe, but
1173 * it's the best we can do. Print the summary if we're
1174 * not reading from a savefile - i.e., if we're doing a
1175 * live capture - and exit.
1177 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
1179 * We got interrupted, so perhaps we didn't
1180 * manage to finish a line we were printing.
1181 * Print an extra newline, just in case.
1184 (void)fflush(stdout
);
1192 child_cleanup(int signo _U_
)
1198 info(register int verbose
)
1200 struct pcap_stat stat
;
1202 if (pcap_stats(pd
, &stat
) < 0) {
1203 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
1208 fprintf(stderr
, "%s: ", program_name
);
1210 (void)fprintf(stderr
, "%u packets captured", packets_captured
);
1212 fputs(", ", stderr
);
1215 (void)fprintf(stderr
, "%d packets received by filter", stat
.ps_recv
);
1217 fputs(", ", stderr
);
1220 (void)fprintf(stderr
, "%d packets dropped by kernel\n", stat
.ps_drop
);
1225 compress_savefile(const char *filename
)
1230 * Set to lowest priority so that this doesn't disturb the capture
1233 setpriority(PRIO_PROCESS
, 0, NZERO
- 1);
1235 setpriority(PRIO_PROCESS
, 0, 19);
1237 if (execlp(zflag
, zflag
, filename
, NULL
) == -1)
1239 "compress_savefile:execlp(%s, %s): %s\n",
1246 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1248 struct dump_info
*dump_info
;
1254 dump_info
= (struct dump_info
*)user
;
1257 * XXX - this won't force the file to rotate on the specified time
1258 * boundary, but it will rotate on the first packet received after the
1259 * specified Gflag number of seconds. Note: if a Gflag time boundary
1260 * and a Cflag size boundary coincide, the time rotation will occur
1261 * first thereby cancelling the Cflag boundary (since the file should
1265 /* Check if it is time to rotate */
1268 /* Get the current time */
1269 if ((t
= time(NULL
)) == (time_t)-1) {
1270 error("dump_and_trunc_packet: can't get current_time: %s",
1271 pcap_strerror(errno
));
1275 /* If the time is greater than the specified window, rotate */
1276 if (t
- Gflag_time
>= Gflag
) {
1277 /* Update the Gflag_time */
1279 /* Update Gflag_count */
1282 * Close the current file and open a new one.
1284 pcap_dump_close(dump_info
->p
);
1287 * Compress the file we just closed, if the user asked for it
1290 compress_savefile(dump_info
->CurrentFileName
);
1293 * Check to see if we've exceeded the Wflag (when
1296 if (Cflag
== 0 && Wflag
> 0 && Gflag_count
>= Wflag
) {
1297 (void)fprintf(stderr
, "Maximum file limit reached: %d\n",
1302 if (dump_info
->CurrentFileName
!= NULL
)
1303 free(dump_info
->CurrentFileName
);
1304 /* Allocate space for max filename + \0. */
1305 dump_info
->CurrentFileName
= (char *)malloc(NAME_MAX
+ 1);
1306 if (dump_info
->CurrentFileName
== NULL
)
1307 error("dump_packet_and_trunc: malloc");
1309 * This is always the first file in the Cflag
1311 * We also don't need numbering if Cflag is not set.
1314 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0,
1317 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0, 0);
1319 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
1320 if (dump_info
->p
== NULL
)
1321 error("%s", pcap_geterr(pd
));
1326 * XXX - this won't prevent capture files from getting
1327 * larger than Cflag - the last packet written to the
1328 * file could put it over Cflag.
1330 if (Cflag
!= 0 && pcap_dump_ftell(dump_info
->p
) > Cflag
) {
1332 * Close the current file and open a new one.
1334 pcap_dump_close(dump_info
->p
);
1337 * Compress the file we just closed, if the user asked for it
1340 compress_savefile(dump_info
->CurrentFileName
);
1344 if (Cflag_count
>= Wflag
)
1347 if (dump_info
->CurrentFileName
!= NULL
)
1348 free(dump_info
->CurrentFileName
);
1349 dump_info
->CurrentFileName
= (char *)malloc(NAME_MAX
+ 1);
1350 if (dump_info
->CurrentFileName
== NULL
)
1351 error("dump_packet_and_trunc: malloc");
1352 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, Cflag_count
, WflagChars
);
1353 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
1354 if (dump_info
->p
== NULL
)
1355 error("%s", pcap_geterr(pd
));
1358 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
1359 #ifdef HAVE_PCAP_DUMP_FLUSH
1361 pcap_dump_flush(dump_info
->p
);
1370 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1376 pcap_dump(user
, h
, sp
);
1377 #ifdef HAVE_PCAP_DUMP_FLUSH
1379 pcap_dump_flush((pcap_dumper_t
*)user
);
1388 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1390 struct print_info
*print_info
;
1398 print_info
= (struct print_info
*)user
;
1401 * Some printers want to check that they're not walking off the
1402 * end of the packet.
1403 * Rather than pass it all the way down, we set this global.
1405 snapend
= sp
+ h
->caplen
;
1407 hdrlen
= (*print_info
->printer
)(h
, sp
);
1410 * Print the raw packet data in hex and ASCII.
1414 * Include the link-layer header.
1416 hex_and_ascii_print("\n\t", sp
, h
->caplen
);
1419 * Don't include the link-layer header - and if
1420 * we have nothing past the link-layer header,
1423 if (h
->caplen
> hdrlen
)
1424 hex_and_ascii_print("\n\t", sp
+ hdrlen
,
1425 h
->caplen
- hdrlen
);
1429 * Print the raw packet data in hex.
1433 * Include the link-layer header.
1435 hex_print("\n\t", sp
, h
->caplen
);
1438 * Don't include the link-layer header - and if
1439 * we have nothing past the link-layer header,
1442 if (h
->caplen
> hdrlen
)
1443 hex_print("\n\t", sp
+ hdrlen
,
1444 h
->caplen
- hdrlen
);
1448 * Print the raw packet data in ASCII.
1452 * Include the link-layer header.
1454 ascii_print(sp
, h
->caplen
);
1457 * Don't include the link-layer header - and if
1458 * we have nothing past the link-layer header,
1461 if (h
->caplen
> hdrlen
)
1462 ascii_print(sp
+ hdrlen
, h
->caplen
- hdrlen
);
1475 * XXX - there should really be libpcap calls to get the version
1476 * number as a string (the string would be generated from #defines
1477 * at run time, so that it's not generated from string constants
1478 * in the library, as, on many UNIX systems, those constants would
1479 * be statically linked into the application executable image, and
1480 * would thus reflect the version of libpcap on the system on
1481 * which the application was *linked*, not the system on which it's
1484 * That routine should be documented, unlike the "version[]"
1485 * string, so that UNIX vendors providing their own libpcaps
1486 * don't omit it (as a couple of vendors have...).
1488 * Packet.dll should perhaps also export a routine to return the
1489 * version number of the Packet.dll code, to supply the
1490 * "Wpcap_version" information on Windows.
1492 char WDversion
[]="current-cvs.tcpdump.org";
1493 #if !defined(HAVE_GENERATED_VERSION)
1494 char version
[]="current-cvs.tcpdump.org";
1496 char pcap_version
[]="current-cvs.tcpdump.org";
1497 char Wpcap_version
[]="3.1";
1501 * By default, print the specified data out in hex and ASCII.
1504 ndo_default_print(netdissect_options
*ndo _U_
, const u_char
*bp
, u_int length
)
1506 hex_and_ascii_print("\n\t", bp
, length
); /* pass on lf and identation string */
1510 default_print(const u_char
*bp
, u_int length
)
1512 ndo_default_print(gndo
, bp
, length
);
1516 RETSIGTYPE
requestinfo(int signo _U_
)
1526 * Called once each second in verbose mode while dumping to file
1528 #ifdef USE_WIN32_MM_TIMER
1529 void CALLBACK
verbose_stats_dump (UINT timer_id _U_
, UINT msg _U_
, DWORD_PTR arg _U_
,
1530 DWORD_PTR dw1 _U_
, DWORD_PTR dw2 _U_
)
1532 struct pcap_stat stat
;
1534 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
1535 fprintf(stderr
, "Got %u\r", packets_captured
);
1537 #elif defined(HAVE_ALARM)
1538 static void verbose_stats_dump(int sig _U_
)
1540 struct pcap_stat stat
;
1542 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
1543 fprintf(stderr
, "Got %u\r", packets_captured
);
1551 extern char version
[];
1552 #ifndef HAVE_PCAP_LIB_VERSION
1553 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
1554 extern char pcap_version
[];
1555 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1556 static char pcap_version
[] = "unknown";
1557 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1558 #endif /* HAVE_PCAP_LIB_VERSION */
1560 #ifdef HAVE_PCAP_LIB_VERSION
1562 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
1564 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1566 (void)fprintf(stderr
, "%s\n",pcap_lib_version());
1567 #else /* HAVE_PCAP_LIB_VERSION */
1569 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
1570 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
1572 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1573 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
1575 #endif /* HAVE_PCAP_LIB_VERSION */
1576 (void)fprintf(stderr
,
1577 "Usage: %s [-aAd" D_FLAG
"efKlLnNOpqRStu" U_FLAG
"vxX]" B_FLAG_USAGE
" [-c count] [ -C file_size ]\n", program_name
);
1578 (void)fprintf(stderr
,
1579 "\t\t[ -E algo:secret ] [ -F file ] [ -G seconds ] [ -i interface ]\n");
1580 (void)fprintf(stderr
,
1581 "\t\t[ -M secret ] [ -r file ] [ -s snaplen ] [ -T type ] [ -w file ]\n");
1582 (void)fprintf(stderr
,
1583 "\t\t[ -W filecount ] [ -y datalinktype ] [ -z command ] [ -Z user ]\n");
1584 (void)fprintf(stderr
,
1585 "\t\t[ expression ]\n");
1593 ndo_error(netdissect_options
*ndo _U_
, const char *fmt
, ...)
1597 (void)fprintf(stderr
, "%s: ", program_name
);
1599 (void)vfprintf(stderr
, fmt
, ap
);
1603 if (fmt
[-1] != '\n')
1604 (void)fputc('\n', stderr
);
1612 ndo_warning(netdissect_options
*ndo _U_
, const char *fmt
, ...)
1616 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
1618 (void)vfprintf(stderr
, fmt
, ap
);
1622 if (fmt
[-1] != '\n')
1623 (void)fputc('\n', stderr
);