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