1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 22:07:35 +00:00

LookupServer: Fix serializing name data in DNS answers

When serializing a RR of type PTR, we should use the DNS name serialization
format, not a raw string.
This commit is contained in:
Sergey Bugaev 2021-02-14 15:17:27 +03:00 committed by Andreas Kling
parent d6f7ced4f1
commit 80f7489df0
3 changed files with 16 additions and 2 deletions

View file

@ -70,6 +70,13 @@ DNSName DNSName::parse(const u8* data, size_t& offset, size_t max_offset, size_t
}
}
size_t DNSName::serialized_size() const
{
if (m_name.is_empty())
return 1;
return m_name.length() + 2;
}
OutputStream& operator<<(OutputStream& stream, const DNSName& name)
{
auto parts = name.as_string().split_view('.');

View file

@ -38,6 +38,7 @@ public:
static DNSName parse(const u8* data, size_t& offset, size_t max_offset, size_t recursion_level = 0);
size_t serialized_size() const;
const String& as_string() const { return m_name; }
private:

View file

@ -92,8 +92,14 @@ ByteBuffer DNSPacket::to_byte_buffer() const
stream << htons(answer.type());
stream << htons(answer.class_code());
stream << htonl(answer.ttl());
stream << htons(answer.record_data().length());
stream << answer.record_data().bytes();
if (answer.type() == T_PTR) {
DNSName name { answer.record_data() };
stream << htons(name.serialized_size());
stream << name;
} else {
stream << htons(answer.record_data().length());
stream << answer.record_data().bytes();
}
}
return stream.copy_into_contiguous_buffer();