2017-03-05 12:47:45 +08:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
|
|
|
extern crate env_logger;
|
|
|
|
extern crate getopts;
|
|
|
|
extern crate smoltcp;
|
|
|
|
|
|
|
|
mod utils;
|
|
|
|
|
|
|
|
use std::str::{self, FromStr};
|
|
|
|
use std::time::Instant;
|
2017-03-05 13:20:33 +08:00
|
|
|
use smoltcp::Error;
|
2017-03-05 12:47:45 +08:00
|
|
|
use smoltcp::wire::{EthernetAddress, IpAddress};
|
|
|
|
use smoltcp::iface::{ArpCache, SliceArpCache, EthernetInterface};
|
|
|
|
use smoltcp::socket::{AsSocket, SocketSet};
|
|
|
|
use smoltcp::socket::{TcpSocket, TcpSocketBuffer};
|
|
|
|
|
|
|
|
fn main() {
|
2017-07-23 22:57:04 +08:00
|
|
|
utils::setup_logging("");
|
|
|
|
|
|
|
|
let (mut opts, mut free) = utils::create_options();
|
|
|
|
utils::add_tap_options(&mut opts, &mut free);
|
|
|
|
utils::add_middleware_options(&mut opts, &mut free);
|
|
|
|
free.push("ADDRESS");
|
|
|
|
free.push("PORT");
|
|
|
|
|
|
|
|
let mut matches = utils::parse_options(&opts, free);
|
|
|
|
let device = utils::parse_tap_options(&mut matches);
|
|
|
|
let device = utils::parse_middleware_options(&mut matches, device, /*loopback=*/false);
|
|
|
|
let address = IpAddress::from_str(&matches.free[0]).expect("invalid address format");
|
|
|
|
let port = u16::from_str(&matches.free[1]).expect("invalid port format");
|
2017-03-05 12:47:45 +08:00
|
|
|
|
|
|
|
let startup_time = Instant::now();
|
|
|
|
|
|
|
|
let arp_cache = SliceArpCache::new(vec![Default::default(); 8]);
|
|
|
|
|
|
|
|
let tcp_rx_buffer = TcpSocketBuffer::new(vec![0; 64]);
|
|
|
|
let tcp_tx_buffer = TcpSocketBuffer::new(vec![0; 128]);
|
|
|
|
let tcp_socket = TcpSocket::new(tcp_rx_buffer, tcp_tx_buffer);
|
|
|
|
|
2017-03-05 14:15:15 +08:00
|
|
|
let hardware_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x02]);
|
|
|
|
let protocol_addr = IpAddress::v4(192, 168, 69, 2);
|
2017-03-05 12:47:45 +08:00
|
|
|
let mut iface = EthernetInterface::new(
|
|
|
|
Box::new(device), Box::new(arp_cache) as Box<ArpCache>,
|
|
|
|
hardware_addr, [protocol_addr]);
|
|
|
|
|
|
|
|
let mut sockets = SocketSet::new(vec![]);
|
|
|
|
let tcp_handle = sockets.add(tcp_socket);
|
|
|
|
|
|
|
|
{
|
|
|
|
let socket: &mut TcpSocket = sockets.get_mut(tcp_handle).as_socket();
|
|
|
|
socket.connect((address, port), (protocol_addr, 49500)).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut tcp_active = false;
|
|
|
|
loop {
|
|
|
|
{
|
|
|
|
let socket: &mut TcpSocket = sockets.get_mut(tcp_handle).as_socket();
|
|
|
|
if socket.is_active() && !tcp_active {
|
|
|
|
debug!("connected");
|
|
|
|
} else if !socket.is_active() && tcp_active {
|
|
|
|
debug!("disconnected");
|
2017-03-05 13:20:33 +08:00
|
|
|
break
|
2017-03-05 12:47:45 +08:00
|
|
|
}
|
|
|
|
tcp_active = socket.is_active();
|
|
|
|
|
|
|
|
if socket.may_recv() {
|
|
|
|
let data = {
|
|
|
|
let mut data = socket.recv(128).unwrap().to_owned();
|
|
|
|
if data.len() > 0 {
|
|
|
|
debug!("recv data: {:?}",
|
|
|
|
str::from_utf8(data.as_ref()).unwrap_or("(invalid utf8)"));
|
|
|
|
data = data.split(|&b| b == b'\n').collect::<Vec<_>>().concat();
|
|
|
|
data.reverse();
|
|
|
|
data.extend(b"\n");
|
|
|
|
}
|
|
|
|
data
|
|
|
|
};
|
|
|
|
if socket.can_send() && data.len() > 0 {
|
|
|
|
debug!("send data: {:?}",
|
|
|
|
str::from_utf8(data.as_ref()).unwrap_or("(invalid utf8)"));
|
|
|
|
socket.send_slice(&data[..]).unwrap();
|
|
|
|
}
|
|
|
|
} else if socket.may_send() {
|
|
|
|
debug!("close");
|
|
|
|
socket.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let timestamp = Instant::now().duration_since(startup_time);
|
|
|
|
let timestamp_ms = (timestamp.as_secs() * 1000) +
|
|
|
|
(timestamp.subsec_nanos() / 1000000) as u64;
|
|
|
|
match iface.poll(&mut sockets, timestamp_ms) {
|
2017-03-05 13:20:33 +08:00
|
|
|
Ok(()) | Err(Error::Exhausted) => (),
|
2017-03-05 12:47:45 +08:00
|
|
|
Err(e) => debug!("poll error: {}", e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|