Merge pull request #38 from shellrow/dev

Dev
main
shellrow 1 year ago committed by GitHub
commit 9061d447b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      Cargo.toml
  2. 33
      src/gateway/macos.rs

@ -1,6 +1,6 @@
[package] [package]
name = "default-net" name = "default-net"
version = "0.16.1" version = "0.16.2"
authors = ["shellrow <shellrow@protonmail.com>"] authors = ["shellrow <shellrow@protonmail.com>"]
edition = "2021" edition = "2021"
description = "Cross-platform library for network interface and gateway" description = "Cross-platform library for network interface and gateway"

@ -175,35 +175,10 @@ unsafe fn sa_to_ip(sa: &sockaddr) -> Option<IpAddr> {
} }
} }
unsafe fn sa_to_link(sa: &sockaddr) -> Option<(Option<[u8; 6]>, u16)> {
match sa.sa_family as u32 {
AF_LINK => {
let sa_dl = sa as *const _ as *const sockaddr_dl;
let ifindex = (*sa_dl).sdl_index;
let mac;
if (*sa_dl).sdl_alen >= 6 {
let i = (*sa_dl).sdl_nlen as usize;
let a = (*sa_dl).sdl_data[i + 0] as u8;
let b = (*sa_dl).sdl_data[i + 1] as u8;
let c = (*sa_dl).sdl_data[i + 2] as u8;
let d = (*sa_dl).sdl_data[i + 3] as u8;
let e = (*sa_dl).sdl_data[i + 4] as u8;
let f = (*sa_dl).sdl_data[i + 5] as u8;
mac = Some([a, b, c, d, e, f]);
} else {
mac = None;
}
Some((mac, ifindex))
}
_ => None,
}
}
fn message_to_route(hdr: &rt_msghdr, msg: *mut u8) -> Option<Route> { fn message_to_route(hdr: &rt_msghdr, msg: *mut u8) -> Option<Route> {
let destination; let destination;
let mut gateway = None; let mut gateway = None;
let mut ifindex = None; let ifindex = None;
if hdr.rtm_addrs & (1 << RTAX_DST) == 0 { if hdr.rtm_addrs & (1 << RTAX_DST) == 0 {
return None; return None;
@ -225,12 +200,6 @@ fn message_to_route(hdr: &rt_msghdr, msg: *mut u8) -> Option<Route> {
std::mem::transmute((msg as *mut sockaddr).add(RTAX_GATEWAY as usize)); std::mem::transmute((msg as *mut sockaddr).add(RTAX_GATEWAY as usize));
gateway = sa_to_ip(gw_sa); gateway = sa_to_ip(gw_sa);
if gateway.is_none() {
if let Some((_mac, ifidx)) = sa_to_link(gw_sa) {
ifindex = Some(ifidx as u32);
}
}
} }
} }

Loading…
Cancel
Save