]> The Tcpdump Group git mirrors - tcpdump/blob - tcpdump.c
Make the default snapshot length the maximum; add a #define for the
[tcpdump] / tcpdump.c
1 /*
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.
4 *
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
16 * written permission.
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.
20 *
21 * Support for splitting captures into multiple files with a maximum
22 * file size:
23 *
24 * Copyright (c) 2001
25 * Seth Webster <swebster@sst.ll.mit.edu>
26 */
27
28 #ifndef lint
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.283 2008-09-25 21:45:50 guy Exp $ (LBL)";
34 #endif
35
36 /*
37 * tcpdump - monitor tcp/ip traffic on an ethernet.
38 *
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.
42 */
43
44 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47
48 #include <tcpdump-stdinc.h>
49
50 #ifdef WIN32
51 #include "getopt.h"
52 #include "w32_fzs.h"
53 extern int strcasecmp (const char *__s1, const char *__s2);
54 extern int SIZE_BUF;
55 #define off_t long
56 #define uint UINT
57 #endif /* WIN32 */
58
59 #ifdef HAVE_SMI_H
60 #include <smi.h>
61 #endif
62
63 #include <pcap.h>
64 #include <signal.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #include <limits.h>
69 #ifndef WIN32
70 #include <sys/wait.h>
71 #include <sys/resource.h>
72 #include <pwd.h>
73 #include <grp.h>
74 #include <errno.h>
75 #endif /* WIN32 */
76
77
78 #include "netdissect.h"
79 #include "interface.h"
80 #include "addrtoname.h"
81 #include "machdep.h"
82 #include "setsignal.h"
83 #include "gmt2local.h"
84 #include "pcap-missing.h"
85
86 #ifndef NAME_MAX
87 #define NAME_MAX 255
88 #endif
89
90 netdissect_options Gndo;
91 netdissect_options *gndo = &Gndo;
92
93 int dflag; /* print filter code */
94 int Lflag; /* list available data link types and exit */
95 char *zflag = NULL; /* compress each savefile using a specified command (like gzip or bzip2) */
96
97 static int infodelay;
98 static int infoprint;
99
100 char *program_name;
101
102 int32_t thiszone; /* seconds offset from gmt to local time */
103
104 /* Forwards */
105 static RETSIGTYPE cleanup(int);
106 static RETSIGTYPE child_cleanup(int);
107 static void usage(void) __attribute__((noreturn));
108 static void show_dlts_and_exit(pcap_t *pd) __attribute__((noreturn));
109
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, ...);
117
118 #ifdef SIGINFO
119 RETSIGTYPE requestinfo(int);
120 #endif
121
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);
128 #endif
129
130 static void info(int);
131 static u_int packets_captured;
132
133 typedef u_int (*if_printer)(const struct pcap_pkthdr *, const u_char *);
134
135 struct printer {
136 if_printer f;
137 int type;
138 };
139
140 static struct printer printers[] = {
141 { arcnet_if_print, DLT_ARCNET },
142 #ifdef DLT_ARCNET_LINUX
143 { arcnet_linux_if_print, DLT_ARCNET_LINUX },
144 #endif
145 { ether_if_print, DLT_EN10MB },
146 { token_if_print, DLT_IEEE802 },
147 #ifdef DLT_LANE8023
148 { lane_if_print, DLT_LANE8023 },
149 #endif
150 #ifdef DLT_CIP
151 { cip_if_print, DLT_CIP },
152 #endif
153 #ifdef DLT_ATM_CLIP
154 { cip_if_print, DLT_ATM_CLIP },
155 #endif
156 { sl_if_print, DLT_SLIP },
157 #ifdef DLT_SLIP_BSDOS
158 { sl_bsdos_if_print, DLT_SLIP_BSDOS },
159 #endif
160 { ppp_if_print, DLT_PPP },
161 #ifdef DLT_PPP_WITHDIRECTION
162 { ppp_if_print, DLT_PPP_WITHDIRECTION },
163 #endif
164 #ifdef DLT_PPP_BSDOS
165 { ppp_bsdos_if_print, DLT_PPP_BSDOS },
166 #endif
167 { fddi_if_print, DLT_FDDI },
168 { null_if_print, DLT_NULL },
169 #ifdef DLT_LOOP
170 { null_if_print, DLT_LOOP },
171 #endif
172 { raw_if_print, DLT_RAW },
173 { atm_if_print, DLT_ATM_RFC1483 },
174 #ifdef DLT_C_HDLC
175 { chdlc_if_print, DLT_C_HDLC },
176 #endif
177 #ifdef DLT_HDLC
178 { chdlc_if_print, DLT_HDLC },
179 #endif
180 #ifdef DLT_PPP_SERIAL
181 { ppp_hdlc_if_print, DLT_PPP_SERIAL },
182 #endif
183 #ifdef DLT_PPP_ETHER
184 { pppoe_if_print, DLT_PPP_ETHER },
185 #endif
186 #ifdef DLT_LINUX_SLL
187 { sll_if_print, DLT_LINUX_SLL },
188 #endif
189 #ifdef DLT_IEEE802_11
190 { ieee802_11_if_print, DLT_IEEE802_11},
191 #endif
192 #ifdef DLT_LTALK
193 { ltalk_if_print, DLT_LTALK },
194 #endif
195 #if defined(DLT_PFLOG) && defined(HAVE_NET_PFVAR_H)
196 { pflog_if_print, DLT_PFLOG },
197 #endif
198 #ifdef DLT_FR
199 { fr_if_print, DLT_FR },
200 #endif
201 #ifdef DLT_FRELAY
202 { fr_if_print, DLT_FRELAY },
203 #endif
204 #ifdef DLT_SUNATM
205 { sunatm_if_print, DLT_SUNATM },
206 #endif
207 #ifdef DLT_IP_OVER_FC
208 { ipfc_if_print, DLT_IP_OVER_FC },
209 #endif
210 #ifdef DLT_PRISM_HEADER
211 { prism_if_print, DLT_PRISM_HEADER },
212 #endif
213 #ifdef DLT_IEEE802_11_RADIO
214 { ieee802_11_radio_if_print, DLT_IEEE802_11_RADIO },
215 #endif
216 #ifdef DLT_ENC
217 { enc_if_print, DLT_ENC },
218 #endif
219 #ifdef DLT_SYMANTEC_FIREWALL
220 { symantec_if_print, DLT_SYMANTEC_FIREWALL },
221 #endif
222 #ifdef DLT_APPLE_IP_OVER_IEEE1394
223 { ap1394_if_print, DLT_APPLE_IP_OVER_IEEE1394 },
224 #endif
225 #ifdef DLT_IEEE802_11_RADIO_AVS
226 { ieee802_11_radio_avs_if_print, DLT_IEEE802_11_RADIO_AVS },
227 #endif
228 #ifdef DLT_JUNIPER_ATM1
229 { juniper_atm1_print, DLT_JUNIPER_ATM1 },
230 #endif
231 #ifdef DLT_JUNIPER_ATM2
232 { juniper_atm2_print, DLT_JUNIPER_ATM2 },
233 #endif
234 #ifdef DLT_JUNIPER_MFR
235 { juniper_mfr_print, DLT_JUNIPER_MFR },
236 #endif
237 #ifdef DLT_JUNIPER_MLFR
238 { juniper_mlfr_print, DLT_JUNIPER_MLFR },
239 #endif
240 #ifdef DLT_JUNIPER_MLPPP
241 { juniper_mlppp_print, DLT_JUNIPER_MLPPP },
242 #endif
243 #ifdef DLT_JUNIPER_PPPOE
244 { juniper_pppoe_print, DLT_JUNIPER_PPPOE },
245 #endif
246 #ifdef DLT_JUNIPER_PPPOE_ATM
247 { juniper_pppoe_atm_print, DLT_JUNIPER_PPPOE_ATM },
248 #endif
249 #ifdef DLT_JUNIPER_GGSN
250 { juniper_ggsn_print, DLT_JUNIPER_GGSN },
251 #endif
252 #ifdef DLT_JUNIPER_ES
253 { juniper_es_print, DLT_JUNIPER_ES },
254 #endif
255 #ifdef DLT_JUNIPER_MONITOR
256 { juniper_monitor_print, DLT_JUNIPER_MONITOR },
257 #endif
258 #ifdef DLT_JUNIPER_SERVICES
259 { juniper_services_print, DLT_JUNIPER_SERVICES },
260 #endif
261 #ifdef DLT_JUNIPER_ETHER
262 { juniper_ether_print, DLT_JUNIPER_ETHER },
263 #endif
264 #ifdef DLT_JUNIPER_PPP
265 { juniper_ppp_print, DLT_JUNIPER_PPP },
266 #endif
267 #ifdef DLT_JUNIPER_FRELAY
268 { juniper_frelay_print, DLT_JUNIPER_FRELAY },
269 #endif
270 #ifdef DLT_JUNIPER_CHDLC
271 { juniper_chdlc_print, DLT_JUNIPER_CHDLC },
272 #endif
273 #ifdef DLT_MFR
274 { mfr_if_print, DLT_MFR },
275 #endif
276 #if defined(DLT_BLUETOOTH_HCI_H4_WITH_PHDR) && defined(HAVE_PCAP_BLUETOOTH_H)
277 { bt_if_print, DLT_BLUETOOTH_HCI_H4_WITH_PHDR},
278 #endif
279 { NULL, 0 },
280 };
281
282 static if_printer
283 lookup_printer(int type)
284 {
285 struct printer *p;
286
287 for (p = printers; p->f; ++p)
288 if (type == p->type)
289 return p->f;
290
291 return NULL;
292 /* NOTREACHED */
293 }
294
295 static pcap_t *pd;
296
297 extern int optind;
298 extern int opterr;
299 extern char *optarg;
300
301 struct print_info {
302 if_printer printer;
303 };
304
305 struct dump_info {
306 char *WFileName;
307 char *CurrentFileName;
308 pcap_t *pd;
309 pcap_dumper_t *p;
310 };
311
312 static void
313 show_dlts_and_exit(pcap_t *pd)
314 {
315 int n_dlts;
316 int *dlts = 0;
317 const char *dlt_name;
318
319 n_dlts = pcap_list_datalinks(pd, &dlts);
320 if (n_dlts < 0)
321 error("%s", pcap_geterr(pd));
322 else if (n_dlts == 0 || !dlts)
323 error("No data link types.");
324
325 (void) fprintf(stderr, "Data link types (use option -y to set):\n");
326
327 while (--n_dlts >= 0) {
328 dlt_name = pcap_datalink_val_to_name(dlts[n_dlts]);
329 if (dlt_name != NULL) {
330 (void) fprintf(stderr, " %s (%s)", dlt_name,
331 pcap_datalink_val_to_description(dlts[n_dlts]));
332
333 /*
334 * OK, does tcpdump handle that type?
335 */
336 if (lookup_printer(dlts[n_dlts]) == NULL)
337 (void) fprintf(stderr, " (printing not supported)");
338 putchar('\n');
339 } else {
340 (void) fprintf(stderr, " DLT %d (printing not supported)\n",
341 dlts[n_dlts]);
342 }
343 }
344 free(dlts);
345 exit(0);
346 }
347
348 /*
349 * Set up flags that might or might not be supported depending on the
350 * version of libpcap we're using.
351 */
352 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
353 #define B_FLAG "B:"
354 #define B_FLAG_USAGE " [ -B size ]"
355 #else /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
356 #define B_FLAG
357 #define B_FLAG_USAGE
358 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
359
360 #ifdef HAVE_PCAP_CREATE
361 #define I_FLAG "I"
362 #else /* HAVE_PCAP_CREATE */
363 #define I_FLAG
364 #endif /* HAVE_PCAP_CREATE */
365
366 #ifdef HAVE_PCAP_FINDALLDEVS
367 #ifndef HAVE_PCAP_IF_T
368 #undef HAVE_PCAP_FINDALLDEVS
369 #endif
370 #endif
371
372 #ifdef HAVE_PCAP_FINDALLDEVS
373 #define D_FLAG "D"
374 #else
375 #define D_FLAG
376 #endif
377
378 #ifdef HAVE_PCAP_DUMP_FLUSH
379 #define U_FLAG "U"
380 #else
381 #define U_FLAG
382 #endif
383
384 #ifndef WIN32
385 /* Drop root privileges and chroot if necessary */
386 static void
387 droproot(const char *username, const char *chroot_dir)
388 {
389 struct passwd *pw = NULL;
390
391 if (chroot_dir && !username) {
392 fprintf(stderr, "tcpdump: Chroot without dropping root is insecure\n");
393 exit(1);
394 }
395
396 pw = getpwnam(username);
397 if (pw) {
398 if (chroot_dir) {
399 if (chroot(chroot_dir) != 0 || chdir ("/") != 0) {
400 fprintf(stderr, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
401 chroot_dir, pcap_strerror(errno));
402 exit(1);
403 }
404 }
405 if (initgroups(pw->pw_name, pw->pw_gid) != 0 ||
406 setgid(pw->pw_gid) != 0 || setuid(pw->pw_uid) != 0) {
407 fprintf(stderr, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
408 username,
409 (unsigned long)pw->pw_uid,
410 (unsigned long)pw->pw_gid,
411 pcap_strerror(errno));
412 exit(1);
413 }
414 }
415 else {
416 fprintf(stderr, "tcpdump: Couldn't find user '%.32s'\n",
417 username);
418 exit(1);
419 }
420 }
421 #endif /* WIN32 */
422
423 static int
424 getWflagChars(int x)
425 {
426 int c = 0;
427
428 x -= 1;
429 while (x > 0) {
430 c += 1;
431 x /= 10;
432 }
433
434 return c;
435 }
436
437
438 static void
439 MakeFilename(char *buffer, char *orig_name, int cnt, int max_chars)
440 {
441 char *filename = malloc(NAME_MAX + 1);
442
443 /* Process with strftime if Gflag is set. */
444 if (Gflag != 0) {
445 struct tm *local_tm;
446
447 /* Convert Gflag_time to a usable format */
448 if ((local_tm = localtime(&Gflag_time)) == NULL) {
449 error("MakeTimedFilename: localtime");
450 }
451
452 /* There's no good way to detect an error in strftime since a return
453 * value of 0 isn't necessarily failure.
454 */
455 strftime(filename, NAME_MAX, orig_name, local_tm);
456 } else {
457 strncpy(filename, orig_name, NAME_MAX);
458 }
459
460 if (cnt == 0 && max_chars == 0)
461 strncpy(buffer, filename, NAME_MAX + 1);
462 else
463 if (snprintf(buffer, NAME_MAX + 1, "%s%0*d", filename, max_chars, cnt) > NAME_MAX)
464 /* Report an error if the filename is too large */
465 error("too many output files or filename is too long (> %d)", NAME_MAX);
466 free(filename);
467 }
468
469 static int tcpdump_printf(netdissect_options *ndo _U_,
470 const char *fmt, ...)
471 {
472
473 va_list args;
474 int ret;
475
476 va_start(args, fmt);
477 ret=vfprintf(stdout, fmt, args);
478 va_end(args);
479
480 return ret;
481 }
482
483 int
484 main(int argc, char **argv)
485 {
486 register int cnt, op, i;
487 bpf_u_int32 localnet, netmask;
488 register char *cp, *infile, *cmdbuf, *device, *RFileName, *WFileName;
489 pcap_handler callback;
490 int type;
491 struct bpf_program fcode;
492 #ifndef WIN32
493 RETSIGTYPE (*oldhandler)(int);
494 #endif
495 struct print_info printinfo;
496 struct dump_info dumpinfo;
497 u_char *pcap_userdata;
498 char ebuf[PCAP_ERRBUF_SIZE];
499 char *username = NULL;
500 char *chroot_dir = NULL;
501 #ifdef HAVE_PCAP_FINDALLDEVS
502 pcap_if_t *devpointer;
503 int devnum;
504 #endif
505 int status;
506 #ifdef WIN32
507 if(wsockinit() != 0) return 1;
508 #endif /* WIN32 */
509
510 gndo->ndo_Oflag=1;
511 gndo->ndo_Rflag=1;
512 gndo->ndo_dlt=-1;
513 gndo->ndo_default_print=ndo_default_print;
514 gndo->ndo_printf=tcpdump_printf;
515 gndo->ndo_error=ndo_error;
516 gndo->ndo_warning=ndo_warning;
517 gndo->ndo_snaplen = DEFAULT_SNAPLEN;
518
519 cnt = -1;
520 device = NULL;
521 infile = NULL;
522 RFileName = NULL;
523 WFileName = NULL;
524 if ((cp = strrchr(argv[0], '/')) != NULL)
525 program_name = cp + 1;
526 else
527 program_name = argv[0];
528
529 if (abort_on_misalignment(ebuf, sizeof(ebuf)) < 0)
530 error("%s", ebuf);
531
532 #ifdef LIBSMI
533 smiInit("tcpdump");
534 #endif
535
536 opterr = 0;
537 while (
538 (op = getopt(argc, argv, "aA" B_FLAG "c:C:d" D_FLAG "eE:fF:G:i:" I_FLAG "KlLm:M:nNOpqr:Rs:StT:u" U_FLAG "vw:W:xXy:Yz:Z:")) != -1)
539 switch (op) {
540
541 case 'a':
542 /* compatibility for old -a */
543 break;
544
545 case 'A':
546 ++Aflag;
547 break;
548 case 'b':
549 ++bflag;
550 break;
551
552 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
553 case 'B':
554 Bflag = atoi(optarg)*1024;
555 if (Bflag <= 0)
556 error("invalid packet buffer size %s", optarg);
557 break;
558 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
559
560 case 'c':
561 cnt = atoi(optarg);
562 if (cnt <= 0)
563 error("invalid packet count %s", optarg);
564 break;
565
566 case 'C':
567 Cflag = atoi(optarg) * 1000000;
568 if (Cflag < 0)
569 error("invalid file size %s", optarg);
570 break;
571
572 case 'd':
573 ++dflag;
574 break;
575
576 #ifdef HAVE_PCAP_FINDALLDEVS
577 case 'D':
578 if (pcap_findalldevs(&devpointer, ebuf) < 0)
579 error("%s", ebuf);
580 else {
581 for (i = 0; devpointer != 0; i++) {
582 printf("%d.%s", i+1, devpointer->name);
583 if (devpointer->description != NULL)
584 printf(" (%s)", devpointer->description);
585 printf("\n");
586 devpointer = devpointer->next;
587 }
588 }
589 return 0;
590 #endif /* HAVE_PCAP_FINDALLDEVS */
591
592 case 'L':
593 Lflag++;
594 break;
595
596 case 'e':
597 ++eflag;
598 break;
599
600 case 'E':
601 #ifndef HAVE_LIBCRYPTO
602 warning("crypto code not compiled in");
603 #endif
604 gndo->ndo_espsecret = optarg;
605 break;
606
607 case 'f':
608 ++fflag;
609 break;
610
611 case 'F':
612 infile = optarg;
613 break;
614
615 case 'G':
616 Gflag = atoi(optarg);
617 if (Gflag < 0)
618 error("invalid number of seconds %s", optarg);
619
620 /* We will create one file initially. */
621 Gflag_count = 0;
622
623 /* Grab the current time for rotation use. */
624 if ((Gflag_time = time(NULL)) == (time_t)-1) {
625 error("main: can't get current time: %s",
626 pcap_strerror(errno));
627 }
628 break;
629
630 case 'i':
631 if (optarg[0] == '0' && optarg[1] == 0)
632 error("Invalid adapter index");
633
634 #ifdef HAVE_PCAP_FINDALLDEVS
635 /*
636 * If the argument is a number, treat it as
637 * an index into the list of adapters, as
638 * printed by "tcpdump -D".
639 *
640 * This should be OK on UNIX systems, as interfaces
641 * shouldn't have names that begin with digits.
642 * It can be useful on Windows, where more than
643 * one interface can have the same name.
644 */
645 if ((devnum = atoi(optarg)) != 0) {
646 if (devnum < 0)
647 error("Invalid adapter index");
648
649 if (pcap_findalldevs(&devpointer, ebuf) < 0)
650 error("%s", ebuf);
651 else {
652 for (i = 0; i < devnum-1; i++){
653 devpointer = devpointer->next;
654 if (devpointer == NULL)
655 error("Invalid adapter index");
656 }
657 }
658 device = devpointer->name;
659 break;
660 }
661 #endif /* HAVE_PCAP_FINDALLDEVS */
662 device = optarg;
663 break;
664
665 #ifdef HAVE_PCAP_CREATE
666 case 'I':
667 ++Iflag;
668 break;
669 #endif /* HAVE_PCAP_CREATE */
670
671 case 'l':
672 #ifdef WIN32
673 /*
674 * _IOLBF is the same as _IOFBF in Microsoft's C
675 * libraries; the only alternative they offer
676 * is _IONBF.
677 *
678 * XXX - this should really be checking for MSVC++,
679 * not WIN32, if, for example, MinGW has its own
680 * C library that is more UNIX-compatible.
681 */
682 setvbuf(stdout, NULL, _IONBF, 0);
683 #else /* WIN32 */
684 #ifdef HAVE_SETLINEBUF
685 setlinebuf(stdout);
686 #else
687 setvbuf(stdout, NULL, _IOLBF, 0);
688 #endif
689 #endif /* WIN32 */
690 break;
691
692 case 'K':
693 ++Kflag;
694 break;
695
696 case 'm':
697 #ifdef LIBSMI
698 if (smiLoadModule(optarg) == 0) {
699 error("could not load MIB module %s", optarg);
700 }
701 sflag = 1;
702 #else
703 (void)fprintf(stderr, "%s: ignoring option `-m %s' ",
704 program_name, optarg);
705 (void)fprintf(stderr, "(no libsmi support)\n");
706 #endif
707 break;
708
709 case 'M':
710 /* TCP-MD5 shared secret */
711 #ifndef HAVE_LIBCRYPTO
712 warning("crypto code not compiled in");
713 #endif
714 sigsecret = optarg;
715 break;
716
717 case 'n':
718 ++nflag;
719 break;
720
721 case 'N':
722 ++Nflag;
723 break;
724
725 case 'O':
726 Oflag = 0;
727 break;
728
729 case 'p':
730 ++pflag;
731 break;
732
733 case 'q':
734 ++qflag;
735 ++suppress_default_print;
736 break;
737
738 case 'r':
739 RFileName = optarg;
740 break;
741
742 case 'R':
743 Rflag = 0;
744 break;
745
746 case 's': {
747 char *end;
748
749 snaplen = strtol(optarg, &end, 0);
750 if (optarg == end || *end != '\0'
751 || snaplen < 0 || snaplen > MAXIMUM_SNAPLEN)
752 error("invalid snaplen %s", optarg);
753 else if (snaplen == 0)
754 snaplen = MAXIMUM_SNAPLEN;
755 break;
756 }
757
758 case 'S':
759 ++Sflag;
760 break;
761
762 case 't':
763 ++tflag;
764 break;
765
766 case 'T':
767 if (strcasecmp(optarg, "vat") == 0)
768 packettype = PT_VAT;
769 else if (strcasecmp(optarg, "wb") == 0)
770 packettype = PT_WB;
771 else if (strcasecmp(optarg, "rpc") == 0)
772 packettype = PT_RPC;
773 else if (strcasecmp(optarg, "rtp") == 0)
774 packettype = PT_RTP;
775 else if (strcasecmp(optarg, "rtcp") == 0)
776 packettype = PT_RTCP;
777 else if (strcasecmp(optarg, "snmp") == 0)
778 packettype = PT_SNMP;
779 else if (strcasecmp(optarg, "cnfp") == 0)
780 packettype = PT_CNFP;
781 else if (strcasecmp(optarg, "tftp") == 0)
782 packettype = PT_TFTP;
783 else if (strcasecmp(optarg, "aodv") == 0)
784 packettype = PT_AODV;
785 else
786 error("unknown packet type `%s'", optarg);
787 break;
788
789 case 'u':
790 ++uflag;
791 break;
792
793 #ifdef HAVE_PCAP_DUMP_FLUSH
794 case 'U':
795 ++Uflag;
796 break;
797 #endif
798
799 case 'v':
800 ++vflag;
801 break;
802
803 case 'w':
804 WFileName = optarg;
805 break;
806
807 case 'W':
808 Wflag = atoi(optarg);
809 if (Wflag < 0)
810 error("invalid number of output files %s", optarg);
811 WflagChars = getWflagChars(Wflag);
812 break;
813
814 case 'x':
815 ++xflag;
816 ++suppress_default_print;
817 break;
818
819 case 'X':
820 ++Xflag;
821 ++suppress_default_print;
822 break;
823
824 case 'y':
825 gndo->ndo_dltname = optarg;
826 gndo->ndo_dlt =
827 pcap_datalink_name_to_val(gndo->ndo_dltname);
828 if (gndo->ndo_dlt < 0)
829 error("invalid data link type %s", gndo->ndo_dltname);
830 break;
831
832 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
833 case 'Y':
834 {
835 /* Undocumented flag */
836 #ifdef HAVE_PCAP_DEBUG
837 extern int pcap_debug;
838 pcap_debug = 1;
839 #else
840 extern int yydebug;
841 yydebug = 1;
842 #endif
843 }
844 break;
845 #endif
846 case 'z':
847 if (optarg) {
848 zflag = strdup(optarg);
849 } else {
850 usage();
851 /* NOTREACHED */
852 }
853 break;
854
855 case 'Z':
856 if (optarg) {
857 username = strdup(optarg);
858 }
859 else {
860 usage();
861 /* NOTREACHED */
862 }
863 break;
864
865 default:
866 usage();
867 /* NOTREACHED */
868 }
869
870 switch (tflag) {
871
872 case 0: /* Default */
873 case 4: /* Default + Date*/
874 thiszone = gmt2local(0);
875 break;
876
877 case 1: /* No time stamp */
878 case 2: /* Unix timeval style */
879 case 3: /* Microseconds since previous packet */
880 case 5: /* Microseconds since first packet */
881 break;
882
883 default: /* Not supported */
884 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
885 break;
886 }
887
888 #ifdef WITH_CHROOT
889 /* if run as root, prepare for chrooting */
890 if (getuid() == 0 || geteuid() == 0) {
891 /* future extensibility for cmd-line arguments */
892 if (!chroot_dir)
893 chroot_dir = WITH_CHROOT;
894 }
895 #endif
896
897 #ifdef WITH_USER
898 /* if run as root, prepare for dropping root privileges */
899 if (getuid() == 0 || geteuid() == 0) {
900 /* Run with '-Z root' to restore old behaviour */
901 if (!username)
902 username = WITH_USER;
903 }
904 #endif
905
906 if (RFileName != NULL) {
907 int dlt;
908 const char *dlt_name;
909
910 #ifndef WIN32
911 /*
912 * We don't need network access, so relinquish any set-UID
913 * or set-GID privileges we have (if any).
914 *
915 * We do *not* want set-UID privileges when opening a
916 * trace file, as that might let the user read other
917 * people's trace files (especially if we're set-UID
918 * root).
919 */
920 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
921 fprintf(stderr, "Warning: setgid/setuid failed !\n");
922 #endif /* WIN32 */
923 pd = pcap_open_offline(RFileName, ebuf);
924 if (pd == NULL)
925 error("%s", ebuf);
926 dlt = pcap_datalink(pd);
927 dlt_name = pcap_datalink_val_to_name(dlt);
928 if (dlt_name == NULL) {
929 fprintf(stderr, "reading from file %s, link-type %u\n",
930 RFileName, dlt);
931 } else {
932 fprintf(stderr,
933 "reading from file %s, link-type %s (%s)\n",
934 RFileName, dlt_name,
935 pcap_datalink_val_to_description(dlt));
936 }
937 localnet = 0;
938 netmask = 0;
939 if (fflag != 0)
940 error("-f and -r options are incompatible");
941 } else {
942 if (device == NULL) {
943 device = pcap_lookupdev(ebuf);
944 if (device == NULL)
945 error("%s", ebuf);
946 }
947 #ifdef WIN32
948 if(strlen(device) == 1) //we assume that an ASCII string is always longer than 1 char
949 { //a Unicode string has a \0 as second byte (so strlen() is 1)
950 fprintf(stderr, "%s: listening on %ws\n", program_name, device);
951 }
952 else
953 {
954 fprintf(stderr, "%s: listening on %s\n", program_name, device);
955 }
956
957 fflush(stderr);
958 #endif /* WIN32 */
959 #ifdef HAVE_PCAP_CREATE
960 pd = pcap_create(device, ebuf);
961 if (pd == NULL)
962 error("%s", ebuf);
963 status = pcap_set_snaplen(pd, snaplen);
964 if (status != 0)
965 error("%s: pcap_set_snaplen failed: %s",
966 device, pcap_statustostr(status));
967 status = pcap_set_promisc(pd, !pflag);
968 if (status != 0)
969 error("%s: pcap_set_promisc failed: %s",
970 device, pcap_statustostr(status));
971 if (Iflag) {
972 status = pcap_set_rfmon(pd, 1);
973 if (status != 0)
974 error("%s: pcap_set_rfmon failed: %s",
975 device, pcap_statustostr(status));
976 }
977 status = pcap_set_timeout(pd, 1000);
978 if (status != 0)
979 error("%s: pcap_set_timeout failed: %s",
980 device, pcap_statustostr(status));
981 if (Bflag != 0) {
982 status = pcap_set_buffer_size(pd, Bflag);
983 if (status != 0)
984 error("%s: pcap_set_buffer_size failed: %s",
985 device, pcap_statustostr(status));
986 }
987 status = pcap_activate(pd);
988 if (status < 0) {
989 /*
990 * pcap_activate() failed.
991 */
992 cp = pcap_geterr(pd);
993 if (status == PCAP_ERROR)
994 error("%s", cp);
995 else if ((status == PCAP_ERROR_NO_SUCH_DEVICE ||
996 status == PCAP_ERROR_PERM_DENIED) &&
997 *cp != '\0')
998 error("%s: %s\n(%s)", device,
999 pcap_statustostr(status), cp);
1000 else
1001 error("%s: %s", device,
1002 pcap_statustostr(status));
1003 } else if (status > 0) {
1004 /*
1005 * pcap_activate() succeeded, but it's warning us
1006 * of a problem it had.
1007 */
1008 cp = pcap_geterr(pd);
1009 if (status == PCAP_WARNING)
1010 warning("%s", cp);
1011 else if (status == PCAP_WARNING_PROMISC_NOTSUP &&
1012 *cp != '\0')
1013 warning("%s: %s\n(%s)", device,
1014 pcap_statustostr(status), cp);
1015 else
1016 warning("%s: %s", device,
1017 pcap_statustostr(status));
1018 }
1019 #else
1020 *ebuf = '\0';
1021 pd = pcap_open_live(device, snaplen, !pflag, 1000, ebuf);
1022 if (pd == NULL)
1023 error("%s", ebuf);
1024 else if (*ebuf)
1025 warning("%s", ebuf);
1026 #endif /* HAVE_PCAP_CREATE */
1027 /*
1028 * Let user own process after socket has been opened.
1029 */
1030 #ifndef WIN32
1031 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
1032 fprintf(stderr, "Warning: setgid/setuid failed !\n");
1033 #endif /* WIN32 */
1034 #if !defined(HAVE_PCAP_CREATE) && defined(WIN32)
1035 if(Bflag != 0)
1036 if(pcap_setbuff(pd, Bflag)==-1){
1037 error("%s", pcap_geterr(pd));
1038 }
1039 #endif /* !defined(HAVE_PCAP_CREATE) && defined(WIN32) */
1040 if (Lflag)
1041 show_dlts_and_exit(pd);
1042 if (gndo->ndo_dlt >= 0) {
1043 #ifdef HAVE_PCAP_SET_DATALINK
1044 if (pcap_set_datalink(pd, gndo->ndo_dlt) < 0)
1045 error("%s", pcap_geterr(pd));
1046 #else
1047 /*
1048 * We don't actually support changing the
1049 * data link type, so we only let them
1050 * set it to what it already is.
1051 */
1052 if (gndo->ndo_dlt != pcap_datalink(pd)) {
1053 error("%s is not one of the DLTs supported by this device\n",
1054 gndo->ndo_dltname);
1055 }
1056 #endif
1057 (void)fprintf(stderr, "%s: data link type %s\n",
1058 program_name, gndo->ndo_dltname);
1059 (void)fflush(stderr);
1060 }
1061 i = pcap_snapshot(pd);
1062 if (snaplen < i) {
1063 warning("snaplen raised from %d to %d", snaplen, i);
1064 snaplen = i;
1065 }
1066 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
1067 localnet = 0;
1068 netmask = 0;
1069 warning("%s", ebuf);
1070 }
1071 }
1072 if (infile)
1073 cmdbuf = read_infile(infile);
1074 else
1075 cmdbuf = copy_argv(&argv[optind]);
1076
1077 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
1078 error("%s", pcap_geterr(pd));
1079 if (dflag) {
1080 bpf_dump(&fcode, dflag);
1081 pcap_close(pd);
1082 exit(0);
1083 }
1084 init_addrtoname(localnet, netmask);
1085 init_checksum();
1086
1087 #ifndef WIN32
1088 (void)setsignal(SIGPIPE, cleanup);
1089 (void)setsignal(SIGTERM, cleanup);
1090 (void)setsignal(SIGINT, cleanup);
1091 (void)setsignal(SIGCHLD, child_cleanup);
1092 #endif /* WIN32 */
1093 /* Cooperate with nohup(1) */
1094 #ifndef WIN32
1095 if ((oldhandler = setsignal(SIGHUP, cleanup)) != SIG_DFL)
1096 (void)setsignal(SIGHUP, oldhandler);
1097 #endif /* WIN32 */
1098
1099 if (pcap_setfilter(pd, &fcode) < 0)
1100 error("%s", pcap_geterr(pd));
1101 if (WFileName) {
1102 pcap_dumper_t *p;
1103 /* Do not exceed the default NAME_MAX for files. */
1104 dumpinfo.CurrentFileName = (char *)malloc(NAME_MAX + 1);
1105
1106 if (dumpinfo.CurrentFileName == NULL)
1107 error("malloc of dumpinfo.CurrentFileName");
1108
1109 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1110 if (Cflag != 0)
1111 MakeFilename(dumpinfo.CurrentFileName, WFileName, 0, WflagChars);
1112 else
1113 MakeFilename(dumpinfo.CurrentFileName, WFileName, 0, 0);
1114
1115 p = pcap_dump_open(pd, dumpinfo.CurrentFileName);
1116 if (p == NULL)
1117 error("%s", pcap_geterr(pd));
1118 if (Cflag != 0 || Gflag != 0) {
1119 callback = dump_packet_and_trunc;
1120 dumpinfo.WFileName = WFileName;
1121 dumpinfo.pd = pd;
1122 dumpinfo.p = p;
1123 pcap_userdata = (u_char *)&dumpinfo;
1124 } else {
1125 callback = dump_packet;
1126 pcap_userdata = (u_char *)p;
1127 }
1128 } else {
1129 type = pcap_datalink(pd);
1130 printinfo.printer = lookup_printer(type);
1131 if (printinfo.printer == NULL) {
1132 gndo->ndo_dltname = pcap_datalink_val_to_name(type);
1133 if (gndo->ndo_dltname != NULL)
1134 error("unsupported data link type %s",
1135 gndo->ndo_dltname);
1136 else
1137 error("unsupported data link type %d", type);
1138 }
1139 callback = print_packet;
1140 pcap_userdata = (u_char *)&printinfo;
1141 }
1142 #ifndef WIN32
1143 /*
1144 * We cannot do this earlier, because we want to be able to open
1145 * the file (if done) for writing before giving up permissions.
1146 */
1147 if (getuid() == 0 || geteuid() == 0) {
1148 if (username || chroot_dir)
1149 droproot(username, chroot_dir);
1150 }
1151 #endif /* WIN32 */
1152 #ifdef SIGINFO
1153 /*
1154 * We can't get statistics when reading from a file rather
1155 * than capturing from a device.
1156 */
1157 if (RFileName == NULL)
1158 (void)setsignal(SIGINFO, requestinfo);
1159 #endif
1160
1161 if (vflag > 0 && WFileName) {
1162 /*
1163 * When capturing to a file, "-v" means tcpdump should,
1164 * every 10 secodns, "v"erbosely report the number of
1165 * packets captured.
1166 */
1167 #ifdef USE_WIN32_MM_TIMER
1168 /* call verbose_stats_dump() each 1000 +/-100msec */
1169 timer_id = timeSetEvent(1000, 100, verbose_stats_dump, 0, TIME_PERIODIC);
1170 setvbuf(stderr, NULL, _IONBF, 0);
1171 #elif defined(HAVE_ALARM)
1172 (void)setsignal(SIGALRM, verbose_stats_dump);
1173 alarm(1);
1174 #endif
1175 }
1176
1177 #ifndef WIN32
1178 if (RFileName == NULL) {
1179 int dlt;
1180 const char *dlt_name;
1181
1182 if (!vflag && !WFileName) {
1183 (void)fprintf(stderr,
1184 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1185 program_name);
1186 } else
1187 (void)fprintf(stderr, "%s: ", program_name);
1188 dlt = pcap_datalink(pd);
1189 dlt_name = pcap_datalink_val_to_name(dlt);
1190 if (dlt_name == NULL) {
1191 (void)fprintf(stderr, "listening on %s, link-type %u, capture size %u bytes\n",
1192 device, dlt, snaplen);
1193 } else {
1194 (void)fprintf(stderr, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1195 device, dlt_name,
1196 pcap_datalink_val_to_description(dlt), snaplen);
1197 }
1198 (void)fflush(stderr);
1199 }
1200 #endif /* WIN32 */
1201 status = pcap_loop(pd, cnt, callback, pcap_userdata);
1202 if (WFileName == NULL) {
1203 /*
1204 * We're printing packets. Flush the printed output,
1205 * so it doesn't get intermingled with error output.
1206 */
1207 if (status == -2) {
1208 /*
1209 * We got interrupted, so perhaps we didn't
1210 * manage to finish a line we were printing.
1211 * Print an extra newline, just in case.
1212 */
1213 putchar('\n');
1214 }
1215 (void)fflush(stdout);
1216 }
1217 if (status == -1) {
1218 /*
1219 * Error. Report it.
1220 */
1221 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
1222 program_name, pcap_geterr(pd));
1223 }
1224 if (RFileName == NULL) {
1225 /*
1226 * We're doing a live capture. Report the capture
1227 * statistics.
1228 */
1229 info(1);
1230 }
1231 pcap_close(pd);
1232 exit(status == -1 ? 1 : 0);
1233 }
1234
1235 /* make a clean exit on interrupts */
1236 static RETSIGTYPE
1237 cleanup(int signo _U_)
1238 {
1239 #ifdef USE_WIN32_MM_TIMER
1240 if (timer_id)
1241 timeKillEvent(timer_id);
1242 timer_id = 0;
1243 #elif defined(HAVE_ALARM)
1244 alarm(0);
1245 #endif
1246
1247 #ifdef HAVE_PCAP_BREAKLOOP
1248 /*
1249 * We have "pcap_breakloop()"; use it, so that we do as little
1250 * as possible in the signal handler (it's probably not safe
1251 * to do anything with standard I/O streams in a signal handler -
1252 * the ANSI C standard doesn't say it is).
1253 */
1254 pcap_breakloop(pd);
1255 #else
1256 /*
1257 * We don't have "pcap_breakloop()"; this isn't safe, but
1258 * it's the best we can do. Print the summary if we're
1259 * not reading from a savefile - i.e., if we're doing a
1260 * live capture - and exit.
1261 */
1262 if (pd != NULL && pcap_file(pd) == NULL) {
1263 /*
1264 * We got interrupted, so perhaps we didn't
1265 * manage to finish a line we were printing.
1266 * Print an extra newline, just in case.
1267 */
1268 putchar('\n');
1269 (void)fflush(stdout);
1270 info(1);
1271 }
1272 exit(0);
1273 #endif
1274 }
1275
1276 /*
1277 On windows, we do not use a fork, so we do not care less about
1278 waiting a child processes to die
1279 */
1280 #ifndef WIN32
1281 static RETSIGTYPE
1282 child_cleanup(int signo _U_)
1283 {
1284 wait(NULL);
1285 }
1286 #endif /* WIN32 */
1287
1288 static void
1289 info(register int verbose)
1290 {
1291 struct pcap_stat stat;
1292
1293 if (pcap_stats(pd, &stat) < 0) {
1294 (void)fprintf(stderr, "pcap_stats: %s\n", pcap_geterr(pd));
1295 infoprint = 0;
1296 return;
1297 }
1298
1299 if (!verbose)
1300 fprintf(stderr, "%s: ", program_name);
1301
1302 (void)fprintf(stderr, "%u packets captured", packets_captured);
1303 if (!verbose)
1304 fputs(", ", stderr);
1305 else
1306 putc('\n', stderr);
1307 (void)fprintf(stderr, "%d packets received by filter", stat.ps_recv);
1308 if (!verbose)
1309 fputs(", ", stderr);
1310 else
1311 putc('\n', stderr);
1312 (void)fprintf(stderr, "%d packets dropped by kernel\n", stat.ps_drop);
1313 infoprint = 0;
1314 }
1315
1316 #ifndef WIN32
1317 static void
1318 compress_savefile(const char *filename)
1319 {
1320 if (fork())
1321 return;
1322 /*
1323 * Set to lowest priority so that this doesn't disturb the capture
1324 */
1325 #ifdef NZERO
1326 setpriority(PRIO_PROCESS, 0, NZERO - 1);
1327 #else
1328 setpriority(PRIO_PROCESS, 0, 19);
1329 #endif
1330 if (execlp(zflag, zflag, filename, NULL) == -1)
1331 fprintf(stderr,
1332 "compress_savefile:execlp(%s, %s): %s\n",
1333 zflag,
1334 filename,
1335 strerror(errno));
1336 }
1337 #else /* WIN32 */
1338 static void
1339 compress_savefile(const char *filename)
1340 {
1341 fprintf(stderr,
1342 "compress_savefile failed. Functionality not implemented under windows\n");
1343 }
1344 #endif /* WIN32 */
1345
1346 static void
1347 dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
1348 {
1349 struct dump_info *dump_info;
1350
1351 ++packets_captured;
1352
1353 ++infodelay;
1354
1355 dump_info = (struct dump_info *)user;
1356
1357 /*
1358 * XXX - this won't force the file to rotate on the specified time
1359 * boundary, but it will rotate on the first packet received after the
1360 * specified Gflag number of seconds. Note: if a Gflag time boundary
1361 * and a Cflag size boundary coincide, the time rotation will occur
1362 * first thereby cancelling the Cflag boundary (since the file should
1363 * be 0).
1364 */
1365 if (Gflag != 0) {
1366 /* Check if it is time to rotate */
1367 time_t t;
1368
1369 /* Get the current time */
1370 if ((t = time(NULL)) == (time_t)-1) {
1371 error("dump_and_trunc_packet: can't get current_time: %s",
1372 pcap_strerror(errno));
1373 }
1374
1375
1376 /* If the time is greater than the specified window, rotate */
1377 if (t - Gflag_time >= Gflag) {
1378 /* Update the Gflag_time */
1379 Gflag_time = t;
1380 /* Update Gflag_count */
1381 Gflag_count++;
1382 /*
1383 * Close the current file and open a new one.
1384 */
1385 pcap_dump_close(dump_info->p);
1386
1387 /*
1388 * Compress the file we just closed, if the user asked for it
1389 */
1390 if (zflag != NULL)
1391 compress_savefile(dump_info->CurrentFileName);
1392
1393 /*
1394 * Check to see if we've exceeded the Wflag (when
1395 * not using Cflag).
1396 */
1397 if (Cflag == 0 && Wflag > 0 && Gflag_count >= Wflag) {
1398 (void)fprintf(stderr, "Maximum file limit reached: %d\n",
1399 Wflag);
1400 exit(0);
1401 /* NOTREACHED */
1402 }
1403 if (dump_info->CurrentFileName != NULL)
1404 free(dump_info->CurrentFileName);
1405 /* Allocate space for max filename + \0. */
1406 dump_info->CurrentFileName = (char *)malloc(NAME_MAX + 1);
1407 if (dump_info->CurrentFileName == NULL)
1408 error("dump_packet_and_trunc: malloc");
1409 /*
1410 * This is always the first file in the Cflag
1411 * rotation: e.g. 0
1412 * We also don't need numbering if Cflag is not set.
1413 */
1414 if (Cflag != 0)
1415 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, 0,
1416 WflagChars);
1417 else
1418 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, 0, 0);
1419
1420 dump_info->p = pcap_dump_open(dump_info->pd, dump_info->CurrentFileName);
1421 if (dump_info->p == NULL)
1422 error("%s", pcap_geterr(pd));
1423 }
1424 }
1425
1426 /*
1427 * XXX - this won't prevent capture files from getting
1428 * larger than Cflag - the last packet written to the
1429 * file could put it over Cflag.
1430 */
1431 if (Cflag != 0 && pcap_dump_ftell(dump_info->p) > Cflag) {
1432 /*
1433 * Close the current file and open a new one.
1434 */
1435 pcap_dump_close(dump_info->p);
1436
1437 /*
1438 * Compress the file we just closed, if the user asked for it
1439 */
1440 if (zflag != NULL)
1441 compress_savefile(dump_info->CurrentFileName);
1442
1443 Cflag_count++;
1444 if (Wflag > 0) {
1445 if (Cflag_count >= Wflag)
1446 Cflag_count = 0;
1447 }
1448 if (dump_info->CurrentFileName != NULL)
1449 free(dump_info->CurrentFileName);
1450 dump_info->CurrentFileName = (char *)malloc(NAME_MAX + 1);
1451 if (dump_info->CurrentFileName == NULL)
1452 error("dump_packet_and_trunc: malloc");
1453 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, Cflag_count, WflagChars);
1454 dump_info->p = pcap_dump_open(dump_info->pd, dump_info->CurrentFileName);
1455 if (dump_info->p == NULL)
1456 error("%s", pcap_geterr(pd));
1457 }
1458
1459 pcap_dump((u_char *)dump_info->p, h, sp);
1460 #ifdef HAVE_PCAP_DUMP_FLUSH
1461 if (Uflag)
1462 pcap_dump_flush(dump_info->p);
1463 #endif
1464
1465 --infodelay;
1466 if (infoprint)
1467 info(0);
1468 }
1469
1470 static void
1471 dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
1472 {
1473 ++packets_captured;
1474
1475 ++infodelay;
1476
1477 pcap_dump(user, h, sp);
1478 #ifdef HAVE_PCAP_DUMP_FLUSH
1479 if (Uflag)
1480 pcap_dump_flush((pcap_dumper_t *)user);
1481 #endif
1482
1483 --infodelay;
1484 if (infoprint)
1485 info(0);
1486 }
1487
1488 static void
1489 print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
1490 {
1491 struct print_info *print_info;
1492 u_int hdrlen;
1493
1494 ++packets_captured;
1495
1496 ++infodelay;
1497 ts_print(&h->ts);
1498
1499 print_info = (struct print_info *)user;
1500
1501 /*
1502 * Some printers want to check that they're not walking off the
1503 * end of the packet.
1504 * Rather than pass it all the way down, we set this global.
1505 */
1506 snapend = sp + h->caplen;
1507
1508 hdrlen = (*print_info->printer)(h, sp);
1509 if (Xflag) {
1510 /*
1511 * Print the raw packet data in hex and ASCII.
1512 */
1513 if (Xflag > 1) {
1514 /*
1515 * Include the link-layer header.
1516 */
1517 hex_and_ascii_print("\n\t", sp, h->caplen);
1518 } else {
1519 /*
1520 * Don't include the link-layer header - and if
1521 * we have nothing past the link-layer header,
1522 * print nothing.
1523 */
1524 if (h->caplen > hdrlen)
1525 hex_and_ascii_print("\n\t", sp + hdrlen,
1526 h->caplen - hdrlen);
1527 }
1528 } else if (xflag) {
1529 /*
1530 * Print the raw packet data in hex.
1531 */
1532 if (xflag > 1) {
1533 /*
1534 * Include the link-layer header.
1535 */
1536 hex_print("\n\t", sp, h->caplen);
1537 } else {
1538 /*
1539 * Don't include the link-layer header - and if
1540 * we have nothing past the link-layer header,
1541 * print nothing.
1542 */
1543 if (h->caplen > hdrlen)
1544 hex_print("\n\t", sp + hdrlen,
1545 h->caplen - hdrlen);
1546 }
1547 } else if (Aflag) {
1548 /*
1549 * Print the raw packet data in ASCII.
1550 */
1551 if (Aflag > 1) {
1552 /*
1553 * Include the link-layer header.
1554 */
1555 ascii_print(sp, h->caplen);
1556 } else {
1557 /*
1558 * Don't include the link-layer header - and if
1559 * we have nothing past the link-layer header,
1560 * print nothing.
1561 */
1562 if (h->caplen > hdrlen)
1563 ascii_print(sp + hdrlen, h->caplen - hdrlen);
1564 }
1565 }
1566
1567 putchar('\n');
1568
1569 --infodelay;
1570 if (infoprint)
1571 info(0);
1572 }
1573
1574 #ifdef WIN32
1575 /*
1576 * XXX - there should really be libpcap calls to get the version
1577 * number as a string (the string would be generated from #defines
1578 * at run time, so that it's not generated from string constants
1579 * in the library, as, on many UNIX systems, those constants would
1580 * be statically linked into the application executable image, and
1581 * would thus reflect the version of libpcap on the system on
1582 * which the application was *linked*, not the system on which it's
1583 * *running*.
1584 *
1585 * That routine should be documented, unlike the "version[]"
1586 * string, so that UNIX vendors providing their own libpcaps
1587 * don't omit it (as a couple of vendors have...).
1588 *
1589 * Packet.dll should perhaps also export a routine to return the
1590 * version number of the Packet.dll code, to supply the
1591 * "Wpcap_version" information on Windows.
1592 */
1593 char WDversion[]="current-cvs.tcpdump.org";
1594 #if !defined(HAVE_GENERATED_VERSION)
1595 char version[]="current-cvs.tcpdump.org";
1596 #endif
1597 char pcap_version[]="current-cvs.tcpdump.org";
1598 char Wpcap_version[]="3.1";
1599 #endif
1600
1601 /*
1602 * By default, print the specified data out in hex and ASCII.
1603 */
1604 static void
1605 ndo_default_print(netdissect_options *ndo _U_, const u_char *bp, u_int length)
1606 {
1607 hex_and_ascii_print("\n\t", bp, length); /* pass on lf and identation string */
1608 }
1609
1610 void
1611 default_print(const u_char *bp, u_int length)
1612 {
1613 ndo_default_print(gndo, bp, length);
1614 }
1615
1616 #ifdef SIGINFO
1617 RETSIGTYPE requestinfo(int signo _U_)
1618 {
1619 if (infodelay)
1620 ++infoprint;
1621 else
1622 info(0);
1623 }
1624 #endif
1625
1626 /*
1627 * Called once each second in verbose mode while dumping to file
1628 */
1629 #ifdef USE_WIN32_MM_TIMER
1630 void CALLBACK verbose_stats_dump (UINT timer_id _U_, UINT msg _U_, DWORD_PTR arg _U_,
1631 DWORD_PTR dw1 _U_, DWORD_PTR dw2 _U_)
1632 {
1633 struct pcap_stat stat;
1634
1635 if (infodelay == 0 && pcap_stats(pd, &stat) >= 0)
1636 fprintf(stderr, "Got %u\r", packets_captured);
1637 }
1638 #elif defined(HAVE_ALARM)
1639 static void verbose_stats_dump(int sig _U_)
1640 {
1641 struct pcap_stat stat;
1642
1643 if (infodelay == 0 && pcap_stats(pd, &stat) >= 0)
1644 fprintf(stderr, "Got %u\r", packets_captured);
1645 alarm(1);
1646 }
1647 #endif
1648
1649 static void
1650 usage(void)
1651 {
1652 extern char version[];
1653 #ifndef HAVE_PCAP_LIB_VERSION
1654 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
1655 extern char pcap_version[];
1656 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1657 static char pcap_version[] = "unknown";
1658 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1659 #endif /* HAVE_PCAP_LIB_VERSION */
1660
1661 #ifdef HAVE_PCAP_LIB_VERSION
1662 #ifdef WIN32
1663 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
1664 #else /* WIN32 */
1665 (void)fprintf(stderr, "%s version %s\n", program_name, version);
1666 #endif /* WIN32 */
1667 (void)fprintf(stderr, "%s\n",pcap_lib_version());
1668 #else /* HAVE_PCAP_LIB_VERSION */
1669 #ifdef WIN32
1670 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
1671 (void)fprintf(stderr, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version, pcap_version);
1672 #else /* WIN32 */
1673 (void)fprintf(stderr, "%s version %s\n", program_name, version);
1674 (void)fprintf(stderr, "libpcap version %s\n", pcap_version);
1675 #endif /* WIN32 */
1676 #endif /* HAVE_PCAP_LIB_VERSION */
1677 (void)fprintf(stderr,
1678 "Usage: %s [-aAd" D_FLAG "ef" I_FLAG "KlLnNOpqRStu" U_FLAG "vxX]" B_FLAG_USAGE " [ -c count ]\n", program_name);
1679 (void)fprintf(stderr,
1680 "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n");
1681 (void)fprintf(stderr,
1682 "\t\t[ -i interface ] [ -M secret ] [ -r file ]\n");
1683 (void)fprintf(stderr,
1684 "\t\t[ -s snaplen ] [ -T type ] [ -w file ] [ -W filecount ]\n");
1685 (void)fprintf(stderr,
1686 "\t\t[ -y datalinktype ] [ -z command ] [ -Z user ]\n");
1687 (void)fprintf(stderr,
1688 "\t\t[ expression ]\n");
1689 exit(1);
1690 }
1691
1692
1693
1694 /* VARARGS */
1695 static void
1696 ndo_error(netdissect_options *ndo _U_, const char *fmt, ...)
1697 {
1698 va_list ap;
1699
1700 (void)fprintf(stderr, "%s: ", program_name);
1701 va_start(ap, fmt);
1702 (void)vfprintf(stderr, fmt, ap);
1703 va_end(ap);
1704 if (*fmt) {
1705 fmt += strlen(fmt);
1706 if (fmt[-1] != '\n')
1707 (void)fputc('\n', stderr);
1708 }
1709 exit(1);
1710 /* NOTREACHED */
1711 }
1712
1713 /* VARARGS */
1714 static void
1715 ndo_warning(netdissect_options *ndo _U_, const char *fmt, ...)
1716 {
1717 va_list ap;
1718
1719 (void)fprintf(stderr, "%s: WARNING: ", program_name);
1720 va_start(ap, fmt);
1721 (void)vfprintf(stderr, fmt, ap);
1722 va_end(ap);
1723 if (*fmt) {
1724 fmt += strlen(fmt);
1725 if (fmt[-1] != '\n')
1726 (void)fputc('\n', stderr);
1727 }
1728 }