dns_message_parser/rr/
enums.rs

1pub use super::{
2    A, AAAA, AFSDB, APL, CAA, CNAME, DNAME, DNSKEY, DS, EID, EUI48, EUI64, GPOS, HINFO, ISDN, KX,
3    L32, L64, LOC, LP, MB, MD, MF, MG, MINFO, MR, MX, NID, NIMLOC, NS, NSAP, NULL, OPT, PTR, PX,
4    RP, RT, SOA, SRV, SSHFP, TXT, URI, WKS, X25,
5};
6use crate::rr::draft_ietf_dnsop_svcb_https::ServiceBinding;
7use std::fmt::{Display, Formatter, Result as FmtResult};
8
9try_from_enum_to_integer! {
10    #[repr(u16)]
11    /// The [class] field in the [resource records].
12    ///
13    /// [class]: https://tools.ietf.org/html/rfc1035#section-3.2.4
14    /// [resource records]: crate::rr::RR
15    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
16    pub enum Class {
17        /// The Internet class.
18        IN = 1,
19        /// The CSNET class. (obsolete)
20        CS = 2,
21        /// The CHAOS class.
22        CH = 3,
23        /// The Hesiod class.
24        HS = 4,
25    }
26}
27
28try_from_enum_to_integer! {
29    #[repr(u16)]
30    /// The [type] field in the [resource records].
31    ///
32    /// [type]: https://tools.ietf.org/html/rfc1035#section-3.2.2
33    /// [resource records]: crate::rr::RR
34    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
35    pub enum Type {
36        /// The [IPv4] [host address] type.
37        ///
38        /// [IPv4]: https://tools.ietf.org/html/rfc791
39        /// [host address]: https://tools.ietf.org/html/rfc1035#section-3.4.1
40        A = 1,
41        /// The [authoritative name server] type.
42        ///
43        /// [authoritative name server]: https://tools.ietf.org/html/rfc1035#section-3.3.11
44        NS = 2,
45        /// The [mail destination] type. (obsolete)
46        ///
47        /// [mail destination]: https://tools.ietf.org/html/rfc1035#section-3.3.4
48        MD = 3,
49        /// The [mail forwarder] type. (obsolete)
50        ///
51        /// [mail forwarder]: https://tools.ietf.org/html/rfc1035#section-3.3.5
52        MF = 4,
53        /// The [canonical name] type.
54        ///
55        /// [canonical name]: https://tools.ietf.org/html/rfc1035#section-3.3.1
56        CNAME = 5,
57        /// The [start of a zone of authority] type.
58        ///
59        /// [start of a zone of authority]: https://tools.ietf.org/html/rfc1035#section-3.3.13
60        SOA = 6,
61        /// The [mailbox domain name] type.
62        ///
63        /// [mailbox domain name]: https://tools.ietf.org/html/rfc1035#section-3.3.3
64        MB = 7,
65        /// The [mail group member] type.
66        ///
67        /// [mail group member]: https://tools.ietf.org/html/rfc1035#section-3.3.6
68        MG = 8,
69        /// The [mail rename domain name] type.
70        ///
71        /// [mail rename domain name]: https://tools.ietf.org/html/rfc1035#section-3.3.8
72        MR = 9,
73        /// The [null] type.
74        ///
75        /// [null]: https://tools.ietf.org/html/rfc1035#section-3.3.10
76        NULL = 10,
77        /// The [well known service] description type.
78        ///
79        /// [well known service]: https://tools.ietf.org/html/rfc1035#section-3.4.2
80        WKS = 11,
81        /// The [domain name pointer] type.
82        ///
83        /// [domain name pointer]: https://tools.ietf.org/html/rfc1035#section-3.3.12
84        PTR = 12,
85        /// The [host information] type.
86        ///
87        /// [host information]: https://tools.ietf.org/html/rfc1035#section-3.3.2
88        HINFO = 13,
89        /// The [mailbox or mail list information] type.
90        ///
91        /// [mailbox or mail list information]: https://tools.ietf.org/html/rfc1035#section-3.3.7
92        MINFO = 14,
93        /// The [mail exchange] type.
94        ///
95        /// [mail exchange]: https://tools.ietf.org/html/rfc1035#section-3.3.9
96        MX = 15,
97        /// The [text] type.
98        ///
99        /// [text]: https://tools.ietf.org/html/rfc1035#section-3.3.14
100        TXT = 16,
101        /// The [responsible person] type.
102        ///
103        /// [responsible person]: https://tools.ietf.org/html/rfc1183#section-2
104        RP = 17,
105        /// The [AFS Data base location] type:
106        ///
107        /// [AFS Data base location]: https://tools.ietf.org/html/rfc1183#section-1
108        AFSDB = 18,
109        /// The [X25] type.
110        ///
111        /// [X25]: https://tools.ietf.org/html/rfc1183#section-3.1
112        X25 = 19,
113        /// The [ISDN] type.
114        ///
115        /// [ISDN]: https://tools.ietf.org/html/rfc1183#section-3.2
116        ISDN = 20,
117        /// The [route through] type.
118        ///
119        /// [route through]: https://tools.ietf.org/html/rfc1183#section-3.3
120        RT = 21,
121        /// The [NSAP] type.
122        ///
123        /// [NSAP]: https://tools.ietf.org/html/rfc1706#section-5
124        NSAP = 22,
125        /// The [NSAP pointer] type.
126        ///
127        /// [NSAP pointer]: https://tools.ietf.org/html/rfc1706#section-6
128        #[allow(non_camel_case_types)]
129        NSAP_PTR = 23,
130        SIG = 24,
131        KEY = 25,
132        /// The [X.400 pointer] type.
133        ///
134        /// [X.400 pointer]: https://tools.ietf.org/html/rfc2163#section-4
135        PX = 26,
136        /// The [geographical location] type.
137        ///
138        /// [geographical location]: https://tools.ietf.org/html/rfc1712#section-4
139        GPOS = 27,
140        /// The [IPv6] [host address] type.
141        ///
142        /// [IPv6]: https://tools.ietf.org/html/rfc2460
143        /// [host address]: https://tools.ietf.org/html/rfc3596#section-2
144        AAAA = 28,
145        /// The [location information] type.
146        ///
147        /// [location information]: https://tools.ietf.org/html/rfc1876#section-2
148        LOC = 29,
149        NXT = 30,
150        EID = 31,
151        NIMLOC = 32,
152        /// The [location of services] type.
153        ///
154        /// [location of services]: https://tools.ietf.org/html/rfc2782
155        SRV = 33,
156        ATMA = 34,
157        NAPTR = 35,
158        /// The [key exchange] type.
159        ///
160        /// [key exchange]: https://tools.ietf.org/html/rfc2230#section-3
161        KX = 36,
162        CERT = 37,
163        A6 = 38,
164        DNAME = 39,
165        SINK = 40,
166        OPT = 41,
167        APL = 42,
168        /// The [DS] type.
169        ///
170        /// [DS]: https://tools.ietf.org/html/rfc4034#section-5
171        DS = 43,
172        SSHFP = 44,
173        IPSECKEY = 45,
174        RRSIG = 46,
175        NSEC = 47,
176        /// The [DNSKEY] type.
177        ///
178        /// [DNSKEY]: https://tools.ietf.org/html/rfc4034#section-2
179        DNSKEY = 48,
180        DHCID = 49,
181        NSEC3 = 50,
182        NSEC3PARAM = 51,
183        TLSA = 52,
184        SMIMEA = 53,
185
186        HIP = 55,
187        NINFO = 56,
188        RKEY = 57,
189        TALINK = 58,
190        CDS = 59,
191        CDNSKEY = 60,
192        OPENPGPKEY = 61,
193        CSYNC = 62,
194        ZONEMD = 63,
195
196        /// The [service binding] resource record type.
197        ///
198        /// [service binding] https://datatracker.ietf.org/doc/draft-ietf-dnsop-svcb-https/
199        SVCB = 64,
200        /// The [https] resource record type.
201        ///
202        /// [https] https://datatracker.ietf.org/doc/draft-ietf-dnsop-svcb-https/
203        HTTPS = 65,
204
205        SPF = 99,
206        UINFO = 100,
207        UID = 101,
208        GID = 102,
209        UNSPEC = 103,
210        NID = 104,
211        L32 = 105,
212        L64 = 106,
213        LP = 107,
214        EUI48 = 108,
215        EUI64 = 109,
216
217        TKEY = 249,
218        TSIG = 250,
219        IXFR = 251,
220        // TODO QType?
221        URI = 256,
222        /// The [certification authority authorization] resource record type.
223        ///
224        /// [certification authority authorization]: https://tools.ietf.org/html/rfc8659
225        CAA = 257,
226        AVC = 258,
227        DOA = 259,
228        AMTRELAY = 260,
229
230        TA = 32768,
231        DLV = 32769,
232    }
233}
234
235pub trait ToType {
236    fn to_type(&self) -> Type;
237}
238
239#[derive(Debug, PartialEq, Clone, Eq)]
240pub enum RR {
241    A(A),
242    NS(NS),
243    MD(MD),
244    MF(MF),
245    CNAME(CNAME),
246    SOA(SOA),
247    MB(MB),
248    MG(MG),
249    MR(MR),
250    NULL(NULL),
251    WKS(WKS),
252    PTR(PTR),
253    HINFO(HINFO),
254    MINFO(MINFO),
255    MX(MX),
256    TXT(TXT),
257    RP(RP),
258    AFSDB(AFSDB),
259    X25(X25),
260    ISDN(ISDN),
261    RT(RT),
262    NSAP(NSAP),
263    PX(PX),
264    GPOS(GPOS),
265    AAAA(AAAA),
266    LOC(LOC),
267    NIMLOC(NIMLOC),
268    SRV(SRV),
269    KX(KX),
270    DNAME(DNAME),
271    OPT(OPT),
272    APL(APL),
273    SSHFP(SSHFP),
274    URI(URI),
275    EID(EID),
276    NID(NID),
277    L32(L32),
278    L64(L64),
279    LP(LP),
280    EUI48(EUI48),
281    EUI64(EUI64),
282    DS(DS),
283    DNSKEY(DNSKEY),
284    CAA(CAA),
285    SVCB(ServiceBinding),
286    HTTPS(ServiceBinding),
287}
288
289impl RR {
290    pub fn get_ttl(&self) -> Option<u32> {
291        match self {
292            RR::A(a) => Some(a.ttl),
293            RR::NS(ns) => Some(ns.ttl),
294            RR::MD(md) => Some(md.ttl),
295            RR::MF(mf) => Some(mf.ttl),
296            RR::CNAME(c_name) => Some(c_name.ttl),
297            RR::SOA(soa) => Some(soa.ttl),
298            RR::MB(mb) => Some(mb.ttl),
299            RR::MG(mg) => Some(mg.ttl),
300            RR::MR(mr) => Some(mr.ttl),
301            RR::NULL(null) => Some(null.ttl),
302            RR::WKS(wks) => Some(wks.ttl),
303            RR::PTR(ptr) => Some(ptr.ttl),
304            RR::HINFO(h_info) => Some(h_info.ttl),
305            RR::MINFO(m_info) => Some(m_info.ttl),
306            RR::MX(mx) => Some(mx.ttl),
307            RR::TXT(txt) => Some(txt.ttl),
308            RR::RP(rp) => Some(rp.ttl),
309            RR::AFSDB(afsdb) => Some(afsdb.ttl),
310            RR::X25(x_25) => Some(x_25.ttl),
311            RR::ISDN(isdn) => Some(isdn.ttl),
312            RR::RT(rt) => Some(rt.ttl),
313            RR::NSAP(nsap) => Some(nsap.ttl),
314            RR::PX(px) => Some(px.ttl),
315            RR::GPOS(gpos) => Some(gpos.ttl),
316            RR::AAAA(aaaa) => Some(aaaa.ttl),
317            RR::LOC(loc) => Some(loc.ttl),
318            RR::NIMLOC(nim_loc) => Some(nim_loc.ttl),
319            RR::SRV(srv) => Some(srv.ttl),
320            RR::KX(kx) => Some(kx.ttl),
321            RR::DNAME(d_name) => Some(d_name.ttl),
322            RR::OPT(_) => None,
323            RR::APL(apl) => Some(apl.ttl),
324            RR::SSHFP(ssh_fp) => Some(ssh_fp.ttl),
325            RR::NID(nid) => Some(nid.ttl),
326            RR::L32(l_32) => Some(l_32.ttl),
327            RR::L64(l_64) => Some(l_64.ttl),
328            RR::LP(lp) => Some(lp.ttl),
329            RR::EUI48(eui_48) => Some(eui_48.ttl),
330            RR::EUI64(eui_64) => Some(eui_64.ttl),
331            RR::URI(uri) => Some(uri.ttl),
332            RR::EID(eid) => Some(eid.ttl),
333            RR::DS(ds) => Some(ds.ttl),
334            RR::DNSKEY(dnskey) => Some(dnskey.ttl),
335            RR::CAA(caa) => Some(caa.ttl),
336            RR::SVCB(svcb) => Some(svcb.ttl),
337            RR::HTTPS(https) => Some(https.ttl),
338        }
339    }
340
341    pub fn get_class(&self) -> Option<Class> {
342        match self {
343            RR::A(_) => Some(Class::IN),
344            RR::NS(ns) => Some(ns.class),
345            RR::MD(md) => Some(md.class),
346            RR::MF(mf) => Some(mf.class),
347            RR::CNAME(c_name) => Some(c_name.class),
348            RR::SOA(soa) => Some(soa.class),
349            RR::MB(mb) => Some(mb.class),
350            RR::MG(mg) => Some(mg.class),
351            RR::MR(mr) => Some(mr.class),
352            RR::NULL(null) => Some(null.class),
353            RR::WKS(_) => Some(Class::IN),
354            RR::PTR(ptr) => Some(ptr.class),
355            RR::HINFO(h_info) => Some(h_info.class),
356            RR::MINFO(m_info) => Some(m_info.class),
357            RR::MX(mx) => Some(mx.class),
358            RR::TXT(txt) => Some(txt.class),
359            RR::RP(rp) => Some(rp.class),
360            RR::AFSDB(afsdb) => Some(afsdb.class),
361            RR::X25(x_25) => Some(x_25.class),
362            RR::ISDN(isdn) => Some(isdn.class),
363            RR::RT(rt) => Some(rt.class),
364            RR::NSAP(nsap) => Some(nsap.class),
365            RR::PX(px) => Some(px.class),
366            RR::GPOS(gpos) => Some(gpos.class),
367            RR::AAAA(_) => Some(Class::IN),
368            RR::LOC(loc) => Some(loc.class),
369            RR::NIMLOC(nim_loc) => Some(nim_loc.class),
370            RR::SRV(srv) => Some(srv.class),
371            RR::KX(kx) => Some(kx.class),
372            RR::DNAME(d_name) => Some(d_name.class),
373            RR::OPT(_) => None,
374            RR::APL(_) => Some(Class::IN),
375            RR::SSHFP(ssh_fp) => Some(ssh_fp.class),
376            RR::NID(nid) => Some(nid.class),
377            RR::L32(l_32) => Some(l_32.class),
378            RR::L64(l_64) => Some(l_64.class),
379            RR::LP(lp) => Some(lp.class),
380            RR::EUI48(eui_48) => Some(eui_48.class),
381            RR::EUI64(eui_64) => Some(eui_64.class),
382            RR::URI(uri) => Some(uri.class),
383            RR::EID(eid) => Some(eid.class),
384            RR::DS(ds) => Some(ds.class),
385            RR::DNSKEY(dnskey) => Some(dnskey.class),
386            RR::CAA(caa) => Some(caa.class),
387            RR::SVCB(_) => Some(Class::IN),
388            RR::HTTPS(_) => Some(Class::IN),
389        }
390    }
391}
392
393impl Display for RR {
394    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
395        match self {
396            RR::A(a) => a.fmt(f),
397            RR::NS(ns) => ns.fmt(f),
398            RR::MD(md) => md.fmt(f),
399            RR::MF(mf) => mf.fmt(f),
400            RR::CNAME(c_name) => c_name.fmt(f),
401            RR::SOA(soa) => soa.fmt(f),
402            RR::MB(mb) => mb.fmt(f),
403            RR::MG(mg) => mg.fmt(f),
404            RR::MR(mr) => mr.fmt(f),
405            RR::NULL(null) => null.fmt(f),
406            RR::WKS(wks) => wks.fmt(f),
407            RR::PTR(ptr) => ptr.fmt(f),
408            RR::HINFO(h_info) => h_info.fmt(f),
409            RR::MINFO(m_info) => m_info.fmt(f),
410            RR::MX(mx) => mx.fmt(f),
411            RR::TXT(txt) => txt.fmt(f),
412            RR::RP(rp) => rp.fmt(f),
413            RR::AFSDB(afsdb) => afsdb.fmt(f),
414            RR::X25(x_25) => x_25.fmt(f),
415            RR::ISDN(isdn) => isdn.fmt(f),
416            RR::RT(rt) => rt.fmt(f),
417            RR::NSAP(nsap) => nsap.fmt(f),
418            RR::PX(px) => px.fmt(f),
419            RR::GPOS(gpos) => gpos.fmt(f),
420            RR::AAAA(aaaa) => aaaa.fmt(f),
421            RR::LOC(loc) => loc.fmt(f),
422            RR::NIMLOC(nim_loc) => nim_loc.fmt(f),
423            RR::SRV(srv) => srv.fmt(f),
424            RR::KX(kx) => kx.fmt(f),
425            RR::DNAME(d_name) => d_name.fmt(f),
426            RR::OPT(opt) => opt.fmt(f),
427            RR::APL(apl) => apl.fmt(f),
428            RR::SSHFP(ssh_fp) => ssh_fp.fmt(f),
429            RR::NID(nid) => nid.fmt(f),
430            RR::L32(l_32) => l_32.fmt(f),
431            RR::L64(l_64) => l_64.fmt(f),
432            RR::LP(lp) => lp.fmt(f),
433            RR::EUI48(eui_48) => eui_48.fmt(f),
434            RR::EUI64(eui_64) => eui_64.fmt(f),
435            RR::URI(uri) => uri.fmt(f),
436            RR::EID(eid) => eid.fmt(f),
437            RR::DS(ds) => ds.fmt(f),
438            RR::DNSKEY(dnskey) => dnskey.fmt(f),
439            RR::CAA(caa) => caa.fmt(f),
440            RR::SVCB(svcb) => svcb.fmt(f),
441            RR::HTTPS(https) => https.fmt(f),
442        }
443    }
444}