Skip to content

Commit

Permalink
LookupServer: Move DNS related code into new LibDNS library
Browse files Browse the repository at this point in the history
This allows other code to use the DNSPacket class, e.g. when sent
over IPC.
  • Loading branch information
tomuta authored and linusg committed Apr 15, 2022
1 parent 0a92dbd commit be4a414
Show file tree
Hide file tree
Showing 17 changed files with 51 additions and 40 deletions.
1 change: 1 addition & 0 deletions Userland/Libraries/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions Userland/Libraries/LibDNS/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
set(SOURCES
DNSAnswer.cpp
DNSName.cpp
DNSPacket.cpp
)

serenity_lib(LibDNS dns)
target_link_libraries(LibDNS LibC)
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <AK/Stream.h>
#include <time.h>

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)
Expand All @@ -28,26 +28,26 @@ bool DNSAnswer::has_expired() const

}

ErrorOr<void> AK::Formatter<LookupServer::DNSRecordType>::format(AK::FormatBuilder& builder, LookupServer::DNSRecordType value)
ErrorOr<void> AK::Formatter<DNS::DNSRecordType>::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");
}

Expand All @@ -56,10 +56,10 @@ ErrorOr<void> AK::Formatter<LookupServer::DNSRecordType>::format(AK::FormatBuild
return {};
}

ErrorOr<void> AK::Formatter<LookupServer::DNSRecordClass>::format(AK::FormatBuilder& builder, LookupServer::DNSRecordClass value)
ErrorOr<void> AK::Formatter<DNS::DNSRecordClass>::format(AK::FormatBuilder& builder, DNS::DNSRecordClass value)
{
switch (value) {
case LookupServer::DNSRecordClass::IN:
case DNS::DNSRecordClass::IN:
return builder.put_string("IN");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <AK/String.h>
#include <AK/Types.h>

namespace LookupServer {
namespace DNS {

enum class DNSRecordType : u16 {
A = 1,
Expand Down Expand Up @@ -58,23 +58,23 @@ class DNSAnswer {

}
template<>
struct AK::Formatter<LookupServer::DNSRecordType> : StandardFormatter {
struct AK::Formatter<DNS::DNSRecordType> : StandardFormatter {
Formatter() = default;
explicit Formatter(StandardFormatter formatter)
: StandardFormatter(formatter)
{
}

ErrorOr<void> format(AK::FormatBuilder&, LookupServer::DNSRecordType);
ErrorOr<void> format(AK::FormatBuilder&, DNS::DNSRecordType);
};

template<>
struct AK::Formatter<LookupServer::DNSRecordClass> : StandardFormatter {
struct AK::Formatter<DNS::DNSRecordClass> : StandardFormatter {
Formatter() = default;
explicit Formatter(StandardFormatter formatter)
: StandardFormatter(formatter)
{
}

ErrorOr<void> format(AK::FormatBuilder&, LookupServer::DNSRecordClass);
ErrorOr<void> format(AK::FormatBuilder&, DNS::DNSRecordClass);
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <AK/Vector.h>
#include <ctype.h>

namespace LookupServer {
namespace DNS {

DNSName::DNSName(String const& name)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <AK/Forward.h>
#include <AK/String.h>

namespace LookupServer {
namespace DNS {

class DNSName {
public:
Expand Down Expand Up @@ -40,8 +40,8 @@ OutputStream& operator<<(OutputStream& stream, DNSName const&);
}

template<>
struct AK::Formatter<LookupServer::DNSName> : Formatter<StringView> {
ErrorOr<void> format(FormatBuilder& builder, LookupServer::DNSName const& value)
struct AK::Formatter<DNS::DNSName> : Formatter<StringView> {
ErrorOr<void> format(FormatBuilder& builder, DNS::DNSName const& value)
{
return Formatter<StringView>::format(builder, value.as_string());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <arpa/inet.h>
#include <stdlib.h>

namespace LookupServer {
namespace DNS {

void DNSPacket::add_question(DNSQuestion const& question)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <AK/Types.h>
#include <AK/Vector.h>

namespace LookupServer {
namespace DNS {

enum class ShouldRandomizeCase {
No = 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <AK/Endian.h>
#include <AK/Types.h>

namespace LookupServer {
namespace DNS {

class [[gnu::packed]] DNSPacketHeader {
public:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "DNSName.h"
#include <AK/Types.h>

namespace LookupServer {
namespace DNS {

#define MDNS_WANTS_UNICAST_RESPONSE 0x8000

Expand Down
5 changes: 1 addition & 4 deletions Userland/Services/LookupServer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,4 +18,4 @@ set(SOURCES
)

serenity_bin(LookupServer)
target_link_libraries(LookupServer LibCore LibIPC LibMain)
target_link_libraries(LookupServer LibCore LibDNS LibIPC LibMain)
4 changes: 3 additions & 1 deletion Userland/Services/LookupServer/ConnectionFromClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
*/

#include "ConnectionFromClient.h"
#include "DNSPacket.h"
#include "LookupServer.h"
#include <AK/IPv4Address.h>
#include <LibDNS/DNSPacket.h>

namespace LookupServer {

using namespace DNS;

static HashMap<int, RefPtr<ConnectionFromClient>> s_connections;

ConnectionFromClient::ConnectionFromClient(NonnullOwnPtr<Core::Stream::LocalSocket> socket, int client_id)
Expand Down
4 changes: 3 additions & 1 deletion Userland/Services/LookupServer/DNSServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
*/

#include "DNSServer.h"
#include "DNSPacket.h"
#include "LookupServer.h"
#include <AK/IPv4Address.h>
#include <LibDNS/DNSPacket.h>

namespace LookupServer {

using namespace DNS;

DNSServer::DNSServer(Object* parent)
: Core::UDPServer(parent)
{
Expand Down
2 changes: 1 addition & 1 deletion Userland/Services/LookupServer/LookupServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include "LookupServer.h"
#include "ConnectionFromClient.h"
#include "DNSPacket.h"
#include <AK/Debug.h>
#include <AK/HashMap.h>
#include <AK/Random.h>
Expand All @@ -16,6 +15,7 @@
#include <LibCore/File.h>
#include <LibCore/LocalServer.h>
#include <LibCore/Stream.h>
#include <LibDNS/DNSPacket.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
Expand Down
6 changes: 3 additions & 3 deletions Userland/Services/LookupServer/LookupServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
#pragma once

#include "ConnectionFromClient.h"
#include "DNSName.h"
#include "DNSPacket.h"
#include "DNSServer.h"
#include "MulticastDNS.h"
#include <LibCore/FileWatcher.h>
#include <LibCore/Object.h>
#include <LibDNS/DNSName.h>
#include <LibDNS/DNSPacket.h>
#include <LibIPC/MultiServer.h>

namespace LookupServer {

class DNSAnswer;
using namespace DNS;

class LookupServer final : public Core::Object {
C_OBJECT(LookupServer);
Expand Down
1 change: 0 additions & 1 deletion Userland/Services/LookupServer/MulticastDNS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

#include "MulticastDNS.h"
#include "DNSPacket.h"
#include <AK/IPv4Address.h>
#include <AK/JsonArray.h>
#include <AK/JsonObject.h>
Expand Down
8 changes: 5 additions & 3 deletions Userland/Services/LookupServer/MulticastDNS.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@

#pragma once

#include "DNSAnswer.h"
#include "DNSName.h"
#include "DNSPacket.h"
#include <AK/IPv4Address.h>
#include <LibCore/UDPServer.h>
#include <LibDNS/DNSAnswer.h>
#include <LibDNS/DNSName.h>
#include <LibDNS/DNSPacket.h>
#include <netinet/in.h>

namespace LookupServer {

using namespace DNS;

class MulticastDNS : public Core::UDPServer {
C_OBJECT(MulticastDNS)
public:
Expand Down

0 comments on commit be4a414

Please sign in to comment.