| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/base/dns_query.h" | 5 #include "net/base/dns_query.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <limits> |
| 8 | 9 |
| 9 #include "base/rand_util.h" | 10 #include "base/rand_util.h" |
| 10 #include "net/base/dns_util.h" | 11 #include "net/base/dns_util.h" |
| 11 | 12 |
| 12 namespace net { | 13 namespace net { |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 16 void PackUint16BE(char buf[2], uint16 v) { | 17 void PackUint16BE(char buf[2], uint16 v) { |
| 17 buf[0] = v >> 8; | 18 buf[0] = v >> 8; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 28 // For details, see RFC 1035 section 4.1.1. This header template sets RD | 29 // For details, see RFC 1035 section 4.1.1. This header template sets RD |
| 29 // bit, which directs the name server to pursue query recursively, and sets | 30 // bit, which directs the name server to pursue query recursively, and sets |
| 30 // the QDCOUNT to 1, meaning the question section has a single entry. The | 31 // the QDCOUNT to 1, meaning the question section has a single entry. The |
| 31 // first two bytes of the header form a 16-bit random query ID to be copied | 32 // first two bytes of the header form a 16-bit random query ID to be copied |
| 32 // in the corresponding reply by the name server -- randomized during | 33 // in the corresponding reply by the name server -- randomized during |
| 33 // DnsQuery construction. | 34 // DnsQuery construction. |
| 34 static const char kHeader[] = {0x00, 0x00, 0x01, 0x00, 0x00, 0x01, | 35 static const char kHeader[] = {0x00, 0x00, 0x01, 0x00, 0x00, 0x01, |
| 35 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; | 36 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 36 static const size_t kHeaderSize = arraysize(kHeader); | 37 static const size_t kHeaderSize = arraysize(kHeader); |
| 37 | 38 |
| 38 DnsQuery::DnsQuery(const std::string& dns_name, uint16 qtype, uint64 (*prng)()) | 39 DnsQuery::DnsQuery(const std::string& dns_name, |
| 40 uint16 qtype, |
| 41 const base::RandIntCallback& rand_int) |
| 39 : dns_name_size_(dns_name.size()), | 42 : dns_name_size_(dns_name.size()), |
| 40 prng_(prng) { | 43 rand_int_(rand_int) { |
| 41 DCHECK(DnsResponseBuffer(reinterpret_cast<const uint8*>(dns_name.c_str()), | 44 DCHECK(DnsResponseBuffer(reinterpret_cast<const uint8*>(dns_name.c_str()), |
| 42 dns_name.size()).DNSName(NULL)); | 45 dns_name.size()).DNSName(NULL)); |
| 43 DCHECK(qtype == kDNS_A || qtype == kDNS_AAAA); | 46 DCHECK(qtype == kDNS_A || qtype == kDNS_AAAA); |
| 44 | 47 |
| 45 io_buffer_ = new IOBufferWithSize(kHeaderSize + question_size()); | 48 io_buffer_ = new IOBufferWithSize(kHeaderSize + question_size()); |
| 46 | 49 |
| 47 int byte_offset = 0; | 50 int byte_offset = 0; |
| 48 char* buffer_head = io_buffer_->data(); | 51 char* buffer_head = io_buffer_->data(); |
| 49 memcpy(&buffer_head[byte_offset], kHeader, kHeaderSize); | 52 memcpy(&buffer_head[byte_offset], kHeader, kHeaderSize); |
| 50 byte_offset += kHeaderSize; | 53 byte_offset += kHeaderSize; |
| 51 memcpy(&buffer_head[byte_offset], &dns_name[0], dns_name_size_); | 54 memcpy(&buffer_head[byte_offset], &dns_name[0], dns_name_size_); |
| 52 byte_offset += dns_name_size_; | 55 byte_offset += dns_name_size_; |
| 53 PackUint16BE(&buffer_head[byte_offset], qtype); | 56 PackUint16BE(&buffer_head[byte_offset], qtype); |
| 54 byte_offset += sizeof(qtype); | 57 byte_offset += sizeof(qtype); |
| 55 PackUint16BE(&buffer_head[byte_offset], kClassIN); | 58 PackUint16BE(&buffer_head[byte_offset], kClassIN); |
| 56 RandomizeId(); | 59 RandomizeId(); |
| 57 } | 60 } |
| 58 | 61 |
| 59 DnsQuery::DnsQuery(const DnsQuery& rhs) | 62 DnsQuery::DnsQuery(const DnsQuery& rhs) |
| 60 : dns_name_size_(rhs.dns_name_size_), | 63 : dns_name_size_(rhs.dns_name_size_), |
| 61 prng_(rhs.prng_) { | 64 rand_int_(rhs.rand_int_) { |
| 62 io_buffer_ = new IOBufferWithSize(rhs.io_buffer()->size()); | 65 io_buffer_ = new IOBufferWithSize(rhs.io_buffer()->size()); |
| 63 memcpy(io_buffer_->data(), rhs.io_buffer()->data(), rhs.io_buffer()->size()); | 66 memcpy(io_buffer_->data(), rhs.io_buffer()->data(), rhs.io_buffer()->size()); |
| 64 RandomizeId(); | 67 RandomizeId(); |
| 65 } | 68 } |
| 66 | 69 |
| 67 DnsQuery::~DnsQuery() { | 70 DnsQuery::~DnsQuery() { |
| 68 } | 71 } |
| 69 | 72 |
| 70 uint16 DnsQuery::id() const { | 73 uint16 DnsQuery::id() const { |
| 71 return UnpackUint16BE(&io_buffer_->data()[0]); | 74 return UnpackUint16BE(&io_buffer_->data()[0]); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 83 return dns_name_size_ // QNAME | 86 return dns_name_size_ // QNAME |
| 84 + sizeof(uint16) // QTYPE | 87 + sizeof(uint16) // QTYPE |
| 85 + sizeof(uint16); // QCLASS | 88 + sizeof(uint16); // QCLASS |
| 86 } | 89 } |
| 87 | 90 |
| 88 const char* DnsQuery::question_data() const { | 91 const char* DnsQuery::question_data() const { |
| 89 return &io_buffer_->data()[kHeaderSize]; | 92 return &io_buffer_->data()[kHeaderSize]; |
| 90 } | 93 } |
| 91 | 94 |
| 92 void DnsQuery::RandomizeId() { | 95 void DnsQuery::RandomizeId() { |
| 93 PackUint16BE(&io_buffer_->data()[0], (*prng_)() & 0xffff); | 96 int min = std::numeric_limits<uint16>::min(); |
| 97 int max = std::numeric_limits<uint16>::max(); |
| 98 PackUint16BE(&io_buffer_->data()[0], rand_int_.Run(min, max)); |
| 94 } | 99 } |
| 95 | 100 |
| 96 } // namespace net | 101 } // namespace net |
| OLD | NEW |