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