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