diff options
author | Tom <tomut@yahoo.com> | 2022-04-14 16:58:40 -0600 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-04-15 16:34:26 +0100 |
commit | 49de4d5f33b5186218734ac78432509a413c1580 (patch) | |
tree | 68014565ed303bcf0af4aed4e9fcfe6ea6327b58 | |
parent | a3a1fe833b22f75319fb8114bb15bb18e0ad91c1 (diff) | |
download | serenity-49de4d5f33b5186218734ac78432509a413c1580.zip |
LibDNS: Remove the 'DNS' prefix from the various type and class names
Since all types and class names live in the DNS namespace, we don't
need to spell it out twice each time.
17 files changed, 219 insertions, 219 deletions
diff --git a/Userland/Libraries/LibDNS/DNSAnswer.cpp b/Userland/Libraries/LibDNS/Answer.cpp index e395eee498..4927eb7fa7 100644 --- a/Userland/Libraries/LibDNS/DNSAnswer.cpp +++ b/Userland/Libraries/LibDNS/Answer.cpp @@ -4,7 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include "DNSAnswer.h" +#include "Answer.h" #include <AK/Stream.h> #include <LibIPC/Decoder.h> #include <LibIPC/Encoder.h> @@ -12,7 +12,7 @@ namespace DNS { -DNSAnswer::DNSAnswer(DNSName const& name, DNSRecordType type, DNSRecordClass class_code, u32 ttl, String const& record_data, bool mdns_cache_flush) +Answer::Answer(Name const& name, RecordType type, RecordClass class_code, u32 ttl, String const& record_data, bool mdns_cache_flush) : m_name(name) , m_type(type) , m_class_code(class_code) @@ -23,12 +23,12 @@ DNSAnswer::DNSAnswer(DNSName const& name, DNSRecordType type, DNSRecordClass cla time(&m_received_time); } -bool DNSAnswer::has_expired() const +bool Answer::has_expired() const { return time(nullptr) >= m_received_time + m_ttl; } -unsigned DNSAnswer::hash() const +unsigned Answer::hash() const { auto hash = pair_int_hash(CaseInsensitiveStringTraits::hash(name().as_string()), (u32)type()); hash = pair_int_hash(hash, pair_int_hash((u32)class_code(), ttl())); @@ -37,11 +37,11 @@ unsigned DNSAnswer::hash() const return hash; } -bool DNSAnswer::operator==(DNSAnswer const& other) const +bool Answer::operator==(Answer const& other) const { if (&other == this) return true; - if (!DNSName::Traits::equals(name(), other.name())) + if (!Name::Traits::equals(name(), other.name())) return false; if (type() != other.type()) return false; @@ -58,26 +58,26 @@ bool DNSAnswer::operator==(DNSAnswer const& other) const } -ErrorOr<void> AK::Formatter<DNS::DNSRecordType>::format(AK::FormatBuilder& builder, DNS::DNSRecordType value) +ErrorOr<void> AK::Formatter<DNS::RecordType>::format(AK::FormatBuilder& builder, DNS::RecordType value) { switch (value) { - case DNS::DNSRecordType::A: + case DNS::RecordType::A: return builder.put_string("A"); - case DNS::DNSRecordType::NS: + case DNS::RecordType::NS: return builder.put_string("NS"); - case DNS::DNSRecordType::CNAME: + case DNS::RecordType::CNAME: return builder.put_string("CNAME"); - case DNS::DNSRecordType::SOA: + case DNS::RecordType::SOA: return builder.put_string("SOA"); - case DNS::DNSRecordType::PTR: + case DNS::RecordType::PTR: return builder.put_string("PTR"); - case DNS::DNSRecordType::MX: + case DNS::RecordType::MX: return builder.put_string("MX"); - case DNS::DNSRecordType::TXT: + case DNS::RecordType::TXT: return builder.put_string("TXT"); - case DNS::DNSRecordType::AAAA: + case DNS::RecordType::AAAA: return builder.put_string("AAAA"); - case DNS::DNSRecordType::SRV: + case DNS::RecordType::SRV: return builder.put_string("SRV"); } @@ -86,10 +86,10 @@ ErrorOr<void> AK::Formatter<DNS::DNSRecordType>::format(AK::FormatBuilder& build return {}; } -ErrorOr<void> AK::Formatter<DNS::DNSRecordClass>::format(AK::FormatBuilder& builder, DNS::DNSRecordClass value) +ErrorOr<void> AK::Formatter<DNS::RecordClass>::format(AK::FormatBuilder& builder, DNS::RecordClass value) { switch (value) { - case DNS::DNSRecordClass::IN: + case DNS::RecordClass::IN: return builder.put_string("IN"); } @@ -100,13 +100,13 @@ ErrorOr<void> AK::Formatter<DNS::DNSRecordClass>::format(AK::FormatBuilder& buil namespace IPC { -bool encode(Encoder& encoder, DNS::DNSAnswer const& answer) +bool encode(Encoder& encoder, DNS::Answer const& answer) { encoder << answer.name().as_string() << (u16)answer.type() << (u16)answer.class_code() << answer.ttl() << answer.record_data() << answer.mdns_cache_flush(); return true; } -ErrorOr<void> decode(Decoder& decoder, DNS::DNSAnswer& answer) +ErrorOr<void> decode(Decoder& decoder, DNS::Answer& answer) { String name; TRY(decoder.decode(name)); @@ -119,7 +119,7 @@ ErrorOr<void> decode(Decoder& decoder, DNS::DNSAnswer& answer) TRY(decoder.decode(record_data)); bool cache_flush; TRY(decoder.decode(cache_flush)); - answer = { { name }, (DNS::DNSRecordType)record_type, (DNS::DNSRecordClass)class_code, ttl, record_data, cache_flush }; + answer = { { name }, (DNS::RecordType)record_type, (DNS::RecordClass)class_code, ttl, record_data, cache_flush }; return {}; } diff --git a/Userland/Libraries/LibDNS/DNSAnswer.h b/Userland/Libraries/LibDNS/Answer.h index b237aad2b1..a3d0ea7063 100644 --- a/Userland/Libraries/LibDNS/DNSAnswer.h +++ b/Userland/Libraries/LibDNS/Answer.h @@ -6,7 +6,7 @@ #pragma once -#include "DNSName.h" +#include "Name.h" #include <AK/Format.h> #include <AK/String.h> #include <AK/Traits.h> @@ -15,7 +15,7 @@ namespace DNS { -enum class DNSRecordType : u16 { +enum class RecordType : u16 { A = 1, NS = 2, CNAME = 5, @@ -27,20 +27,20 @@ enum class DNSRecordType : u16 { SRV = 33, }; -enum class DNSRecordClass : u16 { +enum class RecordClass : u16 { IN = 1 }; #define MDNS_CACHE_FLUSH 0x8000 -class DNSAnswer { +class Answer { public: - DNSAnswer() = default; - DNSAnswer(DNSName const& name, DNSRecordType type, DNSRecordClass class_code, u32 ttl, String const& record_data, bool mdns_cache_flush); + Answer() = default; + Answer(Name const& name, RecordType type, RecordClass class_code, u32 ttl, String const& record_data, bool mdns_cache_flush); - DNSName const& name() const { return m_name; } - DNSRecordType type() const { return m_type; } - DNSRecordClass class_code() const { return m_class_code; } + Name const& name() const { return m_name; } + RecordType type() const { return m_type; } + RecordClass class_code() const { return m_class_code; } u16 raw_class_code() const { return (u16)m_class_code | (m_mdns_cache_flush ? MDNS_CACHE_FLUSH : 0); } u32 ttl() const { return m_ttl; } time_t received_time() const { return m_received_time; } @@ -50,12 +50,12 @@ public: bool has_expired() const; unsigned hash() const; - bool operator==(DNSAnswer const&) const; + bool operator==(Answer const&) const; private: - DNSName m_name; - DNSRecordType m_type { 0 }; - DNSRecordClass m_class_code { 0 }; + Name m_name; + RecordType m_type { 0 }; + RecordClass m_class_code { 0 }; u32 m_ttl { 0 }; time_t m_received_time { 0 }; String m_record_data; @@ -65,36 +65,36 @@ private: } template<> -struct AK::Traits<DNS::DNSAnswer> : public GenericTraits<DNS::DNSAnswer> { +struct AK::Traits<DNS::Answer> : public GenericTraits<DNS::Answer> { static constexpr bool is_trivial() { return false; } - static unsigned hash(DNS::DNSAnswer a) { return a.hash(); } + static unsigned hash(DNS::Answer a) { return a.hash(); } }; template<> -struct AK::Formatter<DNS::DNSRecordType> : StandardFormatter { +struct AK::Formatter<DNS::RecordType> : StandardFormatter { Formatter() = default; explicit Formatter(StandardFormatter formatter) : StandardFormatter(formatter) { } - ErrorOr<void> format(AK::FormatBuilder&, DNS::DNSRecordType); + ErrorOr<void> format(AK::FormatBuilder&, DNS::RecordType); }; template<> -struct AK::Formatter<DNS::DNSRecordClass> : StandardFormatter { +struct AK::Formatter<DNS::RecordClass> : StandardFormatter { Formatter() = default; explicit Formatter(StandardFormatter formatter) : StandardFormatter(formatter) { } - ErrorOr<void> format(AK::FormatBuilder&, DNS::DNSRecordClass); + ErrorOr<void> format(AK::FormatBuilder&, DNS::RecordClass); }; namespace IPC { -bool encode(Encoder&, DNS::DNSAnswer const&); -ErrorOr<void> decode(Decoder&, DNS::DNSAnswer&); +bool encode(Encoder&, DNS::Answer const&); +ErrorOr<void> decode(Decoder&, DNS::Answer&); } diff --git a/Userland/Libraries/LibDNS/CMakeLists.txt b/Userland/Libraries/LibDNS/CMakeLists.txt index 332ebbd00b..5c0ebc6014 100644 --- a/Userland/Libraries/LibDNS/CMakeLists.txt +++ b/Userland/Libraries/LibDNS/CMakeLists.txt @@ -1,7 +1,7 @@ set(SOURCES - DNSAnswer.cpp - DNSName.cpp - DNSPacket.cpp + Answer.cpp + Name.cpp + Packet.cpp ) serenity_lib(LibDNS dns) diff --git a/Userland/Libraries/LibDNS/DNSName.h b/Userland/Libraries/LibDNS/DNSName.h deleted file mode 100644 index 993f3c145b..0000000000 --- a/Userland/Libraries/LibDNS/DNSName.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> - * Copyright (c) 2021, Sergey Bugaev <bugaevc@serenityos.org> - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#pragma once - -#include <AK/Forward.h> -#include <AK/String.h> - -namespace DNS { - -class DNSName { -public: - DNSName() = default; - DNSName(String const&); - - static DNSName parse(u8 const* data, size_t& offset, size_t max_offset, size_t recursion_level = 0); - - size_t serialized_size() const; - String const& as_string() const { return m_name; } - - void randomize_case(); - - bool operator==(DNSName const& other) const { return Traits::equals(*this, other); } - - class Traits : public AK::Traits<DNSName> { - public: - static unsigned hash(DNSName const& name); - static bool equals(DNSName const&, DNSName const&); - }; - -private: - String m_name; -}; - -OutputStream& operator<<(OutputStream& stream, DNSName const&); - -} - -template<> -struct AK::Formatter<DNS::DNSName> : Formatter<StringView> { - ErrorOr<void> format(FormatBuilder& builder, DNS::DNSName const& value) - { - return Formatter<StringView>::format(builder, value.as_string()); - } -}; diff --git a/Userland/Libraries/LibDNS/DNSName.cpp b/Userland/Libraries/LibDNS/Name.cpp index 01787a492c..e757ab0f44 100644 --- a/Userland/Libraries/LibDNS/DNSName.cpp +++ b/Userland/Libraries/LibDNS/Name.cpp @@ -5,14 +5,14 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include "DNSName.h" +#include "Name.h" #include <AK/Random.h> #include <AK/Vector.h> #include <ctype.h> namespace DNS { -DNSName::DNSName(String const& name) +Name::Name(String const& name) { if (name.ends_with('.')) m_name = name.substring(0, name.length() - 1); @@ -20,7 +20,7 @@ DNSName::DNSName(String const& name) m_name = name; } -DNSName DNSName::parse(u8 const* data, size_t& offset, size_t max_offset, size_t recursion_level) +Name Name::parse(u8 const* data, size_t& offset, size_t max_offset, size_t recursion_level) { if (recursion_level > 4) return {}; @@ -52,14 +52,14 @@ DNSName DNSName::parse(u8 const* data, size_t& offset, size_t max_offset, size_t } } -size_t DNSName::serialized_size() const +size_t Name::serialized_size() const { if (m_name.is_empty()) return 1; return m_name.length() + 2; } -void DNSName::randomize_case() +void Name::randomize_case() { StringBuilder builder; for (char c : m_name) { @@ -75,7 +75,7 @@ void DNSName::randomize_case() m_name = builder.to_string(); } -OutputStream& operator<<(OutputStream& stream, DNSName const& name) +OutputStream& operator<<(OutputStream& stream, Name const& name) { auto parts = name.as_string().split_view('.'); for (auto& part : parts) { @@ -86,12 +86,12 @@ OutputStream& operator<<(OutputStream& stream, DNSName const& name) return stream; } -unsigned DNSName::Traits::hash(DNSName const& name) +unsigned Name::Traits::hash(Name const& name) { return CaseInsensitiveStringTraits::hash(name.as_string()); } -bool DNSName::Traits::equals(DNSName const& a, DNSName const& b) +bool Name::Traits::equals(Name const& a, Name const& b) { return CaseInsensitiveStringTraits::equals(a.as_string(), b.as_string()); } diff --git a/Userland/Libraries/LibDNS/Name.h b/Userland/Libraries/LibDNS/Name.h new file mode 100644 index 0000000000..bcc7f83ece --- /dev/null +++ b/Userland/Libraries/LibDNS/Name.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> + * Copyright (c) 2021, Sergey Bugaev <bugaevc@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include <AK/Forward.h> +#include <AK/String.h> + +namespace DNS { + +class Name { +public: + Name() = default; + Name(String const&); + + static Name parse(u8 const* data, size_t& offset, size_t max_offset, size_t recursion_level = 0); + + size_t serialized_size() const; + String const& as_string() const { return m_name; } + + void randomize_case(); + + bool operator==(Name const& other) const { return Traits::equals(*this, other); } + + class Traits : public AK::Traits<Name> { + public: + static unsigned hash(Name const& name); + static bool equals(Name const&, Name const&); + }; + +private: + String m_name; +}; + +OutputStream& operator<<(OutputStream& stream, Name const&); + +} + +template<> +struct AK::Formatter<DNS::Name> : Formatter<StringView> { + ErrorOr<void> format(FormatBuilder& builder, DNS::Name const& value) + { + return Formatter<StringView>::format(builder, value.as_string()); + } +}; diff --git a/Userland/Libraries/LibDNS/DNSPacket.cpp b/Userland/Libraries/LibDNS/Packet.cpp index 305d98fa39..3ad15e8ec8 100644 --- a/Userland/Libraries/LibDNS/DNSPacket.cpp +++ b/Userland/Libraries/LibDNS/Packet.cpp @@ -5,9 +5,9 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include "DNSPacket.h" -#include "DNSName.h" -#include "DNSPacketHeader.h" +#include "Packet.h" +#include "Name.h" +#include "PacketHeader.h" #include <AK/Debug.h> #include <AK/MemoryStream.h> #include <AK/StringBuilder.h> @@ -16,23 +16,23 @@ namespace DNS { -void DNSPacket::add_question(DNSQuestion const& question) +void Packet::add_question(Question const& question) { m_questions.empend(question); VERIFY(m_questions.size() <= UINT16_MAX); } -void DNSPacket::add_answer(DNSAnswer const& answer) +void Packet::add_answer(Answer const& answer) { m_answers.empend(answer); VERIFY(m_answers.size() <= UINT16_MAX); } -ByteBuffer DNSPacket::to_byte_buffer() const +ByteBuffer Packet::to_byte_buffer() const { - DNSPacketHeader header; + PacketHeader header; header.set_id(m_id); if (is_query()) header.set_is_query(); @@ -62,8 +62,8 @@ ByteBuffer DNSPacket::to_byte_buffer() const stream << htons((u16)answer.type()); stream << htons(answer.raw_class_code()); stream << htonl(answer.ttl()); - if (answer.type() == DNSRecordType::PTR) { - DNSName name { answer.record_data() }; + if (answer.type() == RecordType::PTR) { + Name name { answer.record_data() }; stream << htons(name.serialized_size()); stream << name; } else { @@ -96,21 +96,21 @@ private: static_assert(sizeof(DNSRecordWithoutName) == 10); -Optional<DNSPacket> DNSPacket::from_raw_packet(u8 const* raw_data, size_t raw_size) +Optional<Packet> Packet::from_raw_packet(u8 const* raw_data, size_t raw_size) { - if (raw_size < sizeof(DNSPacketHeader)) { - dbgln("DNS response not large enough ({} out of {}) to be a DNS packet.", raw_size, sizeof(DNSPacketHeader)); + if (raw_size < sizeof(PacketHeader)) { + dbgln("DNS response not large enough ({} out of {}) to be a DNS packet.", raw_size, sizeof(PacketHeader)); return {}; } - auto& header = *(DNSPacketHeader const*)(raw_data); + auto& header = *(PacketHeader const*)(raw_data); dbgln_if(LOOKUPSERVER_DEBUG, "Got packet (ID: {})", header.id()); dbgln_if(LOOKUPSERVER_DEBUG, " Question count: {}", header.question_count()); dbgln_if(LOOKUPSERVER_DEBUG, " Answer count: {}", header.answer_count()); dbgln_if(LOOKUPSERVER_DEBUG, " Authority count: {}", header.authority_count()); dbgln_if(LOOKUPSERVER_DEBUG, "Additional count: {}", header.additional_count()); - DNSPacket packet; + Packet packet; packet.m_id = header.id(); packet.m_query_or_response = header.is_response(); packet.m_code = header.response_code(); @@ -119,10 +119,10 @@ Optional<DNSPacket> DNSPacket::from_raw_packet(u8 const* raw_data, size_t raw_si if (packet.code() != Code::NOERROR) return packet; - size_t offset = sizeof(DNSPacketHeader); + size_t offset = sizeof(PacketHeader); for (u16 i = 0; i < header.question_count(); i++) { - auto name = DNSName::parse(raw_data, offset, raw_size); + auto name = Name::parse(raw_data, offset, raw_size); struct RawDNSAnswerQuestion { NetworkOrdered<u16> record_type; NetworkOrdered<u16> class_code; @@ -130,14 +130,14 @@ Optional<DNSPacket> DNSPacket::from_raw_packet(u8 const* raw_data, size_t raw_si auto& record_and_class = *(RawDNSAnswerQuestion const*)&raw_data[offset]; u16 class_code = record_and_class.class_code & ~MDNS_WANTS_UNICAST_RESPONSE; bool mdns_wants_unicast_response = record_and_class.class_code & MDNS_WANTS_UNICAST_RESPONSE; - packet.m_questions.empend(name, (DNSRecordType)(u16)record_and_class.record_type, (DNSRecordClass)class_code, mdns_wants_unicast_response); + packet.m_questions.empend(name, (RecordType)(u16)record_and_class.record_type, (RecordClass)class_code, mdns_wants_unicast_response); offset += 4; auto& question = packet.m_questions.last(); dbgln_if(LOOKUPSERVER_DEBUG, "Question #{}: name=_{}_, type={}, class={}", i, question.name(), question.record_type(), question.class_code()); } for (u16 i = 0; i < header.answer_count(); ++i) { - auto name = DNSName::parse(raw_data, offset, raw_size); + auto name = Name::parse(raw_data, offset, raw_size); auto& record = *(DNSRecordWithoutName const*)(&raw_data[offset]); @@ -145,21 +145,21 @@ Optional<DNSPacket> DNSPacket::from_raw_packet(u8 const* raw_data, size_t raw_si offset += sizeof(DNSRecordWithoutName); - switch ((DNSRecordType)record.type()) { - case DNSRecordType::PTR: { + switch ((RecordType)record.type()) { + case RecordType::PTR: { size_t dummy_offset = offset; - data = DNSName::parse(raw_data, dummy_offset, raw_size).as_string(); + data = Name::parse(raw_data, dummy_offset, raw_size).as_string(); break; } - case DNSRecordType::CNAME: + case RecordType::CNAME: // Fall through - case DNSRecordType::A: + case RecordType::A: // Fall through - case DNSRecordType::TXT: + case RecordType::TXT: // Fall through - case DNSRecordType::AAAA: + case RecordType::AAAA: // Fall through - case DNSRecordType::SRV: + case RecordType::SRV: data = { record.data(), record.data_length() }; break; default: @@ -170,7 +170,7 @@ Optional<DNSPacket> DNSPacket::from_raw_packet(u8 const* raw_data, size_t raw_si dbgln_if(LOOKUPSERVER_DEBUG, "Answer #{}: name=_{}_, type={}, ttl={}, length={}, data=_{}_", i, name, record.type(), record.ttl(), record.data_length(), data); u16 class_code = record.record_class() & ~MDNS_CACHE_FLUSH; bool mdns_cache_flush = record.record_class() & MDNS_CACHE_FLUSH; - packet.m_answers.empend(name, (DNSRecordType)record.type(), (DNSRecordClass)class_code, record.ttl(), data, mdns_cache_flush); + packet.m_answers.empend(name, (RecordType)record.type(), (RecordClass)class_code, record.ttl(), data, mdns_cache_flush); offset += record.data_length(); } diff --git a/Userland/Libraries/LibDNS/DNSPacket.h b/Userland/Libraries/LibDNS/Packet.h index d531ad9073..871c60173d 100644 --- a/Userland/Libraries/LibDNS/DNSPacket.h +++ b/Userland/Libraries/LibDNS/Packet.h @@ -7,8 +7,8 @@ #pragma once -#include "DNSAnswer.h" -#include "DNSQuestion.h" +#include "Answer.h" +#include "Question.h" #include <AK/Optional.h> #include <AK/Types.h> #include <AK/Vector.h> @@ -20,11 +20,11 @@ enum class ShouldRandomizeCase { Yes }; -class DNSPacket { +class Packet { public: - DNSPacket() = default; + Packet() = default; - static Optional<DNSPacket> from_raw_packet(u8 const*, size_t); + static Optional<Packet> from_raw_packet(u8 const*, size_t); ByteBuffer to_byte_buffer() const; bool is_query() const { return !m_query_or_response; } @@ -41,8 +41,8 @@ public: u16 id() const { return m_id; } void set_id(u16 id) { m_id = id; } - Vector<DNSQuestion> const& questions() const { return m_questions; } - Vector<DNSAnswer> const& answers() const { return m_answers; } + Vector<Question> const& questions() const { return m_questions; } + Vector<Answer> const& answers() const { return m_answers; } u16 question_count() const { @@ -56,8 +56,8 @@ public: return m_answers.size(); } - void add_question(DNSQuestion const&); - void add_answer(DNSAnswer const&); + void add_question(Question const&); + void add_answer(Answer const&); enum class Code : u8 { NOERROR = 0, @@ -82,8 +82,8 @@ private: bool m_query_or_response { false }; bool m_recursion_desired { true }; bool m_recursion_available { true }; - Vector<DNSQuestion> m_questions; - Vector<DNSAnswer> m_answers; + Vector<Question> m_questions; + Vector<Answer> m_answers; }; } diff --git a/Userland/Libraries/LibDNS/DNSPacketHeader.h b/Userland/Libraries/LibDNS/PacketHeader.h index 40ddcfe946..166e061f61 100644 --- a/Userland/Libraries/LibDNS/DNSPacketHeader.h +++ b/Userland/Libraries/LibDNS/PacketHeader.h @@ -11,9 +11,9 @@ namespace DNS { -class [[gnu::packed]] DNSPacketHeader { +class [[gnu::packed]] PacketHeader { public: - DNSPacketHeader() + PacketHeader() : m_recursion_desired(false) , m_truncated(false) , m_authoritative_answer(false) @@ -94,6 +94,6 @@ private: NetworkOrdered<u16> m_additional_count; }; -static_assert(sizeof(DNSPacketHeader) == 12); +static_assert(sizeof(PacketHeader) == 12); } diff --git a/Userland/Libraries/LibDNS/DNSQuestion.h b/Userland/Libraries/LibDNS/Question.h index 5897f8e2f6..ccbb7825e6 100644 --- a/Userland/Libraries/LibDNS/DNSQuestion.h +++ b/Userland/Libraries/LibDNS/Question.h @@ -6,16 +6,16 @@ #pragma once -#include "DNSName.h" +#include "Name.h" #include <AK/Types.h> namespace DNS { #define MDNS_WANTS_UNICAST_RESPONSE 0x8000 -class DNSQuestion { +class Question { public: - DNSQuestion(DNSName const& name, DNSRecordType record_type, DNSRecordClass class_code, bool mdns_wants_unicast_response) + Question(Name const& name, RecordType record_type, RecordClass class_code, bool mdns_wants_unicast_response) : m_name(name) , m_record_type(record_type) , m_class_code(class_code) @@ -23,16 +23,16 @@ public: { } - DNSRecordType record_type() const { return m_record_type; } - DNSRecordClass class_code() const { return m_class_code; } + RecordType record_type() const { return m_record_type; } + RecordClass class_code() const { return m_class_code; } u16 raw_class_code() const { return (u16)m_class_code | (m_mdns_wants_unicast_response ? MDNS_WANTS_UNICAST_RESPONSE : 0); } - DNSName const& name() const { return m_name; } + Name const& name() const { return m_name; } bool mdns_wants_unicast_response() const { return m_mdns_wants_unicast_response; } private: - DNSName m_name; - DNSRecordType m_record_type { 0 }; - DNSRecordClass m_class_code { 0 }; + Name m_name; + RecordType m_record_type { 0 }; + RecordClass m_class_code { 0 }; bool m_mdns_wants_unicast_response { false }; }; diff --git a/Userland/Libraries/LibTLS/Certificate.cpp b/Userland/Libraries/LibTLS/Certificate.cpp index 7ef025013e..a746b0663f 100644 --- a/Userland/Libraries/LibTLS/Certificate.cpp +++ b/Userland/Libraries/LibTLS/Certificate.cpp @@ -409,7 +409,7 @@ Optional<Certificate> Certificate::parse_asn1(ReadonlyBytes buffer, bool) break; case 2: { // DNS Name - READ_OBJECT_OR_FAIL(IA5String, StringView, name, "Certificate::TBSCertificate::Extensions::$::Extension::extension_value::SubjectAlternativeName::$::DNSName"); + READ_OBJECT_OR_FAIL(IA5String, StringView, name, "Certificate::TBSCertificate::Extensions::$::Extension::extension_value::SubjectAlternativeName::$::Name"); certificate.SAN.append(name); break; } diff --git a/Userland/Services/LookupServer/ConnectionFromClient.cpp b/Userland/Services/LookupServer/ConnectionFromClient.cpp index ad2b2918ee..1fcdce6c32 100644 --- a/Userland/Services/LookupServer/ConnectionFromClient.cpp +++ b/Userland/Services/LookupServer/ConnectionFromClient.cpp @@ -7,7 +7,7 @@ #include "ConnectionFromClient.h" #include "LookupServer.h" #include <AK/IPv4Address.h> -#include <LibDNS/DNSPacket.h> +#include <LibDNS/Packet.h> namespace LookupServer { @@ -28,7 +28,7 @@ void ConnectionFromClient::die() Messages::LookupServer::LookupNameResponse ConnectionFromClient::lookup_name(String const& name) { - auto maybe_answers = LookupServer::the().lookup(name, DNSRecordType::A); + auto maybe_answers = LookupServer::the().lookup(name, RecordType::A); if (maybe_answers.is_error()) { dbgln("LookupServer: Failed to lookup A record: {}", maybe_answers.error()); return { 1, {} }; @@ -53,7 +53,7 @@ Messages::LookupServer::LookupAddressResponse ConnectionFromClient::lookup_addre ip_address[1], ip_address[0]); - auto maybe_answers = LookupServer::the().lookup(name, DNSRecordType::PTR); + auto maybe_answers = LookupServer::the().lookup(name, RecordType::PTR); if (maybe_answers.is_error()) { dbgln("LookupServer: Failed to lookup PTR record: {}", maybe_answers.error()); return { 1, String() }; diff --git a/Userland/Services/LookupServer/DNSServer.cpp b/Userland/Services/LookupServer/DNSServer.cpp index c01772fa56..8e73fe617c 100644 --- a/Userland/Services/LookupServer/DNSServer.cpp +++ b/Userland/Services/LookupServer/DNSServer.cpp @@ -7,7 +7,7 @@ #include "DNSServer.h" #include "LookupServer.h" #include <AK/IPv4Address.h> -#include <LibDNS/DNSPacket.h> +#include <LibDNS/Packet.h> namespace LookupServer { @@ -29,7 +29,7 @@ ErrorOr<void> DNSServer::handle_client() { sockaddr_in client_address; auto buffer = receive(1024, client_address); - auto optional_request = DNSPacket::from_raw_packet(buffer.data(), buffer.size()); + auto optional_request = Packet::from_raw_packet(buffer.data(), buffer.size()); if (!optional_request.has_value()) { dbgln("Got an invalid DNS packet"); return {}; @@ -43,12 +43,12 @@ ErrorOr<void> DNSServer::handle_client() LookupServer& lookup_server = LookupServer::the(); - DNSPacket response; + Packet response; response.set_is_response(); response.set_id(request.id()); for (auto& question : request.questions()) { - if (question.class_code() != DNSRecordClass::IN) + if (question.class_code() != RecordClass::IN) continue; response.add_question(question); auto answers = TRY(lookup_server.lookup(question.name(), question.record_type())); @@ -58,9 +58,9 @@ ErrorOr<void> DNSServer::handle_client() } if (response.answer_count() == 0) - response.set_code(DNSPacket::Code::NXDOMAIN); + response.set_code(Packet::Code::NXDOMAIN); else - response.set_code(DNSPacket::Code::NOERROR); + response.set_code(Packet::Code::NOERROR); buffer = response.to_byte_buffer(); diff --git a/Userland/Services/LookupServer/LookupServer.cpp b/Userland/Services/LookupServer/LookupServer.cpp index d7a984f594..c69749e401 100644 --- a/Userland/Services/LookupServer/LookupServer.cpp +++ b/Userland/Services/LookupServer/LookupServer.cpp @@ -15,7 +15,7 @@ #include <LibCore/File.h> #include <LibCore/LocalServer.h> #include <LibCore/Stream.h> -#include <LibDNS/DNSPacket.h> +#include <LibDNS/Packet.h> #include <stdio.h> #include <time.h> #include <unistd.h> @@ -78,8 +78,8 @@ LookupServer::LookupServer() void LookupServer::load_etc_hosts() { m_etc_hosts.clear(); - auto add_answer = [this](DNSName const& name, DNSRecordType record_type, String data) { - m_etc_hosts.ensure(name).empend(name, record_type, DNSRecordClass::IN, s_static_ttl, move(data), false); + auto add_answer = [this](Name const& name, RecordType record_type, String data) { + m_etc_hosts.ensure(name).empend(name, record_type, RecordClass::IN, s_static_ttl, move(data), false); }; auto file = Core::File::construct("/etc/hosts"); @@ -114,13 +114,13 @@ void LookupServer::load_etc_hosts() auto raw_addr = maybe_address->to_in_addr_t(); - DNSName name { fields[1] }; - add_answer(name, DNSRecordType::A, String { (char const*)&raw_addr, sizeof(raw_addr) }); + Name name { fields[1] }; + add_answer(name, RecordType::A, String { (char const*)&raw_addr, sizeof(raw_addr) }); StringBuilder builder; builder.append(maybe_address->to_string_reversed()); builder.append(".in-addr.arpa"); - add_answer(builder.to_string(), DNSRecordType::PTR, name.as_string()); + add_answer(builder.to_string(), RecordType::PTR, name.as_string()); } } @@ -131,13 +131,13 @@ static String get_hostname() return buffer; } -ErrorOr<Vector<DNSAnswer>> LookupServer::lookup(DNSName const& name, DNSRecordType record_type) +ErrorOr<Vector<Answer>> LookupServer::lookup(Name const& name, RecordType record_type) { dbgln_if(LOOKUPSERVER_DEBUG, "Got request for '{}'", name.as_string()); - Vector<DNSAnswer> answers; - auto add_answer = [&](DNSAnswer const& answer) { - DNSAnswer answer_with_original_case { + Vector<Answer> answers; + auto add_answer = [&](Answer const& answer) { + Answer answer_with_original_case { name, answer.type(), answer.class_code(), @@ -160,10 +160,10 @@ ErrorOr<Vector<DNSAnswer>> LookupServer::lookup(DNSName const& name, DNSRecordTy // Second, try the hostname. // NOTE: We don't cache the hostname since it could change during runtime. - if (record_type == DNSRecordType::A && get_hostname() == name) { + if (record_type == RecordType::A && get_hostname() == name) { IPv4Address address = { 127, 0, 0, 1 }; auto raw_address = address.to_in_addr_t(); - DNSAnswer answer { name, DNSRecordType::A, DNSRecordClass::IN, s_static_ttl, String { (char const*)&raw_address, sizeof(raw_address) }, false }; + Answer answer { name, RecordType::A, RecordClass::IN, s_static_ttl, String { (char const*)&raw_address, sizeof(raw_address) }, false }; answers.append(move(answer)); return answers; } @@ -194,7 +194,7 @@ ErrorOr<Vector<DNSAnswer>> LookupServer::lookup(DNSName const& name, DNSRecordTy dbgln_if(LOOKUPSERVER_DEBUG, "Doing lookup using nameserver '{}'", nameserver); bool did_get_response = false; int retries = 3; - Vector<DNSAnswer> upstream_answers; + Vector<Answer> upstream_answers; do { upstream_answers = TRY(lookup(name, nameserver, did_get_response, record_type)); if (did_get_response) @@ -215,21 +215,21 @@ ErrorOr<Vector<DNSAnswer>> LookupServer::lookup(DNSName const& name, DNSRecordTy // Sixth, fail. if (answers.is_empty()) { dbgln("Tried all nameservers but never got a response :("); - return Vector<DNSAnswer> {}; + return Vector<Answer> {}; } return answers; } -ErrorOr<Vector<DNSAnswer>> LookupServer::lookup(DNSName const& name, String const& nameserver, bool& did_get_response, DNSRecordType record_type, ShouldRandomizeCase should_randomize_case) +ErrorOr<Vector<Answer>> LookupServer::lookup(Name const& name, String const& nameserver, bool& did_get_response, RecordType record_type, ShouldRandomizeCase should_randomize_case) { - DNSPacket request; + Packet request; request.set_is_query(); request.set_id(get_random_uniform(UINT16_MAX)); - DNSName name_in_question = name; + Name name_in_question = name; if (should_randomize_case == ShouldRandomizeCase::Yes) name_in_question.randomize_case(); - request.add_question({ name_in_question, record_type, DNSRecordClass::IN, false }); + request.add_question({ name_in_question, record_type, RecordClass::IN, false }); auto buffer = request.to_byte_buffer(); @@ -241,32 +241,32 @@ ErrorOr<Vector<DNSAnswer>> LookupServer::lookup(DNSName const& name, String cons u8 response_buffer[4096]; int nrecv = TRY(udp_socket->read({ response_buffer, sizeof(response_buffer) })); if (udp_socket->is_eof()) - return Vector<DNSAnswer> {}; + return Vector<Answer> {}; did_get_response = true; - auto o_response = DNSPacket::from_raw_packet(response_buffer, nrecv); + auto o_response = Packet::from_raw_packet(response_buffer, nrecv); if (!o_response.has_value()) - return Vector<DNSAnswer> {}; + return Vector<Answer> {}; auto& response = o_response.value(); if (response.id() != request.id()) { dbgln("LookupServer: ID mismatch ({} vs {}) :(", response.id(), request.id()); - return Vector<DNSAnswer> {}; + return Vector<Answer> {}; } - if (response.code() == DNSPacket::Code::REFUSED) { + if (response.code() == Packet::Code::REFUSED) { if (should_randomize_case == ShouldRandomizeCase::Yes) { // Retry with 0x20 case randomization turned off. return lookup(name, nameserver, did_get_response, record_type, ShouldRandomizeCase::No); } - return Vector<DNSAnswer> {}; + return Vector<Answer> {}; } if (response.question_count() != request.question_count()) { dbgln("LookupServer: Question count ({} vs {}) :(", response.question_count(), request.question_count()); - return Vector<DNSAnswer> {}; + return Vector<Answer> {}; } // Verify the questions in our request and in their response match, ignoring case. @@ -280,16 +280,16 @@ ErrorOr<Vector<DNSAnswer>> LookupServer::lookup(DNSName const& name, String cons dbgln("Request and response questions do not match"); dbgln(" Request: name=_{}_, type={}, class={}", request_question.name().as_string(), response_question.record_type(), response_question.class_code()); dbgln(" Response: name=_{}_, type={}, class={}", response_question.name().as_string(), response_question.record_type(), response_question.class_code()); - return Vector<DNSAnswer> {}; + return Vector<Answer> {}; } } if (response.answer_count() < 1) { dbgln("LookupServer: No answers :("); - return Vector<DNSAnswer> {}; + return Vector<Answer> {}; } - Vector<DNSAnswer, 8> answers; + Vector<Answer, 8> answers; for (auto& answer : response.answers()) { put_in_cache(answer); if (answer.type() != record_type) @@ -300,7 +300,7 @@ ErrorOr<Vector<DNSAnswer>> LookupServer::lookup(DNSName const& name, String cons return answers; } -void LookupServer::put_in_cache(DNSAnswer const& answer) +void LookupServer::put_in_cache(Answer const& answer) { if (answer.has_expired()) return; @@ -317,7 +317,7 @@ void LookupServer::put_in_cache(DNSAnswer const& answer) if (answer.mdns_cache_flush()) { auto now = time(nullptr); - it->value.remove_all_matching([&](DNSAnswer const& other_answer) { + it->value.remove_all_matching([&](Answer const& other_answer) { if (other_answer.type() != answer.type() || other_answer.class_code() != answer.class_code()) return false; diff --git a/Userland/Services/LookupServer/LookupServer.h b/Userland/Services/LookupServer/LookupServer.h index 28a3b7c618..0b1c2c03d7 100644 --- a/Userland/Services/LookupServer/LookupServer.h +++ b/Userland/Services/LookupServer/LookupServer.h @@ -11,8 +11,8 @@ #include "MulticastDNS.h" #include <LibCore/FileWatcher.h> #include <LibCore/Object.h> -#include <LibDNS/DNSName.h> -#include <LibDNS/DNSPacket.h> +#include <LibDNS/Name.h> +#include <LibDNS/Packet.h> #include <LibIPC/MultiServer.h> namespace LookupServer { @@ -24,23 +24,23 @@ class LookupServer final : public Core::Object { public: static LookupServer& the(); - ErrorOr<Vector<DNSAnswer>> lookup(DNSName const& name, DNSRecordType record_type); + ErrorOr<Vector<Answer>> lookup(Name const& name, RecordType record_type); private: LookupServer(); void load_etc_hosts(); - void put_in_cache(DNSAnswer const&); + void put_in_cache(Answer const&); - ErrorOr<Vector<DNSAnswer>> lookup(DNSName const& hostname, String const& nameserver, bool& did_get_response, DNSRecordType record_type, ShouldRandomizeCase = ShouldRandomizeCase::Yes); + ErrorOr<Vector<Answer>> lookup(Name const& hostname, String const& nameserver, bool& did_get_response, RecordType record_type, ShouldRandomizeCase = ShouldRandomizeCase::Yes); OwnPtr<IPC::MultiServer<ConnectionFromClient>> m_server; RefPtr<DNSServer> m_dns_server; RefPtr<MulticastDNS> m_mdns; Vector<String> m_nameservers; RefPtr<Core::FileWatcher> m_file_watcher; - HashMap<DNSName, Vector<DNSAnswer>, DNSName::Traits> m_etc_hosts; - HashMap<DNSName, Vector<DNSAnswer>, DNSName::Traits> m_lookup_cache; + HashMap<Name, Vector<Answer>, Name::Traits> m_etc_hosts; + HashMap<Name, Vector<Answer>, Name::Traits> m_lookup_cache; }; } diff --git a/Userland/Services/LookupServer/MulticastDNS.cpp b/Userland/Services/LookupServer/MulticastDNS.cpp index 6cc571a68a..342c611584 100644 --- a/Userland/Services/LookupServer/MulticastDNS.cpp +++ b/Userland/Services/LookupServer/MulticastDNS.cpp @@ -52,7 +52,7 @@ MulticastDNS::MulticastDNS(Object* parent) void MulticastDNS::handle_packet() { auto buffer = receive(1024); - auto optional_packet = DNSPacket::from_raw_packet(buffer.data(), buffer.size()); + auto optional_packet = Packet::from_raw_packet(buffer.data(), buffer.size()); if (!optional_packet.has_value()) { dbgln("Got an invalid mDNS packet"); return; @@ -63,7 +63,7 @@ void MulticastDNS::handle_packet() handle_query(packet); } -void MulticastDNS::handle_query(DNSPacket const& packet) +void MulticastDNS::handle_query(Packet const& packet) { bool should_reply = false; @@ -79,19 +79,19 @@ void MulticastDNS::handle_query(DNSPacket const& packet) void MulticastDNS::announce() { - DNSPacket response; + Packet response; response.set_is_response(); - response.set_code(DNSPacket::Code::NOERROR); + response.set_code(Packet::Code::NOERROR); response.set_authoritative_answer(true); response.set_recursion_desired(false); response.set_recursion_available(false); for (auto& address : local_addresses()) { auto raw_addr = address.to_in_addr_t(); - DNSAnswer answer { + Answer answer { m_hostname, - DNSRecordType::A, - DNSRecordClass::IN, + RecordType::A, + RecordClass::IN, 120, String { (char const*)&raw_addr, sizeof(raw_addr) }, true, @@ -103,7 +103,7 @@ void MulticastDNS::announce() perror("Failed to emit response packet"); } -ErrorOr<size_t> MulticastDNS::emit_packet(DNSPacket const& packet, sockaddr_in const* destination) +ErrorOr<size_t> MulticastDNS::emit_packet(Packet const& packet, sockaddr_in const* destination) { auto buffer = packet.to_byte_buffer(); if (!destination) @@ -141,19 +141,19 @@ Vector<IPv4Address> MulticastDNS::local_addresses() const return addresses; } -Vector<DNSAnswer> MulticastDNS::lookup(DNSName const& name, DNSRecordType record_type) +Vector<Answer> MulticastDNS::lookup(Name const& name, RecordType record_type) { - DNSPacket request; + Packet request; request.set_is_query(); request.set_recursion_desired(false); - request.add_question({ name, record_type, DNSRecordClass::IN, false }); + request.add_question({ name, record_type, RecordClass::IN, false }); if (emit_packet(request).is_error()) { perror("failed to emit request packet"); return {}; } - Vector<DNSAnswer> answers; + Vector<Answer> answers; // FIXME: It would be better not to block // the main loop while we wait for a response. @@ -170,7 +170,7 @@ Vector<DNSAnswer> MulticastDNS::lookup(DNSName const& name, DNSRecordType record auto buffer = receive(1024); if (buffer.is_empty()) return {}; - auto optional_packet = DNSPacket::from_raw_packet(buffer.data(), buffer.size()); + auto optional_packet = Packet::from_raw_packet(buffer.data(), buffer.size()); if (!optional_packet.has_value()) { dbgln("Got an invalid mDNS packet"); continue; diff --git a/Userland/Services/LookupServer/MulticastDNS.h b/Userland/Services/LookupServer/MulticastDNS.h index 14b05960be..ecf1b3a274 100644 --- a/Userland/Services/LookupServer/MulticastDNS.h +++ b/Userland/Services/LookupServer/MulticastDNS.h @@ -8,9 +8,9 @@ #include <AK/IPv4Address.h> #include <LibCore/UDPServer.h> -#include <LibDNS/DNSAnswer.h> -#include <LibDNS/DNSName.h> -#include <LibDNS/DNSPacket.h> +#include <LibDNS/Answer.h> +#include <LibDNS/Name.h> +#include <LibDNS/Packet.h> #include <netinet/in.h> namespace LookupServer { @@ -20,20 +20,20 @@ using namespace DNS; class MulticastDNS : public Core::UDPServer { C_OBJECT(MulticastDNS) public: - Vector<DNSAnswer> lookup(DNSName const&, DNSRecordType record_type); + Vector<Answer> lookup(Name const&, RecordType record_type); private: explicit MulticastDNS(Object* parent = nullptr); void announce(); - ErrorOr<size_t> emit_packet(DNSPacket const&, sockaddr_in const* destination = nullptr); + ErrorOr<size_t> emit_packet(Packet const&, sockaddr_in const* destination = nullptr); void handle_packet(); - void handle_query(DNSPacket const&); + void handle_query(Packet const&); Vector<IPv4Address> local_addresses() const; - DNSName m_hostname; + Name m_hostname; static constexpr sockaddr_in mdns_addr { AF_INET, |