Skip to content

Commit 797a39e

Browse files
committed
Get rid of libc on Windows
1 parent c3b1ea8 commit 797a39e

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ repository = "https://github.com/messense/if-addrs"
88
version = "0.10.0"
99
edition = "2018"
1010

11-
[dependencies]
11+
[target.'cfg(not(target_os = "windows"))'.dependencies]
1212
libc = "0.2"
1313

1414
[target.'cfg(target_os = "windows")'.dependencies.windows-sys]
1515
version = "0.45.0"
1616
features = [
1717
"Win32_Foundation",
18+
"Win32_System_Memory",
1819
"Win32_Networking_WinSock",
1920
"Win32_NetworkManagement_IpHelper",
2021
"Win32_NetworkManagement_Ndis",

src/windows.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
// specific language governing permissions and limitations relating to use of the SAFE Network
88
// Software.
99

10-
use libc::{self, c_ulong, c_void, size_t};
1110
use std::ffi::CStr;
1211
use std::{io, ptr};
1312
use windows_sys::Win32::Foundation::{ERROR_BUFFER_OVERFLOW, ERROR_SUCCESS};
@@ -16,6 +15,9 @@ use windows_sys::Win32::NetworkManagement::IpHelper::{
1615
GAA_FLAG_SKIP_FRIENDLY_NAME, GAA_FLAG_SKIP_MULTICAST, IP_ADAPTER_ADDRESSES_LH,
1716
IP_ADAPTER_PREFIX_XP, IP_ADAPTER_UNICAST_ADDRESS_LH,
1817
};
18+
use windows_sys::Win32::System::Memory::{
19+
GetProcessHeap, HeapAlloc, HeapFree, HEAP_NONE, HEAP_ZERO_MEMORY,
20+
};
1921

2022
#[repr(transparent)]
2123
pub struct IpAdapterAddresses(*const IP_ADAPTER_ADDRESSES_LH);
@@ -68,12 +70,13 @@ pub struct IfAddrs {
6870
impl IfAddrs {
6971
#[allow(unsafe_code)]
7072
pub fn new() -> io::Result<Self> {
71-
let mut buffersize: c_ulong = 15000;
73+
let mut buffersize = 15000;
7274
let mut ifaddrs: *mut IP_ADAPTER_ADDRESSES_LH;
7375

7476
loop {
7577
unsafe {
76-
ifaddrs = libc::malloc(buffersize as size_t) as *mut IP_ADAPTER_ADDRESSES_LH;
78+
ifaddrs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buffersize as _)
79+
as *mut IP_ADAPTER_ADDRESSES_LH;
7780
if ifaddrs.is_null() {
7881
panic!("Failed to allocate buffer in get_if_addrs()");
7982
}
@@ -93,12 +96,12 @@ impl IfAddrs {
9396
match retcode {
9497
ERROR_SUCCESS => break,
9598
ERROR_BUFFER_OVERFLOW => {
96-
libc::free(ifaddrs as *mut c_void);
99+
HeapFree(GetProcessHeap(), HEAP_NONE, ifaddrs as _);
97100
buffersize *= 2;
98101
continue;
99102
}
100103
_ => {
101-
libc::free(ifaddrs as *mut c_void);
104+
HeapFree(GetProcessHeap(), HEAP_NONE, ifaddrs as _);
102105
return Err(io::Error::last_os_error());
103106
}
104107
}
@@ -122,7 +125,7 @@ impl Drop for IfAddrs {
122125
#[allow(unsafe_code)]
123126
fn drop(&mut self) {
124127
unsafe {
125-
libc::free(self.inner.0 as *mut c_void);
128+
HeapFree(GetProcessHeap(), HEAP_NONE, self.inner.0 as _);
126129
}
127130
}
128131
}

0 commit comments

Comments
 (0)