1 // Copyright 2021 Ian Jackson and contributors to Hippotat
2 // SPDX-License-Identifier: GPL-3.0-or-later
3 // There is NO WARRANTY.
5 use hippotat::prelude::*;
7 #[derive(StructOpt,Debug)]
16 const METADATA_MAX_LEN: usize = MAX_OVERHEAD;
18 type AllClients = HashMap<ClientName, ClientHandles>;
20 struct ClientHandles {
21 ic: Arc<InstanceConfig>,
22 web: tokio::sync::mpsc::Sender<WebRequest>,
25 /// Sent from hyper worker pool task to client task
26 #[allow(dead_code)] // xxx
29 // initial part of body
30 // used up to and including first 2 lines of metadata
31 // end delimiter for the metadata not yet located, but in here somewhere
33 initial_remaining: usize,
35 body: hyper::body::Body,
36 boundary_finder: multipart::BoundaryFinder,
37 reply_to: tokio::sync::oneshot::Sender<WebResponse>,
41 /// Reply from client task to hyper worker pool task
42 #[allow(dead_code)] // xxx
46 data: Result<WebResponseData, AE>,
49 type WebResponseData = ();
51 #[throws(PacketError)]
52 pub fn route_packet(packet: Box<[u8]>, daddr: IpAddr) {
53 trace!("xxx discarding packet daddr={:?} len={}", daddr, packet.len());
57 all_clients: Arc<AllClients>,
58 req: hyper::Request<hyper::Body>
59 ) -> Result<hyper::Response<hyper::Body>, Void> {
60 if req.method() == Method::GET {
61 let mut resp = hyper::Response::new(hyper::Body::from("hippotat\r\n"));
62 resp.headers_mut().insert(
64 "text/plain; charset=US-ASCII".try_into().unwrap()
69 let mut warnings: Warnings = default();
73 let get_header = |hn: &str| {
74 let mut values = req.headers().get_all(hn).iter();
75 let v = values.next().ok_or_else(|| anyhow!("missing {}", hn))?;
76 if values.next().is_some() { throw!(anyhow!("multiple {}!", hn)); }
77 let v = v.to_str().context(anyhow!("interpret {} as UTF-8", hn))?;
81 let mkboundary = |b: &'_ _| format!("\n--{}", b).into_bytes();
82 let boundary = match (||{
83 let t = get_header("Content-Type")?;
84 let t: mime::Mime = t.parse().context("parse Content-Type")?;
85 if t.type_() != "multipart" { throw!(anyhow!("not multipart/")) }
86 let b = mime::BOUNDARY;
87 let b = t.get_param(b).ok_or_else(|| anyhow!("missing boundary=..."))?;
88 if t.subtype() != "form-data" {
89 warnings.add(&"Content-Type not /form-data")?;
91 let b = mkboundary(b.as_str());
96 warnings.add(&e.wrap_err("guessing boundary"))?;
101 let length_hint: usize = (||{
102 let clength = get_header("Content-Length")?;
103 let clength = clength.parse().context("parse Content-Length")?;
106 |e| { let _ = warnings.add(&e.wrap_err("parsing Content-Length")); 0 }
109 let mut body = req.into_body();
110 let initial = match read_limited_bytes(
111 METADATA_MAX_LEN, default(), length_hint, &mut body
114 Err(ReadLimitedError::Truncated { sofar,.. }) => sofar,
115 Err(ReadLimitedError::Hyper(e)) => throw!(e),
118 let boundary_finder = memmem::Finder::new(&boundary);
119 let mut boundary_iter = boundary_finder.find_iter(&initial);
121 let start = if initial.starts_with(&boundary[1..]) { boundary.len()-1 }
122 else if let Some(start) = boundary_iter.next() { start + boundary.len() }
123 else { throw!(anyhow!("initial boundary not found")) };
125 let comp = multipart::process_boundary
126 (&mut warnings, &initial[start..], PartName::m)?
127 .ok_or_else(|| anyhow!(r#"no "m" component"#))?;
129 if comp.name != PartName::m { throw!(anyhow!(
130 r#"first multipart component must be name="m""#
133 let mut meta = MetadataFieldIterator::new(comp.payload);
135 let client: ClientName = meta.need_parse().context("client addr")?;
137 let mut hmac_got = [0; HMAC_L];
138 let (client_time, hmac_got_l) = (||{
139 let token: &str = meta.need_next().context(r#"find in "m""#)?;
140 let (time_t, hmac_b64) = token.split_once(' ')
141 .ok_or_else(|| anyhow!("split"))?;
142 let time_t = u64::from_str_radix(time_t, 16).context("parse time_t")?;
144 &mut base64::read::DecoderReader::new(&mut hmac_b64.as_bytes(),
146 &mut &mut hmac_got[..]
147 ).context("parse b64 token")?;
148 let l = l.try_into()?;
149 Ok::<_,AE>((time_t, l))
150 })().context("token")?;
151 let hmac_got = &hmac_got[0..hmac_got_l];
153 let client = all_clients.get(&client);
155 // We attempt to hide whether the client exists we don't try to
156 // hide the hash lookup computationgs, but we do try to hide the
157 // HMAC computation by always doing it. We hope that the compiler
158 // doesn't produce a specialised implementation for the dummy
160 let client_exists = subtle::Choice::from(client.is_some() as u8);
161 let secret = client.map(|c| c.ic.secret.0.as_bytes());
162 let secret = secret.unwrap_or(&[0x55; HMAC_B][..]);
163 let client_time_s = format!("{:x}", client_time);
164 let hmac_exp = token_hmac(secret, client_time_s.as_bytes());
165 // We also definitely want a consttime memeq for the hmac value
166 let hmac_ok = hmac_got.ct_eq(&hmac_exp);
167 //dbg!(DumpHex(&hmac_exp), client.is_some());
168 //dbg!(DumpHex(hmac_got), hmac_ok, client_exists);
169 if ! bool::from(hmac_ok & client_exists) {
170 throw!(anyhow!("xxx should be a 403 error"));
173 let client = client.unwrap();
174 let now = time_t_now();
175 let chk_skew = |a: u64, b: u64, c_ahead_behind| {
176 if let Some(a_ahead) = a.checked_sub(b) {
177 if a_ahead > client.ic.max_clock_skew.as_secs() {
178 throw!(anyhow!("too much clock skew (client {} by {})",
179 c_ahead_behind, a_ahead));
184 chk_skew(client_time, now, "ahead")?;
185 chk_skew(now, client_time, "behind")?;
187 let initial_remaining = meta.remaining_bytes_len();
189 //eprintln!("boundary={:?} start={} name={:?} client={}",
190 // boundary, start, &comp.name, &client.ic);
192 let (reply_to, reply_recv) = tokio::sync::oneshot::channel();
193 trace!("{} request xxx={}", &client.ic, initial.len());
194 let wreq = WebRequest {
198 boundary_finder: boundary_finder.into_owned(),
200 warnings: mem::take(&mut warnings),
204 client.web.try_send(wreq)
205 .map_err(|_| anyhow!("client task shut down!"))?;
207 let reply: WebResponse = reply_recv.await?;
208 warnings = reply.warnings;
215 eprintln!("error={}", e);
219 eprintln!("warnings={:?}", &warnings);
221 Ok(hyper::Response::new(hyper::Body::from("Hello World")))
224 #[allow(unused_variables)] // xxx
225 #[allow(unused_mut)] // xxx
226 async fn run_client(ic: Arc<InstanceConfig>,
227 mut web: mpsc::Receiver<WebRequest>)
231 reply_to: tokio::sync::oneshot::Sender<WebResponse>,
232 oi: OutstandingInner,
235 struct OutstandingInner {
236 target_requests_outstanding: u32,
238 let mut outstanding: VecDeque<Outstanding> = default();
239 let downbound: VecDeque<(/*xxx*/)> = default();
241 let try_send_response = |
242 reply_to: tokio::sync::oneshot::Sender<WebResponse>,
243 response: WebResponse
245 reply_to.send(response)
246 .unwrap_or_else(|_: WebResponse| () /* oh dear */ /* xxx trace? */);
251 if ! downbound.is_empty() {
252 outstanding.pop_front()
253 } else if let Some((i,_)) = outstanding.iter().enumerate().find({
254 |(_,o)| outstanding.len() > o.oi.target_requests_outstanding.sat()
256 Some(outstanding.remove(i).unwrap())
261 let response = WebResponse {
267 try_send_response(ret.reply_to, response);
274 initial, initial_remaining, length_hint, mut body,
276 reply_to, mut warnings,
277 } = req.ok_or_else(|| anyhow!("webservers all shut down!"))?;
281 let initial_used = initial.len() - initial_remaining;
283 let whole_request = read_limited_bytes(
284 ic.max_batch_up.sat(),
288 ).await.context("read request body")?;
290 let (meta, mut comps) =
291 multipart::ComponentIterator::resume_mid_component(
292 &whole_request[initial_used..],
294 ).context("resume parsing body, after auth checks")?;
296 let mut meta = MetadataFieldIterator::new(&meta);
299 { $v:ident, ( $( $badcmp:tt )? ), $ret:expr,
300 let $server:ident, $client:ident $($code:tt)*
304 let $client $($code)*
306 if $client $badcmp $server {
307 throw!(anyhow!("mismatch: client={:?} {} server={:?}",
308 $client, stringify!($badcmp), $server));
312 })().context(stringify!($v))?;
318 target_requests_outstanding, ( != ), client,
319 let server, client: u32 = meta.need_parse()?;
323 http_timeout, ( > ), client,
324 let server, client = Duration::from_secs(meta.need_parse()?);
329 let server, client: u32 = meta.parse()?.unwrap_or(server);
333 max_batch_down, (), min(client, server),
334 let server, client: u32 = meta.parse()?.unwrap_or(server);
338 max_batch_up, ( > ), client,
339 let server, client = meta.parse()?.unwrap_or(server);
342 while let Some(comp) = comps.next(&mut warnings, PartName::d)? {
343 if comp.name != PartName::d {
344 warnings.add(&format_args!("unexpected part {:?}", comp.name))?;
346 checkn(Mime2Slip, mtu, comp.payload, |header| {
347 let saddr = ip_packet_addr::<false>(header)?;
348 if saddr != ic.link.client.0 { throw!(PE::Src(saddr)) }
349 let daddr = ip_packet_addr::<true>(header)?;
351 }, |(daddr,packet)| route_packet(daddr,packet),
352 |e| { let _xxx = warnings.add(&e); }
356 let oi = OutstandingInner {
357 target_requests_outstanding,
361 Ok(oi) => outstanding.push_back(Outstanding { reply_to, oi }),
363 try_send_response(reply_to, WebResponse {
372 //Err(anyhow!("xxx"))
377 let opts = Opts::from_args();
383 let (global, ipif) = config::startup(
384 "hippotatd", LinkEnd::Server,
385 &opts.config, &opts.log, |ics|
387 let global = config::InstanceConfigGlobal::from(&ics);
388 let ipif = Ipif::start(&global.ipif, None)?;
390 let all_clients: AllClients = ics.into_iter().map(|ic| {
391 let ic = Arc::new(ic);
393 let (web_send, web_recv) = mpsc::channel(
394 5 // xxx should me max_requests_outstanding but that's
395 // marked client-only so needs rework
398 let ic_ = ic.clone();
399 tasks.push((tokio::spawn(async move {
400 run_client(ic_, web_recv).await.void_unwrap_err()
401 }), format!("client {}", &ic)));
409 let all_clients = Arc::new(all_clients);
411 for addr in &global.addrs {
412 let all_clients_ = all_clients.clone();
413 let make_service = hyper::service::make_service_fn(move |_conn| {
414 let all_clients_ = all_clients_.clone();
415 async { Ok::<_, Void>( hyper::service::service_fn(move |req| {
416 handle(all_clients_.clone(), req)
420 let addr = SocketAddr::new(*addr, global.port);
421 let server = hyper::Server::try_bind(&addr)
423 .http1_preserve_header_case(true)
424 .serve(make_service);
425 info!("listening on {}", &addr);
426 let task = tokio::task::spawn(async move {
428 Ok(()) => anyhow!("shut down?!"),
432 tasks.push((task, format!("http server {}", addr)));
438 let died = future::select_all(
439 tasks.iter_mut().map(|e| &mut e.0)
441 error!("xxx {:?}", &died);
443 ipif.quitting(None).await;