| 1 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 2 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 3 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 4 |
// option. This file may not be copied, modified, or distributed |
| 5 |
// except according to those terms. |
| 6 |
|
| 7 |
use std::{ |
| 8 |
fmt::{self, Display}, |
| 9 |
net::{SocketAddr, ToSocketAddrs as _}, |
| 10 |
path::PathBuf, |
| 11 |
time::Duration, |
| 12 |
}; |
| 13 |
|
| 14 |
use clap::Parser; |
| 15 |
use neqo_transport::{ |
| 16 |
tparams::PreferredAddress, CongestionControlAlgorithm, ConnectionParameters, StreamType, |
| 17 |
Version, |
| 18 |
}; |
| 19 |
|
| 20 |
pub mod client; |
| 21 |
mod send_data; |
| 22 |
pub mod server; |
| 23 |
pub mod udp; |
| 24 |
|
| 25 |
/// Firefox default value |
| 26 |
/// |
| 27 |
/// See `network.buffer.cache.size` pref <https://searchfox.org/mozilla-central/rev/f6e3b81aac49e602f06c204f9278da30993cdc8a/modules/libpref/init/all.js#3212> |
| 28 |
const STREAM_IO_BUFFER_SIZE: usize = 32 * 1024; |
| 29 |
|
| 30 |
#[derive(Debug, Parser)] |
| 31 |
pub struct SharedArgs { |
| 32 |
#[command(flatten)] |
| 33 |
verbose: Option<clap_verbosity_flag::Verbosity>, |
| 34 |
|
| 35 |
#[arg(short = 'a', long, default_value = "h3")] |
| 36 |
/// ALPN labels to negotiate. |
| 37 |
/// |
| 38 |
/// This client still only does HTTP/3 no matter what the ALPN says. |
| 39 |
alpn: String, |
| 40 |
|
| 41 |
#[arg(name = "qlog-dir", long, value_parser=clap::value_parser!(PathBuf))] |
| 42 |
/// Enable QLOG logging and QLOG traces to this directory |
| 43 |
qlog_dir: Option<PathBuf>, |
| 44 |
|
| 45 |
#[arg(name = "encoder-table-size", long, default_value = "16384")] |
| 46 |
max_table_size_encoder: u64, |
| 47 |
|
| 48 |
#[arg(name = "decoder-table-size", long, default_value = "16384")] |
| 49 |
max_table_size_decoder: u64, |
| 50 |
|
| 51 |
#[arg(name = "max-blocked-streams", short = 'b', long, default_value = "10")] |
| 52 |
max_blocked_streams: u16, |
| 53 |
|
| 54 |
#[arg(short = 'c', long, number_of_values = 1)] |
| 55 |
/// The set of TLS cipher suites to enable. |
| 56 |
/// From: `TLS_AES_128_GCM_SHA256`, `TLS_AES_256_GCM_SHA384`, `TLS_CHACHA20_POLY1305_SHA256`. |
| 57 |
ciphers: Vec<String>, |
| 58 |
|
| 59 |
#[arg(name = "qns-test", long)] |
| 60 |
/// Enable special behavior for use with QUIC Network Simulator |
| 61 |
qns_test: Option<String>, |
| 62 |
|
| 63 |
#[command(flatten)] |
| 64 |
quic_parameters: QuicParameters, |
| 65 |
} |
| 66 |
|
| 67 |
#[cfg(any(test, feature = "bench"))] |
| 68 |
impl Default for SharedArgs { |
| 69 |
fn default() -> Self { |
| 70 |
Self { |
| 71 |
verbose: None, |
| 72 |
alpn: "h3".into(), |
| 73 |
qlog_dir: None, |
| 74 |
max_table_size_encoder: 16384, |
| 75 |
max_table_size_decoder: 16384, |
| 76 |
max_blocked_streams: 10, |
| 77 |
ciphers: vec![], |
| 78 |
qns_test: None, |
| 79 |
quic_parameters: QuicParameters::default(), |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
#[derive(Debug, Parser)] |
| 85 |
pub struct QuicParameters { |
| 86 |
#[arg( |
| 87 |
short = 'Q', |
| 88 |
long, |
| 89 |
num_args = 1.., |
| 90 |
value_delimiter = ' ', |
| 91 |
number_of_values = 1, |
| 92 |
value_parser = from_str)] |
| 93 |
/// A list of versions to support, in hex. |
| 94 |
/// The first is the version to attempt. |
| 95 |
/// Adding multiple values adds versions in order of preference. |
| 96 |
/// If the first listed version appears in the list twice, the position |
| 97 |
/// of the second entry determines the preference order of that version. |
| 98 |
pub quic_version: Vec<Version>, |
| 99 |
|
| 100 |
#[arg(long, default_value = "16")] |
| 101 |
/// Set the `MAX_STREAMS_BIDI` limit. |
| 102 |
pub max_streams_bidi: u64, |
| 103 |
|
| 104 |
#[arg(long, default_value = "16")] |
| 105 |
/// Set the `MAX_STREAMS_UNI` limit. |
| 106 |
pub max_streams_uni: u64, |
| 107 |
|
| 108 |
#[arg(long = "idle", default_value = "30")] |
| 109 |
/// The idle timeout for connections, in seconds. |
| 110 |
pub idle_timeout: u64, |
| 111 |
|
| 112 |
#[arg(long = "cc", default_value = "cubic")] |
| 113 |
/// The congestion controller to use. |
| 114 |
pub congestion_control: CongestionControlAlgorithm, |
| 115 |
|
| 116 |
#[arg(long = "no-pacing")] |
| 117 |
/// Whether to disable pacing. |
| 118 |
pub no_pacing: bool, |
| 119 |
|
| 120 |
#[arg(long)] |
| 121 |
/// Whether to disable path MTU discovery. |
| 122 |
pub no_pmtud: bool, |
| 123 |
|
| 124 |
#[arg(long)] |
| 125 |
/// Whether to slice the SNI. |
| 126 |
pub no_sni_slicing: bool, |
| 127 |
|
| 128 |
#[arg(name = "preferred-address-v4", long)] |
| 129 |
/// An IPv4 address for the server preferred address. |
| 130 |
pub preferred_address_v4: Option<String>, |
| 131 |
|
| 132 |
#[arg(name = "preferred-address-v6", long)] |
| 133 |
/// An IPv6 address for the server preferred address. |
| 134 |
pub preferred_address_v6: Option<String>, |
| 135 |
} |
| 136 |
|
| 137 |
#[cfg(any(test, feature = "bench"))] |
| 138 |
impl Default for QuicParameters { |
| 139 |
fn default() -> Self { |
| 140 |
Self { |
| 141 |
quic_version: vec![], |
| 142 |
max_streams_bidi: 16, |
| 143 |
max_streams_uni: 16, |
| 144 |
idle_timeout: 30, |
| 145 |
congestion_control: CongestionControlAlgorithm::Cubic, |
| 146 |
no_pacing: false, |
| 147 |
no_pmtud: false, |
| 148 |
preferred_address_v4: None, |
| 149 |
preferred_address_v6: None, |
| 150 |
no_sni_slicing: false, |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
impl QuicParameters { |
| 156 |
fn get_sock_addr<F>(opt: Option<&String>, v: &str, f: F) -> Option<SocketAddr> |
| 157 |
where |
| 158 |
F: FnMut(&SocketAddr) -> bool, |
| 159 |
{ |
| 160 |
let addr = opt |
| 161 |
.iter() |
| 162 |
.filter_map(|spa| spa.to_socket_addrs().ok()) |
| 163 |
.flatten() |
| 164 |
.find(f); |
| 165 |
assert_eq!( |
| 166 |
opt.is_some(), |
| 167 |
addr.is_some(), |
| 168 |
"unable to resolve '{:?}' to an {v} address", |
| 169 |
opt.as_ref()?, |
| 170 |
); |
| 171 |
addr |
| 172 |
} |
| 173 |
|
| 174 |
#[must_use] |
| 175 |
pub fn preferred_address_v4(&self) -> Option<SocketAddr> { |
| 176 |
Self::get_sock_addr( |
| 177 |
self.preferred_address_v4.as_ref(), |
| 178 |
"IPv4", |
| 179 |
SocketAddr::is_ipv4, |
| 180 |
) |
| 181 |
} |
| 182 |
|
| 183 |
#[must_use] |
| 184 |
pub fn preferred_address_v6(&self) -> Option<SocketAddr> { |
| 185 |
Self::get_sock_addr( |
| 186 |
self.preferred_address_v6.as_ref(), |
| 187 |
"IPv6", |
| 188 |
SocketAddr::is_ipv6, |
| 189 |
) |
| 190 |
} |
| 191 |
|
| 192 |
#[must_use] |
| 193 |
pub fn preferred_address(&self) -> Option<PreferredAddress> { |
| 194 |
let v4 = self.preferred_address_v4(); |
| 195 |
let v6 = self.preferred_address_v6(); |
| 196 |
if v4.is_none() && v6.is_none() { |
| 197 |
None |
| 198 |
} else { |
| 199 |
let v4 = v4.map(|v4| { |
| 200 |
let SocketAddr::V4(v4) = v4 else { |
| 201 |
unreachable!(); |
| 202 |
}; |
| 203 |
v4 |
| 204 |
}); |
| 205 |
let v6 = v6.map(|v6| { |
| 206 |
let SocketAddr::V6(v6) = v6 else { |
| 207 |
unreachable!(); |
| 208 |
}; |
| 209 |
v6 |
| 210 |
}); |
| 211 |
Some(PreferredAddress::new(v4, v6)) |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
#[must_use] |
| 216 |
pub fn get(&self, alpn: &str) -> ConnectionParameters { |
| 217 |
let mut params = ConnectionParameters::default() |
| 218 |
.max_streams(StreamType::BiDi, self.max_streams_bidi) |
| 219 |
.max_streams(StreamType::UniDi, self.max_streams_uni) |
| 220 |
.idle_timeout(Duration::from_secs(self.idle_timeout)) |
| 221 |
.cc_algorithm(self.congestion_control) |
| 222 |
.pacing(!self.no_pacing) |
| 223 |
.pmtud(!self.no_pmtud) |
| 224 |
.sni_slicing(!self.no_sni_slicing); |
| 225 |
params = if let Some(pa) = self.preferred_address() { |
| 226 |
params.preferred_address(pa) |
| 227 |
} else { |
| 228 |
params |
| 229 |
}; |
| 230 |
if let Some(&first) = self.quic_version.first() { |
| 231 |
let all = if self.quic_version[1..].contains(&first) { |
| 232 |
&self.quic_version[1..] |
| 233 |
} else { |
| 234 |
&self.quic_version |
| 235 |
}; |
| 236 |
params.versions(first, all.to_vec()) |
| 237 |
} else { |
| 238 |
let version = match alpn { |
| 239 |
"h3" | "hq-interop" => Version::Version1, |
| 240 |
#[cfg(feature = "draft-29")] |
| 241 |
"h3-29" | "hq-29" => Version::Draft29, |
| 242 |
_ => Version::default(), |
| 243 |
}; |
| 244 |
params.versions(version, Version::all()) |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
fn from_str(s: &str) -> Result<Version, Error> { |
| 250 |
let v = u32::from_str_radix(s, 16) |
| 251 |
.map_err(|_| Error::Argument("versions need to be specified in hex"))?; |
| 252 |
Version::try_from(v).map_err(|_| Error::Argument("unknown version")) |
| 253 |
} |
| 254 |
|
| 255 |
#[derive(Debug)] |
| 256 |
pub enum Error { |
| 257 |
Argument(&'static str), |
| 258 |
} |
| 259 |
|
| 260 |
impl Display for Error { |
| 261 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 262 |
write!(f, "Error: {self:?}")?; |
| 263 |
Ok(()) |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
impl std::error::Error for Error {} |
| 268 |
|
| 269 |
#[cfg(test)] |
| 270 |
mod tests { |
| 271 |
use std::{fs, path::PathBuf, str::FromStr as _, time::SystemTime}; |
| 272 |
|
| 273 |
use crate::{client, server}; |
| 274 |
|
| 275 |
struct TempDir { |
| 276 |
path: PathBuf, |
| 277 |
} |
| 278 |
|
| 279 |
impl TempDir { |
| 280 |
fn new() -> Self { |
| 281 |
let dir = std::env::temp_dir().join(format!( |
| 282 |
"neqo-bin-test-{}", |
| 283 |
SystemTime::now() |
| 284 |
.duration_since(SystemTime::UNIX_EPOCH) |
| 285 |
.unwrap() |
| 286 |
.as_secs() |
| 287 |
)); |
| 288 |
fs::create_dir_all(&dir).unwrap(); |
| 289 |
Self { path: dir } |
| 290 |
} |
| 291 |
|
| 292 |
fn path(&self) -> PathBuf { |
| 293 |
self.path.clone() |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
impl Drop for TempDir { |
| 298 |
fn drop(&mut self) { |
| 299 |
if self.path.exists() { |
| 300 |
fs::remove_dir_all(&self.path).unwrap(); |
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
#[tokio::test] |
| 306 |
async fn write_qlog_file() { |
| 307 |
neqo_crypto::init_db(PathBuf::from_str("../test-fixture/db").unwrap()).unwrap(); |
| 308 |
|
| 309 |
let temp_dir = TempDir::new(); |
| 310 |
|
| 311 |
let mut client_args = client::Args::new(&[1], false); |
| 312 |
client_args.set_qlog_dir(temp_dir.path()); |
| 313 |
let mut server_args = server::Args::default(); |
| 314 |
server_args.set_qlog_dir(temp_dir.path()); |
| 315 |
|
| 316 |
let client = client::client(client_args); |
| 317 |
let server = Box::pin(server::server(server_args)); |
| 318 |
tokio::select! { |
| 319 |
_ = client => {} |
| 320 |
res = server => panic!("expect server not to terminate: {res:?}"), |
| 321 |
}; |
| 322 |
|
| 323 |
// Verify that the directory contains two non-empty files |
| 324 |
let entries: Vec<_> = fs::read_dir(temp_dir.path()) |
| 325 |
.unwrap() |
| 326 |
.filter_map(Result::ok) |
| 327 |
.collect(); |
| 328 |
assert_eq!(entries.len(), 2, "expect 2 files in the directory"); |
| 329 |
|
| 330 |
for entry in entries { |
| 331 |
let metadata = entry.metadata().unwrap(); |
| 332 |
assert!(metadata.is_file(), "expect a file, found something else"); |
| 333 |
assert!(metadata.len() > 0, "expect file not be empty"); |
| 334 |
} |
| 335 |
} |
| 336 |
} |