-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqtavahiservicebrowser.cpp
283 lines (245 loc) · 11.6 KB
/
qtavahiservicebrowser.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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Copyright 2013 - 2020, nymea GmbH
* Contact: [email protected]
*
* This file is part of nymea.
* This project including source code and documentation is protected by
* copyright law, and remains the property of nymea GmbH. All rights, including
* reproduction, publication, editing and translation, are reserved. The use of
* this project is subject to the terms of a license agreement to be concluded
* with nymea GmbH in accordance with the terms of use of nymea GmbH, available
* under https://nymea.io/license
*
* GNU Lesser General Public License Usage
* Alternatively, this project may be redistributed and/or modified under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; version 3. This project 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this project. If not, see <https://www.gnu.org/licenses/>.
*
* For any further details and any questions please contact us under
* [email protected] or see our FAQ/Licensing Information on
* https://nymea.io/license/faq
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "qtavahiservicebrowser.h"
#include <loggingcategories.h>
#include <avahi-common/strlst.h>
#include <avahi-common/error.h>
#include <QTimer>
QtAvahiServiceBrowser::QtAvahiServiceBrowser(QObject *parent): QObject(parent)
{
m_client = new QtAvahiClient(this);
m_serviceTypeBrowser = avahi_service_type_browser_new(m_client->m_client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, (AvahiLookupFlags) 0, QtAvahiServiceBrowser::serviceTypeBrowserCallback, this);
}
QtAvahiServiceBrowser::QtAvahiServiceBrowser(QtAvahiClient *client, QObject *parent):
QObject(parent),
m_client(client)
{
m_serviceTypeBrowser = avahi_service_type_browser_new(m_client->m_client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, (AvahiLookupFlags) 0, QtAvahiServiceBrowser::serviceTypeBrowserCallback, this);
}
QtAvahiServiceBrowser::~QtAvahiServiceBrowser()
{
foreach (AvahiServiceResolver *resolver, m_resolvers.toList()) {
avahi_service_resolver_free(resolver);
}
m_resolvers.clear();
while (!m_serviceBrowsers.isEmpty()) {
AvahiServiceBrowser *browser = m_serviceBrowsers.keys().first();
m_serviceBrowsers.take(browser);
avahi_service_browser_free(browser);
}
if (m_serviceTypeBrowser) {
avahi_service_type_browser_free(m_serviceTypeBrowser);
}
}
QList<ZeroConfServiceEntry> QtAvahiServiceBrowser::entries()
{
return m_entries;
}
void QtAvahiServiceBrowser::registerServiceBrowser(const QString &serviceType, const QString &domain, AvahiIfIndex interface, AvahiProtocol protocol)
{
AvahiServiceBrowser* browser = avahi_service_browser_new(m_client->m_client,
interface,
protocol,
serviceType.toUtf8().data(),
domain.toUtf8().data(),
(AvahiLookupFlags) 0,
QtAvahiServiceBrowser::serviceBrowserCallback,
this);
if (!browser) {
qCWarning(dcPlatformZeroConf()) << "Failed to create service browser for" << serviceType << domain << ":" << avahi_strerror(avahi_client_errno(m_client->m_client));
return;
}
BrowserInfo info;
info.type = serviceType;
info.domain = domain;
info.interface = interface;
info.protocol = protocol;
m_serviceBrowsers.insert(browser, info);
}
void QtAvahiServiceBrowser::unregisterServiceBrowser(const QString &serviceType, const QString &domain, AvahiIfIndex interface, AvahiProtocol protocol)
{
foreach (AvahiServiceBrowser *browser, m_serviceBrowsers.keys()) {
BrowserInfo info = m_serviceBrowsers.value(browser);
if (info.type == serviceType && info.domain == domain && info.interface == interface && info.protocol == protocol) {
m_serviceBrowsers.remove(browser);
avahi_service_browser_free(browser);
}
}
}
void QtAvahiServiceBrowser::registerServiceResolver(const QString &name, const QString &type, const QString &domain, AvahiIfIndex interface, AvahiProtocol protocol)
{
AvahiServiceResolver *resolver = avahi_service_resolver_new(m_client->m_client,
interface,
protocol,
name.toUtf8().data(),
type.toUtf8().data(),
domain.toUtf8().data(),
AVAHI_PROTO_UNSPEC,
(AvahiLookupFlags) 0,
QtAvahiServiceBrowser::serviceResolverCallback,
this);
if (!resolver) {
qCWarning(dcPlatformZeroConf()) << "Failed to resolve service" << type << name << ":" << avahi_strerror(avahi_client_errno(m_client->m_client));
return;
}
m_resolvers.insert(resolver);
}
void QtAvahiServiceBrowser::serviceTypeBrowserCallback(AvahiServiceTypeBrowser *browser, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *type, const char *domain, AvahiLookupResultFlags flags, void *userdata)
{
Q_UNUSED(browser)
Q_UNUSED(flags)
QtAvahiServiceBrowser *instance = static_cast<QtAvahiServiceBrowser*>(userdata);
switch (event) {
case AVAHI_BROWSER_NEW:
qCDebug(dcPlatformZeroConf()) << "New service type:" << type;
instance->registerServiceBrowser(type, domain, interface, protocol);
break;
case AVAHI_BROWSER_REMOVE:
qCDebug(dcPlatformZeroConf()) << "Service type removed:" << type;
instance->unregisterServiceBrowser(type, domain, interface, protocol);
break;
case AVAHI_BROWSER_CACHE_EXHAUSTED:
break;
case AVAHI_BROWSER_ALL_FOR_NOW:
break;
case AVAHI_BROWSER_FAILURE:
qCWarning(dcPlatformZeroConf()) << "Service type browser error:" << QString(avahi_strerror(avahi_client_errno(instance->m_client->m_client)));
break;
}
}
void QtAvahiServiceBrowser::serviceBrowserCallback(AvahiServiceBrowser *browser, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *name, const char *type, const char *domain, AvahiLookupResultFlags flags, void *userdata)
{
Q_UNUSED(browser)
Q_UNUSED(flags)
QtAvahiServiceBrowser *instance = static_cast<QtAvahiServiceBrowser*>(userdata);
switch (event) {
case AVAHI_BROWSER_NEW: {
// Start resolving new service
qCDebug(dcPlatformZeroConf()) << "New Service browser" << type << name;
instance->registerServiceResolver(name, type, domain, interface, protocol);
break;
}
case AVAHI_BROWSER_REMOVE: {
QMutableListIterator<ZeroConfServiceEntry> i(instance->m_entries);
while (i.hasNext()) {
ZeroConfServiceEntry entry = i.next();
// FIXME: In theory we'd need to compare interface too, but nymea's zeroconfentry doesn't store that information
if (entry.name() == name && entry.serviceType() == type && entry.domain() == domain && entry.protocol() == convertProtocol(protocol)) {
i.remove();
qCDebug(dcPlatformZeroConf()) << "Service removed:" << entry;
emit instance->serviceRemoved(entry);
}
}
break;
}
case AVAHI_BROWSER_ALL_FOR_NOW:
break;
case AVAHI_BROWSER_CACHE_EXHAUSTED:
break;
case AVAHI_BROWSER_FAILURE:
qCWarning(dcPlatformZeroConf()) << "Service browser error:" << QString(avahi_strerror(avahi_client_errno(instance->m_client->m_client)));
break;
}
}
void QtAvahiServiceBrowser::serviceResolverCallback(AvahiServiceResolver *resolver, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const char *name, const char *type, const char *domain, const char *host_name, const AvahiAddress *address, uint16_t port, AvahiStringList *txt, AvahiLookupResultFlags flags, void *userdata)
{
Q_UNUSED(interface)
QtAvahiServiceBrowser *instance = static_cast<QtAvahiServiceBrowser*>(userdata);
switch (event) {
case AVAHI_RESOLVER_FAILURE:
{
qCDebug(dcPlatformZeroConf()) << "Failed to resolve" << type << name;
// Retrying in 5 seconds... (Copying char* and capturing the copy in the lambda)
QString nameCopy(name);
QString typeCopy(type);
QString domainCopy(domain);
QTimer::singleShot(5000, instance, [=]{
instance->registerServiceResolver(nameCopy, typeCopy, domainCopy, interface, protocol);
});
break;
}
case AVAHI_RESOLVER_FOUND: {
qCDebug(dcPlatformZeroConf()) << "Resolved" << type << name;
char a[AVAHI_ADDRESS_STR_MAX];
avahi_address_snprint(a, sizeof(a), address);
QHostAddress hostAddress = QHostAddress(QString(a));
ZeroConfServiceEntry entry(name,
type,
hostAddress,
domain,
host_name,
port,
convertProtocol(protocol),
convertTxtList(txt),
flags & AVAHI_LOOKUP_RESULT_CACHED,
flags & AVAHI_LOOKUP_RESULT_WIDE_AREA,
flags & AVAHI_LOOKUP_RESULT_MULTICAST,
flags & AVAHI_LOOKUP_RESULT_LOCAL,
flags & AVAHI_LOOKUP_RESULT_OUR_OWN);
if (!instance->m_entries.contains(entry)) {
instance->m_entries.append(entry);
qCDebug(dcPlatformZeroConf()) << "Service added:" << entry;
emit instance->serviceAdded(entry);
}
break;
}
}
instance->m_resolvers.remove(resolver);
avahi_service_resolver_free(resolver);
}
QStringList QtAvahiServiceBrowser::convertTxtList(AvahiStringList *txt)
{
if (!txt)
return QStringList();
QStringList txtList;
txtList.append(QString(reinterpret_cast<char *>(txt->text)));
while (txt->next) {
AvahiStringList *next = txt->next;
txtList.append(QString(reinterpret_cast<char *>(next->text)));
txt = next;
}
return txtList;
}
QAbstractSocket::NetworkLayerProtocol QtAvahiServiceBrowser::convertProtocol(const AvahiProtocol &protocol)
{
QAbstractSocket::NetworkLayerProtocol networkProtocol = QAbstractSocket::UnknownNetworkLayerProtocol;
switch (protocol) {
case AVAHI_PROTO_INET:
networkProtocol = QAbstractSocket::IPv4Protocol;
break;
case AVAHI_PROTO_INET6:
networkProtocol = QAbstractSocket::IPv6Protocol;
break;
case AVAHI_PROTO_UNSPEC:
networkProtocol = QAbstractSocket::UnknownNetworkLayerProtocol;
break;
}
return networkProtocol;
}