mirror of
https://github.com/mfontanini/libtins
synced 2026-01-23 02:35:57 +01:00
Refactored DNS and UDP class.
This commit is contained in:
@@ -24,6 +24,7 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include <list>
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <map>
|
||||
@@ -134,6 +135,11 @@ 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
|
||||
@@ -146,15 +152,13 @@ namespace Tins {
|
||||
|
||||
/**
|
||||
* \brief Copy constructor.
|
||||
*
|
||||
* \param other The DNS object to be copied.
|
||||
*/
|
||||
DNS(const DNS &other);
|
||||
DNS(const DNS& rhs);
|
||||
|
||||
/**
|
||||
* \brief Destructor.
|
||||
* \brief Copy assignment operator.
|
||||
*/
|
||||
~DNS();
|
||||
DNS& operator=(const DNS& rhs);
|
||||
|
||||
// Getters
|
||||
|
||||
@@ -460,11 +464,11 @@ namespace Tins {
|
||||
std::list<Resource> dns_answers();
|
||||
|
||||
/**
|
||||
* \brief Clones this PDU.
|
||||
*
|
||||
* \sa PDU::clone_pdu
|
||||
*/
|
||||
PDU *clone_pdu() const;
|
||||
PDU *clone_pdu() const {
|
||||
return do_clone_pdu<DNS>();
|
||||
}
|
||||
private:
|
||||
struct dnshdr {
|
||||
uint16_t id;
|
||||
@@ -489,28 +493,24 @@ namespace Tins {
|
||||
uint32_t ttl;
|
||||
} __attribute__((packed)) info;
|
||||
|
||||
uint8_t *data;
|
||||
uint16_t data_sz;
|
||||
std::vector<uint8_t> data;
|
||||
|
||||
ResourceRecord(const uint8_t *d = 0, uint16_t len = 0) : data_sz(len) {
|
||||
if(d && len) {
|
||||
data = new uint8_t[len];
|
||||
std::memcpy(data, d, data_sz);
|
||||
}
|
||||
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;
|
||||
void copy_fields(ResourceRecord *other) const;
|
||||
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_sz;
|
||||
return data.size();
|
||||
}
|
||||
const uint8_t *data_pointer() const {
|
||||
return data;
|
||||
return &data[0];
|
||||
}
|
||||
virtual const std::string *dname_pointer() const { return 0; }
|
||||
};
|
||||
@@ -518,27 +518,33 @@ namespace Tins {
|
||||
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) {}
|
||||
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_sz + sizeof(uint16_t); }
|
||||
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) {}
|
||||
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_sz + sizeof(uint16_t); }
|
||||
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;
|
||||
@@ -552,26 +558,28 @@ namespace Tins {
|
||||
|
||||
typedef std::map<uint16_t, std::string> SuffixMap;
|
||||
typedef std::map<uint16_t, uint16_t> SuffixIndices;
|
||||
typedef std::list<ResourceRecord*> 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);
|
||||
uint32_t find_domain_name(const std::string &dname);
|
||||
bool find_domain_name(const std::string &dname, const std::list<ResourceRecord*> &lst, uint16_t &out);
|
||||
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;
|
||||
void unparse_domain_name(const std::string &dn, std::string &out) const;
|
||||
void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent);
|
||||
void free_list(std::list<ResourceRecord*> &lst);
|
||||
uint8_t *serialize_list(const std::list<ResourceRecord*> &lst, uint8_t *buffer) const;
|
||||
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 std::list<ResourceRecord*> &lst, std::list<Resource> &res);
|
||||
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);
|
||||
void add_suffix(uint32_t index, const uint8_t *data, uint32_t sz);
|
||||
uint32_t build_suffix_map(uint32_t index, const std::list<ResourceRecord*> &lst);
|
||||
uint32_t build_suffix_map(uint32_t index, const std::list<Query> &lst);
|
||||
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 std::list<ResourceRecord*> &from, std::list<ResourceRecord*> &to) const;
|
||||
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);
|
||||
|
||||
|
||||
@@ -54,33 +54,23 @@ namespace Tins {
|
||||
*/
|
||||
UDP(const uint8_t *buffer, uint32_t total_sz);
|
||||
|
||||
/**
|
||||
* \brief Copy constructor.
|
||||
*/
|
||||
UDP(const UDP &other);
|
||||
|
||||
/**
|
||||
* \brief Copy assignment operator.
|
||||
*/
|
||||
UDP &operator= (const UDP& other);
|
||||
|
||||
/**
|
||||
* \brief Getter for the destination port.
|
||||
* \return The datagram's destination port.
|
||||
*/
|
||||
inline uint16_t dport() const { return Utils::net_to_host_s(_udp.dport); }
|
||||
uint16_t dport() const { return Utils::net_to_host_s(_udp.dport); }
|
||||
|
||||
/**
|
||||
* \brief Getter for the source port.
|
||||
* \return The datagram's source port.
|
||||
*/
|
||||
inline uint16_t sport() const { return Utils::net_to_host_s(_udp.sport); }
|
||||
uint16_t sport() const { return Utils::net_to_host_s(_udp.sport); }
|
||||
|
||||
/**
|
||||
* \brief Getter for the length of the datagram.
|
||||
* \return The length of the datagram.
|
||||
*/
|
||||
inline uint16_t length() const { return Utils::net_to_host_s(_udp.len); }
|
||||
uint16_t length() const { return Utils::net_to_host_s(_udp.len); }
|
||||
|
||||
/**
|
||||
* \brief Set the destination port.
|
||||
@@ -126,11 +116,11 @@ namespace Tins {
|
||||
PDUType pdu_type() const { return PDU::UDP; }
|
||||
|
||||
/**
|
||||
* \brief Clones this PDU.
|
||||
*
|
||||
* \sa PDU::clone_pdu
|
||||
*/
|
||||
PDU *clone_pdu() const;
|
||||
PDU *clone_pdu() const {
|
||||
return do_clone_pdu<UDP>();
|
||||
}
|
||||
private:
|
||||
struct udphdr {
|
||||
uint16_t sport;
|
||||
|
||||
199
src/dns.cpp
199
src/dns.cpp
@@ -22,17 +22,19 @@
|
||||
#include <utility>
|
||||
#include <stdexcept>
|
||||
#include <cassert>
|
||||
#include <memory>
|
||||
#include "dns.h"
|
||||
|
||||
using std::string;
|
||||
using std::list;
|
||||
|
||||
namespace Tins {
|
||||
|
||||
Tins::DNS::DNS() : PDU(255), extra_size(0) {
|
||||
DNS::DNS() : PDU(255), extra_size(0) {
|
||||
std::memset(&dns, 0, sizeof(dns));
|
||||
}
|
||||
|
||||
Tins::DNS::DNS(const uint8_t *buffer, uint32_t total_sz) : PDU(255), extra_size(0) {
|
||||
DNS::DNS(const uint8_t *buffer, uint32_t total_sz) : PDU(255), extra_size(0) {
|
||||
if(total_sz < sizeof(dnshdr))
|
||||
throw std::runtime_error("Not enough size for a DNS header in the buffer.");
|
||||
std::memcpy(&dns, buffer, sizeof(dnshdr));
|
||||
@@ -61,23 +63,43 @@ Tins::DNS::DNS(const uint8_t *buffer, uint32_t total_sz) : PDU(255), extra_size(
|
||||
build_resource_list(addit, buffer, total_sz, additional());
|
||||
}
|
||||
|
||||
Tins::DNS::DNS(const DNS &other) : PDU(255) {
|
||||
copy_fields(&other);
|
||||
copy_inner_pdu(other);
|
||||
DNS::DNS(const DNS& rhs) : PDU(rhs) {
|
||||
copy_fields(&rhs);
|
||||
}
|
||||
|
||||
const uint8_t *Tins::DNS::build_resource_list(list<ResourceRecord*> &lst, const uint8_t *ptr, uint32_t &sz, uint16_t nrecs) {
|
||||
DNS& DNS::operator=(const DNS& rhs) {
|
||||
free_list(ans);
|
||||
free_list(arity);
|
||||
free_list(addit);
|
||||
copy_fields(&rhs);
|
||||
return *this;
|
||||
}
|
||||
|
||||
DNS::~DNS() {
|
||||
free_list(ans);
|
||||
free_list(arity);
|
||||
free_list(addit);
|
||||
}
|
||||
|
||||
void DNS::free_list(ResourcesType &lst) {
|
||||
while(lst.size()) {
|
||||
delete lst.front();
|
||||
lst.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
const uint8_t *DNS::build_resource_list(list<ResourceRecord*> &lst, const uint8_t *ptr, uint32_t &sz, uint16_t nrecs) {
|
||||
const uint8_t *ptr_end(ptr + sz);
|
||||
const uint8_t *parse_start(ptr);
|
||||
for(uint16_t i(0); i < nrecs; ++i) {
|
||||
const uint8_t *this_opt_start(ptr);
|
||||
if(ptr + sizeof(uint16_t) > ptr_end)
|
||||
throw std::runtime_error("Not enough size for a given resource.");
|
||||
ResourceRecord *res;
|
||||
std::auto_ptr<ResourceRecord> res;
|
||||
if((*ptr & 0xc0)) {
|
||||
uint16_t offset(*reinterpret_cast<const uint16_t*>(ptr));
|
||||
offset = Utils::net_to_host_s(offset) & 0x3fff;
|
||||
res = new OffsetedResourceRecord(Utils::net_to_host_s(offset));
|
||||
res.reset(new OffsetedResourceRecord(Utils::net_to_host_s(offset)));
|
||||
ptr += sizeof(uint16_t);
|
||||
}
|
||||
else {
|
||||
@@ -87,7 +109,7 @@ const uint8_t *Tins::DNS::build_resource_list(list<ResourceRecord*> &lst, const
|
||||
if(str_end == end)
|
||||
throw std::runtime_error("Not enough size for a resource domain name.");
|
||||
str_end++;
|
||||
res = new NamedResourceRecord(string(ptr, str_end));
|
||||
res.reset(new NamedResourceRecord(string(ptr, str_end)));
|
||||
ptr = str_end;
|
||||
}
|
||||
if(ptr + sizeof(res->info) > ptr_end)
|
||||
@@ -96,95 +118,81 @@ const uint8_t *Tins::DNS::build_resource_list(list<ResourceRecord*> &lst, const
|
||||
ptr += sizeof(res->info);
|
||||
if(ptr + sizeof(uint16_t) > ptr_end)
|
||||
throw std::runtime_error("Not enough size for resource data size.");
|
||||
res->data_sz = Utils::net_to_host_s(
|
||||
*reinterpret_cast<const uint16_t*>(ptr)
|
||||
|
||||
// Store the option size.
|
||||
res->data.resize(
|
||||
Utils::net_to_host_s(*reinterpret_cast<const uint16_t*>(ptr))
|
||||
);
|
||||
ptr += sizeof(uint16_t);
|
||||
if(ptr + res->data_sz > ptr_end)
|
||||
if(ptr + res->data.size() > ptr_end)
|
||||
throw std::runtime_error("Not enough size for resource data");
|
||||
res->data = new uint8_t[res->data_sz];
|
||||
if(contains_dname(res->info.type))
|
||||
std::memcpy(res->data, ptr, res->data_sz);
|
||||
else {
|
||||
*(uint32_t*)res->data = Utils::net_to_host_l(*(uint32_t*)ptr);
|
||||
}
|
||||
std::copy(ptr, ptr + res->data.size(), res->data.begin());
|
||||
else
|
||||
*(uint32_t*)&res->data[0] = Utils::net_to_host_l(*(uint32_t*)ptr);
|
||||
|
||||
ptr += res->data_sz;
|
||||
ptr += res->data.size();
|
||||
extra_size += ptr - this_opt_start;
|
||||
lst.push_back(res);
|
||||
lst.push_back(res.release());
|
||||
}
|
||||
sz -= ptr - parse_start;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
Tins::DNS::~DNS() {
|
||||
free_list(ans);
|
||||
free_list(arity);
|
||||
free_list(addit);
|
||||
}
|
||||
|
||||
void Tins::DNS::free_list(std::list<ResourceRecord*> &lst) {
|
||||
while(lst.size()) {
|
||||
delete[] lst.front()->data;
|
||||
delete lst.front();
|
||||
lst.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t Tins::DNS::header_size() const {
|
||||
uint32_t DNS::header_size() const {
|
||||
return sizeof(dns) + extra_size;
|
||||
}
|
||||
|
||||
void Tins::DNS::id(uint16_t new_id) {
|
||||
void DNS::id(uint16_t new_id) {
|
||||
dns.id = Utils::net_to_host_s(new_id);
|
||||
}
|
||||
|
||||
void Tins::DNS::type(QRType new_qr) {
|
||||
void DNS::type(QRType new_qr) {
|
||||
dns.qr = new_qr;
|
||||
}
|
||||
|
||||
void Tins::DNS::opcode(uint8_t new_opcode) {
|
||||
void DNS::opcode(uint8_t new_opcode) {
|
||||
dns.opcode = new_opcode;
|
||||
}
|
||||
|
||||
void Tins::DNS::authoritative_answer(uint8_t new_aa) {
|
||||
void DNS::authoritative_answer(uint8_t new_aa) {
|
||||
dns.aa = new_aa;
|
||||
}
|
||||
|
||||
void Tins::DNS::truncated(uint8_t new_tc) {
|
||||
void DNS::truncated(uint8_t new_tc) {
|
||||
dns.tc = new_tc;
|
||||
}
|
||||
|
||||
void Tins::DNS::recursion_desired(uint8_t new_rd) {
|
||||
void DNS::recursion_desired(uint8_t new_rd) {
|
||||
dns.rd = new_rd;
|
||||
}
|
||||
|
||||
void Tins::DNS::recursion_available(uint8_t new_ra) {
|
||||
void DNS::recursion_available(uint8_t new_ra) {
|
||||
dns.ra = new_ra;
|
||||
}
|
||||
|
||||
void Tins::DNS::z(uint8_t new_z) {
|
||||
void DNS::z(uint8_t new_z) {
|
||||
dns.z = new_z;
|
||||
}
|
||||
|
||||
void Tins::DNS::authenticated_data(uint8_t new_ad) {
|
||||
void DNS::authenticated_data(uint8_t new_ad) {
|
||||
dns.ad = new_ad;
|
||||
}
|
||||
|
||||
void Tins::DNS::checking_disabled(uint8_t new_cd) {
|
||||
void DNS::checking_disabled(uint8_t new_cd) {
|
||||
dns.cd = new_cd;
|
||||
}
|
||||
|
||||
void Tins::DNS::rcode(uint8_t new_rcode) {
|
||||
void DNS::rcode(uint8_t new_rcode) {
|
||||
dns.rcode = new_rcode;
|
||||
}
|
||||
|
||||
bool Tins::DNS::contains_dname(uint16_t type) {
|
||||
bool DNS::contains_dname(uint16_t type) {
|
||||
return type == Utils::net_to_host_s(MX) || type == Utils::net_to_host_s(CNAME) ||
|
||||
type == Utils::net_to_host_s(PTR) || type == Utils::net_to_host_s(NS);
|
||||
}
|
||||
|
||||
void Tins::DNS::add_query(const string &name, QueryType type, QueryClass qclass) {
|
||||
void DNS::add_query(const string &name, QueryType type, QueryClass qclass) {
|
||||
string new_str;
|
||||
parse_domain_name(name, new_str);
|
||||
|
||||
@@ -197,7 +205,7 @@ void Tins::DNS::add_query(const string &name, QueryType type, QueryClass qclass)
|
||||
dns.questions = Utils::net_to_host_s(queries.size());
|
||||
}
|
||||
|
||||
void Tins::DNS::add_query(const Query &query) {
|
||||
void DNS::add_query(const Query &query) {
|
||||
add_query(
|
||||
query.name,
|
||||
static_cast<QueryType>(query.type),
|
||||
@@ -205,13 +213,13 @@ void Tins::DNS::add_query(const Query &query) {
|
||||
);
|
||||
}
|
||||
|
||||
void Tins::DNS::add_answer(const string &name, QueryType type, QueryClass qclass, uint32_t ttl, uint32_t ip) {
|
||||
void DNS::add_answer(const string &name, QueryType type, QueryClass qclass, uint32_t ttl, uint32_t ip) {
|
||||
ResourceRecord *res = make_record(name, type, qclass, ttl, ip);
|
||||
ans.push_back(res);
|
||||
dns.answers = Utils::net_to_host_s(ans.size());
|
||||
}
|
||||
|
||||
void Tins::DNS::add_answer(const std::string &name, QueryType type, QueryClass qclass,
|
||||
void DNS::add_answer(const std::string &name, QueryType type, QueryClass qclass,
|
||||
uint32_t ttl, const std::string &dname) {
|
||||
string new_str;
|
||||
parse_domain_name(dname, new_str);
|
||||
@@ -220,36 +228,36 @@ void Tins::DNS::add_answer(const std::string &name, QueryType type, QueryClass q
|
||||
dns.answers = Utils::net_to_host_s(ans.size());
|
||||
}
|
||||
|
||||
void Tins::DNS::add_answer(const std::string &name, QueryType type, QueryClass qclass,
|
||||
void DNS::add_answer(const std::string &name, QueryType type, QueryClass qclass,
|
||||
uint32_t ttl, const uint8_t *data, uint32_t sz) {
|
||||
ResourceRecord *res = make_record(name, type, qclass, ttl, data, sz);
|
||||
ans.push_back(res);
|
||||
dns.answers = Utils::net_to_host_s(ans.size());
|
||||
}
|
||||
|
||||
void Tins::DNS::add_authority(const string &name, QueryType type,
|
||||
void DNS::add_authority(const string &name, QueryType type,
|
||||
QueryClass qclass, uint32_t ttl, const uint8_t *data, uint32_t sz) {
|
||||
ResourceRecord *res = make_record(name, type, qclass, ttl, data, sz);
|
||||
arity.push_back(res);
|
||||
dns.authority = Utils::net_to_host_s(arity.size());
|
||||
}
|
||||
|
||||
void Tins::DNS::add_additional(const string &name, QueryType type, QueryClass qclass, uint32_t ttl, uint32_t ip) {
|
||||
void DNS::add_additional(const string &name, QueryType type, QueryClass qclass, uint32_t ttl, uint32_t ip) {
|
||||
ResourceRecord *res = make_record(name, type, qclass, ttl, ip);
|
||||
addit.push_back(res);
|
||||
dns.additional = Utils::net_to_host_s(addit.size());
|
||||
}
|
||||
|
||||
Tins::DNS::ResourceRecord *Tins::DNS::make_record(const std::string &name, QueryType type, QueryClass qclass, uint32_t ttl, uint32_t ip) {
|
||||
DNS::ResourceRecord *DNS::make_record(const std::string &name, QueryType type, QueryClass qclass, uint32_t ttl, uint32_t ip) {
|
||||
ip = Utils::net_to_host_l(ip);
|
||||
return make_record(name, type, qclass, ttl, reinterpret_cast<uint8_t*>(&ip), sizeof(ip));
|
||||
}
|
||||
|
||||
Tins::DNS::ResourceRecord *Tins::DNS::make_record(const std::string &name, QueryType type, QueryClass qclass, uint32_t ttl, const std::string &dname) {
|
||||
DNS::ResourceRecord *DNS::make_record(const std::string &name, QueryType type, QueryClass qclass, uint32_t ttl, const std::string &dname) {
|
||||
return make_record(name, type, qclass, ttl, reinterpret_cast<const uint8_t*>(dname.c_str()), dname.size() + 1);
|
||||
}
|
||||
|
||||
Tins::DNS::ResourceRecord *Tins::DNS::make_record(const std::string &name, QueryType type, QueryClass qclass, uint32_t ttl, const uint8_t *ptr, uint32_t len) {
|
||||
DNS::ResourceRecord *DNS::make_record(const std::string &name, QueryType type, QueryClass qclass, uint32_t ttl, const uint8_t *ptr, uint32_t len) {
|
||||
string nm;
|
||||
parse_domain_name(name, nm);
|
||||
uint16_t index = find_domain_name(nm);
|
||||
@@ -265,7 +273,7 @@ Tins::DNS::ResourceRecord *Tins::DNS::make_record(const std::string &name, Query
|
||||
return res;
|
||||
}
|
||||
|
||||
uint32_t Tins::DNS::find_domain_name(const std::string &dname) {
|
||||
uint32_t DNS::find_domain_name(const std::string &dname) {
|
||||
uint16_t index(sizeof(dnshdr));
|
||||
list<Query>::const_iterator it(queries.begin());
|
||||
for(; it != queries.end() && it->name != dname; ++it)
|
||||
@@ -279,8 +287,8 @@ uint32_t Tins::DNS::find_domain_name(const std::string &dname) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool Tins::DNS::find_domain_name(const std::string &dname, const std::list<ResourceRecord*> &lst, uint16_t &out) {
|
||||
list<ResourceRecord*>::const_iterator it(lst.begin());
|
||||
bool DNS::find_domain_name(const std::string &dname, const ResourcesType &lst, uint16_t &out) {
|
||||
ResourcesType::const_iterator it(lst.begin());
|
||||
while(it != lst.end()) {
|
||||
if((*it)->matches(dname))
|
||||
break;
|
||||
@@ -290,7 +298,7 @@ bool Tins::DNS::find_domain_name(const std::string &dname, const std::list<Resou
|
||||
return it != lst.end();
|
||||
}
|
||||
|
||||
void Tins::DNS::parse_domain_name(const std::string &dn, std::string &out) const {
|
||||
void DNS::parse_domain_name(const std::string &dn, std::string &out) const {
|
||||
size_t last_index(0), index;
|
||||
while((index = dn.find('.', last_index+1)) != string::npos) {
|
||||
out.push_back(index - last_index);
|
||||
@@ -301,7 +309,7 @@ void Tins::DNS::parse_domain_name(const std::string &dn, std::string &out) const
|
||||
out.append(dn.begin() + last_index, dn.end());
|
||||
}
|
||||
|
||||
void Tins::DNS::unparse_domain_name(const std::string &dn, std::string &out) const {
|
||||
void DNS::unparse_domain_name(const std::string &dn, std::string &out) const {
|
||||
if(dn.size()) {
|
||||
uint32_t index(1), len(dn[0]);
|
||||
while(index + len < dn.size() && len) {
|
||||
@@ -320,7 +328,7 @@ void Tins::DNS::unparse_domain_name(const std::string &dn, std::string &out) con
|
||||
}
|
||||
}
|
||||
|
||||
void Tins::DNS::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent) {
|
||||
void DNS::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent) {
|
||||
assert(total_sz >= sizeof(dns) + extra_size);
|
||||
std::memcpy(buffer, &dns, sizeof(dns));
|
||||
buffer += sizeof(dns);
|
||||
@@ -337,13 +345,13 @@ void Tins::DNS::write_serialization(uint8_t *buffer, uint32_t total_sz, const PD
|
||||
buffer = serialize_list(addit, buffer);
|
||||
}
|
||||
|
||||
uint8_t *Tins::DNS::serialize_list(const std::list<ResourceRecord*> &lst, uint8_t *buffer) const {
|
||||
for(list<ResourceRecord*>::const_iterator it(lst.begin()); it != lst.end(); ++it)
|
||||
uint8_t *DNS::serialize_list(const ResourcesType &lst, uint8_t *buffer) const {
|
||||
for(ResourcesType::const_iterator it(lst.begin()); it != lst.end(); ++it)
|
||||
buffer += (*it)->write(buffer);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void Tins::DNS::add_suffix(uint32_t index, const uint8_t *data, uint32_t sz) {
|
||||
void DNS::add_suffix(uint32_t index, const uint8_t *data, uint32_t sz) {
|
||||
uint32_t i(0), suff_sz(data[0]);
|
||||
SuffixMap::iterator it;
|
||||
while((i + suff_sz + 1 <= sz || (suff_sz == 0xc0 && i + 1 < sz)) && suff_sz) {
|
||||
@@ -362,9 +370,9 @@ void Tins::DNS::add_suffix(uint32_t index, const uint8_t *data, uint32_t sz) {
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t Tins::DNS::build_suffix_map(uint32_t index, const list<ResourceRecord*> &lst) {
|
||||
uint32_t DNS::build_suffix_map(uint32_t index, const list<ResourceRecord*> &lst) {
|
||||
const string *str;
|
||||
for(list<ResourceRecord*>::const_iterator it(lst.begin()); it != lst.end(); ++it) {
|
||||
for(ResourcesType::const_iterator it(lst.begin()); it != lst.end(); ++it) {
|
||||
str = (*it)->dname_pointer();
|
||||
if(str) {
|
||||
add_suffix(index, (uint8_t*)str->c_str(), str->size());
|
||||
@@ -387,7 +395,7 @@ uint32_t Tins::DNS::build_suffix_map(uint32_t index, const list<ResourceRecord*>
|
||||
return index;
|
||||
}
|
||||
|
||||
uint32_t Tins::DNS::build_suffix_map(uint32_t index, const list<Query> &lst) {
|
||||
uint32_t DNS::build_suffix_map(uint32_t index, const list<Query> &lst) {
|
||||
for(list<Query>::const_iterator it(lst.begin()); it != lst.end(); ++it) {
|
||||
add_suffix(index, (uint8_t*)it->name.c_str(), it->name.size());
|
||||
index += it->name.size() + 1 + (sizeof(uint16_t) << 1);
|
||||
@@ -395,7 +403,7 @@ uint32_t Tins::DNS::build_suffix_map(uint32_t index, const list<Query> &lst) {
|
||||
return index;
|
||||
}
|
||||
|
||||
void Tins::DNS::build_suffix_map() {
|
||||
void DNS::build_suffix_map() {
|
||||
uint32_t index(sizeof(dnshdr));
|
||||
index = build_suffix_map(index, queries);
|
||||
index = build_suffix_map(index, ans);
|
||||
@@ -403,7 +411,7 @@ void Tins::DNS::build_suffix_map() {
|
||||
build_suffix_map(index, addit);
|
||||
}
|
||||
|
||||
void Tins::DNS::compose_name(const uint8_t *ptr, uint32_t sz, std::string &out) {
|
||||
void DNS::compose_name(const uint8_t *ptr, uint32_t sz, std::string &out) {
|
||||
uint32_t i(0);
|
||||
while(i < sz) {
|
||||
if(i && ptr[i])
|
||||
@@ -444,13 +452,13 @@ void Tins::DNS::compose_name(const uint8_t *ptr, uint32_t sz, std::string &out)
|
||||
}
|
||||
}
|
||||
|
||||
void Tins::DNS::convert_resources(const std::list<ResourceRecord*> &lst, std::list<Resource> &res) {
|
||||
void DNS::convert_resources(const ResourcesType &lst, std::list<Resource> &res) {
|
||||
if(!suffixes.size())
|
||||
build_suffix_map();
|
||||
const string *str_ptr;
|
||||
const uint8_t *ptr;
|
||||
uint32_t sz;
|
||||
for(list<ResourceRecord*>::const_iterator it(lst.begin()); it != lst.end(); ++it) {
|
||||
for(ResourcesType::const_iterator it(lst.begin()); it != lst.end(); ++it) {
|
||||
string dname, addr;
|
||||
if((str_ptr = (*it)->dname_pointer()))
|
||||
compose_name(reinterpret_cast<const uint8_t*>(str_ptr->c_str()), str_ptr->size(), dname);
|
||||
@@ -476,7 +484,7 @@ void Tins::DNS::convert_resources(const std::list<ResourceRecord*> &lst, std::li
|
||||
}
|
||||
}
|
||||
|
||||
list<Tins::DNS::Query> Tins::DNS::dns_queries() const {
|
||||
list<DNS::Query> DNS::dns_queries() const {
|
||||
list<Query> output;
|
||||
for(std::list<Query>::const_iterator it(queries.begin()); it != queries.end(); ++it) {
|
||||
string dn;
|
||||
@@ -486,20 +494,13 @@ list<Tins::DNS::Query> Tins::DNS::dns_queries() const {
|
||||
return output;
|
||||
}
|
||||
|
||||
list<Tins::DNS::Resource> Tins::DNS::dns_answers() {
|
||||
list<DNS::Resource> DNS::dns_answers() {
|
||||
list<Resource> res;
|
||||
convert_resources(ans, res);
|
||||
return res;
|
||||
}
|
||||
|
||||
Tins::PDU *Tins::DNS::clone_pdu() const {
|
||||
DNS *new_pdu = new DNS();
|
||||
new_pdu->copy_fields(this);
|
||||
new_pdu->copy_inner_pdu(*this);
|
||||
return new_pdu;
|
||||
}
|
||||
|
||||
void Tins::DNS::copy_fields(const DNS *other) {
|
||||
void DNS::copy_fields(const DNS *other) {
|
||||
std::memcpy(&dns, &other->dns, sizeof(dns));
|
||||
extra_size = other->extra_size;
|
||||
queries = other->queries;
|
||||
@@ -508,40 +509,30 @@ void Tins::DNS::copy_fields(const DNS *other) {
|
||||
copy_list(other->addit, addit);
|
||||
}
|
||||
|
||||
void Tins::DNS::copy_list(const list<ResourceRecord*> &from, list<ResourceRecord*> &to) const {
|
||||
for(list<ResourceRecord*>::const_iterator it(from.begin()); it != from.end(); ++it) {
|
||||
void DNS::copy_list(const ResourcesType &from, ResourcesType &to) const {
|
||||
for(ResourcesType::const_iterator it(from.begin()); it != from.end(); ++it) {
|
||||
to.push_back((*it)->clone());
|
||||
}
|
||||
}
|
||||
|
||||
// ResourceRecord
|
||||
|
||||
void Tins::DNS::ResourceRecord::copy_fields(ResourceRecord *other) const {
|
||||
std::memcpy(&other->info, &info, sizeof(info));
|
||||
other->data_sz = data_sz;
|
||||
other->data = new uint8_t[data_sz];
|
||||
std::memcpy(other->data, data, data_sz);
|
||||
}
|
||||
|
||||
uint32_t Tins::DNS::ResourceRecord::write(uint8_t *buffer) const {
|
||||
uint32_t sz(do_write(buffer));
|
||||
uint32_t DNS::ResourceRecord::write(uint8_t *buffer) const {
|
||||
const uint32_t sz(do_write(buffer));
|
||||
buffer += sz;
|
||||
std::memcpy(buffer, &info, sizeof(info));
|
||||
buffer += sizeof(info);
|
||||
*((uint16_t*)buffer) = Utils::net_to_host_s(data_sz);
|
||||
*((uint16_t*)buffer) = Utils::net_to_host_s(data.size());
|
||||
buffer += sizeof(uint16_t);
|
||||
std::memcpy(buffer, data, data_sz);
|
||||
return sz + sizeof(info) + sizeof(uint16_t) + data_sz;
|
||||
std::copy(data.begin(), data.end(), buffer);
|
||||
return sz + sizeof(info) + sizeof(uint16_t) + data.size();
|
||||
}
|
||||
|
||||
Tins::DNS::ResourceRecord *Tins::DNS::OffsetedResourceRecord::clone() const {
|
||||
ResourceRecord *r = new OffsetedResourceRecord(offset);
|
||||
copy_fields(r);
|
||||
return r;
|
||||
DNS::ResourceRecord *DNS::OffsetedResourceRecord::clone() const {
|
||||
return new OffsetedResourceRecord(*this);
|
||||
}
|
||||
|
||||
Tins::DNS::ResourceRecord *Tins::DNS::NamedResourceRecord::clone() const {
|
||||
ResourceRecord *r = new NamedResourceRecord(name);
|
||||
copy_fields(r);
|
||||
return r;
|
||||
DNS::ResourceRecord *DNS::NamedResourceRecord::clone() const {
|
||||
return new NamedResourceRecord(*this);
|
||||
}
|
||||
}
|
||||
|
||||
17
src/udp.cpp
17
src/udp.cpp
@@ -43,16 +43,6 @@ Tins::UDP::UDP(const uint8_t *buffer, uint32_t total_sz) : PDU(Constants::IP::PR
|
||||
inner_pdu(new RawPDU(buffer + sizeof(udphdr), total_sz));
|
||||
}
|
||||
|
||||
Tins::UDP::UDP(const UDP &other) : PDU(other) {
|
||||
copy_fields(&other);
|
||||
}
|
||||
|
||||
Tins::UDP &Tins::UDP::operator= (const UDP& other) {
|
||||
copy_fields(&other);
|
||||
copy_inner_pdu(other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Tins::UDP::payload(uint8_t *new_payload, uint32_t new_payload_size) {
|
||||
inner_pdu(new RawPDU(new_payload, new_payload_size));
|
||||
}
|
||||
@@ -92,10 +82,3 @@ void Tins::UDP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PD
|
||||
void Tins::UDP::copy_fields(const UDP *other) {
|
||||
std::memcpy(&_udp, &other->_udp, sizeof(_udp));
|
||||
}
|
||||
|
||||
Tins::PDU *Tins::UDP::clone_pdu() const {
|
||||
UDP *new_pdu = new UDP();
|
||||
new_pdu->copy_fields(this);
|
||||
new_pdu->copy_inner_pdu(*this);
|
||||
return new_pdu;
|
||||
}
|
||||
|
||||
@@ -39,6 +39,7 @@ void DNSTest::test_equals(const DNS &dns1, const DNS &dns2) {
|
||||
EXPECT_EQ(dns1.additional(), dns2.additional());
|
||||
EXPECT_EQ(dns1.pdu_type(), dns2.pdu_type());
|
||||
EXPECT_EQ(dns1.header_size(), dns2.header_size());
|
||||
EXPECT_EQ(bool(dns1.inner_pdu()), bool(dns2.inner_pdu()));
|
||||
}
|
||||
|
||||
void DNSTest::test_equals(const DNS::Query &q1, const DNS::Query &q2) {
|
||||
@@ -79,6 +80,27 @@ TEST_F(DNSTest, ConstructorFromBuffer) {
|
||||
test_equals(answers.front(), DNS::Resource("www.example.com", "192.168.0.1", DNS::A, DNS::IN, 0x1234));
|
||||
}
|
||||
|
||||
TEST_F(DNSTest, CopyConstructor) {
|
||||
DNS dns1(expected_packet, sizeof(expected_packet));
|
||||
DNS dns2(dns1);
|
||||
test_equals(dns1, dns2);
|
||||
}
|
||||
|
||||
TEST_F(DNSTest, CopyAssignmentOperator) {
|
||||
DNS dns1(expected_packet, sizeof(expected_packet));
|
||||
DNS dns2;
|
||||
dns2 = dns1;
|
||||
test_equals(dns1, dns2);
|
||||
}
|
||||
|
||||
TEST_F(DNSTest, NestedCopy) {
|
||||
DNS *nested = new DNS(expected_packet, sizeof(expected_packet));
|
||||
DNS dns1(expected_packet, sizeof(expected_packet));
|
||||
dns1.inner_pdu(nested);
|
||||
DNS dns2(dns1);
|
||||
test_equals(dns1, dns2);
|
||||
}
|
||||
|
||||
TEST_F(DNSTest, ID) {
|
||||
DNS dns;
|
||||
dns.id(0x7263);
|
||||
|
||||
@@ -12,11 +12,22 @@ using namespace Tins;
|
||||
class UDPTest : public testing::Test {
|
||||
public:
|
||||
static const uint8_t expected_packet[];
|
||||
|
||||
void test_equals(const UDP& udp1, const UDP& udp2);
|
||||
};
|
||||
|
||||
const uint8_t UDPTest::expected_packet[] = {'\xf5', '\x1a', 'G', '\xf1', '\x04', 'S', '\x00', '\x00'};
|
||||
|
||||
|
||||
void UDPTest::test_equals(const UDP& udp1, const UDP& udp2) {
|
||||
EXPECT_EQ(udp1.dport(), udp2.dport());
|
||||
EXPECT_EQ(udp1.sport(), udp2.sport());
|
||||
EXPECT_EQ(udp1.length(), udp2.length());
|
||||
EXPECT_EQ(udp1.size(), udp2.size());
|
||||
EXPECT_EQ(udp1.header_size(), udp2.header_size());
|
||||
EXPECT_EQ(bool(udp1.inner_pdu()), bool(udp2.inner_pdu()));
|
||||
}
|
||||
|
||||
TEST_F(UDPTest, DefaultContructor) {
|
||||
UDP udp;
|
||||
EXPECT_EQ(udp.dport(), 0);
|
||||
@@ -24,6 +35,19 @@ TEST_F(UDPTest, DefaultContructor) {
|
||||
EXPECT_FALSE(udp.inner_pdu());
|
||||
}
|
||||
|
||||
TEST_F(UDPTest, CopyContructor) {
|
||||
UDP udp1(expected_packet, sizeof(expected_packet));
|
||||
UDP udp2(udp1);
|
||||
test_equals(udp1, udp2);
|
||||
}
|
||||
|
||||
TEST_F(UDPTest, CopyAssignmentOperator) {
|
||||
UDP udp1(expected_packet, sizeof(expected_packet));
|
||||
UDP udp2;
|
||||
udp2 = udp1;
|
||||
test_equals(udp1, udp2);
|
||||
}
|
||||
|
||||
TEST_F(UDPTest, CompleteConstructor) {
|
||||
UDP *inner = new UDP(0x48fa, 0x716b);
|
||||
UDP udp(0x1234, 0x4321, inner);
|
||||
@@ -58,34 +82,6 @@ TEST_F(UDPTest, PDUType) {
|
||||
EXPECT_EQ(udp.pdu_type(), PDU::UDP);
|
||||
}
|
||||
|
||||
TEST_F(UDPTest, CopyConstructor) {
|
||||
UDP udp1;
|
||||
udp1.dport(0x1234);
|
||||
udp1.sport(0x4321);
|
||||
udp1.length(0xdead);
|
||||
|
||||
UDP udp2(udp1);
|
||||
EXPECT_EQ(udp2.sport(), udp1.sport());
|
||||
EXPECT_EQ(udp2.dport(), udp1.dport());
|
||||
EXPECT_EQ(udp2.length(), udp1.length());
|
||||
EXPECT_EQ(udp2.size(), udp1.size());
|
||||
EXPECT_EQ(udp2.header_size(), udp1.header_size());
|
||||
}
|
||||
|
||||
TEST_F(UDPTest, CopyAssignmentOperator) {
|
||||
UDP udp1;
|
||||
udp1.dport(0x1234);
|
||||
udp1.sport(0x4321);
|
||||
udp1.length(0xdead);
|
||||
|
||||
UDP udp2 = udp1;
|
||||
EXPECT_EQ(udp2.sport(), udp1.sport());
|
||||
EXPECT_EQ(udp2.dport(), udp1.dport());
|
||||
EXPECT_EQ(udp2.length(), udp1.length());
|
||||
EXPECT_EQ(udp2.size(), udp1.size());
|
||||
EXPECT_EQ(udp2.header_size(), udp1.header_size());
|
||||
}
|
||||
|
||||
TEST_F(UDPTest, ClonePDU) {
|
||||
UDP udp1;
|
||||
uint16_t sport = 0x1234, dport = 0x4321, length = 0xdead;
|
||||
|
||||
Reference in New Issue
Block a user