|
@@ -11,6 +11,7 @@
|
|
|
#include <AK/Debug.h>
|
|
|
#include <AK/MemoryStream.h>
|
|
|
#include <AK/StringBuilder.h>
|
|
|
+#include <LibCore/MemoryStream.h>
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
namespace DNS {
|
|
@@ -29,7 +30,7 @@ void Packet::add_answer(Answer const& answer)
|
|
|
VERIFY(m_answers.size() <= UINT16_MAX);
|
|
|
}
|
|
|
|
|
|
-ByteBuffer Packet::to_byte_buffer() const
|
|
|
+ErrorOr<ByteBuffer> Packet::to_byte_buffer() const
|
|
|
{
|
|
|
PacketHeader header;
|
|
|
header.set_id(m_id);
|
|
@@ -48,30 +49,32 @@ ByteBuffer Packet::to_byte_buffer() const
|
|
|
header.set_question_count(m_questions.size());
|
|
|
header.set_answer_count(m_answers.size());
|
|
|
|
|
|
- DuplexMemoryStream stream;
|
|
|
+ Core::Stream::AllocatingMemoryStream stream;
|
|
|
|
|
|
- stream << ReadonlyBytes { &header, sizeof(header) };
|
|
|
+ TRY(stream.write_trivial_value(header));
|
|
|
for (auto& question : m_questions) {
|
|
|
- stream << question.name();
|
|
|
- stream << htons((u16)question.record_type());
|
|
|
- stream << htons(question.raw_class_code());
|
|
|
+ TRY(question.name().write_to_stream(stream));
|
|
|
+ TRY(stream.write_trivial_value(htons((u16)question.record_type())));
|
|
|
+ TRY(stream.write_trivial_value(htons(question.raw_class_code())));
|
|
|
}
|
|
|
for (auto& answer : m_answers) {
|
|
|
- stream << answer.name();
|
|
|
- stream << htons((u16)answer.type());
|
|
|
- stream << htons(answer.raw_class_code());
|
|
|
- stream << htonl(answer.ttl());
|
|
|
+ TRY(answer.name().write_to_stream(stream));
|
|
|
+ TRY(stream.write_trivial_value(htons((u16)answer.type())));
|
|
|
+ TRY(stream.write_trivial_value(htons(answer.raw_class_code())));
|
|
|
+ TRY(stream.write_trivial_value(htonl(answer.ttl())));
|
|
|
if (answer.type() == RecordType::PTR) {
|
|
|
Name name { answer.record_data() };
|
|
|
- stream << htons(name.serialized_size());
|
|
|
- stream << name;
|
|
|
+ TRY(stream.write_trivial_value(htons(name.serialized_size())));
|
|
|
+ TRY(name.write_to_stream(stream));
|
|
|
} else {
|
|
|
- stream << htons(answer.record_data().length());
|
|
|
- stream << answer.record_data().bytes();
|
|
|
+ TRY(stream.write_trivial_value(htons(answer.record_data().length())));
|
|
|
+ TRY(stream.write_entire_buffer(answer.record_data().bytes()));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return stream.copy_into_contiguous_buffer();
|
|
|
+ auto buffer = TRY(ByteBuffer::create_uninitialized(stream.used_buffer_size()));
|
|
|
+ TRY(stream.read_entire_buffer(buffer));
|
|
|
+ return buffer;
|
|
|
}
|
|
|
|
|
|
class [[gnu::packed]] DNSRecordWithoutName {
|