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