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.248 2004-09-04 00:08:04 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
);
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
},
225 lookup_printer(int type
)
229 for (p
= printers
; p
->f
; ++p
)
254 show_dlts_and_exit(pcap_t
*pd
)
258 const char *dlt_name
;
260 n_dlts
= pcap_list_datalinks(pd
, &dlts
);
262 error("%s", pcap_geterr(pd
));
263 else if (n_dlts
== 0 || !dlts
)
264 error("No data link types.");
266 (void) fprintf(stderr
, "Data link types (use option -y to set):\n");
268 while (--n_dlts
>= 0) {
269 dlt_name
= pcap_datalink_val_to_name(dlts
[n_dlts
]);
270 if (dlt_name
!= NULL
) {
271 (void) fprintf(stderr
, " %s (%s)", dlt_name
,
272 pcap_datalink_val_to_description(dlts
[n_dlts
]));
275 * OK, does tcpdump handle that type?
277 if (lookup_printer(dlts
[n_dlts
]) == NULL
)
278 (void) fprintf(stderr
, " (not supported)");
281 (void) fprintf(stderr
, " DLT %d (not supported)\n",
290 * Set up flags that might or might not be supported depending on the
291 * version of libpcap we're using.
295 #define B_FLAG_USAGE " [ -B size ]"
301 #ifdef HAVE_PCAP_FINDALLDEVS
302 #ifndef HAVE_PCAP_IF_T
303 #undef HAVE_PCAP_FINDALLDEVS
307 #ifdef HAVE_PCAP_FINDALLDEVS
313 #ifdef HAVE_PCAP_DUMP_FLUSH
320 /* Drop root privileges and chroot if necessary */
322 droproot(const char *username
, const char *chroot_dir
)
324 struct passwd
*pw
= NULL
;
326 if (chroot_dir
&& !username
) {
327 fprintf(stderr
, "tcpdump: Chroot without dropping root is insecure\n");
331 pw
= getpwnam(username
);
334 if (chroot(chroot_dir
) != 0 || chdir ("/") != 0) {
335 fprintf(stderr
, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
336 chroot_dir
, pcap_strerror(errno
));
340 if (initgroups(pw
->pw_name
, pw
->pw_gid
) != 0 ||
341 setgid(pw
->pw_gid
) != 0 || setuid(pw
->pw_uid
) != 0) {
342 fprintf(stderr
, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
344 (unsigned long)pw
->pw_uid
,
345 (unsigned long)pw
->pw_gid
,
346 pcap_strerror(errno
));
351 fprintf(stderr
, "tcpdump: Couldn't find user '%.32s'\n",
374 MakeFilename(char *buffer
, char *orig_name
, int cnt
, int max_chars
)
376 if (cnt
== 0 && max_chars
== 0)
377 strcpy(buffer
, orig_name
);
379 sprintf(buffer
, "%s%0*d", orig_name
, max_chars
, cnt
);
382 static int tcpdump_printf(netdissect_options
*ndo _U_
,
383 const char *fmt
, ...)
390 ret
=vfprintf(stdout
, fmt
, args
);
397 main(int argc
, char **argv
)
399 register int cnt
, op
, i
;
400 bpf_u_int32 localnet
, netmask
;
401 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *WFileName
, *WFileNameAlt
;
402 pcap_handler callback
;
404 struct bpf_program fcode
;
406 RETSIGTYPE (*oldhandler
)(int);
408 struct print_info printinfo
;
409 struct dump_info dumpinfo
;
410 u_char
*pcap_userdata
;
411 char ebuf
[PCAP_ERRBUF_SIZE
];
412 char *username
= NULL
;
413 char *chroot_dir
= NULL
;
414 #ifdef HAVE_PCAP_FINDALLDEVS
415 pcap_if_t
*devpointer
;
420 u_int UserBufferSize
= 1000000;
421 if(wsockinit() != 0) return 1;
427 gndo
->ndo_printf
=tcpdump_printf
;
428 gndo
->ndo_error
=ndo_error
;
429 gndo
->ndo_warning
=ndo_warning
;
430 gndo
->ndo_snaplen
= DEFAULT_SNAPLEN
;
437 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
438 program_name
= cp
+ 1;
440 program_name
= argv
[0];
442 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
451 (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)
455 /* compatibility for old -a */
466 UserBufferSize
= atoi(optarg
)*1024;
467 if (UserBufferSize
< 0)
468 error("invalid packet buffer size %s", optarg
);
475 error("invalid packet count %s", optarg
);
479 Cflag
= atoi(optarg
) * 1000000;
481 error("invalid file size %s", optarg
);
488 #ifdef HAVE_PCAP_FINDALLDEVS
490 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
493 for (i
= 0; devpointer
!= 0; i
++) {
494 printf("%d.%s", i
+1, devpointer
->name
);
495 if (devpointer
->description
!= NULL
)
496 printf(" (%s)", devpointer
->description
);
498 devpointer
= devpointer
->next
;
502 #endif /* HAVE_PCAP_FINDALLDEVS */
513 #ifndef HAVE_LIBCRYPTO
514 warning("crypto code not compiled in");
516 gndo
->ndo_espsecret
= optarg
;
528 if (optarg
[0] == '0' && optarg
[1] == 0)
529 error("Invalid adapter index");
531 #ifdef HAVE_PCAP_FINDALLDEVS
533 * If the argument is a number, treat it as
534 * an index into the list of adapters, as
535 * printed by "tcpdump -D".
537 * This should be OK on UNIX systems, as interfaces
538 * shouldn't have names that begin with digits.
539 * It can be useful on Windows, where more than
540 * one interface can have the same name.
542 if ((devnum
= atoi(optarg
)) != 0) {
544 error("Invalid adapter index");
546 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
549 for (i
= 0; i
< devnum
-1; i
++){
550 devpointer
= devpointer
->next
;
551 if (devpointer
== NULL
)
552 error("Invalid adapter index");
555 device
= devpointer
->name
;
558 #endif /* HAVE_PCAP_FINDALLDEVS */
565 * _IOLBF is the same as _IOFBF in Microsoft's C
566 * libraries; the only alternative they offer
569 * XXX - this should really be checking for MSVC++,
570 * not WIN32, if, for example, MinGW has its own
571 * C library that is more UNIX-compatible.
573 setvbuf(stdout
, NULL
, _IONBF
, 0);
575 #ifdef HAVE_SETLINEBUF
578 setvbuf(stdout
, NULL
, _IOLBF
, 0);
593 if (smiLoadModule(optarg
) == 0) {
594 error("could not load MIB module %s", optarg
);
598 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
599 program_name
, optarg
);
600 (void)fprintf(stderr
, "(no libsmi support)\n");
605 /* TCP-MD5 shared secret */
606 #ifndef HAVE_LIBCRYPTO
607 warning("crypto code not compiled in");
609 tcpmd5secret
= optarg
;
635 snaplen
= strtol(optarg
, &end
, 0);
636 if (optarg
== end
|| *end
!= '\0'
637 || snaplen
< 0 || snaplen
> 65535)
638 error("invalid snaplen %s", optarg
);
639 else if (snaplen
== 0)
653 if (strcasecmp(optarg
, "vat") == 0)
655 else if (strcasecmp(optarg
, "wb") == 0)
657 else if (strcasecmp(optarg
, "rpc") == 0)
659 else if (strcasecmp(optarg
, "rtp") == 0)
661 else if (strcasecmp(optarg
, "rtcp") == 0)
662 packettype
= PT_RTCP
;
663 else if (strcasecmp(optarg
, "snmp") == 0)
664 packettype
= PT_SNMP
;
665 else if (strcasecmp(optarg
, "cnfp") == 0)
666 packettype
= PT_CNFP
;
667 else if (strcasecmp(optarg
, "tftp") == 0)
668 packettype
= PT_TFTP
;
669 else if (strcasecmp(optarg
, "aodv") == 0)
670 packettype
= PT_AODV
;
672 error("unknown packet type `%s'", optarg
);
679 #ifdef HAVE_PCAP_DUMP_FLUSH
694 Wflag
= atoi(optarg
);
696 error("invalid number of output files %s", optarg
);
697 WflagChars
= getWflagChars(Wflag
);
709 gndo
->ndo_dltname
= optarg
;
711 pcap_datalink_name_to_val(gndo
->ndo_dltname
);
712 if (gndo
->ndo_dlt
< 0)
713 error("invalid data link type %s", gndo
->ndo_dltname
);
716 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
719 /* Undocumented flag */
720 #ifdef HAVE_PCAP_DEBUG
721 extern int pcap_debug
;
732 username
= strdup(optarg
);
747 case 0: /* Default */
748 case 4: /* Default + Date*/
749 thiszone
= gmt2local(0);
752 case 1: /* No time stamp */
753 case 2: /* Unix timeval style */
754 case 3: /* Microseconds since previous packet */
757 default: /* Not supported */
758 error("only -t, -tt, -ttt, and -tttt are supported");
763 /* if run as root, prepare for chrooting */
764 if (getuid() == 0 || geteuid() == 0) {
765 /* future extensibility for cmd-line arguments */
767 chroot_dir
= WITH_CHROOT
;
772 /* if run as root, prepare for dropping root privileges */
773 if (getuid() == 0 || geteuid() == 0) {
774 /* Run with '-Z root' to restore old behaviour */
776 username
= WITH_USER
;
780 if (RFileName
!= NULL
) {
782 const char *dlt_name
;
786 * We don't need network access, so relinquish any set-UID
787 * or set-GID privileges we have (if any).
789 * We do *not* want set-UID privileges when opening a
790 * trace file, as that might let the user read other
791 * people's trace files (especially if we're set-UID
794 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
795 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
797 pd
= pcap_open_offline(RFileName
, ebuf
);
800 dlt
= pcap_datalink(pd
);
801 dlt_name
= pcap_datalink_val_to_name(dlt
);
802 if (dlt_name
== NULL
) {
803 fprintf(stderr
, "reading from file %s, link-type %u\n",
807 "reading from file %s, link-type %s (%s)\n",
809 pcap_datalink_val_to_description(dlt
));
814 error("-f and -r options are incompatible");
816 if (device
== NULL
) {
817 device
= pcap_lookupdev(ebuf
);
822 if(strlen(device
) == 1) //we assume that an ASCII string is always longer than 1 char
823 { //a Unicode string has a \0 as second byte (so strlen() is 1)
824 fprintf(stderr
, "%s: listening on %ws\n", program_name
, device
);
828 fprintf(stderr
, "%s: listening on %s\n", program_name
, device
);
834 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
840 * Let user own process after socket has been opened.
843 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
844 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
847 if(UserBufferSize
!= 1000000)
848 if(pcap_setbuff(pd
, UserBufferSize
)==-1){
849 error("%s", pcap_geterr(pd
));
853 show_dlts_and_exit(pd
);
854 if (gndo
->ndo_dlt
>= 0) {
855 #ifdef HAVE_PCAP_SET_DATALINK
856 if (pcap_set_datalink(pd
, gndo
->ndo_dlt
) < 0)
857 error("%s", pcap_geterr(pd
));
860 * We don't actually support changing the
861 * data link type, so we only let them
862 * set it to what it already is.
864 if (gndo
->ndo_dlt
!= pcap_datalink(pd
)) {
865 error("%s is not one of the DLTs supported by this device\n",
869 (void)fprintf(stderr
, "%s: data link type %s\n",
870 program_name
, gndo
->ndo_dltname
);
871 (void)fflush(stderr
);
873 i
= pcap_snapshot(pd
);
875 warning("snaplen raised from %d to %d", snaplen
, i
);
878 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
885 cmdbuf
= read_infile(infile
);
887 cmdbuf
= copy_argv(&argv
[optind
]);
889 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
890 error("%s", pcap_geterr(pd
));
892 bpf_dump(&fcode
, dflag
);
896 init_addrtoname(localnet
, netmask
);
899 (void)setsignal(SIGPIPE
, cleanup
);
901 (void)setsignal(SIGTERM
, cleanup
);
902 (void)setsignal(SIGINT
, cleanup
);
903 /* Cooperate with nohup(1) */
905 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
906 (void)setsignal(SIGHUP
, oldhandler
);
909 if (pcap_setfilter(pd
, &fcode
) < 0)
910 error("%s", pcap_geterr(pd
));
914 WFileNameAlt
= (char *)malloc(strlen(WFileName
) + MAX_CFLAG_CHARS
+ 1);
915 if (WFileNameAlt
== NULL
)
916 error("malloc of WFileNameAlt");
917 MakeFilename(WFileNameAlt
, WFileName
, 0, WflagChars
);
918 p
= pcap_dump_open(pd
, WFileNameAlt
);
920 error("%s", pcap_geterr(pd
));
922 callback
= dump_packet_and_trunc
;
923 dumpinfo
.WFileName
= WFileName
;
926 pcap_userdata
= (u_char
*)&dumpinfo
;
928 callback
= dump_packet
;
929 pcap_userdata
= (u_char
*)p
;
932 type
= pcap_datalink(pd
);
933 printinfo
.printer
= lookup_printer(type
);
934 if (printinfo
.printer
== NULL
) {
935 gndo
->ndo_dltname
= pcap_datalink_val_to_name(type
);
936 if (gndo
->ndo_dltname
!= NULL
)
937 error("unsupported data link type %s",
940 error("unsupported data link type %d", type
);
942 callback
= print_packet
;
943 pcap_userdata
= (u_char
*)&printinfo
;
947 * We cannot do this earlier, because we want to be able to open
948 * the file (if done) for writing before giving up permissions.
950 if (getuid() == 0 || geteuid() == 0) {
951 if (username
|| chroot_dir
)
952 droproot(username
, chroot_dir
);
956 (void)setsignal(SIGINFO
, requestinfo
);
959 if (vflag
> 0 && WFileName
) {
961 * When capturing to a file, "-v" means tcpdump should,
962 * every 10 secodns, "v"erbosely report the number of
965 #ifdef USE_WIN32_MM_TIMER
966 /* call verbose_stats_dump() each 1000 +/-100msec */
967 timer_id
= timeSetEvent(1000, 100, verbose_stats_dump
, 0, TIME_PERIODIC
);
968 setvbuf(stderr
, NULL
, _IONBF
, 0);
969 #elif defined(HAVE_ALARM)
970 (void)setsignal(SIGALRM
, verbose_stats_dump
);
976 if (RFileName
== NULL
) {
978 const char *dlt_name
;
980 if (!vflag
&& !WFileName
) {
981 (void)fprintf(stderr
,
982 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
985 (void)fprintf(stderr
, "%s: ", program_name
);
986 dlt
= pcap_datalink(pd
);
987 dlt_name
= pcap_datalink_val_to_name(dlt
);
988 if (dlt_name
== NULL
) {
989 (void)fprintf(stderr
, "listening on %s, link-type %u, capture size %u bytes\n",
990 device
, dlt
, snaplen
);
992 (void)fprintf(stderr
, "listening on %s, link-type %s (%s), capture size %u bytes\n",
994 pcap_datalink_val_to_description(dlt
), snaplen
);
996 (void)fflush(stderr
);
999 status
= pcap_loop(pd
, cnt
, callback
, pcap_userdata
);
1000 if (WFileName
== NULL
) {
1002 * We're printing packets. Flush the printed output,
1003 * so it doesn't get intermingled with error output.
1007 * We got interrupted, so perhaps we didn't
1008 * manage to finish a line we were printing.
1009 * Print an extra newline, just in case.
1013 (void)fflush(stdout
);
1019 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
1020 program_name
, pcap_geterr(pd
));
1022 if (RFileName
== NULL
) {
1024 * We're doing a live capture. Report the capture
1030 exit(status
== -1 ? 1 : 0);
1033 /* make a clean exit on interrupts */
1035 cleanup(int signo _U_
)
1037 #ifdef USE_WIN32_MM_TIMER
1039 timeKillEvent(timer_id
);
1041 #elif defined(HAVE_ALARM)
1045 #ifdef HAVE_PCAP_BREAKLOOP
1047 * We have "pcap_breakloop()"; use it, so that we do as little
1048 * as possible in the signal handler (it's probably not safe
1049 * to do anything with standard I/O streams in a signal handler -
1050 * the ANSI C standard doesn't say it is).
1055 * We don't have "pcap_breakloop()"; this isn't safe, but
1056 * it's the best we can do. Print the summary if we're
1057 * not reading from a savefile - i.e., if we're doing a
1058 * live capture - and exit.
1060 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
1062 * We got interrupted, so perhaps we didn't
1063 * manage to finish a line we were printing.
1064 * Print an extra newline, just in case.
1067 (void)fflush(stdout
);
1075 info(register int verbose
)
1077 struct pcap_stat stat
;
1079 if (pcap_stats(pd
, &stat
) < 0) {
1080 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
1085 fprintf(stderr
, "%s: ", program_name
);
1087 (void)fprintf(stderr
, "%u packets captured", packets_captured
);
1089 fputs(", ", stderr
);
1092 (void)fprintf(stderr
, "%d packets received by filter", stat
.ps_recv
);
1094 fputs(", ", stderr
);
1097 (void)fprintf(stderr
, "%d packets dropped by kernel\n", stat
.ps_drop
);
1102 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1104 struct dump_info
*dump_info
;
1111 dump_info
= (struct dump_info
*)user
;
1114 * XXX - this won't prevent capture files from getting
1115 * larger than Cflag - the last packet written to the
1116 * file could put it over Cflag.
1118 if (ftell((FILE *)dump_info
->p
) > Cflag
) {
1120 * Close the current file and open a new one.
1122 pcap_dump_close(dump_info
->p
);
1125 if (Cflag_count
>= Wflag
)
1128 if (Cflag_count
>= MAX_CFLAG
)
1129 error("too many output files");
1131 name
= (char *)malloc(strlen(dump_info
->WFileName
) + MAX_CFLAG_CHARS
+ 1);
1133 error("dump_packet_and_trunc: malloc");
1134 MakeFilename(name
, dump_info
->WFileName
, Cflag_count
, WflagChars
);
1135 dump_info
->p
= pcap_dump_open(dump_info
->pd
, name
);
1137 if (dump_info
->p
== NULL
)
1138 error("%s", pcap_geterr(pd
));
1141 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
1142 #ifdef HAVE_PCAP_DUMP_FLUSH
1144 pcap_dump_flush(dump_info
->p
);
1153 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1159 pcap_dump(user
, h
, sp
);
1160 #ifdef HAVE_PCAP_DUMP_FLUSH
1162 pcap_dump_flush((pcap_dumper_t
*)user
);
1171 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1173 struct print_info
*print_info
;
1181 print_info
= (struct print_info
*)user
;
1184 * Some printers want to check that they're not walking off the
1185 * end of the packet.
1186 * Rather than pass it all the way down, we set this global.
1188 snapend
= sp
+ h
->caplen
;
1190 hdrlen
= (*print_info
->printer
)(h
, sp
);
1193 * Print the raw packet data.
1197 * Include the link-layer header.
1199 hex_print("\n\t", sp
, h
->caplen
);
1202 * Don't include the link-layer header - and if
1203 * we have nothing past the link-layer header,
1206 if (h
->caplen
> hdrlen
)
1207 hex_print("\n\t", sp
+ hdrlen
,
1208 h
->caplen
- hdrlen
);
1212 * Print the raw packet data.
1216 * Include the link-layer header.
1218 ascii_print("\n\t", sp
, h
->caplen
);
1221 * Don't include the link-layer header - and if
1222 * we have nothing past the link-layer header,
1225 if (h
->caplen
> hdrlen
)
1226 ascii_print("\n\t", sp
+ hdrlen
,
1227 h
->caplen
- hdrlen
);
1240 * XXX - there should really be libpcap calls to get the version
1241 * number as a string (the string would be generated from #defines
1242 * at run time, so that it's not generated from string constants
1243 * in the library, as, on many UNIX systems, those constants would
1244 * be statically linked into the application executable image, and
1245 * would thus reflect the version of libpcap on the system on
1246 * which the application was *linked*, not the system on which it's
1249 * That routine should be documented, unlike the "version[]"
1250 * string, so that UNIX vendors providing their own libpcaps
1251 * don't omit it (as a couple of vendors have...).
1253 * Packet.dll should perhaps also export a routine to return the
1254 * version number of the Packet.dll code, to supply the
1255 * "Wpcap_version" information on Windows.
1257 char WDversion
[]="current-cvs.tcpdump.org";
1258 #if !defined(HAVE_GENERATED_VERSION)
1259 char version
[]="current-cvs.tcpdump.org";
1261 char pcap_version
[]="current-cvs.tcpdump.org";
1262 char Wpcap_version
[]="3.1";
1266 * By default, print the specified data out in hex.
1269 default_print(register const u_char
*bp
, register u_int length
)
1271 ascii_print("\n\t", bp
, length
); /* pass on lf and identation string */
1275 RETSIGTYPE
requestinfo(int signo _U_
)
1285 * Called once each second in verbose mode while dumping to file
1287 #ifdef USE_WIN32_MM_TIMER
1288 void CALLBACK
verbose_stats_dump (UINT timer_id _U_
, UINT msg _U_
, DWORD_PTR arg _U_
,
1289 DWORD_PTR dw1 _U_
, DWORD_PTR dw2 _U_
)
1291 struct pcap_stat stat
;
1293 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
1294 fprintf(stderr
, "Got %u\r", packets_captured
);
1296 #elif defined(HAVE_ALARM)
1297 static void verbose_stats_dump(int sig _U_
)
1299 struct pcap_stat stat
;
1301 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
1302 fprintf(stderr
, "Got %u\r", packets_captured
);
1310 extern char version
[];
1311 #ifndef HAVE_PCAP_LIB_VERSION
1312 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
1313 extern char pcap_version
[];
1314 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1315 static char pcap_version
[] = "unknown";
1316 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1317 #endif /* HAVE_PCAP_LIB_VERSION */
1319 #ifdef HAVE_PCAP_LIB_VERSION
1321 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
1323 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1325 (void)fprintf(stderr
, "%s\n",pcap_lib_version());
1326 #else /* HAVE_PCAP_LIB_VERSION */
1328 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
1329 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
1331 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1332 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
1334 #endif /* HAVE_PCAP_LIB_VERSION */
1335 (void)fprintf(stderr
,
1336 "Usage: %s [-aAd" D_FLAG
"eflLnNOpqRStu" U_FLAG
"vxX]" B_FLAG_USAGE
" [-c count] [ -C file_size ]\n", program_name
);
1337 (void)fprintf(stderr
,
1338 "\t\t[ -E algo:secret ] [ -F file ] [ -i interface ] [ -M secret ]\n");
1339 (void)fprintf(stderr
,
1340 "\t\t[ -r file ] [ -s snaplen ] [ -T type ] [ -w file ]\n");
1341 (void)fprintf(stderr
,
1342 "\t\t[ -W filecount ] [ -y datalinktype ] [ -Z user ]\n");
1343 (void)fprintf(stderr
,
1344 "\t\t[ expression ]\n");
1352 ndo_error(netdissect_options
*ndo _U_
, const char *fmt
, ...)
1356 (void)fprintf(stderr
, "%s: ", program_name
);
1358 (void)vfprintf(stderr
, fmt
, ap
);
1362 if (fmt
[-1] != '\n')
1363 (void)fputc('\n', stderr
);
1371 ndo_warning(netdissect_options
*ndo _U_
, const char *fmt
, ...)
1375 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
1377 (void)vfprintf(stderr
, fmt
, ap
);
1381 if (fmt
[-1] != '\n')
1382 (void)fputc('\n', stderr
);