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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
use crate::bail;
use crate::io::{CursorExt, DNSReadExt, SeekExt};
use crate::types::*;
use byteorder::{ReadBytesExt, BE};
use std::io;
use std::io::Cursor;
use std::io::Read;
use std::net::{Ipv4Addr, Ipv6Addr};
use std::time::Duration;
pub type A = Ipv4Addr;
pub type AAAA = Ipv6Addr;
pub type NS = String;
pub type CNAME = String;
pub type PTR = String;
pub type TXT = Vec<Vec<u8>>;
impl Record {
pub(crate) fn parse(
cur: &mut Cursor<&[u8]>,
name: String,
r#type: Type,
class: Class,
) -> io::Result<Record> {
let ttl = cur.read_u32::<BE>()?;
let len = cur.read_u16::<BE>()?;
let pos = cur.position();
let end = pos as usize + len as usize;
let mut record = cur.sub_cursor(0, end)?;
record.set_position(pos);
let resource = match r#type {
Type::A => Resource::A(parse_a(&mut record, class)?),
Type::AAAA => Resource::AAAA(parse_aaaa(&mut record, class)?),
Type::NS => Resource::NS(record.read_qname()?),
Type::SOA => Resource::SOA(SOA::parse(&mut record)?),
Type::CNAME => Resource::CNAME(record.read_qname()?),
Type::PTR => Resource::PTR(record.read_qname()?),
Type::MX => Resource::MX(MX::parse(&mut record)?),
Type::TXT => Resource::TXT(parse_txt(&mut record)?),
Type::SRV => Resource::SRV(SRV::parse(&mut record)?),
Type::Reserved | Type::OPT | Type::ANY => {
bail!(InvalidData, "invalid record type '{}'", r#type);
}
};
if record.remaining()? > 0 {
bail!(
Other,
"finished '{}' parsing record with {} bytes left over",
r#type,
record.remaining()?
);
}
cur.set_position(record.position());
Ok(Record {
name,
r#type,
class,
ttl: Duration::from_secs(ttl.into()),
resource,
})
}
}
pub struct MX {
pub preference: u16,
pub exchange: String,
}
pub struct SOA {
pub mname: String,
pub rname: String,
pub serial: u32,
pub refresh: Duration,
pub retry: Duration,
pub expire: Duration,
pub minimum: Duration,
}
pub struct SRV {
pub priority: u16,
pub weight: u16,
pub port: u16,
pub name: String,
}
fn parse_a(cur: &mut Cursor<&[u8]>, class: Class) -> io::Result<A> {
let mut buf = [0_u8; 4];
cur.read_exact(&mut buf)?;
match class {
Class::Internet => Ok(A::new(buf[0], buf[1], buf[2], buf[3])),
_ => bail!(InvalidData, "unsupported A record class '{}'", class),
}
}
fn parse_aaaa(cur: &mut Cursor<&[u8]>, class: Class) -> io::Result<AAAA> {
let mut buf = [0_u8; 16];
cur.read_exact(&mut buf)?;
match class {
Class::Internet => Ok(AAAA::from(buf)),
_ => bail!(InvalidData, "unsupported AAAA record class '{}'", class),
}
}
fn parse_txt(cur: &mut Cursor<&[u8]>) -> io::Result<TXT> {
let mut txts = TXT::new();
loop {
let len = match cur.read_u8() {
Ok(len) => len,
Err(e) => match e.kind() {
io::ErrorKind::UnexpectedEof => break,
_ => return Err(e),
},
};
let mut txt = vec![0; len.into()];
cur.read_exact(&mut txt)?;
txts.push(txt)
}
Ok(txts)
}
impl SOA {
pub(crate) fn parse(cur: &mut Cursor<&[u8]>) -> io::Result<SOA> {
let mname = cur.read_qname()?;
let rname = cur.read_qname()?;
let serial = cur.read_u32::<BE>()?;
let refresh = cur.read_u32::<BE>()?;
let retry = cur.read_u32::<BE>()?;
let expire = cur.read_u32::<BE>()?;
let minimum = cur.read_u32::<BE>()?;
Ok(SOA {
mname,
rname,
serial,
refresh: Duration::from_secs(refresh.into()),
retry: Duration::from_secs(retry.into()),
expire: Duration::from_secs(expire.into()),
minimum: Duration::from_secs(minimum.into()),
})
}
}
impl MX {
pub(crate) fn parse(cur: &mut Cursor<&[u8]>) -> io::Result<MX> {
let preference = cur.read_u16::<BE>()?;
let exchange = cur.read_qname()?;
Ok(MX {
preference,
exchange,
})
}
}
impl SRV {
pub(crate) fn parse(cur: &mut Cursor<&[u8]>) -> io::Result<SRV> {
let priority = cur.read_u16::<BE>()?;
let weight = cur.read_u16::<BE>()?;
let port = cur.read_u16::<BE>()?;
let name = cur.read_qname()?;
Ok(SRV {
priority,
weight,
port,
name,
})
}
}