pub struct Device { /* private fields */ }
Expand description
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§
Source§impl Device
impl Device
Sourcepub fn create() -> Result<Self>
pub fn create() -> Result<Self>
Creates a new tap-windows device Example
use tap_windows::Device;
let dev = Device::create()
.expect("Failed to create device");
println!("{:?}", dev.get_name());
Sourcepub fn open(name: &str) -> Result<Self>
pub fn open(name: &str) -> Result<Self>
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());
Sourcepub fn delete(self) -> Result<()>
pub fn delete(self) -> Result<()>
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");
Sourcepub fn up(&self) -> Result<()>
pub fn up(&self) -> Result<()>
Sets the status of the interface to connected.
Equivalent to .set_status(true)
Sourcepub fn down(&self) -> Result<()>
pub fn down(&self) -> Result<()>
Sets the status of the interface to disconnected.
Equivalent to .set_status(false)
Sourcepub fn get_version(&self) -> Result<[u32; 3]>
pub fn get_version(&self) -> Result<[u32; 3]>
Retrieve the version of the driver
Sourcepub fn set_ip<A, B>(&self, address: A, mask: B) -> Result<()>
pub fn set_ip<A, B>(&self, address: A, mask: B) -> Result<()>
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());
Sourcepub fn set_status(&self, status: bool) -> Result<()>
pub fn set_status(&self, status: bool) -> Result<()>
Set the status of the interface, true for connected, false for disconnected.
Trait Implementations§
Source§impl Read for Device
impl Read for Device
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
Pull some bytes from this source into the specified buffer, returning
how many bytes were read. Read more
1.36.0 · Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
Like
read
, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
🔬This is a nightly-only experimental API. (
can_vector
)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
Reads all bytes until EOF in this source, placing them into
buf
. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
Reads all bytes until EOF in this source, appending them to
buf
. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
Reads the exact number of bytes required to fill
buf
. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
🔬This is a nightly-only experimental API. (
read_buf
)Pull some bytes from this source into the specified buffer. Read more
Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
🔬This is a nightly-only experimental API. (
read_buf
)Reads the exact number of bytes required to fill
cursor
. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Creates a “by reference” adaptor for this instance of
Read
. Read moreSource§impl Write for Device
impl Write for Device
Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Writes a buffer into this writer, returning how many bytes were written. Read more
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Flushes this output stream, ensuring that all intermediately buffered
contents reach their destination. Read more
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
🔬This is a nightly-only experimental API. (
can_vector
)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Attempts to write an entire buffer into this writer. Read more
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
🔬This is a nightly-only experimental API. (
write_all_vectored
)Attempts to write multiple buffers into this writer. Read more
Auto Trait Implementations§
impl Freeze for Device
impl RefUnwindSafe for Device
impl !Send for Device
impl !Sync for Device
impl Unpin for Device
impl UnwindSafe for Device
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more