use std::{convert::TryFrom, u32};
use super::WinDivertValueError;
#[repr(u32)]
#[derive(Debug, Copy, Clone)]
pub enum WinDivertLayer {
Network = 0,
Forward = 1,
Flow = 2,
Socket = 3,
Reflect = 4,
}
impl TryFrom<u32> for WinDivertLayer {
type Error = WinDivertValueError;
fn try_from(value: u32) -> Result<Self, Self::Error> {
match value {
0 => Ok(WinDivertLayer::Network),
1 => Ok(WinDivertLayer::Forward),
2 => Ok(WinDivertLayer::Flow),
3 => Ok(WinDivertLayer::Socket),
4 => Ok(WinDivertLayer::Reflect),
_ => Err(WinDivertValueError::Layer(value)),
}
}
}
impl From<WinDivertLayer> for u8 {
fn from(value: WinDivertLayer) -> Self {
match value {
WinDivertLayer::Network => 0,
WinDivertLayer::Forward => 1,
WinDivertLayer::Flow => 2,
WinDivertLayer::Socket => 3,
WinDivertLayer::Reflect => 4,
}
}
}
impl From<WinDivertLayer> for u32 {
fn from(value: WinDivertLayer) -> Self {
u8::from(value) as u32
}
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub enum WinDivertEvent {
NetworkPacket = 0,
FlowStablished = 1,
FlowDeleted = 2,
SocketBind = 3,
SocketConnect = 4,
SocketListen = 5,
SocketAccept = 6,
SocketClose = 7,
ReflectOpen = 8,
ReflectClose = 9,
}
impl TryFrom<u8> for WinDivertEvent {
type Error = WinDivertValueError;
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0 => Ok(Self::NetworkPacket),
1 => Ok(Self::FlowStablished),
2 => Ok(Self::FlowDeleted),
3 => Ok(Self::SocketBind),
4 => Ok(Self::SocketConnect),
5 => Ok(Self::SocketListen),
6 => Ok(Self::SocketAccept),
7 => Ok(Self::SocketClose),
8 => Ok(Self::ReflectOpen),
9 => Ok(Self::ReflectClose),
_ => Err(WinDivertValueError::Event(value)),
}
}
}
impl From<WinDivertEvent> for u8 {
fn from(value: WinDivertEvent) -> Self {
match value {
WinDivertEvent::NetworkPacket => 0,
WinDivertEvent::FlowStablished => 1,
WinDivertEvent::FlowDeleted => 2,
WinDivertEvent::SocketBind => 3,
WinDivertEvent::SocketConnect => 4,
WinDivertEvent::SocketListen => 5,
WinDivertEvent::SocketAccept => 6,
WinDivertEvent::SocketClose => 7,
WinDivertEvent::ReflectOpen => 8,
WinDivertEvent::ReflectClose => 9,
}
}
}
impl From<WinDivertEvent> for u32 {
fn from(value: WinDivertEvent) -> Self {
u8::from(value) as u32
}
}
#[repr(u32)]
#[derive(Debug, Copy, Clone)]
pub enum WinDivertShutdownMode {
Recv = 1,
Send = 2,
Both = 3,
}
impl TryFrom<u32> for WinDivertShutdownMode {
type Error = WinDivertValueError;
fn try_from(value: u32) -> Result<Self, Self::Error> {
match value {
1 => Ok(WinDivertShutdownMode::Recv),
2 => Ok(WinDivertShutdownMode::Send),
3 => Ok(WinDivertShutdownMode::Both),
_ => Err(WinDivertValueError::Shutdown(value)),
}
}
}
impl From<WinDivertShutdownMode> for u32 {
fn from(value: WinDivertShutdownMode) -> Self {
match value {
WinDivertShutdownMode::Recv => 1,
WinDivertShutdownMode::Send => 2,
WinDivertShutdownMode::Both => 3,
}
}
}
#[repr(u32)]
#[derive(Debug, Copy, Clone)]
pub enum WinDivertParam {
QueueLength = 0,
QueueTime = 1,
QueueSize = 2,
VersionMajor = 3,
VersionMinor = 4,
}
impl TryFrom<u32> for WinDivertParam {
type Error = WinDivertValueError;
fn try_from(value: u32) -> Result<Self, Self::Error> {
match value {
0 => Ok(WinDivertParam::QueueLength),
1 => Ok(WinDivertParam::QueueTime),
2 => Ok(WinDivertParam::QueueSize),
3 => Ok(WinDivertParam::VersionMajor),
4 => Ok(WinDivertParam::VersionMinor),
_ => Err(WinDivertValueError::Parameter(value)),
}
}
}
impl From<WinDivertParam> for u32 {
fn from(value: WinDivertParam) -> Self {
match value {
WinDivertParam::QueueLength => 0,
WinDivertParam::QueueTime => 1,
WinDivertParam::QueueSize => 2,
WinDivertParam::VersionMajor => 3,
WinDivertParam::VersionMinor => 4,
}
}
}
#[derive(Debug, Default, Copy, Clone)]
#[repr(transparent)]
pub struct WinDivertFlags(u64);
impl WinDivertFlags {
pub const fn new() -> Self {
Self(0)
}
pub const fn set_sniff(mut self) -> Self {
self.0 |= 0x0001;
self
}
pub const fn unset_sniff(mut self) -> Self {
self.0 &= !0x001;
self
}
pub fn set_sniff_value(&mut self, value: bool) {
self.0 = (self.0 & !0x0001) | (value as u64);
}
pub const fn set_drop(mut self) -> Self {
self.0 |= 0x0002;
self
}
pub const fn unset_drop(mut self) -> Self {
self.0 &= !0x0002;
self
}
pub fn set_drop_value(&mut self, value: bool) {
self.0 = (self.0 & !0x0002) | ((value as u64) << 1);
}
pub const fn set_recv_only(mut self) -> Self {
self.0 |= 0x0004;
self
}
pub const fn unset_recv_only(mut self) -> Self {
self.0 &= !0x0004;
self
}
pub fn set_recv_only_value(&mut self, value: bool) {
self.0 = (self.0 & !0x0004) | ((value as u64) << 2);
}
pub const fn set_send_only(mut self) -> Self {
self.0 |= 0x0008;
self
}
pub const fn unset_send_only(mut self) -> Self {
self.0 &= !0x0008;
self
}
pub fn set_send_only_value(&mut self, value: bool) {
self.0 = (self.0 & !0x0008) | ((value as u64) << 3);
}
pub const fn set_no_installs(mut self) -> Self {
self.0 |= 0x0010;
self
}
pub const fn unset_no_installs(mut self) -> Self {
self.0 &= !0x0010;
self
}
pub fn set_no_installs_value(&mut self, value: bool) {
self.0 = (self.0 & !0x0010) | ((value as u64) << 4);
}
pub const fn set_fragments(mut self) -> Self {
self.0 |= 0x0020;
self
}
pub const fn unset_fragments(mut self) -> Self {
self.0 &= !0x0020;
self
}
pub fn set_fragments_value(&mut self, value: bool) {
self.0 = (self.0 & !0x0020) | ((value as u64) << 5);
}
}
impl From<WinDivertFlags> for u64 {
fn from(flags: WinDivertFlags) -> Self {
flags.0
}
}
#[derive(Debug, Default, Copy, Clone)]
#[repr(transparent)]
pub struct ChecksumFlags(u64);
impl ChecksumFlags {
pub const fn new() -> Self {
Self(0)
}
pub const fn set_no_ip(mut self) -> Self {
self.0 |= 0x0001;
self
}
pub const fn unset_no_ip(mut self) -> Self {
self.0 &= !0x0001;
self
}
pub fn set_no_ip_value(&mut self, value: bool) {
self.0 = (self.0 & !0x0001) | (value as u64);
}
pub const fn set_no_icmp(mut self) -> Self {
self.0 |= 0x0002;
self
}
pub const fn unset_no_icmp(mut self) -> Self {
self.0 &= !0x0002;
self
}
pub fn set_no_icmp_value(&mut self, value: bool) {
self.0 = (self.0 & !0x0002) | ((value as u64) << 1);
}
pub const fn set_no_icmpv6(mut self) -> Self {
self.0 |= 0x0004;
self
}
pub const fn unset_no_icmpv6(mut self) -> Self {
self.0 &= !0x0004;
self
}
pub fn set_no_icmpv6_value(&mut self, value: bool) {
self.0 = (self.0 & !0x0004) | ((value as u64) << 2);
}
pub const fn set_no_tcp(mut self) -> Self {
self.0 |= 0x0008;
self
}
pub const fn unset_no_tcp(mut self) -> Self {
self.0 &= !0x0008;
self
}
pub fn set_no_tcp_value(&mut self, value: bool) {
self.0 = (self.0 & !0x0008) | ((value as u64) << 3);
}
pub const fn set_no_udp(mut self) -> Self {
self.0 |= 0x0010;
self
}
pub const fn unset_no_udp(mut self) -> Self {
self.0 &= !0x0010;
self
}
pub fn set_no_udp_value(&mut self, value: bool) {
self.0 = (self.0 & !0x0010) | ((value as u64) << 4);
}
}
impl From<ChecksumFlags> for u64 {
fn from(flags: ChecksumFlags) -> Self {
flags.0
}
}