Skip to content

Commit

Permalink
restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed May 21, 2024
1 parent 4feb8f3 commit d7ccf58
Show file tree
Hide file tree
Showing 3 changed files with 410 additions and 328 deletions.
335 changes: 7 additions & 328 deletions iroh-net/src/net/interfaces/bsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ use tracing::warn;

use super::DefaultRouteDetails;

#[cfg(any(target_os = "freebsd", target_os = "openbsd", target_os = "netbsd"))]
use bsd_libc::*;
#[cfg(target_os = "freebsd")]
use freebsd::*;
mod freebsd;
#[cfg(target_os = "freebsd")]
use self::freebsd::*;

#[cfg(any(target_os = "macos", target_os = "ios"))]
use macos::*;
mod macos;
#[cfg(any(target_os = "macos", target_os = "ios"))]
use self::macos::*;

pub async fn default_route() -> Option<DefaultRouteDetails> {
let idx = default_route_interface_index()?;
Expand Down Expand Up @@ -534,330 +537,6 @@ pub struct InterfaceMulticastAddrMessage {
pub addrs: Vec<Addr>,
}

#[cfg(any(target_os = "macos", target_os = "ios"))]
mod macos {
use super::*;

// Hardcoded based on the generated values here: https://cs.opensource.google/go/x/net/+/master:route/zsys_darwin.go

pub(super) const SIZEOF_IF_MSGHDR_DARWIN15: usize = 0x70;
pub(super) const SIZEOF_IFA_MSGHDR_DARWIN15: usize = 0x14;
pub(super) const SIZEOF_IFMA_MSGHDR_DARWIN15: usize = 0x10;
pub(super) const SIZEOF_IF_MSGHDR2_DARWIN15: usize = 0xa0;
pub(super) const SIZEOF_IFMA_MSGHDR2_DARWIN15: usize = 0x14;
pub(super) const SIZEOF_IF_DATA_DARWIN15: usize = 0x60;
pub(super) const SIZEOF_IF_DATA64_DARWIN15: usize = 0x80;

pub(super) const SIZEOF_RT_MSGHDR_DARWIN15: usize = 0x5c;
pub(super) const SIZEOF_RT_MSGHDR2_DARWIN15: usize = 0x5c;
pub(super) const SIZEOF_RT_METRICS_DARWIN15: usize = 0x38;

pub(super) const SIZEOF_SOCKADDR_STORAGE: usize = 0x80;
pub(super) const SIZEOF_SOCKADDR_INET: usize = 0x10;
pub(super) const SIZEOF_SOCKADDR_INET6: usize = 0x1c;

pub(super) fn probe_routing_stack() -> RoutingStack {
let rtm_version = libc::RTM_VERSION;

let rtm = WireFormat {
ext_off: 36,
body_off: SIZEOF_RT_MSGHDR_DARWIN15,
typ: MessageType::Route,
};
let rtm2 = WireFormat {
ext_off: 36,
body_off: SIZEOF_RT_MSGHDR2_DARWIN15,
typ: MessageType::Route,
};
let ifm = WireFormat {
ext_off: 16,
body_off: SIZEOF_IF_MSGHDR_DARWIN15,
typ: MessageType::Interface,
};
let ifm2 = WireFormat {
ext_off: 32,
body_off: SIZEOF_IF_MSGHDR2_DARWIN15,
typ: MessageType::Interface,
};
let ifam = WireFormat {
ext_off: SIZEOF_IFA_MSGHDR_DARWIN15,
body_off: SIZEOF_IFA_MSGHDR_DARWIN15,
typ: MessageType::InterfaceAddr,
};
let ifmam = WireFormat {
ext_off: SIZEOF_IFMA_MSGHDR_DARWIN15,
body_off: SIZEOF_IFMA_MSGHDR_DARWIN15,
typ: MessageType::InterfaceMulticastAddr,
};
let ifmam2 = WireFormat {
ext_off: SIZEOF_IFMA_MSGHDR2_DARWIN15,
body_off: SIZEOF_IFMA_MSGHDR2_DARWIN15,
typ: MessageType::InterfaceMulticastAddr,
};

let wire_formats = [
(libc::RTM_ADD, rtm),
(libc::RTM_DELETE, rtm),
(libc::RTM_CHANGE, rtm),
(libc::RTM_GET, rtm),
(libc::RTM_LOSING, rtm),
(libc::RTM_REDIRECT, rtm),
(libc::RTM_MISS, rtm),
(libc::RTM_LOCK, rtm),
(libc::RTM_RESOLVE, rtm),
(libc::RTM_NEWADDR, ifam),
(libc::RTM_DELADDR, ifam),
(libc::RTM_IFINFO, ifm),
(libc::RTM_NEWMADDR, ifmam),
(libc::RTM_DELMADDR, ifmam),
(libc::RTM_IFINFO2, ifm2),
(libc::RTM_NEWMADDR2, ifmam2),
(libc::RTM_GET2, rtm2),
]
.into_iter()
.collect();

RoutingStack {
rtm_version,
wire_formats,
kernel_align: 4,
}
}
}

// Patch libc on freebsd
// https://github.com/rust-lang/libc/issues/3711
#[cfg(any(target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))]
pub(crate) mod bsd_libc {
use libc::c_int;
pub const LOCAL_PEERCRED: c_int = 1;

// net/route.h
pub const RTF_GATEWAY: c_int = 0x2;
pub const RTAX_DST: c_int = 0;
pub const RTAX_GATEWAY: c_int = 1;
pub const RTAX_NETMASK: c_int = 2;
pub const RTAX_IFP: c_int = 4;
pub const RTAX_BRD: c_int = 7;
pub const RTAX_MAX: c_int = 8;
pub const RTM_VERSION: c_int = 5;
pub const RTA_DST: c_int = 0x1;
pub const RTA_GATEWAY: c_int = 0x2;
pub const RTA_NETMASK: c_int = 0x4;
pub const RTA_GENMASK: c_int = 0x8;
pub const RTA_IFP: c_int = 0x10;
pub const RTA_IFA: c_int = 0x20;
pub const RTA_AUTHOR: c_int = 0x40;
pub const RTA_BRD: c_int = 0x80;

// Message types
pub const RTM_ADD: c_int = 0x1;
pub const RTM_DELETE: c_int = 0x2;
pub const RTM_CHANGE: c_int = 0x3;
pub const RTM_GET: c_int = 0x4;
pub const RTM_LOSING: c_int = 0x5;
pub const RTM_REDIRECT: c_int = 0x6;
pub const RTM_MISS: c_int = 0x7;
pub const RTM_LOCK: c_int = 0x8;
pub const RTM_OLDADD: c_int = 0x9;
pub const RTM_OLDDEL: c_int = 0xa;
pub const RTM_RESOLVE: c_int = 0xb;
pub const RTM_NEWADDR: c_int = 0xc;
pub const RTM_DELADDR: c_int = 0xd;
pub const RTM_IFINFO: c_int = 0xe;
pub const RTM_NEWMADDR: c_int = 0xf;
pub const RTM_DELMADDR: c_int = 0x10;
pub const RTM_IFANNOUNCE: c_int = 0x11;
pub const RTM_IEEE80211: c_int = 0x12;

pub const SHUT_RD: c_int = 0;
pub const SHUT_WR: c_int = 1;
pub const SHUT_RDWR: c_int = 2;
}

#[cfg(target_os = "freebsd")]
mod freebsd {
use super::*;

use super::bsd_libc::*;

// Hardcoded based on the generated values here: https://cs.opensource.google/go/x/net/+/master:route/zsys_freebsd_amd64.go

#[cfg(target_pointer_width = "64")]
pub(super) const SIZEOF_IF_MSGHDRL_FREEBSD10: usize = 0xb0;
#[cfg(target_pointer_width = "32")]
pub(super) const SIZEOF_IF_MSGHDRL_FREEBSD10: usize = 0x68;
pub(super) const SIZEOF_IFA_MSGHDR_FREEBSD10: usize = 0x14;

#[cfg(target_pointer_width = "64")]
pub(super) const SIZEOF_IFA_MSGHDRL_FREEBSD10: usize = 0xb0;
#[cfg(target_pointer_width = "32")]
pub(super) const SIZEOF_IFA_MSGHDRL_FREEBSD10: usize = 0x6c;

pub(super) const SIZEOF_IFMA_MSGHDR_FREEBSD10: usize = 0x10;
pub(super) const SIZEOF_IF_ANNOUNCEMSGHDR_FREEBSD10: usize = 0x18;

#[cfg(target_pointer_width = "64")]
pub(super) const SIZEOF_RT_MSGHDR_FREEBSD10: usize = 0x98;
#[cfg(target_pointer_width = "32")]
pub(super) const SIZEOF_RT_MSGHDR_FREEBSD10: usize = 0x5c;
#[cfg(target_pointer_width = "64")]
pub(super) const SIZEOF_RT_METRICS_FREEBSD10: usize = 0x70;
#[cfg(target_pointer_width = "32")]
pub(super) const SIZEOF_RT_METRICS_FREEBSD10: usize = 0x38;

#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
pub(super) const SIZEOF_IF_MSGHDR_FREEBSD7: usize = 0xa8;
#[cfg(target_arch = "x86")]
pub(super) const SIZEOF_IF_MSGHDR_FREEBSD7: usize = 0x60;
#[cfg(target_arch = "arm")]
pub(super) const SIZEOF_IF_MSGHDR_FREEBSD7: usize = 0x70;

#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
pub(super) const SIZEOF_IF_MSGHDR_FREEBSD8: usize = 0xa8;
#[cfg(target_arch = "x86")]
pub(super) const SIZEOF_IF_MSGHDR_FREEBSD8: usize = 0x60;
#[cfg(target_arch = "arm")]
pub(super) const SIZEOF_IF_MSGHDR_FREEBSD8: usize = 0x70;

#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
pub(super) const SIZEOF_IF_MSGHDR_FREEBSD9: usize = 0xa8;
#[cfg(target_arch = "x86")]
pub(super) const SIZEOF_IF_MSGHDR_FREEBSD9: usize = 0x60;
#[cfg(target_arch = "arm")]
pub(super) const SIZEOF_IF_MSGHDR_FREEBSD9: usize = 0x70;

#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
pub(super) const SIZEOF_IF_MSGHDR_FREEBSD10: usize = 0xa8;
#[cfg(target_arch = "x86")]
pub(super) const SIZEOF_IF_MSGHDR_FREEBSD10: usize = 0x64;
#[cfg(target_arch = "arm")]
pub(super) const SIZEOF_IF_MSGHDR_FREEBSD10: usize = 0x70;

#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
pub(super) const SIZEOF_IF_MSGHDR_FREEBSD11: usize = 0xa8;
#[cfg(target_arch = "x86")]
pub(super) const SIZEOF_IF_MSGHDR_FREEBSD11: usize = 0xa8;
#[cfg(target_arch = "arm")]
pub(super) const SIZEOF_IF_MSGHDR_FREEBSD11: usize = 0xa8;

#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
pub(super) const SIZE_OF_IF_DATA_FREEBSD7: usize = 0x98;
#[cfg(target_arch = "x86")]
pub(super) const SIZEOF_IF_DATA_FREEBSD7: usize = 0x50;
#[cfg(target_arch = "arm")]
pub(super) const SIZEOF_IF_DATA_FREEBSD7: usize = 0x60;

#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
pub(super) const SIZE_OF_IF_DATA_FREEBSD8: usize = 0x98;
#[cfg(target_arch = "x86")]
pub(super) const SIZEOF_IF_DATA_FREEBSD8: usize = 0x50;
#[cfg(target_arch = "arm")]
pub(super) const SIZEOF_IF_DATA_FREEBSD8: usize = 0x60;

#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
pub(super) const SIZE_OF_IF_DATA_FREEBSD9: usize = 0x98;
#[cfg(target_arch = "x86")]
pub(super) const SIZEOF_IF_DATA_FREEBSD9: usize = 0x50;
#[cfg(target_arch = "arm")]
pub(super) const SIZEOF_IF_DATA_FREEBSD9: usize = 0x60;

#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
pub(super) const SIZE_OF_IF_DATA_FREEBSD10: usize = 0x98;
#[cfg(target_arch = "x86")]
pub(super) const SIZEOF_IF_DATA_FREEBSD10: usize = 0x54;
#[cfg(target_arch = "arm")]
pub(super) const SIZEOF_IF_DATA_FREEBSD10: usize = 0x60;

#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
pub(super) const SIZE_OF_IF_DATA_FREEBSD11: usize = 0x98;
#[cfg(target_arch = "x86")]
pub(super) const SIZEOF_IF_DATA_FREEBSD11: usize = 0x98;
#[cfg(target_arch = "arm")]
pub(super) const SIZEOF_IF_DATA_FREEBSD11: usize = 0x98;

pub(super) const SIZEOF_IF_MSGHDRL_FREEBSD10_EMU: usize = 0xb0;
pub(super) const SIZEOF_IFA_MSGHDR_FREEBSD10_EMU: usize = 0x14;
pub(super) const SIZEOF_IFA_MSGHDRL_FREEBSD10_EMU: usize = 0xb0;
pub(super) const SIZEOF_IFMA_MSGHDR_FREEBSD10_EMU: usize = 0x10;
pub(super) const SIZEOF_IF_ANNOUNCEMSGHDR_FREEBSD10_EMU: usize = 0x18;

pub(super) const SIZEOF_RT_MSGHDR_FREEBSD10_EMU: usize = 0x98;
pub(super) const SIZEOF_RT_METRICS_FREEBSD10_EMU: usize = 0x70;

pub(super) const SIZEOF_IF_MSGHDR_FREEBSD7_EMU: usize = 0xa8;
pub(super) const SIZEOF_IF_MSGHDR_FREEBSD8_EMU: usize = 0xa8;
pub(super) const SIZEOF_IF_MSGHDR_FREEBSD9_EMU: usize = 0xa8;
pub(super) const SIZEOF_IF_MSGHDR_FREEBSD10_EMU: usize = 0xa8;
pub(super) const SIZEOF_IF_MSGHDR_FREEBSD11_EMU: usize = 0xa8;

pub(super) const SIZEOF_IF_DATA_FREEBSD7_EMU: usize = 0x98;
pub(super) const SIZEOF_IF_DATA_FREEBSD8_EMU: usize = 0x98;
pub(super) const SIZEOF_IF_DATA_FREEBSD9_EMU: usize = 0x98;
pub(super) const SIZEOF_IF_DATA_FREEBSD10_EMU: usize = 0x98;
pub(super) const SIZEOF_IF_DATA_FREEBSD11_EMU: usize = 0x98;

pub(super) const SIZEOF_SOCKADDR_STORAGE: usize = 0x80;
pub(super) const SIZEOF_SOCKADDR_INET: usize = 0x10;
pub(super) const SIZEOF_SOCKADDR_INET6: usize = 0x1c;

pub(super) fn probe_routing_stack() -> RoutingStack {
let rtm_version = RTM_VERSION;

let rtm = WireFormat {
ext_off: SIZEOF_RT_MSGHDR_FREEBSD10,
body_off: SIZEOF_RT_MSGHDR_FREEBSD10,
typ: MessageType::Route,
};
let ifm = WireFormat {
ext_off: SIZEOF_IF_MSGHDR_FREEBSD11,
body_off: SIZEOF_IF_MSGHDR_FREEBSD11,
typ: MessageType::Interface,
};
let ifam = WireFormat {
ext_off: SIZEOF_IFA_MSGHDR_FREEBSD10,
body_off: SIZEOF_IFA_MSGHDR_FREEBSD10,
typ: MessageType::InterfaceAddr,
};
let ifmam = WireFormat {
ext_off: SIZEOF_IFMA_MSGHDR_FREEBSD10,
body_off: SIZEOF_IFMA_MSGHDR_FREEBSD10,
typ: MessageType::InterfaceMulticastAddr,
};
let ifannm = WireFormat {
ext_off: SIZEOF_IF_ANNOUNCEMSGHDR_FREEBSD10,
body_off: SIZEOF_IF_ANNOUNCEMSGHDR_FREEBSD10,
typ: MessageType::Interface,
};

let wire_formats = [
(RTM_ADD, rtm),
(RTM_DELETE, rtm),
(RTM_CHANGE, rtm),
(RTM_GET, rtm),
(RTM_LOSING, rtm),
(RTM_REDIRECT, rtm),
(RTM_MISS, rtm),
(RTM_LOCK, rtm),
(RTM_RESOLVE, rtm),
(RTM_NEWADDR, ifam),
(RTM_DELADDR, ifam),
(RTM_IFINFO, ifm),
(RTM_NEWMADDR, ifmam),
(RTM_DELMADDR, ifmam),
(RTM_IFANNOUNCE, ifannm),
(RTM_IEEE80211, ifannm),
]
.into_iter()
.collect();
RoutingStack {
rtm_version,
wire_formats,
kernel_align: 4,
}
}
}

/// Represents a type of routing information base.
type RIBType = i32;

Expand Down
Loading

0 comments on commit d7ccf58

Please sign in to comment.