2 Achegas 56641a30ed ... 796023d3cd

Autor SHA1 Mensaxe Data
  Michael Buesch 796023d3cd Add support for UDP control port hai 2 semanas
  Michael Buesch 56641a30ed Add support for UDP control port hai 2 semanas
Modificáronse 3 ficheiros con 37 adicións e 17 borrados
  1. 1 1
      letmein-proto/src/lib.rs
  2. 22 14
      letmein-proto/src/socket.rs
  3. 14 2
      letmein/src/main.rs

+ 1 - 1
letmein-proto/src/lib.rs

@@ -393,7 +393,7 @@ impl Message {
     /// Send this message over a [MsgNetSocket].
     pub async fn send(&self, sock: &MsgNetSocket) -> ah::Result<()> {
         let buf = self.msg_serialize()?;
-        sock.send(&buf).await?;
+        sock.send(buf).await?;
         if DEBUG {
             println!("TX: {self:?} {buf:?}");
         }

+ 22 - 14
letmein-proto/src/socket.rs

@@ -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) => {

+ 14 - 2
letmein/src/main.rs

@@ -137,11 +137,23 @@ enum Command {
         #[arg(short = 'P', long)]
         server_port: Option<u16>,
 
-        /// TODO
+        /// Enforce TCP connection to letmein server port.
+        ///
+        /// You normally don't have to use this option.
+        ///
+        /// If not given, then the `[GENERAL] port` from the
+        /// letmein.conf configuration file will be used instead.
+        /// TCP will be preferred, if both TCP and UDP are specified.
         #[arg(short = 'T', long)]
         server_port_tcp: bool,
 
-        /// TODO
+        /// Enforce UDP connection to letmein server port.
+        ///
+        /// You normally don't have to use this option.
+        ///
+        /// If not given, then the `[GENERAL] port` from the
+        /// letmein.conf configuration file will be used instead.
+        /// TCP will be preferred, if both TCP and UDP are specified.
         #[arg(short = 'U', long)]
         server_port_udp: bool,