client.rs 4.3 KB

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