forge
Explore
Log in

Don't you dare go Hollow

wifi_lab

Clone
★ 0

0 watching0 forksRust2.1 MiBb265998Tahinli1 month ago27 commits

oui.rs167 lines, 5.5 KiBHistoryRawDownload
1//! OUI vendor lookup for MAC prefixes.
2//!
3//! Three checked-in snapshot tables from IEEE registries (provenance and
4//! update semantics in docs/CONTRACT.md `## oui.rs`):
5//! - `src/data/oui.txt` — MA-L 24-bit (`AABBCC|Vendor`, 6 hex chars)
6//! - `src/data/oui28.txt` — MA-M 28-bit (`AABBCCD|Vendor`, 7 hex chars)
7//! - `src/data/oui36.txt` — MA-S 36-bit (`AABBCCDEE|Vendor`, 9 hex chars)
8//!
9//! Vendors (= short name, ≤32 chars) are byte-sorted and unique per file.
10//! Lookup is longest-prefix-first: MA-S (36) → MA-M (28) → MA-L (24), since
11//! the sub-assignments sit under someone else's MA-L base.
12
13use std::sync::LazyLock;
14
15use crate::common::MacAddr;
16
17fn parse24(src: &'static str) -> Vec<(u32, &'static str)> {
18 src.lines()
19 .filter_map(|line| {
20 let (hex, name) = line.split_once('|')?;
21 let key = u32::from_str_radix(hex.trim(), 16).ok()?;
22 (!name.is_empty()).then_some((key, name))
23 })
24 .collect()
25}
26
27/// Parsed `(prefix, vendor)` pairs from `data/oui.txt` (24-bit MA-L).
28static TABLE24: LazyLock<Vec<(u32, &'static str)>> =
29 LazyLock::new(|| parse24(include_str!("data/oui.txt")));
30/// MA-M 28-bit sub-assignments.
31static TABLE28: LazyLock<Vec<(u32, &'static str)>> =
32 LazyLock::new(|| parse24(include_str!("data/oui28.txt")));
33/// MA-S 36-bit sub-assignments.
34static TABLE36: LazyLock<Vec<(u64, &'static str)>> = LazyLock::new(|| {
35 include_str!("data/oui36.txt")
36 .lines()
37 .filter_map(|line| {
38 let (hex, name) = line.split_once('|')?;
39 let key = u64::from_str_radix(hex.trim(), 16).ok()?;
40 (!name.is_empty()).then_some((key, name))
41 })
42 .collect()
43});
44
45/// True for locally administered MACs (U/L bit set, wire byte order): the
46/// randomized/rotated addresses modern phones and laptops send. These have
47/// no IEEE vendor — `lookup` returns None for them by design.
48pub fn is_locally_administered(mac: &MacAddr) -> bool {
49 mac.0[0] & 0x02 != 0
50}
51
52/// Vendor for a MAC's prefix; None for locally-administered/unknown
53/// prefixes (see `is_locally_administered`).
54pub fn lookup(mac: &MacAddr) -> Option<&'static str> {
55 if is_locally_administered(mac) {
56 return None;
57 }
58 let m = mac.0;
59 let key36 = ((m[0] as u64) << 28)
60 | ((m[1] as u64) << 20)
61 | ((m[2] as u64) << 12)
62 | ((m[3] as u64) << 4)
63 | (m[4] >> 4) as u64;
64 let t36 = &*TABLE36;
65 if let Ok(i) = t36.binary_search_by_key(&key36, |&(k, _)| k) {
66 return Some(t36[i].1);
67 }
68 let key28 =
69 ((m[0] as u32) << 20) | ((m[1] as u32) << 12) | ((m[2] as u32) << 4) | (m[3] >> 4) as u32;
70 let t28 = &*TABLE28;
71 if let Ok(i) = t28.binary_search_by_key(&key28, |&(k, _)| k) {
72 return Some(t28[i].1);
73 }
74 let key24 = ((m[0] as u32) << 16) | ((m[1] as u32) << 8) | m[2] as u32;
75 let t24 = &*TABLE24;
76 t24.binary_search_by_key(&key24, |&(k, _)| k)
77 .ok()
78 .map(|i| t24[i].1)
79}
80
81#[cfg(test)]
82mod tests {
83 use super::*;
84
85 fn mac(b: [u8; 6]) -> MacAddr {
86 MacAddr(b)
87 }
88
89 #[test]
90 fn tables_are_populated_and_sorted() {
91 let t = &*TABLE24;
92 assert!(
93 t.len() > 10_000,
94 "expected a real MA-L snapshot, got {}",
95 t.len()
96 );
97 assert!(
98 t.windows(2).all(|w| w[0].0 < w[1].0),
99 "MA-L must be sorted+unique"
100 );
101 let t = &*TABLE28;
102 assert!(
103 t.len() > 1_000,
104 "expected a real MA-M snapshot, got {}",
105 t.len()
106 );
107 assert!(
108 t.windows(2).all(|w| w[0].0 < w[1].0),
109 "MA-M must be sorted+unique"
110 );
111 let t = &*TABLE36;
112 assert!(
113 t.len() > 1_000,
114 "expected a real MA-S snapshot, got {}",
115 t.len()
116 );
117 assert!(
118 t.windows(2).all(|w| w[0].0 < w[1].0),
119 "MA-S must be sorted+unique"
120 );
121 }
122
123 #[test]
124 fn locally_administered_is_none() {
125 assert!(is_locally_administered(&mac([0x02, 0, 0, 0, 0, 0])));
126 assert!(!is_locally_administered(&mac([0x00, 0, 0, 0, 0, 0])));
127 assert_eq!(lookup(&mac([0x02, 0, 0, 0, 0, 0])), None);
128 }
129
130 #[test]
131 fn multicast_but_globally_unique_still_resolves() {
132 // 01:00:5e… is multicast but not locally administered; resolve per tables.
133 let key_ok = TABLE24.iter().any(|&(k, _)| k == 0x01005E)
134 || TABLE36.iter().any(|&(k, _)| k == 0x01005E000)
135 || TABLE28.iter().any(|&(k, _)| k == 0x01005E0);
136 let got = lookup(&mac([0x01, 0x00, 0x5e, 0, 0, 1]));
137 assert_eq!(got.is_some(), key_ok);
138 }
139
140 #[test]
141 fn pinned_prefix_cisco() {
142 // Pinned from the actual checked-in snapshot: 00000C|Cisco Systems, Inc
143 assert_eq!(
144 lookup(&mac([0x00, 0x00, 0x0c, 0xab, 0xcd, 0xef])),
145 Some("Cisco Systems, Inc")
146 );
147 }
148
149 #[test]
150 fn pinned_mam_prefix_siretta() {
151 // 28-bit MA-M: FCE498C|Siretta Ltd — matches any MAC FC:E4:98:CX….
152 assert_eq!(
153 lookup(&mac([0xfc, 0xe4, 0x98, 0xc7, 0x12, 0x34])),
154 Some("Siretta Ltd")
155 );
156 }
157
158 #[test]
159 fn pinned_mas_prefix_converging() {
160 // 36-bit MA-S: 001BC5000|Converging Systems Inc. — matches
161 // 00:1B:C5:00:0x:…, and outranks the 24-bit 001BC5 base entry.
162 assert_eq!(
163 lookup(&mac([0x00, 0x1b, 0xc5, 0x00, 0x05, 0xaa])),
164 Some("Converging Systems Inc.")
165 );
166 }
167}