client.rs 3.5 KB

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