Skip to content

Commit 0db4ea7

Browse files
authored
Rename IpAddrMask.ip to .address and use u16 for port (#105)
1 parent 1448c9a commit 0db4ea7

File tree

7 files changed

+64
-62
lines changed

7 files changed

+64
-62
lines changed

Cargo.lock

Lines changed: 23 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "defguard_wireguard_rs"
3-
version = "0.8.0"
3+
version = "0.9.0"
44
edition = "2024"
55
rust-version = "1.85"
66
description = "A unified multi-platform high-level API for managing WireGuard interfaces"

src/bsd/mod.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl IpAddrMask {
115115
.and_then(|ipv6| <[u8; 16]>::try_from(ipv6).ok().map(IpAddr::from)),
116116
}
117117
.map(|ip| Self {
118-
ip,
118+
address: ip,
119119
cidr: cidr as u8,
120120
})
121121
})
@@ -129,7 +129,7 @@ impl<'a> IpAddrMask {
129129

130130
nvlist.append_number(NV_CIDR, u64::from(self.cidr));
131131

132-
match self.ip {
132+
match self.address {
133133
IpAddr::V4(ipv4) => nvlist.append_bytes(NV_IPV4, ipv4.octets().into()),
134134
IpAddr::V6(ipv6) => nvlist.append_bytes(NV_IPV6, ipv6.octets().into()),
135135
}
@@ -340,7 +340,7 @@ pub fn delete_interface(if_name: &str) -> Result<(), IoError> {
340340
}
341341

342342
pub fn set_address(if_name: &str, address: &IpAddrMask) -> Result<(), IoError> {
343-
match address.ip {
343+
match address.address {
344344
IpAddr::V4(address) => {
345345
let ifreq = IfReq::new_with_address(if_name, address);
346346
ifreq.set_address()
@@ -356,7 +356,7 @@ pub fn assign_address(if_name: &str, address: &IpAddrMask) -> Result<(), IoError
356356
let broadcast = address.broadcast();
357357
let mask = address.mask();
358358

359-
match (address.ip, broadcast, mask) {
359+
match (address.address, broadcast, mask) {
360360
(IpAddr::V4(address), IpAddr::V4(broadcast), IpAddr::V4(mask)) => {
361361
let inaliasreq = InAliasReq::new(if_name, address, broadcast, mask);
362362
inaliasreq.add_address()
@@ -370,7 +370,7 @@ pub fn assign_address(if_name: &str, address: &IpAddrMask) -> Result<(), IoError
370370
}
371371

372372
pub fn remove_address(if_name: &str, address: &IpAddrMask) -> Result<(), IoError> {
373-
match address.ip {
373+
match address.address {
374374
IpAddr::V4(address) => {
375375
let ifreq = IfReq::new_with_address(if_name, address);
376376
ifreq.delete_address()
@@ -459,7 +459,7 @@ pub fn get_gateway(ip_version: IpVersion) -> Result<Option<IpAddr>, IoError> {
459459
/// Add routing gateway.
460460
pub fn add_gateway(dest: &IpAddrMask, gateway: IpAddr, is_blackhole: bool) -> Result<(), IoError> {
461461
debug!("Adding gateway: destination {dest}, gateway {gateway}, is blackhole {is_blackhole}.");
462-
match (dest.ip, dest.mask(), gateway) {
462+
match (dest.address, dest.mask(), gateway) {
463463
(IpAddr::V4(ip), IpAddr::V4(mask), IpAddr::V4(gw)) => {
464464
let payload = DestAddrMask::<SockAddrIn>::new(ip.into(), mask.into(), gw.into());
465465
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
480480
/// Remove routing gateway.
481481
pub fn delete_gateway(dest: &IpAddrMask) -> Result<(), IoError> {
482482
debug!("Deleting gateway with destination {dest}.");
483-
match (dest.ip, dest.mask()) {
483+
match (dest.address, dest.mask()) {
484484
(IpAddr::V4(ip), IpAddr::V4(mask)) => {
485485
let payload =
486486
DestAddrMask::<SockAddrIn>::new(ip.into(), mask.into(), SockAddrIn::default());
@@ -508,7 +508,7 @@ pub fn add_linked_route(dest: &IpAddrMask, if_name: &str) -> Result<(), IoError>
508508
if if_index == 0 {
509509
return Err(IoError::NetworkInterface);
510510
}
511-
match (dest.ip, dest.mask()) {
511+
match (dest.address, dest.mask()) {
512512
(IpAddr::V4(ip), IpAddr::V4(mask)) => {
513513
let link = SockAddrDl::new(if_index);
514514
let payload = GatewayLink::<SockAddrIn>::new(ip.into(), mask.into(), link);
@@ -535,7 +535,7 @@ pub fn add_route(dest: &IpAddrMask, if_name: &str) -> Result<(), IoError> {
535535
if if_index == 0 {
536536
return Err(IoError::NetworkInterface);
537537
}
538-
match (dest.ip, dest.mask()) {
538+
match (dest.address, dest.mask()) {
539539
(IpAddr::V4(ip), IpAddr::V4(mask)) => {
540540
let payload =
541541
DestAddrMask::<SockAddrIn>::new_for_interface(ip.into(), mask.into(), if_name);
@@ -561,7 +561,7 @@ pub fn delete_route(dest: &IpAddrMask, if_name: &str) -> Result<(), IoError> {
561561
if if_index == 0 {
562562
return Err(IoError::NetworkInterface);
563563
}
564-
match (dest.ip, dest.mask()) {
564+
match (dest.address, dest.mask()) {
565565
(IpAddr::V4(ip), IpAddr::V4(mask)) => {
566566
let payload =
567567
DestAddrMask::<SockAddrIn>::new_for_interface(ip.into(), mask.into(), if_name);

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ pub struct InterfaceConfiguration {
105105
pub name: String,
106106
pub prvkey: String,
107107
pub addresses: Vec<IpAddrMask>,
108-
pub port: u32,
108+
pub port: u16,
109109
pub peers: Vec<Peer>,
110110
/// Maximum transfer unit. `None` means do not set MTU, but keep the system default.
111111
pub mtu: Option<u32>,
@@ -129,7 +129,7 @@ impl TryFrom<&InterfaceConfiguration> for Host {
129129

130130
fn try_from(config: &InterfaceConfiguration) -> Result<Self, Self::Error> {
131131
let key = config.prvkey.as_str().try_into()?;
132-
let mut host = Host::new(config.port as u16, key);
132+
let mut host = Host::new(config.port, key);
133133
for peercfg in &config.peers {
134134
let peer = peercfg.clone();
135135
let key: Key = peer.public_key.clone();

src/net.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,31 @@ use serde::{Deserialize, Serialize};
1919
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
2020
pub struct IpAddrMask {
2121
// IP v4 or v6
22-
pub ip: IpAddr,
22+
pub address: IpAddr,
2323
// Classless Inter-Domain Routing
2424
pub cidr: u8,
2525
}
2626

2727
impl IpAddrMask {
2828
#[must_use]
29-
pub fn new(ip: IpAddr, cidr: u8) -> Self {
30-
Self { ip, cidr }
29+
pub fn new(address: IpAddr, cidr: u8) -> Self {
30+
Self { address, cidr }
3131
}
3232

3333
#[must_use]
34-
pub fn host(ip: IpAddr) -> Self {
35-
let cidr = match ip {
34+
pub fn host(address: IpAddr) -> Self {
35+
let cidr = match address {
3636
IpAddr::V4(_) => 32,
3737
IpAddr::V6(_) => 128,
3838
};
39-
Self { ip, cidr }
39+
Self { address, cidr }
4040
}
4141

4242
/// Returns broadcast address as `IpAddr`.
4343
/// Note: IPv6 does not really use broadcast.
4444
#[must_use]
4545
pub fn broadcast(&self) -> IpAddr {
46-
match self.ip {
46+
match self.address {
4747
IpAddr::V4(ip) => {
4848
let addr = u32::from(ip);
4949
let bits = if self.cidr >= 32 {
@@ -68,7 +68,7 @@ impl IpAddrMask {
6868
/// Returns network mask as `IpAddr`.
6969
#[must_use]
7070
pub fn mask(&self) -> IpAddr {
71-
match self.ip {
71+
match self.address {
7272
IpAddr::V4(_) => {
7373
let mask = if self.cidr == 0 {
7474
0
@@ -91,7 +91,7 @@ impl IpAddrMask {
9191
/// Returns `true` if the address defines a host, `false` if it is a network.
9292
#[must_use]
9393
pub fn is_host(&self) -> bool {
94-
if self.ip.is_ipv4() {
94+
if self.address.is_ipv4() {
9595
self.cidr == 32
9696
} else {
9797
self.cidr == 128
@@ -102,20 +102,20 @@ impl IpAddrMask {
102102
#[must_use]
103103
pub fn to_nlas_allowed_ip(&self) -> WgAllowedIp {
104104
let mut attrs = Vec::new();
105-
attrs.push(WgAllowedIpAttrs::Family(if self.ip.is_ipv4() {
105+
attrs.push(WgAllowedIpAttrs::Family(if self.address.is_ipv4() {
106106
AF_INET
107107
} else {
108108
AF_INET6
109109
}));
110-
attrs.push(WgAllowedIpAttrs::IpAddr(self.ip));
110+
attrs.push(WgAllowedIpAttrs::IpAddr(self.address));
111111
attrs.push(WgAllowedIpAttrs::Cidr(self.cidr));
112112
WgAllowedIp(attrs)
113113
}
114114
}
115115

116116
impl fmt::Display for IpAddrMask {
117117
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
118-
write!(f, "{}/{}", self.ip, self.cidr)
118+
write!(f, "{}/{}", self.address, self.cidr)
119119
}
120120
}
121121

@@ -144,11 +144,11 @@ impl FromStr for IpAddrMask {
144144
if cidr > max_cidr {
145145
return Err(IpAddrParseError);
146146
}
147-
Ok(IpAddrMask { ip, cidr })
147+
Ok(IpAddrMask { address: ip, cidr })
148148
} else {
149149
let ip = ip_str.parse().map_err(|_| IpAddrParseError)?;
150150
Ok(IpAddrMask {
151-
ip,
151+
address: ip,
152152
cidr: if ip.is_ipv4() { 32 } else { 128 },
153153
})
154154
}

src/netlink.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl Key {
102102
impl IpAddrMask {
103103
#[must_use]
104104
fn address_family(&self) -> AddressFamily {
105-
match self.ip {
105+
match self.address {
106106
IpAddr::V4(_) => AddressFamily::Inet,
107107
IpAddr::V6(_) => AddressFamily::Inet6,
108108
}
@@ -260,18 +260,20 @@ fn set_address(index: u32, address: &IpAddrMask) -> NetlinkResult<()> {
260260
message.header.index = index;
261261
message.header.family = address.address_family();
262262

263-
if address.ip.is_multicast() {
264-
if let IpAddr::V6(addr) = address.ip {
263+
if address.address.is_multicast() {
264+
if let IpAddr::V6(addr) = address.address {
265265
message.attributes.push(AddressAttribute::Multicast(addr));
266266
}
267267
} else {
268268
message
269269
.attributes
270-
.push(AddressAttribute::Address(address.ip));
270+
.push(AddressAttribute::Address(address.address));
271271

272272
// For IPv4 the Local address can be set to the same value as
273273
// Address.
274-
message.attributes.push(AddressAttribute::Local(address.ip));
274+
message
275+
.attributes
276+
.push(AddressAttribute::Local(address.address));
275277

276278
// Set the broadcast address as well (IPv6 does not support
277279
// broadcast).
@@ -527,7 +529,7 @@ pub(crate) fn add_route(
527529
};
528530
header.address_family = address.address_family();
529531
header.destination_prefix_length = address.cidr;
530-
let route_address = match address.ip {
532+
let route_address = match address.address {
531533
IpAddr::V4(ipv4) => RouteAddress::Inet(ipv4),
532534
IpAddr::V6(ipv6) => RouteAddress::Inet6(ipv6),
533535
};

0 commit comments

Comments
 (0)