chiark / gitweb /
server: reorganise setup
[hippotat.git] / src / bin / server.rs
index 8446dd3b1b13c5e680a46d3cf1f223ea4df6cd45..fe161cc27f198f4710b7eb5abb222bfe4dc2122a 100644 (file)
@@ -15,11 +15,59 @@ pub struct Opts {
 
 const METADATA_MAX_LEN: usize = MAX_OVERHEAD;
 
+#[derive(Debug)]
+struct Global {
+  config: config::InstanceConfigGlobal,
+  all_clients: HashMap<ClientName, ClientHandles>,
+}
+
+#[derive(Debug)]
+struct ClientHandles {
+  ic: Arc<InstanceConfig>,
+  web: tokio::sync::mpsc::Sender<WebRequest>,
+}
+
+/// Sent from hyper worker pool task to client task
+#[allow(dead_code)] // xxx
+#[derive(Debug)]
+struct WebRequest {
+  // initial part of body
+  // used up to and including first 2 lines of metadata
+  // end delimiter for the metadata not yet located, but in here somewhere
+  initial: Box<[u8]>,
+  initial_remaining: usize,
+  length_hint: usize,
+  body: hyper::body::Body,
+  boundary_finder: multipart::BoundaryFinder,
+  reply_to: tokio::sync::oneshot::Sender<WebResponse>,
+  warnings: Warnings,
+  conn: Arc<String>,
+}
+
+/// Reply from client task to hyper worker pool task
+#[allow(dead_code)] // xxx
+#[derive(Debug)]
+struct WebResponse {
+  warnings: Warnings,
+  data: Result<WebResponseData, AE>,
+}
+
+type WebResponseData = Vec<u8>;
+
+#[throws(PacketError)]
+pub fn route_packet(conn: &str, link: &dyn Display,
+                    packet: Box<[u8]>, daddr: IpAddr)
+{
+  // xxx
+  trace!("{} {} discarding packet daddr={:?} len={}",
+         conn, link, daddr, packet.len());
+}
+
 async fn handle(
-//    context: (),
-//    addr: SocketAddr,
-    req: hyper::Request<hyper::Body>
-) -> Result<hyper::Response<hyper::Body>, Infallible> {
+  conn: Arc<String>,
+  global: Arc<Global>,
+  req: hyper::Request<hyper::Body>
+) -> Result<hyper::Response<hyper::Body>, hyper::http::Error> {
   if req.method() == Method::GET {
     let mut resp = hyper::Response::new(hyper::Body::from("hippotat\r\n"));
     resp.headers_mut().insert(
@@ -31,14 +79,19 @@ async fn handle(
 
   let mut warnings: Warnings = default();
 
-  match async {
+  async {
+
+    let get_header = |hn: &str| {
+      let mut values = req.headers().get_all(hn).iter();
+      let v = values.next().ok_or_else(|| anyhow!("missing {}", hn))?;
+      if values.next().is_some() { throw!(anyhow!("multiple {}!", hn)); }
+      let v = v.to_str().context(anyhow!("interpret {} as UTF-8", hn))?;
+      Ok::<_,AE>(v)
+    };
 
     let mkboundary = |b: &'_ _| format!("\n--{}", b).into_bytes();
     let boundary = match (||{
-      let mut ctypes = req.headers().get_all("Content-Type").iter();
-      let t = ctypes.next().ok_or_else(|| anyhow!("missing Content-Type"))?;
-      if ctypes.next().is_some() { throw!(anyhow!("several Content-Type")) }
-      let t = t.to_str().context("interpret Content-Type as utf-8")?;
+      let t = get_header("Content-Type")?;
       let t: mime::Mime = t.parse().context("parse Content-Type")?;
       if t.type_() != "multipart" { throw!(anyhow!("not multipart/")) }
       let b = mime::BOUNDARY;
@@ -56,74 +109,387 @@ async fn handle(
       },
     };
 
+    let length_hint: usize = (||{
+      let clength = get_header("Content-Length")?;
+      let clength = clength.parse().context("parse Content-Length")?;
+      Ok::<_,AE>(clength)
+    })().unwrap_or_else(
+      |e| { let _ = warnings.add(&e.wrap_err("parsing Content-Length")); 0 }
+    );
+
     let mut body = req.into_body();
-    let initial = match read_limited_bytes(METADATA_MAX_LEN, &mut body).await {
+    let initial = match read_limited_bytes(
+      METADATA_MAX_LEN, default(), length_hint, &mut body
+    ).await {
       Ok(all) => all,
       Err(ReadLimitedError::Truncated { sofar,.. }) => sofar,
       Err(ReadLimitedError::Hyper(e)) => throw!(e),
     };
 
-    let finder = memmem::Finder::new(&boundary);
-    let mut find_iter = finder.find_iter(&initial);
+    let boundary_finder = memmem::Finder::new(&boundary);
+    let mut boundary_iter = boundary_finder.find_iter(&initial);
 
     let start = if initial.starts_with(&boundary[1..]) { boundary.len()-1 }
-    else if let Some(start) = find_iter.next() { start + boundary.len() }
+    else if let Some(start) = boundary_iter.next() { start + boundary.len() }
     else { throw!(anyhow!("initial boundary not found")) };
 
-    let comp = multipart::process_component
-      (&mut warnings, &initial[start..], PartName::m)?;
+    let comp = multipart::process_boundary
+      (&mut warnings, &initial[start..], PartName::m)?
+      .ok_or_else(|| anyhow!(r#"no "m" component"#))?;
+
+    if comp.name != PartName::m { throw!(anyhow!(
+      r#"first multipart component must be name="m""#
+    )) }
 
-    eprintln!("boundary={:?} initial={:?} start={} comp={:?}",
-              boundary, initial, start, &comp);
+    let mut meta = MetadataFieldIterator::new(comp.payload);
 
-    Ok::<_,AE>(())
-  }.await {
-    Ok(()) => {
-    },
-    Err(e) => {
-      eprintln!("error={}", e);
+    let client: ClientName = meta.need_parse().context("client addr")?;
+
+    let mut hmac_got = [0; HMAC_L];
+    let (client_time, hmac_got_l) = (||{
+      let token: &str = meta.need_next().context(r#"find in "m""#)?;
+      let (time_t, hmac_b64) = token.split_once(' ')
+        .ok_or_else(|| anyhow!("split"))?;
+      let time_t = u64::from_str_radix(time_t, 16).context("parse time_t")?;
+      let l = io::copy(
+        &mut base64::read::DecoderReader::new(&mut hmac_b64.as_bytes(),
+                                              BASE64_CONFIG),
+        &mut &mut hmac_got[..]
+      ).context("parse b64 token")?;
+      let l = l.try_into()?;
+      Ok::<_,AE>((time_t, l))
+    })().context("token")?;
+    let hmac_got = &hmac_got[0..hmac_got_l];
+
+    let client_name = client;
+    let client = global.all_clients.get(&client_name);
+
+    // We attempt to hide whether the client exists we don't try to
+    // hide the hash lookup computationgs, but we do try to hide the
+    // HMAC computation by always doing it.  We hope that the compiler
+    // doesn't produce a specialised implementation for the dummy
+    // secret value.
+    let client_exists = subtle::Choice::from(client.is_some() as u8);
+    let secret = client.map(|c| c.ic.secret.0.as_bytes());
+    let secret = secret.unwrap_or(&[0x55; HMAC_B][..]);
+    let client_time_s = format!("{:x}", client_time);
+    let hmac_exp = token_hmac(secret, client_time_s.as_bytes());
+    // We also definitely want a consttime memeq for the hmac value
+    let hmac_ok = hmac_got.ct_eq(&hmac_exp);
+    //dbg!(DumpHex(&hmac_exp), client.is_some());
+    //dbg!(DumpHex(hmac_got), hmac_ok, client_exists);
+    if ! bool::from(hmac_ok & client_exists) {
+      debug!("{} rejected client {}", &conn, &client_name);
+      let body = hyper::Body::from("Not authorised\r\n");
+      return Ok(
+        hyper::Response::builder()
+          .status(hyper::StatusCode::FORBIDDEN)
+          .header("Content-Type", r#"text/plain; charset="utf-8""#)
+          .body(body)
+      )
     }
-  }
 
-  eprintln!("warnings={:?}", &warnings);
+    let client = client.unwrap();
+    let now = time_t_now();
+    let chk_skew = |a: u64, b: u64, c_ahead_behind| {
+      if let Some(a_ahead) = a.checked_sub(b) {
+        if a_ahead > client.ic.max_clock_skew.as_secs() {
+          throw!(anyhow!("too much clock skew (client {} by {})",
+                         c_ahead_behind, a_ahead));
+        }
+      }
+      Ok::<_,AE>(())
+    };
+    chk_skew(client_time, now, "ahead")?;
+    chk_skew(now, client_time, "behind")?;
+
+    let initial_remaining = meta.remaining_bytes_len();
+
+    //eprintln!("boundary={:?} start={} name={:?} client={}",
+    // boundary, start, &comp.name, &client.ic);
 
-  Ok(hyper::Response::new(hyper::Body::from("Hello World")))
+    let (reply_to, reply_recv) = tokio::sync::oneshot::channel();
+    trace!("{} {} request, Content-Length={}",
+           &conn, &client_name, length_hint);
+    let wreq = WebRequest {
+      initial,
+      initial_remaining,
+      length_hint,
+      boundary_finder: boundary_finder.into_owned(),
+      body,
+      warnings: mem::take(&mut warnings),
+      reply_to,
+      conn: conn.clone(),
+    };
+
+    client.web.try_send(wreq)
+      .map_err(|_| anyhow!("client task shut down!"))?;
+
+    let reply: WebResponse = reply_recv.await?;
+    warnings = reply.warnings;
+    let data = reply.data?;
+
+    if warnings.warnings.is_empty() {
+      trace!("{} {} responding, {}",
+             &conn, &client_name, data.len());
+    } else {
+      debug!("{} {} responding, {} warnings={:?}",
+             &conn, &client_name, data.len(),
+             &warnings.warnings);
+    }
+
+    let data = hyper::Body::from(data);
+    Ok::<_,AE>(
+      hyper::Response::builder()
+        .header("Content-Type", r#"application/octet-stream"#)
+        .body(data)
+    )
+  }.await.unwrap_or_else(|e| {
+    debug!("{} error {}", &conn, &e);
+    let mut errmsg = format!("ERROR\n\n{:?}\n\n", &e);
+    for w in warnings.warnings {
+      write!(errmsg, "warning: {}\n", w).unwrap();
+    }
+    hyper::Response::builder()
+      .status(hyper::StatusCode::BAD_REQUEST)
+      .header("Content-Type", r#"text/plain; charset="utf-8""#)
+      .body(errmsg.into())
+  })
 }
 
+#[allow(unused_variables)] // xxx
+#[allow(unused_mut)] // xxx
+async fn run_client(ic: Arc<InstanceConfig>,
+                    mut web: mpsc::Receiver<WebRequest>)
+                    -> Result<Void, AE>
+{
+  struct Outstanding {
+    reply_to: tokio::sync::oneshot::Sender<WebResponse>,
+    oi: OutstandingInner,
+  }
+  #[derive(Debug)]
+  struct OutstandingInner {
+    target_requests_outstanding: u32,
+  }
+  let mut outstanding: VecDeque<Outstanding> = default();
+  let  downbound: VecDeque<(/*xxx*/)> = default();
+
+  let try_send_response = |
+    reply_to: tokio::sync::oneshot::Sender<WebResponse>,
+    response: WebResponse
+  | {
+    reply_to.send(response)
+      .unwrap_or_else(|_: WebResponse| () /* oh dear */ /* xxx trace? */);
+  };
+
+  loop {
+    if let Some(ret) = {
+      if ! downbound.is_empty() {
+        outstanding.pop_front()
+      } else if let Some((i,_)) = outstanding.iter().enumerate().find({
+        |(_,o)| outstanding.len() > o.oi.target_requests_outstanding.sat()
+      }) {
+        Some(outstanding.remove(i).unwrap())
+      } else {
+        None
+      }
+    } {
+      let response = WebResponse {
+        data: Ok(vec![ /* xxx */ ]),
+        warnings: default(),
+      };
+
+      try_send_response(ret.reply_to, response);
+    }
+
+    select!{
+      req = web.recv() =>
+      {
+        let WebRequest {
+          initial, initial_remaining, length_hint, mut body,
+          boundary_finder,
+          reply_to, conn, mut warnings,
+        } = req.ok_or_else(|| anyhow!("webservers all shut down!"))?;
+
+        match async {
+
+          let initial_used = initial.len() - initial_remaining;
+
+          let whole_request = read_limited_bytes(
+            ic.max_batch_up.sat(),
+            initial,
+            length_hint,
+            &mut body
+          ).await.context("read request body")?;
+
+          let (meta, mut comps) =
+            multipart::ComponentIterator::resume_mid_component(
+              &whole_request[initial_used..],
+              boundary_finder
+            ).context("resume parsing body, after auth checks")?;
+
+          let mut meta = MetadataFieldIterator::new(&meta);
+
+          macro_rules! meta {
+            { $v:ident, ( $( $badcmp:tt )? ), $ret:expr,
+              let $server:ident, $client:ident $($code:tt)*
+            } => {
+              let $v = (||{
+                let $server = ic.$v;
+                let $client $($code)*
+                $(
+                  if $client $badcmp $server {
+                    throw!(anyhow!("mismatch: client={:?} {} server={:?}",
+                                   $client, stringify!($badcmp), $server));
+                  }
+                )?
+                Ok::<_,AE>($ret)
+              })().context(stringify!($v))?;
+              //dbg!(&$v);
+            }
+          }
+
+          meta!{
+            target_requests_outstanding, ( != ), client,
+            let server, client: u32 = meta.need_parse()?;
+          }
+
+          meta!{
+            http_timeout, ( > ), client,
+            let server, client = Duration::from_secs(meta.need_parse()?);
+          }
+
+          meta!{
+            mtu, ( != ), client,
+            let server, client: u32 = meta.parse()?.unwrap_or(server);
+          }
+
+          meta!{
+            max_batch_down, (), min(client, server),
+            let server, client: u32 = meta.parse()?.unwrap_or(server);
+          }
+
+          meta!{
+            max_batch_up, ( > ), client,
+            let server, client = meta.parse()?.unwrap_or(server);
+          }
+
+          while let Some(comp) = comps.next(&mut warnings, PartName::d)? {
+            if comp.name != PartName::d {
+              warnings.add(&format_args!("unexpected part {:?}", comp.name))?;
+            }
+            checkn(Mime2Slip, mtu, comp.payload, |header| {
+              let saddr = ip_packet_addr::<false>(header)?;
+              if saddr != ic.link.client.0 { throw!(PE::Src(saddr)) }
+              let daddr = ip_packet_addr::<true>(header)?;
+              Ok(daddr)
+            }, |(daddr,packet)| route_packet(
+              &conn, &ic.link.client, daddr,packet
+            ),
+              |e| Ok::<_,SlipFramesError<_>>({ warnings.add(&e)?; })
+            )?;
+          }
+
+          let oi = OutstandingInner {
+            target_requests_outstanding,
+          };
+          Ok::<_,AE>(oi)
+        }.await {
+          Ok(oi) => outstanding.push_back(Outstanding { reply_to, oi }),
+          Err(e) => {
+            try_send_response(reply_to, WebResponse {
+              data: Err(e),
+              warnings,
+            });
+          },
+        }
+      }
+    }
+  }
+  //Err(anyhow!("xxx"))
+}
 
 #[tokio::main]
 async fn main() {
   let opts = Opts::from_args();
-  let mut hservers = vec![];
-  let (ics,(global,ipif)) = config::startup(
+  let mut tasks: Vec<(
+    JoinHandle<AE>,
+    String,
+  )> = vec![];
+
+  let (global, ipif) = config::startup(
     "hippotatd", LinkEnd::Server,
     &opts.config, &opts.log, |ics|
   {
-    let global = config::InstanceConfigGlobal::from(&ics);
-    let ipif = Ipif::start(&global.ipif, None)?;
+    let global_config = config::InstanceConfigGlobal::from(&ics);
+
+    let ipif = Ipif::start(&global_config.ipif, None)?;
+
+    let (client_handles_send, client_handles_recv) = ics.iter()
+      .map(|_ic| mpsc::channel(
+        5 // xxx should me max_requests_outstanding but that's
+        // marked client-only so needs rework
+      )).unzip::<_,_,Vec<_>,Vec<_>>();
+
+    let all_clients = izip!(
+      ics.into_iter(),
+      client_handles_send,
+      client_handles_recv,
+    ).map(|(ic, web_send, web_recv)| {
+      let ic = Arc::new(ic);
 
-    let make_service = hyper::service::make_service_fn(|_conn| async {
-      Ok::<_, Infallible>(hyper::service::service_fn(handle))
+      let ic_ = ic.clone();
+      tasks.push((tokio::spawn(async move {
+        run_client(ic_, web_recv).await.void_unwrap_err()
+      }), format!("client {}", &ic)));
+
+      (ic.link.client,
+       ClientHandles {
+         ic,
+         web: web_send,
+       })
+    }).collect();
+    let global = Arc::new(Global {
+      config: global_config,
+      all_clients,
     });
 
-    for addr in &global.addrs {
-      let addr = SocketAddr::new(*addr, global.port);
+    for addr in &global.config.addrs {
+      let global_ = global.clone();
+      let make_service = hyper::service::make_service_fn(
+        move |conn: &hyper::server::conn::AddrStream| {
+          let global_ = global_.clone();
+          let conn = Arc::new(format!("[{}]", conn.remote_addr()));
+          async { Ok::<_, Void>( hyper::service::service_fn(move |req| {
+            handle(conn.clone(), global_.clone(), req)
+          }) ) }
+        }
+      );
+
+      let addr = SocketAddr::new(*addr, global.config.port);
       let server = hyper::Server::try_bind(&addr)
         .context("bind")?
         .http1_preserve_header_case(true)
         .serve(make_service);
       info!("listening on {}", &addr);
-      let task = tokio::task::spawn(server);
-      hservers.push(task);
+      let task = tokio::task::spawn(async move {
+        match server.await {
+          Ok(()) => anyhow!("shut down?!"),
+          Err(e) => e.into(),
+        }
+      });
+      tasks.push((task, format!("http server {}", addr)));
     }
-
+    
     Ok((global, ipif))
   });
 
-  let x = future::select_all(&mut hservers).await;
-  error!("xxx hserver {:?}", &x);
+  let died = future::select_all(
+    tasks.iter_mut().map(|e| &mut e.0)
+  ).await;
+  error!("xxx {:?}", &died);
 
   ipif.quitting(None).await;
 
-  dbg!(ics, global);
+  dbg!(global);
 }