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
//! `AF_PACKET` sockets: attach to an existing network interface.
//!
//! This is the plainest way to put a real NIC into a topology. Unlike the
//! `tuntap` feature it does not create a new interface — it binds to one that
//! already exists — and unlike the `afxdp` feature it needs no eBPF, no driver
//! support and no special interface configuration. It is the slowest of the
//! three and the one most likely to just work.
//!
//! (Those modules are named without links here because either may be compiled
//! out while this one is not.)
//!
//! ```no_run
//! use pktkit::afpacket::{Config, Socket};
//! use pktkit::{Frame, L2Device};
//! use std::sync::Arc;
//!
//! # fn main() -> std::io::Result<()> {
//! let dev = Socket::open(Config {
//! interface: "eth0".into(),
//! promiscuous: true,
//! ..Default::default()
//! })?;
//!
//! dev.set_handler(Arc::new(|f: &Frame| {
//! println!("{:?} -> {:?}", f.src_mac(), f.dst_mac());
//! Ok(())
//! }));
//! # Ok(())
//! # }
//! ```
//!
//! # Privileges
//!
//! Opening an `AF_PACKET` socket needs `CAP_NET_RAW`. Without it, [`Socket::open`]
//! fails with `PermissionDenied`.
//!
//! # What you will see
//!
//! A bound socket receives every frame the interface accepts, including traffic
//! for the host itself, and — with [`Config::promiscuous`] set — traffic
//! addressed to other stations. Frames the host *sends* are also delivered back
//! unless [`Config::inbound_only`] is set. Frames written with
//! [`send`](L2Device::send) go out the interface as-is, so the Ethernet header
//! is yours to fill in.
//!
//! # Platform support
//!
//! Linux only. On other platforms the type is still present so cross-platform
//! code compiles, but [`Socket::open`] returns `ErrorKind::Unsupported`.
pub use Socket;
pub use Socket;
use Duration;
/// How to open an [`Socket`].
use crateL2Device;