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