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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
//! A TSIG signing & verifying passthrough transport.
//!
//! This module provides a transport that wraps the [high-level support for
//! signing message exchanges with TSIG][crate::tsig], thereby authenticating
//! them.
//!
//! # Usage
//!
//! 1. Create a signing [Key].
//! 2. Create a [Connection] that wraps an upstream connection and uses the
//!    key.
//! 3. [Send a request][Connection::send_request] using the connection.
//! 4. [Receive the response][GetResponse] or responses.
//!
//! # How it works
//!
//! Requests are automatically signed with the given key and response
//! signatures are automatically verified. On verification failure
//! [Error::ValidationError][crate::net::client::request::Error] will be
//! returned.
//!
//! <div class="warning">
//!
//! TSIG verification is a destructive process. It will alter the response
//! stripping out the TSIG RR contained within the additional section and
//! decrementing the DNS message header ARCOUNT accordingly. It may also
//! adjust the mesage ID, in conformance with [RFC
//! 8945](https://www.rfc-editor.org/rfc/rfc8945.html#name-dns-message).
//!
//! If you wish to receive the response TSIG RR intact, do **NOT** use this
//! transport. Instead process the response records manually using a normal
//! transport.
//!
//! </div>
//!
//! # Requirements
//!
//! This transport works with any upstream transports so long as they don’t
//! modify the message once signed nor modify the response before it can be
//! verified.
//!
//! Failing to do so will result in signature verification failure. For
//! requests this will occur at the receiving server. For responses this will
//! result in [`GetResponse`] returning
//! [Error::ValidationError][crate::net::client::request::Error].
#![cfg(all(feature = "tsig", feature = "unstable-client-transport"))]
#![warn(missing_docs)]
#![warn(clippy::missing_docs_in_private_items)]

use core::ops::DerefMut;

use std::boxed::Box;
use std::fmt::{Debug, Formatter};
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::vec::Vec;

use bytes::Bytes;
use octseq::Octets;
use tracing::trace;

use crate::base::message::CopyRecordsError;
use crate::base::message_builder::AdditionalBuilder;
use crate::base::wire::Composer;
use crate::base::Message;
use crate::base::StaticCompressor;
use crate::net::client::request::{
    ComposeRequest, ComposeRequestMulti, Error, GetResponse,
    GetResponseMulti, SendRequest, SendRequestMulti,
};
use crate::rdata::tsig::Time48;
use crate::tsig::{ClientSequence, ClientTransaction, Key};

/// A wrapper around [`ClientTransaction`] and [`ClientSequence`].
///
/// This wrapper allows us to write calling code once that invokes methods on
/// the TSIG signer/validator which have the same name and purpose for single
/// response vs multiple response streams, yet have distinct Rust types and so
/// must be called on the correct type, without needing to know at the call
/// site which of the distinct types it actually is.
#[derive(Clone, Debug)]
enum TsigClient<K> {
    /// A [`ClientTransaction`] for signing a request and validating a single
    /// response.
    Transaction(ClientTransaction<K>),

    /// A [`ClientSequence`] for signing a request and validating a single
    /// response.
    Sequence(ClientSequence<K>),
}

impl<K> TsigClient<K>
where
    K: AsRef<Key>,
{
    /// A helper wrapper around [`ClientTransaction::answer`] and
    /// [`ClientSequence::answer`] that allows the appropriate method to be
    /// invoked without needing to know which type it actually is.
    pub fn answer<Octs>(
        &mut self,
        message: &mut Message<Octs>,
        now: Time48,
    ) -> Result<(), Error>
    where
        Octs: Octets + AsMut<[u8]> + ?Sized,
    {
        match self {
            TsigClient::Transaction(client) => client.answer(message, now),
            TsigClient::Sequence(client) => client.answer(message, now),
        }
        .map_err(Error::Authentication)
    }

    /// A helper method that allows [`ClientSequence::done`] to be called
    /// without knowing or caring if the underlying type is actually
    /// [`ClientTransaction`] instead (which doesn't have a `done()` method).
    ///
    /// Invoking this method on a [`ClientTransaction`] is harmless and has no
    /// effect.
    fn done(self) -> Result<(), Error> {
        match self {
            TsigClient::Transaction(_) => {
                // Nothing to do.
                Ok(())
            }
            TsigClient::Sequence(client) => {
                client.done().map_err(Error::Authentication)
            }
        }
    }
}

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

/// A TSIG signing and verifying transport.
///
/// This transport signs requests and verifies responses using a provided key
/// and upstream transport. For more information see the [module
/// docs][crate::net::client::tsig].
#[derive(Clone)]
pub struct Connection<Upstream, K> {
    /// Upstream transport to use for requests.
    ///
    /// The upstream transport(s) **MUST NOT** modify the request before it is
    /// sent nor modify the response before this transport can verify it.
    upstream: Arc<Upstream>,

    /// The key to sign requests with.
    key: K,
}

impl<Upstream, K> Connection<Upstream, K> {
    /// Create a new tsig transport.
    ///
    /// After creating the transport call `send_request` via the
    /// [`SendRequest`] or [`SendRequestMulti`] traits to send signed messages
    /// and verify signed responses.
    pub fn new(key: K, upstream: Upstream) -> Self {
        Self {
            upstream: Arc::new(upstream),
            key,
        }
    }
}

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

impl<CR, Upstream, K> SendRequest<CR> for Connection<Upstream, K>
where
    CR: ComposeRequest + 'static,
    Upstream: SendRequest<RequestMessage<CR, K>> + Send + Sync + 'static,
    K: Clone + AsRef<Key> + Send + Sync + 'static,
{
    fn send_request(
        &self,
        request_msg: CR,
    ) -> Box<dyn GetResponse + Send + Sync> {
        Box::new(Request::<CR, Upstream, K>::new(
            request_msg,
            self.key.clone(),
            self.upstream.clone(),
        ))
    }
}

//------------ SendRequestMulti ----------------------------------------------------

impl<CR, Upstream, K> SendRequestMulti<CR> for Connection<Upstream, K>
where
    CR: ComposeRequestMulti + 'static,
    Upstream: SendRequestMulti<RequestMessage<CR, K>> + Send + Sync + 'static,
    K: Clone + AsRef<Key> + Send + Sync + 'static,
{
    fn send_request(
        &self,
        request_msg: CR,
    ) -> Box<dyn GetResponseMulti + Send + Sync> {
        Box::new(Request::<CR, Upstream, K>::new_multi(
            request_msg,
            self.key.clone(),
            self.upstream.clone(),
        ))
    }
}

//------------ Forwarder ------------------------------------------------------

/// A function that can forward a request via an upstream transport.
///
/// This type is generic over whether the [`RequestMessage`] being sent was
/// sent via the [`ComposeRequest`] trait or the  [`ComposeRequestMulti`]
/// trait, which allows common logic to be used for both despite the different
/// trait bounds required to work with them.
type Forwarder<Upstream, CR, K> = fn(
    &Upstream,
    RequestMessage<CR, K>,
    Arc<std::sync::Mutex<Option<TsigClient<K>>>>,
) -> RequestState<K>;

/// Forward a request that should result in a single response.
///
/// This function forwards a [`RequestMessage`] to an upstream transport using
/// a client that can only accept a single response, i.e. was sent via the
/// [`ComposeRequest`] trait.
fn forwarder<CR, K, Upstream>(
    upstream: &Upstream,
    msg: RequestMessage<CR, K>,
    tsig_client: Arc<std::sync::Mutex<Option<TsigClient<K>>>>,
) -> RequestState<K>
where
    CR: ComposeRequest,
    Upstream: SendRequest<RequestMessage<CR, K>> + Send + Sync,
{
    RequestState::GetResponse(upstream.send_request(msg), tsig_client)
}

/// Forward a request that may result in multiple responses.
///
/// This function forwards a [`RequestMessage`] to an upstream transport using
/// a client that can accept multiple responses, i.e. was sent via the
/// [`ComposeRequestMulti`] trait.
fn forwarder_multi<CR, K, Upstream>(
    upstream: &Upstream,
    msg: RequestMessage<CR, K>,
    tsig_client: Arc<std::sync::Mutex<Option<TsigClient<K>>>>,
) -> RequestState<K>
where
    CR: ComposeRequestMulti,
    Upstream: SendRequestMulti<RequestMessage<CR, K>> + Send + Sync,
{
    RequestState::GetResponseMulti(upstream.send_request(msg), tsig_client)
}

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

/// The state and related properties of an in-progress request.
struct Request<CR, Upstream, K> {
    /// State of the request.
    state: RequestState<K>,

    /// The request message.
    ///
    /// Initially Some, consumed when sent.
    request_msg: Option<CR>,

    /// The TSIG key used to sign the request.
    key: K,

    /// The upstream transport of the connection.
    upstream: Arc<Upstream>,
}

impl<CR, Upstream, K> Request<CR, Upstream, K>
where
    CR: ComposeRequest,
    Upstream: SendRequest<RequestMessage<CR, K>> + Send + Sync,
    K: Clone + AsRef<Key>,
    Self: GetResponse,
{
    /// Create a new Request object.
    fn new(request_msg: CR, key: K, upstream: Arc<Upstream>) -> Self {
        Self {
            state: RequestState::Init,
            request_msg: Some(request_msg),
            key,
            upstream,
        }
    }
}

impl<CR, Upstream, K> Request<CR, Upstream, K>
where
    CR: Sync + Send,
    K: Clone + AsRef<Key>,
{
    /// Create a new Request object.
    fn new_multi(request_msg: CR, key: K, upstream: Arc<Upstream>) -> Self {
        Self {
            state: RequestState::Init,
            request_msg: Some(request_msg),
            key,
            upstream,
        }
    }

    /// This is the implementation of the get_response method.
    ///
    /// This function is cancel safe.
    async fn get_response_impl(
        &mut self,
        upstream_sender: Forwarder<Upstream, CR, K>,
    ) -> Result<Option<Message<Bytes>>, Error> {
        let (response, tsig_client) = loop {
            match &mut self.state {
                RequestState::Init => {
                    let tsig_client = Arc::new(std::sync::Mutex::new(None));

                    let msg = RequestMessage::new(
                        self.request_msg.take().unwrap(),
                        self.key.clone(),
                        tsig_client.clone(),
                    );

                    trace!("Sending request upstream...");
                    self.state =
                        upstream_sender(&self.upstream, msg, tsig_client);
                    continue;
                }

                RequestState::GetResponse(request, tsig_client) => {
                    let response = request.get_response().await?;
                    break (Some(response), tsig_client);
                }

                RequestState::GetResponseMulti(request, tsig_client) => {
                    let response = request.get_response().await?;
                    break (response, tsig_client);
                }

                RequestState::Complete => {
                    return Err(Error::StreamReceiveError);
                }
            }
        };

        let res = Self::validate_response(response, tsig_client)?;

        if res.is_none() {
            self.state = RequestState::Complete;
        }

        Ok(res)
    }

    /// Perform TSIG validation on the result of receiving a response.
    ///
    /// If no response were received, validation must still be performed in
    /// order to verify that the final message that was received was signed
    /// correctly. This cannot be done when receiving the final response as we
    /// only know that it is final by trying and failing (which may involve
    /// waiting) to receive another response.
    ///
    /// This function therefore takes an optional response message and a
    /// [`TsigClient`]. The process of validating that the final response was
    /// valid will consume the given [`TsigClient`].
    ///
    /// Note: Validation is a destructive process, as it strips the TSIG RR
    /// out of the response. The given response message is consumed, altered
    /// and returned.
    ///
    /// Returns:
    /// - `Ok(Some)` when returning a successfully validated response.
    /// - `Ok(None)` when the end of a responses stream was successfully validated.
    /// - `Err` if validation or some other error occurred.
    fn validate_response(
        response: Option<Message<Bytes>>,
        tsig_client: &mut Arc<std::sync::Mutex<Option<TsigClient<K>>>>,
    ) -> Result<Option<Message<Bytes>>, Error> {
        let res = match response {
            None => {
                let client = tsig_client.lock().unwrap().take().unwrap();
                client.done()?;
                None
            }

            Some(msg) => {
                let mut modifiable_msg =
                    Message::from_octets(msg.as_slice().to_vec())?;

                if let Some(client) = tsig_client.lock().unwrap().deref_mut()
                {
                    trace!("Validating TSIG for sequence reply");
                    client.answer(&mut modifiable_msg, Time48::now())?;
                }

                let out_vec = modifiable_msg.into_octets();
                let out_bytes = Bytes::from(out_vec);
                let out_msg = Message::<Bytes>::from_octets(out_bytes)?;
                Some(out_msg)
            }
        };

        Ok(res)
    }
}

//-- Debug

impl<CR, Upstream, K> Debug for Request<CR, Upstream, K> {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
        f.debug_struct("Request").finish()
    }
}

//--- GetResponse

impl<CR, Upstream, K> GetResponse for Request<CR, Upstream, K>
where
    CR: ComposeRequest,
    Upstream: SendRequest<RequestMessage<CR, K>> + Send + Sync,
    K: Clone + AsRef<Key> + Send + Sync,
{
    fn get_response(
        &mut self,
    ) -> Pin<
        Box<
            dyn Future<Output = Result<Message<Bytes>, Error>>
                + Send
                + Sync
                + '_,
        >,
    > {
        Box::pin(async move {
            // Unwrap the one and only response, we don't need the multiple
            // response handling ability of [`Request::get_response_impl`].
            self.get_response_impl(forwarder).await.map(|v| v.unwrap())
        })
    }
}

//--- GetResponseMulti

impl<CR, Upstream, K> GetResponseMulti for Request<CR, Upstream, K>
where
    CR: ComposeRequestMulti,
    Upstream: SendRequestMulti<RequestMessage<CR, K>> + Send + Sync,
    K: Clone + AsRef<Key> + Send + Sync,
{
    fn get_response(
        &mut self,
    ) -> Pin<
        Box<
            dyn Future<Output = Result<Option<Message<Bytes>>, Error>>
                + Send
                + Sync
                + '_,
        >,
    > {
        Box::pin(self.get_response_impl(forwarder_multi))
    }
}

//------------ RequestState ---------------------------------------------------

/// State machine used by [`Request::get_response_impl`].
///
/// Possible flows:
///   - Init -> GetResponse
///   - Init -> GetResponseMulti -> Complete
enum RequestState<K> {
    /// Initial state, waiting to sign and send the request.
    Init,

    /// Waiting for a response to verify.
    GetResponse(
        Box<dyn GetResponse + Send + Sync>,
        Arc<std::sync::Mutex<Option<TsigClient<K>>>>,
    ),

    /// Wait for multiple responses to verify.
    GetResponseMulti(
        Box<dyn GetResponseMulti + Send + Sync>,
        Arc<std::sync::Mutex<Option<TsigClient<K>>>>,
    ),

    /// The last of multiple responses was received and verified.
    ///
    /// Note: This state can only be entered when processing a sequence of
    /// responses, i.e. using [`GetResponseMulti`]. When using [`GetResponse`]
    /// this state will not be enetered because it only calls
    /// [`Request::get_response_impl`] once.
    Complete,
}

//------------ RequestMessage -------------------------------------------------

/// A message that can be sent using a [`Connection`].
///
/// This type implements the [`ComposeRequest`] and [`ComposeRequestMulti`]
/// traits and thus is compatible with the [`SendRequest`] and
/// [`SendRequestMulti`] traits implemented by [`Connection`].
///
/// This type stores the message to be sent and implements the
/// [`ComposeRequest`] and [`ComposeRequestMulti`] traits so that when the
/// upstream transport accesses the message via the traits that we can at that
/// point sign the request.
///
/// Signing it earlier is not possible as the upstream transport may modify
/// the request prior to sending it, e.g. to assign a message ID or to add
/// EDNS options, and signing **MUST** be the last modification made to the
/// message prior to sending.
#[derive(Clone, Debug)]
pub struct RequestMessage<CR, K>
where
    CR: Send + Sync,
{
    /// The actual request to sign.
    request: CR,

    /// The TSIG key to sign the request with.
    key: K,

    /// The TSIG signer state.
    ///
    /// This must be kept here as it is created only when signing the request
    /// and is needed later when verifying responses.
    ///
    /// Note: It is wrapped inside an [`Arc<Mutex<T>>`] because the signing is
    /// done in [`Request::get_response_impl`] which returns a [`Future`] and
    /// the compiler has no way of knowing whether or not a second call to
    /// [`Request::get_response_impl`] could be made concurrently with an
    /// earlier invocation which has not yet completed its progression through
    /// its async state machine, and could be "woken up" in parallel on a
    /// different thread thus requiring that access to the signer be made
    /// thread safe via a locking mechanism like [`Mutex`].
    signer: Arc<std::sync::Mutex<Option<TsigClient<K>>>>,
}

impl<CR, K> RequestMessage<CR, K>
where
    CR: Send + Sync,
{
    /// Creates a new [`RequestMessage`].
    fn new(
        request: CR,
        key: K,
        signer: Arc<std::sync::Mutex<Option<TsigClient<K>>>>,
    ) -> Self
    where
        CR: Sync + Send,
        K: Clone + AsRef<Key>,
    {
        Self {
            request,
            key,
            signer,
        }
    }
}

impl<CR, K> ComposeRequest for RequestMessage<CR, K>
where
    CR: ComposeRequest,
    K: Clone + Debug + Send + Sync + AsRef<Key>,
{
    // Used by the stream transport.
    fn append_message<Target: Composer>(
        &self,
        target: Target,
    ) -> Result<AdditionalBuilder<Target>, CopyRecordsError> {
        let mut target = self.request.append_message(target)?;

        let client = {
            trace!(
                "Signing single request transaction with key '{}'",
                self.key.as_ref().name()
            );
            TsigClient::Transaction(
                ClientTransaction::request(
                    self.key.clone(),
                    &mut target,
                    Time48::now(),
                )
                .unwrap(),
            )
        };

        *self.signer.lock().unwrap() = Some(client);

        Ok(target)
    }

    fn to_vec(&self) -> Result<Vec<u8>, Error> {
        let msg = self.to_message()?;
        Ok(msg.as_octets().clone())
    }

    fn to_message(&self) -> Result<Message<Vec<u8>>, Error> {
        let mut target = StaticCompressor::new(Vec::new());

        self.append_message(&mut target)?;

        // It would be nice to use .builder() here. But that one deletes all
        // sections. We have to resort to .as_builder() which gives a
        // reference and then .clone()
        let msg = Message::from_octets(target.into_target()).expect(
            "Message should be able to parse output from MessageBuilder",
        );
        Ok(msg)
    }

    fn header(&self) -> &crate::base::Header {
        self.request.header()
    }

    fn header_mut(&mut self) -> &mut crate::base::Header {
        self.request.header_mut()
    }

    fn set_udp_payload_size(&mut self, value: u16) {
        self.request.set_udp_payload_size(value)
    }

    fn set_dnssec_ok(&mut self, value: bool) {
        self.request.set_dnssec_ok(value)
    }

    fn add_opt(
        &mut self,
        opt: &impl crate::base::opt::ComposeOptData,
    ) -> Result<(), crate::base::opt::LongOptData> {
        self.request.add_opt(opt)
    }

    fn is_answer(&self, answer: &Message<[u8]>) -> bool {
        self.request.is_answer(answer)
    }

    fn dnssec_ok(&self) -> bool {
        self.request.dnssec_ok()
    }
}

impl<CR, K> ComposeRequestMulti for RequestMessage<CR, K>
where
    CR: ComposeRequestMulti,
    K: Clone + Debug + Send + Sync + AsRef<Key>,
{
    // Used by the stream transport.
    fn append_message<Target: Composer>(
        &self,
        target: Target,
    ) -> Result<AdditionalBuilder<Target>, CopyRecordsError> {
        let mut target = self.request.append_message(target)?;

        trace!(
            "Signing streaming request sequence with key '{}'",
            self.key.as_ref().name()
        );
        let client = TsigClient::Sequence(
            ClientSequence::request(
                self.key.clone(),
                &mut target,
                Time48::now(),
            )
            .unwrap(),
        );

        *self.signer.lock().unwrap() = Some(client);

        Ok(target)
    }

    fn to_message(&self) -> Result<Message<Vec<u8>>, Error> {
        let mut target = StaticCompressor::new(Vec::new());

        self.append_message(&mut target)?;

        // It would be nice to use .builder() here. But that one deletes all
        // sections. We have to resort to .as_builder() which gives a
        // reference and then .clone()
        let msg = Message::from_octets(target.into_target()).expect(
            "Message should be able to parse output from MessageBuilder",
        );
        Ok(msg)
    }

    fn header(&self) -> &crate::base::Header {
        self.request.header()
    }

    fn header_mut(&mut self) -> &mut crate::base::Header {
        self.request.header_mut()
    }

    fn set_udp_payload_size(&mut self, value: u16) {
        self.request.set_udp_payload_size(value)
    }

    fn set_dnssec_ok(&mut self, value: bool) {
        self.request.set_dnssec_ok(value)
    }

    fn add_opt(
        &mut self,
        opt: &impl crate::base::opt::ComposeOptData,
    ) -> Result<(), crate::base::opt::LongOptData> {
        self.request.add_opt(opt)
    }

    fn is_answer(&self, answer: &Message<[u8]>) -> bool {
        self.request.is_answer(answer)
    }

    fn dnssec_ok(&self) -> bool {
        self.request.dnssec_ok()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::base::iana::Rcode;
    use crate::base::message_builder::QuestionBuilder;
    use crate::base::{MessageBuilder, Name, Rtype};
    use crate::tsig::{
        Algorithm, KeyName, KeyStore, ServerSequence, ServerTransaction,
        ValidationError,
    };
    use core::future::ready;
    use core::str::FromStr;

    #[tokio::test]
    async fn single_signed_valid_response() {
        do_single_response(false).await;
    }

    #[tokio::test]
    async fn single_signed_invalid_response() {
        do_single_response(true).await;
    }

    async fn do_single_response(invalidate_signature: bool) {
        // Make a query message that would be expected to result in a single
        // reply.
        let msg = mk_request_msg(Rtype::A);

        // Wrap that message into a request message compatible with a
        // transport capable of receving a single response.
        let req =
            crate::net::client::request::RequestMessage::new(msg).unwrap();

        // Make a TSIG key to sign the request with.
        let key = mk_tsig_key();

        // Make a mock upstream that will "send" the request and receives a
        // mock TSIG signed response back.
        let upstream =
            Arc::new(MockUpstream::new(key.clone(), invalidate_signature));

        // Wrap the request message into a TSIG signing request with a signing
        // key and upstream transport.
        let mut req = Request::new(req, key, upstream);

        // "Send" the request and receive the validated mock response.
        let res = req.get_response().await;

        assert_eq!(res.is_err(), invalidate_signature);

        if let Ok(res) = res {
            // Verify that the mock response has had its TSIG RR stripped out
            // during validation.
            assert_eq!(res.header_counts().arcount(), 0, "TSIG RR should have been removed from the additional section during response processing");
        }
    }

    #[tokio::test]
    async fn multiple_signed_valid_responses() {
        do_multiple_responses(false, false).await
    }

    #[tokio::test]
    async fn multiple_signed_responses_with_one_invalid() {
        do_multiple_responses(true, false).await
    }

    #[tokio::test]
    async fn multiple_signed_valid_responses_and_a_final_unsigned_response() {
        do_multiple_responses(false, true).await
    }

    async fn do_multiple_responses(
        invalidate_signature: bool,
        dont_sign_last_response: bool,
    ) {
        // Make a query message that would be expected to result in multiple
        // replies.
        let msg = mk_request_msg(Rtype::AXFR);

        // Wrap that message into a request message compatible with a
        // transport capable of receving multiple responses.
        let req = crate::net::client::request::RequestMessageMulti::new(msg)
            .unwrap();

        // Make a TSIG key to sign the request with.
        let key = mk_tsig_key();

        // Make a mock upstream that will "send" the request and receive
        // multiple mock TSIG signed responses back.
        let upstream = Arc::new(MockUpstreamMulti::new(
            key.clone(),
            invalidate_signature,
            dont_sign_last_response,
        ));

        // Wrap the request message into a TSIG signing request with a signing
        // key and upstream transport.
        let mut req = Request::new_multi(req, key, upstream);

        // "Send" the request and receive the first validated mock response.
        let res = req
            .get_response()
            .await
            .unwrap()
            .expect("First response is missing");

        // Verify that the mock response has had its TSIG RR stripped out
        // during validation.
        assert_eq!(res.header_counts().arcount(), 0, "TSIG RR should have been removed from the additional section during response processing");

        // Receive the second mock response, which may have been deliberately
        // invalidated.
        let res = req.get_response().await;

        if invalidate_signature {
            assert!(
                matches!(
                    res,
                    Err(Error::Authentication(ValidationError::BadSig))
                ),
                "Expected error BadSig but the result was: {res:?}"
            );
        } else {
            assert!(res.is_ok(), "Unexpected error message: {res:?}");
        }

        if let Ok(res) = res {
            let res = res.expect("Second response is missing");

            // Verify that the mock response has had its TSIG RR stripped out
            // during validation.
            assert_eq!(res.header_counts().arcount(), 0, "TSIG RR should have been removed from the additional section during response processing");

            // Receive the third and final mock response, which may have been
            // deliberately not signed, in order to test whether or not we
            // are correctly calling `ClientSequence::done()` to catch this
            // case. This shouldn't fail at this point however as apparently
            // it's only caught when .done() is called when no more responses
            // are received.
            let res = req
                .get_response()
                .await
                .unwrap()
                .expect("Third response is missing");

            // Verify that the mock response has had its TSIG RR stripped out
            // during validation, or it was never added during response
            // generation.
            if dont_sign_last_response {
                assert_eq!(res.header_counts().arcount(), 0, "TSIG RR should never have been added to the additional section during response generation");
            } else {
                assert_eq!(res.header_counts().arcount(), 0, "TSIG RR should have been removed from the additional section during response processing");
            }

            if dont_sign_last_response {
                // Attempt to receive another response but discover that the
                // last response was not signed as it should have been.
                assert!(
                    matches!(req.get_response().await, Err(Error::Authentication(ValidationError::TooManyUnsigned))),
                    "Receiving another response should have failed because the last response should have lacked a signature"
                );
            } else {
                // Attempt to receive another response but discover that this
                // is the end of the response sequence.
                assert!(
                    req.get_response().await.unwrap().is_none(),
                    "There should not be a fourth response"
                );
            }
        }
    }

    // Make a query for the given RTYPE.
    fn mk_request_msg(rtype: Rtype) -> QuestionBuilder<Vec<u8>> {
        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))
            .unwrap();
        msg
    }

    // Make a TSIG key for signing test requests and responses with.
    fn mk_tsig_key() -> Arc<Key> {
        // Create a signing key.
        let key_name = KeyName::from_str("demo-key").unwrap();
        let secret = crate::utils::base64::decode::<Vec<u8>>(
            "zlCZbVJPIhobIs1gJNQfrsS3xCxxsR9pMUrGwG8OgG8=",
        )
        .unwrap();
        Arc::new(
            Key::new(Algorithm::Sha256, &secret, key_name, None, None)
                .unwrap(),
        )
    }

    //------------ MockGetResponse --------------------------------------------

    #[derive(Debug)]
    struct MockGetResponse<CR, KS> {
        request_msg: CR,
        key_store: KS,
        invalidate_signature: bool,
    }

    impl<CR, KS> MockGetResponse<CR, KS> {
        fn new(
            request_msg: CR,
            key_store: KS,
            invalidate_signature: bool,
        ) -> Self {
            Self {
                request_msg,
                key_store,
                invalidate_signature,
            }
        }
    }

    //--- GetResponse

    impl<CR: ComposeRequest + Debug, KS: Debug + KeyStore> GetResponse
        for MockGetResponse<CR, KS>
    {
        fn get_response(
            &mut self,
        ) -> Pin<
            Box<
                dyn Future<Output = Result<Message<Bytes>, Error>>
                    + Send
                    + Sync
                    + '_,
            >,
        > {
            let mut req = self.request_msg.to_message().unwrap();

            // Create a TSIG signer for a single response based on the
            // received request.
            let tsig = ServerTransaction::request(
                &self.key_store,
                &mut req,
                Time48::now(),
            )
            .unwrap()
            .unwrap();

            // Generate a mock response to the request.
            let builder = MessageBuilder::new_bytes();
            let builder = builder.start_answer(&req, Rcode::NOERROR).unwrap();
            let mut builder = builder.additional();

            // Sign the response.
            tsig.answer(&mut builder, Time48::now()).unwrap();

            if self.invalidate_signature {
                // Invalidate the signature.
                builder.header_mut().set_rcode(Rcode::SERVFAIL);
            }

            // Generate the wire format response message and sanity check it
            // before returning it.
            let res = builder.into_message();
            assert_eq!(res.header_counts().arcount(), 1, "Constructed response lacks a TSIG RR in the additional section");
            Box::pin(ready(Ok(res)))
        }
    }

    //------------ MockGetResponseMulti ---------------------------------------

    #[derive(Debug)]
    struct MockGetResponseMulti<CR, KS> {
        request_msg: CR,
        key_store: KS,
        sent_request: Option<Message<Vec<u8>>>,
        num_responses_generated: usize,
        signer: Option<ServerSequence<KS>>,
        invalidate_signature: bool,
        dont_sign_last_response: bool,
    }

    impl<CR, KS> MockGetResponseMulti<CR, KS> {
        fn new(
            request_msg: CR,
            key_store: KS,
            invalidate_signature: bool,
            dont_sign_last_response: bool,
        ) -> Self {
            Self {
                request_msg,
                key_store,
                sent_request: None,
                num_responses_generated: 0,
                signer: None,
                invalidate_signature,
                dont_sign_last_response,
            }
        }
    }

    //--- GetResponseMulti

    impl<CR, KS> GetResponseMulti for MockGetResponseMulti<CR, KS>
    where
        CR: ComposeRequestMulti + Debug,
        KS: Debug + KeyStore<Key = KS> + AsRef<Key>,
    {
        fn get_response(
            &mut self,
        ) -> Pin<
            Box<
                dyn Future<Output = Result<Option<Message<Bytes>>, Error>>
                    + Send
                    + Sync
                    + '_,
            >,
        > {
            // Generate a sequence of at most 3 responses.
            if self.num_responses_generated == 3 {
                return Box::pin(ready(Ok(None)));
            }

            self.num_responses_generated += 1;

            // When first receiving the request, generate a TSIG signer for a
            // multiple response sequence based on the received request.
            let mut tsig = match self.signer.take() {
                Some(tsig) => tsig,
                None => {
                    let mut req = self.request_msg.to_message().unwrap();

                    let tsig = ServerSequence::request(
                        &self.key_store,
                        &mut req,
                        Time48::now(),
                    )
                    .unwrap()
                    .unwrap();

                    // Store the signer, we'll need it to sign subsequent
                    // responses.
                    self.sent_request = Some(req);

                    tsig
                }
            };

            // Generate a mock response to the request.
            let req = self.sent_request.as_ref().unwrap();
            let builder = MessageBuilder::new_bytes();
            let builder = builder.start_answer(req, Rcode::NOERROR).unwrap();
            let mut builder = builder.additional();

            // Decide whether to sign and/or invalidate the response.
            let (sign, invalidate) = match self.num_responses_generated {
                1 => (true, false),
                2 => (true, self.invalidate_signature),
                3 => (!self.dont_sign_last_response, false),
                _ => unreachable!(),
            };

            eprintln!(
                "Response {}: sign={}, invalidate={}",
                self.num_responses_generated, sign, invalidate
            );

            // Sign the response (we might strip the signature out below).
            if sign {
                tsig.answer(&mut builder, Time48::now()).unwrap();
            }

            // Put the signer back in storage for the next response.
            self.signer = Some(tsig);

            if invalidate {
                // Invalidate the signature.
                builder.header_mut().set_rcode(Rcode::SERVFAIL);
            }

            // Generate the wire format response message and sanity check it
            // before returning it.
            let res = builder.into_message();
            if sign {
                assert_eq!(res.header_counts().arcount(), 1, "Constructed response lacks a TSIG RR in the additional section");
                let rec = res.additional().unwrap().next().unwrap().unwrap();
                assert_eq!(rec.rtype(), Rtype::TSIG);
            }
            Box::pin(ready(Ok(Some(res))))
        }
    }

    //------------ MockUpstream -----------------------------------------------

    struct MockUpstream {
        key: Arc<Key>,
        invalidate_signature: bool,
    }

    impl MockUpstream {
        fn new(key: Arc<Key>, invalidate_signature: bool) -> Self {
            Self {
                key,
                invalidate_signature,
            }
        }
    }

    //--- SendRequest

    impl<CR: ComposeRequest + Debug + Send + Sync + 'static> SendRequest<CR>
        for MockUpstream
    {
        fn send_request(
            &self,
            request_msg: CR,
        ) -> Box<dyn GetResponse + Send + Sync> {
            Box::new(MockGetResponse::new(
                request_msg,
                self.key.clone(),
                self.invalidate_signature,
            ))
        }
    }

    //------------ MockUpstreamMulti ------------------------------------------

    struct MockUpstreamMulti {
        key: Arc<Key>,
        invalidate_signature: bool,
        dont_sign_last_response: bool,
    }
    impl MockUpstreamMulti {
        fn new(
            key: Arc<Key>,
            invalidate_signature: bool,
            dont_sign_last_response: bool,
        ) -> Self {
            Self {
                key,
                invalidate_signature,
                dont_sign_last_response,
            }
        }
    }

    impl<CR> SendRequestMulti<CR> for MockUpstreamMulti
    where
        CR: ComposeRequestMulti + Debug + Send + Sync + 'static,
    {
        fn send_request(
            &self,
            request_msg: CR,
        ) -> Box<dyn GetResponseMulti + Send + Sync> {
            Box::new(MockGetResponseMulti::new(
                request_msg,
                self.key.clone(),
                self.invalidate_signature,
                self.dont_sign_last_response,
            ))
        }
    }
}