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 : #include "ResourceRecord.h"
19 :
20 : namespace mdns {
21 : namespace Minimal {
22 :
23 864 : bool ResourceRecord::Append(HeaderRef & hdr, ResourceType asType, RecordWriter & out) const
24 : {
25 : // order is important based on resource type. First come answers, then authorityAnswers
26 : // and then additional:
27 864 : if ((asType == ResourceType::kAuthority) && (hdr.GetAdditionalCount() != 0))
28 : {
29 1 : return false;
30 : }
31 863 : if ((asType == ResourceType::kAnswer) && ((hdr.GetAdditionalCount() != 0) || (hdr.GetAuthorityCount() != 0)))
32 : {
33 2 : return false;
34 : }
35 :
36 861 : out.WriteQName(mQName);
37 :
38 861 : out.Writer() //
39 861 : .Put16(static_cast<uint16_t>(GetType())) //
40 861 : .Put16(static_cast<uint16_t>(GetClass())) //
41 861 : .Put32(static_cast<uint32_t>(GetTtl())) //
42 : ;
43 :
44 861 : chip::Encoding::BigEndian::BufferWriter sizeOutput(out.Writer()); // copy to re-output size
45 861 : out.Put16(0); // dummy, will be replaced later
46 :
47 861 : if (!WriteData(out))
48 : {
49 37 : return false;
50 : }
51 824 : sizeOutput.Put16(static_cast<uint16_t>(out.Writer().Needed() - sizeOutput.Needed() - 2));
52 :
53 : // This MUST be final and separated out: record count is only updated on success.
54 824 : if (out.Fit())
55 : {
56 824 : switch (asType)
57 : {
58 36 : case ResourceType::kAdditional:
59 36 : hdr.SetAdditionalCount(static_cast<uint16_t>(hdr.GetAdditionalCount() + 1));
60 36 : break;
61 12 : case ResourceType::kAuthority:
62 12 : hdr.SetAuthorityCount(static_cast<uint16_t>(hdr.GetAuthorityCount() + 1));
63 12 : break;
64 776 : case ResourceType::kAnswer:
65 776 : hdr.SetAnswerCount(static_cast<uint16_t>(hdr.GetAnswerCount() + 1));
66 776 : break;
67 : }
68 : }
69 :
70 824 : return out.Fit();
71 : }
72 :
73 : } // namespace Minimal
74 : } // namespace mdns
|