summaryrefslogtreecommitdiff
path: root/src/dns/packet/query.rs
blob: 732b9b2a9213f427f4d434e04957a5d4c96e51b0 (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
#[derive(PartialEq, Eq, Debug, Clone, Hash, Copy)]
pub enum QueryType {
    UNKNOWN(u16),
    A,     // 1
    NS,    // 2
    CNAME, // 5
    SOA,   // 6
    PTR,   // 12
    MX,    // 15
    TXT,   // 16
    AAAA,  // 28
    SRV,   // 33
    OPT,   // 41
    CAA,   // 257
    AR,    // 1000
    AAAAR, // 1001
}

impl QueryType {
    pub fn to_num(&self) -> u16 {
        match *self {
            Self::UNKNOWN(x) => x,
            Self::A => 1,
            Self::NS => 2,
            Self::CNAME => 5,
            Self::SOA => 6,
            Self::PTR => 12,
            Self::MX => 15,
            Self::TXT => 16,
            Self::AAAA => 28,
            Self::SRV => 33,
            Self::OPT => 41,
            Self::CAA => 257,
            Self::AR => 1000,
            Self::AAAAR => 1001,
        }
    }

    pub fn from_num(num: u16) -> Self {
        match num {
            1 => Self::A,
            2 => Self::NS,
            5 => Self::CNAME,
            6 => Self::SOA,
            12 => Self::PTR,
            15 => Self::MX,
            16 => Self::TXT,
            28 => Self::AAAA,
            33 => Self::SRV,
            41 => Self::OPT,
            257 => Self::CAA,
            1000 => Self::AR,
            1001 => Self::AAAAR,
            _ => Self::UNKNOWN(num),
        }
    }

    pub fn allowed_actions(&self) -> (bool, bool) {
        // 0. duplicates allowed
        // 1. allowed to be created by database
        match self {
            QueryType::UNKNOWN(_) => (false, false),
            QueryType::A => (true, true),
            QueryType::NS => (false, true),
            QueryType::CNAME => (false, true),
            QueryType::SOA => (false, false),
            QueryType::PTR => (false, true),
            QueryType::MX => (false, true),
            QueryType::TXT => (true, true),
            QueryType::AAAA => (true, true),
            QueryType::SRV => (false, true),
            QueryType::OPT => (false, false),
            QueryType::CAA => (false, true),
            QueryType::AR => (false, true),
            QueryType::AAAAR => (false, true),
        }
    }
}