server.rs 6.0 KB

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