Преглед изворни кода

Add a sinkhole to the server example.

whitequark пре 8 година
родитељ
комит
961b12ce93
2 измењених фајлова са 25 додато и 0 уклоњено
  1. 2 0
      README.md
  2. 23 0
      examples/server.rs

+ 2 - 0
README.md

@@ -189,6 +189,8 @@ It responds to:
     where it will respond "yo dawg" to any incoming connection and immediately close it;
     where it will respond "yo dawg" to any incoming connection and immediately close it;
   * TCP packets on port 6970 (`socat stdio tcp4-connect:192.168.69.1:6970 <<<"abcdefg"`),
   * TCP packets on port 6970 (`socat stdio tcp4-connect:192.168.69.1:6970 <<<"abcdefg"`),
     where it will respond with reversed chunks of the input indefinitely.
     where it will respond with reversed chunks of the input indefinitely.
+  * TCP packets on port 6971 (`cat /dev/urandom | socat stdio tcp4-connect:192.168.69.1:6971`),
+    which will be ignored.
 
 
 The buffers are only 64 bytes long, for convenience of testing resource exhaustion conditions.
 The buffers are only 64 bytes long, for convenience of testing resource exhaustion conditions.
 
 

+ 23 - 0
examples/server.rs

@@ -35,6 +35,10 @@ fn main() {
     let tcp2_tx_buffer = TcpSocketBuffer::new(vec![0; 128]);
     let tcp2_tx_buffer = TcpSocketBuffer::new(vec![0; 128]);
     let tcp2_socket = TcpSocket::new(tcp2_rx_buffer, tcp2_tx_buffer);
     let tcp2_socket = TcpSocket::new(tcp2_rx_buffer, tcp2_tx_buffer);
 
 
+    let tcp3_rx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
+    let tcp3_tx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
+    let tcp3_socket = TcpSocket::new(tcp3_rx_buffer, tcp3_tx_buffer);
+
     let hardware_addr  = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]);
     let hardware_addr  = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]);
     let protocol_addrs = [IpAddress::v4(192, 168, 69, 1)];
     let protocol_addrs = [IpAddress::v4(192, 168, 69, 1)];
     let mut iface      = EthernetInterface::new(
     let mut iface      = EthernetInterface::new(
@@ -45,6 +49,7 @@ fn main() {
     let udp_handle  = sockets.add(udp_socket);
     let udp_handle  = sockets.add(udp_socket);
     let tcp1_handle = sockets.add(tcp1_socket);
     let tcp1_handle = sockets.add(tcp1_socket);
     let tcp2_handle = sockets.add(tcp2_socket);
     let tcp2_handle = sockets.add(tcp2_socket);
+    let tcp3_handle = sockets.add(tcp3_socket);
 
 
     let mut tcp_6970_active = false;
     let mut tcp_6970_active = false;
     loop {
     loop {
@@ -125,6 +130,24 @@ fn main() {
             }
             }
         }
         }
 
 
+        // tcp:6971: sinkhole
+        {
+            let socket: &mut TcpSocket = sockets.get_mut(tcp3_handle).as_socket();
+            if !socket.is_open() {
+                socket.listen(6971).unwrap()
+            }
+
+            if socket.may_recv() {
+                if let Ok(data) = socket.recv(65535) {
+                    if data.len() > 0 {
+                        debug!("tcp:6971 recv {:?} octets", data.len());
+                    }
+                }
+            } else if socket.may_send() {
+                socket.close();
+            }
+        }
+
         let timestamp = Instant::now().duration_since(startup_time);
         let timestamp = Instant::now().duration_since(startup_time);
         let timestamp_ms = (timestamp.as_secs() * 1000) +
         let timestamp_ms = (timestamp.as_secs() * 1000) +
                            (timestamp.subsec_nanos() / 1000000) as u64;
                            (timestamp.subsec_nanos() / 1000000) as u64;