Nothing to see here, move along
at main 100 lines 2.8 kB view raw
1#![no_std] 2#![no_main] 3 4use lancer_user::net; 5use lancer_user::syscall; 6 7#[unsafe(no_mangle)] 8pub extern "C" fn lancer_main() -> ! { 9 let (rx, tx) = match net::init() { 10 Some(pair) => pair, 11 None => { 12 lancer_user::show!(net, error, "netsock init failed"); 13 syscall::exit(); 14 } 15 }; 16 17 if !net::has_relay() { 18 lancer_user::io::write_bytes(b"networking not available (use SSH)\n"); 19 syscall::exit(); 20 } 21 22 let mut args_buf = [0u8; 64]; 23 let _ = net::recv_args(&rx, &mut args_buf); 24 25 net::send_request(&tx, &[net::MSG_ARP_REQUEST]); 26 27 let mut resp = [0u8; 64]; 28 let mut done = false; 29 core::iter::from_fn(|| match done { 30 true => None, 31 false => { 32 let n = net::recv_response_blocking(&rx, &mut resp); 33 match n < 1 { 34 true => None, 35 false => match resp[0] { 36 net::MSG_ARP_DONE => { 37 done = true; 38 None 39 } 40 net::MSG_ARP_ENTRY if n >= 12 => { 41 let mut out = [0u8; 64]; 42 let mut pos = 0usize; 43 44 let ip_len = net::write_ip_decimal(&mut out[pos..], &resp[1..5]); 45 pos += ip_len; 46 47 (pos..18).for_each(|i| { 48 out[i] = b' '; 49 }); 50 pos = pos.max(18); 51 52 pos = write_mac(&mut out, pos, &resp[5..11]); 53 54 (pos..pos + 4).for_each(|i| { 55 out[i] = b' '; 56 }); 57 pos += 4; 58 59 let age_secs = ((resp[11] as u16) << 8) | resp[12] as u16; 60 let age_len = net::write_u16_decimal(&mut out[pos..], age_secs); 61 pos += age_len; 62 out[pos] = b's'; 63 pos += 1; 64 out[pos] = b'\n'; 65 pos += 1; 66 67 lancer_user::io::write_bytes(&out[..pos]); 68 Some(()) 69 } 70 _ => Some(()), 71 }, 72 } 73 } 74 }) 75 .take(16) 76 .count(); 77 78 syscall::exit() 79} 80 81fn write_mac(buf: &mut [u8], start: usize, mac: &[u8]) -> usize { 82 let hex = |n: u8| -> u8 { 83 match n < 10 { 84 true => b'0' + n, 85 false => b'a' + (n - 10), 86 } 87 }; 88 89 let mut p = start; 90 (0..6).for_each(|i| { 91 if i > 0 { 92 buf[p] = b':'; 93 p += 1; 94 } 95 buf[p] = hex(mac[i] >> 4); 96 buf[p + 1] = hex(mac[i] & 0x0f); 97 p += 2; 98 }); 99 p 100}