wtransport 0.6.1

Implementation of the WebTransport (over HTTP3) protocol
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
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
//!
//! This module defines configurations for the WebTransport server and client.
//!
//! It provides builders for creating server and client configurations with various options.
//!
//! The module includes:
//! - [`ServerConfig`]: Configuration for the WebTransport server.
//! - [`ClientConfig`]: Configuration for the WebTransport client.
//!
//! Example for creating a server configuration:
//!
//! ```no_run
//! # async fn run() -> anyhow::Result<()> {
//! use wtransport::Identity;
//! use wtransport::ServerConfig;
//!
//! let server_config = ServerConfig::builder()
//!     .with_bind_default(443)
//!     .with_identity(Identity::load_pemfiles("cert.pem", "key.pem").await?)
//!     .build();
//!
//! # Ok(())
//! # }
//! ```
//!
//! Example for creating a client configuration:
//!
//! ```no_run
//! use wtransport::ClientConfig;
//!
//! let client_config = ClientConfig::builder()
//!     .with_bind_default()
//!     .with_native_certs()
//!     .build();
//! ```

use crate::tls::build_native_cert_store;
use crate::tls::Identity;
use quinn::EndpointConfig;
use quinn::TransportConfig;
use socket2::Domain as SocketDomain;
use socket2::Protocol as SocketProtocol;
use socket2::Socket;
use socket2::Type as SocketType;
use std::fmt::Debug;
use std::fmt::Display;
use std::future::Future;
use std::net::IpAddr;
use std::net::Ipv4Addr;
use std::net::Ipv6Addr;
use std::net::SocketAddr;
use std::net::SocketAddrV4;
use std::net::SocketAddrV6;
use std::net::UdpSocket;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;

/// Alias of [`crate::tls::rustls::ServerConfig`].
pub type TlsServerConfig = crate::tls::rustls::ServerConfig;

/// Alias of [`crate::tls::rustls::ClientConfig`].
pub type TlsClientConfig = crate::tls::rustls::ClientConfig;

/// Alias of [`crate::quinn::TransportConfig`].
#[cfg(feature = "quinn")]
#[cfg_attr(docsrs, doc(cfg(feature = "quinn")))]
pub type QuicTransportConfig = crate::quinn::TransportConfig;

/// Alias of [`crate::quinn::ServerConfig`].
#[cfg(feature = "quinn")]
#[cfg_attr(docsrs, doc(cfg(feature = "quinn")))]
pub type QuicServerConfig = crate::quinn::ServerConfig;

/// Alias of [`crate::quinn::ClientConfig`].
#[cfg(feature = "quinn")]
#[cfg_attr(docsrs, doc(cfg(feature = "quinn")))]
pub type QuicClientConfig = crate::quinn::ClientConfig;

/// Configuration for IP address socket bind.
#[derive(Debug, Copy, Clone)]
pub enum IpBindConfig {
    /// Bind to LOCALHOST IPv4 address (no IPv6).
    LocalV4,

    /// Bind to LOCALHOST IPv6 address (no IPv4).
    LocalV6,

    /// Bind to LOCALHOST both IPv4 and IPv6 address (dual stack, if supported).
    LocalDual,

    /// Bind to `INADDR_ANY` IPv4 address (no IPv6).
    InAddrAnyV4,

    /// Bind to `INADDR_ANY` IPv6 address (no IPv4).
    InAddrAnyV6,

    /// Bind to `INADDR_ANY` both IPv4 and IPv6 address (dual stack, if supported).
    InAddrAnyDual,
}

impl IpBindConfig {
    fn into_ip(self) -> IpAddr {
        match self {
            IpBindConfig::LocalV4 => Ipv4Addr::LOCALHOST.into(),
            IpBindConfig::LocalV6 => Ipv6Addr::LOCALHOST.into(),
            IpBindConfig::LocalDual => Ipv6Addr::LOCALHOST.into(),
            IpBindConfig::InAddrAnyV4 => Ipv4Addr::UNSPECIFIED.into(),
            IpBindConfig::InAddrAnyV6 => Ipv6Addr::UNSPECIFIED.into(),
            IpBindConfig::InAddrAnyDual => Ipv6Addr::UNSPECIFIED.into(),
        }
    }

    fn into_dual_stack_config(self) -> Ipv6DualStackConfig {
        match self {
            IpBindConfig::LocalV4 | IpBindConfig::InAddrAnyV4 => Ipv6DualStackConfig::OsDefault,
            IpBindConfig::LocalV6 | IpBindConfig::InAddrAnyV6 => Ipv6DualStackConfig::Deny,
            IpBindConfig::LocalDual | IpBindConfig::InAddrAnyDual => Ipv6DualStackConfig::Allow,
        }
    }
}

/// Configuration for IPv6 dual stack.
#[derive(Debug, Copy, Clone)]
pub enum Ipv6DualStackConfig {
    /// Do not configure dual stack. Use OS's default.
    OsDefault,

    /// Deny dual stack. This is equivalent to `IPV6_V6ONLY`.
    ///
    /// Socket will only bind for IPv6 (IPv4 port will still be available).
    Deny,

    /// Allow dual stack.
    ///
    /// Please note that not all configurations/platforms support dual stack.
    Allow,
}

/// Invalid idle timeout.
pub struct InvalidIdleTimeout;

/// Server configuration.
///
/// You can create an instance of `ServerConfig` using its builder pattern by calling
/// the [`builder()`](Self::builder) method.
/// Once you have an instance, you can further customize it by chaining method calls
/// to set various configuration options.
///
/// ## Configuration Builder States
///
/// The configuration process follows a *state-based builder pattern*, where the server
/// configuration progresses through *3* states.
///
/// ### 1. `WantsBindAddress`
///
/// The caller must supply a binding address for the server. This is where to specify
/// the listening port of the server.
/// The following options are mutually exclusive:
///
///   - [`with_bind_default`](ServerConfigBuilder::with_bind_default): the simplest
///     configuration where only the port will be specified.
///   - [`with_bind_config`](ServerConfigBuilder::with_bind_config): configures
///     binding to an address determined by a configuration preset.
///   - [`with_bind_address`](ServerConfigBuilder::with_bind_address): configures
///     binding to a custom-specified socket address.
///   - [`with_bind_address_v6`](ServerConfigBuilder::with_bind_address_v6): configures
///     binding to a custom-specified socket address for *IPv6*, along with the [dual stack
///     configuration](Ipv6DualStackConfig).
///   - [`with_bind_socket`](ServerConfigBuilder::with_bind_socket): configures
///     binding directly to a custom-specified socket.
///
/// Only one of these options can be selected during the client configuration process.
///
/// #### Examples:
///
/// ```
/// use wtransport::ServerConfig;
///
/// // Configuration for accepting incoming connection on port 443
/// ServerConfig::builder().with_bind_default(443);
/// ```
///
/// ### 2. `WantsIdentity`
///
/// The caller must supply a TLS identity for the server.
///
/// - [`with_identity`](ServerConfigBuilder::with_identity): configures
///   a TLS [`Identity`] for the server.
/// - [`with_custom_tls`](ServerConfigBuilder::with_custom_tls): sets the TLS
///   server configuration manually.
/// - [`with_custom_transport`](ServerConfigBuilder::with_custom_transport): sets the QUIC
///   transport configuration manually (using default TLS).
/// - [`with_custom_tls_and_transport`](ServerConfigBuilder::with_custom_tls_and_transport): sets both
///   a custom TLS and QUIC transport configuration.
/// - [`build_with_quic_config`](ServerConfigBuilder::build_with_quic_config): directly builds
///   [`ServerConfig`] providing both TLS and QUIC transport configuration given by
///   [`quic_config`](QuicServerConfig).
///
/// #### Examples:
/// ```
/// # use anyhow::Result;
/// use wtransport::Identity;
/// use wtransport::ServerConfig;
///
/// # async fn run() -> Result<()> {
/// ServerConfig::builder()
///     .with_bind_default(443)
///     .with_identity(Identity::load_pemfiles("cert.pem", "key.pem").await?);
/// # Ok(())
/// # }
/// ```
///
/// ### 3. `WantsTransportConfigServer`
///
/// The caller can supply *additional* transport configurations.
/// Multiple options can be given at this stage. Once the configuration is completed, it is possible
/// to finalize with the method [`build()`](ServerConfigBuilder::build).
///
/// All these options can be omitted in the configuration; default values will be used.
///
/// - [`max_idle_timeout`](ServerConfigBuilder::max_idle_timeout)
/// - [`keep_alive_interval`](ServerConfigBuilder::keep_alive_interval)
/// - [`allow_migration`](ServerConfigBuilder::allow_migration)
///
/// #### Examples:
/// ```
/// # use anyhow::Result;
/// use wtransport::ServerConfig;
/// use wtransport::Identity;
/// use std::time::Duration;
///
/// # async fn run() -> Result<()> {
/// let server_config = ServerConfig::builder()
///     .with_bind_default(443)
///     .with_identity(Identity::load_pemfiles("cert.pem", "key.pem").await?)
///     .keep_alive_interval(Some(Duration::from_secs(3)))
///     .build();
/// # Ok(())
/// # }
#[derive(Debug)]
pub struct ServerConfig {
    pub(crate) bind_address_config: BindAddressConfig,
    pub(crate) endpoint_config: quinn::EndpointConfig,
    pub(crate) quic_config: quinn::ServerConfig,
}

impl ServerConfig {
    /// Creates a builder to build up the server configuration.
    ///
    /// For more information, see the [`ServerConfigBuilder`] documentation.
    pub fn builder() -> ServerConfigBuilder<states::WantsBindAddress> {
        ServerConfigBuilder::default()
    }

    /// Returns a reference to the inner QUIC endpoint configuration.
    #[cfg(feature = "quinn")]
    #[cfg_attr(docsrs, doc(cfg(feature = "quinn")))]
    pub fn quic_endpoint_config(&self) -> &quinn::EndpointConfig {
        &self.endpoint_config
    }

    /// Returns a mutable reference to the inner QUIC endpoint configuration.
    #[cfg(feature = "quinn")]
    #[cfg_attr(docsrs, doc(cfg(feature = "quinn")))]
    pub fn quic_endpoint_config_mut(&mut self) -> &mut quinn::EndpointConfig {
        &mut self.endpoint_config
    }

    /// Returns a reference to the inner QUIC configuration.
    #[cfg(feature = "quinn")]
    #[cfg_attr(docsrs, doc(cfg(feature = "quinn")))]
    pub fn quic_config(&self) -> &quinn::ServerConfig {
        &self.quic_config
    }

    /// Returns a mutable reference to the inner QUIC configuration.
    #[cfg(feature = "quinn")]
    #[cfg_attr(docsrs, doc(cfg(feature = "quinn")))]
    pub fn quic_config_mut(&mut self) -> &mut quinn::ServerConfig {
        &mut self.quic_config
    }
}

/// Server builder configuration.
///
/// The builder might have different state at compile time.
///
/// # Examples:
/// ```no_run
/// # async fn run() -> anyhow::Result<()> {
/// # use std::net::Ipv4Addr;
/// # use std::net::SocketAddr;
/// # use wtransport::Identity;
/// # use wtransport::ServerConfig;
/// let config = ServerConfig::builder()
///     .with_bind_default(4433)
///     .with_identity(Identity::load_pemfiles("cert.pem", "key.pem").await?);
/// # Ok(())
/// # }
/// ```
#[must_use]
pub struct ServerConfigBuilder<State>(State);

impl ServerConfigBuilder<states::WantsBindAddress> {
    /// Configures for accepting incoming connections binding ANY IP (allowing IP dual-stack).
    ///
    /// `listening_port` is the port where the server will accept incoming connections.
    ///
    /// This is equivalent to: [`Self::with_bind_config`] with [`IpBindConfig::InAddrAnyDual`].
    pub fn with_bind_default(
        self,
        listening_port: u16,
    ) -> ServerConfigBuilder<states::WantsIdentity> {
        self.with_bind_config(IpBindConfig::InAddrAnyDual, listening_port)
    }

    /// Sets the binding (local) socket address with a specific [`IpBindConfig`].
    ///
    /// `listening_port` is the port where the server will accept incoming connections.
    pub fn with_bind_config(
        self,
        ip_bind_config: IpBindConfig,
        listening_port: u16,
    ) -> ServerConfigBuilder<states::WantsIdentity> {
        let ip_address: IpAddr = ip_bind_config.into_ip();

        match ip_address {
            IpAddr::V4(ip) => self.with_bind_address(SocketAddr::new(ip.into(), listening_port)),
            IpAddr::V6(ip) => self.with_bind_address_v6(
                SocketAddrV6::new(ip, listening_port, 0, 0),
                ip_bind_config.into_dual_stack_config(),
            ),
        }
    }

    /// Sets the binding (local) socket address for the endpoint.
    pub fn with_bind_address(
        self,
        address: SocketAddr,
    ) -> ServerConfigBuilder<states::WantsIdentity> {
        ServerConfigBuilder(states::WantsIdentity {
            bind_address_config: BindAddressConfig::from(address),
        })
    }

    /// Sets the binding (local) socket address for the endpoint with Ipv6 address.
    ///
    /// `dual_stack_config` allows/denies dual stack port binding.
    pub fn with_bind_address_v6(
        self,
        address: SocketAddrV6,
        dual_stack_config: Ipv6DualStackConfig,
    ) -> ServerConfigBuilder<states::WantsIdentity> {
        ServerConfigBuilder(states::WantsIdentity {
            bind_address_config: BindAddressConfig::AddressV6(address, dual_stack_config),
        })
    }

    /// Configures the server to bind to a pre-existing [`UdpSocket`].
    ///
    /// This allows the server to use an already created socket, which may be beneficial
    /// for scenarios where socket reuse or specific socket configuration is needed.
    pub fn with_bind_socket(self, socket: UdpSocket) -> ServerConfigBuilder<states::WantsIdentity> {
        ServerConfigBuilder(states::WantsIdentity {
            bind_address_config: BindAddressConfig::Socket(socket),
        })
    }
}

impl ServerConfigBuilder<states::WantsIdentity> {
    /// Configures TLS with safe defaults and a TLS [`Identity`].
    ///
    /// # Example
    /// ```no_run
    /// use wtransport::Identity;
    /// use wtransport::ServerConfig;
    /// # use anyhow::Result;
    ///
    /// # async fn run() -> Result<()> {
    /// let identity = Identity::load_pemfiles("cert.pem", "key.pem").await?;
    ///
    /// let server_config = ServerConfig::builder()
    ///     .with_bind_default(4433)
    ///     .with_identity(identity)
    ///     .build();
    /// # Ok(())
    /// # }
    /// ```
    pub fn with_identity(
        self,
        identity: Identity,
    ) -> ServerConfigBuilder<states::WantsTransportConfigServer> {
        use crate::tls::server::build_default_tls_config;

        let tls_config = build_default_tls_config(identity);
        let quic_endpoint_config = EndpointConfig::default();
        let quic_transport_config = TransportConfig::default();

        self.with(tls_config, quic_endpoint_config, quic_transport_config)
    }

    /// Allows for manual configuration of a custom TLS setup using a provided
    /// [`rustls::ServerConfig`], which must support
    /// [`rustls::CipherSuite::TLS13_AES_128_GCM_SHA256`]. A suitable configuration
    /// can be obtained using the `ring` crypto provider with a set of versions containing
    /// [`rustls::version::TLS13`].
    ///
    /// This method is provided for advanced users who need fine-grained control over the
    /// TLS configuration. It allows you to pass a preconfigured [`rustls::ServerConfig`]
    /// instance to customize the TLS settings according to your specific requirements.
    ///
    /// Generally, it is recommended to use the [`with_identity`](Self::with_identity) method
    /// to configure TLS with safe defaults and an TLS [`Identity`].
    ///
    /// # Example
    ///
    /// ```no_run
    /// use wtransport::tls::rustls;
    /// use wtransport::ServerConfig;
    ///
    /// // Create a custom rustls::ServerConfig with specific TLS settings
    /// let custom_tls_config = rustls::ServerConfig::builder();
    /// // Customize TLS settings here...
    /// # let custom_tls_config = custom_tls_config
    /// #          .with_no_client_auth()
    /// #          .with_single_cert(todo!(), todo!()).unwrap();
    ///
    /// // Create a ServerConfigBuilder with the custom TLS configuration
    /// let server_config = ServerConfig::builder()
    ///     .with_bind_default(4433)
    ///     .with_custom_tls(custom_tls_config)
    ///     .build();
    /// ```
    pub fn with_custom_tls(
        self,
        tls_config: TlsServerConfig,
    ) -> ServerConfigBuilder<states::WantsTransportConfigServer> {
        let quic_endpoint_config = EndpointConfig::default();
        let quic_transport_config = TransportConfig::default();

        self.with(tls_config, quic_endpoint_config, quic_transport_config)
    }

    /// Configures the server with a custom QUIC transport configuration and a default TLS setup
    /// using the provided [`Identity`].
    ///
    /// This method is useful for scenarios where you need to customize the transport settings
    /// while relying on a default TLS configuration built from an [`Identity`]. It gives you
    /// control over the transport layer while maintaining safe and standard TLS settings.
    ///
    /// **See**: [`with_identity`](Self::with_identity)
    /// for a simpler configuration option that does not require custom transport settings.
    ///
    /// # Parameters
    ///
    /// - `identity`: A reference to an [`Identity`] that contains the server's certificate and
    ///   private key. This will be used to generate the default TLS configuration.
    /// - `quic_transport_config`: A custom [`QuicTransportConfig`] instance that allows you to specify
    ///   various QUIC transport-layer settings according to your requirements.
    ///
    /// # Example
    ///
    /// ```
    /// use wtransport::config::QuicTransportConfig;
    /// use wtransport::Identity;
    /// use wtransport::ServerConfig;
    ///
    /// // Generate a server identity (self signed certificate and private key)
    /// let identity = Identity::self_signed(["localhost", "127.0.0.1", "::1"]).unwrap();
    ///
    /// // Create a custom QuicTransportConfig with specific settings
    /// let mut custom_transport_config = QuicTransportConfig::default();
    /// custom_transport_config.datagram_send_buffer_size(1024);
    ///
    /// // Create a ServerConfigBuilder with the custom transport configuration and default TLS settings
    /// let server_config = ServerConfig::builder()
    ///     .with_bind_default(4433)
    ///     .with_custom_transport(identity, custom_transport_config)
    ///     .build();
    /// ```
    #[cfg(feature = "quinn")]
    #[cfg_attr(docsrs, doc(cfg(feature = "quinn")))]
    pub fn with_custom_transport(
        self,
        identity: Identity,
        quic_transport_config: QuicTransportConfig,
    ) -> ServerConfigBuilder<states::WantsTransportConfigServer> {
        use crate::tls::server::build_default_tls_config;

        let tls_config = build_default_tls_config(identity);
        let quic_endpoint_config = EndpointConfig::default();

        self.with(tls_config, quic_endpoint_config, quic_transport_config)
    }

    /// Configures the server with both a custom TLS configuration and a custom QUIC transport
    /// configuration.
    ///
    /// This method is designed for advanced users who require full control over both the TLS
    /// and transport settings. It allows you to pass a preconfigured [`TlsServerConfig`] and
    /// a custom [`QuicTransportConfig`] to fine-tune both layers of the server configuration.
    ///
    /// # Parameters
    ///
    /// - `tls_config`: A custom [`TlsServerConfig`] instance that allows you to specify
    ///   detailed TLS settings, such as ciphersuites, certificate verification, and more. It must
    ///   support TLS 1.3 (see the documentation of [`Self::with_custom_tls`]).
    /// - `quic_transport_config`: A custom [`QuicTransportConfig`] instance that allows you to specify
    ///   various QUIC transport-layer settings according to your requirements.
    #[cfg(feature = "quinn")]
    #[cfg_attr(docsrs, doc(cfg(feature = "quinn")))]
    pub fn with_custom_tls_and_transport(
        self,
        tls_config: TlsServerConfig,
        quic_transport_config: QuicTransportConfig,
    ) -> ServerConfigBuilder<states::WantsTransportConfigServer> {
        let quic_endpoint_config = EndpointConfig::default();
        self.with(tls_config, quic_endpoint_config, quic_transport_config)
    }

    /// Directly builds [`ServerConfig`] skipping TLS and transport configuration.
    ///
    /// Both TLS and transport configuration is given by [`quic_config`](QuicServerConfig).
    #[cfg(feature = "quinn")]
    #[cfg_attr(docsrs, doc(cfg(feature = "quinn")))]
    pub fn build_with_quic_config(self, quic_config: QuicServerConfig) -> ServerConfig {
        ServerConfig {
            bind_address_config: self.0.bind_address_config,
            endpoint_config: EndpointConfig::default(),
            quic_config,
        }
    }

    fn with(
        self,
        tls_config: TlsServerConfig,
        endpoint_config: EndpointConfig,
        transport_config: TransportConfig,
    ) -> ServerConfigBuilder<states::WantsTransportConfigServer> {
        ServerConfigBuilder(states::WantsTransportConfigServer {
            bind_address_config: self.0.bind_address_config,
            tls_config,
            endpoint_config,
            transport_config,
            migration: true,
        })
    }
}

impl ServerConfigBuilder<states::WantsTransportConfigServer> {
    /// Completes configuration process.
    ///
    /// # Panics
    ///
    /// See the documentation of [`Self::with_custom_tls`] for the TLS 1.3 requirement.
    #[must_use]
    pub fn build(self) -> ServerConfig {
        let crypto: Arc<quinn::crypto::rustls::QuicServerConfig> = Arc::new(
            quinn::crypto::rustls::QuicServerConfig::try_from(self.0.tls_config)
                .expect("CipherSuite::TLS13_AES_128_GCM_SHA256 missing"),
        );

        let mut quic_config = quinn::ServerConfig::with_crypto(crypto);

        quic_config.transport_config(Arc::new(self.0.transport_config));
        quic_config.migration(self.0.migration);

        ServerConfig {
            bind_address_config: self.0.bind_address_config,
            endpoint_config: self.0.endpoint_config,
            quic_config,
        }
    }

    /// Maximum duration of inactivity to accept before timing out the connection.
    ///
    /// The true idle timeout is the minimum of this and the peer's own max idle timeout. `None`
    /// represents an infinite timeout.
    ///
    /// **WARNING**: If a peer or its network path malfunctions or acts maliciously, an infinite
    /// idle timeout can result in permanently hung futures!
    pub fn max_idle_timeout(
        mut self,
        idle_timeout: Option<Duration>,
    ) -> Result<Self, InvalidIdleTimeout> {
        let idle_timeout = idle_timeout
            .map(quinn::IdleTimeout::try_from)
            .transpose()
            .map_err(|_| InvalidIdleTimeout)?;

        self.0.transport_config.max_idle_timeout(idle_timeout);

        Ok(self)
    }

    /// Period of inactivity before sending a keep-alive packet
    ///
    /// Keep-alive packets prevent an inactive but otherwise healthy connection from timing out.
    ///
    /// `None` to disable, which is the default. Only one side of any given connection needs keep-alive
    /// enabled for the connection to be preserved. Must be set lower than the
    /// [`max_idle_timeout`](Self::max_idle_timeout) of both peers to be effective.
    pub fn keep_alive_interval(mut self, interval: Option<Duration>) -> Self {
        self.0.transport_config.keep_alive_interval(interval);
        self
    }

    /// Whether to allow clients to migrate to new addresses.
    ///
    /// Improves behavior for clients that move between different internet connections or suffer NAT
    /// rebinding. Enabled by default.
    pub fn allow_migration(mut self, value: bool) -> Self {
        self.0.migration = value;
        self
    }
}

/// Client configuration.
///
///
/// You can create an instance of `ClientConfig` using its builder pattern by calling
/// the [`builder()`](Self::builder) method.
/// Once you have an instance, you can further customize it by chaining method calls
/// to set various configuration options.
///
/// ## Configuration Builder States
///
/// The configuration process follows a *state-based builder pattern*, where the client
/// configuration progresses through *3* states.
///
/// ### 1. `WantsBindAddress`
///
/// The caller must supply a binding address for the client.
/// The following options are mutually exclusive:
///
///   - [`with_bind_default`](ClientConfigBuilder::with_bind_default): configures to use
///     the default bind address. This is generally the *default* choice for a client.
///   - [`with_bind_config`](ClientConfigBuilder::with_bind_config): configures
///     binding to an address determined by a configuration preset.
///   - [`with_bind_address`](ClientConfigBuilder::with_bind_address): configures
///     binding to a custom-specified socket address.
///   - [`with_bind_address_v6`](ClientConfigBuilder::with_bind_address_v6): configures
///     binding to a custom-specified socket address for *IPv6*, along with the [dual stack
///     configuration](Ipv6DualStackConfig).
///   - [`with_bind_socket`](ClientConfigBuilder::with_bind_socket): configures
///     binding directly to a custom-specified socket.
///
/// Only one of these options can be selected during the client configuration process.
///
/// #### Examples:
///
/// ```
/// use wtransport::ClientConfig;
///
/// ClientConfig::builder().with_bind_default();
/// ```
///
/// ### 2. `WantsRootStore`
///
/// The caller must supply a TLS root store configuration for server certificate validation.
/// The following options are mutually exclusive:
///
/// - [`with_native_certs`](ClientConfigBuilder::with_native_certs): configures to use
///   root certificates found in the platform's native certificate store. This is the *default*
///   configuration as it uses root store installed on the current machine.
/// - [`with_server_certificate_hashes`][cert_hashes]: configures the client to accept
///   *some* certificates mapped to hashes. This can be used to connect to self signed
///   certificates securely, where the hash of the certificate is shared in advance
///   through some other mechanism (such as an invite link).
/// - (**insecure**) [`with_no_cert_validation`](ClientConfigBuilder::with_no_cert_validation):
///   configure to skip server certificate validation. This might be handy for testing purpose
///   to accept *self-signed* certificate, but you should almost always prefer
///   [`with_server_certificate_hashes`][cert_hashes] for that use case.
/// - [`with_custom_tls`](ClientConfigBuilder::with_custom_tls): sets the TLS client
///   configuration manually.
/// - [`with_custom_transport`](ClientConfigBuilder::with_custom_transport): sets the QUIC
///   transport configuration manually (using default TLS).
/// - [`with_custom_tls_and_transport`](ClientConfigBuilder::with_custom_tls_and_transport): sets both
///   a custom TLS and QUIC transport configuration.
/// - [`build_with_quic_config`](ClientConfigBuilder::build_with_quic_config): directly builds
///   [`ClientConfig`] providing both TLS and QUIC transport configuration given by
///   [`quic_config`](QuicClientConfig).
///
/// Only one of these options can be selected during the client configuration process.
///
/// [cert_hashes]: ClientConfigBuilder::with_server_certificate_hashes
///
/// #### Examples:
/// ```
/// use wtransport::ClientConfig;
///
/// ClientConfig::builder()
///     .with_bind_default()
///     .with_native_certs();
/// ```
///
/// ### 3. `WantsTransportConfigClient`
///
/// The caller can supply *additional* transport configurations.
/// Multiple options can be given at this stage. Once the configuration is completed, it is possible
/// to finalize with the method [`build()`](ClientConfigBuilder::build).
///
/// All these options can be omitted in the configuration; default values will be used.
///
/// - [`max_idle_timeout`](ClientConfigBuilder::max_idle_timeout)
/// - [`keep_alive_interval`](ClientConfigBuilder::keep_alive_interval)
/// - [`dns_resolver`](ClientConfigBuilder::dns_resolver)
///
/// #### Examples:
/// ```
/// use std::time::Duration;
/// use wtransport::ClientConfig;
///
/// let client_config = ClientConfig::builder()
///     .with_bind_default()
///     .with_native_certs()
///     .max_idle_timeout(Some(Duration::from_secs(30)))
///     .unwrap()
///     .keep_alive_interval(Some(Duration::from_secs(3)))
///     .build();
/// ```
#[derive(Debug)]
pub struct ClientConfig {
    pub(crate) bind_address_config: BindAddressConfig,
    pub(crate) endpoint_config: quinn::EndpointConfig,
    pub(crate) quic_config: quinn::ClientConfig,
    pub(crate) dns_resolver: Arc<dyn DnsResolver + Send + Sync>,
}

impl ClientConfig {
    /// Creates a builder to build up the client configuration.
    ///
    /// For more information, see the [`ClientConfigBuilder`] documentation.
    pub fn builder() -> ClientConfigBuilder<states::WantsBindAddress> {
        ClientConfigBuilder::default()
    }

    /// Allows setting a custom [`DnsResolver`] for this configuration.
    ///
    /// Default resolver is [`TokioDnsResolver`].
    pub fn set_dns_resolver<R>(&mut self, dns_resolver: R)
    where
        R: DnsResolver + Send + Sync + 'static,
    {
        self.dns_resolver = Arc::new(dns_resolver);
    }

    /// Returns a reference to the inner QUIC endpoint configuration.
    #[cfg(feature = "quinn")]
    #[cfg_attr(docsrs, doc(cfg(feature = "quinn")))]
    pub fn quic_endpoint_config(&self) -> &quinn::EndpointConfig {
        &self.endpoint_config
    }

    /// Returns a mutable reference to the inner QUIC endpoint configuration.
    #[cfg(feature = "quinn")]
    #[cfg_attr(docsrs, doc(cfg(feature = "quinn")))]
    pub fn quic_endpoint_config_mut(&mut self) -> &mut quinn::EndpointConfig {
        &mut self.endpoint_config
    }

    /// Returns a reference to the inner QUIC configuration.
    #[cfg(feature = "quinn")]
    #[cfg_attr(docsrs, doc(cfg(feature = "quinn")))]
    pub fn quic_config(&self) -> &quinn::ClientConfig {
        &self.quic_config
    }

    /// Returns a mutable reference to the inner QUIC configuration.
    #[cfg(feature = "quinn")]
    #[cfg_attr(docsrs, doc(cfg(feature = "quinn")))]
    pub fn quic_config_mut(&mut self) -> &mut quinn::ClientConfig {
        &mut self.quic_config
    }
}

impl Default for ClientConfig {
    fn default() -> Self {
        ClientConfig::builder()
            .with_bind_default()
            .with_native_certs()
            .build()
    }
}

/// Client builder configuration.
///
/// The builder might have different state at compile time.
///
/// # Example
/// ```no_run
/// # use std::net::Ipv4Addr;
/// # use std::net::SocketAddr;
/// # use wtransport::ClientConfig;
/// let config = ClientConfig::builder().with_bind_default();
/// ```
#[must_use]
pub struct ClientConfigBuilder<State>(State);

impl ClientConfigBuilder<states::WantsBindAddress> {
    /// Configures for connecting binding ANY IP (allowing IP dual-stack).
    ///
    /// Bind port will be randomly picked.
    ///
    /// This is equivalent to: [`Self::with_bind_config`] with [`IpBindConfig::InAddrAnyDual`].
    pub fn with_bind_default(self) -> ClientConfigBuilder<states::WantsRootStore> {
        self.with_bind_config(IpBindConfig::InAddrAnyDual)
    }

    /// Sets the binding (local) socket address with a specific [`IpBindConfig`].
    ///
    /// Bind port will be randomly picked.
    pub fn with_bind_config(
        self,
        ip_bind_config: IpBindConfig,
    ) -> ClientConfigBuilder<states::WantsRootStore> {
        let ip_address: IpAddr = ip_bind_config.into_ip();

        match ip_address {
            IpAddr::V4(ip) => self.with_bind_address(SocketAddr::new(ip.into(), 0)),
            IpAddr::V6(ip) => self.with_bind_address_v6(
                SocketAddrV6::new(ip, 0, 0, 0),
                ip_bind_config.into_dual_stack_config(),
            ),
        }
    }

    /// Sets the binding (local) socket address for the endpoint.
    pub fn with_bind_address(
        self,
        address: SocketAddr,
    ) -> ClientConfigBuilder<states::WantsRootStore> {
        ClientConfigBuilder(states::WantsRootStore {
            bind_address_config: BindAddressConfig::from(address),
        })
    }

    /// Sets the binding (local) socket address for the endpoint.
    ///
    /// `dual_stack_config` allows/denies dual stack port binding.
    pub fn with_bind_address_v6(
        self,
        address: SocketAddrV6,
        dual_stack_config: Ipv6DualStackConfig,
    ) -> ClientConfigBuilder<states::WantsRootStore> {
        ClientConfigBuilder(states::WantsRootStore {
            bind_address_config: BindAddressConfig::AddressV6(address, dual_stack_config),
        })
    }

    /// Configures the client to bind to a pre-existing [`UdpSocket`].
    ///
    /// This allows the client to use an already created socket, which can be useful in cases
    /// where socket reuse or specific socket configurations are necessary.
    pub fn with_bind_socket(
        self,
        socket: UdpSocket,
    ) -> ClientConfigBuilder<states::WantsRootStore> {
        ClientConfigBuilder(states::WantsRootStore {
            bind_address_config: BindAddressConfig::Socket(socket),
        })
    }
}

impl ClientConfigBuilder<states::WantsRootStore> {
    /// Configures the client to use native (local) root certificates for server validation.
    ///
    /// This method loads trusted root certificates from the system's certificate store,
    /// ensuring that your client can trust certificates signed by well-known authorities.
    ///
    /// It configures safe default TLS configuration.
    pub fn with_native_certs(self) -> ClientConfigBuilder<states::WantsTransportConfigClient> {
        use crate::tls::client::build_default_tls_config;

        let tls_config = build_default_tls_config(Arc::new(build_native_cert_store()), None);
        let endpoint_config = EndpointConfig::default();
        let transport_config = TransportConfig::default();

        self.with(tls_config, endpoint_config, transport_config)
    }

    /// Configures the client to skip server certificate validation, potentially
    /// compromising security.
    ///
    /// This method is intended for advanced users and should be used with caution. It
    /// configures the client to bypass server certificate validation during the TLS
    /// handshake, effectively trusting any server certificate presented, even if it is
    /// not signed by a trusted certificate authority (CA). Using this method can expose
    /// your application to security risks.
    ///
    /// # Safety Note
    ///
    /// Using [`with_no_cert_validation`] should only be considered when you have a
    /// specific need to disable certificate validation. In most cases, it is strongly
    /// recommended to validate server certificates using trusted root certificates
    /// (e.g., [`with_native_certs`]) to ensure secure communication.
    ///
    /// However, this method can be useful in testing environments or situations where
    /// you intentionally want to skip certificate validation for specific use cases.
    ///
    /// [`with_native_certs`]: #method.with_native_certs
    /// [`with_no_cert_validation`]: #method.with_no_cert_validation
    #[cfg(feature = "dangerous-configuration")]
    #[cfg_attr(docsrs, doc(cfg(feature = "dangerous-configuration")))]
    pub fn with_no_cert_validation(
        self,
    ) -> ClientConfigBuilder<states::WantsTransportConfigClient> {
        use crate::tls::client::build_default_tls_config;
        use crate::tls::client::NoServerVerification;
        use rustls::RootCertStore;

        let tls_config = build_default_tls_config(
            Arc::new(RootCertStore::empty()),
            Some(Arc::new(NoServerVerification::new())),
        );

        let endpoint_config = EndpointConfig::default();
        let transport_config = TransportConfig::default();

        self.with(tls_config, endpoint_config, transport_config)
    }

    /// Configures the client to skip *some* server certificates validation.
    ///
    /// This method configures the client to accept server certificates
    /// whose digests match the specified *SHA-256* hashes and fulfill
    /// some additional constraints (*see notes below*).
    ///
    /// This is useful for scenarios where clients need to accept known
    /// self-signed certificates or certificates from non-standard authorities.
    ///
    /// This method configuration is similar to the
    /// [browser W3C WebTransport API](https://www.w3.org/TR/webtransport/#dom-webtransportoptions-servercertificatehashes).
    ///
    /// # Notes
    ///
    /// - The current time MUST be within the validity period of the certificate.
    /// - The total length of the validity period MUST NOT exceed *two* weeks.
    /// - Only certificates for which the public key algorithm is *ECDSA* with the *secp256r1* are accepted.
    pub fn with_server_certificate_hashes<I>(
        self,
        hashes: I,
    ) -> ClientConfigBuilder<states::WantsTransportConfigClient>
    where
        I: IntoIterator<Item = crate::tls::Sha256Digest>,
    {
        use crate::tls::client::build_default_tls_config;
        use crate::tls::client::ServerHashVerification;
        use rustls::RootCertStore;

        let tls_config = build_default_tls_config(
            Arc::new(RootCertStore::empty()),
            Some(Arc::new(ServerHashVerification::new(hashes))),
        );

        let endpoint_config = EndpointConfig::default();
        let transport_config = TransportConfig::default();

        self.with(tls_config, endpoint_config, transport_config)
    }

    /// Allows for manual configuration of a custom TLS setup using a provided
    /// [`rustls::ClientConfig`], which must support
    /// [`rustls::CipherSuite::TLS13_AES_128_GCM_SHA256`]. A suitable configuration
    /// can be obtained using the `ring` crypto provider with a set of versions containing
    /// [`rustls::version::TLS13`].
    ///
    /// This method is provided for advanced users who need fine-grained control over the
    /// TLS configuration. It allows you to pass a preconfigured [`rustls::ClientConfig`]
    /// instance to customize the TLS settings according to your specific requirements.
    ///
    /// For most use cases, it is recommended to use the [`with_native_certs`](Self::with_native_certs)
    /// method to configure TLS with safe defaults.
    pub fn with_custom_tls(
        self,
        tls_config: TlsClientConfig,
    ) -> ClientConfigBuilder<states::WantsTransportConfigClient> {
        let endpoint_config = EndpointConfig::default();
        let transport_config = TransportConfig::default();

        self.with(tls_config, endpoint_config, transport_config)
    }

    /// Similar to [`with_native_certs`](Self::with_native_certs), but it allows specifying a custom
    /// QUIC transport configuration.
    ///
    /// # Parameters
    ///
    /// - `quic_transport_config`: A custom [`QuicTransportConfig`] instance that allows you to specify
    ///   various QUIC transport-layer settings according to your requirements.
    ///
    /// # Example
    ///
    /// ```
    /// use wtransport::config::QuicTransportConfig;
    /// use wtransport::ClientConfig;
    ///
    /// // Create a custom QuicTransportConfig with specific settings
    /// let mut custom_transport_config = QuicTransportConfig::default();
    /// custom_transport_config.datagram_send_buffer_size(1024);
    ///
    /// // Create a ClientConfigBuilder with the custom transport configuration and default TLS settings
    /// let client_config = ClientConfig::builder()
    ///     .with_bind_default()
    ///     .with_custom_transport(custom_transport_config)
    ///     .build();
    /// ```
    #[cfg(feature = "quinn")]
    #[cfg_attr(docsrs, doc(cfg(feature = "quinn")))]
    pub fn with_custom_transport(
        self,
        quic_transport_config: QuicTransportConfig,
    ) -> ClientConfigBuilder<states::WantsTransportConfigClient> {
        use crate::tls::client::build_default_tls_config;

        let tls_config = build_default_tls_config(Arc::new(build_native_cert_store()), None);
        let quic_endpoint_config = EndpointConfig::default();

        self.with(tls_config, quic_endpoint_config, quic_transport_config)
    }

    /// Configures the client with both a custom TLS configuration and a custom QUIC transport
    /// configuration.
    ///
    /// This method is designed for advanced users who require full control over both the TLS
    /// and transport settings. It allows you to pass a preconfigured [`TlsClientConfig`] and
    /// a custom [`QuicTransportConfig`] to fine-tune both layers of the server configuration.
    ///
    /// # Parameters
    ///
    /// - `tls_config`: A custom [`TlsClientConfig`] instance that allows you to specify
    ///   detailed TLS settings, such as ciphersuites, certificate verification, and more. It must
    ///   support TLS 1.3 (see the documentation of [`Self::with_custom_tls`]).
    /// - `quic_transport_config`: A custom [`QuicTransportConfig`] instance that allows you to specify
    ///   various QUIC transport-layer settings according to your requirements.
    #[cfg(feature = "quinn")]
    #[cfg_attr(docsrs, doc(cfg(feature = "quinn")))]
    pub fn with_custom_tls_and_transport(
        self,
        tls_config: TlsClientConfig,
        quic_transport_config: QuicTransportConfig,
    ) -> ClientConfigBuilder<states::WantsTransportConfigClient> {
        let quic_endpoint_config = EndpointConfig::default();
        self.with(tls_config, quic_endpoint_config, quic_transport_config)
    }

    /// Directly builds [`ClientConfig`] skipping TLS and transport configuration.
    ///
    /// Both TLS and transport configuration is given by [`quic_config`](QuicClientConfig).
    #[cfg(feature = "quinn")]
    #[cfg_attr(docsrs, doc(cfg(feature = "quinn")))]
    pub fn build_with_quic_config(self, quic_config: QuicClientConfig) -> ClientConfig {
        ClientConfig {
            bind_address_config: self.0.bind_address_config,
            endpoint_config: EndpointConfig::default(),
            quic_config,
            dns_resolver: Arc::<TokioDnsResolver>::default(),
        }
    }

    fn with(
        self,
        tls_config: TlsClientConfig,
        endpoint_config: EndpointConfig,
        transport_config: TransportConfig,
    ) -> ClientConfigBuilder<states::WantsTransportConfigClient> {
        ClientConfigBuilder(states::WantsTransportConfigClient {
            bind_address_config: self.0.bind_address_config,
            tls_config,
            endpoint_config,
            transport_config,
            dns_resolver: Arc::<TokioDnsResolver>::default(),
        })
    }
}

impl ClientConfigBuilder<states::WantsTransportConfigClient> {
    /// Completes configuration process.
    ///
    /// # Panics
    ///
    /// See the documentation of [`Self::with_custom_tls`] for the TLS 1.3 requirement.
    #[must_use]
    pub fn build(self) -> ClientConfig {
        let crypto = quinn::crypto::rustls::QuicClientConfig::try_from(self.0.tls_config)
            .expect("CipherSuite::TLS13_AES_128_GCM_SHA256 missing");

        let mut quic_config = quinn::ClientConfig::new(Arc::new(crypto));
        quic_config.transport_config(Arc::new(self.0.transport_config));

        ClientConfig {
            bind_address_config: self.0.bind_address_config,
            endpoint_config: self.0.endpoint_config,
            quic_config,
            dns_resolver: self.0.dns_resolver,
        }
    }

    /// Maximum duration of inactivity to accept before timing out the connection.
    ///
    /// The true idle timeout is the minimum of this and the peer's own max idle timeout. `None`
    /// represents an infinite timeout.
    ///
    /// **WARNING**: If a peer or its network path malfunctions or acts maliciously, an infinite
    /// idle timeout can result in permanently hung futures!
    pub fn max_idle_timeout(
        mut self,
        idle_timeout: Option<Duration>,
    ) -> Result<Self, InvalidIdleTimeout> {
        let idle_timeout = idle_timeout
            .map(quinn::IdleTimeout::try_from)
            .transpose()
            .map_err(|_| InvalidIdleTimeout)?;

        self.0.transport_config.max_idle_timeout(idle_timeout);

        Ok(self)
    }

    /// Period of inactivity before sending a keep-alive packet
    ///
    /// Keep-alive packets prevent an inactive but otherwise healthy connection from timing out.
    ///
    /// `None` to disable, which is the default. Only one side of any given connection needs keep-alive
    /// enabled for the connection to be preserved. Must be set lower than the
    /// [`max_idle_timeout`](Self::max_idle_timeout) of both peers to be effective.
    pub fn keep_alive_interval(mut self, interval: Option<Duration>) -> Self {
        self.0.transport_config.keep_alive_interval(interval);
        self
    }

    /// Sets the *DNS* resolver used during [`Endpoint::connect`](crate::Endpoint::connect).
    ///
    /// Default configuration uses [`TokioDnsResolver`].
    pub fn dns_resolver<R>(mut self, dns_resolver: R) -> Self
    where
        R: DnsResolver + Send + Sync + 'static,
    {
        self.0.dns_resolver = Arc::new(dns_resolver);
        self
    }
}

impl Default for ServerConfigBuilder<states::WantsBindAddress> {
    fn default() -> Self {
        Self(states::WantsBindAddress {})
    }
}

impl Default for ClientConfigBuilder<states::WantsBindAddress> {
    fn default() -> Self {
        Self(states::WantsBindAddress {})
    }
}

#[derive(Debug)]
pub(crate) enum BindAddressConfig {
    AddressV4(SocketAddrV4),
    AddressV6(SocketAddrV6, Ipv6DualStackConfig),
    Socket(UdpSocket),
}

impl BindAddressConfig {
    pub(crate) fn bind_socket(self) -> std::io::Result<UdpSocket> {
        let (bind_address, dual_stack_config) = match self {
            BindAddressConfig::AddressV4(address) => {
                (SocketAddr::from(address), Ipv6DualStackConfig::OsDefault)
            }
            BindAddressConfig::AddressV6(address, ipv6_dual_stack_config) => {
                (SocketAddr::from(address), ipv6_dual_stack_config)
            }
            BindAddressConfig::Socket(socket) => {
                return Ok(socket);
            }
        };

        let domain = match bind_address {
            SocketAddr::V4(_) => SocketDomain::IPV4,
            SocketAddr::V6(_) => SocketDomain::IPV6,
        };

        let socket = Socket::new(domain, SocketType::DGRAM, Some(SocketProtocol::UDP))?;

        match dual_stack_config {
            Ipv6DualStackConfig::OsDefault => {}
            Ipv6DualStackConfig::Deny => socket.set_only_v6(true)?,
            Ipv6DualStackConfig::Allow => socket.set_only_v6(false)?,
        }

        socket.bind(&bind_address.into())?;

        Ok(UdpSocket::from(socket))
    }
}

impl From<SocketAddr> for BindAddressConfig {
    fn from(value: SocketAddr) -> Self {
        match value {
            SocketAddr::V4(address) => BindAddressConfig::AddressV4(address),
            SocketAddr::V6(address) => {
                BindAddressConfig::AddressV6(address, Ipv6DualStackConfig::OsDefault)
            }
        }
    }
}

/// State-types for client/server builder.
pub mod states {
    use super::*;

    /// Config builder state where the caller must supply binding address.
    pub struct WantsBindAddress {}

    /// Config builder state where the caller must supply TLS certificate.
    pub struct WantsIdentity {
        pub(super) bind_address_config: BindAddressConfig,
    }

    /// Config builder state where the caller must supply TLS root store.
    pub struct WantsRootStore {
        pub(super) bind_address_config: BindAddressConfig,
    }

    /// Config builder state where transport properties can be set.
    pub struct WantsTransportConfigServer {
        pub(super) bind_address_config: BindAddressConfig,
        pub(super) tls_config: TlsServerConfig,
        pub(super) endpoint_config: quinn::EndpointConfig,
        pub(super) transport_config: quinn::TransportConfig,
        pub(super) migration: bool,
    }

    /// Config builder state where transport properties can be set.
    pub struct WantsTransportConfigClient {
        pub(super) bind_address_config: BindAddressConfig,
        pub(super) tls_config: TlsClientConfig,
        pub(super) endpoint_config: quinn::EndpointConfig,
        pub(super) transport_config: quinn::TransportConfig,
        pub(super) dns_resolver: Arc<dyn DnsResolver + Send + Sync>,
    }
}

/// Future resolving domain name.
///
/// See [`DnsResolver::resolve`].
pub trait DnsLookupFuture: Future<Output = std::io::Result<Option<SocketAddr>>> + Send {}

impl<F> DnsLookupFuture for F where F: Future<Output = std::io::Result<Option<SocketAddr>>> + Send {}

/// A trait for asynchronously resolving domain names to IP addresses using DNS.
pub trait DnsResolver: Debug {
    /// Resolves a domain name to one IP address.
    fn resolve(&self, host: &str) -> Pin<Box<dyn DnsLookupFuture>>;
}

/// A DNS resolver implementation using the *Tokio* asynchronous runtime.
///
/// Internally, it uses [`tokio::net::lookup_host`].
#[derive(Default)]
pub struct TokioDnsResolver;

impl DnsResolver for TokioDnsResolver {
    fn resolve(&self, host: &str) -> Pin<Box<dyn DnsLookupFuture>> {
        let host = host.to_string();

        Box::pin(async move { Ok(tokio::net::lookup_host(host).await?.next()) })
    }
}

impl Debug for TokioDnsResolver {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TokioDnsResolver").finish()
    }
}

impl std::error::Error for InvalidIdleTimeout {}

impl Debug for InvalidIdleTimeout {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("idle timeout value configuration is invalid")
    }
}

impl Display for InvalidIdleTimeout {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Debug::fmt(self, f)
    }
}