aboutsummaryrefslogtreecommitdiffstats
path: root/ui/qt/extcap_options_dialog.cpp
blob: 61d717a138fec76b6c127045c715b7f89b0efaf7 (plain)
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/* extcap_options_dialog.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>

#include <glib.h>

#include <extcap_options_dialog.h>
#include <ui_extcap_options_dialog.h>

#include <wireshark_application.h>

#ifdef HAVE_EXTCAP
#include <QMessageBox>
#include <QMap>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QUrl>
#include <QDesktopServices>

#include "ringbuffer.h"
#include "ui/capture_ui_utils.h"
#include "ui/capture_globals.h"
#include "ui/iface_lists.h"
#include "ui/last_open_dir.h"

#include "ui/ws_ui_util.h"
#include "ui/util.h"
#include <wsutil/utf8_entities.h>

#include <cstdio>
#include <epan/addr_resolv.h>
#include <wsutil/filesystem.h>

#include <extcap.h>
#include <extcap_parser.h>

#include <ui/qt/utils/qt_ui_utils.h>

#include <epan/prefs.h>
#include <ui/preference_utils.h>

#include <ui/qt/wireshark_application.h>
#include <ui/qt/utils/variant_pointer.h>

#include <ui/qt/extcap_argument.h>
#include <ui/qt/extcap_argument_file.h>
#include <ui/qt/extcap_argument_multiselect.h>

ExtcapOptionsDialog::ExtcapOptionsDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ExtcapOptionsDialog),
    device_name(""),
    device_idx(0)
{
    ui->setupUi(this);

    setWindowTitle(wsApp->windowTitleString(tr("Interface Options")));

    ui->checkSaveOnStart->setCheckState(prefs.extcap_save_on_start ? Qt::Checked : Qt::Unchecked);

    ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Start"));
}

ExtcapOptionsDialog * ExtcapOptionsDialog::createForDevice(QString &dev_name, QWidget *parent)
{
    interface_t *device;
    ExtcapOptionsDialog * resultDialog = NULL;
    bool dev_found = false;
    guint if_idx;

    if ( dev_name.length() == 0 )
        return NULL;

    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);
        if (dev_name.compare(QString(device->name)) == 0 && device->if_info.type == IF_EXTCAP)
        {
            dev_found = true;
            break;
        }
    }

    if ( ! dev_found )
        return NULL;

    resultDialog = new ExtcapOptionsDialog(parent);
    resultDialog->device_name = QString(dev_name);
    resultDialog->device_idx = if_idx;

    resultDialog->setWindowTitle(wsApp->windowTitleString(tr("Interface Options") + ": " + device->display_name));

    resultDialog->updateWidgets();

    /* mark required fields */
    resultDialog->anyValueChanged();

    return resultDialog;
}


ExtcapOptionsDialog::~ExtcapOptionsDialog()
{
    delete ui;
}

void ExtcapOptionsDialog::on_buttonBox_accepted()
{
    if (saveOptionToCaptureInfo()) {
        /* Starting a new capture with those values */
        prefs.extcap_save_on_start = ui->checkSaveOnStart->checkState() == Qt::Checked;

        if ( prefs.extcap_save_on_start )
            storeValues();

        accept();
    }
}

void ExtcapOptionsDialog::anyValueChanged()
{
    bool allowStart = true;

    ExtcapArgumentList::const_iterator iter;

    /* All arguments are being iterated, to ensure, that any error handling catches all arguments */
    for(iter = extcapArguments.constBegin(); iter != extcapArguments.constEnd(); ++iter)
    {
        /* The dynamic casts are necessary, because we come here using the Signal/Slot system
         * of Qt, and -in short- Q_OBJECT classes cannot be multiple inherited. Another possibility
         * would be to use Q_INTERFACE, but this causes way more nightmares, and we really just
         * need here an explicit cast for the check functionality */
        if ( dynamic_cast<ExtArgBool *>((*iter)) != NULL)
        {
            if ( ! ((ExtArgBool *)*iter)->isValid() )
                allowStart = false;
        }
        else if ( dynamic_cast<ExtArgRadio *>((*iter)) != NULL)
        {
            if ( ! ((ExtArgRadio *)*iter)->isValid() )
                allowStart = false;
        }
        else if ( dynamic_cast<ExtArgSelector *>((*iter)) != NULL)
        {
            if ( ! ((ExtArgSelector *)*iter)->isValid() )
                allowStart = false;
        }
        else if ( dynamic_cast<ExtArgMultiSelect *>((*iter)) != NULL)
        {
            if ( ! ((ExtArgMultiSelect *)*iter)->isValid() )
                allowStart = false;
        }
        else if ( dynamic_cast<ExtcapArgumentFileSelection *>((*iter)) != NULL)
        {
            if ( ! ((ExtcapArgumentFileSelection *)*iter)->isValid() )
                allowStart = false;
        }
        else if ( dynamic_cast<ExtArgNumber *>((*iter)) != NULL)
        {
            if ( ! ((ExtArgNumber *)*iter)->isValid() )
                allowStart = false;
        }
        else if ( dynamic_cast<ExtArgText *>((*iter)) != NULL)
        {
            if ( ! ((ExtArgText *)*iter)->isValid() )
                allowStart = false;
        }
        else if ( dynamic_cast<ExtArgTimestamp *>((*iter)) != NULL)
        {
            if ( ! ((ExtArgTimestamp *)*iter)->isValid() )
                allowStart = false;
        }
        else
            if ( ! (*iter)->isValid() )
                allowStart = false;
    }

    ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(allowStart);
}

void ExtcapOptionsDialog::loadArguments()
{
    GList * arguments = NULL, * walker = NULL, * item = NULL;
    ExtcapArgument * argument = NULL;

    if ( device_name.length() == 0  )
        return;

    extcapArguments.clear();

    arguments = g_list_first(extcap_get_if_configuration((const char *)( device_name.toStdString().c_str() ) ));

    ExtcapArgumentList required;
    ExtcapArgumentList optional;

    walker = arguments;
    while ( walker != NULL )
    {
        item = g_list_first((GList *)(walker->data));
        while ( item != NULL )
        {
            argument = ExtcapArgument::create((extcap_arg *)(item->data));
            if ( argument != NULL )
            {
                if ( argument->isRequired() )
                    required << argument;
                else
                    optional << argument;

            }
            item = item->next;
        }
        walker = g_list_next(walker);
    }

    if ( required.length() > 0 )
        extcapArguments << required;

    if ( optional.length() > 0 )
        extcapArguments << optional;

    /* argument items are now owned by ExtcapArgument. Only free the lists */
    extcap_free_if_configuration(arguments, FALSE);
}

void ExtcapOptionsDialog::updateWidgets()
{
    QWidget * lblWidget = NULL, *editWidget = NULL;
    ExtcapArgument * argument = NULL;
    bool allowStart = true;

    unsigned int counter = 0;

    if ( device_name.length() == 0  )
        return;

    /* find existing layout */
    if (ui->verticalLayout->children().count() > 0)
    {
        QGridLayout * layout = (QGridLayout *)ui->verticalLayout->itemAt(0);
        ui->verticalLayout->removeItem(layout);
        ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
    }

    QGridLayout * layout = new QGridLayout();

    /* Load all extcap arguments */
    loadArguments();


    ExtcapArgumentList::iterator iter = extcapArguments.begin();
    while ( iter != extcapArguments.end() )
    {
        argument = (ExtcapArgument *)(*iter);
        lblWidget = argument->createLabel((QWidget *)this);
        if ( lblWidget != NULL )
        {
            layout->addWidget(lblWidget, counter, 0, Qt::AlignVCenter);
            editWidget = argument->createEditor((QWidget *) this);
            if ( editWidget != NULL )
            {
                editWidget->setProperty(QString("extcap").toLocal8Bit(), VariantPointer<ExtcapArgument>::asQVariant(argument));
                layout->addWidget(editWidget, counter, 1, Qt::AlignVCenter);
            }

            if ( argument->isRequired() && ! argument->isValid() )
                allowStart = false;

            connect(argument, SIGNAL(valueChanged()), this, SLOT(anyValueChanged()));

            counter++;
        }
        ++iter;
    }

    if ( counter > 0 )
    {
        setStyleSheet ( "QLabel[isRequired=\"true\"] { font-weight: bold; } ");

        ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(allowStart);

        ui->verticalLayout->addLayout(layout);
        ui->verticalLayout->addSpacerItem(new QSpacerItem(20, 100, QSizePolicy::Minimum, QSizePolicy::Expanding));
    }
    else
    {
        delete layout;
    }
}

// Not sure why we have to do this manually.
void ExtcapOptionsDialog::on_buttonBox_rejected()
{
    reject();
}

void ExtcapOptionsDialog::on_buttonBox_helpRequested()
{
    interface_t *device;
    QString interface_help = NULL;

    device = &g_array_index(global_capture_opts.all_ifaces, interface_t, device_idx);
    interface_help = QString(extcap_get_help_for_ifname(device->name));
    /* The extcap interface didn't provide an help. Let's go with the default */
    if (interface_help.isEmpty()) {
        wsApp->helpTopicAction(HELP_EXTCAP_OPTIONS_DIALOG);
        return;
    }

    QUrl help_url(interface_help);

    /* The help is not a local file, open it and exit */
    if (help_url.scheme().compare("file") != 0) {
        QDesktopServices::openUrl(help_url);
        return;
    }

    /* The help information is a file url and has been provided as-is by the extcap.
       Before attempting to open the it, check if it actually exists.
    */
    QFileInfo help_file(help_url.path());
    if ( !help_file.exists() )
    {
        QMessageBox::warning(this, tr("Extcap Help cannot be found"),
                QString(tr("The help for the extcap interface %1 cannot be found. Given file: %2"))
                    .arg(device->name).arg(help_url.path()),
                QMessageBox::Ok);
    }

}

bool ExtcapOptionsDialog::saveOptionToCaptureInfo()
{
    GHashTable * ret_args;
    interface_t *device;

    device = &g_array_index(global_capture_opts.all_ifaces, interface_t, device_idx);
    ret_args = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);

    ExtcapArgumentList::const_iterator iter;

    for(iter = extcapArguments.constBegin(); iter != extcapArguments.constEnd(); ++iter)
    {
        QString call = (*iter)->call();
        QString value = (*iter)->value();

        if ((*iter)->argument()->arg_type != EXTCAP_ARG_BOOLFLAG && value.length() == 0)
            continue;

        if ( call.length() <= 0 )
            continue;

        if ( value.compare((*iter)->defaultValue()) == 0 )
            continue;

        gchar * call_string = g_strdup(call.toStdString().c_str());
        gchar * value_string = g_strdup(value.toStdString().c_str());

        g_hash_table_insert(ret_args, call_string, value_string );
    }

    if (device->external_cap_args_settings != NULL)
      g_hash_table_unref(device->external_cap_args_settings);
    device->external_cap_args_settings = ret_args;
    return true;
}

void ExtcapOptionsDialog::on_buttonBox_clicked(QAbstractButton *button)
{
    /* Only the save button has the ActionRole */
    if ( ui->buttonBox->buttonRole(button) == QDialogButtonBox::ResetRole )
        resetValues();
}

void ExtcapOptionsDialog::resetValues()
{
    ExtcapArgumentList::const_iterator iter;
    QString value;

    if (ui->verticalLayout->children().count() > 0)
    {
        QGridLayout * layout = (QGridLayout *)ui->verticalLayout->findChild<QGridLayout *>();

        for ( int row = 0; row < layout->rowCount(); row++ )
        {
            QWidget * child = layout->itemAtPosition(row, 1)->widget();

            if ( child )
            {
                /* Don't need labels, the edit widget contains the extcapargument property value */
                ExtcapArgument * arg = 0;
                QVariant prop = child->property(QString("extcap").toLocal8Bit());

                if ( prop.isValid() )
                {
                    arg = VariantPointer<ExtcapArgument>::asPtr(prop);

                    /* value<> can fail */
                    if (arg)
                    {
                        arg->resetValue();

                        /* replacing the edit widget after resetting will lead to default value */
                        layout->removeItem(layout->itemAtPosition(row, 1));
                        QWidget * editWidget = arg->createEditor((QWidget *) this);
                        if ( editWidget != NULL )
                        {
                            editWidget->setProperty(QString("extcap").toLocal8Bit(), VariantPointer<ExtcapArgument>::asQVariant(arg));
                            layout->addWidget(editWidget, row, 1, Qt::AlignVCenter);
                        }
                    }
                }
            }
        }

        /* this stores all values to the preferences */
        storeValues();
    }
}

void ExtcapOptionsDialog::storeValues()
{
    GHashTable * entries = g_hash_table_new(g_str_hash, g_str_equal);
    ExtcapArgumentList::const_iterator iter;

    QString value;

    /* All arguments are being iterated, to ensure, that any error handling catches all arguments */
    for(iter = extcapArguments.constBegin(); iter != extcapArguments.constEnd(); ++iter)
    {
        ExtcapArgument * argument = (ExtcapArgument *)(*iter);

        /* The dynamic casts are necessary, because we come here using the Signal/Slot system
         * of Qt, and -in short- Q_OBJECT classes cannot be multiple inherited. Another possibility
         * would be to use Q_INTERFACE, but this causes way more nightmares, and we really just
         * need here an explicit cast for the check functionality */
        if ( dynamic_cast<ExtArgBool *>((*iter)) != NULL)
        {
            value = ((ExtArgBool *)*iter)->prefValue();
        }
        else if ( dynamic_cast<ExtArgRadio *>((*iter)) != NULL)
        {
            value = ((ExtArgRadio *)*iter)->prefValue();
        }
        else if ( dynamic_cast<ExtArgSelector *>((*iter)) != NULL)
        {
            value = ((ExtArgSelector *)*iter)->prefValue();
        }
        else if ( dynamic_cast<ExtArgMultiSelect *>((*iter)) != NULL)
        {
            value = ((ExtArgMultiSelect *)*iter)->prefValue();
        }
        else if ( dynamic_cast<ExtcapArgumentFileSelection *>((*iter)) != NULL)
        {
            value = ((ExtcapArgumentFileSelection *)*iter)->prefValue();
        }
        else if ( dynamic_cast<ExtArgNumber *>((*iter)) != NULL)
        {
            value = ((ExtArgNumber *)*iter)->prefValue();
        }
        else if ( dynamic_cast<ExtArgText *>((*iter)) != NULL)
        {
            value = ((ExtArgText *)*iter)->prefValue();
        }
        else if ( dynamic_cast<ExtArgTimestamp *>((*iter)) != NULL)
        {
            value = ((ExtArgTimestamp *)*iter)->prefValue();
        }
        else
            value = (*iter)->prefValue();

        QString key = argument->prefKey(device_name);
        if (key.length() > 0)
        {
            gchar * val = g_strdup(value.length() == 0 ? " " : value.toStdString().c_str());

            g_hash_table_insert(entries, g_strdup(key.toStdString().c_str()), val);
        }
    }

    if ( g_hash_table_size(entries) > 0 )
    {
        if ( prefs_store_ext_multiple("extcap", entries) )
            wsApp->emitAppSignal(WiresharkApplication::PreferencesChanged);

    }
}


#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:
 */