Fixed DNS TXT parsing

This commit is contained in:
SChernykh 2023-05-12 15:12:50 +02:00
parent 4fa344a0f7
commit 3c7fcf65a9
2 changed files with 15 additions and 16 deletions

View file

@ -238,20 +238,17 @@ int main(int argc, char* argv[])
for (int rrnum = 0, n = ns_msg_count(handle, ns_s_an); rrnum < n; ++rrnum) {
ns_rr rr;
if ((ns_parserr(&handle, ns_s_an, rrnum, &rr) == 0) && (ns_rr_type(rr) == ns_t_txt)) {
const uint8_t* data = ns_rr_rdata(rr);
int len = ns_rr_rdlen(rr) - 1;
if (len > data[0]) len = data[0];
if (len <= 0) {
continue;
}
for (const uint8_t* data = ns_rr_rdata(rr), *e = data + ns_rr_rdlen(rr); data < e;) {
const int k = *(data++);
if (k && (data + k <= e)) {
char buf[256];
memcpy(buf, data + 1, len);
buf[len] = 0;
memcpy(buf, data, k);
buf[k] = 0;
puts(buf);
}
data += k;
}
}
}
return 0;

View file

@ -517,10 +517,12 @@ bool get_dns_txt_records_base(const std::string& host, Callback<void, const char
for (int rrnum = 0, n = ns_msg_count(handle, ns_s_an); rrnum < n; ++rrnum) {
ns_rr rr;
if ((ns_parserr(&handle, ns_s_an, rrnum, &rr) == 0) && (ns_rr_type(rr) == ns_t_txt)) {
const uint8_t* s = ns_rr_rdata(rr);
const int n = std::min<int>(ns_rr_rdlen(rr) - 1, *s);
if (n > 0) {
callback(reinterpret_cast<const char*>(s + 1), static_cast<size_t>(n));
for (const uint8_t* data = ns_rr_rdata(rr), *e = data + ns_rr_rdlen(rr); data < e;) {
const size_t k = *(data++);
if (k && (data + k <= e)) {
callback(reinterpret_cast<const char*>(data), k);
}
data += k;
}
}
}