mirror of
https://github.com/mfontanini/libtins
synced 2026-01-28 20:44:26 +01:00
Refactored DNS records.
This commit is contained in:
101
include/dns.h
101
include/dns.h
@@ -30,6 +30,7 @@
|
||||
#include <map>
|
||||
#include "pdu.h"
|
||||
#include "endianness.h"
|
||||
#include "dns_record.h"
|
||||
|
||||
namespace Tins {
|
||||
class IPv4Address;
|
||||
@@ -152,11 +153,6 @@ namespace Tins {
|
||||
*/
|
||||
DNS();
|
||||
|
||||
/**
|
||||
* \brief Destructor.
|
||||
*/
|
||||
~DNS();
|
||||
|
||||
/**
|
||||
* \brief Constructor which creates a DNS object from a buffer
|
||||
* and adds all identifiable PDUs found in the buffer as
|
||||
@@ -167,16 +163,6 @@ namespace Tins {
|
||||
*/
|
||||
DNS(const uint8_t *buffer, uint32_t total_sz);
|
||||
|
||||
/**
|
||||
* \brief Copy constructor.
|
||||
*/
|
||||
DNS(const DNS& rhs);
|
||||
|
||||
/**
|
||||
* \brief Copy assignment operator.
|
||||
*/
|
||||
DNS& operator=(const DNS& rhs);
|
||||
|
||||
// Getters
|
||||
|
||||
/**
|
||||
@@ -518,81 +504,12 @@ namespace Tins {
|
||||
authority, additional;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct ResourceRecord {
|
||||
struct Info {
|
||||
uint16_t type, qclass;
|
||||
uint32_t ttl;
|
||||
} __attribute__((packed)) info;
|
||||
|
||||
std::vector<uint8_t> data;
|
||||
|
||||
ResourceRecord(const uint8_t *d = 0, uint16_t len = 0) {
|
||||
if(d && len)
|
||||
data.assign(d, d + len);
|
||||
}
|
||||
|
||||
virtual ~ResourceRecord() {}
|
||||
virtual ResourceRecord *clone() const = 0;
|
||||
uint32_t write(uint8_t *buffer) const;
|
||||
virtual uint32_t do_write(uint8_t *buffer) const = 0;
|
||||
virtual uint32_t size() const = 0;
|
||||
virtual bool matches(const std::string &dname) { return false; }
|
||||
uint32_t data_size() const {
|
||||
return data.size();
|
||||
}
|
||||
const uint8_t *data_pointer() const {
|
||||
return &data[0];
|
||||
}
|
||||
virtual const std::string *dname_pointer() const { return 0; }
|
||||
};
|
||||
|
||||
struct OffsetedResourceRecord : public ResourceRecord {
|
||||
uint16_t offset;
|
||||
|
||||
OffsetedResourceRecord(uint16_t off, const uint8_t *data = 0, uint16_t len = 0)
|
||||
: ResourceRecord(data,len), offset(off | 0xc0) {}
|
||||
|
||||
uint32_t do_write(uint8_t *buffer) const {
|
||||
std::memcpy(buffer, &offset, sizeof(offset));
|
||||
return sizeof(offset);
|
||||
}
|
||||
uint32_t size() const {
|
||||
return sizeof(ResourceRecord::info) + sizeof(offset) + data.size() + sizeof(uint16_t);
|
||||
}
|
||||
ResourceRecord *clone() const;
|
||||
};
|
||||
|
||||
struct NamedResourceRecord : public ResourceRecord {
|
||||
std::string name;
|
||||
|
||||
NamedResourceRecord(const std::string &nm, const uint8_t *data = 0, uint16_t len = 0)
|
||||
: ResourceRecord(data,len), name(nm) {}
|
||||
|
||||
uint32_t do_write(uint8_t *buffer) const {
|
||||
std::memcpy(buffer, name.c_str(), name.size() + 1);
|
||||
return name.size() + 1;
|
||||
}
|
||||
|
||||
uint32_t size() const {
|
||||
return sizeof(ResourceRecord::info) + name.size() + 1 + data.size() + sizeof(uint16_t);
|
||||
}
|
||||
|
||||
bool matches(const std::string &dname) {
|
||||
return dname == name;
|
||||
}
|
||||
|
||||
const std::string *dname_pointer() const {
|
||||
return &name;
|
||||
}
|
||||
ResourceRecord *clone() const;
|
||||
};
|
||||
|
||||
typedef std::map<uint16_t, std::string> SuffixMap;
|
||||
typedef std::map<uint16_t, uint16_t> SuffixIndices;
|
||||
typedef std::list<ResourceRecord*> ResourcesType;
|
||||
typedef std::list<DNSResourceRecord> ResourcesType;
|
||||
typedef std::list<Query> QueriesType;
|
||||
|
||||
const uint8_t *build_resource_list(std::list<ResourceRecord*> &lst, const uint8_t *ptr, uint32_t &sz, uint16_t nrecs);
|
||||
const uint8_t *build_resource_list(ResourcesType &lst, const uint8_t *ptr, uint32_t &sz, uint16_t nrecs);
|
||||
uint32_t find_domain_name(const std::string &dname);
|
||||
bool find_domain_name(const std::string &dname, const ResourcesType &lst, uint16_t &out);
|
||||
void parse_domain_name(const std::string &dn, std::string &out) const;
|
||||
@@ -601,23 +518,19 @@ namespace Tins {
|
||||
uint8_t *serialize_list(const ResourcesType &lst, uint8_t *buffer) const;
|
||||
void compose_name(const uint8_t *ptr, uint32_t sz, std::string &out);
|
||||
void convert_resources(const ResourcesType &lst, std::list<Resource> &res);
|
||||
ResourceRecord *make_record(const std::string &name, QueryType type, QueryClass qclass, uint32_t ttl, uint32_t ip);
|
||||
ResourceRecord *make_record(const std::string &name, QueryType type, QueryClass qclass, uint32_t ttl, const std::string &dname);
|
||||
ResourceRecord *make_record(const std::string &name, QueryType type, QueryClass qclass, uint32_t ttl, const uint8_t *ptr, uint32_t len);
|
||||
DNSResourceRecord make_record(const std::string &name, QueryType type, QueryClass qclass, uint32_t ttl, uint32_t ip);
|
||||
DNSResourceRecord make_record(const std::string &name, QueryType type, QueryClass qclass, uint32_t ttl, const std::string &dname);
|
||||
DNSResourceRecord make_record(const std::string &name, QueryType type, QueryClass qclass, uint32_t ttl, const uint8_t *ptr, uint32_t len);
|
||||
void add_suffix(uint32_t index, const uint8_t *data, uint32_t sz);
|
||||
uint32_t build_suffix_map(uint32_t index, const ResourcesType &lst);
|
||||
uint32_t build_suffix_map(uint32_t index, const QueriesType &lst);
|
||||
void build_suffix_map();
|
||||
void copy_list(const ResourcesType &from, ResourcesType &to) const;
|
||||
bool contains_dname(uint16_t type);
|
||||
void free_list(ResourcesType &lst);
|
||||
|
||||
void copy_fields(const DNS *other);
|
||||
|
||||
dnshdr dns;
|
||||
uint32_t extra_size;
|
||||
std::list<Query> queries;
|
||||
std::list<ResourceRecord*> ans, arity, addit;
|
||||
ResourcesType ans, arity, addit;
|
||||
SuffixMap suffixes;
|
||||
SuffixIndices suffix_indices;
|
||||
};
|
||||
|
||||
231
include/dns_record.h
Normal file
231
include/dns_record.h
Normal file
@@ -0,0 +1,231 @@
|
||||
/*
|
||||
* libtins is a net packet wrapper library for crafting and
|
||||
* interpreting sniffed packets.
|
||||
*
|
||||
* Copyright (C) 2011 Nasel
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef TINS_DNS_RECORD_H
|
||||
#define TINS_DNS_RECORD_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <stdint.h>
|
||||
|
||||
namespace Tins {
|
||||
class DNSRRImpl;
|
||||
|
||||
/**
|
||||
* \brief Abstracts a DNS resource record.
|
||||
*/
|
||||
class DNSResourceRecord {
|
||||
public:
|
||||
/**
|
||||
* \brief The type used to store resource records' information.
|
||||
*/
|
||||
struct Info {
|
||||
uint16_t type, qclass;
|
||||
uint32_t ttl;
|
||||
} __attribute__((packed));
|
||||
|
||||
/**
|
||||
* \brief Constructs a record.
|
||||
* \param impl A pointer to the impl object.
|
||||
* \param data A pointer to the start of the data buffer.
|
||||
* \param len The length of the data.
|
||||
*/
|
||||
DNSResourceRecord(DNSRRImpl *impl = 0, const uint8_t *data = 0, uint16_t len = 0);
|
||||
|
||||
/**
|
||||
* \brief Constructs a record.
|
||||
* \param buffer A pointer to the start of the data buffer.
|
||||
* \param len The length of the data.
|
||||
*/
|
||||
DNSResourceRecord(const uint8_t *buffer, uint32_t size);
|
||||
|
||||
/**
|
||||
* \brief Constructs a record from an input range.
|
||||
* \param impl A pointer to the impl object.
|
||||
* \param start The begining of the range.
|
||||
* \param end The end of the range.
|
||||
*/
|
||||
template<typename ForwardIterator>
|
||||
DNSResourceRecord(DNSRRImpl *impl, ForwardIterator start, ForwardIterator end)
|
||||
: impl(impl), data(start, end)
|
||||
{ }
|
||||
|
||||
/**
|
||||
* \brief Copy constructor.
|
||||
*
|
||||
* This handles cloning the impl object.
|
||||
* \param rhs The record which will be copied.
|
||||
*/
|
||||
DNSResourceRecord(const DNSResourceRecord &rhs);
|
||||
|
||||
/**
|
||||
* \brief Copy assignment operator.
|
||||
*
|
||||
* This handles cloning the impl object.
|
||||
* \param rhs The record which will be copied.
|
||||
*/
|
||||
DNSResourceRecord& operator=(const DNSResourceRecord &rhs);
|
||||
|
||||
/**
|
||||
* \brief Destructor.
|
||||
*
|
||||
* This frees the impl object.
|
||||
*/
|
||||
~DNSResourceRecord();
|
||||
|
||||
/**
|
||||
* \brief Writes this record to a buffer.
|
||||
*
|
||||
* \param buffer The buffer in which to store the serialization.
|
||||
* \return uint32_t containing the number of bytes written.
|
||||
*/
|
||||
uint32_t write(uint8_t *buffer) const;
|
||||
|
||||
/**
|
||||
* \brief Returns the size of the data in this record.
|
||||
*/
|
||||
uint32_t data_size() const {
|
||||
return data.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Returns the pointer to the start of the data buffer.
|
||||
*/
|
||||
const uint8_t *data_ptr() const {
|
||||
return &data[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Returns a bool indicating whether this record contains
|
||||
* a domain name as the name being resolved.
|
||||
*/
|
||||
bool has_domain_name() const;
|
||||
|
||||
/**
|
||||
* \brief Returns a pointer to the domain name stored in this record.
|
||||
*
|
||||
* This will throw a std::bad_cast exception if the impl object is
|
||||
* not of the type NamedDNSRRImpl.
|
||||
*/
|
||||
const std::string *dname() const;
|
||||
|
||||
/**
|
||||
* \brief Returns the offset stored in this record.
|
||||
*
|
||||
* This will throw a std::bad_cast exception if the impl object is
|
||||
* not of the type OffsetedDNSRRImpl.
|
||||
*/
|
||||
uint16_t offset() const;
|
||||
|
||||
/**
|
||||
* \brief Returns the size of this record.
|
||||
*/
|
||||
uint32_t size() const;
|
||||
|
||||
/**
|
||||
* \brief Returns a reference to the info field.
|
||||
*/
|
||||
Info &info() {
|
||||
return info_;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Returns a const reference to the info field.
|
||||
*/
|
||||
const Info &info() const {
|
||||
return info_;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Checks if the domain name stored in this record matches
|
||||
* the given one.
|
||||
*
|
||||
* This is a shortcut
|
||||
*/
|
||||
bool matches(const std::string &dname) const;
|
||||
private:
|
||||
DNSRRImpl *clone_impl() const;
|
||||
size_t impl_size() const;
|
||||
|
||||
Info info_;
|
||||
std::vector<uint8_t> data;
|
||||
DNSRRImpl *impl;
|
||||
};
|
||||
|
||||
/**
|
||||
* \cond
|
||||
*/
|
||||
class DNSRRImpl {
|
||||
public:
|
||||
virtual ~DNSRRImpl() {}
|
||||
virtual uint32_t size() const = 0;
|
||||
virtual uint32_t do_write(uint8_t *buffer) const = 0;
|
||||
virtual bool matches(const std::string &dname) const { return false; }
|
||||
virtual DNSRRImpl *clone() const = 0;
|
||||
};
|
||||
|
||||
class OffsetedDNSRRImpl : public DNSRRImpl {
|
||||
public:
|
||||
OffsetedDNSRRImpl(uint16_t off);
|
||||
|
||||
uint32_t do_write(uint8_t *buffer) const;
|
||||
uint32_t size() const;
|
||||
OffsetedDNSRRImpl *clone() const;
|
||||
uint16_t offset() const;
|
||||
private:
|
||||
uint16_t offset_;
|
||||
};
|
||||
|
||||
class NamedDNSRRImpl : public DNSRRImpl {
|
||||
public:
|
||||
NamedDNSRRImpl(const std::string &nm);
|
||||
|
||||
template<typename ForwardIterator>
|
||||
NamedDNSRRImpl(ForwardIterator start, ForwardIterator end)
|
||||
: name(start, end)
|
||||
{ }
|
||||
|
||||
uint32_t do_write(uint8_t *buffer) const;
|
||||
|
||||
uint32_t size() const;
|
||||
|
||||
bool matches(const std::string &dname) const;
|
||||
|
||||
const std::string *dname_pointer() const;
|
||||
NamedDNSRRImpl *clone() const;
|
||||
private:
|
||||
std::string name;
|
||||
};
|
||||
|
||||
/**
|
||||
* \endcond
|
||||
*/
|
||||
|
||||
inline DNSResourceRecord make_offseted_record(uint16_t offset, const uint8_t *data = 0, uint32_t size = 0) {
|
||||
return DNSResourceRecord(new OffsetedDNSRRImpl(offset), data, size);
|
||||
}
|
||||
|
||||
inline DNSResourceRecord make_named_record(const std::string &name, const uint8_t *data = 0, uint32_t size = 0) {
|
||||
return DNSResourceRecord(new NamedDNSRRImpl(name), data, size);
|
||||
}
|
||||
}
|
||||
|
||||
#endif // TINS_DNS_RECORD_H
|
||||
Reference in New Issue
Block a user