diff --git a/src/core/net/dns_client.cpp b/src/core/net/dns_client.cpp index 48f6d5066..8aeac5999 100644 --- a/src/core/net/dns_client.cpp +++ b/src/core/net/dns_client.cpp @@ -48,30 +48,6 @@ using ot::Encoding::BigEndian::HostSwap16; namespace ot { namespace Dns { -QueryMetadata::QueryMetadata(void) - : mHostname(nullptr) - , mResponseHandler(nullptr) - , mResponseContext(nullptr) - , mTransmissionTime() - , mDestinationPort(0) - , mRetransmissionCount(0) -{ - mSourceAddress.Clear(); - mDestinationAddress.Clear(); -} - -QueryMetadata::QueryMetadata(otDnsResponseHandler aHandler, void *aContext) - : mHostname(nullptr) - , mResponseHandler(aHandler) - , mResponseContext(aContext) - , mTransmissionTime() - , mDestinationPort(0) - , mRetransmissionCount(0) -{ - mSourceAddress.Clear(); - mDestinationAddress.Clear(); -} - Client::Client(Instance &aInstance) : mSocket(aInstance) , mMessageId(0) @@ -112,7 +88,7 @@ otError Client::Stop(void) otError Client::Query(const otDnsQuery *aQuery, otDnsResponseHandler aHandler, void *aContext) { otError error; - QueryMetadata queryMetadata(aHandler, aContext); + QueryMetadata queryMetadata; Message * message = nullptr; Message * messageCopy = nullptr; Header header; @@ -140,10 +116,12 @@ otError Client::Query(const otDnsQuery *aQuery, otDnsResponseHandler aHandler, v messageInfo = static_cast(aQuery->mMessageInfo); queryMetadata.mHostname = aQuery->mHostname; + queryMetadata.mResponseHandler = aHandler; + queryMetadata.mResponseContext = aContext; queryMetadata.mTransmissionTime = TimerMilli::GetNow() + kResponseTimeout; queryMetadata.mSourceAddress = messageInfo->GetSockAddr(); - queryMetadata.mDestinationPort = messageInfo->GetPeerPort(); queryMetadata.mDestinationAddress = messageInfo->GetPeerAddr(); + queryMetadata.mDestinationPort = messageInfo->GetPeerPort(); queryMetadata.mRetransmissionCount = 0; VerifyOrExit((messageCopy = CopyAndEnqueueMessage(*message, queryMetadata)) != nullptr, error = OT_ERROR_NO_BUFS); @@ -522,6 +500,19 @@ exit: } } +void Client::QueryMetadata::ReadFrom(const Message &aMessage) +{ + uint16_t length = aMessage.GetLength(); + + OT_ASSERT(length >= sizeof(*this)); + aMessage.Read(length - sizeof(*this), sizeof(*this), this); +} + +void Client::QueryMetadata::UpdateIn(Message &aMessage) const +{ + aMessage.Write(aMessage.GetLength() - sizeof(*this), sizeof(*this), this); +} + } // namespace Dns } // namespace ot diff --git a/src/core/net/dns_client.hpp b/src/core/net/dns_client.hpp index 32d38a4d5..045e452ef 100644 --- a/src/core/net/dns_client.hpp +++ b/src/core/net/dns_client.hpp @@ -47,78 +47,6 @@ namespace ot { namespace Dns { -/** - * This class implements metadata required for DNS retransmission. - * - */ -class QueryMetadata -{ - friend class Client; - -public: - /** - * Default constructor for the object. - * - */ - QueryMetadata(void); - - /** - * This constructor initializes the object with specific values. - * - * @param[in] aHandler Pointer to a handler function for the response. - * @param[in] aContext Context for the handler function. - * - */ - QueryMetadata(otDnsResponseHandler aHandler, void *aContext); - - /** - * This method appends request data to the message. - * - * @param[in] aMessage A reference to the message. - * - * @retval OT_ERROR_NONE Successfully appended the bytes. - * @retval OT_ERROR_NO_BUFS Insufficient available buffers to grow the message. - * - */ - otError AppendTo(Message &aMessage) const { return aMessage.Append(this, sizeof(*this)); } - - /** - * This method reads request data from the message. - * - * @param[in] aMessage A reference to the message. - * - */ - void ReadFrom(const Message &aMessage) - { - uint16_t length = aMessage.Read(aMessage.GetLength() - sizeof(*this), sizeof(*this), this); - OT_ASSERT(length == sizeof(*this)); - OT_UNUSED_VARIABLE(length); - } - - /** - * This method updates request data in the message. - * - * @param[in] aMessage A reference to the message. - * - * @returns The number of bytes updated. - * - */ - int UpdateIn(Message &aMessage) const - { - return aMessage.Write(aMessage.GetLength() - sizeof(*this), sizeof(*this), this); - } - -private: - const char * mHostname; ///< A hostname to be find. - otDnsResponseHandler mResponseHandler; ///< A function pointer that is called on response reception. - void * mResponseContext; ///< A pointer to arbitrary context information. - TimeMilli mTransmissionTime; ///< Time when the timer should shoot for this message. - Ip6::Address mSourceAddress; ///< IPv6 address of the message source. - Ip6::Address mDestinationAddress; ///< IPv6 address of the message destination. - uint16_t mDestinationPort; ///< UDP port of the message destination. - uint8_t mRetransmissionCount; ///< Number of retransmissions. -}; - /** * This class implements DNS client. * @@ -193,6 +121,22 @@ private: kBufSize = 16 }; + struct QueryMetadata + { + otError AppendTo(Message &aMessage) const { return aMessage.Append(this, sizeof(*this)); } + void ReadFrom(const Message &aMessage); + void UpdateIn(Message &aMessage) const; + + const char * mHostname; + otDnsResponseHandler mResponseHandler; + void * mResponseContext; + TimeMilli mTransmissionTime; + Ip6::Address mSourceAddress; + Ip6::Address mDestinationAddress; + uint16_t mDestinationPort; + uint8_t mRetransmissionCount; + }; + Message *NewMessage(const Header &aHeader); Message *CopyAndEnqueueMessage(const Message &aMessage, const QueryMetadata &aQueryMetadata); void DequeueMessage(Message &aMessage);