forked from analogdevicesinc/libiio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns_sd_avahi.c
388 lines (335 loc) · 9.64 KB
/
dns_sd_avahi.c
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
385
386
387
388
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
* libiio - Library for interfacing industrial I/O (IIO) devices
*
* Copyright (C) 2014-2020 Analog Devices, Inc.
* Author: Paul Cercueil <[email protected]>
* Robin Getz <[email protected]>
*
* Some of this is insipred from libavahi's example:
* https://avahi.org/doxygen/html/client-browse-services_8c-example.html
* which is also LGPL 2.1 or later.
*/
#include "debug.h"
#include "dns_sd.h"
#include "iio-private.h"
#include "iio-lock.h"
#include <errno.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <avahi-common/address.h>
#include <avahi-common/error.h>
#include <avahi-common/simple-watch.h>
#include <avahi-common/malloc.h>
#include <avahi-client/client.h>
#include <avahi-client/lookup.h>
/*
* Fundamentally, this builds up a linked list to manage
* potential clients on the network
*/
static struct dns_sd_discovery_data *new_discovery_data(void)
{
struct dns_sd_discovery_data *d;
d = zalloc(sizeof(*d));
if (!d)
return NULL;
d->address = zalloc(sizeof(*d->address));
if (!d->address) {
free(d);
return NULL;
}
return d;
}
static void avahi_process_resolved(struct dns_sd_discovery_data *ddata,
const AvahiAddress *addr,
const char *host_name,
const uint16_t port)
{
/* Avahi is multi-threaded, so lock the list */
iio_mutex_lock(ddata->lock);
ddata->resolved++;
/* Find empty data to store things*/
while (ddata->next)
ddata = ddata->next;
/* link a new placeholder to the list */
avahi_address_snprint(ddata->addr_str,
sizeof(ddata->addr_str), addr);
memcpy(ddata->address, addr, sizeof(*ddata->address)); /* Flawfinder: ignore */
ddata->port = port;
ddata->hostname = strdup(host_name);
ddata->resolved = true;
/* link a new, empty placeholder to the list */
ddata->next = new_discovery_data();
if (ddata->next) {
/* duplicate poll & lock info,
* since we don't know which might be discarded */
ddata->next->poll = ddata->poll;
ddata->next->lock = ddata->lock;
} else {
IIO_ERROR("Avahi Resolver : memory failure\n");
}
iio_mutex_unlock(ddata->lock);
IIO_DEBUG("\t\t%s:%u (%s)\n", host_name, port, ddata->addr_str);
}
/*
* libavahi callbacks for browser and resolver
* for more info, check out libavahi docs at:
* https://avahi.org/doxygen/html/index.html
*/
static void __avahi_resolver_cb(AvahiServiceResolver *resolver,
AvahiIfIndex iface,
AvahiProtocol proto,
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 *d)
{
struct dns_sd_discovery_data *ddata = d;
AvahiClient *client;
int err;
if (!resolver) {
IIO_ERROR("Fatal Error in Avahi Resolver\n");
return;
}
switch(event) {
case AVAHI_RESOLVER_FAILURE:
client = avahi_service_resolver_get_client(resolver);
err = avahi_client_errno(client);
IIO_ERROR("Avahi Resolver: Failed resolve service '%s' "
"of type '%s' in domain '%s': %s\n",
name, type, domain,
avahi_strerror(err));
break;
case AVAHI_RESOLVER_FOUND: {
avahi_process_resolved(ddata, address, host_name, port);
IIO_DEBUG("Avahi Resolver : service '%s' of type '%s' in domain '%s':\n",
name, type, domain);
break;
}
}
avahi_service_resolver_free(resolver);
}
static void avahi_host_resolver(AvahiHostNameResolver *resolver,
AvahiIfIndex iface,
AvahiProtocol proto,
AvahiResolverEvent event,
const char *host_name,
const AvahiAddress *address,
AvahiLookupResultFlags flags,
void *d)
{
struct dns_sd_discovery_data *ddata = d;
AvahiClient *client;
int err;
switch(event) {
case AVAHI_RESOLVER_FAILURE:
client = avahi_host_name_resolver_get_client(resolver);
err = avahi_client_errno(client);
IIO_ERROR("Avahi Resolver: Failed to resolve host '%s' : %s\n",
host_name, avahi_strerror(err));
break;
case AVAHI_RESOLVER_FOUND:
avahi_process_resolved(ddata, address, host_name, IIOD_PORT);
break;
}
avahi_host_name_resolver_free(resolver);
avahi_simple_poll_quit(ddata->poll);
}
static void __avahi_browser_cb(AvahiServiceBrowser *browser,
AvahiIfIndex iface,
AvahiProtocol proto,
AvahiBrowserEvent event,
const char *name,
const char *type,
const char *domain,
AvahiLookupResultFlags flags,
void *d)
{
struct dns_sd_discovery_data *ddata = (struct dns_sd_discovery_data *) d;
struct AvahiClient *client = avahi_service_browser_get_client(browser);
unsigned int i;
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 5e6; /* 5ms in ns */
if (!browser) {
IIO_ERROR("Fatal Error in Avahi Browser\n");
return;
}
switch (event) {
case AVAHI_BROWSER_REMOVE:
IIO_DEBUG("Avahi Browser : REMOVE : service '%s' of type '%s' in domain '%s'\n",
name, type, domain);
break;
case AVAHI_BROWSER_NEW:
IIO_DEBUG("Avahi Browser : NEW: service '%s' of type '%s' in domain '%s'\n",
name, type, domain);
if(!avahi_service_resolver_new(client, iface,
proto, name, type, domain,
AVAHI_PROTO_UNSPEC, 0,
__avahi_resolver_cb, d)) {
IIO_ERROR("Failed to resolve service '%s\n", name);
} else {
iio_mutex_lock(ddata->lock);
ddata->found++;
iio_mutex_unlock(ddata->lock);
}
break;
case AVAHI_BROWSER_ALL_FOR_NOW:
IIO_DEBUG("Avahi Browser : ALL_FOR_NOW Browser : %d, Resolved : %d\n",
ddata->found, ddata->resolved);
/* 200 * 5ms = wait 1 second */
for (i = 0; ddata->found != ddata->resolved && i <= 200; i++)
nanosleep(&ts, NULL);
avahi_simple_poll_quit(ddata->poll);
break;
case AVAHI_BROWSER_FAILURE:
IIO_DEBUG("Avahi Browser : FAILURE\n");
avahi_simple_poll_quit(ddata->poll);
break;
case AVAHI_BROWSER_CACHE_EXHAUSTED:
IIO_DEBUG("Avahi Browser : CACHE_EXHAUSTED\n");
break;
}
}
/*
* This creates the linked lists, tests it (make sure a context is there)
* and returns. The structure must be freed with free_all_discovery_data();
* The returned value is zero on success, negative error code on failure.
*/
int dnssd_find_hosts(struct dns_sd_discovery_data **ddata)
{
struct dns_sd_discovery_data *d;
AvahiClient *client;
AvahiServiceBrowser *browser;
int ret = 0;
d = new_discovery_data();
if (!d)
return -ENOMEM;
d->lock = iio_mutex_create();
if (!d->lock) {
dnssd_free_all_discovery_data(d);
return -ENOMEM;
}
d->poll = avahi_simple_poll_new();
if (!d->poll) {
iio_mutex_destroy(d->lock);
dnssd_free_all_discovery_data(d);
return -ENOMEM;
}
client = avahi_client_new(avahi_simple_poll_get(d->poll),
0, NULL, NULL, &ret);
if (!client) {
IIO_ERROR("Unable to create Avahi DNS-SD client :%s\n",
avahi_strerror(ret));
goto err_free_poll;
}
browser = avahi_service_browser_new(client,
AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
"_iio._tcp", NULL, 0,
__avahi_browser_cb, d);
if (!browser) {
ret = avahi_client_errno(client);
IIO_ERROR("Unable to create Avahi DNS-SD browser: %s\n",
avahi_strerror(ret));
goto err_free_client;
}
IIO_DEBUG("Trying to discover host\n");
avahi_simple_poll_loop(d->poll);
if (d->resolved) {
port_knock_discovery_data(&d);
remove_dup_discovery_data(&d);
} else
ret = -ENXIO;
avahi_service_browser_free(browser);
err_free_client:
avahi_client_free(client);
err_free_poll:
avahi_simple_poll_free(d->poll);
iio_mutex_destroy(d->lock);
*ddata = d;
return ret;
}
static void avahi_resolve_host(struct dns_sd_discovery_data *d,
const char *hostname,
const AvahiProtocol proto)
{
AvahiClient *client;
AvahiHostNameResolver *resolver;
int ret;
d->poll = avahi_simple_poll_new();
if (!d->poll)
return;
client = avahi_client_new(avahi_simple_poll_get(d->poll), 0, NULL, NULL, &ret);
if (!client) {
IIO_ERROR("Unable to create Avahi DNS-SD client :%s\n",
avahi_strerror(ret));
goto err_free_poll;
}
resolver = avahi_host_name_resolver_new(client, AVAHI_IF_UNSPEC, proto, hostname, proto, 0,
avahi_host_resolver, d);
if (!resolver) {
ret = avahi_client_errno(client);
IIO_ERROR("Unable to create Avahi DNS-SD browser: %s\n",
avahi_strerror(ret));
goto err_free_client;
}
IIO_DEBUG("Trying to resolve host: %s, proto: %d\n", hostname, proto);
avahi_simple_poll_loop(d->poll);
err_free_client:
avahi_client_free(client);
err_free_poll:
avahi_simple_poll_free(d->poll);
}
int dnssd_resolve_host(const char *hostname,
char *ip_addr,
const int addr_len)
{
struct dns_sd_discovery_data *d;
int ret = 0;
if (!hostname || hostname[0] == '\0')
return -EINVAL;
d = new_discovery_data();
if (!d)
return -ENOMEM;
d->lock = iio_mutex_create();
if (!d->lock) {
ret = -ENOMEM;
goto err_free_data;
}
/*
* The reason not to use AVAHI_PROTO_UNSPEC is that avahi sometimes resolves the host
* to an ipv6 link local address which is not suitable to be used by connect. In fact,
* `port_knock_discovery_data()` would discard this entry. On the other hand, some users
* might really want to use ipv6 and have their environment correctly configured. Hence,
* we try to resolve both in ipv4 and ipv6...
*/
avahi_resolve_host(d, hostname, AVAHI_PROTO_INET);
#ifdef HAVE_IPV6
avahi_resolve_host(d, hostname, AVAHI_PROTO_INET6);
#endif
if (d->resolved) {
port_knock_discovery_data(&d);
remove_dup_discovery_data(&d);
} else {
ret = -ENXIO;
goto err_mutex_destroy;
}
/* If next is null it means that d is empty */
if (!d->next) {
ret = -ENXIO;
goto err_mutex_destroy;
}
iio_strlcpy(ip_addr, d->addr_str, addr_len);
err_mutex_destroy:
iio_mutex_destroy(d->lock);
err_free_data:
dnssd_free_all_discovery_data(d);
return ret;
}