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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
use core::fmt;

/// A DNS header.
#[derive(Clone)]
#[repr(C)]
pub struct Header {
  id: [u8; 2],
  flags: [u8; 2],
  question_count: [u8; 2],
  answer_count: [u8; 2],
  name_server_count: [u8; 2],
  additional_records_count: [u8; 2],
}

/// The kind of a DNS header.
#[derive(Debug, PartialEq)]
pub enum HeaderKind {
  Query,
  Response,
}

/// A DNS opcode.
#[derive(Debug, PartialEq)]
pub enum OpCode {
  Query,
  InverseQuery,
  Status,
  Notify,
  Update,
  Reserved(u8)
}

/// A DNS response code.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum ResponseCode {
  NoError,
  FormatError,
  ServerFailure,
  NonExistentDomain,
  NotImplemented,
  Refused,
  ExistentDomain,
  ExistentRrSet,
  NonExistentRrSet,
  NotAuthoritative,
  NotZone,
  BadOptVersionOrBadSignature,
  BadKey,
  BadTime,
  BadMode,
  BadName,
  BadAlg,
  Reserved(u16),
}

impl From<ResponseCode> for u16 {
  fn from(r: ResponseCode) -> Self {
    match r {
      ResponseCode::NoError => 0,
      ResponseCode::FormatError => 1,
      ResponseCode::ServerFailure => 2,
      ResponseCode::NonExistentDomain => 3,
      ResponseCode::NotImplemented => 4,
      ResponseCode::Refused => 5,
      ResponseCode::ExistentDomain => 6,
      ResponseCode::ExistentRrSet => 7,
      ResponseCode::NonExistentRrSet => 8,
      ResponseCode::NotAuthoritative => 9,
      ResponseCode::NotZone => 10,
      ResponseCode::BadOptVersionOrBadSignature => 16,
      ResponseCode::BadKey => 17,
      ResponseCode::BadTime => 18,
      ResponseCode::BadMode => 19,
      ResponseCode::BadName => 20,
      ResponseCode::BadAlg => 21,
      ResponseCode::Reserved(n) => n
    }
  }
}

impl From<u16> for ResponseCode {
  fn from(n: u16) -> Self {
    match n {
      0 => ResponseCode::NoError,
      1 => ResponseCode::FormatError,
      2 => ResponseCode::ServerFailure,
      3 => ResponseCode::NonExistentDomain,
      4 => ResponseCode::NotImplemented,
      5 => ResponseCode::Refused,
      6 => ResponseCode::ExistentDomain,
      7 => ResponseCode::ExistentRrSet,
      8 => ResponseCode::NonExistentRrSet,
      9 => ResponseCode::NotAuthoritative,
      10 => ResponseCode::NotZone,
      16 => ResponseCode::BadOptVersionOrBadSignature,
      17 => ResponseCode::BadKey,
      18 => ResponseCode::BadTime,
      19 => ResponseCode::BadMode,
      20 => ResponseCode::BadName,
      21 => ResponseCode::BadAlg,
      n => ResponseCode::Reserved(n),
    }
  }
}

impl fmt::Debug for Header {
  fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
    fmt.debug_struct("Header")
      .field("id", &self.id())
      .field("kind", &self.kind())
      .field("opcode", &self.opcode())
      .field("authoritative_answer", &self.authoritative_answer())
      .field("truncated", &self.truncated())
      .field("recursion_desired", &self.recursion_desired())
      .field("recursion_available", &self.recursion_available())
      .field("response_code", &self.response_code())
      .field("question_count", &self.question_count())
      .field("answer_count", &self.answer_count())
      .field("name_server_count", &self.name_server_count())
      .field("additional_records_count", &self.additional_records_count())
      .finish()
  }
}

impl Header {
  #[inline]
  pub fn id(&self) -> u16 {
    u16::from_be_bytes(self.id)
  }

  #[inline]
  pub fn set_id(&mut self, id: u16) {
    self.id = id.to_be_bytes()
  }

  #[inline]
  pub fn kind(&self) -> HeaderKind {
    if (self.flags[0] & 0b10000000) == 0 {
      HeaderKind::Query
    } else {
      HeaderKind::Response
    }
  }

  #[inline]
  pub fn set_kind(&mut self, kind: HeaderKind) {
    match kind {
      HeaderKind::Query    => self.flags[0] &= 0b01111111,
      HeaderKind::Response => self.flags[0] |= 0b10000000,
    }
  }

  #[inline]
  pub fn opcode(&self) -> OpCode {
    match (self.flags[0] & 0b01111000) >> 3 {
      0 => OpCode::Query,
      1 => OpCode::InverseQuery,
      2 => OpCode::Status,
      4 => OpCode::Notify,
      5 => OpCode::Update,
      n => OpCode::Reserved(n),
    }
  }

  #[inline]
  pub fn set_opcode(&mut self, opcode: OpCode) {
    self.flags[0] = (self.flags[0] & 0b10000111) | (match opcode {
      OpCode::Query => 0,
      OpCode::InverseQuery => 1,
      OpCode::Status => 2,
      OpCode::Notify => 4,
      OpCode::Update => 5,
      OpCode::Reserved(n) => n,
    } << 3);
  }

  #[inline]
  pub fn authoritative_answer(&self) -> bool {
    (self.flags[0] & 0b00000100) != 0
  }

  #[inline]
  pub fn truncated(&self) -> bool {
    (self.flags[0] & 0b00000010) != 0
  }

  #[inline]
  pub fn recursion_desired(&self) -> bool {
    (self.flags[0] & 0b00000001) != 0
  }

  #[inline]
  pub fn set_recursion_desired(&mut self, recursion_desired: bool) {
    if recursion_desired {
      self.flags[0] |= 0b00000001;
    } else {
      self.flags[0] &= 0b11111110;
    }
  }

  #[inline]
  pub fn recursion_available(&self) -> bool {
    (self.flags[1] & 0b10000000) != 0
  }

  #[inline]
  pub fn set_recursion_available(&mut self, recursion_available: bool) {
    if recursion_available {
      self.flags[1] |= 0b10000000;
    } else {
      self.flags[1] &= 0b01111111;
    }
  }

  #[inline]
  pub fn response_code(&self) -> ResponseCode {
    u16::from(self.flags[1] & 0b00001111).into()
  }

  #[inline]
  pub fn set_response_code(&mut self, response_code: ResponseCode) {
    self.flags[1] = (self.flags[1] & 0b11110000) | u16::from(response_code) as u8;
  }

  #[inline]
  pub fn question_count(&self) -> u16 {
    u16::from_be_bytes(self.question_count)
  }

  /// # Safety
  ///
  /// Setting the question count manually is unsafe. It is the caller's responsibility to
  /// ensure that the corresponding message contains the right amount of questions.
  #[inline]
  pub unsafe fn set_question_count(&mut self, question_count: u16) {
    self.question_count = question_count.to_be_bytes();
  }

  #[inline]
  pub fn answer_count(&self) -> u16 {
    u16::from_be_bytes(self.answer_count)
  }

  /// # Safety
  ///
  /// Setting the answer count manually is unsafe. It is the caller's responsibility to
  /// ensure that the corresponding message contains the right amount of answers.
  #[inline]
  pub unsafe fn set_answer_count(&mut self, answer_count: u16) {
    self.answer_count = answer_count.to_be_bytes();
  }

  #[inline]
  pub fn name_server_count(&self) -> u16 {
    u16::from_be_bytes(self.name_server_count)
  }

  /// # Safety
  ///
  /// Setting the name server count manually is unsafe. It is the caller's responsibility to
  /// ensure that the corresponding message contains the right amount of name servers.
  #[inline]
  pub unsafe fn set_name_server_count(&mut self, name_server_count: u16) {
    self.name_server_count = name_server_count.to_be_bytes();
  }

  #[inline]
  pub fn additional_records_count(&self) -> u16 {
    u16::from_be_bytes(self.additional_records_count)
  }

  /// # Safety
  ///
  /// Setting the additional records count manually is unsafe. It is the caller's responsibility to
  /// ensure that the corresponding message contains the right amount of additional records.
  #[inline]
  pub unsafe fn set_additional_records_count(&mut self, additional_records_count: u16) {
    self.additional_records_count = additional_records_count.to_be_bytes();
  }

  #[inline]
  pub fn builder() -> HeaderBuilder {
    HeaderBuilder::new()
  }
}

/// Builder for [`Header`](struct.Header.html).
pub struct HeaderBuilder(Header);

impl Default for HeaderBuilder {
  fn default() -> Self {
    Self(Header {
      id: [0, 0],
      flags: [0, 0],
      question_count: [0, 0],
      answer_count: [0, 0],
      name_server_count: [0, 0],
      additional_records_count: [0, 0],
    })
  }
}

impl HeaderBuilder {
  #[inline]
  pub fn new() -> Self {
    Self::default()
  }

  pub fn id(mut self, id: u16) -> Self {
    self.0.set_id(id);
    self
  }

  pub fn kind(mut self, kind: HeaderKind) -> Self {
    self.0.set_kind(kind);
    self
  }

  pub fn recursion_desired(mut self, recursion_desired: bool) -> Self {
    self.0.set_recursion_desired(recursion_desired);
    self
  }

  pub fn recursion_available(mut self, recursion_available: bool) -> Self {
    self.0.set_recursion_available(recursion_available);
    self
  }

  pub fn response_code(mut self, response_code: ResponseCode) -> Self {
    self.0.set_response_code(response_code);
    self
  }

  pub fn build(self) -> Header {
    self.0
  }
}