Line data Source code
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 <lib/dnssd/minimal_mdns/core/Constants.h> 21 : #include <lib/dnssd/minimal_mdns/core/DnsHeader.h> 22 : #include <lib/dnssd/minimal_mdns/core/QName.h> 23 : #include <lib/dnssd/minimal_mdns/core/RecordWriter.h> 24 : 25 : namespace mdns { 26 : namespace Minimal { 27 : 28 : class QueryData 29 : { 30 : public: 31 120 : QueryData() {} 32 : QueryData(const QueryData &) = default; 33 : QueryData & operator=(const QueryData &) = default; 34 : 35 114 : QueryData(QType type, QClass klass, bool unicast) : mType(type), mClass(klass), mAnswerViaUnicast(unicast) {} 36 : 37 : QueryData(QType type, QClass klass, bool unicast, const uint8_t * nameStart, const BytesRange & validData) : 38 : mType(type), mClass(klass), mAnswerViaUnicast(unicast), mNameIterator(validData, nameStart) 39 : {} 40 : 41 2737 : QType GetType() const { return mType; } 42 2988 : QClass GetClass() const { return mClass; } 43 1021 : bool RequestedUnicastAnswer() const { return mAnswerViaUnicast; } 44 : 45 : /// Internal broadcasts will advertise all available data and will not apply 46 : /// any broadcast filtering. Intent is for paths such as: 47 : /// - boot time advertisement: advertise all services available 48 : /// - stop-time advertisement: advertise a TTL of 0 as services are removed 49 1398 : bool IsAnnounceBroadcast() const { return mIsAnnounceBroadcast; } 50 114 : void SetIsAnnounceBroadcast(bool isAnnounceBroadcast) { mIsAnnounceBroadcast = isAnnounceBroadcast; } 51 : 52 430 : SerializedQNameIterator GetName() const { return mNameIterator; } 53 : 54 : /// Parses a query structure 55 : /// 56 : /// Parses the query at [start] and updates start to the end of the structure. 57 : /// 58 : /// returns true on parse success, false on failure. 59 : bool Parse(const BytesRange & validData, const uint8_t ** start); 60 : 61 : /// Write out this query data back into an output buffer. 62 : bool Append(HeaderRef & hdr, RecordWriter & out) const; 63 : 64 : private: 65 : QType mType = QType::ANY; 66 : QClass mClass = QClass::ANY; 67 : bool mAnswerViaUnicast = false; 68 : SerializedQNameIterator mNameIterator; 69 : 70 : /// Flag as an internal broadcast, controls reply construction (e.g. no 71 : /// filtering applied) 72 : bool mIsAnnounceBroadcast = false; 73 : }; 74 : 75 : class ResourceData 76 : { 77 : public: 78 120 : ResourceData() {} 79 : 80 : ResourceData(const ResourceData &) = default; 81 : ResourceData & operator=(const ResourceData &) = default; 82 : 83 1161 : QType GetType() const { return mType; } 84 : QClass GetClass() const { return mClass; } 85 : uint64_t GetTtlSeconds() const { return mTtl; } 86 169 : SerializedQNameIterator GetName() const { return mNameIterator; } 87 160 : const BytesRange & GetData() const { return mData; } 88 : 89 : /// Parses a resource data structure 90 : /// 91 : /// Parses the daata at [start] and updates start to the end of the structure. 92 : /// Updates [out] with the parsed data on success. 93 : /// 94 : /// returns true on parse success, false on failure. 95 : bool Parse(const BytesRange & validData, const uint8_t ** start); 96 : 97 : private: 98 : SerializedQNameIterator mNameIterator; 99 : QType mType = QType::ANY; 100 : QClass mClass = QClass::ANY; 101 : uint64_t mTtl = 0; 102 : BytesRange mData; 103 : }; 104 : 105 : class ParserDelegate 106 : { 107 : public: 108 30 : virtual ~ParserDelegate() {} 109 : 110 : virtual void OnHeader(ConstHeaderRef & header) = 0; 111 : 112 : virtual void OnQuery(const QueryData & data) = 0; 113 : 114 : virtual void OnResource(ResourceType type, const ResourceData & data) = 0; 115 : }; 116 : 117 : /// Parses a mMDNS packet. 118 : /// 119 : /// Calls appropriate delegate callbacks while parsing 120 : /// 121 : /// returns true if packet was successfully parsed, false otherwise 122 : bool ParsePacket(const BytesRange & packetData, ParserDelegate * delegate); 123 : 124 : } // namespace Minimal 125 : } // namespace mdns