dns_message_parser/rr/
rfc_1035.rs

1use super::{Class, NonEmptyVec};
2use crate::DomainName;
3use std::fmt::{Display, Formatter, Result as FmtResult};
4use std::net::Ipv4Addr;
5
6/// The [IPv4] [host address] resource record type.
7///
8/// [IPv4]: https://tools.ietf.org/html/rfc791
9/// [host address]: https://tools.ietf.org/html/rfc1035#section-3.4.1
10#[derive(Debug, Clone, PartialEq, Eq, Hash)]
11pub struct A {
12    pub domain_name: DomainName,
13    pub ttl: u32,
14    pub ipv4_addr: Ipv4Addr,
15}
16
17impl_to_type!(A);
18
19impl Display for A {
20    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
21        write!(
22            f,
23            "{} {} IN A {}",
24            self.domain_name, self.ttl, self.ipv4_addr
25        )
26    }
27}
28
29struct_domain_name!(
30    /// The [authoritative name server] resource record type.
31    ///
32    /// [authoritative name server]: https://tools.ietf.org/html/rfc1035#section-3.3.11
33    NS,
34    ns_d_name
35);
36
37struct_domain_name!(
38    /// The [mail destination] resource record type. (obsolete)
39    ///
40    /// [mail destination]: https://tools.ietf.org/html/rfc1035#section-3.3.4
41    MD,
42    mad_name
43);
44
45struct_domain_name!(
46    /// The [mail forwarder] resource record type. (obsolete)
47    ///
48    /// [mail forwarder]: https://tools.ietf.org/html/rfc1035#section-3.3.5
49    MF,
50    mad_name
51);
52
53struct_domain_name!(
54    /// The [canonical name] resource record type.
55    ///
56    /// [canonical name]: https://tools.ietf.org/html/rfc1035#section-3.3.1
57    CNAME,
58    c_name
59);
60
61/// The [start of a zone of authority] resource record type.
62///
63/// [start of a zone of authority]: https://tools.ietf.org/html/rfc1035#section-3.3.13
64#[derive(Debug, Clone, PartialEq, Eq, Hash)]
65pub struct SOA {
66    pub domain_name: DomainName,
67    pub ttl: u32,
68    pub class: Class,
69    pub m_name: DomainName,
70    pub r_name: DomainName,
71    pub serial: u32,
72    pub refresh: u32,
73    pub retry: u32,
74    pub expire: u32,
75    pub min_ttl: u32,
76}
77
78impl_to_type!(SOA);
79
80impl Display for SOA {
81    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
82        write!(
83            f,
84            "{} {} {} SOA {} {} ({} {} {} {} {})",
85            self.domain_name,
86            self.ttl,
87            self.class,
88            self.m_name,
89            self.r_name,
90            self.serial,
91            self.refresh,
92            self.retry,
93            self.expire,
94            self.min_ttl,
95        )
96    }
97}
98
99struct_domain_name!(
100    /// The [mailbox domain name] resource record type.
101    ///
102    /// [mailbox domain name]: https://tools.ietf.org/html/rfc1035#section-3.3.3
103    MB,
104    mad_name
105);
106
107struct_domain_name!(
108    /// The [mail group member] resource record type.
109    ///
110    /// [mail group member]: https://tools.ietf.org/html/rfc1035#section-3.3.6
111    MG,
112    mgm_name
113);
114
115struct_domain_name!(
116    /// The [mail rename domain name] resource record type.
117    ///
118    /// [mail rename domain name]: https://tools.ietf.org/html/rfc1035#section-3.3.8
119    MR,
120    new_name
121);
122
123struct_vec!(
124    /// The [null] type.
125    ///
126    /// [null]: https://tools.ietf.org/html/rfc1035#section-3.3.10
127    NULL,
128    data
129);
130
131/// The [well known service] description resource record type.
132///
133/// [well known service]: https://tools.ietf.org/html/rfc1035#section-3.4.2
134#[derive(Debug, Clone, PartialEq, Eq, Hash)]
135pub struct WKS {
136    pub domain_name: DomainName,
137    pub ttl: u32,
138    pub ipv4_addr: Ipv4Addr,
139    pub protocol: u8,
140    pub bit_map: Vec<u8>,
141}
142
143impl_to_type!(WKS);
144
145impl Display for WKS {
146    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
147        // TODO
148        write!(
149            f,
150            "{} {} IN WKS {} {:x?}",
151            self.domain_name, self.ttl, self.protocol, self.bit_map
152        )
153    }
154}
155
156struct_domain_name!(
157    /// The [domain name pointer] resource record type.
158    ///
159    /// [domain name pointer]: https://tools.ietf.org/html/rfc1035#section-3.3.12
160    PTR,
161    ptr_d_name
162);
163
164/// The [host information] resource record type.
165///
166/// [host information]: https://tools.ietf.org/html/rfc1035#section-3.3.2
167#[derive(Debug, Clone, PartialEq, Eq, Hash)]
168pub struct HINFO {
169    pub domain_name: DomainName,
170    pub ttl: u32,
171    pub class: Class,
172    pub cpu: String,
173    pub os: String,
174}
175
176impl_to_type!(HINFO);
177
178impl Display for HINFO {
179    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
180        // TODO
181        write!(
182            f,
183            "{} {} {} HINFO {} {}",
184            self.domain_name, self.ttl, self.class, self.cpu, self.os,
185        )
186    }
187}
188
189struct_domain_name_domain_name!(
190    /// The [mailbox or mail list information] resource record type.
191    ///
192    /// [mailbox or mail list information]: https://tools.ietf.org/html/rfc1035#section-3.3.7
193    MINFO,
194    r_mail_bx,
195    e_mail_bx
196);
197
198struct_u16_domain_name!(
199    /// The [mail exchange] resource record type.
200    ///
201    /// [mail exchange]: https://tools.ietf.org/html/rfc1035#section-3.3.9
202    MX,
203    preference,
204    exchange
205);
206
207/// The [text] resource record type.
208///
209/// [text]: https://tools.ietf.org/html/rfc1035#section-3.3.14
210#[derive(Debug, Clone, PartialEq, Eq, Hash)]
211pub struct TXT {
212    pub domain_name: DomainName,
213    pub ttl: u32,
214    pub class: Class,
215    pub strings: NonEmptyVec<String>,
216}
217
218impl_to_type!(TXT);
219
220impl Display for TXT {
221    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
222        write!(f, "{} {} {} TXT", self.domain_name, self.ttl, self.class,)?;
223        for string in self.strings.iter() {
224            write!(f, " \"{}\"", string.escape_default())?;
225        }
226        Ok(())
227    }
228}