-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
374 lines (358 loc) · 13.5 KB
/
build.rs
File metadata and controls
374 lines (358 loc) · 13.5 KB
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
use std::collections::HashMap;
use std::env;
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::Path;
/// Parse IEEE OUI CSV content into a map of decimal-key -> vendor name.
/// CSV format: Registry,Assignment,Organization Name,Organization Address
/// Assignment is a 6-char uppercase hex string like "CC03D9".
fn parse_oui_csv(csv: &str) -> HashMap<String, String> {
let mut map = HashMap::new();
for line in csv.lines().skip(1) {
// Simple CSV split; organization names don't contain unescaped commas
// but they may be quoted. We only need fields 1 and 2.
let line = line.trim();
if line.is_empty() {
continue;
}
// Split respecting basic CSV quoting for the first 3 fields
let fields: Vec<&str> = split_csv_line(line);
if fields.len() < 3 {
continue;
}
let assignment = fields[1].trim().trim_matches('"');
let org = fields[2].trim().trim_matches('"');
if assignment.len() != 6 {
continue;
}
if let (Ok(b0), Ok(b1), Ok(b2)) = (
u8::from_str_radix(&assignment[0..2], 16),
u8::from_str_radix(&assignment[2..4], 16),
u8::from_str_radix(&assignment[4..6], 16),
) {
let key = format!("{}_{}_{}", b0, b1, b2);
// Only insert first occurrence (avoid duplicates overwriting)
map.entry(key).or_insert_with(|| org.to_string());
}
}
map
}
fn split_csv_line(line: &str) -> Vec<&str> {
let mut fields = Vec::new();
let bytes = line.as_bytes();
let mut start = 0;
let mut in_quotes = false;
let mut i = 0;
while i < bytes.len() {
match bytes[i] {
b'"' => {
in_quotes = !in_quotes;
}
b',' if !in_quotes => {
fields.push(&line[start..i]);
start = i + 1;
}
_ => {}
}
i += 1;
}
fields.push(&line[start..]);
fields
}
/// Curated fallback list in case the IEEE download fails.
fn curated_vendors() -> HashMap<String, String> {
let vendors: &[([u8; 3], &str)] = &[
([0x00, 0x17, 0x88], "Philips Lighting"),
([0x00, 0x1A, 0x11], "Google"),
([0x00, 0x50, 0x56], "VMware"),
([0x00, 0x0C, 0x29], "VMware"),
([0x00, 0x15, 0x5D], "Microsoft"),
([0x00, 0x1C, 0x42], "Parallels"),
([0x08, 0x00, 0x27], "Oracle VirtualBox"),
([0x00, 0x03, 0x93], "Apple"),
([0x00, 0x05, 0x02], "Apple"),
([0x00, 0x0A, 0x27], "Apple"),
([0x00, 0x0A, 0x95], "Apple"),
([0x00, 0x0D, 0x93], "Apple"),
([0x00, 0x10, 0xFA], "Apple"),
([0x00, 0x11, 0x24], "Apple"),
([0x00, 0x14, 0x51], "Apple"),
([0x00, 0x16, 0xCB], "Apple"),
([0x00, 0x17, 0xF2], "Apple"),
([0x00, 0x19, 0xE3], "Apple"),
([0x00, 0x1B, 0x63], "Apple"),
([0x00, 0x1C, 0xB3], "Apple"),
([0x00, 0x1D, 0x4F], "Apple"),
([0x00, 0x1E, 0x52], "Apple"),
([0x00, 0x1E, 0xC2], "Apple"),
([0x00, 0x1F, 0x5B], "Apple"),
([0x00, 0x1F, 0xF3], "Apple"),
([0x00, 0x21, 0xE9], "Apple"),
([0x00, 0x22, 0x41], "Apple"),
([0x00, 0x23, 0x12], "Apple"),
([0x00, 0x23, 0x32], "Apple"),
([0x00, 0x23, 0x6C], "Apple"),
([0x00, 0x23, 0xDF], "Apple"),
([0x00, 0x24, 0x36], "Apple"),
([0x00, 0x25, 0x00], "Apple"),
([0x00, 0x25, 0x4B], "Apple"),
([0x00, 0x25, 0xBC], "Apple"),
([0x00, 0x26, 0x08], "Apple"),
([0x00, 0x26, 0x4A], "Apple"),
([0x00, 0x26, 0xB0], "Apple"),
([0x00, 0x26, 0xBB], "Apple"),
([0x04, 0x0C, 0xCE], "Apple"),
([0x04, 0x15, 0x52], "Apple"),
([0x04, 0x26, 0x65], "Apple"),
([0x04, 0x48, 0x9A], "Apple"),
([0x04, 0x4B, 0xED], "Apple"),
([0x04, 0x52, 0xF3], "Apple"),
([0x04, 0x54, 0x53], "Apple"),
([0x04, 0x69, 0xF8], "Apple"),
([0x04, 0xDB, 0x56], "Apple"),
([0x04, 0xE5, 0x36], "Apple"),
([0x04, 0xF1, 0x3E], "Apple"),
([0x04, 0xF7, 0xE4], "Apple"),
([0xA4, 0x83, 0xE7], "Apple"),
([0xAC, 0xBC, 0x32], "Apple"),
([0xF0, 0xD1, 0xA9], "Apple"),
([0xF4, 0x5C, 0x89], "Apple"),
([0x00, 0x16, 0x6B], "Samsung"),
([0x00, 0x16, 0x6C], "Samsung"),
([0x00, 0x16, 0xDB], "Samsung"),
([0x00, 0x17, 0xD5], "Samsung"),
([0x00, 0x18, 0xAF], "Samsung"),
([0x00, 0x1A, 0x8A], "Samsung"),
([0x00, 0x21, 0x19], "Samsung"),
([0x00, 0x26, 0x37], "Samsung"),
([0x5C, 0x3A, 0x45], "Samsung"),
([0x8C, 0x77, 0x12], "Samsung"),
([0xAC, 0x36, 0x13], "Samsung"),
([0xBC, 0x72, 0xB1], "Samsung"),
([0xCC, 0x07, 0xAB], "Samsung"),
([0xD0, 0x22, 0xBE], "Samsung"),
([0xF4, 0x42, 0x8F], "Samsung"),
([0x18, 0xD6, 0xC7], "Google"),
([0x54, 0x60, 0x09], "Google"),
([0xA4, 0x77, 0x33], "Google"),
([0xF4, 0xF5, 0xD8], "Google"),
([0x48, 0xD6, 0xD5], "Google Nest"),
([0x64, 0x16, 0x66], "Google Nest"),
([0x00, 0x00, 0x0C], "Cisco"),
([0x00, 0x01, 0x42], "Cisco"),
([0x00, 0x01, 0x43], "Cisco"),
([0x00, 0x01, 0x63], "Cisco"),
([0x00, 0x01, 0x64], "Cisco"),
([0x00, 0x01, 0x96], "Cisco"),
([0x00, 0x01, 0x97], "Cisco"),
([0x00, 0x0A, 0x8A], "Cisco"),
([0x00, 0x12, 0x17], "Cisco-Linksys"),
([0xCC, 0x03, 0xD9], "Cisco Meraki"),
([0x50, 0xC7, 0xBF], "TP-Link"),
([0x54, 0xC8, 0x0F], "TP-Link"),
([0x60, 0xE3, 0x27], "TP-Link"),
([0xAC, 0x84, 0xC6], "TP-Link"),
([0xB0, 0xA7, 0xB9], "TP-Link"),
([0xC0, 0x25, 0xE9], "TP-Link"),
([0x00, 0x09, 0x5B], "Netgear"),
([0x00, 0x0F, 0xB5], "Netgear"),
([0x00, 0x14, 0x6C], "Netgear"),
([0x00, 0x1B, 0x2F], "Netgear"),
([0x00, 0x1E, 0x2A], "Netgear"),
([0x20, 0x4E, 0x7F], "Netgear"),
([0x00, 0x27, 0x22], "Ubiquiti"),
([0x04, 0x18, 0xD6], "Ubiquiti"),
([0x24, 0xA4, 0x3C], "Ubiquiti"),
([0x44, 0xD9, 0xE7], "Ubiquiti"),
([0x68, 0x72, 0x51], "Ubiquiti"),
([0x78, 0x8A, 0x20], "Ubiquiti"),
([0x80, 0x2A, 0xA8], "Ubiquiti"),
([0xB4, 0xFB, 0xE4], "Ubiquiti"),
([0xDC, 0x9F, 0xDB], "Ubiquiti"),
([0xE0, 0x63, 0xDA], "Ubiquiti"),
([0xF0, 0x9F, 0xC2], "Ubiquiti"),
([0xFC, 0xEC, 0xDA], "Ubiquiti"),
([0x00, 0x02, 0xB3], "Intel"),
([0x00, 0x03, 0x47], "Intel"),
([0x00, 0x13, 0x02], "Intel"),
([0x00, 0x13, 0x20], "Intel"),
([0x00, 0x13, 0xCE], "Intel"),
([0x00, 0x13, 0xE8], "Intel"),
([0x00, 0x15, 0x00], "Intel"),
([0x3C, 0x97, 0x0E], "Intel"),
([0x5C, 0x5F, 0x67], "Intel"),
([0x8C, 0x8D, 0x28], "Intel"),
([0xB8, 0x27, 0xEB], "Raspberry Pi"),
([0xDC, 0xA6, 0x32], "Raspberry Pi"),
([0xE4, 0x5F, 0x01], "Raspberry Pi"),
([0x2C, 0xCF, 0x67], "Raspberry Pi"),
([0x00, 0x0E, 0x58], "Sonos"),
([0x5C, 0xAA, 0xFD], "Sonos"),
([0x54, 0x2A, 0x1B], "Sonos"),
([0x78, 0x28, 0xCA], "Sonos"),
([0x94, 0x9F, 0x3E], "Sonos"),
([0x00, 0x11, 0x32], "Synology"),
([0x58, 0x38, 0x79], "Ricoh"),
([0x00, 0x26, 0x73], "Ricoh"),
([0x00, 0x1D, 0x7B], "Ricoh"),
([0x00, 0x04, 0x4B], "Amazon"),
([0x00, 0xFC, 0x8B], "Amazon"),
([0x0C, 0x47, 0xC9], "Amazon"),
([0x10, 0xCE, 0xA9], "Amazon"),
([0x14, 0x91, 0x82], "Amazon"),
([0x18, 0x74, 0x2E], "Amazon"),
([0x34, 0xD2, 0x70], "Amazon"),
([0x40, 0xA2, 0xDB], "Amazon"),
([0x44, 0x65, 0x0D], "Amazon"),
([0x4C, 0xEF, 0xC0], "Amazon"),
([0x68, 0x54, 0xFD], "Amazon"),
([0x74, 0x75, 0x48], "Amazon"),
([0x84, 0xD6, 0xD0], "Amazon"),
([0xA0, 0x02, 0xDC], "Amazon"),
([0xFC, 0x65, 0xDE], "Amazon"),
([0x00, 0x06, 0x5B], "Dell"),
([0x00, 0x08, 0x74], "Dell"),
([0x00, 0x0B, 0xDB], "Dell"),
([0x00, 0x0D, 0x56], "Dell"),
([0x00, 0x0F, 0x1F], "Dell"),
([0x00, 0x11, 0x43], "Dell"),
([0x00, 0x12, 0x3F], "Dell"),
([0x00, 0x13, 0x72], "Dell"),
([0x00, 0x14, 0x22], "Dell"),
([0x00, 0x01, 0xE6], "HP"),
([0x00, 0x02, 0xA5], "HP"),
([0x00, 0x04, 0xEA], "HP"),
([0x00, 0x08, 0x02], "HP"),
([0x00, 0x0A, 0x57], "HP"),
([0x00, 0x0B, 0xCD], "HP"),
([0x00, 0x0D, 0x9D], "HP"),
([0x00, 0x0E, 0x7F], "HP"),
([0x00, 0x0F, 0x20], "HP"),
([0x00, 0x10, 0x83], "HP"),
([0x00, 0x11, 0x0A], "HP"),
([0x00, 0x11, 0x85], "HP"),
([0xB0, 0xA7, 0x37], "Roku"),
([0xBC, 0xD7, 0xD4], "Roku"),
([0xD4, 0xE2, 0x2F], "Roku"),
([0xDC, 0x3A, 0x5E], "Roku"),
([0x00, 0x62, 0x6E], "Ring"),
([0x34, 0x3D, 0xC4], "Ring"),
([0x50, 0xDC, 0x4A], "Ring"),
([0x00, 0x0C, 0x6E], "ASUS"),
([0x00, 0x0E, 0xA6], "ASUS"),
([0x00, 0x11, 0x2F], "ASUS"),
([0x00, 0x13, 0xD4], "ASUS"),
([0x00, 0x15, 0xF2], "ASUS"),
([0x00, 0x17, 0x31], "ASUS"),
([0x00, 0x1A, 0x92], "ASUS"),
([0x1C, 0xB7, 0x2C], "ASUS"),
([0x2C, 0x56, 0xDC], "ASUS"),
([0x60, 0x45, 0xCB], "ASUS"),
([0x00, 0x1E, 0x10], "Huawei"),
([0x00, 0x25, 0x9E], "Huawei"),
([0x00, 0x46, 0x4B], "Huawei"),
([0x04, 0xF9, 0x38], "Huawei"),
([0x0C, 0x37, 0xDC], "Huawei"),
([0x10, 0x44, 0x7F], "Huawei"),
([0x20, 0xA6, 0xCD], "Huawei"),
([0x24, 0x09, 0x95], "Huawei"),
([0x00, 0x1C, 0x62], "LG"),
([0x00, 0x1E, 0x75], "LG"),
([0x00, 0x22, 0xA9], "LG"),
([0x10, 0x68, 0x3F], "LG"),
([0x20, 0x3D, 0xBD], "LG"),
([0x58, 0xA2, 0xB5], "LG"),
([0xA8, 0x16, 0xB2], "LG"),
([0x00, 0x9E, 0xC8], "Xiaomi"),
([0x04, 0xCF, 0x8C], "Xiaomi"),
([0x0C, 0x1D, 0xAF], "Xiaomi"),
([0x10, 0x2A, 0xB3], "Xiaomi"),
([0x14, 0xF6, 0x5A], "Xiaomi"),
([0x18, 0x59, 0x36], "Xiaomi"),
([0x28, 0x6C, 0x07], "Xiaomi"),
([0x34, 0x80, 0xB3], "Xiaomi"),
([0x38, 0xA4, 0xED], "Xiaomi"),
([0x50, 0x64, 0x2B], "Xiaomi"),
([0x64, 0xCC, 0x2E], "Xiaomi"),
([0x78, 0x02, 0xF8], "Xiaomi"),
([0x7C, 0x49, 0xEB], "Xiaomi"),
];
let mut map = HashMap::new();
for (oui, vendor) in vendors {
let key = format!("{}_{}_{}", oui[0], oui[1], oui[2]);
map.entry(key).or_insert_with(|| vendor.to_string());
}
map
}
fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
// --- OUI Table ---
let oui_path = Path::new(&out_dir).join("oui_table.rs");
let mut oui_file = BufWriter::new(File::create(&oui_path).unwrap());
// Try to download the full IEEE OUI database; fall back to curated list.
let vendor_map: HashMap<String, String> = match ureq::get(
"https://standards-oui.ieee.org/oui/oui.csv",
)
.timeout(std::time::Duration::from_secs(30))
.call()
{
Ok(response) => {
match response.into_string() {
Ok(csv) => {
let parsed = parse_oui_csv(&csv);
if parsed.len() > 100 {
eprintln!(
"cargo:warning=Downloaded IEEE OUI database ({} entries)",
parsed.len()
);
parsed
} else {
eprintln!("cargo:warning=IEEE OUI CSV parse yielded too few entries; using curated fallback");
curated_vendors()
}
}
Err(e) => {
eprintln!("cargo:warning=Failed to read IEEE OUI response: {}; using curated fallback", e);
curated_vendors()
}
}
}
Err(e) => {
eprintln!(
"cargo:warning=Failed to download IEEE OUI database: {}; using curated fallback",
e
);
curated_vendors()
}
};
let mut oui_map = phf_codegen::Map::new();
// Collect and sort keys for deterministic codegen
let mut entries: Vec<(String, String)> = vendor_map.into_iter().collect();
entries.sort_by(|a, b| a.0.cmp(&b.0));
for (key, vendor) in &entries {
let escaped = vendor.replace('\\', "\\\\").replace('"', "\\\"");
oui_map.entry(key.as_str(), &format!("\"{}\"", escaped));
}
writeln!(
&mut oui_file,
"static OUI_TABLE: phf::Map<&'static str, &'static str> = {};",
oui_map.build()
)
.unwrap();
// --- Apple Model Table ---
let apple_path = Path::new(&out_dir).join("apple_models.rs");
let mut apple_file = BufWriter::new(File::create(&apple_path).unwrap());
let apple_json = std::fs::read_to_string("data/apple_models.json").unwrap();
let apple_models: HashMap<String, String> = serde_json::from_str(&apple_json).unwrap();
let mut apple_map = phf_codegen::Map::new();
for (id, name) in &apple_models {
let escaped = name.replace('\\', "\\\\").replace('"', "\\\"");
apple_map.entry(id.as_str(), &format!("\"{}\"", escaped));
}
writeln!(
&mut apple_file,
"static APPLE_MODELS: phf::Map<&'static str, &'static str> = {};",
apple_map.build()
)
.unwrap();
}