#include <signal.h>
#include <errno.h>
-#ifdef HAVE_GETOPT_H
-#include <getopt.h>
-#else
+#ifndef HAVE_GETOPT
#include "wsutil/wsgetopt.h"
#endif
/* is defined */
#endif
-#ifdef _WIN32
-#define USE_THREADS
-#endif
-
static GAsyncQueue *pcap_queue;
static gint64 pcap_queue_bytes;
static gint64 pcap_queue_packets;
#endif
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 */
-#ifdef USE_THREADS
+#if defined(USE_THREADS) && defined(_WIN32)
char * cap_pipe_buf; /* Pointer to the data buffer we read into */
-#endif /* USE_THREADS */
+#endif
int cap_pipe_bytes_to_read;/* Used by cap_pipe_dispatch */
int cap_pipe_bytes_read; /* Used by cap_pipe_dispatch */
enum {
STATE_READ_DATA
} cap_pipe_state;
enum { PIPOK, PIPEOF, PIPERR, PIPNEXIST } cap_pipe_err;
-#ifdef USE_THREADS
+#if defined(USE_THREADS) && defined(_WIN32)
GMutex *cap_pipe_read_mtx;
GAsyncQueue *cap_pipe_pending_q, *cap_pipe_done_q;
#endif
* WaitForSingleObject. If it's less than 1000 WaitForSingleObject
* will return immediately.
*/
-#ifndef USE_THREADS
-#define PIPE_READ_TIMEOUT 250000
-#else
+#if defined(USE_THREADS) && defined(_WIN32)
#define PIPE_READ_TIMEOUT 100000
+#else
+#define PIPE_READ_TIMEOUT 250000
#endif
#define WRITER_THREAD_TIMEOUT 100000 /* usecs */
fprintf(output, " -n use pcapng format instead of pcap\n");
/*fprintf(output, "\n");*/
fprintf(output, "Miscellaneous:\n");
+#ifdef USE_THREADS
fprintf(output, " -t use a separate thread per interface\n");
+#endif
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");
}
}
-#ifdef USE_THREADS
+#if defined(USE_THREADS) && defined(_WIN32)
/*
* Thread function that reads from a pipe and pushes the data
* to the main application thread.
}
return NULL;
}
-#endif /* USE_THREADS */
+#endif
+#if (!(defined(USE_THREADS) && defined(_WIN32))) || defined(MUST_DO_SELECT)
/* Provide select() functionality for a single file descriptor
* on UNIX/POSIX. Windows uses cap_pipe_read via a thread.
*
return select(pipe_fd+1, &rfds, NULL, NULL, &timeout);
}
+#endif
/* Mimic pcap_open_live() for pipe captures
wchar_t *err_str;
#endif
#endif
-#ifndef USE_THREADS
+#if !(defined(USE_THREADS) && defined(_WIN32))
int sel_ret;
unsigned int bytes_read;
#endif
pcap_opts->from_cap_pipe = TRUE;
-#ifndef USE_THREADS
+#if !(defined(USE_THREADS) && defined(_WIN32))
/* read the pcap header */
bytes_read = 0;
while (bytes_read < sizeof magic) {
bytes_read += b;
}
}
-#else /* USE_THREADS */
+#else
g_thread_create(&cap_pipe_read, pcap_opts, FALSE, NULL);
pcap_opts->cap_pipe_buf = (char *) &magic;
goto error;
}
-#endif /* USE_THREADS */
+#endif
switch (magic) {
case PCAP_MAGIC:
goto error;
}
-#ifndef USE_THREADS
+#if !(defined(USE_THREADS) && defined(_WIN32))
/* Read the rest of the header */
bytes_read = 0;
while (bytes_read < sizeof(struct pcap_hdr)) {
bytes_read += b;
}
}
-#else /* USE_THREADS */
+#else
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_strerror(errno));
goto error;
}
-#endif /* USE_THREADS */
+#endif
if (pcap_opts->cap_pipe_byte_swapped) {
/* Byte-swap the header fields about which we care. */
struct pcap_pkthdr phdr;
enum { PD_REC_HDR_READ, PD_DATA_READ, PD_PIPE_EOF, PD_PIPE_ERR,
PD_ERR } result;
-#ifdef USE_THREADS
+#if defined(USE_THREADS) && defined(_WIN32)
GTimeVal wait_time;
gpointer q_status;
#else
switch (pcap_opts->cap_pipe_state) {
case STATE_EXPECT_REC_HDR:
-#ifdef USE_THREADS
+#if defined(USE_THREADS) && defined(_WIN32)
if (g_mutex_trylock(pcap_opts->cap_pipe_read_mtx)) {
#endif
sizeof(struct pcaprec_modified_hdr) : sizeof(struct pcaprec_hdr);
pcap_opts->cap_pipe_bytes_read = 0;
-#ifdef USE_THREADS
+#if defined(USE_THREADS) && defined(_WIN32)
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);
/* Fall through */
case STATE_READ_REC_HDR:
-#ifndef USE_THREADS
+#if !(defined(USE_THREADS) && defined(_WIN32))
b = read(pcap_opts->cap_pipe_fd, ((char *)&pcap_opts->cap_pipe_rechdr)+pcap_opts->cap_pipe_bytes_read,
pcap_opts->cap_pipe_bytes_to_read - pcap_opts->cap_pipe_bytes_read);
if (b <= 0) {
break;
}
pcap_opts->cap_pipe_bytes_read += b;
-#else /* USE_THREADS */
+#else
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);
if (!q_status) {
return 0;
}
-#endif /* USE_THREADS */
+#endif
if ((pcap_opts->cap_pipe_bytes_read) < pcap_opts->cap_pipe_bytes_to_read)
return 0;
result = PD_REC_HDR_READ;
break;
case STATE_EXPECT_DATA:
-#ifdef USE_THREADS
+#if defined(USE_THREADS) && defined(_WIN32)
if (g_mutex_trylock(pcap_opts->cap_pipe_read_mtx)) {
#endif
pcap_opts->cap_pipe_bytes_to_read = pcap_opts->cap_pipe_rechdr.hdr.incl_len;
pcap_opts->cap_pipe_bytes_read = 0;
-#ifdef USE_THREADS
+#if defined(USE_THREADS) && defined(_WIN32)
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);
/* Fall through */
case STATE_READ_DATA:
-#ifndef USE_THREADS
+#if !(defined(USE_THREADS) && defined(_WIN32))
b = 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);
if (b <= 0) {
break;
}
pcap_opts->cap_pipe_bytes_read += b;
-#else /* USE_THREADS */
+#else
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);
if (!q_status) {
return 0;
}
-#endif /* USE_THREADS */
+#endif
if ((pcap_opts->cap_pipe_bytes_read) < pcap_opts->cap_pipe_bytes_to_read)
return 0;
result = PD_DATA_READ;
if ((use_threads == FALSE) &&
(capture_opts->ifaces->len > 1)) {
g_snprintf(errmsg, (gulong) errmsg_len,
- "Using threads is required for capturing on mulitple interfaces! Use the -t option.");
+ "Using threads is required for capturing on mulitple interfaces!");
return FALSE;
}
#endif
pcap_opts->cap_pipe_modified = FALSE;
pcap_opts->cap_pipe_byte_swapped = FALSE;
-#ifdef USE_THREADS
+#if defined(USE_THREADS) && defined(_WIN32)
pcap_opts->cap_pipe_buf = NULL;
-#endif /* USE_THREADS */
+#endif
pcap_opts->cap_pipe_bytes_to_read = 0;
pcap_opts->cap_pipe_bytes_read = 0;
pcap_opts->cap_pipe_state = 0;
pcap_opts->cap_pipe_err = PIPOK;
-#ifdef USE_THREADS
+#if defined(USE_THREADS) && defined(_WIN32)
pcap_opts->cap_pipe_read_mtx = g_mutex_new();
pcap_opts->cap_pipe_pending_q = g_async_queue_new();
pcap_opts->cap_pipe_done_q = g_async_queue_new();
int inpkts;
gint packet_count_before;
guchar pcap_data[WTAP_MAX_PACKET_SIZE];
-#ifndef USE_THREADS
+#if !(defined(USE_THREADS) && defined(_WIN32))
int sel_ret;
#endif
#ifdef LOG_CAPTURE_VERBOSE
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_dispatch: from capture pipe");
#endif
-#ifndef USE_THREADS
+#if !(defined(USE_THREADS) && defined(_WIN32))
sel_ret = cap_pipe_select(pcap_opts->cap_pipe_fd);
if (sel_ret <= 0) {
if (sel_ret < 0 && errno != EINTR) {
/*
* "select()" says we can read from the pipe without blocking
*/
-#endif /* USE_THREADS */
+#endif
inpkts = cap_pipe_dispatch(ld, pcap_opts, pcap_data, errmsg, errmsg_len);
if (inpkts < 0) {
ld->go = FALSE;
}
-#ifndef USE_THREADS
+#if !(defined(USE_THREADS) && defined(_WIN32))
}
#endif
}
if (global_capture_opts.ifaces->len > 1) {
prefix = g_strdup_printf("wireshark_%d_interfaces", global_capture_opts.ifaces->len);
} else {
+ gchar *basename;
#ifdef _WIN32
GString *iface;
-
iface = isolate_uuid(g_array_index(global_capture_opts.ifaces, interface_options, 0).name);
- prefix = g_strconcat("wireshark_", g_basename(iface->str), NULL);
+ basename = g_path_get_basename(iface->str);
g_string_free(iface, TRUE);
#else
- prefix = g_strconcat("wireshark_", g_basename(g_array_index(global_capture_opts.ifaces, interface_options, 0).name), NULL);
+ basename = g_path_get_basename(g_array_index(global_capture_opts.ifaces, interface_options, 0).name);
#endif
+ prefix = g_strconcat("wireshark_", basename, NULL);
+ g_free(basename);
}
*save_file_fd = create_tempfile(&tmpname, prefix);
g_free(prefix);
#define OPTSTRING_d ""
#endif
-#define OPTSTRING "a:" OPTSTRING_A "b:" OPTSTRING_B "c:" OPTSTRING_d "Df:ghi:" OPTSTRING_I "L" OPTSTRING_m "MnpPq" OPTSTRING_r "Ss:t" OPTSTRING_u "vw:y:Z:"
+#ifdef USE_THREADS
+#define OPTSTRING_t "t"
+#else
+#define OPTSTRING_t ""
+#endif
+#define OPTSTRING "a:" OPTSTRING_A "b:" OPTSTRING_B "c:" OPTSTRING_d "Df:ghi:" OPTSTRING_I "L" OPTSTRING_m "MnpPq" OPTSTRING_r "Ss:" OPTSTRING_t OPTSTRING_u "vw:y:Z:"
#ifdef DEBUG_CHILD_DUMPCAP
if ((debug_log = ws_fopen("dumpcap_debug_log.tmp","w")) == NULL) {
global_ld.pcaps = g_array_new(FALSE, FALSE, sizeof(pcap_options *));
/* Initialize the thread system */
+#ifdef USE_THREADS
if (!g_thread_supported())
g_thread_init(NULL);
+#endif
#ifdef _WIN32
/* Load wpcap if possible. Do this before collecting the run-time version information */
load_wpcap();
case 'q': /* Quiet */
quiet = TRUE;
break;
-
+#ifdef USE_THREADS
case 't':
use_threads = TRUE;
break;
+#endif
/*** all non capture option specific ***/
case 'D': /* Print a list of capture devices and exit */
list_interfaces = TRUE;
} else {
/* We're supposed to capture traffic; */
/* Are we capturing on multiple interface? If so, use threads and pcapng. */
+#ifdef USE_THREADS
if (global_capture_opts.ifaces->len > 1) {
use_threads = TRUE;
global_capture_opts.use_pcapng = TRUE;
}
+#endif
/* Was the ring buffer option specified and, if so, does it make sense? */
if (global_capture_opts.multi_files_on) {
/* Ring buffer works only under certain conditions: