domain 0.12.0

A DNS library for Rust.
Documentation
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
use core::str::FromStr;

use std::collections::VecDeque;

use bytes::{Bytes, BytesMut};
use octseq::{Octets, Parser};

use crate::base::iana::Rcode;
use crate::base::message_builder::{
    AnswerBuilder, AuthorityBuilder, QuestionBuilder,
};
use crate::base::net::{Ipv4Addr, Ipv6Addr};
use crate::base::rdata::ComposeRecordData;
use crate::base::{
    Message, MessageBuilder, ParsedName, Record, Rtype, Serial, Ttl,
};
use crate::base::{Name, ToName};
use crate::logging::init_logging;
use crate::rdata::{Aaaa, Soa, ZoneRecordData, A};
use crate::zonetree::types::{ZoneUpdate, ZoneUpdate as ZU};

use super::interpreter::XfrResponseInterpreter;
use super::types::{Error, IterationError, ParsedRecord};

#[test]
fn non_xfr_response_is_rejected() {
    init_logging();

    // Create an AXFR-like request to reply to.
    let req = mk_request("example.com", Rtype::AXFR).into_message();

    // Create an XFR response interpreter.
    let mut interpreter = XfrResponseInterpreter::new();

    // Create a non-XFR response.
    let mut answer = mk_empty_answer(&req, Rcode::NOERROR);
    add_answer_record(&req, &mut answer, A::new(Ipv4Addr::LOCALHOST));
    let resp = answer.into_message();

    // Process the response and assert that it is rejected as not being
    // a valid XFR response and that no XFR interpreter updates were emitted.
    assert!(matches!(
        interpreter.interpret_response(resp),
        Err(Error::NotValidXfrResponse)
    ));
}

#[test]
fn axfr_response_with_no_answers_is_rejected() {
    init_logging();

    // Create an AXFR request to reply to.
    let req = mk_request("example.com", Rtype::AXFR).into_message();

    // Create an XFR response interpreter.
    let mut interpreter = XfrResponseInterpreter::new();

    // Create a response that lacks answers.
    let resp = mk_empty_answer(&req, Rcode::NOERROR).into_message();

    // Process the response and assert that it is rejected as not being
    // a valid XFR response and that no XFR interpreter updates were emitted.
    assert!(matches!(
        interpreter.interpret_response(resp),
        Err(Error::NotValidXfrResponse)
    ));
}

#[test]
fn error_axfr_response_is_rejected() {
    init_logging();

    // Create an AXFR request to reply to.
    let req = mk_request("example.com", Rtype::AXFR).into_message();

    // Create an XFR response interpreter.
    let mut interpreter = XfrResponseInterpreter::new();

    // Create a minimal valid AXFR response, just something that should
    // not be rejected by the XFR interpreter due to its content. It should
    // however be rejected due to the non-NOERROR rcode.
    let mut answer = mk_empty_answer(&req, Rcode::SERVFAIL);
    add_answer_record(&req, &mut answer, mk_soa(Serial::now()));
    let resp = answer.into_message();

    // Process the response and assert that it is rejected as not being
    // a valid XFR response and that no XFR interpreter updates were emitted.
    assert!(matches!(
        interpreter.interpret_response(resp),
        Err(Error::NotValidXfrResponse)
    ));
}

#[test]
fn incomplete_axfr_response_is_accepted() {
    init_logging();

    // Create an AXFR request to reply to.
    let req = mk_request("example.com", Rtype::AXFR).into_message();

    // Create an XFR response interpreter.
    let mut interpreter = XfrResponseInterpreter::new();

    // Create an incomplete AXFR response. A proper AXFR response has at
    // least two identical SOA records, one at the start and one at the
    // end, but this response contains only a single SOA record. This is
    // not considered invalid however because a subsequent response could
    // still provide the missing SOA record.
    let mut answer = mk_empty_answer(&req, Rcode::NOERROR);
    add_answer_record(&req, &mut answer, mk_soa(Serial::now()));
    let resp = answer.into_message();

    // Process the response.
    let mut it = interpreter.interpret_response(resp).unwrap();

    // Verify that no updates are output by the XFR interpreter.
    assert!(it.next().is_none());
}

#[test]
fn axfr_response_with_only_soas_is_accepted() {
    init_logging();

    // Create an AXFR request to reply to.
    let req = mk_request("example.com", Rtype::AXFR).into_message();

    // Create an XFR response interpreter.
    let mut interpreter = XfrResponseInterpreter::new();

    // Create a complete but minimal AXFR response. A proper AXFR response
    // has at least two identical SOA records, one at the start and one at
    // the end, with actual zone records in between. This response has only
    // the start and end SOA and no content in between. RFC 5936 doesn't
    // seem to disallow this.
    let mut answer = mk_empty_answer(&req, Rcode::NOERROR);
    let soa = mk_soa(Serial::now());
    add_answer_record(&req, &mut answer, soa.clone());
    add_answer_record(&req, &mut answer, soa);
    let resp = answer.into_message();

    // Process the response.
    let mut it = interpreter.interpret_response(resp).unwrap();

    // Verify the updates emitted by the XFR interpreter.
    assert_eq!(it.next(), Some(Ok(ZoneUpdate::DeleteAllRecords)));
    assert!(matches!(it.next(), Some(Ok(ZU::Finished(_)))));
    assert!(it.next().is_none());
}

#[test]
fn axfr_multi_response_with_only_soas_is_accepted() {
    init_logging();

    // Create an AXFR request to reply to.
    let req = mk_request("example.com", Rtype::AXFR).into_message();

    // Create an XFR response interpreter.
    let mut interpreter = XfrResponseInterpreter::new();

    // Create a complete but minimal AXFR response. A proper AXFR response
    // has at least two identical SOA records, one at the start and one at
    // the end, with actual zone records in between. This response has only
    // the start and end SOA and no content in between. RFC 5936 doesn't
    // seem to disallow this.
    let mut answer = mk_empty_answer(&req, Rcode::NOERROR);
    let soa = mk_soa(Serial::now());
    add_answer_record(&req, &mut answer, soa.clone());
    let resp = answer.into_message();

    // Process the response.
    let mut it = interpreter.interpret_response(resp).unwrap();

    // Verify the updates emitted by the XFR interpreter.
    assert!(it.next().is_none());

    // Create another AXFR response to complete the transfer.
    let mut answer = mk_empty_answer(&req, Rcode::NOERROR);
    add_answer_record(&req, &mut answer, soa);
    let resp = answer.into_message();

    // Process the response.
    let mut it = interpreter.interpret_response(resp).unwrap();

    // Verify the updates emitted by the XFR interpreter.
    assert_eq!(it.next(), Some(Ok(ZoneUpdate::DeleteAllRecords)));
    assert!(matches!(it.next(), Some(Ok(ZU::Finished(_)))));
    assert!(it.next().is_none());
}

#[test]
fn axfr_response_generates_expected_updates() {
    init_logging();

    // Create an AXFR request to reply to.
    let req = mk_request("example.com", Rtype::AXFR).into_message();

    // Create an XFR response interpreter.
    let mut interpreter = XfrResponseInterpreter::new();

    // Create an AXFR response.
    let mut answer = mk_empty_answer(&req, Rcode::NOERROR);
    let serial = Serial::now();
    let soa = mk_soa(serial);
    add_answer_record(&req, &mut answer, soa.clone());
    add_answer_record(&req, &mut answer, A::new(Ipv4Addr::LOCALHOST));
    add_answer_record(&req, &mut answer, Aaaa::new(Ipv6Addr::LOCALHOST));
    add_answer_record(&req, &mut answer, soa);
    let resp = answer.into_message();

    // Process the response.
    let mut it = interpreter.interpret_response(resp).unwrap();

    // Verify the updates emitted by the XFR interpreter.
    assert_eq!(it.next(), Some(Ok(ZoneUpdate::DeleteAllRecords)));
    assert!(
        matches!(it.next(), Some(Ok(ZoneUpdate::AddRecord(r))) if r.rtype() == Rtype::A)
    );
    assert!(
        matches!(it.next(), Some(Ok(ZoneUpdate::AddRecord(r))) if r.rtype() == Rtype::AAAA)
    );
    assert!(matches!(it.next(), Some(Ok(ZoneUpdate::Finished(_)))));
    assert!(it.next().is_none());
}

#[test]
fn ixfr_response_generates_expected_updates() {
    init_logging();

    // Create an IXFR request to reply to.
    let req = mk_request("example.com", Rtype::IXFR);
    let mut authority = req.authority();
    let client_serial = Serial::now();
    let soa = mk_soa(client_serial);
    add_authority_record(&mut authority, soa);
    let req = authority.into_message();

    // Create an XFR response interpreter.
    let mut interpreter = XfrResponseInterpreter::new();

    // Prepare some serial numbers and SOA records to use in the IXFR response.
    let old_serial = client_serial;
    let new_serial = client_serial.add(1);
    let old_soa = mk_soa(old_serial);
    let new_soa = mk_soa(new_serial);

    // Create an IXFR response.
    let mut answer = mk_empty_answer(&req, Rcode::NOERROR);
    // Outer SOA with servers current SOA
    add_answer_record(&req, &mut answer, new_soa.clone());
    // Start of diff sequence: SOA of the servers' previous zone version
    // (which matches that of the client) followed by records to be
    // deleted as they were in that version of the zone but are not in the
    // new version of the zone.
    add_answer_record(&req, &mut answer, old_soa.clone());
    add_answer_record(&req, &mut answer, A::new(Ipv4Addr::LOCALHOST));
    add_answer_record(&req, &mut answer, A::new(Ipv4Addr::BROADCAST));
    // SOA of the servers` new zone version (which is ahead of that of the
    // client) followed by records to be added as they were added in this
    // new version of the zone.`
    add_answer_record(&req, &mut answer, new_soa.clone());
    add_answer_record(&req, &mut answer, A::new(Ipv4Addr::BROADCAST));
    add_answer_record(&req, &mut answer, A::new(Ipv4Addr::LOCALHOST));
    // Closing SOA with servers current SOA
    add_answer_record(&req, &mut answer, new_soa.clone());
    let resp = answer.into_message();

    // Process the response.
    let it = interpreter.interpret_response(resp).unwrap();

    // Make parsed versions of the old and new SOAs.
    let mut buf = BytesMut::new();
    new_soa.compose_rdata(&mut buf).unwrap();
    let buf = buf.freeze();
    let mut parser = Parser::from_ref(&buf);
    let expected_new_soa = Soa::parse(&mut parser).unwrap();

    let mut buf = BytesMut::new();
    old_soa.compose_rdata(&mut buf).unwrap();
    let buf = buf.freeze();
    let mut parser = Parser::from_ref(&buf);
    let expected_old_soa = Soa::parse(&mut parser).unwrap();

    // Verify the updates emitted by the XFR interpreter.
    let owner =
        ParsedName::<Bytes>::from(Name::from_str("example.com").unwrap());
    let expected_updates: [Result<ZoneUpdate<ParsedRecord>, IterationError>;
        7] = [
        Ok(ZoneUpdate::BeginBatchDelete(Record::from((
            owner.clone(),
            0,
            ZoneRecordData::Soa(expected_old_soa),
        )))),
        Ok(ZoneUpdate::DeleteRecord(Record::from((
            owner.clone(),
            0,
            ZoneRecordData::A(A::new(Ipv4Addr::LOCALHOST)),
        )))),
        Ok(ZoneUpdate::DeleteRecord(Record::from((
            owner.clone(),
            0,
            ZoneRecordData::A(A::new(Ipv4Addr::BROADCAST)),
        )))),
        Ok(ZoneUpdate::BeginBatchAdd(Record::from((
            owner.clone(),
            0,
            ZoneRecordData::Soa(expected_new_soa.clone()),
        )))),
        Ok(ZoneUpdate::AddRecord(Record::from((
            owner.clone(),
            0,
            ZoneRecordData::A(A::new(Ipv4Addr::BROADCAST)),
        )))),
        Ok(ZoneUpdate::AddRecord(Record::from((
            owner.clone(),
            0,
            ZoneRecordData::A(A::new(Ipv4Addr::LOCALHOST)),
        )))),
        Ok(ZoneUpdate::Finished(Record::from((
            owner.clone(),
            0,
            ZoneRecordData::Soa(expected_new_soa),
        )))),
    ];

    for (update, expected_update) in it.zip(expected_updates) {
        assert_eq!(update, expected_update);
    }
}

#[test]
fn multi_ixfr_response_generates_expected_updates() {
    init_logging();

    // Create an IXFR request to reply to.
    let req = mk_request("example.com", Rtype::IXFR);
    let mut authority = req.authority();
    let client_serial = Serial::now();
    let soa = mk_soa(client_serial);
    add_authority_record(&mut authority, soa);
    let req = authority.into_message();

    // Prepare some serial numbers and SOA records to use in the IXFR response.
    let old_serial = client_serial;
    let new_serial = client_serial.add(1);
    let old_soa = mk_soa(old_serial);
    let new_soa = mk_soa(new_serial);

    // Create a partial IXFR response.
    let resp = mk_first_ixfr_response(&req, &new_soa, old_soa);

    // Create an XFR response interpreter.
    let mut interpreter = XfrResponseInterpreter::new();

    // Process the response.
    let mut it = interpreter.interpret_response(resp).unwrap();

    // Verify the updates emitted by the XFR interpreter.
    assert!(matches!(it.next(), Some(Ok(ZU::BeginBatchDelete(_)))));
    assert!(matches!(it.next(), Some(Ok(ZU::DeleteRecord(..)))));
    assert!(it.next().is_none());

    // Create a second IXFR response that completes the transfer
    let resp = mk_second_ixfr_response(req, new_soa);

    // Process the response.
    let mut it = interpreter.interpret_response(resp).unwrap();

    // Verify the updates emitted by the XFR interpreter.
    assert!(matches!(it.next(), Some(Ok(ZU::DeleteRecord(..)))));
    assert!(matches!(it.next(), Some(Ok(ZU::BeginBatchAdd(_)))));
    assert!(matches!(it.next(), Some(Ok(ZU::AddRecord(..)))));
    assert!(matches!(it.next(), Some(Ok(ZU::AddRecord(..)))));
    assert!(matches!(it.next(), Some(Ok(ZU::Finished(_)))));
    assert!(it.next().is_none());
}

#[test]
fn ixfr_response_with_fallback_to_axfr_generates_expected_updates() {
    init_logging();

    // Create an IXFR request to reply to.
    let req = mk_request("example.com", Rtype::IXFR);
    let mut authority = req.authority();
    let client_serial = Serial::now();
    let soa = mk_soa(client_serial);
    add_authority_record(&mut authority, soa);
    let req = authority.into_message();

    // Create an AXFR response.
    let mut answer = mk_empty_answer(&req, Rcode::NOERROR);
    let serial = Serial::now();
    let soa = mk_soa(serial);
    add_answer_record(&req, &mut answer, soa.clone());
    add_answer_record(&req, &mut answer, A::new(Ipv4Addr::LOCALHOST));
    add_answer_record(&req, &mut answer, Aaaa::new(Ipv6Addr::LOCALHOST));
    add_answer_record(&req, &mut answer, soa);
    let resp = answer.into_message();

    // Create an XFR response interpreter.
    let mut interpreter = XfrResponseInterpreter::new();

    // Process the response.
    let mut it = interpreter.interpret_response(resp).unwrap();

    // Verify the updates emitted by the XFR interpreter.
    assert_eq!(it.next(), Some(Ok(ZoneUpdate::DeleteAllRecords)));
    assert!(
        matches!(it.next(), Some(Ok(ZoneUpdate::AddRecord(r))) if r.rtype() == Rtype::A)
    );
    assert!(
        matches!(it.next(), Some(Ok(ZoneUpdate::AddRecord(r))) if r.rtype() == Rtype::AAAA)
    );
    assert!(matches!(it.next(), Some(Ok(ZoneUpdate::Finished(_)))));
    assert!(it.next().is_none());
}

#[test]
fn ixfr_response_single_soa_detected_as_udp_to_tcp_upgrade_signal() {
    init_logging();

    // Create an IXFR request to reply to.
    let req = mk_request("example.com", Rtype::IXFR);
    let mut authority = req.authority();
    let client_serial = Serial::now();
    let soa = mk_soa(client_serial);
    add_authority_record(&mut authority, soa);
    let req = authority.into_message();

    // Create an IXFR response consisting only of a single SOA.
    //
    // https://datatracker.ietf.org/doc/html/rfc1995#section-2
    // 2. Brief Description of the Protocol
    //    ..
    //    "Transport of a query may be by either UDP or TCP.  If an IXFR query
    //     is via UDP, the IXFR server may attempt to reply using UDP if the
    //     entire response can be contained in a single DNS packet.  If the
    //     UDP reply does not fit, the query is responded to with a single SOA
    //     record of the server's current version to inform the client that a
    //     TCP query should be initiated."
    let mut answer = mk_empty_answer(&req, Rcode::NOERROR);
    let serial = Serial::now();
    let soa = mk_soa(serial);
    add_answer_record(&req, &mut answer, soa.clone());
    let resp = answer.into_message();

    // Create an XFR response interpreter.
    let mut interpreter = XfrResponseInterpreter::new();

    // Process the response.
    let mut it = interpreter.interpret_response(resp).unwrap();

    // Verify the updates emitted by the XFR interpreter.
    assert_eq!(
        it.next(),
        Some(Err(IterationError::SingleSoaIxfrTcpRetrySignal))
    );
    assert!(it.next().is_none());
}

#[test]
fn is_finished() {
    init_logging();

    // Create an IXFR request to reply to.
    let req = mk_request("example.com", Rtype::IXFR);
    let mut authority = req.authority();
    let client_serial = Serial::now();
    let soa = mk_soa(client_serial);
    add_authority_record(&mut authority, soa);
    let req = authority.into_message();

    // Prepare some serial numbers and SOA records to use in the IXFR response.
    let old_serial = client_serial;
    let new_serial = client_serial.add(1);
    let old_soa = mk_soa(old_serial);
    let new_soa = mk_soa(new_serial);

    // Create a partial IXFR response.
    let mut responses: VecDeque<_> = vec![
        mk_first_ixfr_response(&req, &new_soa, old_soa),
        mk_second_ixfr_response(req, new_soa),
    ]
    .into();

    // Create an XFR response interpreter.
    let mut interpreter = XfrResponseInterpreter::new();

    // Process the responses
    let mut count = 0;
    while !interpreter.is_finished() {
        let resp = responses.pop_front().unwrap();
        let it = interpreter.interpret_response(resp).unwrap();
        count += it.count();
    }

    assert!(interpreter.is_finished());
    assert!(responses.is_empty());
    assert_eq!(count, 7);
}

#[test]
fn cannot_be_used_once_finished() {
    init_logging();

    // Create an AXFR request to reply to.
    let req = mk_request("example.com", Rtype::AXFR).into_message();

    // Create an XFR response interpreter.
    let mut interpreter = XfrResponseInterpreter::new();

    // Create a complete but minimal AXFR response. A proper AXFR response
    // has at least two identical SOA records, one at the start and one at
    // the end, with actual zone records in between. This response has only
    // the start and end SOA and no content in between. RFC 5936 doesn't
    // seem to disallow this.
    let mut answer = mk_empty_answer(&req, Rcode::NOERROR);
    let soa = mk_soa(Serial::now());
    add_answer_record(&req, &mut answer, soa.clone());
    add_answer_record(&req, &mut answer, soa);
    add_answer_record(&req, &mut answer, A::new(Ipv4Addr::BROADCAST));
    let resp = answer.into_message();

    // Process the response.
    let mut it = interpreter.interpret_response(resp).unwrap();

    // Verify the updates emitted by the XFR interpreter.
    assert_eq!(it.next(), Some(Ok(ZoneUpdate::DeleteAllRecords)));
    assert!(matches!(it.next(), Some(Ok(ZU::Finished(_)))));
    assert_eq!(it.next(), Some(Err(IterationError::AlreadyFinished)));
}

fn mk_first_ixfr_response(
    req: &Message<Bytes>,
    new_soa: &Soa<Name<Bytes>>,
    old_soa: Soa<Name<Bytes>>,
) -> Message<Bytes> {
    let mut answer = mk_empty_answer(req, Rcode::NOERROR);
    // Outer SOA with servers current SOA
    add_answer_record(req, &mut answer, new_soa.clone());
    // Start of diff sequence: SOA of the servers' previous zone version
    // (which matches that of the client) followed by records to be
    // deleted as they were in that version of the zone but are not in the
    // new version of the zone.
    add_answer_record(req, &mut answer, old_soa);
    add_answer_record(req, &mut answer, A::new(Ipv4Addr::LOCALHOST));
    answer.into_message()
}

fn mk_second_ixfr_response(
    req: Message<Bytes>,
    new_soa: Soa<Name<Bytes>>,
) -> Message<Bytes> {
    let mut answer = mk_empty_answer(&req, Rcode::NOERROR);
    add_answer_record(&req, &mut answer, A::new(Ipv4Addr::BROADCAST));
    // SOA of the servers` new zone version (which is ahead of that of the
    // client) followed by records to be added as they were added in this
    // new version of the zone.`
    add_answer_record(&req, &mut answer, new_soa.clone());
    add_answer_record(&req, &mut answer, A::new(Ipv4Addr::BROADCAST));
    add_answer_record(&req, &mut answer, A::new(Ipv4Addr::LOCALHOST));
    // Closing SOA with servers current SOA
    add_answer_record(&req, &mut answer, new_soa);
    answer.into_message()
}

//------------ Helper functions -------------------------------------------

fn mk_request(qname: &str, qtype: Rtype) -> QuestionBuilder<BytesMut> {
    let req = MessageBuilder::new_bytes();
    let mut req = req.question();
    req.push((Name::vec_from_str(qname).unwrap(), qtype))
        .unwrap();
    req
}

fn mk_empty_answer(
    req: &Message<Bytes>,
    rcode: Rcode,
) -> AnswerBuilder<BytesMut> {
    let builder = MessageBuilder::new_bytes();
    builder.start_answer(req, rcode).unwrap()
}

fn add_answer_record<O: Octets, T: ComposeRecordData>(
    req: &Message<O>,
    answer: &mut AnswerBuilder<BytesMut>,
    item: T,
) {
    let question = req.sole_question().unwrap();
    let qname = question.qname();
    let qclass = question.qclass();
    answer
        .push((qname, qclass, Ttl::from_secs(0), item))
        .unwrap();
}

fn add_authority_record<T: ComposeRecordData>(
    authority: &mut AuthorityBuilder<BytesMut>,
    item: T,
) {
    let (qname, qclass) = {
        let question = authority.as_message().sole_question().unwrap();
        let qname = question.qname().to_bytes();
        let qclass = question.qclass();
        (qname, qclass)
    };
    authority
        .push((qname, qclass, Ttl::from_secs(0), item))
        .unwrap();
}

fn mk_soa(serial: Serial) -> Soa<Name<Bytes>> {
    let mname = Name::from_str("mname").unwrap();
    let rname = Name::from_str("rname").unwrap();
    let ttl = Ttl::from_secs(0);
    Soa::new(mname, rname, serial, ttl, ttl, ttl, ttl)
}