Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matter.framework should explicitly look for things in the SRP domain. #33319

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 36 additions & 22 deletions src/darwin/Framework/CHIP/MTRDeviceConnectivityMonitor.mm
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,23 @@
#import <os/lock.h>

#include <lib/dnssd/ServiceNaming.h>
#include <lib/support/CodeUtils.h>
#include <vector>

@implementation MTRDeviceConnectivityMonitor {
NSString * _instanceName;
DNSServiceRef _resolver;
std::vector<DNSServiceRef> _resolvers;
NSMutableDictionary<NSString *, nw_connection_t> * _connections;

MTRDeviceConnectivityMonitorHandler _monitorHandler;
dispatch_queue_t _handlerQueue;
}

namespace {
constexpr char kLocalDot[] = "local.";
constexpr const char * kResolveDomains[] = {
"default.service.arpa.", // SRP
"local.",
};
constexpr char kOperationalType[] = "_matter._tcp";
constexpr int64_t kSharedConnectionLingerIntervalSeconds = (10);
}
Expand Down Expand Up @@ -68,8 +73,8 @@ - (instancetype)initWithCompressedFabricID:(NSNumber *)compressedFabricID nodeID

- (void)dealloc
{
if (_resolver) {
DNSServiceRefDeallocate(_resolver);
for (auto & resolver : _resolvers) {
DNSServiceRefDeallocate(resolver);
}
}

Expand Down Expand Up @@ -188,32 +193,39 @@ - (void)startMonitoringWithHandler:(MTRDeviceConnectivityMonitorHandler)handler
_handlerQueue = queue;

// If there's already a resolver running, just return
if (_resolver) {
if (_resolvers.size() != 0) {
MTR_LOG_INFO("%@ connectivity monitor already running", self);
return;
}

MTR_LOG_INFO("%@ start connectivity monitoring for %@ (%lu monitoring objects)", self, _instanceName, static_cast<unsigned long>(sConnectivityMonitorCount));

_resolver = [MTRDeviceConnectivityMonitor _sharedResolverConnection];
if (!_resolver) {
auto sharedConnection = [MTRDeviceConnectivityMonitor _sharedResolverConnection];
if (!sharedConnection) {
MTR_LOG_ERROR("%@ failed to get shared resolver connection", self);
return;
}
DNSServiceErrorType dnsError = DNSServiceResolve(&_resolver,
kDNSServiceFlagsShareConnection,
kDNSServiceInterfaceIndexAny,
_instanceName.UTF8String,
kOperationalType,
kLocalDot,
ResolveCallback,
(__bridge void *) self);
if (dnsError != kDNSServiceErr_NoError) {
MTR_LOG_ERROR("%@ failed to create resolver", self);
return;

for (auto domain : kResolveDomains) {
DNSServiceRef resolver = sharedConnection;
DNSServiceErrorType dnsError = DNSServiceResolve(&resolver,
kDNSServiceFlagsShareConnection,
kDNSServiceInterfaceIndexAny,
_instanceName.UTF8String,
kOperationalType,
domain,
ResolveCallback,
(__bridge void *) self);
if (dnsError == kDNSServiceErr_NoError) {
_resolvers.emplace_back(std::move(resolver));
} else {
MTR_LOG_ERROR("%@ failed to create resolver for \"%s\" domain: %" PRId32, self, StringOrNullMarker(domain), dnsError);
}
}

sConnectivityMonitorCount++;
if (_resolvers.size() != 0) {
sConnectivityMonitorCount++;
}
}

- (void)_stopMonitoring
Expand All @@ -227,9 +239,11 @@ - (void)_stopMonitoring
_monitorHandler = nil;
_handlerQueue = nil;

if (_resolver) {
DNSServiceRefDeallocate(_resolver);
_resolver = NULL;
if (_resolvers.size() != 0) {
for (auto & resolver : _resolvers) {
DNSServiceRefDeallocate(resolver);
}
_resolvers.clear();

// If no monitor objects exist, schedule to deallocate shared connection and queue
sConnectivityMonitorCount--;
Expand Down
24 changes: 17 additions & 7 deletions src/darwin/Framework/CHIP/MTROperationalBrowser.mm
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@

#include <cinttypes>
#include <lib/dnssd/ServiceNaming.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/LockTracker.h>

namespace {
constexpr char kLocalDot[] = "local.";
constexpr const char * kBrowseDomains[] = {
"default.service.arpa.", // SRP
"local.",
};
constexpr char kOperationalType[] = "_matter._tcp";
constexpr DNSServiceFlags kBrowseFlags = 0;
}

MTROperationalBrowser::MTROperationalBrowser(MTRDeviceControllerFactory * aFactory, dispatch_queue_t aQueue)
Expand All @@ -43,23 +46,30 @@
{
assertChipStackLockedByCurrentThread();

ChipLogProgress(Controller, "Trying to start operational browse");
ChipLogProgress(Controller, "Trying to start persistent operational browse");

auto err
= DNSServiceBrowse(&mBrowseRef, kBrowseFlags, kDNSServiceInterfaceIndexAny, kOperationalType, kLocalDot, OnBrowse, this);
auto err = DNSServiceCreateConnection(&mBrowseRef);
if (err != kDNSServiceErr_NoError) {
ChipLogError(Controller, "Failed to start operational browse: %" PRId32, err);
ChipLogError(Controller, "Failed to create connection for persistent operational browse: %" PRId32, err);
return;
}

err = DNSServiceSetDispatchQueue(mBrowseRef, mQueue);
if (err != kDNSServiceErr_NoError) {
ChipLogError(Controller, "Failed to set up dispatch queue properly");
ChipLogError(Controller, "Failed to set up dispatch queue properly for persistent operational browse: %" PRId32, err);
DNSServiceRefDeallocate(mBrowseRef);
return;
}

mInitialized = true;

for (auto domain : kBrowseDomains) {
auto browseRef = mBrowseRef; // Mandatory copy because of kDNSServiceFlagsShareConnection.
jtung-apple marked this conversation as resolved.
Show resolved Hide resolved
err = DNSServiceBrowse(&browseRef, kDNSServiceFlagsShareConnection, kDNSServiceInterfaceIndexAny, kOperationalType, domain, OnBrowse, this);
if (err != kDNSServiceErr_NoError) {
ChipLogError(Controller, "Failed to start persistent operational browse for \"%s\" domain: %" PRId32, StringOrNullMarker(domain), err);
}
}
}

void MTROperationalBrowser::OnBrowse(DNSServiceRef aServiceRef, DNSServiceFlags aFlags, uint32_t aInterfaceId,
Expand Down
Loading