chiark / gitweb /
make route_packet async
[hippotat.git] / src / bin / server.rs
index cabd18ca2f9a0454c5c4569dcdbf00901d630f62..57f9bc75caa6b813365234c4f9c8e79f85a53c80 100644 (file)
@@ -16,17 +16,19 @@ pub struct Opts {
 const METADATA_MAX_LEN: usize = MAX_OVERHEAD;
 
 #[derive(Debug)]
-struct Global {
+pub struct Global {
   config: config::InstanceConfigGlobal,
   all_clients: HashMap<ClientName, Client>,
 }
 
 #[derive(Debug)]
-struct Client {
+pub struct Client {
   ic: Arc<InstanceConfig>,
-  web: tokio::sync::mpsc::Sender<WebRequest>,
+  web: mpsc::Sender<WebRequest>,
 }
 
+pub type RoutedPacket = Box<[u8]>; // not MIME data
+
 /// Sent from hyper worker pool task to client task
 #[allow(dead_code)] // xxx
 #[derive(Debug)]
@@ -39,7 +41,7 @@ struct WebRequest {
   length_hint: usize,
   body: hyper::body::Body,
   boundary_finder: multipart::BoundaryFinder,
-  reply_to: tokio::sync::oneshot::Sender<WebResponse>,
+  reply_to: oneshot::Sender<WebResponse>,
   warnings: Warnings,
   conn: Arc<String>,
 }
@@ -55,12 +57,23 @@ struct WebResponse {
 type WebResponseData = Vec<u8>;
 
 #[throws(PacketError)]
-pub fn route_packet(conn: &str, link: &dyn Display,
-                    packet: Box<[u8]>, daddr: IpAddr)
+pub async fn route_packet(global: &Global,
+                          conn: &str, link: &(dyn Display + Sync),
+                          packet: RoutedPacket, daddr: IpAddr)
 {
-  // xxx
-  trace!("{} {} discarding packet daddr={:?} len={}",
-         conn, link, daddr, packet.len());
+  let c = &global.config;
+  let trace = |how| trace!("{} {} route {} daddr={:?} len={}",
+                           conn, link, how, daddr, packet.len());
+
+  if daddr == c.vaddr || ! c.vnetwork.iter().any(|n| n.contains(&daddr)) {
+    trace("ipif inbound xxx discarding");
+  } else if daddr == c.vrelay {
+    trace("discard (relay)");
+  } else if let Some(_client) = global.all_clients.get(&ClientName(daddr)) {
+    trace("ipif route xxx discarding");
+  } else {
+    trace("discard (no client)");
+  }
 }
 
 async fn handle(
@@ -208,7 +221,7 @@ async fn handle(
     //eprintln!("boundary={:?} start={} name={:?} client={}",
     // boundary, start, &comp.name, &client.ic);
 
-    let (reply_to, reply_recv) = tokio::sync::oneshot::channel();
+    let (reply_to, reply_recv) = oneshot::channel();
     trace!("{} {} request, Content-Length={}",
            &conn, &client_name, length_hint);
     let wreq = WebRequest {
@@ -259,12 +272,13 @@ async fn handle(
 
 #[allow(unused_variables)] // xxx
 #[allow(unused_mut)] // xxx
-async fn run_client(ic: Arc<InstanceConfig>,
+async fn run_client(global: Arc<Global>,
+                    ic: Arc<InstanceConfig>,
                     mut web: mpsc::Receiver<WebRequest>)
                     -> Result<Void, AE>
 {
   struct Outstanding {
-    reply_to: tokio::sync::oneshot::Sender<WebResponse>,
+    reply_to: oneshot::Sender<WebResponse>,
     oi: OutstandingInner,
   }
   #[derive(Debug)]
@@ -275,7 +289,7 @@ async fn run_client(ic: Arc<InstanceConfig>,
   let  downbound: VecDeque<(/*xxx*/)> = default();
 
   let try_send_response = |
-    reply_to: tokio::sync::oneshot::Sender<WebResponse>,
+    reply_to: oneshot::Sender<WebResponse>,
     response: WebResponse
   | {
     reply_to.send(response)
@@ -384,10 +398,10 @@ async fn run_client(ic: Arc<InstanceConfig>,
               let daddr = ip_packet_addr::<true>(header)?;
               Ok(daddr)
             }, |(daddr,packet)| route_packet(
-              &conn, &ic.link.client, daddr,packet
+              &global, &conn, &ic.link.client, daddr,packet
             ),
               |e| Ok::<_,SlipFramesError<_>>({ warnings.add(&e)?; })
-            )?;
+            ).await?;
           }
 
           let oi = OutstandingInner {
@@ -433,7 +447,7 @@ async fn main() {
       )).unzip::<_,_,Vec<_>,Vec<_>>();
 
     let all_clients = izip!(
-      ics.iter(),
+      &ics,
       client_handles_send,
     ).map(|(ic, web_send)| {
       (ic.link.client,
@@ -443,21 +457,22 @@ async fn main() {
        })
     }).collect();
 
+    let global = Arc::new(Global {
+      config: global_config,
+      all_clients,
+    });
+
     for (ic, web_recv) in izip!(
-      ics.into_iter(),
+      ics,
       client_handles_recv,
     ) {
+      let global_ = global.clone();
       let ic_ = ic.clone();
       tasks.push((tokio::spawn(async move {
-        run_client(ic_, web_recv).await.void_unwrap_err()
+        run_client(global_, ic_, web_recv).await.void_unwrap_err()
       }), format!("client {}", &ic)));
     }
 
-    let global = Arc::new(Global {
-      config: global_config,
-      all_clients,
-    });
-
     for addr in &global.config.addrs {
       let global_ = global.clone();
       let make_service = hyper::service::make_service_fn(