server.rs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. mod utils;
  2. use log::debug;
  3. use std::fmt::Write;
  4. use std::os::unix::io::AsRawFd;
  5. use smoltcp::iface::{Config, Interface, SocketSet};
  6. use smoltcp::phy::{wait as phy_wait, Device, Medium};
  7. use smoltcp::socket::{tcp, udp};
  8. use smoltcp::time::{Duration, 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. let mut matches = utils::parse_options(&opts, free);
  16. let device = utils::parse_tuntap_options(&mut matches);
  17. let fd = device.as_raw_fd();
  18. let mut device =
  19. utils::parse_middleware_options(&mut matches, device, /*loopback=*/ false);
  20. // Create interface
  21. let mut config = Config::new();
  22. config.random_seed = rand::random();
  23. if device.capabilities().medium == Medium::Ethernet {
  24. config.hardware_addr = Some(EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]).into());
  25. }
  26. let mut iface = Interface::new(config, &mut device);
  27. iface.update_ip_addrs(|ip_addrs| {
  28. ip_addrs
  29. .push(IpCidr::new(IpAddress::v4(192, 168, 69, 1), 24))
  30. .unwrap();
  31. ip_addrs
  32. .push(IpCidr::new(IpAddress::v6(0xfdaa, 0, 0, 0, 0, 0, 0, 1), 64))
  33. .unwrap();
  34. ip_addrs
  35. .push(IpCidr::new(IpAddress::v6(0xfe80, 0, 0, 0, 0, 0, 0, 1), 64))
  36. .unwrap();
  37. });
  38. iface
  39. .routes_mut()
  40. .add_default_ipv4_route(Ipv4Address::new(192, 168, 69, 100))
  41. .unwrap();
  42. iface
  43. .routes_mut()
  44. .add_default_ipv6_route(Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 0x100))
  45. .unwrap();
  46. // Create sockets
  47. let udp_rx_buffer = udp::PacketBuffer::new(
  48. vec![udp::PacketMetadata::EMPTY, udp::PacketMetadata::EMPTY],
  49. vec![0; 65535],
  50. );
  51. let udp_tx_buffer = udp::PacketBuffer::new(
  52. vec![udp::PacketMetadata::EMPTY, udp::PacketMetadata::EMPTY],
  53. vec![0; 65535],
  54. );
  55. let udp_socket = udp::Socket::new(udp_rx_buffer, udp_tx_buffer);
  56. let tcp1_rx_buffer = tcp::SocketBuffer::new(vec![0; 64]);
  57. let tcp1_tx_buffer = tcp::SocketBuffer::new(vec![0; 128]);
  58. let tcp1_socket = tcp::Socket::new(tcp1_rx_buffer, tcp1_tx_buffer);
  59. let tcp2_rx_buffer = tcp::SocketBuffer::new(vec![0; 64]);
  60. let tcp2_tx_buffer = tcp::SocketBuffer::new(vec![0; 128]);
  61. let tcp2_socket = tcp::Socket::new(tcp2_rx_buffer, tcp2_tx_buffer);
  62. let tcp3_rx_buffer = tcp::SocketBuffer::new(vec![0; 65535]);
  63. let tcp3_tx_buffer = tcp::SocketBuffer::new(vec![0; 65535]);
  64. let tcp3_socket = tcp::Socket::new(tcp3_rx_buffer, tcp3_tx_buffer);
  65. let tcp4_rx_buffer = tcp::SocketBuffer::new(vec![0; 65535]);
  66. let tcp4_tx_buffer = tcp::SocketBuffer::new(vec![0; 65535]);
  67. let tcp4_socket = tcp::Socket::new(tcp4_rx_buffer, tcp4_tx_buffer);
  68. let mut sockets = SocketSet::new(vec![]);
  69. let udp_handle = sockets.add(udp_socket);
  70. let tcp1_handle = sockets.add(tcp1_socket);
  71. let tcp2_handle = sockets.add(tcp2_socket);
  72. let tcp3_handle = sockets.add(tcp3_socket);
  73. let tcp4_handle = sockets.add(tcp4_socket);
  74. let mut tcp_6970_active = false;
  75. loop {
  76. let timestamp = Instant::now();
  77. iface.poll(timestamp, &mut device, &mut sockets);
  78. // udp:6969: respond "hello"
  79. let socket = sockets.get_mut::<udp::Socket>(udp_handle);
  80. if !socket.is_open() {
  81. socket.bind(6969).unwrap()
  82. }
  83. let client = match socket.recv() {
  84. Ok((data, endpoint)) => {
  85. debug!("udp:6969 recv data: {:?} from {}", data, endpoint);
  86. let mut data = data.to_vec();
  87. data.reverse();
  88. Some((endpoint, data))
  89. }
  90. Err(_) => None,
  91. };
  92. if let Some((endpoint, data)) = client {
  93. debug!("udp:6969 send data: {:?} to {}", data, endpoint,);
  94. socket.send_slice(&data, endpoint).unwrap();
  95. }
  96. // tcp:6969: respond "hello"
  97. let socket = sockets.get_mut::<tcp::Socket>(tcp1_handle);
  98. if !socket.is_open() {
  99. socket.listen(6969).unwrap();
  100. }
  101. if socket.can_send() {
  102. debug!("tcp:6969 send greeting");
  103. writeln!(socket, "hello").unwrap();
  104. debug!("tcp:6969 close");
  105. socket.close();
  106. }
  107. // tcp:6970: echo with reverse
  108. let socket = sockets.get_mut::<tcp::Socket>(tcp2_handle);
  109. if !socket.is_open() {
  110. socket.listen(6970).unwrap()
  111. }
  112. if socket.is_active() && !tcp_6970_active {
  113. debug!("tcp:6970 connected");
  114. } else if !socket.is_active() && tcp_6970_active {
  115. debug!("tcp:6970 disconnected");
  116. }
  117. tcp_6970_active = socket.is_active();
  118. if socket.may_recv() {
  119. let data = socket
  120. .recv(|buffer| {
  121. let recvd_len = buffer.len();
  122. let mut data = buffer.to_owned();
  123. if !data.is_empty() {
  124. debug!("tcp:6970 recv data: {:?}", data);
  125. data = data.split(|&b| b == b'\n').collect::<Vec<_>>().concat();
  126. data.reverse();
  127. data.extend(b"\n");
  128. }
  129. (recvd_len, data)
  130. })
  131. .unwrap();
  132. if socket.can_send() && !data.is_empty() {
  133. debug!("tcp:6970 send data: {:?}", data);
  134. socket.send_slice(&data[..]).unwrap();
  135. }
  136. } else if socket.may_send() {
  137. debug!("tcp:6970 close");
  138. socket.close();
  139. }
  140. // tcp:6971: sinkhole
  141. let socket = sockets.get_mut::<tcp::Socket>(tcp3_handle);
  142. if !socket.is_open() {
  143. socket.listen(6971).unwrap();
  144. socket.set_keep_alive(Some(Duration::from_millis(1000)));
  145. socket.set_timeout(Some(Duration::from_millis(2000)));
  146. }
  147. if socket.may_recv() {
  148. socket
  149. .recv(|buffer| {
  150. if !buffer.is_empty() {
  151. debug!("tcp:6971 recv {:?} octets", buffer.len());
  152. }
  153. (buffer.len(), ())
  154. })
  155. .unwrap();
  156. } else if socket.may_send() {
  157. socket.close();
  158. }
  159. // tcp:6972: fountain
  160. let socket = sockets.get_mut::<tcp::Socket>(tcp4_handle);
  161. if !socket.is_open() {
  162. socket.listen(6972).unwrap()
  163. }
  164. if socket.may_send() {
  165. socket
  166. .send(|data| {
  167. if !data.is_empty() {
  168. debug!("tcp:6972 send {:?} octets", data.len());
  169. for (i, b) in data.iter_mut().enumerate() {
  170. *b = (i % 256) as u8;
  171. }
  172. }
  173. (data.len(), ())
  174. })
  175. .unwrap();
  176. }
  177. phy_wait(fd, iface.poll_delay(timestamp, &sockets)).expect("wait error");
  178. }
  179. }