chiark / gitweb /
make route_packet infallible
[hippotat.git] / server / server.rs
index 5da413c501157e38a3158f46c359da087798fd95..667ff2e56df1bf445bfe4851790ea3449422d172 100644 (file)
@@ -26,21 +26,48 @@ pub const INTERNAL_QUEUE: usize = 15; // xxx: config
 #[derive(Debug)]
 pub struct Global {
   config: config::InstanceConfigGlobal,
+  local_rx: mpsc::Sender<RoutedPacket>,
   all_clients: HashMap<ClientName, User>,
 }
 
-pub type RoutedPacket = Box<[u8]>; // not MIME data
+pub struct RoutedPacket {
+  pub data: RoutedPacketData,
+  pub source: Option<ClientName>, // for eh, tracing, etc.
+}
+
+// not MIME data, valid SLIP (checked)
+pub type RoutedPacketData = Box<[u8]>;
+
+// loop prevention
+// we don't decrement the ttl (naughty) but loops cannot arise
+// because only the server has any routing code, and server
+// has no internal loops, so worst case is
+//  client if -> client -> server -> client' -> client if'
+// and the ifs will decrement the ttl.
+mod may_route {
+  #[derive(Clone,Debug)]
+  pub struct MayRoute(());
+  impl MayRoute {
+    pub fn came_from_outside_hippotatd() -> Self { Self(()) }
+  }
+}
+pub use may_route::MayRoute;
 
-#[throws(PacketError)]
 pub async fn route_packet(global: &Global,
-                          conn: &str, link: &(dyn Display + Sync),
-                          packet: RoutedPacket, daddr: IpAddr)
+                          transport_conn: &str, source: Option<&ClientName>,
+                          packet: RoutedPacketData, daddr: IpAddr,
+                          _may_route: MayRoute)
 {
   let c = &global.config;
   let len = packet.len();
   let trace = |how: &str, why: &str| {
     trace!("{} {} {} {} {:?} len={}",
-           conn, link, how, why, daddr, len);
+           transport_conn,
+           match source {
+             Some(s) => (s as &dyn Display),
+             None => &"local",
+           },
+           how, why, daddr, len);
   };
 
   let (dest, why) =
@@ -58,6 +85,10 @@ pub async fn route_packet(global: &Global,
     trace("discard", why); return;
   };
 
+  let packet = RoutedPacket {
+    data: packet,
+    source: source.cloned(),
+  };
   match dest.send(packet).await {
     Ok(()) => trace("forward", why),
     Err(_) => trace("task-crashed!", why),
@@ -72,7 +103,7 @@ async fn main() {
     String,
   )> = vec![];
 
-  let (global, ipif) = config::startup(
+  let global = config::startup(
     "hippotatd", LinkEnd::Server,
     &opts.config, &opts.log, |ics|
   {
@@ -105,8 +136,13 @@ async fn main() {
        })
     }).collect();
 
+    let (local_rx_send, local_tx_recv) = mpsc::channel(
+      50 // xxx configurable?
+    );
+
     let global = Arc::new(Global {
       config: global_config,
+      local_rx: local_rx_send,
       all_clients,
     });
 
@@ -148,8 +184,15 @@ async fn main() {
       });
       tasks.push((task, format!("http server {}", addr)));
     }
-    
-    Ok((global, ipif))
+
+    let global_ = global.clone();
+    let ipif = tokio::task::spawn(async move {
+      slocal::run(global_, local_tx_recv, ipif).await
+        .void_unwrap_err()
+    });
+    tasks.push((ipif, format!("ipif")));
+
+    Ok(global)
   });
 
   let died = future::select_all(
@@ -157,7 +200,5 @@ async fn main() {
   ).await;
   error!("xxx {:?}", &died);
 
-  ipif.quitting(None).await;
-
   dbg!(global);
 }