3 * Copyright (c) 2020 Project CHIP Authors
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "RecordData.h"
20 #include <inet/arpa-inet-compatibility.h>
25 bool ParseTxtRecord(const BytesRange & data, TxtRecordDelegate * callback)
28 // length-prefixed strings of the form "foo=bar" where = may be missing
29 const uint8_t * pos = data.Start();
31 while (data.Contains(pos))
33 uint8_t length = *pos;
35 if (!data.Contains(pos + length))
41 // name=value string of size length
42 const uint8_t * equalPos = pos;
43 while ((*equalPos != '=') && ((equalPos - pos) < length))
48 if (pos + length == equalPos)
50 callback->OnRecord(BytesRange(pos, equalPos), BytesRange());
54 callback->OnRecord(BytesRange(pos, equalPos), BytesRange(equalPos + 1, pos + length));
60 return pos == data.End();
63 bool SrvRecord::Parse(const BytesRange & data, const BytesRange & packet)
75 const uint8_t * p = data.Start();
77 mPriority = chip::Encoding::BigEndian::Read16(p);
78 mWeight = chip::Encoding::BigEndian::Read16(p);
79 mPort = chip::Encoding::BigEndian::Read16(p);
80 mName = SerializedQNameIterator(packet, p);
85 bool ParseARecord(const BytesRange & data, chip::Inet::IPAddress * addr)
94 addr->Addr[2] = htonl(0xFFFF);
95 addr->Addr[3] = htonl(chip::Encoding::BigEndian::Get32(data.Start()));
100 bool ParseAAAARecord(const BytesRange & data, chip::Inet::IPAddress * addr)
102 if (data.Size() != 16)
106 const uint8_t * p = data.Start();
107 chip::Inet::IPAddress::ReadAddress(p, *addr);
111 bool ParsePtrRecord(const BytesRange & data, const BytesRange & packet, SerializedQNameIterator * name)
118 *name = SerializedQNameIterator(packet, data.Start());
123 } // namespace Minimal