| 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_query.h" | 5 #include "net/dns/dns_query.h" | 
| 6 | 6 | 
| 7 #include <limits> | 7 #include <limits> | 
| 8 | 8 | 
| 9 #include "base/sys_byteorder.h" | 9 #include "base/sys_byteorder.h" | 
| 10 #include "net/base/big_endian.h" | 10 #include "net/base/big_endian.h" | 
| (...skipping 10 matching lines...) Expand all  Loading... | 
| 21 DnsQuery::DnsQuery(uint16 id, const base::StringPiece& qname, uint16 qtype) | 21 DnsQuery::DnsQuery(uint16 id, const base::StringPiece& qname, uint16 qtype) | 
| 22     : qname_size_(qname.size()) { | 22     : qname_size_(qname.size()) { | 
| 23   DCHECK(!DNSDomainToString(qname).empty()); | 23   DCHECK(!DNSDomainToString(qname).empty()); | 
| 24   // QNAME + QTYPE + QCLASS | 24   // QNAME + QTYPE + QCLASS | 
| 25   size_t question_size = qname_size_ + sizeof(uint16) + sizeof(uint16); | 25   size_t question_size = qname_size_ + sizeof(uint16) + sizeof(uint16); | 
| 26   io_buffer_ = new IOBufferWithSize(sizeof(dns_protocol::Header) + | 26   io_buffer_ = new IOBufferWithSize(sizeof(dns_protocol::Header) + | 
| 27                                     question_size); | 27                                     question_size); | 
| 28   dns_protocol::Header* header = | 28   dns_protocol::Header* header = | 
| 29       reinterpret_cast<dns_protocol::Header*>(io_buffer_->data()); | 29       reinterpret_cast<dns_protocol::Header*>(io_buffer_->data()); | 
| 30   memset(header, 0, sizeof(dns_protocol::Header)); | 30   memset(header, 0, sizeof(dns_protocol::Header)); | 
| 31   header->id = htons(id); | 31   header->id = base::HostToNet16(id); | 
| 32   header->flags = htons(dns_protocol::kFlagRD); | 32   header->flags = base::HostToNet16(dns_protocol::kFlagRD); | 
| 33   header->qdcount = htons(1); | 33   header->qdcount = base::HostToNet16(1); | 
| 34 | 34 | 
| 35   // Write question section after the header. | 35   // Write question section after the header. | 
| 36   BigEndianWriter writer(reinterpret_cast<char*>(header + 1), question_size); | 36   BigEndianWriter writer(reinterpret_cast<char*>(header + 1), question_size); | 
| 37   writer.WriteBytes(qname.data(), qname.size()); | 37   writer.WriteBytes(qname.data(), qname.size()); | 
| 38   writer.WriteU16(qtype); | 38   writer.WriteU16(qtype); | 
| 39   writer.WriteU16(dns_protocol::kClassIN); | 39   writer.WriteU16(dns_protocol::kClassIN); | 
| 40 } | 40 } | 
| 41 | 41 | 
| 42 DnsQuery::~DnsQuery() { | 42 DnsQuery::~DnsQuery() { | 
| 43 } | 43 } | 
| 44 | 44 | 
| 45 DnsQuery* DnsQuery::CloneWithNewId(uint16 id) const { | 45 DnsQuery* DnsQuery::CloneWithNewId(uint16 id) const { | 
| 46   return new DnsQuery(*this, id); | 46   return new DnsQuery(*this, id); | 
| 47 } | 47 } | 
| 48 | 48 | 
| 49 uint16 DnsQuery::id() const { | 49 uint16 DnsQuery::id() const { | 
| 50   const dns_protocol::Header* header = | 50   const dns_protocol::Header* header = | 
| 51       reinterpret_cast<const dns_protocol::Header*>(io_buffer_->data()); | 51       reinterpret_cast<const dns_protocol::Header*>(io_buffer_->data()); | 
| 52   return ntohs(header->id); | 52   return base::NetToHost16(header->id); | 
| 53 } | 53 } | 
| 54 | 54 | 
| 55 base::StringPiece DnsQuery::qname() const { | 55 base::StringPiece DnsQuery::qname() const { | 
| 56   return base::StringPiece(io_buffer_->data() + sizeof(dns_protocol::Header), | 56   return base::StringPiece(io_buffer_->data() + sizeof(dns_protocol::Header), | 
| 57                            qname_size_); | 57                            qname_size_); | 
| 58 } | 58 } | 
| 59 | 59 | 
| 60 uint16 DnsQuery::qtype() const { | 60 uint16 DnsQuery::qtype() const { | 
| 61   uint16 type; | 61   uint16 type; | 
| 62   ReadBigEndian<uint16>(io_buffer_->data() + | 62   ReadBigEndian<uint16>(io_buffer_->data() + | 
| 63                         sizeof(dns_protocol::Header) + | 63                         sizeof(dns_protocol::Header) + | 
| 64                         qname_size_, &type); | 64                         qname_size_, &type); | 
| 65   return type; | 65   return type; | 
| 66 } | 66 } | 
| 67 | 67 | 
| 68 base::StringPiece DnsQuery::question() const { | 68 base::StringPiece DnsQuery::question() const { | 
| 69   return base::StringPiece(io_buffer_->data() + sizeof(dns_protocol::Header), | 69   return base::StringPiece(io_buffer_->data() + sizeof(dns_protocol::Header), | 
| 70                            qname_size_ + sizeof(uint16) + sizeof(uint16)); | 70                            qname_size_ + sizeof(uint16) + sizeof(uint16)); | 
| 71 } | 71 } | 
| 72 | 72 | 
| 73 DnsQuery::DnsQuery(const DnsQuery& orig, uint16 id) { | 73 DnsQuery::DnsQuery(const DnsQuery& orig, uint16 id) { | 
| 74   qname_size_ = orig.qname_size_; | 74   qname_size_ = orig.qname_size_; | 
| 75   io_buffer_ = new IOBufferWithSize(orig.io_buffer()->size()); | 75   io_buffer_ = new IOBufferWithSize(orig.io_buffer()->size()); | 
| 76   memcpy(io_buffer_.get()->data(), orig.io_buffer()->data(), | 76   memcpy(io_buffer_.get()->data(), orig.io_buffer()->data(), | 
| 77          io_buffer_.get()->size()); | 77          io_buffer_.get()->size()); | 
| 78   dns_protocol::Header* header = | 78   dns_protocol::Header* header = | 
| 79       reinterpret_cast<dns_protocol::Header*>(io_buffer_->data()); | 79       reinterpret_cast<dns_protocol::Header*>(io_buffer_->data()); | 
| 80   header->id = htons(id); | 80   header->id = base::HostToNet16(id); | 
| 81 } | 81 } | 
| 82 | 82 | 
| 83 }  // namespace net | 83 }  // namespace net | 
| OLD | NEW | 
|---|