]> The Tcpdump Group git mirrors - tcpdump/blob - tcpdump.c
added "make check"
[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.274 2007-11-21 20:39:24 guy Exp $ (LBL)";
34 #endif
35
36 /*
37 * tcpdump - monitor tcp/ip traffic on an ethernet.
38 *
39 * First written in 1987 by Van Jacobson, Lawrence Berkeley Laboratory.
40 * Mercilessly hacked and occasionally improved since then via the
41 * combined efforts of Van, Steve McCanne and Craig Leres of LBL.
42 */
43
44 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47
48 #include <tcpdump-stdinc.h>
49
50 #ifdef WIN32
51 #include "getopt.h"
52 #include "w32_fzs.h"
53 extern int strcasecmp (const char *__s1, const char *__s2);
54 extern int SIZE_BUF;
55 #define off_t long
56 #define uint UINT
57 #endif /* WIN32 */
58
59 #ifdef HAVE_SMI_H
60 #include <smi.h>
61 #endif
62
63 #include <pcap.h>
64 #include <signal.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #include <limits.h>
69 #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 /*
1075 * We can't get statistics when reading from a file rather
1076 * than capturing from a device.
1077 */
1078 if (RFileName == NULL)
1079 (void)setsignal(SIGINFO, requestinfo);
1080 #endif
1081
1082 if (vflag > 0 && WFileName) {
1083 /*
1084 * When capturing to a file, "-v" means tcpdump should,
1085 * every 10 secodns, "v"erbosely report the number of
1086 * packets captured.
1087 */
1088 #ifdef USE_WIN32_MM_TIMER
1089 /* call verbose_stats_dump() each 1000 +/-100msec */
1090 timer_id = timeSetEvent(1000, 100, verbose_stats_dump, 0, TIME_PERIODIC);
1091 setvbuf(stderr, NULL, _IONBF, 0);
1092 #elif defined(HAVE_ALARM)
1093 (void)setsignal(SIGALRM, verbose_stats_dump);
1094 alarm(1);
1095 #endif
1096 }
1097
1098 #ifndef WIN32
1099 if (RFileName == NULL) {
1100 int dlt;
1101 const char *dlt_name;
1102
1103 if (!vflag && !WFileName) {
1104 (void)fprintf(stderr,
1105 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1106 program_name);
1107 } else
1108 (void)fprintf(stderr, "%s: ", program_name);
1109 dlt = pcap_datalink(pd);
1110 dlt_name = pcap_datalink_val_to_name(dlt);
1111 if (dlt_name == NULL) {
1112 (void)fprintf(stderr, "listening on %s, link-type %u, capture size %u bytes\n",
1113 device, dlt, snaplen);
1114 } else {
1115 (void)fprintf(stderr, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1116 device, dlt_name,
1117 pcap_datalink_val_to_description(dlt), snaplen);
1118 }
1119 (void)fflush(stderr);
1120 }
1121 #endif /* WIN32 */
1122 status = pcap_loop(pd, cnt, callback, pcap_userdata);
1123 if (WFileName == NULL) {
1124 /*
1125 * We're printing packets. Flush the printed output,
1126 * so it doesn't get intermingled with error output.
1127 */
1128 if (status == -2) {
1129 /*
1130 * We got interrupted, so perhaps we didn't
1131 * manage to finish a line we were printing.
1132 * Print an extra newline, just in case.
1133 */
1134 putchar('\n');
1135 }
1136 (void)fflush(stdout);
1137 }
1138 if (status == -1) {
1139 /*
1140 * Error. Report it.
1141 */
1142 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
1143 program_name, pcap_geterr(pd));
1144 }
1145 if (RFileName == NULL) {
1146 /*
1147 * We're doing a live capture. Report the capture
1148 * statistics.
1149 */
1150 info(1);
1151 }
1152 pcap_close(pd);
1153 exit(status == -1 ? 1 : 0);
1154 }
1155
1156 /* make a clean exit on interrupts */
1157 static RETSIGTYPE
1158 cleanup(int signo _U_)
1159 {
1160 #ifdef USE_WIN32_MM_TIMER
1161 if (timer_id)
1162 timeKillEvent(timer_id);
1163 timer_id = 0;
1164 #elif defined(HAVE_ALARM)
1165 alarm(0);
1166 #endif
1167
1168 #ifdef HAVE_PCAP_BREAKLOOP
1169 /*
1170 * We have "pcap_breakloop()"; use it, so that we do as little
1171 * as possible in the signal handler (it's probably not safe
1172 * to do anything with standard I/O streams in a signal handler -
1173 * the ANSI C standard doesn't say it is).
1174 */
1175 pcap_breakloop(pd);
1176 #else
1177 /*
1178 * We don't have "pcap_breakloop()"; this isn't safe, but
1179 * it's the best we can do. Print the summary if we're
1180 * not reading from a savefile - i.e., if we're doing a
1181 * live capture - and exit.
1182 */
1183 if (pd != NULL && pcap_file(pd) == NULL) {
1184 /*
1185 * We got interrupted, so perhaps we didn't
1186 * manage to finish a line we were printing.
1187 * Print an extra newline, just in case.
1188 */
1189 putchar('\n');
1190 (void)fflush(stdout);
1191 info(1);
1192 }
1193 exit(0);
1194 #endif
1195 }
1196
1197 /*
1198 On windows, we do not use a fork, so we do not care less about
1199 waiting a child processes to die
1200 */
1201 #ifndef WIN32
1202 static RETSIGTYPE
1203 child_cleanup(int signo _U_)
1204 {
1205 wait(NULL);
1206 }
1207 #endif /* WIN32 */
1208
1209 static void
1210 info(register int verbose)
1211 {
1212 struct pcap_stat stat;
1213
1214 if (pcap_stats(pd, &stat) < 0) {
1215 (void)fprintf(stderr, "pcap_stats: %s\n", pcap_geterr(pd));
1216 infoprint = 0;
1217 return;
1218 }
1219
1220 if (!verbose)
1221 fprintf(stderr, "%s: ", program_name);
1222
1223 (void)fprintf(stderr, "%u packets captured", packets_captured);
1224 if (!verbose)
1225 fputs(", ", stderr);
1226 else
1227 putc('\n', stderr);
1228 (void)fprintf(stderr, "%d packets received by filter", stat.ps_recv);
1229 if (!verbose)
1230 fputs(", ", stderr);
1231 else
1232 putc('\n', stderr);
1233 (void)fprintf(stderr, "%d packets dropped by kernel\n", stat.ps_drop);
1234 infoprint = 0;
1235 }
1236
1237 #ifndef WIN32
1238 static void
1239 compress_savefile(const char *filename)
1240 {
1241 if (fork())
1242 return;
1243 /*
1244 * Set to lowest priority so that this doesn't disturb the capture
1245 */
1246 #ifdef NZERO
1247 setpriority(PRIO_PROCESS, 0, NZERO - 1);
1248 #else
1249 setpriority(PRIO_PROCESS, 0, 19);
1250 #endif
1251 if (execlp(zflag, zflag, filename, NULL) == -1)
1252 fprintf(stderr,
1253 "compress_savefile:execlp(%s, %s): %s\n",
1254 zflag,
1255 filename,
1256 strerror(errno));
1257 }
1258 #else /* WIN32 */
1259 static void
1260 compress_savefile(const char *filename)
1261 {
1262 fprintf(stderr,
1263 "compress_savefile failed. Functionality not implemented under windows\n");
1264 }
1265 #endif /* WIN32 */
1266
1267 static void
1268 dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
1269 {
1270 struct dump_info *dump_info;
1271
1272 ++packets_captured;
1273
1274 ++infodelay;
1275
1276 dump_info = (struct dump_info *)user;
1277
1278 /*
1279 * XXX - this won't force the file to rotate on the specified time
1280 * boundary, but it will rotate on the first packet received after the
1281 * specified Gflag number of seconds. Note: if a Gflag time boundary
1282 * and a Cflag size boundary coincide, the time rotation will occur
1283 * first thereby cancelling the Cflag boundary (since the file should
1284 * be 0).
1285 */
1286 if (Gflag != 0) {
1287 /* Check if it is time to rotate */
1288 time_t t;
1289
1290 /* Get the current time */
1291 if ((t = time(NULL)) == (time_t)-1) {
1292 error("dump_and_trunc_packet: can't get current_time: %s",
1293 pcap_strerror(errno));
1294 }
1295
1296
1297 /* If the time is greater than the specified window, rotate */
1298 if (t - Gflag_time >= Gflag) {
1299 /* Update the Gflag_time */
1300 Gflag_time = t;
1301 /* Update Gflag_count */
1302 Gflag_count++;
1303 /*
1304 * Close the current file and open a new one.
1305 */
1306 pcap_dump_close(dump_info->p);
1307
1308 /*
1309 * Compress the file we just closed, if the user asked for it
1310 */
1311 if (zflag != NULL)
1312 compress_savefile(dump_info->CurrentFileName);
1313
1314 /*
1315 * Check to see if we've exceeded the Wflag (when
1316 * not using Cflag).
1317 */
1318 if (Cflag == 0 && Wflag > 0 && Gflag_count >= Wflag) {
1319 (void)fprintf(stderr, "Maximum file limit reached: %d\n",
1320 Wflag);
1321 exit(0);
1322 /* NOTREACHED */
1323 }
1324 if (dump_info->CurrentFileName != NULL)
1325 free(dump_info->CurrentFileName);
1326 /* Allocate space for max filename + \0. */
1327 dump_info->CurrentFileName = (char *)malloc(NAME_MAX + 1);
1328 if (dump_info->CurrentFileName == NULL)
1329 error("dump_packet_and_trunc: malloc");
1330 /*
1331 * This is always the first file in the Cflag
1332 * rotation: e.g. 0
1333 * We also don't need numbering if Cflag is not set.
1334 */
1335 if (Cflag != 0)
1336 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, 0,
1337 WflagChars);
1338 else
1339 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, 0, 0);
1340
1341 dump_info->p = pcap_dump_open(dump_info->pd, dump_info->CurrentFileName);
1342 if (dump_info->p == NULL)
1343 error("%s", pcap_geterr(pd));
1344 }
1345 }
1346
1347 /*
1348 * XXX - this won't prevent capture files from getting
1349 * larger than Cflag - the last packet written to the
1350 * file could put it over Cflag.
1351 */
1352 if (Cflag != 0 && pcap_dump_ftell(dump_info->p) > Cflag) {
1353 /*
1354 * Close the current file and open a new one.
1355 */
1356 pcap_dump_close(dump_info->p);
1357
1358 /*
1359 * Compress the file we just closed, if the user asked for it
1360 */
1361 if (zflag != NULL)
1362 compress_savefile(dump_info->CurrentFileName);
1363
1364 Cflag_count++;
1365 if (Wflag > 0) {
1366 if (Cflag_count >= Wflag)
1367 Cflag_count = 0;
1368 }
1369 if (dump_info->CurrentFileName != NULL)
1370 free(dump_info->CurrentFileName);
1371 dump_info->CurrentFileName = (char *)malloc(NAME_MAX + 1);
1372 if (dump_info->CurrentFileName == NULL)
1373 error("dump_packet_and_trunc: malloc");
1374 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, Cflag_count, WflagChars);
1375 dump_info->p = pcap_dump_open(dump_info->pd, dump_info->CurrentFileName);
1376 if (dump_info->p == NULL)
1377 error("%s", pcap_geterr(pd));
1378 }
1379
1380 pcap_dump((u_char *)dump_info->p, h, sp);
1381 #ifdef HAVE_PCAP_DUMP_FLUSH
1382 if (Uflag)
1383 pcap_dump_flush(dump_info->p);
1384 #endif
1385
1386 --infodelay;
1387 if (infoprint)
1388 info(0);
1389 }
1390
1391 static void
1392 dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
1393 {
1394 ++packets_captured;
1395
1396 ++infodelay;
1397
1398 pcap_dump(user, h, sp);
1399 #ifdef HAVE_PCAP_DUMP_FLUSH
1400 if (Uflag)
1401 pcap_dump_flush((pcap_dumper_t *)user);
1402 #endif
1403
1404 --infodelay;
1405 if (infoprint)
1406 info(0);
1407 }
1408
1409 static void
1410 print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
1411 {
1412 struct print_info *print_info;
1413 u_int hdrlen;
1414
1415 ++packets_captured;
1416
1417 ++infodelay;
1418 ts_print(&h->ts);
1419
1420 print_info = (struct print_info *)user;
1421
1422 /*
1423 * Some printers want to check that they're not walking off the
1424 * end of the packet.
1425 * Rather than pass it all the way down, we set this global.
1426 */
1427 snapend = sp + h->caplen;
1428
1429 hdrlen = (*print_info->printer)(h, sp);
1430 if (Xflag) {
1431 /*
1432 * Print the raw packet data in hex and ASCII.
1433 */
1434 if (Xflag > 1) {
1435 /*
1436 * Include the link-layer header.
1437 */
1438 hex_and_ascii_print("\n\t", sp, h->caplen);
1439 } else {
1440 /*
1441 * Don't include the link-layer header - and if
1442 * we have nothing past the link-layer header,
1443 * print nothing.
1444 */
1445 if (h->caplen > hdrlen)
1446 hex_and_ascii_print("\n\t", sp + hdrlen,
1447 h->caplen - hdrlen);
1448 }
1449 } else if (xflag) {
1450 /*
1451 * Print the raw packet data in hex.
1452 */
1453 if (xflag > 1) {
1454 /*
1455 * Include the link-layer header.
1456 */
1457 hex_print("\n\t", sp, h->caplen);
1458 } else {
1459 /*
1460 * Don't include the link-layer header - and if
1461 * we have nothing past the link-layer header,
1462 * print nothing.
1463 */
1464 if (h->caplen > hdrlen)
1465 hex_print("\n\t", sp + hdrlen,
1466 h->caplen - hdrlen);
1467 }
1468 } else if (Aflag) {
1469 /*
1470 * Print the raw packet data in ASCII.
1471 */
1472 if (Aflag > 1) {
1473 /*
1474 * Include the link-layer header.
1475 */
1476 ascii_print(sp, h->caplen);
1477 } else {
1478 /*
1479 * Don't include the link-layer header - and if
1480 * we have nothing past the link-layer header,
1481 * print nothing.
1482 */
1483 if (h->caplen > hdrlen)
1484 ascii_print(sp + hdrlen, h->caplen - hdrlen);
1485 }
1486 }
1487
1488 putchar('\n');
1489
1490 --infodelay;
1491 if (infoprint)
1492 info(0);
1493 }
1494
1495 #ifdef WIN32
1496 /*
1497 * XXX - there should really be libpcap calls to get the version
1498 * number as a string (the string would be generated from #defines
1499 * at run time, so that it's not generated from string constants
1500 * in the library, as, on many UNIX systems, those constants would
1501 * be statically linked into the application executable image, and
1502 * would thus reflect the version of libpcap on the system on
1503 * which the application was *linked*, not the system on which it's
1504 * *running*.
1505 *
1506 * That routine should be documented, unlike the "version[]"
1507 * string, so that UNIX vendors providing their own libpcaps
1508 * don't omit it (as a couple of vendors have...).
1509 *
1510 * Packet.dll should perhaps also export a routine to return the
1511 * version number of the Packet.dll code, to supply the
1512 * "Wpcap_version" information on Windows.
1513 */
1514 char WDversion[]="current-cvs.tcpdump.org";
1515 #if !defined(HAVE_GENERATED_VERSION)
1516 char version[]="current-cvs.tcpdump.org";
1517 #endif
1518 char pcap_version[]="current-cvs.tcpdump.org";
1519 char Wpcap_version[]="3.1";
1520 #endif
1521
1522 /*
1523 * By default, print the specified data out in hex and ASCII.
1524 */
1525 static void
1526 ndo_default_print(netdissect_options *ndo _U_, const u_char *bp, u_int length)
1527 {
1528 hex_and_ascii_print("\n\t", bp, length); /* pass on lf and identation string */
1529 }
1530
1531 void
1532 default_print(const u_char *bp, u_int length)
1533 {
1534 ndo_default_print(gndo, bp, length);
1535 }
1536
1537 #ifdef SIGINFO
1538 RETSIGTYPE requestinfo(int signo _U_)
1539 {
1540 if (infodelay)
1541 ++infoprint;
1542 else
1543 info(0);
1544 }
1545 #endif
1546
1547 /*
1548 * Called once each second in verbose mode while dumping to file
1549 */
1550 #ifdef USE_WIN32_MM_TIMER
1551 void CALLBACK verbose_stats_dump (UINT timer_id _U_, UINT msg _U_, DWORD_PTR arg _U_,
1552 DWORD_PTR dw1 _U_, DWORD_PTR dw2 _U_)
1553 {
1554 struct pcap_stat stat;
1555
1556 if (infodelay == 0 && pcap_stats(pd, &stat) >= 0)
1557 fprintf(stderr, "Got %u\r", packets_captured);
1558 }
1559 #elif defined(HAVE_ALARM)
1560 static void verbose_stats_dump(int sig _U_)
1561 {
1562 struct pcap_stat stat;
1563
1564 if (infodelay == 0 && pcap_stats(pd, &stat) >= 0)
1565 fprintf(stderr, "Got %u\r", packets_captured);
1566 alarm(1);
1567 }
1568 #endif
1569
1570 static void
1571 usage(void)
1572 {
1573 extern char version[];
1574 #ifndef HAVE_PCAP_LIB_VERSION
1575 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
1576 extern char pcap_version[];
1577 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1578 static char pcap_version[] = "unknown";
1579 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1580 #endif /* HAVE_PCAP_LIB_VERSION */
1581
1582 #ifdef HAVE_PCAP_LIB_VERSION
1583 #ifdef WIN32
1584 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
1585 #else /* WIN32 */
1586 (void)fprintf(stderr, "%s version %s\n", program_name, version);
1587 #endif /* WIN32 */
1588 (void)fprintf(stderr, "%s\n",pcap_lib_version());
1589 #else /* HAVE_PCAP_LIB_VERSION */
1590 #ifdef WIN32
1591 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
1592 (void)fprintf(stderr, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version, pcap_version);
1593 #else /* WIN32 */
1594 (void)fprintf(stderr, "%s version %s\n", program_name, version);
1595 (void)fprintf(stderr, "libpcap version %s\n", pcap_version);
1596 #endif /* WIN32 */
1597 #endif /* HAVE_PCAP_LIB_VERSION */
1598 (void)fprintf(stderr,
1599 "Usage: %s [-aAd" D_FLAG "efKlLnNOpqRStu" U_FLAG "vxX]" B_FLAG_USAGE " [-c count] [ -C file_size ]\n", program_name);
1600 (void)fprintf(stderr,
1601 "\t\t[ -E algo:secret ] [ -F file ] [ -G seconds ] [ -i interface ]\n");
1602 (void)fprintf(stderr,
1603 "\t\t[ -M secret ] [ -r file ] [ -s snaplen ] [ -T type ] [ -w file ]\n");
1604 (void)fprintf(stderr,
1605 "\t\t[ -W filecount ] [ -y datalinktype ] [ -z command ] [ -Z user ]\n");
1606 (void)fprintf(stderr,
1607 "\t\t[ expression ]\n");
1608 exit(1);
1609 }
1610
1611
1612
1613 /* VARARGS */
1614 static void
1615 ndo_error(netdissect_options *ndo _U_, const char *fmt, ...)
1616 {
1617 va_list ap;
1618
1619 (void)fprintf(stderr, "%s: ", program_name);
1620 va_start(ap, fmt);
1621 (void)vfprintf(stderr, fmt, ap);
1622 va_end(ap);
1623 if (*fmt) {
1624 fmt += strlen(fmt);
1625 if (fmt[-1] != '\n')
1626 (void)fputc('\n', stderr);
1627 }
1628 exit(1);
1629 /* NOTREACHED */
1630 }
1631
1632 /* VARARGS */
1633 static void
1634 ndo_warning(netdissect_options *ndo _U_, const char *fmt, ...)
1635 {
1636 va_list ap;
1637
1638 (void)fprintf(stderr, "%s: WARNING: ", program_name);
1639 va_start(ap, fmt);
1640 (void)vfprintf(stderr, fmt, ap);
1641 va_end(ap);
1642 if (*fmt) {
1643 fmt += strlen(fmt);
1644 if (fmt[-1] != '\n')
1645 (void)fputc('\n', stderr);
1646 }
1647 }
1648