rscap 0.3.2

Cross-platform packet capture and transmission utilities
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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
// SPDX-License-Identifier: MIT OR Apache-2.0
//
// Copyright (c) 2024 Nathaniel Bennett <me[at]nathanielbennett[dotcom]>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Network-layer packet capture/transmission utilities.
//!

use std::os::fd::RawFd;
use std::{io, mem, os::fd::AsRawFd, ptr};

use super::addr::{L2Addr, L2Protocol};
use super::mapped::{
    BlockConfig, FrameIndex, OsiLayer, PacketRxRing, PacketTxRing, RxFrame, TxFrame, TxFrameVariant,
};
use super::{FanoutAlgorithm, RxTimestamping, TxTimestamping};

use crate::filter::PacketStatistics;
use crate::Interface;

/// A socket that exchanges packets at the network layer.
///
/// In Linux, this corresponds to `socket(AF_PACKET, SOCK_RAW, 0)`.
pub struct L3Socket {
    fd: i32,
}

impl L3Socket {
    /// Create a new network-layer socket.
    ///
    /// By default, network-layer sockets do not listen or receive packets on any protocol or
    /// interface; to begin receiving packets, call [`bind()`](L3Socket::bind()).
    ///
    /// # Permissions
    ///
    /// A program must have the `CAP_NET_RAW` capability in order for this call to succeed;
    /// otherwise, `EPERM` will be returned.
    #[inline]
    pub fn new() -> io::Result<L3Socket> {
        // Set the socket to receive no packets by default (protocol: 0)
        match unsafe { libc::socket(libc::AF_PACKET, libc::SOCK_DGRAM, 0) } {
            ..=-1 => Err(std::io::Error::last_os_error()),
            fd => Ok(L3Socket { fd }),
        }
    }

    /// Bind the network-layer socket to a particular protocol/address and interface and begin
    /// receiving packets.
    ///
    /// The address type can be any implementor of the [`L2Addr`] trait. For concrete examples,
    /// refer to its documentation.
    pub fn bind(&self, iface: Interface, proto: L2Protocol) -> io::Result<()> {
        let sockaddr = libc::sockaddr_ll {
            sll_family: libc::AF_PACKET as u16,
            sll_protocol: u16::from(proto),
            sll_ifindex: iface.index()? as i32,
            sll_hatype: 0,
            sll_pkttype: 0,
            sll_halen: 6,
            sll_addr: [0u8; 8],
        };

        // SAFETY: `ptr::addr_of!(sockaddr_ll)` will always yield a pointer to
        // `mem::size_of::<libc::sockaddr_ll>()` valid bytes.
        match unsafe {
            libc::bind(
                self.fd,
                ptr::addr_of!(sockaddr) as *const libc::sockaddr,
                mem::size_of::<libc::sockaddr_ll>() as u32,
            )
        } {
            0 => Ok(()),
            _ => Err(io::Error::last_os_error()),
        }
    }

    /// Binds the device to all interfaces, enabling it to begin receiving packets matching the
    /// link-layer protocol `proto`.
    pub fn bind_all(&self, proto: L2Protocol) -> io::Result<()> {
        let sockaddr = libc::sockaddr_ll {
            sll_family: libc::AF_PACKET as u16,
            sll_protocol: u16::from(proto),
            sll_ifindex: 0,
            sll_hatype: 0,
            sll_pkttype: 0,
            sll_halen: 6,
            sll_addr: [0u8; 8],
        };

        // SAFETY: `ptr::addr_of!(sockaddr_ll)` will always yield a pointer to
        // `mem::size_of::<libc::sockaddr_ll>()` valid bytes.
        match unsafe {
            libc::bind(
                self.fd,
                ptr::addr_of!(sockaddr) as *const libc::sockaddr,
                mem::size_of::<libc::sockaddr_ll>() as u32,
            )
        } {
            0 => Ok(()),
            _ => Err(io::Error::last_os_error()),
        }
    }

    /// Moves the network-layer socket's behavior in or out of blocking mode.
    ///
    /// An [`L3Socket`] that is nonblocking will return with an error of kind
    /// [WouldBlock](io::ErrorKind::WouldBlock) whenever a packet cannot be immediately sent or
    /// received. This is only applicable to the [`send()`](L3Socket::send()) or
    /// [`recv()`](L3Socket::recv()) methods; any memory-mapped methods are always guaranteed to be
    /// nonblocking.
    pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
        let mut fcntl_flags = match unsafe { libc::fcntl(self.fd, libc::F_GETFL, 0) } {
            ..=-1 => return Err(io::Error::last_os_error()),
            f => f,
        };

        if nonblocking {
            fcntl_flags |= libc::O_NONBLOCK;
        } else {
            fcntl_flags &= !libc::O_NONBLOCK;
        }

        match unsafe { libc::fcntl(self.fd, libc::F_SETFL, fcntl_flags) } {
            0 => Ok(()),
            _ => Err(io::Error::last_os_error()),
        }
    }

    /// Configures transmitted packets to bypass kernel's traffic control (qdisc) layer.
    ///
    /// This allows packet buffering at the qdisc layer to be avoided, which is useful for
    /// applications that intend to intentionally flood a network with traffic. Enabling this option
    /// will lead to increased packet drops in transmission when network devices are busy (as the
    /// kernel will not be buffering packets originating from the socket).
    ///
    /// This option is disabled (`false`) by default.
    pub fn set_qdisc_bypass(&self, bypass: bool) -> io::Result<()> {
        let bypass_req = if bypass { 1u32 } else { 0u32 };

        if unsafe {
            libc::setsockopt(
                self.fd,
                libc::SOL_PACKET,
                crate::linux::PACKET_QDISC_BYPASS,
                ptr::addr_of!(bypass_req) as *const libc::c_void,
                mem::size_of::<u32>() as u32,
            ) != 0
        } {
            return Err(io::Error::last_os_error());
        }

        Ok(())
    }

    /// Returns packet statistics about the current socket.
    ///
    /// Packet statistics include [`received`](PacketStatistics::received) and
    /// [`dropped`](PacketStatistics::dropped) packet counts; both of these counters are reset
    /// each time this method is called.
    pub fn packet_stats(&self) -> io::Result<PacketStatistics> {
        let mut stats = crate::linux::tpacket_stats {
            tp_packets: 0,
            tp_drops: 0,
        };

        let mut stats_len = mem::size_of::<crate::linux::tpacket_stats>() as u32;

        if unsafe {
            libc::getsockopt(
                self.fd,
                libc::SOL_PACKET,
                crate::linux::PACKET_STATISTICS,
                ptr::addr_of_mut!(stats) as *mut libc::c_void,
                ptr::addr_of_mut!(stats_len),
            ) != 0
        } {
            return Err(io::Error::last_os_error());
        }
        debug_assert!(stats_len == mem::size_of::<crate::linux::tpacket_stats>() as u32);

        Ok(PacketStatistics {
            received: stats.tp_packets,
            dropped: stats.tp_drops,
        })
    }

    /// Returns the interface that the socket is currently sniffing on.
    pub fn interface(&self) -> io::Result<Interface> {
        let mut sockaddr = libc::sockaddr_ll {
            sll_family: 0,
            sll_protocol: 0,
            sll_ifindex: 0,
            sll_hatype: 0,
            sll_pkttype: 0,
            sll_halen: 0,
            sll_addr: [0u8; 8],
        };
        let mut sockaddr_len = mem::size_of::<libc::sockaddr_ll>() as u32;

        let res = unsafe {
            libc::getsockname(
                self.fd,
                ptr::addr_of_mut!(sockaddr) as *mut libc::sockaddr,
                ptr::addr_of_mut!(sockaddr_len),
            )
        };
        if res != 0 {
            return Err(io::Error::last_os_error());
        }

        if sockaddr_len != mem::size_of::<libc::sockaddr_ll>() as u32
            || sockaddr.sll_family != libc::AF_PACKET as u16
        {
            return Err(io::Error::new(
                io::ErrorKind::AddrNotAvailable,
                "bound address was not of type sockaddr_ll",
            ));
        }

        Interface::from_index(sockaddr.sll_ifindex as u32)
    }

    /// Determines the source of timestamp information for TX_RING/RX_RING packets.
    /// If `true`, this option requests that the operating system use hardware timestamps
    /// provided by the NIC.
    ///
    /// For hardware timestamping to be employed, the socket must be bound to a specific interface.
    ///
    /// # Errors
    ///
    /// `ERANGE` - the requested packets cannot be timestamped by hardware.
    ///
    /// `EINVAL` - hardware timestamping is not supported by the network card.
    fn set_timestamp_method(&self, tx: TxTimestamping, rx: RxTimestamping) -> io::Result<()> {
        if tx == TxTimestamping::Hardware || rx == RxTimestamping::Hardware {
            let mut hwtstamp_config = libc::hwtstamp_config {
                flags: 0,
                tx_type: if tx == TxTimestamping::Hardware {
                    libc::HWTSTAMP_TX_ON
                } else {
                    libc::HWTSTAMP_TX_OFF
                } as i32,
                rx_filter: if rx == RxTimestamping::Hardware {
                    libc::HWTSTAMP_FILTER_ALL
                } else {
                    libc::HWTSTAMP_FILTER_NONE
                } as i32,
            };

            let iface = self.interface()?;

            let mut if_name = [0i8; libc::IF_NAMESIZE];
            let if_name_ptr = if_name.as_mut_ptr();

            let res = unsafe { libc::if_indextoname(iface.index()?, if_name_ptr) };
            if res.is_null() {
                return Err(io::Error::last_os_error());
            }

            let mut ifreq = libc::ifreq {
                ifr_name: if_name,
                ifr_ifru: libc::__c_anonymous_ifr_ifru {
                    ifru_data: ptr::addr_of_mut!(hwtstamp_config) as *mut i8,
                },
            };

            let res =
                unsafe { libc::ioctl(self.fd, libc::SIOCSHWTSTAMP, ptr::addr_of_mut!(ifreq)) };
            if res != 0 {
                return Err(io::Error::last_os_error());
            }
        }

        let timestamp_req = match tx {
            TxTimestamping::Hardware => libc::SOF_TIMESTAMPING_TX_HARDWARE,
            TxTimestamping::Software => libc::SOF_TIMESTAMPING_TX_SOFTWARE,
            TxTimestamping::Sched => libc::SOF_TIMESTAMPING_TX_SCHED,
        } | match rx {
            RxTimestamping::Hardware => libc::SOF_TIMESTAMPING_RX_HARDWARE,
            RxTimestamping::Software => libc::SOF_TIMESTAMPING_RX_SOFTWARE,
        };

        if unsafe {
            libc::setsockopt(
                self.fd,
                libc::SOL_PACKET,
                crate::linux::PACKET_TIMESTAMP,
                ptr::addr_of!(timestamp_req) as *const libc::c_void,
                mem::size_of::<crate::linux::tpacket_versions>() as u32,
            ) != 0
        } {
            return Err(io::Error::last_os_error());
        }

        Ok(())
    }

    /// Configures the network device the socket is currently bound to to act in promiscuous mode.
    ///
    /// A network device in promiscuous mode will capture all packets observed on a physical medium,
    /// not just those destined for it.
    pub fn set_promiscuous(&self, promisc: bool) -> io::Result<()> {
        let iface = self.interface()?;

        let req = libc::packet_mreq {
            mr_ifindex: iface.index()? as i32,
            mr_type: libc::PACKET_MR_PROMISC as u16,
            mr_alen: 0,
            mr_address: [0u8; 8],
        };

        if unsafe {
            libc::setsockopt(
                self.fd,
                libc::SOL_PACKET,
                if promisc {
                    libc::PACKET_ADD_MEMBERSHIP
                } else {
                    libc::PACKET_DROP_MEMBERSHIP
                },
                ptr::addr_of!(req) as *const libc::c_void,
                mem::size_of::<libc::packet_mreq>() as u32,
            ) != 0
        } {
            return Err(io::Error::last_os_error());
        }

        Ok(())
    }

    /// Adds the given socket to a fanout group.
    ///
    /// A fanout group is a set of sockets that act together to process packets. Each received
    /// packet is sent to only one of the sockets in the fanout group, based on the algorithm
    /// chosen in `fan_alg`. The group is specified using a 16-bit group identifier, `group_id`.
    /// Some additional options can be set:
    ///
    /// - `defrag` causes the kernel to defragment IP packets prior to sending them to the
    /// fanout group (e.g. to ensure [`FanoutAlgorithm::Hash`] works despite fragmentation)
    /// - `rollover` causes packets to be sent to a different socket than originally decided
    /// by `fan_alg` if the original socket is backlogged with packets.
    pub fn set_fanout(
        &self,
        group_id: u16,
        fan_alg: FanoutAlgorithm,
        defrag: bool,
        rollover: bool,
    ) -> io::Result<()> {
        let mut opt = match fan_alg {
            FanoutAlgorithm::Cpu => crate::linux::PACKET_FANOUT_CPU,
            FanoutAlgorithm::Hash => crate::linux::PACKET_FANOUT_HASH,
            FanoutAlgorithm::QueueMapping => crate::linux::PACKET_FANOUT_QM,
            FanoutAlgorithm::Random => crate::linux::PACKET_FANOUT_RND,
            FanoutAlgorithm::Rollover => crate::linux::PACKET_FANOUT_ROLLOVER,
            FanoutAlgorithm::RoundRobin => crate::linux::PACKET_FANOUT_LB,
        };

        opt |= (group_id as u32) << 16;

        if defrag {
            opt |= crate::linux::PACKET_FANOUT_FLAG_DEFRAG;
        }

        if rollover {
            opt |= crate::linux::PACKET_FANOUT_FLAG_ROLLOVER;
        }

        if unsafe {
            libc::setsockopt(
                self.fd,
                libc::SOL_PACKET,
                crate::linux::PACKET_FANOUT,
                ptr::addr_of!(opt) as *const libc::c_void,
                mem::size_of::<u32>() as u32,
            ) != 0
        } {
            return Err(io::Error::last_os_error());
        }

        Ok(())
    }

    /// Sends a datagram over the socket. On success, returns the number of bytes written.
    ///
    /// This method will fail if the socket has not been bound to an [`L2Addr`] (i.e., via
    /// [`bind()`](L3Socket::bind())).
    pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
        match unsafe { libc::send(self.fd, buf.as_ptr() as *const libc::c_void, buf.len(), 0) } {
            ..=-1 => Err(io::Error::last_os_error()),
            sent => Ok(sent as usize),
        }
    }

    /// Receive a datagram from the socket.
    ///
    /// This method will fail if the socket has not been bound  to an [`L2Addr`] (i.e., via
    /// [`bind()`](L3Socket::bind())).
    pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
        match unsafe { libc::recv(self.fd, buf.as_mut_ptr() as *mut libc::c_void, buf.len(), 0) } {
            ..=-1 => Err(io::Error::last_os_error()),
            recvd => Ok(recvd as usize),
        }
    }

    /// TODO: add addr
    pub fn send_to<A: L2Addr>(&self, buf: &[u8], addr: A) -> io::Result<usize> {
        let sockaddr = addr.to_sockaddr();
        let addrlen = mem::size_of::<libc::sockaddr_ll>() as u32;

        match unsafe {
            libc::sendto(
                self.fd,
                buf.as_ptr() as *mut libc::c_void,
                buf.len(),
                0,
                ptr::addr_of!(sockaddr) as *const libc::sockaddr,
                addrlen,
            )
        } {
            ..=-1 => Err(io::Error::last_os_error()),
            recvd => Ok(recvd as usize),
        }
    }

    /// # Errors
    ///
    /// In the event that a packet is received that does not match either the address family
    /// (`AF_PACKET`) or the specified protocol type of `A`, this function will return.
    /// [`InvalidData`](io::ErrorKind::InvalidData). Note that `buf` will still be
    pub fn recv_from<A: L2Addr>(&self, buf: &mut [u8]) -> io::Result<(usize, A)> {
        let mut sockaddr = libc::sockaddr_ll {
            sll_family: 0,
            sll_protocol: 0,
            sll_ifindex: 0,
            sll_hatype: 0,
            sll_pkttype: 0,
            sll_halen: 0,
            sll_addr: [0u8; 8],
        };
        let mut sockaddr_len: libc::socklen_t = mem::size_of::<libc::sockaddr_ll>() as u32;

        let recvd = match unsafe {
            libc::recvfrom(
                self.fd,
                buf.as_mut_ptr() as *mut libc::c_void,
                buf.len(),
                0,
                ptr::addr_of_mut!(sockaddr) as *mut libc::sockaddr,
                ptr::addr_of_mut!(sockaddr_len),
            )
        } {
            ..=-1 => return Err(io::Error::last_os_error()),
            recvd => recvd as usize,
        };

        if sockaddr.sll_family != libc::AF_PACKET as u16 {
            // Cannot guarantee received sockaddr is size_of::<libc::sockaddr_ll>()
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "invalid address received (address family not AF_PACKET)",
            ));
        }

        let Ok(addr) = A::try_from(sockaddr) else {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "unexpected address received (address protocol mismatch)",
            ));
        };

        Ok((recvd, addr))
    }

    /// Sets the PACKET_VERSION socket option to TPACKET_V3.
    fn set_tpacket_v3_opt(&self) -> io::Result<()> {
        let pkt_version_3 = crate::linux::tpacket_versions::TPACKET_V3;
        if unsafe {
            libc::setsockopt(
                self.fd,
                libc::SOL_PACKET,
                crate::linux::PACKET_VERSION,
                ptr::addr_of!(pkt_version_3) as *const libc::c_void,
                mem::size_of::<crate::linux::tpacket_versions>() as u32,
            ) != 0
        } {
            return Err(io::Error::last_os_error());
        }

        Ok(())
    }

    /// Sets the PACKET_TX_RING socket option.
    fn set_tx_ring_opt(&self, config: BlockConfig) -> io::Result<()> {
        let req_tx = crate::linux::tpacket_req3 {
            tp_block_size: config.block_size(),
            tp_block_nr: config.block_cnt(),
            tp_frame_size: config.frame_size(),
            tp_frame_nr: config.frame_cnt(),
            tp_retire_blk_tov: 0,
            tp_sizeof_priv: 0,
            tp_feature_req_word: 0,
        };

        if unsafe {
            libc::setsockopt(
                self.fd,
                libc::SOL_PACKET,
                crate::linux::PACKET_TX_RING,
                ptr::addr_of!(req_tx) as *const libc::c_void,
                mem::size_of::<crate::linux::tpacket_req>() as u32,
            ) != 0
        } {
            return Err(io::Error::last_os_error());
        }

        Ok(())
    }

    /// Sets the PACKET_RX_RING socket option.
    fn set_rx_ring_opt(
        &self,
        config: BlockConfig,
        mut timeout: Option<u32>,
        private_size: Option<u32>,
    ) -> io::Result<()> {
        if timeout == Some(0) {
            timeout = Some(1); // Prevent user from accidentally selecting kernel default
        }

        let req_rx = crate::linux::tpacket_req3 {
            tp_block_size: config.block_size(),
            tp_block_nr: config.block_cnt(),
            tp_frame_size: config.frame_size(),
            tp_frame_nr: config.frame_cnt(),
            tp_retire_blk_tov: timeout.unwrap_or(0),
            tp_sizeof_priv: private_size.unwrap_or(0),
            tp_feature_req_word: 0,
        };

        if unsafe {
            libc::setsockopt(
                self.fd,
                libc::SOL_PACKET,
                crate::linux::PACKET_RX_RING,
                ptr::addr_of!(req_rx) as *const libc::c_void,
                mem::size_of::<crate::linux::tpacket_req>() as u32,
            ) != 0
        } {
            return Err(io::Error::last_os_error());
        }

        Ok(())
    }

    /// Memory-map the packet's TX/RX ring buffers to enable zero-copy packet exchange.
    ///
    /// On error, the consumed [`L3Socket`] will be closed.
    fn mmap_socket(
        &self,
        config: BlockConfig,
        combined_tx_rx: bool,
    ) -> io::Result<*mut libc::c_void> {
        let map_length = if combined_tx_rx {
            config.map_length() * 2
        } else {
            config.map_length()
        };

        let mapped = unsafe {
            libc::mmap(
                ptr::null::<*mut libc::c_void>() as *mut libc::c_void,
                map_length,
                libc::PROT_READ | libc::PROT_WRITE,
                libc::MAP_SHARED | libc::MAP_LOCKED,
                self.fd,
                0,
            )
        };

        if mapped == libc::MAP_FAILED {
            return Err(io::Error::last_os_error());
        }

        Ok(mapped)
    }

    /// Enables zero-copy packet transmission and reception for the socket.
    ///
    /// On error, the consumed `L3Socket` will be closed.
    ///
    /// NOTE: some performance issues have been noted when TX_RING sockets are used in blocking mode (see
    /// [here](https://stackoverflow.com/questions/43193889/sending-data-with-packet-mmap-and-packet-tx-ring-is-slower-than-normal-withou)).
    /// It is recommended that the socket be set as nonblocking before calling `packet_ring`.
    pub fn packet_ring(
        self,
        config: BlockConfig,
        timeout: Option<u32>,
        reserved: Option<u32>,
    ) -> io::Result<L3MappedSocket> {
        self.set_tpacket_v3_opt()?;
        self.set_rx_ring_opt(config, timeout, reserved)?;
        let mapping = self.mmap_socket(config, true)?;

        let rx_ring = unsafe {
            PacketRxRing::new(
                mapping as *mut u8,
                config,
                reserved.unwrap_or(0) as usize,
                OsiLayer::L3,
            )
        };

        let tx_ring =
            unsafe { PacketTxRing::new((mapping as *mut u8).add(config.map_length()), config) };

        // This will immediately wrap around to the first packet due to `frame_offset: None`
        let start_frame = FrameIndex {
            blocks_index: tx_ring.blocks_cnt() - 1,
            frame_offset: None,
        };

        Ok(L3MappedSocket {
            socket: self,
            rx_ring,
            next_rx: start_frame,
            tx_ring,
            last_checked_tx: start_frame,
            next_tx: start_frame,
            manual_tx_status: false,
            tx_full: false,
        })
    }

    /// Enables zero-copy packet transmission for the socket.
    ///
    /// On error, the consumed `L3Socket` will be closed.
    ///
    /// In past kernel versions, some performance issues have been noted when TX_RING sockets are
    /// used in blocking mode (see [here](https://stackoverflow.com/questions/43193889/sending-data-with-packet-mmap-and-packet-tx-ring-is-slower-than-normal-withou)).
    /// It is recommended that the socket be set as nonblocking before calling `packet_tx_ring`.
    pub fn packet_tx_ring(self, config: BlockConfig) -> io::Result<L3TxMappedSocket> {
        self.set_tpacket_v3_opt()?;
        self.set_tx_ring_opt(config)?;
        let mapping = self.mmap_socket(config, false)?;

        let tx_ring = unsafe { PacketTxRing::new(mapping as *mut u8, config) };

        // This will immediately wrap around to the first packet due to `frame_offset: None`
        let start_frame = FrameIndex {
            blocks_index: tx_ring.blocks_cnt() - 1,
            frame_offset: None,
        };

        Ok(L3TxMappedSocket {
            socket: self,
            tx_ring,
            last_checked_tx: start_frame,
            next_tx: start_frame,
            manual_tx_status: false,
            tx_full: false,
        })
    }

    /// Enables zero-copy packet reception for the socket.
    ///
    /// On error, the consumed `L3Socket` will be closed.
    pub fn packet_rx_ring(
        self,
        config: BlockConfig,
        timeout: Option<u32>,
        reserved: Option<u32>,
    ) -> io::Result<L3RxMappedSocket> {
        self.set_tpacket_v3_opt()?;
        self.set_rx_ring_opt(config, timeout, reserved)?;
        let mapping = self.mmap_socket(config, false)?;

        let rx_ring = unsafe {
            PacketRxRing::new(
                mapping as *mut u8,
                config,
                reserved.unwrap_or(0) as usize,
                OsiLayer::L3,
            )
        };

        // This will immediately wrap around to the first packet due to `frame_offset: None`
        let start_frame = FrameIndex {
            blocks_index: rx_ring.blocks_cnt() - 1,
            frame_offset: None,
        };

        Ok(L3RxMappedSocket {
            socket: self,
            rx_ring,
            next_rx: start_frame,
        })
    }
}

impl Drop for L3Socket {
    fn drop(&mut self) {
        unsafe { libc::close(self.fd) };
    }
}

impl AsRawFd for L3Socket {
    #[inline]
    fn as_raw_fd(&self) -> RawFd {
        self.fd
    }
}

/// A network-layer socket with zero-copy packet transmission and reception.
pub struct L3MappedSocket {
    socket: L3Socket,
    rx_ring: PacketRxRing,
    next_rx: FrameIndex,
    tx_ring: PacketTxRing,
    last_checked_tx: FrameIndex,
    next_tx: FrameIndex,
    manual_tx_status: bool,
    tx_full: bool,
}

impl L3MappedSocket {
    /// Bind the network-layer socket to a particular interface and begin receiving packets matching
    /// the link-layer protocol `proto`.
    pub fn bind(&self, iface: Interface, proto: L2Protocol) -> io::Result<()> {
        self.socket.bind(iface, proto)
    }

    /// Binds the device to all interfaces, enabling it to begin receiving packets matching the
    /// link-layer protocol `proto`.
    #[inline]
    pub fn bind_all(&self, proto: L2Protocol) -> io::Result<()> {
        self.socket.bind_all(proto)
    }

    /// When set, configures transmitted packets to bypass kernel's traffic control (qdisc) layer.
    ///
    /// This allows packet buffering at the qdisc layer to be avoided, which is useful for
    /// applications that intend to intentionally flood a network with traffic. Enabling this option
    /// will lead to increased packet drops in transmission when network devices are busy (as the
    /// kernel will not be buffering packets originating from the socket).
    ///
    /// This option is disabled (`false`) by default.
    pub fn set_qdisc_bypass(&self, bypass: bool) -> io::Result<()> {
        self.socket.set_qdisc_bypass(bypass)
    }

    /// Reserves `amount` padding bytes before the packet in an [`RxFrame`].
    ///
    /// Note that the actual padding bytes available in an [`RxFrame`] may be slightly more than
    /// `amount` due to alignment requirements.
    pub fn set_reserve(&self, amount: u32) -> io::Result<()> {
        if unsafe {
            libc::setsockopt(
                self.socket.fd,
                libc::SOL_PACKET,
                crate::linux::PACKET_RESERVE,
                ptr::addr_of!(amount) as *const libc::c_void,
                mem::size_of::<u32>() as u32,
            ) != 0
        } {
            return Err(io::Error::last_os_error());
        }

        Ok(())
    }

    /// Returns packet statistics about the current socket.
    ///
    /// Packet statistics include [`received`](PacketStatistics::received) and
    /// [`dropped`](PacketStatistics::dropped) packet counts; both of these counters are reset
    /// each time this method is called.
    pub fn packet_stats(&self) -> io::Result<PacketStatistics> {
        self.socket.packet_stats()
    }

    /// Returns the interface that the socket is currently sniffing on.
    #[inline]
    pub fn interface(&self) -> io::Result<Interface> {
        self.socket.interface()
    }

    /// Determines the source of timestamp information for TX_RING/RX_RING packets.
    /// If `true`, this option requests that the operating system use hardware timestamps
    /// provided by the NIC.
    ///
    /// For hardware timestamping to be employed, the socket must be bound to a specific interface.
    ///
    /// # Errors
    ///
    /// `ERANGE` - the requested packets cannot be timestamped by hardware.
    ///
    /// `EINVAL` - hardware timestamping is not supported by the network card.
    pub fn set_timestamp_method(&self, tx: TxTimestamping, rx: RxTimestamping) -> io::Result<()> {
        self.socket.set_timestamp_method(tx, rx)
    }

    /// Configures the network device the socket is currently bound to to act in promiscuous mode.
    ///
    /// A network device in promiscuous mode will capture all packets observed on a physical medium,
    /// not just those destined for it.
    pub fn set_promiscuous(&self, promisc: bool) -> io::Result<()> {
        self.socket.set_promiscuous(promisc)
    }

    /// Adds the given socket to a fanout group.
    ///
    /// A fanout group is a set of sockets that act together to process packets. Each received
    /// packet is sent to only one of the sockets in the fanout group, based on the algorithm
    /// chosen in `fan_alg`. The group is specified using a 16-bit group identifier, `group_id`.
    /// Some additional options can be set:
    ///
    /// - `defrag` causes the kernel to defragment IP packets prior to sending them to the
    /// fanout group (e.g. to ensure [`FanoutAlgorithm::Hash`] works despite fragmentation)
    /// - `rollover` causes packets to be sent to a different socket than originally decided
    /// by `fan_alg` if the original socket is backlogged with packets.
    pub fn set_packet_fanout(
        &self,
        group_id: u16,
        fan_alg: FanoutAlgorithm,
        defrag: bool,
        rollover: bool,
    ) -> io::Result<()> {
        self.socket.set_fanout(group_id, fan_alg, defrag, rollover)
    }

    /// Sets [`mapped_send()`](Self::mapped_send) results to be manually handled through repeated
    /// calls to [`tx_status()`](Self::tx_status).
    ///
    /// By default (i.e., when `manual` = `false`), the results of packet transmission are
    /// transparently handled. As a result, packets flagged as malformed by the kernel are
    /// aggregated into statistics rather than being reported back to the user on a case-by-case
    /// basis.
    ///
    /// If individual packet results are desired, setting this option to `true` modifies socket
    /// behavior such that each sent packet must have its status checked using
    /// [`tx_status()`](Self::tx_status) prior to that packet being discarded from the ring.
    #[inline]
    pub fn manual_tx_status(&mut self, manual: bool) {
        self.manual_tx_status = manual;
    }

    /// Sends a datagram over the socket. On success, returns the number of bytes written.
    ///
    /// NOTE: this method DOES NOT employ memory-mapped I/O and is functionally equivalent
    /// to [`L3Socket::send()`]. If you are looking to send a memory-mapped packet, use
    /// [`mapped_send()`](L3MappedSocket::mapped_send()) instead.
    ///
    /// This method will fail if the socket has not been bound to an [`L2Addr`] (i.e., via
    /// [`bind()`](L3MappedSocket::bind())).
    pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
        self.socket.send(buf)
    }

    /// Receive a datagram from the socket.
    ///
    /// NOTE: this method DOES NOT employ memory-mapped I/O and is functionally equivalent
    /// to [`L3Socket::recv()`]. If you are looking to receive a memory-mapped packet, use
    /// [`L3MappedSocket::mapped_recv()`] instead.
    ///
    /// This method will fail if the socket has not been bound  to an [`L2Addr`] (i.e., via
    /// [`bind()`](L3MappedSocket::bind())).
    pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
        self.socket.recv(buf)
    }

    /// Retrieves the next frame in the memory-mapped ring buffer to transmit a packet with.
    ///
    /// The returned [`TxFrame`] should have data written to it via the
    /// [`data()`](`TxFrame::data()`) method. Following this, the packet can be sent with
    /// [`send()`](`TxFrame::send()`), with the number of bytes written to `data()` specified in
    /// `packet_length`. If `send()` is not called, the packet _will not_ be sent, and subsequent
    /// calls to [`mapped_send()`](Self::mapped_send()) will return the same frame.
    pub fn mapped_send(&mut self) -> Option<TxFrame<'_>> {
        if self.tx_full {
            return None;
        }

        let (frame_variant, next_tx) = self.tx_ring.next_frame(self.next_tx);
        let TxFrameVariant::Available(frame) = frame_variant else {
            return None;
        };

        if !self.manual_tx_status {
            self.last_checked_tx = self.next_tx;
        } else if self.last_checked_tx == next_tx {
            // TX has looped around fully with manual_tx_status enabled--indicate TX ring is now full
            self.tx_full = true;
        }

        self.next_tx = next_tx;

        Some(frame)
    }

    //
    // # Examples
    //
    // ```
    // use std::{thread, time::Duration};
    // use rscap::linux::prelude::*;
    //
    // let mut sock = L3Socket::new()?.packet_tx_ring(BlockConfig::new(65536, 16, 8192)?);
    // sock.manual_tx_status(true);
    // let mut sending = 0;
    // let mut malformed = 0;
    // for _ in 0..5 {
    //     let mut tx_frame = sock.mapped_send().unwrap();
    //     tx_frame.data()[0..11].copy_from_slice(b"hello world"); // This will obviously be malformed
    //     tx_frame.send(11);
    //     sending += 1;
    // }
    //
    // while sending > 0 {
    //     match sock.tx_status() {
    //         TxFrameVariant::Available(_) => sending -= 1,
    //         TxFrameVariant::WrongFormat(pkt) => {
    //             println!("Malformed packet: {:?}", pkt.data());
    //             malformed += 1;
    //             sending -= 1;
    //         }
    //         TxFrameVariant::SendRequest | TxFrameVariant::Sending => thread::sleep(Duration::from_millis(50)),
    //     }
    // }
    //
    // assert!(malformed == 5);
    // # Ok::<(), io::Error>(())
    // ```

    /// Checks the status of previously-sent packets in the order they were sent.
    ///
    /// By default, or when `manual_tx_status` is set to `false`, this method will only return the
    /// status the last packet sent using `mapped_send()`. When `manual_tx_status()` is set to
    /// `true`, this method will return the status of each packet sent with `mapped_send()` in the
    /// order they were sent.
    ///
    /// To correctly handle TX statuses manually, one should count pending packets sent with
    /// `mapped_send()`. The status of each pending packet should then be retrieved via consecutive
    /// calls to `tx_status()`. If `tx_status()` returns [`TxFrameVariant::Available`], the packet
    /// was successfully sent and the count of packets can be decremented. If `tx_status()`
    /// returns [`TxFrameVariant::SendRequest`] or [`TxFrameVariant::Sending`], the packet is still
    /// being handled by the kernel. In this case, the count should not be decremented, as the next
    /// call to `tx_status()` will return the status of that same packet. If `tx_status()` returns
    /// [`TxFrameVariant::WrongFormat`], the kernel has rejected the packet, and the count of pending
    /// packets should be decremented. The contents of the packet can be retrieved from the
    /// [`InvalidTxFrame`](super::mapped::InvalidTxFrame) if desired.
    pub fn tx_status(&mut self) -> TxFrameVariant<'_> {
        let (frame_variant, next_tx) = self.tx_ring.next_frame(self.last_checked_tx);

        if self.manual_tx_status && (self.tx_full || self.last_checked_tx != self.next_tx) {
            // The TX ring is meant to be manually incremented _and_ is not empty
            if let TxFrameVariant::Available(_) | TxFrameVariant::WrongFormat(_) = frame_variant {
                // Increment the TX ring
                self.tx_full = false;
                self.last_checked_tx = next_tx;
            }
        }

        frame_variant
    }

    /// Retrieves the next frame in the memory-mapped ring buffer to transmit a packet with.
    ///
    /// The returned [`TxFrame`] should have data written to it via the
    /// [`data()`](`TxFrame::data()`) method. Following this, the packet can be sent with
    /// [`send()`](`TxFrame::send()`), with the number of bytes written to `data()` specified in
    /// `packet_length`. If `send()` is not called, the packet _will not_ be sent, and subsequent
    /// calls to [`mapped_send()`](Self::mapped_send()) will return the same frame.
    #[inline]
    pub fn mapped_recv(&mut self) -> Option<RxFrame<'_>> {
        let (rx_frame, next_rx) = self.rx_ring.next_frame(self.next_rx)?;
        self.next_rx = next_rx;

        Some(rx_frame)
    }
}

impl Drop for L3MappedSocket {
    fn drop(&mut self) {
        unsafe {
            libc::munmap(
                self.rx_ring.mapped_start() as *mut libc::c_void,
                self.rx_ring.mapped_size() * 2,
            );
        }
        // The L3Socket will close itself when dropped
    }
}

impl AsRawFd for L3MappedSocket {
    #[inline]
    fn as_raw_fd(&self) -> RawFd {
        self.socket.fd
    }
}

/// A network-layer socket with zero-copy packet transmission.
pub struct L3TxMappedSocket {
    socket: L3Socket,
    tx_ring: PacketTxRing,
    last_checked_tx: FrameIndex,
    next_tx: FrameIndex,
    manual_tx_status: bool,
    tx_full: bool,
}

impl L3TxMappedSocket {
    /// Bind the network-layer socket to a particular interface and begin receiving packets matching
    /// the link-layer protocol `proto`.
    #[inline]
    pub fn bind(&self, iface: Interface, proto: L2Protocol) -> io::Result<()> {
        self.socket.bind(iface, proto)
    }

    /// Binds the device to all interfaces, enabling it to begin receiving packets matching the
    /// link-layer protocol `proto`.
    #[inline]
    pub fn bind_all(&self, proto: L2Protocol) -> io::Result<()> {
        self.socket.bind_all(proto)
    }

    /// When set, configures transmitted packets to bypass kernel's traffic control (qdisc) layer.
    ///
    /// This allows packet buffering at the qdisc layer to be avoided, which is useful for
    /// applications that intend to intentionally flood a network with traffic. Enabling this option
    /// will lead to increased packet drops in transmission when network devices are busy (as the
    /// kernel will not be buffering packets originating from the socket).
    ///
    /// This option is disabled (`false`) by default.
    #[inline]
    pub fn set_qdisc_bypass(&self, bypass: bool) -> io::Result<()> {
        self.socket.set_qdisc_bypass(bypass)
    }

    /// Returns packet statistics about the current socket.
    ///
    /// Packet statistics include [`received`](PacketStatistics::received) and
    /// [`dropped`](PacketStatistics::dropped) packet counts; both of these counters are reset
    /// each time this method is called.
    pub fn packet_stats(&self) -> io::Result<PacketStatistics> {
        self.socket.packet_stats()
    }

    /// Returns the interface that the socket is currently sniffing on.
    #[inline]
    pub fn interface(&self) -> io::Result<Interface> {
        self.socket.interface()
    }

    /// Determines the source of timestamp information for TX_RING/RX_RING packets.
    /// If `true`, this option requests that the operating system use hardware timestamps
    /// provided by the NIC.
    ///
    /// For hardware timestamping to be employed, the socket must be bound to a specific interface.
    ///
    /// # Errors
    ///
    /// `ERANGE` - the requested packets cannot be timestamped by hardware.
    ///
    /// `EINVAL` - hardware timestamping is not supported by the network card.
    pub fn set_timestamp_method(&self, tx: TxTimestamping, rx: RxTimestamping) -> io::Result<()> {
        self.socket.set_timestamp_method(tx, rx)
    }

    /// Receive a datagram from the socket.
    ///
    /// NOTE: this method DOES NOT employ memory-mapped I/O and is functionally equivalent
    /// to [`L3Socket::recv()`]. If you are looking to receive a memory-mapped packet, use
    /// [`mapped_recv()`](L3MappedSocket::mapped_recv()) instead.
    ///
    /// This method will fail if the socket has not been bound  to an [`L2Addr`] (i.e., via
    /// [`bind()`](L3RxMappedSocket::bind())).
    pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
        self.socket.recv(buf)
    }

    /// Sends a datagram over the socket. On success, returns the number of bytes written.
    ///
    /// NOTE: this method DOES NOT employ memory-mapped I/O and is functionally equivalent
    /// to [`L3Socket::send()`]. If you are looking to send a memory-mapped packet, use
    /// [`mapped_send()`](L3MappedSocket::mapped_send()) instead.
    ///
    /// This method will fail if the socket has not been bound to an [`L2Addr`] (i.e., via
    /// [`bind()`](L3TxMappedSocket::bind())).
    pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
        self.socket.send(buf)
    }

    /// Retrieves the next frame in the memory-mapped ring buffer to transmit a packet with.
    ///
    /// The returned [`TxFrame`] should have data written to it via the
    /// [`data()`](`TxFrame::data()`) method. Following this, the packet can be sent with
    /// [`send()`](`TxFrame::send()`), with the number of bytes written to `data()` specified in
    /// `packet_length`. If `send()` is not called, the packet _will not_ be sent, and subsequent
    /// calls to [`mapped_send()`](Self::mapped_send()) will return the same frame.
    pub fn mapped_send(&mut self) -> Option<TxFrame<'_>> {
        if self.tx_full {
            return None;
        }

        let (frame_variant, next_tx) = self.tx_ring.next_frame(self.next_tx);
        let TxFrameVariant::Available(frame) = frame_variant else {
            return None;
        };

        if !self.manual_tx_status {
            self.last_checked_tx = self.next_tx;
        } else if self.last_checked_tx == next_tx {
            // TX has looped around fully with manual_tx_status enabled--indicate TX ring is now full
            self.tx_full = true;
        }

        self.next_tx = next_tx;

        Some(frame)
    }

    /// Sets [`mapped_send()`](Self::mapped_send()) results to be manually handled via repeated
    /// calls to [`tx_status()`](Self::tx_status()).
    ///
    /// By default (i.e., when `manual` = `false`), the results of packet transmission are
    /// transparently handled. As a result, packets flagged as malformed by the kernel are
    /// aggregated into statistics rather than being reported back to the user on a case-by-case
    /// basis.
    ///
    /// If individual packet results are desired, setting this option to `true` modifies socket
    /// behavior such that each sent packet must have its status checked using `tx_status` prior
    /// to that packet being discarded from the ring.
    pub fn set_tx_status(&mut self, manual: bool) {
        self.manual_tx_status = manual;
    }

    //
    // # Examples
    //
    // ```
    // use std::{thread, time::Duration};
    // use rscap::linux::prelude::*;
    //
    // let mut sock = L3Socket::new()?.packet_tx_ring(BlockConfig::new(65536, 16, 8192)?);
    // sock.manual_tx_status(true);
    // let mut sending = 0;
    // let mut malformed = 0;
    // for _ in 0..5 {
    //     let mut tx_frame = sock.mapped_send().unwrap();
    //     tx_frame.data()[0..11].copy_from_slice(b"hello world"); // This will obviously be malformed
    //     tx_frame.send(11);
    //     sending += 1;
    // }
    //
    // while sending > 0 {
    //     match sock.tx_status() {
    //         TxFrameVariant::Available(_) => sending -= 1,
    //         TxFrameVariant::WrongFormat(pkt) => {
    //             println!("Malformed packet: {:?}", pkt.data());
    //             malformed += 1;
    //             sending -= 1;
    //         }
    //         TxFrameVariant::SendRequest | TxFrameVariant::Sending => thread::sleep(Duration::from_millis(50)),
    //     }
    // }
    //
    // assert!(malformed == 5);
    // # Ok::<(), io::Error>(())
    // ```

    /// Checks the status of previously-sent packets in the order they were sent.
    ///
    /// By default, or when [`set_tx_status()`](Self::set_tx_status()) is set to `false`, this
    /// method will only return the status the last packet sent using `mapped_send()`. When
    /// `set_tx_status()` is set to `true`, this method will return the status of each packet sent
    /// with [`mapped_send()`](Self::mapped_send()) in the order they were sent.
    ///
    /// To correctly handle TX statuses manually, one should count pending packets sent with
    /// `mapped_send()`. The status of each pending packet should then be retrieved via consecutive
    /// calls to `tx_status()`. If `tx_status()` returns [`TxFrameVariant::Available`], the packet
    /// was successfully sent and the count of packets can be decremented. If `tx_status()`
    /// returns [`TxFrameVariant::SendRequest`] or [`TxFrameVariant::Sending`], the packet is still
    /// being handled by the kernel. In this case, the count should not be decremented, as the next
    /// call to `tx_status()` will return the status of that same packet. If `tx_status()` returns
    /// [`TxFrameVariant::WrongFormat`], the kernel has rejected the packet, and the count of
    /// pending packets should be decremented. The contents of the packet can be retrieved from the
    /// encapsulated [`InvalidTxFrame`](super::mapped::InvalidTxFrame) if desired.
    pub fn tx_status(&mut self) -> TxFrameVariant<'_> {
        let (frame_variant, next_tx) = self.tx_ring.next_frame(self.last_checked_tx);

        if self.manual_tx_status && (self.tx_full || self.last_checked_tx != self.next_tx) {
            // The TX ring is meant to be manually incremented _and_ is not empty
            if let TxFrameVariant::Available(_) | TxFrameVariant::WrongFormat(_) = frame_variant {
                // Increment the TX ring
                self.tx_full = false;
                self.last_checked_tx = next_tx;
            }
        }

        frame_variant
    }
}

impl Drop for L3TxMappedSocket {
    fn drop(&mut self) {
        unsafe {
            libc::munmap(
                self.tx_ring.mapped_start() as *mut libc::c_void,
                self.tx_ring.mapped_size(),
            );
        }
        // The L3Socket will close itself when dropped
    }
}

impl AsRawFd for L3TxMappedSocket {
    #[inline]
    fn as_raw_fd(&self) -> RawFd {
        self.socket.fd
    }
}

/// A network-layer socket with zero-copy packet reception.
pub struct L3RxMappedSocket {
    socket: L3Socket,
    rx_ring: PacketRxRing,
    next_rx: FrameIndex,
}

impl L3RxMappedSocket {
    /// Bind the network-layer socket to a particular interface and begin receiving packets matching
    /// the link-layer protocol `proto`.
    #[inline]
    pub fn bind(&self, iface: Interface, proto: L2Protocol) -> io::Result<()> {
        self.socket.bind(iface, proto)
    }

    /// Binds the network-layer socket to all interfaces, enabling it to begin receiving packets
    /// matching the link-layer protocol `proto`.
    #[inline]
    pub fn bind_all(&self, proto: L2Protocol) -> io::Result<()> {
        self.socket.bind_all(proto)
    }

    /// When set, configures transmitted packets to bypass kernel's traffic control (qdisc) layer.
    ///
    /// This allows packet buffering at the qdisc layer to be avoided, which is useful for
    /// applications that intend to intentionally flood a network with traffic. Enabling this option
    /// will lead to increased packet drops in transmission when network devices are busy (as the
    /// kernel will not be buffering packets originating from the socket).
    ///
    /// This option is disabled (`false`) by default.
    pub fn set_qdisc_bypass(&self, bypass: bool) -> io::Result<()> {
        self.socket.set_qdisc_bypass(bypass)
    }

    /// Returns packet statistics about the current socket.
    ///
    /// Packet statistics include [`received`](PacketStatistics::received) and
    /// [`dropped`](PacketStatistics::dropped) packet counts; both of these counters are reset
    /// each time this method is called.
    pub fn packet_stats(&self) -> io::Result<PacketStatistics> {
        self.socket.packet_stats()
    }

    /// Reserves `amount` padding bytes before the packet in an [`RxFrame`].
    ///
    /// Note that the actual padding bytes available in an [`RxFrame`] may be slightly more than
    /// `amount` due to alignment requirements.
    pub fn set_reserve(&self, amount: u32) -> io::Result<()> {
        if unsafe {
            libc::setsockopt(
                self.socket.fd,
                libc::SOL_PACKET,
                crate::linux::PACKET_RESERVE,
                ptr::addr_of!(amount) as *const libc::c_void,
                mem::size_of::<u32>() as u32,
            ) != 0
        } {
            return Err(io::Error::last_os_error());
        }

        Ok(())
    }

    /// Returns the interface that the socket is currently sniffing on.
    #[inline]
    pub fn interface(&self) -> io::Result<Interface> {
        self.socket.interface()
    }

    /// Determines the source of timestamp information for TX_RING/RX_RING packets.
    /// If `true`, this option requests that the operating system use hardware timestamps
    /// provided by the NIC.
    ///
    /// For hardware timestamping to be employed, the socket must be bound to a specific interface.
    ///
    /// # Errors
    ///
    /// `ERANGE` - the requested packets cannot be timestamped by hardware.
    ///
    /// `EINVAL` - hardware timestamping is not supported by the network card.
    pub fn set_timestamp_method(&self, tx: TxTimestamping, rx: RxTimestamping) -> io::Result<()> {
        self.socket.set_timestamp_method(tx, rx)
    }

    /// Adds the given socket to a fanout group.
    ///
    /// A fanout group is a set of sockets that act together to process packets. Each received
    /// packet is sent to only one of the sockets in the fanout group, based on the algorithm
    /// chosen in `fan_alg`. The group is specified using a 16-bit group identifier, `group_id`.
    /// Some additional options can be set:
    ///
    /// - `defrag` causes the kernel to defragment IP packets prior to sending them to the
    /// fanout group (e.g. to ensure [`FanoutAlgorithm::Hash`] works despite fragmentation)
    /// - `rollover` causes packets to be sent to a different socket than originally decided
    /// by `fan_alg` if the original socket is backlogged with packets.
    pub fn set_packet_fanout(
        &self,
        group_id: u16,
        fan_alg: FanoutAlgorithm,
        defrag: bool,
        rollover: bool,
    ) -> io::Result<()> {
        self.socket.set_fanout(group_id, fan_alg, defrag, rollover)
    }

    /// Sends a datagram over the socket. On success, returns the number of bytes written.
    ///
    /// NOTE: this method DOES NOT employ memory-mapped I/O and is functionally equivalent
    /// to [`L3Socket::send()`]. If you are looking to send a memory-mapped packet, use
    /// [`L3MappedSocket::mapped_send()`] instead.
    ///
    /// This method will fail if the socket has not been bound to an [`L2Addr`] (i.e., via
    /// [`bind()`](L3TxMappedSocket::bind())).
    pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
        self.socket.send(buf)
    }

    /// Receive a datagram from the socket.
    ///
    /// NOTE: this method DOES NOT employ memory-mapped I/O and is functionally equivalent
    /// to [`L3Socket::recv()`]. If you are looking to receive a memory-mapped packet, use
    /// [`mapped_recv()`](L3MappedSocket::mapped_recv()) instead.
    ///
    /// This method will fail if the socket has not been bound  to an [`L2Addr`] (i.e., via
    /// [`bind()`](L3RxMappedSocket::bind())).
    pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
        self.socket.recv(buf)
    }

    /// Retrieves the next frame in the memory-mapped ring buffer to receive a packet from.
    ///
    /// The returned [`RxFrame`] contains packet data that may be modified in-place if desired.
    pub fn mapped_recv(&mut self) -> Option<RxFrame<'_>> {
        let (rx_frame, next_rx) = self.rx_ring.next_frame(self.next_rx)?;
        self.next_rx = next_rx;

        Some(rx_frame)
    }
}

impl Drop for L3RxMappedSocket {
    fn drop(&mut self) {
        unsafe {
            libc::munmap(
                self.rx_ring.mapped_start() as *mut libc::c_void,
                self.rx_ring.mapped_size(),
            );
        }
        // The L3Socket will close itself when dropped
    }
}

impl AsRawFd for L3RxMappedSocket {
    #[inline]
    fn as_raw_fd(&self) -> RawFd {
        self.socket.fd
    }
}