2021-01-04 20:45:23 +00:00
|
|
|
#![allow(dead_code)]
|
|
|
|
|
|
2021-02-10 07:02:48 +00:00
|
|
|
use xous::CID;
|
2021-01-04 20:40:34 +00:00
|
|
|
|
2021-01-12 10:44:02 +00:00
|
|
|
// bottom 16 bits are reserved for structure re-use by other servers
|
2021-02-10 07:02:48 +00:00
|
|
|
pub const ID_REGISTER_NAME: u32 = 0x1_0000;
|
|
|
|
|
pub const ID_LOOKUP_NAME: u32 = 0x2_0000;
|
|
|
|
|
pub const ID_AUTHENTICATE: u32 = 0x3_0000;
|
2021-01-04 18:28:27 +00:00
|
|
|
|
2021-02-10 07:02:48 +00:00
|
|
|
pub const AUTHENTICATE_TIMEOUT: u32 = 10_000; // time in ms that a process has to respond to an authentication request
|
2021-01-04 18:28:27 +00:00
|
|
|
|
2021-03-15 18:02:53 +00:00
|
|
|
#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Debug)]
|
2021-02-10 07:02:48 +00:00
|
|
|
pub(crate) enum Request {
|
|
|
|
|
/// Create a new server with the given name and return its SID.
|
2021-03-15 18:02:53 +00:00
|
|
|
Register(xous::String::<64>),
|
2021-01-05 07:48:51 +00:00
|
|
|
|
2021-02-10 07:02:48 +00:00
|
|
|
/// Create a connection to the target server.
|
2021-03-15 18:02:53 +00:00
|
|
|
Lookup(xous::String::<64>),
|
2021-02-10 07:02:48 +00:00
|
|
|
|
|
|
|
|
/// Create an authenticated connection to the target server.
|
|
|
|
|
AuthenticatedLookup(AuthenticatedLookup),
|
|
|
|
|
|
|
|
|
|
// Return values
|
|
|
|
|
|
|
|
|
|
/// The caller must perform an AuthenticatedLookup using this challenge
|
|
|
|
|
AuthenticateRequest([u32; 8]),
|
|
|
|
|
|
|
|
|
|
/// The connection failed for some reason
|
|
|
|
|
Failure,
|
|
|
|
|
|
|
|
|
|
/// A server was successfully created with the given SID
|
|
|
|
|
SID([u32; 4]),
|
|
|
|
|
|
|
|
|
|
/// A connection was successfully made with the given CID
|
|
|
|
|
CID(CID),
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-15 18:02:53 +00:00
|
|
|
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
|
2021-02-10 07:02:48 +00:00
|
|
|
pub(crate) struct Lookup {
|
2021-03-15 18:02:53 +00:00
|
|
|
pub name: xous::String::<64>,
|
2021-02-10 07:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
2021-03-15 18:02:53 +00:00
|
|
|
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
|
2021-02-10 07:02:48 +00:00
|
|
|
pub(crate) struct AuthenticatedLookup {
|
2021-03-15 18:02:53 +00:00
|
|
|
pub name: xous::String::<64>,
|
2021-02-10 07:02:48 +00:00
|
|
|
pub pubkey_id: [u8; 20], // 160-bit pubkey ID encoded in network order (big endian)
|
|
|
|
|
pub challenge: [u32; 8],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct Authenticate {
|
2021-03-15 18:02:53 +00:00
|
|
|
pub name: xous::String::<64>,
|
2021-02-10 07:02:48 +00:00
|
|
|
pub success: bool,
|
|
|
|
|
pub response_to_challenge: [u32; 8],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Authenticate {
|
|
|
|
|
pub fn mid(&self) -> u32 {
|
|
|
|
|
ID_AUTHENTICATE as u32
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 08:06:06 +00:00
|
|
|
// We keep XousServerName around because want to be able to index off the server name, without
|
|
|
|
|
// burdening the Kernel String type with the Hash32 methods
|
2021-03-16 07:52:13 +00:00
|
|
|
|
2021-02-10 07:02:48 +00:00
|
|
|
// --------------------- Taken from rkyv docs https://docs.rs/rkyv/0.3.0/rkyv/trait.Archive.html //
|
2021-01-05 08:53:19 +00:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
2021-01-05 07:48:51 +00:00
|
|
|
pub struct XousServerName {
|
2021-02-10 07:02:48 +00:00
|
|
|
value: [u8; 64],
|
|
|
|
|
length: u32,
|
2021-01-05 07:48:51 +00:00
|
|
|
}
|
|
|
|
|
|
2021-02-10 07:02:48 +00:00
|
|
|
impl Default for XousServerName {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
XousServerName {
|
|
|
|
|
value: [0u8; 64],
|
|
|
|
|
length: 0,
|
|
|
|
|
}
|
2021-01-05 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-05 07:48:51 +00:00
|
|
|
impl XousServerName {
|
|
|
|
|
pub fn to_str(&self) -> &str {
|
|
|
|
|
core::str::from_utf8(unsafe {
|
2021-02-10 07:02:48 +00:00
|
|
|
core::slice::from_raw_parts(self.value.as_ptr(), self.length as usize)
|
2021-01-05 07:48:51 +00:00
|
|
|
})
|
|
|
|
|
.unwrap()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Allow using the `write!()` macro to write into a `&XousServerName`
|
|
|
|
|
impl core::fmt::Write for XousServerName {
|
|
|
|
|
fn write_str(&mut self, s: &str) -> core::result::Result<(), core::fmt::Error> {
|
|
|
|
|
self.length = 0;
|
|
|
|
|
let b = s.bytes();
|
|
|
|
|
|
|
|
|
|
// Ensure the length is acceptable
|
2021-02-10 07:02:48 +00:00
|
|
|
if b.len() > self.value.len() {
|
2021-01-05 07:48:51 +00:00
|
|
|
Err(core::fmt::Error)?;
|
|
|
|
|
}
|
2021-01-21 16:41:25 +00:00
|
|
|
self.length = b.len() as u32;
|
2021-01-05 07:48:51 +00:00
|
|
|
|
|
|
|
|
// Copy the string into this variable
|
2021-02-10 07:02:48 +00:00
|
|
|
for (dest, src) in self.value.iter_mut().zip(s.bytes()) {
|
2021-01-05 07:48:51 +00:00
|
|
|
*dest = src;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Attempt to convert the string to UTF-8 to validate it's correct UTF-8
|
|
|
|
|
core::str::from_utf8(unsafe {
|
2021-02-10 07:02:48 +00:00
|
|
|
core::slice::from_raw_parts(self.value.as_ptr(), self.length as usize)
|
2021-01-05 07:48:51 +00:00
|
|
|
})
|
|
|
|
|
.map_err(|_| core::fmt::Error)?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-10 07:02:48 +00:00
|
|
|
impl hash32::Hash for XousServerName {
|
|
|
|
|
fn hash<H>(&self, state: &mut H)
|
|
|
|
|
where
|
|
|
|
|
H: Hasher,
|
|
|
|
|
{
|
|
|
|
|
Hash::hash(&self.value[..self.length as usize], state);
|
|
|
|
|
Hash::hash(&self.length, state)
|
2021-01-05 07:48:51 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-10 07:02:48 +00:00
|
|
|
impl PartialEq for XousServerName {
|
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
|
self.value[..self.length as usize] == other.value[..other.length as usize]
|
|
|
|
|
&& self.length == other.length
|
2021-01-05 07:48:51 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-10 07:02:48 +00:00
|
|
|
impl Eq for XousServerName {}
|
2021-01-05 07:48:51 +00:00
|
|
|
|
2021-02-10 07:02:48 +00:00
|
|
|
impl core::fmt::Display for XousServerName {
|
|
|
|
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
|
|
|
|
write!(f, "{}", self.to_str())
|
|
|
|
|
}
|
2021-01-04 15:34:36 +00:00
|
|
|
}
|