LookupServer: Move DNS name serialization to DNSName class

This commit is contained in:
Sergey Bugaev 2021-02-14 15:16:20 +03:00 committed by Andreas Kling
parent 42bc5f2cc1
commit d6f7ced4f1
Notes: sideshowbarker 2024-07-18 22:17:11 +09:00
3 changed files with 17 additions and 12 deletions

View File

@ -26,6 +26,7 @@
*/
#include "DNSName.h"
#include <AK/Vector.h>
namespace LookupServer {
@ -69,4 +70,15 @@ DNSName DNSName::parse(const u8* data, size_t& offset, size_t max_offset, size_t
}
}
OutputStream& operator<<(OutputStream& stream, const DNSName& name)
{
auto parts = name.as_string().split_view('.');
for (auto& part : parts) {
stream << (u8)part.length();
stream << part.bytes();
}
stream << '\0';
return stream;
}
}

View File

@ -27,6 +27,7 @@
#pragma once
#include <AK/Forward.h>
#include <AK/String.h>
namespace LookupServer {
@ -43,4 +44,6 @@ private:
String m_name;
};
OutputStream& operator<<(OutputStream& stream, const DNSName&);
}

View File

@ -83,22 +83,12 @@ ByteBuffer DNSPacket::to_byte_buffer() const
stream << ReadonlyBytes { &header, sizeof(header) };
for (auto& question : m_questions) {
auto parts = question.name().as_string().split('.');
for (auto& part : parts) {
stream << (u8)part.length();
stream << part.bytes();
}
stream << '\0';
stream << question.name();
stream << htons(question.record_type());
stream << htons(question.class_code());
}
for (auto& answer : m_answers) {
auto parts = answer.name().as_string().split('.');
for (auto& part : parts) {
stream << (u8)part.length();
stream << part.bytes();
}
stream << '\0';
stream << answer.name();
stream << htons(answer.type());
stream << htons(answer.class_code());
stream << htonl(answer.ttl());