diff --git a/Cargo.lock b/Cargo.lock index 6143813..9392e8c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14,9 +14,9 @@ dependencies = [ [[package]] name = "aho-corasick" -version = "1.1.3" +version = "1.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" dependencies = [ "memchr", ] @@ -142,9 +142,9 @@ dependencies = [ [[package]] name = "bitflags" -version = "2.9.4" +version = "2.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2261d10cca569e4643e526d8dc2e62e433cc8aba21ab764233731f8d369bf394" +checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3" [[package]] name = "blake2" @@ -234,9 +234,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.2.41" +version = "1.2.43" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac9fe6cdbb24b6ade63616c0a0688e45bb56732262c158df3c0c4bea4ca47cb7" +checksum = "739eb0f94557554b3ca9a86d2d37bebd49c5e6d0c1d2bda35ba5bdac830befc2" dependencies = [ "find-msvc-tools", "shlex", @@ -291,9 +291,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.49" +version = "4.5.50" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4512b90fa68d3a9932cea5184017c5d200f5921df706d45e853537dea51508f" +checksum = "0c2cfd7bf8a6017ddaa4e32ffe7403d547790db06bd171c1c53926faab501623" dependencies = [ "clap_builder", "clap_derive", @@ -301,9 +301,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.49" +version = "4.5.50" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0025e98baa12e766c67ba13ff4695a887a1eba19569aad00a472546795bd6730" +checksum = "0a4c05b9e80c5ccd3a7ef080ad7b6ba7d6fc00a985b8b157197075677c82c7a0" dependencies = [ "anstyle", "clap_lex", @@ -382,7 +382,7 @@ dependencies = [ [[package]] name = "defguard_wireguard_rs" -version = "0.8.0" +version = "0.9.0" dependencies = [ "base64", "boringtun", @@ -563,9 +563,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.11.4" +version = "2.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4b0f83760fb341a774ed326568e19f5a863af4a952def8c39f9ab92fd95b88e5" +checksum = "6717a8d2a5a929a1a2eb43a12812498ed141a0bcfb7e8f7844fbdbe4303bba9f" dependencies = [ "equivalent", "hashbrown", @@ -610,9 +610,9 @@ checksum = "469fb0b9cefa57e3ef31275ee7cacb78f2fdca44e4765491884a2b119d4eb130" [[package]] name = "is_terminal_polyfill" -version = "1.70.1" +version = "1.70.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" [[package]] name = "itoa" @@ -812,9 +812,9 @@ checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" [[package]] name = "once_cell_polyfill" -version = "1.70.1" +version = "1.70.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4895175b425cb1f87721b59f0f286c2092bd4af812243672510e1ac53e2e0ad" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" [[package]] name = "opaque-debug" @@ -903,9 +903,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.101" +version = "1.0.103" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89ae43fd86e4158d6db51ad8e2b80f313af9cc74f5c0e03ccb87de09998732de" +checksum = "5ee95bc4ef87b8d5ba32e8b7714ccc834865276eab0aed5c9958d00ec45f49e8" dependencies = [ "unicode-ident", ] @@ -1171,9 +1171,9 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" [[package]] name = "syn" -version = "2.0.106" +version = "2.0.108" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ede7c438028d4436d71104916910f5bb611972c5cfd7f89b8300a8186e6fada6" +checksum = "da58917d35242480a05c2897064da0a80589a2a0476c9a3f2fdc83b53502e917" dependencies = [ "proc-macro2", "quote", @@ -1325,9 +1325,9 @@ checksum = "562d481066bde0658276a35467c4af00bdc6ee726305698a55b86e61d7ad82bb" [[package]] name = "unicode-ident" -version = "1.0.19" +version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f63a545481291138910575129486daeaf8ac54aee4387fe7906919f7830c7d9d" +checksum = "462eeb75aeb73aea900253ce739c8e18a67423fadf006037cd3ff27e82748a06" [[package]] name = "uniffi" diff --git a/Cargo.toml b/Cargo.toml index d546c40..b18391d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "defguard_wireguard_rs" -version = "0.8.0" +version = "0.9.0" edition = "2024" rust-version = "1.85" description = "A unified multi-platform high-level API for managing WireGuard interfaces" diff --git a/src/bsd/mod.rs b/src/bsd/mod.rs index 31c9512..d027dcb 100644 --- a/src/bsd/mod.rs +++ b/src/bsd/mod.rs @@ -115,7 +115,7 @@ impl IpAddrMask { .and_then(|ipv6| <[u8; 16]>::try_from(ipv6).ok().map(IpAddr::from)), } .map(|ip| Self { - ip, + address: ip, cidr: cidr as u8, }) }) @@ -129,7 +129,7 @@ impl<'a> IpAddrMask { nvlist.append_number(NV_CIDR, u64::from(self.cidr)); - match self.ip { + match self.address { IpAddr::V4(ipv4) => nvlist.append_bytes(NV_IPV4, ipv4.octets().into()), IpAddr::V6(ipv6) => nvlist.append_bytes(NV_IPV6, ipv6.octets().into()), } @@ -340,7 +340,7 @@ pub fn delete_interface(if_name: &str) -> Result<(), IoError> { } pub fn set_address(if_name: &str, address: &IpAddrMask) -> Result<(), IoError> { - match address.ip { + match address.address { IpAddr::V4(address) => { let ifreq = IfReq::new_with_address(if_name, address); ifreq.set_address() @@ -356,7 +356,7 @@ pub fn assign_address(if_name: &str, address: &IpAddrMask) -> Result<(), IoError let broadcast = address.broadcast(); let mask = address.mask(); - match (address.ip, broadcast, mask) { + match (address.address, broadcast, mask) { (IpAddr::V4(address), IpAddr::V4(broadcast), IpAddr::V4(mask)) => { let inaliasreq = InAliasReq::new(if_name, address, broadcast, mask); inaliasreq.add_address() @@ -370,7 +370,7 @@ pub fn assign_address(if_name: &str, address: &IpAddrMask) -> Result<(), IoError } pub fn remove_address(if_name: &str, address: &IpAddrMask) -> Result<(), IoError> { - match address.ip { + match address.address { IpAddr::V4(address) => { let ifreq = IfReq::new_with_address(if_name, address); ifreq.delete_address() @@ -459,7 +459,7 @@ pub fn get_gateway(ip_version: IpVersion) -> Result, IoError> { /// Add routing gateway. pub fn add_gateway(dest: &IpAddrMask, gateway: IpAddr, is_blackhole: bool) -> Result<(), IoError> { debug!("Adding gateway: destination {dest}, gateway {gateway}, is blackhole {is_blackhole}."); - match (dest.ip, dest.mask(), gateway) { + match (dest.address, dest.mask(), gateway) { (IpAddr::V4(ip), IpAddr::V4(mask), IpAddr::V4(gw)) => { let payload = DestAddrMask::::new(ip.into(), mask.into(), gw.into()); let rtmsg = RtMessage::new_for_add_gateway(payload, dest.is_host(), is_blackhole); @@ -480,7 +480,7 @@ pub fn add_gateway(dest: &IpAddrMask, gateway: IpAddr, is_blackhole: bool) -> Re /// Remove routing gateway. pub fn delete_gateway(dest: &IpAddrMask) -> Result<(), IoError> { debug!("Deleting gateway with destination {dest}."); - match (dest.ip, dest.mask()) { + match (dest.address, dest.mask()) { (IpAddr::V4(ip), IpAddr::V4(mask)) => { let payload = DestAddrMask::::new(ip.into(), mask.into(), SockAddrIn::default()); @@ -508,7 +508,7 @@ pub fn add_linked_route(dest: &IpAddrMask, if_name: &str) -> Result<(), IoError> if if_index == 0 { return Err(IoError::NetworkInterface); } - match (dest.ip, dest.mask()) { + match (dest.address, dest.mask()) { (IpAddr::V4(ip), IpAddr::V4(mask)) => { let link = SockAddrDl::new(if_index); let payload = GatewayLink::::new(ip.into(), mask.into(), link); @@ -535,7 +535,7 @@ pub fn add_route(dest: &IpAddrMask, if_name: &str) -> Result<(), IoError> { if if_index == 0 { return Err(IoError::NetworkInterface); } - match (dest.ip, dest.mask()) { + match (dest.address, dest.mask()) { (IpAddr::V4(ip), IpAddr::V4(mask)) => { let payload = DestAddrMask::::new_for_interface(ip.into(), mask.into(), if_name); @@ -561,7 +561,7 @@ pub fn delete_route(dest: &IpAddrMask, if_name: &str) -> Result<(), IoError> { if if_index == 0 { return Err(IoError::NetworkInterface); } - match (dest.ip, dest.mask()) { + match (dest.address, dest.mask()) { (IpAddr::V4(ip), IpAddr::V4(mask)) => { let payload = DestAddrMask::::new_for_interface(ip.into(), mask.into(), if_name); diff --git a/src/lib.rs b/src/lib.rs index 1eaad69..5d22214 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -105,7 +105,7 @@ pub struct InterfaceConfiguration { pub name: String, pub prvkey: String, pub addresses: Vec, - pub port: u32, + pub port: u16, pub peers: Vec, /// Maximum transfer unit. `None` means do not set MTU, but keep the system default. pub mtu: Option, @@ -129,7 +129,7 @@ impl TryFrom<&InterfaceConfiguration> for Host { fn try_from(config: &InterfaceConfiguration) -> Result { let key = config.prvkey.as_str().try_into()?; - let mut host = Host::new(config.port as u16, key); + let mut host = Host::new(config.port, key); for peercfg in &config.peers { let peer = peercfg.clone(); let key: Key = peer.public_key.clone(); diff --git a/src/net.rs b/src/net.rs index e4ac646..c0ba321 100644 --- a/src/net.rs +++ b/src/net.rs @@ -19,31 +19,31 @@ use serde::{Deserialize, Serialize}; #[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct IpAddrMask { // IP v4 or v6 - pub ip: IpAddr, + pub address: IpAddr, // Classless Inter-Domain Routing pub cidr: u8, } impl IpAddrMask { #[must_use] - pub fn new(ip: IpAddr, cidr: u8) -> Self { - Self { ip, cidr } + pub fn new(address: IpAddr, cidr: u8) -> Self { + Self { address, cidr } } #[must_use] - pub fn host(ip: IpAddr) -> Self { - let cidr = match ip { + pub fn host(address: IpAddr) -> Self { + let cidr = match address { IpAddr::V4(_) => 32, IpAddr::V6(_) => 128, }; - Self { ip, cidr } + Self { address, cidr } } /// Returns broadcast address as `IpAddr`. /// Note: IPv6 does not really use broadcast. #[must_use] pub fn broadcast(&self) -> IpAddr { - match self.ip { + match self.address { IpAddr::V4(ip) => { let addr = u32::from(ip); let bits = if self.cidr >= 32 { @@ -68,7 +68,7 @@ impl IpAddrMask { /// Returns network mask as `IpAddr`. #[must_use] pub fn mask(&self) -> IpAddr { - match self.ip { + match self.address { IpAddr::V4(_) => { let mask = if self.cidr == 0 { 0 @@ -91,7 +91,7 @@ impl IpAddrMask { /// Returns `true` if the address defines a host, `false` if it is a network. #[must_use] pub fn is_host(&self) -> bool { - if self.ip.is_ipv4() { + if self.address.is_ipv4() { self.cidr == 32 } else { self.cidr == 128 @@ -102,12 +102,12 @@ impl IpAddrMask { #[must_use] pub fn to_nlas_allowed_ip(&self) -> WgAllowedIp { let mut attrs = Vec::new(); - attrs.push(WgAllowedIpAttrs::Family(if self.ip.is_ipv4() { + attrs.push(WgAllowedIpAttrs::Family(if self.address.is_ipv4() { AF_INET } else { AF_INET6 })); - attrs.push(WgAllowedIpAttrs::IpAddr(self.ip)); + attrs.push(WgAllowedIpAttrs::IpAddr(self.address)); attrs.push(WgAllowedIpAttrs::Cidr(self.cidr)); WgAllowedIp(attrs) } @@ -115,7 +115,7 @@ impl IpAddrMask { impl fmt::Display for IpAddrMask { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}/{}", self.ip, self.cidr) + write!(f, "{}/{}", self.address, self.cidr) } } @@ -144,11 +144,11 @@ impl FromStr for IpAddrMask { if cidr > max_cidr { return Err(IpAddrParseError); } - Ok(IpAddrMask { ip, cidr }) + Ok(IpAddrMask { address: ip, cidr }) } else { let ip = ip_str.parse().map_err(|_| IpAddrParseError)?; Ok(IpAddrMask { - ip, + address: ip, cidr: if ip.is_ipv4() { 32 } else { 128 }, }) } diff --git a/src/netlink.rs b/src/netlink.rs index c68365a..2b416a1 100644 --- a/src/netlink.rs +++ b/src/netlink.rs @@ -102,7 +102,7 @@ impl Key { impl IpAddrMask { #[must_use] fn address_family(&self) -> AddressFamily { - match self.ip { + match self.address { IpAddr::V4(_) => AddressFamily::Inet, IpAddr::V6(_) => AddressFamily::Inet6, } @@ -260,18 +260,20 @@ fn set_address(index: u32, address: &IpAddrMask) -> NetlinkResult<()> { message.header.index = index; message.header.family = address.address_family(); - if address.ip.is_multicast() { - if let IpAddr::V6(addr) = address.ip { + if address.address.is_multicast() { + if let IpAddr::V6(addr) = address.address { message.attributes.push(AddressAttribute::Multicast(addr)); } } else { message .attributes - .push(AddressAttribute::Address(address.ip)); + .push(AddressAttribute::Address(address.address)); // For IPv4 the Local address can be set to the same value as // Address. - message.attributes.push(AddressAttribute::Local(address.ip)); + message + .attributes + .push(AddressAttribute::Local(address.address)); // Set the broadcast address as well (IPv6 does not support // broadcast). @@ -527,7 +529,7 @@ pub(crate) fn add_route( }; header.address_family = address.address_family(); header.destination_prefix_length = address.cidr; - let route_address = match address.ip { + let route_address = match address.address { IpAddr::V4(ipv4) => RouteAddress::Inet(ipv4), IpAddr::V6(ipv6) => RouteAddress::Inet6(ipv6), }; diff --git a/src/utils.rs b/src/utils.rs index 52c96da..8f8852f 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -188,7 +188,7 @@ fn setup_default_route( addr: &crate::IpAddrMask, ) -> Result<(), WireguardInterfaceError> { debug!("Found default route in AllowedIPs: {addr:?}"); - let is_ipv6 = addr.ip.is_ipv6(); + let is_ipv6 = addr.address.is_ipv6(); let proto = if is_ipv6 { "-6" } else { "-4" }; debug!("Using the following IP version: {proto}"); @@ -271,9 +271,9 @@ pub(crate) fn add_peer_routing( // Gather allowed IPs and default routes for peer in peers { for addr in &peer.allowed_ips { - if addr.ip.is_unspecified() { + if addr.address.is_unspecified() { // Default route - store for later - if addr.ip.is_ipv4() { + if addr.address.is_ipv4() { default_routes.0 = Some(addr); } else { default_routes.1 = Some(addr); @@ -281,7 +281,7 @@ pub(crate) fn add_peer_routing( continue; } // Regular route - add to set - if addr.ip.is_ipv4() { + if addr.address.is_ipv4() { allowed_ips.0.insert(addr); } else { allowed_ips.1.insert(addr); @@ -339,14 +339,14 @@ pub(crate) fn add_peer_routing( debug!("Processing route for allowed IP: {addr}, interface: {ifname}"); // FIXME: currently it is impossible to add another default route, so use the hack from // wg-quick for Darwin. - if addr.ip.is_unspecified() && addr.cidr == 0 { + if addr.address.is_unspecified() && addr.cidr == 0 { debug!( "Found following default route in the allowed IPs: {addr}, interface: \ {ifname}, proceeding with default route initial setup." ); let default1; let default2; - if addr.ip.is_ipv4() { + if addr.address.is_ipv4() { // 0.0.0.0/1 default1 = IpAddrMask::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), 1); // 128.0.0.0/1