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 cur_ += consumed + 2 * sizeof(uint16); // QTYPE + QCLASS | |
135 if (static_cast<unsigned>(cur_ - packet_) > length_) | |
136 return false; | |
137 return true; | |
138 } | |
139 | |
130 DnsResponse::DnsResponse() | 140 DnsResponse::DnsResponse() |
131 : io_buffer_(new IOBufferWithSize(dns_protocol::kMaxUDPSize + 1)) { | 141 : io_buffer_(new IOBufferWithSize(dns_protocol::kMaxUDPSize + 1)) { |
132 } | 142 } |
133 | 143 |
134 DnsResponse::DnsResponse(size_t length) | 144 DnsResponse::DnsResponse(size_t length) |
135 : io_buffer_(new IOBufferWithSize(length)) { | 145 : io_buffer_(new IOBufferWithSize(length)) { |
136 } | 146 } |
137 | 147 |
138 DnsResponse::DnsResponse(const void* data, | 148 DnsResponse::DnsResponse(const void* data, |
139 size_t length, | 149 size_t length, |
(...skipping 28 matching lines...) Expand all Loading... | |
168 return false; | 178 return false; |
169 } | 179 } |
170 | 180 |
171 // Construct the parser. | 181 // Construct the parser. |
172 parser_ = DnsRecordParser(io_buffer_->data(), | 182 parser_ = DnsRecordParser(io_buffer_->data(), |
173 nbytes, | 183 nbytes, |
174 hdr_size + question.size()); | 184 hdr_size + question.size()); |
175 return true; | 185 return true; |
176 } | 186 } |
177 | 187 |
188 bool DnsResponse::InitParseWithoutQuery(int nbytes) { | |
189 if (nbytes >= io_buffer_->size()) | |
190 return false; | |
191 | |
192 size_t hdr_size = sizeof(dns_protocol::Header); | |
193 parser_ = DnsRecordParser( | |
194 io_buffer_->data(), nbytes, hdr_size); | |
195 | |
196 unsigned qdcount = base::NetToHost16(header()->qdcount); | |
197 for (unsigned i = 0; i < qdcount; ++i) { | |
198 if (!parser_.SkipQuestion()) { | |
199 parser_ = DnsRecordParser(); // Make parser invalid again | |
szym
2013/04/18 19:12:34
nit: '.' at end of sentence.
| |
200 return false; | |
201 } | |
202 } | |
203 | |
204 return true; | |
205 } | |
206 | |
178 bool DnsResponse::IsValid() const { | 207 bool DnsResponse::IsValid() const { |
179 return parser_.IsValid(); | 208 return parser_.IsValid(); |
180 } | 209 } |
181 | 210 |
182 uint16 DnsResponse::flags() const { | 211 uint16 DnsResponse::flags() const { |
183 DCHECK(parser_.IsValid()); | 212 DCHECK(parser_.IsValid()); |
184 return base::NetToHost16(header()->flags) & ~(dns_protocol::kRcodeMask); | 213 return base::NetToHost16(header()->flags) & ~(dns_protocol::kRcodeMask); |
185 } | 214 } |
186 | 215 |
187 uint8 DnsResponse::rcode() const { | 216 uint8 DnsResponse::rcode() const { |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
286 | 315 |
287 // getcanonname in eglibc returns the first owner name of an A or AAAA RR. | 316 // 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. | 317 // If the response passed all the checks so far, then |expected_name| is it. |
289 *addr_list = AddressList::CreateFromIPAddressList(ip_addresses, | 318 *addr_list = AddressList::CreateFromIPAddressList(ip_addresses, |
290 expected_name); | 319 expected_name); |
291 *ttl = base::TimeDelta::FromSeconds(ttl_sec); | 320 *ttl = base::TimeDelta::FromSeconds(ttl_sec); |
292 return DNS_PARSE_OK; | 321 return DNS_PARSE_OK; |
293 } | 322 } |
294 | 323 |
295 } // namespace net | 324 } // namespace net |
325 | |
szym
2013/04/18 19:12:34
Ditto.
Noam Samuel
2013/04/19 22:13:23
Done.
| |
OLD | NEW |