Skip to content
Merged
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
46 changes: 23 additions & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
20 changes: 10 additions & 10 deletions src/bsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
})
Expand All @@ -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()),
}
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -459,7 +459,7 @@ pub fn get_gateway(ip_version: IpVersion) -> Result<Option<IpAddr>, 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::<SockAddrIn>::new(ip.into(), mask.into(), gw.into());
let rtmsg = RtMessage::new_for_add_gateway(payload, dest.is_host(), is_blackhole);
Expand All @@ -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::<SockAddrIn>::new(ip.into(), mask.into(), SockAddrIn::default());
Expand Down Expand Up @@ -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::<SockAddrIn>::new(ip.into(), mask.into(), link);
Expand All @@ -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::<SockAddrIn>::new_for_interface(ip.into(), mask.into(), if_name);
Expand All @@ -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::<SockAddrIn>::new_for_interface(ip.into(), mask.into(), if_name);
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ pub struct InterfaceConfiguration {
pub name: String,
pub prvkey: String,
pub addresses: Vec<IpAddrMask>,
pub port: u32,
pub port: u16,
pub peers: Vec<Peer>,
/// Maximum transfer unit. `None` means do not set MTU, but keep the system default.
pub mtu: Option<u32>,
Expand All @@ -129,7 +129,7 @@ impl TryFrom<&InterfaceConfiguration> for Host {

fn try_from(config: &InterfaceConfiguration) -> Result<Self, Self::Error> {
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();
Expand Down
28 changes: 14 additions & 14 deletions src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -102,20 +102,20 @@ 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)
}
}

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)
}
}

Expand Down Expand Up @@ -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 },
})
}
Expand Down
14 changes: 8 additions & 6 deletions src/netlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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),
};
Expand Down
Loading
Loading