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.
20 #include <mdns/minimal/core/Constants.h>
21 #include <mdns/minimal/core/DnsHeader.h>
22 #include <mdns/minimal/core/QName.h>
31 QueryData(const QueryData &) = default;
32 QueryData & operator=(const QueryData &) = default;
34 QueryData(QType type, QClass klass, bool unicast) : mType(type), mClass(klass), mAnswerViaUnicast(unicast) {}
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)
40 QType GetType() const { return mType; }
41 QClass GetClass() const { return mClass; }
42 bool RequestedUnicastAnswer() const { return mAnswerViaUnicast; }
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; }
49 SerializedQNameIterator GetName() const { return mNameIterator; }
51 /// Parses a query structure
53 /// Parses the query at [start] and updates start to the end of the structure.
55 /// returns true on parse success, false on failure.
56 bool Parse(const BytesRange & validData, const uint8_t ** start);
58 /// Write out this query data back into an output buffer.
59 bool Append(HeaderRef & hdr, chip::Encoding::BigEndian::BufferWriter & out) const;
62 QType mType = QType::ANY;
63 QClass mClass = QClass::ANY;
64 bool mAnswerViaUnicast = false;
65 SerializedQNameIterator mNameIterator;
67 /// Flag as a boot-time internal query. This allows query replies
68 /// to be built accordingly.
69 bool mIsBootAdvertising = false;
77 ResourceData(const ResourceData &) = default;
78 ResourceData & operator=(const ResourceData &) = default;
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; }
86 /// Parses a resource data structure
88 /// Parses the daata at [start] and updates start to the end of the structure.
89 /// Updates [out] with the parsed data on success.
91 /// returns true on parse success, false on failure.
92 bool Parse(const BytesRange & validData, const uint8_t ** start);
95 SerializedQNameIterator mNameIterator;
96 QType mType = QType::ANY;
97 QClass mClass = QClass::ANY;
105 virtual ~ParserDelegate() {}
107 virtual void OnHeader(ConstHeaderRef & header) = 0;
109 virtual void OnQuery(const QueryData & data) = 0;
111 virtual void OnResource(ResourceType type, const ResourceData & data) = 0;
114 /// Parses a mMDNS packet.
116 /// Calls appropriate delegate callbacks while parsing
118 /// returns true if packet was succesfully parsed, false otherwise
119 bool ParsePacket(const BytesRange & packetData, ParserDelegate * delegate);
121 } // namespace Minimal