client.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 std::os::unix::io::AsRawFd;
  10. use smoltcp::phy::wait as phy_wait;
  11. use smoltcp::wire::{EthernetAddress, Ipv4Address, IpAddress, IpCidr};
  12. use smoltcp::iface::{ArpCache, SliceArpCache, EthernetInterface};
  13. use smoltcp::socket::{AsSocket, SocketSet};
  14. use smoltcp::socket::{TcpSocket, TcpSocketBuffer};
  15. fn main() {
  16. utils::setup_logging("");
  17. let (mut opts, mut free) = utils::create_options();
  18. utils::add_tap_options(&mut opts, &mut free);
  19. utils::add_middleware_options(&mut opts, &mut free);
  20. free.push("ADDRESS");
  21. free.push("PORT");
  22. let mut matches = utils::parse_options(&opts, free);
  23. let device = utils::parse_tap_options(&mut matches);
  24. let fd = device.as_raw_fd();
  25. let device = utils::parse_middleware_options(&mut matches, device, /*loopback=*/false);
  26. let address = IpAddress::from_str(&matches.free[0]).expect("invalid address format");
  27. let port = u16::from_str(&matches.free[1]).expect("invalid port format");
  28. let startup_time = Instant::now();
  29. let arp_cache = SliceArpCache::new(vec![Default::default(); 8]);
  30. let tcp_rx_buffer = TcpSocketBuffer::new(vec![0; 64]);
  31. let tcp_tx_buffer = TcpSocketBuffer::new(vec![0; 128]);
  32. let tcp_socket = TcpSocket::new(tcp_rx_buffer, tcp_tx_buffer);
  33. let ethernet_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x02]);
  34. let ip_addrs = [IpCidr::new(IpAddress::v4(192, 168, 69, 2), 24)];
  35. let default_v4_gw = Ipv4Address::new(192, 168, 69, 100);
  36. let mut iface = EthernetInterface::new(
  37. Box::new(device), Box::new(arp_cache) as Box<ArpCache>,
  38. ethernet_addr, ip_addrs, Some(default_v4_gw));
  39. let mut sockets = SocketSet::new(vec![]);
  40. let tcp_handle = sockets.add(tcp_socket);
  41. {
  42. let socket: &mut TcpSocket = sockets.get_mut(tcp_handle).as_socket();
  43. socket.connect((address, port), 49500).unwrap();
  44. }
  45. let mut tcp_active = false;
  46. loop {
  47. {
  48. let socket: &mut TcpSocket = sockets.get_mut(tcp_handle).as_socket();
  49. if socket.is_active() && !tcp_active {
  50. debug!("connected");
  51. } else if !socket.is_active() && tcp_active {
  52. debug!("disconnected");
  53. break
  54. }
  55. tcp_active = socket.is_active();
  56. if socket.may_recv() {
  57. let data = {
  58. let mut data = socket.recv(128).unwrap().to_owned();
  59. if data.len() > 0 {
  60. debug!("recv data: {:?}",
  61. str::from_utf8(data.as_ref()).unwrap_or("(invalid utf8)"));
  62. data = data.split(|&b| b == b'\n').collect::<Vec<_>>().concat();
  63. data.reverse();
  64. data.extend(b"\n");
  65. }
  66. data
  67. };
  68. if socket.can_send() && data.len() > 0 {
  69. debug!("send data: {:?}",
  70. str::from_utf8(data.as_ref()).unwrap_or("(invalid utf8)"));
  71. socket.send_slice(&data[..]).unwrap();
  72. }
  73. } else if socket.may_send() {
  74. debug!("close");
  75. socket.close();
  76. }
  77. }
  78. let timestamp = utils::millis_since(startup_time);
  79. let poll_at = iface.poll(&mut sockets, timestamp).expect("poll error");
  80. phy_wait(fd, poll_at.map(|at| at.saturating_sub(timestamp))).expect("wait error");
  81. }
  82. }