diff options
Diffstat (limited to 'dns.hh')
-rw-r--r-- | dns.hh | 95 |
1 files changed, 62 insertions, 33 deletions
@@ -32,7 +32,7 @@ struct DNSRecord; class RCode { public: - enum rcodes_ { NoError=0, FormErr=1, ServFail=2, NXDomain=3, NotImp=4, Refused=5, YXDomain=6, YXRRSet=7, NXRRSet=8, NotAuth=9, NotZone=10}; + enum rcodes_ : uint8_t { NoError=0, FormErr=1, ServFail=2, NXDomain=3, NotImp=4, Refused=5, YXDomain=6, YXRRSet=7, NXRRSet=8, NotAuth=9, NotZone=10}; static std::string to_s(uint8_t rcode); static std::string to_short_s(uint8_t rcode); const static std::array<std::string, 24> rcodes_s; @@ -41,14 +41,14 @@ public: class ERCode { public: - enum rcodes_ { BADVERS=16, BADSIG=16, BADKEY=17, BADTIME=18, BADMODE=19, BADNAME=20, BADALG=21, BADTRUNC=22, BADCOOKIE=23 }; - static std::string to_s(uint8_t rcode); + enum rcodes_ : uint16_t { BADVERS=16, BADSIG=16, BADKEY=17, BADTIME=18, BADMODE=19, BADNAME=20, BADALG=21, BADTRUNC=22, BADCOOKIE=23 }; + static std::string to_s(uint16_t rcode); }; class Opcode { public: - enum { Query=0, IQuery=1, Status=2, Notify=4, Update=5 }; + enum opcodes_ : uint8_t { Query=0, IQuery=1, Status=2, Notify=4, Update=5 }; static std::string to_s(uint8_t opcode); }; @@ -56,13 +56,18 @@ public: class DNSResourceRecord { public: - DNSResourceRecord() : last_modified(0), ttl(0), signttl(0), domain_id(-1), qclass(1), scopeMask(0), auth(1), disabled(0) {}; - static DNSResourceRecord fromWire(const DNSRecord& d); + static DNSResourceRecord fromWire(const DNSRecord& wire); - enum Place : uint8_t {QUESTION=0, ANSWER=1, AUTHORITY=2, ADDITIONAL=3}; //!< Type describing the positioning within, say, a DNSPacket + enum Place : uint8_t + { + QUESTION = 0, + ANSWER = 1, + AUTHORITY = 2, + ADDITIONAL = 3 + }; //!< Type describing the positioning within, say, a DNSPacket void setContent(const string& content); - string getZoneRepresentation(bool noDot=false) const; + [[nodiscard]] string getZoneRepresentation(bool noDot = false) const; // data DNSName qname; //!< the name of this record, for example: www.powerdns.com @@ -72,27 +77,29 @@ public: // Aligned on 8-byte boundaries on systems where time_t is 8 bytes and int // is 4 bytes, aka modern linux on x86_64 - time_t last_modified; //!< For autocalculating SOA serial numbers - the backend needs to fill this in + time_t last_modified{}; //!< For autocalculating SOA serial numbers - the backend needs to fill this in - uint32_t ttl; //!< Time To Live of this record - uint32_t signttl; //!< If non-zero, use this TTL as original TTL in the RRSIG + uint32_t ttl{}; //!< Time To Live of this record + uint32_t signttl{}; //!< If non-zero, use this TTL as original TTL in the RRSIG - int domain_id; //!< If a backend implements this, the domain_id of the zone this record is in + int domain_id{-1}; //!< If a backend implements this, the domain_id of the zone this record is in QType qtype; //!< qtype of this record, ie A, CNAME, MX etc - uint16_t qclass; //!< class of this record + uint16_t qclass{1}; //!< class of this record - uint8_t scopeMask; - bool auth; - bool disabled; + uint8_t scopeMask{}; + bool auth{true}; + bool disabled{}; bool operator==(const DNSResourceRecord& rhs); - bool operator<(const DNSResourceRecord &b) const + bool operator<(const DNSResourceRecord& other) const { - if(qname < b.qname) + if (qname < other.qname) { return true; - if(qname == b.qname) - return(content < b.content); + } + if (qname == other.qname) { + return (content < other.content); + } return false; } }; @@ -120,7 +127,7 @@ static_assert(sizeof(EDNS0Record) == 4, "EDNS0Record size must be 4"); #elif __linux__ || __GNU__ # include <endian.h> -#else // with thanks to <arpa/nameser.h> +#else // with thanks to <arpa/nameser.h> # define LITTLE_ENDIAN 1234 /* least-significant byte first (vax, pc) */ # define BIG_ENDIAN 4321 /* most-significant byte first (IBM, net) */ @@ -151,7 +158,7 @@ static_assert(sizeof(EDNS0Record) == 4, "EDNS0Record size must be 4"); #endif struct dnsheader { - unsigned id :16; /* query identification number */ + uint16_t id; /* query identification number */ #if BYTE_ORDER == BIG_ENDIAN /* fields in third byte */ unsigned qr: 1; /* response flag */ @@ -180,10 +187,10 @@ struct dnsheader { unsigned ra :1; /* recursion available */ #endif /* remaining bytes */ - unsigned qdcount :16; /* number of question entries */ - unsigned ancount :16; /* number of answer entries */ - unsigned nscount :16; /* number of authority entries */ - unsigned arcount :16; /* number of resource entries */ + uint16_t qdcount; /* number of question entries */ + uint16_t ancount; /* number of answer entries */ + uint16_t nscount; /* number of authority entries */ + uint16_t arcount; /* number of resource entries */ }; static_assert(sizeof(dnsheader) == 12, "dnsheader size must be 12"); @@ -191,10 +198,15 @@ static_assert(sizeof(dnsheader) == 12, "dnsheader size must be 12"); class dnsheader_aligned { public: + static bool isMemoryAligned(const void* mem) + { + return reinterpret_cast<uintptr_t>(mem) % sizeof(uint32_t) == 0; // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) + } + dnsheader_aligned(const void* mem) { - if (reinterpret_cast<uintptr_t>(mem) % sizeof(uint32_t) == 0) { - d_p = reinterpret_cast<const dnsheader*>(mem); + if (isMemoryAligned(mem)) { + d_p = reinterpret_cast<const dnsheader*>(mem); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) } else { memcpy(&d_h, mem, sizeof(dnsheader)); @@ -202,19 +214,36 @@ public: } } - const dnsheader* get() const + [[nodiscard]] const dnsheader* get() const + { + return d_p; + } + + [[nodiscard]] const dnsheader& operator*() const + { + return *d_p; + } + + [[nodiscard]] const dnsheader* operator->() const { return d_p; } private: - dnsheader d_h; - const dnsheader *d_p; + dnsheader d_h{}; + const dnsheader* d_p{}; }; -inline uint16_t * getFlagsFromDNSHeader(struct dnsheader * dh) +inline uint16_t* getFlagsFromDNSHeader(dnsheader* dh) +{ + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + return reinterpret_cast<uint16_t*>(reinterpret_cast<char*>(dh) + sizeof(uint16_t)); +} + +inline const uint16_t * getFlagsFromDNSHeader(const dnsheader* dh) { - return (uint16_t*) (((char *) dh) + sizeof(uint16_t)); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + return reinterpret_cast<const uint16_t*>(reinterpret_cast<const char*>(dh) + sizeof(uint16_t)); } #define DNS_TYPE_SIZE (2) |