client.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. mod utils;
  2. use log::debug;
  3. use std::collections::BTreeMap;
  4. use std::os::unix::io::AsRawFd;
  5. use std::str::{self, FromStr};
  6. use smoltcp::iface::{InterfaceBuilder, NeighborCache, Routes};
  7. use smoltcp::phy::{wait as phy_wait, Device, Medium};
  8. use smoltcp::socket::{SocketSet, TcpSocket, TcpSocketBuffer};
  9. use smoltcp::time::Instant;
  10. use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv4Address};
  11. fn main() {
  12. utils::setup_logging("");
  13. let (mut opts, mut free) = utils::create_options();
  14. utils::add_tuntap_options(&mut opts, &mut free);
  15. utils::add_middleware_options(&mut opts, &mut free);
  16. free.push("ADDRESS");
  17. free.push("PORT");
  18. let mut matches = utils::parse_options(&opts, free);
  19. let device = utils::parse_tuntap_options(&mut matches);
  20. let fd = device.as_raw_fd();
  21. let device = utils::parse_middleware_options(&mut matches, device, /*loopback=*/ false);
  22. let address = IpAddress::from_str(&matches.free[0]).expect("invalid address format");
  23. let port = u16::from_str(&matches.free[1]).expect("invalid port format");
  24. let neighbor_cache = NeighborCache::new(BTreeMap::new());
  25. let tcp_rx_buffer = TcpSocketBuffer::new(vec![0; 64]);
  26. let tcp_tx_buffer = TcpSocketBuffer::new(vec![0; 128]);
  27. let tcp_socket = TcpSocket::new(tcp_rx_buffer, tcp_tx_buffer);
  28. let ethernet_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x02]);
  29. let ip_addrs = [IpCidr::new(IpAddress::v4(192, 168, 69, 2), 24)];
  30. let default_v4_gw = Ipv4Address::new(192, 168, 69, 100);
  31. let mut routes_storage = [None; 1];
  32. let mut routes = Routes::new(&mut routes_storage[..]);
  33. routes.add_default_ipv4_route(default_v4_gw).unwrap();
  34. let medium = device.capabilities().medium;
  35. let mut builder = InterfaceBuilder::new(device)
  36. .ip_addrs(ip_addrs)
  37. .routes(routes);
  38. if medium == Medium::Ethernet {
  39. builder = builder
  40. .ethernet_addr(ethernet_addr)
  41. .neighbor_cache(neighbor_cache);
  42. }
  43. let mut iface = builder.finalize();
  44. let mut sockets = SocketSet::new(vec![]);
  45. let tcp_handle = sockets.add(tcp_socket);
  46. {
  47. let mut socket = sockets.get::<TcpSocket>(tcp_handle);
  48. socket.connect((address, port), 49500).unwrap();
  49. }
  50. let mut tcp_active = false;
  51. loop {
  52. let timestamp = Instant::now();
  53. match iface.poll(&mut sockets, timestamp) {
  54. Ok(_) => {}
  55. Err(e) => {
  56. debug!("poll error: {}", e);
  57. }
  58. }
  59. {
  60. let mut socket = sockets.get::<TcpSocket>(tcp_handle);
  61. if socket.is_active() && !tcp_active {
  62. debug!("connected");
  63. } else if !socket.is_active() && tcp_active {
  64. debug!("disconnected");
  65. break;
  66. }
  67. tcp_active = socket.is_active();
  68. if socket.may_recv() {
  69. let data = socket
  70. .recv(|data| {
  71. let mut data = data.to_owned();
  72. if !data.is_empty() {
  73. debug!(
  74. "recv data: {:?}",
  75. str::from_utf8(data.as_ref()).unwrap_or("(invalid utf8)")
  76. );
  77. data = data.split(|&b| b == b'\n').collect::<Vec<_>>().concat();
  78. data.reverse();
  79. data.extend(b"\n");
  80. }
  81. (data.len(), data)
  82. })
  83. .unwrap();
  84. if socket.can_send() && !data.is_empty() {
  85. debug!(
  86. "send data: {:?}",
  87. str::from_utf8(data.as_ref()).unwrap_or("(invalid utf8)")
  88. );
  89. socket.send_slice(&data[..]).unwrap();
  90. }
  91. } else if socket.may_send() {
  92. debug!("close");
  93. socket.close();
  94. }
  95. }
  96. phy_wait(fd, iface.poll_delay(&sockets, timestamp)).expect("wait error");
  97. }
  98. }