Skip to content

Commit

Permalink
Fix flag wrong comparison
Browse files Browse the repository at this point in the history
Bump sys version

Make clippy happy
  • Loading branch information
Rubensei committed Apr 3, 2023
1 parent 2a7c742 commit 0a99823
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 98 deletions.
11 changes: 5 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ and this project adheres to

## [Unreleased]

### Added

- Add `wait` method variants to windivert
## [Unreleased-sys]

### Changed
## [sys-0.9.2] - 2023-03-23

- Handle `MAX_BATCH` limit in `send_ex`
### Fixed

## [Unreleased-sys]
- Fix `close` errors due to double inner close call

## [0.5.5] - 2023-03-23

Expand Down Expand Up @@ -76,6 +74,7 @@ and this project adheres to

[unreleased]: https://github.com/Rubensei/windivert-rust/compare/windivert-0.5.5...HEAD
[unreleased-sys]: https://github.com/Rubensei/windivert-rust/compare/windivert-sys-0.9.2...HEAD
[sys-0.9.3]: https://github.com/Rubensei/windivert-rust/compare/windivert-sys-0.9.2...windivert-sys-0.9.3
[sys-0.9.2]: https://github.com/Rubensei/windivert-rust/compare/windivert-sys-0.9.1...windivert-sys-0.9.2
[0.5.5]: https://github.com/Rubensei/windivert-rust/compare/windivert-0.5.4...windivert-0.5.5
[sys-0.9.1]: https://github.com/Rubensei/windivert-rust/compare/windivert-sys-0.9.0...windivert-sys-0.9.1
Expand Down
2 changes: 1 addition & 1 deletion windivert-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "windivert-sys"
version = "0.9.2"
version = "0.9.3"
description = "Raw FFI bindings for WinDivert user mode library"
authors = ["Ruben Serrano Izquierdo <rserranoizq@gmail.com>"]
repository = "https://github.com/Rubensei/windivert-rust.git"
Expand Down
18 changes: 9 additions & 9 deletions windivert-sys/build/gnu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ pub fn compile(build: Build) {
};
set_gnu_c_options(&mut cmd);
cmd.arg(format!("-Wl,--entry=${mangle}WinDivertDllEntry"));
cmd.args(&["-c", "vendor/dll/windivert.c"]);
cmd.args(&["-o", &format!("{out_dir}/WinDivert.o")]);
cmd.args(["-c", "vendor/dll/windivert.c"]);
cmd.args(["-o", &format!("{out_dir}/WinDivert.o")]);
cmd.output().expect("Error compiling windivert c library");

let mut cmd = build.get_compiler().to_command();
set_gnu_c_options(&mut cmd);
cmd.args(&["-o", &format!("{out_dir}/WinDivert.dll")]);
cmd.args(&[
cmd.args(["-o", &format!("{out_dir}/WinDivert.dll")]);
cmd.args([
&format!("{out_dir}/WinDivert.o"),
"vendor/dll/windivert.def",
"-nostdlib",
Expand Down Expand Up @@ -56,16 +56,16 @@ pub fn compile(build: Build) {
let mut dlltool = Command::new(dlltool);
dlltool.stdout(Stdio::inherit()).stderr(Stdio::inherit());

dlltool.args(&["--dllname", &format!("{out_dir}/WinDivert.dll")]);
dlltool.args(&["--def", "vendor/dll/windivert.def"]);
dlltool.args(&["--output-lib", &format!("{out_dir}/WinDivert.lib")]);
dlltool.args(["--dllname", &format!("{out_dir}/WinDivert.dll")]);
dlltool.args(["--def", "vendor/dll/windivert.def"]);
dlltool.args(["--output-lib", &format!("{out_dir}/WinDivert.lib")]);
let _ = dlltool.output().expect("Error building windivert lib");

let _ = fs::remove_file(format!("{out_dir}/WinDivert.o"));
}

fn set_gnu_c_options(cmd: &mut Command) {
cmd.args(&[
cmd.args([
"-fno-ident",
"-shared",
"-Wall",
Expand All @@ -77,5 +77,5 @@ fn set_gnu_c_options(cmd: &mut Command) {
}

fn set_gnu_c_libs(cmd: &mut Command) {
cmd.args(&["-lkernel32", "-ladvapi32"]);
cmd.args(["-lkernel32", "-ladvapi32"]);
}
2 changes: 1 addition & 1 deletion windivert-sys/build/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn main() {
_ => panic!("Unsupported target architecture!"),
};

if let Err(_) = fs::metadata(format!("{out_dir}\\WinDivert{arch}.sys")) {
if fs::metadata(format!("{out_dir}\\WinDivert{arch}.sys")).is_err() {
println!(
"cargo:warning=WinDivert{arch}.sys not found on the same directory as the dll."
)
Expand Down
2 changes: 1 addition & 1 deletion windivert-sys/build/msvc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn compile(build: Build) {
cmd.arg(format!(r#"/Fo{out_dir}\WinDivert.obj"#));
cmd.arg(format!(r#"/Fd{out_dir}\WinDivert.pdb"#));

cmd.args(MSVC_ARGS.split(" "));
cmd.args(MSVC_ARGS.split(' '));

let arch = match env::var("CARGO_CFG_TARGET_ARCH").unwrap().as_ref() {
"x86" => "x86",
Expand Down
95 changes: 28 additions & 67 deletions windivert-sys/src/bindings/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,19 @@ pub struct WINDIVERT_IPHDR {
impl WINDIVERT_IPHDR {
#[inline]
pub fn header_length(&self) -> u8 {
unsafe { ::std::mem::transmute(self.addr_bitfield.get(0usize, 4u8) as u8) }
self.addr_bitfield.get(0usize, 4u8) as u8
}
#[inline]
pub fn set_header_length(&mut self, val: u8) {
unsafe {
let val: u8 = ::std::mem::transmute(val);
self.addr_bitfield.set(0usize, 4u8, val as u64)
}
self.addr_bitfield.set(0usize, 4u8, val as u64)
}
#[inline]
pub fn version(&self) -> u8 {
unsafe { ::std::mem::transmute(self.addr_bitfield.get(4usize, 4u8) as u8) }
self.addr_bitfield.get(4usize, 4u8) as u8
}
#[inline]
pub fn set_version(&mut self, val: u8) {
unsafe {
let val: u8 = ::std::mem::transmute(val);
self.addr_bitfield.set(4usize, 4u8, val as u64)
}
self.addr_bitfield.set(4usize, 4u8, val as u64)
}
#[inline]
pub fn length(&self) -> u16 {
Expand Down Expand Up @@ -79,7 +73,7 @@ impl WINDIVERT_IPHDR {
}
#[inline]
pub fn MF(&self) -> bool {
self.fragment_offset_and_flags & 0x0020 == 1
self.fragment_offset_and_flags & 0x0020 != 0
}
#[inline]
pub fn set_MF(&mut self, value: bool) {
Expand All @@ -88,7 +82,7 @@ impl WINDIVERT_IPHDR {
}
#[inline]
pub fn DF(&self) -> bool {
self.fragment_offset_and_flags & 0x0040 == 1
self.fragment_offset_and_flags & 0x0040 != 0
}
#[inline]
pub fn set_DF(&mut self, value: bool) {
Expand Down Expand Up @@ -159,14 +153,11 @@ pub struct WINDIVERT_IPV6HDR {
impl WINDIVERT_IPV6HDR {
#[inline]
pub fn version(&self) -> u8 {
unsafe { ::std::mem::transmute(self.addr_bitfield.get(4usize, 4u8) as u8) }
self.addr_bitfield.get(4usize, 4u8) as u8
}
#[inline]
pub fn set_version(&mut self, val: u8) {
unsafe {
let val: u8 = ::std::mem::transmute(val);
self.addr_bitfield.set(4usize, 4u8, val as u64)
}
self.addr_bitfield.set(4usize, 4u8, val as u64)
}
#[inline]
pub fn traffic_class(&self) -> u8 {
Expand All @@ -179,25 +170,19 @@ impl WINDIVERT_IPV6HDR {
}
#[inline]
fn traffic_class0(&self) -> u8 {
unsafe { ::std::mem::transmute(self.addr_bitfield.get(0usize, 4u8) as u8) }
self.addr_bitfield.get(0usize, 4u8) as u8
}
#[inline]
fn set_traffic_class0(&mut self, val: u8) {
unsafe {
let val: u8 = ::std::mem::transmute(val);
self.addr_bitfield.set(0usize, 4u8, val as u64)
}
self.addr_bitfield.set(0usize, 4u8, val as u64)
}
#[inline]
fn traffic_class1(&self) -> u8 {
unsafe { ::std::mem::transmute(self.addr_bitfield.get(12usize, 4u8) as u8) }
self.addr_bitfield.get(12usize, 4u8) as u8
}
#[inline]
fn set_traffic_class1(&mut self, val: u8) {
unsafe {
let val: u8 = ::std::mem::transmute(val);
self.addr_bitfield.set(12usize, 4u8, val as u64)
}
self.addr_bitfield.set(12usize, 4u8, val as u64)
}
#[inline]
pub fn flow_label(&self) -> u32 {
Expand All @@ -211,14 +196,11 @@ impl WINDIVERT_IPV6HDR {
}
#[inline]
fn flow_label0(&self) -> u8 {
unsafe { ::std::mem::transmute(self.addr_bitfield.get(8usize, 4u8) as u8) }
self.addr_bitfield.get(8usize, 4u8) as u8
}
#[inline]
fn set_flow_label0(&mut self, val: u8) {
unsafe {
let val: u8 = ::std::mem::transmute(val);
self.addr_bitfield.set(8usize, 4u8, val as u64)
}
self.addr_bitfield.set(8usize, 4u8, val as u64)
}
#[inline]
pub fn length(&self) -> u16 {
Expand Down Expand Up @@ -438,80 +420,59 @@ impl WINDIVERT_TCPHDR {
}
#[inline]
pub fn header_length(&self) -> u16 {
unsafe { ::std::mem::transmute(self.addr_bitfield.get(4usize, 4u8) as u16) }
self.addr_bitfield.get(4usize, 4u8) as u16
}
#[inline]
pub fn set_header_length(&mut self, val: u16) {
unsafe {
let val: u16 = ::std::mem::transmute(val);
self.addr_bitfield.set(4usize, 4u8, val as u64)
}
self.addr_bitfield.set(4usize, 4u8, val as u64)
}
#[inline]
pub fn FIN(&self) -> u16 {
unsafe { ::std::mem::transmute(self.addr_bitfield.get(8usize, 1u8) as u16) }
self.addr_bitfield.get(8usize, 1u8) as u16
}
#[inline]
pub fn set_FIN(&mut self, val: u16) {
unsafe {
let val: u16 = ::std::mem::transmute(val);
self.addr_bitfield.set(8usize, 1u8, val as u64)
}
self.addr_bitfield.set(8usize, 1u8, val as u64)
}
#[inline]
pub fn SYN(&self) -> u16 {
unsafe { ::std::mem::transmute(self.addr_bitfield.get(9usize, 1u8) as u16) }
self.addr_bitfield.get(9usize, 1u8) as u16
}
#[inline]
pub fn set_SYN(&mut self, val: u16) {
unsafe {
let val: u16 = ::std::mem::transmute(val);
self.addr_bitfield.set(9usize, 1u8, val as u64)
}
self.addr_bitfield.set(9usize, 1u8, val as u64)
}
#[inline]
pub fn RST(&self) -> u16 {
unsafe { ::std::mem::transmute(self.addr_bitfield.get(10usize, 1u8) as u16) }
self.addr_bitfield.get(10usize, 1u8) as u16
}
#[inline]
pub fn set_RST(&mut self, val: u16) {
unsafe {
let val: u16 = ::std::mem::transmute(val);
self.addr_bitfield.set(10usize, 1u8, val as u64)
}
self.addr_bitfield.set(10usize, 1u8, val as u64)
}
#[inline]
pub fn PSH(&self) -> u16 {
unsafe { ::std::mem::transmute(self.addr_bitfield.get(11usize, 1u8) as u16) }
self.addr_bitfield.get(11usize, 1u8) as u16
}
#[inline]
pub fn set_PSH(&mut self, val: u16) {
unsafe {
let val: u16 = ::std::mem::transmute(val);
self.addr_bitfield.set(11usize, 1u8, val as u64)
}
self.addr_bitfield.set(11usize, 1u8, val as u64)
}
#[inline]
pub fn ACK(&self) -> u16 {
unsafe { ::std::mem::transmute(self.addr_bitfield.get(12usize, 1u8) as u16) }
self.addr_bitfield.get(12usize, 1u8) as u16
}
#[inline]
pub fn set_ACK(&mut self, val: u16) {
unsafe {
let val: u16 = ::std::mem::transmute(val);
self.addr_bitfield.set(12usize, 1u8, val as u64)
}
self.addr_bitfield.set(12usize, 1u8, val as u64)
}
#[inline]
pub fn URG(&self) -> u16 {
unsafe { ::std::mem::transmute(self.addr_bitfield.get(13usize, 1u8) as u16) }
self.addr_bitfield.get(13usize, 1u8) as u16
}
#[inline]
pub fn set_URG(&mut self, val: u16) {
unsafe {
let val: u16 = ::std::mem::transmute(val);
self.addr_bitfield.set(13usize, 1u8, val as u64)
}
self.addr_bitfield.set(13usize, 1u8, val as u64)
}
#[inline]
pub fn window(&self) -> u16 {
Expand Down
4 changes: 2 additions & 2 deletions windivert-sys/src/bindings/newtypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ impl WinDivertFlags {

/// Sets `sniff` flag to `value`.
pub fn set_sniff_value(&mut self, value: bool) {
self.0 = (self.0 & !0x0001) | ((value as u64) << 0);
self.0 = (self.0 & !0x0001) | (value as u64);
}

/// Sets `drop` flag.
Expand Down Expand Up @@ -425,7 +425,7 @@ impl ChecksumFlags {

/// Sets `no_ip` flag to `value`.
pub fn set_no_ip_value(&mut self, value: bool) {
self.0 = (self.0 & !0x0001) | ((value as u64) << 0);
self.0 = (self.0 & !0x0001) | (value as u64);
}

/// Sets `no_icmp` flag
Expand Down
6 changes: 4 additions & 2 deletions windivert/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ impl<L: layer::WinDivertLayerTrait> AsMut<WINDIVERT_ADDRESS> for WinDivertAddres

impl WinDivertAddress<layer::NetworkLayer> {
/// Create a new [`WinDivertAddress`] to inject new packets.
/// SAFETY: The default value for address is zeroed memory, caller must fill with valid data before sending.
/// # Safety
/// The default value for address is zeroed memory, caller must fill with valid data before sending.
pub unsafe fn new() -> Self {
Self {
data: Default::default(),
Expand Down Expand Up @@ -183,7 +184,8 @@ impl WinDivertAddress<layer::NetworkLayer> {

impl WinDivertAddress<layer::ForwardLayer> {
/// Create a new [`WinDivertAddress`] to inject new packets.
/// SAFETY: The default value for address is zeroed memory, caller must fill with valid data before sending.
/// # Safety
/// The default value for address is zeroed memory, caller must fill with valid data before sending.
pub unsafe fn new() -> Self {
Self {
data: Default::default(),
Expand Down
4 changes: 2 additions & 2 deletions windivert/src/divert/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl WinDivert<layer::NetworkLayer> {
address: WinDivertAddress::<layer::NetworkLayer>::from_raw(addr),
data: buffer
.map(|inner_buffer| {
let headers = SlicedPacket::from_ip(&inner_buffer)
let headers = SlicedPacket::from_ip(inner_buffer)
.expect("WinDivert can't capture anything below ip");
let offset = match headers.ip.unwrap() {
InternetSlice::Ipv4(ip_header, _) => ip_header.total_len() as usize,
Expand Down Expand Up @@ -225,7 +225,7 @@ impl WinDivert<layer::ForwardLayer> {
address: WinDivertAddress::<layer::NetworkLayer>::from_raw(addr),
data: buffer
.map(|inner_buffer| {
let headers = SlicedPacket::from_ip(&inner_buffer)
let headers = SlicedPacket::from_ip(inner_buffer)
.expect("WinDivert can't capture anything below ip");
let offset = match headers.ip.unwrap() {
InternetSlice::Ipv4(ip_header, _) => ip_header.total_len() as usize,
Expand Down
Loading

0 comments on commit 0a99823

Please sign in to comment.