Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Legend-Master committed Jun 1, 2024
1 parent b84c813 commit f65f6f1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 31 deletions.
39 changes: 16 additions & 23 deletions client/src/tap_device.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::{io, net::Ipv4Addr};

use macaddr::MacAddr6;
#[cfg(target_os = "linux")]
use nix::ifaddrs::getifaddrs;

pub trait TapDevice {
fn open_or_create(name: &str) -> io::Result<Self>
Expand All @@ -11,10 +9,7 @@ pub trait TapDevice {
fn up(&self) -> io::Result<()>;
fn get_mac(&self) -> io::Result<MacAddr6>;
fn get_mtu(&self) -> io::Result<u32>;
fn set_ip<A, B>(&self, address: A, mask: B) -> io::Result<()>
where
A: Into<Ipv4Addr>,
B: Into<Ipv4Addr>;
fn set_ip(&self, address: impl Into<Ipv4Addr>, mask: impl Into<Ipv4Addr>) -> io::Result<()>;
fn read_non_mut(&self, buf: &mut [u8]) -> io::Result<usize>;
fn write_non_mut(&self, buf: &[u8]) -> io::Result<usize>;
}
Expand All @@ -25,14 +20,13 @@ pub struct Device(tap_windows::Device);
#[cfg(target_os = "windows")]
impl TapDevice for Device {
fn open_or_create(name: &str) -> io::Result<Self> {
let device = tap_windows::Device::open(tap_windows::HARDWARE_ID, name).or_else(
|_| -> std::io::Result<_> {
tap_windows::Device::open(tap_windows::HARDWARE_ID, name)
.or_else(|_| {
let device = tap_windows::Device::create(tap_windows::HARDWARE_ID)?;
device.set_name(name)?;
Ok(device)
},
)?;
Ok(Device(device))
})
.map(Into::into)
}

fn up(&self) -> io::Result<()> {
Expand All @@ -47,11 +41,7 @@ impl TapDevice for Device {
self.0.get_mtu()
}

fn set_ip<A, B>(&self, address: A, mask: B) -> io::Result<()>
where
A: Into<Ipv4Addr>,
B: Into<Ipv4Addr>,
{
fn set_ip(&self, address: impl Into<Ipv4Addr>, mask: impl Into<Ipv4Addr>) -> io::Result<()> {
self.0.set_ip(address, mask)
}

Expand All @@ -64,6 +54,13 @@ impl TapDevice for Device {
}
}

#[cfg(target_os = "windows")]
impl From<tap_windows::Device> for Device {
fn from(device: tap_windows::Device) -> Self {
Self(device)
}
}

#[cfg(target_os = "linux")]
fn ip_command(args: &[&str]) -> Result<(), io::Error> {
use std::process::Command;
Expand All @@ -86,7 +83,7 @@ impl TapDevice for Device {
}

fn get_mac(&self) -> io::Result<MacAddr6> {
for interface in getifaddrs()? {
for interface in nix::ifaddrs::getifaddrs()? {
if interface.interface_name != self.0.name() {
continue;
}
Expand All @@ -103,11 +100,7 @@ impl TapDevice for Device {
return Ok(1500);
}

fn set_ip<A, B>(&self, address: A, mask: B) -> io::Result<()>
where
A: Into<Ipv4Addr>,
B: Into<Ipv4Addr>,
{
fn set_ip(&self, address: impl Into<Ipv4Addr>, mask: impl Into<Ipv4Addr>) -> io::Result<()> {
let address: Ipv4Addr = address.into();
let mask: Ipv4Addr = mask.into();

Expand Down Expand Up @@ -146,5 +139,5 @@ pub fn setup_tap() -> Device {
// Try to open_or_create the device
let tap_device = Device::open_or_create(INTERFACE_NAME).expect("Failed to open or create TAP");
tap_device.up().expect("Failed to turn on TAP");
return tap_device;
tap_device
}
11 changes: 3 additions & 8 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,13 @@ fn forward_data(
connections: &Mutex<HashMap<MacAddr6, Connection>>,
) {
if let Ok((source_mac_address, destination_mac_address)) = get_mac_addresses(&ethernet_frame) {
let message = &Message::Data { ethernet_frame };
let send = |connection: &Connection| {
// log!(
// "Forwarding to {} ({})",
// &connection.socket_address, &connection.ip
// );
send_to(
socket,
&Message::Data {
ethernet_frame: ethernet_frame.clone(),
},
&connection.socket_address,
);
send_to(socket, message, &connection.socket_address);
};
// Broadcast is a special type of multicast
if destination_mac_address.is_multicast() {
Expand Down Expand Up @@ -237,6 +232,6 @@ fn purge_timedout_connections(
connection.socket_address
);
}
return should_keep;
should_keep
});
}

0 comments on commit f65f6f1

Please sign in to comment.