1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
use std::{
borrow::Cow,
net::{IpAddr, Ipv4Addr, Ipv6Addr},
};
use super::{WinDivertEvent, WinDivertFlags, WinDivertLayer};
use windivert_sys::address::*;
macro_rules! addr_impl {
() => {
pub fn event_timestamp(&self) -> i64 {
self.data.timestamp
}
pub fn event(&self) -> WinDivertEvent {
self.data.event()
}
pub fn event_layer(&self) -> WinDivertLayer {
self.data.layer()
}
pub fn sniffed(&self) -> bool {
self.data.sniffed()
}
pub fn outbound(&self) -> bool {
self.data.outbound()
}
pub fn set_outbound(&mut self, value: bool) {
self.data.to_mut().set_outbound(value)
}
pub fn loopback(&self) -> bool {
self.data.loopback()
}
pub fn impostor(&self) -> bool {
self.data.impostor()
}
pub fn set_impostor(&mut self, value: bool) {
self.data.to_mut().set_impostor(value)
}
pub fn ipv6(&self) -> bool {
self.data.ipv6()
}
pub fn ip_checksum(&self) -> bool {
self.data.ipchecksum()
}
pub fn set_ip_checksum(&mut self, value: bool) {
self.data.to_mut().set_ipchecksum(value)
}
pub fn tcp_checksum(&self) -> bool {
self.data.tcpchecksum()
}
pub fn set_tcp_checksum(&mut self, value: bool) {
self.data.to_mut().set_tcpchecksum(value)
}
pub fn udp_checksum(&self) -> bool {
self.data.udpchecksum()
}
pub fn set_udp_checksum(&mut self, value: bool) {
self.data.to_mut().set_udpchecksum(value)
}
};
}
#[derive(Debug, Default)]
pub struct WinDivertNetworkData<'a> {
pub(crate) data: Cow<'a, WINDIVERT_ADDRESS>,
}
impl<'a> WinDivertNetworkData<'a> {
addr_impl!();
fn data(&self) -> &WINDIVERT_DATA_NETWORK {
unsafe { &self.data.union_field.Network }
}
fn data_mut(&mut self) -> &mut WINDIVERT_DATA_NETWORK {
unsafe { &mut self.data.to_mut().union_field.Network }
}
pub fn interface_index(&self) -> u32 {
self.data().interface_id
}
pub fn set_interface_index(&mut self, value: u32) {
self.data_mut().interface_id = value
}
pub fn subinterface_index(&self) -> u32 {
self.data().subinterface_id
}
pub fn set_subinterface_index(&mut self, value: u32) {
self.data_mut().subinterface_id = value
}
}
#[derive(Debug, Default)]
pub struct WinDivertFlowData<'a> {
pub(crate) data: Cow<'a, WINDIVERT_ADDRESS>,
}
pub type WinDivertSocketData<'a> = WinDivertFlowData<'a>;
impl<'a> WinDivertFlowData<'a> {
addr_impl!();
fn data(&self) -> &WINDIVERT_DATA_FLOW {
unsafe { &self.data.union_field.Flow }
}
pub fn endpoint_id(&self) -> u64 {
self.data().endpoint_id
}
pub fn parent_endpoint_id(&self) -> u64 {
self.data().parent_endpoint_id
}
pub fn process_id(&self) -> u32 {
self.data().process_id
}
pub fn local_address(&self) -> IpAddr {
if self.data.ipv6() {
IpAddr::V6(Ipv6Addr::from(
self.data()
.local_addr
.iter()
.rev()
.fold(0u128, |acc, &x| acc << 32 | (x as u128)),
))
} else {
IpAddr::V4(Ipv4Addr::from(self.data().local_addr[0]))
}
}
pub fn remote_address(&self) -> IpAddr {
if self.data.ipv6() {
IpAddr::V6(Ipv6Addr::from(
self.data()
.remote_addr
.iter()
.rev()
.fold(0u128, |acc, &x| acc << 32 | (x as u128)),
))
} else {
IpAddr::V4(Ipv4Addr::from(self.data().remote_addr[0]))
}
}
pub fn local_port(&self) -> u16 {
self.data().local_port
}
pub fn remote_port(&self) -> u16 {
self.data().remote_port
}
pub fn protocol(&self) -> u8 {
self.data().protocol
}
}
#[derive(Debug)]
pub struct WinDivertReflectData<'a> {
pub(crate) data: Cow<'a, WINDIVERT_ADDRESS>,
}
impl<'a> WinDivertReflectData<'a> {
addr_impl!();
fn data(&self) -> &WINDIVERT_DATA_REFLECT {
unsafe { &self.data.union_field.Reflect }
}
pub fn timestamp(&self) -> i64 {
self.data().timestamp
}
pub fn process_id(&self) -> u32 {
self.data().process_id
}
pub fn layer(&self) -> WinDivertLayer {
self.data().layer
}
pub fn flags(&self) -> WinDivertFlags {
self.data().flags
}
pub fn priority(&self) -> i16 {
self.data().priority
}
}