Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / lib / mdns / minimal / Parser.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 <mdns/minimal/core/Constants.h>
21 #include <mdns/minimal/core/DnsHeader.h>
22 #include <mdns/minimal/core/QName.h>
23
24 namespace mdns {
25 namespace Minimal {
26
27 class QueryData
28 {
29 public:
30     QueryData() {}
31     QueryData(const QueryData &) = default;
32     QueryData & operator=(const QueryData &) = default;
33
34     QueryData(QType type, QClass klass, bool unicast) : mType(type), mClass(klass), mAnswerViaUnicast(unicast) {}
35
36     QueryData(QType type, QClass klass, bool unicast, const uint8_t * nameStart, const BytesRange & validData) :
37         mType(type), mClass(klass), mAnswerViaUnicast(unicast), mNameIterator(validData, nameStart)
38     {}
39
40     QType GetType() const { return mType; }
41     QClass GetClass() const { return mClass; }
42     bool RequestedUnicastAnswer() const { return mAnswerViaUnicast; }
43
44     /// Boot advertisement is an internal query meant to advertise all available
45     /// services at device startup time.
46     bool IsBootAdvertising() const { return mIsBootAdvertising; }
47     void SetIsBootAdvertising(bool isBootAdvertising) { mIsBootAdvertising = isBootAdvertising; }
48
49     SerializedQNameIterator GetName() const { return mNameIterator; }
50
51     /// Parses a query structure
52     ///
53     /// Parses the query at [start] and updates start to the end of the structure.
54     ///
55     /// returns true on parse success, false on failure.
56     bool Parse(const BytesRange & validData, const uint8_t ** start);
57
58     /// Write out this query data back into an output buffer.
59     bool Append(HeaderRef & hdr, chip::Encoding::BigEndian::BufferWriter & out) const;
60
61 private:
62     QType mType            = QType::ANY;
63     QClass mClass          = QClass::ANY;
64     bool mAnswerViaUnicast = false;
65     SerializedQNameIterator mNameIterator;
66
67     /// Flag as a boot-time internal query. This allows query replies
68     /// to be built accordingly.
69     bool mIsBootAdvertising = false;
70 };
71
72 class ResourceData
73 {
74 public:
75     ResourceData() {}
76
77     ResourceData(const ResourceData &) = default;
78     ResourceData & operator=(const ResourceData &) = default;
79
80     QType GetType() const { return mType; }
81     QClass GetClass() const { return mClass; }
82     uint64_t GetTtlSeconds() const { return mTtl; }
83     SerializedQNameIterator GetName() const { return mNameIterator; }
84     const BytesRange & GetData() const { return mData; }
85
86     /// Parses a resource data structure
87     ///
88     /// Parses the daata at [start] and updates start to the end of the structure.
89     /// Updates [out] with the parsed data on success.
90     ///
91     /// returns true on parse success, false on failure.
92     bool Parse(const BytesRange & validData, const uint8_t ** start);
93
94 private:
95     SerializedQNameIterator mNameIterator;
96     QType mType   = QType::ANY;
97     QClass mClass = QClass::ANY;
98     uint64_t mTtl = 0;
99     BytesRange mData;
100 };
101
102 class ParserDelegate
103 {
104 public:
105     virtual ~ParserDelegate() {}
106
107     virtual void OnHeader(ConstHeaderRef & header) = 0;
108
109     virtual void OnQuery(const QueryData & data) = 0;
110
111     virtual void OnResource(ResourceType type, const ResourceData & data) = 0;
112 };
113
114 /// Parses a mMDNS packet.
115 ///
116 /// Calls appropriate delegate callbacks while parsing
117 ///
118 /// returns true if packet was succesfully parsed, false otherwise
119 bool ParsePacket(const BytesRange & packetData, ParserDelegate * delegate);
120
121 } // namespace Minimal
122 } // namespace mdns