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 {
    () => {
        /// Timestamp of the event. Uses same clock as `QueryPerformanceCounter()`
        pub fn event_timestamp(&self) -> i64 {
            self.data.timestamp
        }

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

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

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

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

        /// Outbound setter
        pub fn set_outbound(&mut self, value: bool) {
            self.data.to_mut().set_outbound(value)
        }

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

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

        /// Impostor setter
        pub fn set_impostor(&mut self, value: bool) {
            self.data.to_mut().set_impostor(value)
        }

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

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

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

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

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

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

        /// UDP checksum setter
        pub fn set_udp_checksum(&mut self, value: bool) {
            self.data.to_mut().set_udpchecksum(value)
        }
    };
}

#[derive(Debug, Default)]
/// Extra address data for [`Network`](WinDivertLayer::Network) packets
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 }
    }

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

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

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

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

#[derive(Debug, Default)]
/// Extra address data for [`Flow`](WinDivertLayer::Flow) packets
pub struct WinDivertFlowData<'a> {
    pub(crate) data: Cow<'a, WINDIVERT_ADDRESS>,
}

/// Extra address data for [`Socket`](WinDivertLayer::Socket) packets
pub type WinDivertSocketData<'a> = WinDivertFlowData<'a>;

impl<'a> WinDivertFlowData<'a> {
    addr_impl!();

    fn data(&self) -> &WINDIVERT_DATA_FLOW {
        unsafe { &self.data.union_field.Flow }
    }

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

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

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

    /// The local address associated with the flow
    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
    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
    pub fn local_port(&self) -> u16 {
        self.data().local_port
    }

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

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

#[derive(Debug)]
/// Extra address data for [`Reflect`](WinDivertLayer::Reflect) packets
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 }
    }

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

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

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

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

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