2013-08-08 00:26:57 +00:00
|
|
|
/* capture_filter_syntax_worker.cpp
|
|
|
|
*
|
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
|
|
|
* 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
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBPCAP
|
|
|
|
#include <glib.h>
|
2017-05-11 10:56:44 +00:00
|
|
|
|
2017-05-11 19:57:28 +00:00
|
|
|
#include <wsutil/wspcap.h>
|
2017-05-11 10:56:44 +00:00
|
|
|
|
2013-08-08 00:26:57 +00:00
|
|
|
#include "capture_opts.h"
|
|
|
|
#include "ui/capture_globals.h"
|
|
|
|
#endif
|
2017-05-26 11:52:17 +00:00
|
|
|
#ifdef HAVE_EXTCAP
|
|
|
|
#include "extcap.h"
|
|
|
|
#endif
|
2013-08-08 00:26:57 +00:00
|
|
|
|
|
|
|
#include "capture_filter_syntax_worker.h"
|
2017-07-05 14:56:45 +00:00
|
|
|
#include <ui/qt/widgets/syntax_line_edit.h>
|
2013-08-08 00:26:57 +00:00
|
|
|
|
|
|
|
#include <QMutexLocker>
|
|
|
|
#include <QSet>
|
|
|
|
|
2015-06-03 23:26:00 +00:00
|
|
|
// We use a global mutex to protect pcap_compile since it calls gethostbyname.
|
|
|
|
// This probably isn't needed on Windows (where pcap_comple calls
|
2017-04-05 19:15:27 +00:00
|
|
|
// EnterCriticalSection + LeaveCriticalSection) or *BSD or macOS where
|
2015-06-03 23:26:00 +00:00
|
|
|
// gethostbyname(3) claims that it's thread safe.
|
2013-08-08 00:26:57 +00:00
|
|
|
static QMutex pcap_compile_mtx_;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QThread>
|
|
|
|
#define DEBUG_SYNTAX_CHECK(state1, state2) qDebug() << "CF state" << QThread::currentThreadId() << state1 << "->" << state2 << ":" << filter_text_ << ":" << filter
|
2016-01-07 17:27:13 +00:00
|
|
|
#define DEBUG_SLEEP_TIME 5000 // ms
|
2013-08-08 00:26:57 +00:00
|
|
|
#else
|
|
|
|
#define DEBUG_SYNTAX_CHECK(state1, state2)
|
2016-01-07 17:27:13 +00:00
|
|
|
#define DEBUG_SLEEP_TIME 0 // ms
|
2013-08-08 00:26:57 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define DUMMY_SNAPLENGTH 65535
|
|
|
|
#define DUMMY_NETMASK 0xFF000000
|
|
|
|
|
|
|
|
void CaptureFilterSyntaxWorker::start() {
|
|
|
|
#ifdef HAVE_LIBPCAP
|
|
|
|
forever {
|
|
|
|
QString filter;
|
|
|
|
QSet<gint> active_dlts;
|
2017-05-26 11:52:17 +00:00
|
|
|
#ifdef HAVE_EXTCAP
|
|
|
|
QSet<guint> active_extcap;
|
|
|
|
#endif
|
2013-08-08 00:26:57 +00:00
|
|
|
struct bpf_program fcode;
|
|
|
|
pcap_t *pd;
|
|
|
|
int pc_err;
|
2016-01-01 17:00:19 +00:00
|
|
|
enum SyntaxLineEdit::SyntaxState state = SyntaxLineEdit::Valid;
|
2013-08-08 00:26:57 +00:00
|
|
|
QString err_str;
|
|
|
|
|
|
|
|
data_mtx_.lock();
|
|
|
|
while (filter_text_.isEmpty()) {
|
|
|
|
data_cond_.wait(&data_mtx_);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG_SYNTAX_CHECK("pending", "unknown");
|
|
|
|
filter = filter_text_;
|
|
|
|
filter_text_ = QString();
|
|
|
|
data_mtx_.unlock();
|
|
|
|
|
2014-05-14 10:07:49 +00:00
|
|
|
if (global_capture_opts.num_selected < 1) {
|
2016-01-01 17:00:19 +00:00
|
|
|
emit syntaxResult(filter, SyntaxLineEdit::Invalid, QString("No interfaces selected"));
|
2013-08-08 00:26:57 +00:00
|
|
|
DEBUG_SYNTAX_CHECK("unknown", "no interfaces");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (guint if_idx = 0; if_idx < global_capture_opts.all_ifaces->len; if_idx++) {
|
2017-08-24 14:16:34 +00:00
|
|
|
interface_t *device;
|
2013-08-08 00:26:57 +00:00
|
|
|
|
2017-08-24 14:16:34 +00:00
|
|
|
device = &g_array_index(global_capture_opts.all_ifaces, interface_t, if_idx);
|
2017-08-28 08:38:31 +00:00
|
|
|
if (device->selected) {
|
2017-05-25 18:36:44 +00:00
|
|
|
#ifdef HAVE_EXTCAP
|
2017-08-24 14:16:34 +00:00
|
|
|
if (device->if_info.extcap == NULL || strlen(device->if_info.extcap) == 0) {
|
2017-05-25 18:36:44 +00:00
|
|
|
#endif
|
2017-08-24 14:16:34 +00:00
|
|
|
if (device->active_dlt >= DLT_USER0 && device->active_dlt <= DLT_USER15) {
|
2017-05-25 18:36:44 +00:00
|
|
|
// Capture filter for DLT_USER is unknown
|
|
|
|
state = SyntaxLineEdit::Deprecated;
|
|
|
|
err_str = "Unable to check capture filter";
|
|
|
|
} else {
|
2017-08-24 14:16:34 +00:00
|
|
|
active_dlts.insert(device->active_dlt);
|
2017-05-25 18:36:44 +00:00
|
|
|
}
|
|
|
|
#ifdef HAVE_EXTCAP
|
2017-05-26 11:52:17 +00:00
|
|
|
} else {
|
|
|
|
active_extcap.insert(if_idx);
|
2016-01-01 17:00:19 +00:00
|
|
|
}
|
2017-05-25 18:36:44 +00:00
|
|
|
#endif
|
2013-08-08 00:26:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (gint dlt, active_dlts.toList()) {
|
|
|
|
pcap_compile_mtx_.lock();
|
|
|
|
pd = pcap_open_dead(dlt, DUMMY_SNAPLENGTH);
|
2017-05-25 18:36:44 +00:00
|
|
|
if (pd == NULL)
|
|
|
|
{
|
|
|
|
//don't have ability to verify capture filter
|
|
|
|
break;
|
|
|
|
}
|
2013-08-08 00:26:57 +00:00
|
|
|
#ifdef PCAP_NETMASK_UNKNOWN
|
|
|
|
pc_err = pcap_compile(pd, &fcode, filter.toUtf8().constData(), 1 /* Do optimize */, PCAP_NETMASK_UNKNOWN);
|
|
|
|
#else
|
|
|
|
pc_err = pcap_compile(pd, &fcode, filter.toUtf8().constData(), 1 /* Do optimize */, 0);
|
|
|
|
#endif
|
|
|
|
|
2016-01-07 17:27:13 +00:00
|
|
|
#if DEBUG_SLEEP_TIME > 0
|
|
|
|
QThread::msleep(DEBUG_SLEEP_TIME);
|
|
|
|
#endif
|
|
|
|
|
2013-08-08 00:26:57 +00:00
|
|
|
if (pc_err) {
|
|
|
|
DEBUG_SYNTAX_CHECK("unknown", "known bad");
|
2016-01-01 17:00:19 +00:00
|
|
|
state = SyntaxLineEdit::Invalid;
|
2013-08-08 00:26:57 +00:00
|
|
|
err_str = pcap_geterr(pd);
|
|
|
|
} else {
|
|
|
|
DEBUG_SYNTAX_CHECK("unknown", "known good");
|
|
|
|
}
|
|
|
|
pcap_close(pd);
|
|
|
|
|
|
|
|
pcap_compile_mtx_.unlock();
|
|
|
|
|
2016-01-01 17:00:19 +00:00
|
|
|
if (state == SyntaxLineEdit::Invalid) break;
|
2013-08-08 00:26:57 +00:00
|
|
|
}
|
2017-05-26 11:52:17 +00:00
|
|
|
#ifdef HAVE_EXTCAP
|
|
|
|
// If it's already invalid, don't bother to check extcap
|
|
|
|
if (state != SyntaxLineEdit::Invalid) {
|
|
|
|
foreach (guint extcapif, active_extcap.toList()) {
|
2017-08-24 14:16:34 +00:00
|
|
|
interface_t *device;
|
2017-05-26 11:52:17 +00:00
|
|
|
gchar *error = NULL;
|
|
|
|
|
2017-08-24 14:16:34 +00:00
|
|
|
device = &g_array_index(global_capture_opts.all_ifaces, interface_t, extcapif);
|
|
|
|
extcap_filter_status status = extcap_verify_capture_filter(device->name, filter.toUtf8().constData(), &error);
|
2017-05-26 11:52:17 +00:00
|
|
|
if (status == EXTCAP_FILTER_VALID) {
|
|
|
|
DEBUG_SYNTAX_CHECK("unknown", "known good");
|
|
|
|
} else if (status == EXTCAP_FILTER_INVALID) {
|
|
|
|
DEBUG_SYNTAX_CHECK("unknown", "known bad");
|
|
|
|
state = SyntaxLineEdit::Invalid;
|
|
|
|
err_str = error;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
state = SyntaxLineEdit::Deprecated;
|
|
|
|
err_str = "Unable to check capture filter";
|
|
|
|
}
|
|
|
|
g_free (error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2016-01-01 17:00:19 +00:00
|
|
|
emit syntaxResult(filter, state, err_str);
|
2013-08-08 00:26:57 +00:00
|
|
|
|
|
|
|
DEBUG_SYNTAX_CHECK("known", "idle");
|
|
|
|
}
|
|
|
|
#endif // HAVE_LIBPCAP
|
|
|
|
}
|
|
|
|
|
|
|
|
void CaptureFilterSyntaxWorker::checkFilter(const QString &filter)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_LIBPCAP
|
|
|
|
QMutexLocker ml(&data_mtx_);
|
|
|
|
/* Ruthlessly clobber the current state. */
|
|
|
|
filter_text_ = filter;
|
|
|
|
DEBUG_SYNTAX_CHECK("received", "?");
|
|
|
|
data_cond_.wakeOne();
|
|
|
|
#else
|
2016-01-01 17:00:19 +00:00
|
|
|
emit syntaxResult(filter, SyntaxLineEdit::Deprecated, QString("Syntax checking unavailable"));
|
2013-08-08 00:26:57 +00:00
|
|
|
#endif // HAVE_LIBPCAP
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Editor modelines
|
|
|
|
*
|
|
|
|
* Local Variables:
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* tab-width: 8
|
|
|
|
* indent-tabs-mode: nil
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
* ex: set shiftwidth=4 tabstop=8 expandtab:
|
|
|
|
* :indentSize=4:tabSize=8:noTabs=true:
|
|
|
|
*/
|