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.233 2004-02-26 08:47:27 hannes Exp $ (LBL)";
37 * tcpdump - monitor tcp/ip traffic on an ethernet.
39 * First written in 1987 by Van Jacobson, Lawrence Berkeley Laboratory.
40 * Mercilessly hacked and occasionally improved since then via the
41 * combined efforts of Van, Steve McCanne and Craig Leres of LBL.
48 #include <tcpdump-stdinc.h>
53 extern int strcasecmp (const char *__s1
, const char *__s2
);
71 #include "interface.h"
72 #include "addrtoname.h"
74 #include "setsignal.h"
75 #include "gmt2local.h"
76 #include "pcap-missing.h"
78 int dflag
; /* print filter code */
79 int eflag
; /* print ethernet header */
80 int fflag
; /* don't translate "foreign" IP address */
81 static int Lflag
; /* list available data link types and exit */
82 int nflag
; /* leave addresses as numbers */
83 int Nflag
; /* remove domains from printed host names */
84 static int Oflag
= 1; /* run filter code optimizer */
85 static int pflag
; /* don't go promiscuous */
86 int qflag
; /* quick (shorter) output */
87 int Rflag
= 1; /* print sequence # field in AH/ESP*/
88 int sflag
= 0; /* use the libsmi to translate OIDs */
89 int Sflag
; /* print raw TCP sequence numbers */
90 int tflag
= 1; /* print packet arrival time */
91 int Uflag
= 0; /* "unbuffered" output of dump files */
92 int uflag
= 0; /* Print undecoded NFS handles */
93 int vflag
; /* verbose */
94 int xflag
; /* print packet in hex */
95 int Xflag
; /* print packet in ascii as well as hex */
96 static off_t Cflag
= 0; /* rotate dump files after this many bytes */
97 int Aflag
= 0; /* print packet only in ascii observing LF, CR, TAB, SPACE */
98 static int dlt
= -1; /* if != -1, ask libpcap for the DLT it names */
99 static int Cflag_count
= 0; /* Keep track of which file number we're writing */
100 static int Wflag
= 0; /* recycle output files after this number of files */
101 static int WflagChars
= 0;
104 * Define the maximum number of files for the -C flag, and how many
105 * characters can be added to a filename for the -C flag (which
106 * should be enough to handle MAX_CFLAG - 1).
108 #define MAX_CFLAG 1000000
109 #define MAX_CFLAG_CHARS 6
111 const char *dlt_name
= NULL
;
113 char *espsecret
= NULL
; /* ESP secret key */
117 static int infodelay
;
118 static int infoprint
;
122 int32_t thiszone
; /* seconds offset from gmt to local time */
125 static RETSIGTYPE
cleanup(int);
126 static void usage(void) __attribute__((noreturn
));
127 static void show_dlts_and_exit(pcap_t
*pd
) __attribute__((noreturn
));
129 static void print_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
130 static void dump_packet_and_trunc(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
131 static void dump_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
132 static void droproot(const char *, const char *);
135 RETSIGTYPE
requestinfo(int);
138 #if defined(USE_WIN32_MM_TIMER)
139 #include <MMsystem.h>
140 static UINT timer_id
;
141 static void CALLBACK
verbose_stats_dump(UINT
, UINT
, DWORD_PTR
, DWORD_PTR
, DWORD_PTR
);
142 #elif defined(HAVE_ALARM)
143 static void verbose_stats_dump(int sig
);
146 static void info(int);
147 static u_int packets_captured
;
149 /* Length of saved portion of packet. */
150 int snaplen
= DEFAULT_SNAPLEN
;
152 typedef u_int (*if_printer
)(const struct pcap_pkthdr
*, const u_char
*);
159 static struct printer printers
[] = {
160 { arcnet_if_print
, DLT_ARCNET
},
161 #ifdef DLT_ARCNET_LINUX
162 { arcnet_linux_if_print
, DLT_ARCNET_LINUX
},
164 { ether_if_print
, DLT_EN10MB
},
165 { token_if_print
, DLT_IEEE802
},
167 { lane_if_print
, DLT_LANE8023
},
170 { cip_if_print
, DLT_CIP
},
173 { cip_if_print
, DLT_ATM_CLIP
},
175 { sl_if_print
, DLT_SLIP
},
176 #ifdef DLT_SLIP_BSDOS
177 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
179 { ppp_if_print
, DLT_PPP
},
181 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
183 { fddi_if_print
, DLT_FDDI
},
184 { null_if_print
, DLT_NULL
},
186 { null_if_print
, DLT_LOOP
},
188 { raw_if_print
, DLT_RAW
},
189 { atm_if_print
, DLT_ATM_RFC1483
},
191 { chdlc_if_print
, DLT_C_HDLC
},
194 { chdlc_if_print
, DLT_HDLC
},
196 #ifdef DLT_PPP_SERIAL
197 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
200 { pppoe_if_print
, DLT_PPP_ETHER
},
203 { sll_if_print
, DLT_LINUX_SLL
},
205 #ifdef DLT_IEEE802_11
206 { ieee802_11_if_print
, DLT_IEEE802_11
},
209 { ltalk_if_print
, DLT_LTALK
},
212 { pflog_if_print
, DLT_PFLOG
},
215 { fr_if_print
, DLT_FR
},
218 { fr_if_print
, DLT_FRELAY
},
221 { sunatm_if_print
, DLT_SUNATM
},
223 #ifdef DLT_IP_OVER_FC
224 { ipfc_if_print
, DLT_IP_OVER_FC
},
226 #ifdef DLT_PRISM_HEADER
227 { prism_if_print
, DLT_PRISM_HEADER
},
229 #ifdef DLT_IEEE802_11_RADIO
230 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
233 { enc_if_print
, DLT_ENC
},
239 lookup_printer(int type
)
243 for (p
= printers
; p
->f
; ++p
)
268 show_dlts_and_exit(pcap_t
*pd
)
272 const char *dlt_name
;
274 n_dlts
= pcap_list_datalinks(pd
, &dlts
);
276 error("%s", pcap_geterr(pd
));
277 else if (n_dlts
== 0 || !dlts
)
278 error("No data link types.");
280 (void) fprintf(stderr
, "Data link types (use option -y to set):\n");
282 while (--n_dlts
>= 0) {
283 dlt_name
= pcap_datalink_val_to_name(dlts
[n_dlts
]);
284 if (dlt_name
!= NULL
) {
285 (void) fprintf(stderr
, " %s (%s)", dlt_name
,
286 pcap_datalink_val_to_description(dlts
[n_dlts
]));
289 * OK, does tcpdump handle that type?
291 if (lookup_printer(dlts
[n_dlts
]) == NULL
)
292 (void) fprintf(stderr
, " (not supported)");
295 (void) fprintf(stderr
, " DLT %d (not supported)\n",
304 * Set up flags that might or might not be supported depending on the
305 * version of libpcap we're using.
309 #define B_FLAG_USAGE " [ -B size ]"
315 #ifdef HAVE_PCAP_FINDALLDEVS
321 #ifdef HAVE_PCAP_DUMP_FLUSH
327 /* Drop root privileges and chroot if necessary */
329 droproot(const char *username
, const char *chroot_dir
)
331 struct passwd
*pw
= NULL
;
333 if (chroot_dir
&& !username
) {
334 fprintf(stderr
, "Chroot without dropping root is insecure\n");
338 pw
= getpwnam(username
);
341 if (chroot(chroot_dir
) != 0 || chdir ("/") != 0) {
342 fprintf(stderr
, "Couldn't chroot/chdir to '%.64s'\n", chroot_dir
);
346 if (initgroups(pw
->pw_name
, pw
->pw_gid
) != 0 || setgid(pw
->pw_gid
) != 0 ||
347 setuid(pw
->pw_uid
) != 0) {
348 fprintf(stderr
, "Couldn't change to '%.32s' uid=%d gid=%d\n", username
,
349 pw
->pw_uid
, pw
->pw_gid
);
354 fprintf(stderr
, "Couldn't find user '%.32s'\n", username
);
375 MakeFilename(char *buffer
, char *orig_name
, int cnt
, int max_chars
)
377 if (cnt
== 0 && max_chars
== 0)
378 strcpy(buffer
, orig_name
);
380 sprintf(buffer
, "%s%0*d", orig_name
, max_chars
, cnt
);
384 main(int argc
, char **argv
)
386 register int cnt
, op
, i
;
387 bpf_u_int32 localnet
, netmask
;
388 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *WFileName
, *WFileNameAlt
;
389 pcap_handler callback
;
391 struct bpf_program fcode
;
393 RETSIGTYPE (*oldhandler
)(int);
395 struct print_info printinfo
;
396 struct dump_info dumpinfo
;
397 u_char
*pcap_userdata
;
398 char ebuf
[PCAP_ERRBUF_SIZE
];
399 char *username
= NULL
;
400 char *chroot_dir
= NULL
;
401 #ifdef HAVE_PCAP_FINDALLDEVS
402 pcap_if_t
*devpointer
;
407 u_int UserBufferSize
= 1000000;
408 if(wsockinit() != 0) return 1;
416 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
417 program_name
= cp
+ 1;
419 program_name
= argv
[0];
421 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
430 (op
= getopt(argc
, argv
, "aA" B_FLAG
"c:C:d" D_FLAG
"eE:fF:i:lLm:nNOpqr:Rs:StT:u" U_FLAG
"vw:W:xXy:YZ:")) != -1)
434 /* compatibility for old -a */
445 UserBufferSize
= atoi(optarg
)*1024;
446 if (UserBufferSize
< 0)
447 error("invalid packet buffer size %s", optarg
);
454 error("invalid packet count %s", optarg
);
458 Cflag
= atoi(optarg
) * 1000000;
460 error("invalid file size %s", optarg
);
467 #ifdef HAVE_PCAP_FINDALLDEVS
469 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
472 for (i
= 0; devpointer
!= 0; i
++) {
473 printf("%d.%s", i
+1, devpointer
->name
);
474 if (devpointer
->description
!= NULL
)
475 printf(" (%s)", devpointer
->description
);
477 devpointer
= devpointer
->next
;
481 #endif /* HAVE_PCAP_FINDALLDEVS */
492 #ifndef HAVE_LIBCRYPTO
493 warning("crypto code not compiled in");
507 if (optarg
[0] == '0' && optarg
[1] == 0)
508 error("Invalid adapter index");
510 #ifdef HAVE_PCAP_FINDALLDEVS
512 * If the argument is a number, treat it as
513 * an index into the list of adapters, as
514 * printed by "tcpdump -D".
516 * This should be OK on UNIX systems, as interfaces
517 * shouldn't have names that begin with digits.
518 * It can be useful on Windows, where more than
519 * one interface can have the same name.
521 if ((devnum
= atoi(optarg
)) != 0) {
523 error("Invalid adapter index");
525 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
528 for (i
= 0; i
< devnum
-1; i
++){
529 devpointer
= devpointer
->next
;
530 if (devpointer
== NULL
)
531 error("Invalid adapter index");
534 device
= devpointer
->name
;
537 #endif /* HAVE_PCAP_FINDALLDEVS */
544 * _IOLBF is the same as _IOFBF in Microsoft's C
545 * libraries; the only alternative they offer
548 * XXX - this should really be checking for MSVC++,
549 * not WIN32, if, for example, MinGW has its own
550 * C library that is more UNIX-compatible.
552 setvbuf(stdout
, NULL
, _IONBF
, 0);
554 #ifdef HAVE_SETLINEBUF
557 setvbuf(stdout
, NULL
, _IOLBF
, 0);
572 if (smiLoadModule(optarg
) == 0) {
573 error("could not load MIB module %s", optarg
);
577 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
578 program_name
, optarg
);
579 (void)fprintf(stderr
, "(no libsmi support)\n");
606 snaplen
= strtol(optarg
, &end
, 0);
607 if (optarg
== end
|| *end
!= '\0'
608 || snaplen
< 0 || snaplen
> 65535)
609 error("invalid snaplen %s", optarg
);
610 else if (snaplen
== 0)
624 if (strcasecmp(optarg
, "vat") == 0)
626 else if (strcasecmp(optarg
, "wb") == 0)
628 else if (strcasecmp(optarg
, "rpc") == 0)
630 else if (strcasecmp(optarg
, "rtp") == 0)
632 else if (strcasecmp(optarg
, "rtcp") == 0)
633 packettype
= PT_RTCP
;
634 else if (strcasecmp(optarg
, "snmp") == 0)
635 packettype
= PT_SNMP
;
636 else if (strcasecmp(optarg
, "cnfp") == 0)
637 packettype
= PT_CNFP
;
638 else if (strcasecmp(optarg
, "tftp") == 0)
639 packettype
= PT_TFTP
;
640 else if (strcasecmp(optarg
, "aodv") == 0)
641 packettype
= PT_AODV
;
643 error("unknown packet type `%s'", optarg
);
650 #ifdef HAVE_PCAP_DUMP_FLUSH
665 Wflag
= atoi(optarg
);
667 error("invalid number of output files %s", optarg
);
668 WflagChars
= getWflagChars(Wflag
);
682 dlt
= pcap_datalink_name_to_val(dlt_name
);
684 error("invalid data link type %s", dlt_name
);
687 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
690 /* Undocumented flag */
691 #ifdef HAVE_PCAP_DEBUG
692 extern int pcap_debug
;
703 username
= strdup(optarg
);
717 thiszone
= gmt2local(0);
720 /* if run as root, prepare for chrooting */
721 if (getuid() == 0 || geteuid() == 0) {
722 /* future extensibility for cmd-line arguments */
724 chroot_dir
= WITH_CHROOT
;
729 /* if run as root, prepare for dropping root privileges */
730 if (getuid() == 0 || geteuid() == 0) {
731 /* Run with '-Z root' to restore old behaviour */
733 username
= WITH_USER
;
737 if (RFileName
!= NULL
) {
739 const char *dlt_name
;
743 * We don't need network access, so relinquish any set-UID
744 * or set-GID privileges we have (if any).
746 * We do *not* want set-UID privileges when opening a
747 * trace file, as that might let the user read other
748 * people's trace files (especially if we're set-UID
751 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
752 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
754 pd
= pcap_open_offline(RFileName
, ebuf
);
757 dlt
= pcap_datalink(pd
);
758 dlt_name
= pcap_datalink_val_to_name(dlt
);
759 if (dlt_name
== NULL
) {
760 fprintf(stderr
, "reading from file %s, link-type %u\n",
764 "reading from file %s, link-type %s (%s)\n",
766 pcap_datalink_val_to_description(dlt
));
771 error("-f and -r options are incompatible");
773 if (device
== NULL
) {
774 device
= pcap_lookupdev(ebuf
);
779 if(IsTextUnicode(device
,
780 wcslen((short*)device
), // Device always ends with a double \0, so this way to determine its
781 // length should be always valid
784 fprintf(stderr
, "%s: listening on %ws\n", program_name
, device
);
788 fprintf(stderr
, "%s: listening on %s\n", program_name
, device
);
794 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
800 * Let user own process after socket has been opened.
803 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
804 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
807 if(UserBufferSize
!= 1000000)
808 if(pcap_setbuff(pd
, UserBufferSize
)==-1){
809 error("%s", pcap_geterr(pd
));
813 show_dlts_and_exit(pd
);
815 #ifdef HAVE_PCAP_SET_DATALINK
816 if (pcap_set_datalink(pd
, dlt
) < 0)
817 error("%s", pcap_geterr(pd
));
820 * We don't actually support changing the
821 * data link type, so we only let them
822 * set it to what it already is.
824 if (dlt
!= pcap_datalink(pd
)) {
825 error("%s is not one of the DLTs supported by this device\n",
829 (void)fprintf(stderr
, "%s: data link type %s\n",
830 program_name
, dlt_name
);
831 (void)fflush(stderr
);
833 i
= pcap_snapshot(pd
);
835 warning("snaplen raised from %d to %d", snaplen
, i
);
838 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
845 cmdbuf
= read_infile(infile
);
847 cmdbuf
= copy_argv(&argv
[optind
]);
849 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
850 error("%s", pcap_geterr(pd
));
852 bpf_dump(&fcode
, dflag
);
856 init_addrtoname(localnet
, netmask
);
859 (void)setsignal(SIGPIPE
, cleanup
);
861 (void)setsignal(SIGTERM
, cleanup
);
862 (void)setsignal(SIGINT
, cleanup
);
863 /* Cooperate with nohup(1) */
865 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
866 (void)setsignal(SIGHUP
, oldhandler
);
869 if (pcap_setfilter(pd
, &fcode
) < 0)
870 error("%s", pcap_geterr(pd
));
874 WFileNameAlt
= (char *)malloc(strlen(WFileName
) + MAX_CFLAG_CHARS
+ 1);
875 if (WFileNameAlt
== NULL
)
876 error("malloc of WFileNameAlt");
877 MakeFilename(WFileNameAlt
, WFileName
, 0, WflagChars
);
878 p
= pcap_dump_open(pd
, WFileNameAlt
);
880 error("%s", pcap_geterr(pd
));
882 callback
= dump_packet_and_trunc
;
883 dumpinfo
.WFileName
= WFileName
;
886 pcap_userdata
= (u_char
*)&dumpinfo
;
888 callback
= dump_packet
;
889 pcap_userdata
= (u_char
*)p
;
892 type
= pcap_datalink(pd
);
893 printinfo
.printer
= lookup_printer(type
);
894 if (printinfo
.printer
== NULL
) {
895 dlt_name
= pcap_datalink_val_to_name(type
);
896 if (dlt_name
!= NULL
)
897 error("unsupported data link type %s", dlt_name
);
899 error("unsupported data link type %d", type
);
901 callback
= print_packet
;
902 pcap_userdata
= (u_char
*)&printinfo
;
906 * We cannot do this earlier, because we want to be able to open
907 * the file (if done) for writing before giving up permissions.
909 if (getuid() == 0 || geteuid() == 0) {
910 if (username
|| chroot_dir
)
911 droproot(username
, chroot_dir
);
915 (void)setsignal(SIGINFO
, requestinfo
);
918 if (vflag
> 0 && WFileName
) {
920 * When capturing to a file, "-v" means tcpdump should,
921 * every 10 secodns, "v"erbosely report the number of
924 #ifdef USE_WIN32_MM_TIMER
925 /* call verbose_stats_dump() each 1000 +/-100msec */
926 timer_id
= timeSetEvent(1000, 100, verbose_stats_dump
, 0, TIME_PERIODIC
);
927 setvbuf(stderr
, NULL
, _IONBF
, 0);
928 #elif defined(HAVE_ALARM)
929 (void)setsignal(SIGALRM
, verbose_stats_dump
);
935 if (RFileName
== NULL
) {
937 const char *dlt_name
;
939 if (!vflag
&& !WFileName
) {
940 (void)fprintf(stderr
,
941 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
944 (void)fprintf(stderr
, "%s: ", program_name
);
945 dlt
= pcap_datalink(pd
);
946 dlt_name
= pcap_datalink_val_to_name(dlt
);
947 if (dlt_name
== NULL
) {
948 (void)fprintf(stderr
, "listening on %s, link-type %u, capture size %u bytes\n",
949 device
, dlt
, snaplen
);
951 (void)fprintf(stderr
, "listening on %s, link-type %s (%s), capture size %u bytes\n",
953 pcap_datalink_val_to_description(dlt
), snaplen
);
955 (void)fflush(stderr
);
958 status
= pcap_loop(pd
, cnt
, callback
, pcap_userdata
);
959 if (WFileName
== NULL
) {
961 * We're printing packets. Flush the printed output,
962 * so it doesn't get intermingled with error output.
966 * We got interrupted, so perhaps we didn't
967 * manage to finish a line we were printing.
968 * Print an extra newline, just in case.
972 (void)fflush(stdout
);
978 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
979 program_name
, pcap_geterr(pd
));
981 if (RFileName
== NULL
) {
983 * We're doing a live capture. Report the capture
989 exit(status
== -1 ? 1 : 0);
992 /* make a clean exit on interrupts */
994 cleanup(int signo _U_
)
996 #ifdef USE_WIN32_MM_TIMER
998 timeKillEvent(timer_id
);
1000 #elif defined(HAVE_ALARM)
1004 #ifdef HAVE_PCAP_BREAKLOOP
1006 * We have "pcap_breakloop()"; use it, so that we do as little
1007 * as possible in the signal handler (it's probably not safe
1008 * to do anything with standard I/O streams in a signal handler -
1009 * the ANSI C standard doesn't say it is).
1014 * We don't have "pcap_breakloop()"; this isn't safe, but
1015 * it's the best we can do. Print the summary if we're
1016 * not reading from a savefile - i.e., if we're doing a
1017 * live capture - and exit.
1019 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
1021 * We got interrupted, so perhaps we didn't
1022 * manage to finish a line we were printing.
1023 * Print an extra newline, just in case.
1026 (void)fflush(stdout
);
1034 info(register int verbose
)
1036 struct pcap_stat stat
;
1038 if (pcap_stats(pd
, &stat
) < 0) {
1039 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
1044 fprintf(stderr
, "%s: ", program_name
);
1046 (void)fprintf(stderr
, "%u packets captured", packets_captured
);
1048 fputs(", ", stderr
);
1051 (void)fprintf(stderr
, "%d packets received by filter", stat
.ps_recv
);
1053 fputs(", ", stderr
);
1056 (void)fprintf(stderr
, "%d packets dropped by kernel\n", stat
.ps_drop
);
1061 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1063 struct dump_info
*dump_info
;
1070 dump_info
= (struct dump_info
*)user
;
1073 * XXX - this won't prevent capture files from getting
1074 * larger than Cflag - the last packet written to the
1075 * file could put it over Cflag.
1077 if (ftell((FILE *)dump_info
->p
) > Cflag
) {
1079 * Close the current file and open a new one.
1081 pcap_dump_close(dump_info
->p
);
1084 if (Cflag_count
>= Wflag
)
1087 if (Cflag_count
>= MAX_CFLAG
)
1088 error("too many output files");
1090 name
= (char *)malloc(strlen(dump_info
->WFileName
) + MAX_CFLAG_CHARS
+ 1);
1092 error("dump_packet_and_trunc: malloc");
1093 MakeFilename(name
, dump_info
->WFileName
, Cflag_count
, WflagChars
);
1094 dump_info
->p
= pcap_dump_open(dump_info
->pd
, name
);
1096 if (dump_info
->p
== NULL
)
1097 error("%s", pcap_geterr(pd
));
1100 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
1101 #ifdef HAVE_PCAP_DUMP_FLUSH
1103 pcap_dump_flush(dump_info
->p
);
1112 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1118 pcap_dump(user
, h
, sp
);
1119 #ifdef HAVE_PCAP_DUMP_FLUSH
1121 pcap_dump_flush((pcap_dumper_t
*)user
);
1130 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1132 struct print_info
*print_info
;
1140 print_info
= (struct print_info
*)user
;
1143 * Some printers want to check that they're not walking off the
1144 * end of the packet.
1145 * Rather than pass it all the way down, we set this global.
1147 snapend
= sp
+ h
->caplen
;
1149 hdrlen
= (*print_info
->printer
)(h
, sp
);
1152 * Print the raw packet data.
1156 * Include the link-layer header.
1158 default_print(sp
, h
->caplen
);
1161 * Don't include the link-layer header - and if
1162 * we have nothing past the link-layer header,
1165 if (h
->caplen
> hdrlen
)
1166 default_print(sp
+ hdrlen
,
1167 h
->caplen
- hdrlen
);
1180 * XXX - there should really be libpcap calls to get the version
1181 * number as a string (the string would be generated from #defines
1182 * at run time, so that it's not generated from string constants
1183 * in the library, as, on many UNIX systems, those constants would
1184 * be statically linked into the application executable image, and
1185 * would thus reflect the version of libpcap on the system on
1186 * which the application was *linked*, not the system on which it's
1189 * That routine should be documented, unlike the "version[]"
1190 * string, so that UNIX vendors providing their own libpcaps
1191 * don't omit it (as a couple of vendors have...).
1193 * Packet.dll should perhaps also export a routine to return the
1194 * version number of the Packet.dll code, to supply the
1195 * "Wpcap_version" information on Windows.
1197 char WDversion
[]="current-cvs.tcpdump.org";
1198 #if !defined(HAVE_GENERATED_VERSION)
1199 char version
[]="current-cvs.tcpdump.org";
1201 char pcap_version
[]="current-cvs.tcpdump.org";
1202 char Wpcap_version
[]="3.0 alpha";
1206 * By default, print the specified data out in hex.
1209 default_print(register const u_char
*bp
, register u_int length
)
1211 ascii_print("\n\t", bp
, length
); /* pass on lf and identation string */
1215 RETSIGTYPE
requestinfo(int signo _U_
)
1225 * Called once each second in verbose mode while dumping to file
1227 #ifdef USE_WIN32_MM_TIMER
1228 void CALLBACK
verbose_stats_dump (UINT timer_id _U_
, UINT msg _U_
, DWORD_PTR arg _U_
,
1229 DWORD_PTR dw1 _U_
, DWORD_PTR dw2 _U_
)
1231 struct pcap_stat stat
;
1233 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
1234 fprintf(stderr
, "Got %u\r", packets_captured
);
1236 #elif defined(HAVE_ALARM)
1237 static void verbose_stats_dump(int sig _U_
)
1239 struct pcap_stat stat
;
1241 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
1242 fprintf(stderr
, "Got %u\r", packets_captured
);
1250 extern char version
[];
1251 #ifndef HAVE_PCAP_LIB_VERSION
1252 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
1253 extern char pcap_version
[];
1254 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1255 static char pcap_version
[] = "unknown";
1256 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1257 #endif /* HAVE_PCAP_LIB_VERSION */
1259 #ifdef HAVE_PCAP_LIB_VERSION
1260 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1261 (void)fprintf(stderr
, "%s\n", pcap_lib_version());
1262 #else /* HAVE_PCAP_LIB_VERSION */
1264 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
1265 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
1267 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1268 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
1270 #endif /* HAVE_PCAP_LIB_VERSION */
1271 (void)fprintf(stderr
,
1272 "Usage: %s [-aAd" D_FLAG
"eflLnNOpqRStu" U_FLAG
"vxX]" B_FLAG_USAGE
" [-c count] [ -C file_size ]\n", program_name
);
1273 (void)fprintf(stderr
,
1274 "\t\t[ -E algo:secret ] [ -F file ] [ -i interface ] [ -r file ]\n");
1275 (void)fprintf(stderr
,
1276 "\t\t[ -s snaplen ] [ -T type ] [ -w file ] [ -W filecount ]\n");
1277 (void)fprintf(stderr
,
1278 "\t\t[ -y datalinktype ] [ -Z user ]\n");
1279 (void)fprintf(stderr
,
1280 "\t\t[ expression ]\n");