client.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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::collections::BTreeMap;
  9. use std::time::Instant;
  10. use std::os::unix::io::AsRawFd;
  11. use smoltcp::phy::wait as phy_wait;
  12. use smoltcp::wire::{EthernetAddress, Ipv4Address, IpAddress, IpCidr};
  13. use smoltcp::iface::{NeighborCache, EthernetInterfaceBuilder};
  14. use smoltcp::socket::{SocketSet, 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 neighbor_cache = NeighborCache::new(BTreeMap::new());
  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 = EthernetInterfaceBuilder::new(device)
  37. .ethernet_addr(ethernet_addr)
  38. .neighbor_cache(neighbor_cache)
  39. .ip_addrs(ip_addrs)
  40. .ipv4_gateway(default_v4_gw)
  41. .finalize();
  42. let mut sockets = SocketSet::new(vec![]);
  43. let tcp_handle = sockets.add(tcp_socket);
  44. {
  45. let mut socket = sockets.get::<TcpSocket>(tcp_handle);
  46. socket.connect((address, port), 49500).unwrap();
  47. }
  48. let mut tcp_active = false;
  49. loop {
  50. {
  51. let mut socket = sockets.get::<TcpSocket>(tcp_handle);
  52. if socket.is_active() && !tcp_active {
  53. debug!("connected");
  54. } else if !socket.is_active() && tcp_active {
  55. debug!("disconnected");
  56. break
  57. }
  58. tcp_active = socket.is_active();
  59. if socket.may_recv() {
  60. let data = socket.recv(|data| {
  61. let mut data = data.to_owned();
  62. if data.len() > 0 {
  63. debug!("recv data: {:?}",
  64. str::from_utf8(data.as_ref()).unwrap_or("(invalid utf8)"));
  65. data = data.split(|&b| b == b'\n').collect::<Vec<_>>().concat();
  66. data.reverse();
  67. data.extend(b"\n");
  68. }
  69. (data.len(), data)
  70. }).unwrap();
  71. if socket.can_send() && data.len() > 0 {
  72. debug!("send data: {:?}",
  73. str::from_utf8(data.as_ref()).unwrap_or("(invalid utf8)"));
  74. socket.send_slice(&data[..]).unwrap();
  75. }
  76. } else if socket.may_send() {
  77. debug!("close");
  78. socket.close();
  79. }
  80. }
  81. let timestamp = utils::millis_since(startup_time);
  82. let poll_at = iface.poll(&mut sockets, timestamp).expect("poll error");
  83. phy_wait(fd, poll_at.map(|at| at.saturating_sub(timestamp))).expect("wait error");
  84. }
  85. }