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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
//! A DNSSEC validator.
//!
//! This module implements a DNSSEC validator provided as a pass through
//! transport. It implements that parts of
//! [RFC 4035](https://www.rfc-editor.org/info/rfc4035) related to converting
//! a validation status to a result code (server failure) and setting or
//! clearing the AD flag.
//! For details of the validator see the
//! [validator](crate::dnssec::validator) module.
//!
//! # Upstream transports
//!
//! When it comes to the upstream transport, this module is similar to
//! other client transport modules in that it takes a transport that
//! implements the [SendRequest] trait. However, the validator transport
//! needs a [ValidationContext] which in turn also needs a client transport.
//! The ValidationContext needs to generate DS and DNSKEY request and for
//! it needs a client transport that allows the creation of new DNS
//! request. Therefore the ValidationContext requires client tranport that
//! provides `SendRequest<RequestMessage<Octs>>`. It is often convenient
//! to use a single upstream client transport for both the validator transport
//! and the ValidationContext. However, it is quite possible to use
//! different transports.
//!
//! # Caching
//!
//! Ideally caching should be done before (downstream of) the validator
//! transport. This way validated results are cached. A cache that is
//! upstream of the validator would avoid network traffic, but would require
//! some amount of validating for each request.
//!
//! The validator has some internal caches (see the
//! [validator](crate::dnssec::validator) module) so
//! there is no direct need for a cache upstream of the validator. Caching
//! becomes more complex if there is validator that uses the validator.
//! in that case, the downstream validator will likely issues requests for
//! DS and DNSKEY records that the validator issues as well. Because there is
//! no upstream cache, those requests will go over the upstream transport
//! twice. One solution to that is to create a new type of cache that only
//! caches DS and DNSKEY records and insert that upstream of the validator.

#![cfg(all(
    feature = "unstable-validator",
    any(feature = "ring", feature = "openssl")
))]

//! # Example
//! ```rust,no_run
//! # use domain::base::{MessageBuilder, Name, Rtype};
//! # use domain::net::client::dgram_stream;
//! # use domain::net::client::protocol::{TcpConnect, UdpConnect};
//! # use domain::net::client::request::{RequestMessage, SendRequest};
//! # use domain::net::client::validator;
//! # use domain::dnssec::validator::anchor::TrustAnchors;
//! # use domain::dnssec::validator::context::ValidationContext;
//! # use std::net::{IpAddr, SocketAddr};
//! # use std::str::FromStr;
//! # use std::sync::Arc;
//! #
//! # async fn g() {
//! #     let server_addr = SocketAddr::new(IpAddr::from_str("::1").unwrap(), 53);
//! #
//! #     let udp_connect = UdpConnect::new(server_addr);
//! #     let tcp_connect = TcpConnect::new(server_addr);
//! #     let (udptcp_conn, transport) = dgram_stream::Connection::new(udp_connect, tcp_connect);
//! #
//! #     tokio::spawn(async move {
//! #         transport.run().await;
//! #         println!("UDP+TCP run exited");
//! #     });
//! #
//! #     let mut msg = MessageBuilder::new_vec();
//! #     msg.header_mut().set_rd(true);
//! #     msg.header_mut().set_ad(true);
//! #     let mut msg = msg.question();
//! #     msg.push((Name::vec_from_str("example.com").unwrap(), Rtype::AAAA))
//! #         .unwrap();
//!     let req = RequestMessage::new(msg).unwrap();
//!
//!     let ta = TrustAnchors::from_u8(b". 172800 IN DNSKEY 257 3 8 AwEAAaz/tAm8yTn4Mfeh5eyI96WSVexTBAvkMgJzkKTOiW1vkIbzxeF3+/4RgWOq7HrxRixHlFlExOLAJr5emLvN7SWXgnLh4+B5xQlNVz8Og8kvArMtNROxVQuCaSnIDdD5LKyWbRd2n9WGe2R8PzgCmr3EgVLrjyBxWezF0jLHwVN8efS3rCj/EWgvIWgb9tarpVUDK/b58Da+sqqls3eNbuv7pr+eoZG+SrDK6nWeL3c6H5Apxz7LjVc1uTIdsIXxuOLYA4/ilBmSVIzuDWfdRUfhHdY6+cn8HFRm+2hM8AnXGXws9555KrUB5qihylGa8subX2Nn6UwNR1AkUTV74bU= ;{id = 20326 (ksk), size = 2048b} ;;state=2 [  VALID  ] ;;count=0 ;;lastchange=1683463064 ;;Sun May  7 12:37:44 2023").unwrap();
//!     let vc = ValidationContext::new(ta, udptcp_conn.clone());
//!
//!     let vac_conn = validator::Connection::new(udptcp_conn, Arc::new(vc));
//!
//!     // Send a query message.
//!     let mut request = vac_conn.send_request(req.clone());
//!
//!     // Get the reply
//!     println!("Wating for validator reply");
//!     let reply = request.get_response().await.unwrap();
//!     println!("Validator reply: {reply:?}");
//! }
//! ```

#![cfg(all(
    feature = "unstable-validator",
    any(feature = "ring", feature = "openssl")
))]
#![cfg_attr(
    docsrs,
    doc(cfg(all(
        feature = "unstable-validator",
        any(feature = "ring", feature = "openssl")
    )))
)]

use crate::base::iana::Rcode;
use crate::base::opt::{AllOptData, ExtendedError};
use crate::base::{
    Message, MessageBuilder, ParsedName, Rtype, StaticCompressor,
};
use crate::dep::octseq::{Octets, OctetsFrom, OctetsInto};
use crate::dnssec::validator::context::{ValidationContext, ValidationState};
use crate::net::client::request::{
    ComposeRequest, Error, GetResponse, RequestMessage, SendRequest,
};
use crate::rdata::AllRecordData;
use bytes::Bytes;
use std::boxed::Box;
use std::fmt::{Debug, Formatter};
use std::future::Future;
use std::marker::PhantomData;
use std::pin::Pin;
use std::sync::Arc;
use std::vec::Vec;

//------------ Config ---------------------------------------------------------

/// Configuration of a validator.
#[derive(Clone, Default, Debug)]
pub struct Config {}

impl Config {
    /// Creates a new config with default values.
    ///
    /// The default values are documented at the relevant set_* methods.
    pub fn new() -> Self {
        Default::default()
    }
}

//------------ Connection -----------------------------------------------------

#[derive(Clone)]
/// A connection that DNSSEC validates responses from an upstream connection.
pub struct Connection<Upstream, VCOcts, VCUpstream> {
    /// Upstream transport to use for requests.
    upstream: Upstream,

    /// The validation context for this connection.
    vc: Arc<ValidationContext<VCUpstream>>,

    /// The configuration of this connection.
    config: Config,

    /// Phantom field to capture `VCOcts`.
    _phantom: PhantomData<VCOcts>,
}

impl<Upstream, VCOcts, VCUpstream> Connection<Upstream, VCOcts, VCUpstream> {
    /// Create a new connection with default configuration parameters.
    ///
    /// Note that Upstream needs to implement [SendRequest]
    /// (and Clone/Send/Sync) to be useful.
    pub fn new(
        upstream: Upstream,
        vc: Arc<ValidationContext<VCUpstream>>,
    ) -> Self {
        Self::with_config(upstream, vc, Default::default())
    }

    /// Create a new connection with specified configuration parameters.
    ///
    /// Note that Upstream needs to implement [SendRequest]
    /// (and Clone/Send/Sync) to be useful.
    pub fn with_config(
        upstream: Upstream,
        vc: Arc<ValidationContext<VCUpstream>>,
        config: Config,
    ) -> Self {
        Self {
            upstream,
            vc,
            config,
            _phantom: PhantomData,
        }
    }
}

//------------ SendRequest ----------------------------------------------------

impl<CR, Upstream, VCOcts, VCUpstream> SendRequest<CR>
    for Connection<Upstream, VCOcts, VCUpstream>
where
    CR: ComposeRequest + Clone + 'static,
    Upstream: Clone + SendRequest<CR> + Send + Sync + 'static,
    VCOcts: AsRef<[u8]>
        + Debug
        + Octets
        + OctetsFrom<Vec<u8>>
        + Send
        + Sync
        + 'static,
    VCUpstream:
        Clone + SendRequest<RequestMessage<VCOcts>> + Send + Sync + 'static,
{
    fn send_request(
        &self,
        request_msg: CR,
    ) -> Box<dyn GetResponse + Send + Sync> {
        Box::new(Request::new(
            request_msg,
            self.upstream.clone(),
            self.vc.clone(),
            self.config.clone(),
        ))
    }
}

//------------ Request --------------------------------------------------------

/// The state of a request that is executed.
pub struct Request<CR, Upstream, VCOcts, VCUpstream>
where
    Upstream: Send + Sync,
{
    /// State of the request.
    state: RequestState,

    /// The request message.
    request_msg: CR,

    /// The upstream transport of the connection.
    upstream: Upstream,

    /// The validation context.
    vc: Arc<ValidationContext<VCUpstream>>,

    /// The configuration of the connection.
    _config: Config,

    /// valid of the cd flag in the request.
    cd: bool,

    /// value of the dnssec_ok flag in the request.
    dnssec_ok: bool,

    /// Phantom field to capture `VCOcts`.
    _phantom: PhantomData<VCOcts>,
}

impl<CR, Upstream, VCOcts, VCUpstream>
    Request<CR, Upstream, VCOcts, VCUpstream>
where
    Upstream: SendRequest<CR> + Send + Sync,
{
    /// Create a new Request object.
    fn new(
        request_msg: CR,
        upstream: Upstream,
        vc: Arc<ValidationContext<VCUpstream>>,
        config: Config,
    ) -> Request<CR, Upstream, VCOcts, VCUpstream> {
        Self {
            state: RequestState::Init,
            request_msg,
            upstream,
            vc,
            _config: config,
            cd: false,
            dnssec_ok: false,
            _phantom: PhantomData,
        }
    }

    /// This is the implementation of the get_response method.
    async fn get_response_impl<Octs>(
        &mut self,
    ) -> Result<Message<Bytes>, Error>
    where
        CR: Clone + ComposeRequest,
        Upstream: SendRequest<CR>,
        Octs:
            AsRef<[u8]> + Debug + Octets + OctetsFrom<Vec<u8>> + Send + Sync,
        VCUpstream: SendRequest<RequestMessage<Octs>>,
    {
        loop {
            match &mut self.state {
                RequestState::Init => {
                    // Store the DO flag of the request.
                    self.dnssec_ok = self.request_msg.dnssec_ok();
                    if !self.dnssec_ok {
                        // Set the DO flag, otherwise we can't validate.
                        self.request_msg.set_dnssec_ok(true);
                    }

                    // Store the CD flag of the request.
                    self.cd = self.request_msg.header().cd();
                    if !self.cd {
                        // Set the CD flag to get all results even if they
                        // fail to validate upstream.
                        self.request_msg.header_mut().set_cd(true);
                    }

                    let request =
                        self.upstream.send_request(self.request_msg.clone());
                    self.state = RequestState::GetResponse(request);
                    continue;
                }

                RequestState::GetResponse(request) => {
                    let response_msg = request.get_response().await?;

                    if self.cd {
                        if self.dnssec_ok {
                            // Clear the AD flag if it is clear. Check if CD
                            // is set. If either AD is set or CD is clear then
                            // correct the message.
                            if response_msg.header().ad()
                                || !response_msg.header().cd()
                            {
                                let mut response_msg = Message::from_octets(
                                    response_msg.as_slice().to_vec(),
                                )?;
                                response_msg.header_mut().set_ad(false);
                                response_msg.header_mut().set_cd(true);
                                let response_msg =
                                    Message::<Bytes>::from_octets(
                                        response_msg
                                            .into_octets()
                                            .octets_into(),
                                    )?;
                                return Ok(response_msg);
                            }
                            return Ok(response_msg);
                        } else {
                            let msg =
                                remove_dnssec(&response_msg, false, self.cd);
                            return msg;
                        }
                    }

                    self.state = RequestState::Validate(response_msg);
                    continue;
                }

                RequestState::Validate(response_msg) => {
                    let res = self.vc.validate_msg(response_msg).await;
                    return match res {
                        Err(err) => Err(Error::Validation(err)),
                        Ok((state, opt_ede)) => {
                            match state {
                                ValidationState::Secure => {
                                    // Check the state of the DO flag to see
                                    // if we have to strip DNSSEC records. Set
                                    // the AD flag if it is not set and either
                                    // AD or DO is set in the request.
                                    // We always have to clear CD.
                                    if self.dnssec_ok {
                                        // Set AD and clear CD.
                                        let mut response_msg =
                                            Message::from_octets(
                                                response_msg
                                                    .as_slice()
                                                    .to_vec(),
                                            )?;
                                        response_msg
                                            .header_mut()
                                            .set_ad(true);
                                        response_msg
                                            .header_mut()
                                            .set_cd(false);
                                        let response_msg =
                                            Message::<Bytes>::from_octets(
                                                response_msg
                                                    .into_octets()
                                                    .octets_into(),
                                            )?;
                                        Ok(response_msg)
                                    } else {
                                        // Set AD if it was set in the request.
                                        let msg = remove_dnssec(
                                            response_msg,
                                            self.request_msg.header().ad(),
                                            false,
                                        );
                                        msg
                                    }
                                }
                                ValidationState::Bogus => {
                                    serve_fail(response_msg, opt_ede)
                                }
                                ValidationState::Insecure
                                | ValidationState::Indeterminate => {
                                    let response_msg = match opt_ede {
                                        Some(ede) => {
                                            add_opt(response_msg, ede)?
                                        }
                                        None => response_msg.clone(),
                                    };
                                    // Check the state of the DO flag to see
                                    // if we have to strip DNSSEC records.
                                    // Clear the AD flag if it is set. Always
                                    // clear CD.
                                    if self.dnssec_ok {
                                        // Clear AD if it is set. Clear CD.
                                        let mut response_msg =
                                            Message::from_octets(
                                                response_msg
                                                    .as_slice()
                                                    .to_vec(),
                                            )?;
                                        response_msg
                                            .header_mut()
                                            .set_ad(false);
                                        response_msg
                                            .header_mut()
                                            .set_cd(false);
                                        let response_msg =
                                            Message::<Bytes>::from_octets(
                                                response_msg
                                                    .into_octets()
                                                    .octets_into(),
                                            )?;
                                        Ok(response_msg)
                                    } else {
                                        remove_dnssec(
                                            &response_msg,
                                            false,
                                            false,
                                        )
                                    }
                                }
                            }
                        }
                    };
                }
            }
        }
    }
}

impl<CR, Upstream, VCOcts, VCUpstream> Debug
    for Request<CR, Upstream, VCOcts, VCUpstream>
where
    Upstream: Send + Sync,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
        f.debug_struct("Request")
            .field("fut", &format_args!("_"))
            .finish()
    }
}

impl<CR, Upstream, VCOcts, VCUpstream> GetResponse
    for Request<CR, Upstream, VCOcts, VCUpstream>
where
    CR: Clone + ComposeRequest,
    Upstream: Clone + SendRequest<CR> + Send + Sync,
    VCOcts: AsRef<[u8]> + Debug + Octets + OctetsFrom<Vec<u8>> + Send + Sync,
    VCUpstream: Clone + SendRequest<RequestMessage<VCOcts>> + Send + Sync,
{
    fn get_response(
        &mut self,
    ) -> Pin<
        Box<
            dyn Future<Output = Result<Message<Bytes>, Error>>
                + Send
                + Sync
                + '_,
        >,
    > {
        Box::pin(self.get_response_impl())
    }
}

//------------ RequestState ---------------------------------------------------
/// States of the state machine in get_response_impl
enum RequestState {
    /// Initial state.
    Init,

    /// Wait for a response.
    GetResponse(Box<dyn GetResponse + Send + Sync>),

    /// Wait for validation to complete.
    Validate(Message<Bytes>),
}

/// Return a new message without the DNSSEC type DNSKEY, RRSIG, NSEC, and NSEC3.
/// Only RRSIG needs to be removed
/// from the answer section unless the qtype is RRSIG. Remove all
/// DNSSEC records from the authority and additional sections.
fn remove_dnssec(
    msg: &Message<Bytes>,
    ad: bool,
    cd: bool,
) -> Result<Message<Bytes>, Error> {
    let mut target =
        MessageBuilder::from_target(StaticCompressor::new(Vec::new()))
            .expect("Vec is expected to have enough space");

    let source = msg;
    let opt = source.opt();

    *target.header_mut() = source.header();

    if ad != source.header().ad() {
        // Change AD.
        target.header_mut().set_ad(ad);
    }
    if cd != source.header().cd() {
        // Change CD.
        target.header_mut().set_cd(cd);
    }

    let source = source.question();
    let mut target = target.question();
    let mut qtype = Rtype::ANY;
    for rr in source {
        qtype = rr.clone()?.qtype();
        target.push(rr?).expect("push failed");
    }
    let mut source = source.answer()?;
    let mut target = target.answer();
    for rr in &mut source {
        let rr = rr?
            .into_record::<AllRecordData<_, ParsedName<_>>>()?
            .expect("record expected");
        if is_dnssec(rr.rtype()) && rr.rtype() != qtype {
            continue;
        }
        target.push(rr).expect("push error");
    }

    let mut source =
        source.next_section()?.expect("section should be present");
    let mut target = target.authority();
    for rr in &mut source {
        let rr = rr?
            .into_record::<AllRecordData<_, ParsedName<_>>>()?
            .expect("record expected");
        if is_dnssec(rr.rtype()) {
            continue;
        }
        target.push(rr).expect("push error");
    }

    let source = source.next_section()?.expect("section should be present");
    let mut target = target.additional();
    for rr in source {
        let rr = rr?;
        let rr = rr
            .into_record::<AllRecordData<_, ParsedName<_>>>()?
            .expect("record expected");
        if rr.rtype() == Rtype::OPT {
            continue;
        }
        if is_dnssec(rr.rtype()) {
            continue;
        }
        target.push(rr).expect("push error");
    }
    if let Some(opt) = opt {
        target
            .opt(|ob| {
                ob.set_dnssec_ok(false);
                // XXX something is missing ob.set_rcode(opt.rcode());
                ob.set_udp_payload_size(opt.udp_payload_size());
                ob.set_version(opt.version());
                for o in opt.opt().iter() {
                    let x: AllOptData<_, _> = o.expect("should not fail");
                    ob.push(&x)?;
                }
                Ok(())
            })
            .expect("should not fail");
    }

    let result = target.as_builder().clone();
    Ok(
        Message::<Bytes>::from_octets(result.finish().into_target().into())
            .expect(
                "Message should be able to parse output from MessageBuilder",
            ),
    )
}

/// Check if a type is a DNSSEC type that needs to be removed.
fn is_dnssec(rtype: Rtype) -> bool {
    rtype == Rtype::DNSKEY
        || rtype == Rtype::RRSIG
        || rtype == Rtype::NSEC
        || rtype == Rtype::NSEC3
}

/// Return a new message that adds an `ExtendedError` option to an existing
/// message.
fn add_opt(
    msg: &Message<Bytes>,
    ede: ExtendedError<Vec<u8>>,
) -> Result<Message<Bytes>, Error> {
    let mut target =
        MessageBuilder::from_target(StaticCompressor::new(Vec::new()))
            .expect("Vec is expected to have enough space");

    let source = msg;

    *target.header_mut() = msg.header();

    let source = source.question();
    let mut target = target.question();
    for rr in source {
        target.push(rr?).expect("should not fail");
    }
    let mut source = source.answer()?;
    let mut target = target.answer();
    for rr in &mut source {
        let rr = rr?
            .into_record::<AllRecordData<_, ParsedName<_>>>()?
            .expect("record expected");
        target.push(rr).expect("should not fail");
    }

    let mut source =
        source.next_section()?.expect("section should be present");
    let mut target = target.authority();
    for rr in &mut source {
        let rr = rr?
            .into_record::<AllRecordData<_, ParsedName<_>>>()?
            .expect("record expected");
        target.push(rr).expect("should not fail");
    }

    let source = source.next_section()?.expect("section should be present");
    let mut target = target.additional();
    for rr in source {
        let rr = rr?;
        if rr.rtype() != Rtype::OPT {
            let rr = rr
                .into_record::<AllRecordData<_, ParsedName<_>>>()?
                .expect("record expected");
            target.push(rr).expect("should not fail");
        }
    }

    if let Some(opt) = msg.opt() {
        target
            .opt(|ob| {
                ob.set_dnssec_ok(opt.dnssec_ok());
                // XXX something is missing ob.set_rcode(opt.rcode());
                ob.set_udp_payload_size(opt.udp_payload_size());
                ob.set_version(opt.version());
                for o in opt.opt().iter() {
                    let x: AllOptData<_, _> = o.expect("should not fail");
                    ob.push(&x).expect("should not fail");
                }
                ob.push(&ede).expect("should not fail");
                Ok(())
            })
            .expect("should not fail");
    }

    let result = target.as_builder().clone();
    let msg = Message::<Bytes>::from_octets(
        result.finish().into_target().octets_into(),
    )
    .expect("Message should be able to parse output from MessageBuilder");
    Ok(msg)
}

/// Generate a SERVFAIL reply message.
fn serve_fail(
    msg: &Message<Bytes>,
    opt_ede: Option<ExtendedError<Vec<u8>>>,
) -> Result<Message<Bytes>, Error> {
    let mut target =
        MessageBuilder::from_target(StaticCompressor::new(Vec::new()))
            .expect("Vec is expected to have enough space");

    let source = msg;

    *target.header_mut() = msg.header();
    target.header_mut().set_rcode(Rcode::SERVFAIL);
    target.header_mut().set_ad(false);

    let source = source.question();
    let mut target = target.question();
    for rr in source {
        target.push(rr?).expect("should not fail");
    }
    let mut target = target.additional();

    if let Some(opt) = msg.opt() {
        target
            .opt(|ob| {
                ob.set_dnssec_ok(opt.dnssec_ok());
                // XXX something is missing ob.set_rcode(opt.rcode());
                ob.set_udp_payload_size(opt.udp_payload_size());
                ob.set_version(opt.version());
                for o in opt.opt().iter() {
                    let x: AllOptData<_, _> = o.expect("should not fail");
                    ob.push(&x).expect("should not fail");
                }
                if let Some(ede) = opt_ede {
                    ob.push(&ede).expect("should not fail");
                }
                Ok(())
            })
            .expect("should not fail");
    }

    let result = target.as_builder().clone();
    let msg = Message::<Bytes>::from_octets(
        result.finish().into_target().octets_into(),
    )
    .expect("Message should be able to parse output from MessageBuilder");
    Ok(msg)
}