3 * Copyright (c) 2020 Project CHIP Authors
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
21 #include "ResponseBuilder.h"
24 #include <mdns/minimal/responders/QueryResponder.h>
26 #include <inet/InetLayer.h>
27 #include <system/SystemPacketBuffer.h>
34 /// Represents the internal state for sending a currently active request
35 class ResponseSendingState
38 ResponseSendingState() {}
40 void Reset(uint32_t messageId, const QueryData & query, const chip::Inet::IPPacketInfo * packet)
42 mMessageId = messageId;
45 mSendError = CHIP_NO_ERROR;
46 mResourceType = ResourceType::kAnswer;
49 void SetResourceType(ResourceType resourceType) { mResourceType = resourceType; }
50 ResourceType GetResourceType() const { return mResourceType; }
52 CHIP_ERROR SetError(CHIP_ERROR chipError)
54 mSendError = chipError;
57 CHIP_ERROR GetError() const { return mSendError; }
59 uint32_t GetMessageId() const { return mMessageId; }
61 const QueryData * GetQuery() const { return mQuery; }
63 /// Check if the reply should be sent as a unicast reply
64 bool SendUnicast() const;
66 /// Check if the original query should be included in the reply
67 bool IncludeQuery() const;
69 const chip::Inet::IPPacketInfo * GetSource() const { return mSource; }
71 uint16_t GetSourcePort() const { return mSource->SrcPort; }
72 const chip::Inet::IPAddress & GetSourceAddress() const { return mSource->SrcAddress; }
73 chip::Inet::InterfaceId GetSourceInterfaceId() const { return mSource->Interface; }
76 const QueryData * mQuery = nullptr; // query being replied to
77 const chip::Inet::IPPacketInfo * mSource = nullptr; // Where to send the reply (if unicast)
78 uint32_t mMessageId = 0; // message id for the reply
79 ResourceType mResourceType = ResourceType::kAnswer; // what is being sent right now
80 CHIP_ERROR mSendError = CHIP_NO_ERROR;
83 } // namespace Internal
85 /// Sends responses to mDNS queries.
87 /// Handles processing the query via a QueryResponderBase and then sending back the reply
88 /// using appropriate paths (unicast or multicast) via the given Server.
89 class ResponseSender : public ResponderDelegate
92 ResponseSender(ServerBase * server, QueryResponderBase * responder) : mServer(server), mResponder(responder) {}
94 /// Send back the response to a particular query
95 CHIP_ERROR Respond(uint32_t messageId, const QueryData & query, const chip::Inet::IPPacketInfo * querySource);
97 // Implementation of ResponderDelegate
98 void AddResponse(const ResourceRecord & record) override;
101 CHIP_ERROR FlushReply();
102 CHIP_ERROR PrepareNewReplyPacket();
104 ServerBase * mServer;
105 QueryResponderBase * mResponder;
107 /// Current send state
108 ResponseBuilder mResponseBuilder; // packet being built
109 Internal::ResponseSendingState mSendState; // sending state
112 } // namespace Minimal