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