|
| 1 | +// Copyright 2017-2020 Parity Technologies (UK) Ltd. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a |
| 4 | +// copy of this software and associated documentation files (the "Software"), |
| 5 | +// to deal in the Software without restriction, including without limitation |
| 6 | +// the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 7 | +// and/or sell copies of the Software, and to permit persons to whom the |
| 8 | +// Software is furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 14 | +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 18 | +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 19 | +// DEALINGS IN THE SOFTWARE. |
| 20 | + |
| 21 | +use quinn::VarInt; |
| 22 | +use std::{sync::Arc, time::Duration}; |
| 23 | + |
| 24 | +/// Config for the transport. |
| 25 | +#[derive(Clone)] |
| 26 | +pub struct Config { |
| 27 | + /// Timeout for the initial handshake when establishing a connection. |
| 28 | + /// The actual timeout is the minimum of this and the [`Config::max_idle_timeout`]. |
| 29 | + pub handshake_timeout: Duration, |
| 30 | + /// Maximum duration of inactivity in ms to accept before timing out the connection. |
| 31 | + pub max_idle_timeout: u32, |
| 32 | + /// Period of inactivity before sending a keep-alive packet. |
| 33 | + /// Must be set lower than the idle_timeout of both |
| 34 | + /// peers to be effective. |
| 35 | + /// |
| 36 | + /// See [`quinn::TransportConfig::keep_alive_interval`] for more |
| 37 | + /// info. |
| 38 | + pub keep_alive_interval: Duration, |
| 39 | + /// Maximum number of incoming bidirectional streams that may be open |
| 40 | + /// concurrently by the remote peer. |
| 41 | + pub max_concurrent_stream_limit: u32, |
| 42 | + |
| 43 | + /// Max unacknowledged data in bytes that may be send on a single stream. |
| 44 | + pub max_stream_data: u32, |
| 45 | + |
| 46 | + /// Max unacknowledged data in bytes that may be send in total on all streams |
| 47 | + /// of a connection. |
| 48 | + pub max_connection_data: u32, |
| 49 | + |
| 50 | + /// Support QUIC version draft-29 for dialing and listening. |
| 51 | + /// |
| 52 | + /// Per default only QUIC Version 1 / [`libp2p_core::multiaddr::Protocol::QuicV1`] |
| 53 | + /// is supported. |
| 54 | + /// |
| 55 | + /// If support for draft-29 is enabled servers support draft-29 and version 1 on all |
| 56 | + /// QUIC listening addresses. |
| 57 | + /// As client the version is chosen based on the remote's address. |
| 58 | + pub support_draft_29: bool, |
| 59 | + |
| 60 | + /// TLS client config for the inner [`quinn::ClientConfig`]. |
| 61 | + client_tls_config: Arc<rustls::ClientConfig>, |
| 62 | + /// TLS server config for the inner [`quinn::ServerConfig`]. |
| 63 | + server_tls_config: Arc<rustls::ServerConfig>, |
| 64 | +} |
| 65 | + |
| 66 | +impl Config { |
| 67 | + /// Creates a new configuration object with default values. |
| 68 | + pub fn new(keypair: &libp2p_identity::Keypair) -> Self { |
| 69 | + let client_tls_config = Arc::new(libp2p_tls::make_client_config(keypair, None).unwrap()); |
| 70 | + let server_tls_config = Arc::new(libp2p_tls::make_server_config(keypair).unwrap()); |
| 71 | + Self { |
| 72 | + client_tls_config, |
| 73 | + server_tls_config, |
| 74 | + support_draft_29: false, |
| 75 | + handshake_timeout: Duration::from_secs(5), |
| 76 | + max_idle_timeout: 30 * 1000, |
| 77 | + max_concurrent_stream_limit: 256, |
| 78 | + keep_alive_interval: Duration::from_secs(15), |
| 79 | + max_connection_data: 15_000_000, |
| 80 | + |
| 81 | + // Ensure that one stream is not consuming the whole connection. |
| 82 | + max_stream_data: 10_000_000, |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/// Represents the inner configuration for [`quinn`]. |
| 88 | +#[derive(Debug, Clone)] |
| 89 | +pub(crate) struct QuinnConfig { |
| 90 | + pub(crate) client_config: quinn::ClientConfig, |
| 91 | + pub(crate) server_config: quinn::ServerConfig, |
| 92 | + pub(crate) endpoint_config: quinn::EndpointConfig, |
| 93 | +} |
| 94 | + |
| 95 | +impl From<Config> for QuinnConfig { |
| 96 | + fn from(config: Config) -> QuinnConfig { |
| 97 | + let Config { |
| 98 | + client_tls_config, |
| 99 | + server_tls_config, |
| 100 | + max_idle_timeout, |
| 101 | + max_concurrent_stream_limit, |
| 102 | + keep_alive_interval, |
| 103 | + max_connection_data, |
| 104 | + max_stream_data, |
| 105 | + support_draft_29, |
| 106 | + handshake_timeout: _, |
| 107 | + } = config; |
| 108 | + let mut transport = quinn::TransportConfig::default(); |
| 109 | + // Disable uni-directional streams. |
| 110 | + transport.max_concurrent_uni_streams(0u32.into()); |
| 111 | + transport.max_concurrent_bidi_streams(max_concurrent_stream_limit.into()); |
| 112 | + // Disable datagrams. |
| 113 | + transport.datagram_receive_buffer_size(None); |
| 114 | + transport.keep_alive_interval(Some(keep_alive_interval)); |
| 115 | + transport.max_idle_timeout(Some(VarInt::from_u32(max_idle_timeout).into())); |
| 116 | + transport.allow_spin(false); |
| 117 | + transport.stream_receive_window(max_stream_data.into()); |
| 118 | + transport.receive_window(max_connection_data.into()); |
| 119 | + let transport = Arc::new(transport); |
| 120 | + |
| 121 | + let mut server_config = quinn::ServerConfig::with_crypto(server_tls_config); |
| 122 | + server_config.transport = Arc::clone(&transport); |
| 123 | + // Disables connection migration. |
| 124 | + // Long-term this should be enabled, however we then need to handle address change |
| 125 | + // on connections in the `Connection`. |
| 126 | + server_config.migration(false); |
| 127 | + |
| 128 | + let mut client_config = quinn::ClientConfig::new(client_tls_config); |
| 129 | + client_config.transport_config(transport); |
| 130 | + |
| 131 | + let mut endpoint_config = quinn::EndpointConfig::default(); |
| 132 | + if !support_draft_29 { |
| 133 | + endpoint_config.supported_versions(vec![1]); |
| 134 | + } |
| 135 | + |
| 136 | + QuinnConfig { |
| 137 | + client_config, |
| 138 | + server_config, |
| 139 | + endpoint_config, |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments