diff options
Diffstat (limited to 'chromium/net/dns/record_rdata.cc')
-rw-r--r-- | chromium/net/dns/record_rdata.cc | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/chromium/net/dns/record_rdata.cc b/chromium/net/dns/record_rdata.cc index ff9434b489e..9282b0ed2f7 100644 --- a/chromium/net/dns/record_rdata.cc +++ b/chromium/net/dns/record_rdata.cc @@ -4,6 +4,8 @@ #include "net/dns/record_rdata.h" +#include <numeric> + #include "base/big_endian.h" #include "net/dns/dns_protocol.h" #include "net/dns/dns_response.h" @@ -276,4 +278,66 @@ bool NsecRecordRdata::GetBit(unsigned i) const { return (bitmap_[byte_num] & (1 << bit_num)) != 0; } +OptRecordRdata::OptRecordRdata() {} + +OptRecordRdata::~OptRecordRdata() {} + +// static +std::unique_ptr<OptRecordRdata> OptRecordRdata::Create( + const base::StringPiece& data, + const DnsRecordParser& parser) { + std::unique_ptr<OptRecordRdata> rdata(new OptRecordRdata); + rdata->buf_.assign(data.begin(), data.end()); + + base::BigEndianReader reader(data.data(), data.size()); + while (reader.remaining() > 0) { + uint16_t opt_code, opt_data_size; + base::StringPiece opt_data; + + if (!(reader.ReadU16(&opt_code) && reader.ReadU16(&opt_data_size) && + reader.ReadPiece(&opt_data, opt_data_size))) { + return std::unique_ptr<OptRecordRdata>(); + } + rdata->opts_.push_back(Opt(opt_code, opt_data)); + } + + return rdata; +} + +uint16_t OptRecordRdata::Type() const { + return OptRecordRdata::kType; +} + +bool OptRecordRdata::IsEqual(const RecordRdata* other) const { + if (other->Type() != Type()) + return false; + const OptRecordRdata* opt_other = static_cast<const OptRecordRdata*>(other); + return opt_other->opts_ == opts_; +} + +void OptRecordRdata::AddOpt(const Opt& opt) { + base::StringPiece opt_data = opt.data(); + + // Resize buffer to accommodate new OPT. + const size_t orig_rdata_size = buf_.size(); + buf_.resize(orig_rdata_size + Opt::kHeaderSize + opt_data.size()); + + // Start writing from the end of the existing rdata. + base::BigEndianWriter writer(buf_.data() + orig_rdata_size, buf_.size()); + bool success = writer.WriteU16(opt.code()) && + writer.WriteU16(opt_data.size()) && + writer.WriteBytes(opt_data.data(), opt_data.size()); + DCHECK(success); + + opts_.push_back(opt); +} + +OptRecordRdata::Opt::Opt(uint16_t code, base::StringPiece data) : code_(code) { + data.CopyToString(&data_); +} + +bool OptRecordRdata::Opt::operator==(const OptRecordRdata::Opt& other) const { + return code_ == other.code_ && data_ == other.data_; +} + } // namespace net |