Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / lib / mdns / minimal / RecordData.cpp
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 "RecordData.h"
19
20 #include <inet/arpa-inet-compatibility.h>
21
22 namespace mdns {
23 namespace Minimal {
24
25 bool ParseTxtRecord(const BytesRange & data, TxtRecordDelegate * callback)
26 {
27     // FORMAT:
28     //   length-prefixed strings of the form "foo=bar" where = may be missing
29     const uint8_t * pos = data.Start();
30
31     while (data.Contains(pos))
32     {
33         uint8_t length = *pos;
34
35         if (!data.Contains(pos + length))
36         {
37             return false;
38         }
39         pos++;
40
41         // name=value string of size length
42         const uint8_t * equalPos = pos;
43         while ((*equalPos != '=') && ((equalPos - pos) < length))
44         {
45             equalPos++;
46         }
47
48         if (pos + length == equalPos)
49         {
50             callback->OnRecord(BytesRange(pos, equalPos), BytesRange());
51         }
52         else
53         {
54             callback->OnRecord(BytesRange(pos, equalPos), BytesRange(equalPos + 1, pos + length));
55         }
56
57         pos += length;
58     }
59
60     return pos == data.End();
61 }
62
63 bool SrvRecord::Parse(const BytesRange & data, const BytesRange & packet)
64 {
65     // FORMAT:
66     //   - priority
67     //   - weight
68     //   - port
69     //   - target
70     if (data.Size() < 7)
71     {
72         return false;
73     }
74
75     const uint8_t * p = data.Start();
76
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);
81
82     return true;
83 }
84
85 bool ParseARecord(const BytesRange & data, chip::Inet::IPAddress * addr)
86 {
87     if (data.Size() != 4)
88     {
89         return false;
90     }
91
92     addr->Addr[0] = 0;
93     addr->Addr[1] = 0;
94     addr->Addr[2] = htonl(0xFFFF);
95     addr->Addr[3] = htonl(chip::Encoding::BigEndian::Get32(data.Start()));
96
97     return true;
98 }
99
100 bool ParseAAAARecord(const BytesRange & data, chip::Inet::IPAddress * addr)
101 {
102     if (data.Size() != 16)
103     {
104         return false;
105     }
106     const uint8_t * p = data.Start();
107     chip::Inet::IPAddress::ReadAddress(p, *addr);
108     return true;
109 }
110
111 bool ParsePtrRecord(const BytesRange & data, const BytesRange & packet, SerializedQNameIterator * name)
112 {
113     if (data.Size() < 1)
114     {
115         return false;
116     }
117
118     *name = SerializedQNameIterator(packet, data.Start());
119
120     return true;
121 }
122
123 } // namespace Minimal
124 } // namespace mdns