1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-23 02:35:57 +01:00

Improve DNS class performance slightly

This commit is contained in:
Matias Fontanini
2017-05-13 19:44:57 -07:00
parent 4def995185
commit 993831709d
2 changed files with 43 additions and 12 deletions

View File

@@ -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_() {}

View File

@@ -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<uint16_t>();
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<uint16_t>();
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>();
uint16_t query_class = stream.read_be<uint16_t>();
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;