Skip to content

Commit

Permalink
Merge pull request #22 from rabbit-digger/fix-raw-windows
Browse files Browse the repository at this point in the history
Fix raw windows
  • Loading branch information
spacemeowx2 authored Feb 7, 2022
2 parents 0e75fbf + 56096f0 commit c8898d4
Show file tree
Hide file tree
Showing 16 changed files with 221 additions and 92 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: nightly-2021-05-21
toolchain: nightly-2022-01-01
override: true
- name: Run cargo-tarpaulin
uses: actions-rs/[email protected]
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ jobs:
uses: actions/cache@v1
with:
path: ~/.cargo
key: ${{ matrix.os }}-nightly-2021-05-21-cargo-v1
key: ${{ matrix.os }}-nightly-2022-01-01-cargo-v1
restore-keys: |
${{ matrix.os }}-nightly-2021-05-21-cargo-v1
${{ matrix.os }}-nightly-2022-01-01-cargo-v1
- name: Setup toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: nightly-2021-05-21
toolchain: nightly-2022-01-01
target: ${{ matrix.target }}
override: true
- name: Run tests
Expand Down Expand Up @@ -74,13 +74,13 @@ jobs:
uses: actions/cache@v1
with:
path: ~/.cargo
key: ${{ matrix.target }}-nightly-2021-05-21-cargo-v1
key: ${{ matrix.target }}-nightly-2022-01-01-cargo-v1
restore-keys: |
${{ matrix.target }}-nightly-2021-05-21-cargo-v1
${{ matrix.target }}-nightly-2022-01-01-cargo-v1
- name: Setup toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: nightly-2021-05-21
toolchain: nightly-2022-01-01
target: ${{ matrix.target }}
override: true
- name: Build release
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/update_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ jobs:
uses: actions/cache@v1
with:
path: ~/.cargo
key: ${{ matrix.os }}-nightly-2021-05-21-cargo-v1
key: ${{ matrix.os }}-nightly-2022-01-01-cargo-v1
restore-keys: |
${{ matrix.os }}-nightly-2021-05-21-cargo-v1
${{ matrix.os }}-nightly-2022-01-01-cargo-v1
- name: Setup toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: nightly-2021-05-21
toolchain: nightly-2022-01-01
target: ${{ matrix.target }}
override: true
- name: Generate schema
Expand Down
12 changes: 0 additions & 12 deletions protocol/obfs/src/http_simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,6 @@ impl AsyncWrite for Connect {
) -> Poll<Result<(), io::Error>> {
Pin::new(&mut self.inner).poll_shutdown(cx)
}

fn poll_write_vectored(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[io::IoSlice<'_>],
) -> Poll<Result<usize, io::Error>> {
Pin::new(&mut self.inner).poll_write_vectored(cx, bufs)
}

fn is_write_vectored(&self) -> bool {
self.inner.is_write_vectored()
}
}

#[async_trait]
Expand Down
2 changes: 2 additions & 0 deletions protocol/raw/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ features = ["async"]

[target.'cfg(windows)'.dependencies]
libc = "0.2"
wintun = "0.2"
once_cell = "1.7.2"

[target.'cfg(unix)'.dependencies]
nix = "0.23.1"
13 changes: 12 additions & 1 deletion protocol/raw/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::net::Ipv4Addr;

use rd_interface::{
config::{Config, NetRef},
prelude::*,
};
use tokio_smoltcp::smoltcp::phy::Medium;
use tokio_smoltcp::smoltcp::{phy::Medium, wire::IpCidr};

#[rd_config]
#[serde(rename_all = "lowercase")]
Expand Down Expand Up @@ -46,6 +48,7 @@ impl Default for Layer {
}
}

#[cfg(unix)]
impl From<Layer> for tun_crate::Layer {
fn from(l: Layer) -> Self {
match l {
Expand Down Expand Up @@ -98,3 +101,11 @@ pub struct RawNetConfig {
#[serde(default)]
pub forward: bool,
}

pub struct TunTapSetup {
pub name: Option<String>,
pub addr: Ipv4Addr,
pub destination_addr: IpCidr,
pub mtu: usize,
pub layer: Layer,
}
48 changes: 32 additions & 16 deletions protocol/raw/src/device.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
use std::{net::Ipv4Addr, str::FromStr};
use std::{io, net::Ipv4Addr, str::FromStr};

use crate::config::{DeviceConfig, RawNetConfig, TunTap};
use crate::config::{DeviceConfig, RawNetConfig, TunTap, TunTapSetup};
use boxed::BoxedAsyncDevice;
pub use interface_info::get_interface_info;
use pcap::{Capture, Device};
use rd_interface::{Error, ErrorContext, Result};
use tokio_smoltcp::{
device::AsyncDevice,
smoltcp::wire::{EthernetAddress, IpCidr},
device::{AsyncDevice, DeviceCapabilities},
smoltcp::{
phy::Checksum,
wire::{EthernetAddress, IpCidr},
},
};

mod boxed;
mod interface_info;
#[cfg(unix)]
mod unix;
#[cfg(unix)]
use crate::device::unix::get_tun;

#[cfg(windows)]
mod windows;
#[cfg(windows)]
use crate::device::windows::get_tun;

pub fn get_device(config: &RawNetConfig) -> Result<(EthernetAddress, BoxedAsyncDevice)> {
#[cfg(unix)]
use crate::device::unix::{get_tun, TunTapSetup};
let (ethernet_address, device) = match &config.device {
DeviceConfig::String(dev) => {
let ethernet_address = crate::device::get_interface_info(&dev)
Expand All @@ -44,11 +52,14 @@ pub fn get_device(config: &RawNetConfig) -> Result<(EthernetAddress, BoxedAsyncD
let device = Box::new(get_tun(setup)?);
let ethernet_addr = match cfg.tuntap {
TunTap::Tun => EthernetAddress::BROADCAST,
#[cfg(unix)]
TunTap::Tap => {
crate::device::get_interface_info(&device.name())
.context("Failed to get interface info")?
.ethernet_address
}
#[cfg(windows)]
TunTap::Tap => unreachable!(),
};

(ethernet_addr, BoxedAsyncDevice(device))
Expand Down Expand Up @@ -80,11 +91,7 @@ fn pcap_device_by_name(name: &str) -> Result<Device> {

#[cfg(unix)]
pub fn get_by_device(device: Device) -> Result<impl AsyncDevice> {
use std::io;
use tokio_smoltcp::{
device::{AsyncCapture, DeviceCapabilities},
smoltcp::phy::Checksum,
};
use tokio_smoltcp::device::AsyncCapture;

let cap = Capture::from_device(device.clone())
.context("Failed to capture device")?
Expand Down Expand Up @@ -127,9 +134,9 @@ pub fn get_by_device(device: Device) -> Result<impl AsyncDevice> {
}

#[cfg(windows)]
pub fn get_by_device(device: Device) -> Result<impl Interface> {
pub fn get_by_device(device: Device) -> Result<impl AsyncDevice> {
use tokio::sync::mpsc::{Receiver, Sender};
use tokio_smoltcp::util::ChannelCapture;
use tokio_smoltcp::device::ChannelCapture;

let mut cap = Capture::from_device(device.clone())
.context("Failed to capture device")?
Expand All @@ -146,7 +153,7 @@ pub fn get_by_device(device: Device) -> Result<impl Interface> {
.open()
.context("Failed to open device")?;

let recv = move |tx: Sender<Vec<u8>>| loop {
let recv = move |tx: Sender<io::Result<Vec<u8>>>| loop {
let p = match cap.next().map(|p| p.to_vec()) {
Ok(p) => p,
Err(pcap::Error::TimeoutExpired) => continue,
Expand All @@ -155,13 +162,22 @@ pub fn get_by_device(device: Device) -> Result<impl Interface> {
break;
}
};
tx.blocking_send(p).unwrap();

tx.blocking_send(Ok(p)).unwrap();
};
let send = move |mut rx: Receiver<Vec<u8>>| {
while let Some(pkt) = rx.blocking_recv() {
send.sendpacket(pkt).unwrap();
}
};
let capture = ChannelCapture::new(recv, send);
let mut caps = DeviceCapabilities::default();
caps.max_transmission_unit = 1500;
caps.checksum.ipv4 = Checksum::Tx;
caps.checksum.tcp = Checksum::Tx;
caps.checksum.udp = Checksum::Tx;
caps.checksum.icmpv4 = Checksum::Tx;
caps.checksum.icmpv6 = Checksum::Tx;

let capture = ChannelCapture::new(recv, send, caps);
Ok(capture)
}
11 changes: 6 additions & 5 deletions protocol/raw/src/device/interface_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ mod unix {

#[cfg(windows)]
mod windows {
use super::{Error, InterfaceInfo};
use super::InterfaceInfo;
use smoltcp::wire::EthernetAddress;
use std::{mem, ptr};
use std::{io, mem, ptr};
use tokio_smoltcp::smoltcp;

const NO_ERROR: u32 = 0;
const ERROR_INSUFFICIENT_BUFFER: u32 = 122;
Expand All @@ -77,7 +78,7 @@ mod windows {
return None;
}

pub fn get_interface_info(name: &str) -> Result<InterfaceInfo, Error> {
pub fn get_interface_info(name: &str) -> io::Result<InterfaceInfo> {
if let Some(intf_guid) = get_guid(name) {
let mut size = 0u32;
let table: *mut MibIftable;
Expand All @@ -96,7 +97,7 @@ mod windows {
{
table = mem::transmute(libc::malloc(size as libc::size_t));
} else {
return Err(Error::NotFound);
return Err(io::ErrorKind::NotFound.into());
}

if GetIfTable(table, &mut size as *mut libc::c_ulong, false) == NO_ERROR {
Expand Down Expand Up @@ -130,7 +131,7 @@ mod windows {
libc::free(mem::transmute(table));
}
}
Err(Error::NotFound)
Err(io::ErrorKind::NotFound.into())
}

pub const MAX_INTERFACE_NAME_LEN: usize = 256;
Expand Down
15 changes: 2 additions & 13 deletions protocol/raw/src/device/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ use std::{
task::{Context, Poll},
};

use crate::config::Layer;
use crate::config::TunTapSetup;
use futures::{ready, Sink, SinkExt, Stream, StreamExt};
use rd_interface::{error::map_other, Result};
use tokio_smoltcp::{
device::{AsyncDevice, DeviceCapabilities, Packet},
smoltcp::{
phy::Checksum,
wire::{IpAddress, IpCidr},
},
smoltcp::{phy::Checksum, wire::IpAddress},
};
use tokio_util::codec::Framed;
use tun_crate::{create_as_async, Configuration, Device, TunPacket, TunPacketCodec};
Expand All @@ -24,14 +21,6 @@ pub struct TunAsyncDevice {
caps: DeviceCapabilities,
}

pub struct TunTapSetup {
pub name: Option<String>,
pub addr: Ipv4Addr,
pub destination_addr: IpCidr,
pub mtu: usize,
pub layer: Layer,
}

pub fn get_tun(cfg: TunTapSetup) -> Result<TunAsyncDevice> {
let mut config = Configuration::default();
let netmask = !0u32 >> (32 - cfg.destination_addr.prefix_len());
Expand Down
Loading

0 comments on commit c8898d4

Please sign in to comment.