-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget.cpp
301 lines (258 loc) · 8.3 KB
/
widget.cpp
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
#include "widget.h"
#include <QNetworkInterface>
#include <QHostAddress>
#include <QTimer>
#include <QDebug>
#include <QApplication>
#include <QMessageBox>
#include <QString>
Q_DECLARE_METATYPE(QNetworkAddressEntry)
class BooleanScopeGuard
{
public:
BooleanScopeGuard(std::atomic_bool &boolean) : boolean(boolean){ boolean = true; qDebug () << "True"; }
~BooleanScopeGuard() { boolean = false; qDebug () << "False"; }
private:
std::atomic_bool &boolean;
};
Widget::Widget()
: QObject(),
currentState(&trayIcon),
persistentActionGroup(this),
threadWorking(false),
timer(new QTimer(this)),
srcIpv4(0)
{
timer->setInterval(1000*10);
connect(timer, &QTimer::timeout, this, &Widget::onTimerEvent);
connect(this, &Widget::updateTrayMenu, this, &Widget::onUpdateTrayMenu);
std::vector<QNetworkAddressEntry> interfaces;
if (!setupNetworkInterface(interfaces)) {
qApp->quit();
return;
}
setupPersistentMenu(interfaces);
timer->start();
}
std::vector<QNetworkAddressEntry> Widget::getFilteredAddressEntries()
{
std::vector<QNetworkAddressEntry> resultVector;
QList<QNetworkInterface> interfaceList = QNetworkInterface::allInterfaces();
for (const QNetworkInterface &netInterface : interfaceList)
{
for (QNetworkAddressEntry &addressEntry : netInterface.addressEntries())
{
QHostAddress hostAddress = addressEntry.ip();
if (hostAddress.protocol() != QAbstractSocket::IPv4Protocol ||
hostAddress.isLoopback())
{
continue;
}
resultVector.push_back(addressEntry);
}
}
return resultVector;
}
void Widget::setupPersistentMenu(const std::vector<QNetworkAddressEntry> &interfaces)
{
currentState = NetworkState::Good;
persistentActionGroup.addAction(QStringLiteral("Interfaces"));
persistentActionGroup.addAction(QString())->setSeparator(true);
for (auto &&entry : interfaces)
{
QHostAddress hostAddr = entry.ip();
auto act = persistentActionGroup.addAction(hostAddr.toString());
act->setCheckable(true);
QVariant var;
var.setValue<QNetworkAddressEntry>(entry);
act->setData(var);
if (hostAddr.toIPv4Address() == srcIpv4) {
act->setChecked(true);
}
}
persistentActionGroup.addAction(QString())->setSeparator(true);
auto actExit = persistentActionGroup.addAction(QStringLiteral("Exit"));
connect(actExit, &QAction::triggered, qApp, &QApplication::quit);
connect(&persistentActionGroup, &QActionGroup::triggered, this, &Widget::onSetInterfaceActive);
trayContextMenu.addActions(persistentActionGroup.actions());
trayIcon.setContextMenu(&trayContextMenu);
}
bool Widget::setupNetworkInterface(std::vector<QNetworkAddressEntry> &interfaces)
{
if (srcIpv4 != 0) {
return true;
}
std::vector<QNetworkAddressEntry> addrEntries = getFilteredAddressEntries();
interfaces.reserve((int)addrEntries.size());
quint32 suitableAddress = 0;
for(auto &&addressEntry : addrEntries)
{
QHostAddress hostAddress = addressEntry.ip();
bool ok = false;
quint32 convertedAddress = hostAddress.toIPv4Address(&ok);
if (!ok) {
continue;
}
interfaces.push_back(addressEntry);
if (srcIpv4 == 0) {
suitableAddress = convertedAddress;
int netmask = 32 - addressEntry.prefixLength();
netStartIpv4 = ((suitableAddress >> netmask) << netmask) + 1;
netEndIpv4 = netStartIpv4 + (1 << netmask) - 2;
if (hostAddress.isInSubnet(QHostAddress(QStringLiteral("192.168.0.0")), 16) ||
hostAddress.isInSubnet(QHostAddress(QStringLiteral("10.0.0.0")), 8))
{
srcIpv4 = suitableAddress;
}
}
}
if (suitableAddress == 0)
{
QMessageBox::warning(0, QStringLiteral("No suitable Ipv4 interface found"), QStringLiteral("No suitable Ipv4 interface found.\nApplication is shutting down."));
return false;
}
//autoconf ipv4 ( 169.254.248.201 ) - invalid
if (suitableAddress == 0xA9FEF8C9 && srcIpv4 == 0)
{
QMessageBox::warning(0, QStringLiteral("Ipv4 autoconfigurated interface found"), QStringLiteral("Ipv4 autoconfigurated interface found.\nApplication is shutting down."));
return false;
}
else if (srcIpv4 == 0)
{
srcIpv4 = suitableAddress;
}
return true;
}
void Widget::stopScan()
{
if (pingerThread.joinable())
{
if (threadWorking) {
pinger.stop();
}
pingerThread.join();
}
}
Widget::~Widget()
{
stopScan();
}
void Widget::showIcon()
{
trayIcon.show();
}
void Widget::onTimerEvent()
{
qDebug () << "Timer event";
if (threadWorking) {
return;
}
if (pingerThread.joinable()) {
pingerThread.join();
}
pingerThread = std::thread([this](){
BooleanScopeGuard booleanScopeGuard(threadWorking);
qDebug () << "Ping started";
auto res = pinger.ping(srcIpv4, netStartIpv4, netEndIpv4, 7s);
qDebug () << res.size();
for (auto&& elem : res)
{
qDebug () << QHostAddress(elem).toString();
}
for (auto &&elem : res)
{
presentNodes[elem] = maxThrustLevel;
}
for (auto it = presentNodes.begin(); it != presentNodes.end();)
{
it->second--;
if (!it->second) {
it = presentNodes.erase(it);
}
else {
it++;
}
}
NetworkState::State tmpState;
//ping google dns to check internet access
quint32 googlePublicDNS = 0x08080808;
res = pinger.ping(srcIpv4, googlePublicDNS, googlePublicDNS+1, 1s);
qDebug () << res.size();
for (auto&& elem : res)
{
qDebug () << QHostAddress(elem).toString();
}
if (res.size() == 1 && res[0] == googlePublicDNS) {
tmpState = NetworkState::Good;
}
else
{
tmpState = NetworkState::NoInternetAccess;
}
if (presentNodes.size() == 0 ||
(presentNodes.size() == 1 && presentNodes.count(srcIpv4))) {
tmpState = NetworkState::NoLocalNetAccess;
}
currentState = tmpState;
emit updateTrayMenu();
});
}
void Widget::onUpdateTrayMenu()
{
trayContextMenu.clear();
qDeleteAll(temporaryActionsList);
temporaryActionsList.clear();
temporaryActionsList << new QAction(QStringLiteral("Present hosts"), this);
auto sep = new QAction(this);
sep->setSeparator(true);
temporaryActionsList << sep;
for (const auto &elem : presentNodes)
{
temporaryActionsList << new QAction(QHostAddress(elem.first).toString(),this);
}
sep = new QAction(this);
sep->setSeparator(true);
temporaryActionsList << sep;
trayContextMenu.addActions(temporaryActionsList);
trayContextMenu.addActions(persistentActionGroup.actions());
}
void Widget::onSetInterfaceActive(QAction *sender)
{
bool ok = false;
quint32 tempIPv4 = QHostAddress(sender->text()).toIPv4Address(&ok);
if (!ok || (tempIPv4 == srcIpv4)) {
return;
}
stopScan();
QNetworkAddressEntry addr = sender->data().value<QNetworkAddressEntry>();
srcIpv4 = tempIPv4;
int netmask = 32 - addr.prefixLength();
netStartIpv4 = ((srcIpv4 >> netmask) << netmask) + 1;
netEndIpv4 = netStartIpv4 + (1 << netmask) - 2;
}
Widget::NetworkState &Widget::NetworkState::operator=(const Widget::NetworkState::State &state)
{
if (currState == state) {
return *this;
}
currState = state;
switch (currState) {
case Good:
icon->setToolTip(QStringLiteral("Ok"));
icon->setIcon(QIcon(QStringLiteral(":/images/ok.png")));
break;
case NoInternetAccess:
icon->setToolTip(QStringLiteral("No internet access"));
icon->setIcon(QIcon(QStringLiteral(":/images/noint.png")));
break;
case NoLocalNetAccess:
icon->setToolTip(QStringLiteral("No local network access"));
icon->setIcon(QIcon(QStringLiteral(":/images/noloc.png")));
break;
default:
icon->setIcon(QIcon());
icon->setToolTip(QStringLiteral("Err"));
break;
}
return *this;
}