1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use std::cell::RefCell;
use std::collections::VecDeque;
use std::rc::Rc;

use ya_smoltcp::phy;
use ya_smoltcp::time;

use crate::metrics::ChannelMetrics;

use ya_relay_util::Payload;

type Pcap = RefCell<Box<dyn phy::PcapSink>>;

/// Network device capable of injecting and extracting packets
pub struct CaptureDevice {
    tx_queue: VecDeque<Vec<u8>>,
    rx_queue: VecDeque<Payload>,
    medium: phy::Medium,
    max_transmission_unit: usize,
    pcap: Option<Pcap>,
    metrics: Rc<RefCell<ChannelMetrics>>,
}

impl Default for CaptureDevice {
    fn default() -> Self {
        Self {
            tx_queue: Default::default(),
            rx_queue: Default::default(),
            medium: Default::default(),
            max_transmission_unit: 1280,
            pcap: Default::default(),
            metrics: Default::default(),
        }
    }
}

impl CaptureDevice {
    pub fn tap(mtu: usize) -> Self {
        Self {
            max_transmission_unit: mtu,
            ..Default::default()
        }
    }

    pub fn tun(mtu: usize) -> Self {
        Self {
            medium: phy::Medium::Ip,
            max_transmission_unit: mtu,
            ..Default::default()
        }
    }

    pub fn pcap_tap<S>(mtu: usize, mut pcap: S) -> Self
    where
        S: phy::PcapSink + 'static,
    {
        pcap.global_header(phy::PcapLinkType::Ethernet);
        let pcap = RefCell::new(Box::new(pcap));

        Self {
            max_transmission_unit: mtu,
            pcap: Some(pcap),
            ..Default::default()
        }
    }

    pub fn pcap_tun<S>(mtu: usize, mut pcap: S) -> Self
    where
        S: phy::PcapSink + 'static,
    {
        pcap.global_header(phy::PcapLinkType::Ip);
        let pcap = RefCell::new(Box::new(pcap));

        Self {
            medium: phy::Medium::Ip,
            max_transmission_unit: mtu,
            pcap: Some(pcap),
            ..Default::default()
        }
    }

    #[inline]
    pub fn is_tun(&self) -> bool {
        self.medium == phy::Medium::Ip
    }

    #[inline]
    pub fn metrics(&self) -> ChannelMetrics {
        self.metrics.borrow().clone()
    }

    #[inline]
    pub fn phy_rx(&mut self, data: impl Into<Payload>) {
        self.rx_queue.push_back(data.into());
    }

    #[inline]
    pub fn next_phy_tx(&mut self) -> Option<Vec<u8>> {
        self.tx_queue.pop_front()
    }
}

impl<'a> phy::Device<'a> for CaptureDevice {
    type RxToken = RxToken<'a>;
    type TxToken = TxToken<'a>;

    fn receive(&'a mut self) -> Option<(Self::RxToken, Self::TxToken)> {
        let item = self.rx_queue.pop_front();
        item.map(move |buffer| {
            let rx = RxToken {
                buffer,
                pcap: &self.pcap,
                metrics: self.metrics.clone(),
            };
            let tx = TxToken {
                queue: &mut self.tx_queue,
                pcap: &self.pcap,
                metrics: self.metrics.clone(),
            };
            (rx, tx)
        })
    }

    fn transmit(&'a mut self) -> Option<Self::TxToken> {
        Some(TxToken {
            queue: &mut self.tx_queue,
            pcap: &self.pcap,
            metrics: self.metrics.clone(),
        })
    }

    fn capabilities(&self) -> phy::DeviceCapabilities {
        let mut caps = phy::DeviceCapabilities::default();
        caps.max_transmission_unit = self.max_transmission_unit;
        caps.medium = self.medium;
        caps
    }
}

/// Receipt token
pub struct RxToken<'a> {
    buffer: Payload,
    pcap: &'a Option<Pcap>,
    metrics: Rc<RefCell<ChannelMetrics>>,
}

impl<'a> phy::RxToken for RxToken<'a> {
    fn consume<R, F>(mut self, timestamp: time::Instant, f: F) -> ya_smoltcp::Result<R>
    where
        F: FnOnce(&mut [u8]) -> ya_smoltcp::Result<R>,
    {
        let result = f(self.buffer.as_mut());

        {
            let mut metrics = self.metrics.borrow_mut();
            metrics.rx.push(self.buffer.len() as f32);
        }

        if let Some(pcap) = self.pcap {
            pcap.borrow_mut().packet(timestamp, self.buffer.as_ref());
        }

        result
    }
}

/// Transmission token
pub struct TxToken<'a> {
    queue: &'a mut VecDeque<Vec<u8>>,
    pcap: &'a Option<Pcap>,
    metrics: Rc<RefCell<ChannelMetrics>>,
}

impl<'a> phy::TxToken for TxToken<'a> {
    fn consume<R, F>(self, timestamp: time::Instant, len: usize, f: F) -> ya_smoltcp::Result<R>
    where
        F: FnOnce(&mut [u8]) -> ya_smoltcp::Result<R>,
    {
        let mut buffer = vec![0; len];
        buffer.resize(len, 0);
        let result = f(&mut buffer);

        {
            let mut metrics = self.metrics.borrow_mut();
            metrics.tx.push(buffer.len() as f32);
        }

        if let Some(pcap) = self.pcap {
            pcap.borrow_mut().packet(timestamp, buffer.as_ref());
        }

        self.queue.push_back(buffer);
        result
    }
}