client.rs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 = Config::new();
  26. config.random_seed = rand::random();
  27. if device.capabilities().medium == Medium::Ethernet {
  28. config.hardware_addr = Some(EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]).into());
  29. }
  30. let mut iface = Interface::new(config, &mut device);
  31. iface.update_ip_addrs(|ip_addrs| {
  32. ip_addrs
  33. .push(IpCidr::new(IpAddress::v4(192, 168, 69, 1), 24))
  34. .unwrap();
  35. ip_addrs
  36. .push(IpCidr::new(IpAddress::v6(0xfdaa, 0, 0, 0, 0, 0, 0, 1), 64))
  37. .unwrap();
  38. ip_addrs
  39. .push(IpCidr::new(IpAddress::v6(0xfe80, 0, 0, 0, 0, 0, 0, 1), 64))
  40. .unwrap();
  41. });
  42. iface
  43. .routes_mut()
  44. .add_default_ipv4_route(Ipv4Address::new(192, 168, 69, 100))
  45. .unwrap();
  46. iface
  47. .routes_mut()
  48. .add_default_ipv6_route(Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 0x100))
  49. .unwrap();
  50. // Create sockets
  51. let tcp_rx_buffer = tcp::SocketBuffer::new(vec![0; 1500]);
  52. let tcp_tx_buffer = tcp::SocketBuffer::new(vec![0; 1500]);
  53. let tcp_socket = tcp::Socket::new(tcp_rx_buffer, tcp_tx_buffer);
  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. }