mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-25 19:02:07 -05:00
LookupServer: Move DNS name serialization to DNSName class
This commit is contained in:
parent
42bc5f2cc1
commit
d6f7ced4f1
3 changed files with 17 additions and 12 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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&);
|
||||
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
|
|
Loading…
Add table
Reference in a new issue