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.11 2005/08/23 10:29:41 hannes 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 },
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 },
274 lookup_printer(int type)
278 for (p = printers; p->f; ++p)
303 show_dlts_and_exit(pcap_t *pd)
307 const char *dlt_name;
309 n_dlts = pcap_list_datalinks(pd, &dlts);
311 error("%s", pcap_geterr(pd));
312 else if (n_dlts == 0 || !dlts)
313 error("No data link types.");
315 (void) fprintf(stderr, "Data link types (use option -y to set):\n");
317 while (--n_dlts >= 0) {
318 dlt_name = pcap_datalink_val_to_name(dlts[n_dlts]);
319 if (dlt_name != NULL) {
320 (void) fprintf(stderr, " %s (%s)", dlt_name,
321 pcap_datalink_val_to_description(dlts[n_dlts]));
324 * OK, does tcpdump handle that type?
326 if (lookup_printer(dlts[n_dlts]) == NULL)
327 (void) fprintf(stderr, " (not supported)");
330 (void) fprintf(stderr, " DLT %d (not supported)\n",
339 * Set up flags that might or might not be supported depending on the
340 * version of libpcap we're using.
344 #define B_FLAG_USAGE " [ -B size ]"
350 #ifdef HAVE_PCAP_FINDALLDEVS
351 #ifndef HAVE_PCAP_IF_T
352 #undef HAVE_PCAP_FINDALLDEVS
356 #ifdef HAVE_PCAP_FINDALLDEVS
362 #ifdef HAVE_PCAP_DUMP_FLUSH
369 /* Drop root privileges and chroot if necessary */
371 droproot(const char *username, const char *chroot_dir)
373 struct passwd *pw = NULL;
375 if (chroot_dir && !username) {
376 fprintf(stderr, "tcpdump: Chroot without dropping root is insecure\n");
380 pw = getpwnam(username);
383 if (chroot(chroot_dir) != 0 || chdir ("/") != 0) {
384 fprintf(stderr, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
385 chroot_dir, pcap_strerror(errno));
389 if (initgroups(pw->pw_name, pw->pw_gid) != 0 ||
390 setgid(pw->pw_gid) != 0 || setuid(pw->pw_uid) != 0) {
391 fprintf(stderr, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
393 (unsigned long)pw->pw_uid,
394 (unsigned long)pw->pw_gid,
395 pcap_strerror(errno));
400 fprintf(stderr, "tcpdump: Couldn't find user '%.32s'\n",
423 MakeFilename(char *buffer, char *orig_name, int cnt, int max_chars)
425 if (cnt == 0 && max_chars == 0)
426 strcpy(buffer, orig_name);
428 sprintf(buffer, "%s%0*d", orig_name, max_chars, cnt);
431 static int tcpdump_printf(netdissect_options *ndo _U_,
432 const char *fmt, ...)
439 ret=vfprintf(stdout, fmt, args);
446 main(int argc, char **argv)
448 register int cnt, op, i;
449 bpf_u_int32 localnet, netmask;
450 register char *cp, *infile, *cmdbuf, *device, *RFileName, *WFileName, *WFileNameAlt;
451 pcap_handler callback;
453 struct bpf_program fcode;
455 RETSIGTYPE (*oldhandler)(int);
457 struct print_info printinfo;
458 struct dump_info dumpinfo;
459 u_char *pcap_userdata;
460 char ebuf[PCAP_ERRBUF_SIZE];
461 char *username = NULL;
462 char *chroot_dir = NULL;
463 #ifdef HAVE_PCAP_FINDALLDEVS
464 pcap_if_t *devpointer;
469 u_int UserBufferSize = 1000000;
470 if(wsockinit() != 0) return 1;
476 gndo->ndo_default_print=ndo_default_print;
477 gndo->ndo_printf=tcpdump_printf;
478 gndo->ndo_error=ndo_error;
479 gndo->ndo_warning=ndo_warning;
480 gndo->ndo_snaplen = DEFAULT_SNAPLEN;
487 if ((cp = strrchr(argv[0], '/')) != NULL)
488 program_name = cp + 1;
490 program_name = argv[0];
492 if (abort_on_misalignment(ebuf, sizeof(ebuf)) < 0)
501 (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)
505 /* compatibility for old -a */
514 UserBufferSize = atoi(optarg)*1024;
515 if (UserBufferSize < 0)
516 error("invalid packet buffer size %s", optarg);
523 error("invalid packet count %s", optarg);
527 Cflag = atoi(optarg) * 1000000;
529 error("invalid file size %s", optarg);
536 #ifdef HAVE_PCAP_FINDALLDEVS
538 if (pcap_findalldevs(&devpointer, ebuf) < 0)
541 for (i = 0; devpointer != 0; i++) {
542 printf("%d.%s", i+1, devpointer->name);
543 if (devpointer->description != NULL)
544 printf(" (%s)", devpointer->description);
546 devpointer = devpointer->next;
550 #endif /* HAVE_PCAP_FINDALLDEVS */
561 #ifndef HAVE_LIBCRYPTO
562 warning("crypto code not compiled in");
564 gndo->ndo_espsecret = optarg;
576 if (optarg[0] == '0' && optarg[1] == 0)
577 error("Invalid adapter index");
579 #ifdef HAVE_PCAP_FINDALLDEVS
581 * If the argument is a number, treat it as
582 * an index into the list of adapters, as
583 * printed by "tcpdump -D".
585 * This should be OK on UNIX systems, as interfaces
586 * shouldn't have names that begin with digits.
587 * It can be useful on Windows, where more than
588 * one interface can have the same name.
590 if ((devnum = atoi(optarg)) != 0) {
592 error("Invalid adapter index");
594 if (pcap_findalldevs(&devpointer, ebuf) < 0)
597 for (i = 0; i < devnum-1; i++){
598 devpointer = devpointer->next;
599 if (devpointer == NULL)
600 error("Invalid adapter index");
603 device = devpointer->name;
606 #endif /* HAVE_PCAP_FINDALLDEVS */
613 * _IOLBF is the same as _IOFBF in Microsoft's C
614 * libraries; the only alternative they offer
617 * XXX - this should really be checking for MSVC++,
618 * not WIN32, if, for example, MinGW has its own
619 * C library that is more UNIX-compatible.
621 setvbuf(stdout, NULL, _IONBF, 0);
623 #ifdef HAVE_SETLINEBUF
626 setvbuf(stdout, NULL, _IOLBF, 0);
641 if (smiLoadModule(optarg) == 0) {
642 error("could not load MIB module %s", optarg);
646 (void)fprintf(stderr, "%s: ignoring option `-m %s' ",
647 program_name, optarg);
648 (void)fprintf(stderr, "(no libsmi support)\n");
653 /* TCP-MD5 shared secret */
654 #ifndef HAVE_LIBCRYPTO
655 warning("crypto code not compiled in");
657 tcpmd5secret = optarg;
670 ++suppress_default_print;
684 snaplen = strtol(optarg, &end, 0);
685 if (optarg == end || *end != '\0'
686 || snaplen < 0 || snaplen > 65535)
687 error("invalid snaplen %s", optarg);
688 else if (snaplen == 0)
702 if (strcasecmp(optarg, "vat") == 0)
704 else if (strcasecmp(optarg, "wb") == 0)
706 else if (strcasecmp(optarg, "rpc") == 0)
708 else if (strcasecmp(optarg, "rtp") == 0)
710 else if (strcasecmp(optarg, "rtcp") == 0)
711 packettype = PT_RTCP;
712 else if (strcasecmp(optarg, "snmp") == 0)
713 packettype = PT_SNMP;
714 else if (strcasecmp(optarg, "cnfp") == 0)
715 packettype = PT_CNFP;
716 else if (strcasecmp(optarg, "tftp") == 0)
717 packettype = PT_TFTP;
718 else if (strcasecmp(optarg, "aodv") == 0)
719 packettype = PT_AODV;
721 error("unknown packet type `%s'", optarg);
728 #ifdef HAVE_PCAP_DUMP_FLUSH
743 Wflag = atoi(optarg);
745 error("invalid number of output files %s", optarg);
746 WflagChars = getWflagChars(Wflag);
751 ++suppress_default_print;
756 ++suppress_default_print;
760 gndo->ndo_dltname = optarg;
762 pcap_datalink_name_to_val(gndo->ndo_dltname);
763 if (gndo->ndo_dlt < 0)
764 error("invalid data link type %s", gndo->ndo_dltname);
767 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
770 /* Undocumented flag */
771 #ifdef HAVE_PCAP_DEBUG
772 extern int pcap_debug;
783 username = strdup(optarg);
798 case 0: /* Default */
799 case 4: /* Default + Date*/
800 thiszone = gmt2local(0);
803 case 1: /* No time stamp */
804 case 2: /* Unix timeval style */
805 case 3: /* Microseconds since previous packet */
808 default: /* Not supported */
809 error("only -t, -tt, -ttt, and -tttt are supported");
814 /* if run as root, prepare for chrooting */
815 if (getuid() == 0 || geteuid() == 0) {
816 /* future extensibility for cmd-line arguments */
818 chroot_dir = WITH_CHROOT;
823 /* if run as root, prepare for dropping root privileges */
824 if (getuid() == 0 || geteuid() == 0) {
825 /* Run with '-Z root' to restore old behaviour */
827 username = WITH_USER;
831 if (RFileName != NULL) {
833 const char *dlt_name;
837 * We don't need network access, so relinquish any set-UID
838 * or set-GID privileges we have (if any).
840 * We do *not* want set-UID privileges when opening a
841 * trace file, as that might let the user read other
842 * people's trace files (especially if we're set-UID
845 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
846 fprintf(stderr, "Warning: setgid/setuid failed !\n");
848 pd = pcap_open_offline(RFileName, ebuf);
851 dlt = pcap_datalink(pd);
852 dlt_name = pcap_datalink_val_to_name(dlt);
853 if (dlt_name == NULL) {
854 fprintf(stderr, "reading from file %s, link-type %u\n",
858 "reading from file %s, link-type %s (%s)\n",
860 pcap_datalink_val_to_description(dlt));
865 error("-f and -r options are incompatible");
867 if (device == NULL) {
868 device = pcap_lookupdev(ebuf);
873 if(strlen(device) == 1) //we assume that an ASCII string is always longer than 1 char
874 { //a Unicode string has a \0 as second byte (so strlen() is 1)
875 fprintf(stderr, "%s: listening on %ws\n", program_name, device);
879 fprintf(stderr, "%s: listening on %s\n", program_name, device);
885 pd = pcap_open_live(device, snaplen, !pflag, 1000, ebuf);
891 * Let user own process after socket has been opened.
894 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
895 fprintf(stderr, "Warning: setgid/setuid failed !\n");
898 if(UserBufferSize != 1000000)
899 if(pcap_setbuff(pd, UserBufferSize)==-1){
900 error("%s", pcap_geterr(pd));
904 show_dlts_and_exit(pd);
905 if (gndo->ndo_dlt >= 0) {
906 #ifdef HAVE_PCAP_SET_DATALINK
907 if (pcap_set_datalink(pd, gndo->ndo_dlt) < 0)
908 error("%s", pcap_geterr(pd));
911 * We don't actually support changing the
912 * data link type, so we only let them
913 * set it to what it already is.
915 if (gndo->ndo_dlt != pcap_datalink(pd)) {
916 error("%s is not one of the DLTs supported by this device\n",
920 (void)fprintf(stderr, "%s: data link type %s\n",
921 program_name, gndo->ndo_dltname);
922 (void)fflush(stderr);
924 i = pcap_snapshot(pd);
926 warning("snaplen raised from %d to %d", snaplen, i);
929 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
936 cmdbuf = read_infile(infile);
938 cmdbuf = copy_argv(&argv[optind]);
940 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
941 error("%s", pcap_geterr(pd));
943 bpf_dump(&fcode, dflag);
947 init_addrtoname(localnet, netmask);
950 (void)setsignal(SIGPIPE, cleanup);
952 (void)setsignal(SIGTERM, cleanup);
953 (void)setsignal(SIGINT, cleanup);
954 /* Cooperate with nohup(1) */
956 if ((oldhandler = setsignal(SIGHUP, cleanup)) != SIG_DFL)
957 (void)setsignal(SIGHUP, oldhandler);
960 if (pcap_setfilter(pd, &fcode) < 0)
961 error("%s", pcap_geterr(pd));
965 WFileNameAlt = (char *)malloc(strlen(WFileName) + MAX_CFLAG_CHARS + 1);
966 if (WFileNameAlt == NULL)
967 error("malloc of WFileNameAlt");
968 MakeFilename(WFileNameAlt, WFileName, 0, WflagChars);
969 p = pcap_dump_open(pd, WFileNameAlt);
971 error("%s", pcap_geterr(pd));
973 callback = dump_packet_and_trunc;
974 dumpinfo.WFileName = WFileName;
977 pcap_userdata = (u_char *)&dumpinfo;
979 callback = dump_packet;
980 pcap_userdata = (u_char *)p;
983 type = pcap_datalink(pd);
984 printinfo.printer = lookup_printer(type);
985 if (printinfo.printer == NULL) {
986 gndo->ndo_dltname = pcap_datalink_val_to_name(type);
987 if (gndo->ndo_dltname != NULL)
988 error("unsupported data link type %s",
991 error("unsupported data link type %d", type);
993 callback = print_packet;
994 pcap_userdata = (u_char *)&printinfo;
998 * We cannot do this earlier, because we want to be able to open
999 * the file (if done) for writing before giving up permissions.
1001 if (getuid() == 0 || geteuid() == 0) {
1002 if (username || chroot_dir)
1003 droproot(username, chroot_dir);
1007 (void)setsignal(SIGINFO, requestinfo);
1010 if (vflag > 0 && WFileName) {
1012 * When capturing to a file, "-v" means tcpdump should,
1013 * every 10 secodns, "v"erbosely report the number of
1016 #ifdef USE_WIN32_MM_TIMER
1017 /* call verbose_stats_dump() each 1000 +/-100msec */
1018 timer_id = timeSetEvent(1000, 100, verbose_stats_dump, 0, TIME_PERIODIC);
1019 setvbuf(stderr, NULL, _IONBF, 0);
1020 #elif defined(HAVE_ALARM)
1021 (void)setsignal(SIGALRM, verbose_stats_dump);
1027 if (RFileName == NULL) {
1029 const char *dlt_name;
1031 if (!vflag && !WFileName) {
1032 (void)fprintf(stderr,
1033 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1036 (void)fprintf(stderr, "%s: ", program_name);
1037 dlt = pcap_datalink(pd);
1038 dlt_name = pcap_datalink_val_to_name(dlt);
1039 if (dlt_name == NULL) {
1040 (void)fprintf(stderr, "listening on %s, link-type %u, capture size %u bytes\n",
1041 device, dlt, snaplen);
1043 (void)fprintf(stderr, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1045 pcap_datalink_val_to_description(dlt), snaplen);
1047 (void)fflush(stderr);
1050 status = pcap_loop(pd, cnt, callback, pcap_userdata);
1051 if (WFileName == NULL) {
1053 * We're printing packets. Flush the printed output,
1054 * so it doesn't get intermingled with error output.
1058 * We got interrupted, so perhaps we didn't
1059 * manage to finish a line we were printing.
1060 * Print an extra newline, just in case.
1064 (void)fflush(stdout);
1070 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
1071 program_name, pcap_geterr(pd));
1073 if (RFileName == NULL) {
1075 * We're doing a live capture. Report the capture
1081 exit(status == -1 ? 1 : 0);
1084 /* make a clean exit on interrupts */
1086 cleanup(int signo _U_)
1088 #ifdef USE_WIN32_MM_TIMER
1090 timeKillEvent(timer_id);
1092 #elif defined(HAVE_ALARM)
1096 #ifdef HAVE_PCAP_BREAKLOOP
1098 * We have "pcap_breakloop()"; use it, so that we do as little
1099 * as possible in the signal handler (it's probably not safe
1100 * to do anything with standard I/O streams in a signal handler -
1101 * the ANSI C standard doesn't say it is).
1106 * We don't have "pcap_breakloop()"; this isn't safe, but
1107 * it's the best we can do. Print the summary if we're
1108 * not reading from a savefile - i.e., if we're doing a
1109 * live capture - and exit.
1111 if (pd != NULL && pcap_file(pd) == NULL) {
1113 * We got interrupted, so perhaps we didn't
1114 * manage to finish a line we were printing.
1115 * Print an extra newline, just in case.
1118 (void)fflush(stdout);
1126 info(register int verbose)
1128 struct pcap_stat stat;
1130 if (pcap_stats(pd, &stat) < 0) {
1131 (void)fprintf(stderr, "pcap_stats: %s\n", pcap_geterr(pd));
1136 fprintf(stderr, "%s: ", program_name);
1138 (void)fprintf(stderr, "%u packets captured", packets_captured);
1140 fputs(", ", stderr);
1143 (void)fprintf(stderr, "%d packets received by filter", stat.ps_recv);
1145 fputs(", ", stderr);
1148 (void)fprintf(stderr, "%d packets dropped by kernel\n", stat.ps_drop);
1153 dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
1155 struct dump_info *dump_info;
1162 dump_info = (struct dump_info *)user;
1165 * XXX - this won't prevent capture files from getting
1166 * larger than Cflag - the last packet written to the
1167 * file could put it over Cflag.
1169 if (pcap_dump_ftell(dump_info->p) > Cflag) {
1171 * Close the current file and open a new one.
1173 pcap_dump_close(dump_info->p);
1176 if (Cflag_count >= Wflag)
1179 if (Cflag_count >= MAX_CFLAG)
1180 error("too many output files");
1182 name = (char *)malloc(strlen(dump_info->WFileName) + MAX_CFLAG_CHARS + 1);
1184 error("dump_packet_and_trunc: malloc");
1185 MakeFilename(name, dump_info->WFileName, Cflag_count, WflagChars);
1186 dump_info->p = pcap_dump_open(dump_info->pd, name);
1188 if (dump_info->p == NULL)
1189 error("%s", pcap_geterr(pd));
1192 pcap_dump((u_char *)dump_info->p, h, sp);
1193 #ifdef HAVE_PCAP_DUMP_FLUSH
1195 pcap_dump_flush(dump_info->p);
1204 dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
1210 pcap_dump(user, h, sp);
1211 #ifdef HAVE_PCAP_DUMP_FLUSH
1213 pcap_dump_flush((pcap_dumper_t *)user);
1222 print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
1224 struct print_info *print_info;
1232 print_info = (struct print_info *)user;
1235 * Some printers want to check that they're not walking off the
1236 * end of the packet.
1237 * Rather than pass it all the way down, we set this global.
1239 snapend = sp + h->caplen;
1241 hdrlen = (*print_info->printer)(h, sp);
1244 * Print the raw packet data in hex and ASCII.
1248 * Include the link-layer header.
1250 hex_and_ascii_print("\n\t", sp, h->caplen);
1253 * Don't include the link-layer header - and if
1254 * we have nothing past the link-layer header,
1257 if (h->caplen > hdrlen)
1258 hex_and_ascii_print("\n\t", sp + hdrlen,
1259 h->caplen - hdrlen);
1263 * Print the raw packet data in hex.
1267 * Include the link-layer header.
1269 hex_print("\n\t", sp, h->caplen);
1272 * Don't include the link-layer header - and if
1273 * we have nothing past the link-layer header,
1276 if (h->caplen > hdrlen)
1277 hex_print("\n\t", sp + hdrlen,
1278 h->caplen - hdrlen);
1282 * Print the raw packet data in ASCII.
1286 * Include the link-layer header.
1288 ascii_print(sp, h->caplen);
1291 * Don't include the link-layer header - and if
1292 * we have nothing past the link-layer header,
1295 if (h->caplen > hdrlen)
1296 ascii_print(sp + hdrlen, h->caplen - hdrlen);
1309 * XXX - there should really be libpcap calls to get the version
1310 * number as a string (the string would be generated from #defines
1311 * at run time, so that it's not generated from string constants
1312 * in the library, as, on many UNIX systems, those constants would
1313 * be statically linked into the application executable image, and
1314 * would thus reflect the version of libpcap on the system on
1315 * which the application was *linked*, not the system on which it's
1318 * That routine should be documented, unlike the "version[]"
1319 * string, so that UNIX vendors providing their own libpcaps
1320 * don't omit it (as a couple of vendors have...).
1322 * Packet.dll should perhaps also export a routine to return the
1323 * version number of the Packet.dll code, to supply the
1324 * "Wpcap_version" information on Windows.
1326 char WDversion[]="current-cvs.tcpdump.org";
1327 #if !defined(HAVE_GENERATED_VERSION)
1328 char version[]="current-cvs.tcpdump.org";
1330 char pcap_version[]="current-cvs.tcpdump.org";
1331 char Wpcap_version[]="3.1";
1335 * By default, print the specified data out in hex and ASCII.
1338 ndo_default_print(netdissect_options *ndo _U_, const u_char *bp, u_int length)
1340 hex_and_ascii_print("\n\t", bp, length); /* pass on lf and identation string */
1344 default_print(const u_char *bp, u_int length)
1346 ndo_default_print(gndo, bp, length);
1350 RETSIGTYPE requestinfo(int signo _U_)
1360 * Called once each second in verbose mode while dumping to file
1362 #ifdef USE_WIN32_MM_TIMER
1363 void CALLBACK verbose_stats_dump (UINT timer_id _U_, UINT msg _U_, DWORD_PTR arg _U_,
1364 DWORD_PTR dw1 _U_, DWORD_PTR dw2 _U_)
1366 struct pcap_stat stat;
1368 if (infodelay == 0 && pcap_stats(pd, &stat) >= 0)
1369 fprintf(stderr, "Got %u\r", packets_captured);
1371 #elif defined(HAVE_ALARM)
1372 static void verbose_stats_dump(int sig _U_)
1374 struct pcap_stat stat;
1376 if (infodelay == 0 && pcap_stats(pd, &stat) >= 0)
1377 fprintf(stderr, "Got %u\r", packets_captured);
1385 extern char version[];
1386 #ifndef HAVE_PCAP_LIB_VERSION
1387 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
1388 extern char pcap_version[];
1389 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1390 static char pcap_version[] = "unknown";
1391 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1392 #endif /* HAVE_PCAP_LIB_VERSION */
1394 #ifdef HAVE_PCAP_LIB_VERSION
1396 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
1398 (void)fprintf(stderr, "%s version %s\n", program_name, version);
1400 (void)fprintf(stderr, "%s\n",pcap_lib_version());
1401 #else /* HAVE_PCAP_LIB_VERSION */
1403 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
1404 (void)fprintf(stderr, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version, pcap_version);
1406 (void)fprintf(stderr, "%s version %s\n", program_name, version);
1407 (void)fprintf(stderr, "libpcap version %s\n", pcap_version);
1409 #endif /* HAVE_PCAP_LIB_VERSION */
1410 (void)fprintf(stderr,
1411 "Usage: %s [-aAd" D_FLAG "eflLnNOpqRStu" U_FLAG "vxX]" B_FLAG_USAGE " [-c count] [ -C file_size ]\n", program_name);
1412 (void)fprintf(stderr,
1413 "\t\t[ -E algo:secret ] [ -F file ] [ -i interface ] [ -M secret ]\n");
1414 (void)fprintf(stderr,
1415 "\t\t[ -r file ] [ -s snaplen ] [ -T type ] [ -w file ]\n");
1416 (void)fprintf(stderr,
1417 "\t\t[ -W filecount ] [ -y datalinktype ] [ -Z user ]\n");
1418 (void)fprintf(stderr,
1419 "\t\t[ expression ]\n");
1427 ndo_error(netdissect_options *ndo _U_, const char *fmt, ...)
1431 (void)fprintf(stderr, "%s: ", program_name);
1433 (void)vfprintf(stderr, fmt, ap);
1437 if (fmt[-1] != '\n')
1438 (void)fputc('\n', stderr);
1446 ndo_warning(netdissect_options *ndo _U_, const char *fmt, ...)
1450 (void)fprintf(stderr, "%s: WARNING: ", program_name);
1452 (void)vfprintf(stderr, fmt, ap);
1456 if (fmt[-1] != '\n')
1457 (void)fputc('\n', stderr);