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 :
25 : #include <cstdint>
26 : #include <optional>
27 :
28 : namespace mdns {
29 : namespace Minimal {
30 :
31 : /// Controls specific options for responding to mDNS queries
32 : ///
33 : class ResponseConfiguration
34 : {
35 : public:
36 74 : ResponseConfiguration() {}
37 : ~ResponseConfiguration() = default;
38 :
39 : std::optional<uint32_t> GetTtlSecondsOverride() const { return mTtlSecondsOverride; }
40 18 : ResponseConfiguration & SetTtlSecondsOverride(std::optional<uint32_t> override)
41 : {
42 18 : mTtlSecondsOverride = override;
43 18 : return *this;
44 : }
45 :
46 18 : ResponseConfiguration & SetTtlSecondsOverride(uint32_t value) { return SetTtlSecondsOverride(std::make_optional(value)); }
47 : ResponseConfiguration & ClearTtlSecondsOverride() { return SetTtlSecondsOverride(std::nullopt); }
48 :
49 : /// Applies any adjustments to resource records before they are being serialized
50 : /// to some form of reply.
51 766 : void Adjust(ResourceRecord & record) const
52 : {
53 766 : VerifyOrReturn(mTtlSecondsOverride.has_value());
54 196 : record.SetTtl(*mTtlSecondsOverride);
55 : }
56 :
57 : private:
58 : std::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 161 : Responder(QType qType, const FullQName & qName) : mQType(qType), mQName(qName) {}
69 161 : virtual ~Responder() {}
70 :
71 1853 : QClass GetQClass() const { return QClass::IN; }
72 3789 : QType GetQType() const { return mQType; }
73 :
74 : /// Full name as: "Instance"."Servicer"."Domain"
75 : /// Domain name is generally just 'local'
76 7808 : 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 3 : 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
|