Struct Tun

Source
pub struct Tun { /* private fields */ }
Available on non-Windows or crate feature wintun only.
Expand description

A cross-platform TUN interface, suitable for tunnelling network-layer packets.

Implementations§

Source§

impl Tun

Source

pub fn new() -> Result<Self>

Creates a new, unique TUN device.

Source

pub fn new_named(if_name: Interface) -> Result<Self>

Opens or creates a TUN device of the given name.

Source

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

Retrieves the interface name of the TUN device.

Source

pub fn set_state(&mut self, state: DeviceState) -> Result<()>

Sets the adapter state of the TUN device (e.g. “up” or “down”).

Source

pub fn set_up(&mut self) -> Result<()>

Sets the adapter state of the TUN device to “up”.

Source

pub fn set_down(&mut self) -> Result<()>

Sets the adapter state of the TUN device to “down”.

Source

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

Retrieves the Maximum Transmission Unit (MTU) of the TUN device.

Source

pub fn set_nonblocking(&mut self, nonblocking: bool) -> Result<()>

Sets the blocking mode of the TUN device for reads/writes.

Source

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

Retrieves the blocking mode of the TUN device.

Source

pub fn addrs(&self) -> Result<Vec<AddressInfo>>

Available on non-Windows only.

Retrieves the IP addresses assigned to the interface.

§Portability

In nearly all platforms, no addresses are automatically assigned to TUN interfaces. The exception to this is OpenBSD, which automatically assigns a link-layer IPv6 address (in addition to the specified IPv6 address) the first time an IPv6 address is assigned to a TUN device. As such, portable applications should not rely on the assumption that the only addresses returned from this method are those that were previously assigned via add_addr().

Source

pub fn add_addr<A: Into<AddAddress>>(&self, req: A) -> Result<()>

Available on non-Windows only.

Assigns an IP address to the interface.

§Portability

MacOS, FreeBSD and DragonFlyBSD all require a destination address when assigning an IPv4 address to a TUN device. The destination address is optional for other platforms.

Most platforms automatically assign a link-local IPv6 address to a newly created TAP device. No platforms assign link-local IPv6 addresses to TUN devices on creation. However, OpenBSD will assign a link-local IPv6 address (in addition to the specified IPv6 address) the first time an IPv6 address is assigned to a TUN device.

Source

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

Available on non-Windows only.

Removes the specified network-layer address from the interface.

Source

pub fn send(&self, buf: &[u8]) -> Result<usize>

Sends a packet over the TUN device.

Source

pub fn recv(&self, buf: &mut [u8]) -> Result<usize>

Receives a packet over the TUN device.

Trait Implementations§

Source§

impl AsFd for Tun

Available on non-Windows only.
Source§

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

Borrows the file descriptor. Read more
Source§

impl AsRawFd for Tun

Available on non-Windows only.
Source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more

Auto Trait Implementations§

§

impl Freeze for Tun

§

impl RefUnwindSafe for Tun

§

impl Send for Tun

§

impl Sync for Tun

§

impl Unpin for Tun

§

impl UnwindSafe for Tun

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> AsSource for T
where T: AsFd,

Source§

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

Returns the borrowed file descriptor.
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.