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/QName.h> 21 : #include <lib/dnssd/minimal_mdns/records/ResourceRecord.h> 22 : 23 : #include <inet/IPPacketInfo.h> 24 : #include <lib/core/Optional.h> 25 : 26 : namespace mdns { 27 : namespace Minimal { 28 : 29 : /// Controls specific options for responding to mDNS queries 30 : /// 31 : class ResponseConfiguration 32 : { 33 : public: 34 81 : ResponseConfiguration() {} 35 81 : ~ResponseConfiguration() = default; 36 : 37 : chip::Optional<uint32_t> GetTtlSecondsOverride() const { return mTtlSecondsOverride; } 38 21 : ResponseConfiguration & SetTtlSecondsOverride(chip::Optional<uint32_t> override) 39 : { 40 21 : mTtlSecondsOverride = override; 41 21 : return *this; 42 : } 43 : 44 21 : ResponseConfiguration & SetTtlSecondsOverride(uint32_t value) { return SetTtlSecondsOverride(chip::MakeOptional(value)); } 45 : ResponseConfiguration & ClearTtlSecondsOverride() { return SetTtlSecondsOverride(chip::NullOptional); } 46 : 47 : /// Applies any adjustments to resource records before they are being serialized 48 : /// to some form of reply. 49 822 : void Adjust(ResourceRecord & record) const 50 : { 51 822 : if (mTtlSecondsOverride.HasValue()) 52 : { 53 214 : record.SetTtl(mTtlSecondsOverride.Value()); 54 : } 55 822 : } 56 : 57 : private: 58 : chip::Optional<uint32_t> mTtlSecondsOverride; 59 : }; 60 : 61 : // Delegates that responders can write themselves to 62 : class ResponderDelegate; 63 : 64 : /// Adds ability to respond with specific types of data 65 : class Responder 66 : { 67 : public: 68 168 : Responder(QType qType, const FullQName & qName) : mQType(qType), mQName(qName) {} 69 168 : virtual ~Responder() {} 70 : 71 1961 : QClass GetQClass() const { return QClass::IN; } 72 4037 : QType GetQType() const { return mQType; } 73 : 74 : /// Full name as: "Instance"."Servicer"."Domain" 75 : /// Domain name is generally just 'local' 76 8349 : FullQName GetQName() const { return mQName; } 77 : 78 : /// Report all responses maintained by this responder 79 : /// 80 : /// Responses are associated with the objects type/class/qname. 81 : virtual void AddAllResponses(const chip::Inet::IPPacketInfo * source, ResponderDelegate * delegate, 82 : const ResponseConfiguration & configuration) = 0; 83 : 84 : private: 85 : const QType mQType; 86 : const FullQName mQName; 87 : }; 88 : 89 : class ResponderDelegate 90 : { 91 : public: 92 2 : virtual ~ResponderDelegate() {} 93 : 94 : /// Add the specified resource record to the response 95 : virtual void AddResponse(const ResourceRecord & record) = 0; 96 : 97 : /// Accept to add responses for the particular responder. 98 : /// 99 : /// This will be called before responders serialize their records. 100 0 : virtual bool ShouldSend(const Responder &) const { return true; } 101 : 102 : /// Called when all responses were added for a particular responder 103 : /// 104 : /// Only called if a previous accept returned true. 105 0 : virtual void ResponsesAdded(const Responder &) {} 106 : }; 107 : 108 : } // namespace Minimal 109 : } // namespace mdns