diff options
author | noamsml@chromium.org <noamsml@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-05-02 03:19:35 +0000 |
---|---|---|
committer | noamsml@chromium.org <noamsml@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-05-02 03:19:35 +0000 |
commit | 4e09d241315b862d4a44aaf7672479c352310b4e (patch) | |
tree | 9104698ef1721c6458156271e1ce91d8364cd303 /net/dns/record_rdata.cc | |
parent | cf77bcc0f8e7a526a0fc01903410b58fdeb2ae1c (diff) | |
download | chromium_src-4e09d241315b862d4a44aaf7672479c352310b4e.zip chromium_src-4e09d241315b862d4a44aaf7672479c352310b4e.tar.gz chromium_src-4e09d241315b862d4a44aaf7672479c352310b4e.tar.bz2 |
General utility for parsing DNS records
This is a utility for parsing and DNS records into structured data and a generic
container for DNS records of any type.
BUG=233821
Review URL: https://chromiumcodereview.appspot.com/14377002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@197798 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/dns/record_rdata.cc')
-rw-r--r-- | net/dns/record_rdata.cc | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/net/dns/record_rdata.cc b/net/dns/record_rdata.cc new file mode 100644 index 0000000..1de3a4c --- /dev/null +++ b/net/dns/record_rdata.cc @@ -0,0 +1,131 @@ +// Copyright (c) 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "net/dns/record_rdata.h" + +#include "net/base/big_endian.h" +#include "net/base/dns_util.h" +#include "net/dns/dns_protocol.h" +#include "net/dns/dns_response.h" + +namespace net { + +static const size_t kSrvRecordMinimumSize = 6; + +RecordRdata::RecordRdata() { +} + +SrvRecordRdata::SrvRecordRdata() : priority_(0), weight_(0), port_(0) { +} + +SrvRecordRdata::~SrvRecordRdata() {} + +// static +scoped_ptr<SrvRecordRdata> SrvRecordRdata::Create( + const base::StringPiece& data, + const DnsRecordParser& parser) { + if (data.size() < kSrvRecordMinimumSize) return scoped_ptr<SrvRecordRdata>(); + + scoped_ptr<SrvRecordRdata> rdata(new SrvRecordRdata); + + BigEndianReader reader(data.data(), data.size()); + // 2 bytes for priority, 2 bytes for weight, 2 bytes for port. + reader.ReadU16(&rdata->priority_); + reader.ReadU16(&rdata->weight_); + reader.ReadU16(&rdata->port_); + + if (!parser.ReadName(data.substr(kSrvRecordMinimumSize).begin(), + &rdata->target_)) + return scoped_ptr<SrvRecordRdata>(); + + return rdata.Pass(); +} + +ARecordRdata::ARecordRdata() { +} + +ARecordRdata::~ARecordRdata() { +} + +// static +scoped_ptr<ARecordRdata> ARecordRdata::Create( + const base::StringPiece& data, + const DnsRecordParser& parser) { + if (data.size() != kIPv4AddressSize) + return scoped_ptr<ARecordRdata>(); + + scoped_ptr<ARecordRdata> rdata(new ARecordRdata); + + rdata->address_.resize(kIPv4AddressSize); + for (unsigned i = 0; i < kIPv4AddressSize; ++i) { + rdata->address_[i] = data[i]; + } + + return rdata.Pass(); +} + +CnameRecordRdata::CnameRecordRdata() { +} + +CnameRecordRdata::~CnameRecordRdata() { +} + +// static +scoped_ptr<CnameRecordRdata> CnameRecordRdata::Create( + const base::StringPiece& data, + const DnsRecordParser& parser) { + scoped_ptr<CnameRecordRdata> rdata(new CnameRecordRdata); + + if (!parser.ReadName(data.begin(), &rdata->cname_)) + return scoped_ptr<CnameRecordRdata>(); + + return rdata.Pass(); +} + +PtrRecordRdata::PtrRecordRdata() { +} + +PtrRecordRdata::~PtrRecordRdata() { +} + +// static +scoped_ptr<PtrRecordRdata> PtrRecordRdata::Create( + const base::StringPiece& data, + const DnsRecordParser& parser) { + scoped_ptr<PtrRecordRdata> rdata(new PtrRecordRdata); + + if (!parser.ReadName(data.begin(), &rdata->ptrdomain_)) + return scoped_ptr<PtrRecordRdata>(); + + return rdata.Pass(); +} + +TxtRecordRdata::TxtRecordRdata() { +} + +TxtRecordRdata::~TxtRecordRdata() { +} + +// static +scoped_ptr<TxtRecordRdata> TxtRecordRdata::Create( + const base::StringPiece& data, + const DnsRecordParser& parser) { + scoped_ptr<TxtRecordRdata> rdata(new TxtRecordRdata); + + for (size_t i = 0; i < data.size(); ) { + uint8 length = data[i]; + + if (i + length >= data.size()) + return scoped_ptr<TxtRecordRdata>(); + + rdata->texts_.push_back(data.substr(i + 1, length).as_string()); + + // Move to the next string. + i += length + 1; + } + + return rdata.Pass(); +} + +} // namespace net |