Skip to main content

tokio_quiche/quic/connection/
id.rs

1// Copyright (C) 2025, Cloudflare, Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8//     * Redistributions of source code must retain the above copyright notice,
9//       this list of conditions and the following disclaimer.
10//
11//     * Redistributions in binary form must reproduce the above copyright
12//       notice, this list of conditions and the following disclaimer in the
13//       documentation and/or other materials provided with the distribution.
14//
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
19// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27use quiche::ConnectionId;
28use std::sync::Arc;
29
30use crate::QuicResult;
31
32/// A customizable generator to derive and verify QUIC connection IDs.
33///
34/// For QUIC servers, it can be useful to encode additional information in the
35/// source connection ID. This trait allows users to implement their own logic
36/// for that purpose. The crate also provides [`SimpleConnectionIdGenerator`]
37/// if no such customization is needed.
38///
39/// Clients currently can't configure a [`ConnectionIdGenerator`] and always use
40/// the [`SimpleConnectionIdGenerator`].
41pub trait ConnectionIdGenerator<'a>: Send + Sync + 'static {
42    /// Creates a new [`ConnectionId`] according to the generator's logic.
43    fn new_connection_id(&self) -> ConnectionId<'a>;
44
45    /// Verifies whether `cid` was generated by this [`ConnectionIdGenerator`].
46    fn verify_connection_id(&self, cid: &ConnectionId) -> QuicResult<()>;
47}
48
49pub type SharedConnectionIdGenerator = Arc<dyn ConnectionIdGenerator<'static>>;
50
51/// A [`ConnectionIdGenerator`] which creates random 20-byte connection IDs.
52///
53/// Random bytes are pulled directly from the operating system to create an ID.
54/// Any `socket_cookie` value is ignored.
55#[derive(Debug, Clone, Default)]
56pub struct SimpleConnectionIdGenerator;
57
58impl ConnectionIdGenerator<'static> for SimpleConnectionIdGenerator {
59    fn new_connection_id(&self) -> ConnectionId<'static> {
60        let mut buf = vec![0; 20];
61        boring::rand::rand_bytes(&mut buf).unwrap();
62
63        ConnectionId::from_vec(buf)
64    }
65
66    /// Performs no verification, because this generator can create
67    /// any valid connection ID.
68    fn verify_connection_id(&self, _cid: &ConnectionId<'_>) -> QuicResult<()> {
69        Ok(())
70    }
71}