Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(40)

Side by Side Diff: net/dns/dns_response.cc

Issue 14049018: Add simple non-response-based question parsing for mDNS passive listening (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 return false; 168 return false;
169 } 169 }
170 170
171 // Construct the parser. 171 // Construct the parser.
172 parser_ = DnsRecordParser(io_buffer_->data(), 172 parser_ = DnsRecordParser(io_buffer_->data(),
173 nbytes, 173 nbytes,
174 hdr_size + question.size()); 174 hdr_size + question.size());
175 return true; 175 return true;
176 } 176 }
177 177
178 bool DnsResponse::InitParse(int nbytes) {
179 if (nbytes >= io_buffer_->size())
180 return false;
181
182 // Construct the parser.
183 size_t response_offset = GetResponseOffset();
184 if (response_offset == 0)
185 return false;
186
187 parser_ = DnsRecordParser(
188 io_buffer_->data(), nbytes, response_offset);
189 return true;
190 }
191
178 bool DnsResponse::IsValid() const { 192 bool DnsResponse::IsValid() const {
179 return parser_.IsValid(); 193 return parser_.IsValid();
180 } 194 }
181 195
182 uint16 DnsResponse::flags() const { 196 uint16 DnsResponse::flags() const {
183 DCHECK(parser_.IsValid()); 197 DCHECK(parser_.IsValid());
184 return base::NetToHost16(header()->flags) & ~(dns_protocol::kRcodeMask); 198 return base::NetToHost16(header()->flags) & ~(dns_protocol::kRcodeMask);
185 } 199 }
186 200
187 uint8 DnsResponse::rcode() const { 201 uint8 DnsResponse::rcode() const {
(...skipping 22 matching lines...) Expand all
210 const size_t type_offset = parser_.GetOffset() - 2 * sizeof(uint16); 224 const size_t type_offset = parser_.GetOffset() - 2 * sizeof(uint16);
211 uint16 type; 225 uint16 type;
212 ReadBigEndian<uint16>(io_buffer_->data() + type_offset, &type); 226 ReadBigEndian<uint16>(io_buffer_->data() + type_offset, &type);
213 return type; 227 return type;
214 } 228 }
215 229
216 std::string DnsResponse::GetDottedName() const { 230 std::string DnsResponse::GetDottedName() const {
217 return DNSDomainToString(qname()); 231 return DNSDomainToString(qname());
218 } 232 }
219 233
234 size_t DnsResponse::GetResponseOffset() const {
szym 2013/04/17 22:11:12 Use DnsRecordParser instead. ReadName with |out| s
Noam Samuel 2013/04/18 19:03:17 Done.
235 uint16 qdcount = base::NetToHost16(header()->qdcount);
236 const char* data = io_buffer_->data();
237 size_t data_size = io_buffer_->size();
238 size_t pos = sizeof(dns_protocol::Header);
239
240 for (uint16 i = 0; i < qdcount; i++) {
241 while (pos < data_size && data[pos] != 0) {
242 unsigned txt_len = static_cast<unsigned>(data[pos]);
243 // Traversal and checks adapted from DNSDomainToString.
244
245 // Handling pointers, which have both high bits set and an extra
246 // byte trailing them to indicate a location in the buffer.
247 if (txt_len >= 192) {
248 pos += 2;
249 continue;
szym 2013/04/17 22:11:12 This is an error. At this point you should be done
Noam Samuel 2013/04/18 19:03:17 Made irrelevant. On 2013/04/17 22:11:12, szym wro
250 }
251
252 if (txt_len > 63) {
253 return 0;
254 }
255
256 pos += txt_len + 1;
257 }
258
259 pos += 5; // 1 for 0-length text, 2 for qtype, 2 for qclass
260 }
261
262 if (pos > data_size) return 0;
263
264 return pos;
265 }
266
220 DnsRecordParser DnsResponse::Parser() const { 267 DnsRecordParser DnsResponse::Parser() const {
221 DCHECK(parser_.IsValid()); 268 DCHECK(parser_.IsValid());
222 // Return a copy of the parser. 269 // Return a copy of the parser.
223 return parser_; 270 return parser_;
224 } 271 }
225 272
226 const dns_protocol::Header* DnsResponse::header() const { 273 const dns_protocol::Header* DnsResponse::header() const {
227 return reinterpret_cast<const dns_protocol::Header*>(io_buffer_->data()); 274 return reinterpret_cast<const dns_protocol::Header*>(io_buffer_->data());
228 } 275 }
229 276
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 333
287 // getcanonname in eglibc returns the first owner name of an A or AAAA RR. 334 // 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. 335 // If the response passed all the checks so far, then |expected_name| is it.
289 *addr_list = AddressList::CreateFromIPAddressList(ip_addresses, 336 *addr_list = AddressList::CreateFromIPAddressList(ip_addresses,
290 expected_name); 337 expected_name);
291 *ttl = base::TimeDelta::FromSeconds(ttl_sec); 338 *ttl = base::TimeDelta::FromSeconds(ttl_sec);
292 return DNS_PARSE_OK; 339 return DNS_PARSE_OK;
293 } 340 }
294 341
295 } // namespace net 342 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698