Struct DeviceImpl

Source
pub struct DeviceImpl { /* private fields */ }
Expand description

A TUN device using the TUN/TAP Linux driver.

Implementations§

Source§

impl DeviceImpl

Source

pub fn if_index(&self) -> Result<u32>

Retrieves the interface index for the network interface.

This function converts the interface name (obtained via self.name()) into a C-compatible string (CString) and then calls the libc function if_nametoindex to retrieve the corresponding interface index.

Source

pub fn addresses(&self) -> Result<Vec<IpAddr>>

Retrieves all IP addresses associated with the network interface.

This function calls getifaddrs with the interface name, then iterates over the returned list of interface addresses, extracting and collecting the IP addresses into a vector.

Source§

impl DeviceImpl

Source

pub fn udp_gso(&self) -> bool

Returns whether UDP Generic Segmentation Offload (GSO) is enabled.

This is determined by the udp_gso flag in the device.

Source

pub fn tcp_gso(&self) -> bool

Returns whether TCP Generic Segmentation Offload (GSO) is enabled.

In this implementation, this is represented by the vnet_hdr flag.

Source

pub fn set_tx_queue_len(&self, tx_queue_len: u32) -> Result<()>

Sets the transmit queue length for the network interface.

This method constructs an interface request (ifreq) structure, assigns the desired transmit queue length to the ifru_metric field, and calls the change_tx_queue_len function using the control file descriptor. If the underlying operation fails, an I/O error is returned.

Source

pub fn tx_queue_len(&self) -> Result<u32>

Retrieves the current transmit queue length for the network interface.

This function constructs an interface request structure and calls tx_queue_len to populate it with the current transmit queue length. The value is then returned.

Source

pub fn persist(&self) -> Result<()>

Make the device persistent.

Source

pub fn user(&self, value: i32) -> Result<()>

Set the owner of the device.

Source

pub fn group(&self, value: i32) -> Result<()>

Set the group of the device.

Source

pub fn send_multiple<B: ExpandBuffer>( &self, gro_table: &mut GROTable, bufs: &mut [B], offset: usize, ) -> Result<usize>

send multiple fragmented data packets. GROTable can be reused, as it is used to assist in data merging. Offset is the starting position of the data. Need to meet offset>=10.

Source

pub fn recv_multiple<B: AsRef<[u8]> + AsMut<[u8]>>( &self, original_buffer: &mut [u8], bufs: &mut [B], sizes: &mut [usize], offset: usize, ) -> Result<usize>

Recv a packet from tun device. If offload is enabled. This method can be used to obtain processed data.

original_buffer is used to store raw data, including the VirtioNetHdr and the unsplit IP packet. The recommended size is 10 + 65535. bufs and sizes are used to store the segmented IP packets. bufs.len == sizes.len > 65535/MTU offset: Starting position

Source§

impl DeviceImpl

Source

pub fn remove_address_v6(&self, addr: Ipv6Addr, prefix: u8) -> Result<()>

Source

pub fn name(&self) -> Result<String>

Retrieves the name of the network interface.

Source

pub fn set_name(&self, value: &str) -> Result<()>

Sets a new name for the network interface.

This function converts the provided name into a C-compatible string, checks that its length does not exceed the maximum allowed (IFNAMSIZ), and then copies it into an interface request structure. It then uses a system call (via siocsifname) to apply the new name.

Source

pub fn is_running(&self) -> Result<bool>

Checks whether the network interface is currently running.

The interface is considered running if both the IFF_UP and IFF_RUNNING flags are set.

Source

pub fn enabled(&self, value: bool) -> Result<()>

Enables or disables the network interface.

If value is true, the interface is enabled by setting the IFF_UP and IFF_RUNNING flags. If false, the IFF_UP flag is cleared. The change is applied using a system call.

Source

pub fn broadcast(&self) -> Result<IpAddr>

Retrieves the broadcast address of the network interface.

This function populates an interface request with the broadcast address via a system call, converts it into a sockaddr structure, and then extracts the IP address.

Source

pub fn set_broadcast(&self, value: IpAddr) -> Result<()>

Sets the broadcast address of the network interface.

This function converts the given IP address into a sockaddr structure (with a specified overwrite size) and then applies it to the interface via a system call.

Source

pub fn set_network_address<IPv4: ToIpv4Address, Netmask: ToIpv4Netmask>( &self, address: IPv4, netmask: Netmask, destination: Option<IPv4>, ) -> Result<()>

Sets the IPv4 network address, netmask, and an optional destination address.

This function sets the interface’s address, netmask, and if provided, the destination address. It calls the helper methods set_address_v4, set_netmask, and set_destination respectively.

Source

pub fn remove_address(&self, addr: IpAddr) -> Result<()>

Removes an IP address from the interface.

For IPv4 addresses, it iterates over the current addresses and if a match is found, resets the address to 0.0.0.0 (unspecified). For IPv6 addresses, it retrieves the interface addresses by name and removes the matching address, taking into account its prefix length.

Source

pub fn add_address_v6<IPv6: ToIpv6Address, Netmask: ToIpv6Netmask>( &self, addr: IPv6, netmask: Netmask, ) -> Result<()>

Adds an IPv6 address to the interface.

This function creates an in6_ifreq structure, fills in the interface index, prefix length, and IPv6 address (converted into a sockaddr structure), and then applies it using a system call.

Source

pub fn mtu(&self) -> Result<u16>

Retrieves the current MTU (Maximum Transmission Unit) for the interface.

This function constructs an interface request and uses a system call (via siocgifmtu) to obtain the MTU. The result is then converted to a u16.

Source

pub fn set_mtu(&self, value: u16) -> Result<()>

Sets the MTU (Maximum Transmission Unit) for the interface.

This function creates an interface request, sets the ifru_mtu field to the new value, and then applies it via a system call.

Source

pub fn set_mac_address(&self, eth_addr: [u8; 6]) -> Result<()>

Sets the MAC (hardware) address for the interface.

This function constructs an interface request and copies the provided MAC address into the hardware address field. It then applies the change via a system call. This operation is typically supported only for TAP devices.

Source

pub fn mac_address(&self) -> Result<[u8; 6]>

Retrieves the MAC (hardware) address of the interface.

This function queries the MAC address by the interface name using a helper function. An error is returned if the MAC address cannot be found.

Trait Implementations§

Source§

impl AsFd for DeviceImpl

Source§

fn as_fd(&self) -> BorrowedFd<'_>

Borrows the file descriptor. Read more
Source§

impl AsRawFd for DeviceImpl

Source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more
Source§

impl FromRawFd for DeviceImpl

Source§

unsafe fn from_raw_fd(fd: RawFd) -> Self

Constructs a new instance of Self from the given raw file descriptor. Read more
Source§

impl IntoRawFd for DeviceImpl

Source§

fn into_raw_fd(self) -> RawFd

Consumes this object, returning the raw underlying file descriptor. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.