-
Notifications
You must be signed in to change notification settings - Fork 0
/
upnp.cpp
306 lines (278 loc) · 11 KB
/
upnp.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
/*
* 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 <QString>
#include <QTcpSocket>
#include <QTimer>
#include <QFile>
#include <QUdpSocket>
#include <QVariantMap>
#include "de_web_plugin.h"
#include "de_web_plugin_private.h"
#include "gateway_scanner.h"
/*! Inits the UPnP discorvery. */
void DeRestPluginPrivate::initUpnpDiscovery()
{
DBG_Assert(udpSock == 0);
initDescriptionXml();
if (deCONZ::appArgumentNumeric("--upnp", 1) == 0)
{
udpSock = 0;
joinedMulticastGroup = false;
return;
}
udpSock = new QUdpSocket(this);
joinedMulticastGroup = false;
connect(udpSock, SIGNAL(readyRead()),
this, SLOT(upnpReadyRead()));
upnpTimer = new QTimer(this);
upnpTimer->setSingleShot(false);
connect(upnpTimer, SIGNAL(timeout()),
this, SLOT(announceUpnp()));
upnpTimer->start(1000); // setup phase fast interval
}
/*! Replaces description_in.xml template with dynamic content. */
void DeRestPluginPrivate::initDescriptionXml()
{
deCONZ::ApsController *apsCtrl = deCONZ::ApsController::instance();
QString serverRoot = apsCtrl->getParameter(deCONZ::ParamHttpRoot);
if (!serverRoot.isEmpty())
{
descriptionXml.clear();
QFile f(serverRoot + "/description_in.xml");
if (f.open(QFile::ReadOnly))
{
QByteArray line;
do {
line = f.readLine(320);
if (!line.isEmpty())
{
line.replace(QString("{{IPADDRESS}}"), qPrintable(gwIPAddress));
line.replace(QString("{{PORT}}"), qPrintable(QString::number(gwPort)));
line.replace(QString("{{GWNAME}}"), qPrintable(gwName));
line.replace(QString("{{SERIAL}}"), qPrintable(gwBridgeId.left(6) + gwBridgeId.right(6)));
line.replace(QString("{{UUID}}"), qPrintable(gwUuid));
descriptionXml.append(line);
DBG_Printf(DBG_INFO_L2, "%s", line.constData());
}
} while (!line.isEmpty());
}
}
}
/*! Sends SSDP broadcast for announcement. */
void DeRestPluginPrivate::announceUpnp()
{
if (udpSock->state() != QAbstractSocket::BoundState)
{
joinedMulticastGroup = false;
DBG_Printf(DBG_ERROR, "UPNP socket not bound, state: %d\n", udpSock->state());
// retry
if (!udpSock->bind(QHostAddress("0.0.0.0"), 1900, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint)) // SSDP
{
DBG_Printf(DBG_ERROR, "UPNP error %s\n", qPrintable(udpSock->errorString()));
}
return;
}
if (!joinedMulticastGroup)
{
QHostAddress groupAddress("239.255.255.250");
if (!udpSock->joinMulticastGroup(groupAddress))
{
DBG_Printf(DBG_ERROR, "UPNP error %s\n", qPrintable(udpSock->errorString()));
return;
}
joinedMulticastGroup = true;
}
if (upnpTimer->interval() != (20 * 1000))
upnpTimer->start(20 * 1000);
QByteArray datagram = QString(QLatin1String(
"NOTIFY * HTTP/1.1\r\n"
"HOST: 239.255.255.250:1900\r\n"
"CACHE-CONTROL: max-age=100\r\n"
"LOCATION: http://%1:%2/description.xml\r\n"
"SERVER: Linux/3.14.0 UPnP/1.0 IpBridge/1.26.0\r\n"
"GWID.phoscon.de: %3\r\n"
"hue-bridgeid: %3\r\n"
"NTS: ssdp:alive\r\n"))
.arg(gwConfig["ipaddress"].toString())
.arg(gwConfig["port"].toDouble())
.arg(gwBridgeId.toUpper()).toLocal8Bit();
QByteArray nt1 = QString(QLatin1String(
"NT: upnp:rootdevice\r\n"
"USN: uuid:%1::upnp:rootdevice\r\n")).arg(gwConfig["uuid"].toString()).toLocal8Bit();
QByteArray nt2 = QString(QLatin1String(
"NT: uuid:%1\r\n"
"USN: uuid:%1\r\n")).arg(gwConfig["uuid"].toString()).toLocal8Bit();
QByteArray nt3 = QString(QLatin1String(
"NT: urn:schemas-upnp-org:device:basic:1\r\n"
"USN: uuid:%1::urn:schemas-upnp-org:device:basic:1\r\n")).arg(gwConfig["uuid"].toString()).toLocal8Bit();
quint16 port = 1900;
QHostAddress host;
host.setAddress(QLatin1String("239.255.255.250"));
if (udpSock->writeDatagram(datagram + nt1 + "\r\n", host, port) == -1)
{
DBG_Printf(DBG_ERROR, "UDP send error %s\n", qPrintable(udpSock->errorString()));
}
if (udpSock->writeDatagram(datagram + nt2 + "\r\n", host, port) == -1)
{
DBG_Printf(DBG_ERROR, "UDP send error %s\n", qPrintable(udpSock->errorString()));
}
if (udpSock->writeDatagram(datagram + nt3 + "\r\n", host, port) == -1)
{
DBG_Printf(DBG_ERROR, "UDP send error %s\n", qPrintable(udpSock->errorString()));
}
}
/*! Handles SSDP packets. */
void DeRestPluginPrivate::upnpReadyRead()
{
while (udpSock->hasPendingDatagrams())
{
quint16 port;
QHostAddress host;
QByteArray datagram;
datagram.resize(udpSock->pendingDatagramSize());
udpSock->readDatagram(datagram.data(), datagram.size(), &host, &port);
QTextStream stream(datagram);
enum ST { SearchTargetOther, SearchTargetAll, SearchTargetRoot, SearchTargetUUID, SearchTargetBasic };
ST st = SearchTargetOther;
QString location;
QString gwid;
if (DBG_IsEnabled(DBG_HTTP))
{
DBG_Printf(DBG_HTTP, "%s\n", qPrintable(datagram));
}
if (datagram.startsWith("M-SEARCH *"))
{
while (!stream.atEnd())
{
QString line = stream.readLine();
if (line.startsWith(QLatin1String("ST:")))
{
if (line.contains(QLatin1String("ssdp:all")))
{
st = SearchTargetAll;
break;
}
else if (line.contains(QLatin1String("upnp:rootdevice")))
{
st = SearchTargetRoot;
break;
}
else if (line.contains(QString(QLatin1String("ST: uuid:%1")).arg(gwConfig["uuid"].toString())))
{
st = SearchTargetUUID;
break;
}
else if (line.contains(QLatin1String("urn:schemas-upnp-org:device:basic:1")))
{
st = SearchTargetBasic;
break;
}
}
}
if (st != SearchTargetOther)
{
QByteArray response = QString(QLatin1String(
"HTTP/1.1 200 OK\r\n"
"CACHE-CONTROL: max-age=100\r\n"
"EXT:\r\n"
"LOCATION: http://%1:%2/description.xml\r\n"
"SERVER: Linux/3.14.0 UPnP/1.0 IpBridge/1.26.0\r\n"
"GWID.phoscon.de: %3\r\n"
"hue-bridgeid: %3\r\n"))
.arg(gwConfig["ipaddress"].toString())
.arg(gwConfig["port"].toDouble())
.arg(gwBridgeId.toUpper()).toLocal8Bit();
QByteArray st1 = QString(QLatin1String(
"ST: upnp:rootdevice\r\n"
"USN: uuid:%1::upnp:rootdevice\r\n")).arg(gwConfig["uuid"].toString()).toLocal8Bit();
QByteArray st2 = QString(QLatin1String(
"ST: uuid:%1\r\n"
"USN: uuid:%1\r\n")).arg(gwConfig["uuid"].toString()).toLocal8Bit();
QByteArray st3 = QString(QLatin1String(
"ST: urn:schemas-upnp-org:device:basic:1\r\n"
"USN: uuid:%1::urn:schemas-upnp-org:device:basic:1\r\n")).arg(gwConfig["uuid"].toString()).toLocal8Bit();
datagram.clear();
if (st == SearchTargetAll || st == SearchTargetRoot)
{
datagram.append(response);
datagram.append(st1);
datagram.append("\r\n");
if (udpSock->writeDatagram(datagram.data(), datagram.size(), host, port) == -1)
{
DBG_Printf(DBG_ERROR, "UDP send error %s\n", qPrintable(udpSock->errorString()));
}
datagram.clear();
}
if (st == SearchTargetAll || st == SearchTargetUUID)
{
datagram.append(response);
datagram.append(st2);
datagram.append("\r\n");
if (udpSock->writeDatagram(datagram.data(), datagram.size(), host, port) == -1)
{
DBG_Printf(DBG_ERROR, "UDP send error %s\n", qPrintable(udpSock->errorString()));
}
datagram.clear();
}
if (st == SearchTargetAll || st == SearchTargetBasic)
{
datagram.append(response);
datagram.append(st3);
datagram.append("\r\n");
if (udpSock->writeDatagram(datagram.data(), datagram.size(), host, port) == -1)
{
DBG_Printf(DBG_ERROR, "UDP send error %s\n", qPrintable(udpSock->errorString()));
}
datagram.clear();
}
}
else if (datagram.startsWith("NOTIFY *") && !location.isEmpty())
{
while (!stream.atEnd())
{
QString line = stream.readLine();
if (line.startsWith(QLatin1String("LOCATION:")))
{
location = line;
}
else if (line.startsWith(QLatin1String("GWID.phoscon.de")))
{
gwid = line;
break;
}
else if (line.startsWith(QLatin1String("hue-bridgeid")))
{
gwid = line;
break;
}
}
// phoscon gateway or hue bridge
QStringList ls = gwid.split(' ');
if (ls.size() != 2)
{
continue;
}
if (ls[1] == gwBridgeId)
{
continue; // self
}
ls = location.split(' ');
if (ls.size() != 2 || !ls[1].startsWith(QLatin1String("http://")))
{
continue;
}
// http://192.168.14.103:80/description.xml
QUrl url(ls[1]);
location = QString("http://%1:%2/api/config").arg(url.host()).arg(url.port(80));
gwScanner->queryGateway(location);
}
}
}
}