This source file includes following definitions.
- CloneWithNewId
- id
- qname
- qtype
- question
- set_flags
#include "net/dns/dns_query.h"
#include <limits>
#include "base/big_endian.h"
#include "base/sys_byteorder.h"
#include "net/base/dns_util.h"
#include "net/base/io_buffer.h"
#include "net/dns/dns_protocol.h"
namespace net {
DnsQuery::DnsQuery(uint16 id, const base::StringPiece& qname, uint16 qtype)
: qname_size_(qname.size()) {
DCHECK(!DNSDomainToString(qname).empty());
size_t question_size = qname_size_ + sizeof(uint16) + sizeof(uint16);
io_buffer_ = new IOBufferWithSize(sizeof(dns_protocol::Header) +
question_size);
dns_protocol::Header* header =
reinterpret_cast<dns_protocol::Header*>(io_buffer_->data());
memset(header, 0, sizeof(dns_protocol::Header));
header->id = base::HostToNet16(id);
header->flags = base::HostToNet16(dns_protocol::kFlagRD);
header->qdcount = base::HostToNet16(1);
base::BigEndianWriter writer(reinterpret_cast<char*>(header + 1),
question_size);
writer.WriteBytes(qname.data(), qname.size());
writer.WriteU16(qtype);
writer.WriteU16(dns_protocol::kClassIN);
}
DnsQuery::~DnsQuery() {
}
DnsQuery* DnsQuery::CloneWithNewId(uint16 id) const {
return new DnsQuery(*this, id);
}
uint16 DnsQuery::id() const {
const dns_protocol::Header* header =
reinterpret_cast<const dns_protocol::Header*>(io_buffer_->data());
return base::NetToHost16(header->id);
}
base::StringPiece DnsQuery::qname() const {
return base::StringPiece(io_buffer_->data() + sizeof(dns_protocol::Header),
qname_size_);
}
uint16 DnsQuery::qtype() const {
uint16 type;
base::ReadBigEndian<uint16>(
io_buffer_->data() + sizeof(dns_protocol::Header) + qname_size_, &type);
return type;
}
base::StringPiece DnsQuery::question() const {
return base::StringPiece(io_buffer_->data() + sizeof(dns_protocol::Header),
qname_size_ + sizeof(uint16) + sizeof(uint16));
}
DnsQuery::DnsQuery(const DnsQuery& orig, uint16 id) {
qname_size_ = orig.qname_size_;
io_buffer_ = new IOBufferWithSize(orig.io_buffer()->size());
memcpy(io_buffer_.get()->data(), orig.io_buffer()->data(),
io_buffer_.get()->size());
dns_protocol::Header* header =
reinterpret_cast<dns_protocol::Header*>(io_buffer_->data());
header->id = base::HostToNet16(id);
}
void DnsQuery::set_flags(uint16 flags) {
dns_protocol::Header* header =
reinterpret_cast<dns_protocol::Header*>(io_buffer_->data());
header->flags = flags;
}
}