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