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