forked from sony/nmos-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_discovery_impl.cpp
571 lines (484 loc) · 25.4 KB
/
service_discovery_impl.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
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
#include "mdns/service_discovery_impl.h"
#include <boost/asio/ip/address.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/erase.hpp>
#include "cpprest/basic_utils.h"
#include "cpprest/host_utils.h"
#include "mdns/dns_sd_impl.h"
#include "slog/all_in_one.h"
namespace mdns_details
{
using namespace mdns;
static inline const std::chrono::steady_clock::time_point& reply_timeout(bool had_enough, bool more_coming, const std::chrono::steady_clock::time_point& latest_timeout, const std::chrono::steady_clock::time_point& earliest_timeout)
{
return had_enough
? more_coming ? earliest_timeout : (std::min)(earliest_timeout, latest_timeout)
: more_coming ? (std::max)(earliest_timeout, latest_timeout) : latest_timeout;
}
// map kDNSServiceInterfaceIndexLocalOnly to kDNSServiceInterfaceIndexAny, to handle AVAHI_IF_UNSPEC escaping from the Avahi compatibility layer
static inline std::uint32_t make_interface_id(std::uint32_t interfaceIndex)
{
return kDNSServiceInterfaceIndexLocalOnly == interfaceIndex ? kDNSServiceInterfaceIndexAny : interfaceIndex;
}
struct browse_context
{
// browse in-flight state
const browse_handler& handler;
bool& had_enough;
bool& more_coming;
slog::base_gate& gate;
};
static void DNSSD_API browse_reply(
DNSServiceRef sdRef,
const DNSServiceFlags flags,
uint32_t interfaceIndex,
DNSServiceErrorType errorCode,
const char* serviceName,
const char* regtype,
const char* replyDomain,
void* context)
{
browse_context* impl = (browse_context*)context;
if (errorCode == kDNSServiceErr_NoError)
{
if (0 != (flags & kDNSServiceFlagsAdd))
{
const browse_result result{ serviceName, regtype, replyDomain, make_interface_id(interfaceIndex) };
slog::log<slog::severities::more_info>(impl->gate, SLOG_FLF) << "After DNSServiceBrowse, DNSServiceBrowseReply got service: " << result.name << " for regtype: " << result.type << " domain: " << result.domain << " on interface: " << result.interface_id;
impl->had_enough = impl->handler(result);
}
impl->more_coming = 0 != (flags & kDNSServiceFlagsMoreComing);
}
else
{
slog::log<slog::severities::error>(impl->gate, SLOG_FLF) << "After DNSServiceBrowse, DNSServiceBrowseReply received error: " << errorCode;
}
}
static bool browse(const browse_handler& handler, const std::string& type, const std::string& domain, std::uint32_t interface_id, const std::chrono::steady_clock::duration& latest_timeout_, DNSServiceCancellationToken cancel, slog::base_gate& gate)
{
// in order to ensure the daemon actually performs a query rather than just returning results from its cache
// apply a minimum timeout rather than giving up the first time the more_coming flag is false
// see https://github.com/lathiat/avahi/blob/v0.7/avahi-core/multicast-lookup.c#L120
const auto earliest_timeout_ = std::chrono::seconds(1);
bool had_enough = false;
bool more_coming = true;
DNSServiceRef client = nullptr;
const auto now = std::chrono::steady_clock::now();
const auto latest_timeout = now + latest_timeout_;
const auto earliest_timeout = now + earliest_timeout_;
// could use if_indextoname to get a name for the interface (remembering that 0 means "do the right thing", i.e. usually any interface, and there are some other special values too; see dns_sd.h)
slog::log<slog::severities::more_info>(gate, SLOG_FLF) << "DNSServiceBrowse for regtype: " << type << " domain: " << domain << " on interface: " << interface_id;
browse_context context{ handler, had_enough, more_coming, gate };
DNSServiceErrorType errorCode = DNSServiceBrowse(&client, 0, interface_id, type.c_str(), !domain.empty() ? domain.c_str() : NULL, browse_reply, &context);
if (errorCode == kDNSServiceErr_NoError)
{
do
{
// wait for up to timeout for a response
const auto& absolute_timeout = reply_timeout(had_enough, more_coming, latest_timeout, earliest_timeout);
int wait_millis = (std::max)(0, (int)std::chrono::duration_cast<std::chrono::milliseconds>(absolute_timeout - std::chrono::steady_clock::now()).count());
// process the next browse responses (callback may be called more than once, or (at least with Avahi!) not at all, in any call to DNSServiceProcessResult)
slog::log<slog::severities::too_much_info>(gate, SLOG_FLF) << "DNSServiceProcessResult for " << wait_millis << "ms";
errorCode = DNSServiceProcessResult(client, wait_millis, cancel);
if (errorCode == kDNSServiceErr_NoError)
{
slog::log<slog::severities::too_much_info>(gate, SLOG_FLF) << "After DNSServiceBrowse, DNSServiceProcessResult succeeded";
// callback called, potentially more results coming
}
else if (errorCode == kDNSServiceErr_Timeout_)
{
slog::log<slog::severities::more_info>(gate, SLOG_FLF) << "After DNSServiceBrowse, DNSServiceProcessResult timed out or was cancelled";
break;
}
else
{
slog::log<slog::severities::error>(gate, SLOG_FLF) << "After DNSServiceBrowse, DNSServiceProcessResult reported error: " << errorCode;
break;
}
} while (reply_timeout(had_enough, more_coming, latest_timeout, earliest_timeout) > std::chrono::steady_clock::now());
DNSServiceRefDeallocate(client);
}
else
{
slog::log<slog::severities::error>(gate, SLOG_FLF) << "DNSServiceBrowse reported error: " << errorCode;
}
return had_enough;
}
static txt_records parse_txt_records(const unsigned char* txtRecord, size_t txtLen)
{
txt_records records;
size_t i = 0;
while (i < txtLen)
{
// each record is preceded by a length byte (so max length of each record is 255)
size_t len = txtRecord[i++];
records.emplace_back(&txtRecord[i], &txtRecord[i + len]);
i += len;
}
return records;
}
#ifdef _WIN32
inline std::string ierase_tail_copy(const std::string& input, const std::string& tail)
{
return boost::algorithm::iends_with(input, tail)
? boost::algorithm::erase_tail_copy(input, (int)tail.size())
: input;
}
static const std::string suffix_absolute(".");
static const std::string suffix_local(".local");
static std::string without_suffix(const std::string& host_name)
{
return ierase_tail_copy(ierase_tail_copy(host_name, suffix_absolute), suffix_local);
}
#endif
struct resolve_context
{
// resolve in-flight state
const resolve_handler& handler;
bool& had_enough;
bool& more_coming;
slog::base_gate& gate;
};
static void DNSSD_API resolve_reply(
DNSServiceRef sdRef,
const DNSServiceFlags flags,
uint32_t interfaceIndex,
DNSServiceErrorType errorCode,
const char* fullname,
const char* hosttarget,
uint16_t port,
uint16_t txtLen,
const unsigned char* txtRecord,
void* context)
{
resolve_context* impl = (resolve_context*)context;
if (errorCode == kDNSServiceErr_NoError)
{
{
const resolve_result result{ hosttarget, ntohs(port), parse_txt_records(txtRecord, txtLen), make_interface_id(interfaceIndex) };
slog::log<slog::severities::more_info>(impl->gate, SLOG_FLF) << "After DNSServiceResolve, DNSServiceResolveReply got host: " << result.host_name << " port: " << (int)result.port;
impl->had_enough = impl->handler(result);
}
impl->more_coming = 0 != (flags & kDNSServiceFlagsMoreComing);
}
else
{
slog::log<slog::severities::error>(impl->gate, SLOG_FLF) << "After DNSServiceResolve, DNSServiceResolveReply received error: " << errorCode;
}
}
#ifdef HAVE_DNSSERVICEGETADDRINFO
struct getaddrinfo_context
{
// getaddrinfo in-flight state
const address_handler& handler;
bool& had_enough;
bool& more_coming;
slog::base_gate& gate;
};
// this just shouldn't be this hard!
static boost::asio::ip::address from_sockaddr(const sockaddr& address)
{
if (address.sa_family == AF_INET)
{
auto addr_in = (const sockaddr_in&)address;
auto addr = addr_in.sin_addr.s_addr;
return boost::asio::ip::address_v4(ntohl(addr));
}
else if (address.sa_family == AF_INET6)
{
auto addr_in6 = (const sockaddr_in6&)address;
auto addr6 = addr_in6.sin6_addr.s6_addr;
boost::asio::ip::address_v6::bytes_type addr6_bytes;
std::copy_n(addr6, addr6_bytes.size(), addr6_bytes.begin());
//auto scope_id = addr_in6.sin6_scope_id;
//return boost::asio::ip::address_v6(addr6_bytes, scope_id);
return boost::asio::ip::address_v6(addr6_bytes);
}
else
{
return{};
}
}
static void DNSSD_API getaddrinfo_reply(
DNSServiceRef sdRef,
const DNSServiceFlags flags,
uint32_t interfaceIndex,
DNSServiceErrorType errorCode,
const char* hostname,
const struct sockaddr* address,
uint32_t ttl,
void* context)
{
getaddrinfo_context* impl = (getaddrinfo_context*)context;
if (errorCode == kDNSServiceErr_NoError)
{
if (0 != (flags & kDNSServiceFlagsAdd) && 0 != address)
{
const auto ip_address = from_sockaddr(*address);
if (!ip_address.is_unspecified())
{
const address_result result{ hostname, ip_address.to_string(), ttl, make_interface_id(interfaceIndex) };
slog::log<slog::severities::more_info>(impl->gate, SLOG_FLF) << "After DNSServiceGetAddrInfo, DNSServiceGetAddrInfoReply got address: " << result.ip_address << " for host: " << result.host_name;
impl->had_enough = impl->handler(result);
}
}
impl->more_coming = 0 != (flags & kDNSServiceFlagsMoreComing);
}
else
{
slog::log<slog::severities::error>(impl->gate, SLOG_FLF) << "After DNSServiceGetAddrInfo, DNSServiceGetAddrInfoReply received error: " << errorCode;
}
}
#endif
static bool resolve(const resolve_handler& handler, const std::string& name, const std::string& type, const std::string& domain, std::uint32_t interface_id, const std::chrono::steady_clock::duration& latest_timeout_, DNSServiceCancellationToken cancel, slog::base_gate& gate)
{
const auto earliest_timeout_ = std::chrono::seconds(0);
bool had_enough = false;
bool more_coming = true;
DNSServiceRef client = nullptr;
const auto now = std::chrono::steady_clock::now();
const auto latest_timeout = now + latest_timeout_;
const auto earliest_timeout = now + earliest_timeout_;
// could use if_indextoname to get a name for the interface (remembering that 0 means "do the right thing", i.e. usually any interface, and there are some other special values too; see dns_sd.h)
slog::log<slog::severities::more_info>(gate, SLOG_FLF) << "DNSServiceResolve for name: " << name << " regtype: " << type << " domain: " << domain << " on interface: " << interface_id;
resolve_context context{ handler, had_enough, more_coming, gate };
DNSServiceErrorType errorCode = DNSServiceResolve(&client, 0, interface_id, name.c_str(), type.c_str(), domain.c_str(), (DNSServiceResolveReply)resolve_reply, &context);
if (errorCode == kDNSServiceErr_NoError)
{
do
{
// wait for up to timeout for a response
const auto& absolute_timeout = reply_timeout(had_enough, more_coming, latest_timeout, earliest_timeout);
int wait_millis = (std::max)(0, (int)std::chrono::duration_cast<std::chrono::milliseconds>(absolute_timeout - std::chrono::steady_clock::now()).count());
// process the next resolve responses (callback may be called more than once, or not at all, in any call to DNSServiceProcessResult)
slog::log<slog::severities::too_much_info>(gate, SLOG_FLF) << "DNSServiceProcessResult for " << wait_millis << "ms";
errorCode = DNSServiceProcessResult(client, wait_millis, cancel);
if (errorCode == kDNSServiceErr_NoError)
{
slog::log<slog::severities::too_much_info>(gate, SLOG_FLF) << "After DNSServiceResolve, DNSServiceProcessResult succeeded";
// callback called, potentially more results coming
}
else if (errorCode == kDNSServiceErr_Timeout_)
{
slog::log<slog::severities::more_info>(gate, SLOG_FLF) << "After DNSServiceResolve, DNSServiceProcessResult timed out or was cancelled";
break;
}
else
{
slog::log<slog::severities::error>(gate, SLOG_FLF) << "After DNSServiceResolve, DNSServiceProcessResult reported error: " << errorCode;
break;
}
} while (reply_timeout(had_enough, more_coming, latest_timeout, earliest_timeout) > std::chrono::steady_clock::now());
DNSServiceRefDeallocate(client);
}
else
{
slog::log<slog::severities::error>(gate, SLOG_FLF) << "DNSServiceResolve reported error: " << errorCode;
}
return had_enough;
}
static bool getaddrinfo(const address_handler& handler, const std::string& host_name, std::uint32_t interface_id, const std::chrono::steady_clock::duration& latest_timeout_, DNSServiceCancellationToken cancel, slog::base_gate& gate)
{
const auto earliest_timeout_ = std::chrono::seconds(0);
bool had_enough = false;
#ifdef HAVE_DNSSERVICEGETADDRINFO
bool more_coming = true;
DNSServiceRef client = nullptr;
const auto now = std::chrono::steady_clock::now();
const auto latest_timeout = now + latest_timeout_;
const auto earliest_timeout = now + earliest_timeout_;
// for now, limited to IPv4
const DNSServiceProtocol protocol = kDNSServiceProtocol_IPv4;
#ifdef _WIN32
if (protocol == kDNSServiceProtocol_IPv4 && interface_id >= kIPv6IfIndexBase)
{
// no point trying in this case!
slog::log<slog::severities::too_much_info>(gate, SLOG_FLF) << "DNSServiceGetAddrInfo not tried for hostname: " << host_name << " on interface: " << interface_id;
}
else
#endif
{
slog::log<slog::severities::more_info>(gate, SLOG_FLF) << "DNSServiceGetAddrInfo for hostname: " << host_name << " on interface: " << interface_id;
getaddrinfo_context context{ handler, had_enough, more_coming, gate };
DNSServiceErrorType errorCode = DNSServiceGetAddrInfo(&client, 0, interface_id, protocol, host_name.c_str(), getaddrinfo_reply, &context);
if (errorCode == kDNSServiceErr_NoError)
{
do
{
// wait for up to timeout for a response
const auto& absolute_timeout = reply_timeout(had_enough, more_coming, latest_timeout, earliest_timeout);
int wait_millis = (std::max)(0, (int)std::chrono::duration_cast<std::chrono::milliseconds>(absolute_timeout - std::chrono::steady_clock::now()).count());
// process the next lookup responses (callback may be called more than once, or potentially not at all, in any call to DNSServiceProcessResult)
slog::log<slog::severities::too_much_info>(gate, SLOG_FLF) << "DNSServiceProcessResult for " << wait_millis << "ms";
errorCode = DNSServiceProcessResult(client, wait_millis, cancel);
if (errorCode == kDNSServiceErr_NoError)
{
slog::log<slog::severities::too_much_info>(gate, SLOG_FLF) << "After DNSServiceGetAddrInfo, DNSServiceProcessResult succeeded";
// callback called, potentially more results coming
}
else if (errorCode == kDNSServiceErr_Timeout_)
{
slog::log<slog::severities::more_info>(gate, SLOG_FLF) << "After DNSServiceGetAddrInfo, DNSServiceProcessResult timed out or was cancelled";
break;
}
else
{
slog::log<slog::severities::error>(gate, SLOG_FLF) << "After DNSServiceGetAddrInfo, DNSServiceProcessResult reported error: " << errorCode;
break;
}
} while (reply_timeout(had_enough, more_coming, latest_timeout, earliest_timeout) > std::chrono::steady_clock::now());
DNSServiceRefDeallocate(client);
}
else
{
slog::log<slog::severities::error>(gate, SLOG_FLF) << "DNSServiceGetAddrInfo reported error: " << errorCode;
}
}
#endif
if (!had_enough)
{
// hmmm, plain old getaddrinfo uses all name resolution mechanisms so isn't specific to a particular interface
slog::log<slog::severities::more_info>(gate, SLOG_FLF) << "getaddrinfo for hostname: " << host_name;
#ifdef _WIN32
// on Windows, resolution of multicast .local domain names doesn't seem to work even with the Bonjour service running?
const auto ip_addresses = web::hosts::experimental::host_addresses(utility::s2us(without_suffix(host_name)));
#else
// on Linux, the name-service switch should be configured to use Avahi to resolve multicast .local domain names
// by including 'mdns4' or 'mdns4_minimal' in the hosts stanza of /etc/nsswitch.conf
const auto ip_addresses = web::hosts::experimental::host_addresses(utility::s2us(host_name));
#endif
for (auto& ip_address : ip_addresses)
{
const address_result result{ host_name, utility::us2s(ip_address) };
slog::log<slog::severities::more_info>(gate, SLOG_FLF) << "Using getaddrinfo, got address: " << result.ip_address << " for host: " << result.host_name;
had_enough = handler(result);
}
if (ip_addresses.empty()) slog::log<slog::severities::more_info>(gate, SLOG_FLF) << "Using getaddrinfo, got no addresses for host: " << host_name;
}
return had_enough;
}
}
namespace mdns
{
namespace details
{
struct cancellation_guard
{
cancellation_guard(const pplx::cancellation_token& source)
: source(source)
, target(nullptr)
{
if (source.is_cancelable())
{
DNSServiceCreateCancellationToken(&target);
reg = source.register_callback([this] { DNSServiceCancel(target); });
}
}
~cancellation_guard()
{
if (pplx::cancellation_token_registration{} != reg) source.deregister_callback(reg);
DNSServiceCancellationTokenDeallocate(target);
}
pplx::cancellation_token source;
DNSServiceCancellationToken target;
pplx::cancellation_token_registration reg;
};
// hm, 'final' may be appropriate here rather than 'override'?
class service_discovery_impl_ : public service_discovery_impl
{
public:
explicit service_discovery_impl_(slog::base_gate& gate)
: gate(gate)
{
}
~service_discovery_impl_() override
{
}
pplx::task<bool> browse(const browse_handler& handler, const std::string& type, const std::string& domain, std::uint32_t interface_id, const std::chrono::steady_clock::duration& timeout, const pplx::cancellation_token& token) override
{
auto gate_ = &this->gate;
return pplx::create_task([=]
{
cancellation_guard guard(token);
auto result = mdns_details::browse(handler, type, domain, interface_id, timeout, guard.target, *gate_);
// when this task is cancelled, make sure it doesn't just return an empty/partial result
if (token.is_canceled()) pplx::cancel_current_task();
// hmm, perhaps should throw an exception on timeout, rather than returning an empty result?
return result;
}, token);
}
pplx::task<bool> resolve(const resolve_handler& handler, const std::string& name, const std::string& type, const std::string& domain, std::uint32_t interface_id, const std::chrono::steady_clock::duration& timeout, const pplx::cancellation_token& token) override
{
auto gate_ = &this->gate;
return pplx::create_task([=]
{
cancellation_guard guard(token);
auto result = mdns_details::resolve([&](const mdns::resolve_result& resolved_)
{
auto resolved = resolved_;
mdns_details::getaddrinfo([&](const mdns_details::address_result& address)
{
resolved.ip_addresses.push_back(address.ip_address);
return true;
}, resolved.host_name, resolved.interface_id, timeout, guard.target, *gate_);
return handler(resolved);
}, name, type, domain, interface_id, timeout, guard.target, *gate_);
// when this task is cancelled, make sure it doesn't just return an empty/partial result
if (token.is_canceled()) pplx::cancel_current_task();
// hmm, perhaps should throw an exception on timeout, rather than returning an empty result?
return result;
}, token);
}
pplx::task<bool> getaddrinfo(const address_handler& handler, const std::string& host_name, std::uint32_t interface_id, const std::chrono::steady_clock::duration& timeout, const pplx::cancellation_token& token) override
{
auto gate_ = &this->gate;
return pplx::create_task([=]
{
cancellation_guard guard(token);
auto result = mdns_details::getaddrinfo(handler, host_name, interface_id, timeout, guard.target, *gate_);
// when this task is cancelled, make sure it doesn't just return an empty/partial result
if (token.is_canceled()) pplx::cancel_current_task();
// hmm, perhaps should throw an exception on timeout, rather than returning an empty result?
return result;
}, token);
}
private:
slog::base_gate& gate;
};
}
service_discovery::service_discovery(std::unique_ptr<details::service_discovery_impl> impl)
: impl(std::move(impl))
{
}
service_discovery::service_discovery(slog::base_gate& gate)
: impl(new details::service_discovery_impl_(gate))
{
}
service_discovery::service_discovery(service_discovery&& other)
: impl(std::move(other.impl))
{
}
service_discovery& service_discovery::operator=(service_discovery&& other)
{
if (this != &other)
{
impl = std::move(other.impl);
}
return *this;
}
service_discovery::~service_discovery()
{
}
pplx::task<bool> service_discovery::browse(const browse_handler& handler, const std::string& type, const std::string& domain, std::uint32_t interface_id, const std::chrono::steady_clock::duration& timeout, const pplx::cancellation_token& token)
{
return impl->browse(handler, type, domain, interface_id, timeout, token);
}
pplx::task<bool> service_discovery::resolve(const resolve_handler& handler, const std::string& name, const std::string& type, const std::string& domain, std::uint32_t interface_id, const std::chrono::steady_clock::duration& timeout, const pplx::cancellation_token& token)
{
return impl->resolve(handler, name, type, domain, interface_id, timeout, token);
}
pplx::task<bool> service_discovery::getaddrinfo(const address_handler& handler, const std::string& host_name, std::uint32_t interface_id, const std::chrono::steady_clock::duration& timeout, const pplx::cancellation_token& token)
{
return impl->getaddrinfo(handler, host_name, interface_id, timeout, token);
}
}