Skip to content

Commit

Permalink
Merge pull request #75 from shellrow/windows-sys-migration
Browse files Browse the repository at this point in the history
Migration to windows-sys
  • Loading branch information
shellrow authored Jun 7, 2024
2 parents cbc291d + df7bf64 commit 35b301f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ netlink-sys = "0.8"
[target.'cfg(windows)'.dependencies]
memalloc = "0.1.0"

[target.'cfg(windows)'.dependencies.windows]
version = "0.54.0"
[target.'cfg(windows)'.dependencies.windows-sys]
version = "0.52"
features = ["Win32_Foundation","Win32_NetworkManagement_IpHelper", "Win32_Networking_WinSock", "Win32_NetworkManagement_Ndis"]

[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
Expand Down
30 changes: 15 additions & 15 deletions src/interface/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use memalloc::{allocate, deallocate};
use std::convert::TryFrom;
use std::convert::TryInto;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use windows::Win32::Foundation::{ERROR_BUFFER_OVERFLOW, NO_ERROR};
use windows::Win32::NetworkManagement::IpHelper::{
use windows_sys::Win32::Foundation::{ERROR_BUFFER_OVERFLOW, NO_ERROR};
use windows_sys::Win32::NetworkManagement::IpHelper::{
GetAdaptersAddresses, SendARP, GAA_FLAG_INCLUDE_GATEWAYS, IP_ADAPTER_ADDRESSES_LH,
};
use windows::Win32::NetworkManagement::Ndis::{IF_OPER_STATUS, NET_IF_OPER_STATUS_UP};
use windows::Win32::Networking::WinSock::SOCKET_ADDRESS;
use windows::Win32::Networking::WinSock::{
use windows_sys::Win32::NetworkManagement::Ndis::{IF_OPER_STATUS, NET_IF_OPER_STATUS_UP};
use windows_sys::Win32::Networking::WinSock::SOCKET_ADDRESS;
use windows_sys::Win32::Networking::WinSock::{
AF_INET, AF_INET6, AF_UNSPEC, SOCKADDR_IN, SOCKADDR_IN6,
};

Expand Down Expand Up @@ -47,7 +47,7 @@ fn get_mac_through_arp(src_ip: Ipv4Addr, dst_ip: Ipv4Addr) -> MacAddr {
&mut out_buf_len,
)
};
if res == NO_ERROR.0 {
if res == NO_ERROR {
MacAddr::from_octets(target_mac_addr)
} else {
MacAddr::zero()
Expand Down Expand Up @@ -90,21 +90,21 @@ pub fn interfaces() -> Vec<Interface> {
let old_size = dwsize as usize;
ret_val = unsafe {
GetAdaptersAddresses(
AF_UNSPEC.0 as u32,
AF_UNSPEC as u32,
GAA_FLAG_INCLUDE_GATEWAYS,
Some(std::ptr::null_mut::<std::ffi::c_void>()),
Some(mem),
std::ptr::null_mut::<std::ffi::c_void>(),
mem,
&mut dwsize,
)
};
if ret_val != ERROR_BUFFER_OVERFLOW.0 || retries <= 0 {
if ret_val != ERROR_BUFFER_OVERFLOW || retries <= 0 {
break;
}
unsafe { deallocate(mem as *mut u8, old_size as usize) };
mem = unsafe { allocate(dwsize as usize) as *mut IP_ADAPTER_ADDRESSES_LH };
retries -= 1;
}
if ret_val == NO_ERROR.0 {
if ret_val == NO_ERROR {
// Enumerate all adapters
let mut cur = mem;
while !cur.is_null() {
Expand All @@ -123,7 +123,7 @@ pub fn interfaces() -> Vec<Interface> {
// Flags and Status
let mut flags: u32 = 0;
let status: IF_OPER_STATUS = unsafe { (*cur).OperStatus };
if status.0 == NET_IF_OPER_STATUS_UP.0 {
if status == NET_IF_OPER_STATUS_UP {
flags |= sys::IFF_UP;
}
match if_type {
Expand All @@ -145,17 +145,17 @@ pub fn interfaces() -> Vec<Interface> {
_ => {}
}
// Name
let p_aname = unsafe { (*cur).AdapterName.0 };
let p_aname = unsafe { (*cur).AdapterName };
let aname_len = unsafe { strlen(p_aname as *const c_char) };
let aname_slice = unsafe { std::slice::from_raw_parts(p_aname, aname_len) };
let adapter_name = String::from_utf8(aname_slice.to_vec()).unwrap();
// Friendly Name
let p_fname = unsafe { (*cur).FriendlyName.0 };
let p_fname = unsafe { (*cur).FriendlyName };
let fname_len = unsafe { wcslen(p_fname as *const wchar_t) };
let fname_slice = unsafe { std::slice::from_raw_parts(p_fname, fname_len) };
let friendly_name = String::from_utf16(fname_slice).unwrap();
// Description
let p_desc = unsafe { (*cur).Description.0 };
let p_desc = unsafe { (*cur).Description };
let desc_len = unsafe { wcslen(p_desc as *const wchar_t) };
let desc_slice = unsafe { std::slice::from_raw_parts(p_desc, desc_len) };
let description = String::from_utf16(desc_slice).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/sys/windows.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use windows::Win32::Networking::WinSock as ws;
use windows_sys::Win32::Networking::WinSock as ws;

pub const IFF_UP: u32 = ws::IFF_UP;
pub const IFF_BROADCAST: u32 = ws::IFF_BROADCAST;
Expand Down

0 comments on commit 35b301f

Please sign in to comment.