moq_uring/quic/client.rs
1//! Dialing: everything one outgoing connection needs.
2
3use std::net::SocketAddr;
4
5use super::{Connection, Error, Identity};
6use crate::udp;
7
8/// Where to dial, as whom, and who to trust.
9#[derive(Clone, Debug)]
10#[non_exhaustive]
11pub struct Config {
12 /// The address to dial.
13 pub peer: SocketAddr,
14 /// The name sent as SNI and verified against the server's certificate.
15 pub server_name: String,
16 /// ALPN protocols to offer, in preference order. Required: QUIC without an
17 /// application protocol is refused.
18 pub alpn: Vec<String>,
19 /// Verify the server's certificate against the roots below (default).
20 /// Turn it off only for tests or a pinned local deployment.
21 pub verify: bool,
22 /// PEM root certificate files to trust, on top of the system store.
23 pub roots: Vec<std::path::PathBuf>,
24 /// Trust the platform's root store as well, on by default. Turn it off to
25 /// trust only [`roots`](Self::roots), which is a real restriction here:
26 /// the trust store is built from scratch rather than added to.
27 pub system_roots: bool,
28 /// A certificate to present when the server asks for one (mTLS).
29 pub identity: Option<Identity>,
30 /// The per-connection transport settings (timeouts, stream limits,
31 /// congestion control).
32 pub transport: super::Transport,
33}
34
35impl Config {
36 /// Dial `peer`, verifying it as `server_name`, with default settings.
37 pub fn new(peer: SocketAddr, server_name: impl Into<String>) -> Self {
38 Self {
39 peer,
40 server_name: server_name.into(),
41 alpn: Vec::new(),
42 verify: true,
43 roots: Vec::new(),
44 system_roots: true,
45 identity: None,
46 transport: super::Transport::default(),
47 }
48 }
49}
50
51/// Dial [`Config::peer`] over `socket`, driving the handshake to completion.
52///
53/// Shorthand for a dial-only [`Endpoint`](super::Endpoint) and one
54/// [`connect`](super::Endpoint::connect) through it. The connection's driver
55/// runs as a task on the worker that adopted `socket`, so the returned
56/// [`Connection`] just works: hand it to `moq_net::Client::connect_lite` or
57/// use the stream API directly.
58pub async fn connect(socket: udp::Socket, config: &Config) -> Result<Connection, Error> {
59 let endpoint = super::Endpoint::new(socket, super::endpoint::Config::default())?;
60 endpoint.connect(config).await
61}