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