-
Notifications
You must be signed in to change notification settings - Fork 51
/
avahicore.cpp
380 lines (340 loc) · 10.7 KB
/
avahicore.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
/**************************************************************************************************
---------------------------------------------------------------------------------------------------
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 : avahicore.cpp
Created : 9 September 2015
Author(s) : Jonathan Bagg
---------------------------------------------------------------------------------------------------
Avahi-core wrapper for use in embedded Linux systems (Android)
---------------------------------------------------------------------------------------------------
**************************************************************************************************/
//#include <avahi-qt4/qt-watch.h> //
#include "avahi-qt/qt-watch.h" // fixme don't depend on avahi-qt until it supports Qt5
#include <avahi-core/core.h>
#include <avahi-core/publish.h>
#include <avahi-core/lookup.h>
#include <avahi-common/simple-watch.h>
#include <QCoreApplication>
#include "qzeroconf.h"
class QZeroConfPrivate
{
public:
QZeroConfPrivate(QZeroConf *parent)
{
qint32 error;
pub = parent;
group = NULL;
browser = NULL;
txt = NULL;
ready = 0;
registerWaiting = 0;
poll = avahi_qt_poll_get();
if (!poll) {
return;
}
avahi_server_config_init(&config);
config.publish_workstation = 0;
if (!referenceCount) {
server = avahi_server_new(poll, &config, serverCallback, this, &error);
}
referenceCount++;
if (!server) {
return;
}
}
static void serverCallback(AvahiServer *, AvahiServerState state, AVAHI_GCC_UNUSED void * userdata)
{
QZeroConfPrivate *ref = static_cast<QZeroConfPrivate *>(userdata);
switch (state) {
case AVAHI_SERVER_RUNNING:
ref->ready = 1;
if (ref->registerWaiting) {
ref->registerWaiting = 0;
ref->registerService(ref->name.toUtf8(), ref->type.toUtf8(), ref->domain.toUtf8(), ref->port, 0);
}
break;
case AVAHI_SERVER_COLLISION:
break;
case AVAHI_SERVER_REGISTERING: break;
case AVAHI_SERVER_FAILURE:
break;
case AVAHI_SERVER_INVALID: break;
}
}
static void groupCallback(AvahiServer *, AvahiSEntryGroup *g, AvahiEntryGroupState state, AVAHI_GCC_UNUSED void *userdata)
{
QZeroConfPrivate *ref = static_cast<QZeroConfPrivate *>(userdata);
switch (state) {
case AVAHI_ENTRY_GROUP_ESTABLISHED:
emit ref->pub->servicePublished();
break;
case AVAHI_ENTRY_GROUP_COLLISION:
avahi_s_entry_group_free(g);
ref->group = NULL;
emit ref->pub->error(QZeroConf::serviceNameCollision);
break;
case AVAHI_ENTRY_GROUP_FAILURE :
avahi_s_entry_group_free(g);
ref->group = NULL;
emit ref->pub->error(QZeroConf::serviceRegistrationFailed);
break;
case AVAHI_ENTRY_GROUP_UNCOMMITED: break;
case AVAHI_ENTRY_GROUP_REGISTERING: break;
}
}
static void browseCallback(
AvahiSServiceBrowser *,
AvahiIfIndex interface,
AvahiProtocol protocol,
AvahiBrowserEvent event,
const char *name,
const char *type,
const char *domain,
AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
void* userdata)
{
QString key = name + QString::number(interface);
QZeroConfPrivate *ref = static_cast<QZeroConfPrivate *>(userdata);
QZeroConfService zcs;
switch (event) {
case AVAHI_BROWSER_FAILURE:
ref->broswerCleanUp();
emit ref->pub->error(QZeroConf::browserFailed);
break;
case AVAHI_BROWSER_NEW:
if (!ref->resolvers.contains(key))
ref->resolvers.insert(key, avahi_s_service_resolver_new(ref->server, interface, protocol, name, type, domain, ref->aProtocol, AVAHI_LOOKUP_USE_MULTICAST, resolveCallback, ref));
break;
case AVAHI_BROWSER_REMOVE:
if (!ref->resolvers.contains(key))
return;
avahi_s_service_resolver_free(ref->resolvers[key]);
ref->resolvers.remove(key);
if (!ref->pub->services.contains(key))
return;
zcs = ref->pub->services[key];
ref->pub->services.remove(key);
emit ref->pub->serviceRemoved(zcs);
break;
case AVAHI_BROWSER_ALL_FOR_NOW:
case AVAHI_BROWSER_CACHE_EXHAUSTED:
break;
}
}
static void resolveCallback(
AVAHI_GCC_UNUSED AvahiSServiceResolver *r,
AVAHI_GCC_UNUSED AvahiIfIndex interface,
AVAHI_GCC_UNUSED 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,
AVAHI_GCC_UNUSED void* userdata)
{
bool newRecord = 0;
QZeroConfService zcs;
QZeroConfPrivate *ref = static_cast<QZeroConfPrivate *>(userdata);
QString key = name + QString::number(interface);
if (event == AVAHI_RESOLVER_FOUND) {
if (ref->pub->services.contains(key))
zcs = ref->pub->services[key];
else {
zcs = QZeroConfService(new QZeroConfServiceData);
newRecord = 1;
zcs->m_name = name;
zcs->m_type = type;
zcs->m_domain = domain;
zcs->m_host = host_name;
zcs->m_interfaceIndex = interface;
zcs->m_port = port;
while (txt) // get txt records
{
QByteArray avahiText((const char *)txt->text, txt->size);
const int pos = avahiText.indexOf('=');
if (pos < 0)
zcs->m_txt[avahiText] = "";
else
zcs->m_txt[avahiText.left(pos)] = avahiText.mid(pos + 1, -1);
txt = txt->next;
}
ref->pub->services.insert(key, zcs);
}
char a[AVAHI_ADDRESS_STR_MAX];
avahi_address_snprint(a, sizeof(a), address);
QHostAddress addr(a);
zcs->setIp(addr);
if (newRecord)
emit ref->pub->serviceAdded(zcs);
else
emit ref->pub->serviceUpdated(zcs);
}
else if (ref->pub->services.contains(key)) { // delete service if exists and unable to resolve
zcs = ref->pub->services[key];
ref->pub->services.remove(key);
emit ref->pub->serviceRemoved(zcs);
// don't delete the resolver here...we need to keep it around so Avahi will keep updating....might be able to resolve the service in the future
}
}
void broswerCleanUp(void)
{
if (!browser)
return;
avahi_s_service_browser_free(browser);
browser = NULL;
QMap<QString, QZeroConfService>::iterator i;
for (i = pub->services.begin(); i != pub->services.end(); i++) {
emit pub->serviceRemoved(i.value());
}
pub->services.clear();
QMap<QString, AvahiSServiceResolver *>::iterator r;
for (r = resolvers.begin(); r != resolvers.end(); r++)
avahi_s_service_resolver_free(*r);
resolvers.clear();
}
void registerService(const char *name, const char *type, const char *domain, quint16 port, quint32 interface)
{
qint32 ret;
group = avahi_s_entry_group_new(server, QZeroConfPrivate::groupCallback, this);
if (!group) {
pub->emit error(QZeroConf::serviceRegistrationFailed);
return;
}
if (interface <= 0) {
interface = AVAHI_IF_UNSPEC;
}
ret = avahi_server_add_service_strlst(server, group, interface, AVAHI_PROTO_UNSPEC, AVAHI_PUBLISH_UPDATE, name, type, domain, NULL, port, txt);
if (ret < 0) {
avahi_s_entry_group_free(group);
group = NULL;
pub->emit error(QZeroConf::serviceRegistrationFailed);
return;
}
ret = avahi_s_entry_group_commit(group);
if (ret < 0) {
avahi_s_entry_group_free(group);
group = NULL;
pub->emit error(QZeroConf::serviceRegistrationFailed);
}
}
QZeroConf *pub;
const AvahiPoll *poll;
static AvahiServer *server;
static quint32 referenceCount;
AvahiServerConfig config;
AvahiSEntryGroup *group;
AvahiSServiceBrowser *browser;
AvahiProtocol aProtocol;
QMap <QString, AvahiSServiceResolver *> resolvers;
AvahiStringList *txt;
bool ready, registerWaiting;
QString name, type, domain;
qint32 port;
};
AvahiServer* QZeroConfPrivate::server = nullptr;
quint32 QZeroConfPrivate::referenceCount = 0;
QZeroConf::QZeroConf(QObject *parent) : QObject (parent)
{
pri = new QZeroConfPrivate(this);
qRegisterMetaType<QZeroConfService>("QZeroConfService");
}
QZeroConf::~QZeroConf()
{
avahi_string_list_free(pri->txt);
pri->broswerCleanUp();
avahi_server_config_free(&pri->config);
pri->referenceCount--;
if (!pri->referenceCount)
avahi_server_free(pri->server);
delete pri;
}
void QZeroConf::startServicePublish(const char *name, const char *type, const char *domain, quint16 port, quint32 interface)
{
if (pri->group) {
emit error(QZeroConf::serviceRegistrationFailed);
return;
}
if (pri->ready)
pri->registerService(name, type, domain, port, interface);
else {
pri->registerWaiting = 1;
pri->name = name;
pri->type = type;
pri->domain = domain;
pri->port = port;
}
}
void QZeroConf::stopServicePublish(void)
{
if (pri->group) {
avahi_s_entry_group_free(pri->group);
pri->group = NULL;
}
}
bool QZeroConf::publishExists(void)
{
if (pri->group)
return true;
else
return false;
}
// http://www.zeroconf.org/rendezvous/txtrecords.html
void QZeroConf::addServiceTxtRecord(QString nameOnly)
{
pri->txt = avahi_string_list_add(pri->txt, nameOnly.toUtf8());
}
void QZeroConf::addServiceTxtRecord(QString name, QString value)
{
name.append("=");
name.append(value);
addServiceTxtRecord(name);
}
void QZeroConf::clearServiceTxtRecords()
{
avahi_string_list_free(pri->txt);
pri->txt = NULL;
}
void QZeroConf::startBrowser(QString type, QAbstractSocket::NetworkLayerProtocol protocol)
{
if (pri->browser)
emit error(QZeroConf::browserFailed);
switch (protocol) {
case QAbstractSocket::IPv4Protocol: pri->aProtocol = AVAHI_PROTO_INET; break;
case QAbstractSocket::IPv6Protocol: pri->aProtocol = AVAHI_PROTO_INET6; break;
default:
qDebug("QZeroConf::startBrowser() - unsupported protocol, using IPv4");
pri->aProtocol = AVAHI_PROTO_INET;
break;
};
pri->browser = avahi_s_service_browser_new(pri->server, AVAHI_IF_UNSPEC, pri->aProtocol, type.toUtf8(), NULL, AVAHI_LOOKUP_USE_MULTICAST, QZeroConfPrivate::browseCallback, pri);
if (!pri->browser)
emit error(QZeroConf::browserFailed);
}
void QZeroConf::stopBrowser(void)
{
pri->broswerCleanUp();
}
bool QZeroConf::browserExists(void)
{
if (pri->browser)
return true;
else
return false;
}