1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
use quiche;
use std::io;
use std::net::SocketAddr;
use std::net::UdpSocket as StdUdpSocket;
use tokio;
/// A QUIC socket that has not yet been converted to a `QuicListener`.
///
/// `QuicSocket` wraps an underlying operating system UDP socket and enables the caller to
/// configure the socket before establishing a QUIC connection or accepting
/// inbound connections. The caller is able to set socket option and explicitly
/// bind the socket with a socket address.
///
/// The underlying socket is closed when the `UdpSocket` value is dropped.
///
/// `UdpSocket` should only be used directly if the default configuration used
/// by `QuicListener::bind` does not meet the required use case.
pub struct QuicSocket {
inner: tokio::net::UdpSocket,
addr: SocketAddr,
}
pub struct QuicListener {
socket: QuicSocket,
connection: std::pin::Pin<std::boxed::Box<quiche::Connection>>,
}
impl QuicSocket {
/// Create a new underlying UDP socket and attempts to bind it to the addr provided
///
/// # Examples
///
///
/// ```no_run
/// use quic::QuicSocket;
///
/// #[tokio::main]
/// async fn main() -> io::Result<()> {
/// let addr = "127.0.0.1:8080".parse().unwrap();
///
///
/// let socket = QuickSocket::bind(addr);
/// # drop(socket);
///
/// Ok(())
/// }
/// ```
pub async fn bind(addr: SocketAddr) -> io::Result<QuicSocket> {
match tokio::net::UdpSocket::bind(addr).await {
Ok(inner) => Ok(QuicSocket { inner, addr: addr }),
Err(_) => Err(io::Error::new(
io::ErrorKind::InvalidInput,
"could not resolve to any address",
)),
}
}
/// Creates new `QuicSocket` from a previously bound `std::net::UdpSocket`.
///
/// The conversion assumes nothing about the underlying socket; it is left up to the user to set it in
/// non-blocking mode.
///
///
/// # Example
///
/// ```no_run
/// use quic::QuicSocket;
/// # use std::{net::SocketAddr};
///
/// # #[tokio::main]
/// # async fn main() -> io::Result<()> {
/// let addr = "0.0.0.0:8080".parse::<SocketAddr>().unwrap();
/// let std_sock = std::net::UdpSocket::bind(addr)?;
/// std_sock.set_nonblocking(true)?;
/// let sock = QuicSocket::from_std(std_sock)?;
/// // use `sock`
/// # Ok(())
/// # }
/// ```
pub fn from_std(socket: StdUdpSocket) -> io::Result<QuicSocket> {
let inner = match tokio::net::UdpSocket::from_std(socket) {
Ok(inner) => inner,
Err(_) => {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"could not resolve from std socket",
))
}
};
let addr = match inner.local_addr() {
Ok(addr) => addr,
Err(_) => {
return Err(io::Error::new(
io::ErrorKind::NotFound,
"could not resolve to local address",
))
}
};
Ok(QuicSocket { inner, addr: addr })
}
/// Accept a QUIC connection from a peer at the specified socket address.
///
/// The `QuicSocket` is consumed. Once the connection is established, a
/// connected [`QuicListener`] is returned. If the connection fails, the
/// encountered error is returned.
///
/// # Examples
///
/// Connecting to a peer.
///
/// ```no_run
/// use quic::QuicSocket;
///
/// use std::io;
///
/// #[tokio::main]
/// async fn main() -> io::Result<()> {
/// let addr = "127.0.0.1:8080".parse().unwrap();
///
/// let socket = QuicSocket::bind(addr).await?;
/// let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
/// let listener = socket.accept(addr, config)?;
/// # drop(listener);
///
/// Ok(())
/// }
/// ```
pub fn accept(self, addr: SocketAddr, mut config: quiche::Config) -> io::Result<QuicListener> {
let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
let connection = match quiche::accept(&scid, None, addr, &mut config) {
Ok(conn) => conn,
Err(_) => {
return Err(io::Error::new(
io::ErrorKind::ConnectionRefused,
"could not connect",
))
}
};
Ok(QuicListener {
socket: self,
connection,
})
}
/// Establish a QUIC connection with a peer at the specified socket address.
///
/// The `QuicSocket` is consumed. Once the connection is established, a
/// connected [`QuicListener`] is returned. If the connection fails, the
/// encountered error is returned.
///
/// # Examples
///
/// Connecting to a peer.
///
/// ```no_run
/// use quic::QuicSocket;
///
/// use std::io;
///
/// #[tokio::main]
/// async fn main() -> io::Result<()> {
/// let addr = "127.0.0.1:8080".parse().unwrap();
///
/// let socket = QuicSocket::bind(addr).await?;
/// let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
/// let listener = socket.connect(addr, config)?;
/// # drop(listener);
///
/// Ok(())
/// }
/// ```
pub fn connect(self, addr: SocketAddr, mut config: quiche::Config) -> io::Result<QuicListener> {
let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
let connection = match quiche::connect(None, &scid, addr, &mut config) {
Ok(conn) => conn,
Err(_) => {
return Err(io::Error::new(
io::ErrorKind::ConnectionRefused,
"could not connect",
))
}
};
Ok(QuicListener {
socket: self,
connection,
})
}
}
impl QuicListener {
/// Uses the underlying quiche Connection [send](https://docs.rs/quiche/0.10.0/quiche/struct.Connection.html#method.send)
/// method in order to write a singular QUIC packet to send to the peer.
///
/// # Examples
///
/// ```no_run
/// loop {
/// let read = match listener.send_once(&mut out).await {
/// Ok(v) => v,
/// Err(std::io::Other) => {
/// // done writing
/// break;
/// }
/// Err(_) => {
/// // handle error
/// break;
/// }
/// }
/// }
/// ```
pub async fn send(&mut self, out: &mut [u8]) -> Result<usize, io::Error> {
let (write, info) = match self.connection.send(out) {
Ok(v) => v,
Err(quiche::Error::Done) => {
return Err(io::Error::new(io::ErrorKind::Other, "done writing"))
}
Err(_) => return Err(io::Error::new(io::ErrorKind::Other, "could not send")),
};
self.send_to(&mut out[..write], &info).await
}
/// Wrapper around the underlying socket to send to peer.
async fn send_to(
&self,
out: &mut [u8],
info: &quiche::SendInfo,
) -> Result<usize, std::io::Error> {
self.socket.inner.send_to(out, info.to).await
}
/// Uses the underlying quiche Connection [recv](https://docs.rs/quiche/0.10.0/quiche/struct.Connection.html#method.recv)
/// method in order to process QUIC packets received from the peer.
///
/// # Examples:
///
/// ```no_run
/// loop {
/// let read = match listener.recv(&mut buf).unwrap().await {
/// Ok(v) => v,
/// Err(e) => {
/// // handle error
/// break;
/// }
/// };
/// }
/// ```
pub async fn recv(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
let (read, from) = match self.recv_from(buf).await {
Ok(v) => v,
Err(_) => {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"could not receive",
))
}
};
let info = self.recv_info(from);
match self.connection.recv(&mut buf[..read], info) {
Ok(v) => Ok(v),
Err(_) => Err(io::Error::new(
io::ErrorKind::InvalidData,
"could not receive",
)),
}
}
/// Wrapper around the underlying socket to receive from peer.
async fn recv_from(
&self,
buf: &mut [u8],
) -> Result<(usize, std::net::SocketAddr), std::io::Error> {
self.socket.inner.recv_from(buf).await
}
/// Wrapper around quiche::RecvInfo to convert SocketAddr to
/// quiche::RecvInfo
fn recv_info(&self, from: SocketAddr) -> quiche::RecvInfo {
quiche::RecvInfo { from }
}
}