[][src]Struct tap_windows::Device

pub struct Device { /* fields omitted */ }

A tap-windows device handle, it offers facilities to:

  • create, open and delete interfaces
  • write and read the current configuration
  • write and read packets from the device Example
use tap_windows::Device;
use std::io::Read;

const MY_INTERFACE: &str = "My Interface";

// Try to open the device
let mut dev = Device::open(MY_INTERFACE)
    .or_else(|_| -> std::io::Result<_> {
        // The device does not exists...
        // try creating a new one
         
        let dev = Device::create()?;
        dev.set_name(MY_INTERFACE)?;
     
        Ok(dev)
    })
    // Everything failed, just panic
    .expect("Failed to open device");

// Set the device ip
dev.set_ip([192, 168, 60, 1], [255, 255, 255, 0])
    .expect("Failed to set device ip");

// Setup read buffer
let mtu = dev.get_mtu().unwrap_or(1500);
let mut buf = vec![0; mtu as usize];

// Read a single packet from the device
let amt = dev.read(&mut buf)
    .expect("Failed to read packet");

// Print it
println!("{:#?}", &buf[..amt]);

Implementations

impl Device[src]

pub fn create() -> Result<Self>[src]

Creates a new tap-windows device Example

use tap_windows::Device;

let dev = Device::create()
    .expect("Failed to create device");

println!("{:?}", dev.get_name());

pub fn open(name: &str) -> Result<Self>[src]

Opens an existing tap-windows device by name Example

use tap_windows::Device;

let dev = Device::open("My Own Device")
    .expect("Failed to open device");

println!("{:?}", dev.get_name());

pub fn delete(self) -> Result<()>[src]

Deletes the interface before closing it. By default interfaces are never deleted on Drop, with this you can choose if you want deletion or not Example

use tap_windows::Device;

let dev = Device::create()
    .expect("Failed to create device");

println!("{:?}", dev.get_name());

// Perform a quick cleanup before exiting
dev.delete().expect("Failed to delete device");

pub fn up(&self) -> Result<()>[src]

Sets the status of the interface to connected. Equivalent to .set_status(true)

pub fn down(&self) -> Result<()>[src]

Sets the status of the interface to disconnected. Equivalent to .set_status(false)

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

Retieve the mac of the interface

pub fn get_version(&self) -> Result<[u32; 3]>[src]

Retrieve the version of the driver

pub fn get_mtu(&self) -> Result<u32>[src]

Retieve the mtu of the interface

pub fn get_name(&self) -> Result<String>[src]

Retrieve the name of the interface

pub fn set_name(&self, newname: &str) -> Result<()>[src]

Set the name of the interface

pub fn set_ip<A, B>(&self, address: A, mask: B) -> Result<()> where
    A: Into<Ipv4Addr>,
    B: Into<Ipv4Addr>, 
[src]

Set the ip of the interface

use tap_windows::Device;

let dev = Device::create()
    .expect("Failed to create device");

dev.set_ip([192, 168, 60, 1], [255, 255, 255, 0])
    .expect("Failed to set interface ip");

println!("{:?}", dev.get_name());

pub fn set_status(&self, status: bool) -> Result<()>[src]

Set the status of the interface, true for connected, false for disconnected.

Trait Implementations

impl Drop for Device[src]

impl Read for Device[src]

impl Write for Device[src]

Auto Trait Implementations

impl RefUnwindSafe for Device

impl !Send for Device

impl !Sync for Device

impl Unpin for Device

impl UnwindSafe for Device

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.