-
Notifications
You must be signed in to change notification settings - Fork 316
/
dns_sd_windows.c
470 lines (388 loc) · 12.2 KB
/
dns_sd_windows.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
* libiio - Library for interfacing industrial I/O (IIO) devices
*
* Copyright (C) 2014-2022 Analog Devices, Inc.
* Author: Adrian Suciu <adrian.suciu@analog.com>
*
* Based on https://github.com/mjansson/mdns/blob/main/mdns.c
* which should be sync'ed with the mdns.h file and is Licensed under Public Domain
*/
#include "dns_sd.h"
#include "deps/mdns/mdns.h"
#include "iio-private.h"
#include <stdio.h>
#include <errno.h>
#include <winsock2.h>
#include <iphlpapi.h>
#include <iio/iio-backend.h>
#include <iio/iio-debug.h>
#include <iio/iio-lock.h>
#define _STRINGIFY(x) #x
#define STRINGIFY(x) _STRINGIFY(x)
#define MDNS_PORT_STR STRINGIFY(MDNS_PORT)
static uint64_t lasttime;
#define TIMEOUT 2
#ifdef HAVE_IPV6
static const unsigned char localhost[] = {
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1
};
static const unsigned char localhost_mapped[] = {
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0xff, 0xff, 0x7f, 0, 0, 1
};
static bool is_localhost6(const struct sockaddr_in6 *saddr6)
{
const uint8_t *addr = saddr6->sin6_addr.s6_addr;
return !memcmp(addr, localhost, sizeof(localhost)) ||
!memcmp(addr, localhost_mapped, sizeof(localhost_mapped));
}
#endif
static bool is_localhost4(const struct sockaddr_in *saddr)
{
return saddr->sin_addr.S_un.S_un_b.s_b1 == 127 &&
saddr->sin_addr.S_un.S_un_b.s_b2 == 0 &&
saddr->sin_addr.S_un.S_un_b.s_b3 == 0 &&
saddr->sin_addr.S_un.S_un_b.s_b4 == 1;
}
static struct dns_sd_discovery_data *new_discovery_data(struct dns_sd_discovery_data *dd)
{
struct dns_sd_discovery_data *d;
d = zalloc(sizeof(*d));
if (!d)
return NULL;
if (dd)
d->lock = dd->lock;
return d;
}
static mdns_string_t ip_address_to_string(char *buffer, size_t capacity,
const struct sockaddr *addr, size_t addrlen)
{
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
char host[NI_MAXHOST] = { 0 };
char service[NI_MAXSERV] = { 0 };
int ret, len = 0;
mdns_string_t str;
ret = getnameinfo((const struct sockaddr *)addr, (socklen_t)addrlen, host,
NI_MAXHOST, service, NI_MAXSERV, NI_NUMERICSERV | NI_NUMERICHOST);
if (ret == 0) {
if (addr->sa_family == AF_INET6) {
if (addr6->sin6_port != 0
&& strncmp(service, MDNS_PORT_STR, sizeof(MDNS_PORT_STR))) {
len = snprintf(buffer, capacity, "[%s]:%s", host, service);
} else {
len = snprintf(buffer, capacity, "%s", host);
}
} else if (addr4->sin_port != 0
&& strncmp(service, MDNS_PORT_STR, sizeof(MDNS_PORT_STR))) {
len = snprintf(buffer, capacity, "%s:%s", host, service);
} else {
len = snprintf(buffer, capacity, "%s", host);
}
}
if (len >= (int)capacity)
len = (int)capacity - 1;
str.str = buffer;
str.length = len;
return str;
}
static int open_client_sockets(const struct iio_context_params *params,
int *sockets, unsigned int max_sockets)
{
IP_ADAPTER_UNICAST_ADDRESS *unicast;
IP_ADAPTER_ADDRESSES *adapter_address = 0;
PIP_ADAPTER_ADDRESSES adapter;
ULONG address_size = 8000;
unsigned int i, ret, num_retries = 4, num_sockets = 0;
struct sockaddr_in *saddr;
unsigned long param = 1;
int sock;
/* When sending, each socket can only send to one network interface
* Thus we need to open one socket for each interface and address family */
do {
adapter_address = malloc(address_size);
if (!adapter_address)
return -ENOMEM;
ret = GetAdaptersAddresses(AF_UNSPEC,
GAA_FLAG_SKIP_MULTICAST
#ifndef HAVE_IPV6
| GAA_FLAG_SKIP_ANYCAST
#endif
, 0,
adapter_address, &address_size);
if (ret != ERROR_BUFFER_OVERFLOW)
break;
free(adapter_address);
adapter_address = 0;
} while (num_retries-- > 0);
if (!adapter_address || (ret != NO_ERROR)) {
free(adapter_address);
prm_err(params, "Failed to get network adapter addresses\n");
return num_sockets;
}
for (adapter = adapter_address; adapter; adapter = adapter->Next) {
if (adapter->TunnelType == TUNNEL_TYPE_TEREDO)
continue;
if (adapter->OperStatus != IfOperStatusUp)
continue;
for (unicast = adapter->FirstUnicastAddress;
unicast; unicast = unicast->Next) {
if (unicast->Address.lpSockaddr->sa_family == AF_INET) {
saddr = (struct sockaddr_in *)unicast->Address.lpSockaddr;
if (!is_localhost4(saddr) &&
num_sockets < max_sockets) {
saddr->sin_port = htons((unsigned short)MDNS_PORT);
sock = mdns_socket_open_ipv4(saddr);
if (sock >= 0)
sockets[num_sockets++] = sock;
}
}
#ifdef HAVE_IPV6
else if (unicast->Address.lpSockaddr->sa_family == AF_INET6) {
struct sockaddr_in6 *saddr6;
saddr6 = (struct sockaddr_in6 *)unicast->Address.lpSockaddr;
if (unicast->DadState == NldsPreferred &&
!is_localhost6(saddr6) &&
num_sockets < max_sockets) {
saddr6->sin6_port = htons((unsigned short)MDNS_PORT);
sock = mdns_socket_open_ipv6(saddr6);
if (sock >= 0)
sockets[num_sockets++] = sock;
}
}
#endif
}
}
free(adapter_address);
for (i = 0; i < num_sockets; i++)
ioctlsocket(sockets[i], FIONBIO, ¶m);
return num_sockets;
}
/* We should get:
* - "service" record (SRV) specifying host (name) and port
* - IPv4 "address" record (A) specifying IPv4 address of a given host
* - IPv6 "address" record (AAAA) specifying IPv6 address of a given host
* It's this routine that gets called, and needs to stitch things together
* The DNS host doesn't necessary need to be the actual host (but for
* mdns - it usually is.
*/
static int query_callback(int sock, const struct sockaddr *from, size_t addrlen,
mdns_entry_type_t entry, uint16_t query_id,
uint16_t rtype, uint16_t rclass, uint32_t ttl,
const void *data, size_t size, size_t name_offset,
size_t name_length,
size_t record_offset, size_t record_length,
void *user_data)
{
struct dns_sd_cb_data *cb_data = user_data;
struct dns_sd_discovery_data *dd = cb_data->d;
const struct iio_context_params *params = cb_data->params;
char addrbuffer[64];
char entrybuffer[256];
char namebuffer[256];
mdns_record_srv_t srv;
mdns_string_t entrystr, fromaddrstr;
if (!dd) {
prm_err(params, "DNS SD: Missing info structure. Stop browsing.\n");
goto quit;
}
switch (rtype) {
case MDNS_RECORDTYPE_SRV:
#ifdef HAVE_IPV6
case MDNS_RECORDTYPE_AAAA:
#endif
break;
default:
goto quit;
}
if (entry != MDNS_ENTRYTYPE_ANSWER)
goto quit;
entrystr = mdns_string_extract(data, size, &name_offset,
entrybuffer, sizeof(entrybuffer));
if (!strstr(entrystr.str, "_iio._tcp.local"))
goto quit;
iio_mutex_lock(dd->lock);
if (rtype == MDNS_RECORDTYPE_SRV) {
srv = mdns_record_parse_srv(data, size, record_offset, record_length,
namebuffer, sizeof(namebuffer));
fromaddrstr = ip_address_to_string(addrbuffer, sizeof(addrbuffer),
from, addrlen);
prm_dbg(params, "%.*s : %.*s SRV %.*s priority %d weight %d port %d\n",
MDNS_STRING_FORMAT(fromaddrstr), MDNS_STRING_FORMAT(entrystr),
MDNS_STRING_FORMAT(srv.name), srv.priority, srv.weight, srv.port);
}
#ifdef HAVE_IPV6
else {
struct sockaddr_in6 addr;
mdns_record_parse_aaaa(data, size, record_offset, record_length, &addr);
/* Find a match based on hostname */
for (; dd->next; dd = dd->next) {
if (dd->hostname
&& dd->found
&& !strncmp(dd->hostname, entrystr.str, entrystr.length - 1))
break;
}
if (!dd->next) {
prm_dbg(params, "No SRV found for hostname %.*s\n",
MDNS_STRING_FORMAT(entrystr));
iio_mutex_unlock(dd->lock);
goto quit;
}
srv.name.str = dd->hostname;
srv.name.length = strlen(dd->hostname) + 1;
srv.port = dd->port;
if (IN6_IS_ADDR_LINKLOCAL(&addr.sin6_addr))
addr.sin6_scope_id = dd->iface;
else
addr.sin6_scope_id = 0;
fromaddrstr = ip_address_to_string(addrbuffer, sizeof(addrbuffer),
(struct sockaddr *)&addr, sizeof(addr));
prm_dbg(params, "Found IPv6 address %.*s for hostname %s\n",
MDNS_STRING_FORMAT(fromaddrstr), dd->hostname);
}
#endif
while (dd->next)
dd = dd->next;
/* new hostname and port */
if (srv.name.length > 1) {
dd->hostname = iio_strndup(srv.name.str,
srv.name.length - 1);
if (!dd->hostname)
goto mem_fail;
}
iio_strlcpy(dd->addr_str, fromaddrstr.str, fromaddrstr.length + 1);
dd->port = srv.port;
dd->found = (from->sa_family != AF_INET);
#ifdef HAVE_IPV6
if (dd->found)
dd->iface = (uint16_t)((struct sockaddr_in6 *)from)->sin6_scope_id;
#endif
prm_dbg(params, "DNS SD: added SRV %s (%s port: %hu)\n",
dd->hostname, dd->addr_str, dd->port);
/* A list entry was filled, prepare new item on the list */
dd->next = new_discovery_data(dd);
if (!dd->next)
goto mem_fail;
lasttime = iio_read_counter_us();
iio_mutex_unlock(dd->lock);
quit:
return 0;
mem_fail:
iio_mutex_unlock(dd->lock);
prm_err(params, "DNS SD mDNS Resolver : memory failure\n");
return -ENOMEM;
}
int dnssd_find_hosts(const struct iio_context_params *params,
struct dns_sd_discovery_data **ddata)
{
WORD versionWanted = MAKEWORD(1, 1);
WSADATA wsaData;
const char service[] = "_iio._tcp.local";
size_t rec, records, capacity = 2048;
struct dns_sd_discovery_data *d;
unsigned int isock, num_sockets;
void *buffer;
int sockets[32];
int transaction_id[32];
int nfds, res, ret = -ENOMEM;
struct timeval timeout;
uint64_t nowtime;
int64_t diff;
if (WSAStartup(versionWanted, &wsaData)) {
prm_err(params, "Failed to initialize WinSock\n");
return -WSAGetLastError();
}
prm_dbg(params, "DNS SD: Start service discovery.\n");
d = new_discovery_data(NULL);
if (!d)
goto out_wsa_cleanup;
/* pass the structure back, so it can be freed if err */
*ddata = d;
d->lock = iio_mutex_create();
ret = iio_err(d->lock);
if (ret)
goto out_wsa_cleanup;
buffer = malloc(capacity);
if (!buffer)
goto out_destroy_lock;
prm_dbg(params, "Sending DNS-SD discovery\n");
ret = open_client_sockets(params, sockets, ARRAY_SIZE(sockets));
if (ret <= 0) {
prm_err(params, "Failed to open any client sockets\n");
goto out_free_buffer;
}
num_sockets = (unsigned int)ret;
prm_dbg(params, "Opened %d socket%s for mDNS query\n",
num_sockets, (num_sockets > 1) ? "s" : "");
prm_dbg(params, "Sending mDNS query: %s\n", service);
/* Walk through all the open interfaces/sockets, and send a query */
for (isock = 0; isock < num_sockets; isock++) {
ret = mdns_query_send(sockets[isock], MDNS_RECORDTYPE_PTR,
service, sizeof(service)-1, buffer,
capacity, 0);
if (ret < 0)
prm_err(params, "Failed to send mDNS query: errno %d\n", errno);
transaction_id[isock] = ret;
}
/* This is a simple implementation that loops as long as we get replies */
prm_dbg(params, "Reading mDNS query replies\n");
records = 0;
lasttime = iio_read_counter_us();
do {
nfds = 0;
timeout.tv_sec = TIMEOUT;
timeout.tv_usec = 0;
fd_set readfs;
FD_ZERO(&readfs);
for (isock = 0; isock < num_sockets; ++isock) {
if (sockets[isock] >= nfds)
nfds = sockets[isock] + 1;
FD_SET(sockets[isock], &readfs);
}
res = select(nfds, &readfs, 0, 0, &timeout);
if (res > 0) {
for (isock = 0; isock < num_sockets; ++isock) {
if (FD_ISSET(sockets[isock], &readfs)) {
rec = mdns_query_recv(sockets[isock], buffer,
capacity, query_callback,
d, transaction_id[isock]);
if (rec > 0)
records += rec;
}
FD_SET(sockets[isock], &readfs);
}
}
/* res > 0 even if we didn't process anything :(
* timeout from the last time we successfully added a proper mdns record
*/
nowtime = iio_read_counter_us();
/* convert to ms */
diff = (nowtime - lasttime) / 1000ull;
if (diff > (TIMEOUT * 1000))
res = 0;
} while (res > 0);
for (isock = 0; isock < num_sockets; ++isock)
mdns_socket_close(sockets[isock]);
prm_dbg(params, "Closed %i socket%s, processed %zu record%s\n",
num_sockets, (num_sockets > 1) ? "s" : "",
records, (records > 1) ? "s" : "" );
remove_dup_discovery_data(params, &d);
port_knock_discovery_data(params, &d);
/* since d may have changed, make sure we pass back the start */
*ddata = d;
ret = 0;
out_free_buffer:
free(buffer);
out_destroy_lock:
iio_mutex_destroy(d->lock);
out_wsa_cleanup:
WSACleanup();
return ret;
}
int dnssd_resolve_host(const struct iio_context_params *params,
const char *hostname, char *ip_addr, const int addr_len)
{
return -ENOENT;
}