vector<string> parts;
stringtok(parts, zone);
// we need exactly 3 parts, except if the length field is set to 0 then we only need 2
- if (parts.size() != 3 && !(parts.size() == 2 && equals(parts[1], "0"))) {
+ if (parts.size() != 3 && !(parts.size() == 2 && equals(parts.at(1), "0"))) {
throw MOADNSException("Unknown record was stored incorrectly, need 3 fields, got " + std::to_string(parts.size()) + ": " + zone);
}
- const string& relevant = (parts.size() > 2) ? parts[2] : "";
- unsigned int total = pdns_stou(parts[1]);
+ if (parts.at(0) != "\\#") {
+ throw MOADNSException("Unknown record was stored incorrectly, first part should be '\\#', got '" + parts.at(0) + "'");
+ }
+
+ const string& relevant = (parts.size() > 2) ? parts.at(2) : "";
+ unsigned int total = pdns_stou(parts.at(1));
if (relevant.size() % 2 || (relevant.size() / 2) != total) {
throw MOADNSException((boost::format("invalid unknown record length: size not equal to length field (%d != 2 * %d)") % relevant.size() % total).str());
}
for (unsigned int n = 0; n < total; ++n) {
int c;
- if (sscanf(relevant.c_str()+2*n, "%02x", &c) != 1) {
+ if (sscanf(&relevant.at(2*n), "%02x", &c) != 1) {
throw MOADNSException("unable to read data at position " + std::to_string(2 * n) + " from unknown record of size " + std::to_string(relevant.size()));
}
out.append(1, (char)c);
auto validEmptyUnknown = DNSRecordContent::mastermake(static_cast<QType::typeenum>(65534), QClass::IN, "\\# 0");
BOOST_CHECK_THROW(auto twoPartsNotZeroUnknown = DNSRecordContent::mastermake(static_cast<QType::typeenum>(65534), QClass::IN, "\\# 1"), MOADNSException);
+ // the first part has to be "\#"
+ BOOST_CHECK_THROW(auto invalidFirstPartUnknown = DNSRecordContent::mastermake(static_cast<QType::typeenum>(65534), QClass::IN, "\\$ 0"), MOADNSException);
+
// RDATA length is not even
BOOST_CHECK_THROW(auto unevenUnknown = DNSRecordContent::mastermake(static_cast<QType::typeenum>(65534), QClass::IN, "\\# 1 A"), MOADNSException);