summaryrefslogtreecommitdiff
path: root/Userland/Services/LookupServer/DNSAnswer.h
blob: 4a65dad0db01d39815eb1f0b8a86ce9b319f1c4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include "DNSName.h"
#include <AK/Format.h>
#include <AK/String.h>
#include <AK/Types.h>

namespace LookupServer {

enum class DNSRecordType : u16 {
    A = 1,
    NS = 2,
    CNAME = 5,
    SOA = 6,
    PTR = 12,
    MX = 15,
    TXT = 16,
    AAAA = 28,
    SRV = 33,
};

enum class DNSRecordClass : u16 {
    IN = 1
};

#define MDNS_CACHE_FLUSH 0x8000

class DNSAnswer {
public:
    DNSAnswer(DNSName const& name, DNSRecordType type, DNSRecordClass 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; }
    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; }
    String const& record_data() const { return m_record_data; }
    bool mdns_cache_flush() const { return m_mdns_cache_flush; }

    bool has_expired() const;

private:
    DNSName m_name;
    DNSRecordType m_type { 0 };
    DNSRecordClass m_class_code { 0 };
    u32 m_ttl { 0 };
    time_t m_received_time { 0 };
    String m_record_data;
    bool m_mdns_cache_flush { false };
};

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

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

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

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