scion_sdk_quic_scion/socket.rs
1// Copyright 2026 Anapaya Systems
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Generic SCION UDP socket abstraction for QUIC and HTTP/3 implementations.
16
17use scion_proto::address::SocketAddr;
18
19/// Generic trait for a SCION UDP socket that can be used by the QUIC and HTTP/3 implementations.
20#[async_trait::async_trait]
21pub trait GenericScionUdpSocket: Send + Sync + 'static {
22 /// Asynchronously sends a Datagram to the specified destination address.
23 async fn send_to(
24 &self,
25 payload: &[u8],
26 destination: SocketAddr,
27 ) -> Result<(), BoxedSocketError>;
28
29 /// Asynchronously receives a Datagram, writing it into the provided buffer, and returns the
30 /// number of bytes read and the source address.
31 async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr), BoxedSocketError>;
32
33 /// Returns the local socket address of this socket.
34 fn local_addr(&self) -> SocketAddr;
35}
36
37/// Trait for errors that can occur when using a `GenericScionUdpSocket`.
38pub trait SocketError: std::error::Error + Send + Sync + 'static {}
39impl<T: std::error::Error + Send + Sync + 'static> SocketError for T {}
40
41/// Boxed error type for socket errors.
42pub type BoxedSocketError = Box<dyn SocketError>;