client.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. mod utils;
  2. use log::debug;
  3. use std::os::unix::io::AsRawFd;
  4. use std::str::{self, FromStr};
  5. use smoltcp::iface::{Config, Interface, 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, Ipv6Address};
  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. // Create interface
  25. let mut config = match device.capabilities().medium {
  26. Medium::Ethernet => {
  27. Config::new(EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]).into())
  28. }
  29. Medium::Ip => Config::new(smoltcp::wire::HardwareAddress::Ip),
  30. Medium::Ieee802154 => todo!(),
  31. };
  32. config.random_seed = rand::random();
  33. let mut iface = Interface::new(config, &mut device, Instant::now());
  34. iface.update_ip_addrs(|ip_addrs| {
  35. ip_addrs
  36. .push(IpCidr::new(IpAddress::v4(192, 168, 69, 1), 24))
  37. .unwrap();
  38. ip_addrs
  39. .push(IpCidr::new(IpAddress::v6(0xfdaa, 0, 0, 0, 0, 0, 0, 1), 64))
  40. .unwrap();
  41. ip_addrs
  42. .push(IpCidr::new(IpAddress::v6(0xfe80, 0, 0, 0, 0, 0, 0, 1), 64))
  43. .unwrap();
  44. });
  45. iface
  46. .routes_mut()
  47. .add_default_ipv4_route(Ipv4Address::new(192, 168, 69, 100))
  48. .unwrap();
  49. iface
  50. .routes_mut()
  51. .add_default_ipv6_route(Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 0x100))
  52. .unwrap();
  53. // Create sockets
  54. let tcp_rx_buffer = tcp::SocketBuffer::new(vec![0; 1500]);
  55. let tcp_tx_buffer = tcp::SocketBuffer::new(vec![0; 1500]);
  56. let tcp_socket = tcp::Socket::new(tcp_rx_buffer, tcp_tx_buffer);
  57. let mut sockets = SocketSet::new(vec![]);
  58. let tcp_handle = sockets.add(tcp_socket);
  59. let socket = sockets.get_mut::<tcp::Socket>(tcp_handle);
  60. socket
  61. .connect(iface.context(), (address, port), 49500)
  62. .unwrap();
  63. let mut tcp_active = false;
  64. loop {
  65. let timestamp = Instant::now();
  66. iface.poll(timestamp, &mut device, &mut sockets);
  67. let socket = sockets.get_mut::<tcp::Socket>(tcp_handle);
  68. if socket.is_active() && !tcp_active {
  69. debug!("connected");
  70. } else if !socket.is_active() && tcp_active {
  71. debug!("disconnected");
  72. break;
  73. }
  74. tcp_active = socket.is_active();
  75. if socket.may_recv() {
  76. let data = socket
  77. .recv(|data| {
  78. let mut data = data.to_owned();
  79. if !data.is_empty() {
  80. debug!(
  81. "recv data: {:?}",
  82. str::from_utf8(data.as_ref()).unwrap_or("(invalid utf8)")
  83. );
  84. data = data.split(|&b| b == b'\n').collect::<Vec<_>>().concat();
  85. data.reverse();
  86. data.extend(b"\n");
  87. }
  88. (data.len(), data)
  89. })
  90. .unwrap();
  91. if socket.can_send() && !data.is_empty() {
  92. debug!(
  93. "send data: {:?}",
  94. str::from_utf8(data.as_ref()).unwrap_or("(invalid utf8)")
  95. );
  96. socket.send_slice(&data[..]).unwrap();
  97. }
  98. } else if socket.may_send() {
  99. debug!("close");
  100. socket.close();
  101. }
  102. phy_wait(fd, iface.poll_delay(timestamp, &sockets)).expect("wait error");
  103. }
  104. }