|
@@ -37,7 +37,11 @@ struct UdpConn<const MSG_SIZE: usize, const Q_SIZE: usize> {
|
|
|
/// Very simple "connection" tracking for UDP.
|
|
|
///
|
|
|
/// Tracking is purely based on the peer's IP address and source port.
|
|
|
-/// There are no other advanced TCP-like functionalities.
|
|
|
+/// There are no other advanced TCP-like functionalities or any
|
|
|
+/// safety measures against maliciously crafted datagrams.
|
|
|
+///
|
|
|
+/// The datagram consumer must be able to handle maliciously crafted
|
|
|
+/// datagrams (e.g. source address/port) without problems.
|
|
|
///
|
|
|
/// The maximum number of connections and the maximum number of packets
|
|
|
/// in the RX queue are limited.
|
|
@@ -136,6 +140,9 @@ impl<const MSG_SIZE: usize, const Q_SIZE: usize> UdpDispatcherRx<MSG_SIZE, Q_SIZ
|
|
|
}
|
|
|
|
|
|
/// Simple TX/RX dispatcher for UDP.
|
|
|
+///
|
|
|
+/// The datagram consumer must be able to handle maliciously crafted
|
|
|
+/// datagrams (e.g. source address/port) without problems.
|
|
|
#[derive(Debug)]
|
|
|
pub struct UdpDispatcher<const MSG_SIZE: usize, const Q_SIZE: usize> {
|
|
|
/// RX connection tracking.
|
|
@@ -212,7 +219,10 @@ impl<const MSG_SIZE: usize, const Q_SIZE: usize> UdpDispatcher<MSG_SIZE, Q_SIZE>
|
|
|
/// over a TCP connection.
|
|
|
#[derive(Debug)]
|
|
|
pub struct NetSocketTcp {
|
|
|
+ /// The [TcpStream] of this TCP connection.
|
|
|
stream: TcpStream,
|
|
|
+
|
|
|
+ /// Closed-flag. Note that this does *not* mean that the `stream` is closed.
|
|
|
closed: AtomicBool,
|
|
|
}
|
|
|
|
|
@@ -220,8 +230,13 @@ pub struct NetSocketTcp {
|
|
|
/// over a UDP connection.
|
|
|
#[derive(Debug)]
|
|
|
pub struct NetSocketUdp<const MSG_SIZE: usize, const Q_SIZE: usize> {
|
|
|
+ /// UDP datagram dispatcher for sending and receiving datagrams.
|
|
|
disp: Arc<UdpDispatcher<MSG_SIZE, Q_SIZE>>,
|
|
|
+
|
|
|
+ /// The peer this connection is connected to.
|
|
|
peer_addr: SocketAddr,
|
|
|
+
|
|
|
+ /// Closed-flag.
|
|
|
closed: AtomicBool,
|
|
|
}
|
|
|
|
|
@@ -256,7 +271,7 @@ impl<const MSG_SIZE: usize, const Q_SIZE: usize> NetSocket<MSG_SIZE, Q_SIZE> {
|
|
|
}
|
|
|
|
|
|
/// Send a message to the connected peer.
|
|
|
- pub async fn send(&self, buf: &[u8]) -> ah::Result<()> {
|
|
|
+ pub async fn send(&self, buf: [u8; MSG_SIZE]) -> ah::Result<()> {
|
|
|
// For good measure, check if we're not closed. But this check is racy.
|
|
|
if self.is_closed() {
|
|
|
Err(err!("Socket is closed."))
|
|
@@ -288,14 +303,7 @@ impl<const MSG_SIZE: usize, const Q_SIZE: usize> NetSocket<MSG_SIZE, Q_SIZE> {
|
|
|
}
|
|
|
Self::Udp(inner) => {
|
|
|
// Send the message via UDP.
|
|
|
- inner
|
|
|
- .disp
|
|
|
- .send_to(
|
|
|
- inner.peer_addr,
|
|
|
- buf.try_into()
|
|
|
- .context("NetSocket: Send message buffer size")?,
|
|
|
- )
|
|
|
- .await
|
|
|
+ inner.disp.send_to(inner.peer_addr, buf).await
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -346,10 +354,10 @@ impl<const MSG_SIZE: usize, const Q_SIZE: usize> NetSocket<MSG_SIZE, Q_SIZE> {
|
|
|
|
|
|
/// Close this connection.
|
|
|
///
|
|
|
- /// This only has an effect on UDP.
|
|
|
- /// This does not actually close the TCP connection.
|
|
|
- /// However, it marks both UDP and TCP as closed and no further
|
|
|
- /// TX/RX can happen.
|
|
|
+ /// This marks both UDP and TCP as closed and no further TX/RX can happen.
|
|
|
+ ///
|
|
|
+ /// Note that this does not actually close the TCP connection.
|
|
|
+ /// Dropping this object will close the TCP connection.
|
|
|
pub async fn close(&self) {
|
|
|
match self {
|
|
|
Self::Tcp(inner) => {
|