Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macOS support #18

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
383 changes: 232 additions & 151 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ version = "0.5.0"
[target.'cfg(unix)'.dependencies]
libc = "^0.2"

[target.'cfg(target_os = "macos")'.dependencies]
ioctl-sys = "0.6"
page_size = "0.4"
nix = "0.20"

[dependencies.x25519-dalek]
version = "^1.1"

Expand Down
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

extern crate alloc;

#[cfg(target_os = "macos")]
extern crate ioctl_sys;
#[cfg(target_os = "macos")]
extern crate page_size;

#[cfg(feature = "profiler")]
extern crate cpuprofiler;

Expand Down
3 changes: 1 addition & 2 deletions src/platform/linux/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
mod tun;
mod uapi;
mod udp;

pub use crate::platform::unix::uapi::UnixUAPI as UAPI;
pub use tun::LinuxTun as Tun;
pub use uapi::LinuxUAPI as UAPI;
pub use udp::LinuxUDP as UDP;
2 changes: 1 addition & 1 deletion src/platform/linux/uapi.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::super::uapi::*;
use crate::platform::unix::UnixAPI;

use std::fs;
use std::io;
Expand Down
44 changes: 44 additions & 0 deletions src/platform/macos/fd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use std::{io, os::unix::io::RawFd, sync::Arc};

struct FdInner {
fd: RawFd,
}

impl Drop for FdInner {
fn drop(&mut self) {
unsafe { libc::close(self.fd) };
}
}

#[derive(Clone)]
pub(super) struct Fd {
fd: Arc<FdInner>,
}

impl Fd {
pub fn new(fd: RawFd) -> Self {
Self {
fd: Arc::new(FdInner { fd }),
}
}

pub unsafe fn raw_fd(&self) -> RawFd {
self.fd.fd
}

pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
let bytes_read = unsafe { libc::write(self.raw_fd(), buf.as_ptr() as _, buf.len()) };
if bytes_read < 0 {
return Err(io::Error::from_raw_os_error(-bytes_read as i32));
}
Ok(bytes_read as usize)
}

pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
let bytes_written = unsafe { libc::read(self.raw_fd(), buf.as_mut_ptr() as _, buf.len()) };
if bytes_written < 0 {
return Err(io::Error::last_os_error());
}
Ok(bytes_written as usize)
}
}
8 changes: 8 additions & 0 deletions src/platform/macos/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
mod fd;
mod sys;
mod tun;
mod udp;

pub use crate::platform::unix::uapi::UnixUAPI as UAPI;
pub use tun::MacosTun as Tun;
pub use udp::MacosUDP as UDP;
38 changes: 38 additions & 0 deletions src/platform/macos/sys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#![allow(non_camel_case_types)]
#[repr(C)]
pub struct ctl_info {
pub ctl_id: u32,
pub ctl_name: [u8; 96],
}
ioctl_sys::ioctl!(readwrite ctliocginfo with 'N', 3; ctl_info);

#[repr(C)]
pub struct rt_msghdr {
pub rtm_msglen: u16,
pub rtm_version: u8,
pub rtm_type: u8,
pub rtm_index: u16,
pub rtm_flags: i32,
pub rtm_addrs: i32,
pub rtm_pid: libc::pid_t,
pub rtm_seq: i32,
pub rtm_errno: i32,
pub rtm_use: i32,
pub rtm_inits: u32,
pub rtm_rmx: rt_metrics,
}

#[repr(C)]
pub struct rt_metrics {
pub rmx_locks: u32, /* Kernel must leave these values alone */
pub rmx_mtu: u32, /* MTU for this path */
pub rmx_hopcount: u32, /* max hops expected */
pub rmx_expire: i32, /* lifetime for route, e.g. redirect */
pub rmx_recvpipe: u32, /* inbound delay-bandwidth product */
pub rmx_sendpipe: u32, /* outbound delay-bandwidth product */
pub rmx_ssthresh: u32, /* outbound gateway buffer limit */
pub rmx_rtt: u32, /* estimated round trip time */
pub rmx_rttvar: u32, /* estimated rtt variance */
pub rmx_pksent: u32, /* packets sent using this route */
pub rmx_filler: [u32; 4], /* will be used for T/TCP later */
}
Loading