Skip to content

Commit

Permalink
LibDNS: Remove the 'DNS' prefix from the various type and class names
Browse files Browse the repository at this point in the history
Since all types and class names live in the DNS namespace, we don't
need to spell it out twice each time.
  • Loading branch information
tomuta authored and linusg committed Apr 15, 2022
1 parent a3a1fe8 commit 49de4d5
Show file tree
Hide file tree
Showing 17 changed files with 219 additions and 219 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
* SPDX-License-Identifier: BSD-2-Clause
*/

#include "DNSAnswer.h"
#include "Answer.h"
#include <AK/Stream.h>
#include <LibIPC/Decoder.h>
#include <LibIPC/Encoder.h>
#include <time.h>

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)
Expand All @@ -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()));
Expand All @@ -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;
Expand All @@ -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");
}

Expand All @@ -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");
}

Expand All @@ -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));
Expand All @@ -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 {};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#pragma once

#include "DNSName.h"
#include "Name.h"
#include <AK/Format.h>
#include <AK/String.h>
#include <AK/Traits.h>
Expand All @@ -15,7 +15,7 @@

namespace DNS {

enum class DNSRecordType : u16 {
enum class RecordType : u16 {
A = 1,
NS = 2,
CNAME = 5,
Expand All @@ -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; }
Expand All @@ -50,12 +50,12 @@ class DNSAnswer {
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;
Expand All @@ -65,36 +65,36 @@ class DNSAnswer {
}

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&);

}
6 changes: 3 additions & 3 deletions Userland/Libraries/LibDNS/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
set(SOURCES
DNSAnswer.cpp
DNSName.cpp
DNSPacket.cpp
Answer.cpp
Name.cpp
Packet.cpp
)

serenity_lib(LibDNS dns)
Expand Down
49 changes: 0 additions & 49 deletions Userland/Libraries/LibDNS/DNSName.h

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
* 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);
else
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 {};
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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());
}
Expand Down
Loading

0 comments on commit 49de4d5

Please sign in to comment.