mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-17 16:28:06 +03:00
DNSLookupServer: Start working on a userspace DNS resolver.
This doesn't have any server functionality just yet, but it does post decent-looking DNS queries and parse the responses.
This commit is contained in:
parent
d0559c0e27
commit
fe2fa4ac80
Notes:
sideshowbarker
2024-07-19 15:00:14 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/fe2fa4ac80e
@ -68,6 +68,11 @@ public:
|
||||
|
||||
ssize_t offset() const { return m_offset; }
|
||||
|
||||
void snip()
|
||||
{
|
||||
m_buffer.trim(m_offset);
|
||||
}
|
||||
|
||||
private:
|
||||
ByteBuffer& m_buffer;
|
||||
ssize_t m_offset { 0 };
|
||||
|
@ -12,6 +12,8 @@ $make_cmd -C ../LibM && \
|
||||
(cd ../LibM && ./install.sh) && \
|
||||
$make_cmd -C ../LibM clean && \
|
||||
$make_cmd -C ../LibM clean && \
|
||||
$make_cmd -C ../Servers/DNSLookupServer clean && \
|
||||
$make_cmd -C ../Servers/DNSLookupServer && \
|
||||
$make_cmd -C ../WindowServer clean && \
|
||||
$make_cmd -C ../WindowServer && \
|
||||
$make_cmd -C ../LibGUI clean && \
|
||||
|
@ -87,6 +87,8 @@ cp -v ../Applications/About/About mnt/bin/About
|
||||
cp -v ../Applications/TextEditor/TextEditor mnt/bin/TextEditor
|
||||
cp -v ../Applications/IRCClient/IRCClient mnt/bin/IRCClient
|
||||
ln -s IRCClient mnt/bin/irc
|
||||
cp -v ../Servers/DNSLookupServer/DNSLookupServer mnt/bin/DNSLookupServer
|
||||
ln -s DNSLookupServer mnt/bin/dns
|
||||
cp -v ../WindowServer/WindowServer mnt/bin/WindowServer
|
||||
cp -v kernel.map mnt/
|
||||
sh sync-local.sh
|
||||
|
3
Servers/DNSLookupServer/.gitignore
vendored
Normal file
3
Servers/DNSLookupServer/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
*.o
|
||||
*.d
|
||||
DNSLookupServer
|
89
Servers/DNSLookupServer/DNSPacket.h
Normal file
89
Servers/DNSLookupServer/DNSPacket.h
Normal file
@ -0,0 +1,89 @@
|
||||
#pragma once
|
||||
|
||||
#include <AK/Types.h>
|
||||
#include <Kernel/NetworkOrdered.h>
|
||||
|
||||
class [[gnu::packed]] DNSPacket {
|
||||
public:
|
||||
DNSPacket()
|
||||
: m_recursion_desired(false)
|
||||
, m_truncated(false)
|
||||
, m_authoritative_answer(false)
|
||||
, m_opcode(0)
|
||||
, m_query_or_response(false)
|
||||
, m_response_code(0)
|
||||
, m_checking_disabled(false)
|
||||
, m_authenticated_data(false)
|
||||
, m_zero(false)
|
||||
, m_recursion_available(false)
|
||||
{
|
||||
}
|
||||
|
||||
word id() const { return m_id; }
|
||||
void set_id(word w) { m_id = w; }
|
||||
|
||||
bool recursion_desired() const { return m_recursion_desired; }
|
||||
void set_recursion_desired(bool b) { m_recursion_desired = b; }
|
||||
|
||||
bool is_truncated() const { return m_truncated; }
|
||||
void set_truncated(bool b) { m_truncated = b; }
|
||||
|
||||
bool is_authoritative_answer() const { return m_authoritative_answer; }
|
||||
void set_authoritative_answer(bool b) { m_authoritative_answer = b; }
|
||||
|
||||
byte opcode() const { return m_opcode; }
|
||||
void set_opcode(byte b) { m_opcode = b; }
|
||||
|
||||
bool is_query() const { return !m_query_or_response; }
|
||||
bool is_response() const { return m_query_or_response; }
|
||||
void set_is_query() { m_query_or_response = false; }
|
||||
void set_is_response() { m_query_or_response = true; }
|
||||
|
||||
byte response_code() const { return m_response_code; }
|
||||
void set_response_code(byte b) { m_response_code = b; }
|
||||
|
||||
bool checking_disabled() const { return m_checking_disabled; }
|
||||
void set_checking_disabled(bool b) { m_checking_disabled = b; }
|
||||
|
||||
bool is_authenticated_data() const { return m_authenticated_data; }
|
||||
void set_authenticated_data(bool b) { m_authenticated_data = b; }
|
||||
|
||||
bool is_recursion_available() const { return m_recursion_available; }
|
||||
void set_recursion_available(bool b) { m_recursion_available = b; }
|
||||
|
||||
word question_count() const { return m_question_count; }
|
||||
void set_question_count(word w) { m_question_count = w; }
|
||||
|
||||
word answer_count() const { return m_answer_count; }
|
||||
void set_answer_count(word w) { m_answer_count = w; }
|
||||
|
||||
word authority_count() const { return m_authority_count; }
|
||||
void set_authority_count(word w) { m_authority_count = w; }
|
||||
|
||||
word additional_count() const { return m_additional_count; }
|
||||
void set_additional_count(word w) { m_additional_count = w; }
|
||||
|
||||
void* payload() { return this + 1; }
|
||||
const void* payload() const { return this + 1; }
|
||||
|
||||
private:
|
||||
NetworkOrdered<word> m_id;
|
||||
|
||||
bool m_recursion_desired : 1;
|
||||
bool m_truncated : 1;
|
||||
bool m_authoritative_answer : 1;
|
||||
byte m_opcode : 4;
|
||||
bool m_query_or_response : 1;
|
||||
byte m_response_code : 4;
|
||||
bool m_checking_disabled : 1;
|
||||
bool m_authenticated_data : 1;
|
||||
bool m_zero : 1;
|
||||
bool m_recursion_available : 1;
|
||||
|
||||
NetworkOrdered<word> m_question_count;
|
||||
NetworkOrdered<word> m_answer_count;
|
||||
NetworkOrdered<word> m_authority_count;
|
||||
NetworkOrdered<word> m_additional_count;
|
||||
};
|
||||
|
||||
static_assert(sizeof(DNSPacket) == 12);
|
27
Servers/DNSLookupServer/DNSRecord.h
Normal file
27
Servers/DNSLookupServer/DNSRecord.h
Normal file
@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <AK/Types.h>
|
||||
#include <Kernel/NetworkOrdered.h>
|
||||
|
||||
class [[gnu::packed]] DNSRecord {
|
||||
public:
|
||||
DNSRecord() { }
|
||||
|
||||
word name() const { return m_name; }
|
||||
word type() const { return m_type; }
|
||||
word record_class() const { return m_class; }
|
||||
dword ttl() const { return m_ttl; }
|
||||
word data_length() const { return m_data_length; }
|
||||
|
||||
void* data() { return this + 1; }
|
||||
const void* data() const { return this + 1; }
|
||||
|
||||
private:
|
||||
NetworkOrdered<word> m_name;
|
||||
NetworkOrdered<word> m_type;
|
||||
NetworkOrdered<word> m_class;
|
||||
NetworkOrdered<dword> m_ttl;
|
||||
NetworkOrdered<word> m_data_length;
|
||||
};
|
||||
|
||||
static_assert(sizeof(DNSRecord) == 12);
|
33
Servers/DNSLookupServer/Makefile
Normal file
33
Servers/DNSLookupServer/Makefile
Normal file
@ -0,0 +1,33 @@
|
||||
DNSLOOKUPSERVER_OBJS = \
|
||||
main.o
|
||||
|
||||
APP = DNSLookupServer
|
||||
OBJS = $(DNSLOOKUPSERVER_OBJS)
|
||||
|
||||
STANDARD_FLAGS = -std=c++17
|
||||
WARNING_FLAGS = -Wextra -Wall -Wundef -Wcast-qual -Wwrite-strings -Wimplicit-fallthrough
|
||||
FLAVOR_FLAGS = -fno-exceptions -fno-rtti
|
||||
OPTIMIZATION_FLAGS = -Os
|
||||
INCLUDE_FLAGS = -I../../ -I. -I../../LibC
|
||||
LDFLAGS = -L../../LibC
|
||||
|
||||
DEFINES = -DSERENITY -DSANITIZE_PTRS -DUSERLAND
|
||||
|
||||
CXXFLAGS = -MMD -MP $(WARNING_FLAGS) $(OPTIMIZATION_FLAGS) $(FLAVOR_FLAGS) $(STANDARD_FLAGS) $(INCLUDE_FLAGS) $(DEFINES)
|
||||
CXX = i686-pc-serenity-g++
|
||||
LD = i686-pc-serenity-ld
|
||||
AR = i686-pc-serenity-ar
|
||||
|
||||
all: $(APP)
|
||||
|
||||
$(APP): $(OBJS)
|
||||
$(LD) -o $(APP) $(LDFLAGS) $(OBJS) -lc
|
||||
|
||||
.cpp.o:
|
||||
@echo "CXX $<"; $(CXX) $(CXXFLAGS) -o $@ -c $<
|
||||
|
||||
-include $(OBJS:%.o=%.d)
|
||||
|
||||
clean:
|
||||
@echo "CLEAN"; rm -f $(APPS) $(OBJS) *.d
|
||||
|
197
Servers/DNSLookupServer/main.cpp
Normal file
197
Servers/DNSLookupServer/main.cpp
Normal file
@ -0,0 +1,197 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <unistd.h>
|
||||
#include <unistd.h>
|
||||
#include <Kernel/IPv4.h>
|
||||
#include <AK/AKString.h>
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/ByteBuffer.h>
|
||||
#include <AK/BufferStream.h>
|
||||
#include "DNSPacket.h"
|
||||
#include "DNSRecord.h"
|
||||
|
||||
#define T_A 1
|
||||
#define T_NS 2
|
||||
#define T_CNAME 5
|
||||
#define T_SOA 6
|
||||
#define T_PTR 12
|
||||
#define T_MX 15
|
||||
|
||||
#define C_IN 1
|
||||
|
||||
static Vector<IPv4Address> lookup(const String& hostname);
|
||||
static String parse_dns_name(const byte*, int& offset, int max_offset);
|
||||
|
||||
int main(int argc, char**argv)
|
||||
{
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
String hostname = "disney.com";
|
||||
|
||||
if (argc == 2) {
|
||||
hostname = argv[1];
|
||||
}
|
||||
|
||||
HashMap<String, IPv4Address> dns_cache;
|
||||
|
||||
auto ipv4_addresses = lookup(hostname);
|
||||
if (ipv4_addresses.is_empty()) {
|
||||
printf("Lookup failed\n");
|
||||
} else {
|
||||
printf("DNS lookup result:\n");
|
||||
for (auto& ipv4_address : ipv4_addresses) {
|
||||
printf(" '%s' => %s\n", hostname.characters(), ipv4_address.to_string().characters());
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static word get_next_id()
|
||||
{
|
||||
static word s_next_id = 0;
|
||||
return ++s_next_id;
|
||||
}
|
||||
|
||||
Vector<IPv4Address> lookup(const String& hostname)
|
||||
{
|
||||
// FIXME: First check if it's an IP address in a string!
|
||||
|
||||
DNSPacket request_header;
|
||||
request_header.set_id(get_next_id());
|
||||
request_header.set_is_query();
|
||||
request_header.set_opcode(0);
|
||||
request_header.set_truncated(false);
|
||||
request_header.set_recursion_desired(true);
|
||||
request_header.set_question_count(1);
|
||||
|
||||
auto buffer = ByteBuffer::create_uninitialized(1024);
|
||||
{
|
||||
BufferStream stream(buffer);
|
||||
|
||||
stream << ByteBuffer::wrap(&request_header, sizeof(request_header));
|
||||
auto parts = hostname.split('.');
|
||||
for (auto& part : parts) {
|
||||
stream << (byte)part.length();
|
||||
stream << part;
|
||||
}
|
||||
stream << '\0';
|
||||
stream << htons(T_A);
|
||||
stream << htons(C_IN);
|
||||
stream.snip();
|
||||
}
|
||||
|
||||
int fd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (fd < 0) {
|
||||
perror("socket");
|
||||
return { };
|
||||
}
|
||||
|
||||
struct timeval timeout { 5, 0 };
|
||||
int rc = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
|
||||
if (rc < 0) {
|
||||
perror("setsockopt");
|
||||
close(fd);
|
||||
return { };
|
||||
}
|
||||
|
||||
struct sockaddr_in dst_addr;
|
||||
memset(&dst_addr, 0, sizeof(dst_addr));
|
||||
|
||||
dst_addr.sin_family = AF_INET;
|
||||
dst_addr.sin_port = htons(53);
|
||||
rc = inet_pton(AF_INET, "172.20.10.1", &dst_addr.sin_addr);
|
||||
|
||||
int nsent = sendto(fd, buffer.pointer(), buffer.size(), 0,(const struct sockaddr *)&dst_addr, sizeof(dst_addr));
|
||||
if (nsent < 0) {
|
||||
perror("sendto");
|
||||
return { };
|
||||
}
|
||||
ASSERT(nsent == buffer.size());
|
||||
|
||||
struct sockaddr_in src_addr;
|
||||
socklen_t src_addr_len = sizeof(src_addr);
|
||||
byte response_buffer[4096];
|
||||
ssize_t nrecv = recvfrom(fd, response_buffer, sizeof(response_buffer) - 1, 0, (struct sockaddr*)&src_addr, &src_addr_len);
|
||||
if (nrecv < 0) {
|
||||
perror("recvfrom");
|
||||
close(fd);
|
||||
return { };
|
||||
}
|
||||
close(fd);
|
||||
|
||||
response_buffer[nrecv] = '\0';
|
||||
|
||||
if (nrecv < (int)sizeof(DNSPacket)) {
|
||||
printf("Response not big enough (%d) to be a DNS packet :(\n", nrecv);
|
||||
return { };
|
||||
}
|
||||
|
||||
auto& response_header = *(DNSPacket*)(response_buffer);
|
||||
printf("Got response (ID: %u)\n", response_header.id());
|
||||
//printf(" Question count: %u\n", response_header.question_count());
|
||||
printf(" Answer count: %u\n", response_header.answer_count());
|
||||
//printf(" Authority count: %u\n", response_header.authority_count());
|
||||
//printf("Additional count: %u\n", response_header.additional_count());
|
||||
|
||||
if (response_header.id() != request_header.id()) {
|
||||
printf("ID mismatch (%u vs %u) :(\n", response_header.id(), request_header.id());
|
||||
return { };
|
||||
}
|
||||
if (response_header.question_count() != 1) {
|
||||
printf("Question count (%u vs %u) :(\n", response_header.question_count(), request_header.question_count());
|
||||
return { };
|
||||
}
|
||||
if (response_header.answer_count() < 1) {
|
||||
printf("Not enough answers (%u) :(\n", response_header.answer_count());
|
||||
return { };
|
||||
}
|
||||
|
||||
int offset = 0;
|
||||
auto question = parse_dns_name((const byte*)response_header.payload(), offset, nrecv);
|
||||
offset += 4;
|
||||
|
||||
Vector<IPv4Address> addresses;
|
||||
|
||||
for (word i = 0; i < response_header.answer_count(); ++i) {
|
||||
auto& record = *(const DNSRecord*)(&((const byte*)response_header.payload())[offset]);
|
||||
auto ipv4_address = IPv4Address((const byte*)record.data());
|
||||
printf(" Answer #%u: (question: %s), ttl=%u, length=%u, data=%s\n",
|
||||
i,
|
||||
question.characters(),
|
||||
record.ttl(),
|
||||
record.data_length(),
|
||||
ipv4_address.to_string().characters());
|
||||
|
||||
offset += sizeof(DNSRecord) + record.data_length();
|
||||
addresses.append(ipv4_address);
|
||||
}
|
||||
|
||||
return addresses;
|
||||
}
|
||||
|
||||
static String parse_dns_name(const byte* data, int& offset, int max_offset)
|
||||
{
|
||||
Vector<char> buf;
|
||||
while (offset < max_offset) {
|
||||
byte ch = data[offset];
|
||||
if (ch == '\0') {
|
||||
++offset;
|
||||
break;
|
||||
}
|
||||
if ((ch & 0xc0) == 0xc0) {
|
||||
// FIXME: Parse referential names.
|
||||
offset += 2;
|
||||
}
|
||||
for (int i = 0; i < ch; ++i) {
|
||||
buf.append(data[offset + i + 1]);
|
||||
}
|
||||
buf.append('.');
|
||||
offset += ch + 1;
|
||||
}
|
||||
return String(buf.data(), buf.size());
|
||||
}
|
Loading…
Reference in New Issue
Block a user