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.249 2004-10-19 15:59:40 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
);
74 #include "netdissect.h"
75 #include "interface.h"
76 #include "addrtoname.h"
78 #include "setsignal.h"
79 #include "gmt2local.h"
80 #include "pcap-missing.h"
82 netdissect_options Gndo
;
83 netdissect_options
*gndo
= &Gndo
;
86 * Define the maximum number of files for the -C flag, and how many
87 * characters can be added to a filename for the -C flag (which
88 * should be enough to handle MAX_CFLAG - 1).
90 #define MAX_CFLAG 1000000
91 #define MAX_CFLAG_CHARS 6
93 int Lflag
; /* list available data link types and exit */
100 int32_t thiszone
; /* seconds offset from gmt to local time */
103 static RETSIGTYPE
cleanup(int);
104 static void usage(void) __attribute__((noreturn
));
105 static void show_dlts_and_exit(pcap_t
*pd
) __attribute__((noreturn
));
107 static void print_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
108 static void dump_packet_and_trunc(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
109 static void dump_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
110 static void droproot(const char *, const char *);
111 static void ndo_error(netdissect_options
*ndo
, const char *fmt
, ...);
112 static void ndo_warning(netdissect_options
*ndo
, const char *fmt
, ...);
115 RETSIGTYPE
requestinfo(int);
118 #if defined(USE_WIN32_MM_TIMER)
119 #include <MMsystem.h>
120 static UINT timer_id
;
121 static void CALLBACK
verbose_stats_dump(UINT
, UINT
, DWORD_PTR
, DWORD_PTR
, DWORD_PTR
);
122 #elif defined(HAVE_ALARM)
123 static void verbose_stats_dump(int sig
);
126 static void info(int);
127 static u_int packets_captured
;
129 typedef u_int (*if_printer
)(const struct pcap_pkthdr
*, const u_char
*);
136 static struct printer printers
[] = {
137 { arcnet_if_print
, DLT_ARCNET
},
138 #ifdef DLT_ARCNET_LINUX
139 { arcnet_linux_if_print
, DLT_ARCNET_LINUX
},
141 { ether_if_print
, DLT_EN10MB
},
142 { token_if_print
, DLT_IEEE802
},
144 { lane_if_print
, DLT_LANE8023
},
147 { cip_if_print
, DLT_CIP
},
150 { cip_if_print
, DLT_ATM_CLIP
},
152 { sl_if_print
, DLT_SLIP
},
153 #ifdef DLT_SLIP_BSDOS
154 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
156 { ppp_if_print
, DLT_PPP
},
157 #ifdef DLT_PPP_WITHDIRECTION
158 { ppp_if_print
, DLT_PPP_WITHDIRECTION
},
161 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
163 { fddi_if_print
, DLT_FDDI
},
164 { null_if_print
, DLT_NULL
},
166 { null_if_print
, DLT_LOOP
},
168 { raw_if_print
, DLT_RAW
},
169 { atm_if_print
, DLT_ATM_RFC1483
},
171 { chdlc_if_print
, DLT_C_HDLC
},
174 { chdlc_if_print
, DLT_HDLC
},
176 #ifdef DLT_PPP_SERIAL
177 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
180 { pppoe_if_print
, DLT_PPP_ETHER
},
183 { sll_if_print
, DLT_LINUX_SLL
},
185 #ifdef DLT_IEEE802_11
186 { ieee802_11_if_print
, DLT_IEEE802_11
},
189 { ltalk_if_print
, DLT_LTALK
},
192 { pflog_if_print
, DLT_PFLOG
},
195 { fr_if_print
, DLT_FR
},
198 { fr_if_print
, DLT_FRELAY
},
201 { sunatm_if_print
, DLT_SUNATM
},
203 #ifdef DLT_IP_OVER_FC
204 { ipfc_if_print
, DLT_IP_OVER_FC
},
206 #ifdef DLT_PRISM_HEADER
207 { prism_if_print
, DLT_PRISM_HEADER
},
209 #ifdef DLT_IEEE802_11_RADIO
210 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
213 { enc_if_print
, DLT_ENC
},
215 #ifdef DLT_SYMANTEC_FIREWALL
216 { symantec_if_print
, DLT_SYMANTEC_FIREWALL
},
218 #ifdef DLT_APPLE_IP_OVER_IEEE1394
219 { ap1394_if_print
, DLT_APPLE_IP_OVER_IEEE1394
},
221 #ifdef DLT_JUNIPER_ATM1
222 { juniper_atm1_print
, DLT_JUNIPER_ATM1
},
224 #ifdef DLT_JUNIPER_ATM2
225 { juniper_atm2_print
, DLT_JUNIPER_ATM2
},
231 lookup_printer(int type
)
235 for (p
= printers
; p
->f
; ++p
)
260 show_dlts_and_exit(pcap_t
*pd
)
264 const char *dlt_name
;
266 n_dlts
= pcap_list_datalinks(pd
, &dlts
);
268 error("%s", pcap_geterr(pd
));
269 else if (n_dlts
== 0 || !dlts
)
270 error("No data link types.");
272 (void) fprintf(stderr
, "Data link types (use option -y to set):\n");
274 while (--n_dlts
>= 0) {
275 dlt_name
= pcap_datalink_val_to_name(dlts
[n_dlts
]);
276 if (dlt_name
!= NULL
) {
277 (void) fprintf(stderr
, " %s (%s)", dlt_name
,
278 pcap_datalink_val_to_description(dlts
[n_dlts
]));
281 * OK, does tcpdump handle that type?
283 if (lookup_printer(dlts
[n_dlts
]) == NULL
)
284 (void) fprintf(stderr
, " (not supported)");
287 (void) fprintf(stderr
, " DLT %d (not supported)\n",
296 * Set up flags that might or might not be supported depending on the
297 * version of libpcap we're using.
301 #define B_FLAG_USAGE " [ -B size ]"
307 #ifdef HAVE_PCAP_FINDALLDEVS
308 #ifndef HAVE_PCAP_IF_T
309 #undef HAVE_PCAP_FINDALLDEVS
313 #ifdef HAVE_PCAP_FINDALLDEVS
319 #ifdef HAVE_PCAP_DUMP_FLUSH
326 /* Drop root privileges and chroot if necessary */
328 droproot(const char *username
, const char *chroot_dir
)
330 struct passwd
*pw
= NULL
;
332 if (chroot_dir
&& !username
) {
333 fprintf(stderr
, "tcpdump: Chroot without dropping root is insecure\n");
337 pw
= getpwnam(username
);
340 if (chroot(chroot_dir
) != 0 || chdir ("/") != 0) {
341 fprintf(stderr
, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
342 chroot_dir
, pcap_strerror(errno
));
346 if (initgroups(pw
->pw_name
, pw
->pw_gid
) != 0 ||
347 setgid(pw
->pw_gid
) != 0 || setuid(pw
->pw_uid
) != 0) {
348 fprintf(stderr
, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
350 (unsigned long)pw
->pw_uid
,
351 (unsigned long)pw
->pw_gid
,
352 pcap_strerror(errno
));
357 fprintf(stderr
, "tcpdump: Couldn't find user '%.32s'\n",
380 MakeFilename(char *buffer
, char *orig_name
, int cnt
, int max_chars
)
382 if (cnt
== 0 && max_chars
== 0)
383 strcpy(buffer
, orig_name
);
385 sprintf(buffer
, "%s%0*d", orig_name
, max_chars
, cnt
);
388 static int tcpdump_printf(netdissect_options
*ndo _U_
,
389 const char *fmt
, ...)
396 ret
=vfprintf(stdout
, fmt
, args
);
403 main(int argc
, char **argv
)
405 register int cnt
, op
, i
;
406 bpf_u_int32 localnet
, netmask
;
407 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *WFileName
, *WFileNameAlt
;
408 pcap_handler callback
;
410 struct bpf_program fcode
;
412 RETSIGTYPE (*oldhandler
)(int);
414 struct print_info printinfo
;
415 struct dump_info dumpinfo
;
416 u_char
*pcap_userdata
;
417 char ebuf
[PCAP_ERRBUF_SIZE
];
418 char *username
= NULL
;
419 char *chroot_dir
= NULL
;
420 #ifdef HAVE_PCAP_FINDALLDEVS
421 pcap_if_t
*devpointer
;
426 u_int UserBufferSize
= 1000000;
427 if(wsockinit() != 0) return 1;
433 gndo
->ndo_printf
=tcpdump_printf
;
434 gndo
->ndo_error
=ndo_error
;
435 gndo
->ndo_warning
=ndo_warning
;
436 gndo
->ndo_snaplen
= DEFAULT_SNAPLEN
;
443 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
444 program_name
= cp
+ 1;
446 program_name
= argv
[0];
448 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
457 (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)
461 /* compatibility for old -a */
472 UserBufferSize
= atoi(optarg
)*1024;
473 if (UserBufferSize
< 0)
474 error("invalid packet buffer size %s", optarg
);
481 error("invalid packet count %s", optarg
);
485 Cflag
= atoi(optarg
) * 1000000;
487 error("invalid file size %s", optarg
);
494 #ifdef HAVE_PCAP_FINDALLDEVS
496 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
499 for (i
= 0; devpointer
!= 0; i
++) {
500 printf("%d.%s", i
+1, devpointer
->name
);
501 if (devpointer
->description
!= NULL
)
502 printf(" (%s)", devpointer
->description
);
504 devpointer
= devpointer
->next
;
508 #endif /* HAVE_PCAP_FINDALLDEVS */
519 #ifndef HAVE_LIBCRYPTO
520 warning("crypto code not compiled in");
522 gndo
->ndo_espsecret
= optarg
;
534 if (optarg
[0] == '0' && optarg
[1] == 0)
535 error("Invalid adapter index");
537 #ifdef HAVE_PCAP_FINDALLDEVS
539 * If the argument is a number, treat it as
540 * an index into the list of adapters, as
541 * printed by "tcpdump -D".
543 * This should be OK on UNIX systems, as interfaces
544 * shouldn't have names that begin with digits.
545 * It can be useful on Windows, where more than
546 * one interface can have the same name.
548 if ((devnum
= atoi(optarg
)) != 0) {
550 error("Invalid adapter index");
552 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
555 for (i
= 0; i
< devnum
-1; i
++){
556 devpointer
= devpointer
->next
;
557 if (devpointer
== NULL
)
558 error("Invalid adapter index");
561 device
= devpointer
->name
;
564 #endif /* HAVE_PCAP_FINDALLDEVS */
571 * _IOLBF is the same as _IOFBF in Microsoft's C
572 * libraries; the only alternative they offer
575 * XXX - this should really be checking for MSVC++,
576 * not WIN32, if, for example, MinGW has its own
577 * C library that is more UNIX-compatible.
579 setvbuf(stdout
, NULL
, _IONBF
, 0);
581 #ifdef HAVE_SETLINEBUF
584 setvbuf(stdout
, NULL
, _IOLBF
, 0);
599 if (smiLoadModule(optarg
) == 0) {
600 error("could not load MIB module %s", optarg
);
604 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
605 program_name
, optarg
);
606 (void)fprintf(stderr
, "(no libsmi support)\n");
611 /* TCP-MD5 shared secret */
612 #ifndef HAVE_LIBCRYPTO
613 warning("crypto code not compiled in");
615 tcpmd5secret
= optarg
;
641 snaplen
= strtol(optarg
, &end
, 0);
642 if (optarg
== end
|| *end
!= '\0'
643 || snaplen
< 0 || snaplen
> 65535)
644 error("invalid snaplen %s", optarg
);
645 else if (snaplen
== 0)
659 if (strcasecmp(optarg
, "vat") == 0)
661 else if (strcasecmp(optarg
, "wb") == 0)
663 else if (strcasecmp(optarg
, "rpc") == 0)
665 else if (strcasecmp(optarg
, "rtp") == 0)
667 else if (strcasecmp(optarg
, "rtcp") == 0)
668 packettype
= PT_RTCP
;
669 else if (strcasecmp(optarg
, "snmp") == 0)
670 packettype
= PT_SNMP
;
671 else if (strcasecmp(optarg
, "cnfp") == 0)
672 packettype
= PT_CNFP
;
673 else if (strcasecmp(optarg
, "tftp") == 0)
674 packettype
= PT_TFTP
;
675 else if (strcasecmp(optarg
, "aodv") == 0)
676 packettype
= PT_AODV
;
678 error("unknown packet type `%s'", optarg
);
685 #ifdef HAVE_PCAP_DUMP_FLUSH
700 Wflag
= atoi(optarg
);
702 error("invalid number of output files %s", optarg
);
703 WflagChars
= getWflagChars(Wflag
);
715 gndo
->ndo_dltname
= optarg
;
717 pcap_datalink_name_to_val(gndo
->ndo_dltname
);
718 if (gndo
->ndo_dlt
< 0)
719 error("invalid data link type %s", gndo
->ndo_dltname
);
722 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
725 /* Undocumented flag */
726 #ifdef HAVE_PCAP_DEBUG
727 extern int pcap_debug
;
738 username
= strdup(optarg
);
753 case 0: /* Default */
754 case 4: /* Default + Date*/
755 thiszone
= gmt2local(0);
758 case 1: /* No time stamp */
759 case 2: /* Unix timeval style */
760 case 3: /* Microseconds since previous packet */
763 default: /* Not supported */
764 error("only -t, -tt, -ttt, and -tttt are supported");
769 /* if run as root, prepare for chrooting */
770 if (getuid() == 0 || geteuid() == 0) {
771 /* future extensibility for cmd-line arguments */
773 chroot_dir
= WITH_CHROOT
;
778 /* if run as root, prepare for dropping root privileges */
779 if (getuid() == 0 || geteuid() == 0) {
780 /* Run with '-Z root' to restore old behaviour */
782 username
= WITH_USER
;
786 if (RFileName
!= NULL
) {
788 const char *dlt_name
;
792 * We don't need network access, so relinquish any set-UID
793 * or set-GID privileges we have (if any).
795 * We do *not* want set-UID privileges when opening a
796 * trace file, as that might let the user read other
797 * people's trace files (especially if we're set-UID
800 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
801 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
803 pd
= pcap_open_offline(RFileName
, ebuf
);
806 dlt
= pcap_datalink(pd
);
807 dlt_name
= pcap_datalink_val_to_name(dlt
);
808 if (dlt_name
== NULL
) {
809 fprintf(stderr
, "reading from file %s, link-type %u\n",
813 "reading from file %s, link-type %s (%s)\n",
815 pcap_datalink_val_to_description(dlt
));
820 error("-f and -r options are incompatible");
822 if (device
== NULL
) {
823 device
= pcap_lookupdev(ebuf
);
828 if(strlen(device
) == 1) //we assume that an ASCII string is always longer than 1 char
829 { //a Unicode string has a \0 as second byte (so strlen() is 1)
830 fprintf(stderr
, "%s: listening on %ws\n", program_name
, device
);
834 fprintf(stderr
, "%s: listening on %s\n", program_name
, device
);
840 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
846 * Let user own process after socket has been opened.
849 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
850 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
853 if(UserBufferSize
!= 1000000)
854 if(pcap_setbuff(pd
, UserBufferSize
)==-1){
855 error("%s", pcap_geterr(pd
));
859 show_dlts_and_exit(pd
);
860 if (gndo
->ndo_dlt
>= 0) {
861 #ifdef HAVE_PCAP_SET_DATALINK
862 if (pcap_set_datalink(pd
, gndo
->ndo_dlt
) < 0)
863 error("%s", pcap_geterr(pd
));
866 * We don't actually support changing the
867 * data link type, so we only let them
868 * set it to what it already is.
870 if (gndo
->ndo_dlt
!= pcap_datalink(pd
)) {
871 error("%s is not one of the DLTs supported by this device\n",
875 (void)fprintf(stderr
, "%s: data link type %s\n",
876 program_name
, gndo
->ndo_dltname
);
877 (void)fflush(stderr
);
879 i
= pcap_snapshot(pd
);
881 warning("snaplen raised from %d to %d", snaplen
, i
);
884 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
891 cmdbuf
= read_infile(infile
);
893 cmdbuf
= copy_argv(&argv
[optind
]);
895 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
896 error("%s", pcap_geterr(pd
));
898 bpf_dump(&fcode
, dflag
);
902 init_addrtoname(localnet
, netmask
);
905 (void)setsignal(SIGPIPE
, cleanup
);
907 (void)setsignal(SIGTERM
, cleanup
);
908 (void)setsignal(SIGINT
, cleanup
);
909 /* Cooperate with nohup(1) */
911 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
912 (void)setsignal(SIGHUP
, oldhandler
);
915 if (pcap_setfilter(pd
, &fcode
) < 0)
916 error("%s", pcap_geterr(pd
));
920 WFileNameAlt
= (char *)malloc(strlen(WFileName
) + MAX_CFLAG_CHARS
+ 1);
921 if (WFileNameAlt
== NULL
)
922 error("malloc of WFileNameAlt");
923 MakeFilename(WFileNameAlt
, WFileName
, 0, WflagChars
);
924 p
= pcap_dump_open(pd
, WFileNameAlt
);
926 error("%s", pcap_geterr(pd
));
928 callback
= dump_packet_and_trunc
;
929 dumpinfo
.WFileName
= WFileName
;
932 pcap_userdata
= (u_char
*)&dumpinfo
;
934 callback
= dump_packet
;
935 pcap_userdata
= (u_char
*)p
;
938 type
= pcap_datalink(pd
);
939 printinfo
.printer
= lookup_printer(type
);
940 if (printinfo
.printer
== NULL
) {
941 gndo
->ndo_dltname
= pcap_datalink_val_to_name(type
);
942 if (gndo
->ndo_dltname
!= NULL
)
943 error("unsupported data link type %s",
946 error("unsupported data link type %d", type
);
948 callback
= print_packet
;
949 pcap_userdata
= (u_char
*)&printinfo
;
953 * We cannot do this earlier, because we want to be able to open
954 * the file (if done) for writing before giving up permissions.
956 if (getuid() == 0 || geteuid() == 0) {
957 if (username
|| chroot_dir
)
958 droproot(username
, chroot_dir
);
962 (void)setsignal(SIGINFO
, requestinfo
);
965 if (vflag
> 0 && WFileName
) {
967 * When capturing to a file, "-v" means tcpdump should,
968 * every 10 secodns, "v"erbosely report the number of
971 #ifdef USE_WIN32_MM_TIMER
972 /* call verbose_stats_dump() each 1000 +/-100msec */
973 timer_id
= timeSetEvent(1000, 100, verbose_stats_dump
, 0, TIME_PERIODIC
);
974 setvbuf(stderr
, NULL
, _IONBF
, 0);
975 #elif defined(HAVE_ALARM)
976 (void)setsignal(SIGALRM
, verbose_stats_dump
);
982 if (RFileName
== NULL
) {
984 const char *dlt_name
;
986 if (!vflag
&& !WFileName
) {
987 (void)fprintf(stderr
,
988 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
991 (void)fprintf(stderr
, "%s: ", program_name
);
992 dlt
= pcap_datalink(pd
);
993 dlt_name
= pcap_datalink_val_to_name(dlt
);
994 if (dlt_name
== NULL
) {
995 (void)fprintf(stderr
, "listening on %s, link-type %u, capture size %u bytes\n",
996 device
, dlt
, snaplen
);
998 (void)fprintf(stderr
, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1000 pcap_datalink_val_to_description(dlt
), snaplen
);
1002 (void)fflush(stderr
);
1005 status
= pcap_loop(pd
, cnt
, callback
, pcap_userdata
);
1006 if (WFileName
== NULL
) {
1008 * We're printing packets. Flush the printed output,
1009 * so it doesn't get intermingled with error output.
1013 * We got interrupted, so perhaps we didn't
1014 * manage to finish a line we were printing.
1015 * Print an extra newline, just in case.
1019 (void)fflush(stdout
);
1025 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
1026 program_name
, pcap_geterr(pd
));
1028 if (RFileName
== NULL
) {
1030 * We're doing a live capture. Report the capture
1036 exit(status
== -1 ? 1 : 0);
1039 /* make a clean exit on interrupts */
1041 cleanup(int signo _U_
)
1043 #ifdef USE_WIN32_MM_TIMER
1045 timeKillEvent(timer_id
);
1047 #elif defined(HAVE_ALARM)
1051 #ifdef HAVE_PCAP_BREAKLOOP
1053 * We have "pcap_breakloop()"; use it, so that we do as little
1054 * as possible in the signal handler (it's probably not safe
1055 * to do anything with standard I/O streams in a signal handler -
1056 * the ANSI C standard doesn't say it is).
1061 * We don't have "pcap_breakloop()"; this isn't safe, but
1062 * it's the best we can do. Print the summary if we're
1063 * not reading from a savefile - i.e., if we're doing a
1064 * live capture - and exit.
1066 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
1068 * We got interrupted, so perhaps we didn't
1069 * manage to finish a line we were printing.
1070 * Print an extra newline, just in case.
1073 (void)fflush(stdout
);
1081 info(register int verbose
)
1083 struct pcap_stat stat
;
1085 if (pcap_stats(pd
, &stat
) < 0) {
1086 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
1091 fprintf(stderr
, "%s: ", program_name
);
1093 (void)fprintf(stderr
, "%u packets captured", packets_captured
);
1095 fputs(", ", stderr
);
1098 (void)fprintf(stderr
, "%d packets received by filter", stat
.ps_recv
);
1100 fputs(", ", stderr
);
1103 (void)fprintf(stderr
, "%d packets dropped by kernel\n", stat
.ps_drop
);
1108 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1110 struct dump_info
*dump_info
;
1117 dump_info
= (struct dump_info
*)user
;
1120 * XXX - this won't prevent capture files from getting
1121 * larger than Cflag - the last packet written to the
1122 * file could put it over Cflag.
1124 if (ftell((FILE *)dump_info
->p
) > Cflag
) {
1126 * Close the current file and open a new one.
1128 pcap_dump_close(dump_info
->p
);
1131 if (Cflag_count
>= Wflag
)
1134 if (Cflag_count
>= MAX_CFLAG
)
1135 error("too many output files");
1137 name
= (char *)malloc(strlen(dump_info
->WFileName
) + MAX_CFLAG_CHARS
+ 1);
1139 error("dump_packet_and_trunc: malloc");
1140 MakeFilename(name
, dump_info
->WFileName
, Cflag_count
, WflagChars
);
1141 dump_info
->p
= pcap_dump_open(dump_info
->pd
, name
);
1143 if (dump_info
->p
== NULL
)
1144 error("%s", pcap_geterr(pd
));
1147 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
1148 #ifdef HAVE_PCAP_DUMP_FLUSH
1150 pcap_dump_flush(dump_info
->p
);
1159 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1165 pcap_dump(user
, h
, sp
);
1166 #ifdef HAVE_PCAP_DUMP_FLUSH
1168 pcap_dump_flush((pcap_dumper_t
*)user
);
1177 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1179 struct print_info
*print_info
;
1187 print_info
= (struct print_info
*)user
;
1190 * Some printers want to check that they're not walking off the
1191 * end of the packet.
1192 * Rather than pass it all the way down, we set this global.
1194 snapend
= sp
+ h
->caplen
;
1196 hdrlen
= (*print_info
->printer
)(h
, sp
);
1199 * Print the raw packet data.
1203 * Include the link-layer header.
1205 hex_print("\n\t", sp
, h
->caplen
);
1208 * Don't include the link-layer header - and if
1209 * we have nothing past the link-layer header,
1212 if (h
->caplen
> hdrlen
)
1213 hex_print("\n\t", sp
+ hdrlen
,
1214 h
->caplen
- hdrlen
);
1218 * Print the raw packet data.
1222 * Include the link-layer header.
1224 ascii_print("\n\t", sp
, h
->caplen
);
1227 * Don't include the link-layer header - and if
1228 * we have nothing past the link-layer header,
1231 if (h
->caplen
> hdrlen
)
1232 ascii_print("\n\t", sp
+ hdrlen
,
1233 h
->caplen
- hdrlen
);
1246 * XXX - there should really be libpcap calls to get the version
1247 * number as a string (the string would be generated from #defines
1248 * at run time, so that it's not generated from string constants
1249 * in the library, as, on many UNIX systems, those constants would
1250 * be statically linked into the application executable image, and
1251 * would thus reflect the version of libpcap on the system on
1252 * which the application was *linked*, not the system on which it's
1255 * That routine should be documented, unlike the "version[]"
1256 * string, so that UNIX vendors providing their own libpcaps
1257 * don't omit it (as a couple of vendors have...).
1259 * Packet.dll should perhaps also export a routine to return the
1260 * version number of the Packet.dll code, to supply the
1261 * "Wpcap_version" information on Windows.
1263 char WDversion
[]="current-cvs.tcpdump.org";
1264 #if !defined(HAVE_GENERATED_VERSION)
1265 char version
[]="current-cvs.tcpdump.org";
1267 char pcap_version
[]="current-cvs.tcpdump.org";
1268 char Wpcap_version
[]="3.1";
1272 * By default, print the specified data out in hex.
1275 default_print(register const u_char
*bp
, register u_int length
)
1277 ascii_print("\n\t", bp
, length
); /* pass on lf and identation string */
1281 RETSIGTYPE
requestinfo(int signo _U_
)
1291 * Called once each second in verbose mode while dumping to file
1293 #ifdef USE_WIN32_MM_TIMER
1294 void CALLBACK
verbose_stats_dump (UINT timer_id _U_
, UINT msg _U_
, DWORD_PTR arg _U_
,
1295 DWORD_PTR dw1 _U_
, DWORD_PTR dw2 _U_
)
1297 struct pcap_stat stat
;
1299 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
1300 fprintf(stderr
, "Got %u\r", packets_captured
);
1302 #elif defined(HAVE_ALARM)
1303 static void verbose_stats_dump(int sig _U_
)
1305 struct pcap_stat stat
;
1307 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
1308 fprintf(stderr
, "Got %u\r", packets_captured
);
1316 extern char version
[];
1317 #ifndef HAVE_PCAP_LIB_VERSION
1318 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
1319 extern char pcap_version
[];
1320 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1321 static char pcap_version
[] = "unknown";
1322 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1323 #endif /* HAVE_PCAP_LIB_VERSION */
1325 #ifdef HAVE_PCAP_LIB_VERSION
1327 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
1329 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1331 (void)fprintf(stderr
, "%s\n",pcap_lib_version());
1332 #else /* HAVE_PCAP_LIB_VERSION */
1334 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
1335 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
1337 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1338 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
1340 #endif /* HAVE_PCAP_LIB_VERSION */
1341 (void)fprintf(stderr
,
1342 "Usage: %s [-aAd" D_FLAG
"eflLnNOpqRStu" U_FLAG
"vxX]" B_FLAG_USAGE
" [-c count] [ -C file_size ]\n", program_name
);
1343 (void)fprintf(stderr
,
1344 "\t\t[ -E algo:secret ] [ -F file ] [ -i interface ] [ -M secret ]\n");
1345 (void)fprintf(stderr
,
1346 "\t\t[ -r file ] [ -s snaplen ] [ -T type ] [ -w file ]\n");
1347 (void)fprintf(stderr
,
1348 "\t\t[ -W filecount ] [ -y datalinktype ] [ -Z user ]\n");
1349 (void)fprintf(stderr
,
1350 "\t\t[ expression ]\n");
1358 ndo_error(netdissect_options
*ndo _U_
, const char *fmt
, ...)
1362 (void)fprintf(stderr
, "%s: ", program_name
);
1364 (void)vfprintf(stderr
, fmt
, ap
);
1368 if (fmt
[-1] != '\n')
1369 (void)fputc('\n', stderr
);
1377 ndo_warning(netdissect_options
*ndo _U_
, const char *fmt
, ...)
1381 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
1383 (void)vfprintf(stderr
, fmt
, ap
);
1387 if (fmt
[-1] != '\n')
1388 (void)fputc('\n', stderr
);