1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/dns/record_rdata.h"
#include "base/big_endian.h"
#include "net/base/ip_address_number.h"
#include "net/dns/dns_protocol.h"
#include "net/dns/dns_response.h"
namespace net {
static const size_t kSrvRecordMinimumSize = 6;
RecordRdata::RecordRdata() {
}
SrvRecordRdata::SrvRecordRdata() : priority_(0), weight_(0), port_(0) {
}
SrvRecordRdata::~SrvRecordRdata() {}
// static
scoped_ptr<SrvRecordRdata> SrvRecordRdata::Create(
const base::StringPiece& data,
const DnsRecordParser& parser) {
if (data.size() < kSrvRecordMinimumSize) return scoped_ptr<SrvRecordRdata>();
scoped_ptr<SrvRecordRdata> rdata(new SrvRecordRdata);
base::BigEndianReader reader(data.data(), data.size());
// 2 bytes for priority, 2 bytes for weight, 2 bytes for port.
reader.ReadU16(&rdata->priority_);
reader.ReadU16(&rdata->weight_);
reader.ReadU16(&rdata->port_);
if (!parser.ReadName(data.substr(kSrvRecordMinimumSize).begin(),
&rdata->target_))
return scoped_ptr<SrvRecordRdata>();
return rdata.Pass();
}
uint16_t SrvRecordRdata::Type() const {
return SrvRecordRdata::kType;
}
bool SrvRecordRdata::IsEqual(const RecordRdata* other) const {
if (other->Type() != Type()) return false;
const SrvRecordRdata* srv_other = static_cast<const SrvRecordRdata*>(other);
return weight_ == srv_other->weight_ &&
port_ == srv_other->port_ &&
priority_ == srv_other->priority_ &&
target_ == srv_other->target_;
}
ARecordRdata::ARecordRdata() {
}
ARecordRdata::~ARecordRdata() {
}
// static
scoped_ptr<ARecordRdata> ARecordRdata::Create(
const base::StringPiece& data,
const DnsRecordParser& parser) {
if (data.size() != kIPv4AddressSize)
return scoped_ptr<ARecordRdata>();
scoped_ptr<ARecordRdata> rdata(new ARecordRdata);
rdata->address_.resize(kIPv4AddressSize);
for (unsigned i = 0; i < kIPv4AddressSize; ++i) {
rdata->address_[i] = data[i];
}
return rdata.Pass();
}
uint16_t ARecordRdata::Type() const {
return ARecordRdata::kType;
}
bool ARecordRdata::IsEqual(const RecordRdata* other) const {
if (other->Type() != Type()) return false;
const ARecordRdata* a_other = static_cast<const ARecordRdata*>(other);
return address_ == a_other->address_;
}
AAAARecordRdata::AAAARecordRdata() {
}
AAAARecordRdata::~AAAARecordRdata() {
}
// static
scoped_ptr<AAAARecordRdata> AAAARecordRdata::Create(
const base::StringPiece& data,
const DnsRecordParser& parser) {
if (data.size() != kIPv6AddressSize)
return scoped_ptr<AAAARecordRdata>();
scoped_ptr<AAAARecordRdata> rdata(new AAAARecordRdata);
rdata->address_.resize(kIPv6AddressSize);
for (unsigned i = 0; i < kIPv6AddressSize; ++i) {
rdata->address_[i] = data[i];
}
return rdata.Pass();
}
uint16_t AAAARecordRdata::Type() const {
return AAAARecordRdata::kType;
}
bool AAAARecordRdata::IsEqual(const RecordRdata* other) const {
if (other->Type() != Type()) return false;
const AAAARecordRdata* a_other = static_cast<const AAAARecordRdata*>(other);
return address_ == a_other->address_;
}
CnameRecordRdata::CnameRecordRdata() {
}
CnameRecordRdata::~CnameRecordRdata() {
}
// static
scoped_ptr<CnameRecordRdata> CnameRecordRdata::Create(
const base::StringPiece& data,
const DnsRecordParser& parser) {
scoped_ptr<CnameRecordRdata> rdata(new CnameRecordRdata);
if (!parser.ReadName(data.begin(), &rdata->cname_))
return scoped_ptr<CnameRecordRdata>();
return rdata.Pass();
}
uint16_t CnameRecordRdata::Type() const {
return CnameRecordRdata::kType;
}
bool CnameRecordRdata::IsEqual(const RecordRdata* other) const {
if (other->Type() != Type()) return false;
const CnameRecordRdata* cname_other =
static_cast<const CnameRecordRdata*>(other);
return cname_ == cname_other->cname_;
}
PtrRecordRdata::PtrRecordRdata() {
}
PtrRecordRdata::~PtrRecordRdata() {
}
// static
scoped_ptr<PtrRecordRdata> PtrRecordRdata::Create(
const base::StringPiece& data,
const DnsRecordParser& parser) {
scoped_ptr<PtrRecordRdata> rdata(new PtrRecordRdata);
if (!parser.ReadName(data.begin(), &rdata->ptrdomain_))
return scoped_ptr<PtrRecordRdata>();
return rdata.Pass();
}
uint16_t PtrRecordRdata::Type() const {
return PtrRecordRdata::kType;
}
bool PtrRecordRdata::IsEqual(const RecordRdata* other) const {
if (other->Type() != Type()) return false;
const PtrRecordRdata* ptr_other = static_cast<const PtrRecordRdata*>(other);
return ptrdomain_ == ptr_other->ptrdomain_;
}
TxtRecordRdata::TxtRecordRdata() {
}
TxtRecordRdata::~TxtRecordRdata() {
}
// static
scoped_ptr<TxtRecordRdata> TxtRecordRdata::Create(
const base::StringPiece& data,
const DnsRecordParser& parser) {
scoped_ptr<TxtRecordRdata> rdata(new TxtRecordRdata);
for (size_t i = 0; i < data.size(); ) {
uint8_t length = data[i];
if (i + length >= data.size())
return scoped_ptr<TxtRecordRdata>();
rdata->texts_.push_back(data.substr(i + 1, length).as_string());
// Move to the next string.
i += length + 1;
}
return rdata.Pass();
}
uint16_t TxtRecordRdata::Type() const {
return TxtRecordRdata::kType;
}
bool TxtRecordRdata::IsEqual(const RecordRdata* other) const {
if (other->Type() != Type()) return false;
const TxtRecordRdata* txt_other = static_cast<const TxtRecordRdata*>(other);
return texts_ == txt_other->texts_;
}
NsecRecordRdata::NsecRecordRdata() {
}
NsecRecordRdata::~NsecRecordRdata() {
}
// static
scoped_ptr<NsecRecordRdata> NsecRecordRdata::Create(
const base::StringPiece& data,
const DnsRecordParser& parser) {
scoped_ptr<NsecRecordRdata> rdata(new NsecRecordRdata);
// Read the "next domain". This part for the NSEC record format is
// ignored for mDNS, since it has no semantic meaning.
unsigned next_domain_length = parser.ReadName(data.data(), NULL);
// If we did not succeed in getting the next domain or the data length
// is too short for reading the bitmap header, return.
if (next_domain_length == 0 || data.length() < next_domain_length + 2)
return scoped_ptr<NsecRecordRdata>();
struct BitmapHeader {
uint8_t block_number; // The block number should be zero.
uint8_t length; // Bitmap length in bytes. Between 1 and 32.
};
const BitmapHeader* header = reinterpret_cast<const BitmapHeader*>(
data.data() + next_domain_length);
// The block number must be zero in mDns-specific NSEC records. The bitmap
// length must be between 1 and 32.
if (header->block_number != 0 || header->length == 0 || header->length > 32)
return scoped_ptr<NsecRecordRdata>();
base::StringPiece bitmap_data = data.substr(next_domain_length + 2);
// Since we may only have one block, the data length must be exactly equal to
// the domain length plus bitmap size.
if (bitmap_data.length() != header->length)
return scoped_ptr<NsecRecordRdata>();
rdata->bitmap_.insert(rdata->bitmap_.begin(),
bitmap_data.begin(),
bitmap_data.end());
return rdata.Pass();
}
uint16_t NsecRecordRdata::Type() const {
return NsecRecordRdata::kType;
}
bool NsecRecordRdata::IsEqual(const RecordRdata* other) const {
if (other->Type() != Type())
return false;
const NsecRecordRdata* nsec_other =
static_cast<const NsecRecordRdata*>(other);
return bitmap_ == nsec_other->bitmap_;
}
bool NsecRecordRdata::GetBit(unsigned i) const {
unsigned byte_num = i/8;
if (bitmap_.size() < byte_num + 1)
return false;
unsigned bit_num = 7 - i % 8;
return (bitmap_[byte_num] & (1 << bit_num)) != 0;
}
} // namespace net
|