diff --git a/src/bpf/binding.rs b/src/bpf/binding.rs index 5d1ee88..4374b9b 100644 --- a/src/bpf/binding.rs +++ b/src/bpf/binding.rs @@ -88,4 +88,4 @@ pub struct bpf_hdr { extern "C" { pub fn ioctl(d: libc::c_int, request: libc::c_ulong, ...) -> libc::c_int; -} \ No newline at end of file +} diff --git a/src/bpf/mod.rs b/src/bpf/mod.rs index 14bc9e4..240d020 100644 --- a/src/bpf/mod.rs +++ b/src/bpf/mod.rs @@ -1,27 +1,11 @@ -#[cfg(any( - target_os = "openbsd", - target_os = "freebsd", - target_os = "netbsd" -))] +#[cfg(any(target_os = "openbsd", target_os = "freebsd", target_os = "netbsd"))] mod binding; -#[cfg(any( - target_os = "openbsd", - target_os = "freebsd", - target_os = "netbsd" -))] +#[cfg(any(target_os = "openbsd", target_os = "freebsd", target_os = "netbsd"))] pub use self::binding::*; -#[cfg(any( - target_os = "openbsd", - target_os = "freebsd", - target_os = "netbsd" -))] +#[cfg(any(target_os = "openbsd", target_os = "freebsd", target_os = "netbsd"))] mod unix; -#[cfg(any( - target_os = "openbsd", - target_os = "freebsd", - target_os = "netbsd" -))] +#[cfg(any(target_os = "openbsd", target_os = "freebsd", target_os = "netbsd"))] pub use self::unix::*; #[cfg(any( @@ -39,4 +23,4 @@ mod shared; target_os = "netbsd", target_os = "ios" ))] -pub use self::shared::*; \ No newline at end of file +pub use self::shared::*; diff --git a/src/bpf/shared.rs b/src/bpf/shared.rs index 2c106d3..5701712 100644 --- a/src/bpf/shared.rs +++ b/src/bpf/shared.rs @@ -13,4 +13,4 @@ pub struct sockaddr_dl { pub sdl_alen: libc::c_uchar, pub sdl_slen: libc::c_uchar, pub sdl_data: [libc::c_char; 46], -} \ No newline at end of file +} diff --git a/src/gateway/macos.rs b/src/gateway/macos.rs index 45b12c6..f445776 100644 --- a/src/gateway/macos.rs +++ b/src/gateway/macos.rs @@ -4,8 +4,9 @@ use super::Gateway; use crate::interface::MacAddr; use std::{ + collections::HashMap, io, - net::{IpAddr, Ipv4Addr, Ipv6Addr}, collections::HashMap, + net::{IpAddr, Ipv4Addr, Ipv6Addr}, }; const CTL_NET: u32 = 4; @@ -159,12 +160,12 @@ fn code_to_error(err: i32) -> io::Error { unsafe fn sa_to_ip(sa: &sockaddr) -> Option { match sa.sa_family as u32 { - AF_INET=> { + AF_INET => { let inet: &sockaddr_in = std::mem::transmute(sa); let octets: [u8; 4] = inet.sin_addr.s_addr.to_ne_bytes(); Some(IpAddr::from(octets)) } - AF_INET6=> { + AF_INET6 => { let inet6: &sockaddr_in6 = std::mem::transmute(sa); let octets: [u8; 16] = inet6.sin6_addr.__u6_addr.__u6_addr8; Some(IpAddr::from(octets)) @@ -346,10 +347,32 @@ fn message_to_arppair(msg_bytes: *mut u8) -> (IpAddr, MacAddr) { const IP_END_INDEX: usize = 7; const MAC_START_INDEX: usize = 24; const MAC_END_INDEX: usize = 29; - let ip_bytes = unsafe { std::slice::from_raw_parts(msg_bytes.add(IP_START_INDEX), IP_END_INDEX + 1 - IP_START_INDEX) }; - let mac_bytes = unsafe { std::slice::from_raw_parts(msg_bytes.add(MAC_START_INDEX), MAC_END_INDEX + 1 - MAC_START_INDEX) }; - let ip_addr = IpAddr::V4(Ipv4Addr::new(ip_bytes[0], ip_bytes[1], ip_bytes[2], ip_bytes[3])); - let mac_addr = MacAddr::new([mac_bytes[0], mac_bytes[1], mac_bytes[2], mac_bytes[3], mac_bytes[4], mac_bytes[5]]); + let ip_bytes = unsafe { + std::slice::from_raw_parts( + msg_bytes.add(IP_START_INDEX), + IP_END_INDEX + 1 - IP_START_INDEX, + ) + }; + let mac_bytes = unsafe { + std::slice::from_raw_parts( + msg_bytes.add(MAC_START_INDEX), + MAC_END_INDEX + 1 - MAC_START_INDEX, + ) + }; + let ip_addr = IpAddr::V4(Ipv4Addr::new( + ip_bytes[0], + ip_bytes[1], + ip_bytes[2], + ip_bytes[3], + )); + let mac_addr = MacAddr::new([ + mac_bytes[0], + mac_bytes[1], + mac_bytes[2], + mac_bytes[3], + mac_bytes[4], + mac_bytes[5], + ]); (ip_addr, mac_addr) } @@ -358,11 +381,11 @@ fn get_arp_table() -> io::Result> { let mut mib: [u32; 6] = [0; 6]; let mut len = 0; mib[0] = CTL_NET; - mib[1] = PF_ROUTE; - mib[2] = 0; - mib[3] = AF_INET; - mib[4] = NET_RT_FLAGS; - mib[5] = RTF_LLINFO; + mib[1] = PF_ROUTE; + mib[2] = 0; + mib[3] = AF_INET; + mib[4] = NET_RT_FLAGS; + mib[5] = RTF_LLINFO; if unsafe { sysctl( &mut mib as *mut _ as *mut _, @@ -430,13 +453,13 @@ fn get_default_route() -> Option { } } } - Err(_) => {}, + Err(_) => {} } None } pub fn get_default_gateway(_interface_name: String) -> Result { - if let Some(route) = get_default_route(){ + if let Some(route) = get_default_route() { if let Some(gw_ip) = route.gateway { match get_arp_table() { Ok(arp_map) => { @@ -455,10 +478,10 @@ pub fn get_default_gateway(_interface_name: String) -> Result { ip_addr: gw_ip, }; return Ok(gateway); - }else { + } else { return Err(format!("Failed to get gateway IP address")); } - }else{ + } else { return Err(format!("Failed to get default route")); } -} \ No newline at end of file +} diff --git a/src/gateway/mod.rs b/src/gateway/mod.rs index f41bf7c..67d4b81 100644 --- a/src/gateway/mod.rs +++ b/src/gateway/mod.rs @@ -1,14 +1,7 @@ -#[cfg(any( - target_os = "openbsd", - target_os = "freebsd", - target_os = "netbsd" -))] +#[cfg(any(target_os = "openbsd", target_os = "freebsd", target_os = "netbsd"))] pub(crate) mod unix; -#[cfg(any( - target_os = "macos", - target_os = "ios" -))] +#[cfg(any(target_os = "macos", target_os = "ios"))] pub(crate) mod macos; #[cfg(any(target_os = "linux", target_os = "android"))] @@ -96,4 +89,4 @@ mod tests { fn test_default_gateway() { println!("{:?}", get_default_gateway()); } -} \ No newline at end of file +} diff --git a/src/interface/unix.rs b/src/interface/unix.rs index 65091e1..2e846d7 100644 --- a/src/interface/unix.rs +++ b/src/interface/unix.rs @@ -335,4 +335,4 @@ mod tests { println!("{:#?}", interface); } } -} \ No newline at end of file +} diff --git a/src/lib.rs b/src/lib.rs index 2cd77c0..58ce005 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,11 +6,7 @@ target_os = "ios" ))] mod bpf; -#[cfg(any( - target_os = "openbsd", - target_os = "freebsd", - target_os = "netbsd" -))] +#[cfg(any(target_os = "openbsd", target_os = "freebsd", target_os = "netbsd"))] mod socket; #[cfg(not(target_os = "windows"))] mod sys; @@ -23,4 +19,4 @@ pub use gateway::get_default_gateway; pub use gateway::Gateway; pub use interface::get_default_interface; pub use interface::get_interfaces; -pub use interface::Interface; \ No newline at end of file +pub use interface::Interface; diff --git a/src/socket/mod.rs b/src/socket/mod.rs index c96984e..6c620ae 100644 --- a/src/socket/mod.rs +++ b/src/socket/mod.rs @@ -1,28 +1,12 @@ -#[cfg(any( - target_os = "openbsd", - target_os = "freebsd", - target_os = "netbsd" -))] +#[cfg(any(target_os = "openbsd", target_os = "freebsd", target_os = "netbsd"))] pub mod packet; -#[cfg(any( - target_os = "openbsd", - target_os = "freebsd", - target_os = "netbsd" -))] +#[cfg(any(target_os = "openbsd", target_os = "freebsd", target_os = "netbsd"))] mod unix; -#[cfg(any( - target_os = "openbsd", - target_os = "freebsd", - target_os = "netbsd" -))] +#[cfg(any(target_os = "openbsd", target_os = "freebsd", target_os = "netbsd"))] pub use self::unix::*; -#[cfg(any( - target_os = "openbsd", - target_os = "freebsd", - target_os = "netbsd" -))] +#[cfg(any(target_os = "openbsd", target_os = "freebsd", target_os = "netbsd"))] #[cfg(test)] mod tests { use super::*; @@ -91,4 +75,4 @@ mod tests { send_udp_packet(); } } -} \ No newline at end of file +}