xous-core/services/xous-names/src/main.rs

134 lines
6 KiB
Rust
Raw Normal View History

2021-01-04 15:34:36 +00:00
#![cfg_attr(target_os = "none", no_std)]
#![cfg_attr(target_os = "none", no_main)]
2021-01-04 20:40:34 +00:00
extern crate hash32_derive;
2021-01-04 15:34:36 +00:00
mod api;
2021-03-17 19:54:48 +00:00
use api::*;
2021-01-04 15:34:36 +00:00
2021-01-05 08:53:19 +00:00
// use heapless::String;
2021-01-04 15:34:36 +00:00
use heapless::consts::*;
use heapless::FnvIndexMap;
2021-01-04 15:34:36 +00:00
2021-03-28 21:20:21 +00:00
use num_traits::FromPrimitive;
2021-03-28 20:35:11 +00:00
use xous_ipc::{String, Buffer};
2021-01-04 15:34:36 +00:00
use log::{error, info};
const FAIL_TIMEOUT_MS: u64 = 100;
2021-03-28 20:35:11 +00:00
2021-01-04 15:34:36 +00:00
#[xous::xous_main]
fn xmain() -> ! {
2021-03-10 20:12:14 +00:00
let debug1 = false;
2021-01-04 15:34:36 +00:00
log_server::init_wait().unwrap();
info!("NS: my PID is {}", xous::process::id());
2021-01-04 15:34:36 +00:00
let name_server = xous::create_server_with_address(b"xous-name-server")
.expect("Couldn't create xousnames-server");
2021-03-28 10:14:41 +00:00
let ticktimer = ticktimer_server::Ticktimer::new().expect("Couldn't connect to Ticktimer");
2021-01-04 20:40:34 +00:00
// this limits the number of available servers to be requested to 128...!
let mut name_table = FnvIndexMap::<_, _, U128>::new();
2021-01-04 15:34:36 +00:00
info!("NS: started");
loop {
2021-03-28 20:35:11 +00:00
let mut msg = xous::receive_message(name_server).unwrap();
if debug1{info!("NS: received message");}
2021-03-28 20:35:11 +00:00
match FromPrimitive::from_usize(msg.body.id()) {
Some(api::Opcode::Register) => {
2021-03-28 21:20:21 +00:00
let mem = msg.body.memory_message_mut().unwrap();
2021-03-28 20:35:11 +00:00
let mut buffer = unsafe { Buffer::from_memory_message_mut(mem) };
2021-03-31 19:47:10 +00:00
let xous_string = buffer.as_zerocopy_obj::<String::<64>, _>().unwrap();
2021-03-28 21:20:21 +00:00
let name = XousServerName::from_str(xous_string.as_str());
2021-03-28 20:35:11 +00:00
2021-03-28 21:20:21 +00:00
let response: api::Return;
2021-03-28 20:35:11 +00:00
if debug1{info!("NS: registration request for '{}'", name);}
if !name_table.contains_key(&name) {
let new_sid =
xous::create_server_id().expect("NS: create server failed, maybe OOM?");
name_table
.insert(name, new_sid)
.expect("NS: register name failure, maybe out of HashMap capacity?");
if debug1{info!("NS: request successful, SID is {:?}", new_sid);}
response = api::Return::SID(new_sid.into());
} else {
// compute the next interval, rounded to a multiple of FAIL_TIMEOUT_MS to reduce timing side channels
let target_time: u64 = ((ticktimer.elapsed_ms()
/ FAIL_TIMEOUT_MS)
+ 1)
* FAIL_TIMEOUT_MS;
info!("NS: request failed, waiting for deterministic timeout");
while ticktimer.elapsed_ms() < target_time {
xous::yield_slice();
2021-01-04 20:40:34 +00:00
}
2021-03-28 20:35:11 +00:00
info!("NS: deterministic timeout done");
response = api::Return::Failure
2021-01-04 20:40:34 +00:00
}
2021-03-28 20:35:11 +00:00
buffer.serialize_from(response).unwrap();
}
Some(api::Opcode::Lookup) => {
2021-03-28 21:20:21 +00:00
let mem = msg.body.memory_message_mut().unwrap();
2021-03-28 20:35:11 +00:00
let mut buffer = unsafe { Buffer::from_memory_message_mut(mem) };
2021-03-31 19:47:10 +00:00
let name_string = buffer.as_zerocopy_obj::<String::<64>, _>().unwrap();
2021-03-28 21:20:21 +00:00
let name = XousServerName::from_str(name_string.as_str());
2021-03-28 20:35:11 +00:00
if debug1{info!("NS: Lookup request for '{}'", name);}
2021-03-28 21:20:21 +00:00
let response: api::Return;
2021-03-28 20:35:11 +00:00
if let Some(server_sid) = name_table.get(&name) {
let sender_pid = msg
.sender
.pid()
.expect("NS: can't extract sender PID on Lookup");
match xous::connect_for_process(sender_pid, *server_sid)
.expect("NS: can't broker connection")
{
xous::Result::ConnectionID(connection_id) => {
if debug1{info!("NS: lookup success, returning connection {}", connection_id);}
response = api::Return::CID(connection_id)
}
2021-03-28 20:35:11 +00:00
_ => {
info!(
"NS: Can't find request '{}' in table, dumping table:",
name
);
for (key, val) in name_table.iter() {
info!("NS: name: '{}', sid: '{:?}'", key, val);
}
response = api::Return::Failure
}
}
2021-03-28 20:35:11 +00:00
} else {
info!(
"NS: Can't find request '{}' in table, dumping table:",
name
);
for (key, val) in name_table.iter() {
info!("NS: name: '{}', sid: '{:?}'", key, val);
}
// no authenticate remedy currently supported, but we'd put that code somewhere around here eventually.
2021-03-28 20:44:03 +00:00
let (c1, c2, c3, c4) = xous::create_server_id().unwrap().to_u32();
2021-03-28 20:35:11 +00:00
let auth_request = AuthenticateRequest {
2021-03-28 21:20:21 +00:00
name: String::<64>::from_str(name_string.as_str()),
2021-03-28 20:35:11 +00:00
pubkey_id: [0; 20], // placeholder
2021-03-28 20:44:03 +00:00
challenge: [c1, c2, c3, c4],
2021-03-28 20:35:11 +00:00
};
response = api::Return::AuthenticateRequest(auth_request) // this code just exists to exercise the return path
}
2021-03-28 20:35:11 +00:00
buffer.serialize_from(response).unwrap();
}
Some(api::Opcode::AuthenticatedLookup) => {
2021-03-28 21:20:21 +00:00
let mem = msg.body.memory_message_mut().unwrap();
2021-03-28 20:35:11 +00:00
let buffer = unsafe { Buffer::from_memory_message_mut(mem) };
2021-03-31 19:47:10 +00:00
let auth_lookup: AuthenticatedLookup = buffer.as_copy_obj().unwrap();
2021-03-28 20:35:11 +00:00
info!("NS: AuthenticatedLookup request {:?}", auth_lookup);
error!("NS: AuthenticatedLookup not yet implemented");
unimplemented!("NS: AuthenticatedLookup not yet implemented");
}
None => error!("NS: couldn't decode message: {:?}", msg)
2021-01-04 15:34:36 +00:00
}
}
}