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 "base/string_util.h" | 7 #include "base/string_util.h" |
8 #include "base/sys_byteorder.h" | 8 #include "base/sys_byteorder.h" |
9 #include "net/base/address_list.h" | 9 #include "net/base/address_list.h" |
10 #include "net/base/big_endian.h" | 10 #include "net/base/big_endian.h" |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 reader.ReadU16(&out->klass) && | 120 reader.ReadU16(&out->klass) && |
121 reader.ReadU32(&out->ttl) && | 121 reader.ReadU32(&out->ttl) && |
122 reader.ReadU16(&rdlen) && | 122 reader.ReadU16(&rdlen) && |
123 reader.ReadPiece(&out->rdata, rdlen)) { | 123 reader.ReadPiece(&out->rdata, rdlen)) { |
124 cur_ = reader.ptr(); | 124 cur_ = reader.ptr(); |
125 return true; | 125 return true; |
126 } | 126 } |
127 return false; | 127 return false; |
128 } | 128 } |
129 | 129 |
| 130 bool DnsRecordParser::SkipQuestion() { |
| 131 size_t consumed = ReadName(cur_, NULL); |
| 132 if (!consumed) |
| 133 return false; |
| 134 |
| 135 const char* next = cur_ + consumed + 2 * sizeof(uint16); // QTYPE + QCLASS |
| 136 if (next > packet_ + length_) |
| 137 return false; |
| 138 |
| 139 cur_ = next; |
| 140 |
| 141 return true; |
| 142 } |
| 143 |
130 DnsResponse::DnsResponse() | 144 DnsResponse::DnsResponse() |
131 : io_buffer_(new IOBufferWithSize(dns_protocol::kMaxUDPSize + 1)) { | 145 : io_buffer_(new IOBufferWithSize(dns_protocol::kMaxUDPSize + 1)) { |
132 } | 146 } |
133 | 147 |
134 DnsResponse::DnsResponse(size_t length) | 148 DnsResponse::DnsResponse(size_t length) |
135 : io_buffer_(new IOBufferWithSize(length)) { | 149 : io_buffer_(new IOBufferWithSize(length)) { |
136 } | 150 } |
137 | 151 |
138 DnsResponse::DnsResponse(const void* data, | 152 DnsResponse::DnsResponse(const void* data, |
139 size_t length, | 153 size_t length, |
(...skipping 28 matching lines...) Expand all Loading... |
168 return false; | 182 return false; |
169 } | 183 } |
170 | 184 |
171 // Construct the parser. | 185 // Construct the parser. |
172 parser_ = DnsRecordParser(io_buffer_->data(), | 186 parser_ = DnsRecordParser(io_buffer_->data(), |
173 nbytes, | 187 nbytes, |
174 hdr_size + question.size()); | 188 hdr_size + question.size()); |
175 return true; | 189 return true; |
176 } | 190 } |
177 | 191 |
| 192 bool DnsResponse::InitParseWithoutQuery(int nbytes) { |
| 193 if (nbytes >= io_buffer_->size()) |
| 194 return false; |
| 195 |
| 196 size_t hdr_size = sizeof(dns_protocol::Header); |
| 197 parser_ = DnsRecordParser( |
| 198 io_buffer_->data(), nbytes, hdr_size); |
| 199 |
| 200 unsigned qdcount = base::NetToHost16(header()->qdcount); |
| 201 for (unsigned i = 0; i < qdcount; ++i) { |
| 202 if (!parser_.SkipQuestion()) { |
| 203 parser_ = DnsRecordParser(); // Make parser invalid again. |
| 204 return false; |
| 205 } |
| 206 } |
| 207 |
| 208 return true; |
| 209 } |
| 210 |
178 bool DnsResponse::IsValid() const { | 211 bool DnsResponse::IsValid() const { |
179 return parser_.IsValid(); | 212 return parser_.IsValid(); |
180 } | 213 } |
181 | 214 |
182 uint16 DnsResponse::flags() const { | 215 uint16 DnsResponse::flags() const { |
183 DCHECK(parser_.IsValid()); | 216 DCHECK(parser_.IsValid()); |
184 return base::NetToHost16(header()->flags) & ~(dns_protocol::kRcodeMask); | 217 return base::NetToHost16(header()->flags) & ~(dns_protocol::kRcodeMask); |
185 } | 218 } |
186 | 219 |
187 uint8 DnsResponse::rcode() const { | 220 uint8 DnsResponse::rcode() const { |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 | 319 |
287 // getcanonname in eglibc returns the first owner name of an A or AAAA RR. | 320 // 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. | 321 // If the response passed all the checks so far, then |expected_name| is it. |
289 *addr_list = AddressList::CreateFromIPAddressList(ip_addresses, | 322 *addr_list = AddressList::CreateFromIPAddressList(ip_addresses, |
290 expected_name); | 323 expected_name); |
291 *ttl = base::TimeDelta::FromSeconds(ttl_sec); | 324 *ttl = base::TimeDelta::FromSeconds(ttl_sec); |
292 return DNS_PARSE_OK; | 325 return DNS_PARSE_OK; |
293 } | 326 } |
294 | 327 |
295 } // namespace net | 328 } // namespace net |
OLD | NEW |