-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
DnssdImpl.cpp
831 lines (672 loc) · 30.4 KB
/
DnssdImpl.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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
/*
*
* Copyright (c) 2021-2022 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "DnssdImpl.h"
#include <algorithm>
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <string>
#include <utility>
// Note: Include dns-sd-internal.h is needed, this source file uses some undocumented functions.
#include <dns-sd-internal.h>
#include <dns-sd.h>
#include <glib.h>
#include <lib/dnssd/platform/Dnssd.h>
#include <net/if.h>
#include <inet/InetBuildConfig.h>
#include <lib/support/CHIPMemString.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/SafeInt.h>
#include <lib/support/Span.h>
#include <platform/CHIPDeviceConfig.h>
#include <platform/PlatformManager.h>
#include "MainLoop.h"
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
#include <platform/ThreadStackManager.h>
#endif
namespace {
constexpr uint8_t kDnssdKeyMaxSize = 32;
constexpr const char * kEmptyAddressIpv6 = "0000:0000:0000:0000:0000:0000:0000:0000";
// The number of miliseconds which must elapse without a new "found" event before
// mDNS browsing is considered finished. We need this timeout because Tizen Native
// API does not deliver all-for-now signal (such signal is delivered by e.g. Avahi)
// and the browsing callback is called multiple times (once for each service found).
constexpr unsigned int kDnssdBrowseTimeoutMs = 250;
bool IsSupportedProtocol(chip::Dnssd::DnssdServiceProtocol protocol)
{
return (protocol == chip::Dnssd::DnssdServiceProtocol::kDnssdProtocolUdp) ||
(protocol == chip::Dnssd::DnssdServiceProtocol::kDnssdProtocolTcp);
}
const char * GetProtocolString(chip::Dnssd::DnssdServiceProtocol protocol)
{
return protocol == chip::Dnssd::DnssdServiceProtocol::kDnssdProtocolUdp ? chip::Dnssd::kCommissionProtocol
: chip::Dnssd::kOperationalProtocol;
}
std::string GetFullType(const char * type, chip::Dnssd::DnssdServiceProtocol protocol)
{
std::ostringstream typeBuilder;
typeBuilder << type << "." << GetProtocolString(protocol);
return typeBuilder.str();
}
CHIP_ERROR GetChipError(int dnssdError)
{
switch (dnssdError)
{
case DNSSD_ERROR_NONE:
return CHIP_NO_ERROR;
case DNSSD_ERROR_NAME_CONFLICT:
return CHIP_ERROR_MDNS_COLLISION;
case DNSSD_ERROR_OUT_OF_MEMORY:
return CHIP_ERROR_NO_MEMORY;
default:
return CHIP_ERROR_INTERNAL;
}
}
void OnRegister(dnssd_error_e result, dnssd_service_h service, void * data)
{
auto rCtx = reinterpret_cast<chip::Dnssd::RegisterContext *>(data);
ChipLogDetail(DeviceLayer, "DNSsd %s: name: %s, type: %s, port: %u, interfaceId: %u", __func__, rCtx->mName, rCtx->mType,
rCtx->mPort, rCtx->mInterfaceId);
rCtx->MainLoopQuit();
if (result != DNSSD_ERROR_NONE)
{
ChipLogError(DeviceLayer, "DNSsd %s: Error: %d", __func__, result);
rCtx->mCallback(rCtx->mCbContext, nullptr, nullptr, GetChipError(result));
// After this point, the context might be no longer valid
rCtx->mInstance->RemoveContext(rCtx);
return;
}
rCtx->mCallback(rCtx->mCbContext, rCtx->mType, rCtx->mName, CHIP_NO_ERROR);
}
gboolean RegisterAsync(GMainLoop * mainLoop, gpointer userData)
{
ChipLogDetail(DeviceLayer, "DNSsd %s", __func__);
auto rCtx = reinterpret_cast<chip::Dnssd::RegisterContext *>(userData);
rCtx->mMainLoop = mainLoop;
int ret = dnssd_register_local_service(rCtx->mServiceHandle, OnRegister, rCtx);
VerifyOrReturnValue(ret == DNSSD_ERROR_NONE, false,
ChipLogError(DeviceLayer, "dnssd_register_local_service() failed. ret: %d", ret));
rCtx->mIsRegistered = true;
return true;
}
gboolean OnBrowseTimeout(void * userData)
{
ChipLogDetail(DeviceLayer, "DNSsd %s: all for now", __func__);
auto * bCtx = reinterpret_cast<chip::Dnssd::BrowseContext *>(userData);
bCtx->MainLoopQuit();
bCtx->mCallback(bCtx->mCbContext, bCtx->mServices.data(), bCtx->mServices.size(), true, CHIP_NO_ERROR);
// After this point the context might be no longer valid
bCtx->mInstance->RemoveContext(bCtx);
// This is a one-shot timer
return FALSE;
}
void OnBrowseAdd(chip::Dnssd::BrowseContext * context, const char * type, const char * name, uint32_t interfaceId)
{
ChipLogDetail(DeviceLayer, "DNSsd %s: name: %s, type: %s, interfaceId: %u", __func__, StringOrNullMarker(name),
StringOrNullMarker(type), interfaceId);
char * tokens = strdup(type);
char * regtype = strtok(tokens, ".");
chip::Dnssd::DnssdService dnssdService = {};
chip::Platform::CopyString(dnssdService.mName, name);
chip::Platform::CopyString(dnssdService.mType, regtype);
dnssdService.mProtocol = context->mProtocol;
dnssdService.mInterface = chip::Inet::InterfaceId(interfaceId);
context->mServices.push_back(dnssdService);
free(tokens);
}
void OnBrowseRemove(chip::Dnssd::BrowseContext * context, const char * type, const char * name, uint32_t interfaceId)
{
ChipLogDetail(DeviceLayer, "DNSsd %s: name: %s, type: %s, interfaceId: %u", __func__, StringOrNullMarker(name),
StringOrNullMarker(type), interfaceId);
context->mServices.erase(std::remove_if(
context->mServices.begin(), context->mServices.end(), [name, type, interfaceId](const chip::Dnssd::DnssdService & service) {
return strcmp(name, service.mName) == 0 && type == GetFullType(service.mType, service.mProtocol) &&
interfaceId == service.mInterface.GetPlatformInterface();
}));
}
void OnBrowse(dnssd_service_state_e state, dnssd_service_h service, void * data)
{
ChipLogDetail(DeviceLayer, "DNSsd %s", __func__);
auto bCtx = reinterpret_cast<chip::Dnssd::BrowseContext *>(data);
int ret;
// If there is already a timeout source, so we need to cancel it.
if (bCtx->mTimeoutSource != nullptr)
{
g_source_destroy(bCtx->mTimeoutSource);
g_source_unref(bCtx->mTimeoutSource);
}
// Start a timer, so we could detect when there is no more on-browse events.
// The timeout callback function will be called in the same event loop as the
// browse callback (this one), so locking is not required.
auto * source = g_timeout_source_new(kDnssdBrowseTimeoutMs);
g_source_set_callback(source, OnBrowseTimeout, bCtx, nullptr);
g_source_attach(source, g_main_context_get_thread_default());
bCtx->mTimeoutSource = source;
char * type = nullptr;
char * name = nullptr;
char * ifaceName = nullptr;
uint32_t interfaceId = 0;
ret = dnssd_service_get_type(service, &type);
VerifyOrExit(ret == DNSSD_ERROR_NONE, ChipLogError(DeviceLayer, "dnssd_service_get_type() failed. ret: %d", ret));
ret = dnssd_service_get_name(service, &name);
VerifyOrExit(ret == DNSSD_ERROR_NONE, ChipLogError(DeviceLayer, "dnssd_service_get_name() failed. ret: %d", ret));
ret = dnssd_service_get_interface(service, &ifaceName);
VerifyOrExit(ret == DNSSD_ERROR_NONE, ChipLogError(DeviceLayer, "dnssd_service_get_interface() failed. ret: %d", ret));
interfaceId = if_nametoindex(ifaceName);
VerifyOrExit(interfaceId > 0, ChipLogError(DeviceLayer, "if_nametoindex() failed. errno: %d", errno);
ret = DNSSD_ERROR_OPERATION_FAILED);
if (state == DNSSD_SERVICE_STATE_AVAILABLE)
{
OnBrowseAdd(bCtx, type, name, interfaceId);
}
else
{
OnBrowseRemove(bCtx, type, name, interfaceId);
}
exit:
dnssd_destroy_remote_service(service);
if (ret != DNSSD_ERROR_NONE)
{
bCtx->mCallback(bCtx->mCbContext, nullptr, 0, true, GetChipError(ret));
// After this point the context might be no longer valid
bCtx->mInstance->RemoveContext(bCtx);
}
g_free(type);
g_free(name);
g_free(ifaceName);
}
gboolean BrowseAsync(GMainLoop * mainLoop, gpointer userData)
{
ChipLogDetail(DeviceLayer, "DNSsd %s", __func__);
auto * bCtx = reinterpret_cast<chip::Dnssd::BrowseContext *>(userData);
auto interfaceId = bCtx->mInterfaceId;
bCtx->mMainLoop = mainLoop;
int ret;
if (interfaceId == 0)
{
ret = dnssd_browse_service(bCtx->mType, nullptr, &bCtx->mBrowserHandle, OnBrowse, bCtx);
}
else
{
char iface[IF_NAMESIZE + 1] = "";
VerifyOrReturnValue(if_indextoname(interfaceId, iface) != nullptr, false,
ChipLogError(DeviceLayer, "if_indextoname() failed. errno: %d", errno));
ret = dnssd_browse_service(bCtx->mType, iface, &bCtx->mBrowserHandle, OnBrowse, bCtx);
}
if (ret != DNSSD_ERROR_NONE)
{
ChipLogError(DeviceLayer, "dnssd_browse_service() failed. ret: %d", ret);
return false;
}
bCtx->mIsBrowsing = true;
return true;
}
void GetTextEntries(unsigned short txtLen, uint8_t * txtRecord, std::vector<chip::Dnssd::TextEntry> & textEntries)
{
VerifyOrReturn(txtLen > 1, ChipLogDetail(DeviceLayer, "DNSsd %s: No TXT records", __func__));
const uint8_t * txtRecordEnd = txtRecord + txtLen;
while (txtRecord < txtRecordEnd)
{
uint8_t txtRecordSize = txtRecord[0];
txtRecord++;
VerifyOrReturn(txtRecord + txtRecordSize <= txtRecordEnd,
ChipLogError(DeviceLayer, "DNSsd %s: Invalid TXT data", __func__));
for (size_t i = 0; i < txtRecordSize; i++)
{
if (txtRecord[i] == '=')
{
// NULL-terminate the key string
txtRecord[i] = '\0';
char * key = reinterpret_cast<char *>(txtRecord);
uint8_t * data = txtRecord + i + 1;
size_t dataSize = txtRecordSize - i - 1;
textEntries.push_back({ key, data, dataSize });
break;
}
}
// Move to the next text entry
txtRecord += txtRecordSize;
}
}
void OnResolve(dnssd_error_e result, dnssd_service_h service, void * data)
{
ChipLogDetail(DeviceLayer, "DNSsd %s", __func__);
auto rCtx = reinterpret_cast<chip::Dnssd::ResolveContext *>(data);
char * name = nullptr;
char * ipv4 = nullptr;
char * ipv6 = nullptr;
int port = 0;
char * interface = nullptr;
unsigned short txtLen = 0;
uint8_t * txtRecord = nullptr;
std::vector<chip::Dnssd::TextEntry> textEntries;
chip::Dnssd::DnssdService dnssdService = {};
chip::Inet::IPAddress ipAddr;
CHIP_ERROR err = CHIP_NO_ERROR;
rCtx->MainLoopQuit();
int ret = dnssd_service_get_name(service, &name);
VerifyOrExit(ret == DNSSD_ERROR_NONE, ChipLogError(DeviceLayer, "dnssd_service_get_name() failed. ret: %d", ret));
chip::Platform::CopyString(dnssdService.mName, name);
g_free(name);
ret = dnssd_service_get_ip(service, &ipv4, &ipv6);
VerifyOrExit(ret == DNSSD_ERROR_NONE, ChipLogError(DeviceLayer, "dnssd_service_get_ip() failed. ret: %d", ret));
// If both IPv4 and IPv6 are set, IPv6 address has higher priority.
if (ipv6 != nullptr && strcmp(ipv6, kEmptyAddressIpv6) != 0)
{
if (!chip::Inet::IPAddress::FromString(ipv6, ipAddr) || ipAddr.Type() != chip::Inet::IPAddressType::kIPv6)
{
ret = DNSSD_ERROR_OPERATION_FAILED;
}
}
#if INET_CONFIG_ENABLE_IPV4
else if (ipv4 != nullptr)
{
if (!chip::Inet::IPAddress::FromString(ipv4, ipAddr) || ipAddr.Type() != chip::Inet::IPAddressType::kIPv4)
{
ret = DNSSD_ERROR_OPERATION_FAILED;
}
}
#endif
ChipLogDetail(DeviceLayer, "DNSsd %s: IPv4: %s, IPv6: %s, ret: %d", __func__, StringOrNullMarker(ipv4),
StringOrNullMarker(ipv6), ret);
g_free(ipv4);
g_free(ipv6);
VerifyOrExit(ret == DNSSD_ERROR_NONE, );
ret = dnssd_service_get_port(service, &port);
VerifyOrExit(ret == DNSSD_ERROR_NONE, ChipLogError(DeviceLayer, "dnssd_service_get_port() failed. ret: %d", ret));
dnssdService.mPort = static_cast<uint16_t>(port);
ret = dnssd_service_get_interface(service, &interface);
VerifyOrExit(ret == DNSSD_ERROR_NONE, ChipLogError(DeviceLayer, "dnssd_service_get_interface() failed. ret: %d", ret));
err = chip::Inet::InterfaceId::InterfaceNameToId(interface, dnssdService.mInterface);
VerifyOrExit(
err == CHIP_NO_ERROR,
ChipLogError(DeviceLayer, "chip::Inet::InterfaceId::InterfaceNameToId() failed. ret: %" CHIP_ERROR_FORMAT, err.Format()));
ret = dnssd_service_get_all_txt_record(service, &txtLen, reinterpret_cast<void **>(&txtRecord));
VerifyOrExit(ret == DNSSD_ERROR_NONE, ChipLogError(DeviceLayer, "dnssd_service_get_all_txt_record() failed. ret: %d", ret));
GetTextEntries(txtLen, txtRecord, textEntries);
dnssdService.mTextEntries = textEntries.empty() ? nullptr : textEntries.data();
dnssdService.mTextEntrySize = textEntries.size();
{ // Lock the stack mutex when calling the callback function, so that the callback
// function could safely perform message exchange (e.g. PASE session pairing).
chip::DeviceLayer::StackLock lock;
rCtx->mCallback(rCtx->mCbContext, &dnssdService, chip::Span<chip::Inet::IPAddress>(&ipAddr, 1), CHIP_NO_ERROR);
}
g_free(txtRecord);
rCtx->mInstance->RemoveContext(rCtx);
return;
exit:
if (err == CHIP_NO_ERROR)
{
rCtx->mCallback(rCtx->mCbContext, nullptr, chip::Span<chip::Inet::IPAddress>(), GetChipError(ret));
}
else
{
rCtx->mCallback(rCtx->mCbContext, nullptr, chip::Span<chip::Inet::IPAddress>(), err);
}
rCtx->mInstance->RemoveContext(rCtx);
}
gboolean ResolveAsync(GMainLoop * mainLoop, gpointer userData)
{
ChipLogDetail(DeviceLayer, "DNSsd %s", __func__);
auto * rCtx = reinterpret_cast<chip::Dnssd::ResolveContext *>(userData);
rCtx->mMainLoop = mainLoop;
int ret = dnssd_resolve_service(rCtx->mServiceHandle, OnResolve, rCtx);
VerifyOrReturnValue(ret == DNSSD_ERROR_NONE, false, ChipLogError(DeviceLayer, "dnssd_resolve_service() failed. ret: %d", ret));
rCtx->mIsResolving = true;
return true;
}
} // namespace
namespace chip {
namespace Dnssd {
DnssdTizen DnssdTizen::sInstance;
void GenericContext::MainLoopQuit()
{
VerifyOrReturn(mMainLoop != nullptr, );
g_main_loop_quit(std::exchange(mMainLoop, nullptr));
}
RegisterContext::RegisterContext(DnssdTizen * instance, const char * type, const DnssdService & service,
DnssdPublishCallback callback, void * context) :
GenericContext(ContextType::Register, instance)
{
Platform::CopyString(mName, service.mName);
Platform::CopyString(mType, type);
mInterfaceId = service.mInterface.GetPlatformInterface();
mPort = service.mPort;
mCallback = callback;
mCbContext = context;
}
RegisterContext::~RegisterContext()
{
if (mIsRegistered)
{
dnssd_deregister_local_service(mServiceHandle);
}
if (mServiceHandle != 0)
{
dnssd_destroy_local_service(mServiceHandle);
}
}
BrowseContext::BrowseContext(DnssdTizen * instance, const char * type, Dnssd::DnssdServiceProtocol protocol, uint32_t interfaceId,
DnssdBrowseCallback callback, void * context) :
GenericContext(ContextType::Browse, instance)
{
Platform::CopyString(mType, type);
mProtocol = protocol;
mInterfaceId = interfaceId;
mCallback = callback;
mCbContext = context;
}
BrowseContext::~BrowseContext()
{
if (mTimeoutSource != nullptr)
{
g_source_destroy(mTimeoutSource);
g_source_unref(mTimeoutSource);
}
}
ResolveContext::ResolveContext(DnssdTizen * instance, const char * name, const char * type, uint32_t interfaceId,
DnssdResolveCallback callback, void * context) :
GenericContext(ContextType::Resolve, instance)
{
Platform::CopyString(mName, name);
Platform::CopyString(mType, type);
mInterfaceId = interfaceId;
mCallback = callback;
mCbContext = context;
}
ResolveContext::~ResolveContext() {}
CHIP_ERROR DnssdTizen::Init(DnssdAsyncReturnCallback initCallback, DnssdAsyncReturnCallback errorCallback, void * context)
{
int ret = dnssd_initialize();
VerifyOrExit(ret == DNSSD_ERROR_NONE || ret == DNSSD_ERROR_INVALID_OPERATION /* Already initialized */, );
initCallback(context, CHIP_NO_ERROR);
return CHIP_NO_ERROR;
exit:
errorCallback(context, CHIP_ERROR_INTERNAL);
return CHIP_ERROR_INTERNAL;
}
void DnssdTizen::Shutdown()
{
int ret = dnssd_deinitialize();
if (ret != DNSSD_ERROR_NONE)
ChipLogError(DeviceLayer, "DNSsd %s: Error: %d", __func__, ret);
}
CHIP_ERROR DnssdTizen::RegisterService(const DnssdService & service, DnssdPublishCallback callback, void * context)
{
std::string fullType = GetFullType(service.mType, service.mProtocol);
auto interfaceId = service.mInterface.GetPlatformInterface();
CHIP_ERROR err = CHIP_NO_ERROR;
bool ok = false;
ChipLogProgress(DeviceLayer, "DNSsd %s: name: %s, type: %s, interfaceId: %u, port: %u", __func__, service.mName,
fullType.c_str(), interfaceId, service.mPort);
{ // If the service was already registered, update it
std::lock_guard<std::mutex> lock(mMutex);
auto iServiceCtx = std::find_if(mContexts.begin(), mContexts.end(), [fullType, service, interfaceId](const auto & ctx) {
VerifyOrReturnValue(ctx->mContextType == ContextType::Register, false);
auto * rCtx = static_cast<RegisterContext *>(ctx.get());
return strcmp(rCtx->mName, service.mName) == 0 && strcmp(rCtx->mType, fullType.c_str()) == 0 &&
rCtx->mPort == service.mPort && rCtx->mInterfaceId == interfaceId;
});
if (iServiceCtx != mContexts.end())
{
ChipLogDetail(DeviceLayer, "DNSsd %s: Updating TXT records", __func__);
auto serviceHandle = static_cast<RegisterContext *>(iServiceCtx->get())->mServiceHandle;
for (size_t i = 0; i < service.mTextEntrySize; ++i)
{
TextEntry entry = service.mTextEntries[i];
VerifyOrReturnError(chip::CanCastTo<unsigned short>(entry.mDataSize), CHIP_ERROR_INVALID_ARGUMENT);
auto dataSize = static_cast<unsigned short>(entry.mDataSize);
int ret = dnssd_service_add_txt_record(serviceHandle, entry.mKey, dataSize, entry.mData);
if (ret != DNSSD_ERROR_NONE)
{
ChipLogError(DeviceLayer, "dnssd_service_add_txt_record() failed. ret: %d", ret);
callback(context, nullptr, nullptr, err = GetChipError(ret));
}
}
return err;
}
}
auto serviceCtx = CreateRegisterContext(fullType.c_str(), service, callback, context);
// Local service will be freed by the RegisterContext destructor
int ret = dnssd_create_local_service(fullType.c_str(), &serviceCtx->mServiceHandle);
auto serviceHandle = serviceCtx->mServiceHandle;
VerifyOrExit(ret == DNSSD_ERROR_NONE, ChipLogError(DeviceLayer, "dnssd_create_local_service() failed. ret: %d", ret);
err = GetChipError(ret));
ret = dnssd_service_set_name(serviceHandle, service.mName);
VerifyOrExit(ret == DNSSD_ERROR_NONE, ChipLogError(DeviceLayer, "dnssd_service_set_name() failed. ret: %d", ret);
err = GetChipError(ret));
ret = dnssd_service_set_port(serviceHandle, service.mPort);
VerifyOrExit(ret == DNSSD_ERROR_NONE, ChipLogError(DeviceLayer, "dnssd_service_set_port() failed. ret: %d", ret);
err = GetChipError(ret));
if (interfaceId > 0)
{
char iface[IF_NAMESIZE + 1] = "";
VerifyOrExit(if_indextoname(interfaceId, iface) != nullptr,
ChipLogError(DeviceLayer, "if_indextoname() failed. errno: %d", errno);
err = CHIP_ERROR_INTERNAL);
ret = dnssd_service_set_interface(serviceHandle, iface);
VerifyOrExit(ret == DNSSD_ERROR_NONE, ChipLogError(DeviceLayer, "dnssd_service_set_interface() failed. ret: %d", ret);
err = GetChipError(ret));
}
for (size_t i = 0; i < service.mTextEntrySize; ++i)
{
TextEntry entry = service.mTextEntries[i];
VerifyOrReturnError(chip::CanCastTo<unsigned short>(entry.mDataSize), CHIP_ERROR_INVALID_ARGUMENT);
ret = dnssd_service_add_txt_record(serviceHandle, entry.mKey, static_cast<unsigned short>(entry.mDataSize), entry.mData);
VerifyOrExit(ret == DNSSD_ERROR_NONE, ChipLogError(DeviceLayer, "dnssd_service_add_txt_record() failed. ret: %d", ret);
err = GetChipError(ret));
}
ok = DeviceLayer::Internal::MainLoop::Instance().AsyncRequest(RegisterAsync, serviceCtx);
VerifyOrExit(ok, err = CHIP_ERROR_INTERNAL);
exit:
if (err != CHIP_NO_ERROR)
{ // Notify caller about error
callback(context, nullptr, nullptr, err);
RemoveContext(serviceCtx);
}
return err;
}
CHIP_ERROR DnssdTizen::UnregisterAllServices()
{
std::lock_guard<std::mutex> lock(mMutex);
unsigned int numServices = 0;
for (auto it = mContexts.begin(); it != mContexts.end(); it++)
{
if ((*it)->mContextType == ContextType::Register)
{
mContexts.erase(it--);
numServices++;
}
}
ChipLogDetail(DeviceLayer, "DNSsd %s: %u", __func__, numServices);
return CHIP_NO_ERROR;
}
CHIP_ERROR DnssdTizen::Browse(const char * type, Dnssd::DnssdServiceProtocol protocol, chip::Inet::IPAddressType addressType,
chip::Inet::InterfaceId interface, DnssdBrowseCallback callback, void * context)
{
std::string fullType = GetFullType(type, protocol);
auto interfaceId = interface.GetPlatformInterface();
CHIP_ERROR err = CHIP_NO_ERROR;
auto browseCtx = CreateBrowseContext(fullType.c_str(), protocol, interfaceId, callback, context);
bool ok = DeviceLayer::Internal::MainLoop::Instance().AsyncRequest(BrowseAsync, browseCtx);
VerifyOrExit(ok, err = CHIP_ERROR_INTERNAL);
exit:
if (err != CHIP_NO_ERROR)
{ // Notify caller about error
callback(context, nullptr, 0, true, err);
RemoveContext(browseCtx);
}
return err;
}
CHIP_ERROR DnssdTizen::Resolve(const DnssdService & browseResult, chip::Inet::InterfaceId interface, DnssdResolveCallback callback,
void * context)
{
std::string fullType = GetFullType(browseResult.mType, browseResult.mProtocol);
auto interfaceId = interface.GetPlatformInterface();
CHIP_ERROR err = CHIP_NO_ERROR;
bool ok = false;
int ret;
ChipLogDetail(DeviceLayer, "DNSsd %s: name: %s, type: %s, interfaceId: %u", __func__, browseResult.mName, fullType.c_str(),
interfaceId);
auto resolveCtx = CreateResolveContext(browseResult.mName, fullType.c_str(), interfaceId, callback, context);
if (interfaceId == 0)
{
ret = dnssd_create_remote_service(fullType.c_str(), browseResult.mName, nullptr, &resolveCtx->mServiceHandle);
}
else
{
char iface[IF_NAMESIZE + 1] = "";
VerifyOrExit(if_indextoname(interfaceId, iface) != nullptr,
ChipLogError(DeviceLayer, "if_indextoname() failed. errno: %d", errno);
err = CHIP_ERROR_INTERNAL);
ret = dnssd_create_remote_service(fullType.c_str(), browseResult.mName, iface, &resolveCtx->mServiceHandle);
}
VerifyOrExit(ret == DNSSD_ERROR_NONE, ChipLogError(DeviceLayer, "dnssd_create_remote_service() failed. ret: %d", ret);
err = GetChipError(ret));
ok = DeviceLayer::Internal::MainLoop::Instance().AsyncRequest(ResolveAsync, resolveCtx);
VerifyOrExit(ok, err = CHIP_ERROR_INTERNAL);
exit:
if (err != CHIP_NO_ERROR)
RemoveContext(resolveCtx);
return err;
}
RegisterContext * DnssdTizen::CreateRegisterContext(const char * type, const DnssdService & service, DnssdPublishCallback callback,
void * context)
{
auto ctx = std::make_unique<RegisterContext>(this, type, service, callback, context);
auto ctxPtr = ctx.get();
std::lock_guard<std::mutex> lock(mMutex);
mContexts.emplace(std::move(ctx));
return ctxPtr;
}
BrowseContext * DnssdTizen::CreateBrowseContext(const char * type, Dnssd::DnssdServiceProtocol protocol, uint32_t interfaceId,
DnssdBrowseCallback callback, void * context)
{
auto ctx = std::make_unique<BrowseContext>(this, type, protocol, interfaceId, callback, context);
auto ctxPtr = ctx.get();
std::lock_guard<std::mutex> lock(mMutex);
mContexts.emplace(std::move(ctx));
return ctxPtr;
}
ResolveContext * DnssdTizen::CreateResolveContext(const char * name, const char * type, uint32_t interfaceId,
DnssdResolveCallback callback, void * context)
{
auto ctx = std::make_unique<ResolveContext>(this, name, type, interfaceId, callback, context);
auto ctxPtr = ctx.get();
std::lock_guard<std::mutex> lock(mMutex);
mContexts.emplace(std::move(ctx));
return ctxPtr;
}
CHIP_ERROR DnssdTizen::RemoveContext(GenericContext * context)
{
std::lock_guard<std::mutex> lock(mMutex);
mContexts.erase(std::find_if(mContexts.begin(), mContexts.end(), [context](const auto & ctx) { return ctx.get() == context; }));
return CHIP_NO_ERROR;
}
CHIP_ERROR ChipDnssdInit(DnssdAsyncReturnCallback initCallback, DnssdAsyncReturnCallback errorCallback, void * context)
{
VerifyOrReturnError(initCallback != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(errorCallback != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
return DnssdTizen::GetInstance().Init(initCallback, errorCallback, context);
}
void ChipDnssdShutdown()
{
DnssdTizen::GetInstance().Shutdown();
}
CHIP_ERROR ChipDnssdPublishService(const DnssdService * service, DnssdPublishCallback callback, void * context)
{
VerifyOrReturnError(service != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(IsSupportedProtocol(service->mProtocol), CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(callback != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
if (DeviceLayer::ThreadStackMgr().IsThreadEnabled())
{
std::string regtype = GetFullType(service->mType, service->mProtocol);
Span<const char * const> subTypes(service->mSubTypes, service->mSubTypeSize);
Span<const TextEntry> textEntries(service->mTextEntries, service->mTextEntrySize);
CHIP_ERROR err;
if (strcmp(service->mHostName, "") != 0)
{
err = DeviceLayer::ThreadStackMgr().SetupSrpHost(service->mHostName);
VerifyOrReturnError(err == CHIP_NO_ERROR, err);
}
err = DeviceLayer::ThreadStackMgr().AddSrpService(service->mName, regtype.c_str(), service->mPort, subTypes, textEntries);
VerifyOrReturnError(err == CHIP_NO_ERROR, err);
return CHIP_NO_ERROR;
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
return DnssdTizen::GetInstance().RegisterService(*service, callback, context);
}
CHIP_ERROR ChipDnssdRemoveServices()
{
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
if (DeviceLayer::ThreadStackMgr().IsThreadEnabled())
{
DeviceLayer::ThreadStackMgr().InvalidateAllSrpServices();
return DeviceLayer::ThreadStackMgr().RemoveInvalidSrpServices();
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
return DnssdTizen::GetInstance().UnregisterAllServices();
}
CHIP_ERROR ChipDnssdFinalizeServiceUpdate()
{
return CHIP_NO_ERROR;
}
CHIP_ERROR ChipDnssdBrowse(const char * type, Dnssd::DnssdServiceProtocol protocol, chip::Inet::IPAddressType addressType,
chip::Inet::InterfaceId interface, DnssdBrowseCallback callback, void * context,
intptr_t * browseIdentifier)
{
VerifyOrReturnError(type != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(IsSupportedProtocol(protocol), CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(callback != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
*browseIdentifier = reinterpret_cast<intptr_t>(nullptr);
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT && CHIP_DEVICE_CONFIG_ENABLE_THREAD_DNS_CLIENT
if (DeviceLayer::ThreadStackMgr().IsThreadEnabled())
{
std::string fullType = GetFullType(type, protocol);
return DeviceLayer::ThreadStackMgr().DnsBrowse(fullType.c_str(), callback, context);
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT && CHIP_DEVICE_CONFIG_ENABLE_THREAD_DNS_CLIENT
return DnssdTizen::GetInstance().Browse(type, protocol, addressType, interface, callback, context);
}
CHIP_ERROR ChipDnssdStopBrowse(intptr_t browseIdentifier)
{
return CHIP_ERROR_NOT_IMPLEMENTED;
}
CHIP_ERROR ChipDnssdResolve(DnssdService * browseResult, chip::Inet::InterfaceId interface, DnssdResolveCallback callback,
void * context)
{
VerifyOrReturnError(browseResult != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(IsSupportedProtocol(browseResult->mProtocol), CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(callback != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT && CHIP_DEVICE_CONFIG_ENABLE_THREAD_DNS_CLIENT
if (DeviceLayer::ThreadStackMgr().IsThreadEnabled())
{
std::string fullType = GetFullType(browseResult->mType, browseResult->mProtocol);
return DeviceLayer::ThreadStackMgr().DnsResolve(fullType.c_str(), browseResult->mName, callback, context);
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT && CHIP_DEVICE_CONFIG_ENABLE_THREAD_DNS_CLIENT
return DnssdTizen::GetInstance().Resolve(*browseResult, interface, callback, context);
}
void ChipDnssdResolveNoLongerNeeded(const char * instanceName) {}
CHIP_ERROR ChipDnssdReconfirmRecord(const char * hostname, chip::Inet::IPAddress address, chip::Inet::InterfaceId interface)
{
return CHIP_ERROR_NOT_IMPLEMENTED;
}
} // namespace Dnssd
} // namespace chip