summary refs log tree commit diff
path: root/host
diff options
context:
space:
mode:
authorAlyssa Ross <hi@alyssa.is>2022-02-15 14:08:28 +0000
committerAlyssa Ross <hi@alyssa.is>2022-02-15 14:08:28 +0000
commit322f0ada8100ebed733dd7ad2154039070b86656 (patch)
tree70b7a5b5506f886671be358da68f0d3c85358fcc /host
parent618530af0cf10cad1aadbff9c1e6112498db8d5f (diff)
downloadspectrum-322f0ada8100ebed733dd7ad2154039070b86656.tar
spectrum-322f0ada8100ebed733dd7ad2154039070b86656.tar.gz
spectrum-322f0ada8100ebed733dd7ad2154039070b86656.tar.bz2
spectrum-322f0ada8100ebed733dd7ad2154039070b86656.tar.lz
spectrum-322f0ada8100ebed733dd7ad2154039070b86656.tar.xz
spectrum-322f0ada8100ebed733dd7ad2154039070b86656.tar.zst
spectrum-322f0ada8100ebed733dd7ad2154039070b86656.zip
host/start-vm: implement format_mac in Rust
All callers are Rust at this point, so there's no point having a C
version available.
Diffstat (limited to 'host')
-rw-r--r--host/start-vm/net.c9
-rw-r--r--host/start-vm/net.rs23
2 files changed, 7 insertions, 25 deletions
diff --git a/host/start-vm/net.c b/host/start-vm/net.c
index 41bf1c2..ebd6126 100644
--- a/host/start-vm/net.c
+++ b/host/start-vm/net.c
@@ -18,15 +18,6 @@
 
 #include <linux/if_tun.h>
 
-#define MAC_STR_LEN 17
-
-int format_mac(char s[static MAC_STR_LEN + 1], const uint8_t mac[6])
-{
-	return snprintf(s, MAC_STR_LEN + 1,
-			"%.2hhX:%.2hhX:%.2hhX:%.2hhX:%.2hhX:%.2hhX",
-			mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
-}
-
 static int setup_tap(const char *bridge_name, const char *tap_prefix)
 {
 	int fd;
diff --git a/host/start-vm/net.rs b/host/start-vm/net.rs
index 12a027f..98a8f48 100644
--- a/host/start-vm/net.rs
+++ b/host/start-vm/net.rs
@@ -1,7 +1,7 @@
 // SPDX-License-Identifier: EUPL-1.2
 // SPDX-FileCopyrightText: 2022 Alyssa Ross <hi@alyssa.is>
 
-use std::os::raw::{c_char, c_int};
+use std::os::raw::c_char;
 
 #[repr(C)]
 pub struct NetConfig {
@@ -14,20 +14,10 @@ extern "C" {
 }
 
 pub fn format_mac(mac: &[u8; 6]) -> String {
-    extern "C" {
-        fn format_mac(s: *mut c_char, mac: *const [u8; 6]) -> c_int;
-    }
-
-    let mut s = vec![0; 18];
-
-    // Safe because s and mac are correctly sized.
-    assert_ne!(unsafe { format_mac(s.as_mut_ptr() as _, mac) }, -1);
-
-    // Drop the null byte.
-    s.pop();
-
-    // Safe because a formatted MAC address is always UTF-8.
-    unsafe { String::from_utf8_unchecked(s) }
+    format!(
+        "{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}",
+        mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]
+    )
 }
 
 #[cfg(test)]
@@ -41,6 +31,7 @@ mod tests {
 
     #[test]
     fn format_mac_hex() {
-        assert_eq!(format_mac(&[0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54]), "FE:DC:BA:98:76:54");
+        let mac = [0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54];
+        assert_eq!(format_mac(&mac), "FE:DC:BA:98:76:54");
     }
 }