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