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 <cstddef>
21 :
22 : #include <lib/dnssd/minimal_mdns/core/Constants.h>
23 : #include <lib/dnssd/minimal_mdns/core/QName.h>
24 : #include <lib/dnssd/minimal_mdns/core/RecordWriter.h>
25 :
26 : #include <lib/support/BufferWriter.h>
27 :
28 : namespace mdns {
29 : namespace Minimal {
30 :
31 : /// A generic Reply record that supports data serialization
32 : class ResourceRecord
33 : {
34 : public:
35 : static constexpr uint32_t kDefaultTtl = 120; // 2 minutes
36 :
37 411 : virtual ~ResourceRecord() {}
38 :
39 : ResourceRecord & operator=(const ResourceRecord & other) = default;
40 :
41 14 : const FullQName & GetName() const { return mQName; }
42 797 : QClass GetClass() const { return mCacheFlush ? QClass::IN_FLUSH : QClass::IN; }
43 462 : QType GetType() const { return mType; }
44 :
45 861 : uint32_t GetTtl() const { return mTtl; }
46 133 : ResourceRecord & SetTtl(uint32_t ttl)
47 : {
48 133 : mTtl = ttl;
49 133 : return *this;
50 : }
51 :
52 176 : ResourceRecord & SetCacheFlush(bool set)
53 : {
54 176 : mCacheFlush = set;
55 176 : return *this;
56 : }
57 : bool GetCacheFlush() const { return mCacheFlush; }
58 :
59 : /// Append the given record to the underlying output.
60 : /// Updates header item count on success, does NOT update header on failure.
61 : bool Append(HeaderRef & hdr, ResourceType asType, RecordWriter & out) const;
62 :
63 : protected:
64 : /// Output the data portion of the resource record.
65 : virtual bool WriteData(RecordWriter & out) const = 0;
66 :
67 313 : ResourceRecord(QType type, FullQName name) : mType(type), mQName(name) {}
68 :
69 : private:
70 : QType mType;
71 : uint32_t mTtl = kDefaultTtl;
72 : FullQName mQName;
73 : bool mCacheFlush = false;
74 : };
75 :
76 : } // namespace Minimal
77 : } // namespace mdns
|