From be4a4144f289bafbaf81a506138f5a53ffc97ae0 Mon Sep 17 00:00:00 2001 From: Tom Date: Tue, 12 Apr 2022 23:25:07 -0600 Subject: [PATCH] LookupServer: Move DNS related code into new LibDNS library This allows other code to use the DNSPacket class, e.g. when sent over IPC. --- Userland/Libraries/CMakeLists.txt | 1 + Userland/Libraries/LibDNS/CMakeLists.txt | 8 ++++++ .../LibDNS}/DNSAnswer.cpp | 26 +++++++++---------- .../LibDNS}/DNSAnswer.h | 10 +++---- .../LibDNS}/DNSName.cpp | 2 +- .../LibDNS}/DNSName.h | 6 ++--- .../LibDNS}/DNSPacket.cpp | 2 +- .../LibDNS}/DNSPacket.h | 2 +- .../LibDNS}/DNSPacketHeader.h | 2 +- .../LibDNS}/DNSQuestion.h | 2 +- Userland/Services/LookupServer/CMakeLists.txt | 5 +--- .../LookupServer/ConnectionFromClient.cpp | 4 ++- Userland/Services/LookupServer/DNSServer.cpp | 4 ++- .../Services/LookupServer/LookupServer.cpp | 2 +- Userland/Services/LookupServer/LookupServer.h | 6 ++--- .../Services/LookupServer/MulticastDNS.cpp | 1 - Userland/Services/LookupServer/MulticastDNS.h | 8 +++--- 17 files changed, 51 insertions(+), 40 deletions(-) create mode 100644 Userland/Libraries/LibDNS/CMakeLists.txt rename Userland/{Services/LookupServer => Libraries/LibDNS}/DNSAnswer.cpp (64%) rename Userland/{Services/LookupServer => Libraries/LibDNS}/DNSAnswer.h (83%) rename Userland/{Services/LookupServer => Libraries/LibDNS}/DNSName.cpp (99%) rename Userland/{Services/LookupServer => Libraries/LibDNS}/DNSName.h (84%) rename Userland/{Services/LookupServer => Libraries/LibDNS}/DNSPacket.cpp (99%) rename Userland/{Services/LookupServer => Libraries/LibDNS}/DNSPacket.h (99%) rename Userland/{Services/LookupServer => Libraries/LibDNS}/DNSPacketHeader.h (99%) rename Userland/{Services/LookupServer => Libraries/LibDNS}/DNSQuestion.h (97%) diff --git a/Userland/Libraries/CMakeLists.txt b/Userland/Libraries/CMakeLists.txt index cf19348520..6b3cb12b76 100644 --- a/Userland/Libraries/CMakeLists.txt +++ b/Userland/Libraries/CMakeLists.txt @@ -15,6 +15,7 @@ add_subdirectory(LibDesktop) add_subdirectory(LibDeviceTree) add_subdirectory(LibDiff) add_subdirectory(LibDl) +add_subdirectory(LibDNS) add_subdirectory(LibDSP) add_subdirectory(LibEDID) add_subdirectory(LibELF) diff --git a/Userland/Libraries/LibDNS/CMakeLists.txt b/Userland/Libraries/LibDNS/CMakeLists.txt new file mode 100644 index 0000000000..332ebbd00b --- /dev/null +++ b/Userland/Libraries/LibDNS/CMakeLists.txt @@ -0,0 +1,8 @@ +set(SOURCES + DNSAnswer.cpp + DNSName.cpp + DNSPacket.cpp +) + +serenity_lib(LibDNS dns) +target_link_libraries(LibDNS LibC) diff --git a/Userland/Services/LookupServer/DNSAnswer.cpp b/Userland/Libraries/LibDNS/DNSAnswer.cpp similarity index 64% rename from Userland/Services/LookupServer/DNSAnswer.cpp rename to Userland/Libraries/LibDNS/DNSAnswer.cpp index 06f8b516c0..2997027a33 100644 --- a/Userland/Services/LookupServer/DNSAnswer.cpp +++ b/Userland/Libraries/LibDNS/DNSAnswer.cpp @@ -8,7 +8,7 @@ #include #include -namespace LookupServer { +namespace DNS { DNSAnswer::DNSAnswer(DNSName const& name, DNSRecordType type, DNSRecordClass class_code, u32 ttl, String const& record_data, bool mdns_cache_flush) : m_name(name) @@ -28,26 +28,26 @@ bool DNSAnswer::has_expired() const } -ErrorOr AK::Formatter::format(AK::FormatBuilder& builder, LookupServer::DNSRecordType value) +ErrorOr AK::Formatter::format(AK::FormatBuilder& builder, DNS::DNSRecordType value) { switch (value) { - case LookupServer::DNSRecordType::A: + case DNS::DNSRecordType::A: return builder.put_string("A"); - case LookupServer::DNSRecordType::NS: + case DNS::DNSRecordType::NS: return builder.put_string("NS"); - case LookupServer::DNSRecordType::CNAME: + case DNS::DNSRecordType::CNAME: return builder.put_string("CNAME"); - case LookupServer::DNSRecordType::SOA: + case DNS::DNSRecordType::SOA: return builder.put_string("SOA"); - case LookupServer::DNSRecordType::PTR: + case DNS::DNSRecordType::PTR: return builder.put_string("PTR"); - case LookupServer::DNSRecordType::MX: + case DNS::DNSRecordType::MX: return builder.put_string("MX"); - case LookupServer::DNSRecordType::TXT: + case DNS::DNSRecordType::TXT: return builder.put_string("TXT"); - case LookupServer::DNSRecordType::AAAA: + case DNS::DNSRecordType::AAAA: return builder.put_string("AAAA"); - case LookupServer::DNSRecordType::SRV: + case DNS::DNSRecordType::SRV: return builder.put_string("SRV"); } @@ -56,10 +56,10 @@ ErrorOr AK::Formatter::format(AK::FormatBuild return {}; } -ErrorOr AK::Formatter::format(AK::FormatBuilder& builder, LookupServer::DNSRecordClass value) +ErrorOr AK::Formatter::format(AK::FormatBuilder& builder, DNS::DNSRecordClass value) { switch (value) { - case LookupServer::DNSRecordClass::IN: + case DNS::DNSRecordClass::IN: return builder.put_string("IN"); } diff --git a/Userland/Services/LookupServer/DNSAnswer.h b/Userland/Libraries/LibDNS/DNSAnswer.h similarity index 83% rename from Userland/Services/LookupServer/DNSAnswer.h rename to Userland/Libraries/LibDNS/DNSAnswer.h index 4a65dad0db..714804aaf8 100644 --- a/Userland/Services/LookupServer/DNSAnswer.h +++ b/Userland/Libraries/LibDNS/DNSAnswer.h @@ -11,7 +11,7 @@ #include #include -namespace LookupServer { +namespace DNS { enum class DNSRecordType : u16 { A = 1, @@ -58,23 +58,23 @@ private: } template<> -struct AK::Formatter : StandardFormatter { +struct AK::Formatter : StandardFormatter { Formatter() = default; explicit Formatter(StandardFormatter formatter) : StandardFormatter(formatter) { } - ErrorOr format(AK::FormatBuilder&, LookupServer::DNSRecordType); + ErrorOr format(AK::FormatBuilder&, DNS::DNSRecordType); }; template<> -struct AK::Formatter : StandardFormatter { +struct AK::Formatter : StandardFormatter { Formatter() = default; explicit Formatter(StandardFormatter formatter) : StandardFormatter(formatter) { } - ErrorOr format(AK::FormatBuilder&, LookupServer::DNSRecordClass); + ErrorOr format(AK::FormatBuilder&, DNS::DNSRecordClass); }; diff --git a/Userland/Services/LookupServer/DNSName.cpp b/Userland/Libraries/LibDNS/DNSName.cpp similarity index 99% rename from Userland/Services/LookupServer/DNSName.cpp rename to Userland/Libraries/LibDNS/DNSName.cpp index a29cbec521..281ee41646 100644 --- a/Userland/Services/LookupServer/DNSName.cpp +++ b/Userland/Libraries/LibDNS/DNSName.cpp @@ -10,7 +10,7 @@ #include #include -namespace LookupServer { +namespace DNS { DNSName::DNSName(String const& name) { diff --git a/Userland/Services/LookupServer/DNSName.h b/Userland/Libraries/LibDNS/DNSName.h similarity index 84% rename from Userland/Services/LookupServer/DNSName.h rename to Userland/Libraries/LibDNS/DNSName.h index 375f61c1b7..74aeb447fc 100644 --- a/Userland/Services/LookupServer/DNSName.h +++ b/Userland/Libraries/LibDNS/DNSName.h @@ -10,7 +10,7 @@ #include #include -namespace LookupServer { +namespace DNS { class DNSName { public: @@ -40,8 +40,8 @@ OutputStream& operator<<(OutputStream& stream, DNSName const&); } template<> -struct AK::Formatter : Formatter { - ErrorOr format(FormatBuilder& builder, LookupServer::DNSName const& value) +struct AK::Formatter : Formatter { + ErrorOr format(FormatBuilder& builder, DNS::DNSName const& value) { return Formatter::format(builder, value.as_string()); } diff --git a/Userland/Services/LookupServer/DNSPacket.cpp b/Userland/Libraries/LibDNS/DNSPacket.cpp similarity index 99% rename from Userland/Services/LookupServer/DNSPacket.cpp rename to Userland/Libraries/LibDNS/DNSPacket.cpp index 90753cde26..305d98fa39 100644 --- a/Userland/Services/LookupServer/DNSPacket.cpp +++ b/Userland/Libraries/LibDNS/DNSPacket.cpp @@ -14,7 +14,7 @@ #include #include -namespace LookupServer { +namespace DNS { void DNSPacket::add_question(DNSQuestion const& question) { diff --git a/Userland/Services/LookupServer/DNSPacket.h b/Userland/Libraries/LibDNS/DNSPacket.h similarity index 99% rename from Userland/Services/LookupServer/DNSPacket.h rename to Userland/Libraries/LibDNS/DNSPacket.h index 60225cec0c..d531ad9073 100644 --- a/Userland/Services/LookupServer/DNSPacket.h +++ b/Userland/Libraries/LibDNS/DNSPacket.h @@ -13,7 +13,7 @@ #include #include -namespace LookupServer { +namespace DNS { enum class ShouldRandomizeCase { No = 0, diff --git a/Userland/Services/LookupServer/DNSPacketHeader.h b/Userland/Libraries/LibDNS/DNSPacketHeader.h similarity index 99% rename from Userland/Services/LookupServer/DNSPacketHeader.h rename to Userland/Libraries/LibDNS/DNSPacketHeader.h index 9b3f627122..40ddcfe946 100644 --- a/Userland/Services/LookupServer/DNSPacketHeader.h +++ b/Userland/Libraries/LibDNS/DNSPacketHeader.h @@ -9,7 +9,7 @@ #include #include -namespace LookupServer { +namespace DNS { class [[gnu::packed]] DNSPacketHeader { public: diff --git a/Userland/Services/LookupServer/DNSQuestion.h b/Userland/Libraries/LibDNS/DNSQuestion.h similarity index 97% rename from Userland/Services/LookupServer/DNSQuestion.h rename to Userland/Libraries/LibDNS/DNSQuestion.h index eaa83bb566..5897f8e2f6 100644 --- a/Userland/Services/LookupServer/DNSQuestion.h +++ b/Userland/Libraries/LibDNS/DNSQuestion.h @@ -9,7 +9,7 @@ #include "DNSName.h" #include -namespace LookupServer { +namespace DNS { #define MDNS_WANTS_UNICAST_RESPONSE 0x8000 diff --git a/Userland/Services/LookupServer/CMakeLists.txt b/Userland/Services/LookupServer/CMakeLists.txt index 05187fb073..b4be1ec03c 100644 --- a/Userland/Services/LookupServer/CMakeLists.txt +++ b/Userland/Services/LookupServer/CMakeLists.txt @@ -8,9 +8,6 @@ compile_ipc(LookupServer.ipc LookupServerEndpoint.h) compile_ipc(LookupClient.ipc LookupClientEndpoint.h) set(SOURCES - DNSAnswer.cpp - DNSName.cpp - DNSPacket.cpp DNSServer.cpp LookupServer.cpp LookupServerEndpoint.h @@ -21,4 +18,4 @@ set(SOURCES ) serenity_bin(LookupServer) -target_link_libraries(LookupServer LibCore LibIPC LibMain) +target_link_libraries(LookupServer LibCore LibDNS LibIPC LibMain) diff --git a/Userland/Services/LookupServer/ConnectionFromClient.cpp b/Userland/Services/LookupServer/ConnectionFromClient.cpp index 6212e9af9e..ad2b2918ee 100644 --- a/Userland/Services/LookupServer/ConnectionFromClient.cpp +++ b/Userland/Services/LookupServer/ConnectionFromClient.cpp @@ -5,12 +5,14 @@ */ #include "ConnectionFromClient.h" -#include "DNSPacket.h" #include "LookupServer.h" #include +#include namespace LookupServer { +using namespace DNS; + static HashMap> s_connections; ConnectionFromClient::ConnectionFromClient(NonnullOwnPtr socket, int client_id) diff --git a/Userland/Services/LookupServer/DNSServer.cpp b/Userland/Services/LookupServer/DNSServer.cpp index 7d3846f0d0..c01772fa56 100644 --- a/Userland/Services/LookupServer/DNSServer.cpp +++ b/Userland/Services/LookupServer/DNSServer.cpp @@ -5,12 +5,14 @@ */ #include "DNSServer.h" -#include "DNSPacket.h" #include "LookupServer.h" #include +#include namespace LookupServer { +using namespace DNS; + DNSServer::DNSServer(Object* parent) : Core::UDPServer(parent) { diff --git a/Userland/Services/LookupServer/LookupServer.cpp b/Userland/Services/LookupServer/LookupServer.cpp index e45bd6e697..d7a984f594 100644 --- a/Userland/Services/LookupServer/LookupServer.cpp +++ b/Userland/Services/LookupServer/LookupServer.cpp @@ -6,7 +6,6 @@ #include "LookupServer.h" #include "ConnectionFromClient.h" -#include "DNSPacket.h" #include #include #include @@ -16,6 +15,7 @@ #include #include #include +#include #include #include #include diff --git a/Userland/Services/LookupServer/LookupServer.h b/Userland/Services/LookupServer/LookupServer.h index 6f5a90d904..28a3b7c618 100644 --- a/Userland/Services/LookupServer/LookupServer.h +++ b/Userland/Services/LookupServer/LookupServer.h @@ -7,17 +7,17 @@ #pragma once #include "ConnectionFromClient.h" -#include "DNSName.h" -#include "DNSPacket.h" #include "DNSServer.h" #include "MulticastDNS.h" #include #include +#include +#include #include namespace LookupServer { -class DNSAnswer; +using namespace DNS; class LookupServer final : public Core::Object { C_OBJECT(LookupServer); diff --git a/Userland/Services/LookupServer/MulticastDNS.cpp b/Userland/Services/LookupServer/MulticastDNS.cpp index b7b4a4490e..6cc571a68a 100644 --- a/Userland/Services/LookupServer/MulticastDNS.cpp +++ b/Userland/Services/LookupServer/MulticastDNS.cpp @@ -5,7 +5,6 @@ */ #include "MulticastDNS.h" -#include "DNSPacket.h" #include #include #include diff --git a/Userland/Services/LookupServer/MulticastDNS.h b/Userland/Services/LookupServer/MulticastDNS.h index 23fc71d430..14b05960be 100644 --- a/Userland/Services/LookupServer/MulticastDNS.h +++ b/Userland/Services/LookupServer/MulticastDNS.h @@ -6,15 +6,17 @@ #pragma once -#include "DNSAnswer.h" -#include "DNSName.h" -#include "DNSPacket.h" #include #include +#include +#include +#include #include namespace LookupServer { +using namespace DNS; + class MulticastDNS : public Core::UDPServer { C_OBJECT(MulticastDNS) public: