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