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

LookupServer: Unify DNSRequest & DNSResponse

They're really the same thing: a DNS packet can contain both questions and
answers, and there's a single bit in the header that determines whether the
packet represents a query or a response. It'll be simpler for us to represent
both types of packets using the same class.

This class can be both serialized and deserialized to/from a raw DNS packet.
This commit is contained in:
Sergey Bugaev 2021-02-06 17:48:12 +03:00 committed by Andreas Kling
parent e3135e7ca5
commit 1dad63824b
8 changed files with 145 additions and 207 deletions

View file

@ -26,8 +26,7 @@
#include "LookupServer.h"
#include "ClientConnection.h"
#include "DNSRequest.h"
#include "DNSResponse.h"
#include "DNSPacket.h"
#include <AK/ByteBuffer.h>
#include <AK/Debug.h>
#include <AK/HashMap.h>
@ -173,7 +172,9 @@ Vector<String> LookupServer::lookup(const String& hostname, const String& namese
m_lookup_cache.remove(it);
}
DNSRequest request;
DNSPacket request;
request.set_is_query();
request.set_id(arc4random_uniform(UINT16_MAX));
request.add_question(hostname, record_type, should_randomize_case);
auto buffer = request.to_byte_buffer();
@ -204,7 +205,7 @@ Vector<String> LookupServer::lookup(const String& hostname, const String& namese
did_get_response = true;
auto o_response = DNSResponse::from_raw_response(response_buffer, nrecv);
auto o_response = DNSPacket::from_raw_packet(response_buffer, nrecv);
if (!o_response.has_value())
return {};
@ -215,7 +216,7 @@ Vector<String> LookupServer::lookup(const String& hostname, const String& namese
return {};
}
if (response.code() == DNSResponse::Code::REFUSED) {
if (response.code() == DNSPacket::Code::REFUSED) {
if (should_randomize_case == ShouldRandomizeCase::Yes) {
// Retry with 0x20 case randomization turned off.
return lookup(hostname, nameserver, did_get_response, record_type, ShouldRandomizeCase::No);