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