Skip to content

Commit

Permalink
refactor: remove dependency on lazy_static
Browse files Browse the repository at this point in the history
This was only used in `src/sys/windows/afd.rs` for a set of objects that
could instead be defined using constant expressions.
  • Loading branch information
FaultyRAM authored and Thomasdezeeuw committed Jun 30, 2020
1 parent a07d38b commit 57e4c2a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 39 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ libc = "0.2.69"
miow = "0.3.3"
winapi = { version = "0.3", features = ["winsock2", "mswsock"] }
ntapi = "0.3"
lazy_static = "1.4.0"

[dev-dependencies]
env_logger = { version = "0.6.2", default-features = false }
Expand Down
74 changes: 36 additions & 38 deletions src/sys/windows/afd.rs
Original file line number Diff line number Diff line change
@@ -1,55 +1,19 @@
use lazy_static::lazy_static;
use ntapi::ntioapi::{IO_STATUS_BLOCK_u, IO_STATUS_BLOCK};
use ntapi::ntioapi::{NtCancelIoFileEx, NtDeviceIoControlFile};
use ntapi::ntrtl::RtlNtStatusToDosError;
use std::ffi::OsStr;
use std::fmt;
use std::fs::File;
use std::io;
use std::mem::size_of;
use std::os::windows::ffi::OsStrExt;
use std::os::windows::io::AsRawHandle;
use std::ptr::null_mut;
use winapi::shared::ntdef::{
HANDLE, LARGE_INTEGER, NTSTATUS, OBJECT_ATTRIBUTES, PVOID, ULONG, UNICODE_STRING,
HANDLE, LARGE_INTEGER, NTSTATUS, PVOID, ULONG,
};
use winapi::shared::ntstatus::{STATUS_NOT_FOUND, STATUS_PENDING, STATUS_SUCCESS};

const IOCTL_AFD_POLL: ULONG = 0x00012024;

lazy_static! {
static ref AFD_HELPER_NAME: Vec<u16> = {
OsStr::new("\\Device\\Afd\\Mio")
.encode_wide()
.collect::<Vec<_>>()
};
}

struct UnicodeString(UNICODE_STRING);
unsafe impl Send for UnicodeString {}
unsafe impl Sync for UnicodeString {}

struct ObjectAttributes(OBJECT_ATTRIBUTES);
unsafe impl Send for ObjectAttributes {}
unsafe impl Sync for ObjectAttributes {}

lazy_static! {
static ref AFD_OBJ_NAME: UnicodeString = UnicodeString(UNICODE_STRING {
// Lengths are calced in bytes
Length: (AFD_HELPER_NAME.len() * 2) as u16,
MaximumLength: (AFD_HELPER_NAME.len() * 2) as u16,
Buffer: AFD_HELPER_NAME.as_ptr() as *mut _,
});
static ref AFD_HELPER_ATTRIBUTES: ObjectAttributes = ObjectAttributes(OBJECT_ATTRIBUTES {
Length: size_of::<OBJECT_ATTRIBUTES>() as ULONG,
RootDirectory: null_mut() as HANDLE,
ObjectName: &AFD_OBJ_NAME.0 as *const _ as *mut _,
Attributes: 0 as ULONG,
SecurityDescriptor: null_mut() as PVOID,
SecurityQualityOfService: null_mut() as PVOID,
});
}

/// Winsock2 AFD driver instance.
///
/// All operations are unsafe due to IO_STATUS_BLOCK parameter are being used by Afd driver during STATUS_PENDING before I/O Completion Port returns its result.
Expand Down Expand Up @@ -156,11 +120,45 @@ cfg_net! {
use std::mem::zeroed;
use std::os::windows::io::{FromRawHandle, RawHandle};
use std::sync::atomic::{AtomicUsize, Ordering};
use winapi::shared::ntdef::{OBJECT_ATTRIBUTES, UNICODE_STRING, USHORT, WCHAR};
use winapi::um::handleapi::INVALID_HANDLE_VALUE;
use winapi::um::winbase::{SetFileCompletionNotificationModes, FILE_SKIP_SET_EVENT_ON_HANDLE};
use winapi::um::winnt::SYNCHRONIZE;
use winapi::um::winnt::{FILE_SHARE_READ, FILE_SHARE_WRITE};

const AFD_HELPER_ATTRIBUTES: OBJECT_ATTRIBUTES = OBJECT_ATTRIBUTES {
Length: size_of::<OBJECT_ATTRIBUTES>() as ULONG,
RootDirectory: null_mut(),
ObjectName: &AFD_OBJ_NAME as *const _ as *mut _,
Attributes: 0,
SecurityDescriptor: null_mut(),
SecurityQualityOfService: null_mut(),
};

const AFD_OBJ_NAME: UNICODE_STRING = UNICODE_STRING {
Length: (AFD_HELPER_NAME.len() * size_of::<WCHAR>()) as USHORT,
MaximumLength: (AFD_HELPER_NAME.len() * size_of::<WCHAR>()) as USHORT,
Buffer: AFD_HELPER_NAME.as_ptr() as *mut _,
};

const AFD_HELPER_NAME: &[WCHAR] = &[
'\\' as _,
'D' as _,
'e' as _,
'v' as _,
'i' as _,
'c' as _,
'e' as _,
'\\' as _,
'A' as _,
'f' as _,
'd' as _,
'\\' as _,
'M' as _,
'i' as _,
'o' as _
];

static NEXT_TOKEN: AtomicUsize = AtomicUsize::new(0);

impl AfdPollInfo {
Expand All @@ -182,7 +180,7 @@ cfg_net! {
let status = NtCreateFile(
&mut afd_helper_handle as *mut _,
SYNCHRONIZE,
&AFD_HELPER_ATTRIBUTES.0 as *const _ as *mut _,
&AFD_HELPER_ATTRIBUTES as *const _ as *mut _,
&mut iosb,
null_mut(),
0 as ULONG,
Expand Down

0 comments on commit 57e4c2a

Please sign in to comment.