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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
|
/* interface_toolbar.cpp
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include <errno.h>
#include <ws_diag_control.h>
#if WS_IS_AT_LEAST_GNUC_VERSION(12,1)
DIAG_OFF(stringop-overflow)
#if WS_IS_AT_LEAST_GNUC_VERSION(13,0)
DIAG_OFF(restrict)
#endif
#endif
#include "interface_toolbar.h"
#if WS_IS_AT_LEAST_GNUC_VERSION(12,1)
DIAG_ON(stringop-overflow)
#if WS_IS_AT_LEAST_GNUC_VERSION(13,0)
DIAG_ON(restrict)
#endif
#endif
#include <ui/qt/widgets/interface_toolbar_lineedit.h>
#include "simple_dialog.h"
#include "main_application.h"
#include <ui_interface_toolbar.h>
#include "capture_opts.h"
#include "ui/capture_globals.h"
#include "sync_pipe.h"
#include "wsutil/file_util.h"
#ifdef _WIN32
#include <wsutil/win32-utils.h>
#endif
#include <QCheckBox>
#include <QComboBox>
#include <QDesktopServices>
#include <QLineEdit>
#include <QPushButton>
#include <QThread>
#include <QUrl>
static const char *interface_type_property = "control_type";
static const char *interface_role_property = "control_role";
// From interface control protocol.
enum InterfaceControlCommand {
commandControlInitialized = 0,
commandControlSet = 1,
commandControlAdd = 2,
commandControlRemove = 3,
commandControlEnable = 4,
commandControlDisable = 5,
commandStatusMessage = 6,
commandInformationMessage = 7,
commandWarningMessage = 8,
commandErrorMessage = 9
};
// To do:
// - Move control pipe handling to extcap
InterfaceToolbar::InterfaceToolbar(QWidget *parent, const iface_toolbar *toolbar) :
QFrame(parent),
ui(new Ui::InterfaceToolbar),
help_link_(toolbar->help),
use_spacer_(true)
{
ui->setupUi(this);
// Fill inn interfaces list and initialize default interface values
for (GList *walker = toolbar->ifnames; walker; walker = walker->next)
{
QString ifname((gchar *)walker->data);
interface_[ifname].reader_thread = NULL;
interface_[ifname].out_fd = -1;
}
initializeControls(toolbar);
#ifdef Q_OS_MAC
foreach (QWidget *w, findChildren<QWidget *>())
{
w->setAttribute(Qt::WA_MacSmallSize, true);
}
#endif
if (!use_spacer_)
{
ui->horizontalSpacer->changeSize(0,0, QSizePolicy::Fixed, QSizePolicy::Fixed);
}
updateWidgets();
}
InterfaceToolbar::~InterfaceToolbar()
{
foreach (QString ifname, interface_.keys())
{
foreach (int num, control_widget_.keys())
{
if (interface_[ifname].log_dialog.contains(num))
{
interface_[ifname].log_dialog[num]->close();
}
}
}
delete ui;
}
void InterfaceToolbar::initializeControls(const iface_toolbar *toolbar)
{
for (GList *walker = toolbar->controls; walker; walker = walker->next)
{
iface_toolbar_control *control = (iface_toolbar_control *)walker->data;
if (control_widget_.contains(control->num))
{
// Already have a widget with this number
continue;
}
QWidget *widget = NULL;
switch (control->ctrl_type)
{
case INTERFACE_TYPE_BOOLEAN:
widget = createCheckbox(control);
break;
case INTERFACE_TYPE_BUTTON:
widget = createButton(control);
break;
case INTERFACE_TYPE_SELECTOR:
widget = createSelector(control);
break;
case INTERFACE_TYPE_STRING:
widget = createString(control);
break;
default:
// Not supported
break;
}
if (widget)
{
widget->setProperty(interface_type_property, control->ctrl_type);
widget->setProperty(interface_role_property, control->ctrl_role);
control_widget_[control->num] = widget;
}
}
}
void InterfaceToolbar::setDefaultValue(int num, const QByteArray &value)
{
foreach (QString ifname, interface_.keys())
{
// Adding default value to all interfaces
interface_[ifname].value[num] = value;
}
default_value_[num] = value;
}
QWidget *InterfaceToolbar::createCheckbox(iface_toolbar_control *control)
{
QCheckBox *checkbox = new QCheckBox(QString().fromUtf8(control->display));
checkbox->setToolTip(QString().fromUtf8(control->tooltip));
if (control->default_value.boolean)
{
checkbox->setCheckState(Qt::Checked);
QByteArray default_value(1, 1);
setDefaultValue(control->num, default_value);
}
connect(checkbox, SIGNAL(stateChanged(int)), this, SLOT(onCheckBoxChanged(int)));
ui->leftLayout->addWidget(checkbox);
return checkbox;
}
QWidget *InterfaceToolbar::createButton(iface_toolbar_control *control)
{
QPushButton *button = new QPushButton(QString().fromUtf8((gchar *)control->display));
button->setMaximumHeight(27);
button->setToolTip(QString().fromUtf8(control->tooltip));
switch (control->ctrl_role)
{
case INTERFACE_ROLE_CONTROL:
setDefaultValue(control->num, (gchar *)control->display);
connect(button, SIGNAL(clicked()), this, SLOT(onControlButtonClicked()));
break;
case INTERFACE_ROLE_HELP:
connect(button, SIGNAL(clicked()), this, SLOT(onHelpButtonClicked()));
if (help_link_.isEmpty())
{
// No help URL provided
button->hide();
}
break;
case INTERFACE_ROLE_LOGGER:
connect(button, SIGNAL(clicked()), this, SLOT(onLogButtonClicked()));
break;
case INTERFACE_ROLE_RESTORE:
connect(button, SIGNAL(clicked()), this, SLOT(onRestoreButtonClicked()));
break;
default:
// Not supported
break;
}
ui->rightLayout->addWidget(button);
return button;
}
QWidget *InterfaceToolbar::createSelector(iface_toolbar_control *control)
{
QLabel *label = new QLabel(QString().fromUtf8(control->display));
label->setToolTip(QString().fromUtf8(control->tooltip));
QComboBox *combobox = new QComboBox();
combobox->setToolTip(QString().fromUtf8(control->tooltip));
combobox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
for (GList *walker = control->values; walker; walker = walker->next)
{
iface_toolbar_value *val = (iface_toolbar_value *)walker->data;
QString value = QString().fromUtf8((gchar *)val->value);
if (value.isEmpty())
{
// Invalid value
continue;
}
QString display = QString().fromUtf8((gchar *)val->display);
QByteArray interface_value;
interface_value.append(value.toUtf8());
if (display.isEmpty())
{
display = value;
}
else
{
interface_value.append(QString('\0' + display).toUtf8());
}
combobox->addItem(display, value);
if (val->is_default)
{
combobox->setCurrentText(display);
setDefaultValue(control->num, value.toUtf8());
}
foreach (QString ifname, interface_.keys())
{
// Adding values to all interfaces
interface_[ifname].list[control->num].append(interface_value);
}
default_list_[control->num].append(interface_value);
}
connect(combobox, SIGNAL(currentIndexChanged(int)), this, SLOT(onComboBoxChanged(int)));
ui->leftLayout->addWidget(label);
ui->leftLayout->addWidget(combobox);
label_widget_[control->num] = label;
return combobox;
}
QWidget *InterfaceToolbar::createString(iface_toolbar_control *control)
{
QLabel *label = new QLabel(QString().fromUtf8(control->display));
label->setToolTip(QString().fromUtf8(control->tooltip));
InterfaceToolbarLineEdit *lineedit = new InterfaceToolbarLineEdit(NULL, control->validation, control->is_required);
lineedit->setToolTip(QString().fromUtf8(control->tooltip));
lineedit->setPlaceholderText(QString().fromUtf8(control->placeholder));
if (control->default_value.string)
{
lineedit->setText(QString().fromUtf8(control->default_value.string));
setDefaultValue(control->num, control->default_value.string);
}
connect(lineedit, SIGNAL(editedTextApplied()), this, SLOT(onLineEditChanged()));
ui->leftLayout->addWidget(label);
ui->leftLayout->addWidget(lineedit);
label_widget_[control->num] = label;
use_spacer_ = false;
return lineedit;
}
void InterfaceToolbar::setWidgetValue(QWidget *widget, int command, QByteArray payload)
{
if (QComboBox *combobox = qobject_cast<QComboBox *>(widget))
{
combobox->blockSignals(true);
switch (command)
{
case commandControlSet:
{
int idx = combobox->findData(payload);
if (idx != -1)
{
combobox->setCurrentIndex(idx);
}
break;
}
case commandControlAdd:
{
QString value;
QString display;
if (payload.contains('\0'))
{
// The payload contains "value\0display"
QList<QByteArray> values = payload.split('\0');
value = values[0];
display = values[1];
}
else
{
value = display = payload;
}
int idx = combobox->findData(value);
if (idx != -1)
{
// The value already exists, update item text
combobox->setItemText(idx, display);
}
else
{
combobox->addItem(display, value);
}
break;
}
case commandControlRemove:
{
if (payload.size() == 0)
{
combobox->clear();
}
else
{
int idx = combobox->findData(payload);
if (idx != -1)
{
combobox->removeItem(idx);
}
}
break;
}
default:
break;
}
combobox->blockSignals(false);
}
else if (InterfaceToolbarLineEdit *lineedit = qobject_cast<InterfaceToolbarLineEdit *>(widget))
{
// We don't block signals here because changes are applied with enter or apply button,
// and we want InterfaceToolbarLineEdit to always syntax check the text.
switch (command)
{
case commandControlSet:
lineedit->setText(payload);
lineedit->disableApplyButton();
break;
default:
break;
}
}
else if (QCheckBox *checkbox = qobject_cast<QCheckBox *>(widget))
{
checkbox->blockSignals(true);
switch (command)
{
case commandControlSet:
{
Qt::CheckState state = Qt::Unchecked;
if (payload.size() > 0 && payload.at(0) != 0)
{
state = Qt::Checked;
}
checkbox->setCheckState(state);
break;
}
default:
break;
}
checkbox->blockSignals(false);
}
else if (QPushButton *button = qobject_cast<QPushButton *>(widget))
{
if ((command == commandControlSet) &&
widget->property(interface_role_property).toInt() == INTERFACE_ROLE_CONTROL)
{
button->setText(payload);
}
}
}
void InterfaceToolbar::setInterfaceValue(QString ifname, QWidget *widget, int num, int command, QByteArray payload)
{
if (!widget) {
return;
}
if (qobject_cast<QComboBox *>(widget))
{
switch (command)
{
case commandControlSet:
if (interface_[ifname].value[num] != payload)
{
interface_[ifname].value_changed[num] = false;
}
foreach (QByteArray entry, interface_[ifname].list[num])
{
if (entry == payload || entry.startsWith(payload + '\0'))
{
interface_[ifname].value[num] = payload;
}
}
break;
case commandControlAdd:
interface_[ifname].list[num].append(payload);
break;
case commandControlRemove:
if (payload.size() == 0)
{
interface_[ifname].value[num].clear();
interface_[ifname].list[num].clear();
}
else
{
foreach (QByteArray entry, interface_[ifname].list[num])
{
if (entry == payload || entry.startsWith(payload + '\0'))
{
interface_[ifname].list[num].removeAll(entry);
}
}
}
break;
default:
break;
}
}
else if (qobject_cast<InterfaceToolbarLineEdit *>(widget))
{
switch (command)
{
case commandControlSet:
if (interface_[ifname].value[num] != payload)
{
interface_[ifname].value_changed[num] = false;
}
interface_[ifname].value[num] = payload;
break;
default:
break;
}
}
else if ((widget->property(interface_type_property).toInt() == INTERFACE_TYPE_BUTTON) &&
(widget->property(interface_role_property).toInt() == INTERFACE_ROLE_LOGGER))
{
if (command == commandControlSet)
{
if (interface_[ifname].log_dialog.contains(num))
{
interface_[ifname].log_dialog[num]->clearText();
}
interface_[ifname].log_text.clear();
}
if (command == commandControlSet || command == commandControlAdd)
{
if (interface_[ifname].log_dialog.contains(num))
{
interface_[ifname].log_dialog[num]->appendText(payload);
}
interface_[ifname].log_text[num].append(payload);
}
}
else if (widget->property(interface_role_property).toInt() == INTERFACE_ROLE_CONTROL)
{
// QCheckBox or QPushButton
switch (command)
{
case commandControlSet:
if (interface_[ifname].value[num] != payload)
{
interface_[ifname].value_changed[num] = false;
}
interface_[ifname].value[num] = payload;
break;
default:
break;
}
}
}
void InterfaceToolbar::controlReceived(QString ifname, int num, int command, QByteArray payload)
{
switch (command)
{
case commandControlSet:
case commandControlAdd:
case commandControlRemove:
if (control_widget_.contains(num))
{
QWidget *widget = control_widget_[num];
setInterfaceValue(ifname, widget, num, command, payload);
if (ifname.compare(ui->interfacesComboBox->currentText()) == 0)
{
setWidgetValue(widget, command, payload);
}
}
break;
case commandControlEnable:
case commandControlDisable:
if (control_widget_.contains(num))
{
QWidget *widget = control_widget_[num];
if (widget->property(interface_role_property).toInt() == INTERFACE_ROLE_CONTROL)
{
bool enable = (command == commandControlEnable ? true : false);
interface_[ifname].widget_disabled[num] = !enable;
if (ifname.compare(ui->interfacesComboBox->currentText()) == 0)
{
widget->setEnabled(enable);
if (label_widget_.contains(num))
{
label_widget_[num]->setEnabled(enable);
}
}
}
}
break;
case commandStatusMessage:
mainApp->pushStatus(MainApplication::TemporaryStatus, payload);
break;
case commandInformationMessage:
simple_dialog_async(ESD_TYPE_INFO, ESD_BTN_OK, "%s", payload.data());
break;
case commandWarningMessage:
simple_dialog_async(ESD_TYPE_WARN, ESD_BTN_OK, "%s", payload.data());
break;
case commandErrorMessage:
simple_dialog_async(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", payload.data());
break;
default:
// Unknown commands are silently ignored
break;
}
}
void InterfaceToolbar::controlSend(QString ifname, int num, int command, const QByteArray &payload = QByteArray())
{
if (payload.length() > 65535)
{
// Not supported
return;
}
if (ifname.isEmpty() || interface_[ifname].out_fd == -1)
{
// Does not have a control out channel
return;
}
ssize_t payload_length = payload.length() + 2;
unsigned char high_nibble = (payload_length >> 16) & 0xFF;
unsigned char mid_nibble = (payload_length >> 8) & 0xFF;
unsigned char low_nibble = (payload_length >> 0) & 0xFF;
QByteArray ba;
ba.append(SP_TOOLBAR_CTRL);
ba.append(high_nibble);
ba.append(mid_nibble);
ba.append(low_nibble);
ba.append(num);
ba.append(command);
ba.append(payload);
if (ws_write(interface_[ifname].out_fd, ba.data(), ba.length()) != ba.length())
{
simple_dialog_async(ESD_TYPE_ERROR, ESD_BTN_OK,
"Unable to send control message:\n%s.",
g_strerror(errno));
}
}
void InterfaceToolbar::onControlButtonClicked()
{
const QString &ifname = ui->interfacesComboBox->currentText();
QPushButton *button = static_cast<QPushButton *>(sender());
int num = control_widget_.key(button);
controlSend(ifname, num, commandControlSet);
}
void InterfaceToolbar::onCheckBoxChanged(int state)
{
const QString &ifname = ui->interfacesComboBox->currentText();
QCheckBox *checkbox = static_cast<QCheckBox *>(sender());
int num = control_widget_.key(checkbox);
QByteArray payload(1, state == Qt::Unchecked ? 0 : 1);
controlSend(ifname, num, commandControlSet, payload);
interface_[ifname].value[num] = payload;
interface_[ifname].value_changed[num] = true;
}
void InterfaceToolbar::onComboBoxChanged(int idx)
{
const QString &ifname = ui->interfacesComboBox->currentText();
QComboBox *combobox = static_cast<QComboBox *>(sender());
int num = control_widget_.key(combobox);
QString value = combobox->itemData(idx).toString();
QByteArray payload(value.toUtf8());
controlSend(ifname, num, commandControlSet, payload);
interface_[ifname].value[num] = payload;
interface_[ifname].value_changed[num] = true;
}
void InterfaceToolbar::onLineEditChanged()
{
const QString &ifname = ui->interfacesComboBox->currentText();
InterfaceToolbarLineEdit *lineedit = static_cast<InterfaceToolbarLineEdit *>(sender());
int num = control_widget_.key(lineedit);
QByteArray payload(lineedit->text().toUtf8());
controlSend(ifname, num, commandControlSet, payload);
interface_[ifname].value[num] = payload;
interface_[ifname].value_changed[num] = true;
}
void InterfaceToolbar::onLogButtonClicked()
{
const QString &ifname = ui->interfacesComboBox->currentText();
QPushButton *button = static_cast<QPushButton *>(sender());
int num = control_widget_.key(button);
if (!interface_[ifname].log_dialog.contains(num))
{
interface_[ifname].log_dialog[num] = new FunnelTextDialog(window(), ifname + " " + button->text());
connect(interface_[ifname].log_dialog[num], SIGNAL(accepted()), this, SLOT(closeLog()));
connect(interface_[ifname].log_dialog[num], SIGNAL(rejected()), this, SLOT(closeLog()));
interface_[ifname].log_dialog[num]->setText(interface_[ifname].log_text[num]);
}
interface_[ifname].log_dialog[num]->show();
interface_[ifname].log_dialog[num]->raise();
interface_[ifname].log_dialog[num]->activateWindow();
}
void InterfaceToolbar::onHelpButtonClicked()
{
QUrl help_url(help_link_);
if (help_url.scheme().compare("file") != 0)
{
QDesktopServices::openUrl(help_url);
}
}
void InterfaceToolbar::closeLog()
{
FunnelTextDialog *log_dialog = static_cast<FunnelTextDialog *>(sender());
foreach (QString ifname, interface_.keys())
{
foreach (int num, control_widget_.keys())
{
if (interface_[ifname].log_dialog.value(num) == log_dialog)
{
interface_[ifname].log_dialog.remove(num);
}
}
}
}
void InterfaceToolbar::startReaderThread(QString ifname, void *control_in)
{
QThread *thread = new QThread;
InterfaceToolbarReader *reader = new InterfaceToolbarReader(ifname, control_in);
reader->moveToThread(thread);
connect(thread, SIGNAL(started()), reader, SLOT(loop()));
connect(reader, SIGNAL(finished()), thread, SLOT(quit()));
connect(reader, SIGNAL(finished()), reader, SLOT(deleteLater()));
connect(thread, SIGNAL(finished()), reader, SLOT(deleteLater()));
connect(reader, SIGNAL(received(QString, int, int, QByteArray)),
this, SLOT(controlReceived(QString, int, int, QByteArray)));
interface_[ifname].reader_thread = thread;
thread->start();
}
void InterfaceToolbar::startCapture(GArray *ifaces)
{
if (!ifaces || ifaces->len == 0)
return;
const QString &selected_ifname = ui->interfacesComboBox->currentText();
QString first_capturing_ifname;
bool selected_found = false;
for (guint i = 0; i < ifaces->len; i++)
{
interface_options *interface_opts = &g_array_index(ifaces, interface_options, i);
QString ifname(interface_opts->name);
if (!interface_.contains(ifname))
// This interface is not for us
continue;
if (first_capturing_ifname.isEmpty())
first_capturing_ifname = ifname;
if (ifname.compare(selected_ifname) == 0)
selected_found = true;
if (interface_[ifname].out_fd != -1)
// Already have control channels for this interface
continue;
// Open control out channel
#ifdef _WIN32
startReaderThread(ifname, interface_opts->extcap_control_in_h);
// Duplicate control out handle and pass the duplicate handle to _open_osfhandle().
// This allows the C run-time file descriptor (out_fd) and the extcap_control_out_h to be closed independently.
// The duplicated handle will get closed at the same time the file descriptor is closed.
// The control out pipe will close when both out_fd and extcap_control_out_h are closed.
HANDLE duplicate_out_handle = INVALID_HANDLE_VALUE;
if (!DuplicateHandle(GetCurrentProcess(), interface_opts->extcap_control_out_h,
GetCurrentProcess(), &duplicate_out_handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
{
simple_dialog_async(ESD_TYPE_ERROR, ESD_BTN_OK,
"Failed to duplicate extcap control out handle: %s\n.",
win32strerror(GetLastError()));
}
else
{
interface_[ifname].out_fd = _open_osfhandle((intptr_t)duplicate_out_handle, O_APPEND | O_BINARY);
}
#else
startReaderThread(ifname, interface_opts->extcap_control_in);
interface_[ifname].out_fd = ws_open(interface_opts->extcap_control_out, O_WRONLY | O_BINARY, 0);
#endif
sendChangedValues(ifname);
controlSend(ifname, 0, commandControlInitialized);
}
if (!selected_found && !first_capturing_ifname.isEmpty())
{
ui->interfacesComboBox->setCurrentText(first_capturing_ifname);
}
else
{
updateWidgets();
}
}
void InterfaceToolbar::stopCapture()
{
foreach (QString ifname, interface_.keys())
{
if (interface_[ifname].reader_thread)
{
if (!interface_[ifname].reader_thread->isFinished())
{
interface_[ifname].reader_thread->requestInterruption();
}
interface_[ifname].reader_thread = NULL;
}
if (interface_[ifname].out_fd != -1)
{
ws_close_if_possible (interface_[ifname].out_fd);
interface_[ifname].out_fd = -1;
}
foreach (int num, control_widget_.keys())
{
// Reset disabled property for all widgets
interface_[ifname].widget_disabled[num] = false;
QWidget *widget = control_widget_[num];
if ((widget->property(interface_type_property).toInt() == INTERFACE_TYPE_BUTTON) &&
(widget->property(interface_role_property).toInt() == INTERFACE_ROLE_CONTROL))
{
// Reset default value for control buttons
interface_[ifname].value[num] = default_value_[num];
if (ifname.compare(ui->interfacesComboBox->currentText()) == 0)
{
setWidgetValue(widget, commandControlSet, default_value_[num]);
}
}
}
}
updateWidgets();
}
void InterfaceToolbar::sendChangedValues(QString ifname)
{
// Send all values which has changed
foreach (int num, control_widget_.keys())
{
QWidget *widget = control_widget_[num];
if ((interface_[ifname].value_changed[num]) &&
(widget->property(interface_type_property).toInt() != INTERFACE_TYPE_BUTTON) &&
(widget->property(interface_role_property).toInt() == INTERFACE_ROLE_CONTROL))
{
controlSend(ifname, num, commandControlSet, interface_[ifname].value[num]);
}
}
}
void InterfaceToolbar::onRestoreButtonClicked()
{
const QString &ifname = ui->interfacesComboBox->currentText();
// Set default values to all widgets and interfaces
foreach (int num, control_widget_.keys())
{
QWidget *widget = control_widget_[num];
if (default_list_[num].size() > 0)
{
// This is a QComboBox. Clear list and add new entries.
setWidgetValue(widget, commandControlRemove, QByteArray());
interface_[ifname].list[num].clear();
foreach (QByteArray value, default_list_[num])
{
setWidgetValue(widget, commandControlAdd, value);
interface_[ifname].list[num].append(value);
}
}
switch (widget->property(interface_role_property).toInt())
{
case INTERFACE_ROLE_CONTROL:
setWidgetValue(widget, commandControlSet, default_value_[num]);
interface_[ifname].value[num] = default_value_[num];
interface_[ifname].value_changed[num] = false;
break;
case INTERFACE_ROLE_LOGGER:
if (interface_[ifname].log_dialog.contains(num))
{
interface_[ifname].log_dialog[num]->clearText();
}
interface_[ifname].log_text[num].clear();
break;
default:
break;
}
}
}
bool InterfaceToolbar::hasInterface(QString ifname)
{
return interface_.contains(ifname);
}
void InterfaceToolbar::updateWidgets()
{
const QString &ifname = ui->interfacesComboBox->currentText();
bool is_capturing = (interface_[ifname].out_fd == -1 ? false : true);
foreach (int num, control_widget_.keys())
{
QWidget *widget = control_widget_[num];
bool widget_enabled = true;
if (ifname.isEmpty() &&
(widget->property(interface_role_property).toInt() != INTERFACE_ROLE_HELP))
{
// No interface selected, disable all but Help button
widget_enabled = false;
}
else if (!is_capturing &&
(widget->property(interface_type_property).toInt() == INTERFACE_TYPE_BUTTON) &&
(widget->property(interface_role_property).toInt() == INTERFACE_ROLE_CONTROL))
{
widget_enabled = false;
}
else if (widget->property(interface_role_property).toInt() == INTERFACE_ROLE_CONTROL)
{
widget_enabled = !interface_[ifname].widget_disabled[num];
}
widget->setEnabled(widget_enabled);
if (label_widget_.contains(num))
{
label_widget_[num]->setEnabled(widget_enabled);
}
}
foreach (int num, control_widget_.keys())
{
QWidget *widget = control_widget_[num];
if ((widget->property(interface_type_property).toInt() == INTERFACE_TYPE_BUTTON) &&
(widget->property(interface_role_property).toInt() == INTERFACE_ROLE_RESTORE))
{
widget->setEnabled(!is_capturing);
}
}
}
void InterfaceToolbar::interfaceListChanged()
{
#ifdef HAVE_LIBPCAP
const QString &selected_ifname = ui->interfacesComboBox->currentText();
bool keep_selected = false;
ui->interfacesComboBox->blockSignals(true);
ui->interfacesComboBox->clear();
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->hidden)
continue;
if (interface_.keys().contains(device->name))
{
ui->interfacesComboBox->addItem(device->name);
if (selected_ifname.compare(device->name) == 0)
{
// Keep selected interface
ui->interfacesComboBox->setCurrentText(device->name);
keep_selected = true;
}
}
}
ui->interfacesComboBox->blockSignals(false);
if (!keep_selected)
{
// Select the first interface
on_interfacesComboBox_currentTextChanged(ui->interfacesComboBox->currentText());
}
updateWidgets();
#endif
}
void InterfaceToolbar::on_interfacesComboBox_currentTextChanged(const QString &ifname)
{
foreach (int num, control_widget_.keys())
{
QWidget *widget = control_widget_[num];
if (interface_[ifname].list[num].size() > 0)
{
// This is a QComboBox. Clear list and add new entries.
setWidgetValue(widget, commandControlRemove, QByteArray());
foreach (QByteArray value, interface_[ifname].list[num])
{
setWidgetValue(widget, commandControlAdd, value);
}
}
if (widget->property(interface_role_property).toInt() == INTERFACE_ROLE_CONTROL)
{
setWidgetValue(widget, commandControlSet, interface_[ifname].value[num]);
}
}
updateWidgets();
}
|