-
Notifications
You must be signed in to change notification settings - Fork 51
/
bonjour.cpp
384 lines (338 loc) · 11.2 KB
/
bonjour.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/**************************************************************************************************
---------------------------------------------------------------------------------------------------
Copyright (C) 2015 Jonathan Bagg
This file is part of QtZeroConf.
QtZeroConf is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
QtZeroConf 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 QtZeroConf. If not, see <http://www.gnu.org/licenses/>.
---------------------------------------------------------------------------------------------------
Project name : QtZeroConf
File name : bonjour.cpp
Created : 20 July 2015
Author(s) : Jonathan Bagg
---------------------------------------------------------------------------------------------------
Wrapper for Apple's Bonjour library for use on Windows, MACs and iOS
---------------------------------------------------------------------------------------------------
**************************************************************************************************/
#include "qzeroconf.h"
#include "bonjour_p.h"
void Resolver::resolverReady()
{
DNSServiceErrorType err = DNSServiceProcessResult(DNSresolverRef);
if (err != kDNSServiceErr_NoError)
cleanUp();
}
void Resolver::addressReady()
{
DNSServiceErrorType err = DNSServiceProcessResult(DNSaddressRef);
if (err != kDNSServiceErr_NoError)
cleanUp();
}
void Resolver::cleanUp()
{
DNSServiceRefDeallocate(DNSresolverRef);
DNSServiceRefDeallocate(DNSaddressRef);
// the QSocketNotifiers resolverNotifier and addressNotifier get deleted when the QSharedPointer gets deleted along with the Resolver. No need to clear them here.
QString key = zcs->name() + QString::number(zcs->interfaceIndex());
ref->resolvers.remove(key);
delete this;
}
QZeroConfPrivate::QZeroConfPrivate(QZeroConf *parent)
{
pub = parent;
}
void QZeroConfPrivate::bsRead()
{
DNSServiceErrorType err = DNSServiceProcessResult(dnssRef);
if (err != kDNSServiceErr_NoError) {
cleanUp(dnssRef);
emit pub->error(QZeroConf::serviceRegistrationFailed);
}
}
void QZeroConfPrivate::browserRead()
{
DNSServiceErrorType err = DNSServiceProcessResult(browser);
if (err != kDNSServiceErr_NoError) {
cleanUp(browser);
emit pub->error(QZeroConf::browserFailed);
}
}
void QZeroConfPrivate::resolve(QZeroConfService zcs)
{
DNSServiceErrorType err;
Resolver *resolver = new Resolver;
QString key = zcs->name() + QString::number(zcs->interfaceIndex());
resolvers.insert(key, resolver);
resolver->ref = this;
resolver->zcs = zcs;
err = DNSServiceResolve(&resolver->DNSresolverRef, kDNSServiceFlagsTimeout, zcs->interfaceIndex(), zcs->name().toUtf8(), zcs->type().toUtf8(), zcs->domain().toUtf8(), static_cast<DNSServiceResolveReply>(resolverCallback), resolver);
if (err == kDNSServiceErr_NoError) {
int sockfd = DNSServiceRefSockFD(resolver->DNSresolverRef);
if (sockfd == -1) {
resolver->cleanUp();
}
else {
resolver->resolverNotifier = QSharedPointer<QSocketNotifier>::create(sockfd, QSocketNotifier::Read);
connect(resolver->resolverNotifier.data(), &QSocketNotifier::activated, resolver, &Resolver::resolverReady);
}
}
else {
resolver->cleanUp();
}
}
void DNSSD_API QZeroConfPrivate::registerCallback(DNSServiceRef, DNSServiceFlags, DNSServiceErrorType errorCode, const char *, const char *, const char *, void *userdata)
{
QZeroConfPrivate *ref = static_cast<QZeroConfPrivate *>(userdata);
if (errorCode == kDNSServiceErr_NoError) {
emit ref->pub->servicePublished();
}
else {
ref->cleanUp(ref->dnssRef);
emit ref->pub->error(QZeroConf::serviceRegistrationFailed);
}
}
void DNSSD_API QZeroConfPrivate::browseCallback(DNSServiceRef, DNSServiceFlags flags,
quint32 interfaceIndex, DNSServiceErrorType err, const char *name,
const char *type, const char *domain, void *userdata)
{
QString key;
QZeroConfService zcs;
QZeroConfPrivate *ref = static_cast<QZeroConfPrivate *>(userdata);
//qDebug() << name;
if (err == kDNSServiceErr_NoError) {
key = name + QString::number(interfaceIndex);
if (flags & kDNSServiceFlagsAdd) {
if (!ref->pub->services.contains(key)) {
zcs = QZeroConfService::create();
zcs->m_name = name;
zcs->m_type = type;
zcs->m_domain = domain;
zcs->m_interfaceIndex = interfaceIndex;
ref->resolve(zcs);
}
}
else if (ref->pub->services.contains(key)) {
zcs = ref->pub->services[key];
ref->pub->services.remove(key);
if (ref->resolvers.contains(key))
ref->resolvers[key]->cleanUp();
emit ref->pub->serviceRemoved(zcs);
}
}
else {
ref->cleanUp(ref->browser);
emit ref->pub->error(QZeroConf::browserFailed);
}
}
void DNSSD_API QZeroConfPrivate::resolverCallback(DNSServiceRef, DNSServiceFlags,
quint32 interfaceIndex, DNSServiceErrorType err, const char *,
const char *hostName, quint16 port, quint16 txtLen,
const unsigned char *txtRecord, void *userdata)
{
Resolver *resolver = static_cast<Resolver *>(userdata);
if (err != kDNSServiceErr_NoError) {
resolver->cleanUp();
return;
}
uchar recLen;
while (txtLen > 0) // add txt records
{
recLen = txtRecord[0];
txtRecord++;
QByteArray avahiText(reinterpret_cast<const char *>(txtRecord), recLen);
const int pos = avahiText.indexOf('=');
if (pos < 0)
resolver->zcs->m_txt[avahiText] = "";
else
resolver->zcs->m_txt[avahiText.left(pos)] = avahiText.mid(pos + 1, -1);
txtLen-= recLen + 1;
txtRecord+= recLen;
}
resolver->zcs->m_host = hostName;
resolver->zcs->m_port = qFromBigEndian<quint16>(port);
if (resolver->DNSaddressRef) {
DNSServiceRefDeallocate(resolver->DNSaddressRef);
resolver->DNSaddressRef = nullptr;
}
err = DNSServiceGetAddrInfo(&resolver->DNSaddressRef, kDNSServiceFlagsForceMulticast, interfaceIndex, resolver->ref->protocol, hostName, static_cast<DNSServiceGetAddrInfoReply>(addressReply), resolver);
if (err == kDNSServiceErr_NoError) {
int sockfd = DNSServiceRefSockFD(resolver->DNSaddressRef);
if (sockfd == -1) {
resolver->cleanUp();
}
else {
// Fix "multiple socket notifiers for same socket" warning
resolver->addressNotifier.clear();
resolver->addressNotifier = QSharedPointer<QSocketNotifier>::create(sockfd, QSocketNotifier::Read);
connect(resolver->addressNotifier.data(), &QSocketNotifier::activated, resolver, &Resolver::addressReady);
}
}
else {
resolver->cleanUp();
}
}
void DNSSD_API QZeroConfPrivate::addressReply(DNSServiceRef sdRef,
DNSServiceFlags flags, quint32 interfaceIndex,
DNSServiceErrorType err, const char *hostName,
const struct sockaddr* address, quint32 ttl, void *userdata)
{
Q_UNUSED(interfaceIndex)
Q_UNUSED(sdRef)
Q_UNUSED(ttl)
Q_UNUSED(hostName)
Resolver *resolver = static_cast<Resolver *>(userdata);
if (err == kDNSServiceErr_NoError) {
if ((flags & kDNSServiceFlagsAdd) != 0) {
QHostAddress hAddress(address);
resolver->zcs->setIp(hAddress);
QString key = resolver->zcs->name() + QString::number(interfaceIndex);
if (!resolver->ref->pub->services.contains(key)) {
resolver->ref->pub->services.insert(key, resolver->zcs);
emit resolver->ref->pub->serviceAdded(resolver->zcs);
}
else
emit resolver->ref->pub->serviceUpdated(resolver->zcs);
}
}
else
resolver->cleanUp();
}
void QZeroConfPrivate::cleanUp(DNSServiceRef toClean)
{
if (!toClean)
return;
else if (toClean == browser) {
browser = nullptr;
browserNotifier.clear();
QMap<QString, QZeroConfService >::iterator i;
for (i = pub->services.begin(); i != pub->services.end(); i++) {
QString key = (*i)->name() + QString::number((*i)->interfaceIndex());
resolvers[key]->cleanUp();
emit pub->serviceRemoved(*i);
}
pub->services.clear();
}
else if (toClean == dnssRef) {
dnssRef = nullptr;
serviceNotifier.clear();
}
DNSServiceRefDeallocate(toClean);
}
QZeroConf::QZeroConf(QObject *parent) : QObject (parent)
{
pri = new QZeroConfPrivate(this);
qRegisterMetaType<QZeroConfService>("QZeroConfService");
}
QZeroConf::~QZeroConf()
{
pri->cleanUp(pri->dnssRef);
pri->cleanUp(pri->browser);
delete pri;
}
void QZeroConf::startServicePublish(const char *name, const char *type, const char *domain, quint16 port, quint32 interface)
{
DNSServiceErrorType err;
if (pri->dnssRef) {
emit error(QZeroConf::serviceRegistrationFailed);
return;
}
err = DNSServiceRegister(&pri->dnssRef, 0, interface,
name,
type,
domain,
nullptr,
qFromBigEndian<quint16>(port),
static_cast<uint16_t>(pri->txt.size()), pri->txt.data(),
static_cast<DNSServiceRegisterReply>(QZeroConfPrivate::registerCallback), pri);
if (err == kDNSServiceErr_NoError) {
int sockfd = DNSServiceRefSockFD(pri->dnssRef);
if (sockfd == -1) {
pri->cleanUp(pri->dnssRef);
emit error(QZeroConf::serviceRegistrationFailed);
}
else {
pri->serviceNotifier = QSharedPointer<QSocketNotifier>::create(sockfd, QSocketNotifier::Read, this);
connect(pri->serviceNotifier.data(), &QSocketNotifier::activated, pri, &QZeroConfPrivate::bsRead);
}
}
else {
pri->cleanUp(pri->dnssRef);
emit error(QZeroConf::serviceRegistrationFailed);
}
}
void QZeroConf::stopServicePublish(void)
{
pri->cleanUp(pri->dnssRef);
}
bool QZeroConf::publishExists(void)
{
if (pri->dnssRef)
return true;
else
return false;
}
void QZeroConf::addServiceTxtRecord(QString nameOnly)
{
pri->txt.append(static_cast<char>(nameOnly.size()));
pri->txt.append(nameOnly.toUtf8());
}
void QZeroConf::addServiceTxtRecord(QString name, QString value)
{
name.append("=");
name.append(value);
addServiceTxtRecord(name);
}
void QZeroConf::clearServiceTxtRecords()
{
pri->txt.clear();
}
void QZeroConf::startBrowser(QString type, QAbstractSocket::NetworkLayerProtocol protocol)
{
DNSServiceErrorType err;
if (pri->browser) {
emit error(QZeroConf::browserFailed);
return;
}
switch (protocol) {
case QAbstractSocket::IPv4Protocol: pri->protocol = kDNSServiceProtocol_IPv4; break;
case QAbstractSocket::IPv6Protocol: pri->protocol = kDNSServiceProtocol_IPv6; break;
default:
qDebug("QZeroConf::startBrowser() - unsupported protocol, using IPv4");
pri->protocol = kDNSServiceProtocol_IPv4;
break;
}
err = DNSServiceBrowse(&pri->browser, 0, 0, type.toUtf8(), nullptr, static_cast<DNSServiceBrowseReply>(QZeroConfPrivate::browseCallback), pri);
if (err == kDNSServiceErr_NoError) {
int sockfd = DNSServiceRefSockFD(pri->browser);
if (sockfd == -1) {
pri->cleanUp(pri->browser);
emit error(QZeroConf::browserFailed);
}
else {
pri->browserNotifier = QSharedPointer<QSocketNotifier>::create(sockfd, QSocketNotifier::Read, this);
connect(pri->browserNotifier.data(), &QSocketNotifier::activated, pri, &QZeroConfPrivate::browserRead);
}
}
else {
pri->cleanUp(pri->browser);
emit error(QZeroConf::browserFailed);
}
}
void QZeroConf::stopBrowser(void)
{
pri->cleanUp(pri->browser);
}
bool QZeroConf::browserExists(void)
{
if (pri->browser)
return true;
else
return false;
}