-
Notifications
You must be signed in to change notification settings - Fork 8
/
zconfservicebrowser.cpp
279 lines (242 loc) · 9.91 KB
/
zconfservicebrowser.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
/*
* This file is part of qtzeroconf. (c) 2012 Johannes Hilden
* https://github.com/johanneshilden/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 2.1 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#include <QHash>
#include <QStringBuilder>
#include <QDebug>
#include <cassert>
#include <avahi-common/error.h>
#include "zconfservicebrowser.h"
#include "zconfserviceclient.h"
/*!
\struct ZConfServiceEntry
\brief This struct contains information about a particular Zeroconf service
available on the local network.
*/
/*!
\property QString ZConfServiceEntry::ip
A string representation of the IPv4 or IPv6 IP address associated with this
service.
*/
/*!
\property QString ZConfServiceEntry::domain
The domain associated with this service.
*/
/*!
\property QString ZConfServiceEntry::host
The host name associated with this service.
*/
/*!
\property uint16_t ZConfServiceEntry::port
The IP port number associated with this service.
*/
/*!
A human-readable string representation of the network layer protocol used
by this service. Possible values are "IPv4", "IPv6", and "Unspecified".
*/
QString ZConfServiceEntry::protocolName() const
{
switch (protocol)
{
case AVAHI_PROTO_INET: return "IPv4";
case AVAHI_PROTO_INET6: return "IPv6";
default: return "Unspecified";
}
}
/*!
\fn bool ZConfServiceEntry::isCached() const
Returns true if this response originates from the cache.
*/
/*!
\fn bool ZConfServiceEntry::isWideArea() const
Returns true if this response originates from wide area DNS.
*/
/*!
\fn bool ZConfServiceEntry::isMulticast() const
Returns true if this response originates from multicast DNS.
*/
/*!
\fn bool ZConfServiceEntry::isLocal() const
Returns true if this service resides on and was announced by the local host.
*/
class ZConfServiceBrowserPrivate
{
public:
ZConfServiceBrowserPrivate(ZConfServiceClient *client)
: client(client), browser(0)
{
}
static void callback(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);
ZConfServiceBrowser *serviceBrowser = static_cast<ZConfServiceBrowser *>(userdata);
if (serviceBrowser) {
switch (event) {
case AVAHI_BROWSER_FAILURE:
qDebug() << ("Avahi browser error: " % QString(avahi_strerror(avahi_client_errno(serviceBrowser->d_ptr->client->client))));
break;
case AVAHI_BROWSER_NEW:
qDebug() << ("New service '" % QString(name) % "' of type " % QString(type) % " in domain " % QString(domain) % ".");
// We ignore the returned resolver object. In the callback
// function we free it. If the server is terminated before
// the callback function is called the server will free
// the resolver for us.
if (!(avahi_service_resolver_new(serviceBrowser->d_ptr->client->client,
interface,
protocol,
name,
serviceBrowser->d_ptr->type.toLatin1().data(),
domain,
AVAHI_PROTO_UNSPEC,
(AvahiLookupFlags) 0,
ZConfServiceBrowserPrivate::resolve,
serviceBrowser)))
qDebug() << ("Failed to resolve service '" % QString(name) % "': " % avahi_strerror(avahi_client_errno(serviceBrowser->d_ptr->client->client)));
break;
case AVAHI_BROWSER_REMOVE:
serviceBrowser->d_ptr->entries.remove(name);
emit serviceBrowser->serviceEntryRemoved(name);
qDebug() << "Service '" % QString(name) % "' removed from the network.";
break;
case AVAHI_BROWSER_ALL_FOR_NOW:
case AVAHI_BROWSER_CACHE_EXHAUSTED:
qDebug() << (AVAHI_BROWSER_ALL_FOR_NOW == event
? "AVAHI_BROWSER_ALL_FOR_NOW"
: "AVAHI_BROWSER_CACHE_EXHAUSTED");
} // end switch
}
}
static void resolve(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);
Q_UNUSED(type);
Q_UNUSED(txt);
ZConfServiceBrowser *serviceBrowser = static_cast<ZConfServiceBrowser *>(userdata);
if (serviceBrowser) {
switch (event) {
case AVAHI_RESOLVER_FAILURE:
qDebug() << ("Failed to resolve service '" % QString(name) % "': " % avahi_strerror(avahi_client_errno(serviceBrowser->d_ptr->client->client)));
break;
case AVAHI_RESOLVER_FOUND:
{
char a[AVAHI_ADDRESS_STR_MAX];
avahi_address_snprint(a, sizeof(a), address);
ZConfServiceEntry entry;
entry.ip = QString(a);
entry.domain = domain;
entry.host = host_name;
entry.port = port;
entry.protocol = protocol;
entry.flags = flags;
serviceBrowser->d_ptr->entries.insert(name, entry);
emit serviceBrowser->serviceEntryAdded(name);
}
}
avahi_service_resolver_free(resolver);
}
}
typedef QHash<QString, ZConfServiceEntry> ZConfServiceEntryTable;
ZConfServiceClient *const client;
AvahiServiceBrowser *browser;
ZConfServiceEntryTable entries;
QString type;
};
/*!
\class ZConfServiceBrowser
\brief AvahiServiceBrowser wrapper that lets you browse for services
available on the local network. This class can be used to handle Zeroconf
service discovery in a Qt-based client application.
Instantiate a ZConfServiceBrowser object and call browse() with the desired
service type as argument (e.g., "_http._tcp" or "_ipp._tcp").
ZConfServiceBrowser will emit serviceEntryAdded() when a new service is
discovered and serviceEntryRemoved() when a service is removed from the
network.
*/
/*!
Creates a Zeroconf service browser. Call browse() to start browsing for
services.
*/
ZConfServiceBrowser::ZConfServiceBrowser(QObject *parent)
: QObject(parent),
d_ptr(new ZConfServiceBrowserPrivate(new ZConfServiceClient(this)))
{
connect(d_ptr->client, SIGNAL(clientRunning()), this, SLOT(createServiceBrowser()));
}
/*!
Destroys the browser object and releases all resources associated with it.
*/
ZConfServiceBrowser::~ZConfServiceBrowser()
{
if (d_ptr->browser)
avahi_service_browser_free(d_ptr->browser);
delete d_ptr;
}
/*!
Browses for Zeroconf services on the LAN. This is a non-blocking call.
ZConfServiceBrowser will emit serviceEntryAdded() when a new service is
discovered and serviceEntryRemoved() when a service is removed from the
network.
*/
void ZConfServiceBrowser::browse(QString serviceType)
{
d_ptr->type = serviceType;
assert(d_ptr->client);
d_ptr->client->run();
}
/*!
Returns a ZConfServiceEntry struct with detailed information about the
Zeroconf service associated with the name.
*/
ZConfServiceEntry ZConfServiceBrowser::serviceEntry(QString name)
{
return d_ptr->entries.value(name);
}
void ZConfServiceBrowser::createServiceBrowser()
{
if (d_ptr->browser)
return;
d_ptr->browser = avahi_service_browser_new(d_ptr->client->client,
AVAHI_IF_UNSPEC,
AVAHI_PROTO_UNSPEC,
d_ptr->type.toLatin1().data(),
NULL,
(AvahiLookupFlags) 0,
ZConfServiceBrowserPrivate::callback,
this);
}