server.rs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #[macro_use]
  2. extern crate log;
  3. extern crate env_logger;
  4. extern crate getopts;
  5. extern crate smoltcp;
  6. mod utils;
  7. use std::str;
  8. use std::fmt::Write;
  9. use std::time::Instant;
  10. use std::os::unix::io::AsRawFd;
  11. use smoltcp::phy::wait as phy_wait;
  12. use smoltcp::wire::{EthernetAddress, IpAddress};
  13. use smoltcp::iface::{ArpCache, SliceArpCache, EthernetInterface};
  14. use smoltcp::socket::{AsSocket, SocketSet};
  15. use smoltcp::socket::{UdpSocket, UdpSocketBuffer, UdpPacketBuffer};
  16. use smoltcp::socket::{TcpSocket, TcpSocketBuffer};
  17. fn main() {
  18. utils::setup_logging("");
  19. let (mut opts, mut free) = utils::create_options();
  20. utils::add_tap_options(&mut opts, &mut free);
  21. utils::add_middleware_options(&mut opts, &mut free);
  22. let mut matches = utils::parse_options(&opts, free);
  23. let device = utils::parse_tap_options(&mut matches);
  24. let fd = device.as_raw_fd();
  25. let device = utils::parse_middleware_options(&mut matches, device, /*loopback=*/false);
  26. let startup_time = Instant::now();
  27. let arp_cache = SliceArpCache::new(vec![Default::default(); 8]);
  28. let udp_rx_buffer = UdpSocketBuffer::new(vec![UdpPacketBuffer::new(vec![0; 64])]);
  29. let udp_tx_buffer = UdpSocketBuffer::new(vec![UdpPacketBuffer::new(vec![0; 128])]);
  30. let udp_socket = UdpSocket::new(udp_rx_buffer, udp_tx_buffer);
  31. let tcp1_rx_buffer = TcpSocketBuffer::new(vec![0; 64]);
  32. let tcp1_tx_buffer = TcpSocketBuffer::new(vec![0; 128]);
  33. let tcp1_socket = TcpSocket::new(tcp1_rx_buffer, tcp1_tx_buffer);
  34. let tcp2_rx_buffer = TcpSocketBuffer::new(vec![0; 64]);
  35. let tcp2_tx_buffer = TcpSocketBuffer::new(vec![0; 128]);
  36. let tcp2_socket = TcpSocket::new(tcp2_rx_buffer, tcp2_tx_buffer);
  37. let tcp3_rx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
  38. let tcp3_tx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
  39. let tcp3_socket = TcpSocket::new(tcp3_rx_buffer, tcp3_tx_buffer);
  40. let hardware_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]);
  41. let protocol_addrs = [IpAddress::v4(192, 168, 69, 1)];
  42. let mut iface = EthernetInterface::new(
  43. Box::new(device), Box::new(arp_cache) as Box<ArpCache>,
  44. hardware_addr, protocol_addrs);
  45. let mut sockets = SocketSet::new(vec![]);
  46. let udp_handle = sockets.add(udp_socket);
  47. let tcp1_handle = sockets.add(tcp1_socket);
  48. let tcp2_handle = sockets.add(tcp2_socket);
  49. let tcp3_handle = sockets.add(tcp3_socket);
  50. let mut tcp_6970_active = false;
  51. loop {
  52. // udp:6969: respond "hello"
  53. {
  54. let socket: &mut UdpSocket = sockets.get_mut(udp_handle).as_socket();
  55. if !socket.is_open() {
  56. socket.bind(6969).unwrap()
  57. }
  58. let client = match socket.recv() {
  59. Ok((data, endpoint)) => {
  60. debug!("udp:6969 recv data: {:?} from {}",
  61. str::from_utf8(data.as_ref()).unwrap(), endpoint);
  62. Some(endpoint)
  63. }
  64. Err(_) => None
  65. };
  66. if let Some(endpoint) = client {
  67. let data = b"hello\n";
  68. debug!("udp:6969 send data: {:?}",
  69. str::from_utf8(data.as_ref()).unwrap());
  70. socket.send_slice(data, endpoint).unwrap();
  71. }
  72. }
  73. // tcp:6969: respond "hello"
  74. {
  75. let socket: &mut TcpSocket = sockets.get_mut(tcp1_handle).as_socket();
  76. if !socket.is_open() {
  77. socket.listen(6969).unwrap();
  78. }
  79. if socket.can_send() {
  80. debug!("tcp:6969 send greeting");
  81. write!(socket, "hello\n").unwrap();
  82. debug!("tcp:6969 close");
  83. socket.close();
  84. }
  85. }
  86. // tcp:6970: echo with reverse
  87. {
  88. let socket: &mut TcpSocket = sockets.get_mut(tcp2_handle).as_socket();
  89. if !socket.is_open() {
  90. socket.listen(6970).unwrap()
  91. }
  92. if socket.is_active() && !tcp_6970_active {
  93. debug!("tcp:6970 connected");
  94. } else if !socket.is_active() && tcp_6970_active {
  95. debug!("tcp:6970 disconnected");
  96. }
  97. tcp_6970_active = socket.is_active();
  98. if socket.may_recv() {
  99. let data = {
  100. let mut data = socket.recv(128).unwrap().to_owned();
  101. if data.len() > 0 {
  102. debug!("tcp:6970 recv data: {:?}",
  103. str::from_utf8(data.as_ref()).unwrap_or("(invalid utf8)"));
  104. data = data.split(|&b| b == b'\n').collect::<Vec<_>>().concat();
  105. data.reverse();
  106. data.extend(b"\n");
  107. }
  108. data
  109. };
  110. if socket.can_send() && data.len() > 0 {
  111. debug!("tcp:6970 send data: {:?}",
  112. str::from_utf8(data.as_ref()).unwrap_or("(invalid utf8)"));
  113. socket.send_slice(&data[..]).unwrap();
  114. }
  115. } else if socket.may_send() {
  116. debug!("tcp:6970 close");
  117. socket.close();
  118. }
  119. }
  120. // tcp:6971: sinkhole
  121. {
  122. let socket: &mut TcpSocket = sockets.get_mut(tcp3_handle).as_socket();
  123. if !socket.is_open() {
  124. socket.listen(6971).unwrap()
  125. }
  126. if socket.may_recv() {
  127. if let Ok(data) = socket.recv(65535) {
  128. if data.len() > 0 {
  129. debug!("tcp:6971 recv {:?} octets", data.len());
  130. }
  131. }
  132. } else if socket.may_send() {
  133. socket.close();
  134. }
  135. }
  136. let timestamp = utils::millis_since(startup_time);
  137. let poll_at = iface.poll(&mut sockets, timestamp).expect("poll error");
  138. phy_wait(fd, poll_at).expect("wait error");
  139. }
  140. }