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