From 993831709d6bc47633172920e367180f83fb7d0e Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Sat, 13 May 2017 19:44:57 -0700 Subject: [PATCH] Improve DNS class performance slightly --- include/tins/dns.h | 16 ++++++++++++++++ src/dns.cpp | 39 +++++++++++++++++++++++++++------------ 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/include/tins/dns.h b/include/tins/dns.h index fa12cd4..ef93eaa 100644 --- a/include/tins/dns.h +++ b/include/tins/dns.h @@ -183,8 +183,13 @@ public: * \param tp The query type. * \param cl The query class. */ + #if TINS_IS_CXX11 + query(std::string nm, QueryType tp, QueryClass cl) + : name_(std::move(nm)), type_(tp), qclass_(cl) {} + #else query(const std::string& nm, QueryType tp, QueryClass cl) : name_(nm), type_(tp), qclass_(cl) {} + #endif /** * \brief Default constructs this Query. @@ -448,6 +453,16 @@ public: * \param rclass The class of this record. * \param ttl The time-to-live of this record. */ + #if TINS_IS_CXX11 + resource(std::string dname, + std::string data, + uint16_t type, + uint16_t rclass, + uint32_t ttl, + uint16_t preference = 0) + : dname_(std::move(dname)), data_(std::move(data)), type_(type), + qclass_(rclass), ttl_(ttl), preference_(preference) {} + #else resource(const std::string& dname, const std::string& data, uint16_t type, @@ -456,6 +471,7 @@ public: uint16_t preference = 0) : dname_(dname), data_(data), type_(type), qclass_(rclass), ttl_(ttl), preference_(preference) {} + #endif resource() : type_(), qclass_(), ttl_(), preference_() {} diff --git a/src/dns.cpp b/src/dns.cpp index 046fec1..d36c87a 100644 --- a/src/dns.cpp +++ b/src/dns.cpp @@ -105,7 +105,7 @@ void DNS::skip_to_section_end(InputMemoryStream& stream, skip_to_dname_end(stream); stream.skip(sizeof(uint16_t) * 2 + sizeof(uint32_t)); uint16_t data_size = stream.read_be(); - if (!stream.can_read(data_size)) { + if (TINS_UNLIKELY(!stream.can_read(data_size))) { throw malformed_packet(); } stream.skip(data_size); @@ -339,7 +339,7 @@ uint32_t DNS::compose_name(const uint8_t* ptr, char* out_ptr) const { while (*ptr) { // It's an offset if ((*ptr & 0xc0)) { - if (ptr + sizeof(uint16_t) > end) { + if (TINS_UNLIKELY(ptr + sizeof(uint16_t) > end)) { throw malformed_packet(); } uint16_t index; @@ -360,7 +360,7 @@ uint32_t DNS::compose_name(const uint8_t* ptr, char* out_ptr) const { // It's a label, grab its size. uint8_t size = *ptr; ptr++; - if (ptr + size > end || current_out_ptr - out_ptr + size + 1 > 255) { + if (TINS_UNLIKELY(ptr + size > end || current_out_ptr - out_ptr + size + 1 > 255)) { throw malformed_packet(); } // Append a dot if it's not the first one. @@ -430,7 +430,7 @@ void DNS::convert_records(const uint8_t* ptr, preference = stream.read_be(); data_size -= sizeof(uint16_t); } - if (!stream.can_read(data_size)) { + if (TINS_UNLIKELY(!stream.can_read(data_size))) { throw malformed_packet(); } @@ -470,16 +470,27 @@ void DNS::convert_records(const uint8_t* ptr, stream.skip(data_size); break; } - res.push_back( - resource( + #if TINS_IS_CXX11 + res.emplace_back( dname, - (used_small_buffer) ? small_addr_buf : data, + (used_small_buffer) ? small_addr_buf : std::move(data), type, qclass, ttl, preference - ) - ); + ); + #else + res.push_back( + resource( + dname, + (used_small_buffer) ? small_addr_buf : data, + type, + qclass, + ttl, + preference + ) + ); + #endif } } @@ -544,9 +555,13 @@ DNS::queries_type DNS::queries() const { stream.skip(compose_name(stream.pointer(), buffer)); uint16_t query_type = stream.read_be(); uint16_t query_class = stream.read_be(); - output.push_back( - query(buffer, (QueryType)query_type, (QueryClass)query_class) - ); + #if TINS_IS_CXX11 + output.emplace_back(buffer, (QueryType)query_type, (QueryClass)query_class); + #else + output.push_back( + query(buffer, (QueryType)query_type, (QueryClass)query_class) + ); + #endif } } return output;