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