1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-30 05:24:26 +01:00

Removed DNSResourceRecord. Records in DNS are now managed completely by DNS::Resource.

This commit is contained in:
Matias Fontanini
2014-01-21 22:56:53 -03:00
parent 6d7e06535a
commit c5404a6111
8 changed files with 236 additions and 702 deletions

View File

@@ -185,61 +185,40 @@ void DNS::add_query(const Query &query) {
);
}
void DNS::add_answer(const string &name, const DNSResourceRecord::info &info,
address_type ip)
{
uint32_t ip_int = ip;
add_answer(
name,
info,
(const uint8_t*)&ip_int,
sizeof(ip_int)
);
}
void DNS::add_answer(const string &name, const DNSResourceRecord::info &info,
address_v6_type ip)
{
add_answer(
name,
info,
ip.begin(),
address_v6_type::address_size
);
}
void DNS::add_answer(const std::string &name, const DNSResourceRecord::info &info,
const std::string &dname)
{
std::string parsed = encode_domain_name(dname);
add_answer(
name,
info,
(const uint8_t*)parsed.c_str(),
parsed.size()
);
}
void DNS::add_answer(const std::string &name, const DNSResourceRecord::info &info,
const uint8_t *data, uint32_t sz)
{
void DNS::add_answer(const Resource &resource) {
sections_type sections;
sections.push_back(std::make_pair(&authority_idx, authority_count()));
sections.push_back(std::make_pair(&additional_idx, additional_count()));
add_record(name, info, data, sz, sections);
add_record(resource, sections);
dns.answers = Endian::host_to_be<uint16_t>(
answers_count() + 1
);
}
void DNS::add_record(const std::string &name, const DNSResourceRecord::info &info,
const uint8_t *data, uint32_t sz, const sections_type &sections)
{
std::string buffer = encode_domain_name(name);
uint32_t offset = buffer.size() + sizeof(uint16_t) * 3 + sizeof(uint32_t) + sz,
void DNS::add_record(const Resource &resource, const sections_type &sections) {
// We need to check that the data provided is correct. Otherwise, the sections
// will end up being inconsistent.
IPv4Address v4_addr;
IPv6Address v6_addr;
std::string buffer = encode_domain_name(resource.dname()), encoded_data;
// By default the data size is the length of the data field.
uint32_t data_size = resource.data().size();
if(resource.type() == A) {
v4_addr = resource.data();
data_size = 4;
}
else if(resource.type() == AAAA) {
v6_addr = resource.data();
data_size = IPv6Address::address_size;
}
else if(contains_dname(resource.type())) {
encoded_data = encode_domain_name(resource.data());
data_size = encoded_data.size();
}
uint32_t offset = buffer.size() + sizeof(uint16_t) * 3 + sizeof(uint32_t) + data_size,
threshold = sections.empty() ? records_data.size() : *sections.front().first;
// Skip the preference field
if(info.type == MX) {
if(resource.type() == MX) {
offset += sizeof(uint16_t);
}
for(size_t i = 0; i < sections.size(); ++i) {
@@ -256,39 +235,45 @@ void DNS::add_record(const std::string &name, const DNSResourceRecord::info &inf
buffer.end(),
&records_data[threshold]
);
*(uint16_t*)ptr = Endian::host_to_be(info.type);
*(uint16_t*)ptr = Endian::host_to_be(resource.type());
ptr += sizeof(uint16_t);
*(uint16_t*)ptr = Endian::host_to_be(info.qclass);
*(uint16_t*)ptr = Endian::host_to_be(resource.query_class());
ptr += sizeof(uint16_t);
*(uint32_t*)ptr = Endian::host_to_be(info.ttl);
*(uint32_t*)ptr = Endian::host_to_be(resource.ttl());
ptr += sizeof(uint32_t);
*(uint16_t*)ptr = Endian::host_to_be<uint16_t>(sz + (info.type == MX ? 2 : 0));
*(uint16_t*)ptr = Endian::host_to_be<uint16_t>(
data_size + (resource.type() == MX ? 2 : 0)
);
ptr += sizeof(uint16_t);
if(info.type == MX) {
if(resource.type() == MX) {
ptr += sizeof(uint16_t);
}
std::copy(
data,
data + sz,
ptr
);
if(resource.type() == A) {
uint32_t ip_int = v4_addr;
std::memcpy(ptr, &ip_int, sizeof(ip_int));
}
else if(resource.type() == AAAA) {
std::copy(v6_addr.begin(), v6_addr.end(), ptr);
}
else if(!encoded_data.empty()) {
std::copy(encoded_data.begin(), encoded_data.end(), ptr);
}
else {
std::copy(resource.data().begin(), resource.data().end(), ptr);
}
}
void DNS::add_authority(const string &name, const DNSResourceRecord::info &info,
const uint8_t *data, uint32_t sz)
{
void DNS::add_authority(const Resource &resource) {
sections_type sections;
sections.push_back(std::make_pair(&additional_idx, additional_count()));
add_record(name, info, data, sz, sections);
add_record(resource, sections);
dns.authority = Endian::host_to_be<uint16_t>(
authority_count() + 1
);
}
void DNS::add_additional(const string &name, const DNSResourceRecord::info &info,
const uint8_t *data, uint32_t sz)
{
add_record(name, info, data, sz, sections_type());
void DNS::add_additional(const Resource &resource){
add_record(resource, sections_type());
dns.additional = Endian::host_to_be<uint16_t>(
additional_count() + 1
);

View File

@@ -1,212 +0,0 @@
/*
* Copyright (c) 2012, Matias Fontanini
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <cstring>
#include <stdexcept>
#include <memory>
#include <typeinfo>
#include "dns_record.h"
#include "endianness.h"
#include "exceptions.h"
namespace Tins {
bool contains_dname(uint16_t type) {
type = Endian::be_to_host(type);
return type == 1 || type == 2 ||
type == 5 || type == 6 ||
type == 12 || type == 15 ||
type == 28 || type == 41;
}
DNSResourceRecord::DNSResourceRecord(DNSRRImpl *impl,
const uint8_t *d, uint16_t len) : impl(impl)
{
if(d && len)
data.assign(d, d + len);
}
DNSResourceRecord::DNSResourceRecord(const uint8_t *buffer, uint32_t size)
{
const uint8_t *buffer_end = buffer + size;
Internals::smart_ptr<DNSRRImpl>::type tmp_impl;
if((*buffer & 0xc0)) {
uint16_t offset(*reinterpret_cast<const uint16_t*>(buffer));
offset = Endian::be_to_host(offset) & 0x3fff;
tmp_impl.reset(new OffsetedDNSRRImpl(Endian::host_to_be(offset)));
buffer += sizeof(uint16_t);
}
else {
const uint8_t *str_end(buffer);
while(str_end < buffer_end && *str_end)
str_end++;
if(str_end == buffer_end)
throw malformed_packet();
//str_end++;
tmp_impl.reset(new NamedDNSRRImpl(buffer, str_end));
buffer = ++str_end;
}
if(buffer + sizeof(info_) > buffer_end)
throw malformed_packet();
std::memcpy(&info_, buffer, sizeof(info_));
buffer += sizeof(info_);
if(buffer + sizeof(uint16_t) > buffer_end)
throw malformed_packet();
// Store the option size.
data.resize(
Endian::be_to_host(*reinterpret_cast<const uint16_t*>(buffer))
);
buffer += sizeof(uint16_t);
if(buffer + data.size() > buffer_end)
throw malformed_packet();
if(contains_dname(info_.type) || data.size() != sizeof(uint32_t))
std::copy(buffer, buffer + data.size(), data.begin());
else if(data.size() == sizeof(uint32_t))
*(uint32_t*)&data[0] = *(uint32_t*)buffer;
impl = tmp_impl.release();
}
DNSResourceRecord::DNSResourceRecord(const DNSResourceRecord &rhs)
: info_(rhs.info_), data(rhs.data), impl(rhs.clone_impl())
{
}
DNSResourceRecord& DNSResourceRecord::operator=(const DNSResourceRecord &rhs)
{
delete impl;
info_ = rhs.info_;
data = rhs.data;
impl = rhs.clone_impl();
return *this;
}
DNSResourceRecord::~DNSResourceRecord() {
delete impl;
}
uint32_t DNSResourceRecord::write(uint8_t *buffer) const {
const uint32_t sz(impl ? impl->do_write(buffer) : 0);
buffer += sz;
std::memcpy(buffer, &info_, sizeof(info_));
buffer += sizeof(info_);
*((uint16_t*)buffer) = Endian::host_to_be<uint16_t>(data.size());
buffer += sizeof(uint16_t);
std::copy(data.begin(), data.end(), buffer);
return sz + sizeof(info_) + sizeof(uint16_t) + data.size();
}
DNSRRImpl *DNSResourceRecord::clone_impl() const {
return impl ? impl->clone() : 0;
}
bool DNSResourceRecord::has_domain_name() const {
if(!impl)
throw std::bad_cast();
return dynamic_cast<NamedDNSRRImpl*>(impl) != 0;
}
const std::string *DNSResourceRecord::dname() const {
if(!impl)
throw std::bad_cast();
return dynamic_cast<NamedDNSRRImpl&>(*impl).dname_pointer();
}
uint16_t DNSResourceRecord::offset() const {
return dynamic_cast<OffsetedDNSRRImpl&>(*impl).offset();
}
size_t DNSResourceRecord::impl_size() const {
return impl ? impl->size() : 0;
}
uint32_t DNSResourceRecord::size() const {
return sizeof(info_) + data.size() + sizeof(uint16_t) + impl_size();
}
bool DNSResourceRecord::matches(const std::string &dname) const {
return impl ? impl->matches(dname) : false;
}
// OffsetedRecord
OffsetedDNSRRImpl::OffsetedDNSRRImpl(uint16_t off)
: offset_(off | Endian::host_to_be<uint16_t>(0xc000))
{
}
uint32_t OffsetedDNSRRImpl::do_write(uint8_t *buffer) const {
std::memcpy(buffer, &offset_, sizeof(offset_));
return sizeof(offset_);
}
uint32_t OffsetedDNSRRImpl::size() const {
return sizeof(offset_);
}
OffsetedDNSRRImpl *OffsetedDNSRRImpl::clone() const {
return new OffsetedDNSRRImpl(*this);
}
uint16_t OffsetedDNSRRImpl::offset() const {
return offset_;
}
// NamedRecord
NamedDNSRRImpl::NamedDNSRRImpl(const std::string &nm)
: name(nm)
{
}
uint32_t NamedDNSRRImpl::size() const {
return name.size() + 1;
}
uint32_t NamedDNSRRImpl::do_write(uint8_t *buffer) const {
buffer = std::copy(name.begin(), name.end(), buffer);
*buffer = 0;
return name.size() + 1;
}
const std::string *NamedDNSRRImpl::dname_pointer() const {
return &name;
}
bool NamedDNSRRImpl::matches(const std::string &dname) const {
return dname == name;
}
NamedDNSRRImpl *NamedDNSRRImpl::clone() const {
return new NamedDNSRRImpl(*this);
}
}