Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / lib / mdns / minimal / ResponseSender.h
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #pragma once
19
20 #include "Parser.h"
21 #include "ResponseBuilder.h"
22 #include "Server.h"
23
24 #include <mdns/minimal/responders/QueryResponder.h>
25
26 #include <inet/InetLayer.h>
27 #include <system/SystemPacketBuffer.h>
28
29 namespace mdns {
30 namespace Minimal {
31
32 namespace Internal {
33
34 /// Represents the internal state for sending a currently active request
35 class ResponseSendingState
36 {
37 public:
38     ResponseSendingState() {}
39
40     void Reset(uint32_t messageId, const QueryData & query, const chip::Inet::IPPacketInfo * packet)
41     {
42         mMessageId    = messageId;
43         mQuery        = &query;
44         mSource       = packet;
45         mSendError    = CHIP_NO_ERROR;
46         mResourceType = ResourceType::kAnswer;
47     }
48
49     void SetResourceType(ResourceType resourceType) { mResourceType = resourceType; }
50     ResourceType GetResourceType() const { return mResourceType; }
51
52     CHIP_ERROR SetError(CHIP_ERROR chipError)
53     {
54         mSendError = chipError;
55         return mSendError;
56     }
57     CHIP_ERROR GetError() const { return mSendError; }
58
59     uint32_t GetMessageId() const { return mMessageId; }
60
61     const QueryData * GetQuery() const { return mQuery; }
62
63     /// Check if the reply should be sent as a unicast reply
64     bool SendUnicast() const;
65
66     /// Check if the original query should be included in the reply
67     bool IncludeQuery() const;
68
69     const chip::Inet::IPPacketInfo * GetSource() const { return mSource; }
70
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; }
74
75 private:
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;
81 };
82
83 } // namespace Internal
84
85 /// Sends responses to mDNS queries.
86 ///
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
90 {
91 public:
92     ResponseSender(ServerBase * server, QueryResponderBase * responder) : mServer(server), mResponder(responder) {}
93
94     /// Send back the response to a particular query
95     CHIP_ERROR Respond(uint32_t messageId, const QueryData & query, const chip::Inet::IPPacketInfo * querySource);
96
97     // Implementation of ResponderDelegate
98     void AddResponse(const ResourceRecord & record) override;
99
100 private:
101     CHIP_ERROR FlushReply();
102     CHIP_ERROR PrepareNewReplyPacket();
103
104     ServerBase * mServer;
105     QueryResponderBase * mResponder;
106
107     /// Current send state
108     ResponseBuilder mResponseBuilder;          // packet being built
109     Internal::ResponseSendingState mSendState; // sending state
110 };
111
112 } // namespace Minimal
113 } // namespace mdns