diff --git a/Cargo.lock b/Cargo.lock index 08eb709..8ba3c8d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -180,7 +180,7 @@ dependencies = [ "structopt", "tempfile", "ureq", - "wgctrl", + "wireguard-control", ] [[package]] @@ -964,9 +964,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.67" +version = "1.0.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7f9e390c27c3c0ce8bc5d725f6e4d30a29d26659494aa4b17535f7522c5c950" +checksum = "0f690853975602e1bfe1ccbf50504d67174e3bcf340f23b5ea9992e0587a52d8" dependencies = [ "itoa", "ryu", @@ -1003,7 +1003,7 @@ dependencies = [ "tokio", "toml", "url", - "wgctrl", + "wireguard-control", ] [[package]] @@ -1029,8 +1029,8 @@ dependencies = [ "structopt", "toml", "url", - "wgctrl", - "wgctrl-sys", + "wireguard-control", + "wireguard-control-sys", ] [[package]] @@ -1358,27 +1358,6 @@ version = "0.10.2+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" -[[package]] -name = "wgctrl" -version = "1.5.0-beta.4" -dependencies = [ - "base64", - "curve25519-dalek", - "hex", - "libc", - "rand_core", - "wgctrl-sys", -] - -[[package]] -name = "wgctrl-sys" -version = "1.5.0-beta.4" -dependencies = [ - "bindgen", - "cc", - "libc", -] - [[package]] name = "winapi" version = "0.3.9" @@ -1410,6 +1389,27 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "wireguard-control" +version = "1.5.0-beta.4" +dependencies = [ + "base64", + "curve25519-dalek", + "hex", + "libc", + "rand_core", + "wireguard-control-sys", +] + +[[package]] +name = "wireguard-control-sys" +version = "1.5.0-beta.4" +dependencies = [ + "bindgen", + "cc", + "libc", +] + [[package]] name = "wyz" version = "0.2.0" diff --git a/client/Cargo.toml b/client/Cargo.toml index 3d41912..d4db095 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -28,7 +28,7 @@ serde_json = "1.0" shared = { path = "../shared", default-features = false } structopt = "0.3" ureq = { version = "2", default-features = false, features = ["json"] } -wgctrl = { path = "../wgctrl-rs" } +wireguard-control = { path = "../wireguard-control" } [dev-dependencies] tempfile = "3" diff --git a/client/src/data_store.rs b/client/src/data_store.rs index d36a9f1..a817d82 100644 --- a/client/src/data_store.rs +++ b/client/src/data_store.rs @@ -9,7 +9,7 @@ use std::{ io::{self, Read, Seek, SeekFrom, Write}, path::{Path, PathBuf}, }; -use wgctrl::InterfaceName; +use wireguard_control::InterfaceName; #[derive(Debug)] pub struct DataStore { diff --git a/client/src/main.rs b/client/src/main.rs index fc695bc..6488e82 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -21,7 +21,7 @@ use std::{ time::Duration, }; use structopt::{clap::AppSettings, StructOpt}; -use wgctrl::{Device, DeviceUpdate, InterfaceName, PeerConfigBuilder, PeerInfo}; +use wireguard_control::{Device, DeviceUpdate, InterfaceName, PeerConfigBuilder, PeerInfo}; mod data_store; mod nat; @@ -428,7 +428,7 @@ fn redeem_invite( .with_str(iface.to_string())?; log::info!("Generating new keypair."); - let keypair = wgctrl::KeyPair::generate(); + let keypair = wireguard_control::KeyPair::generate(); log::info!( "Registering keypair with server (at {}).", diff --git a/client/src/nat.rs b/client/src/nat.rs index ed1979e..25d0714 100644 --- a/client/src/nat.rs +++ b/client/src/nat.rs @@ -10,7 +10,7 @@ use shared::{ wg::{DeviceExt, PeerInfoExt}, Endpoint, Peer, PeerDiff, }; -use wgctrl::{Backend, Device, DeviceUpdate, InterfaceName, Key, PeerConfigBuilder}; +use wireguard_control::{Backend, Device, DeviceUpdate, InterfaceName, Key, PeerConfigBuilder}; const STEP_INTERVAL: Duration = Duration::from_secs(5); diff --git a/server/Cargo.toml b/server/Cargo.toml index 8230b15..2621a42 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -40,7 +40,7 @@ thiserror = "1" tokio = { version = "1", features = ["macros", "rt-multi-thread", "time"] } toml = "0.5" url = "2" -wgctrl = { path = "../wgctrl-rs" } +wireguard-control = { path = "../wireguard-control" } [target.'cfg(target_os = "linux")'.dependencies] socket2 = { version = "0.4", features = ["all"] } diff --git a/server/src/api/admin/peer.rs b/server/src/api/admin/peer.rs index 11c7856..25c3003 100644 --- a/server/src/api/admin/peer.rs +++ b/server/src/api/admin/peer.rs @@ -8,7 +8,7 @@ use crate::{ }; use hyper::{Body, Method, Request, Response, StatusCode}; use shared::PeerContents; -use wgctrl::DeviceUpdate; +use wireguard_control::DeviceUpdate; pub async fn routes( req: Request, diff --git a/server/src/api/user.rs b/server/src/api/user.rs index 99509b8..a6a8200 100644 --- a/server/src/api/user.rs +++ b/server/src/api/user.rs @@ -8,7 +8,7 @@ use crate::{ }; use hyper::{Body, Method, Request, Response, StatusCode}; use shared::{EndpointContents, PeerContents, RedeemContents, State, REDEEM_TRANSITION_WAIT}; -use wgctrl::DeviceUpdate; +use wireguard_control::DeviceUpdate; pub async fn routes( req: Request, @@ -86,7 +86,7 @@ mod handlers { let conn = session.context.db.lock(); let mut selected_peer = DatabasePeer::get(&conn, session.peer.id)?; - let old_public_key = wgctrl::Key::from_base64(&selected_peer.public_key) + let old_public_key = wireguard_control::Key::from_base64(&selected_peer.public_key) .map_err(|_| ServerError::WireGuard)?; selected_peer.redeem(&conn, &form.public_key)?; diff --git a/server/src/initialize.rs b/server/src/initialize.rs index 3695475..6c553d4 100644 --- a/server/src/initialize.rs +++ b/server/src/initialize.rs @@ -6,7 +6,7 @@ use indoc::printdoc; use publicip::Preference; use rusqlite::{params, Connection}; use shared::{prompts, CidrContents, Endpoint, PeerContents, PERSISTENT_KEEPALIVE_INTERVAL_SECS}; -use wgctrl::KeyPair; +use wireguard_control::KeyPair; fn create_database>( database_path: P, diff --git a/server/src/main.rs b/server/src/main.rs index 4ae602b..45ca4cd 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -25,7 +25,7 @@ use std::{ }; use structopt::{clap::AppSettings, StructOpt}; use subtle::ConstantTimeEq; -use wgctrl::{Backend, Device, DeviceUpdate, InterfaceName, Key, PeerConfigBuilder}; +use wireguard_control::{Backend, Device, DeviceUpdate, InterfaceName, Key, PeerConfigBuilder}; pub mod api; pub mod db; @@ -523,7 +523,7 @@ async fn serve( num_candidates ); - let public_key = wgctrl::Key::from_base64(&config.private_key)?.generate_public(); + let public_key = wireguard_control::Key::from_base64(&config.private_key)?.generate_public(); let db = Arc::new(Mutex::new(conn)); let endpoints = spawn_endpoint_refresher(interface, network); spawn_expired_invite_sweeper(db.clone()); diff --git a/server/src/test.rs b/server/src/test.rs index cff4325..c293a5e 100644 --- a/server/src/test.rs +++ b/server/src/test.rs @@ -12,7 +12,7 @@ use serde::Serialize; use shared::{Cidr, CidrContents, Error, PeerContents}; use std::{collections::HashMap, net::SocketAddr, path::PathBuf, sync::Arc}; use tempfile::TempDir; -use wgctrl::{Backend, InterfaceName, Key, KeyPair}; +use wireguard_control::{Backend, InterfaceName, Key, KeyPair}; #[cfg(not(feature = "v6-test"))] mod v4 { diff --git a/shared/Cargo.toml b/shared/Cargo.toml index de485f1..8b5c3cd 100644 --- a/shared/Cargo.toml +++ b/shared/Cargo.toml @@ -22,13 +22,13 @@ serde = { version = "1", features = ["derive"] } structopt = "0.3" toml = "0.5" url = "2" -wgctrl = { path = "../wgctrl-rs" } +wireguard-control = { path = "../wireguard-control" } [target.'cfg(target_os = "linux")'.dependencies] netlink-sys = "0.7" netlink-packet-core = "0.2" netlink-packet-route = "0.7" -wgctrl-sys = { path = "../wgctrl-sys" } +wireguard-control-sys = { path = "../wireguard-control-sys" } [target.'cfg(target_os = "macos")'.dependencies] nix = "0.22" diff --git a/shared/src/interface_config.rs b/shared/src/interface_config.rs index b042d1d..9b79128 100644 --- a/shared/src/interface_config.rs +++ b/shared/src/interface_config.rs @@ -10,7 +10,7 @@ use std::{ net::SocketAddr, path::{Path, PathBuf}, }; -use wgctrl::InterfaceName; +use wireguard_control::InterfaceName; #[derive(Clone, Deserialize, Serialize, Debug)] #[serde(rename_all = "kebab-case")] @@ -133,7 +133,7 @@ impl InterfaceConfig { impl InterfaceInfo { pub fn public_key(&self) -> Result { - Ok(wgctrl::Key::from_base64(&self.private_key)? + Ok(wireguard_control::Key::from_base64(&self.private_key)? .generate_public() .to_base64()) } diff --git a/shared/src/netlink.rs b/shared/src/netlink.rs index d85d011..58a2802 100644 --- a/shared/src/netlink.rs +++ b/shared/src/netlink.rs @@ -11,7 +11,7 @@ use netlink_packet_route::{ }; use netlink_sys::{protocols::NETLINK_ROUTE, Socket, SocketAddr}; use std::{io, net::IpAddr}; -use wgctrl::InterfaceName; +use wireguard_control::InterfaceName; fn if_nametoindex(interface: &InterfaceName) -> Result { match unsafe { libc::if_nametoindex(interface.as_ptr()) } { diff --git a/shared/src/prompts.rs b/shared/src/prompts.rs index 27d3d61..bfcb635 100644 --- a/shared/src/prompts.rs +++ b/shared/src/prompts.rs @@ -17,7 +17,7 @@ use std::{ str::FromStr, time::SystemTime, }; -use wgctrl::{InterfaceName, KeyPair}; +use wireguard_control::{InterfaceName, KeyPair}; lazy_static! { pub static ref THEME: ColorfulTheme = ColorfulTheme::default(); diff --git a/shared/src/types.rs b/shared/src/types.rs index b4ce30f..e8cdaa5 100644 --- a/shared/src/types.rs +++ b/shared/src/types.rs @@ -15,7 +15,7 @@ use std::{ }; use structopt::StructOpt; use url::Host; -use wgctrl::{ +use wireguard_control::{ AllowedIp, Backend, InterfaceName, InvalidInterfaceName, Key, PeerConfig, PeerConfigBuilder, PeerInfo, }; @@ -753,7 +753,7 @@ impl std::error::Error for WrappedIoError {} mod tests { use super::*; use std::net::IpAddr; - use wgctrl::{Key, PeerConfigBuilder, PeerStats}; + use wireguard_control::{Key, PeerConfigBuilder, PeerStats}; #[test] fn test_peer_no_diff() { diff --git a/shared/src/wg.rs b/shared/src/wg.rs index e44367e..82ba8ee 100644 --- a/shared/src/wg.rs +++ b/shared/src/wg.rs @@ -5,7 +5,9 @@ use std::{ net::{IpAddr, SocketAddr}, time::Duration, }; -use wgctrl::{Backend, Device, DeviceUpdate, InterfaceName, Key, PeerConfigBuilder, PeerInfo}; +use wireguard_control::{ + Backend, Device, DeviceUpdate, InterfaceName, Key, PeerConfigBuilder, PeerInfo, +}; #[cfg(target_os = "macos")] fn cmd(bin: &str, args: &[&str]) -> Result { @@ -31,7 +33,7 @@ fn cmd(bin: &str, args: &[&str]) -> Result { #[cfg(target_os = "macos")] pub fn set_addr(interface: &InterfaceName, addr: IpNetwork) -> Result<(), io::Error> { - let real_interface = wgctrl::backends::userspace::resolve_tun(interface)?; + let real_interface = wireguard_control::backends::userspace::resolve_tun(interface)?; if addr.is_ipv4() { cmd( @@ -56,7 +58,7 @@ pub fn set_addr(interface: &InterfaceName, addr: IpNetwork) -> Result<(), io::Er #[cfg(target_os = "macos")] pub fn set_up(interface: &InterfaceName, mtu: u32) -> Result<(), io::Error> { - let real_interface = wgctrl::backends::userspace::resolve_tun(interface)?; + let real_interface = wireguard_control::backends::userspace::resolve_tun(interface)?; cmd("ifconfig", &[&real_interface, "mtu", &mtu.to_string()])?; Ok(()) } @@ -78,23 +80,24 @@ pub fn up( let mut device = DeviceUpdate::new(); if let Some((public_key, address, endpoint)) = peer { let prefix = if address.is_ipv4() { 32 } else { 128 }; - let peer_config = - PeerConfigBuilder::new(&wgctrl::Key::from_base64(public_key).map_err(|_| { + let peer_config = PeerConfigBuilder::new( + &wireguard_control::Key::from_base64(public_key).map_err(|_| { io::Error::new( io::ErrorKind::InvalidInput, "failed to parse base64 public key", ) - })?) - .add_allowed_ip(address, prefix) - .set_persistent_keepalive_interval(25) - .set_endpoint(endpoint); + })?, + ) + .add_allowed_ip(address, prefix) + .set_persistent_keepalive_interval(25) + .set_endpoint(endpoint); device = device.add_peer(peer_config); } if let Some(listen_port) = listen_port { device = device.set_listen_port(listen_port); } device - .set_private_key(wgctrl::Key::from_base64(private_key).unwrap()) + .set_private_key(wireguard_control::Key::from_base64(private_key).unwrap()) .apply(interface, network.backend)?; set_addr(interface, address)?; set_up( @@ -137,7 +140,7 @@ pub fn down(interface: &InterfaceName, backend: Backend) -> Result<(), Error> { /// true if the route was changed, false if the route already exists. #[cfg(target_os = "macos")] pub fn add_route(interface: &InterfaceName, cidr: IpNetwork) -> Result { - let real_interface = wgctrl::backends::userspace::resolve_tun(interface)?; + let real_interface = wireguard_control::backends::userspace::resolve_tun(interface)?; let output = cmd( "route", &[ diff --git a/wgctrl-rs/.gitignore b/wireguard-control-sys/.gitignore similarity index 100% rename from wgctrl-rs/.gitignore rename to wireguard-control-sys/.gitignore diff --git a/wgctrl-sys/Cargo.toml b/wireguard-control-sys/Cargo.toml similarity index 77% rename from wgctrl-sys/Cargo.toml rename to wireguard-control-sys/Cargo.toml index 05652a8..512bf56 100644 --- a/wgctrl-sys/Cargo.toml +++ b/wireguard-control-sys/Cargo.toml @@ -3,10 +3,9 @@ authors = ["K900 ", "Jake McGinty "] categories = ["external-ffi-bindings", "os::unix-apis"] description = "Raw bindings to the WireGuard embeddable C library" license = "LGPL-2.1-or-later" -name = "wgctrl-sys" -publish = false -readme = "../README.md" -repository = "https://gitlab.com/K900/wgctrl-rs" +name = "wireguard-control-sys" +readme = "README.md" +repository = "https://github.com/tonarino/innernet" version = "1.5.0-beta.4" [dependencies] diff --git a/wireguard-control-sys/README.md b/wireguard-control-sys/README.md new file mode 100644 index 0000000..bd0017d --- /dev/null +++ b/wireguard-control-sys/README.md @@ -0,0 +1,5 @@ +# `wireguard-control-sys` + +A low-level FFI around the [`embaddable-wg-library`](https://git.zx2c4.com/wireguard-tools/tree/contrib/embeddable-wg-library) WireGuard C library, which in turn communicates with the Linux kernel WireGuard via Netlink. + +You *probably* want to use the [`wireguard-control`](https://crates.io/crates/wireguard-control) crate instead. diff --git a/wgctrl-sys/build.rs b/wireguard-control-sys/build.rs similarity index 100% rename from wgctrl-sys/build.rs rename to wireguard-control-sys/build.rs diff --git a/wgctrl-sys/c/wireguard.c b/wireguard-control-sys/c/wireguard.c similarity index 100% rename from wgctrl-sys/c/wireguard.c rename to wireguard-control-sys/c/wireguard.c diff --git a/wgctrl-sys/c/wireguard.h b/wireguard-control-sys/c/wireguard.h similarity index 100% rename from wgctrl-sys/c/wireguard.h rename to wireguard-control-sys/c/wireguard.h diff --git a/wgctrl-sys/src/lib.rs b/wireguard-control-sys/src/lib.rs similarity index 100% rename from wgctrl-sys/src/lib.rs rename to wireguard-control-sys/src/lib.rs diff --git a/wgctrl-sys/.gitignore b/wireguard-control/.gitignore similarity index 100% rename from wgctrl-sys/.gitignore rename to wireguard-control/.gitignore diff --git a/wgctrl-rs/Cargo.toml b/wireguard-control/Cargo.toml similarity index 61% rename from wgctrl-rs/Cargo.toml rename to wireguard-control/Cargo.toml index f7a95de..5ea7475 100644 --- a/wgctrl-rs/Cargo.toml +++ b/wireguard-control/Cargo.toml @@ -1,12 +1,12 @@ [package] -authors = ["K900 "] +authors = ["K900 ", "Jake McGinty "] categories = ["os::unix-apis"] description = "High level bindings to the WireGuard embeddable C library" edition = "2018" license = "LGPL-2.1-or-later" -name = "wgctrl" -publish = false -readme = "../README.md" +name = "wireguard-control" +readme = "README.md" +repository = "https://github.com/tonarino/innernet" version = "1.5.0-beta.4" [dependencies] @@ -15,7 +15,7 @@ hex = "0.4" libc = "0.2" [target.'cfg(target_os = "linux")'.dependencies] -wgctrl-sys = { path = "../wgctrl-sys" } +wireguard-control-sys = { path = "../wireguard-control-sys", version = "1.5.0-beta.1" } [target.'cfg(not(target_os = "linux"))'.dependencies] rand_core = "0.6" diff --git a/wireguard-control/README.md b/wireguard-control/README.md new file mode 100644 index 0000000..bf19bad --- /dev/null +++ b/wireguard-control/README.md @@ -0,0 +1,12 @@ +# `wireguard-control` + +A high-level control library to get and set configurations for WireGuard interfaces. + +It's a heavily-modified fork of [K900's `wgctrl-rs`](https://gitlab.com/K900/wgctrl-rs), and was made for [innernet](https://github.com/tonarino/innernet). + +Currently supports: + +* Linux kernel WireGuard implementation +* Userspace WireGuard implementations (tested against `wireguard-go` on macOS and Linux) + +Versioning is held in lockstep with innernet, although this may change in the future. \ No newline at end of file diff --git a/wgctrl-rs/src/backends/kernel.rs b/wireguard-control/src/backends/kernel.rs similarity index 75% rename from wgctrl-rs/src/backends/kernel.rs rename to wireguard-control/src/backends/kernel.rs index a2f6ed2..c574f03 100644 --- a/wgctrl-rs/src/backends/kernel.rs +++ b/wireguard-control/src/backends/kernel.rs @@ -2,7 +2,7 @@ use crate::{ device::AllowedIp, Backend, Device, DeviceUpdate, InterfaceName, InvalidInterfaceName, InvalidKey, PeerConfig, PeerConfigBuilder, PeerInfo, PeerStats, }; -use wgctrl_sys::{timespec64, wg_device_flags as wgdf, wg_peer_flags as wgpf}; +use wireguard_control_sys::{timespec64, wg_device_flags as wgdf, wg_peer_flags as wgpf}; use std::{ ffi::{CStr, CString}, @@ -13,8 +13,8 @@ use std::{ time::{Duration, SystemTime}, }; -impl<'a> From<&'a wgctrl_sys::wg_allowedip> for AllowedIp { - fn from(raw: &wgctrl_sys::wg_allowedip) -> AllowedIp { +impl<'a> From<&'a wireguard_control_sys::wg_allowedip> for AllowedIp { + fn from(raw: &wireguard_control_sys::wg_allowedip) -> AllowedIp { let addr = match i32::from(raw.family) { libc::AF_INET => IpAddr::V4(unsafe { raw.__bindgen_anon_1.ip4.s_addr }.to_be().into()), libc::AF_INET6 => { @@ -30,8 +30,8 @@ impl<'a> From<&'a wgctrl_sys::wg_allowedip> for AllowedIp { } } -impl<'a> From<&'a wgctrl_sys::wg_peer> for PeerInfo { - fn from(raw: &wgctrl_sys::wg_peer) -> PeerInfo { +impl<'a> From<&'a wireguard_control_sys::wg_peer> for PeerInfo { + fn from(raw: &wireguard_control_sys::wg_peer) -> PeerInfo { PeerInfo { config: PeerConfig { public_key: Key::from_raw(raw.public_key), @@ -63,8 +63,8 @@ impl<'a> From<&'a wgctrl_sys::wg_peer> for PeerInfo { } } -impl<'a> From<&'a wgctrl_sys::wg_device> for Device { - fn from(raw: &wgctrl_sys::wg_device) -> Device { +impl<'a> From<&'a wireguard_control_sys::wg_device> for Device { + fn from(raw: &wireguard_control_sys::wg_device) -> Device { // SAFETY: The name string buffer came directly from wgctrl so its NUL terminated. let name = unsafe { InterfaceName::from_wg(raw.name) }; Device { @@ -95,7 +95,7 @@ impl<'a> From<&'a wgctrl_sys::wg_device> for Device { } } -fn parse_peers(dev: &wgctrl_sys::wg_device) -> Vec { +fn parse_peers(dev: &wireguard_control_sys::wg_device) -> Vec { let mut result = Vec::new(); let mut current_peer = dev.first_peer; @@ -118,10 +118,10 @@ fn parse_peers(dev: &wgctrl_sys::wg_device) -> Vec { result } -fn parse_allowed_ips(peer: &wgctrl_sys::wg_peer) -> Vec { +fn parse_allowed_ips(peer: &wireguard_control_sys::wg_peer) -> Vec { let mut result = Vec::new(); - let mut current_ip: *mut wgctrl_sys::wg_allowedip = peer.first_allowedip; + let mut current_ip: *mut wireguard_control_sys::wg_allowedip = peer.first_allowedip; if current_ip.is_null() { return result; @@ -141,7 +141,7 @@ fn parse_allowed_ips(peer: &wgctrl_sys::wg_peer) -> Vec { result } -fn parse_endpoint(endpoint: &wgctrl_sys::wg_peer__bindgen_ty_1) -> Option { +fn parse_endpoint(endpoint: &wireguard_control_sys::wg_peer__bindgen_ty_1) -> Option { let addr = unsafe { endpoint.addr }; match i32::from(addr.sa_family) { libc::AF_INET => { @@ -166,16 +166,19 @@ fn parse_endpoint(endpoint: &wgctrl_sys::wg_peer__bindgen_ty_1) -> Option (*mut wgctrl_sys::wg_allowedip, *mut wgctrl_sys::wg_allowedip) { +) -> ( + *mut wireguard_control_sys::wg_allowedip, + *mut wireguard_control_sys::wg_allowedip, +) { if allowed_ips.is_empty() { return (ptr::null_mut(), ptr::null_mut()); } let mut first_ip = ptr::null_mut(); - let mut last_ip: *mut wgctrl_sys::wg_allowedip = ptr::null_mut(); + let mut last_ip: *mut wireguard_control_sys::wg_allowedip = ptr::null_mut(); for ip in allowed_ips { - let mut wg_allowedip = Box::new(wgctrl_sys::wg_allowedip { + let mut wg_allowedip = Box::new(wireguard_control_sys::wg_allowedip { family: 0, __bindgen_anon_1: Default::default(), cidr: ip.cidr, @@ -202,13 +205,13 @@ fn encode_allowedips( (first_ip, last_ip) } -fn encode_endpoint(endpoint: Option) -> wgctrl_sys::wg_peer__bindgen_ty_1 { +fn encode_endpoint(endpoint: Option) -> wireguard_control_sys::wg_peer__bindgen_ty_1 { match endpoint { Some(SocketAddr::V4(s)) => { - let mut peer = wgctrl_sys::wg_peer__bindgen_ty_1::default(); - peer.addr4 = wgctrl_sys::sockaddr_in { + let mut peer = wireguard_control_sys::wg_peer__bindgen_ty_1::default(); + peer.addr4 = wireguard_control_sys::sockaddr_in { sin_family: libc::AF_INET as u16, - sin_addr: wgctrl_sys::in_addr { + sin_addr: wireguard_control_sys::in_addr { s_addr: u32::from_be((*s.ip()).into()), }, sin_port: u16::to_be(s.port()), @@ -217,35 +220,38 @@ fn encode_endpoint(endpoint: Option) -> wgctrl_sys::wg_peer__bindgen peer }, Some(SocketAddr::V6(s)) => { - let mut peer = wgctrl_sys::wg_peer__bindgen_ty_1::default(); - let in6_addr = wgctrl_sys::in6_addr__bindgen_ty_1 { + let mut peer = wireguard_control_sys::wg_peer__bindgen_ty_1::default(); + let in6_addr = wireguard_control_sys::in6_addr__bindgen_ty_1 { __u6_addr8: s.ip().octets(), }; - peer.addr6 = wgctrl_sys::sockaddr_in6 { + peer.addr6 = wireguard_control_sys::sockaddr_in6 { sin6_family: libc::AF_INET6 as u16, - sin6_addr: wgctrl_sys::in6_addr { __in6_u: in6_addr }, + sin6_addr: wireguard_control_sys::in6_addr { __in6_u: in6_addr }, sin6_port: u16::to_be(s.port()), sin6_flowinfo: 0, sin6_scope_id: 0, }; peer }, - None => wgctrl_sys::wg_peer__bindgen_ty_1::default(), + None => wireguard_control_sys::wg_peer__bindgen_ty_1::default(), } } fn encode_peers( peers: &[PeerConfigBuilder], -) -> (*mut wgctrl_sys::wg_peer, *mut wgctrl_sys::wg_peer) { +) -> ( + *mut wireguard_control_sys::wg_peer, + *mut wireguard_control_sys::wg_peer, +) { let mut first_peer = ptr::null_mut(); - let mut last_peer: *mut wgctrl_sys::wg_peer = ptr::null_mut(); + let mut last_peer: *mut wireguard_control_sys::wg_peer = ptr::null_mut(); for peer in peers { let (first_allowedip, last_allowedip) = encode_allowedips(&peer.allowed_ips); - let mut wg_peer = Box::new(wgctrl_sys::wg_peer { + let mut wg_peer = Box::new(wireguard_control_sys::wg_peer { public_key: peer.public_key.0, - preshared_key: wgctrl_sys::wg_key::default(), + preshared_key: wireguard_control_sys::wg_key::default(), endpoint: encode_endpoint(peer.endpoint), last_handshake_time: timespec64 { tv_sec: 0, @@ -288,7 +294,7 @@ fn encode_peers( } pub fn enumerate() -> Result, io::Error> { - let base = unsafe { wgctrl_sys::wg_list_device_names() }; + let base = unsafe { wireguard_control_sys::wg_list_device_names() }; if base.is_null() { return Err(io::Error::last_os_error()); @@ -323,17 +329,17 @@ pub fn enumerate() -> Result, io::Error> { pub fn apply(builder: &DeviceUpdate, iface: &InterfaceName) -> io::Result<()> { let (first_peer, last_peer) = encode_peers(&builder.peers); - let result = unsafe { wgctrl_sys::wg_add_device(iface.as_ptr()) }; + let result = unsafe { wireguard_control_sys::wg_add_device(iface.as_ptr()) }; match result { 0 | -17 => {}, _ => return Err(io::Error::last_os_error()), }; - let mut wg_device = Box::new(wgctrl_sys::wg_device { + let mut wg_device = Box::new(wireguard_control_sys::wg_device { name: iface.into_inner(), ifindex: 0, - public_key: wgctrl_sys::wg_key::default(), - private_key: wgctrl_sys::wg_key::default(), + public_key: wireguard_control_sys::wg_key::default(), + private_key: wireguard_control_sys::wg_key::default(), fwmark: 0, listen_port: 0, first_peer, @@ -366,9 +372,9 @@ pub fn apply(builder: &DeviceUpdate, iface: &InterfaceName) -> io::Result<()> { } let ptr = Box::into_raw(wg_device); - let result = unsafe { wgctrl_sys::wg_set_device(ptr) }; + let result = unsafe { wireguard_control_sys::wg_set_device(ptr) }; - unsafe { wgctrl_sys::wg_free_device(ptr) }; + unsafe { wireguard_control_sys::wg_free_device(ptr) }; if result == 0 { Ok(()) @@ -378,11 +384,11 @@ pub fn apply(builder: &DeviceUpdate, iface: &InterfaceName) -> io::Result<()> { } pub fn get_by_name(name: &InterfaceName) -> Result { - let mut device: *mut wgctrl_sys::wg_device = ptr::null_mut(); + let mut device: *mut wireguard_control_sys::wg_device = ptr::null_mut(); let result = unsafe { - wgctrl_sys::wg_get_device( - (&mut device) as *mut _ as *mut *mut wgctrl_sys::wg_device, + wireguard_control_sys::wg_get_device( + (&mut device) as *mut _ as *mut *mut wireguard_control_sys::wg_device, name.as_ptr(), ) }; @@ -393,13 +399,13 @@ pub fn get_by_name(name: &InterfaceName) -> Result { Err(io::Error::last_os_error()) }; - unsafe { wgctrl_sys::wg_free_device(device) }; + unsafe { wireguard_control_sys::wg_free_device(device) }; result } pub fn delete_interface(iface: &InterfaceName) -> io::Result<()> { - let result = unsafe { wgctrl_sys::wg_del_device(iface.as_ptr()) }; + let result = unsafe { wireguard_control_sys::wg_del_device(iface.as_ptr()) }; if result == 0 { Ok(()) @@ -418,21 +424,21 @@ pub fn delete_interface(iface: &InterfaceName) -> io::Result<()> { /// `Key`s, especially ones created from external data. #[cfg(target_os = "linux")] #[derive(PartialEq, Eq, Clone)] -pub struct Key(wgctrl_sys::wg_key); +pub struct Key(wireguard_control_sys::wg_key); #[cfg(target_os = "linux")] impl Key { /// Creates a new `Key` from raw bytes. - pub fn from_raw(key: wgctrl_sys::wg_key) -> Self { + pub fn from_raw(key: wireguard_control_sys::wg_key) -> Self { Self(key) } /// Generates and returns a new private key. pub fn generate_private() -> Self { - let mut private_key = wgctrl_sys::wg_key::default(); + let mut private_key = wireguard_control_sys::wg_key::default(); unsafe { - wgctrl_sys::wg_generate_private_key(private_key.as_mut_ptr()); + wireguard_control_sys::wg_generate_private_key(private_key.as_mut_ptr()); } Self(private_key) @@ -440,10 +446,10 @@ impl Key { /// Generates and returns a new preshared key. pub fn generate_preshared() -> Self { - let mut preshared_key = wgctrl_sys::wg_key::default(); + let mut preshared_key = wireguard_control_sys::wg_key::default(); unsafe { - wgctrl_sys::wg_generate_preshared_key(preshared_key.as_mut_ptr()); + wireguard_control_sys::wg_generate_preshared_key(preshared_key.as_mut_ptr()); } Self(preshared_key) @@ -451,10 +457,10 @@ impl Key { /// Generates a public key for this private key. pub fn generate_public(&self) -> Self { - let mut public_key = wgctrl_sys::wg_key::default(); + let mut public_key = wireguard_control_sys::wg_key::default(); unsafe { - wgctrl_sys::wg_generate_public_key( + wireguard_control_sys::wg_generate_public_key( public_key.as_mut_ptr(), &self.0 as *const u8 as *mut u8, ); @@ -465,7 +471,7 @@ impl Key { /// Generates an all-zero key. pub fn zero() -> Self { - Self(wgctrl_sys::wg_key::default()) + Self(wireguard_control_sys::wg_key::default()) } pub fn as_bytes(&self) -> &[u8] { @@ -474,9 +480,12 @@ impl Key { /// Converts the key to a standardized base64 representation, as used by the `wg` utility and `wg-quick`. pub fn to_base64(&self) -> String { - let mut key_b64: wgctrl_sys::wg_key_b64_string = [0; 45]; + let mut key_b64: wireguard_control_sys::wg_key_b64_string = [0; 45]; unsafe { - wgctrl_sys::wg_key_to_base64(key_b64.as_mut_ptr(), &self.0 as *const u8 as *mut u8); + wireguard_control_sys::wg_key_to_base64( + key_b64.as_mut_ptr(), + &self.0 as *const u8 as *mut u8, + ); str::from_utf8_unchecked(&*(&key_b64[..44] as *const [c_char] as *const [u8])).into() } @@ -487,11 +496,14 @@ impl Key { /// This can fail, as not all text input is valid base64 - in this case /// `Err(InvalidKey)` is returned. pub fn from_base64(key: &str) -> Result { - let mut decoded = wgctrl_sys::wg_key::default(); + let mut decoded = wireguard_control_sys::wg_key::default(); let key_str = CString::new(key)?; let result = unsafe { - wgctrl_sys::wg_key_from_base64(decoded.as_mut_ptr(), key_str.as_ptr() as *mut _) + wireguard_control_sys::wg_key_from_base64( + decoded.as_mut_ptr(), + key_str.as_ptr() as *mut _, + ) }; if result == 0 { diff --git a/wgctrl-rs/src/backends/mod.rs b/wireguard-control/src/backends/mod.rs similarity index 100% rename from wgctrl-rs/src/backends/mod.rs rename to wireguard-control/src/backends/mod.rs diff --git a/wgctrl-rs/src/backends/userspace.rs b/wireguard-control/src/backends/userspace.rs similarity index 100% rename from wgctrl-rs/src/backends/userspace.rs rename to wireguard-control/src/backends/userspace.rs diff --git a/wgctrl-rs/src/config.rs b/wireguard-control/src/config.rs similarity index 99% rename from wgctrl-rs/src/config.rs rename to wireguard-control/src/config.rs index 85d8178..1a0fde1 100644 --- a/wgctrl-rs/src/config.rs +++ b/wireguard-control/src/config.rs @@ -18,7 +18,7 @@ use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}; /// /// # Example /// ```rust -/// # use wgctrl::*; +/// # use wireguard_control::*; /// # use std::net::AddrParseError; /// # fn try_main() -> Result<(), AddrParseError> { /// let peer_keypair = KeyPair::generate(); diff --git a/wgctrl-rs/src/device.rs b/wireguard-control/src/device.rs similarity index 99% rename from wgctrl-rs/src/device.rs rename to wireguard-control/src/device.rs index 721dce1..acc42a3 100644 --- a/wgctrl-rs/src/device.rs +++ b/wireguard-control/src/device.rs @@ -273,7 +273,7 @@ impl Device { /// /// # Example /// ```rust -/// # use wgctrl::*; +/// # use wireguard_control::*; /// # use std::net::AddrParseError; /// # fn try_main() -> Result<(), AddrParseError> { /// let our_keypair = KeyPair::generate(); diff --git a/wgctrl-rs/src/key.rs b/wireguard-control/src/key.rs similarity index 100% rename from wgctrl-rs/src/key.rs rename to wireguard-control/src/key.rs diff --git a/wgctrl-rs/src/lib.rs b/wireguard-control/src/lib.rs similarity index 100% rename from wgctrl-rs/src/lib.rs rename to wireguard-control/src/lib.rs