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
use std::io;
use crate::dns::DnsRequest;
impl<'a> DnsRequest<'a> {
pub fn to_dns_binary(&self, w: &mut impl io::Write) -> Result<(), io::Error> {
let query_kind_num: u8 = self.query_kind.into();
debug_assert!(query_kind_num <= 0b00001111);
let query_kind_num = query_kind_num & 0b00001111;
w.write_all(&self.id.to_be_bytes()[..])?;
w.write_all(&[
0b00000000 | (query_kind_num << 3), 0b00000000
])?;
w.write_all(&1u16.to_be_bytes()[..])?;
w.write_all(&0u16.to_be_bytes()[..])?;
w.write_all(&0u16.to_be_bytes()[..])?;
w.write_all(&0u16.to_be_bytes()[..])?;
for l in self.query_name.split(".") {
if l.len() > 63 {
return Err(io::Error::new(io::ErrorKind::Other, "Some label in name is too long"));
}
if l.len() == 0 {
return Err(io::Error::new(io::ErrorKind::Other, "Some label in name is zero sized"));
}
w.write_all(&[(l.len() as u8)])?;
w.write_all(l.as_bytes())?;
}
w.write_all(&[0u8])?;
let ty: u16 = self.ty.into();
let cls: u16 = self.cls.into();
w.write_all(&ty.to_be_bytes()[..])?;
w.write_all(&cls.to_be_bytes()[..])?;
Ok(())
}
}