Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/dns/dns_response.h" | 5 #include "net/dns/dns_response.h" |
| 6 | 6 |
| 7 #include <climits> | |
| 8 | |
| 7 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 8 #include "base/sys_byteorder.h" | 10 #include "base/sys_byteorder.h" |
| 9 #include "net/base/address_list.h" | 11 #include "net/base/address_list.h" |
| 10 #include "net/base/big_endian.h" | 12 #include "net/base/big_endian.h" |
| 11 #include "net/base/dns_util.h" | 13 #include "net/base/dns_util.h" |
| 12 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
| 13 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
| 14 #include "net/dns/dns_protocol.h" | 16 #include "net/dns/dns_protocol.h" |
| 15 #include "net/dns/dns_query.h" | 17 #include "net/dns/dns_query.h" |
| 16 | 18 |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 168 return false; | 170 return false; |
| 169 } | 171 } |
| 170 | 172 |
| 171 // Construct the parser. | 173 // Construct the parser. |
| 172 parser_ = DnsRecordParser(io_buffer_->data(), | 174 parser_ = DnsRecordParser(io_buffer_->data(), |
| 173 nbytes, | 175 nbytes, |
| 174 hdr_size + question.size()); | 176 hdr_size + question.size()); |
| 175 return true; | 177 return true; |
| 176 } | 178 } |
| 177 | 179 |
| 180 bool DnsResponse::InitParseNoQuery(int nbytes) { | |
|
gene
2013/04/16 21:55:22
nit: you may consider naming this InitParse() as w
Noam Samuel
2013/04/16 23:28:19
Done.
| |
| 181 if (nbytes >= io_buffer_->size()) | |
| 182 return false; | |
| 183 | |
| 184 const size_t hdr_size = sizeof(dns_protocol::Header); | |
| 185 | |
| 186 // Construct the parser. | |
| 187 parser_ = DnsRecordParser(io_buffer_->data(), | |
| 188 nbytes, | |
| 189 hdr_size + GetQuestionSectionSize()); | |
| 190 return true; | |
| 191 } | |
| 192 | |
| 178 bool DnsResponse::IsValid() const { | 193 bool DnsResponse::IsValid() const { |
| 179 return parser_.IsValid(); | 194 return parser_.IsValid(); |
| 180 } | 195 } |
| 181 | 196 |
| 182 uint16 DnsResponse::flags() const { | 197 uint16 DnsResponse::flags() const { |
| 183 DCHECK(parser_.IsValid()); | 198 DCHECK(parser_.IsValid()); |
| 184 return base::NetToHost16(header()->flags) & ~(dns_protocol::kRcodeMask); | 199 return base::NetToHost16(header()->flags) & ~(dns_protocol::kRcodeMask); |
| 185 } | 200 } |
| 186 | 201 |
| 187 uint8 DnsResponse::rcode() const { | 202 uint8 DnsResponse::rcode() const { |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 210 const size_t type_offset = parser_.GetOffset() - 2 * sizeof(uint16); | 225 const size_t type_offset = parser_.GetOffset() - 2 * sizeof(uint16); |
| 211 uint16 type; | 226 uint16 type; |
| 212 ReadBigEndian<uint16>(io_buffer_->data() + type_offset, &type); | 227 ReadBigEndian<uint16>(io_buffer_->data() + type_offset, &type); |
| 213 return type; | 228 return type; |
| 214 } | 229 } |
| 215 | 230 |
| 216 std::string DnsResponse::GetDottedName() const { | 231 std::string DnsResponse::GetDottedName() const { |
| 217 return DNSDomainToString(qname()); | 232 return DNSDomainToString(qname()); |
| 218 } | 233 } |
| 219 | 234 |
| 235 size_t DnsResponse::GetQuestionSectionSize() const { | |
|
gene
2013/04/16 21:55:22
nit: You may consider changing this function to Ge
Noam Samuel
2013/04/16 23:28:19
Done.
| |
| 236 uint16 qdcount = base::NetToHost16(header()->qdcount); | |
| 237 size_t hdr_size = sizeof(dns_protocol::Header); | |
| 238 const char* data = io_buffer_->data(); | |
| 239 size_t data_size = io_buffer_->size(); | |
| 240 size_t pos = hdr_size; | |
| 241 | |
| 242 for (uint16 i = 0; i < qdcount; i++) { | |
| 243 while (pos < data_size && data[pos]) { | |
|
gene
2013/04/16 21:55:22
data[pos] -> data[pos] != 0
you can make it more e
Noam Samuel
2013/04/16 23:28:19
Done.
| |
| 244 // Traversal and checks adapted from DNSDomainToString. | |
| 245 | |
| 246 // Handling pointers, which have both high bits set and an extra | |
| 247 // byte trailing them to indicate a location in the buffer. | |
| 248 if (static_cast<unsigned>(data[pos]) >= 192) { | |
|
gene
2013/04/16 21:55:22
could you please make a new variable
usigned txt_l
| |
| 249 pos+=2; | |
| 250 continue; | |
| 251 } | |
| 252 | |
| 253 #if CHAR_MIN < 0 | |
| 254 if (data[pos] < 0) | |
| 255 return 0; | |
| 256 #endif | |
| 257 if (data[pos] > 63) { | |
| 258 return 0; | |
| 259 } | |
| 260 | |
| 261 if (static_cast<int>(data[pos] + pos + 1 > data_size)) | |
|
gene
2013/04/16 21:55:22
why do you need static_cast here?
btw, you may mov
Noam Samuel
2013/04/16 23:28:19
Done.
| |
| 262 return 0; | |
| 263 | |
| 264 pos += data[pos]+1; | |
|
gene
2013/04/16 21:55:22
need spaces aroud +
Noam Samuel
2013/04/16 23:28:19
Done.
| |
| 265 } | |
| 266 | |
| 267 pos += 5; // 2 for qtype, 2 for qclass, 1 for null char | |
|
gene
2013/04/16 21:55:22
Comment is a bit misleading, I think it should be
Noam Samuel
2013/04/16 23:28:19
Done.
| |
| 268 } | |
| 269 return pos - hdr_size; | |
| 270 } | |
| 271 | |
| 220 DnsRecordParser DnsResponse::Parser() const { | 272 DnsRecordParser DnsResponse::Parser() const { |
| 221 DCHECK(parser_.IsValid()); | 273 DCHECK(parser_.IsValid()); |
| 222 // Return a copy of the parser. | 274 // Return a copy of the parser. |
| 223 return parser_; | 275 return parser_; |
| 224 } | 276 } |
| 225 | 277 |
| 226 const dns_protocol::Header* DnsResponse::header() const { | 278 const dns_protocol::Header* DnsResponse::header() const { |
| 227 return reinterpret_cast<const dns_protocol::Header*>(io_buffer_->data()); | 279 return reinterpret_cast<const dns_protocol::Header*>(io_buffer_->data()); |
| 228 } | 280 } |
| 229 | 281 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 286 | 338 |
| 287 // getcanonname in eglibc returns the first owner name of an A or AAAA RR. | 339 // getcanonname in eglibc returns the first owner name of an A or AAAA RR. |
| 288 // If the response passed all the checks so far, then |expected_name| is it. | 340 // If the response passed all the checks so far, then |expected_name| is it. |
| 289 *addr_list = AddressList::CreateFromIPAddressList(ip_addresses, | 341 *addr_list = AddressList::CreateFromIPAddressList(ip_addresses, |
| 290 expected_name); | 342 expected_name); |
| 291 *ttl = base::TimeDelta::FromSeconds(ttl_sec); | 343 *ttl = base::TimeDelta::FromSeconds(ttl_sec); |
| 292 return DNS_PARSE_OK; | 344 return DNS_PARSE_OK; |
| 293 } | 345 } |
| 294 | 346 |
| 295 } // namespace net | 347 } // namespace net |
| OLD | NEW |