1 /* $NetBSD: tcpdump.c,v 1.8 2007/07/24 11:53:50 drochner Exp $ */
4 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions
9 * retain the above copyright notice and this paragraph in its entirety, (2)
10 * distributions including binary code include the above copyright notice and
11 * this paragraph in its entirety in the documentation or other materials
12 * provided with the distribution, and (3) all advertising materials mentioning
13 * features or use of this software display the following acknowledgement:
14 * ``This product includes software developed by the University of California,
15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16 * the University nor the names of its contributors may be used to endorse
17 * or promote products derived from this software without specific prior
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 * Support for splitting captures into multiple files with a maximum
27 * Seth Webster <swebster@sst.ll.mit.edu>
30 #include <sys/cdefs.h>
33 static const char copyright
[] _U_
=
34 "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
35 The Regents of the University of California. All rights reserved.\n";
36 static const char rcsid
[] _U_
=
37 "@(#) Header: /tcpdump/master/tcpdump/tcpdump.c,v 1.253.2.12 2006/02/01 14:39:56 hannes Exp (LBL)";
39 __RCSID("$NetBSD: tcpdump.c,v 1.8 2007/07/24 11:53:50 drochner Exp $");
44 * tcpdump - monitor tcp/ip traffic on an ethernet.
46 * First written in 1987 by Van Jacobson, Lawrence Berkeley Laboratory.
47 * Mercilessly hacked and occasionally improved since then via the
48 * combined efforts of Van, Steve McCanne and Craig Leres of LBL.
55 #include <tcpdump-stdinc.h>
60 extern int strcasecmp (const char *__s1
, const char *__s2
);
81 #include "netdissect.h"
82 #include "interface.h"
83 #include "addrtoname.h"
85 #include "setsignal.h"
86 #include "gmt2local.h"
87 #include "pcap-missing.h"
89 netdissect_options Gndo
;
90 netdissect_options
*gndo
= &Gndo
;
93 * Define the maximum number of files for the -C flag, and how many
94 * characters can be added to a filename for the -C flag (which
95 * should be enough to handle MAX_CFLAG - 1).
97 #define MAX_CFLAG 1000000
98 #define MAX_CFLAG_CHARS 6
100 int dflag
; /* print filter code */
101 int Lflag
; /* list available data link types and exit */
103 static int infodelay
;
104 static int infoprint
;
108 int32_t thiszone
; /* seconds offset from gmt to local time */
111 static RETSIGTYPE
cleanup(int);
112 static void usage(void) __attribute__((noreturn
));
113 static void show_dlts_and_exit(pcap_t
*pd
) __attribute__((noreturn
));
115 static void print_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
116 static void ndo_default_print(netdissect_options
*, const u_char
*, u_int
);
117 static void dump_packet_and_trunc(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
118 static void dump_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
119 static void droproot(const char *, const char *);
120 static void ndo_error(netdissect_options
*ndo
, const char *fmt
, ...);
121 static void ndo_warning(netdissect_options
*ndo
, const char *fmt
, ...);
124 RETSIGTYPE
requestinfo(int);
127 #if defined(USE_WIN32_MM_TIMER)
128 #include <MMsystem.h>
129 static UINT timer_id
;
130 static void CALLBACK
verbose_stats_dump(UINT
, UINT
, DWORD_PTR
, DWORD_PTR
, DWORD_PTR
);
131 #elif defined(HAVE_ALARM)
132 static void verbose_stats_dump(int sig
);
135 static void info(int);
136 static u_int packets_captured
;
138 typedef u_int (*if_printer
)(const struct pcap_pkthdr
*, const u_char
*);
145 static struct printer printers
[] = {
146 { arcnet_if_print
, DLT_ARCNET
},
147 #ifdef DLT_ARCNET_LINUX
148 { arcnet_linux_if_print
, DLT_ARCNET_LINUX
},
150 { ether_if_print
, DLT_EN10MB
},
151 { token_if_print
, DLT_IEEE802
},
153 { lane_if_print
, DLT_LANE8023
},
156 { cip_if_print
, DLT_CIP
},
159 { cip_if_print
, DLT_ATM_CLIP
},
161 { sl_if_print
, DLT_SLIP
},
162 #ifdef DLT_SLIP_BSDOS
163 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
165 { ppp_if_print
, DLT_PPP
},
166 #ifdef DLT_PPP_WITHDIRECTION
167 { ppp_if_print
, DLT_PPP_WITHDIRECTION
},
170 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
172 { fddi_if_print
, DLT_FDDI
},
173 { null_if_print
, DLT_NULL
},
175 { null_if_print
, DLT_LOOP
},
177 { raw_if_print
, DLT_RAW
},
178 { atm_if_print
, DLT_ATM_RFC1483
},
180 { chdlc_if_print
, DLT_C_HDLC
},
183 { chdlc_if_print
, DLT_HDLC
},
185 #ifdef DLT_PPP_SERIAL
186 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
189 { pppoe_if_print
, DLT_PPP_ETHER
},
192 { sll_if_print
, DLT_LINUX_SLL
},
194 #ifdef DLT_IEEE802_11
195 { ieee802_11_if_print
, DLT_IEEE802_11
},
198 { ltalk_if_print
, DLT_LTALK
},
201 { pflog_if_print
, DLT_PFLOG
},
204 { pfsync_if_print
, DLT_PFSYNC
},
207 { fr_if_print
, DLT_FR
},
210 { fr_if_print
, DLT_FRELAY
},
213 { sunatm_if_print
, DLT_SUNATM
},
215 #ifdef DLT_IP_OVER_FC
216 { ipfc_if_print
, DLT_IP_OVER_FC
},
218 #ifdef DLT_PRISM_HEADER
219 { prism_if_print
, DLT_PRISM_HEADER
},
221 #ifdef DLT_IEEE802_11_RADIO
222 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
225 { enc_if_print
, DLT_ENC
},
227 #ifdef DLT_SYMANTEC_FIREWALL
228 { symantec_if_print
, DLT_SYMANTEC_FIREWALL
},
230 #ifdef DLT_APPLE_IP_OVER_IEEE1394
231 { ap1394_if_print
, DLT_APPLE_IP_OVER_IEEE1394
},
233 #ifdef DLT_JUNIPER_ATM1
234 { juniper_atm1_print
, DLT_JUNIPER_ATM1
},
236 #ifdef DLT_JUNIPER_ATM2
237 { juniper_atm2_print
, DLT_JUNIPER_ATM2
},
239 #ifdef DLT_JUNIPER_MFR
240 { juniper_mfr_print
, DLT_JUNIPER_MFR
},
242 #ifdef DLT_JUNIPER_MLFR
243 { juniper_mlfr_print
, DLT_JUNIPER_MLFR
},
245 #ifdef DLT_JUNIPER_MLPPP
246 { juniper_mlppp_print
, DLT_JUNIPER_MLPPP
},
248 #ifdef DLT_JUNIPER_PPPOE
249 { juniper_pppoe_print
, DLT_JUNIPER_PPPOE
},
251 #ifdef DLT_JUNIPER_PPPOE_ATM
252 { juniper_pppoe_atm_print
, DLT_JUNIPER_PPPOE_ATM
},
254 #ifdef DLT_JUNIPER_GGSN
255 { juniper_ggsn_print
, DLT_JUNIPER_GGSN
},
257 #ifdef DLT_JUNIPER_ES
258 { juniper_es_print
, DLT_JUNIPER_ES
},
260 #ifdef DLT_JUNIPER_MONITOR
261 { juniper_monitor_print
, DLT_JUNIPER_MONITOR
},
263 #ifdef DLT_JUNIPER_SERVICES
264 { juniper_services_print
, DLT_JUNIPER_SERVICES
},
266 #ifdef DLT_JUNIPER_ETHER
267 { juniper_ether_print
, DLT_JUNIPER_ETHER
},
269 #ifdef DLT_JUNIPER_PPP
270 { juniper_ppp_print
, DLT_JUNIPER_PPP
},
272 #ifdef DLT_JUNIPER_FRELAY
273 { juniper_frelay_print
, DLT_JUNIPER_FRELAY
},
275 #ifdef DLT_JUNIPER_CHDLC
276 { juniper_chdlc_print
, DLT_JUNIPER_CHDLC
},
279 { mfr_if_print
, DLT_MFR
},
285 lookup_printer(int type
)
289 for (p
= printers
; p
->f
; ++p
)
314 show_dlts_and_exit(pcap_t
*pd
)
318 const char *dlt_name
;
320 n_dlts
= pcap_list_datalinks(pd
, &dlts
);
322 error("%s", pcap_geterr(pd
));
323 else if (n_dlts
== 0 || !dlts
)
324 error("No data link types.");
326 (void) fprintf(stderr
, "Data link types (use option -y to set):\n");
328 while (--n_dlts
>= 0) {
329 dlt_name
= pcap_datalink_val_to_name(dlts
[n_dlts
]);
330 if (dlt_name
!= NULL
) {
331 (void) fprintf(stderr
, " %s (%s)", dlt_name
,
332 pcap_datalink_val_to_description(dlts
[n_dlts
]));
335 * OK, does tcpdump handle that type?
337 if (lookup_printer(dlts
[n_dlts
]) == NULL
)
338 (void) fprintf(stderr
, " (not supported)");
341 (void) fprintf(stderr
, " DLT %d (not supported)\n",
350 * Set up flags that might or might not be supported depending on the
351 * version of libpcap we're using.
355 #define B_FLAG_USAGE " [ -B size ]"
361 #ifdef HAVE_PCAP_FINDALLDEVS
362 #ifndef HAVE_PCAP_IF_T
363 #undef HAVE_PCAP_FINDALLDEVS
367 #ifdef HAVE_PCAP_FINDALLDEVS
373 #ifdef HAVE_PCAP_DUMP_FLUSH
380 /* Drop root privileges and chroot if necessary */
382 droproot(const char *username
, const char *chroot_dir
)
384 struct passwd
*pw
= NULL
;
386 if (chroot_dir
&& !username
) {
387 fprintf(stderr
, "tcpdump: Chroot without dropping root is insecure\n");
391 pw
= getpwnam(username
);
394 if (chroot(chroot_dir
) != 0 || chdir ("/") != 0) {
395 fprintf(stderr
, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
396 chroot_dir
, pcap_strerror(errno
));
400 if (initgroups(pw
->pw_name
, pw
->pw_gid
) != 0 ||
401 setgid(pw
->pw_gid
) != 0 || setuid(pw
->pw_uid
) != 0) {
402 fprintf(stderr
, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
404 (unsigned long)pw
->pw_uid
,
405 (unsigned long)pw
->pw_gid
,
406 pcap_strerror(errno
));
411 fprintf(stderr
, "tcpdump: Couldn't find user '%.32s'\n",
434 MakeFilename(char *buffer
, char *orig_name
, int cnt
, int max_chars
)
436 if (cnt
== 0 && max_chars
== 0)
437 strcpy(buffer
, orig_name
);
439 sprintf(buffer
, "%s%0*d", orig_name
, max_chars
, cnt
);
442 static int tcpdump_printf(netdissect_options
*ndo _U_
,
443 const char *fmt
, ...)
450 ret
=vfprintf(stdout
, fmt
, args
);
457 main(int argc
, char **argv
)
459 register int cnt
, op
, i
;
460 bpf_u_int32 localnet
, netmask
;
461 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *WFileName
, *WFileNameAlt
;
462 pcap_handler callback
;
464 struct bpf_program fcode
;
466 RETSIGTYPE (*oldhandler
)(int);
468 struct print_info printinfo
;
469 struct dump_info dumpinfo
;
470 u_char
*pcap_userdata
;
471 char ebuf
[PCAP_ERRBUF_SIZE
];
472 char *username
= NULL
;
473 char *chroot_dir
= NULL
;
474 #ifdef HAVE_PCAP_FINDALLDEVS
475 pcap_if_t
*devpointer
;
480 u_int UserBufferSize
= 1000000;
481 if(wsockinit() != 0) return 1;
487 gndo
->ndo_default_print
=ndo_default_print
;
488 gndo
->ndo_printf
=tcpdump_printf
;
489 gndo
->ndo_error
=ndo_error
;
490 gndo
->ndo_warning
=ndo_warning
;
491 gndo
->ndo_snaplen
= DEFAULT_SNAPLEN
;
498 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
499 program_name
= cp
+ 1;
501 program_name
= argv
[0];
503 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
512 (op
= getopt(argc
, argv
, "aA" B_FLAG
"c:C:d" D_FLAG
"eE:fF:i:lLm:M:nNOpqr:Rs:StT:u" U_FLAG
"vw:W:xXy:YZ:")) != -1)
516 /* compatibility for old -a */
525 UserBufferSize
= atoi(optarg
)*1024;
526 if (UserBufferSize
< 0)
527 error("invalid packet buffer size %s", optarg
);
534 error("invalid packet count %s", optarg
);
538 Cflag
= atoi(optarg
) * 1000000;
540 error("invalid file size %s", optarg
);
547 #ifdef HAVE_PCAP_FINDALLDEVS
549 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
552 for (i
= 0; devpointer
!= 0; i
++) {
553 printf("%d.%s", i
+1, devpointer
->name
);
554 if (devpointer
->description
!= NULL
)
555 printf(" (%s)", devpointer
->description
);
557 devpointer
= devpointer
->next
;
561 #endif /* HAVE_PCAP_FINDALLDEVS */
572 #ifndef HAVE_LIBCRYPTO
573 warning("crypto code not compiled in");
575 gndo
->ndo_espsecret
= optarg
;
587 if (optarg
[0] == '0' && optarg
[1] == 0)
588 error("Invalid adapter index");
590 #ifdef HAVE_PCAP_FINDALLDEVS
592 * If the argument is a number, treat it as
593 * an index into the list of adapters, as
594 * printed by "tcpdump -D".
596 * This should be OK on UNIX systems, as interfaces
597 * shouldn't have names that begin with digits.
598 * It can be useful on Windows, where more than
599 * one interface can have the same name.
601 if ((devnum
= atoi(optarg
)) != 0) {
603 error("Invalid adapter index");
605 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
608 for (i
= 0; i
< devnum
-1; i
++){
609 devpointer
= devpointer
->next
;
610 if (devpointer
== NULL
)
611 error("Invalid adapter index");
614 device
= devpointer
->name
;
617 #endif /* HAVE_PCAP_FINDALLDEVS */
624 * _IOLBF is the same as _IOFBF in Microsoft's C
625 * libraries; the only alternative they offer
628 * XXX - this should really be checking for MSVC++,
629 * not WIN32, if, for example, MinGW has its own
630 * C library that is more UNIX-compatible.
632 setvbuf(stdout
, NULL
, _IONBF
, 0);
634 #ifdef HAVE_SETLINEBUF
637 setvbuf(stdout
, NULL
, _IOLBF
, 0);
652 if (smiLoadModule(optarg
) == 0) {
653 error("could not load MIB module %s", optarg
);
657 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
658 program_name
, optarg
);
659 (void)fprintf(stderr
, "(no libsmi support)\n");
664 /* TCP-MD5 shared secret */
665 #ifndef HAVE_LIBCRYPTO
666 warning("crypto code not compiled in");
668 tcpmd5secret
= optarg
;
681 ++suppress_default_print
;
695 snaplen
= strtol(optarg
, &end
, 0);
696 if (optarg
== end
|| *end
!= '\0'
697 || snaplen
< 0 || snaplen
> 65535)
698 error("invalid snaplen %s", optarg
);
699 else if (snaplen
== 0)
713 if (strcasecmp(optarg
, "vat") == 0)
715 else if (strcasecmp(optarg
, "wb") == 0)
717 else if (strcasecmp(optarg
, "rpc") == 0)
719 else if (strcasecmp(optarg
, "rtp") == 0)
721 else if (strcasecmp(optarg
, "rtcp") == 0)
722 packettype
= PT_RTCP
;
723 else if (strcasecmp(optarg
, "snmp") == 0)
724 packettype
= PT_SNMP
;
725 else if (strcasecmp(optarg
, "cnfp") == 0)
726 packettype
= PT_CNFP
;
727 else if (strcasecmp(optarg
, "tftp") == 0)
728 packettype
= PT_TFTP
;
729 else if (strcasecmp(optarg
, "aodv") == 0)
730 packettype
= PT_AODV
;
732 error("unknown packet type `%s'", optarg
);
739 #ifdef HAVE_PCAP_DUMP_FLUSH
754 Wflag
= atoi(optarg
);
756 error("invalid number of output files %s", optarg
);
757 WflagChars
= getWflagChars(Wflag
);
762 ++suppress_default_print
;
767 ++suppress_default_print
;
771 gndo
->ndo_dltname
= optarg
;
773 pcap_datalink_name_to_val(gndo
->ndo_dltname
);
774 if (gndo
->ndo_dlt
< 0)
775 error("invalid data link type %s", gndo
->ndo_dltname
);
778 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
781 /* Undocumented flag */
782 #ifdef HAVE_PCAP_DEBUG
783 extern int pcap_debug
;
794 username
= strdup(optarg
);
809 case 0: /* Default */
810 case 4: /* Default + Date*/
811 thiszone
= gmt2local(0);
814 case 1: /* No time stamp */
815 case 2: /* Unix timeval style */
816 case 3: /* Microseconds since previous packet */
819 default: /* Not supported */
820 error("only -t, -tt, -ttt, and -tttt are supported");
825 /* if run as root, prepare for chrooting */
826 if (getuid() == 0 || geteuid() == 0) {
827 /* future extensibility for cmd-line arguments */
829 chroot_dir
= WITH_CHROOT
;
834 /* if run as root, prepare for dropping root privileges */
835 if (getuid() == 0 || geteuid() == 0) {
836 /* Run with '-Z root' to restore old behaviour */
838 username
= WITH_USER
;
842 if (RFileName
!= NULL
) {
844 const char *dlt_name
;
848 * We don't need network access, so relinquish any set-UID
849 * or set-GID privileges we have (if any).
851 * We do *not* want set-UID privileges when opening a
852 * trace file, as that might let the user read other
853 * people's trace files (especially if we're set-UID
856 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
857 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
859 pd
= pcap_open_offline(RFileName
, ebuf
);
862 dlt
= pcap_datalink(pd
);
863 dlt_name
= pcap_datalink_val_to_name(dlt
);
864 if (dlt_name
== NULL
) {
865 fprintf(stderr
, "reading from file %s, link-type %u\n",
869 "reading from file %s, link-type %s (%s)\n",
871 pcap_datalink_val_to_description(dlt
));
876 error("-f and -r options are incompatible");
878 if (device
== NULL
) {
879 device
= pcap_lookupdev(ebuf
);
884 if(strlen(device
) == 1) //we assume that an ASCII string is always longer than 1 char
885 { //a Unicode string has a \0 as second byte (so strlen() is 1)
886 fprintf(stderr
, "%s: listening on %ws\n", program_name
, device
);
890 fprintf(stderr
, "%s: listening on %s\n", program_name
, device
);
896 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
902 * Let user own process after socket has been opened.
905 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
906 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
909 if(UserBufferSize
!= 1000000)
910 if(pcap_setbuff(pd
, UserBufferSize
)==-1){
911 error("%s", pcap_geterr(pd
));
915 show_dlts_and_exit(pd
);
916 if (gndo
->ndo_dlt
>= 0) {
917 #ifdef HAVE_PCAP_SET_DATALINK
918 if (pcap_set_datalink(pd
, gndo
->ndo_dlt
) < 0)
919 error("%s", pcap_geterr(pd
));
922 * We don't actually support changing the
923 * data link type, so we only let them
924 * set it to what it already is.
926 if (gndo
->ndo_dlt
!= pcap_datalink(pd
)) {
927 error("%s is not one of the DLTs supported by this device\n",
931 (void)fprintf(stderr
, "%s: data link type %s\n",
932 program_name
, gndo
->ndo_dltname
);
933 (void)fflush(stderr
);
935 i
= pcap_snapshot(pd
);
937 warning("snaplen raised from %d to %d", snaplen
, i
);
940 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
947 cmdbuf
= read_infile(infile
);
949 cmdbuf
= copy_argv(&argv
[optind
]);
951 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
952 error("%s", pcap_geterr(pd
));
954 bpf_dump(&fcode
, dflag
);
958 init_addrtoname(localnet
, netmask
);
961 (void)setsignal(SIGPIPE
, cleanup
);
963 (void)setsignal(SIGTERM
, cleanup
);
964 (void)setsignal(SIGINT
, cleanup
);
965 /* Cooperate with nohup(1) */
967 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
968 (void)setsignal(SIGHUP
, oldhandler
);
971 if (pcap_setfilter(pd
, &fcode
) < 0)
972 error("%s", pcap_geterr(pd
));
976 WFileNameAlt
= (char *)malloc(strlen(WFileName
) + MAX_CFLAG_CHARS
+ 1);
977 if (WFileNameAlt
== NULL
)
978 error("malloc of WFileNameAlt");
979 MakeFilename(WFileNameAlt
, WFileName
, 0, WflagChars
);
980 p
= pcap_dump_open(pd
, WFileNameAlt
);
982 error("%s", pcap_geterr(pd
));
984 callback
= dump_packet_and_trunc
;
985 dumpinfo
.WFileName
= WFileName
;
988 pcap_userdata
= (u_char
*)&dumpinfo
;
990 callback
= dump_packet
;
991 pcap_userdata
= (u_char
*)p
;
994 type
= pcap_datalink(pd
);
995 printinfo
.printer
= lookup_printer(type
);
996 if (printinfo
.printer
== NULL
) {
997 gndo
->ndo_dltname
= pcap_datalink_val_to_name(type
);
998 if (gndo
->ndo_dltname
!= NULL
)
999 error("unsupported data link type %s",
1002 error("unsupported data link type %d", type
);
1004 callback
= print_packet
;
1005 pcap_userdata
= (u_char
*)&printinfo
;
1009 * We cannot do this earlier, because we want to be able to open
1010 * the file (if done) for writing before giving up permissions.
1012 if (getuid() == 0 || geteuid() == 0) {
1013 if (username
|| chroot_dir
)
1014 droproot(username
, chroot_dir
);
1018 (void)setsignal(SIGINFO
, requestinfo
);
1021 if (vflag
> 0 && WFileName
) {
1023 * When capturing to a file, "-v" means tcpdump should,
1024 * every 10 secodns, "v"erbosely report the number of
1027 #ifdef USE_WIN32_MM_TIMER
1028 /* call verbose_stats_dump() each 1000 +/-100msec */
1029 timer_id
= timeSetEvent(1000, 100, verbose_stats_dump
, 0, TIME_PERIODIC
);
1030 setvbuf(stderr
, NULL
, _IONBF
, 0);
1031 #elif defined(HAVE_ALARM)
1032 (void)setsignal(SIGALRM
, verbose_stats_dump
);
1038 if (RFileName
== NULL
) {
1040 const char *dlt_name
;
1042 if (!vflag
&& !WFileName
) {
1043 (void)fprintf(stderr
,
1044 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1047 (void)fprintf(stderr
, "%s: ", program_name
);
1048 dlt
= pcap_datalink(pd
);
1049 dlt_name
= pcap_datalink_val_to_name(dlt
);
1050 if (dlt_name
== NULL
) {
1051 (void)fprintf(stderr
, "listening on %s, link-type %u, capture size %u bytes\n",
1052 device
, dlt
, snaplen
);
1054 (void)fprintf(stderr
, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1056 pcap_datalink_val_to_description(dlt
), snaplen
);
1058 (void)fflush(stderr
);
1061 status
= pcap_loop(pd
, cnt
, callback
, pcap_userdata
);
1062 if (WFileName
== NULL
) {
1064 * We're printing packets. Flush the printed output,
1065 * so it doesn't get intermingled with error output.
1069 * We got interrupted, so perhaps we didn't
1070 * manage to finish a line we were printing.
1071 * Print an extra newline, just in case.
1075 (void)fflush(stdout
);
1081 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
1082 program_name
, pcap_geterr(pd
));
1084 if (RFileName
== NULL
) {
1086 * We're doing a live capture. Report the capture
1092 exit(status
== -1 ? 1 : 0);
1095 /* make a clean exit on interrupts */
1097 cleanup(int signo _U_
)
1099 #ifdef USE_WIN32_MM_TIMER
1101 timeKillEvent(timer_id
);
1103 #elif defined(HAVE_ALARM)
1107 #ifdef HAVE_PCAP_BREAKLOOP
1109 * We have "pcap_breakloop()"; use it, so that we do as little
1110 * as possible in the signal handler (it's probably not safe
1111 * to do anything with standard I/O streams in a signal handler -
1112 * the ANSI C standard doesn't say it is).
1117 * We don't have "pcap_breakloop()"; this isn't safe, but
1118 * it's the best we can do. Print the summary if we're
1119 * not reading from a savefile - i.e., if we're doing a
1120 * live capture - and exit.
1122 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
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.
1129 (void)fflush(stdout
);
1137 info(register int verbose
)
1139 struct pcap_stat stat
;
1141 if (pcap_stats(pd
, &stat
) < 0) {
1142 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
1147 fprintf(stderr
, "%s: ", program_name
);
1149 (void)fprintf(stderr
, "%u packets captured", packets_captured
);
1151 fputs(", ", stderr
);
1154 (void)fprintf(stderr
, "%d packets received by filter", stat
.ps_recv
);
1156 fputs(", ", stderr
);
1159 (void)fprintf(stderr
, "%d packets dropped by kernel\n", stat
.ps_drop
);
1164 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1166 struct dump_info
*dump_info
;
1173 dump_info
= (struct dump_info
*)user
;
1176 * XXX - this won't prevent capture files from getting
1177 * larger than Cflag - the last packet written to the
1178 * file could put it over Cflag.
1180 if (pcap_dump_ftell(dump_info
->p
) > Cflag
) {
1182 * Close the current file and open a new one.
1184 pcap_dump_close(dump_info
->p
);
1187 if (Cflag_count
>= Wflag
)
1190 if (Cflag_count
>= MAX_CFLAG
)
1191 error("too many output files");
1193 name
= (char *)malloc(strlen(dump_info
->WFileName
) + MAX_CFLAG_CHARS
+ 1);
1195 error("dump_packet_and_trunc: malloc");
1196 MakeFilename(name
, dump_info
->WFileName
, Cflag_count
, WflagChars
);
1197 dump_info
->p
= pcap_dump_open(dump_info
->pd
, name
);
1199 if (dump_info
->p
== NULL
)
1200 error("%s", pcap_geterr(pd
));
1203 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
1204 #ifdef HAVE_PCAP_DUMP_FLUSH
1206 pcap_dump_flush(dump_info
->p
);
1215 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1221 pcap_dump(user
, h
, sp
);
1222 #ifdef HAVE_PCAP_DUMP_FLUSH
1224 pcap_dump_flush((pcap_dumper_t
*)user
);
1233 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1235 struct print_info
*print_info
;
1243 print_info
= (struct print_info
*)user
;
1246 * Some printers want to check that they're not walking off the
1247 * end of the packet.
1248 * Rather than pass it all the way down, we set this global.
1250 snapend
= sp
+ h
->caplen
;
1252 hdrlen
= (*print_info
->printer
)(h
, sp
);
1255 * Print the raw packet data in hex and ASCII.
1259 * Include the link-layer header.
1261 hex_and_ascii_print("\n\t", sp
, h
->caplen
);
1264 * Don't include the link-layer header - and if
1265 * we have nothing past the link-layer header,
1268 if (h
->caplen
> hdrlen
)
1269 hex_and_ascii_print("\n\t", sp
+ hdrlen
,
1270 h
->caplen
- hdrlen
);
1274 * Print the raw packet data in hex.
1278 * Include the link-layer header.
1280 hex_print("\n\t", sp
, h
->caplen
);
1283 * Don't include the link-layer header - and if
1284 * we have nothing past the link-layer header,
1287 if (h
->caplen
> hdrlen
)
1288 hex_print("\n\t", sp
+ hdrlen
,
1289 h
->caplen
- hdrlen
);
1293 * Print the raw packet data in ASCII.
1297 * Include the link-layer header.
1299 ascii_print(sp
, h
->caplen
);
1302 * Don't include the link-layer header - and if
1303 * we have nothing past the link-layer header,
1306 if (h
->caplen
> hdrlen
)
1307 ascii_print(sp
+ hdrlen
, h
->caplen
- hdrlen
);
1320 * XXX - there should really be libpcap calls to get the version
1321 * number as a string (the string would be generated from #defines
1322 * at run time, so that it's not generated from string constants
1323 * in the library, as, on many UNIX systems, those constants would
1324 * be statically linked into the application executable image, and
1325 * would thus reflect the version of libpcap on the system on
1326 * which the application was *linked*, not the system on which it's
1329 * That routine should be documented, unlike the "version[]"
1330 * string, so that UNIX vendors providing their own libpcaps
1331 * don't omit it (as a couple of vendors have...).
1333 * Packet.dll should perhaps also export a routine to return the
1334 * version number of the Packet.dll code, to supply the
1335 * "Wpcap_version" information on Windows.
1337 char WDversion
[]="current-cvs.tcpdump.org";
1338 #if !defined(HAVE_GENERATED_VERSION)
1339 char version
[]="current-cvs.tcpdump.org";
1341 char pcap_version
[]="current-cvs.tcpdump.org";
1342 char Wpcap_version
[]="3.1";
1346 * By default, print the specified data out in hex and ASCII.
1349 ndo_default_print(netdissect_options
*ndo _U_
, const u_char
*bp
, u_int length
)
1351 hex_and_ascii_print("\n\t", bp
, length
); /* pass on lf and identation string */
1355 default_print(const u_char
*bp
, u_int length
)
1357 ndo_default_print(gndo
, bp
, length
);
1361 RETSIGTYPE
requestinfo(int signo _U_
)
1371 * Called once each second in verbose mode while dumping to file
1373 #ifdef USE_WIN32_MM_TIMER
1374 void CALLBACK
verbose_stats_dump (UINT timer_id _U_
, UINT msg _U_
, DWORD_PTR arg _U_
,
1375 DWORD_PTR dw1 _U_
, DWORD_PTR dw2 _U_
)
1377 struct pcap_stat stat
;
1379 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
1380 fprintf(stderr
, "Got %u\r", packets_captured
);
1382 #elif defined(HAVE_ALARM)
1383 static void verbose_stats_dump(int sig _U_
)
1385 struct pcap_stat stat
;
1387 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
1388 fprintf(stderr
, "Got %u\r", packets_captured
);
1396 extern char version
[];
1397 #ifndef HAVE_PCAP_LIB_VERSION
1398 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
1399 extern char pcap_version
[];
1400 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1401 static char pcap_version
[] = "unknown";
1402 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1403 #endif /* HAVE_PCAP_LIB_VERSION */
1405 #ifdef HAVE_PCAP_LIB_VERSION
1407 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
1409 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1411 (void)fprintf(stderr
, "%s\n",pcap_lib_version());
1412 #else /* HAVE_PCAP_LIB_VERSION */
1414 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
1415 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
1417 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1418 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
1420 #endif /* HAVE_PCAP_LIB_VERSION */
1421 (void)fprintf(stderr
,
1422 "Usage: %s [-aAd" D_FLAG
"eflLnNOpqRStu" U_FLAG
"vxX]" B_FLAG_USAGE
" [-c count] [ -C file_size ]\n", program_name
);
1423 (void)fprintf(stderr
,
1424 "\t\t[ -E algo:secret ] [ -F file ] [ -i interface ] [ -M secret ]\n");
1425 (void)fprintf(stderr
,
1426 "\t\t[ -r file ] [ -s snaplen ] [ -T type ] [ -w file ]\n");
1427 (void)fprintf(stderr
,
1428 "\t\t[ -W filecount ] [ -y datalinktype ] [ -Z user ]\n");
1429 (void)fprintf(stderr
,
1430 "\t\t[ expression ]\n");
1438 ndo_error(netdissect_options
*ndo _U_
, const char *fmt
, ...)
1442 (void)fprintf(stderr
, "%s: ", program_name
);
1444 (void)vfprintf(stderr
, fmt
, ap
);
1448 if (fmt
[-1] != '\n')
1449 (void)fputc('\n', stderr
);
1457 ndo_warning(netdissect_options
*ndo _U_
, const char *fmt
, ...)
1461 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
1463 (void)vfprintf(stderr
, fmt
, ap
);
1467 if (fmt
[-1] != '\n')
1468 (void)fputc('\n', stderr
);