a14e0687d296964be5e6b8058ca512c5f105119c
[platform/upstream/connectedhomeip.git] / src / lib / mdns / minimal / ResourceRecord.h
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 "Constants.h"
23 #include "QName.h"
24
25 #include <support/BufBound.h>
26
27 namespace mdns {
28 namespace Minimal {
29
30 /// A generic Reply record that supports data serialization
31 class ResourceRecord
32 {
33 public:
34     virtual ~ResourceRecord() {}
35
36     QClass GetClass() const { return QClass::IN; }
37     QType GetType() const { return mType; }
38
39     uint64_t GetTtl() const { return mTtl; }
40     ResourceRecord & SetTtl(uint64_t ttl)
41     {
42         mTtl = ttl;
43         return *this;
44     }
45
46     /// Append the given record to the underlying output.
47     /// Updates header item count on success, does NOT update header on failure.
48     bool Append(HeaderRef & hdr, ResourceType asType, chip::BufBound & out) const;
49
50 protected:
51     /// Output the data portion of the resource record.
52     virtual bool WriteData(chip::BufBound & out) const = 0;
53
54     ResourceRecord(QType type, const QNamePart * names, uint16_t namesCount) : mType(type), mQNameCount(namesCount), mQName(names)
55     {}
56
57 private:
58     const QType mType;
59     uint64_t mTtl = 0;
60
61     const uint16_t mQNameCount;
62     const QNamePart * mQName;
63 };
64
65 } // namespace Minimal
66 } // namespace mdns