extern crate futures;
extern crate tokio_core;
extern crate tun_tap;
use std::process::Command;
use std::time::Duration;
use futures::{Future, Stream};
use tokio_core::reactor::{Core, Interval};
use tun_tap::{Iface, Mode};
use tun_tap::async::Async;
const PING: &[u8] = &[0, 0, 8, 0, 69, 0, 0, 84, 44, 166, 64, 0, 64, 1, 247, 40, 10, 107, 1, 2, 10,
107, 1, 3, 8, 0, 62, 248, 19, 160, 0, 2, 232, 228, 34, 90, 0, 0, 0, 0, 216, 83, 3, 0, 0, 0, 0,
0, 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];
fn cmd(cmd: &str, args: &[&str]) {
let ecode = Command::new("ip")
.args(args)
.spawn()
.unwrap()
.wait()
.unwrap();
assert!(ecode.success(), "Failed to execte {}", cmd);
}
fn main() {
let iface = Iface::new("testtun%d", Mode::Tun).unwrap();
eprintln!("Iface: {:?}", iface);
cmd("ip", &["addr", "add", "dev", iface.name(), "10.107.1.3/24"]);
cmd("ip", &["link", "set", "up", "dev", iface.name()]);
let mut core = Core::new().unwrap();
let iface = Async::new(iface, &core.handle()).unwrap();
let (sink, stream) = iface.split();
let writer = Interval::new(Duration::from_secs(1), &core.handle())
.unwrap()
.map(|_| {
println!("Sending ping");
PING.to_owned()
})
.forward(sink);
let reader = stream.for_each(|packet| {
println!("Received: {:?}", packet);
Ok(())
});
core.run(reader.join(writer)).unwrap();
}