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

[dnssd] implement otPlatDnssd APIs #2042

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions etc/cmake/options.cmake
Original file line number Diff line number Diff line change
@@ -98,6 +98,12 @@ if (OTBR_DNSSD_DISCOVERY_PROXY)
target_compile_definitions(otbr-config INTERFACE OTBR_ENABLE_DNSSD_DISCOVERY_PROXY=1)
endif()

# When enabled the Advertising Proxy and Discovery proxy will be supported by OpenThread core
option(OTBR_DNSSD_PLAT "Enable DNS-SD plat APIs for Advertising proxy and Discovery proxy in core" OFF)
if (OTBR_DNSSD_PLAT)
target_compile_definitions(otbr-config INTERFACE OTBR_ENABLE_DNSSD_PLAT=1)
endif()

option(OTBR_UNSECURE_JOIN "Enable unsecure joining" OFF)
if(OTBR_UNSECURE_JOIN)
target_compile_definitions(otbr-config INTERFACE OTBR_ENABLE_UNSECURE_JOIN=1)
1 change: 1 addition & 0 deletions script/_otbr
Original file line number Diff line number Diff line change
@@ -73,6 +73,7 @@ otbr_install()
"-DOTBR_DBUS=ON"
"-DOTBR_DNSSD_DISCOVERY_PROXY=ON"
"-DOTBR_SRP_ADVERTISING_PROXY=ON"
"-DOTBR_DNSSD_PLAT=OFF"
"-DOTBR_INFRA_IF_NAME=${INFRA_IF_NAME}"
"-DOTBR_MDNS=${OTBR_MDNS:=mDNSResponder}"
# Force re-evaluation of version strings
12 changes: 12 additions & 0 deletions src/agent/application.cpp
Original file line number Diff line number Diff line change
@@ -70,6 +70,9 @@ Application::Application(const std::string &aInterfaceName,
#if OTBR_ENABLE_BACKBONE_ROUTER
, mBackboneAgent(mNcp, aInterfaceName, mBackboneInterfaceName)
#endif
#if OTBR_ENABLE_DNSSD_PLAT
, mDnssdPlatform(mNcp, *mPublisher)
#endif
#if OTBR_ENABLE_SRP_ADVERTISING_PROXY
, mAdvertisingProxy(mNcp, *mPublisher)
#endif
@@ -109,6 +112,9 @@ void Application::Init(void)
#if OTBR_ENABLE_BACKBONE_ROUTER
mBackboneAgent.Init();
#endif
#if OTBR_ENABLE_DNSSD_PLAT
mDnssdPlatform.Start();
#endif
#if OTBR_ENABLE_SRP_ADVERTISING_PROXY
mAdvertisingProxy.SetEnabled(true);
#endif
@@ -131,6 +137,9 @@ void Application::Init(void)

void Application::Deinit(void)
{
#if OTBR_ENABLE_DNSSD_PLAT
mDnssdPlatform.Stop();
#endif
#if OTBR_ENABLE_SRP_ADVERTISING_PROXY
mAdvertisingProxy.SetEnabled(false);
#endif
@@ -226,6 +235,9 @@ void Application::HandleMdnsState(Mdns::Publisher::State aState)
{
OTBR_UNUSED_VARIABLE(aState);

#if OTBR_ENABLE_DNSSD_PLAT
mDnssdPlatform.HandleMdnsPublisherStateChange(aState);
#endif
#if OTBR_ENABLE_BORDER_AGENT
mBorderAgent.HandleMdnsState(aState);
#endif
3 changes: 3 additions & 0 deletions src/agent/application.hpp
Original file line number Diff line number Diff line change
@@ -271,6 +271,9 @@ class Application : private NonCopyable
#if OTBR_ENABLE_BACKBONE_ROUTER
BackboneRouter::BackboneAgent mBackboneAgent;
#endif
#if OTBR_ENABLE_DNSSD_PLAT
DnssdPlatform mDnssdPlatform;
#endif
#if OTBR_ENABLE_SRP_ADVERTISING_PROXY
AdvertisingProxy mAdvertisingProxy;
#endif
1 change: 1 addition & 0 deletions src/border_agent/border_agent.hpp
Original file line number Diff line number Diff line change
@@ -47,6 +47,7 @@
#include "backbone_router/backbone_agent.hpp"
#include "common/code_utils.hpp"
#include "common/mainloop.hpp"
#include "mdns/dnssd_plat.hpp"
#include "mdns/mdns.hpp"
#include "ncp/ncp_openthread.hpp"
#include "sdp_proxy/advertising_proxy.hpp"
2 changes: 2 additions & 0 deletions src/mdns/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -30,6 +30,7 @@ if(OTBR_MDNS STREQUAL "avahi")
add_library(otbr-mdns
mdns.cpp
mdns_avahi.cpp
dnssd_plat.cpp
)
target_compile_definitions(otbr-mdns PUBLIC
OTBR_ENABLE_MDNS_AVAHI=1
@@ -48,6 +49,7 @@ if(OTBR_MDNS STREQUAL "mDNSResponder")
add_library(otbr-mdns
mdns.cpp
mdns_mdnssd.cpp
dnssd_plat.cpp
)
target_compile_definitions(otbr-mdns PUBLIC
OTBR_ENABLE_MDNS_MDNSSD=1
597 changes: 597 additions & 0 deletions src/mdns/dnssd_plat.cpp

Large diffs are not rendered by default.

212 changes: 212 additions & 0 deletions src/mdns/dnssd_plat.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
/*
* Copyright (c) 2023, The OpenThread Authors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

/**
* @file
* This file includes definitions for implementing OpenThread DNS-SD platform APIs.
*/

#ifndef OTBR_AGENT_DNSSD_PLAT_HPP_
#define OTBR_AGENT_DNSSD_PLAT_HPP_

#include "openthread-br/config.h"

#include <algorithm>
#include <functional>
#include <map>

#include <openthread/instance.h>
#include <openthread/platform/dnssd.h>

#include "common/dns_utils.hpp"
#include "mdns/mdns.hpp"
#include "ncp/ncp_openthread.hpp"
#include "utils/string_utils.hpp"

namespace otbr {

/**
* This class implements the DNS-SD platform.
*
*/
class DnssdPlatform : private NonCopyable
{
public:
/**
* Initializes the `DnssdPlatform` instance
*
* @param[in] aNcp The OT controller.
* @param[in] aPublisher A reference to `Mdns::Publisher` to use.
*
*/
DnssdPlatform(Ncp::ControllerOpenThread &aNcp, Mdns::Publisher &aPublisher);

/**
* Starts the `DnssdPlatform` module.
*
*/
void Start(void);

/**
* Stops the `DnssdPlatform` module
*
*/
void Stop(void);

/**
* Gets the singleton `DnssdPlatform` instance.
*
* @retval A reference to the `DnssdPlatform` instance.
*
*/
static DnssdPlatform &Get(void) { return *sDnssdPlatform; }

//-----------------------------------------------------------------------------------------------------------------
// `otPlatDnssd` APIs (see `openthread/include/openthread/platform/dnssd.h` for detailed documentation).

typedef otPlatDnssdState State;
typedef otPlatDnssdServiceInstance ServiceInstance;
typedef otPlatDnssdService Service;
typedef otPlatDnssdHost Host;
typedef otPlatDnssdKey Key;
typedef otPlatDnssdRequestId RequestId;
typedef otPlatDnssdRegisterCallback RegisterCallback;
typedef otPlatDnssdEvent Event;

State GetState(void) const { return mState; }
void RegisterService(const Service &aService, RequestId aRequestId, RegisterCallback aCallback);
void UnregisterService(const Service &aService, RequestId aRequestId, RegisterCallback aCallback);
void RegisterHost(const Host &aHost, RequestId aRequestId, RegisterCallback aCallback);
void UnregisterHost(const Host &aHost, RequestId aRequestId, RegisterCallback aCallback);
void RegisterKey(const Key &aKey, RequestId aRequestId, RegisterCallback aCallback);
void UnregisterKey(const Key &aKey, RequestId aRequestId, RegisterCallback aCallback);
void StartServiceBrowser(const char *aServiceType, uint32_t aInfraIfIndex);
void StopServiceBrowser(const char *aServiceType, uint32_t aInfraIfIndex);
void StartServiceResolver(const ServiceInstance &aInfo);
void StopServiceResolver(const ServiceInstance &aInfo);
void StartIp6AddressResolver(const char *aHostName, uint32_t aInfraIfIndex);
void StopIp6AddressResolver(const char *aHostName, uint32_t aInfraIfIndex);

// Callback from BR agent
void HandleMdnsPublisherStateChange(Mdns::Publisher::State aState);

private:
static constexpr State kStateReady = OT_PLAT_DNSSD_READY;
static constexpr State kStateStopped = OT_PLAT_DNSSD_STOPPED;
static constexpr Event kEventEntryAdded = OT_PLAT_DNSSD_EVENT_ENTRY_ADDED;
static constexpr Event kEventEntryRemoved = OT_PLAT_DNSSD_EVENT_ENTRY_REMOVED;
static constexpr uint32_t kAnyNetifIndex = 0;

class DnsName
{
public:
DnsName(std::string aName)
: mName(aName)
{
}

bool operator==(const DnsName &aOther) const
{
return StringUtils::ToLowercase(mName) == StringUtils::ToLowercase(aOther.mName);
}

bool operator<(const DnsName &aOther) const
{
return StringUtils::ToLowercase(mName) < StringUtils::ToLowercase(aOther.mName);
}

private:
std::string mName;
};

class DnsServiceName
{
public:
DnsServiceName(std::string aInstance, std::string aType)
: mInstance(aInstance)
, mType(aType)
{
}

bool operator==(const DnsServiceName &aOther) const
{
return (mInstance == aOther.mInstance) && (mType == aOther.mType);
}

bool operator<(const DnsServiceName &aOther) const
{
return (mInstance == aOther.mInstance) ? (mType < aOther.mType) : (mInstance < aOther.mInstance);
}

private:
DnsName mInstance;
DnsName mType;
};

class NetifIndexList
{
public:
bool Matches(uint32_t aInterfaceIndex) const;
bool IsEmpty(void) const { return mList.empty(); }
bool Contains(uint32_t aInterfaceIndex) const;
void Add(uint32_t aInterfaceIndex);
void Remove(uint32_t aInterfaceIndex);

private:
std::vector<uint32_t> mList;
};

void UpdateState(void);
Mdns::Publisher::ResultCallback MakePublisherCallback(RequestId aRequestId, RegisterCallback aCallback);

static otError ResultToError(otbrError aOtbrError);
static std::string KeyNameFor(const Key &aKey);

static void HandleDiscoveredService(const std::string &aType, const Mdns::Publisher::DiscoveredInstanceInfo &aInfo);
static void HandleDiscoveredHost(const std::string &aHostName, const Mdns::Publisher::DiscoveredHostInfo &aInfo);

void ProcessServiceBrowsers(const std::string &aType, const Mdns::Publisher::DiscoveredInstanceInfo &aInfo);
void ProcessServiceResolvers(const std::string &aType, const Mdns::Publisher::DiscoveredInstanceInfo &aInfo);
void ProcessIp6AddrResolvers(const std::string &aHostName, const Mdns::Publisher::DiscoveredHostInfo &aInfo);

static DnssdPlatform *sDnssdPlatform;

Ncp::ControllerOpenThread &mNcp;
Mdns::Publisher &mPublisher;
State mState;
bool mRunning;
Mdns::Publisher::State mPublisherState;
uint64_t mSubscriberId;
std::map<DnsName, NetifIndexList> mServiceBrowsers;
std::map<DnsServiceName, NetifIndexList> mServiceResolvers;
std::map<DnsName, NetifIndexList> mIp6AddrResolvers;
};

} // namespace otbr

#endif // OTBR_AGENT_DNSSD_PLAT_HPP_
13 changes: 11 additions & 2 deletions third_party/openthread/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -46,7 +46,6 @@ set(OT_DAEMON ON CACHE STRING "enable daemon mode" FORCE)
set(OT_DATASET_UPDATER ON CACHE STRING "enable dataset updater" FORCE)
set(OT_DNS_CLIENT ON CACHE STRING "enable DNS client" FORCE)
set(OT_DNS_UPSTREAM_QUERY ${OTBR_DNS_UPSTREAM_QUERY} CACHE STRING "enable sending DNS queries to upstream" FORCE)
set(OT_DNSSD_SERVER ${OTBR_DNSSD_DISCOVERY_PROXY} CACHE STRING "enable DNS-SD server support" FORCE)
set(OT_ECDSA ON CACHE STRING "enable ECDSA" FORCE)
set(OT_FIREWALL ON CACHE STRING "enable firewall feature")
set(OT_HISTORY_TRACKER ON CACHE STRING "enable history tracker" FORCE)
@@ -71,11 +70,21 @@ set(OT_TREL ${OTBR_TREL} CACHE STRING "enable TREL" FORCE)
set(OT_UDP_FORWARD OFF CACHE STRING "disable udp forward" FORCE)
set(OT_UPTIME ON CACHE STRING "enable uptime" FORCE)

if (OTBR_SRP_ADVERTISING_PROXY)
if (OTBR_DNSSD_PLAT)
set(OT_SRP_SERVER ON CACHE STRING "enable SRP server" FORCE)
set(OT_EXTERNAL_HEAP ON CACHE STRING "enable external heap" FORCE)
set(OT_SRP_ADV_PROXY ON CACHE STRING "enable SRP adv proxy" FORCE)
set(OT_DNSSD_SERVER ON CACHE STRING "enable DNS-SD server support" FORCE)
# TODO: Add for Discovery proxy later
else ()
if (OTBR_SRP_ADVERTISING_PROXY)
set(OT_SRP_SERVER ON CACHE STRING "enable SRP server" FORCE)
set(OT_EXTERNAL_HEAP ON CACHE STRING "enable external heap" FORCE)
endif()
set(OT_DNSSD_SERVER ${OTBR_DNSSD_DISCOVERY_PROXY} CACHE STRING "enable DNS-SD server support" FORCE)
endif()


if (NOT OT_THREAD_VERSION STREQUAL "1.1")
if (OT_REFERENCE_DEVICE)
set(OT_DUA ON CACHE STRING "Enable Thread 1.2 DUA for reference devices")