Skip to content

Commit 3ae1160

Browse files
arihant2mathcoolreader18
authored andcommitted
Remove winapi dependency
1 parent 6804dd4 commit 3ae1160

3 files changed

Lines changed: 48 additions & 25 deletions

File tree

‎Cargo.lock‎

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎stdlib/Cargo.toml‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,6 @@ paste = { workspace = true }
117117
schannel = { workspace = true }
118118
widestring = { workspace = true }
119119

120-
[target.'cfg(windows)'.dependencies.winapi]
121-
version = "0.3.9"
122-
features = [
123-
"winsock2", "ifdef", "netioapi", "ws2tcpip",
124-
]
125-
126120
[target.'cfg(windows)'.dependencies.windows-sys]
127121
workspace = true
128122
features = [

‎stdlib/src/socket.rs‎

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,27 @@ mod _socket {
3535
use libc as c;
3636
#[cfg(windows)]
3737
mod c {
38-
pub use winapi::shared::netioapi::{if_indextoname, if_nametoindex};
39-
pub use winapi::shared::ws2def::{
40-
INADDR_ANY, INADDR_BROADCAST, INADDR_LOOPBACK, INADDR_NONE,
41-
};
42-
pub use winapi::um::winsock2::{
43-
SO_EXCLUSIVEADDRUSE, getprotobyname, getservbyname, getservbyport, getsockopt,
38+
pub use windows_sys::Win32::NetworkManagement::IpHelper::{if_indextoname, if_nametoindex};
39+
40+
pub const INADDR_ANY: u32 = 0x00000000;
41+
pub const INADDR_LOOPBACK: u32 = 0x7f000001;
42+
pub const INADDR_BROADCAST: u32 = 0xffffffff;
43+
pub const INADDR_NONE: u32 = 0xffffffff;
44+
45+
pub use windows_sys::Win32::Networking::WinSock::{
46+
SO_REUSEADDR as SO_EXCLUSIVEADDRUSE, getprotobyname, getservbyname, getservbyport, getsockopt,
4447
setsockopt,
4548
};
46-
pub use winapi::um::ws2tcpip::{
47-
EAI_AGAIN, EAI_BADFLAGS, EAI_FAIL, EAI_FAMILY, EAI_MEMORY, EAI_NODATA, EAI_NONAME,
48-
EAI_SERVICE, EAI_SOCKTYPE,
49+
pub use windows_sys::Win32::Networking::WinSock::{
50+
WSATRY_AGAIN as EAI_AGAIN,
51+
WSAEINVAL as EAI_BADFLAGS,
52+
WSANO_RECOVERY as EAI_FAIL,
53+
WSAEAFNOSUPPORT as EAI_FAMILY,
54+
WSA_NOT_ENOUGH_MEMORY as EAI_MEMORY,
55+
WSAHOST_NOT_FOUND as EAI_NODATA,
56+
WSAHOST_NOT_FOUND as EAI_NONAME,
57+
WSATYPE_NOT_FOUND as EAI_SERVICE,
58+
WSAESOCKTNOSUPPORT as EAI_SOCKTYPE,
4959
};
5060
pub use windows_sys::Win32::Networking::WinSock::{
5161
AF_APPLETALK, AF_DECnet, AF_IPX, AF_LINK, AI_ADDRCONFIG, AI_ALL, AI_CANONNAME,
@@ -755,7 +765,7 @@ mod _socket {
755765
}
756766

757767
#[cfg(windows)]
758-
use winapi::shared::netioapi;
768+
use windows_sys::Win32::NetworkManagement::IpHelper;
759769

760770
fn get_raw_sock(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<RawSocket> {
761771
#[cfg(unix)]
@@ -1755,6 +1765,9 @@ mod _socket {
17551765
.map(|s| s.to_cstring(vm))
17561766
.transpose()?;
17571767
let cstr_proto = cstr_opt_as_ptr(&cstr_proto);
1768+
#[cfg(windows)]
1769+
let serv = unsafe { c::getservbyname(cstr_name.as_ptr() as windows_sys::core::PCSTR, cstr_proto as windows_sys::core::PCSTR) };
1770+
#[cfg(not(windows))]
17581771
let serv = unsafe { c::getservbyname(cstr_name.as_ptr(), cstr_proto) };
17591772
if serv.is_null() {
17601773
return Err(vm.new_os_error("service/proto not found".to_owned()));
@@ -1777,10 +1790,16 @@ mod _socket {
17771790
.map(|s| s.to_cstring(vm))
17781791
.transpose()?;
17791792
let cstr_proto = cstr_opt_as_ptr(&cstr_proto);
1793+
#[cfg(windows)]
1794+
let serv = unsafe { c::getservbyport(port.to_be() as _, cstr_proto as windows_sys::core::PCSTR) };
1795+
#[cfg(not(windows))]
17801796
let serv = unsafe { c::getservbyport(port.to_be() as _, cstr_proto) };
17811797
if serv.is_null() {
17821798
return Err(vm.new_os_error("port/proto not found".to_owned()));
17831799
}
1800+
#[cfg(windows)]
1801+
let s = unsafe { ffi::CStr::from_ptr((*serv).s_name as *const i8) };
1802+
#[cfg(not(windows))]
17841803
let s = unsafe { ffi::CStr::from_ptr((*serv).s_name) };
17851804
Ok(s.to_string_lossy().into_owned())
17861805
}
@@ -2033,6 +2052,9 @@ mod _socket {
20332052
#[pyfunction]
20342053
fn getprotobyname(name: PyStrRef, vm: &VirtualMachine) -> PyResult {
20352054
let cstr = name.to_cstring(vm)?;
2055+
#[cfg(windows)]
2056+
let proto = unsafe { c::getprotobyname(cstr.as_ptr() as *const u8) };
2057+
#[cfg(not(windows))]
20362058
let proto = unsafe { c::getprotobyname(cstr.as_ptr()) };
20372059
if proto.is_null() {
20382060
return Err(vm.new_os_error("protocol not found".to_owned()));
@@ -2111,13 +2133,16 @@ mod _socket {
21112133
#[cfg(all(unix, not(target_os = "redox")))]
21122134
type IfIndex = c::c_uint;
21132135
#[cfg(windows)]
2114-
type IfIndex = winapi::shared::ifdef::NET_IFINDEX;
2136+
type IfIndex = u32;
21152137

21162138
#[cfg(not(target_os = "redox"))]
21172139
#[pyfunction]
21182140
fn if_nametoindex(name: FsPath, vm: &VirtualMachine) -> PyResult<IfIndex> {
21192141
let name = name.to_cstring(vm)?;
21202142

2143+
#[cfg(windows)]
2144+
let ret = unsafe { c::if_nametoindex(name.as_ptr() as *const u8) };
2145+
#[cfg(not(windows))]
21212146
let ret = unsafe { c::if_nametoindex(name.as_ptr()) };
21222147
if ret == 0 {
21232148
Err(vm.new_os_error("no interface with this name".to_owned()))
@@ -2134,6 +2159,9 @@ mod _socket {
21342159
if ret.is_null() {
21352160
Err(crate::vm::stdlib::os::errno_err(vm))
21362161
} else {
2162+
#[cfg(windows)]
2163+
let buf = unsafe { ffi::CStr::from_ptr(buf.as_ptr() as *const i8) };
2164+
#[cfg(not(windows))]
21372165
let buf = unsafe { ffi::CStr::from_ptr(buf.as_ptr()) };
21382166
Ok(buf.to_string_lossy().into_owned())
21392167
}
@@ -2152,6 +2180,8 @@ mod _socket {
21522180
))]
21532181
#[pyfunction]
21542182
fn if_nameindex(vm: &VirtualMachine) -> PyResult<Vec<PyObjectRef>> {
2183+
use windows_sys::Win32::NetworkManagement::Ndis::NET_LUID_LH;
2184+
21552185
#[cfg(not(windows))]
21562186
{
21572187
let list = nix::net::if_::if_nameindex()
@@ -2182,11 +2212,11 @@ mod _socket {
21822212
return Ok(list);
21832213

21842214
fn get_name(
2185-
luid: &winapi::shared::ifdef::NET_LUID,
2215+
luid: &NET_LUID_LH,
21862216
) -> io::Result<widestring::WideCString> {
21872217
let mut buf = [0; c::IF_NAMESIZE + 1];
21882218
let ret = unsafe {
2189-
netioapi::ConvertInterfaceLuidToNameW(luid, buf.as_mut_ptr(), buf.len())
2219+
IpHelper::ConvertInterfaceLuidToNameW(luid, buf.as_mut_ptr(), buf.len())
21902220
};
21912221
if ret == 0 {
21922222
Ok(widestring::WideCString::from_ustr_truncate(
@@ -2197,12 +2227,12 @@ mod _socket {
21972227
}
21982228
}
21992229
struct MibTable {
2200-
ptr: ptr::NonNull<netioapi::MIB_IF_TABLE2>,
2230+
ptr: ptr::NonNull<IpHelper::MIB_IF_TABLE2>,
22012231
}
22022232
impl MibTable {
22032233
fn get_raw() -> io::Result<Self> {
22042234
let mut ptr = ptr::null_mut();
2205-
let ret = unsafe { netioapi::GetIfTable2Ex(netioapi::MibIfTableRaw, &mut ptr) };
2235+
let ret = unsafe { IpHelper::GetIfTable2Ex(IpHelper::MibIfTableRaw, &mut ptr) };
22062236
if ret == 0 {
22072237
let ptr = unsafe { ptr::NonNull::new_unchecked(ptr) };
22082238
Ok(Self { ptr })
@@ -2212,17 +2242,17 @@ mod _socket {
22122242
}
22132243
}
22142244
impl MibTable {
2215-
fn as_slice(&self) -> &[netioapi::MIB_IF_ROW2] {
2245+
fn as_slice(&self) -> &[IpHelper::MIB_IF_ROW2] {
22162246
unsafe {
22172247
let p = self.ptr.as_ptr();
2218-
let ptr = &raw const (*p).Table as *const netioapi::MIB_IF_ROW2;
2248+
let ptr = &raw const (*p).Table as *const IpHelper::MIB_IF_ROW2;
22192249
std::slice::from_raw_parts(ptr, (*p).NumEntries as usize)
22202250
}
22212251
}
22222252
}
22232253
impl Drop for MibTable {
22242254
fn drop(&mut self) {
2225-
unsafe { netioapi::FreeMibTable(self.ptr.as_ptr() as *mut _) }
2255+
unsafe { IpHelper::FreeMibTable(self.ptr.as_ptr() as *mut _) };
22262256
}
22272257
}
22282258
}

0 commit comments

Comments
 (0)