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