ndisapi 0.6.5

Rust crate for interacting with the Windows Packet Filter driver (NDISAPI)
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/// This module provides functionality for asynchronously reading packets from a network adapter, processing them, and logging them.
///
/// # Dependencies
///
/// This module depends on the following external crates:
/// * `clap`: For parsing command line arguments.
/// * `ndisapi`: For interacting with the network adapter.
/// * `smoltcp`: For parsing network packets.
/// * `std`: For various standard library features.
/// * `tokio`: For asynchronous programming.
/// * `windows`: For interacting with the Windows API.
/// * `crossterm`: For terminal manipulation.
/// * `prettytable`: For creating formatted tables.
///
/// # Constants
///
/// This module defines the following constants:
/// * `PACKET_NUMBER`: The number of packets to read from the adapter at a time.
///
/// # Structs
///
/// This module defines the following structs:
/// * `PacketInfo`: Represents the information contained in a network packet headers.
///
/// # Functions
///
/// This module defines the following functions:
/// * `main`: The main function of the program.
/// * `main_async`: Asynchronously reads packets from an adapter, processes them, and logs them.
/// * `async_loop`: Asynchronously reads packets from an adapter, processes them, and re-injects them back into the network stack.
/// * `process_packet_logs`: Asynchronously processes packet logs received from a channel.
/// * `update_display`: Asynchronously updates the terminal display with packet information every second.
///
/// Note: The specific behavior of each function is documented in the function's own documentation comment.
use clap::Parser;
use crossterm::{
    cursor::MoveTo,
    execute,
    terminal::{Clear, ClearType},
};
use ndisapi::{AsyncNdisapiAdapter, DirectionFlags, FilterFlags, IntermediateBuffer, Ndisapi};
use prettytable::{row, Table};
use smoltcp::wire::{
    ArpPacket, EthernetFrame, EthernetProtocol, Icmpv4Message, Icmpv4Packet, Icmpv6Message,
    Icmpv6Packet, IpAddress, IpProtocol, Ipv4Address, Ipv4Packet, Ipv6Packet, TcpPacket, UdpPacket,
};
use std::{collections::HashMap, fmt, sync::Arc};
use tokio::{
    sync::{
        mpsc::{self, Receiver},
        oneshot, Mutex,
    },
    time::{self, Duration},
};
use windows::core::Result;

const PACKET_NUMBER: usize = 510;

/// `PacketInfo` is a struct that represents the information contained in a network packet headers.
///
/// # Fields
/// * `ethertype`: The Ethernet protocol type of the packet. This field is of type `EthernetProtocol`.
/// * `src_addr`: The source IP address of the packet. This field is an `Option` that contains an `IpAddress`.
/// * `dst_addr`: The destination IP address of the packet. This field is an `Option` that contains an `IpAddress`.
/// * `protocol`: The protocol used by the packet (e.g., TCP, UDP, ICMP). This field is an `Option` that contains an `IpProtocol`.
/// * `src_port`: The source port number used by the packet. This field is an `Option` that contains a `u16`.
/// * `dst_port`: The destination port number used by the packet. This field is an `Option` that contains a `u16`.
/// * `icmp_v4_type`: The type of ICMPv4 message, if applicable. This field is an `Option` that contains an `Icmpv4Message`.
/// * `icmp_v6_type`: The type of ICMPv6 message, if applicable. This field is an `Option` that contains an `Icmpv6Message`.
/// * `icmp_code`: The ICMP code, if applicable. This field is an `Option` that contains a `u8`.
#[derive(Hash, Eq, PartialEq, Debug)]
struct PacketInfo {
    ethertype: EthernetProtocol,
    src_addr: Option<IpAddress>,
    dst_addr: Option<IpAddress>,
    protocol: Option<IpProtocol>,
    src_port: Option<u16>,
    dst_port: Option<u16>,
    icmp_v4_type: Option<Icmpv4Message>,
    icmp_v6_type: Option<Icmpv6Message>,
    icmp_code: Option<u8>,
}

/// Provides a default value for the `PacketInfo` struct.
///
/// # Returns
///
/// * A `PacketInfo` struct with the following default values:
///     * `ethertype`: `EthernetProtocol::Unknown(0)`. This represents an unknown or unsupported Ethernet protocol.
///     * `src_addr`: `None`. This indicates that the source IP address is not known or not applicable.
///     * `dst_addr`: `None`. This indicates that the destination IP address is not known or not applicable.
///     * `protocol`: `None`. This indicates that the protocol is not known or not applicable.
///     * `src_port`: `None`. This indicates that the source port is not known or not applicable.
///     * `dst_port`: `None`. This indicates that the destination port is not known or not applicable.
///     * `icmp_v4_type`: `None`. This indicates that the ICMPv4 message type is not known or not applicable.
///     * `icmp_v6_type`: `None`. This indicates that the ICMPv6 message type is not known or not applicable.
///     * `icmp_code`: `None`. This indicates that the ICMP code is not known or not applicable.
impl Default for PacketInfo {
    fn default() -> Self {
        PacketInfo {
            ethertype: EthernetProtocol::Unknown(0), // or any other default value you want
            src_addr: None,
            dst_addr: None,
            protocol: None,
            src_port: None,
            dst_port: None,
            icmp_v4_type: None,
            icmp_v6_type: None,
            icmp_code: None,
        }
    }
}

impl PacketInfo {
    /// Handles an IPv4 packet encapsulated in an Ethernet frame.
    ///
    /// # Arguments
    ///
    /// * `eth_hdr`: A reference to an `EthernetFrame` that encapsulates the IPv4 packet.
    ///
    /// # Returns
    ///
    /// * A `PacketInfo` struct that contains the parsed information from the IPv4 packet.
    ///
    /// # Behavior
    ///
    /// This function first creates an `Ipv4Packet` from the payload of the Ethernet frame.
    /// It then checks the protocol of the IPv4 packet and handles the packet accordingly:
    /// * If the protocol is ICMP, it creates an `Icmpv4Packet` from the payload of the IPv4 packet and sets the relevant fields in the `PacketInfo`.
    /// * If the protocol is TCP, it creates a `TcpPacket` from the payload of the IPv4 packet and sets the relevant fields in the `PacketInfo`.
    /// * If the protocol is UDP, it creates a `UdpPacket` from the payload of the IPv4 packet and sets the relevant fields in the `PacketInfo`.
    /// * For any other protocol, it sets the `protocol` field in the `PacketInfo` to the protocol of the IPv4 packet and leaves all other fields at their default values.
    fn handle_ipv4_packet(eth_hdr: &EthernetFrame<&[u8]>) -> PacketInfo {
        let ipv4_packet = Ipv4Packet::new_unchecked(eth_hdr.payload());
        match ipv4_packet.next_header() {
            IpProtocol::Icmp => {
                let icmp_packet = Icmpv4Packet::new_unchecked(ipv4_packet.payload());
                PacketInfo {
                    ethertype: EthernetProtocol::Ipv4,
                    src_addr: Some(IpAddress::Ipv4(ipv4_packet.src_addr())),
                    dst_addr: Some(IpAddress::Ipv4(ipv4_packet.dst_addr())),
                    protocol: Some(IpProtocol::Icmp),
                    src_port: None,
                    dst_port: None,
                    icmp_v4_type: Some(icmp_packet.msg_type()),
                    icmp_v6_type: None,
                    icmp_code: Some(icmp_packet.msg_code()),
                }
            }
            IpProtocol::Tcp => {
                let tcp_packet = TcpPacket::new_unchecked(ipv4_packet.payload());
                PacketInfo {
                    ethertype: EthernetProtocol::Ipv4,
                    src_addr: Some(IpAddress::Ipv4(ipv4_packet.src_addr())),
                    dst_addr: Some(IpAddress::Ipv4(ipv4_packet.dst_addr())),
                    protocol: Some(IpProtocol::Tcp),
                    src_port: Some(tcp_packet.src_port()),
                    dst_port: Some(tcp_packet.dst_port()),
                    icmp_v4_type: None,
                    icmp_v6_type: None,
                    icmp_code: None,
                }
            }
            IpProtocol::Udp => {
                let udp_packet = UdpPacket::new_unchecked(ipv4_packet.payload());
                PacketInfo {
                    ethertype: EthernetProtocol::Ipv4,
                    src_addr: Some(IpAddress::Ipv4(ipv4_packet.src_addr())),
                    dst_addr: Some(IpAddress::Ipv4(ipv4_packet.dst_addr())),
                    protocol: Some(IpProtocol::Udp),
                    src_port: Some(udp_packet.src_port()),
                    dst_port: Some(udp_packet.dst_port()),
                    icmp_v4_type: None,
                    icmp_v6_type: None,
                    icmp_code: None,
                }
            }
            _ => PacketInfo {
                ethertype: EthernetProtocol::Ipv4,
                src_addr: Some(IpAddress::Ipv4(ipv4_packet.src_addr())),
                dst_addr: Some(IpAddress::Ipv4(ipv4_packet.dst_addr())),
                protocol: Some(ipv4_packet.next_header()),
                src_port: None,
                dst_port: None,
                icmp_v4_type: None,
                icmp_v6_type: None,
                icmp_code: None,
            },
        }
    }

    /// Handles an IPv6 packet encapsulated in an Ethernet frame.
    ///
    /// # Arguments
    ///
    /// * `eth_hdr`: A reference to an `EthernetFrame` that encapsulates the IPv6 packet.
    ///
    /// # Returns
    ///
    /// * A `PacketInfo` struct that contains the parsed information from the IPv6 packet.
    ///
    /// # Behavior
    ///
    /// This function first creates an `Ipv6Packet` from the payload of the Ethernet frame.
    /// It then checks the next header field of the IPv6 packet and handles the packet accordingly:
    /// * If the next header is ICMPv6, it creates an `Icmpv6Packet` from the payload of the IPv6 packet and sets the relevant fields in the `PacketInfo`.
    /// * If the next header is TCP, it creates a `TcpPacket` from the payload of the IPv6 packet and sets the relevant fields in the `PacketInfo`.
    /// * If the next header is UDP, it creates a `UdpPacket` from the payload of the IPv6 packet and sets the relevant fields in the `PacketInfo`.
    /// * For any other next header, it sets the `protocol` field in the `PacketInfo` to the next header of the IPv6 packet and leaves all other fields at their default values.
    fn handle_ipv6_packet(eth_hdr: &EthernetFrame<&[u8]>) -> PacketInfo {
        let ipv6_packet = Ipv6Packet::new_unchecked(eth_hdr.payload());
        match ipv6_packet.next_header() {
            IpProtocol::Icmpv6 => {
                let icmp_packet = Icmpv6Packet::new_unchecked(ipv6_packet.payload());
                PacketInfo {
                    ethertype: EthernetProtocol::Ipv6,
                    src_addr: Some(IpAddress::Ipv6(ipv6_packet.src_addr())),
                    dst_addr: Some(IpAddress::Ipv6(ipv6_packet.dst_addr())),
                    protocol: Some(IpProtocol::Icmpv6),
                    src_port: None,
                    dst_port: None,
                    icmp_v4_type: None,
                    icmp_v6_type: Some(icmp_packet.msg_type()),
                    icmp_code: Some(icmp_packet.msg_code()),
                }
            }
            IpProtocol::Tcp => {
                let tcp_packet = TcpPacket::new_unchecked(ipv6_packet.payload());
                PacketInfo {
                    ethertype: EthernetProtocol::Ipv6,
                    src_addr: Some(IpAddress::Ipv6(ipv6_packet.src_addr())),
                    dst_addr: Some(IpAddress::Ipv6(ipv6_packet.dst_addr())),
                    protocol: Some(IpProtocol::Tcp),
                    src_port: Some(tcp_packet.src_port()),
                    dst_port: Some(tcp_packet.dst_port()),
                    icmp_v4_type: None,
                    icmp_v6_type: None,
                    icmp_code: None,
                }
            }
            IpProtocol::Udp => {
                let udp_packet = UdpPacket::new_unchecked(ipv6_packet.payload());
                PacketInfo {
                    ethertype: EthernetProtocol::Ipv6,
                    src_addr: Some(IpAddress::Ipv6(ipv6_packet.src_addr())),
                    dst_addr: Some(IpAddress::Ipv6(ipv6_packet.dst_addr())),
                    protocol: Some(IpProtocol::Udp),
                    src_port: Some(udp_packet.src_port()),
                    dst_port: Some(udp_packet.dst_port()),
                    icmp_v4_type: None,
                    icmp_v6_type: None,
                    icmp_code: None,
                }
            }
            _ => PacketInfo {
                ethertype: EthernetProtocol::Ipv6,
                src_addr: Some(IpAddress::Ipv6(ipv6_packet.src_addr())),
                dst_addr: Some(IpAddress::Ipv6(ipv6_packet.dst_addr())),
                protocol: Some(ipv6_packet.next_header()),
                src_port: None,
                dst_port: None,
                icmp_v4_type: None,
                icmp_v6_type: None,
                icmp_code: None,
            },
        }
    }

    /// Handles an ARP packet encapsulated in an Ethernet frame.
    ///
    /// # Arguments
    ///
    /// * `eth_hdr`: A reference to an `EthernetFrame` that encapsulates the ARP packet.
    ///
    /// # Returns
    ///
    /// * A `PacketInfo` struct that contains the parsed information from the ARP packet.
    ///
    /// # Behavior
    ///
    /// This function first creates an `ArpPacket` from the payload of the Ethernet frame.
    /// It then creates a `PacketInfo` with the `ethertype` field set to `EthernetProtocol::Arp`,
    /// the `src_addr` field set to the source protocol address from the ARP packet,
    /// and the `dst_addr` field set to the target protocol address from the ARP packet.
    /// All other fields are set to `None` because they are not applicable to ARP packets.
    fn handle_arp_packet(eth_hdr: &EthernetFrame<&[u8]>) -> PacketInfo {
        let arp_packet = ArpPacket::new_unchecked(eth_hdr.payload());
        
        // Convert slices to fixed-size arrays
        let src_bytes: [u8; 4] = arp_packet
            .source_protocol_addr()
            .try_into()
            .unwrap_or([0u8; 4]);
        let dst_bytes: [u8; 4] = arp_packet
            .target_protocol_addr()
            .try_into()
            .unwrap_or([0u8; 4]);
        
        PacketInfo {
            ethertype: EthernetProtocol::Arp,
            src_addr: Some(IpAddress::Ipv4(Ipv4Address::from_octets(src_bytes))),
            dst_addr: Some(IpAddress::Ipv4(Ipv4Address::from_octets(dst_bytes))),
            protocol: None,
            src_port: None,
            dst_port: None,
            icmp_v4_type: None,
            icmp_v6_type: None,
            icmp_code: None,
        }
    }

    /// Creates a new `PacketInfo` from an `IntermediateBuffer`.
    ///
    /// # Arguments
    ///
    /// * `packet`: A reference to an `IntermediateBuffer` that contains the packet data.
    ///
    /// # Returns
    ///
    /// * A `PacketInfo` struct that contains the parsed information from the packet.
    ///
    /// # Behavior
    ///
    /// This function first creates an `EthernetFrame` from the data in the `IntermediateBuffer`.
    /// It then checks the ethertype of the Ethernet frame and handles the packet accordingly:
    /// * If the ethertype is IPv4, it calls `handle_ipv4_packet` to handle the packet.
    /// * If the ethertype is IPv6, it calls `handle_ipv6_packet` to handle the packet.
    /// * If the ethertype is ARP, it calls `handle_arp_packet` to handle the packet.
    /// * For any other ethertype, it creates a `PacketInfo` with the `ethertype` field set to the ethertype of the Ethernet frame and all other fields set to their default values.
    pub fn new(packet: &IntermediateBuffer) -> Self {
        let eth_hdr = EthernetFrame::new_unchecked(packet.get_data());
        match eth_hdr.ethertype() {
            EthernetProtocol::Ipv4 => Self::handle_ipv4_packet(&eth_hdr),
            EthernetProtocol::Ipv6 => Self::handle_ipv6_packet(&eth_hdr),
            EthernetProtocol::Arp => Self::handle_arp_packet(&eth_hdr),
            _ => {
                // Handle other ethertypes here
                PacketInfo {
                    ethertype: eth_hdr.ethertype(),
                    ..PacketInfo::default()
                }
            }
        }
    }
}

/// Implementation of the `Display` trait for `PacketInfo`.
///
/// This implementation provides a human-readable representation of a `PacketInfo` instance. It includes the
/// Ethernet type, source and destination IP addresses, and protocol of the packet. If applicable, it also includes
/// the source and destination port numbers, and the ICMP type and code.
///
/// # Example
///
/// ```rust
/// let packet_info = PacketInfo {
///     ethertype: EthernetProtocol::Ipv4,
///     src_addr: IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1)),
///     dst_addr: IpAddr::V4(Ipv4Addr::new(192, 168, 1, 2)),
///     protocol: IpProtocol::Tcp,
///     src_port: Some(12345),
///     dst_port: Some(80),
///     icmp_type: None,
///     icmp_code: None,
/// };
///
/// println!("{}", packet_info);
/// ```
///
/// This will output:
///
/// ```text
/// Ethertype: Ipv4
/// Source Address: 192.168.1.1
/// Destination Address: 192.168.1.2
/// Protocol: Tcp
/// Source Port: 12345
/// Destination Port: 80
/// ```
impl fmt::Display for PacketInfo {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "Ethertype: {:?}\nSource Address: {:?}\nDestination Address: {:?}\nProtocol: {:?}\n",
            self.ethertype, self.src_addr, self.dst_addr, self.protocol
        )?;

        if let Some(src_port) = self.src_port {
            writeln!(f, "Source Port: {}", src_port)?;
        }

        if let Some(dst_port) = self.dst_port {
            writeln!(f, "Destination Port: {}", dst_port)?;
        }

        if let Some(icmp_v4_type) = self.icmp_v4_type {
            writeln!(f, "ICMPv4 Type: {:?}", icmp_v4_type)?;
        }

        if let Some(icmp_v6_type) = self.icmp_v6_type {
            writeln!(f, "ICMPv6 Type: {:?}", icmp_v6_type)?;
        }

        if let Some(icmp_code) = self.icmp_code {
            writeln!(f, "ICMP Code: {:?}", icmp_code)?;
        }

        Ok(())
    }
}

/// Asynchronously reads packets from an adapter, processes them, and re-injects them back into the network stack.
///
/// # Arguments
///
/// * `adapter`: A mutable reference to an `AsyncNdisapiAdapter` that represents the network adapter.
/// * `tx`: A `mpsc::Sender` that is used to send `PacketInfo` structs to another part of the program.
///
/// # Returns
///
/// * A `Result` that indicates whether the function succeeded or failed. The function returns `Ok(())` if it succeeded and `Err(e)` if it failed, where `e` is the error that occurred.
///
/// # Behavior
///
/// This function first sets the adapter mode to `MSTCP_FLAG_SENT_RECEIVE_TUNNEL` to capture both sent and received packets.
/// It then enters a loop where it does the following on each iteration:
/// * Reads packets from the adapter into a vector of `IntermediateBuffer`s.
/// * For each packet read, it creates a `PacketInfo` from the packet data and sends it to the `tx` channel.
/// * It partitions the packets into two collections: one for packets that were sent and one for packets that were received.
/// * It re-injects the sent packets back into the adapter and the received packets back into the MSTCP.
async fn async_loop(adapter: &mut AsyncNdisapiAdapter, tx: mpsc::Sender<PacketInfo>) -> Result<()> {
    // Set the adapter mode to MSTCP_FLAG_SENT_RECEIVE_TUNNEL.
    adapter.set_adapter_mode(FilterFlags::MSTCP_FLAG_SENT_RECEIVE_TUNNEL)?;

    // Initialize a vector of IntermediateBuffers, `ibs`, to store network packet data.
    // Each IntermediateBuffer is heap-allocated, providing a structure to handle raw packet data.
    let mut packets: Vec<IntermediateBuffer> = vec![Default::default(); PACKET_NUMBER];

    loop {
        // Read packets from the adapter.
        let packets_read = match adapter.read_packets::<PACKET_NUMBER>(&mut packets).await {
            Ok(packets_read) => {
                if packets_read == 0 {
                    println!("No packets read. Continue reading.");
                    continue;
                } else {
                    packets_read
                }
            }
            Err(_) => {
                continue;
            }
        };

        for packet in packets[0..packets_read].iter() {
            // Get packet information as a string.
            let packet_info = PacketInfo::new(packet);

            // Send packet information to the tx channel.
            tx.send(packet_info).await.unwrap();
        }

        // Partition the iterator into two collections based on the device flag.
        let (send_packets, receive_packets): (Vec<_>, Vec<_>) = packets[0..packets_read]
            .iter()
            .partition(|ib| ib.get_device_flags() == DirectionFlags::PACKET_FLAG_ON_SEND);

        // Re-inject packets back into the network stack
        match adapter.send_packets_to_adapter::<PACKET_NUMBER>(send_packets) {
            Ok(_) => {}
            Err(err) => println!("Error sending packet to adapter. Error code = {err}"),
        }

        match adapter.send_packets_to_mstcp::<PACKET_NUMBER>(receive_packets) {
            Ok(_) => {}
            Err(err) => println!("Error sending packet to adapter. Error code = {err}"),
        }
    }
}

/// Asynchronously updates the terminal display with packet information every second.
///
/// # Arguments
///
/// * `shared_table`: An `Arc<Mutex<HashMap<PacketInfo, u32>>>` that represents a shared `HashMap` mapping `PacketInfo` structs to counts.
///
/// # Behavior
///
/// This function does the following:
/// * Sets up a one-second interval timer.
/// * Enters a loop where it does the following on each iteration:
///     * Waits for the interval timer to tick.
///     * Locks the `shared_table` to access the `HashMap`.
///     * Clears the terminal.
///     * Creates a new table with columns for the protocol, source, destination, and count.
///     * Sorts the entries in the `HashMap` by count in descending order and takes the top 10 entries.
///     * For each entry, it formats the source and destination as strings, adds a row to the table with the protocol, source, destination, and count, and then prints the table.
///
/// Note: This function does not return. It continues to update the display until the program is terminated.
async fn update_display(shared_table: Arc<Mutex<HashMap<PacketInfo, u32>>>) {
    let mut interval = time::interval(Duration::from_secs(1));

    loop {
        interval.tick().await;
        let packet_info_table = shared_table.lock().await;

        // Clear the terminal
        execute!(std::io::stdout(), Clear(ClearType::All), MoveTo(0, 0)).unwrap();

        // Create and populate the table
        let mut table = Table::new();
        table.add_row(row!["Protocol", "Source", "Destination", "Count"]);

        let mut counts: Vec<_> = packet_info_table
            .iter()
            .map(|(pi, &count)| (pi, count))
            .collect();
        counts.sort_by(|a, b| b.1.cmp(&a.1));
        let top_entries = &counts[..std::cmp::min(10, counts.len())];

        for (packet_info, count) in top_entries {
            let src = format!(
                "{}:{}",
                packet_info
                    .src_addr
                    .unwrap_or(IpAddress::Ipv4(Ipv4Address::new(0, 0, 0, 0))),
                packet_info.src_port.unwrap_or_default()
            );
            let dst = format!(
                "{}:{}",
                packet_info
                    .dst_addr
                    .unwrap_or(IpAddress::Ipv4(Ipv4Address::new(0, 0, 0, 0))),
                packet_info.dst_port.unwrap_or_default()
            );
            table.add_row(row![
                packet_info.protocol.unwrap_or(IpProtocol::Unknown(0)),
                src,
                dst,
                count.to_string()
            ]);
        }

        // Print the table
        table.printstd();
    }
}

/// Asynchronously processes packet logs received from a channel.
///
/// # Arguments
///
/// * `log_rx`: A mutable reference to a `Receiver<PacketInfo>` that receives `PacketInfo` structs from a channel.
///
/// # Behavior
///
/// This function does the following:
/// * Creates a shared `HashMap` that maps `PacketInfo` structs to counts.
/// * Spawns a new task that updates the display with the contents of the `HashMap`.
/// * Enters a loop where it does the following on each iteration:
///     * Waits for a `PacketInfo` struct from the `log_rx` channel.
///     * If it receives a `PacketInfo` struct, it increments the count for that `PacketInfo` in the `HashMap`.
///
/// Note: This function does not return. It continues to process packet logs until the program is terminated.
async fn process_packet_logs(mut log_rx: Receiver<PacketInfo>) {
    let packet_info_table = Arc::new(Mutex::new(HashMap::new()));
    tokio::spawn(update_display(packet_info_table.clone()));

    loop {
        if let Some(log_message) = log_rx.recv().await {
            let mut table = packet_info_table.lock().await;
            *table.entry(log_message).or_insert(0) += 1;
        }
    }
}

/// Asynchronously reads packets from an adapter, processes them, and logs them.
///
/// # Arguments
///
/// * `adapter`: A mutable reference to an `AsyncNdisapiAdapter` that represents the network adapter.
///
/// # Behavior
///
/// This function does the following:
/// * Prompts the user to press ENTER to exit.
/// * Initializes a `oneshot` channel for communication between this function and a spawned thread that waits for the user to press ENTER.
/// * Initializes a `mpsc` channel for logging. The transmitter end of the channel is passed to the `async_loop` function, which sends `PacketInfo` structs to the receiver end of the channel.
/// * Spawns a new thread that waits for the user to press ENTER and sends a message through the `oneshot` channel when the user does so.
/// * Spawns a new thread that receives `PacketInfo` structs from the `mpsc` channel and logs them.
/// * Calls the `async_loop` function to read packets from the adapter and process them.
/// * Waits for either the `async_loop` function to return a result or the thread that waits for the user to press ENTER to receive a message. If the latter happens, it prints "Shutting down..." and returns.
/// * Prints any errors that may have occurred during the program's execution.
async fn main_async(adapter: &mut AsyncNdisapiAdapter) {
    // Prompts the user to press ENTER to exit.
    println!("Press ENTER to exit");

    // Initializes a channel for communication between this function and a spawned thread.
    // `tx` is the transmitter and `rx` is the receiver end of the channel.
    let (tx, rx) = oneshot::channel::<()>();

    // Initializes a channel for logging.
    // `log_tx` is the transmitter and `log_rx` is the receiver end of the channel.
    let (log_tx, log_rx) = mpsc::channel::<PacketInfo>(PACKET_NUMBER * 100);

    // Spawns a new thread using Tokio's runtime, that waits for the user to press ENTER.
    tokio::spawn(async move {
        let mut line = String::new();
        std::io::stdin().read_line(&mut line).unwrap();

        // Sends a message through the channel when the user presses ENTER.
        let _ = tx.send(());
    });

    // Waits for either the server to return a result or the thread with `rx` to receive a message.
    // This is achieved by using the select! macro which polls multiple futures and blocks until one of them is ready.
    let result = tokio::select! {
        // The async_loop function reads from the adapter and processes the packets.
        result = async_loop(adapter, log_tx) => result,
        _ = process_packet_logs(log_rx) => Ok(()),
        // If the receiver end of the channel receives a message, the program prints "Shutting down..." and returns Ok(()).
        _ = rx => {
            println!("Shutting down...");
            Ok(()) // Thread returns Ok() if it receives the message successfully.
        }
    };

    // Prints any errors that may have occurred during the program's execution.
    if let Err(e) = result {
        eprintln!("Server error: {}", e);
    }
}

/// A struct representing the command line arguments.
#[derive(Parser)]
struct Cli {
    /// Network interface index (please use listadapters example to determine the right one)
    #[clap(short, long)]
    interface_index: usize,
}

/// The main function of the program.
///
/// # Behavior
///
/// This function does the following:
/// * Parses command line arguments to get the interface index.
/// * Decrements the interface index to match zero-based index.
/// * Creates a new `Ndisapi` driver instance.
/// * Prints the detected version of the Windows Packet Filter.
/// * Gets a list of TCP/IP bound adapters in the system.
/// * Checks if the selected interface index is within the range of available interfaces. If not, it panics.
/// * Prints the name of the selected interface.
/// * Creates a new instance of `AsyncNdisapiAdapter` with the selected interface.
/// * Executes the `main_async` function using the previously defined adapter.
///
/// # Returns
///
/// * A `Result` that indicates whether the function succeeded or failed. The function returns `Ok(())` if it succeeded and `Err(e)` if it failed, where `e` is the error that occurred.
#[tokio::main]
async fn main() -> Result<()> {
    // Parsing command line arguments.
    let Cli {
        mut interface_index,
    } = Cli::parse();

    // Decrement interface index to match zero-based index.
    interface_index -= 1;

    // Create a new Ndisapi driver instance.
    let driver = Arc::new(
        Ndisapi::new("NDISRD").expect("WinpkFilter driver is not installed or failed to load!"),
    );

    // Print the detected version of the Windows Packet Filter.
    println!(
        "Detected Windows Packet Filter version {}",
        driver.get_version()?
    );

    // Get a list of TCP/IP bound adapters in the system.
    let adapters = driver.get_tcpip_bound_adapters_info()?;

    // Check if the selected interface index is within the range of available interfaces.
    if interface_index + 1 > adapters.len() {
        panic!("Interface index is beyond the number of available interfaces");
    }

    // Print the name of the selected interface.
    println!("Using interface {}", adapters[interface_index].get_name(),);

    // Create a new instance of AsyncNdisapiAdapter with the selected interface.
    let mut adapter =
        AsyncNdisapiAdapter::new(Arc::clone(&driver), adapters[interface_index].get_handle())
            .unwrap();

    // Execute the main_async function using the previously defined adapter.
    main_async(&mut adapter).await;
    Ok(())
}