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