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