Skip to content

Commit

Permalink
Merge pull request #89 from shellrow/88-replace-ipnet-impl
Browse files Browse the repository at this point in the history
#88 Replace ip.rs impl with ipnet
  • Loading branch information
shellrow authored Sep 20, 2024
2 parents 10843e9 + c13a205 commit 16464e6
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 392 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ license = "MIT"

[dependencies]
serde = { version = "1", features = ["derive"], optional = true }
ipnet = { version = "2.10" }

[target.'cfg(unix)'.dependencies]
libc = "0.2"
Expand All @@ -36,7 +37,7 @@ system-configuration = "0.6"
serde_json = "1.0"

[features]
serde = ["dep:serde"]
serde = ["dep:serde", "ipnet/serde"]

[[example]]
name = "list_interfaces"
Expand Down
4 changes: 2 additions & 2 deletions src/gateway/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ pub fn get_default_gateway() -> Result<NetworkDevice, String> {
for iface in interfaces {
match local_ip {
IpAddr::V4(local_ipv4) => {
if iface.ipv4.iter().any(|x| x.addr == local_ipv4) {
if iface.ipv4.iter().any(|x| x.addr() == local_ipv4) {
if let Some(gateway) = iface.gateway {
return Ok(gateway);
}
}
}
IpAddr::V6(local_ipv6) => {
if iface.ipv6.iter().any(|x| x.addr == local_ipv6) {
if iface.ipv6.iter().any(|x| x.addr() == local_ipv6) {
if let Some(gateway) = iface.gateway {
return Ok(gateway);
}
Expand Down
21 changes: 12 additions & 9 deletions src/interface/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,10 @@ pub mod netlink {
}
4 => {
let ip = Ipv4Addr::from(<[u8; 4]>::try_from(addr).unwrap());
interface
.ipv4
.push(Ipv4Net::new_with_netmask(ip, Ipv4Addr::UNSPECIFIED));
match Ipv4Net::with_netmask(ip, Ipv4Addr::UNSPECIFIED) {
Ok(ipv4) => interface.ipv4.push(ipv4),
Err(_) => {}
}
}
_ => {
// unclear what these would be
Expand Down Expand Up @@ -149,15 +150,17 @@ pub mod netlink {
AddressNla::Address(addr) => match addr.len() {
4 => {
let ip = Ipv4Addr::from(<[u8; 4]>::try_from(addr).unwrap());
interface
.ipv4
.push(Ipv4Net::new(ip, addr_msg.header.prefix_len));
match Ipv4Net::new(ip, addr_msg.header.prefix_len) {
Ok(ipv4) => interface.ipv4.push(ipv4),
Err(_) => {}
}
}
16 => {
let ip = Ipv6Addr::from(<[u8; 16]>::try_from(addr).unwrap());
interface
.ipv6
.push(Ipv6Net::new(ip, addr_msg.header.prefix_len));
match Ipv6Net::new(ip, addr_msg.header.prefix_len) {
Ok(ipv6) => interface.ipv6.push(ipv6),
Err(_) => {}
}
}
_ => {
// what else?
Expand Down
10 changes: 5 additions & 5 deletions src/interface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ mod android;
mod macos;

use crate::device::NetworkDevice;
use crate::ip::{Ipv4Net, Ipv6Net};
use crate::ipnet::{Ipv4Net, Ipv6Net};
use crate::mac::MacAddr;
use crate::sys;
use std::net::IpAddr;
Expand Down Expand Up @@ -98,12 +98,12 @@ impl Interface {
for iface in interfaces {
match local_ip {
IpAddr::V4(local_ipv4) => {
if iface.ipv4.iter().any(|x| x.addr == local_ipv4) {
if iface.ipv4.iter().any(|x| x.addr() == local_ipv4) {
return Ok(iface);
}
}
IpAddr::V6(local_ipv6) => {
if iface.ipv6.iter().any(|x| x.addr == local_ipv6) {
if iface.ipv6.iter().any(|x| x.addr() == local_ipv6) {
return Ok(iface);
}
}
Expand Down Expand Up @@ -181,12 +181,12 @@ pub fn get_default_interface() -> Result<Interface, String> {
for iface in interfaces {
match local_ip {
IpAddr::V4(local_ipv4) => {
if iface.ipv4.iter().any(|x| x.addr == local_ipv4) {
if iface.ipv4.iter().any(|x| x.addr() == local_ipv4) {
return Ok(iface);
}
}
IpAddr::V6(local_ipv6) => {
if iface.ipv6.iter().any(|x| x.addr == local_ipv6) {
if iface.ipv6.iter().any(|x| x.addr() == local_ipv6) {
return Ok(iface);
}
}
Expand Down
38 changes: 23 additions & 15 deletions src/interface/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::Interface;
use super::MacAddr;
use crate::gateway;
use crate::interface::InterfaceType;
use crate::ip::{Ipv4Net, Ipv6Net};
use crate::ipnet::{Ipv4Net, Ipv6Net};
use crate::sys;
use libc;
use std::ffi::{CStr, CString};
Expand Down Expand Up @@ -62,13 +62,13 @@ pub fn interfaces() -> Vec<Interface> {
iface.gateway = Some(gateway.clone());
}
iface.ipv4.iter().for_each(|ipv4| {
if IpAddr::V4(ipv4.addr) == local_ip {
if IpAddr::V4(ipv4.addr()) == local_ip {
iface.dns_servers = get_system_dns_conf();
iface.default = true;
}
});
iface.ipv6.iter().for_each(|ipv6| {
if IpAddr::V6(ipv6.addr) == local_ip {
if IpAddr::V6(ipv6.addr()) == local_ip {
iface.dns_servers = get_system_dns_conf();
iface.default = true;
}
Expand Down Expand Up @@ -97,13 +97,13 @@ pub fn interfaces() -> Vec<Interface> {
}
match local_ip {
IpAddr::V4(local_ipv4) => {
if iface.ipv4.iter().any(|x| x.addr == local_ipv4) {
if iface.ipv4.iter().any(|x| x.addr() == local_ipv4) {
iface.default = true;
iface.dns_servers = get_system_dns_conf();
}
}
IpAddr::V6(local_ipv6) => {
if iface.ipv6.iter().any(|x| x.addr == local_ipv6) {
if iface.ipv6.iter().any(|x| x.addr() == local_ipv6) {
iface.default = true;
iface.dns_servers = get_system_dns_conf();
}
Expand All @@ -126,13 +126,13 @@ pub fn interfaces() -> Vec<Interface> {
iface.gateway = Some(gateway.clone());
}
iface.ipv4.iter().for_each(|ipv4| {
if IpAddr::V4(ipv4.addr) == local_ip {
if IpAddr::V4(ipv4.addr()) == local_ip {
iface.dns_servers = get_system_dns_conf();
iface.default = true;
}
});
iface.ipv6.iter().for_each(|ipv6| {
if IpAddr::V6(ipv6.addr) == local_ip {
if IpAddr::V6(ipv6.addr()) == local_ip {
iface.dns_servers = get_system_dns_conf();
iface.default = true;
}
Expand Down Expand Up @@ -310,8 +310,10 @@ fn unix_interfaces_inner(
},
None => Ipv4Addr::UNSPECIFIED,
};
let ipv4_net: Ipv4Net = Ipv4Net::new_with_netmask(ipv4, netmask);
ini_ipv4.push(ipv4_net);
match Ipv4Net::with_netmask(ipv4, netmask) {
Ok(ipv4_net) => ini_ipv4.push(ipv4_net),
Err(_) => {}
}
}
IpAddr::V6(ipv6) => {
let netmask: Ipv6Addr = match netmask {
Expand All @@ -321,8 +323,10 @@ fn unix_interfaces_inner(
},
None => Ipv6Addr::UNSPECIFIED,
};
let ipv6_net: Ipv6Net = Ipv6Net::new_with_netmask(ipv6, netmask);
ini_ipv6.push(ipv6_net);
match Ipv6Net::with_netmask(ipv6, netmask) {
Ok(ipv6_net) => ini_ipv6.push(ipv6_net),
Err(_) => {}
}
}
}
}
Expand Down Expand Up @@ -358,8 +362,10 @@ fn unix_interfaces_inner(
},
None => Ipv4Addr::UNSPECIFIED,
};
let ipv4_net: Ipv4Net = Ipv4Net::new_with_netmask(ipv4, netmask);
iface.ipv4.push(ipv4_net);
match Ipv4Net::with_netmask(ipv4, netmask) {
Ok(ipv4_net) => iface.ipv4.push(ipv4_net),
Err(_) => {}
}
}
IpAddr::V6(ipv6) => {
let netmask: Ipv6Addr = match netmask {
Expand All @@ -369,8 +375,10 @@ fn unix_interfaces_inner(
},
None => Ipv6Addr::UNSPECIFIED,
};
let ipv6_net: Ipv6Net = Ipv6Net::new_with_netmask(ipv6, netmask);
iface.ipv6.push(ipv6_net);
match Ipv6Net::with_netmask(ipv6, netmask) {
Ok(ipv6_net) => iface.ipv6.push(ipv6_net),
Err(_) => {}
}
}
}
}
Expand Down
24 changes: 12 additions & 12 deletions src/interface/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use windows_sys::Win32::Networking::WinSock::{

use crate::device::NetworkDevice;
use crate::interface::{Interface, InterfaceType};
use crate::ip::{Ipv4Net, Ipv6Net};
use crate::ipnet::{Ipv4Net, Ipv6Net};
use crate::mac::MacAddr;
use crate::sys;
use std::ffi::CStr;
Expand Down Expand Up @@ -205,14 +205,14 @@ pub fn interfaces() -> Vec<Interface> {
};
let prefix_len = cur_a.OnLinkPrefixLength;
match ip_addr {
IpAddr::V4(ipv4) => {
let ipv4_net: Ipv4Net = Ipv4Net::new(ipv4, prefix_len);
ipv4_vec.push(ipv4_net);
}
IpAddr::V6(ipv6) => {
let ipv6_net: Ipv6Net = Ipv6Net::new(ipv6, prefix_len);
ipv6_vec.push(ipv6_net);
}
IpAddr::V4(ipv4) => match Ipv4Net::new(ipv4, prefix_len) {
Ok(ipv4_net) => ipv4_vec.push(ipv4_net),
Err(_) => {}
},
IpAddr::V6(ipv6) => match Ipv6Net::new(ipv6, prefix_len) {
Ok(ipv6_net) => ipv6_vec.push(ipv6_net),
Err(_) => {}
},
}
}
// Gateway
Expand All @@ -225,7 +225,7 @@ pub fn interfaces() -> Vec<Interface> {
match gateway_ip {
IpAddr::V4(ipv4) => {
if let Some(ip_net) = ipv4_vec.first() {
let mac_addr = get_mac_through_arp(ip_net.addr, ipv4);
let mac_addr = get_mac_through_arp(ip_net.addr(), ipv4);
default_gateway.mac_addr = mac_addr;
default_gateway.ipv4.push(ipv4);
}
Expand All @@ -243,8 +243,8 @@ pub fn interfaces() -> Vec<Interface> {
.filter_map(|cur_d| unsafe { socket_address_to_ipaddr(&cur_d.Address) })
.collect();
let default: bool = match local_ip {
IpAddr::V4(local_ipv4) => ipv4_vec.iter().any(|x| x.addr == local_ipv4),
IpAddr::V6(local_ipv6) => ipv6_vec.iter().any(|x| x.addr == local_ipv6),
IpAddr::V4(local_ipv4) => ipv4_vec.iter().any(|x| x.addr() == local_ipv4),
IpAddr::V6(local_ipv6) => ipv6_vec.iter().any(|x| x.addr() == local_ipv6),
};
let interface: Interface = Interface {
index,
Expand Down
Loading

0 comments on commit 16464e6

Please sign in to comment.