diff --git a/zerotier-network-hypervisor/src/vl1/inetaddress.rs b/zerotier-network-hypervisor/src/vl1/inetaddress.rs index 6ae04fd5e..e67110642 100644 --- a/zerotier-network-hypervisor/src/vl1/inetaddress.rs +++ b/zerotier-network-hypervisor/src/vl1/inetaddress.rs @@ -38,13 +38,13 @@ type sockaddr_storage = libc::sockaddr_storage; type in6_addr = libc::in6_addr; #[cfg(all(not(target_os = "windows"), not(target_os = "linux")))] -type INetType = u8; +pub type AddressFamilyType = u8; #[cfg(target_os = "linux")] -type InetType = u16; +pub type AddressFamilyType = u16; -pub const AF_INET: InetType = libc::AF_INET as InetType; -pub const AF_INET6: InetType = libc::AF_INET6 as InetType; +pub const AF_INET: AddressFamilyType = libc::AF_INET as AddressFamilyType; +pub const AF_INET6: AddressFamilyType = libc::AF_INET6 as AddressFamilyType; #[repr(u8)] pub enum IpScope { @@ -500,7 +500,7 @@ impl InetAddress { /// Whether this is IPv4 or IPv6 is inferred from the size of ip[], which must be /// either 4 or 16 bytes. The family (AF_INET or AF_INET6) is returned, or zero on /// failure. - pub fn set(&mut self, ip: &[u8], port: u16) -> InetType { + pub fn set(&mut self, ip: &[u8], port: u16) -> AddressFamilyType { self.zero(); let port = port.to_be(); unsafe { @@ -523,7 +523,7 @@ impl InetAddress { /// Get raw IP bytes, with length dependent on address family (4 or 16). pub fn ip_bytes(&self) -> &[u8] { unsafe { - match self.sa.sa_family as InetType { + match self.sa.sa_family as AddressFamilyType { AF_INET => &*(&self.sin.sin_addr.s_addr as *const u32).cast::<[u8; 4]>(), AF_INET6 => &*(&self.sin6.sin6_addr as *const in6_addr).cast::<[u8; 16]>(), _ => &[], @@ -545,7 +545,7 @@ impl InetAddress { /// Get the IP port for this InetAddress. pub fn port(&self) -> u16 { unsafe { - u16::from_be(match self.sa.sa_family as InetType { + u16::from_be(match self.sa.sa_family as AddressFamilyType { AF_INET => self.sin.sin_port as u16, AF_INET6 => self.sin6.sin6_port as u16, _ => 0, @@ -560,7 +560,7 @@ impl InetAddress { pub fn set_port(&mut self, port: u16) { let port = port.to_be(); unsafe { - match self.sa.sa_family as InetType { + match self.sa.sa_family as AddressFamilyType { AF_INET => self.sin.sin_port = port, AF_INET6 => self.sin6.sin6_port = port, _ => {} @@ -575,7 +575,7 @@ impl InetAddress { unsafe { if self.sa.sa_family == cidr.sa.sa_family { let mut cidr_bits = cidr.port() as u32; - match self.sa.sa_family as InetType { + match self.sa.sa_family as AddressFamilyType { AF_INET => { if cidr_bits <= 32 { let discard_bits = 32 - cidr_bits; @@ -612,7 +612,7 @@ impl InetAddress { /// Get this IP address's scope as per RFC documents and what is advertised via BGP. pub fn scope(&self) -> IpScope { unsafe { - match self.sa.sa_family as InetType { + match self.sa.sa_family as AddressFamilyType { AF_INET => { let ip = self.sin.sin_addr.s_addr as u32; let class_a = (ip >> 24) as u8; @@ -737,7 +737,7 @@ impl InetAddress { /// Get only the IP portion of this address as a string. pub fn to_ip_string(&self) -> String { unsafe { - match self.sa.sa_family as InetType { + match self.sa.sa_family as AddressFamilyType { AF_INET => { let ip = &*(&self.sin.sin_addr.s_addr as *const u32).cast::<[u8; 4]>(); format!("{}.{}.{}.{}", ip[0], ip[1], ip[2], ip[3]) @@ -754,7 +754,7 @@ impl Marshalable for InetAddress { fn marshal(&self, buf: &mut Buffer) -> std::io::Result<()> { unsafe { - match self.sa.sa_family as InetType { + match self.sa.sa_family as AddressFamilyType { AF_INET => { let b = buf.append_bytes_fixed_get_mut::<7>()?; b[0] = 4; @@ -794,7 +794,7 @@ impl ToString for InetAddress { fn to_string(&self) -> String { unsafe { let mut s = self.to_ip_string(); - match self.sa.sa_family as InetType { + match self.sa.sa_family as AddressFamilyType { AF_INET => { s.push('/'); s.push_str(u16::from_be(self.sin.sin_port as u16).to_string().as_str()) @@ -858,7 +858,7 @@ impl PartialEq for InetAddress { fn eq(&self, other: &Self) -> bool { unsafe { if self.sa.sa_family == other.sa.sa_family { - match self.sa.sa_family as InetType { + match self.sa.sa_family as AddressFamilyType { AF_INET => self.sin.sin_port == other.sin.sin_port && self.sin.sin_addr.s_addr == other.sin.sin_addr.s_addr, AF_INET6 => { if self.sin6.sin6_port == other.sin6.sin6_port { @@ -891,7 +891,7 @@ impl Ord for InetAddress { fn cmp(&self, other: &Self) -> Ordering { unsafe { if self.sa.sa_family == other.sa.sa_family { - match self.sa.sa_family as InetType { + match self.sa.sa_family as AddressFamilyType { 0 => Ordering::Equal, AF_INET => { let ip_ordering = u32::from_be(self.sin.sin_addr.s_addr as u32).cmp(&u32::from_be(other.sin.sin_addr.s_addr as u32)); @@ -917,17 +917,17 @@ impl Ord for InetAddress { } } } else { - match self.sa.sa_family as InetType { + match self.sa.sa_family as AddressFamilyType { 0 => Ordering::Less, AF_INET => { - if other.sa.sa_family as InetType == AF_INET6 { + if other.sa.sa_family as AddressFamilyType == AF_INET6 { Ordering::Less } else { self.sa.sa_family.cmp(&other.sa.sa_family) } } AF_INET6 => { - if other.sa.sa_family as InetType == AF_INET { + if other.sa.sa_family as AddressFamilyType == AF_INET { Ordering::Greater } else { self.sa.sa_family.cmp(&other.sa.sa_family) @@ -948,7 +948,7 @@ impl Hash for InetAddress { unsafe { state.write_u8(self.sa.sa_family as u8); - match self.sa.sa_family as InetType { + match self.sa.sa_family as AddressFamilyType { AF_INET => { state.write_u16(self.sin.sin_port as u16); state.write_u32(self.sin.sin_addr.s_addr as u32); diff --git a/zerotier-network-hypervisor/src/vl1/mod.rs b/zerotier-network-hypervisor/src/vl1/mod.rs index 63c5a3ddc..80c9409e0 100644 --- a/zerotier-network-hypervisor/src/vl1/mod.rs +++ b/zerotier-network-hypervisor/src/vl1/mod.rs @@ -21,7 +21,7 @@ pub use address::Address; pub use dictionary::Dictionary; pub use endpoint::Endpoint; pub use identity::*; -pub use inetaddress::{InetAddress, IpScope}; +pub use inetaddress::{AddressFamilyType, InetAddress, IpScope, AF_INET, AF_INET6}; pub use mac::MAC; pub use node::SystemInterface; pub use path::Path; diff --git a/zerotier-system-service/src/udp.rs b/zerotier-system-service/src/udp.rs index 39086a9ad..305750b97 100644 --- a/zerotier-system-service/src/udp.rs +++ b/zerotier-system-service/src/udp.rs @@ -19,7 +19,7 @@ use crate::localinterface::LocalInterface; #[allow(unused_imports)] use num_traits::AsPrimitive; -use zerotier_network_hypervisor::vl1::{InetAddress, IpScope}; +use zerotier_network_hypervisor::vl1::{InetAddress, IpScope, AF_INET, AF_INET6}; /// A local port to which one or more UDP sockets is bound. /// @@ -201,8 +201,8 @@ impl BoundUdpPort { #[cfg(unix)] unsafe fn bind_udp_to_device(device_name: &str, address: &InetAddress) -> Result { let (af, sa_len) = match address.family() { - InetAddress::AF_INET => (libc::AF_INET, std::mem::size_of::().as_()), - InetAddress::AF_INET6 => (libc::AF_INET6, std::mem::size_of::().as_()), + AF_INET => (libc::AF_INET, std::mem::size_of::().as_()), + AF_INET6 => (libc::AF_INET6, std::mem::size_of::().as_()), _ => { return Err("unrecognized address family"); }