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