1 /* compiled_filter_output.cpp
3 * Wireshark - Network traffic analyzer
4 * By Gerald Combs <gerald@wireshark.org>
5 * Copyright 1998 Gerald Combs
7 * SPDX-License-Identifier: GPL-2.0-or-later
12 #include <ui_compiled_filter_output.h>
13 #include "compiled_filter_output.h"
17 #include <_bsd_types.h>
22 #include "capture_opts.h"
23 #include <wiretap/wtap.h>
24 #include "ui/capture_globals.h"
26 #include "main_application.h"
29 #include <QPushButton>
31 CompiledFilterOutput::CompiledFilterOutput(QWidget
*parent
, QList
<InterfaceFilter
> &intList
) :
32 GeometryStateDialog(parent
),
34 ui(new Ui::CompiledFilterOutput
)
38 setAttribute(Qt::WA_DeleteOnClose
, true);
39 ui
->filterList
->setCurrentFont(mainApp
->monospaceFont());
41 copy_bt_
= ui
->buttonBox
->addButton(tr("Copy"), QDialogButtonBox::ActionRole
);
42 copy_bt_
->setToolTip(tr("Copy filter text to the clipboard."));
43 connect(copy_bt_
, &QPushButton::clicked
, this, &CompiledFilterOutput::copyFilterText
);
45 QPushButton
*close_bt
= ui
->buttonBox
->button(QDialogButtonBox::Close
);
46 close_bt
->setDefault(true);
48 interface_list_
= ui
->interfaceList
;
49 g_mutex_init(&pcap_compile_mtx_
);
55 CompiledFilterOutput::~CompiledFilterOutput()
57 // For some reason closing this dialog either lowers the Capture Options dialog
58 // or raises the main window. Work around the problem for now by manually raising
59 // and activating our parent (presumably the Capture Options dialog).
61 parentWidget()->raise();
62 parentWidget()->activateWindow();
65 g_mutex_clear(&pcap_compile_mtx_
);
69 void CompiledFilterOutput::compileFilter()
71 struct bpf_program fcode
;
73 foreach (InterfaceFilter current
, intList_
) {
74 for (unsigned i
= 0; i
< global_capture_opts
.all_ifaces
->len
; i
++) {
75 interface_t
*device
= &g_array_index(global_capture_opts
.all_ifaces
, interface_t
, i
);
77 if (current
.interface
.compare(device
->display_name
)) {
80 pcap_t
*pd
= pcap_open_dead(device
->active_dlt
, WTAP_MAX_PACKET_SIZE_STANDARD
);
83 g_mutex_lock(&pcap_compile_mtx_
);
84 if (pcap_compile(pd
, &fcode
, current
.filter
.toUtf8().data(), 1, 0) < 0) {
85 compile_results
.insert(current
.interface
, QString(pcap_geterr(pd
)));
86 g_mutex_unlock(&pcap_compile_mtx_
);
87 ui
->interfaceList
->addItem(new QListWidgetItem(QIcon(":expert/expert_error.png"), current
.interface
));
89 GString
*bpf_code_dump
= g_string_new("");
90 struct bpf_insn
*insn
= fcode
.bf_insns
;
91 int ii
, n
= fcode
.bf_len
;
92 for (ii
= 0; ii
< n
; ++insn
, ++ii
) {
93 g_string_append(bpf_code_dump
, bpf_image(insn
, ii
));
94 g_string_append(bpf_code_dump
, "\n");
96 g_mutex_unlock(&pcap_compile_mtx_
);
97 compile_results
.insert(current
.interface
, QString(bpf_code_dump
->str
));
98 g_string_free(bpf_code_dump
, TRUE
);
99 ui
->interfaceList
->addItem(new QListWidgetItem(current
.interface
));
100 pcap_freecode(&fcode
);
110 void CompiledFilterOutput::on_interfaceList_currentItemChanged(QListWidgetItem
*current
, QListWidgetItem
*)
112 QString interface
= current
->text();
113 QHash
<QString
, QString
>::const_iterator iter
= compile_results
.find(interface
);
114 ui
->filterList
->clear();
115 ui
->filterList
->setPlainText(iter
.value());
118 void CompiledFilterOutput::copyFilterText()
120 mainApp
->clipboard()->setText(ui
->filterList
->toPlainText());