/* tethereal.c * * $Id: tethereal.c,v 1.151 2002/08/02 23:36:07 jmayer 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. */ #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_LIBZ #include /* to get the libz version number */ #endif #ifdef NEED_SNPRINTF_H # include "snprintf.h" #endif #ifdef HAVE_UCD_SNMP_VERSION_H #include #endif /* HAVE_UCD_SNMP_VERSION_H */ #ifdef NEED_STRERROR_H #include "strerror.h" #endif #ifdef NEED_GETOPT_H #include "getopt.h" #endif #include #include #include #include "globals.h" #include #include #include "file.h" #include "prefs.h" #include "column.h" #include "print.h" #include #include "util.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 #ifdef HAVE_LIBPCAP #include #endif #ifdef WIN32 #include "capture-wpcap.h" #endif static guint32 firstsec, firstusec; static guint32 prevsec, prevusec; static GString *comp_info_str; static gboolean quiet; static gboolean verbose; static gboolean print_hex; static gboolean line_buffered; #ifdef HAVE_LIBPCAP typedef struct _loop_data { gboolean go; /* TRUE as long as we're supposed to keep capturing */ gint linktype; pcap_t *pch; wtap_dumper *pdh; jmp_buf stopenv; gboolean output_to_pipe; int packet_count; } loop_data; static loop_data ld; static int capture(int); static void capture_pcap_cb(guchar *, const struct pcap_pkthdr *, const guchar *); #ifdef _WIN32 static BOOL WINAPI capture_cleanup(DWORD); #else /* _WIN32 */ static void capture_cleanup(int); #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 *); capture_file cfile; ts_type timestamp_type = RELATIVE; #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 */ } 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 */ }; #endif static void print_usage(gboolean print_ver) { int i; if (print_ver) { fprintf(stderr, "This is GNU t%s %s, compiled %s\n", PACKAGE, VERSION, comp_info_str->str); } #ifdef HAVE_LIBPCAP fprintf(stderr, "\nt%s [ -DvVhqlp ] [ -a ] ...\n", PACKAGE); fprintf(stderr, "\t[ -b ] [ -c ]\n"); fprintf(stderr, "\t[ -f ] [ -F ]\n"); fprintf(stderr, "\t[ -i ] [ -n ] [ -N ]\n"); fprintf(stderr, "\t[ -o ] ... [ -r ] [ -R ]\n"); fprintf(stderr, "\t[ -s ] [ -t