summaryrefslogtreecommitdiff
path: root/Userland/Services/LookupServer/DNSAnswer.cpp
blob: 06f8b516c0e9964ddae7a45cb3e7c70e4d6ba1eb (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
/*
 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "DNSAnswer.h"
#include <AK/Stream.h>
#include <time.h>

namespace LookupServer {

DNSAnswer::DNSAnswer(DNSName const& name, DNSRecordType type, DNSRecordClass class_code, u32 ttl, String const& record_data, bool mdns_cache_flush)
    : m_name(name)
    , m_type(type)
    , m_class_code(class_code)
    , m_ttl(ttl)
    , m_record_data(record_data)
    , m_mdns_cache_flush(mdns_cache_flush)
{
    time(&m_received_time);
}

bool DNSAnswer::has_expired() const
{
    return time(nullptr) >= m_received_time + m_ttl;
}

}

ErrorOr<void> AK::Formatter<LookupServer::DNSRecordType>::format(AK::FormatBuilder& builder, LookupServer::DNSRecordType value)
{
    switch (value) {
    case LookupServer::DNSRecordType::A:
        return builder.put_string("A");
    case LookupServer::DNSRecordType::NS:
        return builder.put_string("NS");
    case LookupServer::DNSRecordType::CNAME:
        return builder.put_string("CNAME");
    case LookupServer::DNSRecordType::SOA:
        return builder.put_string("SOA");
    case LookupServer::DNSRecordType::PTR:
        return builder.put_string("PTR");
    case LookupServer::DNSRecordType::MX:
        return builder.put_string("MX");
    case LookupServer::DNSRecordType::TXT:
        return builder.put_string("TXT");
    case LookupServer::DNSRecordType::AAAA:
        return builder.put_string("AAAA");
    case LookupServer::DNSRecordType::SRV:
        return builder.put_string("SRV");
    }

    TRY(builder.put_string("DNS record type "));
    TRY(builder.put_u64((u16)value));
    return {};
}

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

    TRY(builder.put_string("DNS record class "));
    TRY(builder.put_u64((u16)value));
    return {};
}