-
Notifications
You must be signed in to change notification settings - Fork 146
/
streamecho.rs
43 lines (36 loc) · 1.22 KB
/
streamecho.rs
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
//! Example of using streams for an echo server.
//!
//! For brewity replies are sent with the same headers as the incoming
//! packets.
use futures::StreamExt;
use pcap::{Active, Capture, Device, Error, Packet, PacketCodec, PacketStream};
use std::error;
// Simple codec that returns owned copies, since the result may not
// reference the input packet.
pub struct BoxCodec;
impl PacketCodec for BoxCodec {
type Item = Box<[u8]>;
fn decode(&mut self, packet: Packet) -> Self::Item {
packet.data.into()
}
}
fn new_stream(device: Device) -> Result<PacketStream<Active, BoxCodec>, Error> {
// get the default Device
println!("Using device {}", device.name);
let cap = Capture::from_device(device)?
.immediate_mode(true)
.open()?
.setnonblock()?;
cap.stream(BoxCodec)
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn error::Error>> {
let device = Device::lookup()?.ok_or("no device available")?;
let mut stream = new_stream(device)?;
loop {
// Here in the event loop we may await a bunch of other
// futures too, using the select! macro from tokio.
let data = stream.next().await.unwrap()?;
stream.capture_mut().sendpacket(data)?;
}
}