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.253.2.13 2007/09/12 19:48:51 guy Exp $ (LBL)";
39 * tcpdump - monitor tcp/ip traffic on an ethernet.
41 * First written in 1987 by Van Jacobson, Lawrence Berkeley Laboratory.
42 * Mercilessly hacked and occasionally improved since then via the
43 * combined efforts of Van, Steve McCanne and Craig Leres of LBL.
50 #include <tcpdump-stdinc.h>
55 extern int strcasecmp (const char *__s1, const char *__s2);
76 #include "netdissect.h"
77 #include "interface.h"
78 #include "addrtoname.h"
80 #include "setsignal.h"
81 #include "gmt2local.h"
82 #include "pcap-missing.h"
84 netdissect_options Gndo;
85 netdissect_options *gndo = &Gndo;
88 * Define the maximum number of files for the -C flag, and how many
89 * characters can be added to a filename for the -C flag (which
90 * should be enough to handle MAX_CFLAG - 1).
92 #define MAX_CFLAG 1000000
93 #define MAX_CFLAG_CHARS 6
95 int dflag; /* print filter code */
96 int Lflag; /* list available data link types and exit */
103 int32_t thiszone; /* seconds offset from gmt to local time */
106 static RETSIGTYPE cleanup(int);
107 static void usage(void) __attribute__((noreturn));
108 static void show_dlts_and_exit(pcap_t *pd) __attribute__((noreturn));
110 static void print_packet(u_char *, const struct pcap_pkthdr *, const u_char *);
111 static void ndo_default_print(netdissect_options *, const u_char *, u_int);
112 static void dump_packet_and_trunc(u_char *, const struct pcap_pkthdr *, const u_char *);
113 static void dump_packet(u_char *, const struct pcap_pkthdr *, const u_char *);
114 static void droproot(const char *, const char *);
115 static void ndo_error(netdissect_options *ndo, const char *fmt, ...);
116 static void ndo_warning(netdissect_options *ndo, const char *fmt, ...);
119 RETSIGTYPE requestinfo(int);
122 #if defined(USE_WIN32_MM_TIMER)
123 #include <MMsystem.h>
124 static UINT timer_id;
125 static void CALLBACK verbose_stats_dump(UINT, UINT, DWORD_PTR, DWORD_PTR, DWORD_PTR);
126 #elif defined(HAVE_ALARM)
127 static void verbose_stats_dump(int sig);
130 static void info(int);
131 static u_int packets_captured;
133 typedef u_int (*if_printer)(const struct pcap_pkthdr *, const u_char *);
140 static struct printer printers[] = {
141 { arcnet_if_print, DLT_ARCNET },
142 #ifdef DLT_ARCNET_LINUX
143 { arcnet_linux_if_print, DLT_ARCNET_LINUX },
145 { ether_if_print, DLT_EN10MB },
146 { token_if_print, DLT_IEEE802 },
148 { lane_if_print, DLT_LANE8023 },
151 { cip_if_print, DLT_CIP },
154 { cip_if_print, DLT_ATM_CLIP },
156 { sl_if_print, DLT_SLIP },
157 #ifdef DLT_SLIP_BSDOS
158 { sl_bsdos_if_print, DLT_SLIP_BSDOS },
160 { ppp_if_print, DLT_PPP },
161 #ifdef DLT_PPP_WITHDIRECTION
162 { ppp_if_print, DLT_PPP_WITHDIRECTION },
165 { ppp_bsdos_if_print, DLT_PPP_BSDOS },
167 { fddi_if_print, DLT_FDDI },
168 { null_if_print, DLT_NULL },
170 { null_if_print, DLT_LOOP },
172 { raw_if_print, DLT_RAW },
173 { atm_if_print, DLT_ATM_RFC1483 },
175 { chdlc_if_print, DLT_C_HDLC },
178 { chdlc_if_print, DLT_HDLC },
180 #ifdef DLT_PPP_SERIAL
181 { ppp_hdlc_if_print, DLT_PPP_SERIAL },
184 { pppoe_if_print, DLT_PPP_ETHER },
187 { sll_if_print, DLT_LINUX_SLL },
189 #ifdef DLT_IEEE802_11
190 { ieee802_11_if_print, DLT_IEEE802_11},
193 { ltalk_if_print, DLT_LTALK },
195 #if defined(DLT_PFLOG) && defined(HAVE_NET_PFVAR_H)
196 { pflog_if_print, DLT_PFLOG },
199 { fr_if_print, DLT_FR },
202 { fr_if_print, DLT_FRELAY },
205 { sunatm_if_print, DLT_SUNATM },
207 #ifdef DLT_IP_OVER_FC
208 { ipfc_if_print, DLT_IP_OVER_FC },
210 #ifdef DLT_PRISM_HEADER
211 { prism_if_print, DLT_PRISM_HEADER },
213 #ifdef DLT_IEEE802_11_RADIO
214 { ieee802_11_radio_if_print, DLT_IEEE802_11_RADIO },
217 { enc_if_print, DLT_ENC },
219 #ifdef DLT_SYMANTEC_FIREWALL
220 { symantec_if_print, DLT_SYMANTEC_FIREWALL },
222 #ifdef DLT_APPLE_IP_OVER_IEEE1394
223 { ap1394_if_print, DLT_APPLE_IP_OVER_IEEE1394 },
225 #ifdef DLT_JUNIPER_ATM1
226 { juniper_atm1_print, DLT_JUNIPER_ATM1 },
228 #ifdef DLT_JUNIPER_ATM2
229 { juniper_atm2_print, DLT_JUNIPER_ATM2 },
231 #ifdef DLT_JUNIPER_MFR
232 { juniper_mfr_print, DLT_JUNIPER_MFR },
234 #ifdef DLT_JUNIPER_MLFR
235 { juniper_mlfr_print, DLT_JUNIPER_MLFR },
237 #ifdef DLT_JUNIPER_MLPPP
238 { juniper_mlppp_print, DLT_JUNIPER_MLPPP },
240 #ifdef DLT_JUNIPER_PPPOE
241 { juniper_pppoe_print, DLT_JUNIPER_PPPOE },
243 #ifdef DLT_JUNIPER_PPPOE_ATM
244 { juniper_pppoe_atm_print, DLT_JUNIPER_PPPOE_ATM },
246 #ifdef DLT_JUNIPER_GGSN
247 { juniper_ggsn_print, DLT_JUNIPER_GGSN },
249 #ifdef DLT_JUNIPER_ES
250 { juniper_es_print, DLT_JUNIPER_ES },
252 #ifdef DLT_JUNIPER_MONITOR
253 { juniper_monitor_print, DLT_JUNIPER_MONITOR },
255 #ifdef DLT_JUNIPER_SERVICES
256 { juniper_services_print, DLT_JUNIPER_SERVICES },
258 #ifdef DLT_JUNIPER_ETHER
259 { juniper_ether_print, DLT_JUNIPER_ETHER },
261 #ifdef DLT_JUNIPER_PPP
262 { juniper_ppp_print, DLT_JUNIPER_PPP },
264 #ifdef DLT_JUNIPER_FRELAY
265 { juniper_frelay_print, DLT_JUNIPER_FRELAY },
267 #ifdef DLT_JUNIPER_CHDLC
268 { juniper_chdlc_print, DLT_JUNIPER_CHDLC },
271 { mfr_if_print, DLT_MFR },
277 lookup_printer(int type)
281 for (p = printers; p->f; ++p)
306 show_dlts_and_exit(pcap_t *pd)
310 const char *dlt_name;
312 n_dlts = pcap_list_datalinks(pd, &dlts);
314 error("%s", pcap_geterr(pd));
315 else if (n_dlts == 0 || !dlts)
316 error("No data link types.");
318 (void) fprintf(stderr, "Data link types (use option -y to set):\n");
320 while (--n_dlts >= 0) {
321 dlt_name = pcap_datalink_val_to_name(dlts[n_dlts]);
322 if (dlt_name != NULL) {
323 (void) fprintf(stderr, " %s (%s)", dlt_name,
324 pcap_datalink_val_to_description(dlts[n_dlts]));
327 * OK, does tcpdump handle that type?
329 if (lookup_printer(dlts[n_dlts]) == NULL)
330 (void) fprintf(stderr, " (not supported)");
333 (void) fprintf(stderr, " DLT %d (not supported)\n",
342 * Set up flags that might or might not be supported depending on the
343 * version of libpcap we're using.
347 #define B_FLAG_USAGE " [ -B size ]"
353 #ifdef HAVE_PCAP_FINDALLDEVS
354 #ifndef HAVE_PCAP_IF_T
355 #undef HAVE_PCAP_FINDALLDEVS
359 #ifdef HAVE_PCAP_FINDALLDEVS
365 #ifdef HAVE_PCAP_DUMP_FLUSH
372 /* Drop root privileges and chroot if necessary */
374 droproot(const char *username, const char *chroot_dir)
376 struct passwd *pw = NULL;
378 if (chroot_dir && !username) {
379 fprintf(stderr, "tcpdump: Chroot without dropping root is insecure\n");
383 pw = getpwnam(username);
386 if (chroot(chroot_dir) != 0 || chdir ("/") != 0) {
387 fprintf(stderr, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
388 chroot_dir, pcap_strerror(errno));
392 if (initgroups(pw->pw_name, pw->pw_gid) != 0 ||
393 setgid(pw->pw_gid) != 0 || setuid(pw->pw_uid) != 0) {
394 fprintf(stderr, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
396 (unsigned long)pw->pw_uid,
397 (unsigned long)pw->pw_gid,
398 pcap_strerror(errno));
403 fprintf(stderr, "tcpdump: Couldn't find user '%.32s'\n",
426 MakeFilename(char *buffer, char *orig_name, int cnt, int max_chars)
428 if (cnt == 0 && max_chars == 0)
429 strcpy(buffer, orig_name);
431 sprintf(buffer, "%s%0*d", orig_name, max_chars, cnt);
434 static int tcpdump_printf(netdissect_options *ndo _U_,
435 const char *fmt, ...)
442 ret=vfprintf(stdout, fmt, args);
449 main(int argc, char **argv)
451 register int cnt, op, i;
452 bpf_u_int32 localnet, netmask;
453 register char *cp, *infile, *cmdbuf, *device, *RFileName, *WFileName, *WFileNameAlt;
454 pcap_handler callback;
456 struct bpf_program fcode;
458 RETSIGTYPE (*oldhandler)(int);
460 struct print_info printinfo;
461 struct dump_info dumpinfo;
462 u_char *pcap_userdata;
463 char ebuf[PCAP_ERRBUF_SIZE];
464 char *username = NULL;
465 char *chroot_dir = NULL;
466 #ifdef HAVE_PCAP_FINDALLDEVS
467 pcap_if_t *devpointer;
472 u_int UserBufferSize = 1000000;
473 if(wsockinit() != 0) return 1;
479 gndo->ndo_default_print=ndo_default_print;
480 gndo->ndo_printf=tcpdump_printf;
481 gndo->ndo_error=ndo_error;
482 gndo->ndo_warning=ndo_warning;
483 gndo->ndo_snaplen = DEFAULT_SNAPLEN;
490 if ((cp = strrchr(argv[0], '/')) != NULL)
491 program_name = cp + 1;
493 program_name = argv[0];
495 if (abort_on_misalignment(ebuf, sizeof(ebuf)) < 0)
504 (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)
508 /* compatibility for old -a */
517 UserBufferSize = atoi(optarg)*1024;
518 if (UserBufferSize < 0)
519 error("invalid packet buffer size %s", optarg);
526 error("invalid packet count %s", optarg);
530 Cflag = atoi(optarg) * 1000000;
532 error("invalid file size %s", optarg);
539 #ifdef HAVE_PCAP_FINDALLDEVS
541 if (pcap_findalldevs(&devpointer, ebuf) < 0)
544 for (i = 0; devpointer != 0; i++) {
545 printf("%d.%s", i+1, devpointer->name);
546 if (devpointer->description != NULL)
547 printf(" (%s)", devpointer->description);
549 devpointer = devpointer->next;
553 #endif /* HAVE_PCAP_FINDALLDEVS */
564 #ifndef HAVE_LIBCRYPTO
565 warning("crypto code not compiled in");
567 gndo->ndo_espsecret = optarg;
579 if (optarg[0] == '0' && optarg[1] == 0)
580 error("Invalid adapter index");
582 #ifdef HAVE_PCAP_FINDALLDEVS
584 * If the argument is a number, treat it as
585 * an index into the list of adapters, as
586 * printed by "tcpdump -D".
588 * This should be OK on UNIX systems, as interfaces
589 * shouldn't have names that begin with digits.
590 * It can be useful on Windows, where more than
591 * one interface can have the same name.
593 if ((devnum = atoi(optarg)) != 0) {
595 error("Invalid adapter index");
597 if (pcap_findalldevs(&devpointer, ebuf) < 0)
600 for (i = 0; i < devnum-1; i++){
601 devpointer = devpointer->next;
602 if (devpointer == NULL)
603 error("Invalid adapter index");
606 device = devpointer->name;
609 #endif /* HAVE_PCAP_FINDALLDEVS */
616 * _IOLBF is the same as _IOFBF in Microsoft's C
617 * libraries; the only alternative they offer
620 * XXX - this should really be checking for MSVC++,
621 * not WIN32, if, for example, MinGW has its own
622 * C library that is more UNIX-compatible.
624 setvbuf(stdout, NULL, _IONBF, 0);
626 #ifdef HAVE_SETLINEBUF
629 setvbuf(stdout, NULL, _IOLBF, 0);
644 if (smiLoadModule(optarg) == 0) {
645 error("could not load MIB module %s", optarg);
649 (void)fprintf(stderr, "%s: ignoring option `-m %s' ",
650 program_name, optarg);
651 (void)fprintf(stderr, "(no libsmi support)\n");
656 /* TCP-MD5 shared secret */
657 #ifndef HAVE_LIBCRYPTO
658 warning("crypto code not compiled in");
660 tcpmd5secret = optarg;
673 ++suppress_default_print;
687 snaplen = strtol(optarg, &end, 0);
688 if (optarg == end || *end != '\0'
689 || snaplen < 0 || snaplen > 65535)
690 error("invalid snaplen %s", optarg);
691 else if (snaplen == 0)
705 if (strcasecmp(optarg, "vat") == 0)
707 else if (strcasecmp(optarg, "wb") == 0)
709 else if (strcasecmp(optarg, "rpc") == 0)
711 else if (strcasecmp(optarg, "rtp") == 0)
713 else if (strcasecmp(optarg, "rtcp") == 0)
714 packettype = PT_RTCP;
715 else if (strcasecmp(optarg, "snmp") == 0)
716 packettype = PT_SNMP;
717 else if (strcasecmp(optarg, "cnfp") == 0)
718 packettype = PT_CNFP;
719 else if (strcasecmp(optarg, "tftp") == 0)
720 packettype = PT_TFTP;
721 else if (strcasecmp(optarg, "aodv") == 0)
722 packettype = PT_AODV;
724 error("unknown packet type `%s'", optarg);
731 #ifdef HAVE_PCAP_DUMP_FLUSH
746 Wflag = atoi(optarg);
748 error("invalid number of output files %s", optarg);
749 WflagChars = getWflagChars(Wflag);
754 ++suppress_default_print;
759 ++suppress_default_print;
763 gndo->ndo_dltname = optarg;
765 pcap_datalink_name_to_val(gndo->ndo_dltname);
766 if (gndo->ndo_dlt < 0)
767 error("invalid data link type %s", gndo->ndo_dltname);
770 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
773 /* Undocumented flag */
774 #ifdef HAVE_PCAP_DEBUG
775 extern int pcap_debug;
786 username = strdup(optarg);
801 case 0: /* Default */
802 case 4: /* Default + Date*/
803 thiszone = gmt2local(0);
806 case 1: /* No time stamp */
807 case 2: /* Unix timeval style */
808 case 3: /* Microseconds since previous packet */
811 default: /* Not supported */
812 error("only -t, -tt, -ttt, and -tttt are supported");
817 /* if run as root, prepare for chrooting */
818 if (getuid() == 0 || geteuid() == 0) {
819 /* future extensibility for cmd-line arguments */
821 chroot_dir = WITH_CHROOT;
826 /* if run as root, prepare for dropping root privileges */
827 if (getuid() == 0 || geteuid() == 0) {
828 /* Run with '-Z root' to restore old behaviour */
830 username = WITH_USER;
834 if (RFileName != NULL) {
836 const char *dlt_name;
840 * We don't need network access, so relinquish any set-UID
841 * or set-GID privileges we have (if any).
843 * We do *not* want set-UID privileges when opening a
844 * trace file, as that might let the user read other
845 * people's trace files (especially if we're set-UID
848 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
849 fprintf(stderr, "Warning: setgid/setuid failed !\n");
851 pd = pcap_open_offline(RFileName, ebuf);
854 dlt = pcap_datalink(pd);
855 dlt_name = pcap_datalink_val_to_name(dlt);
856 if (dlt_name == NULL) {
857 fprintf(stderr, "reading from file %s, link-type %u\n",
861 "reading from file %s, link-type %s (%s)\n",
863 pcap_datalink_val_to_description(dlt));
868 error("-f and -r options are incompatible");
870 if (device == NULL) {
871 device = pcap_lookupdev(ebuf);
876 if(strlen(device) == 1) //we assume that an ASCII string is always longer than 1 char
877 { //a Unicode string has a \0 as second byte (so strlen() is 1)
878 fprintf(stderr, "%s: listening on %ws\n", program_name, device);
882 fprintf(stderr, "%s: listening on %s\n", program_name, device);
888 pd = pcap_open_live(device, snaplen, !pflag, 1000, ebuf);
894 * Let user own process after socket has been opened.
897 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
898 fprintf(stderr, "Warning: setgid/setuid failed !\n");
901 if(UserBufferSize != 1000000)
902 if(pcap_setbuff(pd, UserBufferSize)==-1){
903 error("%s", pcap_geterr(pd));
907 show_dlts_and_exit(pd);
908 if (gndo->ndo_dlt >= 0) {
909 #ifdef HAVE_PCAP_SET_DATALINK
910 if (pcap_set_datalink(pd, gndo->ndo_dlt) < 0)
911 error("%s", pcap_geterr(pd));
914 * We don't actually support changing the
915 * data link type, so we only let them
916 * set it to what it already is.
918 if (gndo->ndo_dlt != pcap_datalink(pd)) {
919 error("%s is not one of the DLTs supported by this device\n",
923 (void)fprintf(stderr, "%s: data link type %s\n",
924 program_name, gndo->ndo_dltname);
925 (void)fflush(stderr);
927 i = pcap_snapshot(pd);
929 warning("snaplen raised from %d to %d", snaplen, i);
932 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
939 cmdbuf = read_infile(infile);
941 cmdbuf = copy_argv(&argv[optind]);
943 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
944 error("%s", pcap_geterr(pd));
946 bpf_dump(&fcode, dflag);
950 init_addrtoname(localnet, netmask);
953 (void)setsignal(SIGPIPE, cleanup);
955 (void)setsignal(SIGTERM, cleanup);
956 (void)setsignal(SIGINT, cleanup);
957 /* Cooperate with nohup(1) */
959 if ((oldhandler = setsignal(SIGHUP, cleanup)) != SIG_DFL)
960 (void)setsignal(SIGHUP, oldhandler);
963 if (pcap_setfilter(pd, &fcode) < 0)
964 error("%s", pcap_geterr(pd));
968 WFileNameAlt = (char *)malloc(strlen(WFileName) + MAX_CFLAG_CHARS + 1);
969 if (WFileNameAlt == NULL)
970 error("malloc of WFileNameAlt");
971 MakeFilename(WFileNameAlt, WFileName, 0, WflagChars);
972 p = pcap_dump_open(pd, WFileNameAlt);
974 error("%s", pcap_geterr(pd));
976 callback = dump_packet_and_trunc;
977 dumpinfo.WFileName = WFileName;
980 pcap_userdata = (u_char *)&dumpinfo;
982 callback = dump_packet;
983 pcap_userdata = (u_char *)p;
986 type = pcap_datalink(pd);
987 printinfo.printer = lookup_printer(type);
988 if (printinfo.printer == NULL) {
989 gndo->ndo_dltname = pcap_datalink_val_to_name(type);
990 if (gndo->ndo_dltname != NULL)
991 error("unsupported data link type %s",
994 error("unsupported data link type %d", type);
996 callback = print_packet;
997 pcap_userdata = (u_char *)&printinfo;
1001 * We cannot do this earlier, because we want to be able to open
1002 * the file (if done) for writing before giving up permissions.
1004 if (getuid() == 0 || geteuid() == 0) {
1005 if (username || chroot_dir)
1006 droproot(username, chroot_dir);
1010 (void)setsignal(SIGINFO, requestinfo);
1013 if (vflag > 0 && WFileName) {
1015 * When capturing to a file, "-v" means tcpdump should,
1016 * every 10 secodns, "v"erbosely report the number of
1019 #ifdef USE_WIN32_MM_TIMER
1020 /* call verbose_stats_dump() each 1000 +/-100msec */
1021 timer_id = timeSetEvent(1000, 100, verbose_stats_dump, 0, TIME_PERIODIC);
1022 setvbuf(stderr, NULL, _IONBF, 0);
1023 #elif defined(HAVE_ALARM)
1024 (void)setsignal(SIGALRM, verbose_stats_dump);
1030 if (RFileName == NULL) {
1032 const char *dlt_name;
1034 if (!vflag && !WFileName) {
1035 (void)fprintf(stderr,
1036 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1039 (void)fprintf(stderr, "%s: ", program_name);
1040 dlt = pcap_datalink(pd);
1041 dlt_name = pcap_datalink_val_to_name(dlt);
1042 if (dlt_name == NULL) {
1043 (void)fprintf(stderr, "listening on %s, link-type %u, capture size %u bytes\n",
1044 device, dlt, snaplen);
1046 (void)fprintf(stderr, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1048 pcap_datalink_val_to_description(dlt), snaplen);
1050 (void)fflush(stderr);
1053 status = pcap_loop(pd, cnt, callback, pcap_userdata);
1054 if (WFileName == NULL) {
1056 * We're printing packets. Flush the printed output,
1057 * so it doesn't get intermingled with error output.
1061 * We got interrupted, so perhaps we didn't
1062 * manage to finish a line we were printing.
1063 * Print an extra newline, just in case.
1067 (void)fflush(stdout);
1073 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
1074 program_name, pcap_geterr(pd));
1076 if (RFileName == NULL) {
1078 * We're doing a live capture. Report the capture
1084 exit(status == -1 ? 1 : 0);
1087 /* make a clean exit on interrupts */
1089 cleanup(int signo _U_)
1091 #ifdef USE_WIN32_MM_TIMER
1093 timeKillEvent(timer_id);
1095 #elif defined(HAVE_ALARM)
1099 #ifdef HAVE_PCAP_BREAKLOOP
1101 * We have "pcap_breakloop()"; use it, so that we do as little
1102 * as possible in the signal handler (it's probably not safe
1103 * to do anything with standard I/O streams in a signal handler -
1104 * the ANSI C standard doesn't say it is).
1109 * We don't have "pcap_breakloop()"; this isn't safe, but
1110 * it's the best we can do. Print the summary if we're
1111 * not reading from a savefile - i.e., if we're doing a
1112 * live capture - and exit.
1114 if (pd != NULL && pcap_file(pd) == NULL) {
1116 * We got interrupted, so perhaps we didn't
1117 * manage to finish a line we were printing.
1118 * Print an extra newline, just in case.
1121 (void)fflush(stdout);
1129 info(register int verbose)
1131 struct pcap_stat stat;
1133 if (pcap_stats(pd, &stat) < 0) {
1134 (void)fprintf(stderr, "pcap_stats: %s\n", pcap_geterr(pd));
1140 fprintf(stderr, "%s: ", program_name);
1142 (void)fprintf(stderr, "%u packets captured", packets_captured);
1144 fputs(", ", stderr);
1147 (void)fprintf(stderr, "%d packets received by filter", stat.ps_recv);
1149 fputs(", ", stderr);
1152 (void)fprintf(stderr, "%d packets dropped by kernel\n", stat.ps_drop);
1157 dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
1159 struct dump_info *dump_info;
1166 dump_info = (struct dump_info *)user;
1169 * XXX - this won't prevent capture files from getting
1170 * larger than Cflag - the last packet written to the
1171 * file could put it over Cflag.
1173 if (pcap_dump_ftell(dump_info->p) > Cflag) {
1175 * Close the current file and open a new one.
1177 pcap_dump_close(dump_info->p);
1180 if (Cflag_count >= Wflag)
1183 if (Cflag_count >= MAX_CFLAG)
1184 error("too many output files");
1186 name = (char *)malloc(strlen(dump_info->WFileName) + MAX_CFLAG_CHARS + 1);
1188 error("dump_packet_and_trunc: malloc");
1189 MakeFilename(name, dump_info->WFileName, Cflag_count, WflagChars);
1190 dump_info->p = pcap_dump_open(dump_info->pd, name);
1192 if (dump_info->p == NULL)
1193 error("%s", pcap_geterr(pd));
1196 pcap_dump((u_char *)dump_info->p, h, sp);
1197 #ifdef HAVE_PCAP_DUMP_FLUSH
1199 pcap_dump_flush(dump_info->p);
1208 dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
1214 pcap_dump(user, h, sp);
1215 #ifdef HAVE_PCAP_DUMP_FLUSH
1217 pcap_dump_flush((pcap_dumper_t *)user);
1226 print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
1228 struct print_info *print_info;
1236 print_info = (struct print_info *)user;
1239 * Some printers want to check that they're not walking off the
1240 * end of the packet.
1241 * Rather than pass it all the way down, we set this global.
1243 snapend = sp + h->caplen;
1245 hdrlen = (*print_info->printer)(h, sp);
1248 * Print the raw packet data in hex and ASCII.
1252 * Include the link-layer header.
1254 hex_and_ascii_print("\n\t", sp, h->caplen);
1257 * Don't include the link-layer header - and if
1258 * we have nothing past the link-layer header,
1261 if (h->caplen > hdrlen)
1262 hex_and_ascii_print("\n\t", sp + hdrlen,
1263 h->caplen - hdrlen);
1267 * Print the raw packet data in hex.
1271 * Include the link-layer header.
1273 hex_print("\n\t", sp, h->caplen);
1276 * Don't include the link-layer header - and if
1277 * we have nothing past the link-layer header,
1280 if (h->caplen > hdrlen)
1281 hex_print("\n\t", sp + hdrlen,
1282 h->caplen - hdrlen);
1286 * Print the raw packet data in ASCII.
1290 * Include the link-layer header.
1292 ascii_print(sp, h->caplen);
1295 * Don't include the link-layer header - and if
1296 * we have nothing past the link-layer header,
1299 if (h->caplen > hdrlen)
1300 ascii_print(sp + hdrlen, h->caplen - hdrlen);
1313 * XXX - there should really be libpcap calls to get the version
1314 * number as a string (the string would be generated from #defines
1315 * at run time, so that it's not generated from string constants
1316 * in the library, as, on many UNIX systems, those constants would
1317 * be statically linked into the application executable image, and
1318 * would thus reflect the version of libpcap on the system on
1319 * which the application was *linked*, not the system on which it's
1322 * That routine should be documented, unlike the "version[]"
1323 * string, so that UNIX vendors providing their own libpcaps
1324 * don't omit it (as a couple of vendors have...).
1326 * Packet.dll should perhaps also export a routine to return the
1327 * version number of the Packet.dll code, to supply the
1328 * "Wpcap_version" information on Windows.
1330 char WDversion[]="current-cvs.tcpdump.org";
1331 #if !defined(HAVE_GENERATED_VERSION)
1332 char version[]="current-cvs.tcpdump.org";
1334 char pcap_version[]="current-cvs.tcpdump.org";
1335 char Wpcap_version[]="3.1";
1339 * By default, print the specified data out in hex and ASCII.
1342 ndo_default_print(netdissect_options *ndo _U_, const u_char *bp, u_int length)
1344 hex_and_ascii_print("\n\t", bp, length); /* pass on lf and identation string */
1348 default_print(const u_char *bp, u_int length)
1350 ndo_default_print(gndo, bp, length);
1354 RETSIGTYPE requestinfo(int signo _U_)
1364 * Called once each second in verbose mode while dumping to file
1366 #ifdef USE_WIN32_MM_TIMER
1367 void CALLBACK verbose_stats_dump (UINT timer_id _U_, UINT msg _U_, DWORD_PTR arg _U_,
1368 DWORD_PTR dw1 _U_, DWORD_PTR dw2 _U_)
1370 struct pcap_stat stat;
1372 if (infodelay == 0 && pcap_stats(pd, &stat) >= 0)
1373 fprintf(stderr, "Got %u\r", packets_captured);
1375 #elif defined(HAVE_ALARM)
1376 static void verbose_stats_dump(int sig _U_)
1378 struct pcap_stat stat;
1380 if (infodelay == 0 && pcap_stats(pd, &stat) >= 0)
1381 fprintf(stderr, "Got %u\r", packets_captured);
1389 extern char version[];
1390 #ifndef HAVE_PCAP_LIB_VERSION
1391 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
1392 extern char pcap_version[];
1393 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1394 static char pcap_version[] = "unknown";
1395 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1396 #endif /* HAVE_PCAP_LIB_VERSION */
1398 #ifdef HAVE_PCAP_LIB_VERSION
1400 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
1402 (void)fprintf(stderr, "%s version %s\n", program_name, version);
1404 (void)fprintf(stderr, "%s\n",pcap_lib_version());
1405 #else /* HAVE_PCAP_LIB_VERSION */
1407 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
1408 (void)fprintf(stderr, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version, pcap_version);
1410 (void)fprintf(stderr, "%s version %s\n", program_name, version);
1411 (void)fprintf(stderr, "libpcap version %s\n", pcap_version);
1413 #endif /* HAVE_PCAP_LIB_VERSION */
1414 (void)fprintf(stderr,
1415 "Usage: %s [-aAd" D_FLAG "eflLnNOpqRStu" U_FLAG "vxX]" B_FLAG_USAGE " [-c count] [ -C file_size ]\n", program_name);
1416 (void)fprintf(stderr,
1417 "\t\t[ -E algo:secret ] [ -F file ] [ -i interface ] [ -M secret ]\n");
1418 (void)fprintf(stderr,
1419 "\t\t[ -r file ] [ -s snaplen ] [ -T type ] [ -w file ]\n");
1420 (void)fprintf(stderr,
1421 "\t\t[ -W filecount ] [ -y datalinktype ] [ -Z user ]\n");
1422 (void)fprintf(stderr,
1423 "\t\t[ expression ]\n");
1431 ndo_error(netdissect_options *ndo _U_, const char *fmt, ...)
1435 (void)fprintf(stderr, "%s: ", program_name);
1437 (void)vfprintf(stderr, fmt, ap);
1441 if (fmt[-1] != '\n')
1442 (void)fputc('\n', stderr);
1450 ndo_warning(netdissect_options *ndo _U_, const char *fmt, ...)
1454 (void)fprintf(stderr, "%s: WARNING: ", program_name);
1456 (void)vfprintf(stderr, fmt, ap);
1460 if (fmt[-1] != '\n')
1461 (void)fputc('\n', stderr);