fn read_packet(&self, buf: &mut [u8]) -> Result<(usize, Self::PktInfo), TunError> {
        use libc::iovec;

        // packet info data is the first four bytes (if enabled)
        let mut hdr = [0u8; 4];

        let iovs = [
            iovec {
                iov_base: hdr.as_mut_ptr() as *mut libc::c_void,
                iov_len: hdr.len(),
            },
            iovec {
                iov_base: buf.as_mut_ptr() as *mut libc::c_void,
                iov_len: buf.len(),
            },
        ];

        let idx = match self.packet_info {
            true => 0,
            false => 1,
        };

        // SAFETY: hdr and buf are guarenteed to be valid buffers
        let n: isize =
            unsafe { libc::readv(self.fd, &iovs[idx] as *const _, (iovs.len() - idx) as _) };
        tracing::trace!("tun read: read {} bytes", n);

        match n {
            -1 => Err(TunError::IO(io::Error::last_os_error())),
            n => Ok((n as usize, u32::from_be_bytes(hdr))),
        }
    }
/// Reads a packet from this tun device, including potentially packet information
///
/// The buffer must be at least 5 bytes or an error is returned
///
/// # Arguments
/// * `buf` - buffer to read data into
///
/// # Returns
/// * A tuple containing the total number of bytes read and (optionally)
/// the address family if `packet_info` is enabled
///
/// # Errors
/// * I/O
Reads a packet from this tun device, including potentially packet information
The buffer must be at least 5 bytes or an error is returned
Reads a packet from this tun device including potentially packet information The buffer must be at least 5 bytes or an error is returned
fn read_packet(&self, buf: &mut [u8]) -> Result<(usize, Self::PktInfo), TunError> {
        use libc::iovec;
        // packet info data is the first four bytes (if enabled)
        let mut hdr = [0u8; 4];
        let iovs = [
            iovec {
                iov_base: hdr.as_mut_ptr() as *mut libc::c_void,
                iov_len: hdr.len(),
            },
            iovec {
                iov_base: buf.as_mut_ptr() as *mut libc::c_void,
                iov_len: buf.len(),
            },
        ];
        let idx = match self.packet_info {
            true => 0,
            false => 1,
        };
        // SAFETY: hdr and buf are guarenteed to be valid buffers
        let n: isize =
            unsafe { libc::readv(self.fd, &iovs[idx] as *const _, (iovs.len() - idx) as _) };
        tracing::trace!("tun read: read {} bytes", n);
        match n {
            -1 => Err(TunError::IO(io::Error::last_os_error())),
            n => Ok((n as usize, u32::from_be_bytes(hdr))),
        }
    }
fn read_packet ( & self , buf : & mut [ u8 ] ) -> Result < ( usize , Self :: PktInfo ) , TunError > { use libc :: iovec ; // packet info data is the first four bytes (if enabled) let mut hdr = [ 0u8 ; 4 ] ; let iovs = [ iovec { iov_base : hdr . as_mut_ptr ( ) as * mut libc :: c_void , iov_len : hdr . len ( ) , } , iovec { iov_base : buf . as_mut_ptr ( ) as * mut libc :: c_void , iov_len : buf . len ( ) , } , ] ; let idx = match self . packet_info { true => 0 , false => 1 , } ; // SAFETY: hdr and buf are guarenteed to be valid buffers let n : isize = unsafe { libc :: readv ( self . fd , & iovs [ idx ] as * const _ , ( iovs . len ( ) - idx ) as _ ) } ; tracing :: trace ! ( " " n ) ; match n { - 1 => Err ( TunError :: IO ( io :: Error :: last_os_error ( ) ) ) , n => Ok ( ( n as usize , u32 :: from_be_bytes ( hdr ) ) ) , } }
Reads a packet from this tun device, including potentially packet information
The buffer must be at least 5 bytes or an error is returned
Reads a packet from this tun device including potentially packet information The buffer must be at least 5 bytes or an error is returned
&self
buf
&mut [u8]
A tuple containing the total number of bytes read and (optionally)
the address family if `packet_info` is enabled
A tuple containing the total number of bytes read and ( optionally ) the address family if ` packet_info ` is enabled
&self
buf
&mut [u8]
errors
None