diff --git a/src/seeder/dns.h b/src/seeder/dns.h --- a/src/seeder/dns.h +++ b/src/seeder/dns.h @@ -8,6 +8,11 @@ #include #include +constexpr int MAX_LABEL_LENGTH = 63; +constexpr int MAX_QUERY_NAME_LENGTH = 255; +// Max size of the null-terminated buffer parse_name() writes to. +constexpr int MAX_QUERY_NAME_BUFFER_LENGTH = MAX_QUERY_NAME_LENGTH + 1; + struct addr_t { int v; union { @@ -30,8 +35,8 @@ }; // 0: ok -// -1: premature end of input, forward reference, component > 63 char, invalid -// character +// -1: premature end of input, forward reference, label > MAX_LABEL_LENGTH, +// invalid character // -2: insufficient space in output int parse_name(const uint8_t **inpos, const uint8_t *inend, const uint8_t *inbuf, char *buf, size_t bufsize); diff --git a/src/seeder/dns.cpp b/src/seeder/dns.cpp --- a/src/seeder/dns.cpp +++ b/src/seeder/dns.cpp @@ -101,13 +101,13 @@ return parse_name(&newbuf, (*inpos) - 2, inbuf, buf + bufused, bufsize - bufused); } - if (octet > 63) { + if (octet > MAX_LABEL_LENGTH) { return -1; } // The maximum size of a query name is 255. The buffer must have // room for the null-character at the end of the buffer after writing // the label. - if (octet + bufused > 255) { + if (octet + bufused > MAX_QUERY_NAME_LENGTH) { return -1; } // copy label @@ -128,8 +128,8 @@ } while (1); } -// 0: k -// -1: component > 63 characters +// 0: ok +// -1: label > MAX_LABEL_LENGTH characters // -2: insufficent space in output // -3: two subsequent dots static int write_name(uint8_t **outpos, const uint8_t *outend, const char *name, @@ -140,7 +140,7 @@ if (!dot) { fin = name + strlen(name); } - if (fin - name > 63) { + if (fin - name > MAX_LABEL_LENGTH) { return -1; } if (fin == name) { @@ -422,9 +422,10 @@ { const uint8_t *inpos = inbuf + 12; const uint8_t *inend = inbuf + insize; - char name[256]; + char name[MAX_QUERY_NAME_BUFFER_LENGTH]; int offset = inpos - inbuf; - int ret = parse_name(&inpos, inend, inbuf, name, 256); + int ret = parse_name(&inpos, inend, inbuf, name, + MAX_QUERY_NAME_BUFFER_LENGTH); if (ret == -1) { responseCode = DNSResponseCode::FORMAT_ERROR; goto error; diff --git a/src/seeder/test/dns_tests.cpp b/src/seeder/test/dns_tests.cpp --- a/src/seeder/test/dns_tests.cpp +++ b/src/seeder/test/dns_tests.cpp @@ -11,11 +11,7 @@ BOOST_AUTO_TEST_SUITE(dns_tests) -static const int MAX_QUERY_NAME_LENGTH = 255; -// Max size of the null-terminated buffer parse_name() writes to. -static const int MAX_QUERY_NAME_BUFFER_LENGTH = MAX_QUERY_NAME_LENGTH + 1; static const uint8_t END_OF_NAME_FIELD = 0; -static const size_t MAX_LABEL_LENGTH = 63; // Builds the name field of the question section of a DNS query static std::vector