net-tools-rs 0.1.2

Rust implementation of net-tools (Linux networking base tools)
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
//! Functions for manipulating arpreq structures

use crate::{NetToolsError, Result};
use std::mem;
use std::net::Ipv4Addr;

/// Parsed options for setting an ARP entry
#[derive(Debug)]
pub(super) struct ArpSetOptions {
    pub host: String,
    pub mac: String,
    pub use_device: bool,
    pub device: Option<String>,
    pub netmask: Option<String>,
    pub flags: libc::c_int,
}

/// Parse command-line arguments for arp -s
pub(super) fn parse_set_args(
    args: &[String],
    use_device: bool,
    initial_device: Option<String>,
) -> Result<ArpSetOptions> {
    if args.is_empty() {
        return Err(NetToolsError::Other("arp: need host name".to_string()));
    }
    if args.len() < 2 && !use_device {
        return Err(NetToolsError::Other(
            "arp: need hardware address".to_string(),
        ));
    }

    let host = args[0].clone();
    let mut flags = libc::ATF_PERM | libc::ATF_COM;
    let mut device = initial_device;
    let mut netmask: Option<String> = None;
    let mac: String;

    // Get MAC address (either from argument or from device)
    if use_device {
        if args.len() < 2 {
            return Err(NetToolsError::Other("arp: need device name".to_string()));
        }
        device = Some(args[1].clone());
        mac = String::new(); // Will be filled from device
    } else {
        mac = args[1].clone();
    }

    // Parsing additional arguments
    // Start with index 2 because the first two arguments were already handled
    let mut arg_idx = 2;
    while arg_idx < args.len() {
        match args[arg_idx].as_str() {
            "pub" => flags |= libc::ATF_PUBL,
            "priv" => flags &= !libc::ATF_PUBL,
            "temp" => flags &= !libc::ATF_PERM,
            "trail" => flags |= libc::ATF_USETRAILERS,
            "dontpub" => flags |= libc::ATF_DONTPUB,
            "auto" => flags |= libc::ATF_MAGIC,
            "netmask" => {
                arg_idx += 1;
                if arg_idx >= args.len() {
                    return Err(NetToolsError::Other("arp: need netmask value".to_string()));
                }
                netmask = Some(args[arg_idx].clone());
            }
            "dev" => {
                arg_idx += 1;
                if arg_idx >= args.len() {
                    return Err(NetToolsError::Other("arp: need device name".to_string()));
                }
                device = Some(args[arg_idx].clone());
            }
            _ => {
                return Err(NetToolsError::Other(format!(
                    "arp: invalid argument: {}",
                    args[arg_idx]
                )));
            }
        }
        arg_idx += 1;
    }

    Ok(ArpSetOptions {
        host,
        mac,
        use_device,
        device,
        netmask,
        flags,
    })
}

/// Set IP address in arpreq structure
pub(super) fn set_ip(req: &mut libc::arpreq, ip: &str) -> Result<()> {
    let ip_addr: Ipv4Addr = ip
        .parse()
        .map_err(|_| NetToolsError::Other(format!("arp: invalid IP address: {}", ip)))?;

    // SAFETY: Casting to sockaddr_in to set IP address
    let sa = unsafe { &mut *(&mut req.arp_pa as *mut libc::sockaddr as *mut libc::sockaddr_in) };
    sa.sin_family = libc::AF_INET as libc::sa_family_t;
    sa.sin_addr.s_addr = u32::from(ip_addr).to_be();

    Ok(())
}

/// Set MAC address in arpreq from a device interface
pub(super) fn set_mac_from_device(
    sockfd: libc::c_int,
    req: &mut libc::arpreq,
    device: &str,
) -> Result<()> {
    // SAFETY: Initialize ifreq structure
    let mut ifr: libc::ifreq = unsafe { mem::zeroed() };
    let dev_bytes = device.as_bytes();
    if dev_bytes.len() >= libc::IFNAMSIZ {
        return Err(NetToolsError::Other(format!(
            "arp: device name too long: {}",
            device
        )));
    }

    // SAFETY: Copy device name to ifreq.ifr_name
    // We've already verified that dev_bytes.len() < IFNAMSIZ, so this won't overflow
    // ifr_name is properly initialized (zeroed) and the pointers are valid
    unsafe {
        std::ptr::copy_nonoverlapping(
            dev_bytes.as_ptr() as *const libc::c_char,
            ifr.ifr_name.as_mut_ptr(),
            dev_bytes.len(),
        );
    }

    // SAFETY: Get hardware address from interface
    let result = unsafe { libc::ioctl(sockfd, libc::SIOCGIFHWADDR as _, &mut ifr) };
    if result < 0 {
        return Err(NetToolsError::Other(format!(
            "arp: cant get HW-Address for `{}': {}.",
            device,
            std::io::Error::last_os_error()
        )));
    }

    // SAFETY: Copy hardware address from ifreq to arpreq
    // Both structures are properly initialized and the union access is valid
    unsafe {
        req.arp_ha = ifr.ifr_ifru.ifru_hwaddr;
    }

    Ok(())
}

/// Set MAC address in arpreq from a MAC address string
pub(super) fn set_mac_from_string(req: &mut libc::arpreq, mac: &str) -> Result<()> {
    let mac_bytes = parse_mac_address(mac)?;

    // SAFETY: Set MAC address in arpreq structure
    let ha = unsafe { &mut *(&mut req.arp_ha as *mut libc::sockaddr) };
    ha.sa_family = libc::ARPHRD_ETHER as libc::sa_family_t;
    // SAFETY: Copy 6 bytes of MAC address to sockaddr sa_data
    // mac_bytes is guaranteed to be 6 bytes, sa_data has at least 14 bytes
    unsafe {
        std::ptr::copy_nonoverlapping(mac_bytes.as_ptr(), ha.sa_data.as_mut_ptr() as *mut u8, 6);
    }

    Ok(())
}

/// Set device name in arpreq structure
pub(super) fn set_device(req: &mut libc::arpreq, device: &str) -> Result<()> {
    let dev_bytes = device.as_bytes();
    if dev_bytes.len() >= libc::IFNAMSIZ {
        return Err(NetToolsError::Other(format!(
            "arp: device name too long: {}",
            device
        )));
    }
    // SAFETY: Casting byte slice to c_char slice for copy
    // Both types are single-byte and the length is preserved
    req.arp_dev[..dev_bytes.len()]
        .copy_from_slice(unsafe { &*(dev_bytes as *const [u8] as *const [libc::c_char]) });
    Ok(())
}

/// Set netmask in arpreq structure
pub(super) fn set_netmask(
    req: &mut libc::arpreq,
    netmask: &str,
    flags: &mut libc::c_int,
) -> Result<()> {
    let mask_addr: Ipv4Addr = netmask
        .parse()
        .map_err(|_| NetToolsError::Other(format!("arp: invalid netmask: {}", netmask)))?;

    // SAFETY: Casting to sockaddr_in to set netmask
    let mask_sa =
        unsafe { &mut *(&mut req.arp_netmask as *mut libc::sockaddr as *mut libc::sockaddr_in) };
    mask_sa.sin_family = libc::AF_INET as libc::sa_family_t;
    mask_sa.sin_addr.s_addr = u32::from(mask_addr).to_be();
    *flags |= libc::ATF_NETMASK;

    Ok(())
}

/// Parse a MAC address string into bytes
fn parse_mac_address(mac: &str) -> Result<[u8; 6]> {
    let parts: Vec<&str> = if mac.contains(':') {
        mac.split(':').collect()
    } else if mac.contains('-') {
        mac.split('-').collect()
    } else {
        return Err(NetToolsError::Other(
            "arp: invalid hardware address".to_string(),
        ));
    };

    if parts.len() != 6 {
        return Err(NetToolsError::Other(
            "arp: invalid hardware address".to_string(),
        ));
    }

    let mut bytes = [0u8; 6];
    for (i, part) in parts.iter().enumerate() {
        bytes[i] = u8::from_str_radix(part, 16)
            .map_err(|_| NetToolsError::Other("arp: invalid hardware address".to_string()))?;
    }

    Ok(bytes)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_mac_address_colon() {
        let mac = parse_mac_address("aa:bb:cc:dd:ee:ff").unwrap();
        assert_eq!(mac, [0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);

        let mac = parse_mac_address("00:11:22:33:44:55").unwrap();
        assert_eq!(mac, [0x00, 0x11, 0x22, 0x33, 0x44, 0x55]);
    }

    #[test]
    fn test_parse_mac_address_dash() {
        let mac = parse_mac_address("aa-bb-cc-dd-ee-ff").unwrap();
        assert_eq!(mac, [0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
    }

    #[test]
    fn test_parse_mac_address_invalid_format() {
        assert!(parse_mac_address("aabbccddeeff").is_err());
        assert!(parse_mac_address("aa:bb:cc:dd:ee").is_err());
        assert!(parse_mac_address("aa:bb:cc:dd:ee:ff:00").is_err());
        assert!(parse_mac_address("gg:bb:cc:dd:ee:ff").is_err());
        assert!(parse_mac_address("").is_err());
    }

    #[test]
    fn test_parse_set_args_basic() {
        let args = vec!["192.168.1.1".to_string(), "aa:bb:cc:dd:ee:ff".to_string()];
        let opts = parse_set_args(&args, false, None).unwrap();
        assert_eq!(opts.host, "192.168.1.1");
        assert_eq!(opts.mac, "aa:bb:cc:dd:ee:ff");
        assert!(!opts.use_device);
        assert_eq!(opts.device, None);
        assert_eq!(opts.netmask, None);
        assert_eq!(opts.flags, libc::ATF_PERM | libc::ATF_COM);
    }

    #[test]
    fn test_parse_set_args_with_flags() {
        let args = vec![
            "192.168.1.1".to_string(),
            "aa:bb:cc:dd:ee:ff".to_string(),
            "pub".to_string(),
            "temp".to_string(),
        ];
        let opts = parse_set_args(&args, false, None).unwrap();
        assert_eq!(opts.host, "192.168.1.1");
        assert_eq!(opts.flags & libc::ATF_PUBL, libc::ATF_PUBL);
        assert_eq!(opts.flags & libc::ATF_PERM, 0); // temp removes PERM
    }

    #[test]
    fn test_parse_set_args_with_device() {
        let args = vec![
            "192.168.1.1".to_string(),
            "aa:bb:cc:dd:ee:ff".to_string(),
            "dev".to_string(),
            "eth0".to_string(),
        ];
        let opts = parse_set_args(&args, false, None).unwrap();
        assert_eq!(opts.device, Some("eth0".to_string()));
    }

    #[test]
    fn test_parse_set_args_with_netmask() {
        let args = vec![
            "192.168.1.1".to_string(),
            "aa:bb:cc:dd:ee:ff".to_string(),
            "netmask".to_string(),
            "255.255.255.0".to_string(),
        ];
        let opts = parse_set_args(&args, false, None).unwrap();
        assert_eq!(opts.netmask, Some("255.255.255.0".to_string()));
    }

    #[test]
    fn test_parse_set_args_use_device() {
        let args = vec!["192.168.1.1".to_string(), "eth0".to_string()];
        let opts = parse_set_args(&args, true, None).unwrap();
        assert_eq!(opts.host, "192.168.1.1");
        assert_eq!(opts.device, Some("eth0".to_string()));
        assert!(opts.use_device);
        assert_eq!(opts.mac, ""); // Empty when using device
    }

    #[test]
    fn test_parse_set_args_missing_host() {
        let args = vec![];
        let result = parse_set_args(&args, false, None);
        assert!(result.is_err());
        assert_eq!(result.unwrap_err().to_string(), "arp: need host name");
    }

    #[test]
    fn test_parse_set_args_missing_mac() {
        let args = vec!["192.168.1.1".to_string()];
        let result = parse_set_args(&args, false, None);
        assert!(result.is_err());
        assert_eq!(
            result.unwrap_err().to_string(),
            "arp: need hardware address"
        );
    }

    #[test]
    fn test_parse_set_args_missing_device() {
        let args = vec!["192.168.1.1".to_string()];
        let result = parse_set_args(&args, true, None);
        assert!(result.is_err());
        assert_eq!(result.unwrap_err().to_string(), "arp: need device name");
    }

    #[test]
    fn test_parse_set_args_invalid_argument() {
        let args = vec![
            "192.168.1.1".to_string(),
            "aa:bb:cc:dd:ee:ff".to_string(),
            "invalid_flag".to_string(),
        ];
        let result = parse_set_args(&args, false, None);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("invalid argument"));
    }

    #[test]
    fn test_parse_set_args_missing_netmask_value() {
        let args = vec![
            "192.168.1.1".to_string(),
            "aa:bb:cc:dd:ee:ff".to_string(),
            "netmask".to_string(),
        ];
        let result = parse_set_args(&args, false, None);
        assert!(result.is_err());
        assert_eq!(result.unwrap_err().to_string(), "arp: need netmask value");
    }

    #[test]
    fn test_parse_set_args_missing_dev_value() {
        let args = vec![
            "192.168.1.1".to_string(),
            "aa:bb:cc:dd:ee:ff".to_string(),
            "dev".to_string(),
        ];
        let result = parse_set_args(&args, false, None);
        assert!(result.is_err());
        assert_eq!(result.unwrap_err().to_string(), "arp: need device name");
    }

    #[test]
    fn test_set_ip_valid() {
        // SAFETY: Initialize arpreq structure with zeros
        let mut req: libc::arpreq = unsafe { mem::zeroed() };
        let result = set_ip(&mut req, "192.168.1.1");
        assert!(result.is_ok());

        // Verify IP was set correctly
        // SAFETY: Reading the sockaddr_in we just set
        let sa = unsafe { &*(&req.arp_pa as *const libc::sockaddr as *const libc::sockaddr_in) };
        assert_eq!(sa.sin_family, libc::AF_INET as libc::sa_family_t);
        let expected_ip: Ipv4Addr = "192.168.1.1".parse().unwrap();
        assert_eq!(sa.sin_addr.s_addr, u32::from(expected_ip).to_be());
    }

    #[test]
    fn test_set_ip_invalid() {
        // SAFETY: Initialize arpreq structure with zeros
        let mut req: libc::arpreq = unsafe { mem::zeroed() };
        let result = set_ip(&mut req, "invalid");
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("invalid IP address")
        );
    }

    #[test]
    fn test_set_device_valid() {
        // SAFETY: Initialize arpreq structure with zeros
        let mut req: libc::arpreq = unsafe { mem::zeroed() };
        let result = set_device(&mut req, "eth0");
        assert!(result.is_ok());

        // Verify device name was set
        let dev_name = std::str::from_utf8(
            // SAFETY: Converting c_char array to bytes for comparison
            unsafe { &*(&req.arp_dev[..4] as *const [libc::c_char] as *const [u8]) },
        )
        .unwrap();
        assert_eq!(dev_name, "eth0");
    }

    #[test]
    fn test_set_device_too_long() {
        // SAFETY: Initialize arpreq structure with zeros
        let mut req: libc::arpreq = unsafe { mem::zeroed() };
        let long_name = "a".repeat(libc::IFNAMSIZ);
        let result = set_device(&mut req, &long_name);
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("device name too long")
        );
    }

    #[test]
    fn test_set_netmask_valid() {
        // SAFETY: Initialize arpreq structure with zeros
        let mut req: libc::arpreq = unsafe { mem::zeroed() };
        let mut flags = 0;
        let result = set_netmask(&mut req, "255.255.255.0", &mut flags);
        assert!(result.is_ok());
        assert_eq!(flags & libc::ATF_NETMASK, libc::ATF_NETMASK);

        // Verify netmask was set correctly
        // SAFETY: Reading the sockaddr_in we just set
        let mask_sa =
            unsafe { &*(&req.arp_netmask as *const libc::sockaddr as *const libc::sockaddr_in) };
        assert_eq!(mask_sa.sin_family, libc::AF_INET as libc::sa_family_t);
    }

    #[test]
    fn test_set_netmask_invalid() {
        // SAFETY: Initialize arpreq structure with zeros
        let mut req: libc::arpreq = unsafe { mem::zeroed() };
        let mut flags = 0;
        let result = set_netmask(&mut req, "invalid", &mut flags);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("invalid netmask"));
    }

    #[test]
    fn test_set_mac_from_string_valid() {
        // SAFETY: Initialize arpreq structure with zeros
        let mut req: libc::arpreq = unsafe { mem::zeroed() };
        let result = set_mac_from_string(&mut req, "aa:bb:cc:dd:ee:ff");
        assert!(result.is_ok());

        // Verify MAC was set correctly
        // SAFETY: Reading the sockaddr we just set
        let ha = unsafe { &*(&req.arp_ha as *const libc::sockaddr) };
        assert_eq!(ha.sa_family, libc::ARPHRD_ETHER as libc::sa_family_t);
        // SAFETY: Reading the MAC bytes we just set
        let mac_bytes = unsafe { std::slice::from_raw_parts(ha.sa_data.as_ptr() as *const u8, 6) };
        assert_eq!(mac_bytes, &[0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
    }

    #[test]
    fn test_set_mac_from_string_invalid() {
        // SAFETY: Initialize arpreq structure with zeros
        let mut req: libc::arpreq = unsafe { mem::zeroed() };
        let result = set_mac_from_string(&mut req, "invalid");
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("invalid hardware address")
        );
    }
}