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