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
extern crate libc;
extern crate ipnetwork;
extern crate pnet_base;
extern crate pnet_sys;
use std::io;
use std::option::Option;
use std::time::Duration;
use ipnetwork::IpNetwork;
pub use pnet_base::{MacAddr, ParseMacAddrErr};
mod bindings;
#[cfg(windows)]
#[path = "winpcap.rs"]
mod backend;
#[cfg(windows)]
pub mod winpcap;
#[cfg(all(not(feature = "netmap"),
target_os = "linux"
)
)]
#[path = "linux.rs"]
mod backend;
#[cfg(target_os = "linux")]
pub mod linux;
#[cfg(all(not(feature = "netmap"),
any(target_os = "freebsd",
target_os = "openbsd",
target_os = "macos")
)
)]
#[path = "bpf.rs"]
mod backend;
#[cfg(any(target_os = "freebsd", target_os = "macos"))]
pub mod bpf;
#[cfg(feature = "netmap")]
#[path = "netmap.rs"]
mod backend;
#[cfg(feature = "netmap")]
pub mod netmap;
#[cfg(feature = "pcap")]
pub mod pcap;
pub mod dummy;
pub type EtherType = u16;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum ChannelType {
Layer2,
Layer3(EtherType),
}
pub enum Channel {
Ethernet(Box<DataLinkSender>, Box<DataLinkReceiver>),
PleaseIncludeACatchAllVariantWhenMatchingOnThisEnum,
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Config {
pub write_buffer_size: usize,
pub read_buffer_size: usize,
pub read_timeout: Option<Duration>,
pub write_timeout: Option<Duration>,
pub channel_type: ChannelType,
pub bpf_fd_attempts: usize,
}
impl Default for Config {
fn default() -> Config {
Config {
write_buffer_size: 4096,
read_buffer_size: 4096,
channel_type: ChannelType::Layer2,
bpf_fd_attempts: 1000,
read_timeout: None,
write_timeout: None,
}
}
}
#[inline]
pub fn channel(network_interface: &NetworkInterface, configuration: Config) -> io::Result<Channel> {
backend::channel(network_interface, (&configuration).into())
}
pub trait DataLinkSender: Send {
#[inline]
fn build_and_send(&mut self,
num_packets: usize,
packet_size: usize,
func: &mut FnMut(&mut [u8]))
-> Option<io::Result<()>>;
#[inline]
fn send_to(&mut self,
packet: &[u8],
dst: Option<NetworkInterface>)
-> Option<io::Result<()>>;
}
pub trait DataLinkReceiver: Send {
#[inline]
fn next(&mut self) -> io::Result<&[u8]>;
}
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub struct NetworkInterface {
pub name: String,
pub index: u32,
pub mac: Option<MacAddr>,
pub ips: Vec<IpNetwork>,
pub flags: u32,
}
impl NetworkInterface {
pub fn mac_address(&self) -> MacAddr {
self.mac.unwrap()
}
pub fn is_up(&self) -> bool {
self.flags & (pnet_sys::IFF_UP as u32) != 0
}
pub fn is_broadcast(&self) -> bool {
self.flags & (pnet_sys::IFF_BROADCAST as u32) != 0
}
pub fn is_loopback(&self) -> bool {
self.flags & (pnet_sys::IFF_LOOPBACK as u32) != 0
}
pub fn is_point_to_point(&self) -> bool {
self.flags & (pnet_sys::IFF_POINTOPOINT as u32) != 0
}
pub fn is_multicast(&self) -> bool {
self.flags & (pnet_sys::IFF_MULTICAST as u32) != 0
}
}
impl ::std::fmt::Display for NetworkInterface {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
const FLAGS: [&'static str; 5] = ["UP", "BROADCAST", "LOOPBACK", "POINTOPOINT", "MULTICAST"];
let flags = if self.flags > 0 {
let rets = [
self.is_up(),
self.is_broadcast(),
self.is_loopback(),
self.is_point_to_point(),
self.is_multicast()
];
format!("{:X}<{}>", self.flags,
rets.iter()
.zip(FLAGS.iter())
.filter(|&(ret, _)| ret == &true)
.map(|(_, name)| name.to_string())
.collect::<Vec<String>>()
.join(","))
} else {
format!("{:X}", self.flags)
};
let mac = self.mac.map(|mac| mac.to_string())
.unwrap_or("N/A".to_owned());
let ips = if self.ips.len() > 0 {
format!("\n{}",
self.ips.iter()
.map(|ip| {
if ip.is_ipv4(){
format!(" inet: {}", ip)
} else {
format!(" inet6: {}", ip)
}
})
.collect::<Vec<String>>()
.join("\n"))
} else {
"".to_string()
};
write!(f, "{}: flags={}
index: {}
ether: {}{}", self.name, flags, self.index, mac, ips)
}
}
pub fn interfaces() -> Vec<NetworkInterface> {
backend::interfaces()
}