windivert 0.6.0

Wrapper library around windivert-sys
Documentation
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
use std::{
    marker::PhantomData,
    net::{IpAddr, Ipv4Addr, Ipv6Addr},
};

use crate::{layer, prelude::*};
use windivert_sys::address::*;

/// Newtype wrapper around [`WINDIVERT_ADDRESS`] using typestate to provide a safe interface.
#[repr(transparent)]
#[derive(Debug, Clone)]
pub struct WinDivertAddress<L: layer::WinDivertLayerTrait> {
    data: WINDIVERT_ADDRESS,
    _layer: PhantomData<L>,
}

impl<L: layer::WinDivertLayerTrait> WinDivertAddress<L> {
    #[inline]
    pub(crate) fn from_raw(data: WINDIVERT_ADDRESS) -> Self {
        Self {
            data,
            _layer: PhantomData,
        }
    }

    /// Timestamp of the event. Uses same clock as `QueryPerformanceCounter()`
    #[inline]
    pub fn event_timestamp(&self) -> i64 {
        self.data.timestamp
    }

    /// Type of captured event
    #[inline]
    pub fn event(&self) -> WinDivertEvent {
        self.data.event()
    }

    /// The handle's layer
    #[inline]
    pub fn event_layer(&self) -> WinDivertLayer {
        self.data.layer()
    }

    /// Set to `true` if the event was sniffed (i.e., not blocked), `false` otherwise
    #[inline]
    pub fn sniffed(&self) -> bool {
        self.data.sniffed()
    }

    /// Set to `true` for outbound packets/event, `false` for inbound or otherwise
    #[inline]
    pub fn outbound(&self) -> bool {
        self.data.outbound()
    }

    /// Outbound setter
    #[inline]
    pub fn set_outbound(&mut self, value: bool) {
        self.data.set_outbound(value)
    }

    /// Set to `true` for loopback packets, `false` otherwise
    #[inline]
    pub fn loopback(&self) -> bool {
        self.data.loopback()
    }

    ///  Set to `true` for impostor packets, `false` otherwise.
    #[inline]
    pub fn impostor(&self) -> bool {
        self.data.impostor()
    }

    /// Impostor setter
    #[inline]
    pub fn set_impostor(&mut self, value: bool) {
        self.data.set_impostor(value)
    }

    /// Set to `true` for IPv6 packets/events, `false` otherwise
    #[inline]
    pub fn ipv6(&self) -> bool {
        self.data.ipv6()
    }

    /// Set to `true` if the IPv4 checksum is valid, `false` otherwise.
    #[inline]
    pub fn ip_checksum(&self) -> bool {
        self.data.ipchecksum()
    }

    /// IPv4 checksum setter
    #[inline]
    pub fn set_ip_checksum(&mut self, value: bool) {
        self.data.set_ipchecksum(value)
    }

    /// Set to `true` if the TCP checksum is valid, `false` otherwise.
    #[inline]
    pub fn tcp_checksum(&self) -> bool {
        self.data.tcpchecksum()
    }

    /// TCP checksum setter
    #[inline]
    pub fn set_tcp_checksum(&mut self, value: bool) {
        self.data.set_tcpchecksum(value)
    }

    /// Set to `true` if the UDP checksum is valid, `false` otherwise.
    #[inline]
    pub fn udp_checksum(&self) -> bool {
        self.data.udpchecksum()
    }

    /// UDP checksum setter
    #[inline]
    pub fn set_udp_checksum(&mut self, value: bool) {
        self.data.set_udpchecksum(value)
    }
}

impl<L: layer::WinDivertLayerTrait> AsRef<WINDIVERT_ADDRESS> for WinDivertAddress<L> {
    #[inline]
    fn as_ref(&self) -> &WINDIVERT_ADDRESS {
        &self.data
    }
}

impl<L: layer::WinDivertLayerTrait> AsMut<WINDIVERT_ADDRESS> for WinDivertAddress<L> {
    #[inline]
    fn as_mut(&mut self) -> &mut WINDIVERT_ADDRESS {
        &mut self.data
    }
}

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.
    pub unsafe fn new() -> Self {
        Self {
            data: Default::default(),
            _layer: PhantomData,
        }
    }

    #[inline]
    fn data(&self) -> &WINDIVERT_DATA_NETWORK {
        // SAFETY: Thanks to typestate, we know that self is a network layer address
        unsafe { &self.data.union_field.Network }
    }

    #[inline]
    fn data_mut(&mut self) -> &mut WINDIVERT_DATA_NETWORK {
        // SAFETY: Thanks to typestate, we know that self is a network layer address
        unsafe { &mut self.data.union_field.Network }
    }

    /// The interface index on which the packet arrived (for inbound packets), or is to be sent (for outbound packets)
    #[inline]
    pub fn interface_index(&self) -> u32 {
        self.data().interface_id
    }

    /// Interface index setter
    #[inline]
    pub fn set_interface_index(&mut self, value: u32) {
        self.data_mut().interface_id = value
    }

    /// The sub-interface index for `interface_id()`
    #[inline]
    pub fn subinterface_index(&self) -> u32 {
        self.data().subinterface_id
    }

    /// Sub interface index setter
    #[inline]
    pub fn set_subinterface_index(&mut self, value: u32) {
        self.data_mut().subinterface_id = value
    }
}

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.
    pub unsafe fn new() -> Self {
        Self {
            data: Default::default(),
            _layer: PhantomData,
        }
    }

    #[inline]
    fn data(&self) -> &WINDIVERT_DATA_NETWORK {
        // SAFETY: Thanks to typestate, we know that self is a network layer address
        unsafe { &self.data.union_field.Network }
    }

    #[inline]
    fn data_mut(&mut self) -> &mut WINDIVERT_DATA_NETWORK {
        // SAFETY: Thanks to typestate, we know that self is a network layer address
        unsafe { &mut self.data.union_field.Network }
    }

    /// The interface index on which the packet arrived (for inbound packets), or is to be sent (for outbound packets)
    #[inline]
    pub fn interface_index(&self) -> u32 {
        self.data().interface_id
    }

    /// Interface index setter
    #[inline]
    pub fn set_interface_index(&mut self, value: u32) {
        self.data_mut().interface_id = value
    }

    /// The sub-interface index for `interface_id()`
    #[inline]
    pub fn subinterface_index(&self) -> u32 {
        self.data().subinterface_id
    }

    /// Sub interface index setter
    #[inline]
    pub fn set_subinterface_index(&mut self, value: u32) {
        self.data_mut().subinterface_id = value
    }
}

impl WinDivertAddress<layer::FlowLayer> {
    #[inline]
    fn data(&self) -> &WINDIVERT_DATA_FLOW {
        // SAFETY: Thanks to typestate, we know that self is a flow layer address
        unsafe { &self.data.union_field.Flow }
    }

    /// The endpoint ID of the flow
    #[inline]
    pub fn endpoint_id(&self) -> u64 {
        self.data().endpoint_id
    }

    /// The parent endpoint ID of the flow
    #[inline]
    pub fn parent_endpoint_id(&self) -> u64 {
        self.data().parent_endpoint_id
    }

    /// The parent endpoint ID of the flow
    #[inline]
    pub fn process_id(&self) -> u32 {
        self.data().process_id
    }

    /// The local address associated with the flow
    #[inline]
    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]))
        }
    }

    /// The remote address associated with the flow
    #[inline]
    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]))
        }
    }

    /// The locla port associated with the flow
    #[inline]
    pub fn local_port(&self) -> u16 {
        self.data().local_port
    }

    /// The remote port associated with the flow
    #[inline]
    pub fn remote_port(&self) -> u16 {
        self.data().remote_port
    }

    /// The protocol associated with the flow
    #[inline]
    pub fn protocol(&self) -> u8 {
        self.data().protocol
    }
}

impl WinDivertAddress<layer::SocketLayer> {
    #[inline]
    fn data(&self) -> &WINDIVERT_DATA_FLOW {
        // SAFETY: Thanks to typestate, we know that self is a flow layer address
        unsafe { &self.data.union_field.Flow }
    }

    /// The endpoint ID of the flow
    #[inline]
    pub fn endpoint_id(&self) -> u64 {
        self.data().endpoint_id
    }

    /// The parent endpoint ID of the flow
    #[inline]
    pub fn parent_endpoint_id(&self) -> u64 {
        self.data().parent_endpoint_id
    }

    /// The parent endpoint ID of the flow
    #[inline]
    pub fn process_id(&self) -> u32 {
        self.data().process_id
    }

    /// The local address associated with the flow
    #[inline]
    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]))
        }
    }

    /// The remote address associated with the flow
    #[inline]
    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]))
        }
    }

    /// The locla port associated with the flow
    #[inline]
    pub fn local_port(&self) -> u16 {
        self.data().local_port
    }

    /// The remote port associated with the flow
    #[inline]
    pub fn remote_port(&self) -> u16 {
        self.data().remote_port
    }

    /// The protocol associated with the flow
    #[inline]
    pub fn protocol(&self) -> u8 {
        self.data().protocol
    }
}

impl WinDivertAddress<layer::ReflectLayer> {
    #[inline]
    fn data(&self) -> &WINDIVERT_DATA_REFLECT {
        // SAFETY: Thanks to typestate, we know that self is a reflect layer address
        unsafe { &self.data.union_field.Reflect }
    }

    /// A timestamp indicating when the handle was opened
    #[inline]
    pub fn timestamp(&self) -> i64 {
        self.data().timestamp
    }

    /// The ID of the process that opened the handle
    #[inline]
    pub fn process_id(&self) -> u32 {
        self.data().process_id
    }

    /// The layer of the opened handle
    #[inline]
    pub fn layer(&self) -> WinDivertLayer {
        self.data().layer
    }

    /// The flags of the opened handle
    #[inline]
    pub fn flags(&self) -> WinDivertFlags {
        self.data().flags
    }

    /// The priority of the opened handle
    #[inline]
    pub fn priority(&self) -> i16 {
        self.data().priority
    }
}