crafter 0.3.0

Packet-level network interaction for Rust tools and agents.
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
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

use crate::MacAddr;
use pnet_datalink as datalink;

use super::error::{NetError, Result};

/// One IP address assigned to a network interface.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct InterfaceAddress {
    address: IpAddr,
    prefix_len: u8,
}

impl InterfaceAddress {
    /// Create an interface address with a prefix length.
    pub fn new(address: IpAddr, prefix_len: u8) -> Self {
        let max_prefix = match address {
            IpAddr::V4(_) => 32,
            IpAddr::V6(_) => 128,
        };
        Self {
            address,
            prefix_len: prefix_len.min(max_prefix),
        }
    }

    /// The host address.
    pub const fn address(&self) -> IpAddr {
        self.address
    }

    /// The network prefix length.
    pub const fn prefix_len(&self) -> u8 {
        self.prefix_len
    }

    /// Return true when this is an IPv4 address.
    pub const fn is_ipv4(&self) -> bool {
        matches!(self.address, IpAddr::V4(_))
    }

    /// Return true when this is an IPv6 address.
    pub const fn is_ipv6(&self) -> bool {
        matches!(self.address, IpAddr::V6(_))
    }

    /// Return true when `candidate` belongs to this address prefix.
    pub fn contains(&self, candidate: IpAddr) -> bool {
        match (self.address, candidate) {
            (IpAddr::V4(base), IpAddr::V4(candidate)) => {
                ipv4_in_prefix(candidate, base, self.prefix_len)
            }
            (IpAddr::V6(base), IpAddr::V6(candidate)) => {
                ipv6_in_prefix(candidate, base, self.prefix_len)
            }
            _ => false,
        }
    }
}

/// Stable interface snapshot used by generated tools and tests.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InterfaceInfo {
    name: String,
    description: String,
    index: u32,
    mac: Option<MacAddr>,
    addresses: Vec<InterfaceAddress>,
    flags: u64,
    up: bool,
    loopback: bool,
    running: bool,
}

impl InterfaceInfo {
    /// Create a mockable interface snapshot.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            description: String::new(),
            index: 0,
            mac: None,
            addresses: Vec::new(),
            flags: 0,
            up: false,
            loopback: false,
            running: false,
        }
    }

    /// Snapshot a pnet interface into the stable public type.
    pub fn from_pnet(interface: &datalink::NetworkInterface) -> Self {
        let mac = interface.mac.map(|mac| MacAddr::new(mac.octets()));
        let addresses = interface
            .ips
            .iter()
            .map(|network| InterfaceAddress::new(network.ip(), network.prefix()))
            .collect();

        Self {
            name: interface.name.clone(),
            description: interface.description.clone(),
            index: interface.index,
            mac,
            addresses,
            flags: interface.flags as u64,
            up: interface.is_up(),
            loopback: interface.is_loopback(),
            running: interface_is_running(interface),
        }
    }

    /// Set a human-readable description.
    pub fn description(mut self, description: impl Into<String>) -> Self {
        self.description = description.into();
        self
    }

    /// Set the platform interface index.
    pub const fn index(mut self, index: u32) -> Self {
        self.index = index;
        self
    }

    /// Set the MAC address.
    pub fn mac(mut self, mac: impl Into<MacAddr>) -> Self {
        self.mac = Some(mac.into());
        self
    }

    /// Add an interface address.
    pub fn address(mut self, address: IpAddr, prefix_len: u8) -> Self {
        self.addresses
            .push(InterfaceAddress::new(address, prefix_len));
        self
    }

    /// Add an IPv4 address.
    pub fn ipv4(self, address: Ipv4Addr, prefix_len: u8) -> Self {
        self.address(IpAddr::V4(address), prefix_len)
    }

    /// Add an IPv6 address.
    pub fn ipv6(self, address: Ipv6Addr, prefix_len: u8) -> Self {
        self.address(IpAddr::V6(address), prefix_len)
    }

    /// Set whether the interface is up.
    pub const fn up(mut self, up: bool) -> Self {
        self.up = up;
        self
    }

    /// Set whether the interface is loopback.
    pub const fn loopback(mut self, loopback: bool) -> Self {
        self.loopback = loopback;
        self
    }

    /// Set whether the interface is running.
    pub const fn running(mut self, running: bool) -> Self {
        self.running = running;
        self
    }

    /// Interface name.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Interface description.
    pub fn description_value(&self) -> &str {
        &self.description
    }

    /// Platform interface index.
    pub const fn index_value(&self) -> u32 {
        self.index
    }

    /// Platform flags as a stable unsigned value.
    pub const fn flags(&self) -> u64 {
        self.flags
    }

    /// Interface MAC address.
    pub const fn mac_address(&self) -> Option<MacAddr> {
        self.mac
    }

    /// Assigned interface addresses.
    pub fn addresses(&self) -> &[InterfaceAddress] {
        &self.addresses
    }

    /// IPv4 addresses assigned to the interface.
    pub fn ipv4_addresses(&self) -> Vec<Ipv4Addr> {
        self.addresses
            .iter()
            .filter_map(|address| match address.address() {
                IpAddr::V4(ip) => Some(ip),
                IpAddr::V6(_) => None,
            })
            .collect()
    }

    /// IPv6 addresses assigned to the interface.
    pub fn ipv6_addresses(&self) -> Vec<Ipv6Addr> {
        self.addresses
            .iter()
            .filter_map(|address| match address.address() {
                IpAddr::V4(_) => None,
                IpAddr::V6(ip) => Some(ip),
            })
            .collect()
    }

    /// First IPv4 address, if present.
    pub fn first_ipv4(&self) -> Option<Ipv4Addr> {
        self.ipv4_addresses().into_iter().next()
    }

    /// First IPv6 address, optionally skipping link-local addresses.
    pub fn first_ipv6(&self, include_link_local: bool) -> Option<Ipv6Addr> {
        self.ipv6_addresses()
            .into_iter()
            .find(|addr| include_link_local || !is_ipv6_link_local(*addr))
    }

    /// Return true when the interface is reported up.
    pub const fn is_up(&self) -> bool {
        self.up
    }

    /// Return true when the interface is loopback.
    pub const fn is_loopback(&self) -> bool {
        self.loopback
    }

    /// Return true when the platform reports the interface as running.
    pub const fn is_running(&self) -> bool {
        self.running
    }

    /// Return true when this is a useful default interface candidate.
    pub fn is_default_candidate(&self) -> bool {
        self.is_up()
            && !self.is_loopback()
            && (self.first_ipv4().is_some() || self.first_ipv6(true).is_some())
    }

    /// Return true when any assigned address contains `destination`.
    pub fn contains_destination(&self, destination: IpAddr) -> bool {
        self.addresses
            .iter()
            .any(|address| address.contains(destination))
    }
}

/// Parsed IPv4 target range compatible with libcrafter's wildcard examples.
pub fn interfaces() -> Vec<InterfaceInfo> {
    datalink::interfaces()
        .iter()
        .map(InterfaceInfo::from_pnet)
        .collect()
}

/// Find an interface by exact name.
pub fn find_interface(name: impl AsRef<str>) -> Result<InterfaceInfo> {
    find_interface_in(name, &interfaces())
}

/// Find an interface in a supplied interface table.
pub fn find_interface_in(name: impl AsRef<str>, table: &[InterfaceInfo]) -> Result<InterfaceInfo> {
    let name = name.as_ref();
    validate_interface_name(name)?;
    table
        .iter()
        .find(|interface| interface.name() == name)
        .cloned()
        .ok_or_else(|| NetError::InterfaceNotFound {
            name: name.to_string(),
        })
}

/// Select a likely default interface from the local interface table.
pub fn default_interface() -> Result<InterfaceInfo> {
    default_interface_in(&interfaces())
}

/// Select a likely default interface from a supplied table.
pub fn default_interface_in(table: &[InterfaceInfo]) -> Result<InterfaceInfo> {
    table
        .iter()
        .find(|interface| interface.is_default_candidate() && interface.is_running())
        .or_else(|| {
            table
                .iter()
                .find(|interface| interface.is_default_candidate())
        })
        .cloned()
        .ok_or(NetError::NoDefaultInterface)
}

/// Return the likely default interface name.
pub fn default_interface_name() -> Result<String> {
    Ok(default_interface()?.name().to_string())
}

/// Select an interface that can directly reach `destination`, falling back to default.
pub fn interface_for(destination: IpAddr) -> Result<InterfaceInfo> {
    interface_for_in(destination, &interfaces())
}

/// Select an interface for `destination` from a supplied table.
pub fn interface_for_in(destination: IpAddr, table: &[InterfaceInfo]) -> Result<InterfaceInfo> {
    table
        .iter()
        .find(|interface| {
            interface.is_up()
                && !interface.is_loopback()
                && interface.contains_destination(destination)
        })
        .cloned()
        .or_else(|| default_interface_in(table).ok())
        .ok_or(NetError::NoDefaultInterface)
}

/// Get the selected interface MAC address.
pub fn get_my_mac(interface: impl AsRef<str>) -> Result<MacAddr> {
    get_my_mac_in(interface, &interfaces())
}

/// Get the selected interface MAC address from a supplied table.
pub fn get_my_mac_in(interface: impl AsRef<str>, table: &[InterfaceInfo]) -> Result<MacAddr> {
    let interface = select_interface(interface.as_ref(), table)?;
    interface
        .mac_address()
        .ok_or_else(|| NetError::InterfaceMacNotFound {
            name: interface.name().to_string(),
        })
}

/// Get the selected interface IPv4 address.
pub fn get_my_ip(interface: impl AsRef<str>) -> Result<Ipv4Addr> {
    get_my_ip_in(interface, &interfaces())
}

/// Get the selected interface IPv4 address from a supplied table.
pub fn get_my_ip_in(interface: impl AsRef<str>, table: &[InterfaceInfo]) -> Result<Ipv4Addr> {
    let interface = select_interface(interface.as_ref(), table)?;
    interface
        .first_ipv4()
        .ok_or_else(|| NetError::InterfaceAddressNotFound {
            name: interface.name().to_string(),
            family: "ipv4",
        })
}

/// Get the selected interface IPv6 address.
pub fn get_my_ipv6(interface: impl AsRef<str>, include_link_local: bool) -> Result<Ipv6Addr> {
    get_my_ipv6_in(interface, include_link_local, &interfaces())
}

/// Get the selected interface IPv6 address from a supplied table.
pub fn get_my_ipv6_in(
    interface: impl AsRef<str>,
    include_link_local: bool,
    table: &[InterfaceInfo],
) -> Result<Ipv6Addr> {
    let interface = select_interface(interface.as_ref(), table)?;
    interface
        .first_ipv6(include_link_local)
        .ok_or_else(|| NetError::InterfaceAddressNotFound {
            name: interface.name().to_string(),
            family: "ipv6",
        })
}

fn select_interface(name: &str, table: &[InterfaceInfo]) -> Result<InterfaceInfo> {
    if name.trim().is_empty() {
        default_interface_in(table)
    } else {
        find_interface_in(name, table)
    }
}

fn validate_interface_name(name: &str) -> Result<()> {
    if name.trim().is_empty() {
        return Err(NetError::InvalidInterfaceName {
            name: name.to_string(),
            reason: "interface name must not be empty",
        });
    }
    if name.as_bytes().contains(&0) {
        return Err(NetError::InvalidInterfaceName {
            name: name.to_string(),
            reason: "interface name must not contain NUL bytes",
        });
    }
    Ok(())
}

fn is_ipv6_link_local(address: Ipv6Addr) -> bool {
    (address.segments()[0] & 0xffc0) == 0xfe80
}

fn ipv4_in_prefix(candidate: Ipv4Addr, base: Ipv4Addr, prefix_len: u8) -> bool {
    let mask = ipv4_mask(prefix_len.min(32));
    (u32::from(candidate) & mask) == (u32::from(base) & mask)
}

fn ipv4_mask(prefix_len: u8) -> u32 {
    if prefix_len == 0 {
        0
    } else {
        u32::MAX << (32 - prefix_len)
    }
}

fn ipv6_in_prefix(candidate: Ipv6Addr, base: Ipv6Addr, prefix_len: u8) -> bool {
    let prefix_len = prefix_len.min(128);
    let mask = if prefix_len == 0 {
        0
    } else {
        u128::MAX << (128 - prefix_len)
    };
    let candidate = u128::from_be_bytes(candidate.octets());
    let base = u128::from_be_bytes(base.octets());
    (candidate & mask) == (base & mask)
}

#[cfg(unix)]
fn interface_is_running(interface: &datalink::NetworkInterface) -> bool {
    interface.is_running()
}

#[cfg(not(unix))]
fn interface_is_running(interface: &datalink::NetworkInterface) -> bool {
    interface.is_up()
}