forked from analogdevicesinc/libiio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns_sd_avahi.c
256 lines (225 loc) · 6.72 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
/*
* 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]>
*
* This library 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.
*
* This library 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.
*
* 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 "iio-private.h"
#include "network.h"
#include "iio-lock.h"
#include <time.h>
#include <unistd.h>
#include "debug.h"
/*
* Fundamentally, this builds up a linked list to manage
* potential clients on the network
*/
static int new_discovery_data(struct dns_sd_discovery_data **data)
{
struct dns_sd_discovery_data *d;
d = zalloc(sizeof(struct dns_sd_discovery_data));
if (!d)
return -ENOMEM;
d->address = zalloc(sizeof(struct AvahiAddress));
if (!d->address) {
free(d);
return -ENOMEM;
}
*data = d;
return 0;
}
void dnssd_free_discovery_data(struct dns_sd_discovery_data *d)
{
free(d->hostname);
free(d->address);
free(d);
}
/*
* 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,
__notused AvahiIfIndex iface, __notused 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,
__notused AvahiLookupResultFlags flags, void *d)
{
struct dns_sd_discovery_data *ddata = (struct dns_sd_discovery_data *) d;
if (!resolver) {
IIO_ERROR("Fatal Error in Avahi Resolver\n");
return;
}
switch(event) {
case AVAHI_RESOLVER_FAILURE:
IIO_ERROR("Avahi Resolver: Failed resolve service '%s' "
"of type '%s' in domain '%s': %s\n",
name, type, domain,
avahi_strerror(
avahi_client_errno(
avahi_service_resolver_get_client(
resolver))));
break;
case AVAHI_RESOLVER_FOUND: {
/* 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), address);
memcpy(ddata->address, address, sizeof(*address));
ddata->port = port;
ddata->hostname = strdup(host_name);
ddata->resolved = true;
/* link a new, empty placeholder to the list */
if (!new_discovery_data(&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("Avahi Resolver : service '%s' of type '%s' in domain '%s':\n",
name, type, domain);
IIO_DEBUG("\t\t%s:%u (%s)\n", host_name, port, ddata->addr_str);
break;
}
}
avahi_service_resolver_free(resolver);
}
static void __avahi_browser_cb(AvahiServiceBrowser *browser,
AvahiIfIndex iface, AvahiProtocol proto,
AvahiBrowserEvent event, const char *name,
const char *type, const char *domain,
__notused 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);
int i;
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:
/* Wait for a max of 1 second */
i = 0;
IIO_DEBUG("Avahi Browser : ALL_FOR_NOW Browser : %d, Resolved : %d\n",
ddata->found, ddata->resolved);
/* 200 * 5ms = wait 1 second */
while ((ddata->found != ddata->resolved) && i <= 200) {
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 5e6; /* 5ms in ns*/
nanosleep(&ts, NULL);
i++;
}
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;
if (new_discovery_data(&d) < 0)
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) {
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;
}