/* tethereal.c * * $Id: tethereal.c,v 1.239 2004/05/09 10:03:39 guy Exp $ * * 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. */ /* With MSVC and a libethereal.dll this file needs to import some variables in a special way. Therefore _NEED_VAR_IMPORT_ is defined. */ #define _NEED_VAR_IMPORT_ #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_LIBPCAP #include #include #endif #ifdef HAVE_SYS_STAT_H # include #endif #ifdef NEED_SNPRINTF_H # include "snprintf.h" #endif #ifdef NEED_STRERROR_H #include "strerror.h" #endif #ifdef NEED_GETOPT_H #include "getopt.h" #endif #include "cvsversion.h" #include #include #include #include "globals.h" #include #include #include "file.h" #include "disabled_protos.h" #include "prefs.h" #include "column.h" #include "print.h" #include #include "util.h" #include "version_info.h" #ifdef HAVE_LIBPCAP #include "pcap-util.h" #endif #include #include #include "register.h" #include "conditions.h" #include "capture_stop_conditions.h" #include "ringbuffer.h" #include #include "tap.h" #include #ifdef HAVE_LIBPCAP #include #include #endif #ifdef _WIN32 #include "capture-wpcap.h" #endif /* * 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 guint32 firstsec, firstusec; static guint32 prevsec, prevusec; static GString *comp_info_str, *runtime_info_str; static gboolean quiet; static gboolean decode; static gboolean verbose; static gboolean print_hex; static gboolean line_buffered; static guint32 cum_bytes = 0; static print_format_e print_format; #ifdef HAVE_LIBPCAP 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; 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; static int capture(int); static void capture_pcap_cb(guchar *, const struct pcap_pkthdr *, const guchar *); 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 */ typedef struct { capture_file *cf; wtap_dumper *pdh; } cb_args_t; static int load_cap_file(capture_file *, int); static void wtap_dispatch_cb_write(guchar *, const struct wtap_pkthdr *, long, union wtap_pseudo_header *, const guchar *); static void show_capture_file_io_error(const char *, int, gboolean); static void wtap_dispatch_cb_print(guchar *, const struct wtap_pkthdr *, long, union wtap_pseudo_header *, const guchar *); static void show_print_file_io_error(int err); static 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; #ifdef HAVE_LIBPCAP typedef struct { int snaplen; /* Maximum captured packet length */ int promisc_mode; /* Capture in promiscuous mode */ int autostop_count; /* Maximum packet count */ gboolean has_autostop_duration; /* TRUE if maximum capture duration is specified */ gint32 autostop_duration; /* Maximum capture duration */ gboolean has_autostop_filesize; /* TRUE if maximum capture file size is specified */ gint32 autostop_filesize; /* Maximum capture file size */ gboolean ringbuffer_on; /* TRUE if ring buffer in use */ guint32 ringbuffer_num_files; /* Number of ring buffer files */ gboolean has_ring_duration; /* TRUE if ring duration specified */ gint32 ringbuffer_duration; /* Switch file after n seconds */ int linktype; /* Data link type to use, or -1 for "use default" */ } capture_options; static capture_options capture_opts = { WTAP_MAX_PACKET_SIZE, /* snapshot length - default is infinite, in effect */ TRUE, /* promiscuous mode is the default */ 0, /* max packet count - default is 0, meaning infinite */ FALSE, /* maximum capture duration not specified by default */ 0, /* maximum capture duration */ FALSE, /* maximum capture file size not specified by default */ 0, /* maximum capture file size */ FALSE, /* ring buffer off by default */ RINGBUFFER_MIN_NUM_FILES, /* default number of ring buffer files */ FALSE, /* Switch ring file after some */ 0, /* specified time is off by default */ -1 /* Default to not change link type */ }; static gboolean list_link_layer_types; #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 void print_usage(gboolean print_ver) { int i; FILE *output; if (print_ver) { output = stdout; fprintf(output, "This is GNU t" PACKAGE " " VERSION #ifdef CVSVERSION " (" CVSVERSION ")" #endif "\n (C) 1998-2004 Gerald Combs " "\n%s\n%s\n", 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 [:] ] [ -c ]\n"); fprintf(output, "\t[ -d %s ] ...\n", decode_as_arg_template); fprintf(output, "\t[ -f ] [ -F ] [ -i ]\n"); fprintf(output, "\t[ -N ] [ -o ] ... [ -r ]\n"); fprintf(output, "\t[ -R ] [ -s ] [ -t