server.rs 6.1 KB

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