GitLab's annual major release is around the corner. Along with a lot of new and exciting features, there will be a few breaking changes. Learn more here.

Commit bdcc8e0c authored by K900's avatar K900
Browse files

rename: wg-rs -> wgctrl-rs, wg-sys -> wgctrl-sys

parent 803f1ab3
Pipeline #28676017 passed with stage
in 3 minutes and 50 seconds
target/
wg-rs/src/bin/test.rs
wgctrl-rs/src/bin/test.rs
......@@ -332,16 +332,16 @@ version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "wg-rs"
name = "wgctrl-rs"
version = "0.1.0"
dependencies = [
"chrono 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)",
"wg-sys 0.1.0",
"wgctrl-sys 0.1.0",
]
[[package]]
name = "wg-sys"
name = "wgctrl-sys"
version = "0.1.0"
dependencies = [
"bindgen 0.38.0 (registry+https://github.com/rust-lang/crates.io-index)",
......
[workspace]
members = ["wg-rs", "wg-sys"]
members = ["wgctrl-rs", "wgctrl-sys"]
# wg-rs
# wgctrl-rs
This is really hacky, pre-pre-pre-alpha quality software. Nothing works, and things that do work don't actually work. Hide your cat.
[Docs](https://k900.gitlab.io/wg-rs/wg_rs/index.html) (wip!)
[Docs](https://k900.gitlab.io/wgctrl-rs/wgctrl_rs/index.html) (wip!)
# License
LGPL 2.1, cause that's what the wireguard C library is licensed under
......
#!/bin/sh -e
cd $(dirname $0)
wget https://git.zx2c4.com/WireGuard/plain/contrib/examples/embeddable-wg-library/wireguard.c -O wg-sys/c/wireguard.c
wget https://git.zx2c4.com/WireGuard/plain/contrib/examples/embeddable-wg-library/wireguard.h -O wg-sys/c/wireguard.h
wget https://git.zx2c4.com/WireGuard/plain/contrib/examples/embeddable-wg-library/wireguard.c -O wgctrl-sys/c/wireguard.c
wget https://git.zx2c4.com/WireGuard/plain/contrib/examples/embeddable-wg-library/wireguard.h -O wgctrl-sys/c/wireguard.h
[package]
name = "wg-rs"
name = "wgctrl-rs"
version = "0.1.0"
authors = ["K900 <me@0upti.me>"]
[dependencies]
wg-sys = { path = "../wg-sys" }
wgctrl-sys = { path = "../wgctrl-sys" }
libc = "*"
chrono = "*"
\ No newline at end of file
chrono = "*"
extern crate wg_rs;
extern crate wgctrl_rs;
use wg_rs::*;
use wgctrl_rs::*;
fn main() {
println!("{}", Key::generate_private().to_base64());
......
extern crate wg_rs;
extern crate wgctrl_rs;
use wg_rs::*;
use wgctrl_rs::*;
fn main() {
println!("{}", Key::generate_preshared().to_base64());
......
extern crate wg_rs;
extern crate wgctrl_rs;
use wg_rs::*;
use wgctrl_rs::*;
use std::io::{self, BufRead};
......
extern crate wg_rs;
extern crate wgctrl_rs;
use wg_rs::*;
use wgctrl_rs::*;
fn main() {
for dev in DeviceInfo::enumerate().expect("Failed to enumerate devices!") {
......
extern crate libc;
extern crate wg_sys;
extern crate wgctrl_sys;
use self::wg_sys::wg_device_flags as wgdf;
use self::wg_sys::wg_peer_flags as wgpf;
use self::wgctrl_sys::wg_device_flags as wgdf;
use self::wgctrl_sys::wg_peer_flags as wgpf;
use device::{AllowedIp, PeerConfig};
use key::{Key, KeyPair};
......@@ -14,16 +14,16 @@ use std::ptr;
fn encode_allowedips(
allowed_ips: &[AllowedIp],
) -> (*mut wg_sys::wg_allowedip, *mut wg_sys::wg_allowedip) {
) -> (*mut wgctrl_sys::wg_allowedip, *mut wgctrl_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 wg_sys::wg_allowedip = ptr::null_mut();
let mut last_ip: *mut wgctrl_sys::wg_allowedip = ptr::null_mut();
for ip in allowed_ips {
let mut wg_allowedip = Box::new(wg_sys::wg_allowedip {
let mut wg_allowedip = Box::new(wgctrl_sys::wg_allowedip {
family: 0,
__bindgen_anon_1: unsafe { mem::uninitialized() },
cidr: ip.cidr,
......@@ -50,9 +50,9 @@ fn encode_allowedips(
(first_ip, last_ip)
}
fn encode_endpoint(endpoint: Option<SocketAddr>) -> wg_sys::wg_peer__bindgen_ty_1 {
fn encode_endpoint(endpoint: Option<SocketAddr>) -> wgctrl_sys::wg_peer__bindgen_ty_1 {
match endpoint {
Some(SocketAddr::V4(s)) => wg_sys::wg_peer__bindgen_ty_1 {
Some(SocketAddr::V4(s)) => wgctrl_sys::wg_peer__bindgen_ty_1 {
addr4: libc::sockaddr_in {
sin_family: libc::AF_INET as u16,
sin_addr: libc::in_addr {
......@@ -63,7 +63,7 @@ fn encode_endpoint(endpoint: Option<SocketAddr>) -> wg_sys::wg_peer__bindgen_ty_
},
},
Some(SocketAddr::V6(s)) => {
let mut result = wg_sys::wg_peer__bindgen_ty_1 {
let mut result = wgctrl_sys::wg_peer__bindgen_ty_1 {
addr6: libc::sockaddr_in6 {
sin6_family: libc::AF_INET6 as u16,
sin6_addr: unsafe { mem::uninitialized() },
......@@ -75,22 +75,22 @@ fn encode_endpoint(endpoint: Option<SocketAddr>) -> wg_sys::wg_peer__bindgen_ty_
unsafe { result.addr6.sin6_addr.s6_addr = s.ip().octets() };
result
}
None => wg_sys::wg_peer__bindgen_ty_1 {
None => wgctrl_sys::wg_peer__bindgen_ty_1 {
addr6: unsafe { mem::zeroed() },
},
}
}
fn encode_peers(peers: Vec<PeerConfigBuilder>) -> (*mut wg_sys::wg_peer, *mut wg_sys::wg_peer) {
fn encode_peers(peers: Vec<PeerConfigBuilder>) -> (*mut wgctrl_sys::wg_peer, *mut wgctrl_sys::wg_peer) {
let mut first_peer = ptr::null_mut();
let mut last_peer: *mut wg_sys::wg_peer = ptr::null_mut();
let mut last_peer: *mut wgctrl_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(wg_sys::wg_peer {
let mut wg_peer = Box::new(wgctrl_sys::wg_peer {
public_key: peer.public_key.0,
preshared_key: wg_sys::wg_key::default(),
preshared_key: wgctrl_sys::wg_key::default(),
endpoint: encode_endpoint(peer.endpoint),
last_handshake_time: libc::timespec {
tv_sec: 0,
......@@ -144,7 +144,7 @@ fn encode_name(name: &str) -> [i8; 16] {
///
/// # Example
/// ```rust
/// # use wg_rs::*;
/// # use wgctrl_rs::*;
/// # use std::net::AddrParseError;
/// # fn try_main() -> Result<(), AddrParseError> {
/// let our_keypair = KeyPair::generate();
......@@ -301,17 +301,17 @@ impl DeviceConfigBuilder {
let (first_peer, last_peer) = encode_peers(self.peers);
let iface_str = CString::new(iface)?;
let result = unsafe { wg_sys::wg_add_device(iface_str.as_ptr()) };
let result = unsafe { wgctrl_sys::wg_add_device(iface_str.as_ptr()) };
match result {
0 | -17 => {}
_ => return Err(io::Error::last_os_error()),
};
let mut wg_device = Box::new(wg_sys::wg_device {
let mut wg_device = Box::new(wgctrl_sys::wg_device {
name: encode_name(iface),
ifindex: 0,
public_key: wg_sys::wg_key::default(),
private_key: wg_sys::wg_key::default(),
public_key: wgctrl_sys::wg_key::default(),
private_key: wgctrl_sys::wg_key::default(),
fwmark: 0,
listen_port: 0,
first_peer,
......@@ -336,9 +336,9 @@ impl DeviceConfigBuilder {
}
let ptr = Box::into_raw(wg_device);
let result = unsafe { wg_sys::wg_set_device(ptr) };
let result = unsafe { wgctrl_sys::wg_set_device(ptr) };
unsafe { wg_sys::wg_free_device(ptr) };
unsafe { wgctrl_sys::wg_free_device(ptr) };
if result == 0 {
Ok(())
......@@ -367,7 +367,7 @@ impl Default for DeviceConfigBuilder {
///
/// # Example
/// ```rust
/// # use wg_rs::*;
/// # use wgctrl_rs::*;
/// # use std::net::AddrParseError;
/// # fn try_main() -> Result<(), AddrParseError> {
/// let peer_keypair = KeyPair::generate();
......@@ -500,7 +500,7 @@ impl PeerConfigBuilder {
/// Deletes an existing WireGuard interface by name.
pub fn delete_interface(iface: &str) -> io::Result<()> {
let iface_str = CString::new(iface)?;
let result = unsafe { wg_sys::wg_del_device(iface_str.as_ptr()) };
let result = unsafe { wgctrl_sys::wg_del_device(iface_str.as_ptr()) };
if result == 0 {
Ok(())
......
extern crate chrono;
extern crate libc;
extern crate wg_sys;
extern crate wgctrl_sys;
use key::Key;
......@@ -26,8 +26,8 @@ pub struct AllowedIp {
pub cidr: u8,
}
impl<'a> From<&'a wg_sys::wg_allowedip> for AllowedIp {
fn from(raw: &wg_sys::wg_allowedip) -> AllowedIp {
impl<'a> From<&'a wgctrl_sys::wg_allowedip> for AllowedIp {
fn from(raw: &wgctrl_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 => IpAddr::V6(unsafe { raw.__bindgen_anon_1.ip6 }.s6_addr.into()),
......@@ -84,10 +84,10 @@ pub struct PeerInfo {
pub stats: PeerStats,
}
fn parse_allowed_ips(peer: &wg_sys::wg_peer) -> Vec<AllowedIp> {
fn parse_allowed_ips(peer: &wgctrl_sys::wg_peer) -> Vec<AllowedIp> {
let mut result = Vec::new();
let mut current_ip: *mut wg_sys::wg_allowedip = peer.first_allowedip;
let mut current_ip: *mut wgctrl_sys::wg_allowedip = peer.first_allowedip;
if current_ip.is_null() {
return result;
......@@ -107,7 +107,7 @@ fn parse_allowed_ips(peer: &wg_sys::wg_peer) -> Vec<AllowedIp> {
result
}
unsafe fn parse_endpoint(endpoint: &wg_sys::wg_peer__bindgen_ty_1) -> Option<SocketAddr> {
unsafe fn parse_endpoint(endpoint: &wgctrl_sys::wg_peer__bindgen_ty_1) -> Option<SocketAddr> {
match i32::from(endpoint.addr.sa_family) {
libc::AF_INET => Some(SocketAddr::new(
IpAddr::V4(endpoint.addr4.sin_addr.s_addr.into()),
......@@ -125,9 +125,9 @@ unsafe fn parse_endpoint(endpoint: &wg_sys::wg_peer__bindgen_ty_1) -> Option<Soc
}
}
impl<'a> From<&'a wg_sys::wg_peer> for PeerInfo {
fn from(raw: &wg_sys::wg_peer) -> PeerInfo {
use self::wg_sys::wg_peer_flags as wgpf;
impl<'a> From<&'a wgctrl_sys::wg_peer> for PeerInfo {
fn from(raw: &wgctrl_sys::wg_peer) -> PeerInfo {
use self::wgctrl_sys::wg_peer_flags as wgpf;
PeerInfo {
config: PeerConfig {
......@@ -184,7 +184,7 @@ pub struct DeviceInfo {
__cant_construct_me: (),
}
fn parse_peers(dev: &wg_sys::wg_device) -> Vec<PeerInfo> {
fn parse_peers(dev: &wgctrl_sys::wg_device) -> Vec<PeerInfo> {
let mut result = Vec::new();
let mut current_peer = dev.first_peer;
......@@ -216,9 +216,9 @@ fn parse_device_name(name: [i8; 16]) -> String {
unsafe { str::from_utf8_unchecked(&name[..idx]) }.to_owned()
}
impl<'a> From<&'a wg_sys::wg_device> for DeviceInfo {
fn from(raw: &wg_sys::wg_device) -> DeviceInfo {
use self::wg_sys::wg_device_flags as wgdf;
impl<'a> From<&'a wgctrl_sys::wg_device> for DeviceInfo {
fn from(raw: &wgctrl_sys::wg_device) -> DeviceInfo {
use self::wgctrl_sys::wg_device_flags as wgdf;
DeviceInfo {
name: parse_device_name(raw.name),
......@@ -256,7 +256,7 @@ impl DeviceInfo {
/// # Example
///
/// ```rust
/// # use wg_rs::*;
/// # use wgctrl_rs::*;
/// # fn main() -> Result<(), std::io::Error> {
/// for dev in DeviceInfo::enumerate()? {
/// println!("{:#?}", DeviceInfo::get_by_name(&dev));
......@@ -265,7 +265,7 @@ impl DeviceInfo {
/// # }
/// ```
pub fn enumerate() -> Result<Vec<String>, io::Error> {
let base = unsafe { wg_sys::wg_list_device_names() };
let base = unsafe { wgctrl_sys::wg_list_device_names() };
if base.is_null() {
return Err(io::Error::last_os_error());
......@@ -299,7 +299,7 @@ impl DeviceInfo {
/// # Example
///
/// ```rust
/// # use wg_rs::*;
/// # use wgctrl_rs::*;
/// # fn main() -> Result<(), std::io::Error> {
/// for dev in DeviceInfo::enumerate()? {
/// if let Ok(dev) = DeviceInfo::get_by_name(&dev) {
......@@ -313,13 +313,13 @@ impl DeviceInfo {
/// # }
/// ```
pub fn get_by_name(name: &str) -> Result<Self, io::Error> {
let mut device: *mut wg_sys::wg_device = ptr::null_mut();
let mut device: *mut wgctrl_sys::wg_device = ptr::null_mut();
let cs = CString::new(name)?;
let result = unsafe {
wg_sys::wg_get_device(
(&mut device) as *mut _ as *mut *mut wg_sys::wg_device,
wgctrl_sys::wg_get_device(
(&mut device) as *mut _ as *mut *mut wgctrl_sys::wg_device,
cs.as_ptr(),
)
};
......@@ -330,7 +330,7 @@ impl DeviceInfo {
Err(io::Error::last_os_error())
};
unsafe { wg_sys::wg_free_device(device) };
unsafe { wgctrl_sys::wg_free_device(device) };
result
}
......
extern crate wg_sys;
extern crate wgctrl_sys;
use std::ffi::{CString, NulError};
use std::fmt;
......@@ -29,7 +29,7 @@ impl From<NulError> for InvalidKey {
/// This means that you need to be careful when working with
/// `Key`s, especially ones created from external data.
#[derive(PartialEq, Eq, Clone)]
pub struct Key(pub wg_sys::wg_key);
pub struct Key(pub wgctrl_sys::wg_key);
/// Represents a pair of private and public keys.
///
......@@ -45,16 +45,16 @@ pub struct KeyPair {
impl Key {
/// Creates a new `Key` from raw bytes.
pub fn from_raw(key: wg_sys::wg_key) -> Self {
pub fn from_raw(key: wgctrl_sys::wg_key) -> Self {
Self { 0: key }
}
/// Generates and returns a new private key.
pub fn generate_private() -> Self {
let mut private_key = wg_sys::wg_key::default();
let mut private_key = wgctrl_sys::wg_key::default();
unsafe {
wg_sys::wg_generate_private_key(private_key.as_mut_ptr());
wgctrl_sys::wg_generate_private_key(private_key.as_mut_ptr());
}
Self { 0: private_key }
......@@ -62,10 +62,10 @@ impl Key {
/// Generates and returns a new preshared key.
pub fn generate_preshared() -> Self {
let mut preshared_key = wg_sys::wg_key::default();
let mut preshared_key = wgctrl_sys::wg_key::default();
unsafe {
wg_sys::wg_generate_preshared_key(preshared_key.as_mut_ptr());
wgctrl_sys::wg_generate_preshared_key(preshared_key.as_mut_ptr());
}
Self { 0: preshared_key }
......@@ -73,12 +73,12 @@ impl Key {
/// Generates a public key for this private key.
pub fn generate_public(&self) -> Self {
let mut public_key = wg_sys::wg_key::default();
let mut public_key = wgctrl_sys::wg_key::default();
unsafe {
// TODO: this cast is potentially UB, but bindgen doesn't realise that
// the second argument here should be `const`
wg_sys::wg_generate_public_key(
wgctrl_sys::wg_generate_public_key(
public_key.as_mut_ptr(),
&self.0 as *const u8 as *mut u8,
);
......@@ -90,7 +90,7 @@ impl Key {
/// Generates an all-zero key.
pub fn zero() -> Self {
Self {
0: wg_sys::wg_key::default(),
0: wgctrl_sys::wg_key::default(),
}
}
......@@ -98,16 +98,16 @@ impl Key {
pub fn is_zero(&self) -> bool {
// TODO: this cast is potentially UB, but bindgen doesn't realise that
// the argument here should be `const`
unsafe { wg_sys::wg_key_is_zero(&self.0 as *const u8 as *mut u8) }
unsafe { wgctrl_sys::wg_key_is_zero(&self.0 as *const u8 as *mut u8) }
}
/// 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: wg_sys::wg_key_b64_string = [0i8; 45];
let mut key_b64: wgctrl_sys::wg_key_b64_string = [0i8; 45];
unsafe {
// TODO: this cast is potentially UB, but bindgen doesn't realise that
// the second argument here should be `const`
wg_sys::wg_key_to_base64(key_b64.as_mut_ptr(), &self.0 as *const u8 as *mut u8);
wgctrl_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 [i8] as *const [u8])).into()
}
......@@ -118,11 +118,11 @@ 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<Self, InvalidKey> {
let mut decoded = wg_sys::wg_key::default();
let mut decoded = wgctrl_sys::wg_key::default();
let key_str = CString::new(key)?;
let result =
unsafe { wg_sys::wg_key_from_base64(decoded.as_mut_ptr(), key_str.as_ptr() as *mut _) };
unsafe { wgctrl_sys::wg_key_from_base64(decoded.as_mut_ptr(), key_str.as_ptr() as *mut _) };
if result == 0 {
Ok(Self { 0: decoded })
......
[package]
name = "wg-sys"
name = "wgctrl-sys"
version = "0.1.0"
authors = ["K900 <me@0upti.me>"]
......@@ -8,4 +8,4 @@ libc = "*"
[build-dependencies]
bindgen = "*"
cc = "*"
\ No newline at end of file
cc = "*"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment