2005-11-28 08:54:52 +00:00
|
|
|
/* dumpcap.c
|
|
|
|
*
|
2006-05-21 05:12:17 +00:00
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
2005-11-28 08:54:52 +00:00
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
|
|
|
* 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
|
2012-06-28 22:56:06 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2005-11-28 08:54:52 +00:00
|
|
|
*/
|
|
|
|
|
2014-08-22 21:13:05 +00:00
|
|
|
#include <config.h>
|
2005-11-28 08:54:52 +00:00
|
|
|
|
2009-08-27 02:53:13 +00:00
|
|
|
#include <stdio.h>
|
2006-11-02 09:45:12 +00:00
|
|
|
#include <stdlib.h> /* for exit() */
|
2005-11-28 08:54:52 +00:00
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2008-02-16 02:39:58 +00:00
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
|
|
|
# include <sys/types.h>
|
|
|
|
#endif
|
|
|
|
|
2012-06-18 21:50:59 +00:00
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_NETINET_IN_H
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#endif
|
|
|
|
|
2013-07-24 20:42:39 +00:00
|
|
|
#ifdef HAVE_GETOPT_H
|
|
|
|
#include <getopt.h>
|
|
|
|
#endif
|
|
|
|
|
2010-05-07 19:24:32 +00:00
|
|
|
#ifdef HAVE_ARPA_INET_H
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
|
2009-11-26 04:17:18 +00:00
|
|
|
#if defined(__APPLE__) && defined(__LP64__)
|
|
|
|
#include <sys/utsname.h>
|
|
|
|
#endif
|
|
|
|
|
2007-11-21 22:49:02 +00:00
|
|
|
#include <signal.h>
|
2008-02-16 02:39:58 +00:00
|
|
|
#include <errno.h>
|
2007-11-21 22:49:02 +00:00
|
|
|
|
2014-06-29 21:37:21 +00:00
|
|
|
#include <wsutil/cmdarg_err.h>
|
2013-01-15 21:54:41 +00:00
|
|
|
#include <wsutil/crash_info.h>
|
2016-09-05 13:45:21 +00:00
|
|
|
#include <wsutil/strtoi.h>
|
2016-04-19 07:03:08 +00:00
|
|
|
#include <ws_version_info.h>
|
2013-01-15 21:54:41 +00:00
|
|
|
|
2014-12-25 01:10:40 +00:00
|
|
|
#ifndef HAVE_GETOPT_LONG
|
2010-05-28 20:19:55 +00:00
|
|
|
#include "wsutil/wsgetopt.h"
|
2005-11-28 08:54:52 +00:00
|
|
|
#endif
|
|
|
|
|
2005-12-04 23:50:57 +00:00
|
|
|
#ifdef HAVE_NETDB_H
|
|
|
|
#include <netdb.h>
|
|
|
|
#endif
|
|
|
|
|
2007-11-20 16:53:01 +00:00
|
|
|
#ifdef HAVE_LIBCAP
|
|
|
|
# include <sys/prctl.h>
|
|
|
|
# include <sys/capability.h>
|
|
|
|
#endif
|
|
|
|
|
2005-11-28 08:54:52 +00:00
|
|
|
#include "ringbuffer.h"
|
|
|
|
|
2014-07-04 07:24:02 +00:00
|
|
|
#include "caputils/capture_ifinfo.h"
|
|
|
|
#include "caputils/capture-pcap-util.h"
|
|
|
|
#include "caputils/capture-pcap-util-int.h"
|
2011-12-29 21:59:24 +00:00
|
|
|
#ifdef _WIN32
|
2014-07-04 07:24:02 +00:00
|
|
|
#include "caputils/capture-wpcap.h"
|
2011-12-29 21:59:24 +00:00
|
|
|
#endif /* _WIN32 */
|
2005-11-28 08:54:52 +00:00
|
|
|
|
2016-03-24 20:00:07 +00:00
|
|
|
#include "writecap/pcapio.h"
|
2010-03-04 07:19:43 +00:00
|
|
|
|
2005-11-28 08:54:52 +00:00
|
|
|
#ifdef _WIN32
|
2008-06-30 17:16:29 +00:00
|
|
|
#include <wsutil/unicode-utils.h>
|
2007-10-26 16:32:28 +00:00
|
|
|
#endif
|
|
|
|
|
2010-03-13 00:29:30 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <sys/un.h>
|
|
|
|
#endif
|
|
|
|
|
2014-06-29 21:37:21 +00:00
|
|
|
#include <wsutil/clopts_common.h>
|
2008-06-30 17:16:29 +00:00
|
|
|
#include <wsutil/privileges.h>
|
2007-11-20 16:53:01 +00:00
|
|
|
|
2006-04-03 01:56:53 +00:00
|
|
|
#include "sync_pipe.h"
|
|
|
|
|
2008-02-16 08:44:33 +00:00
|
|
|
#include "capture_opts.h"
|
2014-06-30 05:08:59 +00:00
|
|
|
#include <capchild/capture_session.h>
|
2014-06-24 00:27:28 +00:00
|
|
|
#include <capchild/capture_sync.h>
|
2005-12-04 02:04:18 +00:00
|
|
|
|
2008-02-16 02:39:58 +00:00
|
|
|
#include "conditions.h"
|
|
|
|
#include "capture_stop_conditions.h"
|
|
|
|
|
2013-06-26 01:14:35 +00:00
|
|
|
#include "wsutil/tempfile.h"
|
2005-11-28 08:54:52 +00:00
|
|
|
#include "log.h"
|
2008-05-22 15:46:27 +00:00
|
|
|
#include "wsutil/file_util.h"
|
2016-09-22 11:23:37 +00:00
|
|
|
#include "wsutil/cpu_info.h"
|
2014-06-21 18:01:19 +00:00
|
|
|
#include "wsutil/os_version_info.h"
|
2015-11-04 08:45:54 +00:00
|
|
|
#include "wsutil/str_util.h"
|
2016-02-10 09:11:12 +00:00
|
|
|
#include "wsutil/inet_addr.h"
|
2005-11-28 08:54:52 +00:00
|
|
|
|
2014-07-04 07:24:02 +00:00
|
|
|
#include "caputils/ws80211_utils.h"
|
2012-06-24 19:45:49 +00:00
|
|
|
|
2015-04-10 17:04:22 +00:00
|
|
|
#ifdef HAVE_EXTCAP
|
|
|
|
#include "extcap.h"
|
|
|
|
#endif
|
|
|
|
|
2008-02-16 02:39:58 +00:00
|
|
|
/*
|
|
|
|
* Get information about libpcap format from "wiretap/libpcap.h".
|
2016-07-09 07:07:52 +00:00
|
|
|
* Get information about pcapng format from "wiretap/pcapng_module.h".
|
2008-02-16 02:39:58 +00:00
|
|
|
* XXX - can we just use pcap_open_offline() to read the pipe?
|
|
|
|
*/
|
|
|
|
#include "wiretap/libpcap.h"
|
2016-07-09 07:07:52 +00:00
|
|
|
#include "wiretap/pcapng_module.h"
|
2005-11-28 08:54:52 +00:00
|
|
|
|
2011-05-19 05:51:52 +00:00
|
|
|
/**#define DEBUG_DUMPCAP**/
|
2011-05-16 00:19:47 +00:00
|
|
|
/**#define DEBUG_CHILD_DUMPCAP**/
|
2008-02-23 19:59:38 +00:00
|
|
|
|
2011-05-16 19:56:27 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#ifdef DEBUG_DUMPCAP
|
|
|
|
#include <conio.h> /* _getch() */
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2008-02-23 19:59:38 +00:00
|
|
|
#ifdef DEBUG_CHILD_DUMPCAP
|
2008-02-25 20:43:03 +00:00
|
|
|
FILE *debug_log; /* for logging debug messages to */
|
2008-02-23 19:59:38 +00:00
|
|
|
/* a file if DEBUG_CHILD_DUMPCAP */
|
|
|
|
/* is defined */
|
|
|
|
#endif
|
2005-11-28 08:54:52 +00:00
|
|
|
|
2011-05-16 21:56:12 +00:00
|
|
|
static GAsyncQueue *pcap_queue;
|
2011-05-19 05:49:46 +00:00
|
|
|
static gint64 pcap_queue_bytes;
|
|
|
|
static gint64 pcap_queue_packets;
|
2013-03-10 11:57:40 +00:00
|
|
|
static gint64 pcap_queue_byte_limit = 0;
|
|
|
|
static gint64 pcap_queue_packet_limit = 0;
|
2011-05-16 21:56:12 +00:00
|
|
|
|
2008-02-16 02:39:58 +00:00
|
|
|
static gboolean capture_child = FALSE; /* FALSE: standalone call, TRUE: this is an Wireshark capture child */
|
2007-10-26 16:32:28 +00:00
|
|
|
#ifdef _WIN32
|
2008-02-16 02:39:58 +00:00
|
|
|
static gchar *sig_pipe_name = NULL;
|
|
|
|
static HANDLE sig_pipe_handle = NULL;
|
2009-08-26 23:16:37 +00:00
|
|
|
static gboolean signal_pipe_check_running(void);
|
|
|
|
#endif
|
|
|
|
|
2010-05-26 00:19:27 +00:00
|
|
|
#ifdef SIGINFO
|
2011-03-21 16:57:11 +00:00
|
|
|
static gboolean infodelay; /* if TRUE, don't print capture info in SIGINFO handler */
|
|
|
|
static gboolean infoprint; /* if TRUE, print capture info after clearing infodelay */
|
2010-05-26 00:19:27 +00:00
|
|
|
#endif /* SIGINFO */
|
|
|
|
|
2008-02-16 02:39:58 +00:00
|
|
|
/** Stop a low-level capture (stops the capture child). */
|
|
|
|
static void capture_loop_stop(void);
|
2012-06-09 03:36:15 +00:00
|
|
|
/** Close a pipe, or socket if \a from_socket is TRUE */
|
|
|
|
static void cap_pipe_close(int pipe_fd, gboolean from_socket _U_);
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2013-08-22 17:36:30 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
/*
|
|
|
|
* Enable kernel BPF JIT compiler if available.
|
|
|
|
* If any calls fail, just drive on - the JIT compiler might not be
|
|
|
|
* enabled, but filtering will still work, and it's not clear what
|
|
|
|
* we could do if the calls fail; should we just report the error
|
|
|
|
* and not continue to capture, should we report it as a warning, or
|
|
|
|
* what?
|
|
|
|
*/
|
2014-01-17 19:35:57 +00:00
|
|
|
static void
|
2013-08-22 17:36:30 +00:00
|
|
|
enable_kernel_bpf_jit_compiler(void)
|
2013-08-22 17:20:09 +00:00
|
|
|
{
|
|
|
|
int fd;
|
2013-08-22 18:49:31 +00:00
|
|
|
ssize_t written _U_;
|
2013-08-22 17:36:30 +00:00
|
|
|
static const char file[] = "/proc/sys/net/core/bpf_jit_enable";
|
2013-08-22 17:20:09 +00:00
|
|
|
|
2014-02-25 13:05:11 +00:00
|
|
|
fd = ws_open(file, O_WRONLY);
|
2013-08-22 17:20:09 +00:00
|
|
|
if (fd < 0)
|
2013-08-22 17:36:30 +00:00
|
|
|
return;
|
2013-08-22 17:20:09 +00:00
|
|
|
|
2015-11-08 10:31:01 +00:00
|
|
|
written = ws_write(fd, "1", strlen("1"));
|
2013-08-22 17:20:09 +00:00
|
|
|
|
2015-11-08 10:31:01 +00:00
|
|
|
ws_close(fd);
|
2013-08-22 17:20:09 +00:00
|
|
|
}
|
2013-08-22 17:36:30 +00:00
|
|
|
#endif
|
2013-08-22 17:20:09 +00:00
|
|
|
|
2008-02-16 02:39:58 +00:00
|
|
|
#if !defined (__linux__)
|
|
|
|
#ifndef HAVE_PCAP_BREAKLOOP
|
|
|
|
/*
|
|
|
|
* We don't have pcap_breakloop(), which is the only way to ensure that
|
|
|
|
* pcap_dispatch(), pcap_loop(), or even pcap_next() or pcap_next_ex()
|
|
|
|
* won't, if the call to read the next packet or batch of packets is
|
|
|
|
* is interrupted by a signal on UN*X, just go back and try again to
|
|
|
|
* read again.
|
|
|
|
*
|
2009-09-13 17:46:10 +00:00
|
|
|
* On UN*X, we catch SIGINT as a "stop capturing" signal, and, in
|
2008-02-16 02:39:58 +00:00
|
|
|
* the signal handler, set a flag to stop capturing; however, without
|
|
|
|
* a guarantee of that sort, we can't guarantee that we'll stop capturing
|
|
|
|
* if the read will be retried and won't time out if no packets arrive.
|
|
|
|
*
|
|
|
|
* Therefore, on at least some platforms, we work around the lack of
|
|
|
|
* pcap_breakloop() by doing a select() on the pcap_t's file descriptor
|
|
|
|
* to wait for packets to arrive, so that we're probably going to be
|
|
|
|
* blocked in the select() when the signal arrives, and can just bail
|
|
|
|
* out of the loop at that point.
|
|
|
|
*
|
2011-02-24 19:26:38 +00:00
|
|
|
* However, we don't want to do that on BSD (because "select()" doesn't work
|
2008-02-16 02:39:58 +00:00
|
|
|
* correctly on BPF devices on at least some releases of some flavors of
|
|
|
|
* BSD), and we don't want to do it on Windows (because "select()" is
|
|
|
|
* something for sockets, not for arbitrary handles). (Note that "Windows"
|
|
|
|
* here includes Cygwin; even in its pretend-it's-UNIX environment, we're
|
|
|
|
* using WinPcap, not a UNIX libpcap.)
|
|
|
|
*
|
|
|
|
* Fortunately, we don't need to do it on BSD, because the libpcap timeout
|
|
|
|
* on BSD times out even if no packets have arrived, so we'll eventually
|
|
|
|
* exit pcap_dispatch() with an indication that no packets have arrived,
|
|
|
|
* and will break out of the capture loop at that point.
|
|
|
|
*
|
2009-09-13 17:46:10 +00:00
|
|
|
* On Windows, we can't send a SIGINT to stop capturing, so none of this
|
2008-02-16 02:39:58 +00:00
|
|
|
* applies in any case.
|
|
|
|
*
|
|
|
|
* XXX - the various BSDs appear to define BSD in <sys/param.h>; we don't
|
|
|
|
* want to include it if it's not present on this platform, however.
|
|
|
|
*/
|
|
|
|
# if !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__) && \
|
|
|
|
!defined(__bsdi__) && !defined(__APPLE__) && !defined(_WIN32) && \
|
|
|
|
!defined(__CYGWIN__)
|
|
|
|
# define MUST_DO_SELECT
|
|
|
|
# endif /* avoid select */
|
|
|
|
#endif /* HAVE_PCAP_BREAKLOOP */
|
|
|
|
#else /* linux */
|
|
|
|
/* whatever the deal with pcap_breakloop, linux doesn't support timeouts
|
|
|
|
* in pcap_dispatch(); on the other hand, select() works just fine there.
|
|
|
|
* Hence we use a select for that come what may.
|
2013-08-22 17:36:30 +00:00
|
|
|
*
|
|
|
|
* XXX - with TPACKET_V1 and TPACKET_V2, it currently uses select()
|
|
|
|
* internally, and, with TPACKET_V3, once that's supported, it'll
|
|
|
|
* support timeouts, at least as I understand the way the code works.
|
2008-02-16 02:39:58 +00:00
|
|
|
*/
|
|
|
|
#define MUST_DO_SELECT
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/** init the capture filter */
|
|
|
|
typedef enum {
|
2011-03-21 16:57:11 +00:00
|
|
|
INITFILTER_NO_ERROR,
|
|
|
|
INITFILTER_BAD_FILTER,
|
|
|
|
INITFILTER_OTHER_ERROR
|
2008-02-16 02:39:58 +00:00
|
|
|
} initfilter_status_t;
|
|
|
|
|
2013-03-03 17:14:19 +00:00
|
|
|
typedef enum {
|
|
|
|
STATE_EXPECT_REC_HDR,
|
|
|
|
STATE_READ_REC_HDR,
|
|
|
|
STATE_EXPECT_DATA,
|
|
|
|
STATE_READ_DATA
|
|
|
|
} cap_pipe_state_t;
|
2013-06-27 17:10:50 +00:00
|
|
|
|
2013-07-15 15:51:45 +00:00
|
|
|
typedef enum {
|
|
|
|
PIPOK,
|
|
|
|
PIPEOF,
|
|
|
|
PIPERR,
|
|
|
|
PIPNEXIST
|
2013-03-03 17:14:19 +00:00
|
|
|
} cap_pipe_err_t;
|
2013-06-27 17:10:50 +00:00
|
|
|
|
2011-05-15 22:54:52 +00:00
|
|
|
typedef struct _pcap_options {
|
2012-11-25 18:35:41 +00:00
|
|
|
guint32 received;
|
|
|
|
guint32 dropped;
|
2013-03-10 11:57:40 +00:00
|
|
|
guint32 flushed;
|
2012-11-25 18:35:41 +00:00
|
|
|
pcap_t *pcap_h;
|
2008-02-16 02:39:58 +00:00
|
|
|
#ifdef MUST_DO_SELECT
|
2013-01-22 08:28:46 +00:00
|
|
|
int pcap_fd; /**< pcap file descriptor */
|
2012-11-25 18:35:41 +00:00
|
|
|
#endif
|
|
|
|
gboolean pcap_err;
|
|
|
|
guint interface_id;
|
|
|
|
GThread *tid;
|
|
|
|
int snaplen;
|
|
|
|
int linktype;
|
2013-01-22 08:28:46 +00:00
|
|
|
gboolean ts_nsec; /**< TRUE if we're using nanosecond precision. */
|
|
|
|
/**< capture pipe (unix only "input file") */
|
|
|
|
gboolean from_cap_pipe; /**< TRUE if we are capturing data from a capture pipe */
|
|
|
|
gboolean from_cap_socket; /**< TRUE if we're capturing from socket */
|
|
|
|
struct pcap_hdr cap_pipe_hdr; /**< Pcap header when capturing from a pipe */
|
|
|
|
struct pcaprec_modified_hdr cap_pipe_rechdr; /**< Pcap record header when capturing from a pipe */
|
2009-08-26 23:16:37 +00:00
|
|
|
#ifdef _WIN32
|
2013-01-22 08:28:46 +00:00
|
|
|
HANDLE cap_pipe_h; /**< The handle of the capture pipe */
|
2009-08-26 23:16:37 +00:00
|
|
|
#endif
|
2013-01-22 08:28:46 +00:00
|
|
|
int cap_pipe_fd; /**< the file descriptor of the capture pipe */
|
|
|
|
gboolean cap_pipe_modified; /**< TRUE if data in the pipe uses modified pcap headers */
|
|
|
|
gboolean cap_pipe_byte_swapped; /**< TRUE if data in the pipe is byte swapped */
|
2011-11-07 23:42:53 +00:00
|
|
|
#if defined(_WIN32)
|
2013-01-22 08:28:46 +00:00
|
|
|
char * cap_pipe_buf; /**< Pointer to the data buffer we read into */
|
|
|
|
DWORD cap_pipe_bytes_to_read; /**< Used by cap_pipe_dispatch */
|
|
|
|
DWORD cap_pipe_bytes_read; /**< Used by cap_pipe_dispatch */
|
2012-12-26 15:02:32 +00:00
|
|
|
#else
|
2013-01-22 08:28:46 +00:00
|
|
|
size_t cap_pipe_bytes_to_read; /**< Used by cap_pipe_dispatch */
|
|
|
|
size_t cap_pipe_bytes_read; /**< Used by cap_pipe_dispatch */
|
2012-12-26 15:02:32 +00:00
|
|
|
#endif
|
2013-03-03 17:14:19 +00:00
|
|
|
cap_pipe_state_t cap_pipe_state;
|
|
|
|
cap_pipe_err_t cap_pipe_err;
|
|
|
|
|
2011-11-07 23:42:53 +00:00
|
|
|
#if defined(_WIN32)
|
2012-11-25 18:35:41 +00:00
|
|
|
GMutex *cap_pipe_read_mtx;
|
|
|
|
GAsyncQueue *cap_pipe_pending_q, *cap_pipe_done_q;
|
2011-05-15 22:54:52 +00:00
|
|
|
#endif
|
|
|
|
} pcap_options;
|
2011-03-21 16:57:11 +00:00
|
|
|
|
2011-05-15 22:54:52 +00:00
|
|
|
typedef struct _loop_data {
|
|
|
|
/* common */
|
2013-01-22 08:28:46 +00:00
|
|
|
gboolean go; /**< TRUE as long as we're supposed to keep capturing */
|
|
|
|
int err; /**< if non-zero, error seen while capturing */
|
|
|
|
gint packet_count; /**< Number of packets we have already captured */
|
|
|
|
gint packet_max; /**< Number of packets we're supposed to capture - 0 means infinite */
|
|
|
|
guint inpkts_to_sync_pipe; /**< Packets not already send out to the sync_pipe */
|
2011-05-15 22:54:52 +00:00
|
|
|
#ifdef SIGINFO
|
2013-01-22 08:28:46 +00:00
|
|
|
gboolean report_packet_count; /**< Set by SIGINFO handler; print packet count */
|
2011-05-15 22:54:52 +00:00
|
|
|
#endif
|
2012-11-25 18:35:41 +00:00
|
|
|
GArray *pcaps;
|
2011-03-21 16:57:11 +00:00
|
|
|
/* output file(s) */
|
2012-11-25 18:35:41 +00:00
|
|
|
FILE *pdh;
|
|
|
|
int save_file_fd;
|
2012-12-26 15:02:32 +00:00
|
|
|
guint64 bytes_written;
|
2012-11-25 18:35:41 +00:00
|
|
|
guint32 autostop_files;
|
2008-02-16 02:39:58 +00:00
|
|
|
} loop_data;
|
|
|
|
|
2011-06-23 18:54:07 +00:00
|
|
|
typedef struct _pcap_queue_element {
|
|
|
|
pcap_options *pcap_opts;
|
2012-11-25 18:35:41 +00:00
|
|
|
struct pcap_pkthdr phdr;
|
2011-06-23 18:54:07 +00:00
|
|
|
u_char *pd;
|
|
|
|
} pcap_queue_element;
|
|
|
|
|
2008-02-16 02:39:58 +00:00
|
|
|
/*
|
|
|
|
* Standard secondary message for unexpected errors.
|
|
|
|
*/
|
|
|
|
static const char please_report[] =
|
|
|
|
"Please report this to the Wireshark developers.\n"
|
2015-03-10 17:46:50 +00:00
|
|
|
"https://bugs.wireshark.org/\n"
|
2008-02-16 02:39:58 +00:00
|
|
|
"(This is not a crash; please do not report it as such.)";
|
|
|
|
|
|
|
|
/*
|
2009-09-13 17:46:10 +00:00
|
|
|
* This needs to be static, so that the SIGINT handler can clear the "go"
|
2008-02-16 02:39:58 +00:00
|
|
|
* flag.
|
|
|
|
*/
|
2008-06-23 21:22:11 +00:00
|
|
|
static loop_data global_ld;
|
2008-02-16 02:39:58 +00:00
|
|
|
|
|
|
|
/*
|
2010-09-30 01:01:25 +00:00
|
|
|
* Timeout, in milliseconds, for reads from the stream of captured packets
|
|
|
|
* from a capture device.
|
2009-11-26 04:17:18 +00:00
|
|
|
*
|
|
|
|
* A bug in Mac OS X 10.6 and 10.6.1 causes calls to pcap_open_live(), in
|
|
|
|
* 64-bit applications, with sub-second timeouts not to work. The bug is
|
2010-11-11 07:34:12 +00:00
|
|
|
* fixed in 10.6.2, re-broken in 10.6.3, and again fixed in 10.6.5.
|
2008-02-16 02:39:58 +00:00
|
|
|
*/
|
2009-08-31 19:13:50 +00:00
|
|
|
#if defined(__APPLE__) && defined(__LP64__)
|
2010-04-01 00:27:25 +00:00
|
|
|
static gboolean need_timeout_workaround;
|
2009-11-26 04:17:18 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
#define CAP_READ_TIMEOUT (need_timeout_workaround ? 1000 : 250)
|
2009-08-31 19:13:50 +00:00
|
|
|
#else
|
2011-03-21 16:57:11 +00:00
|
|
|
#define CAP_READ_TIMEOUT 250
|
2009-08-31 19:13:50 +00:00
|
|
|
#endif
|
2009-11-26 04:17:18 +00:00
|
|
|
|
2009-08-26 23:16:37 +00:00
|
|
|
/*
|
2010-09-30 01:01:25 +00:00
|
|
|
* Timeout, in microseconds, for reads from the stream of captured packets
|
2010-09-30 01:40:03 +00:00
|
|
|
* from a pipe. Pipes don't have the same problem that BPF devices do
|
2010-09-30 01:01:25 +00:00
|
|
|
* in OS X 10.6, 10.6.1, 10.6.3, and 10.6.4, so we always use a timeout
|
2010-09-30 01:40:03 +00:00
|
|
|
* of 250ms, i.e. the same value as CAP_READ_TIMEOUT when not on one
|
|
|
|
* of the offending versions of Snow Leopard.
|
2010-11-18 17:29:02 +00:00
|
|
|
*
|
|
|
|
* On Windows this value is converted to milliseconds and passed to
|
|
|
|
* WaitForSingleObject. If it's less than 1000 WaitForSingleObject
|
|
|
|
* will return immediately.
|
2009-08-26 23:16:37 +00:00
|
|
|
*/
|
2011-11-07 23:42:53 +00:00
|
|
|
#if defined(_WIN32)
|
2010-11-17 23:51:31 +00:00
|
|
|
#define PIPE_READ_TIMEOUT 100000
|
2011-07-16 12:45:47 +00:00
|
|
|
#else
|
|
|
|
#define PIPE_READ_TIMEOUT 250000
|
2010-09-30 01:01:25 +00:00
|
|
|
#endif
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-05-16 21:56:12 +00:00
|
|
|
#define WRITER_THREAD_TIMEOUT 100000 /* usecs */
|
|
|
|
|
2005-11-28 08:54:52 +00:00
|
|
|
static void
|
|
|
|
console_log_handler(const char *log_domain, GLogLevelFlags log_level,
|
2011-03-21 16:57:11 +00:00
|
|
|
const char *message, gpointer user_data _U_);
|
2005-11-28 08:54:52 +00:00
|
|
|
|
2005-12-15 00:45:29 +00:00
|
|
|
/* capture related options */
|
2008-02-16 02:39:58 +00:00
|
|
|
static capture_options global_capture_opts;
|
2011-02-24 19:26:38 +00:00
|
|
|
static gboolean quiet = FALSE;
|
2011-05-16 21:56:12 +00:00
|
|
|
static gboolean use_threads = FALSE;
|
2012-02-23 12:57:17 +00:00
|
|
|
static guint64 start_time;
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-06-23 18:54:07 +00:00
|
|
|
static void capture_loop_write_packet_cb(u_char *pcap_opts_p, const struct pcap_pkthdr *phdr,
|
2011-05-16 21:56:12 +00:00
|
|
|
const u_char *pd);
|
2011-06-23 18:54:07 +00:00
|
|
|
static void capture_loop_queue_packet_cb(u_char *pcap_opts_p, const struct pcap_pkthdr *phdr,
|
2011-05-16 21:56:12 +00:00
|
|
|
const u_char *pd);
|
2008-02-16 02:39:58 +00:00
|
|
|
static void capture_loop_get_errmsg(char *errmsg, int errmsglen, const char *fname,
|
2011-03-21 16:57:11 +00:00
|
|
|
int err, gboolean is_close);
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2016-04-05 20:49:05 +00:00
|
|
|
static void WS_NORETURN exit_main(int err);
|
2006-01-05 22:27:25 +00:00
|
|
|
|
2008-02-16 02:39:58 +00:00
|
|
|
static void report_new_capture_file(const char *filename);
|
2012-09-09 13:57:43 +00:00
|
|
|
static void report_packet_count(unsigned int packet_count);
|
2013-11-04 19:38:20 +00:00
|
|
|
static void report_packet_drops(guint32 received, guint32 pcap_drops, guint32 drops, guint32 flushed, guint32 ps_ifdrop, gchar *name);
|
2008-02-16 02:39:58 +00:00
|
|
|
static void report_capture_error(const char *error_msg, const char *secondary_error_msg);
|
2011-06-27 11:30:39 +00:00
|
|
|
static void report_cfilter_error(capture_options *capture_opts, guint i, const char *errmsg);
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2010-07-13 23:26:07 +00:00
|
|
|
#define MSG_MAX_LENGTH 4096
|
|
|
|
|
2013-09-29 20:53:13 +00:00
|
|
|
/* Copied from pcapio.c pcapng_write_interface_statistics_block()*/
|
2012-02-23 12:57:17 +00:00
|
|
|
static guint64
|
|
|
|
create_timestamp(void) {
|
2012-11-25 18:35:41 +00:00
|
|
|
guint64 timestamp;
|
2012-02-22 14:41:02 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
FILETIME now;
|
|
|
|
#else
|
|
|
|
struct timeval now;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
/*
|
|
|
|
* Current time, represented as 100-nanosecond intervals since
|
|
|
|
* January 1, 1601, 00:00:00 UTC.
|
|
|
|
*
|
|
|
|
* I think DWORD might be signed, so cast both parts of "now"
|
|
|
|
* to guint32 so that the sign bit doesn't get treated specially.
|
2012-03-06 21:11:31 +00:00
|
|
|
*
|
|
|
|
* Windows 8 provides GetSystemTimePreciseAsFileTime which we
|
|
|
|
* might want to use instead.
|
2012-02-22 14:41:02 +00:00
|
|
|
*/
|
|
|
|
GetSystemTimeAsFileTime(&now);
|
|
|
|
timestamp = (((guint64)(guint32)now.dwHighDateTime) << 32) +
|
|
|
|
(guint32)now.dwLowDateTime;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert to same thing but as 1-microsecond, i.e. 1000-nanosecond,
|
|
|
|
* intervals.
|
|
|
|
*/
|
|
|
|
timestamp /= 10;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Subtract difference, in microseconds, between January 1, 1601
|
|
|
|
* 00:00:00 UTC and January 1, 1970, 00:00:00 UTC.
|
|
|
|
*/
|
2013-12-20 22:39:32 +00:00
|
|
|
timestamp -= G_GUINT64_CONSTANT(11644473600000000);
|
2012-02-22 14:41:02 +00:00
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* Current time, represented as seconds and microseconds since
|
|
|
|
* January 1, 1970, 00:00:00 UTC.
|
|
|
|
*/
|
|
|
|
gettimeofday(&now, NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert to delta in microseconds.
|
|
|
|
*/
|
|
|
|
timestamp = (guint64)(now.tv_sec) * 1000000 +
|
|
|
|
(guint64)(now.tv_usec);
|
|
|
|
#endif
|
|
|
|
return timestamp;
|
|
|
|
}
|
|
|
|
|
2005-11-28 08:54:52 +00:00
|
|
|
static void
|
2014-07-03 02:29:21 +00:00
|
|
|
print_usage(FILE *output)
|
2011-03-21 16:57:11 +00:00
|
|
|
{
|
|
|
|
fprintf(output, "\nUsage: dumpcap [options] ...\n");
|
|
|
|
fprintf(output, "\n");
|
|
|
|
fprintf(output, "Capture interface:\n");
|
2012-06-08 13:55:14 +00:00
|
|
|
fprintf(output, " -i <interface> name or idx of interface (def: first non-loopback),\n"
|
|
|
|
" or for remote capturing, use one of these formats:\n"
|
|
|
|
" rpcap://<host>/<interface>\n"
|
|
|
|
" TCP@<host>:<port>\n");
|
2011-03-21 16:57:11 +00:00
|
|
|
fprintf(output, " -f <capture filter> packet filter in libpcap filter syntax\n");
|
|
|
|
fprintf(output, " -s <snaplen> packet snapshot length (def: 65535)\n");
|
|
|
|
fprintf(output, " -p don't capture in promiscuous mode\n");
|
2010-05-07 01:22:33 +00:00
|
|
|
#ifdef HAVE_PCAP_CREATE
|
2011-03-21 16:57:11 +00:00
|
|
|
fprintf(output, " -I capture in monitor mode, if available\n");
|
2010-05-07 01:22:33 +00:00
|
|
|
#endif
|
2015-03-25 03:22:00 +00:00
|
|
|
#ifdef CAN_SET_CAPTURE_BUFFER_SIZE
|
2013-12-02 19:17:12 +00:00
|
|
|
fprintf(output, " -B <buffer size> size of kernel buffer in MiB (def: %dMiB)\n", DEFAULT_CAPTURE_BUFFER_SIZE);
|
2005-11-28 08:54:52 +00:00
|
|
|
#endif
|
2011-03-21 16:57:11 +00:00
|
|
|
fprintf(output, " -y <link type> link layer type (def: first appropriate)\n");
|
|
|
|
fprintf(output, " -D print list of interfaces and exit\n");
|
|
|
|
fprintf(output, " -L print list of link-layer types of iface and exit\n");
|
2010-07-13 23:26:07 +00:00
|
|
|
#ifdef HAVE_BPF_IMAGE
|
2011-03-21 16:57:11 +00:00
|
|
|
fprintf(output, " -d print generated BPF code for capture filter\n");
|
2010-07-13 23:26:07 +00:00
|
|
|
#endif
|
2016-02-06 23:40:51 +00:00
|
|
|
fprintf(output, " -k set channel on wifi interface:\n"
|
|
|
|
" <freq>,[<type>],[<center_freq1>],[<center_freq2>]\n");
|
2011-09-27 18:19:23 +00:00
|
|
|
fprintf(output, " -S print statistics for each interface once per second\n");
|
2011-03-21 16:57:11 +00:00
|
|
|
fprintf(output, " -M for -D, -L, and -S, produce machine-readable output\n");
|
|
|
|
fprintf(output, "\n");
|
2007-12-04 11:19:29 +00:00
|
|
|
#ifdef HAVE_PCAP_REMOTE
|
2011-09-27 18:19:23 +00:00
|
|
|
fprintf(output, "RPCAP options:\n");
|
2011-03-21 16:57:11 +00:00
|
|
|
fprintf(output, " -r don't ignore own RPCAP traffic in capture\n");
|
|
|
|
fprintf(output, " -u use UDP for RPCAP data transfer\n");
|
|
|
|
fprintf(output, " -A <user>:<password> use RPCAP password authentication\n");
|
2007-12-04 11:19:29 +00:00
|
|
|
#ifdef HAVE_PCAP_SETSAMPLING
|
2011-03-21 16:57:11 +00:00
|
|
|
fprintf(output, " -m <sampling type> use packet sampling\n");
|
|
|
|
fprintf(output, " count:NUM - capture one packet of every NUM\n");
|
|
|
|
fprintf(output, " timer:NUM - capture no more than 1 packet in NUM ms\n");
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
fprintf(output, "Stop conditions:\n");
|
|
|
|
fprintf(output, " -c <packet count> stop after n packets (def: infinite)\n");
|
|
|
|
fprintf(output, " -a <autostop cond.> ... duration:NUM - stop after NUM seconds\n");
|
|
|
|
fprintf(output, " filesize:NUM - stop this file after NUM KB\n");
|
|
|
|
fprintf(output, " files:NUM - stop after NUM files\n");
|
|
|
|
/*fprintf(output, "\n");*/
|
|
|
|
fprintf(output, "Output (files):\n");
|
|
|
|
fprintf(output, " -w <filename> name of file to save (def: tempfile)\n");
|
|
|
|
fprintf(output, " -g enable group read access on the output file(s)\n");
|
|
|
|
fprintf(output, " -b <ringbuffer opt.> ... duration:NUM - switch to next file after NUM secs\n");
|
|
|
|
fprintf(output, " filesize:NUM - switch to next file after NUM KB\n");
|
|
|
|
fprintf(output, " files:NUM - ringbuffer: replace after NUM files\n");
|
2012-01-23 07:48:53 +00:00
|
|
|
fprintf(output, " -n use pcapng format instead of pcap (default)\n");
|
|
|
|
fprintf(output, " -P use libpcap format instead of pcapng\n");
|
2013-07-27 16:25:08 +00:00
|
|
|
fprintf(output, " --capture-comment <comment>\n");
|
|
|
|
fprintf(output, " add a capture comment to the output file\n");
|
|
|
|
fprintf(output, " (only for pcapng)\n");
|
2011-09-27 18:19:23 +00:00
|
|
|
fprintf(output, "\n");
|
2011-03-21 16:57:11 +00:00
|
|
|
fprintf(output, "Miscellaneous:\n");
|
2013-03-10 11:57:40 +00:00
|
|
|
fprintf(output, " -N <packet_limit> maximum number of packets buffered within dumpcap\n");
|
2013-07-29 15:45:36 +00:00
|
|
|
fprintf(output, " -C <byte_limit> maximum number of bytes used for buffering packets\n");
|
|
|
|
fprintf(output, " within dumpcap\n");
|
2011-05-16 21:56:12 +00:00
|
|
|
fprintf(output, " -t use a separate thread per interface\n");
|
2011-03-21 16:57:11 +00:00
|
|
|
fprintf(output, " -q don't report packet capture counts\n");
|
|
|
|
fprintf(output, " -v print version information and exit\n");
|
|
|
|
fprintf(output, " -h display this help and exit\n");
|
|
|
|
fprintf(output, "\n");
|
2013-08-23 05:21:25 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
fprintf(output, "WARNING: dumpcap will enable kernel BPF JIT compiler if available.\n");
|
|
|
|
fprintf(output, "You might want to reset it\n");
|
2013-08-23 05:25:30 +00:00
|
|
|
fprintf(output, "By doing \"echo 0 > /proc/sys/net/core/bpf_jit_enable\"\n");
|
2013-08-23 05:21:25 +00:00
|
|
|
fprintf(output, "\n");
|
|
|
|
#endif
|
2012-01-23 07:48:53 +00:00
|
|
|
fprintf(output, "Example: dumpcap -i eth0 -a duration:60 -w output.pcapng\n");
|
2012-06-08 13:55:14 +00:00
|
|
|
fprintf(output, "\"Capture packets from interface eth0 until 60s passed into output.pcapng\"\n");
|
2011-03-21 16:57:11 +00:00
|
|
|
fprintf(output, "\n");
|
|
|
|
fprintf(output, "Use Ctrl-C to stop capturing at any time.\n");
|
2005-11-28 08:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Report an error in command-line arguments.
|
2014-06-29 21:37:21 +00:00
|
|
|
* If we're a capture child, send a message back to the parent, otherwise
|
|
|
|
* just print it.
|
2005-11-28 08:54:52 +00:00
|
|
|
*/
|
2014-06-29 21:37:21 +00:00
|
|
|
static void
|
|
|
|
dumpcap_cmdarg_err(const char *fmt, va_list ap)
|
2005-11-28 08:54:52 +00:00
|
|
|
{
|
2012-11-25 18:35:41 +00:00
|
|
|
if (capture_child) {
|
2011-03-21 16:57:11 +00:00
|
|
|
gchar *msg;
|
|
|
|
/* Generate a 'special format' message back to parent */
|
|
|
|
msg = g_strdup_vprintf(fmt, ap);
|
|
|
|
sync_pipe_errmsg_to_parent(2, msg, "");
|
|
|
|
g_free(msg);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "dumpcap: ");
|
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
2005-11-28 08:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Report additional information for an error in command-line arguments.
|
2014-06-29 21:37:21 +00:00
|
|
|
* If we're a capture child, send a message back to the parent, otherwise
|
|
|
|
* just print it.
|
2005-11-28 08:54:52 +00:00
|
|
|
*/
|
2014-06-29 21:37:21 +00:00
|
|
|
static void
|
|
|
|
dumpcap_cmdarg_err_cont(const char *fmt, va_list ap)
|
2005-11-28 08:54:52 +00:00
|
|
|
{
|
2012-11-25 18:35:41 +00:00
|
|
|
if (capture_child) {
|
2011-03-21 16:57:11 +00:00
|
|
|
gchar *msg;
|
|
|
|
msg = g_strdup_vprintf(fmt, ap);
|
|
|
|
sync_pipe_errmsg_to_parent(2, msg, "");
|
|
|
|
g_free(msg);
|
|
|
|
} else {
|
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
2005-11-28 08:54:52 +00:00
|
|
|
}
|
|
|
|
|
2010-07-14 02:14:54 +00:00
|
|
|
#ifdef HAVE_LIBCAP
|
2010-07-14 04:31:52 +00:00
|
|
|
static void
|
|
|
|
#if 0 /* Set to enable capability debugging */
|
|
|
|
/* see 'man cap_to_text()' for explanation of output */
|
|
|
|
/* '=' means 'all= ' ie: no capabilities */
|
|
|
|
/* '=ip' means 'all=ip' ie: all capabilities are permissible and inheritable */
|
|
|
|
/* .... */
|
|
|
|
print_caps(const char *pfx) {
|
|
|
|
cap_t caps = cap_get_proc();
|
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG,
|
|
|
|
"%s: EUID: %d Capabilities: %s", pfx,
|
|
|
|
geteuid(), cap_to_text(caps, NULL));
|
|
|
|
cap_free(caps);
|
|
|
|
#else
|
|
|
|
print_caps(const char *pfx _U_) {
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-07-14 02:14:54 +00:00
|
|
|
static void
|
|
|
|
relinquish_all_capabilities(void)
|
|
|
|
{
|
|
|
|
/* Drop any and all capabilities this process may have. */
|
|
|
|
/* Allowed whether or not process has any privileges. */
|
|
|
|
cap_t caps = cap_init(); /* all capabilities initialized to off */
|
|
|
|
print_caps("Pre-clear");
|
|
|
|
if (cap_set_proc(caps)) {
|
2011-06-28 09:00:11 +00:00
|
|
|
cmdarg_err("cap_set_proc() fail return: %s", g_strerror(errno));
|
2010-07-14 02:14:54 +00:00
|
|
|
}
|
|
|
|
print_caps("Post-clear");
|
|
|
|
cap_free(caps);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-07-13 23:26:07 +00:00
|
|
|
static void
|
|
|
|
get_capture_device_open_failure_messages(const char *open_err_str,
|
2013-11-26 03:13:16 +00:00
|
|
|
const char *iface,
|
2010-07-13 23:26:07 +00:00
|
|
|
char *errmsg, size_t errmsg_len,
|
|
|
|
char *secondary_errmsg,
|
|
|
|
size_t secondary_errmsg_len)
|
|
|
|
{
|
2012-05-07 00:49:39 +00:00
|
|
|
#ifndef _WIN32
|
2011-03-21 16:57:11 +00:00
|
|
|
const char *libpcap_warn;
|
|
|
|
static const char ppamsg[] = "can't find PPA for ";
|
2012-05-07 00:49:39 +00:00
|
|
|
#endif
|
2011-03-21 16:57:11 +00:00
|
|
|
|
|
|
|
g_snprintf(errmsg, (gulong) errmsg_len,
|
2013-11-26 03:13:16 +00:00
|
|
|
"The capture session could not be initiated on interface '%s' (%s).",
|
|
|
|
iface, open_err_str);
|
2011-12-29 21:59:24 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (!has_wpcap) {
|
|
|
|
g_snprintf(secondary_errmsg, (gulong) secondary_errmsg_len,
|
2011-12-29 22:06:22 +00:00
|
|
|
"\n"
|
|
|
|
"In order to capture packets, WinPcap must be installed; see\n"
|
|
|
|
"\n"
|
2015-03-10 17:46:50 +00:00
|
|
|
" https://www.winpcap.org/\n"
|
2011-12-29 22:06:22 +00:00
|
|
|
"\n"
|
|
|
|
"for a downloadable version of WinPcap and for instructions on how to install\n"
|
|
|
|
"WinPcap.");
|
2011-12-29 21:59:24 +00:00
|
|
|
} else {
|
|
|
|
g_snprintf(secondary_errmsg, (gulong) secondary_errmsg_len,
|
|
|
|
"\n"
|
|
|
|
"Please check that \"%s\" is the proper interface.\n"
|
|
|
|
"\n"
|
|
|
|
"\n"
|
2016-11-29 17:32:47 +00:00
|
|
|
"Help can be found on the following pages:\n"
|
2011-12-29 21:59:24 +00:00
|
|
|
"\n"
|
2015-03-10 17:46:50 +00:00
|
|
|
" https://wiki.wireshark.org/WinPcap\n"
|
|
|
|
" https://wiki.wireshark.org/CaptureSetup\n",
|
2011-12-29 21:59:24 +00:00
|
|
|
iface);
|
|
|
|
}
|
|
|
|
#else
|
2012-05-07 00:49:39 +00:00
|
|
|
/* If we got a "can't find PPA for X" message, warn the user (who
|
|
|
|
is running dumpcap on HP-UX) that they don't have a version of
|
|
|
|
libpcap that properly handles HP-UX (libpcap 0.6.x and later
|
|
|
|
versions, which properly handle HP-UX, say "can't find /dev/dlpi
|
|
|
|
PPA for X" rather than "can't find PPA for X"). */
|
|
|
|
if (strncmp(open_err_str, ppamsg, sizeof ppamsg - 1) == 0)
|
|
|
|
libpcap_warn =
|
|
|
|
"\n\n"
|
|
|
|
"You are running (T)Wireshark with a version of the libpcap library\n"
|
|
|
|
"that doesn't handle HP-UX network devices well; this means that\n"
|
|
|
|
"(T)Wireshark may not be able to capture packets.\n"
|
|
|
|
"\n"
|
|
|
|
"To fix this, you should install libpcap 0.6.2, or a later version\n"
|
|
|
|
"of libpcap, rather than libpcap 0.4 or 0.5.x. It is available in\n"
|
|
|
|
"packaged binary form from the Software Porting And Archive Centre\n"
|
|
|
|
"for HP-UX; the Centre is at http://hpux.connect.org.uk/ - the page\n"
|
|
|
|
"at the URL lists a number of mirror sites.";
|
|
|
|
else
|
|
|
|
libpcap_warn = "";
|
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
g_snprintf(secondary_errmsg, (gulong) secondary_errmsg_len,
|
|
|
|
"Please check to make sure you have sufficient permissions, and that you have "
|
|
|
|
"the proper interface or pipe specified.%s", libpcap_warn);
|
2010-07-13 23:26:07 +00:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
compile_capture_filter(const char *iface, pcap_t *pcap_h,
|
2011-06-15 20:21:45 +00:00
|
|
|
struct bpf_program *fcode, const char *cfilter)
|
2010-07-13 23:26:07 +00:00
|
|
|
{
|
2011-03-21 16:57:11 +00:00
|
|
|
bpf_u_int32 netnum, netmask;
|
|
|
|
gchar lookup_net_err_str[PCAP_ERRBUF_SIZE];
|
2010-07-13 23:26:07 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
if (pcap_lookupnet(iface, &netnum, &netmask, lookup_net_err_str) < 0) {
|
|
|
|
/*
|
|
|
|
* Well, we can't get the netmask for this interface; it's used
|
|
|
|
* only for filters that check for broadcast IP addresses, so
|
|
|
|
* we just punt and use 0. It might be nice to warn the user,
|
|
|
|
* but that's a pain in a GUI application, as it'd involve popping
|
|
|
|
* up a message box, and it's not clear how often this would make
|
|
|
|
* a difference (only filters that check for IP broadcast addresses
|
|
|
|
* use the netmask).
|
|
|
|
*/
|
|
|
|
/*cmdarg_err(
|
|
|
|
"Warning: Couldn't obtain netmask info (%s).", lookup_net_err_str);*/
|
|
|
|
netmask = 0;
|
|
|
|
}
|
2011-06-15 21:22:30 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Sigh. Older versions of libpcap don't properly declare the
|
|
|
|
* third argument to pcap_compile() as a const pointer. Cast
|
|
|
|
* away the warning.
|
|
|
|
*/
|
2015-02-20 00:05:49 +00:00
|
|
|
DIAG_OFF(cast-qual)
|
2011-06-15 21:22:30 +00:00
|
|
|
if (pcap_compile(pcap_h, fcode, (char *)cfilter, 1, netmask) < 0)
|
2011-03-21 16:57:11 +00:00
|
|
|
return FALSE;
|
2015-02-20 00:05:49 +00:00
|
|
|
DIAG_ON(cast-qual)
|
2011-03-21 16:57:11 +00:00
|
|
|
return TRUE;
|
2010-07-13 23:26:07 +00:00
|
|
|
}
|
|
|
|
|
2010-07-14 13:22:34 +00:00
|
|
|
#ifdef HAVE_BPF_IMAGE
|
2010-07-13 23:26:07 +00:00
|
|
|
static gboolean
|
|
|
|
show_filter_code(capture_options *capture_opts)
|
|
|
|
{
|
2011-05-13 11:28:51 +00:00
|
|
|
interface_options interface_opts;
|
2011-03-21 16:57:11 +00:00
|
|
|
pcap_t *pcap_h;
|
|
|
|
gchar open_err_str[PCAP_ERRBUF_SIZE];
|
|
|
|
char errmsg[MSG_MAX_LENGTH+1];
|
|
|
|
char secondary_errmsg[MSG_MAX_LENGTH+1];
|
|
|
|
struct bpf_program fcode;
|
|
|
|
struct bpf_insn *insn;
|
|
|
|
u_int i;
|
2011-05-15 22:54:52 +00:00
|
|
|
guint j;
|
2011-05-13 11:28:51 +00:00
|
|
|
|
2011-05-15 22:54:52 +00:00
|
|
|
for (j = 0; j < capture_opts->ifaces->len; j++) {
|
2011-05-13 11:28:51 +00:00
|
|
|
interface_opts = g_array_index(capture_opts->ifaces, interface_options, j);
|
2016-01-05 21:54:02 +00:00
|
|
|
pcap_h = open_capture_device(capture_opts, &interface_opts,
|
|
|
|
CAP_READ_TIMEOUT, &open_err_str);
|
2011-05-13 11:28:51 +00:00
|
|
|
if (pcap_h == NULL) {
|
|
|
|
/* Open failed; get messages */
|
|
|
|
get_capture_device_open_failure_messages(open_err_str,
|
|
|
|
interface_opts.name,
|
|
|
|
errmsg, sizeof errmsg,
|
|
|
|
secondary_errmsg,
|
|
|
|
sizeof secondary_errmsg);
|
|
|
|
/* And report them */
|
|
|
|
report_capture_error(errmsg, secondary_errmsg);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-03-21 16:57:11 +00:00
|
|
|
|
2011-05-13 11:28:51 +00:00
|
|
|
/* Set the link-layer type. */
|
2016-01-05 21:54:02 +00:00
|
|
|
if (!set_pcap_datalink(pcap_h, interface_opts.linktype, interface_opts.name,
|
2011-05-15 22:54:52 +00:00
|
|
|
errmsg, sizeof errmsg,
|
2011-05-13 11:28:51 +00:00
|
|
|
secondary_errmsg, sizeof secondary_errmsg)) {
|
|
|
|
pcap_close(pcap_h);
|
|
|
|
report_capture_error(errmsg, secondary_errmsg);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2010-07-13 23:26:07 +00:00
|
|
|
|
2011-05-13 11:28:51 +00:00
|
|
|
/* OK, try to compile the capture filter. */
|
|
|
|
if (!compile_capture_filter(interface_opts.name, pcap_h, &fcode,
|
|
|
|
interface_opts.cfilter)) {
|
|
|
|
pcap_close(pcap_h);
|
2011-06-27 11:30:39 +00:00
|
|
|
report_cfilter_error(capture_opts, j, errmsg);
|
2011-05-13 11:28:51 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2011-03-21 16:57:11 +00:00
|
|
|
pcap_close(pcap_h);
|
2010-07-14 00:50:25 +00:00
|
|
|
|
2011-05-13 11:28:51 +00:00
|
|
|
/* Now print the filter code. */
|
|
|
|
insn = fcode.bf_insns;
|
2010-07-13 23:26:07 +00:00
|
|
|
|
2011-05-13 11:28:51 +00:00
|
|
|
for (i = 0; i < fcode.bf_len; insn++, i++)
|
|
|
|
printf("%s\n", bpf_image(insn, i));
|
|
|
|
}
|
2011-05-19 20:48:58 +00:00
|
|
|
/* If not using libcap: we now can now set euid/egid to ruid/rgid */
|
|
|
|
/* to remove any suid privileges. */
|
|
|
|
/* If using libcap: we can now remove NET_RAW and NET_ADMIN capabilities */
|
|
|
|
/* (euid/egid have already previously been set to ruid/rgid. */
|
|
|
|
/* (See comment in main() for details) */
|
|
|
|
#ifndef HAVE_LIBCAP
|
|
|
|
relinquish_special_privs_perm();
|
|
|
|
#else
|
|
|
|
relinquish_all_capabilities();
|
|
|
|
#endif
|
2011-03-21 16:57:11 +00:00
|
|
|
if (capture_child) {
|
|
|
|
/* Let our parent know we succeeded. */
|
2013-07-15 15:48:10 +00:00
|
|
|
pipe_write_block(2, SP_SUCCESS, NULL);
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
|
|
|
return TRUE;
|
2010-07-13 23:26:07 +00:00
|
|
|
}
|
2010-07-14 13:22:34 +00:00
|
|
|
#endif
|
2010-07-13 23:26:07 +00:00
|
|
|
|
2010-03-04 01:12:04 +00:00
|
|
|
/*
|
|
|
|
* capture_interface_list() is expected to do the right thing to get
|
|
|
|
* a list of interfaces.
|
|
|
|
*
|
|
|
|
* In most of the programs in the Wireshark suite, "the right thing"
|
|
|
|
* is to run dumpcap and ask it for the list, because dumpcap may
|
|
|
|
* be the only program in the suite with enough privileges to get
|
|
|
|
* the list.
|
|
|
|
*
|
|
|
|
* In dumpcap itself, however, we obviously can't run dumpcap to
|
|
|
|
* ask for the list. Therefore, our capture_interface_list() should
|
|
|
|
* just call get_interface_list().
|
|
|
|
*/
|
|
|
|
GList *
|
2013-06-27 17:10:50 +00:00
|
|
|
capture_interface_list(int *err, char **err_str, void(*update_cb)(void) _U_)
|
2010-03-04 01:12:04 +00:00
|
|
|
{
|
2011-03-21 16:57:11 +00:00
|
|
|
return get_interface_list(err, err_str);
|
2010-03-04 01:12:04 +00:00
|
|
|
}
|
|
|
|
|
2010-05-07 19:24:32 +00:00
|
|
|
#define ADDRSTRLEN 46 /* Covers IPv4 & IPv6 */
|
From Mike Garratt:
Friendly Names for interfaces on Windows
Notes on the changes the patch covers:
* if_info_t struct: addition of friendly_name
* Dumpcap Interface list format changes:
+ Win32: "dumpcap -D" shows friendly_name in place of descript if known
+ All: machine interface "dumpcap -D -Z none" includes friendly_name in the
list in addition to the existing parameters
* interface_options struct: addition of console_display_name
+ When an interface name is displayed in a console, it will typically be the
console_display_name (instead of name).
+ console_display_name is used as the basis of the autogenerated temp
filenames
+ console_display_name is typically set to the friendly_name if known,
otherwise it is set to the interface name
* Enhancements to capture_opts_add_iface_opt() (the function which process -i
options).
+ Can now specify the interface using its name and friendly_name
+ Interface name matching is case insenstive
+ Name matching first attempts exact matching, then falls back to prefix
matching
(e.g. dumpcap -i local)
+ Validates interface names, instead of blindly sending them off to
winpcap/libpcap
+ Interface specification by number is still supported.
* capture_opts_trim_iface() has been refactored:
+ Instead of repeating a decent chunk of the cost in
capture_opts_add_iface_opt(), it calls capture_opts_trim_iface() to specify the
interface.
* introduction of capture_win_ifnames.[ch] (windows only code)
+ Implements static function GetInterfaceFriendlyNameFromDeviceGuid() - a
windows version independant function to convert an interface guid into its
friendly name. Uses published api functions on windows vista and higher, but
falls back to unpublished API functions on older windows releases.
+ void get_windows_interface_friendlyname(/* IN */ char
*interface_devicename, /* OUT */char **interface_friendlyname); - extracts the
GUID from the interface_devicename, then uses
GetInterfaceFriendlyNameFromDeviceGuid() to do the resolution
* Auto temp filename generation:
+ Now uses wireshark_pcapng_* or wireshark_pcap_* depending on file format
+ Basis temp filename format on console_display_name
+ Win32: if console_display_name is a windows interface guid, extracts
numbers from GUID here (instead of in interface option processing)
GUI CHANGES:
* Dialog that displays when you click the "Manage Interfaces" button (within
Capture Options dialog) has been renamed from "Add new interfaces" to
"Interface Management"
* ui/gtk/capture_dlg.c: new_interfaces_w variable renamed to
interface_management_w
* Win32: Local Interfaces tab on Interface Management dialog, shows includes
friendly name as far left column
* Interface Management dialog defaults to larger size on win32 - so it fits
without resizing local interfaces tab
* Interface Management dialog now saves preferences when you click the apply
button (local hidden interfaces was not persisting across restarts)
* Tweaks: "Interface Details" dialog (Interface list->Capture Interfaces ->
Details):
+ "Friendly Name" renamed to "NDIS Friendly Name"
+ Added "OS Friendly Name" to the top of the list
* Win32: The "Capture Interfaces" dialog now shows the friendly name instead of
device guid
* Welcome screen:
+ The height of the interface list scrollbox dynamically adjusts & updates to
the number visible interfaces.
Up to 10 interfaces can be listed without a scroll bar, the minimum height
is for 2 interfaces.
+ Win32: now shows just the Friendly Name if known - in place of
"Interfacename_Guid:(Description)"
svn path=/trunk/; revision=46083
2012-11-19 20:07:27 +00:00
|
|
|
/*
|
2012-11-25 18:35:41 +00:00
|
|
|
* Output a machine readable list of the interfaces
|
From Mike Garratt:
Friendly Names for interfaces on Windows
Notes on the changes the patch covers:
* if_info_t struct: addition of friendly_name
* Dumpcap Interface list format changes:
+ Win32: "dumpcap -D" shows friendly_name in place of descript if known
+ All: machine interface "dumpcap -D -Z none" includes friendly_name in the
list in addition to the existing parameters
* interface_options struct: addition of console_display_name
+ When an interface name is displayed in a console, it will typically be the
console_display_name (instead of name).
+ console_display_name is used as the basis of the autogenerated temp
filenames
+ console_display_name is typically set to the friendly_name if known,
otherwise it is set to the interface name
* Enhancements to capture_opts_add_iface_opt() (the function which process -i
options).
+ Can now specify the interface using its name and friendly_name
+ Interface name matching is case insenstive
+ Name matching first attempts exact matching, then falls back to prefix
matching
(e.g. dumpcap -i local)
+ Validates interface names, instead of blindly sending them off to
winpcap/libpcap
+ Interface specification by number is still supported.
* capture_opts_trim_iface() has been refactored:
+ Instead of repeating a decent chunk of the cost in
capture_opts_add_iface_opt(), it calls capture_opts_trim_iface() to specify the
interface.
* introduction of capture_win_ifnames.[ch] (windows only code)
+ Implements static function GetInterfaceFriendlyNameFromDeviceGuid() - a
windows version independant function to convert an interface guid into its
friendly name. Uses published api functions on windows vista and higher, but
falls back to unpublished API functions on older windows releases.
+ void get_windows_interface_friendlyname(/* IN */ char
*interface_devicename, /* OUT */char **interface_friendlyname); - extracts the
GUID from the interface_devicename, then uses
GetInterfaceFriendlyNameFromDeviceGuid() to do the resolution
* Auto temp filename generation:
+ Now uses wireshark_pcapng_* or wireshark_pcap_* depending on file format
+ Basis temp filename format on console_display_name
+ Win32: if console_display_name is a windows interface guid, extracts
numbers from GUID here (instead of in interface option processing)
GUI CHANGES:
* Dialog that displays when you click the "Manage Interfaces" button (within
Capture Options dialog) has been renamed from "Add new interfaces" to
"Interface Management"
* ui/gtk/capture_dlg.c: new_interfaces_w variable renamed to
interface_management_w
* Win32: Local Interfaces tab on Interface Management dialog, shows includes
friendly name as far left column
* Interface Management dialog defaults to larger size on win32 - so it fits
without resizing local interfaces tab
* Interface Management dialog now saves preferences when you click the apply
button (local hidden interfaces was not persisting across restarts)
* Tweaks: "Interface Details" dialog (Interface list->Capture Interfaces ->
Details):
+ "Friendly Name" renamed to "NDIS Friendly Name"
+ Added "OS Friendly Name" to the top of the list
* Win32: The "Capture Interfaces" dialog now shows the friendly name instead of
device guid
* Welcome screen:
+ The height of the interface list scrollbox dynamically adjusts & updates to
the number visible interfaces.
Up to 10 interfaces can be listed without a scroll bar, the minimum height
is for 2 interfaces.
+ Win32: now shows just the Friendly Name if known - in place of
"Interfacename_Guid:(Description)"
svn path=/trunk/; revision=46083
2012-11-19 20:07:27 +00:00
|
|
|
* This list is retrieved by the sync_interface_list_open() function
|
2012-11-25 18:35:41 +00:00
|
|
|
* The actual output of this function can be viewed with the command "dumpcap -D -Z none"
|
From Mike Garratt:
Friendly Names for interfaces on Windows
Notes on the changes the patch covers:
* if_info_t struct: addition of friendly_name
* Dumpcap Interface list format changes:
+ Win32: "dumpcap -D" shows friendly_name in place of descript if known
+ All: machine interface "dumpcap -D -Z none" includes friendly_name in the
list in addition to the existing parameters
* interface_options struct: addition of console_display_name
+ When an interface name is displayed in a console, it will typically be the
console_display_name (instead of name).
+ console_display_name is used as the basis of the autogenerated temp
filenames
+ console_display_name is typically set to the friendly_name if known,
otherwise it is set to the interface name
* Enhancements to capture_opts_add_iface_opt() (the function which process -i
options).
+ Can now specify the interface using its name and friendly_name
+ Interface name matching is case insenstive
+ Name matching first attempts exact matching, then falls back to prefix
matching
(e.g. dumpcap -i local)
+ Validates interface names, instead of blindly sending them off to
winpcap/libpcap
+ Interface specification by number is still supported.
* capture_opts_trim_iface() has been refactored:
+ Instead of repeating a decent chunk of the cost in
capture_opts_add_iface_opt(), it calls capture_opts_trim_iface() to specify the
interface.
* introduction of capture_win_ifnames.[ch] (windows only code)
+ Implements static function GetInterfaceFriendlyNameFromDeviceGuid() - a
windows version independant function to convert an interface guid into its
friendly name. Uses published api functions on windows vista and higher, but
falls back to unpublished API functions on older windows releases.
+ void get_windows_interface_friendlyname(/* IN */ char
*interface_devicename, /* OUT */char **interface_friendlyname); - extracts the
GUID from the interface_devicename, then uses
GetInterfaceFriendlyNameFromDeviceGuid() to do the resolution
* Auto temp filename generation:
+ Now uses wireshark_pcapng_* or wireshark_pcap_* depending on file format
+ Basis temp filename format on console_display_name
+ Win32: if console_display_name is a windows interface guid, extracts
numbers from GUID here (instead of in interface option processing)
GUI CHANGES:
* Dialog that displays when you click the "Manage Interfaces" button (within
Capture Options dialog) has been renamed from "Add new interfaces" to
"Interface Management"
* ui/gtk/capture_dlg.c: new_interfaces_w variable renamed to
interface_management_w
* Win32: Local Interfaces tab on Interface Management dialog, shows includes
friendly name as far left column
* Interface Management dialog defaults to larger size on win32 - so it fits
without resizing local interfaces tab
* Interface Management dialog now saves preferences when you click the apply
button (local hidden interfaces was not persisting across restarts)
* Tweaks: "Interface Details" dialog (Interface list->Capture Interfaces ->
Details):
+ "Friendly Name" renamed to "NDIS Friendly Name"
+ Added "OS Friendly Name" to the top of the list
* Win32: The "Capture Interfaces" dialog now shows the friendly name instead of
device guid
* Welcome screen:
+ The height of the interface list scrollbox dynamically adjusts & updates to
the number visible interfaces.
Up to 10 interfaces can be listed without a scroll bar, the minimum height
is for 2 interfaces.
+ Win32: now shows just the Friendly Name if known - in place of
"Interfacename_Guid:(Description)"
svn path=/trunk/; revision=46083
2012-11-19 20:07:27 +00:00
|
|
|
*/
|
2010-05-07 19:24:32 +00:00
|
|
|
static void
|
|
|
|
print_machine_readable_interfaces(GList *if_list)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
GList *if_entry;
|
|
|
|
if_info_t *if_info;
|
|
|
|
GSList *addr;
|
|
|
|
if_addr_t *if_addr;
|
|
|
|
char addr_str[ADDRSTRLEN];
|
|
|
|
|
2010-05-18 00:20:30 +00:00
|
|
|
if (capture_child) {
|
|
|
|
/* Let our parent know we succeeded. */
|
2013-07-15 15:51:45 +00:00
|
|
|
pipe_write_block(2, SP_SUCCESS, NULL);
|
2010-05-18 00:20:30 +00:00
|
|
|
}
|
2010-05-17 18:40:23 +00:00
|
|
|
|
2010-05-07 19:24:32 +00:00
|
|
|
i = 1; /* Interface id number */
|
|
|
|
for (if_entry = g_list_first(if_list); if_entry != NULL;
|
|
|
|
if_entry = g_list_next(if_entry)) {
|
|
|
|
if_info = (if_info_t *)if_entry->data;
|
2013-05-22 01:19:18 +00:00
|
|
|
printf("%d. %s\t", i++, if_info->name);
|
2010-05-07 19:24:32 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Print the contents of the if_entry struct in a parseable format.
|
|
|
|
* Each if_entry element is tab-separated. Addresses are comma-
|
|
|
|
* separated.
|
|
|
|
*/
|
|
|
|
/* XXX - Make sure our description doesn't contain a tab */
|
2012-11-22 06:02:49 +00:00
|
|
|
if (if_info->vendor_description != NULL)
|
2013-05-22 01:19:18 +00:00
|
|
|
printf("%s\t", if_info->vendor_description);
|
2010-05-07 19:24:32 +00:00
|
|
|
else
|
2013-05-22 01:19:18 +00:00
|
|
|
printf("\t");
|
2012-11-25 18:35:41 +00:00
|
|
|
|
2012-11-22 06:02:49 +00:00
|
|
|
/* XXX - Make sure our friendly name doesn't contain a tab */
|
From Mike Garratt:
Friendly Names for interfaces on Windows
Notes on the changes the patch covers:
* if_info_t struct: addition of friendly_name
* Dumpcap Interface list format changes:
+ Win32: "dumpcap -D" shows friendly_name in place of descript if known
+ All: machine interface "dumpcap -D -Z none" includes friendly_name in the
list in addition to the existing parameters
* interface_options struct: addition of console_display_name
+ When an interface name is displayed in a console, it will typically be the
console_display_name (instead of name).
+ console_display_name is used as the basis of the autogenerated temp
filenames
+ console_display_name is typically set to the friendly_name if known,
otherwise it is set to the interface name
* Enhancements to capture_opts_add_iface_opt() (the function which process -i
options).
+ Can now specify the interface using its name and friendly_name
+ Interface name matching is case insenstive
+ Name matching first attempts exact matching, then falls back to prefix
matching
(e.g. dumpcap -i local)
+ Validates interface names, instead of blindly sending them off to
winpcap/libpcap
+ Interface specification by number is still supported.
* capture_opts_trim_iface() has been refactored:
+ Instead of repeating a decent chunk of the cost in
capture_opts_add_iface_opt(), it calls capture_opts_trim_iface() to specify the
interface.
* introduction of capture_win_ifnames.[ch] (windows only code)
+ Implements static function GetInterfaceFriendlyNameFromDeviceGuid() - a
windows version independant function to convert an interface guid into its
friendly name. Uses published api functions on windows vista and higher, but
falls back to unpublished API functions on older windows releases.
+ void get_windows_interface_friendlyname(/* IN */ char
*interface_devicename, /* OUT */char **interface_friendlyname); - extracts the
GUID from the interface_devicename, then uses
GetInterfaceFriendlyNameFromDeviceGuid() to do the resolution
* Auto temp filename generation:
+ Now uses wireshark_pcapng_* or wireshark_pcap_* depending on file format
+ Basis temp filename format on console_display_name
+ Win32: if console_display_name is a windows interface guid, extracts
numbers from GUID here (instead of in interface option processing)
GUI CHANGES:
* Dialog that displays when you click the "Manage Interfaces" button (within
Capture Options dialog) has been renamed from "Add new interfaces" to
"Interface Management"
* ui/gtk/capture_dlg.c: new_interfaces_w variable renamed to
interface_management_w
* Win32: Local Interfaces tab on Interface Management dialog, shows includes
friendly name as far left column
* Interface Management dialog defaults to larger size on win32 - so it fits
without resizing local interfaces tab
* Interface Management dialog now saves preferences when you click the apply
button (local hidden interfaces was not persisting across restarts)
* Tweaks: "Interface Details" dialog (Interface list->Capture Interfaces ->
Details):
+ "Friendly Name" renamed to "NDIS Friendly Name"
+ Added "OS Friendly Name" to the top of the list
* Win32: The "Capture Interfaces" dialog now shows the friendly name instead of
device guid
* Welcome screen:
+ The height of the interface list scrollbox dynamically adjusts & updates to
the number visible interfaces.
Up to 10 interfaces can be listed without a scroll bar, the minimum height
is for 2 interfaces.
+ Win32: now shows just the Friendly Name if known - in place of
"Interfacename_Guid:(Description)"
svn path=/trunk/; revision=46083
2012-11-19 20:07:27 +00:00
|
|
|
if (if_info->friendly_name != NULL)
|
|
|
|
printf("%s\t", if_info->friendly_name);
|
|
|
|
else
|
2012-11-25 18:35:41 +00:00
|
|
|
printf("\t");
|
2010-05-07 19:24:32 +00:00
|
|
|
|
2014-01-17 20:06:39 +00:00
|
|
|
printf("%i\t", if_info->type);
|
2013-05-22 01:19:18 +00:00
|
|
|
|
2012-11-25 18:35:41 +00:00
|
|
|
for (addr = g_slist_nth(if_info->addrs, 0); addr != NULL;
|
2010-05-07 19:24:32 +00:00
|
|
|
addr = g_slist_next(addr)) {
|
|
|
|
if (addr != g_slist_nth(if_info->addrs, 0))
|
|
|
|
printf(",");
|
|
|
|
|
|
|
|
if_addr = (if_addr_t *)addr->data;
|
|
|
|
switch(if_addr->ifat_type) {
|
|
|
|
case IF_AT_IPv4:
|
2016-02-10 09:11:12 +00:00
|
|
|
if (ws_inet_ntop4(&if_addr->addr.ip4_addr, addr_str,
|
2010-05-07 19:24:32 +00:00
|
|
|
ADDRSTRLEN)) {
|
|
|
|
printf("%s", addr_str);
|
|
|
|
} else {
|
|
|
|
printf("<unknown IPv4>");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IF_AT_IPv6:
|
2016-02-10 09:11:12 +00:00
|
|
|
if (ws_inet_ntop6(&if_addr->addr.ip6_addr,
|
2010-05-07 19:24:32 +00:00
|
|
|
addr_str, ADDRSTRLEN)) {
|
|
|
|
printf("%s", addr_str);
|
|
|
|
} else {
|
|
|
|
printf("<unknown IPv6>");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2014-01-17 20:06:39 +00:00
|
|
|
printf("<type unknown %i>", if_addr->ifat_type);
|
2010-05-07 19:24:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (if_info->loopback)
|
|
|
|
printf("\tloopback");
|
|
|
|
else
|
|
|
|
printf("\tnetwork");
|
2014-02-25 13:05:11 +00:00
|
|
|
#ifdef HAVE_EXTCAP
|
|
|
|
printf("\t%s", if_info->extcap);
|
|
|
|
#endif
|
2010-05-07 19:24:32 +00:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-07 08:06:25 +00:00
|
|
|
/*
|
|
|
|
* If you change the machine-readable output format of this function,
|
2010-05-13 17:37:39 +00:00
|
|
|
* you MUST update capture_ifinfo.c:capture_get_if_capabilities() accordingly!
|
2010-05-07 08:06:25 +00:00
|
|
|
*/
|
|
|
|
static void
|
2010-05-14 00:49:28 +00:00
|
|
|
print_machine_readable_if_capabilities(if_capabilities_t *caps)
|
2010-05-07 08:06:25 +00:00
|
|
|
{
|
|
|
|
GList *lt_entry;
|
|
|
|
data_link_info_t *data_link_info;
|
|
|
|
const gchar *desc_str;
|
|
|
|
|
2010-05-18 00:20:30 +00:00
|
|
|
if (capture_child) {
|
|
|
|
/* Let our parent know we succeeded. */
|
2013-07-15 15:51:45 +00:00
|
|
|
pipe_write_block(2, SP_SUCCESS, NULL);
|
2010-05-18 00:20:30 +00:00
|
|
|
}
|
2010-05-17 18:40:23 +00:00
|
|
|
|
2010-05-13 17:37:39 +00:00
|
|
|
if (caps->can_set_rfmon)
|
|
|
|
printf("1\n");
|
|
|
|
else
|
|
|
|
printf("0\n");
|
|
|
|
for (lt_entry = caps->data_link_types; lt_entry != NULL;
|
|
|
|
lt_entry = g_list_next(lt_entry)) {
|
2010-05-07 08:06:25 +00:00
|
|
|
data_link_info = (data_link_info_t *)lt_entry->data;
|
|
|
|
if (data_link_info->description != NULL)
|
|
|
|
desc_str = data_link_info->description;
|
|
|
|
else
|
|
|
|
desc_str = "(not supported)";
|
|
|
|
printf("%d\t%s\t%s\n", data_link_info->dlt, data_link_info->name,
|
|
|
|
desc_str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-20 00:09:47 +00:00
|
|
|
typedef struct {
|
|
|
|
char *name;
|
|
|
|
pcap_t *pch;
|
|
|
|
} if_stat_t;
|
|
|
|
|
|
|
|
/* Print the number of packets captured for each interface until we're killed. */
|
|
|
|
static int
|
|
|
|
print_statistics_loop(gboolean machine_readable)
|
|
|
|
{
|
|
|
|
GList *if_list, *if_entry, *stat_list = NULL, *stat_entry;
|
|
|
|
if_info_t *if_info;
|
|
|
|
if_stat_t *if_stat;
|
|
|
|
int err;
|
|
|
|
gchar *err_str;
|
|
|
|
pcap_t *pch;
|
|
|
|
char errbuf[PCAP_ERRBUF_SIZE];
|
|
|
|
struct pcap_stat ps;
|
|
|
|
|
|
|
|
if_list = get_interface_list(&err, &err_str);
|
|
|
|
if (if_list == NULL) {
|
2014-07-22 23:55:58 +00:00
|
|
|
if (err == 0)
|
2014-07-22 23:53:18 +00:00
|
|
|
cmdarg_err("There are no interfaces on which a capture can be done");
|
|
|
|
else {
|
2008-06-20 00:09:47 +00:00
|
|
|
cmdarg_err("%s", err_str);
|
|
|
|
g_free(err_str);
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (if_entry = g_list_first(if_list); if_entry != NULL; if_entry = g_list_next(if_entry)) {
|
2010-03-01 23:43:47 +00:00
|
|
|
if_info = (if_info_t *)if_entry->data;
|
2015-02-02 09:37:17 +00:00
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
/* On Linux nf* interfaces don't collect stats properly and don't allows multiple
|
|
|
|
* connections. We avoid collecting stats on them.
|
|
|
|
*/
|
|
|
|
if (!strncmp(if_info->name, "nf", 2)) {
|
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "Skipping interface %s for stats",
|
|
|
|
if_info->name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-06-20 00:09:47 +00:00
|
|
|
#ifdef HAVE_PCAP_OPEN
|
|
|
|
pch = pcap_open(if_info->name, MIN_PACKET_SIZE, 0, 0, NULL, errbuf);
|
|
|
|
#else
|
|
|
|
pch = pcap_open_live(if_info->name, MIN_PACKET_SIZE, 0, 0, errbuf);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (pch) {
|
2010-03-01 23:43:47 +00:00
|
|
|
if_stat = (if_stat_t *)g_malloc(sizeof(if_stat_t));
|
2008-06-20 00:09:47 +00:00
|
|
|
if_stat->name = g_strdup(if_info->name);
|
|
|
|
if_stat->pch = pch;
|
|
|
|
stat_list = g_list_append(stat_list, if_stat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!stat_list) {
|
|
|
|
cmdarg_err("There are no interfaces on which a capture can be done");
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2010-05-18 00:20:30 +00:00
|
|
|
if (capture_child) {
|
|
|
|
/* Let our parent know we succeeded. */
|
2013-07-15 15:51:45 +00:00
|
|
|
pipe_write_block(2, SP_SUCCESS, NULL);
|
2010-05-18 00:20:30 +00:00
|
|
|
}
|
2010-05-17 18:40:23 +00:00
|
|
|
|
2008-06-20 00:09:47 +00:00
|
|
|
if (!machine_readable) {
|
|
|
|
printf("%-15s %10s %10s\n", "Interface", "Received",
|
|
|
|
"Dropped");
|
|
|
|
}
|
|
|
|
|
2008-06-23 21:22:11 +00:00
|
|
|
global_ld.go = TRUE;
|
|
|
|
while (global_ld.go) {
|
2008-06-20 00:09:47 +00:00
|
|
|
for (stat_entry = g_list_first(stat_list); stat_entry != NULL; stat_entry = g_list_next(stat_entry)) {
|
2010-03-01 23:43:47 +00:00
|
|
|
if_stat = (if_stat_t *)stat_entry->data;
|
2008-06-20 00:09:47 +00:00
|
|
|
pcap_stats(if_stat->pch, &ps);
|
|
|
|
|
|
|
|
if (!machine_readable) {
|
2008-08-19 05:10:16 +00:00
|
|
|
printf("%-15s %10u %10u\n", if_stat->name,
|
2008-06-20 00:09:47 +00:00
|
|
|
ps.ps_recv, ps.ps_drop);
|
|
|
|
} else {
|
2008-08-19 05:10:16 +00:00
|
|
|
printf("%s\t%u\t%u\n", if_stat->name,
|
2008-06-20 00:09:47 +00:00
|
|
|
ps.ps_recv, ps.ps_drop);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef _WIN32
|
2013-08-25 22:41:54 +00:00
|
|
|
/* If we have a dummy signal pipe check it */
|
|
|
|
if (!signal_pipe_check_running()) {
|
|
|
|
global_ld.go = FALSE;
|
|
|
|
}
|
2011-05-15 23:41:21 +00:00
|
|
|
Sleep(1 * 1000);
|
2008-06-20 00:09:47 +00:00
|
|
|
#else
|
|
|
|
sleep(1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX - Not reached. Should we look for 'q' in stdin? */
|
|
|
|
for (stat_entry = g_list_first(stat_list); stat_entry != NULL; stat_entry = g_list_next(stat_entry)) {
|
2010-03-01 23:43:47 +00:00
|
|
|
if_stat = (if_stat_t *)stat_entry->data;
|
2008-06-20 00:09:47 +00:00
|
|
|
pcap_close(if_stat->pch);
|
|
|
|
g_free(if_stat->name);
|
|
|
|
g_free(if_stat);
|
|
|
|
}
|
|
|
|
g_list_free(stat_list);
|
|
|
|
free_interface_list(if_list);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-11-28 08:54:52 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2007-11-21 22:49:02 +00:00
|
|
|
static BOOL WINAPI
|
2009-09-13 17:46:10 +00:00
|
|
|
capture_cleanup_handler(DWORD dwCtrlType)
|
2005-11-28 08:54:52 +00:00
|
|
|
{
|
2007-11-21 22:49:02 +00:00
|
|
|
/* CTRL_C_EVENT is sort of like SIGINT, CTRL_BREAK_EVENT is unique to
|
|
|
|
Windows, CTRL_CLOSE_EVENT is sort of like SIGHUP, CTRL_LOGOFF_EVENT
|
|
|
|
is also sort of like SIGHUP, and CTRL_SHUTDOWN_EVENT is sort of
|
|
|
|
like SIGTERM at least when the machine's shutting down.
|
|
|
|
|
|
|
|
For now, if we're running as a command rather than a capture child,
|
|
|
|
we handle all but CTRL_LOGOFF_EVENT as indications that we should
|
|
|
|
clean up and quit, just as we handle SIGINT, SIGHUP, and SIGTERM
|
|
|
|
in that way on UN*X.
|
|
|
|
|
|
|
|
If we're not running as a capture child, we might be running as
|
|
|
|
a service; ignore CTRL_LOGOFF_EVENT, so we keep running after the
|
|
|
|
user logs out. (XXX - can we explicitly check whether we're
|
|
|
|
running as a service?) */
|
|
|
|
|
2006-02-17 02:18:48 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO,
|
2006-10-20 06:24:56 +00:00
|
|
|
"Console: Control signal");
|
2006-02-17 02:18:48 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG,
|
2006-10-20 06:24:56 +00:00
|
|
|
"Console: Control signal, CtrlType: %u", dwCtrlType);
|
|
|
|
|
|
|
|
/* Keep capture running if we're a service and a user logs off */
|
|
|
|
if (capture_child || (dwCtrlType != CTRL_LOGOFF_EVENT)) {
|
|
|
|
capture_loop_stop();
|
|
|
|
return TRUE;
|
|
|
|
} else {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2005-11-28 08:54:52 +00:00
|
|
|
}
|
2007-11-21 22:49:02 +00:00
|
|
|
#else
|
|
|
|
static void
|
2011-05-26 23:20:35 +00:00
|
|
|
capture_cleanup_handler(int signum _U_)
|
2007-11-21 22:49:02 +00:00
|
|
|
{
|
|
|
|
/* On UN*X, we cleanly shut down the capture on SIGINT, SIGHUP, and
|
|
|
|
SIGTERM. We assume that if the user wanted it to keep running
|
|
|
|
after they logged out, they'd have nohupped it. */
|
|
|
|
|
2009-09-13 17:46:10 +00:00
|
|
|
/* Note: don't call g_log() in the signal handler: if we happened to be in
|
|
|
|
* g_log() in process context when the signal came in, g_log will detect
|
|
|
|
* the "recursion" and abort.
|
|
|
|
*/
|
2007-11-21 22:49:02 +00:00
|
|
|
|
|
|
|
capture_loop_stop();
|
|
|
|
}
|
2005-11-28 08:54:52 +00:00
|
|
|
#endif
|
|
|
|
|
2010-05-26 00:19:27 +00:00
|
|
|
|
|
|
|
static void
|
2011-05-26 14:33:55 +00:00
|
|
|
report_capture_count(gboolean reportit)
|
2010-05-26 00:19:27 +00:00
|
|
|
{
|
2011-03-21 16:57:11 +00:00
|
|
|
/* Don't print this if we're a capture child. */
|
2011-05-26 14:33:55 +00:00
|
|
|
if (!capture_child && reportit) {
|
2015-02-09 18:42:51 +00:00
|
|
|
fprintf(stderr, "\rPackets captured: %d\n", global_ld.packet_count);
|
2011-05-26 14:33:55 +00:00
|
|
|
/* stderr could be line buffered */
|
|
|
|
fflush(stderr);
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
2011-02-24 19:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef SIGINFO
|
|
|
|
static void
|
|
|
|
report_counts_for_siginfo(void)
|
|
|
|
{
|
2011-05-26 14:33:55 +00:00
|
|
|
report_capture_count(quiet);
|
2011-03-21 16:57:11 +00:00
|
|
|
infoprint = FALSE; /* we just reported it */
|
2010-05-26 00:19:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
report_counts_siginfo(int signum _U_)
|
|
|
|
{
|
2011-03-21 16:57:11 +00:00
|
|
|
int sav_errno = errno;
|
|
|
|
|
|
|
|
/* If we've been told to delay printing, just set a flag asking
|
|
|
|
that we print counts (if we're supposed to), otherwise print
|
|
|
|
the count of packets captured (if we're supposed to). */
|
|
|
|
if (infodelay)
|
|
|
|
infoprint = TRUE;
|
|
|
|
else
|
|
|
|
report_counts_for_siginfo();
|
|
|
|
errno = sav_errno;
|
2010-05-26 00:19:27 +00:00
|
|
|
}
|
|
|
|
#endif /* SIGINFO */
|
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
static void
|
|
|
|
exit_main(int status)
|
2005-12-31 10:51:15 +00:00
|
|
|
{
|
2005-12-01 20:19:30 +00:00
|
|
|
#ifdef _WIN32
|
2011-03-21 16:57:11 +00:00
|
|
|
/* Shutdown windows sockets */
|
|
|
|
WSACleanup();
|
2005-12-01 20:19:30 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
/* can be helpful for debugging */
|
2006-02-11 23:25:11 +00:00
|
|
|
#ifdef DEBUG_DUMPCAP
|
2011-03-21 16:57:11 +00:00
|
|
|
printf("Press any key\n");
|
|
|
|
_getch();
|
2006-02-11 23:25:11 +00:00
|
|
|
#endif
|
|
|
|
|
2006-04-03 00:52:59 +00:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
2017-01-25 09:16:35 +00:00
|
|
|
capture_opts_cleanup(&global_capture_opts);
|
2011-03-21 16:57:11 +00:00
|
|
|
exit(status);
|
2005-12-01 20:19:30 +00:00
|
|
|
}
|
|
|
|
|
2007-11-20 16:53:01 +00:00
|
|
|
#ifdef HAVE_LIBCAP
|
|
|
|
/*
|
2012-10-03 15:42:41 +00:00
|
|
|
* If we were linked with libcap (not related to libpcap), make sure we have
|
2007-11-20 16:53:01 +00:00
|
|
|
* CAP_NET_ADMIN and CAP_NET_RAW, then relinquish our permissions.
|
2008-03-22 19:04:26 +00:00
|
|
|
* (See comment in main() for details)
|
2007-11-20 16:53:01 +00:00
|
|
|
*/
|
2008-02-16 02:39:58 +00:00
|
|
|
static void
|
2007-11-20 16:53:01 +00:00
|
|
|
relinquish_privs_except_capture(void)
|
|
|
|
{
|
2008-03-24 16:22:30 +00:00
|
|
|
/* If 'started_with_special_privs' (ie: suid) then enable for
|
2008-03-22 19:04:26 +00:00
|
|
|
* ourself the NET_ADMIN and NET_RAW capabilities and then
|
|
|
|
* drop our suid privileges.
|
|
|
|
*
|
|
|
|
* CAP_NET_ADMIN: Promiscuous mode and a truckload of other
|
2007-11-20 16:53:01 +00:00
|
|
|
* stuff we don't need (and shouldn't have).
|
|
|
|
* CAP_NET_RAW: Packet capture (raw sockets).
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (started_with_special_privs()) {
|
2008-03-22 19:04:26 +00:00
|
|
|
cap_value_t cap_list[2] = { CAP_NET_ADMIN, CAP_NET_RAW };
|
|
|
|
int cl_len = sizeof(cap_list) / sizeof(cap_value_t);
|
|
|
|
|
|
|
|
cap_t caps = cap_init(); /* all capabilities initialized to off */
|
|
|
|
|
2007-11-20 16:53:01 +00:00
|
|
|
print_caps("Pre drop, pre set");
|
2008-03-22 19:04:26 +00:00
|
|
|
|
2007-11-20 16:53:01 +00:00
|
|
|
if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) == -1) {
|
2011-06-28 09:00:11 +00:00
|
|
|
cmdarg_err("prctl() fail return: %s", g_strerror(errno));
|
2007-11-20 16:53:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cap_set_flag(caps, CAP_PERMITTED, cl_len, cap_list, CAP_SET);
|
|
|
|
cap_set_flag(caps, CAP_INHERITABLE, cl_len, cap_list, CAP_SET);
|
|
|
|
|
|
|
|
if (cap_set_proc(caps)) {
|
2011-06-28 09:00:11 +00:00
|
|
|
cmdarg_err("cap_set_proc() fail return: %s", g_strerror(errno));
|
2007-11-20 16:53:01 +00:00
|
|
|
}
|
|
|
|
print_caps("Pre drop, post set");
|
|
|
|
|
2008-03-22 19:04:26 +00:00
|
|
|
relinquish_special_privs_perm();
|
2007-11-20 16:53:01 +00:00
|
|
|
|
2008-03-20 19:18:33 +00:00
|
|
|
print_caps("Post drop, pre set");
|
|
|
|
cap_set_flag(caps, CAP_EFFECTIVE, cl_len, cap_list, CAP_SET);
|
|
|
|
if (cap_set_proc(caps)) {
|
2011-06-28 09:00:11 +00:00
|
|
|
cmdarg_err("cap_set_proc() fail return: %s", g_strerror(errno));
|
2008-03-20 19:18:33 +00:00
|
|
|
}
|
|
|
|
print_caps("Post drop, post set");
|
2008-03-22 19:04:26 +00:00
|
|
|
|
|
|
|
cap_free(caps);
|
2007-11-20 16:53:01 +00:00
|
|
|
}
|
2008-03-22 19:04:26 +00:00
|
|
|
}
|
|
|
|
|
2007-11-20 16:53:01 +00:00
|
|
|
#endif /* HAVE_LIBCAP */
|
|
|
|
|
2008-02-16 02:39:58 +00:00
|
|
|
/* Take care of byte order in the libpcap headers read from pipes.
|
|
|
|
* (function taken from wiretap/libpcap.c) */
|
|
|
|
static void
|
|
|
|
cap_pipe_adjust_header(gboolean byte_swapped, struct pcap_hdr *hdr, struct pcaprec_hdr *rechdr)
|
|
|
|
{
|
2011-03-21 16:57:11 +00:00
|
|
|
if (byte_swapped) {
|
|
|
|
/* Byte-swap the record header fields. */
|
2013-11-29 19:21:20 +00:00
|
|
|
rechdr->ts_sec = GUINT32_SWAP_LE_BE(rechdr->ts_sec);
|
|
|
|
rechdr->ts_usec = GUINT32_SWAP_LE_BE(rechdr->ts_usec);
|
|
|
|
rechdr->incl_len = GUINT32_SWAP_LE_BE(rechdr->incl_len);
|
|
|
|
rechdr->orig_len = GUINT32_SWAP_LE_BE(rechdr->orig_len);
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* In file format version 2.3, the "incl_len" and "orig_len" fields were
|
|
|
|
swapped, in order to match the BPF header layout.
|
|
|
|
|
|
|
|
Unfortunately, some files were, according to a comment in the "libpcap"
|
|
|
|
source, written with version 2.3 in their headers but without the
|
|
|
|
interchanged fields, so if "incl_len" is greater than "orig_len" - which
|
|
|
|
would make no sense - we assume that we need to swap them. */
|
|
|
|
if (hdr->version_major == 2 &&
|
|
|
|
(hdr->version_minor < 3 ||
|
|
|
|
(hdr->version_minor == 3 && rechdr->incl_len > rechdr->orig_len))) {
|
|
|
|
guint32 temp;
|
|
|
|
|
|
|
|
temp = rechdr->orig_len;
|
|
|
|
rechdr->orig_len = rechdr->incl_len;
|
|
|
|
rechdr->incl_len = temp;
|
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 11:54:26 +00:00
|
|
|
/* Wrapper: distinguish between recv/read if we're reading on Windows,
|
|
|
|
* or just read().
|
|
|
|
*/
|
2012-12-26 05:57:06 +00:00
|
|
|
static ssize_t
|
2012-06-08 11:54:26 +00:00
|
|
|
cap_pipe_read(int pipe_fd, char *buf, size_t sz, gboolean from_socket _U_)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (from_socket) {
|
2012-06-08 12:27:22 +00:00
|
|
|
return recv(pipe_fd, buf, (int)sz, 0);
|
2012-06-08 11:54:26 +00:00
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
return ws_read(pipe_fd, buf, sz);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-11-07 23:42:53 +00:00
|
|
|
#if defined(_WIN32)
|
2009-08-26 23:16:37 +00:00
|
|
|
/*
|
|
|
|
* Thread function that reads from a pipe and pushes the data
|
|
|
|
* to the main application thread.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* XXX Right now we use async queues for basic signaling. The main thread
|
|
|
|
* sets cap_pipe_buf and cap_bytes_to_read, then pushes an item onto
|
|
|
|
* cap_pipe_pending_q which triggers a read in the cap_pipe_read thread.
|
|
|
|
* Iff the read is successful cap_pipe_read pushes an item onto
|
|
|
|
* cap_pipe_done_q, otherwise an error is signaled. No data is passed in
|
|
|
|
* the queues themselves (yet).
|
2009-09-08 18:02:43 +00:00
|
|
|
*
|
2009-08-26 23:16:37 +00:00
|
|
|
* We might want to move some of the cap_pipe_dispatch logic here so that
|
2012-06-08 11:54:26 +00:00
|
|
|
* we can let cap_thread_read run independently, queuing up multiple reads
|
2009-08-26 23:16:37 +00:00
|
|
|
* for the main thread (and possibly get rid of cap_pipe_read_mtx).
|
|
|
|
*/
|
2012-06-08 11:54:26 +00:00
|
|
|
static void *cap_thread_read(void *arg)
|
2011-03-21 16:57:11 +00:00
|
|
|
{
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_options *pcap_opts;
|
2009-08-26 23:16:37 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
BOOL res;
|
2012-12-26 15:02:32 +00:00
|
|
|
DWORD b, last_err, bytes_read;
|
2009-08-26 23:16:37 +00:00
|
|
|
#else /* _WIN32 */
|
2012-12-26 15:02:32 +00:00
|
|
|
size_t bytes_read;
|
2009-08-26 23:16:37 +00:00
|
|
|
int b;
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts = (pcap_options *)arg;
|
2011-05-15 22:54:52 +00:00
|
|
|
while (pcap_opts->cap_pipe_err == PIPOK) {
|
|
|
|
g_async_queue_pop(pcap_opts->cap_pipe_pending_q); /* Wait for our cue (ahem) from the main thread */
|
|
|
|
g_mutex_lock(pcap_opts->cap_pipe_read_mtx);
|
2009-08-26 23:16:37 +00:00
|
|
|
bytes_read = 0;
|
2012-12-26 05:57:06 +00:00
|
|
|
while (bytes_read < pcap_opts->cap_pipe_bytes_to_read) {
|
2012-06-21 13:23:45 +00:00
|
|
|
if ((pcap_opts->from_cap_socket)
|
2012-06-08 11:54:26 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
|| 1
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
{
|
|
|
|
b = cap_pipe_read(pcap_opts->cap_pipe_fd, pcap_opts->cap_pipe_buf+bytes_read,
|
|
|
|
pcap_opts->cap_pipe_bytes_to_read - bytes_read, pcap_opts->from_cap_socket);
|
|
|
|
if (b <= 0) {
|
|
|
|
if (b == 0) {
|
|
|
|
pcap_opts->cap_pipe_err = PIPEOF;
|
|
|
|
bytes_read = 0;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
pcap_opts->cap_pipe_err = PIPERR;
|
|
|
|
bytes_read = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bytes_read += b;
|
|
|
|
}
|
|
|
|
}
|
2009-08-26 23:16:37 +00:00
|
|
|
#ifdef _WIN32
|
2012-06-08 11:54:26 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* If we try to use read() on a named pipe on Windows with partial
|
|
|
|
* data it appears to return EOF.
|
|
|
|
*/
|
|
|
|
res = ReadFile(pcap_opts->cap_pipe_h, pcap_opts->cap_pipe_buf+bytes_read,
|
|
|
|
pcap_opts->cap_pipe_bytes_to_read - bytes_read,
|
|
|
|
&b, NULL);
|
|
|
|
|
|
|
|
bytes_read += b;
|
|
|
|
if (!res) {
|
|
|
|
last_err = GetLastError();
|
|
|
|
if (last_err == ERROR_MORE_DATA) {
|
|
|
|
continue;
|
|
|
|
} else if (last_err == ERROR_HANDLE_EOF || last_err == ERROR_BROKEN_PIPE || last_err == ERROR_PIPE_NOT_CONNECTED) {
|
|
|
|
pcap_opts->cap_pipe_err = PIPEOF;
|
|
|
|
bytes_read = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pcap_opts->cap_pipe_err = PIPERR;
|
|
|
|
bytes_read = -1;
|
|
|
|
break;
|
|
|
|
} else if (b == 0 && pcap_opts->cap_pipe_bytes_to_read > 0) {
|
|
|
|
pcap_opts->cap_pipe_err = PIPEOF;
|
|
|
|
bytes_read = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-08-26 23:16:37 +00:00
|
|
|
#endif /*_WIN32 */
|
|
|
|
}
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->cap_pipe_bytes_read = bytes_read;
|
|
|
|
if (pcap_opts->cap_pipe_bytes_read >= pcap_opts->cap_pipe_bytes_to_read) {
|
|
|
|
g_async_queue_push(pcap_opts->cap_pipe_done_q, pcap_opts->cap_pipe_buf); /* Any non-NULL value will do */
|
2009-08-26 23:16:37 +00:00
|
|
|
}
|
2011-05-15 22:54:52 +00:00
|
|
|
g_mutex_unlock(pcap_opts->cap_pipe_read_mtx);
|
2009-08-26 23:16:37 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-07-16 12:45:47 +00:00
|
|
|
#endif
|
2009-08-26 23:16:37 +00:00
|
|
|
|
2008-02-16 02:39:58 +00:00
|
|
|
/* Provide select() functionality for a single file descriptor
|
2009-08-26 23:16:37 +00:00
|
|
|
* on UNIX/POSIX. Windows uses cap_pipe_read via a thread.
|
2008-02-16 02:39:58 +00:00
|
|
|
*
|
2011-06-13 15:28:37 +00:00
|
|
|
* Returns the same values as select.
|
2008-02-16 02:39:58 +00:00
|
|
|
*/
|
|
|
|
static int
|
2011-03-21 16:57:11 +00:00
|
|
|
cap_pipe_select(int pipe_fd)
|
|
|
|
{
|
|
|
|
fd_set rfds;
|
|
|
|
struct timeval timeout;
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(pipe_fd, &rfds);
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
timeout.tv_sec = PIPE_READ_TIMEOUT / 1000000;
|
|
|
|
timeout.tv_usec = PIPE_READ_TIMEOUT % 1000000;
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-06-13 15:28:37 +00:00
|
|
|
return select(pipe_fd+1, &rfds, NULL, NULL, &timeout);
|
2008-02-16 02:39:58 +00:00
|
|
|
}
|
2012-06-08 11:54:26 +00:00
|
|
|
|
|
|
|
#define DEF_TCP_PORT 19000
|
|
|
|
|
|
|
|
static int
|
|
|
|
cap_open_socket(char *pipename, pcap_options *pcap_opts, char *errmsg, int errmsgl)
|
|
|
|
{
|
|
|
|
char *sockname = pipename + 4;
|
|
|
|
struct sockaddr_in sa;
|
|
|
|
char buf[16];
|
|
|
|
char *p;
|
|
|
|
unsigned long port;
|
|
|
|
size_t len;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
|
|
|
|
p = strchr(sockname, ':');
|
|
|
|
if (p == NULL) {
|
|
|
|
len = strlen(sockname);
|
|
|
|
port = DEF_TCP_PORT;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
len = p - sockname;
|
|
|
|
port = strtoul(p + 1, &p, 10);
|
|
|
|
if (*p || port > 65535) {
|
|
|
|
goto fail_invalid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len > 15) {
|
|
|
|
goto fail_invalid;
|
|
|
|
}
|
|
|
|
|
2014-02-25 13:05:11 +00:00
|
|
|
g_snprintf ( buf,(gulong)len + 1, "%s", sockname );
|
2012-06-08 11:54:26 +00:00
|
|
|
buf[len] = '\0';
|
2016-02-10 09:11:12 +00:00
|
|
|
if (!ws_inet_pton4(buf, (guint32 *)&sa.sin_addr)) {
|
2012-06-08 11:54:26 +00:00
|
|
|
goto fail_invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
sa.sin_family = AF_INET;
|
2014-02-25 13:05:11 +00:00
|
|
|
sa.sin_port = g_htons((u_short)port);
|
2012-06-08 11:54:26 +00:00
|
|
|
|
2012-06-09 03:36:15 +00:00
|
|
|
if (((fd = (int)socket(AF_INET, SOCK_STREAM, 0)) < 0) ||
|
2012-06-08 11:54:26 +00:00
|
|
|
(connect(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)) {
|
2012-06-09 03:36:15 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
LPTSTR errorText = NULL;
|
|
|
|
int lastError;
|
|
|
|
|
|
|
|
lastError = WSAGetLastError();
|
2012-06-21 13:23:45 +00:00
|
|
|
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
|
2012-06-09 03:36:15 +00:00
|
|
|
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL, lastError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
(LPTSTR)&errorText, 0, NULL);
|
|
|
|
#endif
|
2012-06-08 11:54:26 +00:00
|
|
|
g_snprintf(errmsg, errmsgl,
|
2012-06-09 03:36:15 +00:00
|
|
|
"The capture session could not be initiated due to the socket error: \n"
|
2012-06-08 11:54:26 +00:00
|
|
|
#ifdef _WIN32
|
2012-06-09 03:36:15 +00:00
|
|
|
" %d: %S", lastError, errorText ? (char *)errorText : "Unknown");
|
|
|
|
if (errorText)
|
|
|
|
LocalFree(errorText);
|
2012-06-08 11:54:26 +00:00
|
|
|
#else
|
2014-02-25 13:05:11 +00:00
|
|
|
" %d: %s", errno, g_strerror(errno));
|
2011-07-15 14:31:27 +00:00
|
|
|
#endif
|
2012-06-08 11:54:26 +00:00
|
|
|
pcap_opts->cap_pipe_err = PIPERR;
|
2012-06-09 03:36:15 +00:00
|
|
|
|
|
|
|
if (fd >= 0)
|
|
|
|
cap_pipe_close(fd, TRUE);
|
2012-06-08 11:54:26 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pcap_opts->from_cap_socket = TRUE;
|
|
|
|
return fd;
|
|
|
|
|
|
|
|
fail_invalid:
|
|
|
|
g_snprintf(errmsg, errmsgl,
|
|
|
|
"The capture session could not be initiated because\n"
|
|
|
|
"\"%s\" is not a valid socket specification", pipename);
|
|
|
|
pcap_opts->cap_pipe_err = PIPERR;
|
|
|
|
return -1;
|
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2012-06-08 11:54:26 +00:00
|
|
|
/* Wrapper: distinguish between closesocket on Windows; use ws_close
|
|
|
|
* otherwise.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
cap_pipe_close(int pipe_fd, gboolean from_socket _U_)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (from_socket) {
|
|
|
|
closesocket(pipe_fd);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
ws_close(pipe_fd);
|
|
|
|
#endif
|
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
|
|
|
|
/* Mimic pcap_open_live() for pipe captures
|
2010-03-13 00:29:30 +00:00
|
|
|
|
|
|
|
* We check if "pipename" is "-" (stdin), a AF_UNIX socket, or a FIFO,
|
|
|
|
* open it, and read the header.
|
|
|
|
*
|
2008-02-16 02:39:58 +00:00
|
|
|
* N.B. : we can't read the libpcap formats used in RedHat 6.1 or SuSE 6.3
|
|
|
|
* because we can't seek on pipes (see wiretap/libpcap.c for details) */
|
2009-08-26 23:16:37 +00:00
|
|
|
static void
|
2011-05-15 22:54:52 +00:00
|
|
|
cap_pipe_open_live(char *pipename,
|
|
|
|
pcap_options *pcap_opts,
|
|
|
|
struct pcap_hdr *hdr,
|
2010-03-13 00:29:30 +00:00
|
|
|
char *errmsg, int errmsgl)
|
2008-02-16 02:39:58 +00:00
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
2012-11-25 18:35:41 +00:00
|
|
|
ws_statb64 pipe_stat;
|
2011-03-21 16:57:11 +00:00
|
|
|
struct sockaddr_un sa;
|
2009-08-26 23:16:37 +00:00
|
|
|
#else /* _WIN32 */
|
2012-11-25 18:35:41 +00:00
|
|
|
char *pncopy, *pos;
|
2011-03-21 16:57:11 +00:00
|
|
|
wchar_t *err_str;
|
2015-04-10 17:04:22 +00:00
|
|
|
#ifdef HAVE_EXTCAP
|
|
|
|
char* extcap_pipe_name;
|
|
|
|
#endif
|
2009-06-26 21:25:09 +00:00
|
|
|
#endif
|
2016-01-04 17:32:39 +00:00
|
|
|
#ifdef HAVE_EXTCAP
|
|
|
|
gboolean extcap_pipe = FALSE;
|
|
|
|
interface_options interface_opts;
|
2016-08-01 15:33:05 +00:00
|
|
|
#endif
|
2012-12-26 05:57:06 +00:00
|
|
|
ssize_t b;
|
2014-02-25 16:25:47 +00:00
|
|
|
int fd = -1, sel_ret;
|
2012-12-26 05:57:06 +00:00
|
|
|
size_t bytes_read;
|
|
|
|
guint32 magic = 0;
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->cap_pipe_fd = -1;
|
2012-06-08 11:54:26 +00:00
|
|
|
#ifdef _WIN32
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->cap_pipe_h = INVALID_HANDLE_VALUE;
|
2009-08-26 23:16:37 +00:00
|
|
|
#endif
|
2015-04-10 17:04:22 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "cap_pipe_open_live: %s", pipename);
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
/*
|
|
|
|
* XXX - this blocks until a pcap per-file header has been written to
|
|
|
|
* the pipe, so it could block indefinitely.
|
|
|
|
*/
|
|
|
|
if (strcmp(pipename, "-") == 0) {
|
2009-08-26 23:16:37 +00:00
|
|
|
#ifndef _WIN32
|
2011-03-21 16:57:11 +00:00
|
|
|
fd = 0; /* read from stdin */
|
2009-08-26 23:16:37 +00:00
|
|
|
#else /* _WIN32 */
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->cap_pipe_h = GetStdHandle(STD_INPUT_HANDLE);
|
2008-02-16 02:39:58 +00:00
|
|
|
#endif /* _WIN32 */
|
2012-06-08 11:54:26 +00:00
|
|
|
} else if (!strncmp(pipename, "TCP@", 4)) {
|
|
|
|
if ((fd = cap_open_socket(pipename, pcap_opts, errmsg, errmsgl)) < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2010-03-13 00:29:30 +00:00
|
|
|
} else {
|
2016-01-04 17:32:39 +00:00
|
|
|
|
2016-08-01 15:33:05 +00:00
|
|
|
#ifdef HAVE_EXTCAP
|
2016-01-04 17:32:39 +00:00
|
|
|
interface_opts = g_array_index(global_capture_opts.ifaces, interface_options, 0);
|
2016-08-01 15:33:05 +00:00
|
|
|
#endif
|
2016-01-04 17:32:39 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
#ifndef _WIN32
|
2016-01-04 17:32:39 +00:00
|
|
|
#ifdef HAVE_EXTCAP
|
|
|
|
if ( g_strrstr(interface_opts.name, EXTCAP_PIPE_PREFIX) != NULL )
|
|
|
|
extcap_pipe = TRUE;
|
|
|
|
#endif
|
|
|
|
|
2011-04-10 20:59:10 +00:00
|
|
|
if (ws_stat64(pipename, &pipe_stat) < 0) {
|
2011-03-21 16:57:11 +00:00
|
|
|
if (errno == ENOENT || errno == ENOTDIR)
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->cap_pipe_err = PIPNEXIST;
|
2011-03-21 16:57:11 +00:00
|
|
|
else {
|
|
|
|
g_snprintf(errmsg, errmsgl,
|
|
|
|
"The capture session could not be initiated "
|
2016-07-09 07:07:52 +00:00
|
|
|
"due to error getting information on pipe/socket: %s.", g_strerror(errno));
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->cap_pipe_err = PIPERR;
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (S_ISFIFO(pipe_stat.st_mode)) {
|
|
|
|
fd = ws_open(pipename, O_RDONLY | O_NONBLOCK, 0000 /* no creation so don't matter */);
|
|
|
|
if (fd == -1) {
|
|
|
|
g_snprintf(errmsg, errmsgl,
|
|
|
|
"The capture session could not be initiated "
|
2016-07-09 07:07:52 +00:00
|
|
|
"due to error on pipe open: %s.", g_strerror(errno));
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->cap_pipe_err = PIPERR;
|
2011-03-21 16:57:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (S_ISSOCK(pipe_stat.st_mode)) {
|
|
|
|
fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (fd == -1) {
|
|
|
|
g_snprintf(errmsg, errmsgl,
|
|
|
|
"The capture session could not be initiated "
|
2016-07-09 07:07:52 +00:00
|
|
|
"due to error on socket create: %s.", g_strerror(errno));
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->cap_pipe_err = PIPERR;
|
2011-03-21 16:57:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
sa.sun_family = AF_UNIX;
|
|
|
|
/*
|
|
|
|
* The Single UNIX Specification says:
|
|
|
|
*
|
|
|
|
* The size of sun_path has intentionally been left undefined.
|
|
|
|
* This is because different implementations use different sizes.
|
|
|
|
* For example, 4.3 BSD uses a size of 108, and 4.4 BSD uses a size
|
|
|
|
* of 104. Since most implementations originate from BSD versions,
|
|
|
|
* the size is typically in the range 92 to 108.
|
|
|
|
*
|
|
|
|
* Applications should not assume a particular length for sun_path
|
|
|
|
* or assume that it can hold {_POSIX_PATH_MAX} bytes (256).
|
|
|
|
*
|
|
|
|
* It also says
|
|
|
|
*
|
|
|
|
* The <sys/un.h> header shall define the sockaddr_un structure,
|
|
|
|
* which shall include at least the following members:
|
|
|
|
*
|
|
|
|
* sa_family_t sun_family Address family.
|
|
|
|
* char sun_path[] Socket pathname.
|
|
|
|
*
|
|
|
|
* so we assume that it's an array, with a specified size,
|
|
|
|
* and that the size reflects the maximum path length.
|
|
|
|
*/
|
|
|
|
if (g_strlcpy(sa.sun_path, pipename, sizeof sa.sun_path) > sizeof sa.sun_path) {
|
|
|
|
/* Path name too long */
|
|
|
|
g_snprintf(errmsg, errmsgl,
|
|
|
|
"The capture session coud not be initiated "
|
2016-07-09 07:07:52 +00:00
|
|
|
"due to error on socket connect: Path name too long.");
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->cap_pipe_err = PIPERR;
|
2012-05-25 01:32:26 +00:00
|
|
|
ws_close(fd);
|
2011-03-21 16:57:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
b = connect(fd, (struct sockaddr *)&sa, sizeof sa);
|
|
|
|
if (b == -1) {
|
|
|
|
g_snprintf(errmsg, errmsgl,
|
|
|
|
"The capture session coud not be initiated "
|
2016-07-09 07:07:52 +00:00
|
|
|
"due to error on socket connect: %s.", g_strerror(errno));
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->cap_pipe_err = PIPERR;
|
2012-05-25 01:32:26 +00:00
|
|
|
ws_close(fd);
|
2011-03-21 16:57:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (S_ISCHR(pipe_stat.st_mode)) {
|
|
|
|
/*
|
|
|
|
* Assume the user specified an interface on a system where
|
|
|
|
* interfaces are in /dev. Pretend we haven't seen it.
|
|
|
|
*/
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->cap_pipe_err = PIPNEXIST;
|
2012-12-23 09:54:05 +00:00
|
|
|
} else {
|
2011-03-21 16:57:11 +00:00
|
|
|
g_snprintf(errmsg, errmsgl,
|
|
|
|
"The capture session could not be initiated because\n"
|
2016-07-09 07:07:52 +00:00
|
|
|
"\"%s\" is neither an interface nor a socket nor a pipe.", pipename);
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->cap_pipe_err = PIPERR;
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2016-01-04 17:32:39 +00:00
|
|
|
|
2008-02-16 02:39:58 +00:00
|
|
|
#else /* _WIN32 */
|
|
|
|
#define PIPE_STR "\\pipe\\"
|
2011-03-21 16:57:11 +00:00
|
|
|
/* Under Windows, named pipes _must_ have the form
|
|
|
|
* "\\<server>\pipe\<pipename>". <server> may be "." for localhost.
|
|
|
|
*/
|
|
|
|
pncopy = g_strdup(pipename);
|
|
|
|
if ( (pos=strstr(pncopy, "\\\\")) == pncopy) {
|
|
|
|
pos = strchr(pncopy + 3, '\\');
|
|
|
|
if (pos && g_ascii_strncasecmp(pos, PIPE_STR, strlen(PIPE_STR)) != 0)
|
|
|
|
pos = NULL;
|
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
g_free(pncopy);
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
if (!pos) {
|
|
|
|
g_snprintf(errmsg, errmsgl,
|
|
|
|
"The capture session could not be initiated because\n"
|
2016-07-09 07:07:52 +00:00
|
|
|
"\"%s\" is neither an interface nor a pipe.", pipename);
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->cap_pipe_err = PIPNEXIST;
|
2011-03-21 16:57:11 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-04-10 17:04:22 +00:00
|
|
|
#ifdef HAVE_EXTCAP
|
|
|
|
extcap_pipe_name = g_strconcat("\\\\.\\pipe\\", EXTCAP_PIPE_PREFIX, NULL);
|
|
|
|
extcap_pipe = strstr(interface_opts.name, extcap_pipe_name) ? TRUE : FALSE;
|
|
|
|
g_free(extcap_pipe_name);
|
|
|
|
#endif
|
2014-02-25 13:05:11 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
/* Wait for the pipe to appear */
|
|
|
|
while (1) {
|
2014-02-25 13:05:11 +00:00
|
|
|
|
2015-04-10 17:04:22 +00:00
|
|
|
#ifdef HAVE_EXTCAP
|
|
|
|
if(extcap_pipe)
|
2014-02-25 13:05:11 +00:00
|
|
|
pcap_opts->cap_pipe_h = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
else
|
2015-04-10 17:04:22 +00:00
|
|
|
#endif
|
2014-02-25 13:05:11 +00:00
|
|
|
pcap_opts->cap_pipe_h = CreateFile(utf_8to16(pipename), GENERIC_READ, 0, NULL,
|
|
|
|
OPEN_EXISTING, 0, NULL);
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-05-15 22:54:52 +00:00
|
|
|
if (pcap_opts->cap_pipe_h != INVALID_HANDLE_VALUE)
|
2011-03-21 16:57:11 +00:00
|
|
|
break;
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
if (GetLastError() != ERROR_PIPE_BUSY) {
|
2012-06-09 03:36:15 +00:00
|
|
|
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS,
|
2011-03-21 16:57:11 +00:00
|
|
|
NULL, GetLastError(), 0, (LPTSTR) &err_str, 0, NULL);
|
|
|
|
g_snprintf(errmsg, errmsgl,
|
|
|
|
"The capture session on \"%s\" could not be started "
|
2016-07-09 07:07:52 +00:00
|
|
|
"due to error on pipe open: %s (error %d).",
|
2011-03-21 16:57:11 +00:00
|
|
|
pipename, utf_16to8(err_str), GetLastError());
|
|
|
|
LocalFree(err_str);
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->cap_pipe_err = PIPERR;
|
2011-03-21 16:57:11 +00:00
|
|
|
return;
|
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
if (!WaitNamedPipe(utf_8to16(pipename), 30 * 1000)) {
|
2012-06-09 03:36:15 +00:00
|
|
|
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS,
|
2014-02-25 13:05:11 +00:00
|
|
|
NULL, GetLastError(), 0, (LPTSTR) &err_str, 0, NULL);
|
2011-03-21 16:57:11 +00:00
|
|
|
g_snprintf(errmsg, errmsgl,
|
|
|
|
"The capture session on \"%s\" timed out during "
|
2016-07-09 07:07:52 +00:00
|
|
|
"pipe open: %s (error %d).",
|
2011-03-21 16:57:11 +00:00
|
|
|
pipename, utf_16to8(err_str), GetLastError());
|
|
|
|
LocalFree(err_str);
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->cap_pipe_err = PIPERR;
|
2011-03-21 16:57:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
#endif /* _WIN32 */
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->from_cap_pipe = TRUE;
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2013-03-19 05:09:37 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (pcap_opts->from_cap_socket)
|
2012-06-08 11:54:26 +00:00
|
|
|
#endif
|
2013-03-19 05:09:37 +00:00
|
|
|
{
|
2012-12-23 09:54:05 +00:00
|
|
|
/* read the pcap header */
|
|
|
|
bytes_read = 0;
|
|
|
|
while (bytes_read < sizeof magic) {
|
2014-02-25 16:25:47 +00:00
|
|
|
if (fd == -1) {
|
2016-07-09 07:07:52 +00:00
|
|
|
g_snprintf(errmsg, errmsgl, "Invalid file descriptor.");
|
2014-02-25 16:25:47 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2012-12-23 09:54:05 +00:00
|
|
|
sel_ret = cap_pipe_select(fd);
|
|
|
|
if (sel_ret < 0) {
|
|
|
|
g_snprintf(errmsg, errmsgl,
|
2016-07-09 07:07:52 +00:00
|
|
|
"Unexpected error from select: %s.", g_strerror(errno));
|
2012-12-23 09:54:05 +00:00
|
|
|
goto error;
|
|
|
|
} else if (sel_ret > 0) {
|
2012-12-26 05:57:06 +00:00
|
|
|
b = cap_pipe_read(fd, ((char *)&magic)+bytes_read,
|
|
|
|
sizeof magic-bytes_read,
|
|
|
|
pcap_opts->from_cap_socket);
|
2016-08-01 09:09:16 +00:00
|
|
|
#ifdef HAVE_EXTCAP
|
2016-01-04 17:32:39 +00:00
|
|
|
/* jump messaging, if extcap had an error, stderr will provide the correct message */
|
|
|
|
if (extcap_pipe && b <= 0)
|
|
|
|
goto error;
|
2016-08-01 09:09:16 +00:00
|
|
|
#endif
|
2012-12-23 09:54:05 +00:00
|
|
|
if (b <= 0) {
|
|
|
|
if (b == 0)
|
2016-07-09 07:07:52 +00:00
|
|
|
g_snprintf(errmsg, errmsgl, "End of file on pipe magic during open.");
|
2012-12-23 09:54:05 +00:00
|
|
|
else
|
2016-07-09 07:07:52 +00:00
|
|
|
g_snprintf(errmsg, errmsgl, "Error on pipe magic during open: %s.",
|
2012-12-23 09:54:05 +00:00
|
|
|
g_strerror(errno));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
bytes_read += b;
|
|
|
|
}
|
|
|
|
}
|
2012-06-21 13:23:45 +00:00
|
|
|
}
|
2012-06-08 11:54:26 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
else {
|
2012-12-18 11:58:22 +00:00
|
|
|
#if GLIB_CHECK_VERSION(2,31,0)
|
2012-12-23 09:54:05 +00:00
|
|
|
g_thread_new("cap_pipe_open_live", &cap_thread_read, pcap_opts);
|
2012-12-18 11:58:22 +00:00
|
|
|
#else
|
2012-12-23 09:54:05 +00:00
|
|
|
g_thread_create(&cap_thread_read, pcap_opts, FALSE, NULL);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
pcap_opts->cap_pipe_buf = (char *) &magic;
|
|
|
|
pcap_opts->cap_pipe_bytes_read = 0;
|
|
|
|
pcap_opts->cap_pipe_bytes_to_read = sizeof(magic);
|
|
|
|
/* We don't have to worry about cap_pipe_read_mtx here */
|
|
|
|
g_async_queue_push(pcap_opts->cap_pipe_pending_q, pcap_opts->cap_pipe_buf);
|
|
|
|
g_async_queue_pop(pcap_opts->cap_pipe_done_q);
|
2016-01-04 17:32:39 +00:00
|
|
|
/* jump messaging, if extcap had an error, stderr will provide the correct message */
|
|
|
|
if (pcap_opts->cap_pipe_bytes_read <= 0 && extcap_pipe)
|
|
|
|
goto error;
|
|
|
|
|
2012-12-23 09:54:05 +00:00
|
|
|
if (pcap_opts->cap_pipe_bytes_read <= 0) {
|
|
|
|
if (pcap_opts->cap_pipe_bytes_read == 0)
|
2016-07-09 07:07:52 +00:00
|
|
|
g_snprintf(errmsg, errmsgl, "End of file on pipe magic during open.");
|
2012-12-23 09:54:05 +00:00
|
|
|
else
|
2016-07-09 07:07:52 +00:00
|
|
|
g_snprintf(errmsg, errmsgl, "Error on pipe magic during open: %s.",
|
2012-12-23 09:54:05 +00:00
|
|
|
g_strerror(errno));
|
|
|
|
goto error;
|
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
}
|
2011-07-16 12:45:47 +00:00
|
|
|
#endif
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
switch (magic) {
|
|
|
|
case PCAP_MAGIC:
|
2012-03-10 00:24:42 +00:00
|
|
|
case PCAP_NSEC_MAGIC:
|
2011-03-21 16:57:11 +00:00
|
|
|
/* Host that wrote it has our byte order, and was running
|
|
|
|
a program using either standard or ss990417 libpcap. */
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->cap_pipe_byte_swapped = FALSE;
|
|
|
|
pcap_opts->cap_pipe_modified = FALSE;
|
2012-03-10 00:24:42 +00:00
|
|
|
pcap_opts->ts_nsec = magic == PCAP_NSEC_MAGIC;
|
2011-03-21 16:57:11 +00:00
|
|
|
break;
|
|
|
|
case PCAP_MODIFIED_MAGIC:
|
|
|
|
/* Host that wrote it has our byte order, but was running
|
|
|
|
a program using either ss990915 or ss991029 libpcap. */
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->cap_pipe_byte_swapped = FALSE;
|
|
|
|
pcap_opts->cap_pipe_modified = TRUE;
|
2011-03-21 16:57:11 +00:00
|
|
|
break;
|
|
|
|
case PCAP_SWAPPED_MAGIC:
|
2012-03-10 00:24:42 +00:00
|
|
|
case PCAP_SWAPPED_NSEC_MAGIC:
|
2011-03-21 16:57:11 +00:00
|
|
|
/* Host that wrote it has a byte order opposite to ours,
|
|
|
|
and was running a program using either standard or
|
|
|
|
ss990417 libpcap. */
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->cap_pipe_byte_swapped = TRUE;
|
|
|
|
pcap_opts->cap_pipe_modified = FALSE;
|
2012-03-10 00:24:42 +00:00
|
|
|
pcap_opts->ts_nsec = magic == PCAP_SWAPPED_NSEC_MAGIC;
|
2011-03-21 16:57:11 +00:00
|
|
|
break;
|
|
|
|
case PCAP_SWAPPED_MODIFIED_MAGIC:
|
|
|
|
/* Host that wrote it out has a byte order opposite to
|
|
|
|
ours, and was running a program using either ss990915
|
|
|
|
or ss991029 libpcap. */
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->cap_pipe_byte_swapped = TRUE;
|
|
|
|
pcap_opts->cap_pipe_modified = TRUE;
|
2011-03-21 16:57:11 +00:00
|
|
|
break;
|
2016-07-09 07:07:52 +00:00
|
|
|
case BLOCK_TYPE_SHB:
|
|
|
|
/* This isn't pcap, it's pcapng. We don't yet support
|
|
|
|
reading it. */
|
|
|
|
g_snprintf(errmsg, errmsgl, "Capturing from a pipe doesn't support pcapng format.");
|
|
|
|
goto error;
|
2011-03-21 16:57:11 +00:00
|
|
|
default:
|
2016-07-09 07:07:52 +00:00
|
|
|
/* Not a pcap type we know about, or not pcap at all. */
|
|
|
|
g_snprintf(errmsg, errmsgl, "Unrecognized libpcap format or not libpcap data.");
|
2011-03-21 16:57:11 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2013-03-19 05:09:37 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (pcap_opts->from_cap_socket)
|
2012-06-08 11:54:26 +00:00
|
|
|
#endif
|
2013-03-19 05:09:37 +00:00
|
|
|
{
|
2012-12-23 09:54:05 +00:00
|
|
|
/* Read the rest of the header */
|
|
|
|
bytes_read = 0;
|
|
|
|
while (bytes_read < sizeof(struct pcap_hdr)) {
|
|
|
|
sel_ret = cap_pipe_select(fd);
|
|
|
|
if (sel_ret < 0) {
|
|
|
|
g_snprintf(errmsg, errmsgl,
|
2016-07-09 07:07:52 +00:00
|
|
|
"Unexpected error from select: %s.", g_strerror(errno));
|
2012-12-23 09:54:05 +00:00
|
|
|
goto error;
|
|
|
|
} else if (sel_ret > 0) {
|
|
|
|
b = cap_pipe_read(fd, ((char *)hdr)+bytes_read,
|
2012-12-26 05:57:06 +00:00
|
|
|
sizeof(struct pcap_hdr) - bytes_read,
|
|
|
|
pcap_opts->from_cap_socket);
|
2012-12-23 09:54:05 +00:00
|
|
|
if (b <= 0) {
|
|
|
|
if (b == 0)
|
2016-07-09 07:07:52 +00:00
|
|
|
g_snprintf(errmsg, errmsgl, "End of file on pipe header during open.");
|
2012-12-23 09:54:05 +00:00
|
|
|
else
|
2016-07-09 07:07:52 +00:00
|
|
|
g_snprintf(errmsg, errmsgl, "Error on pipe header during open: %s.",
|
2012-12-23 09:54:05 +00:00
|
|
|
g_strerror(errno));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
bytes_read += b;
|
|
|
|
}
|
|
|
|
}
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
2012-06-08 11:54:26 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
else {
|
2012-12-23 09:54:05 +00:00
|
|
|
pcap_opts->cap_pipe_buf = (char *) hdr;
|
|
|
|
pcap_opts->cap_pipe_bytes_read = 0;
|
|
|
|
pcap_opts->cap_pipe_bytes_to_read = sizeof(struct pcap_hdr);
|
|
|
|
g_async_queue_push(pcap_opts->cap_pipe_pending_q, pcap_opts->cap_pipe_buf);
|
|
|
|
g_async_queue_pop(pcap_opts->cap_pipe_done_q);
|
|
|
|
if (pcap_opts->cap_pipe_bytes_read <= 0) {
|
|
|
|
if (pcap_opts->cap_pipe_bytes_read == 0)
|
2016-07-09 07:07:52 +00:00
|
|
|
g_snprintf(errmsg, errmsgl, "End of file on pipe header during open.");
|
2012-12-23 09:54:05 +00:00
|
|
|
else
|
2016-07-09 07:07:52 +00:00
|
|
|
g_snprintf(errmsg, errmsgl, "Error on pipe header header during open: %s.",
|
2012-12-23 09:54:05 +00:00
|
|
|
g_strerror(errno));
|
|
|
|
goto error;
|
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
}
|
2011-07-16 12:45:47 +00:00
|
|
|
#endif
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-05-15 22:54:52 +00:00
|
|
|
if (pcap_opts->cap_pipe_byte_swapped) {
|
2011-03-21 16:57:11 +00:00
|
|
|
/* Byte-swap the header fields about which we care. */
|
2013-11-29 19:21:20 +00:00
|
|
|
hdr->version_major = GUINT16_SWAP_LE_BE(hdr->version_major);
|
|
|
|
hdr->version_minor = GUINT16_SWAP_LE_BE(hdr->version_minor);
|
|
|
|
hdr->snaplen = GUINT32_SWAP_LE_BE(hdr->snaplen);
|
|
|
|
hdr->network = GUINT32_SWAP_LE_BE(hdr->network);
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->linktype = hdr->network;
|
2011-03-21 16:57:11 +00:00
|
|
|
|
|
|
|
if (hdr->version_major < 2) {
|
|
|
|
g_snprintf(errmsg, errmsgl, "Unable to read old libpcap format");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->cap_pipe_state = STATE_EXPECT_REC_HDR;
|
|
|
|
pcap_opts->cap_pipe_err = PIPOK;
|
|
|
|
pcap_opts->cap_pipe_fd = fd;
|
2011-03-21 16:57:11 +00:00
|
|
|
return;
|
2008-02-16 02:39:58 +00:00
|
|
|
|
|
|
|
error:
|
2011-03-21 16:57:11 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "cap_pipe_open_live: error %s", errmsg);
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->cap_pipe_err = PIPERR;
|
2012-06-08 11:54:26 +00:00
|
|
|
cap_pipe_close(fd, pcap_opts->from_cap_socket);
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->cap_pipe_fd = -1;
|
2017-01-30 20:52:35 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
pcap_opts->cap_pipe_h = INVALID_HANDLE_VALUE;
|
|
|
|
#endif
|
2008-02-16 02:39:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* We read one record from the pipe, take care of byte order in the record
|
|
|
|
* header, write the record to the capture file, and update capture statistics. */
|
|
|
|
static int
|
2011-05-15 22:54:52 +00:00
|
|
|
cap_pipe_dispatch(loop_data *ld, pcap_options *pcap_opts, guchar *data, char *errmsg, int errmsgl)
|
2008-02-16 02:39:58 +00:00
|
|
|
{
|
2012-11-25 18:35:41 +00:00
|
|
|
struct pcap_pkthdr phdr;
|
2011-03-21 16:57:11 +00:00
|
|
|
enum { PD_REC_HDR_READ, PD_DATA_READ, PD_PIPE_EOF, PD_PIPE_ERR,
|
|
|
|
PD_ERR } result;
|
2011-11-07 23:42:53 +00:00
|
|
|
#ifdef _WIN32
|
2012-02-29 18:04:23 +00:00
|
|
|
#if !GLIB_CHECK_VERSION(2,31,18)
|
2012-11-25 18:35:41 +00:00
|
|
|
GTimeVal wait_time;
|
2012-02-29 18:04:23 +00:00
|
|
|
#endif
|
2012-11-25 18:35:41 +00:00
|
|
|
gpointer q_status;
|
|
|
|
wchar_t *err_str;
|
2009-08-26 23:16:37 +00:00
|
|
|
#endif
|
2012-12-26 05:57:06 +00:00
|
|
|
ssize_t b;
|
2008-02-16 02:39:58 +00:00
|
|
|
|
|
|
|
#ifdef LOG_CAPTURE_VERBOSE
|
2011-03-21 16:57:11 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "cap_pipe_dispatch");
|
2008-02-16 02:39:58 +00:00
|
|
|
#endif
|
|
|
|
|
2011-05-15 22:54:52 +00:00
|
|
|
switch (pcap_opts->cap_pipe_state) {
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
case STATE_EXPECT_REC_HDR:
|
2011-11-07 23:42:53 +00:00
|
|
|
#ifdef _WIN32
|
2011-05-15 22:54:52 +00:00
|
|
|
if (g_mutex_trylock(pcap_opts->cap_pipe_read_mtx)) {
|
2009-08-26 23:16:37 +00:00
|
|
|
#endif
|
|
|
|
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->cap_pipe_state = STATE_READ_REC_HDR;
|
|
|
|
pcap_opts->cap_pipe_bytes_to_read = pcap_opts->cap_pipe_modified ?
|
2011-03-21 16:57:11 +00:00
|
|
|
sizeof(struct pcaprec_modified_hdr) : sizeof(struct pcaprec_hdr);
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->cap_pipe_bytes_read = 0;
|
2009-08-26 23:16:37 +00:00
|
|
|
|
2011-11-07 23:42:53 +00:00
|
|
|
#ifdef _WIN32
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->cap_pipe_buf = (char *) &pcap_opts->cap_pipe_rechdr;
|
|
|
|
g_async_queue_push(pcap_opts->cap_pipe_pending_q, pcap_opts->cap_pipe_buf);
|
|
|
|
g_mutex_unlock(pcap_opts->cap_pipe_read_mtx);
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
2009-08-26 23:16:37 +00:00
|
|
|
#endif
|
2011-03-21 16:57:11 +00:00
|
|
|
/* Fall through */
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
case STATE_READ_REC_HDR:
|
2013-03-19 05:09:37 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (pcap_opts->from_cap_socket)
|
2012-06-08 11:54:26 +00:00
|
|
|
#endif
|
2013-03-19 05:09:37 +00:00
|
|
|
{
|
2012-12-23 06:52:50 +00:00
|
|
|
b = cap_pipe_read(pcap_opts->cap_pipe_fd, ((char *)&pcap_opts->cap_pipe_rechdr)+pcap_opts->cap_pipe_bytes_read,
|
2012-06-08 11:54:26 +00:00
|
|
|
pcap_opts->cap_pipe_bytes_to_read - pcap_opts->cap_pipe_bytes_read, pcap_opts->from_cap_socket);
|
2012-12-23 06:52:50 +00:00
|
|
|
if (b <= 0) {
|
|
|
|
if (b == 0)
|
|
|
|
result = PD_PIPE_EOF;
|
|
|
|
else
|
|
|
|
result = PD_PIPE_ERR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pcap_opts->cap_pipe_bytes_read += b;
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
2012-06-08 11:54:26 +00:00
|
|
|
#ifdef _WIN32
|
2012-12-23 06:52:50 +00:00
|
|
|
else {
|
2012-02-29 18:04:23 +00:00
|
|
|
#if GLIB_CHECK_VERSION(2,31,18)
|
2012-12-23 06:52:50 +00:00
|
|
|
q_status = g_async_queue_timeout_pop(pcap_opts->cap_pipe_done_q, PIPE_READ_TIMEOUT);
|
2011-07-16 12:45:47 +00:00
|
|
|
#else
|
2012-12-23 06:52:50 +00:00
|
|
|
g_get_current_time(&wait_time);
|
|
|
|
g_time_val_add(&wait_time, PIPE_READ_TIMEOUT);
|
|
|
|
q_status = g_async_queue_timed_pop(pcap_opts->cap_pipe_done_q, &wait_time);
|
2012-02-29 18:04:23 +00:00
|
|
|
#endif
|
2012-12-23 06:52:50 +00:00
|
|
|
if (pcap_opts->cap_pipe_err == PIPEOF) {
|
|
|
|
result = PD_PIPE_EOF;
|
|
|
|
break;
|
|
|
|
} else if (pcap_opts->cap_pipe_err == PIPERR) {
|
|
|
|
result = PD_PIPE_ERR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!q_status) {
|
|
|
|
return 0;
|
|
|
|
}
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
2011-07-16 12:45:47 +00:00
|
|
|
#endif
|
2012-12-26 05:57:06 +00:00
|
|
|
if (pcap_opts->cap_pipe_bytes_read < pcap_opts->cap_pipe_bytes_to_read)
|
2011-03-21 16:57:11 +00:00
|
|
|
return 0;
|
|
|
|
result = PD_REC_HDR_READ;
|
|
|
|
break;
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
case STATE_EXPECT_DATA:
|
2011-11-07 23:42:53 +00:00
|
|
|
#ifdef _WIN32
|
2011-05-15 22:54:52 +00:00
|
|
|
if (g_mutex_trylock(pcap_opts->cap_pipe_read_mtx)) {
|
2009-08-26 23:16:37 +00:00
|
|
|
#endif
|
|
|
|
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->cap_pipe_state = STATE_READ_DATA;
|
|
|
|
pcap_opts->cap_pipe_bytes_to_read = pcap_opts->cap_pipe_rechdr.hdr.incl_len;
|
|
|
|
pcap_opts->cap_pipe_bytes_read = 0;
|
2009-08-26 23:16:37 +00:00
|
|
|
|
2011-11-07 23:42:53 +00:00
|
|
|
#ifdef _WIN32
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->cap_pipe_buf = (char *) data;
|
|
|
|
g_async_queue_push(pcap_opts->cap_pipe_pending_q, pcap_opts->cap_pipe_buf);
|
|
|
|
g_mutex_unlock(pcap_opts->cap_pipe_read_mtx);
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
2009-08-26 23:16:37 +00:00
|
|
|
#endif
|
2011-03-21 16:57:11 +00:00
|
|
|
/* Fall through */
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
case STATE_READ_DATA:
|
2013-03-19 05:09:37 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (pcap_opts->from_cap_socket)
|
2012-06-08 11:54:26 +00:00
|
|
|
#endif
|
2013-03-19 05:09:37 +00:00
|
|
|
{
|
2012-12-26 05:57:06 +00:00
|
|
|
b = cap_pipe_read(pcap_opts->cap_pipe_fd,
|
|
|
|
data+pcap_opts->cap_pipe_bytes_read,
|
|
|
|
pcap_opts->cap_pipe_bytes_to_read - pcap_opts->cap_pipe_bytes_read,
|
|
|
|
pcap_opts->from_cap_socket);
|
2012-12-23 06:52:50 +00:00
|
|
|
if (b <= 0) {
|
|
|
|
if (b == 0)
|
|
|
|
result = PD_PIPE_EOF;
|
|
|
|
else
|
|
|
|
result = PD_PIPE_ERR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pcap_opts->cap_pipe_bytes_read += b;
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
2012-06-08 11:54:26 +00:00
|
|
|
#ifdef _WIN32
|
2012-12-23 06:52:50 +00:00
|
|
|
else {
|
2012-06-08 11:54:26 +00:00
|
|
|
|
2012-02-29 18:04:23 +00:00
|
|
|
#if GLIB_CHECK_VERSION(2,31,18)
|
2012-12-23 06:52:50 +00:00
|
|
|
q_status = g_async_queue_timeout_pop(pcap_opts->cap_pipe_done_q, PIPE_READ_TIMEOUT);
|
2011-07-16 12:45:47 +00:00
|
|
|
#else
|
2012-12-23 06:52:50 +00:00
|
|
|
g_get_current_time(&wait_time);
|
|
|
|
g_time_val_add(&wait_time, PIPE_READ_TIMEOUT);
|
|
|
|
q_status = g_async_queue_timed_pop(pcap_opts->cap_pipe_done_q, &wait_time);
|
2013-03-19 05:09:37 +00:00
|
|
|
#endif /* GLIB_CHECK_VERSION(2,31,18) */
|
2012-12-23 06:52:50 +00:00
|
|
|
if (pcap_opts->cap_pipe_err == PIPEOF) {
|
|
|
|
result = PD_PIPE_EOF;
|
|
|
|
break;
|
|
|
|
} else if (pcap_opts->cap_pipe_err == PIPERR) {
|
|
|
|
result = PD_PIPE_ERR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!q_status) {
|
|
|
|
return 0;
|
|
|
|
}
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
2013-03-19 05:09:37 +00:00
|
|
|
#endif /* _WIN32 */
|
2012-12-26 05:57:06 +00:00
|
|
|
if (pcap_opts->cap_pipe_bytes_read < pcap_opts->cap_pipe_bytes_to_read)
|
2011-03-21 16:57:11 +00:00
|
|
|
return 0;
|
|
|
|
result = PD_DATA_READ;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
g_snprintf(errmsg, errmsgl, "cap_pipe_dispatch: invalid state");
|
|
|
|
result = PD_ERR;
|
|
|
|
|
2012-03-05 06:43:44 +00:00
|
|
|
} /* switch (pcap_opts->cap_pipe_state) */
|
2011-03-21 16:57:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We've now read as much data as we were expecting, so process it.
|
|
|
|
*/
|
|
|
|
switch (result) {
|
|
|
|
|
|
|
|
case PD_REC_HDR_READ:
|
|
|
|
/* We've read the header. Take care of byte order. */
|
2011-05-15 22:54:52 +00:00
|
|
|
cap_pipe_adjust_header(pcap_opts->cap_pipe_byte_swapped, &pcap_opts->cap_pipe_hdr,
|
|
|
|
&pcap_opts->cap_pipe_rechdr.hdr);
|
|
|
|
if (pcap_opts->cap_pipe_rechdr.hdr.incl_len > WTAP_MAX_PACKET_SIZE) {
|
2011-03-21 16:57:11 +00:00
|
|
|
g_snprintf(errmsg, errmsgl, "Frame %u too long (%d bytes)",
|
2011-05-15 22:54:52 +00:00
|
|
|
ld->packet_count+1, pcap_opts->cap_pipe_rechdr.hdr.incl_len);
|
2011-03-21 16:57:11 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-07-06 16:22:11 +00:00
|
|
|
|
|
|
|
if (pcap_opts->cap_pipe_rechdr.hdr.incl_len) {
|
|
|
|
pcap_opts->cap_pipe_state = STATE_EXPECT_DATA;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* no data to read? fall through */
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
case PD_DATA_READ:
|
|
|
|
/* Fill in a "struct pcap_pkthdr", and process the packet. */
|
2011-05-15 22:54:52 +00:00
|
|
|
phdr.ts.tv_sec = pcap_opts->cap_pipe_rechdr.hdr.ts_sec;
|
|
|
|
phdr.ts.tv_usec = pcap_opts->cap_pipe_rechdr.hdr.ts_usec;
|
|
|
|
phdr.caplen = pcap_opts->cap_pipe_rechdr.hdr.incl_len;
|
|
|
|
phdr.len = pcap_opts->cap_pipe_rechdr.hdr.orig_len;
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-05-16 21:56:12 +00:00
|
|
|
if (use_threads) {
|
2011-05-26 19:22:16 +00:00
|
|
|
capture_loop_queue_packet_cb((u_char *)pcap_opts, &phdr, data);
|
2011-05-16 21:56:12 +00:00
|
|
|
} else {
|
2011-05-26 19:22:16 +00:00
|
|
|
capture_loop_write_packet_cb((u_char *)pcap_opts, &phdr, data);
|
2011-05-16 21:56:12 +00:00
|
|
|
}
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->cap_pipe_state = STATE_EXPECT_REC_HDR;
|
2011-03-21 16:57:11 +00:00
|
|
|
return 1;
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
case PD_PIPE_EOF:
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->cap_pipe_err = PIPEOF;
|
2011-03-21 16:57:11 +00:00
|
|
|
return -1;
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
case PD_PIPE_ERR:
|
2009-08-26 23:16:37 +00:00
|
|
|
#ifdef _WIN32
|
2012-06-09 03:36:15 +00:00
|
|
|
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS,
|
2011-03-21 16:57:11 +00:00
|
|
|
NULL, GetLastError(), 0, (LPTSTR) &err_str, 0, NULL);
|
|
|
|
g_snprintf(errmsg, errmsgl,
|
|
|
|
"Error reading from pipe: %s (error %d)",
|
|
|
|
utf_16to8(err_str), GetLastError());
|
|
|
|
LocalFree(err_str);
|
2009-08-26 23:16:37 +00:00
|
|
|
#else
|
2011-03-21 16:57:11 +00:00
|
|
|
g_snprintf(errmsg, errmsgl, "Error reading from pipe: %s",
|
2011-06-28 09:00:11 +00:00
|
|
|
g_strerror(errno));
|
2009-08-26 23:16:37 +00:00
|
|
|
#endif
|
2011-03-21 16:57:11 +00:00
|
|
|
/* Fall through */
|
|
|
|
case PD_ERR:
|
|
|
|
break;
|
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->cap_pipe_err = PIPERR;
|
2011-03-21 16:57:11 +00:00
|
|
|
/* Return here rather than inside the switch to prevent GCC warning */
|
|
|
|
return -1;
|
2008-02-16 02:39:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Open the capture input file (pcap or capture pipe).
|
|
|
|
* Returns TRUE if it succeeds, FALSE otherwise. */
|
|
|
|
static gboolean
|
|
|
|
capture_loop_open_input(capture_options *capture_opts, loop_data *ld,
|
|
|
|
char *errmsg, size_t errmsg_len,
|
|
|
|
char *secondary_errmsg, size_t secondary_errmsg_len)
|
|
|
|
{
|
2011-05-15 22:54:52 +00:00
|
|
|
gchar open_err_str[PCAP_ERRBUF_SIZE];
|
|
|
|
gchar *sync_msg_str;
|
2011-05-13 11:28:51 +00:00
|
|
|
interface_options interface_opts;
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_options *pcap_opts;
|
2011-05-15 22:54:52 +00:00
|
|
|
guint i;
|
2008-02-16 02:39:58 +00:00
|
|
|
#ifdef _WIN32
|
2011-03-21 16:57:11 +00:00
|
|
|
int err;
|
|
|
|
WORD wVersionRequested;
|
|
|
|
WSADATA wsaData;
|
2008-02-16 02:39:58 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* XXX - opening Winsock on tshark? */
|
|
|
|
|
2015-09-02 21:35:37 +00:00
|
|
|
/* Initialize Windows Socket if we are in a Win32 OS
|
2011-03-21 16:57:11 +00:00
|
|
|
This needs to be done before querying the interface for network/netmask */
|
2008-02-16 02:39:58 +00:00
|
|
|
#ifdef _WIN32
|
2011-03-21 16:57:11 +00:00
|
|
|
/* XXX - do we really require 1.1 or earlier?
|
|
|
|
Are there any versions that support only 2.0 or higher? */
|
|
|
|
wVersionRequested = MAKEWORD(1, 1);
|
|
|
|
err = WSAStartup(wVersionRequested, &wsaData);
|
|
|
|
if (err != 0) {
|
|
|
|
switch (err) {
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
case WSASYSNOTREADY:
|
|
|
|
g_snprintf(errmsg, (gulong) errmsg_len,
|
|
|
|
"Couldn't initialize Windows Sockets: Network system not ready for network communication");
|
|
|
|
break;
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
case WSAVERNOTSUPPORTED:
|
|
|
|
g_snprintf(errmsg, (gulong) errmsg_len,
|
|
|
|
"Couldn't initialize Windows Sockets: Windows Sockets version %u.%u not supported",
|
|
|
|
LOBYTE(wVersionRequested), HIBYTE(wVersionRequested));
|
|
|
|
break;
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
case WSAEINPROGRESS:
|
|
|
|
g_snprintf(errmsg, (gulong) errmsg_len,
|
|
|
|
"Couldn't initialize Windows Sockets: Blocking operation is in progress");
|
|
|
|
break;
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
case WSAEPROCLIM:
|
|
|
|
g_snprintf(errmsg, (gulong) errmsg_len,
|
|
|
|
"Couldn't initialize Windows Sockets: Limit on the number of tasks supported by this WinSock implementation has been reached");
|
|
|
|
break;
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
case WSAEFAULT:
|
|
|
|
g_snprintf(errmsg, (gulong) errmsg_len,
|
|
|
|
"Couldn't initialize Windows Sockets: Bad pointer passed to WSAStartup");
|
|
|
|
break;
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
default:
|
|
|
|
g_snprintf(errmsg, (gulong) errmsg_len,
|
|
|
|
"Couldn't initialize Windows Sockets: error %d", err);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
g_snprintf(secondary_errmsg, (gulong) secondary_errmsg_len, please_report);
|
|
|
|
return FALSE;
|
2008-02-16 02:39:58 +00:00
|
|
|
}
|
|
|
|
#endif
|
2011-05-16 21:56:12 +00:00
|
|
|
if ((use_threads == FALSE) &&
|
|
|
|
(capture_opts->ifaces->len > 1)) {
|
2011-05-17 00:08:47 +00:00
|
|
|
g_snprintf(errmsg, (gulong) errmsg_len,
|
2014-09-24 21:06:23 +00:00
|
|
|
"Using threads is required for capturing on multiple interfaces.");
|
2011-05-16 21:56:12 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-05-15 22:54:52 +00:00
|
|
|
for (i = 0; i < capture_opts->ifaces->len; i++) {
|
|
|
|
interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts = (pcap_options *)g_malloc(sizeof (pcap_options));
|
|
|
|
if (pcap_opts == NULL) {
|
|
|
|
g_snprintf(errmsg, (gulong) errmsg_len,
|
|
|
|
"Could not allocate memory.");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-05-26 19:22:16 +00:00
|
|
|
pcap_opts->received = 0;
|
|
|
|
pcap_opts->dropped = 0;
|
2013-03-10 11:57:40 +00:00
|
|
|
pcap_opts->flushed = 0;
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts->pcap_h = NULL;
|
2011-05-15 22:54:52 +00:00
|
|
|
#ifdef MUST_DO_SELECT
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts->pcap_fd = -1;
|
|
|
|
#endif
|
|
|
|
pcap_opts->pcap_err = FALSE;
|
|
|
|
pcap_opts->interface_id = i;
|
|
|
|
pcap_opts->tid = NULL;
|
|
|
|
pcap_opts->snaplen = 0;
|
|
|
|
pcap_opts->linktype = -1;
|
2012-03-10 00:24:42 +00:00
|
|
|
pcap_opts->ts_nsec = FALSE;
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts->from_cap_pipe = FALSE;
|
2012-06-08 11:54:26 +00:00
|
|
|
pcap_opts->from_cap_socket = FALSE;
|
2011-05-26 18:12:41 +00:00
|
|
|
memset(&pcap_opts->cap_pipe_hdr, 0, sizeof(struct pcap_hdr));
|
|
|
|
memset(&pcap_opts->cap_pipe_rechdr, 0, sizeof(struct pcaprec_modified_hdr));
|
2008-02-16 02:39:58 +00:00
|
|
|
#ifdef _WIN32
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts->cap_pipe_h = INVALID_HANDLE_VALUE;
|
2011-05-15 22:54:52 +00:00
|
|
|
#endif
|
2012-06-08 11:54:26 +00:00
|
|
|
pcap_opts->cap_pipe_fd = -1;
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts->cap_pipe_modified = FALSE;
|
|
|
|
pcap_opts->cap_pipe_byte_swapped = FALSE;
|
2011-11-07 23:42:53 +00:00
|
|
|
#ifdef _WIN32
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts->cap_pipe_buf = NULL;
|
2011-07-16 12:45:47 +00:00
|
|
|
#endif
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts->cap_pipe_bytes_to_read = 0;
|
|
|
|
pcap_opts->cap_pipe_bytes_read = 0;
|
2013-03-03 17:14:19 +00:00
|
|
|
pcap_opts->cap_pipe_state = STATE_EXPECT_REC_HDR;
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts->cap_pipe_err = PIPOK;
|
2011-11-07 23:42:53 +00:00
|
|
|
#ifdef _WIN32
|
2011-11-09 23:43:50 +00:00
|
|
|
#if GLIB_CHECK_VERSION(2,31,0)
|
|
|
|
pcap_opts->cap_pipe_read_mtx = g_malloc(sizeof(GMutex));
|
|
|
|
g_mutex_init(pcap_opts->cap_pipe_read_mtx);
|
|
|
|
#else
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts->cap_pipe_read_mtx = g_mutex_new();
|
2011-11-09 23:43:50 +00:00
|
|
|
#endif
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts->cap_pipe_pending_q = g_async_queue_new();
|
|
|
|
pcap_opts->cap_pipe_done_q = g_async_queue_new();
|
2008-02-16 02:39:58 +00:00
|
|
|
#endif
|
2011-05-26 18:12:41 +00:00
|
|
|
g_array_append_val(ld->pcaps, pcap_opts);
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-05-15 22:54:52 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_open_input : %s", interface_opts.name);
|
2016-01-05 21:54:02 +00:00
|
|
|
pcap_opts->pcap_h = open_capture_device(capture_opts, &interface_opts,
|
|
|
|
CAP_READ_TIMEOUT, &open_err_str);
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-05-26 18:12:41 +00:00
|
|
|
if (pcap_opts->pcap_h != NULL) {
|
2011-05-15 22:54:52 +00:00
|
|
|
/* we've opened "iface" as a network device */
|
2014-09-28 23:28:36 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
|
|
|
|
/* Find out if we're getting nanosecond-precision time stamps */
|
2014-11-05 02:08:47 +00:00
|
|
|
pcap_opts->ts_nsec = have_high_resolution_timestamp(pcap_opts->pcap_h);
|
2014-09-28 23:28:36 +00:00
|
|
|
#endif
|
|
|
|
|
2011-05-16 14:12:35 +00:00
|
|
|
#if defined(HAVE_PCAP_SETSAMPLING)
|
|
|
|
if (interface_opts.sampling_method != CAPTURE_SAMP_NONE) {
|
2011-05-15 22:54:52 +00:00
|
|
|
struct pcap_samp *samp;
|
|
|
|
|
2011-05-26 18:12:41 +00:00
|
|
|
if ((samp = pcap_setsampling(pcap_opts->pcap_h)) != NULL) {
|
2011-05-16 14:12:35 +00:00
|
|
|
switch (interface_opts.sampling_method) {
|
2011-05-15 22:54:52 +00:00
|
|
|
case CAPTURE_SAMP_BY_COUNT:
|
|
|
|
samp->method = PCAP_SAMP_1_EVERY_N;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CAPTURE_SAMP_BY_TIMER:
|
|
|
|
samp->method = PCAP_SAMP_FIRST_AFTER_N_MS;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
sync_msg_str = g_strdup_printf(
|
|
|
|
"Unknown sampling method %d specified,\n"
|
|
|
|
"continue without packet sampling",
|
2011-05-16 15:25:33 +00:00
|
|
|
interface_opts.sampling_method);
|
2011-05-15 22:54:52 +00:00
|
|
|
report_capture_error("Couldn't set the capture "
|
|
|
|
"sampling", sync_msg_str);
|
|
|
|
g_free(sync_msg_str);
|
|
|
|
}
|
2011-05-16 15:25:33 +00:00
|
|
|
samp->value = interface_opts.sampling_param;
|
2011-05-15 22:54:52 +00:00
|
|
|
} else {
|
|
|
|
report_capture_error("Couldn't set the capture sampling",
|
|
|
|
"Cannot get packet sampling data structure");
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
2011-05-15 22:54:52 +00:00
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
#endif
|
|
|
|
|
2011-05-15 22:54:52 +00:00
|
|
|
/* setting the data link type only works on real interfaces */
|
2016-01-05 21:54:02 +00:00
|
|
|
if (!set_pcap_datalink(pcap_opts->pcap_h, interface_opts.linktype,
|
|
|
|
interface_opts.name,
|
2011-05-15 22:54:52 +00:00
|
|
|
errmsg, errmsg_len,
|
|
|
|
secondary_errmsg, secondary_errmsg_len)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2016-01-05 21:54:02 +00:00
|
|
|
pcap_opts->linktype = get_pcap_datalink(pcap_opts->pcap_h, interface_opts.name);
|
2011-05-15 22:54:52 +00:00
|
|
|
} else {
|
|
|
|
/* We couldn't open "iface" as a network device. */
|
|
|
|
/* Try to open it as a pipe */
|
2011-05-26 18:12:41 +00:00
|
|
|
cap_pipe_open_live(interface_opts.name, pcap_opts, &pcap_opts->cap_pipe_hdr, errmsg, (int) errmsg_len);
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2009-08-26 23:16:37 +00:00
|
|
|
#ifndef _WIN32
|
2011-05-26 18:12:41 +00:00
|
|
|
if (pcap_opts->cap_pipe_fd == -1) {
|
2009-08-26 23:16:37 +00:00
|
|
|
#else
|
2011-05-26 18:12:41 +00:00
|
|
|
if (pcap_opts->cap_pipe_h == INVALID_HANDLE_VALUE) {
|
2009-08-26 23:16:37 +00:00
|
|
|
#endif
|
2011-05-26 18:12:41 +00:00
|
|
|
if (pcap_opts->cap_pipe_err == PIPNEXIST) {
|
2016-01-07 08:26:12 +00:00
|
|
|
/*
|
|
|
|
* We tried opening as an interface, and that failed,
|
|
|
|
* so we tried to open it as a pipe, but the pipe
|
|
|
|
* doesn't exist. Report the error message for
|
|
|
|
* the interface.
|
|
|
|
*/
|
2011-03-21 16:57:11 +00:00
|
|
|
get_capture_device_open_failure_messages(open_err_str,
|
2011-05-13 11:28:51 +00:00
|
|
|
interface_opts.name,
|
2011-03-21 16:57:11 +00:00
|
|
|
errmsg,
|
|
|
|
errmsg_len,
|
|
|
|
secondary_errmsg,
|
|
|
|
secondary_errmsg_len);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Else pipe (or file) does exist and cap_pipe_open_live() has
|
|
|
|
* filled in errmsg
|
|
|
|
*/
|
|
|
|
return FALSE;
|
2011-05-15 22:54:52 +00:00
|
|
|
} else {
|
2011-03-21 16:57:11 +00:00
|
|
|
/* cap_pipe_open_live() succeeded; don't want
|
|
|
|
error message from pcap_open_live() */
|
|
|
|
open_err_str[0] = '\0';
|
2011-05-15 22:54:52 +00:00
|
|
|
}
|
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
|
|
|
|
/* XXX - will this work for tshark? */
|
|
|
|
#ifdef MUST_DO_SELECT
|
2011-05-26 18:12:41 +00:00
|
|
|
if (!pcap_opts->from_cap_pipe) {
|
2008-02-16 02:39:58 +00:00
|
|
|
#ifdef HAVE_PCAP_GET_SELECTABLE_FD
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts->pcap_fd = pcap_get_selectable_fd(pcap_opts->pcap_h);
|
2008-02-16 02:39:58 +00:00
|
|
|
#else
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts->pcap_fd = pcap_fileno(pcap_opts->pcap_h);
|
2008-02-16 02:39:58 +00:00
|
|
|
#endif
|
2011-05-15 22:54:52 +00:00
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
#endif
|
|
|
|
|
2011-05-15 22:54:52 +00:00
|
|
|
/* Does "open_err_str" contain a non-empty string? If so, "pcap_open_live()"
|
|
|
|
returned a warning; print it, but keep capturing. */
|
|
|
|
if (open_err_str[0] != '\0') {
|
|
|
|
sync_msg_str = g_strdup_printf("%s.", open_err_str);
|
|
|
|
report_capture_error(sync_msg_str, "");
|
|
|
|
g_free(sync_msg_str);
|
|
|
|
}
|
|
|
|
capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, i);
|
|
|
|
g_array_insert_val(capture_opts->ifaces, i, interface_opts);
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-05-19 20:48:58 +00:00
|
|
|
/* If not using libcap: we now can now set euid/egid to ruid/rgid */
|
|
|
|
/* to remove any suid privileges. */
|
|
|
|
/* If using libcap: we can now remove NET_RAW and NET_ADMIN capabilities */
|
|
|
|
/* (euid/egid have already previously been set to ruid/rgid. */
|
|
|
|
/* (See comment in main() for details) */
|
|
|
|
#ifndef HAVE_LIBCAP
|
|
|
|
relinquish_special_privs_perm();
|
|
|
|
#else
|
|
|
|
relinquish_all_capabilities();
|
|
|
|
#endif
|
2011-03-21 16:57:11 +00:00
|
|
|
return TRUE;
|
2008-02-16 02:39:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* close the capture input file (pcap or capture pipe) */
|
2011-03-21 16:57:11 +00:00
|
|
|
static void capture_loop_close_input(loop_data *ld)
|
|
|
|
{
|
2012-11-25 18:35:41 +00:00
|
|
|
guint i;
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_options *pcap_opts;
|
2011-05-15 22:54:52 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_close_input");
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-05-15 22:54:52 +00:00
|
|
|
for (i = 0; i < ld->pcaps->len; i++) {
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts = g_array_index(ld->pcaps, pcap_options *, i);
|
2011-05-15 22:54:52 +00:00
|
|
|
/* if open, close the capture pipe "input file" */
|
2011-05-26 18:12:41 +00:00
|
|
|
if (pcap_opts->cap_pipe_fd >= 0) {
|
|
|
|
g_assert(pcap_opts->from_cap_pipe);
|
2012-06-08 11:54:26 +00:00
|
|
|
cap_pipe_close(pcap_opts->cap_pipe_fd, pcap_opts->from_cap_socket);
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts->cap_pipe_fd = -1;
|
2011-05-15 22:54:52 +00:00
|
|
|
}
|
2012-06-08 11:54:26 +00:00
|
|
|
#ifdef _WIN32
|
2011-05-26 18:12:41 +00:00
|
|
|
if (pcap_opts->cap_pipe_h != INVALID_HANDLE_VALUE) {
|
|
|
|
CloseHandle(pcap_opts->cap_pipe_h);
|
|
|
|
pcap_opts->cap_pipe_h = INVALID_HANDLE_VALUE;
|
2011-05-15 22:54:52 +00:00
|
|
|
}
|
2009-08-26 23:16:37 +00:00
|
|
|
#endif
|
2011-05-15 22:54:52 +00:00
|
|
|
/* if open, close the pcap "input file" */
|
2011-05-26 18:12:41 +00:00
|
|
|
if (pcap_opts->pcap_h != NULL) {
|
2011-06-15 20:21:45 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_close_input: closing %p", (void *)pcap_opts->pcap_h);
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_close(pcap_opts->pcap_h);
|
|
|
|
pcap_opts->pcap_h = NULL;
|
2011-05-15 22:54:52 +00:00
|
|
|
}
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
ld->go = FALSE;
|
2008-02-16 02:39:58 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2011-03-21 16:57:11 +00:00
|
|
|
/* Shut down windows sockets */
|
|
|
|
WSACleanup();
|
2008-02-16 02:39:58 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* init the capture filter */
|
|
|
|
static initfilter_status_t
|
2010-07-13 23:26:07 +00:00
|
|
|
capture_loop_init_filter(pcap_t *pcap_h, gboolean from_cap_pipe,
|
2011-06-15 20:21:45 +00:00
|
|
|
const gchar * name, const gchar * cfilter)
|
2010-07-13 23:26:07 +00:00
|
|
|
{
|
2011-03-21 16:57:11 +00:00
|
|
|
struct bpf_program fcode;
|
|
|
|
|
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_init_filter: %s", cfilter);
|
|
|
|
|
|
|
|
/* capture filters only work on real interfaces */
|
|
|
|
if (cfilter && !from_cap_pipe) {
|
|
|
|
/* A capture filter was specified; set it up. */
|
2011-05-13 11:28:51 +00:00
|
|
|
if (!compile_capture_filter(name, pcap_h, &fcode, cfilter)) {
|
2011-03-21 16:57:11 +00:00
|
|
|
/* Treat this specially - our caller might try to compile this
|
|
|
|
as a display filter and, if that succeeds, warn the user that
|
|
|
|
the display and capture filter syntaxes are different. */
|
|
|
|
return INITFILTER_BAD_FILTER;
|
|
|
|
}
|
|
|
|
if (pcap_setfilter(pcap_h, &fcode) < 0) {
|
2008-02-16 02:39:58 +00:00
|
|
|
#ifdef HAVE_PCAP_FREECODE
|
2011-03-21 16:57:11 +00:00
|
|
|
pcap_freecode(&fcode);
|
2008-02-16 02:39:58 +00:00
|
|
|
#endif
|
2011-03-21 16:57:11 +00:00
|
|
|
return INITFILTER_OTHER_ERROR;
|
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
#ifdef HAVE_PCAP_FREECODE
|
2011-03-21 16:57:11 +00:00
|
|
|
pcap_freecode(&fcode);
|
2008-02-16 02:39:58 +00:00
|
|
|
#endif
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
return INITFILTER_NO_ERROR;
|
2008-02-16 02:39:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* set up to write to the already-opened capture output file/files */
|
|
|
|
static gboolean
|
2011-03-21 16:57:11 +00:00
|
|
|
capture_loop_init_output(capture_options *capture_opts, loop_data *ld, char *errmsg, int errmsg_len)
|
|
|
|
{
|
2012-11-25 18:35:41 +00:00
|
|
|
int err;
|
|
|
|
guint i;
|
|
|
|
pcap_options *pcap_opts;
|
|
|
|
interface_options interface_opts;
|
|
|
|
gboolean successful;
|
2011-03-21 16:57:11 +00:00
|
|
|
|
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_init_output");
|
|
|
|
|
2011-05-15 22:54:52 +00:00
|
|
|
if ((capture_opts->use_pcapng == FALSE) &&
|
|
|
|
(capture_opts->ifaces->len > 1)) {
|
|
|
|
g_snprintf(errmsg, errmsg_len,
|
2014-09-24 21:06:23 +00:00
|
|
|
"Using PCAPNG is required for capturing on multiple interfaces. Use the -n option.");
|
2011-05-15 22:54:52 +00:00
|
|
|
return FALSE;
|
2009-04-27 08:11:10 +00:00
|
|
|
}
|
2011-03-21 16:57:11 +00:00
|
|
|
|
|
|
|
/* Set up to write to the capture file. */
|
|
|
|
if (capture_opts->multi_files_on) {
|
|
|
|
ld->pdh = ringbuf_init_libpcap_fdopen(&err);
|
|
|
|
} else {
|
2012-12-20 14:53:09 +00:00
|
|
|
ld->pdh = ws_fdopen(ld->save_file_fd, "wb");
|
|
|
|
if (ld->pdh == NULL) {
|
|
|
|
err = errno;
|
|
|
|
}
|
2009-04-26 15:51:25 +00:00
|
|
|
}
|
2011-03-21 16:57:11 +00:00
|
|
|
if (ld->pdh) {
|
|
|
|
if (capture_opts->use_pcapng) {
|
2015-01-03 22:00:47 +00:00
|
|
|
char *appname;
|
2016-09-22 11:23:37 +00:00
|
|
|
GString *cpu_info_str;
|
2015-01-03 22:00:47 +00:00
|
|
|
GString *os_info_str;
|
2012-02-14 17:07:52 +00:00
|
|
|
|
2016-09-22 11:23:37 +00:00
|
|
|
cpu_info_str = g_string_new("");
|
2012-02-20 21:17:43 +00:00
|
|
|
os_info_str = g_string_new("");
|
2016-09-22 11:23:37 +00:00
|
|
|
get_cpu_info(cpu_info_str);
|
2012-02-20 21:17:43 +00:00
|
|
|
get_os_version_info(os_info_str);
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2015-01-03 22:00:47 +00:00
|
|
|
appname = g_strdup_printf("Dumpcap (Wireshark) %s", get_ws_vcs_version_info());
|
2013-09-29 20:53:13 +00:00
|
|
|
successful = pcapng_write_session_header_block(ld->pdh,
|
2016-09-22 11:23:37 +00:00
|
|
|
(const char *)capture_opts->capture_comment, /* Comment */
|
|
|
|
cpu_info_str->str, /* HW */
|
|
|
|
os_info_str->str, /* OS */
|
2012-02-14 17:07:52 +00:00
|
|
|
appname,
|
2012-02-21 18:01:01 +00:00
|
|
|
-1, /* section_length */
|
2012-02-29 20:32:12 +00:00
|
|
|
&ld->bytes_written,
|
2012-02-14 17:07:52 +00:00
|
|
|
&err);
|
2015-01-03 22:00:47 +00:00
|
|
|
g_free(appname);
|
2012-02-14 17:07:52 +00:00
|
|
|
|
2011-05-15 22:54:52 +00:00
|
|
|
for (i = 0; successful && (i < capture_opts->ifaces->len); i++) {
|
|
|
|
interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts = g_array_index(ld->pcaps, pcap_options *, i);
|
|
|
|
if (pcap_opts->from_cap_pipe) {
|
|
|
|
pcap_opts->snaplen = pcap_opts->cap_pipe_hdr.snaplen;
|
2011-05-15 22:54:52 +00:00
|
|
|
} else {
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts->snaplen = pcap_snapshot(pcap_opts->pcap_h);
|
2011-05-15 22:54:52 +00:00
|
|
|
}
|
2013-09-29 20:53:13 +00:00
|
|
|
successful = pcapng_write_interface_description_block(global_ld.pdh,
|
|
|
|
NULL, /* OPT_COMMENT 1 */
|
|
|
|
interface_opts.name, /* IDB_NAME 2 */
|
|
|
|
interface_opts.descr, /* IDB_DESCRIPTION 3 */
|
|
|
|
interface_opts.cfilter, /* IDB_FILTER 11 */
|
|
|
|
os_info_str->str, /* IDB_OS 12 */
|
|
|
|
pcap_opts->linktype,
|
|
|
|
pcap_opts->snaplen,
|
|
|
|
&(global_ld.bytes_written),
|
|
|
|
0, /* IDB_IF_SPEED 8 */
|
|
|
|
pcap_opts->ts_nsec ? 9 : 6, /* IDB_TSRESOL 9 */
|
|
|
|
&global_ld.err);
|
2011-05-15 22:54:52 +00:00
|
|
|
}
|
2012-02-21 16:14:57 +00:00
|
|
|
|
|
|
|
g_string_free(os_info_str, TRUE);
|
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
} else {
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts = g_array_index(ld->pcaps, pcap_options *, 0);
|
|
|
|
if (pcap_opts->from_cap_pipe) {
|
|
|
|
pcap_opts->snaplen = pcap_opts->cap_pipe_hdr.snaplen;
|
2011-05-15 22:54:52 +00:00
|
|
|
} else {
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts->snaplen = pcap_snapshot(pcap_opts->pcap_h);
|
2011-05-15 22:54:52 +00:00
|
|
|
}
|
2013-09-29 20:26:25 +00:00
|
|
|
successful = libpcap_write_file_header(ld->pdh, pcap_opts->linktype, pcap_opts->snaplen,
|
2012-03-10 00:24:42 +00:00
|
|
|
pcap_opts->ts_nsec, &ld->bytes_written, &err);
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
|
|
|
if (!successful) {
|
|
|
|
fclose(ld->pdh);
|
|
|
|
ld->pdh = NULL;
|
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
}
|
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
if (ld->pdh == NULL) {
|
|
|
|
/* We couldn't set up to write to the capture file. */
|
|
|
|
/* XXX - use cf_open_error_message from tshark instead? */
|
|
|
|
switch (err) {
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (err < 0) {
|
|
|
|
g_snprintf(errmsg, errmsg_len,
|
|
|
|
"The file to which the capture would be"
|
|
|
|
" saved (\"%s\") could not be opened: Error %d.",
|
|
|
|
capture_opts->save_file, err);
|
|
|
|
} else {
|
|
|
|
g_snprintf(errmsg, errmsg_len,
|
|
|
|
"The file to which the capture would be"
|
|
|
|
" saved (\"%s\") could not be opened: %s.",
|
2011-06-28 09:00:11 +00:00
|
|
|
capture_opts->save_file, g_strerror(err));
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
return TRUE;
|
2008-02-16 02:39:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2011-03-21 16:57:11 +00:00
|
|
|
capture_loop_close_output(capture_options *capture_opts, loop_data *ld, int *err_close)
|
|
|
|
{
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2012-11-25 18:35:41 +00:00
|
|
|
unsigned int i;
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_options *pcap_opts;
|
2012-11-25 18:35:41 +00:00
|
|
|
guint64 end_time = create_timestamp();
|
2011-05-15 22:54:52 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_close_output");
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
if (capture_opts->multi_files_on) {
|
|
|
|
return ringbuf_libpcap_dump_close(&capture_opts->save_file, err_close);
|
|
|
|
} else {
|
|
|
|
if (capture_opts->use_pcapng) {
|
2011-05-15 22:54:52 +00:00
|
|
|
for (i = 0; i < global_ld.pcaps->len; i++) {
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts = g_array_index(global_ld.pcaps, pcap_options *, i);
|
|
|
|
if (!pcap_opts->from_cap_pipe) {
|
2012-04-25 20:50:38 +00:00
|
|
|
guint64 isb_ifrecv, isb_ifdrop;
|
|
|
|
struct pcap_stat stats;
|
|
|
|
|
|
|
|
if (pcap_stats(pcap_opts->pcap_h, &stats) >= 0) {
|
2012-04-25 21:09:33 +00:00
|
|
|
isb_ifrecv = pcap_opts->received;
|
2013-03-10 11:57:40 +00:00
|
|
|
isb_ifdrop = stats.ps_drop + pcap_opts->dropped + pcap_opts->flushed;
|
2012-04-25 20:50:38 +00:00
|
|
|
} else {
|
|
|
|
isb_ifrecv = G_MAXUINT64;
|
|
|
|
isb_ifdrop = G_MAXUINT64;
|
|
|
|
}
|
2013-09-29 20:53:13 +00:00
|
|
|
pcapng_write_interface_statistics_block(ld->pdh,
|
|
|
|
i,
|
|
|
|
&ld->bytes_written,
|
|
|
|
"Counters provided by dumpcap",
|
|
|
|
start_time,
|
|
|
|
end_time,
|
|
|
|
isb_ifrecv,
|
|
|
|
isb_ifdrop,
|
|
|
|
err_close);
|
2011-05-15 22:54:52 +00:00
|
|
|
}
|
|
|
|
}
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
2012-12-20 20:00:06 +00:00
|
|
|
if (fclose(ld->pdh) == EOF) {
|
2012-12-20 14:53:09 +00:00
|
|
|
if (err_close != NULL) {
|
|
|
|
*err_close = errno;
|
|
|
|
}
|
|
|
|
return (FALSE);
|
|
|
|
} else {
|
|
|
|
return (TRUE);
|
|
|
|
}
|
2009-04-27 08:11:10 +00:00
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* dispatch incoming packets (pcap or capture pipe)
|
|
|
|
*
|
|
|
|
* Waits for incoming packets to be available, and calls pcap_dispatch()
|
|
|
|
* to cause them to be processed.
|
|
|
|
*
|
|
|
|
* Returns the number of packets which were processed.
|
|
|
|
*
|
|
|
|
* Times out (returning zero) after CAP_READ_TIMEOUT ms; this ensures that the
|
|
|
|
* packet-batching behaviour does not cause packets to get held back
|
|
|
|
* indefinitely.
|
|
|
|
*/
|
|
|
|
static int
|
2011-05-16 21:56:12 +00:00
|
|
|
capture_loop_dispatch(loop_data *ld,
|
2011-05-15 22:54:52 +00:00
|
|
|
char *errmsg, int errmsg_len, pcap_options *pcap_opts)
|
2008-02-16 02:39:58 +00:00
|
|
|
{
|
2012-11-25 18:35:41 +00:00
|
|
|
int inpkts;
|
|
|
|
gint packet_count_before;
|
|
|
|
guchar pcap_data[WTAP_MAX_PACKET_SIZE];
|
2011-11-07 23:42:53 +00:00
|
|
|
#ifndef _WIN32
|
2012-11-25 18:35:41 +00:00
|
|
|
int sel_ret;
|
2009-08-26 23:16:37 +00:00
|
|
|
#endif
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
packet_count_before = ld->packet_count;
|
2011-05-15 22:54:52 +00:00
|
|
|
if (pcap_opts->from_cap_pipe) {
|
2011-03-21 16:57:11 +00:00
|
|
|
/* dispatch from capture pipe */
|
2008-02-16 02:39:58 +00:00
|
|
|
#ifdef LOG_CAPTURE_VERBOSE
|
2011-03-21 16:57:11 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_dispatch: from capture pipe");
|
2008-02-16 02:39:58 +00:00
|
|
|
#endif
|
2011-11-07 23:42:53 +00:00
|
|
|
#ifndef _WIN32
|
2011-05-15 22:54:52 +00:00
|
|
|
sel_ret = cap_pipe_select(pcap_opts->cap_pipe_fd);
|
2011-03-21 16:57:11 +00:00
|
|
|
if (sel_ret <= 0) {
|
|
|
|
if (sel_ret < 0 && errno != EINTR) {
|
|
|
|
g_snprintf(errmsg, errmsg_len,
|
2011-06-28 09:00:11 +00:00
|
|
|
"Unexpected error from select: %s", g_strerror(errno));
|
2011-03-21 16:57:11 +00:00
|
|
|
report_capture_error(errmsg, please_report);
|
|
|
|
ld->go = FALSE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* "select()" says we can read from the pipe without blocking
|
|
|
|
*/
|
2011-07-16 12:45:47 +00:00
|
|
|
#endif
|
2011-05-15 22:54:52 +00:00
|
|
|
inpkts = cap_pipe_dispatch(ld, pcap_opts, pcap_data, errmsg, errmsg_len);
|
2011-03-21 16:57:11 +00:00
|
|
|
if (inpkts < 0) {
|
|
|
|
ld->go = FALSE;
|
|
|
|
}
|
2011-11-07 23:42:53 +00:00
|
|
|
#ifndef _WIN32
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
2009-08-26 23:16:37 +00:00
|
|
|
#endif
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* dispatch from pcap */
|
2008-02-16 02:39:58 +00:00
|
|
|
#ifdef MUST_DO_SELECT
|
|
|
|
/*
|
2011-03-21 16:57:11 +00:00
|
|
|
* If we have "pcap_get_selectable_fd()", we use it to get the
|
|
|
|
* descriptor on which to select; if that's -1, it means there
|
|
|
|
* is no descriptor on which you can do a "select()" (perhaps
|
|
|
|
* because you're capturing on a special device, and that device's
|
|
|
|
* driver unfortunately doesn't support "select()", in which case
|
|
|
|
* we don't do the select - which means it might not be possible
|
|
|
|
* to stop a capture until a packet arrives. If that's unacceptable,
|
|
|
|
* plead with whoever supplies the software for that device to add
|
|
|
|
* "select()" support, or upgrade to libpcap 0.8.1 or later, and
|
|
|
|
* rebuild Wireshark or get a version built with libpcap 0.8.1 or
|
|
|
|
* later, so it can use pcap_breakloop().
|
2008-02-16 02:39:58 +00:00
|
|
|
*/
|
2011-03-21 16:57:11 +00:00
|
|
|
#ifdef LOG_CAPTURE_VERBOSE
|
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_dispatch: from pcap_dispatch with select");
|
|
|
|
#endif
|
2011-05-15 22:54:52 +00:00
|
|
|
if (pcap_opts->pcap_fd != -1) {
|
|
|
|
sel_ret = cap_pipe_select(pcap_opts->pcap_fd);
|
2011-03-21 16:57:11 +00:00
|
|
|
if (sel_ret > 0) {
|
|
|
|
/*
|
|
|
|
* "select()" says we can read from it without blocking; go for
|
|
|
|
* it.
|
|
|
|
*
|
|
|
|
* We don't have pcap_breakloop(), so we only process one packet
|
|
|
|
* per pcap_dispatch() call, to allow a signal to stop the
|
|
|
|
* processing immediately, rather than processing all packets
|
|
|
|
* in a batch before quitting.
|
|
|
|
*/
|
2011-05-21 00:10:57 +00:00
|
|
|
if (use_threads) {
|
2011-05-26 19:22:16 +00:00
|
|
|
inpkts = pcap_dispatch(pcap_opts->pcap_h, 1, capture_loop_queue_packet_cb, (u_char *)pcap_opts);
|
2011-05-16 21:56:12 +00:00
|
|
|
} else {
|
2011-05-26 19:22:16 +00:00
|
|
|
inpkts = pcap_dispatch(pcap_opts->pcap_h, 1, capture_loop_write_packet_cb, (u_char *)pcap_opts);
|
2011-05-16 21:56:12 +00:00
|
|
|
}
|
2011-03-21 16:57:11 +00:00
|
|
|
if (inpkts < 0) {
|
|
|
|
if (inpkts == -1) {
|
|
|
|
/* Error, rather than pcap_breakloop(). */
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->pcap_err = TRUE;
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
|
|
|
ld->go = FALSE; /* error or pcap_breakloop() - stop capturing */
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (sel_ret < 0 && errno != EINTR) {
|
|
|
|
g_snprintf(errmsg, errmsg_len,
|
2011-06-28 09:00:11 +00:00
|
|
|
"Unexpected error from select: %s", g_strerror(errno));
|
2011-03-21 16:57:11 +00:00
|
|
|
report_capture_error(errmsg, please_report);
|
|
|
|
ld->go = FALSE;
|
|
|
|
}
|
2009-08-22 22:49:59 +00:00
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
}
|
2011-03-21 16:57:11 +00:00
|
|
|
else
|
2008-02-16 02:39:58 +00:00
|
|
|
#endif /* MUST_DO_SELECT */
|
2011-03-21 16:57:11 +00:00
|
|
|
{
|
|
|
|
/* dispatch from pcap without select */
|
2008-02-16 02:39:58 +00:00
|
|
|
#if 1
|
|
|
|
#ifdef LOG_CAPTURE_VERBOSE
|
2011-03-21 16:57:11 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_dispatch: from pcap_dispatch");
|
2008-02-16 02:39:58 +00:00
|
|
|
#endif
|
|
|
|
#ifdef _WIN32
|
2011-03-21 16:57:11 +00:00
|
|
|
/*
|
|
|
|
* On Windows, we don't support asynchronously telling a process to
|
|
|
|
* stop capturing; instead, we check for an indication on a pipe
|
|
|
|
* after processing packets. We therefore process only one packet
|
|
|
|
* at a time, so that we can check the pipe after every packet.
|
|
|
|
*/
|
2011-05-21 00:10:57 +00:00
|
|
|
if (use_threads) {
|
2011-05-26 19:22:16 +00:00
|
|
|
inpkts = pcap_dispatch(pcap_opts->pcap_h, 1, capture_loop_queue_packet_cb, (u_char *)pcap_opts);
|
2011-05-16 21:56:12 +00:00
|
|
|
} else {
|
2011-05-26 19:22:16 +00:00
|
|
|
inpkts = pcap_dispatch(pcap_opts->pcap_h, 1, capture_loop_write_packet_cb, (u_char *)pcap_opts);
|
2011-05-16 21:56:12 +00:00
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
#else
|
2011-05-21 00:10:57 +00:00
|
|
|
if (use_threads) {
|
2011-05-26 19:22:16 +00:00
|
|
|
inpkts = pcap_dispatch(pcap_opts->pcap_h, -1, capture_loop_queue_packet_cb, (u_char *)pcap_opts);
|
2011-05-16 21:56:12 +00:00
|
|
|
} else {
|
2011-05-26 19:22:16 +00:00
|
|
|
inpkts = pcap_dispatch(pcap_opts->pcap_h, -1, capture_loop_write_packet_cb, (u_char *)pcap_opts);
|
2011-05-16 21:56:12 +00:00
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
#endif
|
2011-03-21 16:57:11 +00:00
|
|
|
if (inpkts < 0) {
|
|
|
|
if (inpkts == -1) {
|
|
|
|
/* Error, rather than pcap_breakloop(). */
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->pcap_err = TRUE;
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
|
|
|
ld->go = FALSE; /* error or pcap_breakloop() - stop capturing */
|
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
#else /* pcap_next_ex */
|
|
|
|
#ifdef LOG_CAPTURE_VERBOSE
|
2011-03-21 16:57:11 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_dispatch: from pcap_next_ex");
|
|
|
|
#endif
|
|
|
|
/* XXX - this is currently unused, as there is some confusion with pcap_next_ex() vs. pcap_dispatch() */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* WinPcap's remote capturing feature doesn't work with pcap_dispatch(),
|
2015-03-10 17:46:50 +00:00
|
|
|
* see https://wiki.wireshark.org/CaptureSetup_2fWinPcapRemote
|
2011-03-21 16:57:11 +00:00
|
|
|
* This should be fixed in the WinPcap 4.0 alpha release.
|
|
|
|
*
|
|
|
|
* For reference, an example remote interface:
|
|
|
|
* rpcap://[1.2.3.4]/\Device\NPF_{39993D68-7C9B-4439-A329-F2D888DA7C5C}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* emulate dispatch from pcap */
|
|
|
|
{
|
|
|
|
int in;
|
|
|
|
struct pcap_pkthdr *pkt_header;
|
|
|
|
u_char *pkt_data;
|
|
|
|
|
|
|
|
in = 0;
|
|
|
|
while(ld->go &&
|
2011-05-16 21:56:12 +00:00
|
|
|
(in = pcap_next_ex(pcap_opts->pcap_h, &pkt_header, &pkt_data)) == 1) {
|
|
|
|
if (use_threads) {
|
2011-05-26 19:22:16 +00:00
|
|
|
capture_loop_queue_packet_cb((u_char *)pcap_opts, pkt_header, pkt_data);
|
2011-05-16 21:56:12 +00:00
|
|
|
} else {
|
2011-05-26 19:22:16 +00:00
|
|
|
capture_loop_write_packet_cb((u_char *)pcap_opts, pkt_header, pkt_data);
|
2011-05-16 21:56:12 +00:00
|
|
|
}
|
|
|
|
}
|
2011-03-21 16:57:11 +00:00
|
|
|
|
2012-11-25 18:35:41 +00:00
|
|
|
if (in < 0) {
|
2011-05-15 22:54:52 +00:00
|
|
|
pcap_opts->pcap_err = TRUE;
|
2011-03-21 16:57:11 +00:00
|
|
|
ld->go = FALSE;
|
|
|
|
}
|
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
#endif /* pcap_next_ex */
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef LOG_CAPTURE_VERBOSE
|
2011-03-21 16:57:11 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_dispatch: %d new packet%s", inpkts, plurality(inpkts, "", "s"));
|
2008-02-16 02:39:58 +00:00
|
|
|
#endif
|
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
return ld->packet_count - packet_count_before;
|
2008-02-16 02:39:58 +00:00
|
|
|
}
|
|
|
|
|
2010-11-16 15:37:37 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
/* Isolate the Universally Unique Identifier from the interface. Basically, we
|
2011-05-05 20:46:02 +00:00
|
|
|
* want to grab only the characters between the '{' and '}' delimiters.
|
|
|
|
*
|
2010-11-16 15:37:37 +00:00
|
|
|
* Returns a GString that must be freed with g_string_free(). */
|
2011-03-21 16:57:11 +00:00
|
|
|
static GString *
|
|
|
|
isolate_uuid(const char *iface)
|
2010-11-16 15:37:37 +00:00
|
|
|
{
|
2012-11-25 18:35:41 +00:00
|
|
|
gchar *ptr;
|
2010-11-16 15:37:37 +00:00
|
|
|
GString *gstr;
|
|
|
|
|
|
|
|
ptr = strchr(iface, '{');
|
|
|
|
if (ptr == NULL)
|
|
|
|
return g_string_new(iface);
|
|
|
|
gstr = g_string_new(ptr + 1);
|
|
|
|
|
|
|
|
ptr = strchr(gstr->str, '}');
|
|
|
|
if (ptr == NULL)
|
|
|
|
return gstr;
|
|
|
|
|
|
|
|
gstr = g_string_truncate(gstr, ptr - gstr->str);
|
|
|
|
return gstr;
|
|
|
|
}
|
|
|
|
#endif
|
2008-02-16 02:39:58 +00:00
|
|
|
|
|
|
|
/* open the output file (temporary/specified name/ringbuffer/named pipe/stdout) */
|
|
|
|
/* Returns TRUE if the file opened successfully, FALSE otherwise. */
|
|
|
|
static gboolean
|
|
|
|
capture_loop_open_output(capture_options *capture_opts, int *save_file_fd,
|
2011-03-21 16:57:11 +00:00
|
|
|
char *errmsg, int errmsg_len)
|
|
|
|
{
|
2012-11-25 18:35:41 +00:00
|
|
|
char *tmpname;
|
|
|
|
gchar *capfile_name;
|
2016-05-29 17:52:19 +00:00
|
|
|
gchar *prefix, *suffix;
|
2012-11-25 18:35:41 +00:00
|
|
|
gboolean is_tempfile;
|
2011-03-21 16:57:11 +00:00
|
|
|
|
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_open_output: %s",
|
2011-05-15 22:54:52 +00:00
|
|
|
(capture_opts->save_file) ? capture_opts->save_file : "(not specified)");
|
2011-03-21 16:57:11 +00:00
|
|
|
|
|
|
|
if (capture_opts->save_file != NULL) {
|
|
|
|
/* We return to the caller while the capture is in progress.
|
|
|
|
* Therefore we need to take a copy of save_file in
|
|
|
|
* case the caller destroys it after we return.
|
|
|
|
*/
|
|
|
|
capfile_name = g_strdup(capture_opts->save_file);
|
|
|
|
|
|
|
|
if (capture_opts->output_to_pipe == TRUE) { /* either "-" or named pipe */
|
|
|
|
if (capture_opts->multi_files_on) {
|
|
|
|
/* ringbuffer is enabled; that doesn't work with standard output or a named pipe */
|
|
|
|
g_snprintf(errmsg, errmsg_len,
|
|
|
|
"Ring buffer requested, but capture is being written to standard output or to a named pipe.");
|
|
|
|
g_free(capfile_name);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (strcmp(capfile_name, "-") == 0) {
|
|
|
|
/* write to stdout */
|
|
|
|
*save_file_fd = 1;
|
2008-02-16 02:39:58 +00:00
|
|
|
#ifdef _WIN32
|
2011-03-21 16:57:11 +00:00
|
|
|
/* set output pipe to binary mode to avoid Windows text-mode processing (eg: for CR/LF) */
|
|
|
|
_setmode(1, O_BINARY);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
} /* if (...output_to_pipe ... */
|
|
|
|
|
|
|
|
else {
|
|
|
|
if (capture_opts->multi_files_on) {
|
|
|
|
/* ringbuffer is enabled */
|
|
|
|
*save_file_fd = ringbuf_init(capfile_name,
|
|
|
|
(capture_opts->has_ring_num_files) ? capture_opts->ring_num_files : 0,
|
|
|
|
capture_opts->group_read_access);
|
|
|
|
|
|
|
|
/* we need the ringbuf name */
|
2012-11-25 18:35:41 +00:00
|
|
|
if (*save_file_fd != -1) {
|
2011-03-21 16:57:11 +00:00
|
|
|
g_free(capfile_name);
|
|
|
|
capfile_name = g_strdup(ringbuf_current_filename());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Try to open/create the specified file for use as a capture buffer. */
|
|
|
|
*save_file_fd = ws_open(capfile_name, O_RDWR|O_BINARY|O_TRUNC|O_CREAT,
|
|
|
|
(capture_opts->group_read_access) ? 0640 : 0600);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
is_tempfile = FALSE;
|
|
|
|
} else {
|
|
|
|
/* Choose a random name for the temporary capture buffer */
|
2011-05-21 20:57:00 +00:00
|
|
|
if (global_capture_opts.ifaces->len > 1) {
|
|
|
|
prefix = g_strdup_printf("wireshark_%d_interfaces", global_capture_opts.ifaces->len);
|
2016-06-01 15:42:12 +00:00
|
|
|
if (capture_opts->use_pcapng) {
|
|
|
|
suffix = ".pcapng";
|
|
|
|
}else{
|
|
|
|
suffix = ".pcap";
|
|
|
|
}
|
2011-05-21 20:57:00 +00:00
|
|
|
} else {
|
2011-08-07 18:15:45 +00:00
|
|
|
gchar *basename;
|
From Mike Garratt:
Friendly Names for interfaces on Windows
Notes on the changes the patch covers:
* if_info_t struct: addition of friendly_name
* Dumpcap Interface list format changes:
+ Win32: "dumpcap -D" shows friendly_name in place of descript if known
+ All: machine interface "dumpcap -D -Z none" includes friendly_name in the
list in addition to the existing parameters
* interface_options struct: addition of console_display_name
+ When an interface name is displayed in a console, it will typically be the
console_display_name (instead of name).
+ console_display_name is used as the basis of the autogenerated temp
filenames
+ console_display_name is typically set to the friendly_name if known,
otherwise it is set to the interface name
* Enhancements to capture_opts_add_iface_opt() (the function which process -i
options).
+ Can now specify the interface using its name and friendly_name
+ Interface name matching is case insenstive
+ Name matching first attempts exact matching, then falls back to prefix
matching
(e.g. dumpcap -i local)
+ Validates interface names, instead of blindly sending them off to
winpcap/libpcap
+ Interface specification by number is still supported.
* capture_opts_trim_iface() has been refactored:
+ Instead of repeating a decent chunk of the cost in
capture_opts_add_iface_opt(), it calls capture_opts_trim_iface() to specify the
interface.
* introduction of capture_win_ifnames.[ch] (windows only code)
+ Implements static function GetInterfaceFriendlyNameFromDeviceGuid() - a
windows version independant function to convert an interface guid into its
friendly name. Uses published api functions on windows vista and higher, but
falls back to unpublished API functions on older windows releases.
+ void get_windows_interface_friendlyname(/* IN */ char
*interface_devicename, /* OUT */char **interface_friendlyname); - extracts the
GUID from the interface_devicename, then uses
GetInterfaceFriendlyNameFromDeviceGuid() to do the resolution
* Auto temp filename generation:
+ Now uses wireshark_pcapng_* or wireshark_pcap_* depending on file format
+ Basis temp filename format on console_display_name
+ Win32: if console_display_name is a windows interface guid, extracts
numbers from GUID here (instead of in interface option processing)
GUI CHANGES:
* Dialog that displays when you click the "Manage Interfaces" button (within
Capture Options dialog) has been renamed from "Add new interfaces" to
"Interface Management"
* ui/gtk/capture_dlg.c: new_interfaces_w variable renamed to
interface_management_w
* Win32: Local Interfaces tab on Interface Management dialog, shows includes
friendly name as far left column
* Interface Management dialog defaults to larger size on win32 - so it fits
without resizing local interfaces tab
* Interface Management dialog now saves preferences when you click the apply
button (local hidden interfaces was not persisting across restarts)
* Tweaks: "Interface Details" dialog (Interface list->Capture Interfaces ->
Details):
+ "Friendly Name" renamed to "NDIS Friendly Name"
+ Added "OS Friendly Name" to the top of the list
* Win32: The "Capture Interfaces" dialog now shows the friendly name instead of
device guid
* Welcome screen:
+ The height of the interface list scrollbox dynamically adjusts & updates to
the number visible interfaces.
Up to 10 interfaces can be listed without a scroll bar, the minimum height
is for 2 interfaces.
+ Win32: now shows just the Friendly Name if known - in place of
"Interfacename_Guid:(Description)"
svn path=/trunk/; revision=46083
2012-11-19 20:07:27 +00:00
|
|
|
basename = g_path_get_basename(g_array_index(global_capture_opts.ifaces, interface_options, 0).console_display_name);
|
2010-11-16 15:37:37 +00:00
|
|
|
#ifdef _WIN32
|
From Mike Garratt:
Friendly Names for interfaces on Windows
Notes on the changes the patch covers:
* if_info_t struct: addition of friendly_name
* Dumpcap Interface list format changes:
+ Win32: "dumpcap -D" shows friendly_name in place of descript if known
+ All: machine interface "dumpcap -D -Z none" includes friendly_name in the
list in addition to the existing parameters
* interface_options struct: addition of console_display_name
+ When an interface name is displayed in a console, it will typically be the
console_display_name (instead of name).
+ console_display_name is used as the basis of the autogenerated temp
filenames
+ console_display_name is typically set to the friendly_name if known,
otherwise it is set to the interface name
* Enhancements to capture_opts_add_iface_opt() (the function which process -i
options).
+ Can now specify the interface using its name and friendly_name
+ Interface name matching is case insenstive
+ Name matching first attempts exact matching, then falls back to prefix
matching
(e.g. dumpcap -i local)
+ Validates interface names, instead of blindly sending them off to
winpcap/libpcap
+ Interface specification by number is still supported.
* capture_opts_trim_iface() has been refactored:
+ Instead of repeating a decent chunk of the cost in
capture_opts_add_iface_opt(), it calls capture_opts_trim_iface() to specify the
interface.
* introduction of capture_win_ifnames.[ch] (windows only code)
+ Implements static function GetInterfaceFriendlyNameFromDeviceGuid() - a
windows version independant function to convert an interface guid into its
friendly name. Uses published api functions on windows vista and higher, but
falls back to unpublished API functions on older windows releases.
+ void get_windows_interface_friendlyname(/* IN */ char
*interface_devicename, /* OUT */char **interface_friendlyname); - extracts the
GUID from the interface_devicename, then uses
GetInterfaceFriendlyNameFromDeviceGuid() to do the resolution
* Auto temp filename generation:
+ Now uses wireshark_pcapng_* or wireshark_pcap_* depending on file format
+ Basis temp filename format on console_display_name
+ Win32: if console_display_name is a windows interface guid, extracts
numbers from GUID here (instead of in interface option processing)
GUI CHANGES:
* Dialog that displays when you click the "Manage Interfaces" button (within
Capture Options dialog) has been renamed from "Add new interfaces" to
"Interface Management"
* ui/gtk/capture_dlg.c: new_interfaces_w variable renamed to
interface_management_w
* Win32: Local Interfaces tab on Interface Management dialog, shows includes
friendly name as far left column
* Interface Management dialog defaults to larger size on win32 - so it fits
without resizing local interfaces tab
* Interface Management dialog now saves preferences when you click the apply
button (local hidden interfaces was not persisting across restarts)
* Tweaks: "Interface Details" dialog (Interface list->Capture Interfaces ->
Details):
+ "Friendly Name" renamed to "NDIS Friendly Name"
+ Added "OS Friendly Name" to the top of the list
* Win32: The "Capture Interfaces" dialog now shows the friendly name instead of
device guid
* Welcome screen:
+ The height of the interface list scrollbox dynamically adjusts & updates to
the number visible interfaces.
Up to 10 interfaces can be listed without a scroll bar, the minimum height
is for 2 interfaces.
+ Win32: now shows just the Friendly Name if known - in place of
"Interfacename_Guid:(Description)"
svn path=/trunk/; revision=46083
2012-11-19 20:07:27 +00:00
|
|
|
/* use the generic portion of the interface guid to form the basis of the filename */
|
2012-11-25 18:35:41 +00:00
|
|
|
if (strncmp("NPF_{", basename, 5)==0)
|
From Mike Garratt:
Friendly Names for interfaces on Windows
Notes on the changes the patch covers:
* if_info_t struct: addition of friendly_name
* Dumpcap Interface list format changes:
+ Win32: "dumpcap -D" shows friendly_name in place of descript if known
+ All: machine interface "dumpcap -D -Z none" includes friendly_name in the
list in addition to the existing parameters
* interface_options struct: addition of console_display_name
+ When an interface name is displayed in a console, it will typically be the
console_display_name (instead of name).
+ console_display_name is used as the basis of the autogenerated temp
filenames
+ console_display_name is typically set to the friendly_name if known,
otherwise it is set to the interface name
* Enhancements to capture_opts_add_iface_opt() (the function which process -i
options).
+ Can now specify the interface using its name and friendly_name
+ Interface name matching is case insenstive
+ Name matching first attempts exact matching, then falls back to prefix
matching
(e.g. dumpcap -i local)
+ Validates interface names, instead of blindly sending them off to
winpcap/libpcap
+ Interface specification by number is still supported.
* capture_opts_trim_iface() has been refactored:
+ Instead of repeating a decent chunk of the cost in
capture_opts_add_iface_opt(), it calls capture_opts_trim_iface() to specify the
interface.
* introduction of capture_win_ifnames.[ch] (windows only code)
+ Implements static function GetInterfaceFriendlyNameFromDeviceGuid() - a
windows version independant function to convert an interface guid into its
friendly name. Uses published api functions on windows vista and higher, but
falls back to unpublished API functions on older windows releases.
+ void get_windows_interface_friendlyname(/* IN */ char
*interface_devicename, /* OUT */char **interface_friendlyname); - extracts the
GUID from the interface_devicename, then uses
GetInterfaceFriendlyNameFromDeviceGuid() to do the resolution
* Auto temp filename generation:
+ Now uses wireshark_pcapng_* or wireshark_pcap_* depending on file format
+ Basis temp filename format on console_display_name
+ Win32: if console_display_name is a windows interface guid, extracts
numbers from GUID here (instead of in interface option processing)
GUI CHANGES:
* Dialog that displays when you click the "Manage Interfaces" button (within
Capture Options dialog) has been renamed from "Add new interfaces" to
"Interface Management"
* ui/gtk/capture_dlg.c: new_interfaces_w variable renamed to
interface_management_w
* Win32: Local Interfaces tab on Interface Management dialog, shows includes
friendly name as far left column
* Interface Management dialog defaults to larger size on win32 - so it fits
without resizing local interfaces tab
* Interface Management dialog now saves preferences when you click the apply
button (local hidden interfaces was not persisting across restarts)
* Tweaks: "Interface Details" dialog (Interface list->Capture Interfaces ->
Details):
+ "Friendly Name" renamed to "NDIS Friendly Name"
+ Added "OS Friendly Name" to the top of the list
* Win32: The "Capture Interfaces" dialog now shows the friendly name instead of
device guid
* Welcome screen:
+ The height of the interface list scrollbox dynamically adjusts & updates to
the number visible interfaces.
Up to 10 interfaces can be listed without a scroll bar, the minimum height
is for 2 interfaces.
+ Win32: now shows just the Friendly Name if known - in place of
"Interfacename_Guid:(Description)"
svn path=/trunk/; revision=46083
2012-11-19 20:07:27 +00:00
|
|
|
{
|
|
|
|
/* we have a windows guid style device name, extract the guid digits as the basis of the filename */
|
|
|
|
GString *iface;
|
|
|
|
iface = isolate_uuid(basename);
|
|
|
|
g_free(basename);
|
|
|
|
basename = g_strdup(iface->str);
|
|
|
|
g_string_free(iface, TRUE);
|
|
|
|
}
|
2011-03-21 16:57:11 +00:00
|
|
|
#endif
|
2016-05-29 17:52:19 +00:00
|
|
|
/* generate the temp file name prefix and suffix */
|
From Mike Garratt:
Friendly Names for interfaces on Windows
Notes on the changes the patch covers:
* if_info_t struct: addition of friendly_name
* Dumpcap Interface list format changes:
+ Win32: "dumpcap -D" shows friendly_name in place of descript if known
+ All: machine interface "dumpcap -D -Z none" includes friendly_name in the
list in addition to the existing parameters
* interface_options struct: addition of console_display_name
+ When an interface name is displayed in a console, it will typically be the
console_display_name (instead of name).
+ console_display_name is used as the basis of the autogenerated temp
filenames
+ console_display_name is typically set to the friendly_name if known,
otherwise it is set to the interface name
* Enhancements to capture_opts_add_iface_opt() (the function which process -i
options).
+ Can now specify the interface using its name and friendly_name
+ Interface name matching is case insenstive
+ Name matching first attempts exact matching, then falls back to prefix
matching
(e.g. dumpcap -i local)
+ Validates interface names, instead of blindly sending them off to
winpcap/libpcap
+ Interface specification by number is still supported.
* capture_opts_trim_iface() has been refactored:
+ Instead of repeating a decent chunk of the cost in
capture_opts_add_iface_opt(), it calls capture_opts_trim_iface() to specify the
interface.
* introduction of capture_win_ifnames.[ch] (windows only code)
+ Implements static function GetInterfaceFriendlyNameFromDeviceGuid() - a
windows version independant function to convert an interface guid into its
friendly name. Uses published api functions on windows vista and higher, but
falls back to unpublished API functions on older windows releases.
+ void get_windows_interface_friendlyname(/* IN */ char
*interface_devicename, /* OUT */char **interface_friendlyname); - extracts the
GUID from the interface_devicename, then uses
GetInterfaceFriendlyNameFromDeviceGuid() to do the resolution
* Auto temp filename generation:
+ Now uses wireshark_pcapng_* or wireshark_pcap_* depending on file format
+ Basis temp filename format on console_display_name
+ Win32: if console_display_name is a windows interface guid, extracts
numbers from GUID here (instead of in interface option processing)
GUI CHANGES:
* Dialog that displays when you click the "Manage Interfaces" button (within
Capture Options dialog) has been renamed from "Add new interfaces" to
"Interface Management"
* ui/gtk/capture_dlg.c: new_interfaces_w variable renamed to
interface_management_w
* Win32: Local Interfaces tab on Interface Management dialog, shows includes
friendly name as far left column
* Interface Management dialog defaults to larger size on win32 - so it fits
without resizing local interfaces tab
* Interface Management dialog now saves preferences when you click the apply
button (local hidden interfaces was not persisting across restarts)
* Tweaks: "Interface Details" dialog (Interface list->Capture Interfaces ->
Details):
+ "Friendly Name" renamed to "NDIS Friendly Name"
+ Added "OS Friendly Name" to the top of the list
* Win32: The "Capture Interfaces" dialog now shows the friendly name instead of
device guid
* Welcome screen:
+ The height of the interface list scrollbox dynamically adjusts & updates to
the number visible interfaces.
Up to 10 interfaces can be listed without a scroll bar, the minimum height
is for 2 interfaces.
+ Win32: now shows just the Friendly Name if known - in place of
"Interfacename_Guid:(Description)"
svn path=/trunk/; revision=46083
2012-11-19 20:07:27 +00:00
|
|
|
if (capture_opts->use_pcapng) {
|
2016-05-29 17:52:19 +00:00
|
|
|
prefix = g_strconcat("wireshark_", basename, NULL);
|
|
|
|
suffix = ".pcapng";
|
From Mike Garratt:
Friendly Names for interfaces on Windows
Notes on the changes the patch covers:
* if_info_t struct: addition of friendly_name
* Dumpcap Interface list format changes:
+ Win32: "dumpcap -D" shows friendly_name in place of descript if known
+ All: machine interface "dumpcap -D -Z none" includes friendly_name in the
list in addition to the existing parameters
* interface_options struct: addition of console_display_name
+ When an interface name is displayed in a console, it will typically be the
console_display_name (instead of name).
+ console_display_name is used as the basis of the autogenerated temp
filenames
+ console_display_name is typically set to the friendly_name if known,
otherwise it is set to the interface name
* Enhancements to capture_opts_add_iface_opt() (the function which process -i
options).
+ Can now specify the interface using its name and friendly_name
+ Interface name matching is case insenstive
+ Name matching first attempts exact matching, then falls back to prefix
matching
(e.g. dumpcap -i local)
+ Validates interface names, instead of blindly sending them off to
winpcap/libpcap
+ Interface specification by number is still supported.
* capture_opts_trim_iface() has been refactored:
+ Instead of repeating a decent chunk of the cost in
capture_opts_add_iface_opt(), it calls capture_opts_trim_iface() to specify the
interface.
* introduction of capture_win_ifnames.[ch] (windows only code)
+ Implements static function GetInterfaceFriendlyNameFromDeviceGuid() - a
windows version independant function to convert an interface guid into its
friendly name. Uses published api functions on windows vista and higher, but
falls back to unpublished API functions on older windows releases.
+ void get_windows_interface_friendlyname(/* IN */ char
*interface_devicename, /* OUT */char **interface_friendlyname); - extracts the
GUID from the interface_devicename, then uses
GetInterfaceFriendlyNameFromDeviceGuid() to do the resolution
* Auto temp filename generation:
+ Now uses wireshark_pcapng_* or wireshark_pcap_* depending on file format
+ Basis temp filename format on console_display_name
+ Win32: if console_display_name is a windows interface guid, extracts
numbers from GUID here (instead of in interface option processing)
GUI CHANGES:
* Dialog that displays when you click the "Manage Interfaces" button (within
Capture Options dialog) has been renamed from "Add new interfaces" to
"Interface Management"
* ui/gtk/capture_dlg.c: new_interfaces_w variable renamed to
interface_management_w
* Win32: Local Interfaces tab on Interface Management dialog, shows includes
friendly name as far left column
* Interface Management dialog defaults to larger size on win32 - so it fits
without resizing local interfaces tab
* Interface Management dialog now saves preferences when you click the apply
button (local hidden interfaces was not persisting across restarts)
* Tweaks: "Interface Details" dialog (Interface list->Capture Interfaces ->
Details):
+ "Friendly Name" renamed to "NDIS Friendly Name"
+ Added "OS Friendly Name" to the top of the list
* Win32: The "Capture Interfaces" dialog now shows the friendly name instead of
device guid
* Welcome screen:
+ The height of the interface list scrollbox dynamically adjusts & updates to
the number visible interfaces.
Up to 10 interfaces can be listed without a scroll bar, the minimum height
is for 2 interfaces.
+ Win32: now shows just the Friendly Name if known - in place of
"Interfacename_Guid:(Description)"
svn path=/trunk/; revision=46083
2012-11-19 20:07:27 +00:00
|
|
|
}else{
|
2016-05-29 17:52:19 +00:00
|
|
|
prefix = g_strconcat("wireshark_", basename, NULL);
|
|
|
|
suffix = ".pcap";
|
From Mike Garratt:
Friendly Names for interfaces on Windows
Notes on the changes the patch covers:
* if_info_t struct: addition of friendly_name
* Dumpcap Interface list format changes:
+ Win32: "dumpcap -D" shows friendly_name in place of descript if known
+ All: machine interface "dumpcap -D -Z none" includes friendly_name in the
list in addition to the existing parameters
* interface_options struct: addition of console_display_name
+ When an interface name is displayed in a console, it will typically be the
console_display_name (instead of name).
+ console_display_name is used as the basis of the autogenerated temp
filenames
+ console_display_name is typically set to the friendly_name if known,
otherwise it is set to the interface name
* Enhancements to capture_opts_add_iface_opt() (the function which process -i
options).
+ Can now specify the interface using its name and friendly_name
+ Interface name matching is case insenstive
+ Name matching first attempts exact matching, then falls back to prefix
matching
(e.g. dumpcap -i local)
+ Validates interface names, instead of blindly sending them off to
winpcap/libpcap
+ Interface specification by number is still supported.
* capture_opts_trim_iface() has been refactored:
+ Instead of repeating a decent chunk of the cost in
capture_opts_add_iface_opt(), it calls capture_opts_trim_iface() to specify the
interface.
* introduction of capture_win_ifnames.[ch] (windows only code)
+ Implements static function GetInterfaceFriendlyNameFromDeviceGuid() - a
windows version independant function to convert an interface guid into its
friendly name. Uses published api functions on windows vista and higher, but
falls back to unpublished API functions on older windows releases.
+ void get_windows_interface_friendlyname(/* IN */ char
*interface_devicename, /* OUT */char **interface_friendlyname); - extracts the
GUID from the interface_devicename, then uses
GetInterfaceFriendlyNameFromDeviceGuid() to do the resolution
* Auto temp filename generation:
+ Now uses wireshark_pcapng_* or wireshark_pcap_* depending on file format
+ Basis temp filename format on console_display_name
+ Win32: if console_display_name is a windows interface guid, extracts
numbers from GUID here (instead of in interface option processing)
GUI CHANGES:
* Dialog that displays when you click the "Manage Interfaces" button (within
Capture Options dialog) has been renamed from "Add new interfaces" to
"Interface Management"
* ui/gtk/capture_dlg.c: new_interfaces_w variable renamed to
interface_management_w
* Win32: Local Interfaces tab on Interface Management dialog, shows includes
friendly name as far left column
* Interface Management dialog defaults to larger size on win32 - so it fits
without resizing local interfaces tab
* Interface Management dialog now saves preferences when you click the apply
button (local hidden interfaces was not persisting across restarts)
* Tweaks: "Interface Details" dialog (Interface list->Capture Interfaces ->
Details):
+ "Friendly Name" renamed to "NDIS Friendly Name"
+ Added "OS Friendly Name" to the top of the list
* Win32: The "Capture Interfaces" dialog now shows the friendly name instead of
device guid
* Welcome screen:
+ The height of the interface list scrollbox dynamically adjusts & updates to
the number visible interfaces.
Up to 10 interfaces can be listed without a scroll bar, the minimum height
is for 2 interfaces.
+ Win32: now shows just the Friendly Name if known - in place of
"Interfacename_Guid:(Description)"
svn path=/trunk/; revision=46083
2012-11-19 20:07:27 +00:00
|
|
|
}
|
2011-08-07 18:15:45 +00:00
|
|
|
g_free(basename);
|
2011-05-21 20:57:00 +00:00
|
|
|
}
|
2016-05-29 17:52:19 +00:00
|
|
|
*save_file_fd = create_tempfile(&tmpname, prefix, suffix);
|
2011-03-21 16:57:11 +00:00
|
|
|
g_free(prefix);
|
|
|
|
capfile_name = g_strdup(tmpname);
|
|
|
|
is_tempfile = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* did we fail to open the output file? */
|
|
|
|
if (*save_file_fd == -1) {
|
|
|
|
if (is_tempfile) {
|
|
|
|
g_snprintf(errmsg, errmsg_len,
|
|
|
|
"The temporary file to which the capture would be saved (\"%s\") "
|
2011-06-28 09:00:11 +00:00
|
|
|
"could not be opened: %s.", capfile_name, g_strerror(errno));
|
2011-03-21 16:57:11 +00:00
|
|
|
} else {
|
|
|
|
if (capture_opts->multi_files_on) {
|
|
|
|
ringbuf_error_cleanup();
|
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
g_snprintf(errmsg, errmsg_len,
|
|
|
|
"The file to which the capture would be saved (\"%s\") "
|
|
|
|
"could not be opened: %s.", capfile_name,
|
2011-06-28 09:00:11 +00:00
|
|
|
g_strerror(errno));
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
|
|
|
g_free(capfile_name);
|
|
|
|
return FALSE;
|
2008-02-16 02:39:58 +00:00
|
|
|
}
|
|
|
|
|
2012-11-25 18:35:41 +00:00
|
|
|
if (capture_opts->save_file != NULL) {
|
2011-03-21 16:57:11 +00:00
|
|
|
g_free(capture_opts->save_file);
|
|
|
|
}
|
|
|
|
capture_opts->save_file = capfile_name;
|
|
|
|
/* capture_opts.save_file is "g_free"ed later, which is equivalent to
|
|
|
|
"g_free(capfile_name)". */
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
return TRUE;
|
2008-02-16 02:39:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-25 20:14:22 +00:00
|
|
|
/* Do the work of handling either the file size or file duration capture
|
|
|
|
conditions being reached, and switching files or stopping. */
|
|
|
|
static gboolean
|
|
|
|
do_file_switch_or_stop(capture_options *capture_opts,
|
|
|
|
condition *cnd_autostop_files,
|
|
|
|
condition *cnd_autostop_size,
|
|
|
|
condition *cnd_file_duration)
|
|
|
|
{
|
2012-11-25 18:35:41 +00:00
|
|
|
guint i;
|
|
|
|
pcap_options *pcap_opts;
|
|
|
|
interface_options interface_opts;
|
|
|
|
gboolean successful;
|
2011-05-15 22:54:52 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
if (capture_opts->multi_files_on) {
|
|
|
|
if (cnd_autostop_files != NULL &&
|
2015-06-24 21:18:13 +00:00
|
|
|
cnd_eval(cnd_autostop_files, (guint64)++global_ld.autostop_files)) {
|
2011-03-21 16:57:11 +00:00
|
|
|
/* no files left: stop here */
|
|
|
|
global_ld.go = FALSE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Switch to the next ringbuffer file */
|
|
|
|
if (ringbuf_switch_file(&global_ld.pdh, &capture_opts->save_file,
|
|
|
|
&global_ld.save_file_fd, &global_ld.err)) {
|
|
|
|
|
|
|
|
/* File switch succeeded: reset the conditions */
|
|
|
|
global_ld.bytes_written = 0;
|
|
|
|
if (capture_opts->use_pcapng) {
|
2015-01-03 22:00:47 +00:00
|
|
|
char *appname;
|
2016-09-22 11:23:37 +00:00
|
|
|
GString *cpu_info_str;
|
2015-01-03 22:00:47 +00:00
|
|
|
GString *os_info_str;
|
2012-02-14 17:07:52 +00:00
|
|
|
|
2016-09-22 11:23:37 +00:00
|
|
|
cpu_info_str = g_string_new("");
|
2012-02-21 05:52:50 +00:00
|
|
|
os_info_str = g_string_new("");
|
2016-09-22 11:23:37 +00:00
|
|
|
get_cpu_info(cpu_info_str);
|
2012-02-21 05:52:50 +00:00
|
|
|
get_os_version_info(os_info_str);
|
2011-03-21 16:57:11 +00:00
|
|
|
|
2015-01-03 22:00:47 +00:00
|
|
|
appname = g_strdup_printf("Dumpcap (Wireshark) %s", get_ws_vcs_version_info());
|
2013-09-29 20:53:13 +00:00
|
|
|
successful = pcapng_write_session_header_block(global_ld.pdh,
|
2016-09-22 11:23:37 +00:00
|
|
|
(const char *)capture_opts->capture_comment, /* Comment */
|
|
|
|
cpu_info_str->str, /* HW */
|
2012-02-21 05:52:50 +00:00
|
|
|
os_info_str->str, /* OS */
|
2012-02-14 17:07:52 +00:00
|
|
|
appname,
|
2016-09-22 11:23:37 +00:00
|
|
|
-1, /* section_length */
|
2012-02-29 20:32:12 +00:00
|
|
|
&(global_ld.bytes_written),
|
2012-02-14 17:07:52 +00:00
|
|
|
&global_ld.err);
|
2015-01-03 22:00:47 +00:00
|
|
|
g_free(appname);
|
2012-02-14 17:07:52 +00:00
|
|
|
|
2011-05-15 22:54:52 +00:00
|
|
|
for (i = 0; successful && (i < capture_opts->ifaces->len); i++) {
|
|
|
|
interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts = g_array_index(global_ld.pcaps, pcap_options *, i);
|
2013-09-29 20:53:13 +00:00
|
|
|
successful = pcapng_write_interface_description_block(global_ld.pdh,
|
|
|
|
NULL, /* OPT_COMMENT 1 */
|
|
|
|
interface_opts.name, /* IDB_NAME 2 */
|
|
|
|
interface_opts.descr, /* IDB_DESCRIPTION 3 */
|
|
|
|
interface_opts.cfilter, /* IDB_FILTER 11 */
|
|
|
|
os_info_str->str, /* IDB_OS 12 */
|
|
|
|
pcap_opts->linktype,
|
|
|
|
pcap_opts->snaplen,
|
|
|
|
&(global_ld.bytes_written),
|
|
|
|
0, /* IDB_IF_SPEED 8 */
|
|
|
|
pcap_opts->ts_nsec ? 9 : 6, /* IDB_TSRESOL 9 */
|
|
|
|
&global_ld.err);
|
2011-05-15 22:54:52 +00:00
|
|
|
}
|
2012-02-21 16:14:57 +00:00
|
|
|
|
|
|
|
g_string_free(os_info_str, TRUE);
|
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
} else {
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts = g_array_index(global_ld.pcaps, pcap_options *, 0);
|
2013-09-29 20:26:25 +00:00
|
|
|
successful = libpcap_write_file_header(global_ld.pdh, pcap_opts->linktype, pcap_opts->snaplen,
|
2012-03-10 00:24:42 +00:00
|
|
|
pcap_opts->ts_nsec, &global_ld.bytes_written, &global_ld.err);
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
|
|
|
if (!successful) {
|
|
|
|
fclose(global_ld.pdh);
|
|
|
|
global_ld.pdh = NULL;
|
|
|
|
global_ld.go = FALSE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-05-15 22:54:52 +00:00
|
|
|
if (cnd_autostop_size)
|
2011-03-21 16:57:11 +00:00
|
|
|
cnd_reset(cnd_autostop_size);
|
2011-05-15 22:54:52 +00:00
|
|
|
if (cnd_file_duration)
|
2011-03-21 16:57:11 +00:00
|
|
|
cnd_reset(cnd_file_duration);
|
2012-12-20 20:00:06 +00:00
|
|
|
fflush(global_ld.pdh);
|
2011-03-21 16:57:11 +00:00
|
|
|
if (!quiet)
|
|
|
|
report_packet_count(global_ld.inpkts_to_sync_pipe);
|
|
|
|
global_ld.inpkts_to_sync_pipe = 0;
|
|
|
|
report_new_capture_file(capture_opts->save_file);
|
|
|
|
} else {
|
|
|
|
/* File switch failed: stop here */
|
|
|
|
global_ld.go = FALSE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* single file, stop now */
|
2010-05-25 20:14:22 +00:00
|
|
|
global_ld.go = FALSE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-03-21 16:57:11 +00:00
|
|
|
return TRUE;
|
2010-05-25 20:14:22 +00:00
|
|
|
}
|
|
|
|
|
2011-05-16 21:56:12 +00:00
|
|
|
static void *
|
|
|
|
pcap_read_handler(void* arg)
|
|
|
|
{
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_options *pcap_opts;
|
2012-11-25 18:35:41 +00:00
|
|
|
char errmsg[MSG_MAX_LENGTH+1];
|
2011-05-16 21:56:12 +00:00
|
|
|
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts = (pcap_options *)arg;
|
2011-05-16 21:56:12 +00:00
|
|
|
|
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Started thread for interface %d.",
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts->interface_id);
|
2011-05-16 21:56:12 +00:00
|
|
|
|
|
|
|
while (global_ld.go) {
|
|
|
|
/* dispatch incoming packets */
|
2011-05-26 18:12:41 +00:00
|
|
|
capture_loop_dispatch(&global_ld, errmsg, sizeof(errmsg), pcap_opts);
|
2011-05-16 21:56:12 +00:00
|
|
|
}
|
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Stopped thread for interface %d.",
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts->interface_id);
|
2011-05-16 21:56:12 +00:00
|
|
|
g_thread_exit(NULL);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2008-02-16 02:39:58 +00:00
|
|
|
/* Do the low-level work of a capture.
|
|
|
|
Returns TRUE if it succeeds, FALSE otherwise. */
|
|
|
|
static gboolean
|
|
|
|
capture_loop_start(capture_options *capture_opts, gboolean *stats_known, struct pcap_stat *stats)
|
|
|
|
{
|
2015-09-02 21:35:37 +00:00
|
|
|
#ifdef _WIN32
|
2012-11-25 18:35:41 +00:00
|
|
|
DWORD upd_time, cur_time; /* GetTickCount() returns a "DWORD" (which is 'unsigned long') */
|
2011-05-05 20:46:02 +00:00
|
|
|
#else
|
2012-11-25 18:35:41 +00:00
|
|
|
struct timeval upd_time, cur_time;
|
|
|
|
#endif
|
|
|
|
int err_close;
|
|
|
|
int inpkts;
|
|
|
|
condition *cnd_file_duration = NULL;
|
|
|
|
condition *cnd_autostop_files = NULL;
|
|
|
|
condition *cnd_autostop_size = NULL;
|
|
|
|
condition *cnd_autostop_duration = NULL;
|
|
|
|
gboolean write_ok;
|
|
|
|
gboolean close_ok;
|
|
|
|
gboolean cfilter_error = FALSE;
|
|
|
|
char errmsg[MSG_MAX_LENGTH+1];
|
|
|
|
char secondary_errmsg[MSG_MAX_LENGTH+1];
|
|
|
|
pcap_options *pcap_opts;
|
|
|
|
interface_options interface_opts;
|
|
|
|
guint i, error_index = 0;
|
2011-03-21 16:57:11 +00:00
|
|
|
|
|
|
|
*errmsg = '\0';
|
|
|
|
*secondary_errmsg = '\0';
|
|
|
|
|
|
|
|
/* init the loop data */
|
|
|
|
global_ld.go = TRUE;
|
|
|
|
global_ld.packet_count = 0;
|
2010-05-26 00:19:27 +00:00
|
|
|
#ifdef SIGINFO
|
2011-03-21 16:57:11 +00:00
|
|
|
global_ld.report_packet_count = FALSE;
|
|
|
|
#endif
|
|
|
|
if (capture_opts->has_autostop_packets)
|
2011-05-15 22:54:52 +00:00
|
|
|
global_ld.packet_max = capture_opts->autostop_packets;
|
2011-03-21 16:57:11 +00:00
|
|
|
else
|
2011-05-15 22:54:52 +00:00
|
|
|
global_ld.packet_max = 0; /* no limit */
|
2011-03-21 16:57:11 +00:00
|
|
|
global_ld.inpkts_to_sync_pipe = 0;
|
|
|
|
global_ld.err = 0; /* no error seen yet */
|
|
|
|
global_ld.pdh = NULL;
|
|
|
|
global_ld.autostop_files = 0;
|
|
|
|
global_ld.save_file_fd = -1;
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
/* We haven't yet gotten the capture statistics. */
|
|
|
|
*stats_known = FALSE;
|
|
|
|
|
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Capture loop starting ...");
|
|
|
|
capture_opts_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, capture_opts);
|
|
|
|
|
|
|
|
/* open the "input file" from network interface or capture pipe */
|
|
|
|
if (!capture_loop_open_input(capture_opts, &global_ld, errmsg, sizeof(errmsg),
|
|
|
|
secondary_errmsg, sizeof(secondary_errmsg))) {
|
|
|
|
goto error;
|
|
|
|
}
|
2011-05-15 22:54:52 +00:00
|
|
|
for (i = 0; i < capture_opts->ifaces->len; i++) {
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts = g_array_index(global_ld.pcaps, pcap_options *, i);
|
2011-05-15 22:54:52 +00:00
|
|
|
interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
|
|
|
|
/* init the input filter from the network interface (capture pipe will do nothing) */
|
2011-06-07 12:23:15 +00:00
|
|
|
/*
|
|
|
|
* When remote capturing WinPCap crashes when the capture filter
|
2012-03-03 16:17:20 +00:00
|
|
|
* is NULL. This might be a bug in WPCap. Therefore we provide an empty
|
2011-06-07 12:23:15 +00:00
|
|
|
* string.
|
|
|
|
*/
|
2011-05-26 18:12:41 +00:00
|
|
|
switch (capture_loop_init_filter(pcap_opts->pcap_h, pcap_opts->from_cap_pipe,
|
2011-05-15 22:54:52 +00:00
|
|
|
interface_opts.name,
|
2011-06-07 12:23:15 +00:00
|
|
|
interface_opts.cfilter?interface_opts.cfilter:"")) {
|
2011-03-21 16:57:11 +00:00
|
|
|
|
2011-05-15 22:54:52 +00:00
|
|
|
case INITFILTER_NO_ERROR:
|
|
|
|
break;
|
2011-03-21 16:57:11 +00:00
|
|
|
|
2011-05-15 22:54:52 +00:00
|
|
|
case INITFILTER_BAD_FILTER:
|
|
|
|
cfilter_error = TRUE;
|
2011-06-27 11:30:39 +00:00
|
|
|
error_index = i;
|
2011-05-26 18:12:41 +00:00
|
|
|
g_snprintf(errmsg, sizeof(errmsg), "%s", pcap_geterr(pcap_opts->pcap_h));
|
2011-05-15 22:54:52 +00:00
|
|
|
goto error;
|
2011-03-21 16:57:11 +00:00
|
|
|
|
2011-05-15 22:54:52 +00:00
|
|
|
case INITFILTER_OTHER_ERROR:
|
|
|
|
g_snprintf(errmsg, sizeof(errmsg), "Can't install filter (%s).",
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_geterr(pcap_opts->pcap_h));
|
2011-05-15 22:54:52 +00:00
|
|
|
g_snprintf(secondary_errmsg, sizeof(secondary_errmsg), "%s", please_report);
|
|
|
|
goto error;
|
|
|
|
}
|
2010-05-26 00:19:27 +00:00
|
|
|
}
|
2011-03-21 16:57:11 +00:00
|
|
|
|
|
|
|
/* If we're supposed to write to a capture file, open it for output
|
|
|
|
(temporary/specified name/ringbuffer) */
|
|
|
|
if (capture_opts->saving_to_file) {
|
|
|
|
if (!capture_loop_open_output(capture_opts, &global_ld.save_file_fd,
|
|
|
|
errmsg, sizeof(errmsg))) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set up to write to the already-opened capture output file/files */
|
|
|
|
if (!capture_loop_init_output(capture_opts, &global_ld, errmsg,
|
|
|
|
sizeof(errmsg))) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX - capture SIGTERM and close the capture, in case we're on a
|
|
|
|
Linux 2.0[.x] system and you have to explicitly close the capture
|
|
|
|
stream in order to turn promiscuous mode off? We need to do that
|
|
|
|
in other places as well - and I don't think that works all the
|
|
|
|
time in any case, due to libpcap bugs. */
|
|
|
|
|
|
|
|
/* Well, we should be able to start capturing.
|
|
|
|
|
|
|
|
Sync out the capture file, so the header makes it to the file system,
|
|
|
|
and send a "capture started successfully and capture file created"
|
|
|
|
message to our parent so that they'll open the capture file and
|
|
|
|
update its windows to indicate that we have a live capture in
|
|
|
|
progress. */
|
2012-12-20 20:00:06 +00:00
|
|
|
fflush(global_ld.pdh);
|
2011-03-21 16:57:11 +00:00
|
|
|
report_new_capture_file(capture_opts->save_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize capture stop (and alike) conditions */
|
|
|
|
init_capture_stop_conditions();
|
|
|
|
/* create stop conditions */
|
2013-08-29 18:15:13 +00:00
|
|
|
if (capture_opts->has_autostop_filesize) {
|
2013-12-02 19:17:12 +00:00
|
|
|
if (capture_opts->autostop_filesize > (((guint32)INT_MAX + 1) / 1000)) {
|
|
|
|
capture_opts->autostop_filesize = ((guint32)INT_MAX + 1) / 1000;
|
2013-08-29 18:15:13 +00:00
|
|
|
}
|
2011-03-21 16:57:11 +00:00
|
|
|
cnd_autostop_size =
|
2013-12-02 19:17:12 +00:00
|
|
|
cnd_new(CND_CLASS_CAPTURESIZE, (guint64)capture_opts->autostop_filesize * 1000);
|
2013-08-29 18:15:13 +00:00
|
|
|
}
|
2011-03-21 16:57:11 +00:00
|
|
|
if (capture_opts->has_autostop_duration)
|
|
|
|
cnd_autostop_duration =
|
|
|
|
cnd_new(CND_CLASS_TIMEOUT,(gint32)capture_opts->autostop_duration);
|
|
|
|
|
|
|
|
if (capture_opts->multi_files_on) {
|
|
|
|
if (capture_opts->has_file_duration)
|
|
|
|
cnd_file_duration =
|
|
|
|
cnd_new(CND_CLASS_TIMEOUT, capture_opts->file_duration);
|
|
|
|
|
|
|
|
if (capture_opts->has_autostop_files)
|
|
|
|
cnd_autostop_files =
|
2015-06-24 21:18:13 +00:00
|
|
|
cnd_new(CND_CLASS_CAPTURESIZE, (guint64)capture_opts->autostop_files);
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* init the time values */
|
2015-09-02 21:35:37 +00:00
|
|
|
#ifdef _WIN32
|
2011-05-05 20:46:02 +00:00
|
|
|
upd_time = GetTickCount();
|
|
|
|
#else
|
|
|
|
gettimeofday(&upd_time, NULL);
|
|
|
|
#endif
|
2012-02-23 12:57:17 +00:00
|
|
|
start_time = create_timestamp();
|
2014-09-24 21:06:23 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Capture loop running.");
|
2011-03-21 16:57:11 +00:00
|
|
|
|
|
|
|
/* WOW, everything is prepared! */
|
|
|
|
/* please fasten your seat belts, we will enter now the actual capture loop */
|
2011-05-16 21:56:12 +00:00
|
|
|
if (use_threads) {
|
|
|
|
pcap_queue = g_async_queue_new();
|
2011-05-19 05:49:46 +00:00
|
|
|
pcap_queue_bytes = 0;
|
|
|
|
pcap_queue_packets = 0;
|
2011-05-16 21:56:12 +00:00
|
|
|
for (i = 0; i < global_ld.pcaps->len; i++) {
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts = g_array_index(global_ld.pcaps, pcap_options *, i);
|
2011-11-09 23:43:50 +00:00
|
|
|
#if GLIB_CHECK_VERSION(2,31,0)
|
|
|
|
/* XXX - Add an interface name here? */
|
|
|
|
pcap_opts->tid = g_thread_new("Capture read", pcap_read_handler, pcap_opts);
|
|
|
|
#else
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts->tid = g_thread_create(pcap_read_handler, pcap_opts, TRUE, NULL);
|
2011-11-09 23:43:50 +00:00
|
|
|
#endif
|
2011-05-16 21:56:12 +00:00
|
|
|
}
|
|
|
|
}
|
2011-03-21 16:57:11 +00:00
|
|
|
while (global_ld.go) {
|
|
|
|
/* dispatch incoming packets */
|
2011-05-16 21:56:12 +00:00
|
|
|
if (use_threads) {
|
|
|
|
pcap_queue_element *queue_element;
|
2012-02-29 18:04:23 +00:00
|
|
|
#if GLIB_CHECK_VERSION(2,31,18)
|
|
|
|
|
2012-04-25 16:05:29 +00:00
|
|
|
g_async_queue_lock(pcap_queue);
|
2013-03-03 17:14:19 +00:00
|
|
|
queue_element = (pcap_queue_element *)g_async_queue_timeout_pop_unlocked(pcap_queue, WRITER_THREAD_TIMEOUT);
|
2012-02-29 18:04:23 +00:00
|
|
|
#else
|
|
|
|
GTimeVal write_thread_time;
|
2011-05-16 21:56:12 +00:00
|
|
|
|
|
|
|
g_get_current_time(&write_thread_time);
|
|
|
|
g_time_val_add(&write_thread_time, WRITER_THREAD_TIMEOUT);
|
2011-05-19 05:49:46 +00:00
|
|
|
g_async_queue_lock(pcap_queue);
|
2013-03-03 17:14:19 +00:00
|
|
|
queue_element = (pcap_queue_element *)g_async_queue_timed_pop_unlocked(pcap_queue, &write_thread_time);
|
2012-02-29 18:04:23 +00:00
|
|
|
#endif
|
2011-05-19 05:49:46 +00:00
|
|
|
if (queue_element) {
|
|
|
|
pcap_queue_bytes -= queue_element->phdr.caplen;
|
|
|
|
pcap_queue_packets -= 1;
|
|
|
|
}
|
|
|
|
g_async_queue_unlock(pcap_queue);
|
2011-05-16 21:56:12 +00:00
|
|
|
if (queue_element) {
|
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO,
|
|
|
|
"Dequeued a packet of length %d captured on interface %d.",
|
2011-06-23 18:54:07 +00:00
|
|
|
queue_element->phdr.caplen, queue_element->pcap_opts->interface_id);
|
2011-05-16 21:56:12 +00:00
|
|
|
|
2011-06-23 18:54:07 +00:00
|
|
|
capture_loop_write_packet_cb((u_char *) queue_element->pcap_opts,
|
2011-05-16 21:56:12 +00:00
|
|
|
&queue_element->phdr,
|
|
|
|
queue_element->pd);
|
|
|
|
g_free(queue_element->pd);
|
|
|
|
g_free(queue_element);
|
|
|
|
inpkts = 1;
|
|
|
|
} else {
|
|
|
|
inpkts = 0;
|
|
|
|
}
|
|
|
|
} else {
|
2011-06-23 18:54:07 +00:00
|
|
|
pcap_opts = g_array_index(global_ld.pcaps, pcap_options *, 0);
|
2011-05-16 21:56:12 +00:00
|
|
|
inpkts = capture_loop_dispatch(&global_ld, errmsg,
|
2011-05-26 18:12:41 +00:00
|
|
|
sizeof(errmsg), pcap_opts);
|
2011-05-16 21:56:12 +00:00
|
|
|
}
|
2011-03-21 16:57:11 +00:00
|
|
|
#ifdef SIGINFO
|
|
|
|
/* Were we asked to print packet counts by the SIGINFO handler? */
|
|
|
|
if (global_ld.report_packet_count) {
|
|
|
|
fprintf(stderr, "%u packet%s captured\n", global_ld.packet_count,
|
|
|
|
plurality(global_ld.packet_count, "", "s"));
|
|
|
|
global_ld.report_packet_count = FALSE;
|
|
|
|
}
|
2010-05-26 00:19:27 +00:00
|
|
|
#endif
|
|
|
|
|
2008-02-16 02:39:58 +00:00
|
|
|
#ifdef _WIN32
|
2011-03-21 16:57:11 +00:00
|
|
|
/* any news from our parent (signal pipe)? -> just stop the capture */
|
|
|
|
if (!signal_pipe_check_running()) {
|
|
|
|
global_ld.go = FALSE;
|
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
#endif
|
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
if (inpkts > 0) {
|
|
|
|
global_ld.inpkts_to_sync_pipe += inpkts;
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
/* check capture size condition */
|
|
|
|
if (cnd_autostop_size != NULL &&
|
2013-08-29 18:15:13 +00:00
|
|
|
cnd_eval(cnd_autostop_size, global_ld.bytes_written)) {
|
2011-03-21 16:57:11 +00:00
|
|
|
/* Capture size limit reached, do we have another file? */
|
|
|
|
if (!do_file_switch_or_stop(capture_opts, cnd_autostop_files,
|
|
|
|
cnd_autostop_size, cnd_file_duration))
|
|
|
|
continue;
|
|
|
|
} /* cnd_autostop_size */
|
|
|
|
if (capture_opts->output_to_pipe) {
|
2012-12-20 20:00:06 +00:00
|
|
|
fflush(global_ld.pdh);
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
|
|
|
} /* inpkts */
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-05-05 20:46:02 +00:00
|
|
|
/* Only update once every 500ms so as not to overload slow displays.
|
|
|
|
* This also prevents too much context-switching between the dumpcap
|
|
|
|
* and wireshark processes.
|
2011-05-05 21:41:51 +00:00
|
|
|
*/
|
2011-03-21 16:57:11 +00:00
|
|
|
#define DUMPCAP_UPD_TIME 500
|
2011-05-05 20:46:02 +00:00
|
|
|
|
2015-09-02 21:35:37 +00:00
|
|
|
#ifdef _WIN32
|
2011-09-07 18:25:54 +00:00
|
|
|
cur_time = GetTickCount(); /* Note: wraps to 0 if sys runs for 49.7 days */
|
|
|
|
if ((cur_time - upd_time) > DUMPCAP_UPD_TIME) { /* wrap just causes an extra update */
|
2008-02-16 02:39:58 +00:00
|
|
|
#else
|
2011-05-05 21:41:51 +00:00
|
|
|
gettimeofday(&cur_time, NULL);
|
2013-12-17 15:02:47 +00:00
|
|
|
if (((guint64)cur_time.tv_sec * 1000000 + cur_time.tv_usec) >
|
|
|
|
((guint64)upd_time.tv_sec * 1000000 + upd_time.tv_usec + DUMPCAP_UPD_TIME*1000)) {
|
2008-02-16 02:39:58 +00:00
|
|
|
#endif
|
2011-05-05 20:46:02 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
upd_time = cur_time;
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
#if 0
|
|
|
|
if (pcap_stats(pch, stats) >= 0) {
|
|
|
|
*stats_known = TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* Let the parent process know. */
|
|
|
|
if (global_ld.inpkts_to_sync_pipe) {
|
|
|
|
/* do sync here */
|
2012-12-20 20:00:06 +00:00
|
|
|
fflush(global_ld.pdh);
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
/* Send our parent a message saying we've written out
|
|
|
|
"global_ld.inpkts_to_sync_pipe" packets to the capture file. */
|
|
|
|
if (!quiet)
|
|
|
|
report_packet_count(global_ld.inpkts_to_sync_pipe);
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
global_ld.inpkts_to_sync_pipe = 0;
|
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
/* check capture duration condition */
|
|
|
|
if (cnd_autostop_duration != NULL && cnd_eval(cnd_autostop_duration)) {
|
|
|
|
/* The maximum capture time has elapsed; stop the capture. */
|
|
|
|
global_ld.go = FALSE;
|
|
|
|
continue;
|
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
/* check capture file duration condition */
|
|
|
|
if (cnd_file_duration != NULL && cnd_eval(cnd_file_duration)) {
|
|
|
|
/* duration limit reached, do we have another file? */
|
|
|
|
if (!do_file_switch_or_stop(capture_opts, cnd_autostop_files,
|
|
|
|
cnd_autostop_size, cnd_file_duration))
|
|
|
|
continue;
|
|
|
|
} /* cnd_file_duration */
|
|
|
|
}
|
2011-05-15 22:54:52 +00:00
|
|
|
}
|
2011-03-21 16:57:11 +00:00
|
|
|
|
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Capture loop stopping ...");
|
2011-05-16 21:56:12 +00:00
|
|
|
if (use_threads) {
|
|
|
|
pcap_queue_element *queue_element;
|
|
|
|
|
|
|
|
for (i = 0; i < global_ld.pcaps->len; i++) {
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts = g_array_index(global_ld.pcaps, pcap_options *, i);
|
2011-05-16 21:56:12 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Waiting for thread of interface %u...",
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts->interface_id);
|
|
|
|
g_thread_join(pcap_opts->tid);
|
2011-05-16 21:56:12 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Thread of interface %u terminated.",
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts->interface_id);
|
2011-05-16 21:56:12 +00:00
|
|
|
}
|
2011-05-19 05:49:46 +00:00
|
|
|
while (1) {
|
|
|
|
g_async_queue_lock(pcap_queue);
|
2013-03-03 17:14:19 +00:00
|
|
|
queue_element = (pcap_queue_element *)g_async_queue_try_pop_unlocked(pcap_queue);
|
2011-05-19 05:49:46 +00:00
|
|
|
if (queue_element) {
|
|
|
|
pcap_queue_bytes -= queue_element->phdr.caplen;
|
|
|
|
pcap_queue_packets -= 1;
|
|
|
|
}
|
|
|
|
g_async_queue_unlock(pcap_queue);
|
|
|
|
if (queue_element == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
2011-05-16 21:56:12 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO,
|
|
|
|
"Dequeued a packet of length %d captured on interface %d.",
|
2011-06-23 18:54:07 +00:00
|
|
|
queue_element->phdr.caplen, queue_element->pcap_opts->interface_id);
|
|
|
|
capture_loop_write_packet_cb((u_char *)queue_element->pcap_opts,
|
2011-05-16 21:56:12 +00:00
|
|
|
&queue_element->phdr,
|
|
|
|
queue_element->pd);
|
|
|
|
g_free(queue_element->pd);
|
|
|
|
g_free(queue_element);
|
|
|
|
global_ld.inpkts_to_sync_pipe += 1;
|
|
|
|
if (capture_opts->output_to_pipe) {
|
2012-12-20 20:00:06 +00:00
|
|
|
fflush(global_ld.pdh);
|
2011-05-16 21:56:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
|
|
|
|
/* delete stop conditions */
|
|
|
|
if (cnd_file_duration != NULL)
|
|
|
|
cnd_delete(cnd_file_duration);
|
|
|
|
if (cnd_autostop_files != NULL)
|
|
|
|
cnd_delete(cnd_autostop_files);
|
|
|
|
if (cnd_autostop_size != NULL)
|
|
|
|
cnd_delete(cnd_autostop_size);
|
|
|
|
if (cnd_autostop_duration != NULL)
|
|
|
|
cnd_delete(cnd_autostop_duration);
|
|
|
|
|
2011-05-26 14:33:55 +00:00
|
|
|
/* did we have a pcap (input) error? */
|
2011-05-15 22:54:52 +00:00
|
|
|
for (i = 0; i < capture_opts->ifaces->len; i++) {
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts = g_array_index(global_ld.pcaps, pcap_options *, i);
|
|
|
|
if (pcap_opts->pcap_err) {
|
2011-05-15 22:54:52 +00:00
|
|
|
/* On Linux, if an interface goes down while you're capturing on it,
|
|
|
|
you'll get a "recvfrom: Network is down" or
|
|
|
|
"The interface went down" error (ENETDOWN).
|
2011-06-28 09:00:11 +00:00
|
|
|
(At least you will if g_strerror() doesn't show a local translation
|
2011-05-15 22:54:52 +00:00
|
|
|
of the error.)
|
|
|
|
|
|
|
|
On FreeBSD and OS X, if a network adapter disappears while
|
|
|
|
you're capturing on it, you'll get a "read: Device not configured"
|
|
|
|
error (ENXIO). (See previous parenthetical note.)
|
|
|
|
|
|
|
|
On OpenBSD, you get "read: I/O error" (EIO) in the same case.
|
|
|
|
|
|
|
|
These should *not* be reported to the Wireshark developers. */
|
|
|
|
char *cap_err_str;
|
|
|
|
|
2011-05-26 18:12:41 +00:00
|
|
|
cap_err_str = pcap_geterr(pcap_opts->pcap_h);
|
2011-05-15 22:54:52 +00:00
|
|
|
if (strcmp(cap_err_str, "recvfrom: Network is down") == 0 ||
|
|
|
|
strcmp(cap_err_str, "The interface went down") == 0 ||
|
|
|
|
strcmp(cap_err_str, "read: Device not configured") == 0 ||
|
|
|
|
strcmp(cap_err_str, "read: I/O error") == 0 ||
|
|
|
|
strcmp(cap_err_str, "read error: PacketReceivePacket failed") == 0) {
|
|
|
|
report_capture_error("The network adapter on which the capture was being done "
|
|
|
|
"is no longer running; the capture has stopped.",
|
|
|
|
"");
|
|
|
|
} else {
|
|
|
|
g_snprintf(errmsg, sizeof(errmsg), "Error while capturing packets: %s",
|
|
|
|
cap_err_str);
|
|
|
|
report_capture_error(errmsg, please_report);
|
|
|
|
}
|
|
|
|
break;
|
2011-05-26 18:12:41 +00:00
|
|
|
} else if (pcap_opts->from_cap_pipe && pcap_opts->cap_pipe_err == PIPERR) {
|
2011-05-15 22:54:52 +00:00
|
|
|
report_capture_error(errmsg, "");
|
|
|
|
break;
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* did we have an output error while capturing? */
|
|
|
|
if (global_ld.err == 0) {
|
|
|
|
write_ok = TRUE;
|
2008-02-16 02:39:58 +00:00
|
|
|
} else {
|
2011-03-21 16:57:11 +00:00
|
|
|
capture_loop_get_errmsg(errmsg, sizeof(errmsg), capture_opts->save_file,
|
|
|
|
global_ld.err, FALSE);
|
|
|
|
report_capture_error(errmsg, please_report);
|
|
|
|
write_ok = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (capture_opts->saving_to_file) {
|
|
|
|
/* close the output file */
|
|
|
|
close_ok = capture_loop_close_output(capture_opts, &global_ld, &err_close);
|
|
|
|
} else
|
|
|
|
close_ok = TRUE;
|
|
|
|
|
|
|
|
/* there might be packets not yet notified to the parent */
|
|
|
|
/* (do this after closing the file, so all packets are already flushed) */
|
2012-11-25 18:35:41 +00:00
|
|
|
if (global_ld.inpkts_to_sync_pipe) {
|
2011-03-21 16:57:11 +00:00
|
|
|
if (!quiet)
|
|
|
|
report_packet_count(global_ld.inpkts_to_sync_pipe);
|
|
|
|
global_ld.inpkts_to_sync_pipe = 0;
|
2008-02-16 02:39:58 +00:00
|
|
|
}
|
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
/* If we've displayed a message about a write error, there's no point
|
|
|
|
in displaying another message about an error on close. */
|
|
|
|
if (!close_ok && write_ok) {
|
|
|
|
capture_loop_get_errmsg(errmsg, sizeof(errmsg), capture_opts->save_file, err_close,
|
|
|
|
TRUE);
|
|
|
|
report_capture_error(errmsg, "");
|
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
/*
|
|
|
|
* XXX We exhibit different behaviour between normal mode and sync mode
|
|
|
|
* when the pipe is stdin and not already at EOF. If we're a child, the
|
|
|
|
* parent's stdin isn't closed, so if the user starts another capture,
|
|
|
|
* cap_pipe_open_live() will very likely not see the expected magic bytes and
|
|
|
|
* will say "Unrecognized libpcap format". On the other hand, in normal
|
|
|
|
* mode, cap_pipe_open_live() will say "End of file on pipe during open".
|
|
|
|
*/
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-05-26 14:33:55 +00:00
|
|
|
report_capture_count(TRUE);
|
2011-03-21 16:57:11 +00:00
|
|
|
|
|
|
|
/* get packet drop statistics from pcap */
|
2011-05-15 22:54:52 +00:00
|
|
|
for (i = 0; i < capture_opts->ifaces->len; i++) {
|
2011-05-26 19:22:16 +00:00
|
|
|
guint32 received;
|
2013-03-10 11:57:40 +00:00
|
|
|
guint32 pcap_dropped = 0;
|
2011-05-26 19:22:16 +00:00
|
|
|
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_opts = g_array_index(global_ld.pcaps, pcap_options *, i);
|
2011-05-15 22:54:52 +00:00
|
|
|
interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
|
2011-05-26 19:22:16 +00:00
|
|
|
received = pcap_opts->received;
|
2011-05-26 18:12:41 +00:00
|
|
|
if (pcap_opts->pcap_h != NULL) {
|
|
|
|
g_assert(!pcap_opts->from_cap_pipe);
|
2011-05-15 22:54:52 +00:00
|
|
|
/* Get the capture statistics, so we know how many packets were dropped. */
|
2013-11-04 19:38:20 +00:00
|
|
|
/*
|
|
|
|
* Older versions of libpcap didn't set ps_ifdrop on some
|
|
|
|
* platforms; initialize it to 0 to handle that.
|
|
|
|
*/
|
|
|
|
stats->ps_ifdrop = 0;
|
2011-05-26 18:12:41 +00:00
|
|
|
if (pcap_stats(pcap_opts->pcap_h, stats) >= 0) {
|
2011-05-15 22:54:52 +00:00
|
|
|
*stats_known = TRUE;
|
|
|
|
/* Let the parent process know. */
|
2013-03-10 11:57:40 +00:00
|
|
|
pcap_dropped += stats->ps_drop;
|
2011-05-15 22:54:52 +00:00
|
|
|
} else {
|
|
|
|
g_snprintf(errmsg, sizeof(errmsg),
|
|
|
|
"Can't get packet-drop statistics: %s",
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_geterr(pcap_opts->pcap_h));
|
2011-05-15 22:54:52 +00:00
|
|
|
report_capture_error(errmsg, please_report);
|
|
|
|
}
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
2013-11-04 19:38:20 +00:00
|
|
|
report_packet_drops(received, pcap_dropped, pcap_opts->dropped, pcap_opts->flushed, stats->ps_ifdrop, interface_opts.console_display_name);
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* close the input file (pcap or capture pipe) */
|
|
|
|
capture_loop_close_input(&global_ld);
|
|
|
|
|
2014-09-24 21:06:23 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Capture loop stopped.");
|
2011-03-21 16:57:11 +00:00
|
|
|
|
|
|
|
/* ok, if the write and the close were successful. */
|
|
|
|
return write_ok && close_ok;
|
2008-02-16 02:39:58 +00:00
|
|
|
|
|
|
|
error:
|
2011-03-21 16:57:11 +00:00
|
|
|
if (capture_opts->multi_files_on) {
|
|
|
|
/* cleanup ringbuffer */
|
|
|
|
ringbuf_error_cleanup();
|
|
|
|
} else {
|
|
|
|
/* We can't use the save file, and we have no FILE * for the stream
|
|
|
|
to close in order to close it, so close the FD directly. */
|
2011-05-15 22:54:52 +00:00
|
|
|
if (global_ld.save_file_fd != -1) {
|
2011-03-21 16:57:11 +00:00
|
|
|
ws_close(global_ld.save_file_fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We couldn't even start the capture, so get rid of the capture
|
|
|
|
file. */
|
2011-05-15 22:54:52 +00:00
|
|
|
if (capture_opts->save_file != NULL) {
|
2011-03-21 16:57:11 +00:00
|
|
|
ws_unlink(capture_opts->save_file);
|
|
|
|
g_free(capture_opts->save_file);
|
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
}
|
2011-03-21 16:57:11 +00:00
|
|
|
capture_opts->save_file = NULL;
|
|
|
|
if (cfilter_error)
|
2011-06-27 11:30:39 +00:00
|
|
|
report_cfilter_error(capture_opts, error_index, errmsg);
|
2011-03-21 16:57:11 +00:00
|
|
|
else
|
|
|
|
report_capture_error(errmsg, secondary_errmsg);
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
/* close the input file (pcap or cap_pipe) */
|
|
|
|
capture_loop_close_input(&global_ld);
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Capture loop stopped with error");
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
return FALSE;
|
2008-02-16 02:39:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-25 18:35:41 +00:00
|
|
|
static void
|
|
|
|
capture_loop_stop(void)
|
2008-02-16 02:39:58 +00:00
|
|
|
{
|
|
|
|
#ifdef HAVE_PCAP_BREAKLOOP
|
2012-11-25 18:35:41 +00:00
|
|
|
guint i;
|
2011-05-26 18:12:41 +00:00
|
|
|
pcap_options *pcap_opts;
|
2011-05-15 22:54:52 +00:00
|
|
|
|
2011-05-28 12:48:21 +00:00
|
|
|
for (i = 0; i < global_ld.pcaps->len; i++) {
|
|
|
|
pcap_opts = g_array_index(global_ld.pcaps, pcap_options *, i);
|
|
|
|
if (pcap_opts->pcap_h != NULL)
|
|
|
|
pcap_breakloop(pcap_opts->pcap_h);
|
2011-05-15 22:54:52 +00:00
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
#endif
|
2011-03-21 16:57:11 +00:00
|
|
|
global_ld.go = FALSE;
|
2008-02-16 02:39:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
capture_loop_get_errmsg(char *errmsg, int errmsglen, const char *fname,
|
2011-03-21 16:57:11 +00:00
|
|
|
int err, gboolean is_close)
|
2008-02-16 02:39:58 +00:00
|
|
|
{
|
2011-03-21 16:57:11 +00:00
|
|
|
switch (err) {
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
case ENOSPC:
|
|
|
|
g_snprintf(errmsg, errmsglen,
|
|
|
|
"Not all the packets could be written to the file"
|
|
|
|
" to which the capture was being saved\n"
|
|
|
|
"(\"%s\") because there is no space left on the file system\n"
|
|
|
|
"on which that file resides.",
|
|
|
|
fname);
|
|
|
|
break;
|
2008-02-16 02:39:58 +00:00
|
|
|
|
|
|
|
#ifdef EDQUOT
|
2011-03-21 16:57:11 +00:00
|
|
|
case EDQUOT:
|
|
|
|
g_snprintf(errmsg, errmsglen,
|
|
|
|
"Not all the packets could be written to the file"
|
|
|
|
" to which the capture was being saved\n"
|
|
|
|
"(\"%s\") because you are too close to, or over,"
|
|
|
|
" your disk quota\n"
|
|
|
|
"on the file system on which that file resides.",
|
|
|
|
fname);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (is_close) {
|
|
|
|
g_snprintf(errmsg, errmsglen,
|
|
|
|
"The file to which the capture was being saved\n"
|
|
|
|
"(\"%s\") could not be closed: %s.",
|
2011-06-28 09:00:11 +00:00
|
|
|
fname, g_strerror(err));
|
2011-03-21 16:57:11 +00:00
|
|
|
} else {
|
|
|
|
g_snprintf(errmsg, errmsglen,
|
|
|
|
"An error occurred while writing to the file"
|
|
|
|
" to which the capture was being saved\n"
|
|
|
|
"(\"%s\"): %s.",
|
2011-06-28 09:00:11 +00:00
|
|
|
fname, g_strerror(err));
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* one packet was captured, process it */
|
|
|
|
static void
|
2011-06-23 18:54:07 +00:00
|
|
|
capture_loop_write_packet_cb(u_char *pcap_opts_p, const struct pcap_pkthdr *phdr,
|
2011-05-16 21:56:12 +00:00
|
|
|
const u_char *pd)
|
2008-02-16 02:39:58 +00:00
|
|
|
{
|
2011-07-08 02:02:37 +00:00
|
|
|
pcap_options *pcap_opts = (pcap_options *) (void *) pcap_opts_p;
|
2012-11-25 18:35:41 +00:00
|
|
|
int err;
|
|
|
|
guint ts_mul = pcap_opts->ts_nsec ? 1000000000 : 1000000;
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
/* We may be called multiple times from pcap_dispatch(); if we've set
|
|
|
|
the "stop capturing" flag, ignore this packet, as we're not
|
|
|
|
supposed to be saving any more packets. */
|
2012-07-25 07:08:20 +00:00
|
|
|
if (!global_ld.go) {
|
2013-03-10 11:57:40 +00:00
|
|
|
pcap_opts->flushed++;
|
2011-03-21 16:57:11 +00:00
|
|
|
return;
|
2012-07-25 07:08:20 +00:00
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
|
2011-05-16 21:56:12 +00:00
|
|
|
if (global_ld.pdh) {
|
2011-03-21 16:57:11 +00:00
|
|
|
gboolean successful;
|
2011-05-16 00:19:47 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
/* We're supposed to write the packet to a file; do so.
|
|
|
|
If this fails, set "ld->go" to FALSE, to stop the capture, and set
|
|
|
|
"ld->err" to the error. */
|
|
|
|
if (global_capture_opts.use_pcapng) {
|
2013-09-29 20:53:13 +00:00
|
|
|
successful = pcapng_write_enhanced_packet_block(global_ld.pdh,
|
|
|
|
NULL,
|
|
|
|
phdr->ts.tv_sec, (gint32)phdr->ts.tv_usec,
|
|
|
|
phdr->caplen, phdr->len,
|
|
|
|
pcap_opts->interface_id,
|
|
|
|
ts_mul,
|
|
|
|
pd, 0,
|
|
|
|
&global_ld.bytes_written, &err);
|
2011-03-21 16:57:11 +00:00
|
|
|
} else {
|
2013-09-29 20:26:25 +00:00
|
|
|
successful = libpcap_write_packet(global_ld.pdh,
|
2013-08-10 21:27:26 +00:00
|
|
|
phdr->ts.tv_sec, (gint32)phdr->ts.tv_usec,
|
2012-12-20 15:20:10 +00:00
|
|
|
phdr->caplen, phdr->len,
|
|
|
|
pd,
|
|
|
|
&global_ld.bytes_written, &err);
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
|
|
|
if (!successful) {
|
2011-05-16 21:56:12 +00:00
|
|
|
global_ld.go = FALSE;
|
|
|
|
global_ld.err = err;
|
2012-07-25 07:08:20 +00:00
|
|
|
pcap_opts->dropped++;
|
2011-03-21 16:57:11 +00:00
|
|
|
} else {
|
2014-05-08 19:32:36 +00:00
|
|
|
#if defined(DEBUG_DUMPCAP) || defined(DEBUG_CHILD_DUMPCAP)
|
2011-05-16 21:56:12 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO,
|
|
|
|
"Wrote a packet of length %d captured on interface %u.",
|
2011-05-26 19:22:16 +00:00
|
|
|
phdr->caplen, pcap_opts->interface_id);
|
2014-05-08 19:32:36 +00:00
|
|
|
#endif
|
2011-05-16 21:56:12 +00:00
|
|
|
global_ld.packet_count++;
|
2012-07-25 07:08:20 +00:00
|
|
|
pcap_opts->received++;
|
2011-03-21 16:57:11 +00:00
|
|
|
/* if the user told us to stop after x packets, do we already have enough? */
|
2011-05-16 21:56:12 +00:00
|
|
|
if ((global_ld.packet_max > 0) && (global_ld.packet_count >= global_ld.packet_max)) {
|
|
|
|
global_ld.go = FALSE;
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-27 08:11:10 +00:00
|
|
|
}
|
2008-02-16 02:39:58 +00:00
|
|
|
}
|
|
|
|
|
2011-05-16 21:56:12 +00:00
|
|
|
/* one packet was captured, queue it */
|
|
|
|
static void
|
2011-06-23 18:54:07 +00:00
|
|
|
capture_loop_queue_packet_cb(u_char *pcap_opts_p, const struct pcap_pkthdr *phdr,
|
2011-05-16 21:56:12 +00:00
|
|
|
const u_char *pd)
|
|
|
|
{
|
2012-11-25 18:35:41 +00:00
|
|
|
pcap_options *pcap_opts = (pcap_options *) (void *) pcap_opts_p;
|
2011-05-16 21:56:12 +00:00
|
|
|
pcap_queue_element *queue_element;
|
2012-11-25 18:35:41 +00:00
|
|
|
gboolean limit_reached;
|
2011-05-16 21:56:12 +00:00
|
|
|
|
|
|
|
/* We may be called multiple times from pcap_dispatch(); if we've set
|
|
|
|
the "stop capturing" flag, ignore this packet, as we're not
|
|
|
|
supposed to be saving any more packets. */
|
2012-04-25 17:43:44 +00:00
|
|
|
if (!global_ld.go) {
|
2013-03-10 11:57:40 +00:00
|
|
|
pcap_opts->flushed++;
|
2011-05-16 21:56:12 +00:00
|
|
|
return;
|
2012-04-25 17:43:44 +00:00
|
|
|
}
|
2011-05-16 21:56:12 +00:00
|
|
|
|
|
|
|
queue_element = (pcap_queue_element *)g_malloc(sizeof(pcap_queue_element));
|
2011-05-19 05:49:46 +00:00
|
|
|
if (queue_element == NULL) {
|
2011-05-26 19:22:16 +00:00
|
|
|
pcap_opts->dropped++;
|
|
|
|
return;
|
2011-05-19 05:49:46 +00:00
|
|
|
}
|
2011-06-23 18:54:07 +00:00
|
|
|
queue_element->pcap_opts = pcap_opts;
|
2011-05-16 21:56:12 +00:00
|
|
|
queue_element->phdr = *phdr;
|
|
|
|
queue_element->pd = (u_char *)g_malloc(phdr->caplen);
|
2011-05-19 05:49:46 +00:00
|
|
|
if (queue_element->pd == NULL) {
|
2011-05-26 19:22:16 +00:00
|
|
|
pcap_opts->dropped++;
|
2011-05-19 05:49:46 +00:00
|
|
|
g_free(queue_element);
|
|
|
|
return;
|
|
|
|
}
|
2011-05-16 21:56:12 +00:00
|
|
|
memcpy(queue_element->pd, pd, phdr->caplen);
|
2011-05-19 05:49:46 +00:00
|
|
|
g_async_queue_lock(pcap_queue);
|
2013-03-10 11:57:40 +00:00
|
|
|
if (((pcap_queue_byte_limit == 0) || (pcap_queue_bytes < pcap_queue_byte_limit)) &&
|
|
|
|
((pcap_queue_packet_limit == 0) || (pcap_queue_packets < pcap_queue_packet_limit))) {
|
2011-05-19 05:49:46 +00:00
|
|
|
limit_reached = FALSE;
|
|
|
|
g_async_queue_push_unlocked(pcap_queue, queue_element);
|
|
|
|
pcap_queue_bytes += phdr->caplen;
|
|
|
|
pcap_queue_packets += 1;
|
|
|
|
} else {
|
|
|
|
limit_reached = TRUE;
|
|
|
|
}
|
|
|
|
g_async_queue_unlock(pcap_queue);
|
|
|
|
if (limit_reached) {
|
2011-05-26 19:22:16 +00:00
|
|
|
pcap_opts->dropped++;
|
2011-05-19 05:49:46 +00:00
|
|
|
g_free(queue_element->pd);
|
|
|
|
g_free(queue_element);
|
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO,
|
|
|
|
"Dropped a packet of length %d captured on interface %u.",
|
2011-05-26 19:22:16 +00:00
|
|
|
phdr->caplen, pcap_opts->interface_id);
|
2011-05-19 05:49:46 +00:00
|
|
|
} else {
|
2011-05-26 19:22:16 +00:00
|
|
|
pcap_opts->received++;
|
2011-05-19 05:49:46 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO,
|
|
|
|
"Queued a packet of length %d captured on interface %u.",
|
2011-05-26 19:22:16 +00:00
|
|
|
phdr->caplen, pcap_opts->interface_id);
|
2011-05-19 05:49:46 +00:00
|
|
|
}
|
|
|
|
/* I don't want to hold the mutex over the debug output. So the
|
|
|
|
output may be wrong */
|
2011-05-16 21:56:12 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO,
|
2011-05-19 05:49:46 +00:00
|
|
|
"Queue size is now %" G_GINT64_MODIFIER "d bytes (%" G_GINT64_MODIFIER "d packets)",
|
|
|
|
pcap_queue_bytes, pcap_queue_packets);
|
2011-05-16 21:56:12 +00:00
|
|
|
}
|
2005-12-01 20:19:30 +00:00
|
|
|
|
2012-06-24 19:45:49 +00:00
|
|
|
static int
|
|
|
|
set_80211_channel(const char *iface, const char *opt)
|
|
|
|
{
|
2016-09-05 13:45:21 +00:00
|
|
|
guint32 freq = 0;
|
2016-02-06 23:40:51 +00:00
|
|
|
int type = -1;
|
2016-09-05 13:45:21 +00:00
|
|
|
guint32 center_freq1 = 0;
|
|
|
|
guint32 center_freq2 = 0;
|
2016-02-06 23:40:51 +00:00
|
|
|
int args;
|
2016-09-05 13:45:21 +00:00
|
|
|
int ret = 0;
|
2012-06-24 19:45:49 +00:00
|
|
|
gchar **options = NULL;
|
2012-11-25 18:35:41 +00:00
|
|
|
|
2016-02-06 23:40:51 +00:00
|
|
|
options = g_strsplit_set(opt, ",", 4);
|
|
|
|
for (args = 0; options[args]; args++);
|
2012-06-24 19:45:49 +00:00
|
|
|
|
|
|
|
if (options[0])
|
2016-09-08 18:40:20 +00:00
|
|
|
freq = get_nonzero_guint32(options[0], "802.11 channel frequency");
|
2012-06-24 19:45:49 +00:00
|
|
|
|
2016-02-06 23:40:51 +00:00
|
|
|
if (args >= 1 && options[1]) {
|
2012-06-24 19:45:49 +00:00
|
|
|
type = ws80211_str_to_chan_type(options[1]);
|
|
|
|
if (type == -1) {
|
2016-09-05 20:45:35 +00:00
|
|
|
cmdarg_err("\"%s\" is not a valid 802.11 channel type", options[1]);
|
2012-06-24 19:45:49 +00:00
|
|
|
ret = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
2016-02-06 23:40:51 +00:00
|
|
|
|
|
|
|
if (args >= 2 && options[2])
|
2016-09-08 18:40:20 +00:00
|
|
|
center_freq1 = get_nonzero_guint32(options[2], "VHT center frequency");
|
2016-02-06 23:40:51 +00:00
|
|
|
|
|
|
|
if (args >= 3 && options[3])
|
2016-09-08 18:40:20 +00:00
|
|
|
center_freq2 = get_nonzero_guint32(options[3], "VHT center frequency 2");
|
2012-06-24 19:45:49 +00:00
|
|
|
|
|
|
|
ret = ws80211_init();
|
|
|
|
if (ret) {
|
|
|
|
cmdarg_err("%d: Failed to init ws80211: %s\n", abs(ret), g_strerror(abs(ret)));
|
|
|
|
ret = 2;
|
|
|
|
goto out;
|
|
|
|
}
|
2016-02-06 23:40:51 +00:00
|
|
|
ret = ws80211_set_freq(iface, freq, type, center_freq1, center_freq2);
|
2012-06-24 19:45:49 +00:00
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
cmdarg_err("%d: Failed to set channel: %s\n", abs(ret), g_strerror(abs(ret)));
|
|
|
|
ret = 2;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (capture_child)
|
2013-07-15 15:51:45 +00:00
|
|
|
pipe_write_block(2, SP_SUCCESS, NULL);
|
2012-06-24 19:45:49 +00:00
|
|
|
|
|
|
|
out:
|
|
|
|
g_strfreev(options);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-07-03 08:45:32 +00:00
|
|
|
static void
|
2014-08-05 21:51:16 +00:00
|
|
|
get_dumpcap_compiled_info(GString *str)
|
2014-07-03 08:45:32 +00:00
|
|
|
{
|
2014-08-05 20:10:44 +00:00
|
|
|
/* Capture libraries */
|
|
|
|
g_string_append(str, ", ");
|
|
|
|
get_compiled_caplibs_version(str);
|
2014-07-03 08:45:32 +00:00
|
|
|
}
|
|
|
|
|
2014-06-23 21:55:56 +00:00
|
|
|
static void
|
|
|
|
get_dumpcap_runtime_info(GString *str)
|
|
|
|
{
|
2014-07-04 07:24:02 +00:00
|
|
|
/* Capture libraries */
|
2014-06-23 21:55:56 +00:00
|
|
|
g_string_append(str, ", ");
|
2014-07-04 07:24:02 +00:00
|
|
|
get_runtime_caplibs_version(str);
|
2014-06-23 21:55:56 +00:00
|
|
|
}
|
|
|
|
|
2005-11-28 08:54:52 +00:00
|
|
|
/* And now our feature presentation... [ fade to music ] */
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2013-01-15 21:54:41 +00:00
|
|
|
GString *comp_info_str;
|
|
|
|
GString *runtime_info_str;
|
2012-11-25 18:35:41 +00:00
|
|
|
int opt;
|
2014-06-20 19:18:48 +00:00
|
|
|
static const struct option long_options[] = {
|
2015-11-21 23:48:27 +00:00
|
|
|
{"help", no_argument, NULL, 'h'},
|
|
|
|
{"version", no_argument, NULL, 'v'},
|
2014-06-20 21:51:10 +00:00
|
|
|
LONGOPT_CAPTURE_COMMON
|
2013-07-27 16:25:08 +00:00
|
|
|
{0, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
2012-11-25 18:35:41 +00:00
|
|
|
gboolean arg_error = FALSE;
|
2005-11-28 08:54:52 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2012-11-25 18:35:41 +00:00
|
|
|
WSADATA wsaData;
|
2009-01-11 18:16:50 +00:00
|
|
|
#else
|
2012-11-25 18:35:41 +00:00
|
|
|
struct sigaction action, oldaction;
|
2007-11-21 22:49:02 +00:00
|
|
|
#endif
|
2005-11-28 08:54:52 +00:00
|
|
|
|
2012-11-25 18:35:41 +00:00
|
|
|
gboolean start_capture = TRUE;
|
|
|
|
gboolean stats_known;
|
|
|
|
struct pcap_stat stats;
|
|
|
|
GLogLevelFlags log_flags;
|
|
|
|
gboolean list_interfaces = FALSE;
|
|
|
|
gboolean list_link_layer_types = FALSE;
|
2010-07-13 23:26:07 +00:00
|
|
|
#ifdef HAVE_BPF_IMAGE
|
2012-11-25 18:35:41 +00:00
|
|
|
gboolean print_bpf_code = FALSE;
|
|
|
|
#endif
|
|
|
|
gboolean set_chan = FALSE;
|
|
|
|
gchar *set_chan_arg = NULL;
|
|
|
|
gboolean machine_readable = FALSE;
|
|
|
|
gboolean print_statistics = FALSE;
|
|
|
|
int status, run_once_args = 0;
|
|
|
|
gint i;
|
|
|
|
guint j;
|
2009-11-26 04:17:18 +00:00
|
|
|
#if defined(__APPLE__) && defined(__LP64__)
|
2012-11-25 18:35:41 +00:00
|
|
|
struct utsname osinfo;
|
2009-11-26 04:17:18 +00:00
|
|
|
#endif
|
2012-11-25 18:35:41 +00:00
|
|
|
GString *str;
|
2005-11-28 08:54:52 +00:00
|
|
|
|
2014-06-29 21:37:21 +00:00
|
|
|
cmdarg_err_init(dumpcap_cmdarg_err, dumpcap_cmdarg_err_cont);
|
|
|
|
|
2014-12-31 06:28:25 +00:00
|
|
|
/* Get the compile-time version information string */
|
|
|
|
comp_info_str = get_compiled_version_info(NULL, get_dumpcap_compiled_info);
|
2013-01-15 21:54:41 +00:00
|
|
|
|
2014-12-31 06:28:25 +00:00
|
|
|
/* Get the run-time version information string */
|
|
|
|
runtime_info_str = get_runtime_version_info(get_dumpcap_runtime_info);
|
2013-01-15 21:54:41 +00:00
|
|
|
|
|
|
|
/* Add it to the information to be reported on a crash. */
|
2014-07-03 02:03:07 +00:00
|
|
|
ws_add_crash_info("Dumpcap (Wireshark) %s\n"
|
2013-01-15 21:54:41 +00:00
|
|
|
"\n"
|
|
|
|
"%s"
|
|
|
|
"\n"
|
|
|
|
"%s",
|
2014-06-22 01:02:27 +00:00
|
|
|
get_ws_vcs_version_info(), comp_info_str->str, runtime_info_str->str);
|
2016-09-11 09:25:51 +00:00
|
|
|
g_string_free(comp_info_str, TRUE);
|
|
|
|
g_string_free(runtime_info_str, TRUE);
|
2013-01-15 21:54:41 +00:00
|
|
|
|
2011-01-06 23:28:58 +00:00
|
|
|
#ifdef _WIN32
|
2011-05-24 00:07:56 +00:00
|
|
|
arg_list_utf_16to8(argc, argv);
|
2013-02-20 01:19:42 +00:00
|
|
|
create_app_running_mutex();
|
2011-01-06 23:28:58 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
/*
|
|
|
|
* Initialize our DLL search path. MUST be called before LoadLibrary
|
|
|
|
* or g_module_open.
|
|
|
|
*/
|
|
|
|
ws_init_dll_search_path();
|
2010-08-25 00:20:50 +00:00
|
|
|
#endif
|
2010-08-24 23:39:29 +00:00
|
|
|
|
2014-06-20 21:31:47 +00:00
|
|
|
#ifdef HAVE_BPF_IMAGE
|
|
|
|
#define OPTSTRING_d "d"
|
|
|
|
#else
|
|
|
|
#define OPTSTRING_d ""
|
|
|
|
#endif
|
|
|
|
|
2007-12-04 11:19:29 +00:00
|
|
|
#ifdef HAVE_PCAP_REMOTE
|
2010-05-07 01:22:33 +00:00
|
|
|
#define OPTSTRING_r "r"
|
|
|
|
#define OPTSTRING_u "u"
|
|
|
|
#else
|
|
|
|
#define OPTSTRING_r ""
|
|
|
|
#define OPTSTRING_u ""
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_PCAP_SETSAMPLING
|
|
|
|
#define OPTSTRING_m "m:"
|
2007-12-04 11:19:29 +00:00
|
|
|
#else
|
2010-05-07 01:22:33 +00:00
|
|
|
#define OPTSTRING_m ""
|
2007-12-04 11:19:29 +00:00
|
|
|
#endif
|
2005-11-28 08:54:52 +00:00
|
|
|
|
2014-06-20 20:59:45 +00:00
|
|
|
#define OPTSTRING OPTSTRING_CAPTURE_COMMON "C:" OPTSTRING_d "gh" "k:" OPTSTRING_m "MN:nPq" OPTSTRING_r "St" OPTSTRING_u "vw:Z:"
|
2005-11-28 08:54:52 +00:00
|
|
|
|
2008-02-23 19:59:38 +00:00
|
|
|
#ifdef DEBUG_CHILD_DUMPCAP
|
2011-03-21 16:57:11 +00:00
|
|
|
if ((debug_log = ws_fopen("dumpcap_debug_log.tmp","w")) == NULL) {
|
2014-09-24 21:06:23 +00:00
|
|
|
fprintf (stderr, "Unable to open debug log file .\n");
|
2011-03-21 16:57:11 +00:00
|
|
|
exit (1);
|
|
|
|
}
|
2008-02-23 19:59:38 +00:00
|
|
|
#endif
|
|
|
|
|
2009-11-26 04:17:18 +00:00
|
|
|
#if defined(__APPLE__) && defined(__LP64__)
|
|
|
|
/*
|
2011-03-21 16:57:11 +00:00
|
|
|
* Is this Mac OS X 10.6.0, 10.6.1, 10.6.3, or 10.6.4? If so, we need
|
|
|
|
* a bug workaround - timeouts less than 1 second don't work with libpcap
|
|
|
|
* in 64-bit code. (The bug was introduced in 10.6, fixed in 10.6.2,
|
|
|
|
* re-introduced in 10.6.3, not fixed in 10.6.4, and fixed in 10.6.5.
|
|
|
|
* The problem is extremely unlikely to be reintroduced in a future
|
|
|
|
* release.)
|
|
|
|
*/
|
|
|
|
if (uname(&osinfo) == 0) {
|
|
|
|
/*
|
|
|
|
* Mac OS X 10.x uses Darwin {x+4}.0.0. Mac OS X 10.x.y uses Darwin
|
|
|
|
* {x+4}.y.0 (except that 10.6.1 appears to have a uname version
|
|
|
|
* number of 10.0.0, not 10.1.0 - go figure).
|
|
|
|
*/
|
|
|
|
if (strcmp(osinfo.release, "10.0.0") == 0 || /* 10.6, 10.6.1 */
|
|
|
|
strcmp(osinfo.release, "10.3.0") == 0 || /* 10.6.3 */
|
|
|
|
strcmp(osinfo.release, "10.4.0") == 0) /* 10.6.4 */
|
|
|
|
need_timeout_workaround = TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine if dumpcap is being requested to run in a special
|
|
|
|
* capture_child mode by going thru the command line args to see if
|
|
|
|
* a -Z is present. (-Z is a hidden option).
|
|
|
|
*
|
|
|
|
* The primary result of running in capture_child mode is that
|
|
|
|
* all messages sent out on stderr are in a special type/len/string
|
|
|
|
* format to allow message processing by type. These messages include
|
|
|
|
* error messages if dumpcap fails to start the operation it was
|
|
|
|
* requested to do, as well as various "status" messages which are sent
|
|
|
|
* when an actual capture is in progress, and a "success" message sent
|
|
|
|
* if dumpcap was requested to perform an operation other than a
|
|
|
|
* capture.
|
|
|
|
*
|
|
|
|
* Capture_child mode would normally be requested by a parent process
|
|
|
|
* which invokes dumpcap and obtains dumpcap stderr output via a pipe
|
|
|
|
* to which dumpcap stderr has been redirected. It might also have
|
|
|
|
* another pipe to obtain dumpcap stdout output; for operations other
|
|
|
|
* than a capture, that information is formatted specially for easier
|
|
|
|
* parsing by the parent process.
|
|
|
|
*
|
|
|
|
* Capture_child mode needs to be determined immediately upon
|
|
|
|
* startup so that any messages generated by dumpcap in this mode
|
|
|
|
* (eg: during initialization) will be formatted properly.
|
2009-11-26 04:17:18 +00:00
|
|
|
*/
|
2011-03-21 16:57:11 +00:00
|
|
|
|
|
|
|
for (i=1; i<argc; i++) {
|
|
|
|
if (strcmp("-Z", argv[i]) == 0) {
|
2012-11-25 18:35:41 +00:00
|
|
|
capture_child = TRUE;
|
2011-03-21 16:57:11 +00:00
|
|
|
machine_readable = TRUE; /* request machine-readable output */
|
2008-02-23 19:59:38 +00:00
|
|
|
#ifdef _WIN32
|
2011-03-21 16:57:11 +00:00
|
|
|
/* set output pipe to binary mode, to avoid ugly text conversions */
|
|
|
|
_setmode(2, O_BINARY);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The default_log_handler will use stdout, which makes trouble in */
|
2013-02-26 01:43:39 +00:00
|
|
|
/* capture child mode, as it uses stdout for its sync_pipe. */
|
2011-03-21 16:57:11 +00:00
|
|
|
/* So: the filtering is done in the console_log_handler and not here.*/
|
|
|
|
/* We set the log handlers right up front to make sure that any log */
|
|
|
|
/* messages when running as child will be sent back to the parent */
|
|
|
|
/* with the correct format. */
|
|
|
|
|
|
|
|
log_flags =
|
2013-03-03 17:14:19 +00:00
|
|
|
(GLogLevelFlags)(
|
2011-03-21 16:57:11 +00:00
|
|
|
G_LOG_LEVEL_ERROR|
|
|
|
|
G_LOG_LEVEL_CRITICAL|
|
|
|
|
G_LOG_LEVEL_WARNING|
|
|
|
|
G_LOG_LEVEL_MESSAGE|
|
|
|
|
G_LOG_LEVEL_INFO|
|
|
|
|
G_LOG_LEVEL_DEBUG|
|
2013-03-03 17:14:19 +00:00
|
|
|
G_LOG_FLAG_FATAL|
|
|
|
|
G_LOG_FLAG_RECURSION);
|
2011-03-21 16:57:11 +00:00
|
|
|
|
|
|
|
g_log_set_handler(NULL,
|
|
|
|
log_flags,
|
|
|
|
console_log_handler, NULL /* user_data */);
|
|
|
|
g_log_set_handler(LOG_DOMAIN_MAIN,
|
|
|
|
log_flags,
|
|
|
|
console_log_handler, NULL /* user_data */);
|
|
|
|
g_log_set_handler(LOG_DOMAIN_CAPTURE,
|
|
|
|
log_flags,
|
|
|
|
console_log_handler, NULL /* user_data */);
|
|
|
|
g_log_set_handler(LOG_DOMAIN_CAPTURE_CHILD,
|
|
|
|
log_flags,
|
|
|
|
console_log_handler, NULL /* user_data */);
|
2008-02-23 19:59:38 +00:00
|
|
|
|
2011-05-27 06:52:32 +00:00
|
|
|
/* Initialize the pcaps list */
|
2011-05-28 12:48:21 +00:00
|
|
|
global_ld.pcaps = g_array_new(FALSE, FALSE, sizeof(pcap_options *));
|
2011-05-27 06:52:32 +00:00
|
|
|
|
2011-11-09 23:43:50 +00:00
|
|
|
#if !GLIB_CHECK_VERSION(2,31,0)
|
2011-05-12 18:55:24 +00:00
|
|
|
/* Initialize the thread system */
|
2011-11-07 23:42:53 +00:00
|
|
|
g_thread_init(NULL);
|
2011-11-09 23:43:50 +00:00
|
|
|
#endif
|
2011-11-07 23:42:53 +00:00
|
|
|
|
2005-11-28 08:54:52 +00:00
|
|
|
#ifdef _WIN32
|
2011-03-21 16:57:11 +00:00
|
|
|
/* Load wpcap if possible. Do this before collecting the run-time version information */
|
|
|
|
load_wpcap();
|
2005-11-28 08:54:52 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
/* ... and also load the packet.dll from wpcap */
|
|
|
|
/* XXX - currently not required, may change later. */
|
|
|
|
/*wpcap_packet_load();*/
|
2005-11-28 08:54:52 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
/* Start windows sockets */
|
|
|
|
WSAStartup( MAKEWORD( 1, 1 ), &wsaData );
|
2005-11-28 08:54:52 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
/* Set handler for Ctrl+C key */
|
|
|
|
SetConsoleCtrlHandler(capture_cleanup_handler, TRUE);
|
2007-11-21 22:49:02 +00:00
|
|
|
#else
|
2011-03-21 16:57:11 +00:00
|
|
|
/* Catch SIGINT and SIGTERM and, if we get either of them, clean up
|
2012-05-18 04:06:35 +00:00
|
|
|
and exit. Do the same with SIGPIPE, in case, for example,
|
|
|
|
we're writing to our standard output and it's a pipe.
|
|
|
|
Do the same with SIGHUP if it's not being ignored (if we're
|
|
|
|
being run under nohup, it might be ignored, in which case we
|
|
|
|
should leave it ignored).
|
|
|
|
|
|
|
|
XXX - apparently, Coverity complained that part of action
|
|
|
|
wasn't initialized. Perhaps it's running on Linux, where
|
|
|
|
struct sigaction has an ignored "sa_restorer" element and
|
|
|
|
where "sa_handler" and "sa_sigaction" might not be two
|
|
|
|
members of a union. */
|
|
|
|
memset(&action, 0, sizeof(action));
|
2011-03-21 16:57:11 +00:00
|
|
|
action.sa_handler = capture_cleanup_handler;
|
|
|
|
/*
|
|
|
|
* Arrange that system calls not get restarted, because when
|
|
|
|
* our signal handler returns we don't want to restart
|
|
|
|
* a call that was waiting for packets to arrive.
|
|
|
|
*/
|
|
|
|
action.sa_flags = 0;
|
|
|
|
sigemptyset(&action.sa_mask);
|
|
|
|
sigaction(SIGTERM, &action, NULL);
|
|
|
|
sigaction(SIGINT, &action, NULL);
|
|
|
|
sigaction(SIGPIPE, &action, NULL);
|
|
|
|
sigaction(SIGHUP, NULL, &oldaction);
|
|
|
|
if (oldaction.sa_handler == SIG_DFL)
|
|
|
|
sigaction(SIGHUP, &action, NULL);
|
2010-05-26 00:19:27 +00:00
|
|
|
|
|
|
|
#ifdef SIGINFO
|
2011-03-21 16:57:11 +00:00
|
|
|
/* Catch SIGINFO and, if we get it and we're capturing in
|
|
|
|
quiet mode, report the number of packets we've captured. */
|
|
|
|
action.sa_handler = report_counts_siginfo;
|
|
|
|
action.sa_flags = SA_RESTART;
|
|
|
|
sigemptyset(&action.sa_mask);
|
|
|
|
sigaction(SIGINFO, &action, NULL);
|
2010-05-26 00:19:27 +00:00
|
|
|
#endif /* SIGINFO */
|
2005-11-28 08:54:52 +00:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
2013-08-22 17:36:30 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
enable_kernel_bpf_jit_compiler();
|
|
|
|
#endif
|
2013-08-22 17:20:09 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
/* ----------------------------------------------------------------- */
|
|
|
|
/* Privilege and capability handling */
|
|
|
|
/* Cases: */
|
|
|
|
/* 1. Running not as root or suid root; no special capabilities. */
|
|
|
|
/* Action: none */
|
|
|
|
/* */
|
|
|
|
/* 2. Running logged in as root (euid=0; ruid=0); Not using libcap. */
|
|
|
|
/* Action: none */
|
|
|
|
/* */
|
|
|
|
/* 3. Running logged in as root (euid=0; ruid=0). Using libcap. */
|
|
|
|
/* Action: */
|
|
|
|
/* - Near start of program: Enable NET_RAW and NET_ADMIN */
|
|
|
|
/* capabilities; Drop all other capabilities; */
|
|
|
|
/* - If not -w (ie: doing -S or -D, etc) run to completion; */
|
|
|
|
/* else: after pcap_open_live() in capture_loop_open_input() */
|
|
|
|
/* drop all capabilities (NET_RAW and NET_ADMIN); */
|
|
|
|
/* (Note: this means that the process, although logged in */
|
|
|
|
/* as root, does not have various permissions such as the */
|
|
|
|
/* ability to bypass file access permissions). */
|
|
|
|
/* XXX: Should we just leave capabilities alone in this case */
|
|
|
|
/* so that user gets expected effect that root can do */
|
|
|
|
/* anything ?? */
|
|
|
|
/* */
|
|
|
|
/* 4. Running as suid root (euid=0, ruid=n); Not using libcap. */
|
|
|
|
/* Action: */
|
|
|
|
/* - If not -w (ie: doing -S or -D, etc) run to completion; */
|
|
|
|
/* else: after pcap_open_live() in capture_loop_open_input() */
|
|
|
|
/* drop suid root (set euid=ruid).(ie: keep suid until after */
|
|
|
|
/* pcap_open_live). */
|
|
|
|
/* */
|
|
|
|
/* 5. Running as suid root (euid=0, ruid=n); Using libcap. */
|
|
|
|
/* Action: */
|
|
|
|
/* - Near start of program: Enable NET_RAW and NET_ADMIN */
|
|
|
|
/* capabilities; Drop all other capabilities; */
|
|
|
|
/* Drop suid privileges (euid=ruid); */
|
|
|
|
/* - If not -w (ie: doing -S or -D, etc) run to completion; */
|
|
|
|
/* else: after pcap_open_live() in capture_loop_open_input() */
|
|
|
|
/* drop all capabilities (NET_RAW and NET_ADMIN). */
|
|
|
|
/* */
|
|
|
|
/* XXX: For some Linux versions/distros with capabilities */
|
|
|
|
/* a 'normal' process with any capabilities cannot be */
|
|
|
|
/* 'killed' (signaled) from another (same uid) non-privileged */
|
|
|
|
/* process. */
|
|
|
|
/* For example: If (non-suid) Wireshark forks a */
|
|
|
|
/* child suid dumpcap which acts as described here (case 5), */
|
|
|
|
/* Wireshark will be unable to kill (signal) the child */
|
|
|
|
/* dumpcap process until the capabilities have been dropped */
|
|
|
|
/* (after pcap_open_live()). */
|
|
|
|
/* This behaviour will apparently be changed in the kernel */
|
|
|
|
/* to allow the kill (signal) in this case. */
|
|
|
|
/* See the following for details: */
|
2015-03-10 17:46:50 +00:00
|
|
|
/* https://www.mail-archive.com/ [wrapped] */
|
2011-03-21 16:57:11 +00:00
|
|
|
/* linux-security-module@vger.kernel.org/msg02913.html */
|
|
|
|
/* */
|
|
|
|
/* It is therefore conceivable that if dumpcap somehow hangs */
|
|
|
|
/* in pcap_open_live or before that wireshark will not */
|
|
|
|
/* be able to stop dumpcap using a signal (INT, TERM, etc). */
|
|
|
|
/* In this case, exiting wireshark will kill the child */
|
|
|
|
/* dumpcap process. */
|
|
|
|
/* */
|
|
|
|
/* 6. Not root or suid root; Running with NET_RAW & NET_ADMIN */
|
|
|
|
/* capabilities; Using libcap. Note: capset cmd (which see) */
|
|
|
|
/* used to assign capabilities to file. */
|
|
|
|
/* Action: */
|
|
|
|
/* - If not -w (ie: doing -S or -D, etc) run to completion; */
|
|
|
|
/* else: after pcap_open_live() in capture_loop_open_input() */
|
|
|
|
/* drop all capabilities (NET_RAW and NET_ADMIN) */
|
|
|
|
/* */
|
|
|
|
/* ToDo: -S (stats) should drop privileges/capabilities when no */
|
|
|
|
/* longer required (similar to capture). */
|
|
|
|
/* */
|
|
|
|
/* ----------------------------------------------------------------- */
|
|
|
|
|
|
|
|
init_process_policies();
|
2008-03-22 19:04:26 +00:00
|
|
|
|
2008-02-16 03:18:36 +00:00
|
|
|
#ifdef HAVE_LIBCAP
|
2011-03-21 16:57:11 +00:00
|
|
|
/* If 'started with special privileges' (and using libcap) */
|
|
|
|
/* Set to keep only NET_RAW and NET_ADMIN capabilities; */
|
|
|
|
/* Set euid/egid = ruid/rgid to remove suid privileges */
|
|
|
|
relinquish_privs_except_capture();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Set the initial values in the capture options. This might be overwritten
|
|
|
|
by the command line parameters. */
|
2013-05-22 07:44:28 +00:00
|
|
|
capture_opts_init(&global_capture_opts);
|
2011-03-21 16:57:11 +00:00
|
|
|
/* We always save to a file - if no file was specified, we save to a
|
|
|
|
temporary file. */
|
|
|
|
global_capture_opts.saving_to_file = TRUE;
|
|
|
|
global_capture_opts.has_ring_num_files = TRUE;
|
|
|
|
|
2014-06-20 19:18:48 +00:00
|
|
|
/* Pass on capture_child mode for capture_opts */
|
|
|
|
global_capture_opts.capture_child = capture_child;
|
2013-06-04 04:36:19 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
/* Now get our args */
|
2013-07-27 16:25:08 +00:00
|
|
|
while ((opt = getopt_long(argc, argv, OPTSTRING, long_options, NULL)) != -1) {
|
2011-03-21 16:57:11 +00:00
|
|
|
switch (opt) {
|
|
|
|
case 'h': /* Print help and exit */
|
2014-07-03 02:29:21 +00:00
|
|
|
printf("Dumpcap (Wireshark) %s\n"
|
|
|
|
"Capture network packets and dump them into a pcapng or pcap file.\n"
|
2015-03-10 17:46:50 +00:00
|
|
|
"See https://www.wireshark.org for more information.\n",
|
2014-07-03 02:29:21 +00:00
|
|
|
get_ws_vcs_version_info());
|
|
|
|
print_usage(stdout);
|
2011-03-21 16:57:11 +00:00
|
|
|
exit_main(0);
|
|
|
|
break;
|
|
|
|
case 'v': /* Show version and exit */
|
2016-09-11 09:25:51 +00:00
|
|
|
comp_info_str = get_compiled_version_info(NULL, get_dumpcap_compiled_info);
|
|
|
|
runtime_info_str = get_runtime_version_info(get_dumpcap_runtime_info);
|
2014-12-23 21:58:38 +00:00
|
|
|
show_version("Dumpcap (Wireshark)", comp_info_str, runtime_info_str);
|
2011-03-21 16:57:11 +00:00
|
|
|
g_string_free(comp_info_str, TRUE);
|
|
|
|
g_string_free(runtime_info_str, TRUE);
|
|
|
|
exit_main(0);
|
|
|
|
break;
|
|
|
|
/*** capture option specific ***/
|
|
|
|
case 'a': /* autostop criteria */
|
|
|
|
case 'b': /* Ringbuffer option */
|
|
|
|
case 'c': /* Capture x packets */
|
|
|
|
case 'f': /* capture filter */
|
2012-12-12 03:25:35 +00:00
|
|
|
case 'g': /* enable group read access on file(s) */
|
2011-03-21 16:57:11 +00:00
|
|
|
case 'i': /* Use interface x */
|
|
|
|
case 'n': /* Use pcapng format */
|
|
|
|
case 'p': /* Don't capture in promiscuous mode */
|
2011-06-17 17:52:31 +00:00
|
|
|
case 'P': /* Use pcap format */
|
2011-03-21 16:57:11 +00:00
|
|
|
case 's': /* Set the snapshot (capture) length */
|
|
|
|
case 'w': /* Write to capture file x */
|
|
|
|
case 'y': /* Set the pcap data link type */
|
2013-07-27 16:25:08 +00:00
|
|
|
case LONGOPT_NUM_CAP_COMMENT: /* add a capture comment */
|
2007-12-04 11:19:29 +00:00
|
|
|
#ifdef HAVE_PCAP_REMOTE
|
2011-03-21 16:57:11 +00:00
|
|
|
case 'u': /* Use UDP for data transfer */
|
|
|
|
case 'r': /* Capture own RPCAP traffic too */
|
|
|
|
case 'A': /* Authentication */
|
2007-12-04 11:19:29 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_PCAP_SETSAMPLING
|
2011-03-21 16:57:11 +00:00
|
|
|
case 'm': /* Sampling */
|
2007-12-04 11:19:29 +00:00
|
|
|
#endif
|
2015-03-25 03:22:00 +00:00
|
|
|
#ifdef CAN_SET_CAPTURE_BUFFER_SIZE
|
2011-03-21 16:57:11 +00:00
|
|
|
case 'B': /* Buffer size */
|
2015-03-25 03:22:00 +00:00
|
|
|
#endif
|
2010-05-07 01:22:33 +00:00
|
|
|
#ifdef HAVE_PCAP_CREATE
|
2011-03-21 16:57:11 +00:00
|
|
|
case 'I': /* Monitor mode */
|
2010-05-07 01:22:33 +00:00
|
|
|
#endif
|
2011-03-21 16:57:11 +00:00
|
|
|
status = capture_opts_add_opt(&global_capture_opts, opt, optarg, &start_capture);
|
2012-11-25 18:35:41 +00:00
|
|
|
if (status != 0) {
|
2011-03-21 16:57:11 +00:00
|
|
|
exit_main(status);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
/*** hidden option: Wireshark child mode (using binary output messages) ***/
|
|
|
|
case 'Z':
|
|
|
|
capture_child = TRUE;
|
2006-03-13 00:30:51 +00:00
|
|
|
#ifdef _WIN32
|
2011-03-21 16:57:11 +00:00
|
|
|
/* set output pipe to binary mode, to avoid ugly text conversions */
|
|
|
|
_setmode(2, O_BINARY);
|
|
|
|
/*
|
|
|
|
* optarg = the control ID, aka the PPID, currently used for the
|
|
|
|
* signal pipe name.
|
|
|
|
*/
|
|
|
|
if (strcmp(optarg, SIGNAL_PIPE_CTRL_ID_NONE) != 0) {
|
|
|
|
sig_pipe_name = g_strdup_printf(SIGNAL_PIPE_FORMAT, optarg);
|
|
|
|
sig_pipe_handle = CreateFile(utf_8to16(sig_pipe_name),
|
|
|
|
GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
|
|
|
|
|
|
|
|
if (sig_pipe_handle == INVALID_HANDLE_VALUE) {
|
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO,
|
|
|
|
"Signal pipe: Unable to open %s. Dead parent?",
|
|
|
|
sig_pipe_name);
|
|
|
|
exit_main(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'q': /* Quiet */
|
|
|
|
quiet = TRUE;
|
|
|
|
break;
|
2011-05-16 21:56:12 +00:00
|
|
|
case 't':
|
|
|
|
use_threads = TRUE;
|
|
|
|
break;
|
2011-03-21 16:57:11 +00:00
|
|
|
/*** all non capture option specific ***/
|
|
|
|
case 'D': /* Print a list of capture devices and exit */
|
2015-03-17 20:11:12 +00:00
|
|
|
if (!list_interfaces) {
|
|
|
|
list_interfaces = TRUE;
|
|
|
|
run_once_args++;
|
|
|
|
}
|
2011-03-21 16:57:11 +00:00
|
|
|
break;
|
|
|
|
case 'L': /* Print list of link-layer types and exit */
|
2015-03-17 20:11:12 +00:00
|
|
|
if (!list_link_layer_types) {
|
|
|
|
list_link_layer_types = TRUE;
|
|
|
|
run_once_args++;
|
|
|
|
}
|
2011-03-21 16:57:11 +00:00
|
|
|
break;
|
|
|
|
#ifdef HAVE_BPF_IMAGE
|
|
|
|
case 'd': /* Print BPF code for capture filter and exit */
|
2015-03-17 20:11:12 +00:00
|
|
|
if (!print_bpf_code) {
|
|
|
|
print_bpf_code = TRUE;
|
|
|
|
run_once_args++;
|
|
|
|
}
|
2011-03-21 16:57:11 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case 'S': /* Print interface statistics once a second */
|
2015-03-17 20:11:12 +00:00
|
|
|
if (!print_statistics) {
|
|
|
|
print_statistics = TRUE;
|
|
|
|
run_once_args++;
|
|
|
|
}
|
2011-03-21 16:57:11 +00:00
|
|
|
break;
|
2012-06-24 19:45:49 +00:00
|
|
|
case 'k': /* Set wireless channel */
|
2015-03-17 20:11:12 +00:00
|
|
|
if (!set_chan) {
|
|
|
|
set_chan = TRUE;
|
|
|
|
set_chan_arg = optarg;
|
|
|
|
run_once_args++;
|
|
|
|
} else {
|
|
|
|
cmdarg_err("Only one -k flag may be specified");
|
|
|
|
arg_error = TRUE;
|
|
|
|
}
|
|
|
|
break;
|
2011-03-21 16:57:11 +00:00
|
|
|
case 'M': /* For -D, -L, and -S, print machine-readable output */
|
|
|
|
machine_readable = TRUE;
|
|
|
|
break;
|
2013-03-10 11:57:40 +00:00
|
|
|
case 'C':
|
|
|
|
pcap_queue_byte_limit = get_positive_int(optarg, "byte_limit");
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
pcap_queue_packet_limit = get_positive_int(optarg, "packet_limit");
|
|
|
|
break;
|
2011-03-21 16:57:11 +00:00
|
|
|
default:
|
|
|
|
cmdarg_err("Invalid Option: %s", argv[optind-1]);
|
2011-08-29 21:36:48 +00:00
|
|
|
/* FALLTHROUGH */
|
2011-05-31 14:45:55 +00:00
|
|
|
case '?': /* Bad flag - print usage message */
|
2011-03-21 16:57:11 +00:00
|
|
|
arg_error = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-05-31 14:45:55 +00:00
|
|
|
if (!arg_error) {
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
if (argc >= 1) {
|
|
|
|
/* user specified file name as regular command-line argument */
|
|
|
|
/* XXX - use it as the capture file name (or something else)? */
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
if (argc != 0) {
|
|
|
|
/*
|
|
|
|
* Extra command line arguments were specified; complain.
|
|
|
|
* XXX - interpret as capture filter, as tcpdump and tshark do?
|
|
|
|
*/
|
|
|
|
cmdarg_err("Invalid argument: %s", argv[0]);
|
|
|
|
arg_error = TRUE;
|
|
|
|
}
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
|
|
|
|
2013-03-10 11:57:40 +00:00
|
|
|
if ((pcap_queue_byte_limit > 0) || (pcap_queue_packet_limit > 0)) {
|
|
|
|
use_threads = TRUE;
|
|
|
|
}
|
|
|
|
if ((pcap_queue_byte_limit == 0) && (pcap_queue_packet_limit == 0)) {
|
|
|
|
/* Use some default if the user hasn't specified some */
|
|
|
|
/* XXX: Are these defaults good enough? */
|
|
|
|
pcap_queue_byte_limit = 1000 * 1000;
|
|
|
|
pcap_queue_packet_limit = 1000;
|
|
|
|
}
|
2011-03-21 16:57:11 +00:00
|
|
|
if (arg_error) {
|
2014-07-03 02:29:21 +00:00
|
|
|
print_usage(stderr);
|
2011-03-21 16:57:11 +00:00
|
|
|
exit_main(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (run_once_args > 1) {
|
2015-03-16 20:57:37 +00:00
|
|
|
#ifdef HAVE_BPF_IMAGE
|
|
|
|
cmdarg_err("Only one of -D, -L, -d, -k, or -S may be supplied.");
|
|
|
|
#else
|
|
|
|
cmdarg_err("Only one of -D, -L, -k, or -S may be supplied.");
|
|
|
|
#endif
|
2011-03-21 16:57:11 +00:00
|
|
|
exit_main(1);
|
|
|
|
} else if (run_once_args == 1) {
|
|
|
|
/* We're supposed to print some information, rather than
|
|
|
|
to capture traffic; did they specify a ring buffer option? */
|
|
|
|
if (global_capture_opts.multi_files_on) {
|
|
|
|
cmdarg_err("Ring buffer requested, but a capture isn't being done.");
|
2008-03-22 19:04:26 +00:00
|
|
|
exit_main(1);
|
|
|
|
}
|
2011-03-21 16:57:11 +00:00
|
|
|
} else {
|
2011-05-21 18:51:25 +00:00
|
|
|
/* We're supposed to capture traffic; */
|
2013-07-27 16:16:57 +00:00
|
|
|
|
2013-07-27 16:48:06 +00:00
|
|
|
/* Are we capturing on multiple interface? If so, use threads and pcapng. */
|
|
|
|
if (global_capture_opts.ifaces->len > 1) {
|
|
|
|
use_threads = TRUE;
|
|
|
|
global_capture_opts.use_pcapng = TRUE;
|
|
|
|
}
|
|
|
|
|
2013-07-27 16:16:57 +00:00
|
|
|
if (global_capture_opts.capture_comment &&
|
|
|
|
(!global_capture_opts.use_pcapng || global_capture_opts.multi_files_on)) {
|
|
|
|
/* XXX - for ringbuffer, should we apply the comment to each file? */
|
|
|
|
cmdarg_err("A capture comment can only be set if we capture into a single pcapng file.");
|
|
|
|
exit_main(1);
|
|
|
|
}
|
|
|
|
|
2011-05-21 18:51:25 +00:00
|
|
|
/* Was the ring buffer option specified and, if so, does it make sense? */
|
2011-03-21 16:57:11 +00:00
|
|
|
if (global_capture_opts.multi_files_on) {
|
|
|
|
/* Ring buffer works only under certain conditions:
|
|
|
|
a) ring buffer does not work with temporary files;
|
|
|
|
b) it makes no sense to enable the ring buffer if the maximum
|
|
|
|
file size is set to "infinite". */
|
|
|
|
if (global_capture_opts.save_file == NULL) {
|
|
|
|
cmdarg_err("Ring buffer requested, but capture isn't being saved to a permanent file.");
|
|
|
|
global_capture_opts.multi_files_on = FALSE;
|
|
|
|
}
|
|
|
|
if (!global_capture_opts.has_autostop_filesize && !global_capture_opts.has_file_duration) {
|
|
|
|
cmdarg_err("Ring buffer requested, but no maximum capture file size or duration were specified.");
|
|
|
|
#if 0
|
|
|
|
/* XXX - this must be redesigned as the conditions changed */
|
|
|
|
global_capture_opts.multi_files_on = FALSE;
|
2006-03-13 00:30:51 +00:00
|
|
|
#endif
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-11-28 08:54:52 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
/*
|
|
|
|
* "-D" requires no interface to be selected; it's supposed to list
|
|
|
|
* all interfaces.
|
|
|
|
*/
|
|
|
|
if (list_interfaces) {
|
|
|
|
/* Get the list of interfaces */
|
2012-11-25 18:35:41 +00:00
|
|
|
GList *if_list;
|
|
|
|
int err;
|
|
|
|
gchar *err_str;
|
2011-03-21 16:57:11 +00:00
|
|
|
|
2013-06-27 17:10:50 +00:00
|
|
|
if_list = capture_interface_list(&err, &err_str,NULL);
|
2011-03-21 16:57:11 +00:00
|
|
|
if (if_list == NULL) {
|
2014-07-22 23:53:18 +00:00
|
|
|
if (err == 0) {
|
2011-03-21 16:57:11 +00:00
|
|
|
/*
|
|
|
|
* If we're being run by another program, just give them
|
|
|
|
* an empty list of interfaces, don't report this as
|
|
|
|
* an error; that lets them decide whether to report
|
|
|
|
* this as an error or not.
|
|
|
|
*/
|
|
|
|
if (!machine_readable) {
|
|
|
|
cmdarg_err("There are no interfaces on which a capture can be done");
|
|
|
|
exit_main(2);
|
|
|
|
}
|
2014-07-22 23:53:18 +00:00
|
|
|
} else {
|
|
|
|
cmdarg_err("%s", err_str);
|
|
|
|
g_free(err_str);
|
|
|
|
exit_main(2);
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (machine_readable) /* tab-separated values to stdout */
|
|
|
|
print_machine_readable_interfaces(if_list);
|
|
|
|
else
|
|
|
|
capture_opts_print_interfaces(if_list);
|
|
|
|
free_interface_list(if_list);
|
|
|
|
exit_main(0);
|
2005-11-28 08:54:52 +00:00
|
|
|
}
|
2011-03-21 16:57:11 +00:00
|
|
|
|
2005-11-28 08:54:52 +00:00
|
|
|
/*
|
2011-03-21 16:57:11 +00:00
|
|
|
* "-S" requires no interface to be selected; it gives statistics
|
|
|
|
* for all interfaces.
|
2005-11-28 08:54:52 +00:00
|
|
|
*/
|
2011-03-21 16:57:11 +00:00
|
|
|
if (print_statistics) {
|
|
|
|
status = print_statistics_loop(machine_readable);
|
|
|
|
exit_main(status);
|
|
|
|
}
|
2010-05-07 19:24:32 +00:00
|
|
|
|
2012-06-24 19:45:49 +00:00
|
|
|
if (set_chan) {
|
|
|
|
interface_options interface_opts;
|
|
|
|
|
|
|
|
if (global_capture_opts.ifaces->len != 1) {
|
|
|
|
cmdarg_err("Need one interface");
|
|
|
|
exit_main(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
interface_opts = g_array_index(global_capture_opts.ifaces, interface_options, 0);
|
|
|
|
status = set_80211_channel(interface_opts.name, set_chan_arg);
|
|
|
|
exit_main(status);
|
|
|
|
}
|
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
/*
|
|
|
|
* "-L", "-d", and capturing act on a particular interface, so we have to
|
|
|
|
* have an interface; if none was specified, pick a default.
|
|
|
|
*/
|
2013-05-22 04:49:31 +00:00
|
|
|
status = capture_opts_default_iface_if_necessary(&global_capture_opts, NULL);
|
2012-11-21 17:14:54 +00:00
|
|
|
if (status != 0) {
|
2011-03-21 16:57:11 +00:00
|
|
|
/* cmdarg_err() already called .... */
|
2012-11-21 17:14:54 +00:00
|
|
|
exit_main(status);
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
|
|
|
|
2015-03-17 20:35:55 +00:00
|
|
|
if (list_link_layer_types) {
|
|
|
|
/* Get the list of link-layer types for the capture device. */
|
|
|
|
if_capabilities_t *caps;
|
|
|
|
gchar *err_str;
|
|
|
|
guint ii;
|
|
|
|
|
|
|
|
for (ii = 0; ii < global_capture_opts.ifaces->len; ii++) {
|
|
|
|
interface_options interface_opts;
|
|
|
|
|
|
|
|
interface_opts = g_array_index(global_capture_opts.ifaces, interface_options, ii);
|
|
|
|
|
2015-07-17 15:01:27 +00:00
|
|
|
caps = get_if_capabilities(&interface_opts, &err_str);
|
2015-03-17 20:35:55 +00:00
|
|
|
if (caps == NULL) {
|
|
|
|
cmdarg_err("The capabilities of the capture device \"%s\" could not be obtained (%s).\n"
|
|
|
|
"Please check to make sure you have sufficient permissions, and that\n"
|
|
|
|
"you have the proper interface or pipe specified.", interface_opts.name, err_str);
|
|
|
|
g_free(err_str);
|
|
|
|
exit_main(2);
|
|
|
|
}
|
|
|
|
if (caps->data_link_types == NULL) {
|
|
|
|
cmdarg_err("The capture device \"%s\" has no data link types.", interface_opts.name);
|
|
|
|
exit_main(2);
|
|
|
|
}
|
|
|
|
if (machine_readable) /* tab-separated values to stdout */
|
|
|
|
/* XXX: We need to change the format and adopt consumers */
|
|
|
|
print_machine_readable_if_capabilities(caps);
|
|
|
|
else
|
|
|
|
/* XXX: We might want to print also the interface name */
|
|
|
|
capture_opts_print_if_capabilities(caps, interface_opts.name,
|
|
|
|
interface_opts.monitor_mode);
|
|
|
|
free_if_capabilities(caps);
|
|
|
|
}
|
|
|
|
exit_main(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We're supposed to do a capture, or print the BPF code for a filter. */
|
|
|
|
|
2011-05-12 19:25:44 +00:00
|
|
|
/* Let the user know what interfaces were chosen. */
|
2011-11-22 19:07:49 +00:00
|
|
|
if (capture_child) {
|
|
|
|
for (j = 0; j < global_capture_opts.ifaces->len; j++) {
|
|
|
|
interface_options interface_opts;
|
|
|
|
|
|
|
|
interface_opts = g_array_index(global_capture_opts.ifaces, interface_options, j);
|
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "Interface: %s\n",
|
|
|
|
interface_opts.name);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
str = g_string_new("");
|
|
|
|
#ifdef _WIN32
|
From Mike Garratt:
Friendly Names for interfaces on Windows
Notes on the changes the patch covers:
* if_info_t struct: addition of friendly_name
* Dumpcap Interface list format changes:
+ Win32: "dumpcap -D" shows friendly_name in place of descript if known
+ All: machine interface "dumpcap -D -Z none" includes friendly_name in the
list in addition to the existing parameters
* interface_options struct: addition of console_display_name
+ When an interface name is displayed in a console, it will typically be the
console_display_name (instead of name).
+ console_display_name is used as the basis of the autogenerated temp
filenames
+ console_display_name is typically set to the friendly_name if known,
otherwise it is set to the interface name
* Enhancements to capture_opts_add_iface_opt() (the function which process -i
options).
+ Can now specify the interface using its name and friendly_name
+ Interface name matching is case insenstive
+ Name matching first attempts exact matching, then falls back to prefix
matching
(e.g. dumpcap -i local)
+ Validates interface names, instead of blindly sending them off to
winpcap/libpcap
+ Interface specification by number is still supported.
* capture_opts_trim_iface() has been refactored:
+ Instead of repeating a decent chunk of the cost in
capture_opts_add_iface_opt(), it calls capture_opts_trim_iface() to specify the
interface.
* introduction of capture_win_ifnames.[ch] (windows only code)
+ Implements static function GetInterfaceFriendlyNameFromDeviceGuid() - a
windows version independant function to convert an interface guid into its
friendly name. Uses published api functions on windows vista and higher, but
falls back to unpublished API functions on older windows releases.
+ void get_windows_interface_friendlyname(/* IN */ char
*interface_devicename, /* OUT */char **interface_friendlyname); - extracts the
GUID from the interface_devicename, then uses
GetInterfaceFriendlyNameFromDeviceGuid() to do the resolution
* Auto temp filename generation:
+ Now uses wireshark_pcapng_* or wireshark_pcap_* depending on file format
+ Basis temp filename format on console_display_name
+ Win32: if console_display_name is a windows interface guid, extracts
numbers from GUID here (instead of in interface option processing)
GUI CHANGES:
* Dialog that displays when you click the "Manage Interfaces" button (within
Capture Options dialog) has been renamed from "Add new interfaces" to
"Interface Management"
* ui/gtk/capture_dlg.c: new_interfaces_w variable renamed to
interface_management_w
* Win32: Local Interfaces tab on Interface Management dialog, shows includes
friendly name as far left column
* Interface Management dialog defaults to larger size on win32 - so it fits
without resizing local interfaces tab
* Interface Management dialog now saves preferences when you click the apply
button (local hidden interfaces was not persisting across restarts)
* Tweaks: "Interface Details" dialog (Interface list->Capture Interfaces ->
Details):
+ "Friendly Name" renamed to "NDIS Friendly Name"
+ Added "OS Friendly Name" to the top of the list
* Win32: The "Capture Interfaces" dialog now shows the friendly name instead of
device guid
* Welcome screen:
+ The height of the interface list scrollbox dynamically adjusts & updates to
the number visible interfaces.
Up to 10 interfaces can be listed without a scroll bar, the minimum height
is for 2 interfaces.
+ Win32: now shows just the Friendly Name if known - in place of
"Interfacename_Guid:(Description)"
svn path=/trunk/; revision=46083
2012-11-19 20:07:27 +00:00
|
|
|
if (global_capture_opts.ifaces->len < 2)
|
2011-11-22 19:07:49 +00:00
|
|
|
#else
|
2012-11-25 18:35:41 +00:00
|
|
|
if (global_capture_opts.ifaces->len < 4)
|
2011-11-22 19:07:49 +00:00
|
|
|
#endif
|
From Mike Garratt:
Friendly Names for interfaces on Windows
Notes on the changes the patch covers:
* if_info_t struct: addition of friendly_name
* Dumpcap Interface list format changes:
+ Win32: "dumpcap -D" shows friendly_name in place of descript if known
+ All: machine interface "dumpcap -D -Z none" includes friendly_name in the
list in addition to the existing parameters
* interface_options struct: addition of console_display_name
+ When an interface name is displayed in a console, it will typically be the
console_display_name (instead of name).
+ console_display_name is used as the basis of the autogenerated temp
filenames
+ console_display_name is typically set to the friendly_name if known,
otherwise it is set to the interface name
* Enhancements to capture_opts_add_iface_opt() (the function which process -i
options).
+ Can now specify the interface using its name and friendly_name
+ Interface name matching is case insenstive
+ Name matching first attempts exact matching, then falls back to prefix
matching
(e.g. dumpcap -i local)
+ Validates interface names, instead of blindly sending them off to
winpcap/libpcap
+ Interface specification by number is still supported.
* capture_opts_trim_iface() has been refactored:
+ Instead of repeating a decent chunk of the cost in
capture_opts_add_iface_opt(), it calls capture_opts_trim_iface() to specify the
interface.
* introduction of capture_win_ifnames.[ch] (windows only code)
+ Implements static function GetInterfaceFriendlyNameFromDeviceGuid() - a
windows version independant function to convert an interface guid into its
friendly name. Uses published api functions on windows vista and higher, but
falls back to unpublished API functions on older windows releases.
+ void get_windows_interface_friendlyname(/* IN */ char
*interface_devicename, /* OUT */char **interface_friendlyname); - extracts the
GUID from the interface_devicename, then uses
GetInterfaceFriendlyNameFromDeviceGuid() to do the resolution
* Auto temp filename generation:
+ Now uses wireshark_pcapng_* or wireshark_pcap_* depending on file format
+ Basis temp filename format on console_display_name
+ Win32: if console_display_name is a windows interface guid, extracts
numbers from GUID here (instead of in interface option processing)
GUI CHANGES:
* Dialog that displays when you click the "Manage Interfaces" button (within
Capture Options dialog) has been renamed from "Add new interfaces" to
"Interface Management"
* ui/gtk/capture_dlg.c: new_interfaces_w variable renamed to
interface_management_w
* Win32: Local Interfaces tab on Interface Management dialog, shows includes
friendly name as far left column
* Interface Management dialog defaults to larger size on win32 - so it fits
without resizing local interfaces tab
* Interface Management dialog now saves preferences when you click the apply
button (local hidden interfaces was not persisting across restarts)
* Tweaks: "Interface Details" dialog (Interface list->Capture Interfaces ->
Details):
+ "Friendly Name" renamed to "NDIS Friendly Name"
+ Added "OS Friendly Name" to the top of the list
* Win32: The "Capture Interfaces" dialog now shows the friendly name instead of
device guid
* Welcome screen:
+ The height of the interface list scrollbox dynamically adjusts & updates to
the number visible interfaces.
Up to 10 interfaces can be listed without a scroll bar, the minimum height
is for 2 interfaces.
+ Win32: now shows just the Friendly Name if known - in place of
"Interfacename_Guid:(Description)"
svn path=/trunk/; revision=46083
2012-11-19 20:07:27 +00:00
|
|
|
{
|
2011-11-22 19:07:49 +00:00
|
|
|
for (j = 0; j < global_capture_opts.ifaces->len; j++) {
|
|
|
|
interface_options interface_opts;
|
|
|
|
|
|
|
|
interface_opts = g_array_index(global_capture_opts.ifaces, interface_options, j);
|
|
|
|
if (j > 0) {
|
|
|
|
if (global_capture_opts.ifaces->len > 2) {
|
|
|
|
g_string_append_printf(str, ",");
|
|
|
|
}
|
|
|
|
g_string_append_printf(str, " ");
|
|
|
|
if (j == global_capture_opts.ifaces->len - 1) {
|
|
|
|
g_string_append_printf(str, "and ");
|
|
|
|
}
|
2012-11-25 18:35:41 +00:00
|
|
|
}
|
From Mike Garratt:
Friendly Names for interfaces on Windows
Notes on the changes the patch covers:
* if_info_t struct: addition of friendly_name
* Dumpcap Interface list format changes:
+ Win32: "dumpcap -D" shows friendly_name in place of descript if known
+ All: machine interface "dumpcap -D -Z none" includes friendly_name in the
list in addition to the existing parameters
* interface_options struct: addition of console_display_name
+ When an interface name is displayed in a console, it will typically be the
console_display_name (instead of name).
+ console_display_name is used as the basis of the autogenerated temp
filenames
+ console_display_name is typically set to the friendly_name if known,
otherwise it is set to the interface name
* Enhancements to capture_opts_add_iface_opt() (the function which process -i
options).
+ Can now specify the interface using its name and friendly_name
+ Interface name matching is case insenstive
+ Name matching first attempts exact matching, then falls back to prefix
matching
(e.g. dumpcap -i local)
+ Validates interface names, instead of blindly sending them off to
winpcap/libpcap
+ Interface specification by number is still supported.
* capture_opts_trim_iface() has been refactored:
+ Instead of repeating a decent chunk of the cost in
capture_opts_add_iface_opt(), it calls capture_opts_trim_iface() to specify the
interface.
* introduction of capture_win_ifnames.[ch] (windows only code)
+ Implements static function GetInterfaceFriendlyNameFromDeviceGuid() - a
windows version independant function to convert an interface guid into its
friendly name. Uses published api functions on windows vista and higher, but
falls back to unpublished API functions on older windows releases.
+ void get_windows_interface_friendlyname(/* IN */ char
*interface_devicename, /* OUT */char **interface_friendlyname); - extracts the
GUID from the interface_devicename, then uses
GetInterfaceFriendlyNameFromDeviceGuid() to do the resolution
* Auto temp filename generation:
+ Now uses wireshark_pcapng_* or wireshark_pcap_* depending on file format
+ Basis temp filename format on console_display_name
+ Win32: if console_display_name is a windows interface guid, extracts
numbers from GUID here (instead of in interface option processing)
GUI CHANGES:
* Dialog that displays when you click the "Manage Interfaces" button (within
Capture Options dialog) has been renamed from "Add new interfaces" to
"Interface Management"
* ui/gtk/capture_dlg.c: new_interfaces_w variable renamed to
interface_management_w
* Win32: Local Interfaces tab on Interface Management dialog, shows includes
friendly name as far left column
* Interface Management dialog defaults to larger size on win32 - so it fits
without resizing local interfaces tab
* Interface Management dialog now saves preferences when you click the apply
button (local hidden interfaces was not persisting across restarts)
* Tweaks: "Interface Details" dialog (Interface list->Capture Interfaces ->
Details):
+ "Friendly Name" renamed to "NDIS Friendly Name"
+ Added "OS Friendly Name" to the top of the list
* Win32: The "Capture Interfaces" dialog now shows the friendly name instead of
device guid
* Welcome screen:
+ The height of the interface list scrollbox dynamically adjusts & updates to
the number visible interfaces.
Up to 10 interfaces can be listed without a scroll bar, the minimum height
is for 2 interfaces.
+ Win32: now shows just the Friendly Name if known - in place of
"Interfacename_Guid:(Description)"
svn path=/trunk/; revision=46083
2012-11-19 20:07:27 +00:00
|
|
|
g_string_append_printf(str, "'%s'", interface_opts.console_display_name);
|
2011-11-22 19:07:49 +00:00
|
|
|
}
|
2011-11-22 10:08:26 +00:00
|
|
|
} else {
|
2011-11-22 19:07:49 +00:00
|
|
|
g_string_append_printf(str, "%u interfaces", global_capture_opts.ifaces->len);
|
|
|
|
}
|
|
|
|
fprintf(stderr, "Capturing on %s\n", str->str);
|
|
|
|
g_string_free(str, TRUE);
|
2011-05-12 19:25:44 +00:00
|
|
|
}
|
2011-03-21 16:57:11 +00:00
|
|
|
|
2015-03-17 20:35:55 +00:00
|
|
|
/* Process the snapshot length, as that affects the generated BPF code. */
|
2011-03-21 16:57:11 +00:00
|
|
|
capture_opts_trim_snaplen(&global_capture_opts, MIN_PACKET_SIZE);
|
2010-07-13 23:26:07 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_BPF_IMAGE
|
2011-03-21 16:57:11 +00:00
|
|
|
if (print_bpf_code) {
|
|
|
|
show_filter_code(&global_capture_opts);
|
|
|
|
exit_main(0);
|
|
|
|
}
|
2010-07-13 23:26:07 +00:00
|
|
|
#endif
|
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
/* We're supposed to do a capture. Process the ring buffer arguments. */
|
|
|
|
capture_opts_trim_ring_num_files(&global_capture_opts);
|
2005-11-28 08:54:52 +00:00
|
|
|
|
From Mike Garratt:
Friendly Names for interfaces on Windows
Notes on the changes the patch covers:
* if_info_t struct: addition of friendly_name
* Dumpcap Interface list format changes:
+ Win32: "dumpcap -D" shows friendly_name in place of descript if known
+ All: machine interface "dumpcap -D -Z none" includes friendly_name in the
list in addition to the existing parameters
* interface_options struct: addition of console_display_name
+ When an interface name is displayed in a console, it will typically be the
console_display_name (instead of name).
+ console_display_name is used as the basis of the autogenerated temp
filenames
+ console_display_name is typically set to the friendly_name if known,
otherwise it is set to the interface name
* Enhancements to capture_opts_add_iface_opt() (the function which process -i
options).
+ Can now specify the interface using its name and friendly_name
+ Interface name matching is case insenstive
+ Name matching first attempts exact matching, then falls back to prefix
matching
(e.g. dumpcap -i local)
+ Validates interface names, instead of blindly sending them off to
winpcap/libpcap
+ Interface specification by number is still supported.
* capture_opts_trim_iface() has been refactored:
+ Instead of repeating a decent chunk of the cost in
capture_opts_add_iface_opt(), it calls capture_opts_trim_iface() to specify the
interface.
* introduction of capture_win_ifnames.[ch] (windows only code)
+ Implements static function GetInterfaceFriendlyNameFromDeviceGuid() - a
windows version independant function to convert an interface guid into its
friendly name. Uses published api functions on windows vista and higher, but
falls back to unpublished API functions on older windows releases.
+ void get_windows_interface_friendlyname(/* IN */ char
*interface_devicename, /* OUT */char **interface_friendlyname); - extracts the
GUID from the interface_devicename, then uses
GetInterfaceFriendlyNameFromDeviceGuid() to do the resolution
* Auto temp filename generation:
+ Now uses wireshark_pcapng_* or wireshark_pcap_* depending on file format
+ Basis temp filename format on console_display_name
+ Win32: if console_display_name is a windows interface guid, extracts
numbers from GUID here (instead of in interface option processing)
GUI CHANGES:
* Dialog that displays when you click the "Manage Interfaces" button (within
Capture Options dialog) has been renamed from "Add new interfaces" to
"Interface Management"
* ui/gtk/capture_dlg.c: new_interfaces_w variable renamed to
interface_management_w
* Win32: Local Interfaces tab on Interface Management dialog, shows includes
friendly name as far left column
* Interface Management dialog defaults to larger size on win32 - so it fits
without resizing local interfaces tab
* Interface Management dialog now saves preferences when you click the apply
button (local hidden interfaces was not persisting across restarts)
* Tweaks: "Interface Details" dialog (Interface list->Capture Interfaces ->
Details):
+ "Friendly Name" renamed to "NDIS Friendly Name"
+ Added "OS Friendly Name" to the top of the list
* Win32: The "Capture Interfaces" dialog now shows the friendly name instead of
device guid
* Welcome screen:
+ The height of the interface list scrollbox dynamically adjusts & updates to
the number visible interfaces.
Up to 10 interfaces can be listed without a scroll bar, the minimum height
is for 2 interfaces.
+ Win32: now shows just the Friendly Name if known - in place of
"Interfacename_Guid:(Description)"
svn path=/trunk/; revision=46083
2012-11-19 20:07:27 +00:00
|
|
|
/* flush stderr prior to starting the main capture loop */
|
|
|
|
fflush(stderr);
|
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
/* Now start the capture. */
|
2012-11-25 18:35:41 +00:00
|
|
|
if (capture_loop_start(&global_capture_opts, &stats_known, &stats) == TRUE) {
|
2011-03-21 16:57:11 +00:00
|
|
|
/* capture ok */
|
|
|
|
exit_main(0);
|
|
|
|
} else {
|
|
|
|
/* capture failed */
|
|
|
|
exit_main(1);
|
|
|
|
}
|
2011-04-22 07:15:45 +00:00
|
|
|
return 0; /* never here, make compiler happy */
|
2005-11-28 08:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
console_log_handler(const char *log_domain, GLogLevelFlags log_level,
|
2011-03-21 16:57:11 +00:00
|
|
|
const char *message, gpointer user_data _U_)
|
2005-11-28 08:54:52 +00:00
|
|
|
{
|
2012-11-25 18:35:41 +00:00
|
|
|
time_t curr;
|
2011-03-21 16:57:11 +00:00
|
|
|
struct tm *today;
|
|
|
|
const char *level;
|
|
|
|
gchar *msg;
|
2005-11-28 08:54:52 +00:00
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
/* ignore log message, if log_level isn't interesting */
|
2012-11-25 18:35:41 +00:00
|
|
|
if ( !(log_level & G_LOG_LEVEL_MASK & ~(G_LOG_LEVEL_DEBUG|G_LOG_LEVEL_INFO))) {
|
2008-02-23 19:59:38 +00:00
|
|
|
#if !defined(DEBUG_DUMPCAP) && !defined(DEBUG_CHILD_DUMPCAP)
|
2011-03-21 16:57:11 +00:00
|
|
|
return;
|
2006-02-11 23:25:11 +00:00
|
|
|
#endif
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch(log_level & G_LOG_LEVEL_MASK) {
|
|
|
|
case G_LOG_LEVEL_ERROR:
|
|
|
|
level = "Err ";
|
|
|
|
break;
|
|
|
|
case G_LOG_LEVEL_CRITICAL:
|
|
|
|
level = "Crit";
|
|
|
|
break;
|
|
|
|
case G_LOG_LEVEL_WARNING:
|
|
|
|
level = "Warn";
|
|
|
|
break;
|
|
|
|
case G_LOG_LEVEL_MESSAGE:
|
|
|
|
level = "Msg ";
|
|
|
|
break;
|
|
|
|
case G_LOG_LEVEL_INFO:
|
|
|
|
level = "Info";
|
|
|
|
break;
|
|
|
|
case G_LOG_LEVEL_DEBUG:
|
|
|
|
level = "Dbg ";
|
|
|
|
break;
|
|
|
|
default:
|
2015-02-09 18:42:51 +00:00
|
|
|
fprintf(stderr, "unknown log_level %d\n", log_level);
|
2011-03-21 16:57:11 +00:00
|
|
|
level = NULL;
|
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate the output message */
|
2012-11-25 18:35:41 +00:00
|
|
|
if (log_level & G_LOG_LEVEL_MESSAGE) {
|
2011-03-21 16:57:11 +00:00
|
|
|
/* normal user messages without additional infos */
|
|
|
|
msg = g_strdup_printf("%s\n", message);
|
|
|
|
} else {
|
2016-10-22 02:18:15 +00:00
|
|
|
/* create a "timestamp" */
|
|
|
|
time(&curr);
|
|
|
|
today = localtime(&curr);
|
|
|
|
|
2011-03-21 16:57:11 +00:00
|
|
|
/* info/debug messages with additional infos */
|
2016-10-22 02:18:15 +00:00
|
|
|
if (today != NULL)
|
|
|
|
msg = g_strdup_printf("%02u:%02u:%02u %8s %s %s\n",
|
|
|
|
today->tm_hour, today->tm_min, today->tm_sec,
|
|
|
|
log_domain != NULL ? log_domain : "",
|
|
|
|
level, message);
|
|
|
|
else
|
|
|
|
msg = g_strdup_printf("Time not representable %8s %s %s\n",
|
|
|
|
log_domain != NULL ? log_domain : "",
|
|
|
|
level, message);
|
2011-03-21 16:57:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* DEBUG & INFO msgs (if we're debugging today) */
|
2008-02-23 19:59:38 +00:00
|
|
|
#if defined(DEBUG_DUMPCAP) || defined(DEBUG_CHILD_DUMPCAP)
|
2012-11-25 18:35:41 +00:00
|
|
|
if ( !(log_level & G_LOG_LEVEL_MASK & ~(G_LOG_LEVEL_DEBUG|G_LOG_LEVEL_INFO))) {
|
2008-02-23 19:59:38 +00:00
|
|
|
#ifdef DEBUG_DUMPCAP
|
2011-03-21 16:57:11 +00:00
|
|
|
fprintf(stderr, "%s", msg);
|
|
|
|
fflush(stderr);
|
2008-02-23 19:59:38 +00:00
|
|
|
#endif
|
|
|
|
#ifdef DEBUG_CHILD_DUMPCAP
|
2011-03-21 16:57:11 +00:00
|
|
|
fprintf(debug_log, "%s", msg);
|
|
|
|
fflush(debug_log);
|
|
|
|
#endif
|
|
|
|
g_free(msg);
|
|
|
|
return;
|
|
|
|
}
|
2008-02-23 19:59:38 +00:00
|
|
|
#endif
|
2011-03-21 16:57:11 +00:00
|
|
|
|
|
|
|
/* ERROR, CRITICAL, WARNING, MESSAGE messages goto stderr or */
|
|
|
|
/* to parent especially formatted if dumpcap running as child. */
|
|
|
|
if (capture_child) {
|
|
|
|
sync_pipe_errmsg_to_parent(2, msg, "");
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "%s", msg);
|
|
|
|
fflush(stderr);
|
|
|
|
}
|
2008-03-22 19:04:26 +00:00
|
|
|
g_free(msg);
|
2005-11-28 08:54:52 +00:00
|
|
|
}
|
|
|
|
|
2006-02-17 02:18:48 +00:00
|
|
|
|
2005-12-04 02:04:18 +00:00
|
|
|
/****************************************************************************************************************/
|
2006-04-03 01:56:53 +00:00
|
|
|
/* indication report routines */
|
2005-11-28 08:54:52 +00:00
|
|
|
|
2005-12-13 22:48:58 +00:00
|
|
|
|
2010-05-26 00:19:27 +00:00
|
|
|
static void
|
2012-09-09 13:57:43 +00:00
|
|
|
report_packet_count(unsigned int packet_count)
|
2005-11-28 08:54:52 +00:00
|
|
|
{
|
|
|
|
char tmp[SP_DECISIZE+1+1];
|
2012-09-09 13:57:43 +00:00
|
|
|
static unsigned int count = 0;
|
2005-11-28 08:54:52 +00:00
|
|
|
|
2012-11-25 18:35:41 +00:00
|
|
|
if (capture_child) {
|
2012-09-09 13:57:43 +00:00
|
|
|
g_snprintf(tmp, sizeof(tmp), "%u", packet_count);
|
2006-02-17 02:18:48 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "Packets: %s", tmp);
|
2013-07-15 15:51:45 +00:00
|
|
|
pipe_write_block(2, SP_PACKET_COUNT, tmp);
|
2006-02-17 02:18:48 +00:00
|
|
|
} else {
|
2006-02-10 01:18:24 +00:00
|
|
|
count += packet_count;
|
2006-02-17 02:18:48 +00:00
|
|
|
fprintf(stderr, "\rPackets: %u ", count);
|
2006-02-10 01:18:24 +00:00
|
|
|
/* stderr could be line buffered */
|
|
|
|
fflush(stderr);
|
|
|
|
}
|
2005-11-28 08:54:52 +00:00
|
|
|
}
|
|
|
|
|
2011-05-26 14:33:55 +00:00
|
|
|
static void
|
2006-04-03 01:56:53 +00:00
|
|
|
report_new_capture_file(const char *filename)
|
2005-11-28 08:54:52 +00:00
|
|
|
{
|
2012-11-25 18:35:41 +00:00
|
|
|
if (capture_child) {
|
2007-02-19 20:25:56 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "File: %s", filename);
|
2013-07-15 15:51:45 +00:00
|
|
|
pipe_write_block(2, SP_FILE, filename);
|
2007-02-19 20:25:56 +00:00
|
|
|
} else {
|
2010-05-26 00:19:27 +00:00
|
|
|
#ifdef SIGINFO
|
|
|
|
/*
|
|
|
|
* Prevent a SIGINFO handler from writing to the standard error
|
|
|
|
* while we're doing so; instead, have it just set a flag telling
|
|
|
|
* us to print that information when we're done.
|
|
|
|
*/
|
|
|
|
infodelay = TRUE;
|
|
|
|
#endif /* SIGINFO */
|
2007-02-19 20:25:56 +00:00
|
|
|
fprintf(stderr, "File: %s\n", filename);
|
|
|
|
/* stderr could be line buffered */
|
|
|
|
fflush(stderr);
|
2010-05-26 00:19:27 +00:00
|
|
|
|
|
|
|
#ifdef SIGINFO
|
|
|
|
/*
|
|
|
|
* Allow SIGINFO handlers to write.
|
|
|
|
*/
|
|
|
|
infodelay = FALSE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If a SIGINFO handler asked us to write out capture counts, do so.
|
|
|
|
*/
|
|
|
|
if (infoprint)
|
2011-02-24 19:26:38 +00:00
|
|
|
report_counts_for_siginfo();
|
2010-05-26 00:19:27 +00:00
|
|
|
#endif /* SIGINFO */
|
2006-03-05 03:14:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-26 14:33:55 +00:00
|
|
|
static void
|
2011-06-27 11:30:39 +00:00
|
|
|
report_cfilter_error(capture_options *capture_opts, guint i, const char *errmsg)
|
2006-03-05 03:14:16 +00:00
|
|
|
{
|
2011-06-27 11:30:39 +00:00
|
|
|
interface_options interface_opts;
|
2011-06-27 12:13:15 +00:00
|
|
|
char tmp[MSG_MAX_LENGTH+1+6];
|
2011-06-27 11:30:39 +00:00
|
|
|
|
|
|
|
if (i < capture_opts->ifaces->len) {
|
|
|
|
if (capture_child) {
|
2011-06-27 12:13:15 +00:00
|
|
|
g_snprintf(tmp, sizeof(tmp), "%u:%s", i, errmsg);
|
2011-06-27 11:30:39 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "Capture filter error: %s", errmsg);
|
2013-07-15 15:51:45 +00:00
|
|
|
pipe_write_block(2, SP_BAD_FILTER, tmp);
|
2011-06-27 11:30:39 +00:00
|
|
|
} else {
|
2011-06-27 16:09:49 +00:00
|
|
|
/*
|
|
|
|
* clopts_step_invalid_capfilter in test/suite-clopts.sh MUST match
|
|
|
|
* the error message below.
|
|
|
|
*/
|
2011-06-27 11:30:39 +00:00
|
|
|
interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
|
2011-11-22 10:08:26 +00:00
|
|
|
cmdarg_err(
|
2014-09-24 21:06:23 +00:00
|
|
|
"Invalid capture filter \"%s\" for interface '%s'.\n"
|
2011-06-27 11:30:39 +00:00
|
|
|
"\n"
|
|
|
|
"That string isn't a valid capture filter (%s).\n"
|
2011-11-22 10:08:26 +00:00
|
|
|
"See the User's Guide for a description of the capture filter syntax.",
|
2011-06-27 11:30:39 +00:00
|
|
|
interface_opts.cfilter, interface_opts.name, errmsg);
|
|
|
|
}
|
2006-02-17 02:18:48 +00:00
|
|
|
}
|
2005-11-28 08:54:52 +00:00
|
|
|
}
|
|
|
|
|
2011-05-26 14:33:55 +00:00
|
|
|
static void
|
2006-04-03 01:56:53 +00:00
|
|
|
report_capture_error(const char *error_msg, const char *secondary_error_msg)
|
2005-11-28 08:54:52 +00:00
|
|
|
{
|
2012-11-25 18:35:41 +00:00
|
|
|
if (capture_child) {
|
2007-06-11 03:58:58 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG,
|
2007-02-19 20:25:56 +00:00
|
|
|
"Primary Error: %s", error_msg);
|
2007-06-11 03:58:58 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG,
|
2007-02-19 20:25:56 +00:00
|
|
|
"Secondary Error: %s", secondary_error_msg);
|
2011-03-21 16:57:11 +00:00
|
|
|
sync_pipe_errmsg_to_parent(2, error_msg, secondary_error_msg);
|
2007-02-19 20:25:56 +00:00
|
|
|
} else {
|
2011-11-22 10:08:26 +00:00
|
|
|
cmdarg_err("%s", error_msg);
|
2010-07-14 04:30:37 +00:00
|
|
|
if (secondary_error_msg[0] != '\0')
|
2011-11-22 10:08:26 +00:00
|
|
|
cmdarg_err_cont("%s", secondary_error_msg);
|
2006-02-17 02:18:48 +00:00
|
|
|
}
|
2005-11-28 08:54:52 +00:00
|
|
|
}
|
|
|
|
|
2011-05-26 14:33:55 +00:00
|
|
|
static void
|
2013-11-04 19:38:20 +00:00
|
|
|
report_packet_drops(guint32 received, guint32 pcap_drops, guint32 drops, guint32 flushed, guint32 ps_ifdrop, gchar *name)
|
2005-11-28 08:54:52 +00:00
|
|
|
{
|
2011-05-26 20:05:11 +00:00
|
|
|
char tmp[SP_DECISIZE+1+1];
|
2013-03-10 11:57:40 +00:00
|
|
|
guint32 total_drops = pcap_drops + drops + flushed;
|
2005-11-28 08:54:52 +00:00
|
|
|
|
2013-03-10 11:57:40 +00:00
|
|
|
g_snprintf(tmp, sizeof(tmp), "%u", total_drops);
|
2005-11-28 08:54:52 +00:00
|
|
|
|
2012-11-25 18:35:41 +00:00
|
|
|
if (capture_child) {
|
2011-05-26 20:05:11 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG,
|
2013-11-26 03:13:16 +00:00
|
|
|
"Packets received/dropped on interface '%s': %u/%u (pcap:%u/dumpcap:%u/flushed:%u/ps_ifdrop:%u)",
|
2013-11-04 19:38:20 +00:00
|
|
|
name, received, total_drops, pcap_drops, drops, flushed, ps_ifdrop);
|
2011-05-13 05:14:00 +00:00
|
|
|
/* XXX: Need to provide interface id, changes to consumers required. */
|
2013-07-15 15:51:45 +00:00
|
|
|
pipe_write_block(2, SP_DROPS, tmp);
|
2007-02-19 20:25:56 +00:00
|
|
|
} else {
|
2011-05-26 20:05:11 +00:00
|
|
|
fprintf(stderr,
|
2013-11-04 19:38:20 +00:00
|
|
|
"Packets received/dropped on interface '%s': %u/%u (pcap:%u/dumpcap:%u/flushed:%u/ps_ifdrop:%u) (%.1f%%)\n",
|
|
|
|
name, received, total_drops, pcap_drops, drops, flushed, ps_ifdrop,
|
2013-03-10 11:57:40 +00:00
|
|
|
received ? 100.0 * received / (received + total_drops) : 0.0);
|
2007-02-19 20:25:56 +00:00
|
|
|
/* stderr could be line buffered */
|
|
|
|
fflush(stderr);
|
2006-02-17 02:18:48 +00:00
|
|
|
}
|
2005-11-28 08:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-03 15:42:41 +00:00
|
|
|
/************************************************************************************************/
|
2006-02-17 02:18:48 +00:00
|
|
|
/* signal_pipe handling */
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2008-02-16 02:39:58 +00:00
|
|
|
static gboolean
|
2006-02-17 02:18:48 +00:00
|
|
|
signal_pipe_check_running(void)
|
|
|
|
{
|
2007-10-26 16:32:28 +00:00
|
|
|
/* any news from our parent? -> just stop the capture */
|
2012-11-25 18:35:41 +00:00
|
|
|
DWORD avail = 0;
|
2006-02-17 02:18:48 +00:00
|
|
|
gboolean result;
|
|
|
|
|
|
|
|
/* if we are running standalone, no check required */
|
2012-11-25 18:35:41 +00:00
|
|
|
if (!capture_child) {
|
2006-02-17 02:18:48 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-11-25 18:35:41 +00:00
|
|
|
if (!sig_pipe_name || !sig_pipe_handle) {
|
2007-10-26 16:32:28 +00:00
|
|
|
/* This shouldn't happen */
|
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO,
|
|
|
|
"Signal pipe: No name or handle");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX - We should have the process ID of the parent (from the "-Z" flag)
|
|
|
|
* at this point. Should we check to see if the parent is still alive,
|
|
|
|
* e.g. by using OpenProcess?
|
|
|
|
*/
|
|
|
|
|
|
|
|
result = PeekNamedPipe(sig_pipe_handle, NULL, 0, NULL, &avail, NULL);
|
2006-02-17 02:18:48 +00:00
|
|
|
|
2012-11-25 18:35:41 +00:00
|
|
|
if (!result || avail > 0) {
|
2006-02-17 02:18:48 +00:00
|
|
|
/* peek failed or some bytes really available */
|
|
|
|
/* (if not piping from stdin this would fail) */
|
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO,
|
2007-10-26 16:32:28 +00:00
|
|
|
"Signal pipe: Stop capture: %s", sig_pipe_name);
|
2006-02-17 02:18:48 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG,
|
2007-10-26 16:32:28 +00:00
|
|
|
"Signal pipe: %s (%p) result: %u avail: %u", sig_pipe_name,
|
|
|
|
sig_pipe_handle, result, avail);
|
2006-02-17 02:18:48 +00:00
|
|
|
return FALSE;
|
|
|
|
} else {
|
|
|
|
/* pipe ok and no bytes available */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2009-08-26 23:16:37 +00:00
|
|
|
|
|
|
|
/*
|
2015-03-10 17:46:50 +00:00
|
|
|
* Editor modelines - https://www.wireshark.org/tools/modelines.html
|
2009-08-26 23:16:37 +00:00
|
|
|
*
|
|
|
|
* Local variables:
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* tab-width: 8
|
|
|
|
* indent-tabs-mode: nil
|
|
|
|
* End:
|
|
|
|
*
|
2011-09-21 16:44:10 +00:00
|
|
|
* vi: set shiftwidth=4 tabstop=8 expandtab:
|
2009-08-26 23:16:37 +00:00
|
|
|
* :indentSize=4:tabSize=8:noTabs=true:
|
|
|
|
*/
|