From a7c46702841348785f22e550636cb66d534be087 Mon Sep 17 00:00:00 2001 From: bert hubert Date: Sun, 15 Apr 2018 16:29:17 +0200 Subject: [PATCH] remove safearray, dnsmessagereader can now parse "any" length --- tdns/dnsmessages.cc | 25 +++++++++++++------------ tdns/dnsmessages.hh | 27 ++++++++++++++++++++++++--- tdns/safearray.hh | 38 -------------------------------------- 3 files changed, 37 insertions(+), 53 deletions(-) delete mode 100644 tdns/safearray.hh diff --git a/tdns/dnsmessages.cc b/tdns/dnsmessages.cc index 65f778c..4779c48 100644 --- a/tdns/dnsmessages.cc +++ b/tdns/dnsmessages.cc @@ -8,19 +8,20 @@ DNSMessageReader::DNSMessageReader(const char* in, uint16_t size) if(size < sizeof(dnsheader)) throw std::runtime_error("DNS message too small"); memcpy(&dh, in, sizeof(dh)); - auto rest = size-sizeof(dh); - memcpy(&payload.payload.at(rest)-rest, in+sizeof(dh), rest); + payload.reserve(size-12); + payload.insert(payload.begin(), (const unsigned char*)in + 12, (const unsigned char*)in + size); + d_qname = getName(); - d_qtype = (DNSType) payload.getUInt16(); - d_qclass = (DNSClass) payload.getUInt16(); + d_qtype = (DNSType) getUInt16(); + d_qclass = (DNSClass) getUInt16(); if(dh.arcount) { - if(payload.getUInt8() == 0 && payload.getUInt16() == (uint16_t)DNSType::OPT) { - d_bufsize=payload.getUInt16(); - payload.getUInt8(); // extended RCODE - d_ednsVersion = payload.getUInt8(); - auto flags = payload.getUInt8(); + if(getUInt8() == 0 && getUInt16() == (uint16_t)DNSType::OPT) { + d_bufsize=getUInt16(); + getUInt8(); // extended RCODE + d_ednsVersion = getUInt8(); + auto flags = getUInt8(); d_doBit = flags & 0x80; - payload.getUInt8(); payload.getUInt16(); // ignore rest + getUInt8(); getUInt16(); // ignore rest cout<<" There was an EDNS section, size supported: "<< d_bufsize< 63) throw std::runtime_error("Got a compressed label"); if(!labellen) // end of DNSName break; - DNSLabel label = payload.getBlob(labellen); + DNSLabel label = getBlob(labellen); name.push_back(label); } return name; diff --git a/tdns/dnsmessages.hh b/tdns/dnsmessages.hh index 763451f..f539336 100644 --- a/tdns/dnsmessages.hh +++ b/tdns/dnsmessages.hh @@ -1,8 +1,8 @@ #pragma once #include "dns.hh" -#include "safearray.hh" #include "dns-storage.hh" #include "record-types.hh" +#include #include class DNSMessageReader @@ -11,13 +11,34 @@ public: DNSMessageReader(const char* input, uint16_t length); DNSMessageReader(const std::string& str) : DNSMessageReader(str.c_str(), str.size()) {} struct dnsheader dh=dnsheader{}; - SafeArray<500> payload; - + std::vector payload; + uint16_t payloadpos{0}; + void getQuestion(DNSName& name, DNSType& type) const; bool getEDNS(uint16_t* newsize, bool* doBit) const; uint8_t d_ednsVersion{0}; private: DNSName getName(); + uint8_t getUInt8() + { + return payload.at(payloadpos++); + } + + uint16_t getUInt16() + { + uint16_t ret; + memcpy(&ret, &payload.at(payloadpos+1)-1, 2); + payloadpos+=2; + return htons(ret); + } + + std::string getBlob(int size) + { + std::string ret(&payload.at(payloadpos), &payload.at(payloadpos+size)); + payloadpos += size; + return ret; + } + DNSName d_qname; DNSType d_qtype; DNSClass d_qclass; diff --git a/tdns/safearray.hh b/tdns/safearray.hh deleted file mode 100644 index fe13da0..0000000 --- a/tdns/safearray.hh +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once -#include -#include -#include -#include -#include - -template -struct SafeArray -{ - std::array payload; - uint16_t payloadpos{0}, payloadsize{0}; - - void rewind() - { - payloadpos = 0; - } - - uint8_t getUInt8() - { - return payload.at(payloadpos++); - } - - uint16_t getUInt16() - { - uint16_t ret; - memcpy(&ret, &payload.at(payloadpos+2)-2, 2); - payloadpos+=2; - return htons(ret); - } - - std::string getBlob(int size) - { - std::string ret(&payload.at(payloadpos), &payload.at(payloadpos+size)); - payloadpos += size; - return ret; - } -};