forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateway_scanner.cpp
363 lines (310 loc) · 8.05 KB
/
gateway_scanner.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
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
/*
* Copyright (c) 2016-2017 dresden elektronik ingenieurtechnik gmbh.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
* style license a copy of which has been included with this distribution in
* the LICENSE.txt file.
*
*/
#include <QApplication>
#include <QTimer>
#include <QNetworkAccessManager>
#include <QNetworkInterface>
#include <QNetworkReply>
#include <vector>
#include "gateway_scanner.h"
#include "deconz.h"
#include "json.h"
enum ScanState
{
StateInit,
StateIdle,
StateRunning,
};
enum ScanEvent
{
ActionProcess,
EventTimeout,
EventGotReply
};
class GatewayScannerPrivate
{
public:
void initScanner();
void handleEvent(ScanEvent event);
void startScanTimer(int msec, ScanEvent action);
void stopTimer();
void queryNextIp();
void processReply();
GatewayScanner *q;
ScanState state;
QNetworkAccessManager *manager;
QNetworkReply *reply;
QTimer *timer;
ScanEvent timerAction;
std::vector<quint32> interfaces;
quint32 scanIp;
quint16 scanPort;
int scanIteration;
quint32 host;
size_t interfaceIter;
};
GatewayScanner::GatewayScanner(QObject *parent) :
QObject(parent),
d_ptr(new GatewayScannerPrivate)
{
Q_D(GatewayScanner);
d->q = this;
d->scanIteration = 0;
d->state = StateInit;
d->manager = new QNetworkAccessManager(this);
connect(d->manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinished(QNetworkReply*)));
d->timer = new QTimer(this);
d->timer->setSingleShot(true);
connect(d->timer, SIGNAL(timeout()), this, SLOT(scanTimerFired()));
}
GatewayScanner::~GatewayScanner()
{
Q_ASSERT(d_ptr != nullptr);
if (d_ptr)
{
delete d_ptr;
d_ptr = nullptr;
}
}
bool GatewayScanner::isRunning() const
{
Q_D(const GatewayScanner);
return (d->state != StateInit);
}
void GatewayScanner::queryGateway(const QString &url)
{
Q_D(GatewayScanner);
if (!isRunning() && d->reply == nullptr)
{
d->reply = d->manager->get(QNetworkRequest(url));
QObject::connect(d->reply, SIGNAL(error(QNetworkReply::NetworkError)),
d->manager->parent(), SLOT(onError(QNetworkReply::NetworkError)));
}
}
void GatewayScanner::startScan()
{
Q_D(GatewayScanner);
// TODO fix: GLib-ERROR **: Creating pipes for GWakeup: Too many open files
if (d->state == StateInit)
{
d->startScanTimer(1, ActionProcess);
}
}
void GatewayScanner::scanTimerFired()
{
Q_D(GatewayScanner);
d->handleEvent(d->timerAction);
}
void GatewayScanner::requestFinished(QNetworkReply *reply)
{
Q_D(GatewayScanner);
if (reply == d->reply)
{
d->processReply();
}
if (isRunning())
{
d->handleEvent(EventGotReply);
}
reply->deleteLater();
}
void GatewayScannerPrivate::processReply()
{
if (!reply)
{
return;
}
QNetworkReply *r = reply;
reply = nullptr;
int code = r->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if (code != 200) // not authorized or ok
{
return;
}
bool ok;
QVariant var = Json::parse(r->readAll(), ok);
if (!ok)
{
return;
}
QVariantMap map = var.toMap();
if (map.isEmpty())
{
return;
}
if (!map.contains(QLatin1String("bridgeid")) ||
!map.contains(QLatin1String("modelid")) ||
!map.contains(QLatin1String("name")))
{
return;
}
QString name = map["name"].toString();
//QString modelid = map["modelid"].toString();
QString bridgeid = map["bridgeid"].toString();
QUrl url = r->url();
QHostAddress host(url.host());
if (host.isNull() || name.isEmpty() || bridgeid.isEmpty())
{
return;
}
//DBG_Printf(DBG_INFO, "GW: %s %s, %s, %s\n", qPrintable(url.host()), qPrintable(name), qPrintable(modelid), qPrintable(bridgeid));
q->foundGateway(host, static_cast<quint16>(url.port(80)), bridgeid, name);
}
void GatewayScanner::onError(QNetworkReply::NetworkError code)
{
Q_D(GatewayScanner);
Q_UNUSED(code)
sender()->deleteLater();
if (!d->timer->isActive())
{
// must be in timeout window
return;
}
if (d->reply && sender() == d->reply)
{
//DBG_Printf(DBG_INFO, "reply err: %d\n", code);
d->timer->stop();
d->handleEvent(EventGotReply);
}
}
void GatewayScannerPrivate::initScanner()
{
QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();
QList<QNetworkInterface>::Iterator ifi = ifaces.begin();
QList<QNetworkInterface>::Iterator ifend = ifaces.end();
for (; ifi != ifend; ++ifi)
{
QString name = ifi->humanReadableName();
// filter
if (name.contains("vm", Qt::CaseInsensitive) ||
name.contains("virtual", Qt::CaseInsensitive) ||
name.contains("loop", Qt::CaseInsensitive))
{
continue;
}
QList<QNetworkAddressEntry> addr = ifi->addressEntries();
QList<QNetworkAddressEntry>::Iterator i = addr.begin();
QList<QNetworkAddressEntry>::Iterator end = addr.end();
for (; i != end; ++i)
{
QHostAddress a = i->ip();
if (a.protocol() == QAbstractSocket::IPv4Protocol)
{
quint32 ipv4 = a.toIPv4Address();
if ((ipv4 & 0xff000000UL) == 0x7f000000UL)
{
// 127.x.x.x
continue;
}
if (std::find(interfaces.begin(), interfaces.end(), ipv4) == interfaces.end())
{
interfaces.push_back(ipv4);
}
}
}
}
scanIteration++;
interfaceIter = 0;
host = 0;
}
void GatewayScannerPrivate::handleEvent(ScanEvent event)
{
if (state == StateInit)
{
if (event == ActionProcess)
{
initScanner();
state = StateIdle;
startScanTimer(10, ActionProcess);
}
else
{
Q_ASSERT(0);
}
}
else if (state == StateIdle)
{
if (event == ActionProcess)
{
queryNextIp();
}
else if (event == EventTimeout)
{
QNetworkReply *r = reply;
if (reply)
{
reply = nullptr;
if (r->isRunning())
{
r->abort();
}
r->deleteLater();
}
host++;
startScanTimer(1000, ActionProcess);
}
else if (event == EventGotReply)
{
host++;
startScanTimer(1, ActionProcess);
}
else
{
Q_ASSERT(0);
}
}
else
{
Q_ASSERT(0);
}
}
void GatewayScannerPrivate::startScanTimer(int msec, ScanEvent action)
{
timerAction = action;
timer->stop();
timer->start(msec);
}
void GatewayScannerPrivate::stopTimer()
{
timer->stop();
}
void GatewayScannerPrivate::queryNextIp()
{
if (!interfaces.empty() && host > 255)
{
interfaces.pop_back();
host = 0;
}
if (interfaces.empty())
{
state = StateInit;
DBG_Printf(DBG_INFO, "scan finished\n");
return;
}
scanIp = interfaces.back();
scanPort = 80;
if (host == (scanIp & 0x000000fful))
{
DBG_Printf(DBG_INFO, "scan skip host .%u\n", host);
host++; // don't scan own ip
}
const QString url = QString("http://%1.%2.%3.%4:%5/api/config")
.arg(((scanIp >> 24) & 0xff))
.arg(((scanIp >> 16) & 0xff))
.arg(((scanIp >> 8) & 0xff))
.arg(host & 0xff).arg(scanPort);
scanIp &= 0xffffff00ull;
scanIp |= host & 0xff;
//DBG_Printf(DBG_INFO, "scan %s\n", qPrintable(url));
reply = manager->get(QNetworkRequest(url));
QObject::connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
manager->parent(), SLOT(onError(QNetworkReply::NetworkError)));
startScanTimer(1000, EventTimeout);
}