1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
|
/* interface_tree.cpp
* Display of interface names, traffic sparklines, and, if available,
* extcap options
*
* 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 "interface_tree.h"
#include "epan/prefs.h"
#ifdef HAVE_LIBPCAP
#include "ui/capture_globals.h"
#endif
#include "ui/iface_lists.h"
#include <wsutil/utf8_entities.h>
#include "ui/ui_util.h"
#include "qt_ui_utils.h"
#include "sparkline_delegate.h"
#include "stock_icon.h"
#include "wireshark_application.h"
#ifdef HAVE_EXTCAP
#include "extcap.h"
#endif
#include <QLabel>
#include <QHeaderView>
#include <QTimer>
// The interface list and capture filter editor in the main window and
// the capture interfaces dialog should have the following behavior:
//
// - The global capture options are the source of truth for selected
// interfaces.
// - The global capture options are the source of truth for the capture
// filter for an interface.
// - If multiple interfaces with different filters are selected, the
// CaptureFilterEdit should be cleared and show a corresponding
// placeholder message. Device cfilters should not be changed.
// - Entering a filter in a CaptureFilterEdit should update the device
// cfilter for each selected interface. This should happen even when
// conflicting filters are selected, as described above.
// - Interface selections and cfilter changes in CaptureInterfacesDialog
// should be reflected in MainWelcome.
#ifdef HAVE_LIBPCAP
const int stat_update_interval_ = 1000; // ms
#endif
InterfaceTree::InterfaceTree(QWidget *parent) :
QTreeWidget(parent)
#ifdef HAVE_LIBPCAP
,stat_cache_(NULL)
,stat_timer_(NULL)
#endif // HAVE_LIBPCAP
{
QTreeWidgetItem *ti;
qRegisterMetaType< PointList >("PointList");
header()->setVisible(false);
setRootIsDecorated(false);
setUniformRowHeights(true);
/* Seems to have no effect, still the default value (2) is being used, as it
* was set in the .ui file. But better safe, then sorry. */
resetColumnCount();
setSelectionMode(QAbstractItemView::ExtendedSelection);
setAccessibleName(tr("Welcome screen list"));
setItemDelegateForColumn(IFTREE_COL_STATS, new SparkLineDelegate(this));
setDisabled(true);
ti = new QTreeWidgetItem();
ti->setText(IFTREE_COL_NAME, tr("Waiting for startup%1").arg(UTF8_HORIZONTAL_ELLIPSIS));
addTopLevelItem(ti);
resizeColumnToContents(IFTREE_COL_NAME);
connect(wsApp, SIGNAL(appInitialized()), this, SLOT(getInterfaceList()));
connect(wsApp, SIGNAL(localInterfaceListChanged()), this, SLOT(interfaceListChanged()));
connect(this, SIGNAL(itemSelectionChanged()), this, SLOT(selectedInterfaceChanged()));
}
InterfaceTree::~InterfaceTree() {
#ifdef HAVE_LIBPCAP
if (stat_cache_) {
capture_stat_stop(stat_cache_);
stat_cache_ = NULL;
}
#endif // HAVE_LIBPCAP
}
class InterfaceTreeWidgetItem : public QTreeWidgetItem
{
public:
InterfaceTreeWidgetItem() : QTreeWidgetItem() {}
QList<int> points;
};
/* Resets the column count to the maximum colum count
*
* This is necessary, because the treeview may have more columns than
* the default value (2).
*/
void InterfaceTree::resetColumnCount()
{
setColumnCount(IFTREE_COL_MAX);
}
void InterfaceTree::hideEvent(QHideEvent *) {
#ifdef HAVE_LIBPCAP
if (stat_timer_) stat_timer_->stop();
if (stat_cache_) {
capture_stat_stop(stat_cache_);
stat_cache_ = NULL;
}
#endif // HAVE_LIBPCAP
}
void InterfaceTree::showEvent(QShowEvent *) {
#ifdef HAVE_LIBPCAP
if (stat_timer_) stat_timer_->start(stat_update_interval_);
#endif // HAVE_LIBPCAP
}
void InterfaceTree::resizeEvent(QResizeEvent *evt)
{
int max_if_width = width() * 2 / 3; // Arbitrary
setUpdatesEnabled(false);
resizeColumnToContents(IFTREE_COL_NAME);
if (columnWidth(IFTREE_COL_NAME) > max_if_width) {
setColumnWidth(IFTREE_COL_NAME, max_if_width);
}
setUpdatesEnabled(true);
QTreeWidget::resizeEvent(evt);
}
void InterfaceTree::display()
{
#ifdef HAVE_LIBPCAP
interface_t device;
#ifdef HAVE_EXTCAP
QIcon extcap_icon(StockIcon("x-capture-options"));
#endif
setDisabled(false);
clear();
if (global_capture_opts.all_ifaces->len == 0) {
// Error,or just no interfaces?
QTreeWidgetItem *ti = new QTreeWidgetItem();
QLabel *err_label;
if (global_capture_opts.ifaces_err == 0) {
err_label = new QLabel("No interfaces found");
} else {
err_label = new QLabel(global_capture_opts.ifaces_err_info);
}
err_label->setWordWrap(true);
setColumnCount(1);
addTopLevelItem(ti);
setItemWidget(ti, 0, err_label);
resizeColumnToContents(0);
return;
}
/* when no interfaces were available initially and an update of the
interface list called this function, the column count is set to 1
reset it to ensure that the interface list is properly displayed */
resetColumnCount();
// List physical interfaces first. Alternatively we could sort them by
// traffic, interface name, or most recently used.
QList<QTreeWidgetItem *> phys_ifaces;
QList<QTreeWidgetItem *> virt_ifaces;
global_capture_opts.num_selected = 0;
for (guint i = 0; i < global_capture_opts.all_ifaces->len; i++) {
device = g_array_index(global_capture_opts.all_ifaces, interface_t, i);
/* Continue if capture device is hidden */
if (device.hidden) {
continue;
}
InterfaceTreeWidgetItem *ti = new InterfaceTreeWidgetItem();
QString if_name = device.display_name;
ti->setText(IFTREE_COL_NAME, if_name);
ti->setData(IFTREE_COL_NAME, Qt::AccessibleTextRole, if_name);
ti->setData(IFTREE_COL_NAME, Qt::UserRole, QString(device.name));
ti->setData(IFTREE_COL_STATS, Qt::UserRole, qVariantFromValue(&ti->points));
#ifdef HAVE_EXTCAP
if (device.if_info.type == IF_EXTCAP) {
if (extcap_has_configuration((const char *)(device.name), FALSE)) {
ti->setIcon(IFTREE_COL_EXTCAP, extcap_icon);
ti->setData(IFTREE_COL_EXTCAP, Qt::UserRole, QString(device.if_info.extcap));
if (!(device.external_cap_args_settings != 0 &&
g_hash_table_size(device.external_cap_args_settings) > 0))
{
QFont ti_font = ti->font(IFTREE_COL_NAME);
ti_font.setItalic(true);
ti->setFont(IFTREE_COL_NAME, ti_font);
}
}
virt_ifaces << ti;
} else
#endif
{
phys_ifaces << ti;
}
// XXX Need to handle interfaces passed from the command line.
if (strstr(prefs.capture_device, device.name) != NULL) {
device.selected = TRUE;
global_capture_opts.num_selected++;
global_capture_opts.all_ifaces = g_array_remove_index(global_capture_opts.all_ifaces, i);
g_array_insert_val(global_capture_opts.all_ifaces, i, device);
}
}
if (!phys_ifaces.isEmpty()) addTopLevelItems(phys_ifaces);
if (!virt_ifaces.isEmpty()) addTopLevelItems(virt_ifaces);
updateSelectedInterfaces();
updateToolTips();
// XXX Add other device information
resizeColumnToContents(IFTREE_COL_NAME);
resizeColumnToContents(IFTREE_COL_STATS);
#ifdef HAVE_EXTCAP
resizeColumnToContents(IFTREE_COL_EXTCAP);
#endif
#else
QTreeWidgetItem *ti = new QTreeWidgetItem();
clear();
setColumnCount(1);
ti->setText(0, tr("Interface information not available"));
addTopLevelItem(ti);
resizeColumnToContents(0);
#endif // HAVE_LIBPCAP
}
void InterfaceTree::getInterfaceList()
{
display();
resizeEvent(NULL);
#ifdef HAVE_LIBPCAP
if (!stat_timer_) {
updateStatistics();
stat_timer_ = new QTimer(this);
connect(stat_timer_, SIGNAL(timeout()), this, SLOT(updateStatistics()));
stat_timer_->start(stat_update_interval_);
}
#endif
}
void InterfaceTree::getPoints(int row, PointList *pts)
{
QTreeWidgetItemIterator iter(this);
//qDebug("iter;..!");
for (int i = 0; (*iter); i++)
{
if (row == i)
{
//qDebug("found! row:%d", row);
QList<int> *punkt = (*iter)->data(IFTREE_COL_STATS, Qt::UserRole).value<QList<int> *>();
for (int j = 0; j < punkt->length(); j++)
{
pts->append(punkt->at(j));
}
//pts = new QList<int>(*punkt);
//pts->operator =(punkt);
//pts = punkt;
//pts->append(150);
//qDebug("done");
return;
}
++iter;
}
}
void InterfaceTree::updateStatistics(void) {
#ifdef HAVE_LIBPCAP
interface_t device;
guint diff, if_idx;
struct pcap_stat stats;
if (!stat_cache_) {
// Start gathering statistics using dumpcap
// We crash (on OS X at least) if we try to do this from ::showEvent.
stat_cache_ = capture_stat_start(&global_capture_opts);
}
if (!stat_cache_) return;
QTreeWidgetItemIterator iter(this);
while (*iter) {
QList<int> *points;
for (if_idx = 0; if_idx < global_capture_opts.all_ifaces->len; if_idx++) {
device = g_array_index(global_capture_opts.all_ifaces, interface_t, if_idx);
QString device_name = (*iter)->data(IFTREE_COL_NAME, Qt::UserRole).value<QString>();
if (device_name.compare(device.name) || device.hidden || device.type == IF_PIPE)
continue;
diff = 0;
if (capture_stats(stat_cache_, device.name, &stats)) {
if ((int)(stats.ps_recv - device.last_packets) >= 0) {
diff = stats.ps_recv - device.last_packets;
device.packet_diff = diff;
}
device.last_packets = stats.ps_recv;
}
points = (*iter)->data(IFTREE_COL_STATS, Qt::UserRole).value<QList<int> *>();
points->append(diff);
update(indexFromItem((*iter), IFTREE_COL_STATS));
global_capture_opts.all_ifaces = g_array_remove_index(global_capture_opts.all_ifaces, if_idx);
g_array_insert_val(global_capture_opts.all_ifaces, if_idx, device);
}
++iter;
}
#endif // HAVE_LIBPCAP
}
// Update our global device selections based on the given TreeWidget.
// This is shared with CaptureInterfacesDialog.
// Column name_col UserRole data MUST be set to the interface name.
void InterfaceTree::updateGlobalDeviceSelections(QTreeWidget *if_tree, int name_col)
{
#ifdef HAVE_LIBPCAP
if (!if_tree) return;
QTreeWidgetItemIterator iter(if_tree);
global_capture_opts.num_selected = 0;
while (*iter) {
QString device_name = (*iter)->data(name_col, Qt::UserRole).value<QString>();
for (guint i = 0; i < global_capture_opts.all_ifaces->len; i++) {
interface_t device = g_array_index(global_capture_opts.all_ifaces, interface_t, i);
if (device_name.compare(QString().fromUtf8(device.name)) == 0) {
if (!device.locked) {
if ((*iter)->isSelected()) {
device.selected = TRUE;
global_capture_opts.num_selected++;
} else {
device.selected = FALSE;
}
device.locked = TRUE;
global_capture_opts.all_ifaces = g_array_remove_index(global_capture_opts.all_ifaces, i);
g_array_insert_val(global_capture_opts.all_ifaces, i, device);
device.locked = FALSE;
global_capture_opts.all_ifaces = g_array_remove_index(global_capture_opts.all_ifaces, i);
g_array_insert_val(global_capture_opts.all_ifaces, i, device);
}
break;
}
}
++iter;
}
#else // HAVE_LIBPCAP
Q_UNUSED(if_tree)
Q_UNUSED(name_col)
#endif // HAVE_LIBPCAP
}
// Update selected interfaces based on the global interface list..
// Must not change any interface data.
// Must not emit itemSelectionChanged.
void InterfaceTree::updateSelectedInterfaces()
{
#ifdef HAVE_LIBPCAP
interface_t *device;
QTreeWidgetItemIterator iter(this);
bool blocking = blockSignals(true);
while (*iter) {
QString device_name = (*iter)->data(IFTREE_COL_NAME, Qt::UserRole).value<QString>();
for (guint i = 0; i < global_capture_opts.all_ifaces->len; i++) {
device = &g_array_index(global_capture_opts.all_ifaces, interface_t, i);
if (device_name.compare(QString().fromUtf8(device->name)) == 0) {
(*iter)->setSelected(device->selected);
break;
}
}
++iter;
}
blockSignals(blocking);
#endif // HAVE_LIBPCAP
}
// Update the tooltip for each interface based on the global interface list..
// Must not change any interface data.
void InterfaceTree::updateToolTips()
{
#ifdef HAVE_LIBPCAP
QTreeWidgetItemIterator iter(this);
while (*iter) {
QString device_name = (*iter)->data(IFTREE_COL_NAME, Qt::UserRole).value<QString>();
for (guint i = 0; i < global_capture_opts.all_ifaces->len; i++) {
interface_t device = g_array_index(global_capture_opts.all_ifaces, interface_t, i);
if (device_name.compare(QString().fromUtf8(device.name)) == 0) {
// To do:
// - Sync with code in CaptureInterfacesDialog.
// - Add more information to the tooltip.
QString tt_str = "<p>";
if (device.no_addresses > 0) {
tt_str += QString("%1: %2").arg(device.no_addresses > 1 ? tr("Addresses") : tr("Address")).arg(device.addresses);
tt_str.replace('\n', ", ");
} else {
tt_str = tr("No addresses");
}
tt_str += "<br/>";
QString cfilter = device.cfilter;
if (cfilter.isEmpty()) {
tt_str += tr("No capture filter");
} else {
tt_str += QString("%1: %2")
.arg(tr("Capture filter"))
.arg(cfilter);
}
tt_str += "</p>";
for (int col = 0; col < columnCount(); col++) {
(*iter)->setToolTip(col, tt_str);
}
}
}
++iter;
}
#endif // HAVE_LIBPCAP
}
void InterfaceTree::interfaceListChanged()
{
#ifdef HAVE_LIBPCAP
display();
#endif
}
/*
* 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:
*/
|