[dnssd-server] simplifications and enhancements (#9334)

This commit contains changes and enhancements in the DNS-SD
server/resolver class `Dns::ServiceDiscovery::Server`.

- It defines `Request` and `Response` structures, which contain all
  related information for a DNS query request and response. These
  structures simplify the code by encapsulating all the related
  information in one place.
- The `Response` class provides methods for preparing the response,
  such as appending records, DNS names to the response, or checking
  the questions or updating the header. These methods replace the
  previous `static` methods, which required all the information to be
  passed as input parameters.
- The `QueryTransacation` type is simplified by declaring it as a
  subclass of `Response`. It inherits all the helper methods from
  `Response`. Other previously defined `static` methods are now
  defined as methods of `QueryTransacation` (such as `CanAnswer()`).
- `ResolveBySrp()` and its related methods now directly populate the
  DNS response code in the `Response` DNS header instead of returning
  it.
- `ResolveBySrp()` is updated such that if a failure is encountered
  when preparing the answer section, no further processing is
  performed. This ensures that a previous failure `rcode` is not
  overwritten. When preparing the additional section, certain
  `rcode` failures are allowed, such as if the DNS name is not found.
- The processing of `Timer` is simplified, using `FiretAtIfEarlier()`
  and determining next expire time from `HandleTimer()`.
- This commit also adds an empty implementation of the `otPlatDns{}`
  functions (used with `OPENTHREAD_CONFIG_DNS_UPSTREAM_QUERY_ENABLE`)
  under the simulation platform.
This commit is contained in:
Abtin Keshavarzian
2023-08-03 09:33:31 -07:00
committed by GitHub
parent 992be2781f
commit 037056b97e
4 changed files with 515 additions and 598 deletions
@@ -61,6 +61,7 @@ add_library(openthread-simulation
alarm.c
crypto.c
diag.c
dns.c
dso_transport.c
entropy.c
flash.c
+47
View File
@@ -0,0 +1,47 @@
/*
* 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.
*/
#include "platform-simulation.h"
#include <openthread/platform/dns.h>
#if OPENTHREAD_CONFIG_DNS_UPSTREAM_QUERY_ENABLE
void otPlatDnsStartUpstreamQuery(otInstance *aInstance, otPlatDnsUpstreamQuery *aTxn, const otMessage *aQuery)
{
OT_UNUSED_VARIABLE(aInstance);
OT_UNUSED_VARIABLE(aTxn);
OT_UNUSED_VARIABLE(aQuery);
}
void otPlatDnsCancelUpstreamQuery(otInstance *aInstance, otPlatDnsUpstreamQuery *aTxn)
{
otPlatDnsUpstreamQueryDone(aInstance, aTxn, NULL);
}
#endif
File diff suppressed because it is too large Load Diff
+85 -142
View File
@@ -36,6 +36,7 @@
#include <openthread/dnssd_server.h>
#include "common/as_core_type.hpp"
#include "common/callback.hpp"
#include "common/message.hpp"
#include "common/non_copyable.hpp"
#include "common/timer.hpp"
@@ -147,6 +148,12 @@ public:
kDnsQueryResolveHost = OT_DNSSD_QUERY_TYPE_RESOLVE_HOST, ///< Service type resolve hostname.
};
typedef otDnssdServiceInstanceInfo ServiceInstanceInfo; ///< A discovered service instance for a DNS-SD query.
typedef otDnssdHostInfo HostInfo; ///< A discover host for a DNS-SD query.
typedef otDnssdQuerySubscribeCallback SubscribeCallback;
typedef otDnssdQueryUnsubscribeCallback UnsubscribeCallback;
static constexpr uint16_t kPort = OPENTHREAD_CONFIG_DNSSD_SERVER_PORT; ///< The DNS-SD server port.
/**
@@ -180,9 +187,7 @@ public:
* @param[in] aContext A pointer to the application-specific context.
*
*/
void SetQueryCallbacks(otDnssdQuerySubscribeCallback aSubscribe,
otDnssdQueryUnsubscribeCallback aUnsubscribe,
void *aContext);
void SetQueryCallbacks(SubscribeCallback aSubscribe, UnsubscribeCallback aUnsubscribe, void *aContext);
/**
* Notifies a discovered service instance.
@@ -191,7 +196,7 @@ public:
* @param[in] aInstanceInfo A reference to the discovered service instance information.
*
*/
void HandleDiscoveredServiceInstance(const char *aServiceFullName, const otDnssdServiceInstanceInfo &aInstanceInfo);
void HandleDiscoveredServiceInstance(const char *aServiceFullName, const ServiceInstanceInfo &aInstanceInfo);
#if OPENTHREAD_CONFIG_DNS_UPSTREAM_QUERY_ENABLE
/**
@@ -231,7 +236,7 @@ public:
* @param[in] aHostInfo A reference to the discovered host information.
*
*/
void HandleDiscoveredHost(const char *aHostFullName, const otDnssdHostInfo &aHostInfo);
void HandleDiscoveredHost(const char *aHostFullName, const HostInfo &aHostInfo);
/**
* Acquires the next query in the server.
@@ -289,25 +294,14 @@ private:
class NameCompressInfo : public Clearable<NameCompressInfo>
{
public:
explicit NameCompressInfo(void) = default;
explicit NameCompressInfo(const char *aDomainName)
: mDomainName(aDomainName)
, mDomainNameOffset(kUnknownOffset)
, mServiceNameOffset(kUnknownOffset)
, mInstanceNameOffset(kUnknownOffset)
, mHostNameOffset(kUnknownOffset)
{
}
static constexpr uint16_t kUnknownOffset = 0; // Unknown offset value (used when offset is not yet set).
NameCompressInfo(void) { Clear(); }
uint16_t GetDomainNameOffset(void) const { return mDomainNameOffset; }
void SetDomainNameOffset(uint16_t aOffset) { mDomainNameOffset = aOffset; }
const char *GetDomainName(void) const { return mDomainName; }
uint16_t GetServiceNameOffset(const Message &aMessage, const char *aServiceName) const
{
return MatchCompressedName(aMessage, mServiceNameOffset, aServiceName)
@@ -357,11 +351,10 @@ private:
return aOffset != kUnknownOffset && Name::CompareName(aMessage, aOffset, aName) == kErrorNone;
}
const char *mDomainName; // The serialized domain name.
uint16_t mDomainNameOffset; // Offset of domain name serialization into the response message.
uint16_t mServiceNameOffset; // Offset of service name serialization into the response message.
uint16_t mInstanceNameOffset; // Offset of instance name serialization into the response message.
uint16_t mHostNameOffset; // Offset of host name serialization into the response message.
uint16_t mDomainNameOffset; // Offset of domain name serialization into the response message.
uint16_t mServiceNameOffset; // Offset of service name serialization into the response message.
uint16_t mInstanceNameOffset; // Offset of instance name serialization into the response message.
uint16_t mHostNameOffset; // Offset of host name serialization into the response message.
};
static constexpr bool kBindUnspecifiedNetif = OPENTHREAD_CONFIG_DNSSD_SERVER_BIND_UNSPECIFIED_NETIF;
@@ -390,140 +383,91 @@ private:
bool IsHostName(void) const { return mProtocolOffset == kNotPresent && mDomainOffset != 0; }
uint8_t mDomainOffset; // The offset to the beginning of <Domain>.
uint8_t mProtocolOffset; // The offset to the beginning of <Protocol> (i.e. _tcp or _udp) or `kNotPresent` if
// the name is not a service or instance.
uint8_t mServiceOffset; // The offset to the beginning of <Service> or `kNotPresent` if the name is not a
// service or instance.
uint8_t mSubTypeOffset; // The offset to the beginning of sub-type label or `kNotPresent` is not a sub-type.
uint8_t mInstanceOffset; // The offset to the beginning of <Instance> or `kNotPresent` if the name is not a
// instance.
uint8_t mDomainOffset; // Offset to <Domain>.
uint8_t mProtocolOffset; // Offset to <Protocol> (i.e. _tcp or _udp) or `kNotPresent` if not service name.
uint8_t mServiceOffset; // Offset to <Service> or `kNotPresent` if not service or instance.
uint8_t mSubTypeOffset; // Offset to sub-type label or `kNotPresent` is not a sub-type.
uint8_t mInstanceOffset; // Offset to <Instance> or `kNotPresent` if the name is not a instance.
};
/**
* Contains the compress information for a dns packet.
*
*/
class QueryTransaction : public InstanceLocatorInit
struct Request
{
public:
explicit QueryTransaction(void)
: mResponseMessage(nullptr)
const Message *mMessage;
const Ip6::MessageInfo *mMessageInfo;
Header mHeader;
};
struct Response : public GetProvider<Response>
{
Response(void)
: mMessage(nullptr)
, mAdditional(false)
{
}
void Init(const Header &aResponseHeader,
Message &aResponseMessage,
const NameCompressInfo &aCompressInfo,
const Ip6::MessageInfo &aMessageInfo,
Instance &aInstance);
bool IsValid(void) const { return mResponseMessage != nullptr; }
const Ip6::MessageInfo &GetMessageInfo(void) const { return mMessageInfo; }
const Header &GetResponseHeader(void) const { return mResponseHeader; }
Header &GetResponseHeader(void) { return mResponseHeader; }
const Message &GetResponseMessage(void) const { return *mResponseMessage; }
Message &GetResponseMessage(void) { return const_cast<Message &>(*mResponseMessage); }
TimeMilli GetStartTime(void) const { return mStartTime; }
NameCompressInfo &GetNameCompressInfo(void) { return mCompressInfo; };
void Finalize(Header::Response aResponseMessage, Ip6::Udp::Socket &aSocket);
Instance &GetInstance(void) const { return mMessage->GetInstance(); }
Header mResponseHeader;
Message *mResponseMessage;
Error AddQuestionsFrom(const Request &aRequest);
Error AppendQuestion(const char *aName, const Question &aQuestion);
Error AppendPtrRecord(const char *aServiceName, const char *aInstanceName, uint32_t aTtl);
Error AppendSrvRecord(const char *aInstanceName,
const char *aHostName,
uint32_t aTtl,
uint16_t aPriority,
uint16_t aWeight,
uint16_t aPort);
Error AppendTxtRecord(const char *aInstanceName, const void *aTxtData, uint16_t aTxtLength, uint32_t aTtl);
Error AppendAaaaRecord(const char *aHostName, const Ip6::Address &aAddress, uint32_t aTtl);
Error AppendServiceName(const char *aName);
Error AppendInstanceName(const char *aName);
Error AppendHostName(const char *aName);
void IncResourceRecordCount(void);
bool HasQuestion(const char *aName, uint16_t aQuestionType) const;
void Send(const Ip6::MessageInfo &aMessageInfo);
void GetQueryTypeAndName(DnsQueryType &aType, char (&aName)[Name::kMaxNameSize]) const;
#if OPENTHREAD_CONFIG_SRP_SERVER_ENABLE
void ResolveBySrp(void);
void ResolveQuestionBySrp(const char *aName, const Question &aQuestion);
#endif
Message *mMessage;
Header mHeader;
NameCompressInfo mCompressInfo;
bool mAdditional; // Whether or not appending new records in additional data section.
};
struct QueryTransaction : public Response
{
bool IsValid(void) const { return mMessage != nullptr; }
bool CanAnswer(const char *aServiceFullName, const ServiceInstanceInfo &aInstanceInfo) const;
bool CanAnswer(const char *aHostFullName) const;
void Answer(const char *aServiceFullName, const ServiceInstanceInfo &aInstanceInfo);
void Answer(const char *aHostFullName, const HostInfo &aHostInfo);
void Finalize(Header::Response aResponseCode);
Ip6::MessageInfo mMessageInfo;
TimeMilli mStartTime;
TimeMilli mExpireTime;
};
static constexpr uint32_t kQueryTimeout = OPENTHREAD_CONFIG_DNSSD_QUERY_TIMEOUT;
bool IsRunning(void) const { return mSocket.IsBound(); }
static void HandleUdpReceive(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo);
void HandleUdpReceive(Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
void ProcessQuery(const Header &aRequestHeader, Message &aRequestMessage, const Ip6::MessageInfo &aMessageInfo);
static Header::Response AddQuestions(const Header &aRequestHeader,
const Message &aRequestMessage,
Header &aResponseHeader,
Message &aResponseMessage,
NameCompressInfo &aCompressInfo);
static Error AppendQuestion(const char *aName,
const Question &aQuestion,
Message &aMessage,
NameCompressInfo &aCompressInfo);
static Error AppendPtrRecord(Message &aMessage,
const char *aServiceName,
const char *aInstanceName,
uint32_t aTtl,
NameCompressInfo &aCompressInfo);
static Error AppendSrvRecord(Message &aMessage,
const char *aInstanceName,
const char *aHostName,
uint32_t aTtl,
uint16_t aPriority,
uint16_t aWeight,
uint16_t aPort,
NameCompressInfo &aCompressInfo);
static Error AppendTxtRecord(Message &aMessage,
const char *aInstanceName,
const void *aTxtData,
uint16_t aTxtLength,
uint32_t aTtl,
NameCompressInfo &aCompressInfo);
static Error AppendAaaaRecord(Message &aMessage,
const char *aHostName,
const Ip6::Address &aAddress,
uint32_t aTtl,
NameCompressInfo &aCompressInfo);
static Error AppendServiceName(Message &aMessage, const char *aName, NameCompressInfo &aCompressInfo);
static Error AppendInstanceName(Message &aMessage, const char *aName, NameCompressInfo &aCompressInfo);
static Error AppendHostName(Message &aMessage, const char *aName, NameCompressInfo &aCompressInfo);
static void IncResourceRecordCount(Header &aHeader, bool aAdditional);
static Error FindNameComponents(const char *aName, const char *aDomain, NameComponentsOffsetInfo &aInfo);
static Error FindPreviousLabel(const char *aName, uint8_t &aStart, uint8_t &aStop);
void SendResponse(Header aHeader,
Header::Response aResponseCode,
Message &aMessage,
const Ip6::MessageInfo &aMessageInfo,
Ip6::Udp::Socket &aSocket);
#if OPENTHREAD_CONFIG_SRP_SERVER_ENABLE
Header::Response ResolveBySrp(Header &aResponseHeader,
Message &aResponseMessage,
Server::NameCompressInfo &aCompressInfo);
Header::Response ResolveQuestionBySrp(const char *aName,
const Question &aQuestion,
Header &aResponseHeader,
Message &aResponseMessage,
NameCompressInfo &aCompressInfo,
bool aAdditional);
#endif
bool IsRunning(void) const { return mSocket.IsBound(); }
static void HandleUdpReceive(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo);
void HandleUdpReceive(Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
void ProcessQuery(const Request &aRequest);
static Error FindNameComponents(const char *aName, const char *aDomain, NameComponentsOffsetInfo &aInfo);
static Error FindPreviousLabel(const char *aName, uint8_t &aStart, uint8_t &aStop);
#if OPENTHREAD_CONFIG_DNS_UPSTREAM_QUERY_ENABLE
static bool ShouldForwardToUpstream(const Header &aRequestHeader, const Message &aRequestMessage);
static bool ShouldForwardToUpstream(const Request &aRequest);
UpstreamQueryTransaction *AllocateUpstreamQueryTransaction(const Ip6::MessageInfo &aMessageInfo);
void ResetUpstreamQueryTransaction(UpstreamQueryTransaction &aTxn, Error aError);
Error ResolveByUpstream(const Message &aRequestMessage, const Ip6::MessageInfo &aMessageInfo);
Error ResolveByUpstream(const Request &aRequest);
#endif
Error ResolveByQueryCallbacks(Header &aResponseHeader,
Message &aResponseMessage,
NameCompressInfo &aCompressInfo,
const Ip6::MessageInfo &aMessageInfo);
QueryTransaction *NewQuery(const Header &aResponseHeader,
Message &aResponseMessage,
const NameCompressInfo &aCompressInfo,
const Ip6::MessageInfo &aMessageInfo);
static bool CanAnswerQuery(const QueryTransaction &aQuery,
const char *aServiceFullName,
const otDnssdServiceInstanceInfo &aInstanceInfo);
void AnswerQuery(QueryTransaction &aQuery,
const char *aServiceFullName,
const otDnssdServiceInstanceInfo &aInstanceInfo);
static bool CanAnswerQuery(const Server::QueryTransaction &aQuery, const char *aHostFullName);
void AnswerQuery(QueryTransaction &aQuery, const char *aHostFullName, const otDnssdHostInfo &aHostInfo);
void FinalizeQuery(QueryTransaction &aQuery, Header::Response aResponseCode);
static DnsQueryType GetQueryTypeAndName(const Header &aHeader,
const Message &aMessage,
char (&aName)[Name::kMaxNameSize]);
static bool HasQuestion(const Header &aHeader, const Message &aMessage, const char *aName, uint16_t aQuestionType);
Error ResolveByQueryCallbacks(Response &aResponse, const Ip6::MessageInfo &aMessageInfo);
QueryTransaction *NewQuery(Response &aResponse, const Ip6::MessageInfo &aMessageInfo);
void HandleTimer(void);
void ResetTimer(void);
@@ -539,10 +483,9 @@ private:
Ip6::Udp::Socket mSocket;
QueryTransaction mQueryTransactions[kMaxConcurrentQueries];
void *mQueryCallbackContext;
otDnssdQuerySubscribeCallback mQuerySubscribe;
otDnssdQueryUnsubscribeCallback mQueryUnsubscribe;
QueryTransaction mQueryTransactions[kMaxConcurrentQueries];
Callback<SubscribeCallback> mQuerySubscribe;
Callback<UnsubscribeCallback> mQueryUnsubscribe;
static const char *kBlockedDomains[];
#if OPENTHREAD_CONFIG_DNS_UPSTREAM_QUERY_ENABLE