pub fn send_tcp(&mut self, dst: SocketAddrV4, src: SocketAddrV4) -> io::Result<()> {
        // Retransmit unhandled SYN
        let state = self
            .get_state(dst, src)
            .ok_or(io::Error::from(io::ErrorKind::NotFound))?;
        if state.cache_syn().is_some() {
            return self.send_tcp_ack_syn(dst, src);
        }

        if state.src_window() > 0 {
            // TCP sequence
            let sent_size = state.cache().len();
            let remain_size = state.send_window().checked_sub(sent_size).unwrap_or(0);
            let remain_size = min(remain_size, u16::MAX as usize) as u16;

            let mut size = min(remain_size as usize, state.queue().len());
            // Avoid SWS
            if ENABLE_SEND_SWS_AVOID {
                let mtu = *self.src_mtu_map.get(src.ip()).unwrap_or(&self.local_mtu);
                let mss = mtu - (Ipv4::minimum_len() + Tcp::minimum_len());

                if size < mss && !state.cache().is_empty() {
                    size = 0;
                }
            }
            let size = size;
            if size > 0 {
                let state = self
                    .get_state_mut(dst, src)
                    .ok_or(io::Error::from(io::ErrorKind::NotFound))?;
                let payload = state.append_cache(size)?;

                // If the queue is empty and a FIN is in the queue, pop it
                if state.queue().is_empty() && state.queue_fin() {
                    // ACK/FIN
                    state.append_cache_fin();

                    // Send
                    let state = self
                        .get_state(dst, src)
                        .ok_or(io::Error::from(io::ErrorKind::NotFound))?;
                    let sequence = state.sequence();
                    self.send_tcp_ack(dst, src, sequence, &payload, true)?;
                } else {
                    // ACK
                    let state = self
                        .get_state(dst, src)
                        .ok_or(io::Error::from(io::ErrorKind::NotFound))?;
                    let sequence = state.sequence();
                    self.send_tcp_ack(dst, src, sequence, &payload, false)?;
                }
            }
        }

        // If the queue is empty and a FIN is in the queue, pop it
        // FIN
        let state = self
            .get_state_mut(dst, src)
            .ok_or(io::Error::from(io::ErrorKind::NotFound))?;
        if state.queue_fin() {
            if state.cache().is_empty() {
                // FIN
                state.append_cache_fin();

                // Send
                self.send_tcp_fin(dst, src)?;
            }
        }

        Ok(())
    }
/// Sends TCP packets from the queue.
Sends TCP packets from the queue.
Sends TCP packets from the queue .
pub fn send_tcp(&mut self, dst: SocketAddrV4, src: SocketAddrV4) -> io::Result<()> {
        // Retransmit unhandled SYN
        let state = self
            .get_state(dst, src)
            .ok_or(io::Error::from(io::ErrorKind::NotFound))?;
        if state.cache_syn().is_some() {
            return self.send_tcp_ack_syn(dst, src);
        }
        if state.src_window() > 0 {
            // TCP sequence
            let sent_size = state.cache().len();
            let remain_size = state.send_window().checked_sub(sent_size).unwrap_or(0);
            let remain_size = min(remain_size, u16::MAX as usize) as u16;
            let mut size = min(remain_size as usize, state.queue().len());
            // Avoid SWS
            if ENABLE_SEND_SWS_AVOID {
                let mtu = *self.src_mtu_map.get(src.ip()).unwrap_or(&self.local_mtu);
                let mss = mtu - (Ipv4::minimum_len() + Tcp::minimum_len());
                if size < mss && !state.cache().is_empty() {
                    size = 0;
                }
            }
            let size = size;
            if size > 0 {
                let state = self
                    .get_state_mut(dst, src)
                    .ok_or(io::Error::from(io::ErrorKind::NotFound))?;
                let payload = state.append_cache(size)?;
                // If the queue is empty and a FIN is in the queue, pop it
                if state.queue().is_empty() && state.queue_fin() {
                    // ACK/FIN
                    state.append_cache_fin();
                    // Send
                    let state = self
                        .get_state(dst, src)
                        .ok_or(io::Error::from(io::ErrorKind::NotFound))?;
                    let sequence = state.sequence();
                    self.send_tcp_ack(dst, src, sequence, &payload, true)?;
                } else {
                    // ACK
                    let state = self
                        .get_state(dst, src)
                        .ok_or(io::Error::from(io::ErrorKind::NotFound))?;
                    let sequence = state.sequence();
                    self.send_tcp_ack(dst, src, sequence, &payload, false)?;
                }
            }
        }
        // If the queue is empty and a FIN is in the queue, pop it
        // FIN
        let state = self
            .get_state_mut(dst, src)
            .ok_or(io::Error::from(io::ErrorKind::NotFound))?;
        if state.queue_fin() {
            if state.cache().is_empty() {
                // FIN
                state.append_cache_fin();
                // Send
                self.send_tcp_fin(dst, src)?;
            }
        }
        Ok(())
    }
pub fn send_tcp ( & mut self , dst : SocketAddrV4 , src : SocketAddrV4 ) -> io :: Result < ( ) > { // Retransmit unhandled SYN let state = self . get_state ( dst , src ) . ok_or ( io :: Error :: from ( io :: ErrorKind :: NotFound ) ) ? ; if state . cache_syn ( ) . is_some ( ) { return self . send_tcp_ack_syn ( dst , src ) ; } if state . src_window ( ) > 0 { // TCP sequence let sent_size = state . cache ( ) . len ( ) ; let remain_size = state . send_window ( ) . checked_sub ( sent_size ) . unwrap_or ( 0 ) ; let remain_size = min ( remain_size , u16 :: MAX as usize ) as u16 ; let mut size = min ( remain_size as usize , state . queue ( ) . len ( ) ) ; // Avoid SWS if ENABLE_SEND_SWS_AVOID { let mtu = * self . src_mtu_map . get ( src . ip ( ) ) . unwrap_or ( & self . local_mtu ) ; let mss = mtu - ( Ipv4 :: minimum_len ( ) + Tcp :: minimum_len ( ) ) ; if size < mss && ! state . cache ( ) . is_empty ( ) { size = 0 ; } } let size = size ; if size > 0 { let state = self . get_state_mut ( dst , src ) . ok_or ( io :: Error :: from ( io :: ErrorKind :: NotFound ) ) ? ; let payload = state . append_cache ( size ) ? ; // If the queue is empty and a FIN is in the queue, pop it if state . queue ( ) . is_empty ( ) && state . queue_fin ( ) { // ACK/FIN state . append_cache_fin ( ) ; // Send let state = self . get_state ( dst , src ) . ok_or ( io :: Error :: from ( io :: ErrorKind :: NotFound ) ) ? ; let sequence = state . sequence ( ) ; self . send_tcp_ack ( dst , src , sequence , & payload , true ) ? ; } else { // ACK let state = self . get_state ( dst , src ) . ok_or ( io :: Error :: from ( io :: ErrorKind :: NotFound ) ) ? ; let sequence = state . sequence ( ) ; self . send_tcp_ack ( dst , src , sequence , & payload , false ) ? ; } } } // If the queue is empty and a FIN is in the queue, pop it // FIN let state = self . get_state_mut ( dst , src ) . ok_or ( io :: Error :: from ( io :: ErrorKind :: NotFound ) ) ? ; if state . queue_fin ( ) { if state . cache ( ) . is_empty ( ) { // FIN state . append_cache_fin ( ) ; // Send self . send_tcp_fin ( dst , src ) ? ; } } Ok ( ( ) ) }
Sends TCP packets from the queue.
Sends TCP packets from the queue .
&mut self
dst
SocketAddrV4
src
SocketAddrV4
&mut self
dst
SocketAddrV4
src
SocketAddrV4