Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.rs]
indent_size = 4
max_line_length = 100

[*.md]
max_line_length = 80
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
- run: cargo fmt --all -- --check
- run: cargo build --workspace --all-targets --locked
- run: cargo test --workspace --all-targets --locked
9 changes: 9 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
{
"editor.formatOnSave": true,
"editor.rulers": [
100
],
"[rust]": {
"editor.defaultFormatter": "rust-lang.rust-analyzer"
},
"files.insertFinalNewline": true,
"files.trimTrailingWhitespace": true,
"cSpell.words": [
"Kademlia",
"multiaddress"
Expand Down
2 changes: 2 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
max_width = 100
use_small_heuristics = "Max"
4 changes: 2 additions & 2 deletions virtual_network_core/src/driver.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::net::Ipv4Addr;
use async_trait::async_trait;
use std::net::Ipv4Addr;

#[async_trait]
pub trait DeviceDriver: Send + Sync {
Expand All @@ -12,4 +12,4 @@ pub trait DeviceDriver: Send + Sync {

/// Returns the local node's virtual IPv4 address.
fn virtual_ipv4(&self) -> Ipv4Addr;
}
}
6 changes: 3 additions & 3 deletions virtual_network_core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod mesh;
pub mod driver;
pub mod router;
pub mod packet;
pub mod mesh;
pub mod network;
pub mod packet;
pub mod router;

pub const RECV_BUFFER_SIZE: usize = 65535;
10 changes: 6 additions & 4 deletions virtual_network_core/src/mesh.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use async_trait::async_trait;
use std::net::Ipv4Addr;
use std::sync::Arc;
use async_trait::async_trait;
use tokio::sync::mpsc;

#[async_trait]
Expand Down Expand Up @@ -30,7 +30,7 @@ pub struct MeshConfig {
pub virtual_ipv4: Ipv4Addr,
}

pub enum MeshEvent<MeshPeerLink : PeerLink> {
pub enum MeshEvent<MeshPeerLink: PeerLink> {
/// A peer was authenticated and can be routed to.
PeerFound(Arc<MeshPeerLink>),

Expand All @@ -45,7 +45,9 @@ pub trait Mesh {
///
/// Returns the mesh and the receiver for its events. Dropping the mesh leaves the network.
/// Must be called from within a tokio runtime.
fn join(config: MeshConfig) -> anyhow::Result<(Self, mpsc::Receiver<MeshEvent<Self::MeshPeerLink>>)>
fn join(
config: MeshConfig,
) -> anyhow::Result<(Self, mpsc::Receiver<MeshEvent<Self::MeshPeerLink>>)>
where
Self: Sized;
}
}
5 changes: 1 addition & 4 deletions virtual_network_core/src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ pub fn parse_ipv4(packet: &[u8]) -> Option<Ipv4Header> {
return None;
}

Some(Ipv4Header {
source: header.source_addr(),
destination: header.destination_addr(),
})
Some(Ipv4Header { source: header.source_addr(), destination: header.destination_addr() })
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion virtual_network_driver/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod tun;
pub mod mock;
pub mod tun;
30 changes: 7 additions & 23 deletions virtual_network_driver/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,18 @@ impl MockDriver {
let (outbound_tx, outbound_rx) = mpsc::unbounded_channel();
let (inbound_tx, inbound_rx) = mpsc::unbounded_channel();

let driver = MockDriver {
virtual_ipv4,
outbound: Mutex::new(outbound_rx),
inbound: inbound_tx,
};
let handle = MockHandle {
outbound: outbound_tx,
inbound: inbound_rx,
};
let driver =
MockDriver { virtual_ipv4, outbound: Mutex::new(outbound_rx), inbound: inbound_tx };
let handle = MockHandle { outbound: outbound_tx, inbound: inbound_rx };
(driver, handle)
}
}

#[async_trait]
impl DeviceDriver for MockDriver {
async fn recv(&self, buf: &mut [u8; RECV_BUFFER_SIZE]) -> anyhow::Result<usize> {
let packet = self
.outbound
.lock()
.await
.recv()
.await
.context("mock driver handle was dropped")?;
let packet =
self.outbound.lock().await.recv().await.context("mock driver handle was dropped")?;

// `MockHandle::inject` should reject packets that don't fit.
debug_assert!(packet.len() <= buf.len());
Expand All @@ -66,9 +55,7 @@ impl DeviceDriver for MockDriver {
}

async fn send(&self, packet: &[u8]) -> anyhow::Result<()> {
self.inbound
.send(packet.to_vec())
.context("mock driver handle was dropped")
self.inbound.send(packet.to_vec()).context("mock driver handle was dropped")
}

fn virtual_ipv4(&self) -> Ipv4Addr {
Expand All @@ -90,10 +77,7 @@ impl MockHandle {
);
}

self.outbound
.send(packet)
.ok()
.context("mock driver was dropped")
self.outbound.send(packet).ok().context("mock driver was dropped")
}

/// Waits for the next packet the driver delivered to the OS.
Expand Down
29 changes: 9 additions & 20 deletions virtual_network_driver/src/tun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ impl TunDriver {
///
/// The kernel routes every destination inside `network` into this
/// interface.
pub fn new(name: &str, ipv4_address: Ipv4Addr, network: &VirtualNetwork) -> anyhow::Result<Self> {
pub fn new(
name: &str,
ipv4_address: Ipv4Addr,
network: &VirtualNetwork,
) -> anyhow::Result<Self> {
ensure!(
!name.is_empty() && name.len() <= MAX_IFNAME_LEN,
"interface name {name:?} must be 1..={MAX_IFNAME_LEN} bytes"
Expand Down Expand Up @@ -67,11 +71,7 @@ impl DeviceDriver for TunDriver {
// on the interface; the trait only promises IPv4, so skip anything
// that isn't a valid IPv4 packet.
loop {
let len = self
.device
.recv(buf)
.await
.context("failed to read from TUN interface")?;
let len = self.device.recv(buf).await.context("failed to read from TUN interface")?;

if parse_ipv4(&buf[..len]).is_some() {
return Ok(len);
Expand All @@ -83,21 +83,10 @@ impl DeviceDriver for TunDriver {
// Without packet info, Linux picks the protocol from the version
// nibble, so this is what stops a peer from injecting IPv6 or
// malformed packets.
ensure!(
parse_ipv4(packet).is_some(),
"refusing to write an invalid IPv4 packet to TUN"
);
ensure!(parse_ipv4(packet).is_some(), "refusing to write an invalid IPv4 packet to TUN");

let written = self
.device
.send(packet)
.await
.context("failed to write to TUN interface")?;
ensure!(
written == packet.len(),
"short write to TUN: {written}/{} bytes",
packet.len()
);
let written = self.device.send(packet).await.context("failed to write to TUN interface")?;
ensure!(written == packet.len(), "short write to TUN: {written}/{} bytes", packet.len());
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion virtual_network_mesh/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod mesh;
mod link;
mod mesh;

pub use mesh::Mesh;
4 changes: 1 addition & 3 deletions virtual_network_mesh/src/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use std::net::Ipv4Addr;
use async_trait::async_trait;
use virtual_network_core::RECV_BUFFER_SIZE;

pub struct PeerLink {

}
pub struct PeerLink {}

#[async_trait]
impl virtual_network_core::mesh::PeerLink for PeerLink {
Expand Down
8 changes: 5 additions & 3 deletions virtual_network_mesh/src/mesh.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::link;
use tokio::sync::{mpsc, oneshot};
use virtual_network_core::{mesh::MeshEvent};
use virtual_network_core::mesh::MeshConfig;
use crate::link;
use virtual_network_core::mesh::MeshEvent;

/// A joined network. Dropping it leaves the network.
pub struct Mesh {
Expand All @@ -12,7 +12,9 @@ pub struct Mesh {
impl virtual_network_core::mesh::Mesh for Mesh {
type MeshPeerLink = link::PeerLink;

fn join(config: MeshConfig) -> anyhow::Result<(Self, mpsc::Receiver<MeshEvent<link::PeerLink>>)> {
fn join(
config: MeshConfig,
) -> anyhow::Result<(Self, mpsc::Receiver<MeshEvent<link::PeerLink>>)> {
todo!()
}
}
Expand Down
2 changes: 1 addition & 1 deletion virtual_network_router/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
mod router;
mod router;
6 changes: 2 additions & 4 deletions virtual_network_router/src/router.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::sync::Arc;
use virtual_network_core::mesh::PeerLink;

struct Router {

}
struct Router {}

impl Router {
fn new() -> Self {
Expand All @@ -19,4 +17,4 @@ impl virtual_network_core::router::Router for Router {
fn remove_peer_link(&self, link: Arc<dyn PeerLink>) {
todo!()
}
}
}
Loading