/* tethereal.c * * $Id$ * * Ethereal - Network traffic analyzer * By Gerald Combs * Copyright 1998 Gerald Combs * * Text-mode variant, by Gilbert Ramirez * and Guy Harris . * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifdef HAVE_CONFIG_H # include "config.h" #endif #include #include #include #include #include #include #ifdef HAVE_UNISTD_H #include #endif #include #ifdef HAVE_FCNTL_H #include #endif #include #ifdef HAVE_SYS_STAT_H # include #endif #ifdef NEED_STRERROR_H #include "strerror.h" #endif #ifdef NEED_GETOPT_H #include "getopt.h" #endif #include #include #include #include #include "globals.h" #include #include #include "file.h" #include "disabled_protos.h" #include #include #include "print.h" #include #include "util.h" #include "clopts_common.h" #include "cmdarg_err.h" #include "version_info.h" #include #include #include "register.h" #include "conditions.h" #include "capture_stop_conditions.h" #include "ringbuffer.h" #include "capture_ui_utils.h" #include #include #include #include #ifdef HAVE_LIBPCAP #include #include #include "pcap-util.h" #include #include #ifdef _WIN32 #include "capture-wpcap.h" #include "capture_errs.h" #endif /* _WIN32 */ #include "capture.h" #endif /* HAVE_LIBPCAP */ #include "epan/emem.h" /* * This is the template for the decode as option; it is shared between the * various functions that output the usage for this parameter. */ static const gchar decode_as_arg_template[] = "==,"; static nstime_t first_ts; static nstime_t prev_ts; static GString *comp_info_str, *runtime_info_str; static gboolean print_packet_info; /* TRUE if we're to print packet information */ /* * The way the packet decode is to be written. */ typedef enum { WRITE_TEXT, /* summary or detail text */ WRITE_XML /* PDML or PSML */ /* Add CSV and the like here */ } output_action_e; static output_action_e output_action; static gboolean do_dissection; /* TRUE if we have to dissect each packet */ static gboolean verbose; static gboolean print_hex; static gboolean line_buffered; static guint32 cum_bytes = 0; static print_format_e print_format = PR_FMT_TEXT; static print_stream_t *print_stream; #ifdef HAVE_LIBPCAP /* * TRUE if we're to print packet counts to keep track of captured packets. */ static gboolean print_packet_counts; typedef struct _loop_data { gboolean go; /* TRUE as long as we're supposed to keep capturing */ gint linktype; gboolean from_pipe; /* TRUE if we are capturing data from a pipe */ pcap_t *pch; char *save_file; /* Name of file to which we're writing */ wtap_dumper *pdh; jmp_buf stopenv; gboolean output_to_pipe; int packet_count; #ifndef _WIN32 gboolean modified; /* TRUE if data in the pipe uses modified pcap headers */ gboolean byte_swapped; /* TRUE if data in the pipe is byte swapped */ unsigned int bytes_to_read, bytes_read; /* Used by pipe_dispatch */ enum { STATE_EXPECT_REC_HDR, STATE_READ_REC_HDR, STATE_EXPECT_DATA, STATE_READ_DATA } pipe_state; enum { PIPOK, PIPEOF, PIPERR, PIPNEXIST } pipe_err; #endif } loop_data; static loop_data ld; #ifdef HAVE_LIBPCAP static capture_options capture_opts; #ifdef SIGINFO static gboolean infodelay; /* if TRUE, don't print capture info in SIGINFO handler */ static gboolean infoprint; /* if TRUE, print capture info after clearing infodelay */ #endif /* SIGINFO */ #endif /* HAVE_LIBPCAP */ static int capture(char *, int); static void capture_pcap_cb(u_char *, const struct pcap_pkthdr *, const u_char *); static void report_counts(void); #ifdef _WIN32 static BOOL WINAPI capture_cleanup(DWORD); #else /* _WIN32 */ static void capture_cleanup(int); #ifdef SIGINFO static void report_counts_siginfo(int); #endif /* SIGINFO */ #endif /* _WIN32 */ #endif /* HAVE_LIBPCAP */ static int load_cap_file(capture_file *, char *, int); static gboolean process_packet(capture_file *cf, wtap_dumper *pdh, long offset, const struct wtap_pkthdr *whdr, union wtap_pseudo_header *pseudo_header, const guchar *pd, int *err); static void show_capture_file_io_error(const char *, int, gboolean); static void show_print_file_io_error(int err); static gboolean write_preamble(capture_file *cf); static gboolean print_packet(capture_file *cf, epan_dissect_t *edt); static gboolean write_finale(void); static const char *cf_open_error_message(int err, gchar *err_info, gboolean for_writing, int file_type); #ifdef HAVE_LIBPCAP #ifndef _WIN32 static void adjust_header(loop_data *, struct pcap_hdr *, struct pcaprec_hdr *); static int pipe_open_live(char *, struct pcap_hdr *, loop_data *, char *, int); static int pipe_dispatch(int, loop_data *, struct pcap_hdr *, \ struct pcaprec_modified_hdr *, guchar *, char *, int); #endif /* _WIN32 */ #endif static void open_failure_message(const char *filename, int err, gboolean for_writing); static void failure_message(const char *msg_format, va_list ap); static void read_failure_message(const char *filename, int err); capture_file cfile; static void print_usage(gboolean print_ver) { int i; FILE *output; if (print_ver) { output = stdout; fprintf(output, "This is t" PACKAGE " " VERSION "%s" "\n (C) 1998-2005 Gerald Combs " "\n%s\n%s\n", svnversion, comp_info_str->str, runtime_info_str->str); } else { output = stderr; } #ifdef HAVE_LIBPCAP fprintf(output, "\nt%s [ -vh ] [ -DlLnpqSVx ] [ -a ] ...\n", PACKAGE); fprintf(output, "\t[ -b ] ...\n"); #ifdef _WIN32 fprintf(output, "\t[ -B ]\n"); #endif fprintf(output, "\t[ -c ]\n"); fprintf(output, "\t[ -d %s ] ...\n", decode_as_arg_template); fprintf(output, "\t[ -f ] [ -F ]\n"); fprintf(output, "\t[ -i ] [ -N ]\n"); fprintf(output, "\t[ -o ] ... [ -r ]\n"); fprintf(output, "\t[ -R ] [ -s ]\n"); fprintf(output, "\t[ -t