client.rs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #[macro_use]
  2. extern crate log;
  3. extern crate env_logger;
  4. extern crate getopts;
  5. extern crate smoltcp;
  6. mod utils;
  7. use std::str::{self, FromStr};
  8. use std::time::Instant;
  9. use smoltcp::Error;
  10. use smoltcp::wire::{EthernetAddress, IpAddress};
  11. use smoltcp::iface::{ArpCache, SliceArpCache, EthernetInterface};
  12. use smoltcp::socket::{AsSocket, SocketSet};
  13. use smoltcp::socket::{TcpSocket, TcpSocketBuffer};
  14. fn main() {
  15. utils::setup_logging("");
  16. let (mut opts, mut free) = utils::create_options();
  17. utils::add_tap_options(&mut opts, &mut free);
  18. utils::add_middleware_options(&mut opts, &mut free);
  19. free.push("ADDRESS");
  20. free.push("PORT");
  21. let mut matches = utils::parse_options(&opts, free);
  22. let device = utils::parse_tap_options(&mut matches);
  23. let device = utils::parse_middleware_options(&mut matches, device, /*loopback=*/false);
  24. let address = IpAddress::from_str(&matches.free[0]).expect("invalid address format");
  25. let port = u16::from_str(&matches.free[1]).expect("invalid port format");
  26. let startup_time = Instant::now();
  27. let arp_cache = SliceArpCache::new(vec![Default::default(); 8]);
  28. let tcp_rx_buffer = TcpSocketBuffer::new(vec![0; 64]);
  29. let tcp_tx_buffer = TcpSocketBuffer::new(vec![0; 128]);
  30. let tcp_socket = TcpSocket::new(tcp_rx_buffer, tcp_tx_buffer);
  31. let hardware_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x02]);
  32. let protocol_addr = IpAddress::v4(192, 168, 69, 2);
  33. let mut iface = EthernetInterface::new(
  34. Box::new(device), Box::new(arp_cache) as Box<ArpCache>,
  35. hardware_addr, [protocol_addr]);
  36. let mut sockets = SocketSet::new(vec![]);
  37. let tcp_handle = sockets.add(tcp_socket);
  38. {
  39. let socket: &mut TcpSocket = sockets.get_mut(tcp_handle).as_socket();
  40. socket.connect((address, port), (protocol_addr, 49500)).unwrap();
  41. }
  42. let mut tcp_active = false;
  43. loop {
  44. {
  45. let socket: &mut TcpSocket = sockets.get_mut(tcp_handle).as_socket();
  46. if socket.is_active() && !tcp_active {
  47. debug!("connected");
  48. } else if !socket.is_active() && tcp_active {
  49. debug!("disconnected");
  50. break
  51. }
  52. tcp_active = socket.is_active();
  53. if socket.may_recv() {
  54. let data = {
  55. let mut data = socket.recv(128).unwrap().to_owned();
  56. if data.len() > 0 {
  57. debug!("recv data: {:?}",
  58. str::from_utf8(data.as_ref()).unwrap_or("(invalid utf8)"));
  59. data = data.split(|&b| b == b'\n').collect::<Vec<_>>().concat();
  60. data.reverse();
  61. data.extend(b"\n");
  62. }
  63. data
  64. };
  65. if socket.can_send() && data.len() > 0 {
  66. debug!("send data: {:?}",
  67. str::from_utf8(data.as_ref()).unwrap_or("(invalid utf8)"));
  68. socket.send_slice(&data[..]).unwrap();
  69. }
  70. } else if socket.may_send() {
  71. debug!("close");
  72. socket.close();
  73. }
  74. }
  75. let timestamp = Instant::now().duration_since(startup_time);
  76. let timestamp_ms = (timestamp.as_secs() * 1000) +
  77. (timestamp.subsec_nanos() / 1000000) as u64;
  78. match iface.poll(&mut sockets, timestamp_ms) {
  79. Ok(()) | Err(Error::Exhausted) => (),
  80. Err(e) => debug!("poll error: {}", e)
  81. }
  82. }
  83. }