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.274 2007-11-21 20:39:24 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
);
71 #include <sys/resource.h>
78 #include "netdissect.h"
79 #include "interface.h"
80 #include "addrtoname.h"
82 #include "setsignal.h"
83 #include "gmt2local.h"
84 #include "pcap-missing.h"
90 netdissect_options Gndo
;
91 netdissect_options
*gndo
= &Gndo
;
93 int dflag
; /* print filter code */
94 int Lflag
; /* list available data link types and exit */
95 char *zflag
= NULL
; /* compress each savefile using a specified command (like gzip or bzip2) */
102 int32_t thiszone
; /* seconds offset from gmt to local time */
105 static RETSIGTYPE
cleanup(int);
106 static RETSIGTYPE
child_cleanup(int);
107 static void usage(void) __attribute__((noreturn
));
108 static void show_dlts_and_exit(pcap_t
*pd
) __attribute__((noreturn
));
110 static void print_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
111 static void ndo_default_print(netdissect_options
*, const u_char
*, u_int
);
112 static void dump_packet_and_trunc(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
113 static void dump_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
114 static void droproot(const char *, const char *);
115 static void ndo_error(netdissect_options
*ndo
, const char *fmt
, ...);
116 static void ndo_warning(netdissect_options
*ndo
, const char *fmt
, ...);
119 RETSIGTYPE
requestinfo(int);
122 #if defined(USE_WIN32_MM_TIMER)
123 #include <MMsystem.h>
124 static UINT timer_id
;
125 static void CALLBACK
verbose_stats_dump(UINT
, UINT
, DWORD_PTR
, DWORD_PTR
, DWORD_PTR
);
126 #elif defined(HAVE_ALARM)
127 static void verbose_stats_dump(int sig
);
130 static void info(int);
131 static u_int packets_captured
;
133 typedef u_int (*if_printer
)(const struct pcap_pkthdr
*, const u_char
*);
140 static struct printer printers
[] = {
141 { arcnet_if_print
, DLT_ARCNET
},
142 #ifdef DLT_ARCNET_LINUX
143 { arcnet_linux_if_print
, DLT_ARCNET_LINUX
},
145 { ether_if_print
, DLT_EN10MB
},
146 { token_if_print
, DLT_IEEE802
},
148 { lane_if_print
, DLT_LANE8023
},
151 { cip_if_print
, DLT_CIP
},
154 { cip_if_print
, DLT_ATM_CLIP
},
156 { sl_if_print
, DLT_SLIP
},
157 #ifdef DLT_SLIP_BSDOS
158 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
160 { ppp_if_print
, DLT_PPP
},
161 #ifdef DLT_PPP_WITHDIRECTION
162 { ppp_if_print
, DLT_PPP_WITHDIRECTION
},
165 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
167 { fddi_if_print
, DLT_FDDI
},
168 { null_if_print
, DLT_NULL
},
170 { null_if_print
, DLT_LOOP
},
172 { raw_if_print
, DLT_RAW
},
173 { atm_if_print
, DLT_ATM_RFC1483
},
175 { chdlc_if_print
, DLT_C_HDLC
},
178 { chdlc_if_print
, DLT_HDLC
},
180 #ifdef DLT_PPP_SERIAL
181 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
184 { pppoe_if_print
, DLT_PPP_ETHER
},
187 { sll_if_print
, DLT_LINUX_SLL
},
189 #ifdef DLT_IEEE802_11
190 { ieee802_11_if_print
, DLT_IEEE802_11
},
193 { ltalk_if_print
, DLT_LTALK
},
195 #if defined(DLT_PFLOG) && defined(HAVE_NET_PFVAR_H)
196 { pflog_if_print
, DLT_PFLOG
},
199 { fr_if_print
, DLT_FR
},
202 { fr_if_print
, DLT_FRELAY
},
205 { sunatm_if_print
, DLT_SUNATM
},
207 #ifdef DLT_IP_OVER_FC
208 { ipfc_if_print
, DLT_IP_OVER_FC
},
210 #ifdef DLT_PRISM_HEADER
211 { prism_if_print
, DLT_PRISM_HEADER
},
213 #ifdef DLT_IEEE802_11_RADIO
214 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
217 { enc_if_print
, DLT_ENC
},
219 #ifdef DLT_SYMANTEC_FIREWALL
220 { symantec_if_print
, DLT_SYMANTEC_FIREWALL
},
222 #ifdef DLT_APPLE_IP_OVER_IEEE1394
223 { ap1394_if_print
, DLT_APPLE_IP_OVER_IEEE1394
},
225 #ifdef DLT_JUNIPER_ATM1
226 { juniper_atm1_print
, DLT_JUNIPER_ATM1
},
228 #ifdef DLT_JUNIPER_ATM2
229 { juniper_atm2_print
, DLT_JUNIPER_ATM2
},
231 #ifdef DLT_JUNIPER_MFR
232 { juniper_mfr_print
, DLT_JUNIPER_MFR
},
234 #ifdef DLT_JUNIPER_MLFR
235 { juniper_mlfr_print
, DLT_JUNIPER_MLFR
},
237 #ifdef DLT_JUNIPER_MLPPP
238 { juniper_mlppp_print
, DLT_JUNIPER_MLPPP
},
240 #ifdef DLT_JUNIPER_PPPOE
241 { juniper_pppoe_print
, DLT_JUNIPER_PPPOE
},
243 #ifdef DLT_JUNIPER_PPPOE_ATM
244 { juniper_pppoe_atm_print
, DLT_JUNIPER_PPPOE_ATM
},
246 #ifdef DLT_JUNIPER_GGSN
247 { juniper_ggsn_print
, DLT_JUNIPER_GGSN
},
249 #ifdef DLT_JUNIPER_ES
250 { juniper_es_print
, DLT_JUNIPER_ES
},
252 #ifdef DLT_JUNIPER_MONITOR
253 { juniper_monitor_print
, DLT_JUNIPER_MONITOR
},
255 #ifdef DLT_JUNIPER_SERVICES
256 { juniper_services_print
, DLT_JUNIPER_SERVICES
},
258 #ifdef DLT_JUNIPER_ETHER
259 { juniper_ether_print
, DLT_JUNIPER_ETHER
},
261 #ifdef DLT_JUNIPER_PPP
262 { juniper_ppp_print
, DLT_JUNIPER_PPP
},
264 #ifdef DLT_JUNIPER_FRELAY
265 { juniper_frelay_print
, DLT_JUNIPER_FRELAY
},
267 #ifdef DLT_JUNIPER_CHDLC
268 { juniper_chdlc_print
, DLT_JUNIPER_CHDLC
},
271 { mfr_if_print
, DLT_MFR
},
273 #ifdef DLT_BLUETOOTH_HCI_H4_WITH_PHDR
274 { bt_if_print
, DLT_BLUETOOTH_HCI_H4_WITH_PHDR
},
280 lookup_printer(int type
)
284 for (p
= printers
; p
->f
; ++p
)
304 char *CurrentFileName
;
310 show_dlts_and_exit(pcap_t
*pd
)
314 const char *dlt_name
;
316 n_dlts
= pcap_list_datalinks(pd
, &dlts
);
318 error("%s", pcap_geterr(pd
));
319 else if (n_dlts
== 0 || !dlts
)
320 error("No data link types.");
322 (void) fprintf(stderr
, "Data link types (use option -y to set):\n");
324 while (--n_dlts
>= 0) {
325 dlt_name
= pcap_datalink_val_to_name(dlts
[n_dlts
]);
326 if (dlt_name
!= NULL
) {
327 (void) fprintf(stderr
, " %s (%s)", dlt_name
,
328 pcap_datalink_val_to_description(dlts
[n_dlts
]));
331 * OK, does tcpdump handle that type?
333 if (lookup_printer(dlts
[n_dlts
]) == NULL
)
334 (void) fprintf(stderr
, " (not supported)");
337 (void) fprintf(stderr
, " DLT %d (not supported)\n",
346 * Set up flags that might or might not be supported depending on the
347 * version of libpcap we're using.
351 #define B_FLAG_USAGE " [ -B size ]"
357 #ifdef HAVE_PCAP_FINDALLDEVS
358 #ifndef HAVE_PCAP_IF_T
359 #undef HAVE_PCAP_FINDALLDEVS
363 #ifdef HAVE_PCAP_FINDALLDEVS
369 #ifdef HAVE_PCAP_DUMP_FLUSH
376 /* Drop root privileges and chroot if necessary */
378 droproot(const char *username
, const char *chroot_dir
)
380 struct passwd
*pw
= NULL
;
382 if (chroot_dir
&& !username
) {
383 fprintf(stderr
, "tcpdump: Chroot without dropping root is insecure\n");
387 pw
= getpwnam(username
);
390 if (chroot(chroot_dir
) != 0 || chdir ("/") != 0) {
391 fprintf(stderr
, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
392 chroot_dir
, pcap_strerror(errno
));
396 if (initgroups(pw
->pw_name
, pw
->pw_gid
) != 0 ||
397 setgid(pw
->pw_gid
) != 0 || setuid(pw
->pw_uid
) != 0) {
398 fprintf(stderr
, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
400 (unsigned long)pw
->pw_uid
,
401 (unsigned long)pw
->pw_gid
,
402 pcap_strerror(errno
));
407 fprintf(stderr
, "tcpdump: Couldn't find user '%.32s'\n",
430 MakeFilename(char *buffer
, char *orig_name
, int cnt
, int max_chars
)
432 char *filename
= malloc(NAME_MAX
+ 1);
434 /* Process with strftime if Gflag is set. */
438 /* Convert Gflag_time to a usable format */
439 if ((local_tm
= localtime(&Gflag_time
)) == NULL
) {
440 error("MakeTimedFilename: localtime");
443 /* There's no good way to detect an error in strftime since a return
444 * value of 0 isn't necessarily failure.
446 strftime(filename
, NAME_MAX
, orig_name
, local_tm
);
448 strncpy(filename
, orig_name
, NAME_MAX
);
451 if (cnt
== 0 && max_chars
== 0)
452 strncpy(buffer
, filename
, NAME_MAX
+ 1);
454 if (snprintf(buffer
, NAME_MAX
+ 1, "%s%0*d", filename
, max_chars
, cnt
) > NAME_MAX
)
455 /* Report an error if the filename is too large */
456 error("too many output files or filename is too long (> %d)", NAME_MAX
);
460 static int tcpdump_printf(netdissect_options
*ndo _U_
,
461 const char *fmt
, ...)
468 ret
=vfprintf(stdout
, fmt
, args
);
475 main(int argc
, char **argv
)
477 register int cnt
, op
, i
;
478 bpf_u_int32 localnet
, netmask
;
479 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *WFileName
;
480 pcap_handler callback
;
482 struct bpf_program fcode
;
484 RETSIGTYPE (*oldhandler
)(int);
486 struct print_info printinfo
;
487 struct dump_info dumpinfo
;
488 u_char
*pcap_userdata
;
489 char ebuf
[PCAP_ERRBUF_SIZE
];
490 char *username
= NULL
;
491 char *chroot_dir
= NULL
;
492 #ifdef HAVE_PCAP_FINDALLDEVS
493 pcap_if_t
*devpointer
;
498 u_int UserBufferSize
= 1000000;
499 if(wsockinit() != 0) return 1;
505 gndo
->ndo_default_print
=ndo_default_print
;
506 gndo
->ndo_printf
=tcpdump_printf
;
507 gndo
->ndo_error
=ndo_error
;
508 gndo
->ndo_warning
=ndo_warning
;
509 gndo
->ndo_snaplen
= DEFAULT_SNAPLEN
;
516 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
517 program_name
= cp
+ 1;
519 program_name
= argv
[0];
521 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
530 (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)
534 /* compatibility for old -a */
543 UserBufferSize
= atoi(optarg
)*1024;
544 if (UserBufferSize
< 0)
545 error("invalid packet buffer size %s", optarg
);
552 error("invalid packet count %s", optarg
);
556 Cflag
= atoi(optarg
) * 1000000;
558 error("invalid file size %s", optarg
);
565 #ifdef HAVE_PCAP_FINDALLDEVS
567 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
570 for (i
= 0; devpointer
!= 0; i
++) {
571 printf("%d.%s", i
+1, devpointer
->name
);
572 if (devpointer
->description
!= NULL
)
573 printf(" (%s)", devpointer
->description
);
575 devpointer
= devpointer
->next
;
579 #endif /* HAVE_PCAP_FINDALLDEVS */
590 #ifndef HAVE_LIBCRYPTO
591 warning("crypto code not compiled in");
593 gndo
->ndo_espsecret
= optarg
;
605 Gflag
= atoi(optarg
);
607 error("invalid number of seconds %s", optarg
);
609 /* We will create one file initially. */
612 /* Grab the current time for rotation use. */
613 if ((Gflag_time
= time(NULL
)) == (time_t)-1) {
614 error("main: can't get current time: %s",
615 pcap_strerror(errno
));
620 if (optarg
[0] == '0' && optarg
[1] == 0)
621 error("Invalid adapter index");
623 #ifdef HAVE_PCAP_FINDALLDEVS
625 * If the argument is a number, treat it as
626 * an index into the list of adapters, as
627 * printed by "tcpdump -D".
629 * This should be OK on UNIX systems, as interfaces
630 * shouldn't have names that begin with digits.
631 * It can be useful on Windows, where more than
632 * one interface can have the same name.
634 if ((devnum
= atoi(optarg
)) != 0) {
636 error("Invalid adapter index");
638 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
641 for (i
= 0; i
< devnum
-1; i
++){
642 devpointer
= devpointer
->next
;
643 if (devpointer
== NULL
)
644 error("Invalid adapter index");
647 device
= devpointer
->name
;
650 #endif /* HAVE_PCAP_FINDALLDEVS */
657 * _IOLBF is the same as _IOFBF in Microsoft's C
658 * libraries; the only alternative they offer
661 * XXX - this should really be checking for MSVC++,
662 * not WIN32, if, for example, MinGW has its own
663 * C library that is more UNIX-compatible.
665 setvbuf(stdout
, NULL
, _IONBF
, 0);
667 #ifdef HAVE_SETLINEBUF
670 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
;
718 ++suppress_default_print
;
732 snaplen
= strtol(optarg
, &end
, 0);
733 if (optarg
== end
|| *end
!= '\0'
734 || snaplen
< 0 || snaplen
> 65535)
735 error("invalid snaplen %s", optarg
);
736 else if (snaplen
== 0)
750 if (strcasecmp(optarg
, "vat") == 0)
752 else if (strcasecmp(optarg
, "wb") == 0)
754 else if (strcasecmp(optarg
, "rpc") == 0)
756 else if (strcasecmp(optarg
, "rtp") == 0)
758 else if (strcasecmp(optarg
, "rtcp") == 0)
759 packettype
= PT_RTCP
;
760 else if (strcasecmp(optarg
, "snmp") == 0)
761 packettype
= PT_SNMP
;
762 else if (strcasecmp(optarg
, "cnfp") == 0)
763 packettype
= PT_CNFP
;
764 else if (strcasecmp(optarg
, "tftp") == 0)
765 packettype
= PT_TFTP
;
766 else if (strcasecmp(optarg
, "aodv") == 0)
767 packettype
= PT_AODV
;
769 error("unknown packet type `%s'", optarg
);
776 #ifdef HAVE_PCAP_DUMP_FLUSH
791 Wflag
= atoi(optarg
);
793 error("invalid number of output files %s", optarg
);
794 WflagChars
= getWflagChars(Wflag
);
799 ++suppress_default_print
;
804 ++suppress_default_print
;
808 gndo
->ndo_dltname
= optarg
;
810 pcap_datalink_name_to_val(gndo
->ndo_dltname
);
811 if (gndo
->ndo_dlt
< 0)
812 error("invalid data link type %s", gndo
->ndo_dltname
);
815 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
818 /* Undocumented flag */
819 #ifdef HAVE_PCAP_DEBUG
820 extern int pcap_debug
;
831 zflag
= strdup(optarg
);
840 username
= strdup(optarg
);
855 case 0: /* Default */
856 case 4: /* Default + Date*/
857 thiszone
= gmt2local(0);
860 case 1: /* No time stamp */
861 case 2: /* Unix timeval style */
862 case 3: /* Microseconds since previous packet */
863 case 5: /* Microseconds since first packet */
866 default: /* Not supported */
867 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
872 /* if run as root, prepare for chrooting */
873 if (getuid() == 0 || geteuid() == 0) {
874 /* future extensibility for cmd-line arguments */
876 chroot_dir
= WITH_CHROOT
;
881 /* if run as root, prepare for dropping root privileges */
882 if (getuid() == 0 || geteuid() == 0) {
883 /* Run with '-Z root' to restore old behaviour */
885 username
= WITH_USER
;
889 if (RFileName
!= NULL
) {
891 const char *dlt_name
;
895 * We don't need network access, so relinquish any set-UID
896 * or set-GID privileges we have (if any).
898 * We do *not* want set-UID privileges when opening a
899 * trace file, as that might let the user read other
900 * people's trace files (especially if we're set-UID
903 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
904 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
906 pd
= pcap_open_offline(RFileName
, ebuf
);
909 dlt
= pcap_datalink(pd
);
910 dlt_name
= pcap_datalink_val_to_name(dlt
);
911 if (dlt_name
== NULL
) {
912 fprintf(stderr
, "reading from file %s, link-type %u\n",
916 "reading from file %s, link-type %s (%s)\n",
918 pcap_datalink_val_to_description(dlt
));
923 error("-f and -r options are incompatible");
925 if (device
== NULL
) {
926 device
= pcap_lookupdev(ebuf
);
931 if(strlen(device
) == 1) //we assume that an ASCII string is always longer than 1 char
932 { //a Unicode string has a \0 as second byte (so strlen() is 1)
933 fprintf(stderr
, "%s: listening on %ws\n", program_name
, device
);
937 fprintf(stderr
, "%s: listening on %s\n", program_name
, device
);
943 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
949 * Let user own process after socket has been opened.
952 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
953 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
956 if(UserBufferSize
!= 1000000)
957 if(pcap_setbuff(pd
, UserBufferSize
)==-1){
958 error("%s", pcap_geterr(pd
));
962 show_dlts_and_exit(pd
);
963 if (gndo
->ndo_dlt
>= 0) {
964 #ifdef HAVE_PCAP_SET_DATALINK
965 if (pcap_set_datalink(pd
, gndo
->ndo_dlt
) < 0)
966 error("%s", pcap_geterr(pd
));
969 * We don't actually support changing the
970 * data link type, so we only let them
971 * set it to what it already is.
973 if (gndo
->ndo_dlt
!= pcap_datalink(pd
)) {
974 error("%s is not one of the DLTs supported by this device\n",
978 (void)fprintf(stderr
, "%s: data link type %s\n",
979 program_name
, gndo
->ndo_dltname
);
980 (void)fflush(stderr
);
982 i
= pcap_snapshot(pd
);
984 warning("snaplen raised from %d to %d", snaplen
, i
);
987 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
994 cmdbuf
= read_infile(infile
);
996 cmdbuf
= copy_argv(&argv
[optind
]);
998 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
999 error("%s", pcap_geterr(pd
));
1001 bpf_dump(&fcode
, dflag
);
1005 init_addrtoname(localnet
, netmask
);
1009 (void)setsignal(SIGPIPE
, cleanup
);
1010 (void)setsignal(SIGTERM
, cleanup
);
1011 (void)setsignal(SIGINT
, cleanup
);
1012 (void)setsignal(SIGCHLD
, child_cleanup
);
1014 /* Cooperate with nohup(1) */
1016 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
1017 (void)setsignal(SIGHUP
, oldhandler
);
1020 if (pcap_setfilter(pd
, &fcode
) < 0)
1021 error("%s", pcap_geterr(pd
));
1024 /* Do not exceed the default NAME_MAX for files. */
1025 dumpinfo
.CurrentFileName
= (char *)malloc(NAME_MAX
+ 1);
1027 if (dumpinfo
.CurrentFileName
== NULL
)
1028 error("malloc of dumpinfo.CurrentFileName");
1030 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1032 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, WflagChars
);
1034 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, 0);
1036 p
= pcap_dump_open(pd
, dumpinfo
.CurrentFileName
);
1038 error("%s", pcap_geterr(pd
));
1039 if (Cflag
!= 0 || Gflag
!= 0) {
1040 callback
= dump_packet_and_trunc
;
1041 dumpinfo
.WFileName
= WFileName
;
1044 pcap_userdata
= (u_char
*)&dumpinfo
;
1046 callback
= dump_packet
;
1047 pcap_userdata
= (u_char
*)p
;
1050 type
= pcap_datalink(pd
);
1051 printinfo
.printer
= lookup_printer(type
);
1052 if (printinfo
.printer
== NULL
) {
1053 gndo
->ndo_dltname
= pcap_datalink_val_to_name(type
);
1054 if (gndo
->ndo_dltname
!= NULL
)
1055 error("unsupported data link type %s",
1058 error("unsupported data link type %d", type
);
1060 callback
= print_packet
;
1061 pcap_userdata
= (u_char
*)&printinfo
;
1065 * We cannot do this earlier, because we want to be able to open
1066 * the file (if done) for writing before giving up permissions.
1068 if (getuid() == 0 || geteuid() == 0) {
1069 if (username
|| chroot_dir
)
1070 droproot(username
, chroot_dir
);
1075 * We can't get statistics when reading from a file rather
1076 * than capturing from a device.
1078 if (RFileName
== NULL
)
1079 (void)setsignal(SIGINFO
, requestinfo
);
1082 if (vflag
> 0 && WFileName
) {
1084 * When capturing to a file, "-v" means tcpdump should,
1085 * every 10 secodns, "v"erbosely report the number of
1088 #ifdef USE_WIN32_MM_TIMER
1089 /* call verbose_stats_dump() each 1000 +/-100msec */
1090 timer_id
= timeSetEvent(1000, 100, verbose_stats_dump
, 0, TIME_PERIODIC
);
1091 setvbuf(stderr
, NULL
, _IONBF
, 0);
1092 #elif defined(HAVE_ALARM)
1093 (void)setsignal(SIGALRM
, verbose_stats_dump
);
1099 if (RFileName
== NULL
) {
1101 const char *dlt_name
;
1103 if (!vflag
&& !WFileName
) {
1104 (void)fprintf(stderr
,
1105 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1108 (void)fprintf(stderr
, "%s: ", program_name
);
1109 dlt
= pcap_datalink(pd
);
1110 dlt_name
= pcap_datalink_val_to_name(dlt
);
1111 if (dlt_name
== NULL
) {
1112 (void)fprintf(stderr
, "listening on %s, link-type %u, capture size %u bytes\n",
1113 device
, dlt
, snaplen
);
1115 (void)fprintf(stderr
, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1117 pcap_datalink_val_to_description(dlt
), snaplen
);
1119 (void)fflush(stderr
);
1122 status
= pcap_loop(pd
, cnt
, callback
, pcap_userdata
);
1123 if (WFileName
== NULL
) {
1125 * We're printing packets. Flush the printed output,
1126 * so it doesn't get intermingled with error output.
1130 * We got interrupted, so perhaps we didn't
1131 * manage to finish a line we were printing.
1132 * Print an extra newline, just in case.
1136 (void)fflush(stdout
);
1142 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
1143 program_name
, pcap_geterr(pd
));
1145 if (RFileName
== NULL
) {
1147 * We're doing a live capture. Report the capture
1153 exit(status
== -1 ? 1 : 0);
1156 /* make a clean exit on interrupts */
1158 cleanup(int signo _U_
)
1160 #ifdef USE_WIN32_MM_TIMER
1162 timeKillEvent(timer_id
);
1164 #elif defined(HAVE_ALARM)
1168 #ifdef HAVE_PCAP_BREAKLOOP
1170 * We have "pcap_breakloop()"; use it, so that we do as little
1171 * as possible in the signal handler (it's probably not safe
1172 * to do anything with standard I/O streams in a signal handler -
1173 * the ANSI C standard doesn't say it is).
1178 * We don't have "pcap_breakloop()"; this isn't safe, but
1179 * it's the best we can do. Print the summary if we're
1180 * not reading from a savefile - i.e., if we're doing a
1181 * live capture - and exit.
1183 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
1185 * We got interrupted, so perhaps we didn't
1186 * manage to finish a line we were printing.
1187 * Print an extra newline, just in case.
1190 (void)fflush(stdout
);
1198 On windows, we do not use a fork, so we do not care less about
1199 waiting a child processes to die
1203 child_cleanup(int signo _U_
)
1210 info(register int verbose
)
1212 struct pcap_stat stat
;
1214 if (pcap_stats(pd
, &stat
) < 0) {
1215 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
1221 fprintf(stderr
, "%s: ", program_name
);
1223 (void)fprintf(stderr
, "%u packets captured", packets_captured
);
1225 fputs(", ", stderr
);
1228 (void)fprintf(stderr
, "%d packets received by filter", stat
.ps_recv
);
1230 fputs(", ", stderr
);
1233 (void)fprintf(stderr
, "%d packets dropped by kernel\n", stat
.ps_drop
);
1239 compress_savefile(const char *filename
)
1244 * Set to lowest priority so that this doesn't disturb the capture
1247 setpriority(PRIO_PROCESS
, 0, NZERO
- 1);
1249 setpriority(PRIO_PROCESS
, 0, 19);
1251 if (execlp(zflag
, zflag
, filename
, NULL
) == -1)
1253 "compress_savefile:execlp(%s, %s): %s\n",
1260 compress_savefile(const char *filename
)
1263 "compress_savefile failed. Functionality not implemented under windows\n");
1268 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1270 struct dump_info
*dump_info
;
1276 dump_info
= (struct dump_info
*)user
;
1279 * XXX - this won't force the file to rotate on the specified time
1280 * boundary, but it will rotate on the first packet received after the
1281 * specified Gflag number of seconds. Note: if a Gflag time boundary
1282 * and a Cflag size boundary coincide, the time rotation will occur
1283 * first thereby cancelling the Cflag boundary (since the file should
1287 /* Check if it is time to rotate */
1290 /* Get the current time */
1291 if ((t
= time(NULL
)) == (time_t)-1) {
1292 error("dump_and_trunc_packet: can't get current_time: %s",
1293 pcap_strerror(errno
));
1297 /* If the time is greater than the specified window, rotate */
1298 if (t
- Gflag_time
>= Gflag
) {
1299 /* Update the Gflag_time */
1301 /* Update Gflag_count */
1304 * Close the current file and open a new one.
1306 pcap_dump_close(dump_info
->p
);
1309 * Compress the file we just closed, if the user asked for it
1312 compress_savefile(dump_info
->CurrentFileName
);
1315 * Check to see if we've exceeded the Wflag (when
1318 if (Cflag
== 0 && Wflag
> 0 && Gflag_count
>= Wflag
) {
1319 (void)fprintf(stderr
, "Maximum file limit reached: %d\n",
1324 if (dump_info
->CurrentFileName
!= NULL
)
1325 free(dump_info
->CurrentFileName
);
1326 /* Allocate space for max filename + \0. */
1327 dump_info
->CurrentFileName
= (char *)malloc(NAME_MAX
+ 1);
1328 if (dump_info
->CurrentFileName
== NULL
)
1329 error("dump_packet_and_trunc: malloc");
1331 * This is always the first file in the Cflag
1333 * We also don't need numbering if Cflag is not set.
1336 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0,
1339 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0, 0);
1341 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
1342 if (dump_info
->p
== NULL
)
1343 error("%s", pcap_geterr(pd
));
1348 * XXX - this won't prevent capture files from getting
1349 * larger than Cflag - the last packet written to the
1350 * file could put it over Cflag.
1352 if (Cflag
!= 0 && pcap_dump_ftell(dump_info
->p
) > Cflag
) {
1354 * Close the current file and open a new one.
1356 pcap_dump_close(dump_info
->p
);
1359 * Compress the file we just closed, if the user asked for it
1362 compress_savefile(dump_info
->CurrentFileName
);
1366 if (Cflag_count
>= Wflag
)
1369 if (dump_info
->CurrentFileName
!= NULL
)
1370 free(dump_info
->CurrentFileName
);
1371 dump_info
->CurrentFileName
= (char *)malloc(NAME_MAX
+ 1);
1372 if (dump_info
->CurrentFileName
== NULL
)
1373 error("dump_packet_and_trunc: malloc");
1374 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, Cflag_count
, WflagChars
);
1375 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
1376 if (dump_info
->p
== NULL
)
1377 error("%s", pcap_geterr(pd
));
1380 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
1381 #ifdef HAVE_PCAP_DUMP_FLUSH
1383 pcap_dump_flush(dump_info
->p
);
1392 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1398 pcap_dump(user
, h
, sp
);
1399 #ifdef HAVE_PCAP_DUMP_FLUSH
1401 pcap_dump_flush((pcap_dumper_t
*)user
);
1410 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1412 struct print_info
*print_info
;
1420 print_info
= (struct print_info
*)user
;
1423 * Some printers want to check that they're not walking off the
1424 * end of the packet.
1425 * Rather than pass it all the way down, we set this global.
1427 snapend
= sp
+ h
->caplen
;
1429 hdrlen
= (*print_info
->printer
)(h
, sp
);
1432 * Print the raw packet data in hex and ASCII.
1436 * Include the link-layer header.
1438 hex_and_ascii_print("\n\t", sp
, h
->caplen
);
1441 * Don't include the link-layer header - and if
1442 * we have nothing past the link-layer header,
1445 if (h
->caplen
> hdrlen
)
1446 hex_and_ascii_print("\n\t", sp
+ hdrlen
,
1447 h
->caplen
- hdrlen
);
1451 * Print the raw packet data in hex.
1455 * Include the link-layer header.
1457 hex_print("\n\t", sp
, h
->caplen
);
1460 * Don't include the link-layer header - and if
1461 * we have nothing past the link-layer header,
1464 if (h
->caplen
> hdrlen
)
1465 hex_print("\n\t", sp
+ hdrlen
,
1466 h
->caplen
- hdrlen
);
1470 * Print the raw packet data in ASCII.
1474 * Include the link-layer header.
1476 ascii_print(sp
, h
->caplen
);
1479 * Don't include the link-layer header - and if
1480 * we have nothing past the link-layer header,
1483 if (h
->caplen
> hdrlen
)
1484 ascii_print(sp
+ hdrlen
, h
->caplen
- hdrlen
);
1497 * XXX - there should really be libpcap calls to get the version
1498 * number as a string (the string would be generated from #defines
1499 * at run time, so that it's not generated from string constants
1500 * in the library, as, on many UNIX systems, those constants would
1501 * be statically linked into the application executable image, and
1502 * would thus reflect the version of libpcap on the system on
1503 * which the application was *linked*, not the system on which it's
1506 * That routine should be documented, unlike the "version[]"
1507 * string, so that UNIX vendors providing their own libpcaps
1508 * don't omit it (as a couple of vendors have...).
1510 * Packet.dll should perhaps also export a routine to return the
1511 * version number of the Packet.dll code, to supply the
1512 * "Wpcap_version" information on Windows.
1514 char WDversion
[]="current-cvs.tcpdump.org";
1515 #if !defined(HAVE_GENERATED_VERSION)
1516 char version
[]="current-cvs.tcpdump.org";
1518 char pcap_version
[]="current-cvs.tcpdump.org";
1519 char Wpcap_version
[]="3.1";
1523 * By default, print the specified data out in hex and ASCII.
1526 ndo_default_print(netdissect_options
*ndo _U_
, const u_char
*bp
, u_int length
)
1528 hex_and_ascii_print("\n\t", bp
, length
); /* pass on lf and identation string */
1532 default_print(const u_char
*bp
, u_int length
)
1534 ndo_default_print(gndo
, bp
, length
);
1538 RETSIGTYPE
requestinfo(int signo _U_
)
1548 * Called once each second in verbose mode while dumping to file
1550 #ifdef USE_WIN32_MM_TIMER
1551 void CALLBACK
verbose_stats_dump (UINT timer_id _U_
, UINT msg _U_
, DWORD_PTR arg _U_
,
1552 DWORD_PTR dw1 _U_
, DWORD_PTR dw2 _U_
)
1554 struct pcap_stat stat
;
1556 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
1557 fprintf(stderr
, "Got %u\r", packets_captured
);
1559 #elif defined(HAVE_ALARM)
1560 static void verbose_stats_dump(int sig _U_
)
1562 struct pcap_stat stat
;
1564 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
1565 fprintf(stderr
, "Got %u\r", packets_captured
);
1573 extern char version
[];
1574 #ifndef HAVE_PCAP_LIB_VERSION
1575 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
1576 extern char pcap_version
[];
1577 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1578 static char pcap_version
[] = "unknown";
1579 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1580 #endif /* HAVE_PCAP_LIB_VERSION */
1582 #ifdef HAVE_PCAP_LIB_VERSION
1584 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
1586 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1588 (void)fprintf(stderr
, "%s\n",pcap_lib_version());
1589 #else /* HAVE_PCAP_LIB_VERSION */
1591 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
1592 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
1594 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1595 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
1597 #endif /* HAVE_PCAP_LIB_VERSION */
1598 (void)fprintf(stderr
,
1599 "Usage: %s [-aAd" D_FLAG
"efKlLnNOpqRStu" U_FLAG
"vxX]" B_FLAG_USAGE
" [-c count] [ -C file_size ]\n", program_name
);
1600 (void)fprintf(stderr
,
1601 "\t\t[ -E algo:secret ] [ -F file ] [ -G seconds ] [ -i interface ]\n");
1602 (void)fprintf(stderr
,
1603 "\t\t[ -M secret ] [ -r file ] [ -s snaplen ] [ -T type ] [ -w file ]\n");
1604 (void)fprintf(stderr
,
1605 "\t\t[ -W filecount ] [ -y datalinktype ] [ -z command ] [ -Z user ]\n");
1606 (void)fprintf(stderr
,
1607 "\t\t[ expression ]\n");
1615 ndo_error(netdissect_options
*ndo _U_
, const char *fmt
, ...)
1619 (void)fprintf(stderr
, "%s: ", program_name
);
1621 (void)vfprintf(stderr
, fmt
, ap
);
1625 if (fmt
[-1] != '\n')
1626 (void)fputc('\n', stderr
);
1634 ndo_warning(netdissect_options
*ndo _U_
, const char *fmt
, ...)
1638 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
1640 (void)vfprintf(stderr
, fmt
, ap
);
1644 if (fmt
[-1] != '\n')
1645 (void)fputc('\n', stderr
);