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
use std::fmt;
use std::fmt::{Display, Formatter};
use rr::domain::Name;
use rr::record_type::RecordType;
use rr::dns_class::DNSClass;
use serialize::binary::*;
use error::*;
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Query {
name: Name,
query_type: RecordType,
query_class: DNSClass,
}
impl Default for Query {
fn default() -> Self {
Query {
name: Name::new(),
query_type: RecordType::A,
query_class: DNSClass::IN,
}
}
}
impl Query {
pub fn new() -> Self {
Default::default()
}
pub fn query(name: Name, query_type: RecordType) -> Self {
Query {
name,
query_type,
query_class: DNSClass::IN,
}
}
pub fn set_name(&mut self, name: Name) -> &mut Self {
self.name = name;
self
}
pub fn set_query_type(&mut self, query_type: RecordType) -> &mut Self {
self.query_type = query_type;
self
}
pub fn set_query_class(&mut self, query_class: DNSClass) -> &mut Self {
self.query_class = query_class;
self
}
pub fn name(&self) -> &Name {
&self.name
}
pub fn query_type(&self) -> RecordType {
self.query_type
}
pub fn query_class(&self) -> DNSClass {
self.query_class
}
}
impl BinEncodable for Query {
fn emit(&self, encoder: &mut BinEncoder) -> ProtoResult<()> {
self.name.emit(encoder)?;
self.query_type.emit(encoder)?;
self.query_class.emit(encoder)?;
Ok(())
}
}
impl<'r> BinDecodable<'r> for Query {
fn read(decoder: &mut BinDecoder<'r>) -> ProtoResult<Self> {
let name = Name::read(decoder)?;
let query_type = RecordType::read(decoder)?;
let query_class = DNSClass::read(decoder)?;
Ok(Query {
name: name,
query_type: query_type,
query_class: query_class,
})
}
}
impl Display for Query {
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
write!(
f,
"name: {} type: {} class: {}",
self.name,
self.query_type,
self.query_class
)
}
}
#[test]
fn test_read_and_emit() {
let expect = Query {
name: Name::from_ascii("WWW.example.com").unwrap(),
query_type: RecordType::AAAA,
query_class: DNSClass::IN,
};
let mut byte_vec: Vec<u8> = Vec::with_capacity(512);
{
let mut encoder = BinEncoder::new(&mut byte_vec);
expect.emit(&mut encoder).unwrap();
}
let mut decoder = BinDecoder::new(&byte_vec);
let got = Query::read(&mut decoder).unwrap();
assert_eq!(got, expect);
}