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