chiark / gitweb /
62fb556829c459243c41448ea89dc98af4503d26
[hippotat.git] / server / slocal.rs
1 // Copyright 2021 Ian Jackson and contributors to Hippotat
2 // SPDX-License-Identifier: GPL-3.0-or-later
3 // There is NO WARRANTY.
4
5 use super::*;
6
7 #[allow(dead_code)] // xxx
8 #[allow(unused_variables)] // xxx
9 pub async fn run(global: Arc<Global>,
10                  mut rx: mpsc::Receiver<RoutedPacket>,
11                  mut ipif: Ipif) -> Result<Void,AE> {
12   let r = async {
13     let mut goodness: i32 = 0;
14     const GOODNESS_SHIFT: u8 = 8;
15     const GOODNESS_MIN: i32 = -16;
16
17     loop {
18       select!{
19         biased;
20
21         data = rx.recv() =>
22         {
23           let data = data.ok_or_else(|| anyhow!("rx stream end!"))?;
24           let mut data = &*data.data;
25           let mut slip_end = &[SLIP_END][..];
26           let mut buf = Buf::chain(&mut data, &mut slip_end);
27           ipif.rx.write_all_buf(&mut buf).await
28             .context("write to ipif")?;
29         },
30
31         data = Ipif::next_frame(&mut ipif.tx) =>
32         {
33           let data = data?;
34           let may_route = MayRoute::came_from_outside_hippotatd();
35
36           goodness -= goodness >> GOODNESS_SHIFT;
37
38           // xxx should be process1
39           match process1(SlipNoConv, global.config.mtu, &data, |header|{
40             let saddr = ip_packet_addr::<false>(header)?;
41             let daddr = ip_packet_addr::<true>(header)?;
42             Ok((saddr,daddr))
43           }) {
44             Err(PE::Empty) => { },
45
46             Err(pe) => {
47               goodness -= 1;
48               error!("[good={}] invalid data from local tx ipif {}",
49                      goodness, pe);
50               if goodness < GOODNESS_MIN {
51                 throw!(anyhow!("too many bad packets, too few good ones!"))
52               }
53             },
54
55             Ok((ref data, (ref saddr, ref daddr)))
56             if ! global.config.vnetwork.iter().any(|n| n.contains(saddr)) => {
57               // pretent as if this came from route
58               trace!(
59                 target: "hippotatd",
60  "discard to={:?} came=ipif user=local len={} outside-vnets: from={:?}",
61                 daddr, saddr, data.len());
62             },
63
64             Ok((data, (saddr, daddr))) => {
65               goodness += 1;
66               route_packet(
67                 &global, "ipif", None,
68                 data, daddr, may_route.clone()
69               ).await;
70             }
71           }
72         },
73       }
74     }
75   }.await;
76
77   ipif.quitting(None).await;
78   r
79 }