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