aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Alessandro Ghedini <alessandro@ghedini.me> 2020-12-16 13:39:16 +0000
committerGravatar Alessandro Ghedini <alessandro@ghedini.me> 2021-01-21 14:11:19 +0000
commitc4786c06a71cd6b41d9f49d6a055800e19f96a57 (patch)
tree65ce40def20b20bdd274286dbcde9f31d392e640
parent9d0c677ef1411b24d720b5c8b73bcc94b5535c29 (diff)
downloadquiche-c4786c06a71cd6b41d9f49d6a055800e19f96a57.tar.gz
quiche-c4786c06a71cd6b41d9f49d6a055800e19f96a57.tar.zst
quiche-c4786c06a71cd6b41d9f49d6a055800e19f96a57.zip
use ConnectionId in public functions
This changes the remaining public APIs to use the ConnectionId type for connection ID values, instead of slices. The FFI API is left as-is, as there isn't much point to introduce a new type there.
-rw-r--r--examples/client.rs4
-rw-r--r--examples/http3-client.rs4
-rw-r--r--examples/http3-server.rs11
-rw-r--r--examples/server.rs11
-rw-r--r--fuzz/src/packet_recv_client.rs3
-rw-r--r--fuzz/src/packet_recv_server.rs3
-rw-r--r--src/ffi.rs31
-rw-r--r--src/h3/mod.rs10
-rw-r--r--src/lib.rs77
-rw-r--r--src/packet.rs4
-rw-r--r--tools/apps/src/bin/quiche-server.rs10
-rw-r--r--tools/apps/src/client.rs4
-rw-r--r--tools/http3_test/src/runner.rs3
13 files changed, 109 insertions, 66 deletions
diff --git a/examples/client.rs b/examples/client.rs
index b019322b..28655980 100644
--- a/examples/client.rs
+++ b/examples/client.rs
@@ -101,10 +101,10 @@ fn main() {
config.set_disable_active_migration(true);
// Generate a random source connection ID for the connection.
- let mut scid = vec![0; quiche::MAX_CONN_ID_LEN];
+ let mut scid = [0; quiche::MAX_CONN_ID_LEN];
SystemRandom::new().fill(&mut scid[..]).unwrap();
- let scid = quiche::ConnectionId::from_vec(scid);
+ let scid = quiche::ConnectionId::from_ref(&scid);
// Create a QUIC connection and initiate handshake.
let mut conn = quiche::connect(url.domain(), &scid, &mut config).unwrap();
diff --git a/examples/http3-client.rs b/examples/http3-client.rs
index abd504cc..bb770100 100644
--- a/examples/http3-client.rs
+++ b/examples/http3-client.rs
@@ -102,10 +102,10 @@ fn main() {
let mut http3_conn = None;
// Generate a random source connection ID for the connection.
- let mut scid = vec![0; quiche::MAX_CONN_ID_LEN];
+ let mut scid = [0; quiche::MAX_CONN_ID_LEN];
SystemRandom::new().fill(&mut scid[..]).unwrap();
- let scid = quiche::ConnectionId::from_vec(scid);
+ let scid = quiche::ConnectionId::from_ref(&scid);
// Create a QUIC connection and initiate handshake.
let mut conn = quiche::connect(url.domain(), &scid, &mut config).unwrap();
diff --git a/examples/http3-server.rs b/examples/http3-server.rs
index 23e8eedc..cf8b7387 100644
--- a/examples/http3-server.rs
+++ b/examples/http3-server.rs
@@ -214,6 +214,8 @@ fn main() {
let mut scid = [0; quiche::MAX_CONN_ID_LEN];
scid.copy_from_slice(&conn_id);
+ let scid = quiche::ConnectionId::from_ref(&scid);
+
// Token is always present in Initial packets.
let token = hdr.token.as_ref().unwrap();
@@ -250,7 +252,7 @@ fn main() {
// The token was not valid, meaning the retry failed, so
// drop the packet.
- if odcid == None {
+ if odcid.is_none() {
error!("Invalid address validation token");
continue 'read;
}
@@ -266,7 +268,8 @@ fn main() {
debug!("New connection: dcid={:?} scid={:?}", hdr.dcid, scid);
- let conn = quiche::accept(&scid, odcid, &mut config).unwrap();
+ let conn =
+ quiche::accept(&scid, odcid.as_ref(), &mut config).unwrap();
let client = Client {
conn,
@@ -463,7 +466,7 @@ fn mint_token(hdr: &quiche::Header, src: &net::SocketAddr) -> Vec<u8> {
/// authenticate of the token. *It should not be used in production system*.
fn validate_token<'a>(
src: &net::SocketAddr, token: &'a [u8],
-) -> Option<&'a [u8]> {
+) -> Option<quiche::ConnectionId<'a>> {
if token.len() < 6 {
return None;
}
@@ -485,7 +488,7 @@ fn validate_token<'a>(
let token = &token[addr.len()..];
- Some(&token[..])
+ Some(quiche::ConnectionId::from_ref(&token[..]))
}
/// Handles incoming HTTP/3 requests.
diff --git a/examples/server.rs b/examples/server.rs
index 04d1f5fb..425d245b 100644
--- a/examples/server.rs
+++ b/examples/server.rs
@@ -206,6 +206,8 @@ fn main() {
let mut scid = [0; quiche::MAX_CONN_ID_LEN];
scid.copy_from_slice(&conn_id);
+ let scid = quiche::ConnectionId::from_ref(&scid);
+
// Token is always present in Initial packets.
let token = hdr.token.as_ref().unwrap();
@@ -242,7 +244,7 @@ fn main() {
// The token was not valid, meaning the retry failed, so
// drop the packet.
- if odcid == None {
+ if odcid.is_none() {
error!("Invalid address validation token");
continue 'read;
}
@@ -258,7 +260,8 @@ fn main() {
debug!("New connection: dcid={:?} scid={:?}", hdr.dcid, scid);
- let conn = quiche::accept(&scid, odcid, &mut config).unwrap();
+ let conn =
+ quiche::accept(&scid, odcid.as_ref(), &mut config).unwrap();
let client = Client {
conn,
@@ -406,7 +409,7 @@ fn mint_token(hdr: &quiche::Header, src: &net::SocketAddr) -> Vec<u8> {
/// authenticate of the token. *It should not be used in production system*.
fn validate_token<'a>(
src: &net::SocketAddr, token: &'a [u8],
-) -> Option<&'a [u8]> {
+) -> Option<quiche::ConnectionId<'a>> {
if token.len() < 6 {
return None;
}
@@ -428,7 +431,7 @@ fn validate_token<'a>(
let token = &token[addr.len()..];
- Some(&token[..])
+ Some(quiche::ConnectionId::from_ref(&token[..]))
}
/// Handles incoming HTTP/0.9 requests.
diff --git a/fuzz/src/packet_recv_client.rs b/fuzz/src/packet_recv_client.rs
index 34c9f045..84ea7be8 100644
--- a/fuzz/src/packet_recv_client.rs
+++ b/fuzz/src/packet_recv_client.rs
@@ -26,7 +26,8 @@ lazy_static! {
};
}
-static SCID: [u8; quiche::MAX_CONN_ID_LEN] = [0; quiche::MAX_CONN_ID_LEN];
+static SCID: quiche::ConnectionId<'static> =
+ quiche::ConnectionId::from_ref(&[0; quiche::MAX_CONN_ID_LEN]);
fuzz_target!(|data: &[u8]| {
let mut buf = data.to_vec();
diff --git a/fuzz/src/packet_recv_server.rs b/fuzz/src/packet_recv_server.rs
index 51bfc26a..671a4631 100644
--- a/fuzz/src/packet_recv_server.rs
+++ b/fuzz/src/packet_recv_server.rs
@@ -32,7 +32,8 @@ lazy_static! {
};
}
-static SCID: [u8; quiche::MAX_CONN_ID_LEN] = [0; quiche::MAX_CONN_ID_LEN];
+static SCID: quiche::ConnectionId<'static> =
+ quiche::ConnectionId::from_ref(&[0; quiche::MAX_CONN_ID_LEN]);
fuzz_target!(|data: &[u8]| {
let mut buf = data.to_vec();
diff --git a/src/ffi.rs b/src/ffi.rs
index f2e88574..4120e350 100644
--- a/src/ffi.rs
+++ b/src/ffi.rs
@@ -332,14 +332,17 @@ pub extern fn quiche_accept(
config: &mut Config,
) -> *mut Connection {
let scid = unsafe { slice::from_raw_parts(scid, scid_len) };
+ let scid = ConnectionId::from_ref(scid);
let odcid = if !odcid.is_null() && odcid_len > 0 {
- Some(unsafe { slice::from_raw_parts(odcid, odcid_len) })
+ Some(ConnectionId::from_ref(unsafe {
+ slice::from_raw_parts(odcid, odcid_len)
+ }))
} else {
None
};
- match accept(scid, odcid, config) {
+ match accept(&scid, odcid.as_ref(), config) {
Ok(c) => Box::into_raw(Pin::into_inner(c)),
Err(_) => ptr::null_mut(),
@@ -358,8 +361,9 @@ pub extern fn quiche_connect(
};
let scid = unsafe { slice::from_raw_parts(scid, scid_len) };
+ let scid = ConnectionId::from_ref(scid);
- match connect(server_name, scid, config) {
+ match connect(server_name, &scid, config) {
Ok(c) => Box::into_raw(Pin::into_inner(c)),
Err(_) => ptr::null_mut(),
@@ -372,10 +376,14 @@ pub extern fn quiche_negotiate_version(
out: *mut u8, out_len: size_t,
) -> ssize_t {
let scid = unsafe { slice::from_raw_parts(scid, scid_len) };
+ let scid = ConnectionId::from_ref(scid);
+
let dcid = unsafe { slice::from_raw_parts(dcid, dcid_len) };
+ let dcid = ConnectionId::from_ref(dcid);
+
let out = unsafe { slice::from_raw_parts_mut(out, out_len) };
- match negotiate_version(scid, dcid, out) {
+ match negotiate_version(&scid, &dcid, out) {
Ok(v) => v as ssize_t,
Err(e) => e.to_c(),
@@ -394,12 +402,18 @@ pub extern fn quiche_retry(
token_len: size_t, version: u32, out: *mut u8, out_len: size_t,
) -> ssize_t {
let scid = unsafe { slice::from_raw_parts(scid, scid_len) };
+ let scid = ConnectionId::from_ref(scid);
+
let dcid = unsafe { slice::from_raw_parts(dcid, dcid_len) };
+ let dcid = ConnectionId::from_ref(dcid);
+
let new_scid = unsafe { slice::from_raw_parts(new_scid, new_scid_len) };
+ let new_scid = ConnectionId::from_ref(new_scid);
+
let token = unsafe { slice::from_raw_parts(token, token_len) };
let out = unsafe { slice::from_raw_parts_mut(out, out_len) };
- match retry(scid, dcid, new_scid, token, version, out) {
+ match retry(&scid, &dcid, &new_scid, token, version, out) {
Ok(v) => v as ssize_t,
Err(e) => e.to_c(),
@@ -412,16 +426,19 @@ pub extern fn quiche_conn_new_with_tls(
config: &mut Config, ssl: *mut c_void, is_server: bool,
) -> *mut Connection {
let scid = unsafe { slice::from_raw_parts(scid, scid_len) };
+ let scid = ConnectionId::from_ref(scid);
let odcid = if !odcid.is_null() && odcid_len > 0 {
- Some(unsafe { slice::from_raw_parts(odcid, odcid_len) })
+ Some(ConnectionId::from_ref(unsafe {
+ slice::from_raw_parts(odcid, odcid_len)
+ }))
} else {
None
};
let tls = unsafe { tls::Handshake::from_ptr(ssl) };
- match Connection::with_tls(scid, odcid, config, tls, is_server) {
+ match Connection::with_tls(&scid, odcid.as_ref(), config, tls, is_server) {
Ok(c) => Box::into_raw(Pin::into_inner(c)),
Err(_) => ptr::null_mut(),
diff --git a/src/h3/mod.rs b/src/h3/mod.rs
index ce72ff8e..98204a90 100644
--- a/src/h3/mod.rs
+++ b/src/h3/mod.rs
@@ -59,7 +59,7 @@
//!
//! ```no_run
//! # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION).unwrap();
-//! # let scid = [0xba; 16];
+//! # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
//! # let mut conn = quiche::connect(None, &scid, &mut config).unwrap();
//! # let h3_config = quiche::h3::Config::new()?;
//! let h3_conn = quiche::h3::Connection::with_transport(&mut conn, &h3_config)?;
@@ -74,7 +74,7 @@
//!
//! ```no_run
//! # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION).unwrap();
-//! # let scid = [0xba; 16];
+//! # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
//! # let mut conn = quiche::connect(None, &scid, &mut config).unwrap();
//! # let h3_config = quiche::h3::Config::new()?;
//! # let mut h3_conn = quiche::h3::Connection::with_transport(&mut conn, &h3_config)?;
@@ -95,7 +95,7 @@
//!
//! ```no_run
//! # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION).unwrap();
-//! # let scid = [0xba; 16];
+//! # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
//! # let mut conn = quiche::connect(None, &scid, &mut config).unwrap();
//! # let h3_config = quiche::h3::Config::new()?;
//! # let mut h3_conn = quiche::h3::Connection::with_transport(&mut conn, &h3_config)?;
@@ -125,7 +125,7 @@
//! use quiche::h3::NameValue;
//!
//! # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION).unwrap();
-//! # let scid = [0xba; 16];
+//! # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
//! # let mut conn = quiche::accept(&scid, None, &mut config).unwrap();
//! # let h3_config = quiche::h3::Config::new()?;
//! # let mut h3_conn = quiche::h3::Connection::with_transport(&mut conn, &h3_config)?;
@@ -186,7 +186,7 @@
//! use quiche::h3::NameValue;
//!
//! # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION).unwrap();
-//! # let scid = [0xba; 16];
+//! # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
//! # let mut conn = quiche::connect(None, &scid, &mut config).unwrap();
//! # let h3_config = quiche::h3::Config::new()?;
//! # let mut h3_conn = quiche::h3::Connection::with_transport(&mut conn, &h3_config)?;
diff --git a/src/lib.rs b/src/lib.rs
index cb291a9d..865a4a66 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -54,7 +54,7 @@
//! ```
//! # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
//! # let server_name = "quic.tech";
-//! # let scid = [0xba; 16];
+//! # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
//! // Client connection.
//! let conn = quiche::connect(Some(&server_name), &scid, &mut config)?;
//!
@@ -72,7 +72,7 @@
//! # let mut buf = [0; 512];
//! # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
//! # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
-//! # let scid = [0xba; 16];
+//! # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
//! # let mut conn = quiche::accept(&scid, None, &mut config)?;
//! loop {
//! let read = socket.recv(&mut buf).unwrap();
@@ -103,7 +103,7 @@
//! # let mut out = [0; 512];
//! # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
//! # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
-//! # let scid = [0xba; 16];
+//! # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
//! # let mut conn = quiche::accept(&scid, None, &mut config)?;
//! loop {
//! let write = match conn.send(&mut out) {
@@ -131,7 +131,7 @@
//!
//! ```
//! # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
-//! # let scid = [0xba; 16];
+//! # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
//! # let mut conn = quiche::accept(&scid, None, &mut config)?;
//! let timeout = conn.timeout();
//! # Ok::<(), quiche::Error>(())
@@ -146,7 +146,7 @@
//! # let mut out = [0; 512];
//! # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
//! # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
-//! # let scid = [0xba; 16];
+//! # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
//! # let mut conn = quiche::accept(&scid, None, &mut config)?;
//! // Timeout expired, handle it.
//! conn.on_timeout();
@@ -181,7 +181,7 @@
//!
//! ```no_run
//! # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
-//! # let scid = [0xba; 16];
+//! # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
//! # let mut conn = quiche::accept(&scid, None, &mut config)?;
//! if conn.is_established() {
//! // Handshake completed, send some data on stream 0.
@@ -200,7 +200,7 @@
//! ```no_run
//! # let mut buf = [0; 512];
//! # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
-//! # let scid = [0xba; 16];
+//! # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
//! # let mut conn = quiche::accept(&scid, None, &mut config)?;
//! if conn.is_established() {
//! // Iterate over readable streams.
@@ -1003,13 +1003,13 @@ pub struct Connection {
///
/// ```no_run
/// # let mut config = quiche::Config::new(0xbabababa)?;
-/// # let scid = [0xba; 16];
+/// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
/// let conn = quiche::accept(&scid, None, &mut config)?;
/// # Ok::<(), quiche::Error>(())
/// ```
#[inline]
pub fn accept(
- scid: &[u8], odcid: Option<&[u8]>, config: &mut Config,
+ scid: &ConnectionId, odcid: Option<&ConnectionId>, config: &mut Config,
) -> Result<Pin<Box<Connection>>> {
let conn = Connection::new(scid, odcid, config, true)?;
@@ -1027,13 +1027,13 @@ pub fn accept(
/// ```no_run
/// # let mut config = quiche::Config::new(0xbabababa)?;
/// # let server_name = "quic.tech";
-/// # let scid = [0xba; 16];
+/// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
/// let conn = quiche::connect(Some(&server_name), &scid, &mut config)?;
/// # Ok::<(), quiche::Error>(())
/// ```
#[inline]
pub fn connect(
- server_name: Option<&str>, scid: &[u8], config: &mut Config,
+ server_name: Option<&str>, scid: &ConnectionId, config: &mut Config,
) -> Result<Pin<Box<Connection>>> {
let conn = Connection::new(scid, None, config, false)?;
@@ -1069,7 +1069,7 @@ pub fn connect(
/// ```
#[inline]
pub fn negotiate_version(
- scid: &[u8], dcid: &[u8], out: &mut [u8],
+ scid: &ConnectionId, dcid: &ConnectionId, out: &mut [u8],
) -> Result<usize> {
packet::negotiate_version(scid, dcid, out)
}
@@ -1095,12 +1095,12 @@ pub fn negotiate_version(
/// # let mut config = quiche::Config::new(0xbabababa)?;
/// # let mut buf = [0; 512];
/// # let mut out = [0; 512];
-/// # let scid = [0xba; 16];
+/// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
/// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
/// # fn mint_token(hdr: &quiche::Header, src: &std::net::SocketAddr) -> Vec<u8> {
/// # vec![]
/// # }
-/// # fn validate_token<'a>(src: &std::net::SocketAddr, token: &'a [u8]) -> Option<&'a [u8]> {
+/// # fn validate_token<'a>(src: &std::net::SocketAddr, token: &'a [u8]) -> Option<quiche::ConnectionId<'a>> {
/// # None
/// # }
/// let (len, src) = socket.recv_from(&mut buf).unwrap();
@@ -1124,18 +1124,18 @@ pub fn negotiate_version(
/// // Client sent token, validate it.
/// let odcid = validate_token(&src, token);
///
-/// if odcid == None {
+/// if odcid.is_none() {
/// // Invalid address validation token.
/// return Ok(());
/// }
///
-/// let conn = quiche::accept(&scid, odcid, &mut config)?;
+/// let conn = quiche::accept(&scid, odcid.as_ref(), &mut config)?;
/// # Ok::<(), quiche::Error>(())
/// ```
#[inline]
pub fn retry(
- scid: &[u8], dcid: &[u8], new_scid: &[u8], token: &[u8], version: u32,
- out: &mut [u8],
+ scid: &ConnectionId, dcid: &ConnectionId, new_scid: &ConnectionId,
+ token: &[u8], version: u32, out: &mut [u8],
) -> Result<usize> {
packet::retry(scid, dcid, new_scid, token, version, out)
}
@@ -1189,14 +1189,15 @@ macro_rules! qlog_with {
impl Connection {
fn new(
- scid: &[u8], odcid: Option<&[u8]>, config: &mut Config, is_server: bool,
+ scid: &ConnectionId, odcid: Option<&ConnectionId>, config: &mut Config,
+ is_server: bool,
) -> Result<Pin<Box<Connection>>> {
let tls = config.tls_ctx.lock().unwrap().new_handshake()?;
Connection::with_tls(scid, odcid, config, tls, is_server)
}
fn with_tls(
- scid: &[u8], odcid: Option<&[u8]>, config: &mut Config,
+ scid: &ConnectionId, odcid: Option<&ConnectionId>, config: &mut Config,
tls: tls::Handshake, is_server: bool,
) -> Result<Pin<Box<Connection>>> {
let max_rx_data = config.local_transport_params.initial_max_data;
@@ -1445,7 +1446,7 @@ impl Connection {
/// # let mut buf = [0; 512];
/// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
/// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
- /// # let scid = [0xba; 16];
+ /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
/// # let mut conn = quiche::accept(&scid, None, &mut config)?;
/// loop {
/// let read = socket.recv(&mut buf).unwrap();
@@ -2016,7 +2017,7 @@ impl Connection {
/// # let mut out = [0; 512];
/// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
/// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
- /// # let scid = [0xba; 16];
+ /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
/// # let mut conn = quiche::accept(&scid, None, &mut config)?;
/// loop {
/// let write = match conn.send(&mut out) {
@@ -2864,7 +2865,7 @@ impl Connection {
/// # let mut buf = [0; 512];
/// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
/// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
- /// # let scid = [0xba; 16];
+ /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
/// # let mut conn = quiche::accept(&scid, None, &mut config)?;
/// # let stream_id = 0;
/// while let Ok((read, fin)) = conn.stream_recv(stream_id, &mut buf) {
@@ -2969,7 +2970,7 @@ impl Connection {
/// # let mut buf = [0; 512];
/// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
/// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
- /// # let scid = [0xba; 16];
+ /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
/// # let mut conn = quiche::accept(&scid, None, &mut config)?;
/// # let stream_id = 0;
/// conn.stream_send(stream_id, b"hello", true)?;
@@ -3248,7 +3249,7 @@ impl Connection {
/// # let mut buf = [0; 512];
/// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
/// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
- /// # let scid = [0xba; 16];
+ /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
/// # let mut conn = quiche::accept(&scid, None, &mut config)?;
/// // Iterate over readable streams.
/// for stream_id in conn.readable() {
@@ -3282,7 +3283,7 @@ impl Connection {
/// # let mut buf = [0; 512];
/// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
/// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
- /// # let scid = [0xba; 16];
+ /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
/// # let mut conn = quiche::accept(&scid, None, &mut config)?;
/// // Iterate over writable streams.
/// for stream_id in conn.writable() {
@@ -3322,7 +3323,7 @@ impl Connection {
/// # let mut buf = [0; 512];
/// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
/// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
- /// # let scid = [0xba; 16];
+ /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
/// # let mut conn = quiche::accept(&scid, None, &mut config)?;
/// let mut dgram_buf = [0; 512];
/// while let Ok((len)) = conn.dgram_recv(&mut dgram_buf) {
@@ -3395,7 +3396,7 @@ impl Connection {
/// # let mut buf = [0; 512];
/// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
/// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
- /// # let scid = [0xba; 16];
+ /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
/// # let mut conn = quiche::accept(&scid, None, &mut config)?;
/// conn.dgram_send(b"hello")?;
/// # Ok::<(), quiche::Error>(())
@@ -3429,7 +3430,7 @@ impl Connection {
/// ```no_run
/// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
/// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
- /// # let scid = [0xba; 16];
+ /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
/// # let mut conn = quiche::accept(&scid, None, &mut config)?;
/// conn.dgram_send(b"hello")?;
/// conn.dgram_purge_outgoing(&|d: &[u8]| -> bool { d[0] == 0 });
@@ -3451,7 +3452,7 @@ impl Connection {
/// # let mut buf = [0; 512];
/// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
/// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
- /// # let scid = [0xba; 16];
+ /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
/// # let mut conn = quiche::accept(&scid, None, &mut config)?;
/// if let Some(payload_size) = conn.dgram_max_writable_len() {
/// if payload_size > 5 {
@@ -4843,9 +4844,11 @@ pub mod testing {
pub fn with_config(config: &mut Config) -> Result<Pipe> {
let mut client_scid = [0; 16];
rand::rand_bytes(&mut client_scid[..]);
+ let client_scid = ConnectionId::from_ref(&client_scid);
let mut server_scid = [0; 16];
rand::rand_bytes(&mut server_scid[..]);
+ let server_scid = ConnectionId::from_ref(&server_scid);
Ok(Pipe {
client: connect(Some("quic.tech"), &client_scid, config)?,
@@ -4856,9 +4859,11 @@ pub mod testing {
pub fn with_client_config(client_config: &mut Config) -> Result<Pipe> {
let mut client_scid = [0; 16];
rand::rand_bytes(&mut client_scid[..]);
+ let client_scid = ConnectionId::from_ref(&client_scid);
let mut server_scid = [0; 16];
rand::rand_bytes(&mut server_scid[..]);
+ let server_scid = ConnectionId::from_ref(&server_scid);
let mut config = Config::new(crate::PROTOCOL_VERSION)?;
config.load_cert_chain_from_pem_file("examples/cert.crt")?;
@@ -4879,9 +4884,11 @@ pub mod testing {
pub fn with_server_config(server_config: &mut Config) -> Result<Pipe> {
let mut client_scid = [0; 16];
rand::rand_bytes(&mut client_scid[..]);
+ let client_scid = ConnectionId::from_ref(&client_scid);
let mut server_scid = [0; 16];
rand::rand_bytes(&mut server_scid[..]);
+ let server_scid = ConnectionId::from_ref(&server_scid);
let mut config = Config::new(crate::PROTOCOL_VERSION)?;
config.set_application_protos(b"\x06proto1\x06proto2")?;
@@ -7087,10 +7094,11 @@ mod tests {
// Server sends Retry packet.
let hdr = Header::from_slice(&mut buf[..len], MAX_CONN_ID_LEN).unwrap();
- let odcid = hdr.dcid.to_vec();
+ let odcid = hdr.dcid.clone();
let mut scid = [0; MAX_CONN_ID_LEN];
rand::rand_bytes(&mut scid[..]);
+ let scid = ConnectionId::from_ref(&scid);
let token = b"quiche test retry token";
@@ -7148,6 +7156,7 @@ mod tests {
let mut scid = [0; MAX_CONN_ID_LEN];
rand::rand_bytes(&mut scid[..]);
+ let scid = ConnectionId::from_ref(&scid);
let token = b"quiche test retry token";
@@ -7203,6 +7212,7 @@ mod tests {
let mut scid = [0; MAX_CONN_ID_LEN];
rand::rand_bytes(&mut scid[..]);
+ let scid = ConnectionId::from_ref(&scid);
let token = b"quiche test retry token";
@@ -7223,7 +7233,8 @@ mod tests {
// Server accepts connection and send first flight. But original
// destination connection ID is invalid.
- pipe.server = accept(&scid, Some(b"bogus value"), &mut config).unwrap();
+ let odcid = ConnectionId::from_ref(b"bogus value");
+ pipe.server = accept(&scid, Some(&odcid), &mut config).unwrap();
len = testing::recv_send(&mut pipe.server, &mut buf, len).unwrap();
@@ -8507,9 +8518,11 @@ mod tests {
let mut client_scid = [0; 16];
rand::rand_bytes(&mut client_scid[..]);
+ let client_scid = ConnectionId::from_ref(&client_scid);
let mut server_scid = [0; 16];
rand::rand_bytes(&mut server_scid[..]);
+ let server_scid = ConnectionId::from_ref(&server_scid);
let mut client_config = Config::new(crate::PROTOCOL_VERSION).unwrap();
client_config
diff --git a/src/packet.rs b/src/packet.rs
index 3906dbb6..83a220af 100644
--- a/src/packet.rs
+++ b/src/packet.rs
@@ -140,13 +140,13 @@ enum ConnectionIdInner<'a> {
impl<'a> ConnectionId<'a> {
/// Creates a new connection ID from the given vector.
#[inline]
- pub fn from_vec(cid: Vec<u8>) -> Self {
+ pub const fn from_vec(cid: Vec<u8>) -> Self {
Self(ConnectionIdInner::Vec(cid))
}
/// Creates a new connection ID from the given slice.
#[inline]
- pub fn from_ref(cid: &'a [u8]) -> Self {
+ pub const fn from_ref(cid: &'a [u8]) -> Self {
Self(ConnectionIdInner::Ref(cid))
}
}
diff --git a/tools/apps/src/bin/quiche-server.rs b/tools/apps/src/bin/quiche-server.rs
index 3004d877..2bc3ddc9 100644
--- a/tools/apps/src/bin/quiche-server.rs
+++ b/tools/apps/src/bin/quiche-server.rs
@@ -252,6 +252,7 @@ fn main() {
if token.is_empty() {
warn!("Doing stateless retry");
+ let scid = quiche::ConnectionId::from_ref(&scid);
let new_token = mint_token(&hdr, &src);
let len = quiche::retry(
@@ -281,7 +282,7 @@ fn main() {
// The token was not valid, meaning the retry failed, so
// drop the packet.
- if odcid == None {
+ if odcid.is_none() {
error!("Invalid address validation token");
continue;
}
@@ -301,7 +302,8 @@ fn main() {
debug!("New connection: dcid={:?} scid={:?}", hdr.dcid, scid);
#[allow(unused_mut)]
- let mut conn = quiche::accept(&scid, odcid, &mut config).unwrap();
+ let mut conn =
+ quiche::accept(&scid, odcid.as_ref(), &mut config).unwrap();
if let Some(keylog) = &mut keylog {
if let Ok(keylog) = keylog.try_clone() {
@@ -525,7 +527,7 @@ fn mint_token(hdr: &quiche::Header, src: &net::SocketAddr) -> Vec<u8> {
/// authenticate of the token. *It should not be used in production system*.
fn validate_token<'a>(
src: &net::SocketAddr, token: &'a [u8],
-) -> Option<&'a [u8]> {
+) -> Option<quiche::ConnectionId<'a>> {
if token.len() < 6 {
return None;
}
@@ -547,5 +549,5 @@ fn validate_token<'a>(
let token = &token[addr.len()..];
- Some(&token[..])
+ Some(quiche::ConnectionId::from_ref(&token[..]))
}
diff --git a/tools/apps/src/client.rs b/tools/apps/src/client.rs
index 58df1944..f75af336 100644
--- a/tools/apps/src/client.rs
+++ b/tools/apps/src/client.rs
@@ -146,10 +146,10 @@ pub fn connect(
let mut app_proto_selected = false;
// Generate a random source connection ID for the connection.
- let mut scid = vec![0; quiche::MAX_CONN_ID_LEN];
+ let mut scid = [0; quiche::MAX_CONN_ID_LEN];
SystemRandom::new().fill(&mut scid[..]).unwrap();
- let scid = quiche::ConnectionId::from_vec(scid);
+ let scid = quiche::ConnectionId::from_ref(&scid);
// Create a QUIC connection and initiate handshake.
let mut conn =
diff --git a/tools/http3_test/src/runner.rs b/tools/http3_test/src/runner.rs
index 37a5dbba..62f3ef8c 100644
--- a/tools/http3_test/src/runner.rs
+++ b/tools/http3_test/src/runner.rs
@@ -110,8 +110,11 @@ pub fn run(
let mut scid = [0; quiche::MAX_CONN_ID_LEN];
SystemRandom::new().fill(&mut scid[..]).unwrap();
+ let scid = quiche::ConnectionId::from_ref(&scid);
+
// Create a QUIC connection and initiate handshake.
let url = &test.endpoint();
+
let mut conn = quiche::connect(url.domain(), &scid, &mut config).unwrap();
let write = match conn.send(&mut out) {