]> The Tcpdump Group git mirrors - tcpdump/blob - tcpdump.c
from Sebastien Raveau <[email protected]>
[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.268 2006-03-23 17:33:01 hannes 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:lLm: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 'n':
672 ++nflag;
673 break;
674
675 case 'N':
676 ++Nflag;
677 break;
678
679 case 'm':
680 #ifdef LIBSMI
681 if (smiLoadModule(optarg) == 0) {
682 error("could not load MIB module %s", optarg);
683 }
684 sflag = 1;
685 #else
686 (void)fprintf(stderr, "%s: ignoring option `-m %s' ",
687 program_name, optarg);
688 (void)fprintf(stderr, "(no libsmi support)\n");
689 #endif
690 break;
691
692 case 'M':
693 /* TCP-MD5 shared secret */
694 #ifndef HAVE_LIBCRYPTO
695 warning("crypto code not compiled in");
696 #endif
697 tcpmd5secret = optarg;
698 break;
699
700 case 'O':
701 Oflag = 0;
702 break;
703
704 case 'p':
705 ++pflag;
706 break;
707
708 case 'q':
709 ++qflag;
710 ++suppress_default_print;
711 break;
712
713 case 'r':
714 RFileName = optarg;
715 break;
716
717 case 'R':
718 Rflag = 0;
719 break;
720
721 case 's': {
722 char *end;
723
724 snaplen = strtol(optarg, &end, 0);
725 if (optarg == end || *end != '\0'
726 || snaplen < 0 || snaplen > 65535)
727 error("invalid snaplen %s", optarg);
728 else if (snaplen == 0)
729 snaplen = 65535;
730 break;
731 }
732
733 case 'S':
734 ++Sflag;
735 break;
736
737 case 't':
738 ++tflag;
739 break;
740
741 case 'T':
742 if (strcasecmp(optarg, "vat") == 0)
743 packettype = PT_VAT;
744 else if (strcasecmp(optarg, "wb") == 0)
745 packettype = PT_WB;
746 else if (strcasecmp(optarg, "rpc") == 0)
747 packettype = PT_RPC;
748 else if (strcasecmp(optarg, "rtp") == 0)
749 packettype = PT_RTP;
750 else if (strcasecmp(optarg, "rtcp") == 0)
751 packettype = PT_RTCP;
752 else if (strcasecmp(optarg, "snmp") == 0)
753 packettype = PT_SNMP;
754 else if (strcasecmp(optarg, "cnfp") == 0)
755 packettype = PT_CNFP;
756 else if (strcasecmp(optarg, "tftp") == 0)
757 packettype = PT_TFTP;
758 else if (strcasecmp(optarg, "aodv") == 0)
759 packettype = PT_AODV;
760 else
761 error("unknown packet type `%s'", optarg);
762 break;
763
764 case 'u':
765 ++uflag;
766 break;
767
768 #ifdef HAVE_PCAP_DUMP_FLUSH
769 case 'U':
770 ++Uflag;
771 break;
772 #endif
773
774 case 'v':
775 ++vflag;
776 break;
777
778 case 'w':
779 WFileName = optarg;
780 break;
781
782 case 'W':
783 Wflag = atoi(optarg);
784 if (Wflag < 0)
785 error("invalid number of output files %s", optarg);
786 WflagChars = getWflagChars(Wflag);
787 break;
788
789 case 'x':
790 ++xflag;
791 ++suppress_default_print;
792 break;
793
794 case 'X':
795 ++Xflag;
796 ++suppress_default_print;
797 break;
798
799 case 'y':
800 gndo->ndo_dltname = optarg;
801 gndo->ndo_dlt =
802 pcap_datalink_name_to_val(gndo->ndo_dltname);
803 if (gndo->ndo_dlt < 0)
804 error("invalid data link type %s", gndo->ndo_dltname);
805 break;
806
807 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
808 case 'Y':
809 {
810 /* Undocumented flag */
811 #ifdef HAVE_PCAP_DEBUG
812 extern int pcap_debug;
813 pcap_debug = 1;
814 #else
815 extern int yydebug;
816 yydebug = 1;
817 #endif
818 }
819 break;
820 #endif
821 case 'z':
822 if (optarg) {
823 zflag = strdup(optarg);
824 } else {
825 usage();
826 /* NOTREACHED */
827 }
828 break;
829
830 case 'Z':
831 if (optarg) {
832 username = strdup(optarg);
833 }
834 else {
835 usage();
836 /* NOTREACHED */
837 }
838 break;
839
840 default:
841 usage();
842 /* NOTREACHED */
843 }
844
845 switch (tflag) {
846
847 case 0: /* Default */
848 case 4: /* Default + Date*/
849 thiszone = gmt2local(0);
850 break;
851
852 case 1: /* No time stamp */
853 case 2: /* Unix timeval style */
854 case 3: /* Microseconds since previous packet */
855 case 5: /* Microseconds since first packet */
856 break;
857
858 default: /* Not supported */
859 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
860 break;
861 }
862
863 #ifdef WITH_CHROOT
864 /* if run as root, prepare for chrooting */
865 if (getuid() == 0 || geteuid() == 0) {
866 /* future extensibility for cmd-line arguments */
867 if (!chroot_dir)
868 chroot_dir = WITH_CHROOT;
869 }
870 #endif
871
872 #ifdef WITH_USER
873 /* if run as root, prepare for dropping root privileges */
874 if (getuid() == 0 || geteuid() == 0) {
875 /* Run with '-Z root' to restore old behaviour */
876 if (!username)
877 username = WITH_USER;
878 }
879 #endif
880
881 if (RFileName != NULL) {
882 int dlt;
883 const char *dlt_name;
884
885 #ifndef WIN32
886 /*
887 * We don't need network access, so relinquish any set-UID
888 * or set-GID privileges we have (if any).
889 *
890 * We do *not* want set-UID privileges when opening a
891 * trace file, as that might let the user read other
892 * people's trace files (especially if we're set-UID
893 * root).
894 */
895 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
896 fprintf(stderr, "Warning: setgid/setuid failed !\n");
897 #endif /* WIN32 */
898 pd = pcap_open_offline(RFileName, ebuf);
899 if (pd == NULL)
900 error("%s", ebuf);
901 dlt = pcap_datalink(pd);
902 dlt_name = pcap_datalink_val_to_name(dlt);
903 if (dlt_name == NULL) {
904 fprintf(stderr, "reading from file %s, link-type %u\n",
905 RFileName, dlt);
906 } else {
907 fprintf(stderr,
908 "reading from file %s, link-type %s (%s)\n",
909 RFileName, dlt_name,
910 pcap_datalink_val_to_description(dlt));
911 }
912 localnet = 0;
913 netmask = 0;
914 if (fflag != 0)
915 error("-f and -r options are incompatible");
916 } else {
917 if (device == NULL) {
918 device = pcap_lookupdev(ebuf);
919 if (device == NULL)
920 error("%s", ebuf);
921 }
922 #ifdef WIN32
923 if(strlen(device) == 1) //we assume that an ASCII string is always longer than 1 char
924 { //a Unicode string has a \0 as second byte (so strlen() is 1)
925 fprintf(stderr, "%s: listening on %ws\n", program_name, device);
926 }
927 else
928 {
929 fprintf(stderr, "%s: listening on %s\n", program_name, device);
930 }
931
932 fflush(stderr);
933 #endif /* WIN32 */
934 *ebuf = '\0';
935 pd = pcap_open_live(device, snaplen, !pflag, 1000, ebuf);
936 if (pd == NULL)
937 error("%s", ebuf);
938 else if (*ebuf)
939 warning("%s", ebuf);
940 /*
941 * Let user own process after socket has been opened.
942 */
943 #ifndef WIN32
944 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
945 fprintf(stderr, "Warning: setgid/setuid failed !\n");
946 #endif /* WIN32 */
947 #ifdef WIN32
948 if(UserBufferSize != 1000000)
949 if(pcap_setbuff(pd, UserBufferSize)==-1){
950 error("%s", pcap_geterr(pd));
951 }
952 #endif /* WIN32 */
953 if (Lflag)
954 show_dlts_and_exit(pd);
955 if (gndo->ndo_dlt >= 0) {
956 #ifdef HAVE_PCAP_SET_DATALINK
957 if (pcap_set_datalink(pd, gndo->ndo_dlt) < 0)
958 error("%s", pcap_geterr(pd));
959 #else
960 /*
961 * We don't actually support changing the
962 * data link type, so we only let them
963 * set it to what it already is.
964 */
965 if (gndo->ndo_dlt != pcap_datalink(pd)) {
966 error("%s is not one of the DLTs supported by this device\n",
967 gndo->ndo_dltname);
968 }
969 #endif
970 (void)fprintf(stderr, "%s: data link type %s\n",
971 program_name, gndo->ndo_dltname);
972 (void)fflush(stderr);
973 }
974 i = pcap_snapshot(pd);
975 if (snaplen < i) {
976 warning("snaplen raised from %d to %d", snaplen, i);
977 snaplen = i;
978 }
979 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
980 localnet = 0;
981 netmask = 0;
982 warning("%s", ebuf);
983 }
984 }
985 if (infile)
986 cmdbuf = read_infile(infile);
987 else
988 cmdbuf = copy_argv(&argv[optind]);
989
990 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
991 error("%s", pcap_geterr(pd));
992 if (dflag) {
993 bpf_dump(&fcode, dflag);
994 pcap_close(pd);
995 exit(0);
996 }
997 init_addrtoname(localnet, netmask);
998 init_checksum();
999
1000 #ifndef WIN32
1001 (void)setsignal(SIGPIPE, cleanup);
1002 #endif /* WIN32 */
1003 (void)setsignal(SIGTERM, cleanup);
1004 (void)setsignal(SIGINT, cleanup);
1005 (void)setsignal(SIGCHLD, child_cleanup);
1006 /* Cooperate with nohup(1) */
1007 #ifndef WIN32
1008 if ((oldhandler = setsignal(SIGHUP, cleanup)) != SIG_DFL)
1009 (void)setsignal(SIGHUP, oldhandler);
1010 #endif /* WIN32 */
1011
1012 if (pcap_setfilter(pd, &fcode) < 0)
1013 error("%s", pcap_geterr(pd));
1014 if (WFileName) {
1015 pcap_dumper_t *p;
1016 /* Do not exceed the default NAME_MAX for files. */
1017 dumpinfo.CurrentFileName = (char *)malloc(NAME_MAX + 1);
1018
1019 if (dumpinfo.CurrentFileName == NULL)
1020 error("malloc of dumpinfo.CurrentFileName");
1021
1022 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1023 if (Cflag != 0)
1024 MakeFilename(dumpinfo.CurrentFileName, WFileName, 0, WflagChars);
1025 else
1026 MakeFilename(dumpinfo.CurrentFileName, WFileName, 0, 0);
1027
1028 p = pcap_dump_open(pd, dumpinfo.CurrentFileName);
1029 if (p == NULL)
1030 error("%s", pcap_geterr(pd));
1031 if (Cflag != 0 || Gflag != 0) {
1032 callback = dump_packet_and_trunc;
1033 dumpinfo.WFileName = WFileName;
1034 dumpinfo.pd = pd;
1035 dumpinfo.p = p;
1036 pcap_userdata = (u_char *)&dumpinfo;
1037 } else {
1038 callback = dump_packet;
1039 pcap_userdata = (u_char *)p;
1040 }
1041 } else {
1042 type = pcap_datalink(pd);
1043 printinfo.printer = lookup_printer(type);
1044 if (printinfo.printer == NULL) {
1045 gndo->ndo_dltname = pcap_datalink_val_to_name(type);
1046 if (gndo->ndo_dltname != NULL)
1047 error("unsupported data link type %s",
1048 gndo->ndo_dltname);
1049 else
1050 error("unsupported data link type %d", type);
1051 }
1052 callback = print_packet;
1053 pcap_userdata = (u_char *)&printinfo;
1054 }
1055 #ifndef WIN32
1056 /*
1057 * We cannot do this earlier, because we want to be able to open
1058 * the file (if done) for writing before giving up permissions.
1059 */
1060 if (getuid() == 0 || geteuid() == 0) {
1061 if (username || chroot_dir)
1062 droproot(username, chroot_dir);
1063 }
1064 #endif /* WIN32 */
1065 #ifdef SIGINFO
1066 (void)setsignal(SIGINFO, requestinfo);
1067 #endif
1068
1069 if (vflag > 0 && WFileName) {
1070 /*
1071 * When capturing to a file, "-v" means tcpdump should,
1072 * every 10 secodns, "v"erbosely report the number of
1073 * packets captured.
1074 */
1075 #ifdef USE_WIN32_MM_TIMER
1076 /* call verbose_stats_dump() each 1000 +/-100msec */
1077 timer_id = timeSetEvent(1000, 100, verbose_stats_dump, 0, TIME_PERIODIC);
1078 setvbuf(stderr, NULL, _IONBF, 0);
1079 #elif defined(HAVE_ALARM)
1080 (void)setsignal(SIGALRM, verbose_stats_dump);
1081 alarm(1);
1082 #endif
1083 }
1084
1085 #ifndef WIN32
1086 if (RFileName == NULL) {
1087 int dlt;
1088 const char *dlt_name;
1089
1090 if (!vflag && !WFileName) {
1091 (void)fprintf(stderr,
1092 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1093 program_name);
1094 } else
1095 (void)fprintf(stderr, "%s: ", program_name);
1096 dlt = pcap_datalink(pd);
1097 dlt_name = pcap_datalink_val_to_name(dlt);
1098 if (dlt_name == NULL) {
1099 (void)fprintf(stderr, "listening on %s, link-type %u, capture size %u bytes\n",
1100 device, dlt, snaplen);
1101 } else {
1102 (void)fprintf(stderr, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1103 device, dlt_name,
1104 pcap_datalink_val_to_description(dlt), snaplen);
1105 }
1106 (void)fflush(stderr);
1107 }
1108 #endif /* WIN32 */
1109 status = pcap_loop(pd, cnt, callback, pcap_userdata);
1110 if (WFileName == NULL) {
1111 /*
1112 * We're printing packets. Flush the printed output,
1113 * so it doesn't get intermingled with error output.
1114 */
1115 if (status == -2) {
1116 /*
1117 * We got interrupted, so perhaps we didn't
1118 * manage to finish a line we were printing.
1119 * Print an extra newline, just in case.
1120 */
1121 putchar('\n');
1122 }
1123 (void)fflush(stdout);
1124 }
1125 if (status == -1) {
1126 /*
1127 * Error. Report it.
1128 */
1129 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
1130 program_name, pcap_geterr(pd));
1131 }
1132 if (RFileName == NULL) {
1133 /*
1134 * We're doing a live capture. Report the capture
1135 * statistics.
1136 */
1137 info(1);
1138 }
1139 pcap_close(pd);
1140 exit(status == -1 ? 1 : 0);
1141 }
1142
1143 /* make a clean exit on interrupts */
1144 static RETSIGTYPE
1145 cleanup(int signo _U_)
1146 {
1147 #ifdef USE_WIN32_MM_TIMER
1148 if (timer_id)
1149 timeKillEvent(timer_id);
1150 timer_id = 0;
1151 #elif defined(HAVE_ALARM)
1152 alarm(0);
1153 #endif
1154
1155 #ifdef HAVE_PCAP_BREAKLOOP
1156 /*
1157 * We have "pcap_breakloop()"; use it, so that we do as little
1158 * as possible in the signal handler (it's probably not safe
1159 * to do anything with standard I/O streams in a signal handler -
1160 * the ANSI C standard doesn't say it is).
1161 */
1162 pcap_breakloop(pd);
1163 #else
1164 /*
1165 * We don't have "pcap_breakloop()"; this isn't safe, but
1166 * it's the best we can do. Print the summary if we're
1167 * not reading from a savefile - i.e., if we're doing a
1168 * live capture - and exit.
1169 */
1170 if (pd != NULL && pcap_file(pd) == NULL) {
1171 /*
1172 * We got interrupted, so perhaps we didn't
1173 * manage to finish a line we were printing.
1174 * Print an extra newline, just in case.
1175 */
1176 putchar('\n');
1177 (void)fflush(stdout);
1178 info(1);
1179 }
1180 exit(0);
1181 #endif
1182 }
1183
1184 static RETSIGTYPE
1185 child_cleanup(int signo _U_)
1186 {
1187 wait(NULL);
1188 }
1189
1190 static void
1191 info(register int verbose)
1192 {
1193 struct pcap_stat stat;
1194
1195 if (pcap_stats(pd, &stat) < 0) {
1196 (void)fprintf(stderr, "pcap_stats: %s\n", pcap_geterr(pd));
1197 return;
1198 }
1199
1200 if (!verbose)
1201 fprintf(stderr, "%s: ", program_name);
1202
1203 (void)fprintf(stderr, "%u packets captured", packets_captured);
1204 if (!verbose)
1205 fputs(", ", stderr);
1206 else
1207 putc('\n', stderr);
1208 (void)fprintf(stderr, "%d packets received by filter", stat.ps_recv);
1209 if (!verbose)
1210 fputs(", ", stderr);
1211 else
1212 putc('\n', stderr);
1213 (void)fprintf(stderr, "%d packets dropped by kernel\n", stat.ps_drop);
1214 infoprint = 0;
1215 }
1216
1217 static void
1218 compress_savefile(const char *filename)
1219 {
1220 if (fork())
1221 return;
1222 /*
1223 * Set to lowest priority so that this doesn't disturb the capture
1224 */
1225 #ifdef NZERO
1226 setpriority(PRIO_PROCESS, 0, NZERO - 1);
1227 #else
1228 setpriority(PRIO_PROCESS, 0, 19);
1229 #endif
1230 if (execlp(zflag, zflag, filename, NULL) == -1)
1231 fprintf(stderr,
1232 "compress_savefile:execlp(%s, %s): %s\n",
1233 zflag,
1234 filename,
1235 strerror(errno));
1236 }
1237
1238 static void
1239 dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
1240 {
1241 struct dump_info *dump_info;
1242
1243 ++packets_captured;
1244
1245 ++infodelay;
1246
1247 dump_info = (struct dump_info *)user;
1248
1249 /*
1250 * XXX - this won't force the file to rotate on the specified time
1251 * boundary, but it will rotate on the first packet received after the
1252 * specified Gflag number of seconds. Note: if a Gflag time boundary
1253 * and a Cflag size boundary coincide, the time rotation will occur
1254 * first thereby cancelling the Cflag boundary (since the file should
1255 * be 0).
1256 */
1257 if (Gflag != 0) {
1258 /* Check if it is time to rotate */
1259 time_t t;
1260
1261 /* Get the current time */
1262 if ((t = time(NULL)) == (time_t)-1) {
1263 error("dump_and_trunc_packet: can't get current_time: %s",
1264 pcap_strerror(errno));
1265 }
1266
1267
1268 /* If the time is greater than the specified window, rotate */
1269 if (t - Gflag_time >= Gflag) {
1270 /* Update the Gflag_time */
1271 Gflag_time = t;
1272 /* Update Gflag_count */
1273 Gflag_count++;
1274 /*
1275 * Close the current file and open a new one.
1276 */
1277 pcap_dump_close(dump_info->p);
1278
1279 /*
1280 * Compress the file we just closed, if the user asked for it
1281 */
1282 if (zflag != NULL)
1283 compress_savefile(dump_info->CurrentFileName);
1284
1285 /*
1286 * Check to see if we've exceeded the Wflag (when
1287 * not using Cflag).
1288 */
1289 if (Cflag == 0 && Wflag > 0 && Gflag_count >= Wflag) {
1290 (void)fprintf(stderr, "Maximum file limit reached: %d\n",
1291 Wflag);
1292 exit(0);
1293 /* NOTREACHED */
1294 }
1295 if (dump_info->CurrentFileName != NULL)
1296 free(dump_info->CurrentFileName);
1297 /* Allocate space for max filename + \0. */
1298 dump_info->CurrentFileName = (char *)malloc(NAME_MAX + 1);
1299 if (dump_info->CurrentFileName == NULL)
1300 error("dump_packet_and_trunc: malloc");
1301 /*
1302 * This is always the first file in the Cflag
1303 * rotation: e.g. 0
1304 * We also don't need numbering if Cflag is not set.
1305 */
1306 if (Cflag != 0)
1307 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, 0,
1308 WflagChars);
1309 else
1310 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, 0, 0);
1311
1312 dump_info->p = pcap_dump_open(dump_info->pd, dump_info->CurrentFileName);
1313 if (dump_info->p == NULL)
1314 error("%s", pcap_geterr(pd));
1315 }
1316 }
1317
1318 /*
1319 * XXX - this won't prevent capture files from getting
1320 * larger than Cflag - the last packet written to the
1321 * file could put it over Cflag.
1322 */
1323 if (Cflag != 0 && pcap_dump_ftell(dump_info->p) > Cflag) {
1324 /*
1325 * Close the current file and open a new one.
1326 */
1327 pcap_dump_close(dump_info->p);
1328
1329 /*
1330 * Compress the file we just closed, if the user asked for it
1331 */
1332 if (zflag != NULL)
1333 compress_savefile(dump_info->CurrentFileName);
1334
1335 Cflag_count++;
1336 if (Wflag > 0) {
1337 if (Cflag_count >= Wflag)
1338 Cflag_count = 0;
1339 }
1340 if (dump_info->CurrentFileName != NULL)
1341 free(dump_info->CurrentFileName);
1342 dump_info->CurrentFileName = (char *)malloc(NAME_MAX + 1);
1343 if (dump_info->CurrentFileName == NULL)
1344 error("dump_packet_and_trunc: malloc");
1345 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, Cflag_count, WflagChars);
1346 dump_info->p = pcap_dump_open(dump_info->pd, dump_info->CurrentFileName);
1347 if (dump_info->p == NULL)
1348 error("%s", pcap_geterr(pd));
1349 }
1350
1351 pcap_dump((u_char *)dump_info->p, h, sp);
1352 #ifdef HAVE_PCAP_DUMP_FLUSH
1353 if (Uflag)
1354 pcap_dump_flush(dump_info->p);
1355 #endif
1356
1357 --infodelay;
1358 if (infoprint)
1359 info(0);
1360 }
1361
1362 static void
1363 dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
1364 {
1365 ++packets_captured;
1366
1367 ++infodelay;
1368
1369 pcap_dump(user, h, sp);
1370 #ifdef HAVE_PCAP_DUMP_FLUSH
1371 if (Uflag)
1372 pcap_dump_flush((pcap_dumper_t *)user);
1373 #endif
1374
1375 --infodelay;
1376 if (infoprint)
1377 info(0);
1378 }
1379
1380 static void
1381 print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
1382 {
1383 struct print_info *print_info;
1384 u_int hdrlen;
1385
1386 ++packets_captured;
1387
1388 ++infodelay;
1389 ts_print(&h->ts);
1390
1391 print_info = (struct print_info *)user;
1392
1393 /*
1394 * Some printers want to check that they're not walking off the
1395 * end of the packet.
1396 * Rather than pass it all the way down, we set this global.
1397 */
1398 snapend = sp + h->caplen;
1399
1400 hdrlen = (*print_info->printer)(h, sp);
1401 if (Xflag) {
1402 /*
1403 * Print the raw packet data in hex and ASCII.
1404 */
1405 if (Xflag > 1) {
1406 /*
1407 * Include the link-layer header.
1408 */
1409 hex_and_ascii_print("\n\t", sp, h->caplen);
1410 } else {
1411 /*
1412 * Don't include the link-layer header - and if
1413 * we have nothing past the link-layer header,
1414 * print nothing.
1415 */
1416 if (h->caplen > hdrlen)
1417 hex_and_ascii_print("\n\t", sp + hdrlen,
1418 h->caplen - hdrlen);
1419 }
1420 } else if (xflag) {
1421 /*
1422 * Print the raw packet data in hex.
1423 */
1424 if (xflag > 1) {
1425 /*
1426 * Include the link-layer header.
1427 */
1428 hex_print("\n\t", sp, h->caplen);
1429 } else {
1430 /*
1431 * Don't include the link-layer header - and if
1432 * we have nothing past the link-layer header,
1433 * print nothing.
1434 */
1435 if (h->caplen > hdrlen)
1436 hex_print("\n\t", sp + hdrlen,
1437 h->caplen - hdrlen);
1438 }
1439 } else if (Aflag) {
1440 /*
1441 * Print the raw packet data in ASCII.
1442 */
1443 if (Aflag > 1) {
1444 /*
1445 * Include the link-layer header.
1446 */
1447 ascii_print(sp, h->caplen);
1448 } else {
1449 /*
1450 * Don't include the link-layer header - and if
1451 * we have nothing past the link-layer header,
1452 * print nothing.
1453 */
1454 if (h->caplen > hdrlen)
1455 ascii_print(sp + hdrlen, h->caplen - hdrlen);
1456 }
1457 }
1458
1459 putchar('\n');
1460
1461 --infodelay;
1462 if (infoprint)
1463 info(0);
1464 }
1465
1466 #ifdef WIN32
1467 /*
1468 * XXX - there should really be libpcap calls to get the version
1469 * number as a string (the string would be generated from #defines
1470 * at run time, so that it's not generated from string constants
1471 * in the library, as, on many UNIX systems, those constants would
1472 * be statically linked into the application executable image, and
1473 * would thus reflect the version of libpcap on the system on
1474 * which the application was *linked*, not the system on which it's
1475 * *running*.
1476 *
1477 * That routine should be documented, unlike the "version[]"
1478 * string, so that UNIX vendors providing their own libpcaps
1479 * don't omit it (as a couple of vendors have...).
1480 *
1481 * Packet.dll should perhaps also export a routine to return the
1482 * version number of the Packet.dll code, to supply the
1483 * "Wpcap_version" information on Windows.
1484 */
1485 char WDversion[]="current-cvs.tcpdump.org";
1486 #if !defined(HAVE_GENERATED_VERSION)
1487 char version[]="current-cvs.tcpdump.org";
1488 #endif
1489 char pcap_version[]="current-cvs.tcpdump.org";
1490 char Wpcap_version[]="3.1";
1491 #endif
1492
1493 /*
1494 * By default, print the specified data out in hex and ASCII.
1495 */
1496 static void
1497 ndo_default_print(netdissect_options *ndo _U_, const u_char *bp, u_int length)
1498 {
1499 hex_and_ascii_print("\n\t", bp, length); /* pass on lf and identation string */
1500 }
1501
1502 void
1503 default_print(const u_char *bp, u_int length)
1504 {
1505 ndo_default_print(gndo, bp, length);
1506 }
1507
1508 #ifdef SIGINFO
1509 RETSIGTYPE requestinfo(int signo _U_)
1510 {
1511 if (infodelay)
1512 ++infoprint;
1513 else
1514 info(0);
1515 }
1516 #endif
1517
1518 /*
1519 * Called once each second in verbose mode while dumping to file
1520 */
1521 #ifdef USE_WIN32_MM_TIMER
1522 void CALLBACK verbose_stats_dump (UINT timer_id _U_, UINT msg _U_, DWORD_PTR arg _U_,
1523 DWORD_PTR dw1 _U_, DWORD_PTR dw2 _U_)
1524 {
1525 struct pcap_stat stat;
1526
1527 if (infodelay == 0 && pcap_stats(pd, &stat) >= 0)
1528 fprintf(stderr, "Got %u\r", packets_captured);
1529 }
1530 #elif defined(HAVE_ALARM)
1531 static void verbose_stats_dump(int sig _U_)
1532 {
1533 struct pcap_stat stat;
1534
1535 if (infodelay == 0 && pcap_stats(pd, &stat) >= 0)
1536 fprintf(stderr, "Got %u\r", packets_captured);
1537 alarm(1);
1538 }
1539 #endif
1540
1541 static void
1542 usage(void)
1543 {
1544 extern char version[];
1545 #ifndef HAVE_PCAP_LIB_VERSION
1546 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
1547 extern char pcap_version[];
1548 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1549 static char pcap_version[] = "unknown";
1550 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1551 #endif /* HAVE_PCAP_LIB_VERSION */
1552
1553 #ifdef HAVE_PCAP_LIB_VERSION
1554 #ifdef WIN32
1555 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
1556 #else /* WIN32 */
1557 (void)fprintf(stderr, "%s version %s\n", program_name, version);
1558 #endif /* WIN32 */
1559 (void)fprintf(stderr, "%s\n",pcap_lib_version());
1560 #else /* 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 (void)fprintf(stderr, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version, pcap_version);
1564 #else /* WIN32 */
1565 (void)fprintf(stderr, "%s version %s\n", program_name, version);
1566 (void)fprintf(stderr, "libpcap version %s\n", pcap_version);
1567 #endif /* WIN32 */
1568 #endif /* HAVE_PCAP_LIB_VERSION */
1569 (void)fprintf(stderr,
1570 "Usage: %s [-aAd" D_FLAG "eflLnNOpqRStu" U_FLAG "vxX]" B_FLAG_USAGE " [-c count] [ -C file_size ]\n", program_name);
1571 (void)fprintf(stderr,
1572 "\t\t[ -E algo:secret ] [ -F file ] [ -G seconds ] [ -i interface ]\n");
1573 (void)fprintf(stderr,
1574 "\t\t[ -M secret ] [ -r file ] [ -s snaplen ] [ -T type ] [ -w file ]\n");
1575 (void)fprintf(stderr,
1576 "\t\t[ -W filecount ] [ -y datalinktype ] [ -z command ] [ -Z user ]\n");
1577 (void)fprintf(stderr,
1578 "\t\t[ expression ]\n");
1579 exit(1);
1580 }
1581
1582
1583
1584 /* VARARGS */
1585 static void
1586 ndo_error(netdissect_options *ndo _U_, const char *fmt, ...)
1587 {
1588 va_list ap;
1589
1590 (void)fprintf(stderr, "%s: ", program_name);
1591 va_start(ap, fmt);
1592 (void)vfprintf(stderr, fmt, ap);
1593 va_end(ap);
1594 if (*fmt) {
1595 fmt += strlen(fmt);
1596 if (fmt[-1] != '\n')
1597 (void)fputc('\n', stderr);
1598 }
1599 exit(1);
1600 /* NOTREACHED */
1601 }
1602
1603 /* VARARGS */
1604 static void
1605 ndo_warning(netdissect_options *ndo _U_, const char *fmt, ...)
1606 {
1607 va_list ap;
1608
1609 (void)fprintf(stderr, "%s: WARNING: ", program_name);
1610 va_start(ap, fmt);
1611 (void)vfprintf(stderr, fmt, ap);
1612 va_end(ap);
1613 if (*fmt) {
1614 fmt += strlen(fmt);
1615 if (fmt[-1] != '\n')
1616 (void)fputc('\n', stderr);
1617 }
1618 }
1619