2020-01-26 15:53:36 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-26 15:53:36 +03:00
|
|
|
*/
|
|
|
|
|
2020-01-26 14:27:18 +03:00
|
|
|
#pragma once
|
|
|
|
|
2022-04-15 01:58:40 +03:00
|
|
|
#include "Name.h"
|
2021-05-08 00:58:51 +03:00
|
|
|
#include <AK/Format.h>
|
2020-01-26 14:27:18 +03:00
|
|
|
#include <AK/String.h>
|
2022-04-13 08:38:39 +03:00
|
|
|
#include <AK/Traits.h>
|
2020-01-26 14:27:18 +03:00
|
|
|
#include <AK/Types.h>
|
2022-04-13 08:41:02 +03:00
|
|
|
#include <LibIPC/Forward.h>
|
2020-01-26 14:27:18 +03:00
|
|
|
|
2022-04-13 08:25:07 +03:00
|
|
|
namespace DNS {
|
2021-02-04 19:07:45 +03:00
|
|
|
|
2022-04-15 01:58:40 +03:00
|
|
|
enum class RecordType : u16 {
|
2021-05-08 00:58:51 +03:00
|
|
|
A = 1,
|
|
|
|
NS = 2,
|
|
|
|
CNAME = 5,
|
|
|
|
SOA = 6,
|
|
|
|
PTR = 12,
|
|
|
|
MX = 15,
|
2021-05-07 16:03:52 +03:00
|
|
|
TXT = 16,
|
|
|
|
AAAA = 28,
|
|
|
|
SRV = 33,
|
2021-05-08 00:58:51 +03:00
|
|
|
};
|
|
|
|
|
2022-04-15 01:58:40 +03:00
|
|
|
enum class RecordClass : u16 {
|
2021-05-08 00:58:51 +03:00
|
|
|
IN = 1
|
|
|
|
};
|
|
|
|
|
2021-05-08 00:58:46 +03:00
|
|
|
#define MDNS_CACHE_FLUSH 0x8000
|
|
|
|
|
2022-04-15 01:58:40 +03:00
|
|
|
class Answer {
|
2020-01-26 14:27:18 +03:00
|
|
|
public:
|
2022-04-15 01:58:40 +03:00
|
|
|
Answer() = default;
|
|
|
|
Answer(Name const& name, RecordType type, RecordClass class_code, u32 ttl, String const& record_data, bool mdns_cache_flush);
|
2020-01-26 14:27:18 +03:00
|
|
|
|
2022-04-15 01:58:40 +03:00
|
|
|
Name const& name() const { return m_name; }
|
|
|
|
RecordType type() const { return m_type; }
|
|
|
|
RecordClass class_code() const { return m_class_code; }
|
2021-05-08 00:58:51 +03:00
|
|
|
u16 raw_class_code() const { return (u16)m_class_code | (m_mdns_cache_flush ? MDNS_CACHE_FLUSH : 0); }
|
2020-01-26 14:27:18 +03:00
|
|
|
u32 ttl() const { return m_ttl; }
|
2021-05-09 17:47:51 +03:00
|
|
|
time_t received_time() const { return m_received_time; }
|
2022-04-01 20:58:27 +03:00
|
|
|
String const& record_data() const { return m_record_data; }
|
2021-05-08 00:58:46 +03:00
|
|
|
bool mdns_cache_flush() const { return m_mdns_cache_flush; }
|
2020-01-26 14:27:18 +03:00
|
|
|
|
2020-01-26 17:42:03 +03:00
|
|
|
bool has_expired() const;
|
|
|
|
|
2022-04-13 08:38:39 +03:00
|
|
|
unsigned hash() const;
|
2022-04-15 01:58:40 +03:00
|
|
|
bool operator==(Answer const&) const;
|
2022-04-13 08:38:39 +03:00
|
|
|
|
2020-01-26 14:27:18 +03:00
|
|
|
private:
|
2022-04-15 01:58:40 +03:00
|
|
|
Name m_name;
|
|
|
|
RecordType m_type { 0 };
|
|
|
|
RecordClass m_class_code { 0 };
|
2020-01-26 14:27:18 +03:00
|
|
|
u32 m_ttl { 0 };
|
2021-05-09 17:47:51 +03:00
|
|
|
time_t m_received_time { 0 };
|
2020-01-26 14:27:18 +03:00
|
|
|
String m_record_data;
|
2021-05-08 00:58:46 +03:00
|
|
|
bool m_mdns_cache_flush { false };
|
2020-01-26 14:27:18 +03:00
|
|
|
};
|
2021-02-04 19:07:45 +03:00
|
|
|
|
|
|
|
}
|
2022-04-13 08:38:39 +03:00
|
|
|
|
|
|
|
template<>
|
2022-04-15 01:58:40 +03:00
|
|
|
struct AK::Traits<DNS::Answer> : public GenericTraits<DNS::Answer> {
|
2022-04-13 08:38:39 +03:00
|
|
|
static constexpr bool is_trivial() { return false; }
|
2022-04-15 01:58:40 +03:00
|
|
|
static unsigned hash(DNS::Answer a) { return a.hash(); }
|
2022-04-13 08:38:39 +03:00
|
|
|
};
|
|
|
|
|
2021-05-08 00:58:51 +03:00
|
|
|
template<>
|
2022-04-15 01:58:40 +03:00
|
|
|
struct AK::Formatter<DNS::RecordType> : StandardFormatter {
|
2021-05-08 00:58:51 +03:00
|
|
|
Formatter() = default;
|
|
|
|
explicit Formatter(StandardFormatter formatter)
|
|
|
|
: StandardFormatter(formatter)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-04-15 01:58:40 +03:00
|
|
|
ErrorOr<void> format(AK::FormatBuilder&, DNS::RecordType);
|
2021-05-08 00:58:51 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
2022-04-15 01:58:40 +03:00
|
|
|
struct AK::Formatter<DNS::RecordClass> : StandardFormatter {
|
2021-05-08 00:58:51 +03:00
|
|
|
Formatter() = default;
|
|
|
|
explicit Formatter(StandardFormatter formatter)
|
|
|
|
: StandardFormatter(formatter)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-04-15 01:58:40 +03:00
|
|
|
ErrorOr<void> format(AK::FormatBuilder&, DNS::RecordClass);
|
2021-05-08 00:58:51 +03:00
|
|
|
};
|
2022-04-13 08:41:02 +03:00
|
|
|
|
|
|
|
namespace IPC {
|
|
|
|
|
2022-04-15 01:58:40 +03:00
|
|
|
bool encode(Encoder&, DNS::Answer const&);
|
|
|
|
ErrorOr<void> decode(Decoder&, DNS::Answer&);
|
2022-04-13 08:41:02 +03:00
|
|
|
|
|
|
|
}
|