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

193 lines
5 KiB
Rust
Raw Normal View History

2021-01-04 20:45:23 +00:00
#![allow(dead_code)]
2021-01-04 20:40:34 +00:00
use xous::{SID, CID};
2021-01-05 08:53:19 +00:00
use hash32::{Hash, Hasher};
2021-01-04 20:40:34 +00:00
use core::cmp::Eq;
2021-01-04 15:34:36 +00:00
// bottom 16 bits are reserved for structure re-use by other servers
pub const ID_REGISTER_NAME: usize = 0x1_0000;
pub const ID_LOOKUP_NAME: usize = 0x2_0000;
pub const ID_AUTHENTICATE: usize = 0x3_0000;
pub const AUTHENTICATE_TIMEOUT: usize = 10_000; // time in ms that a process has to respond to an authentication request
//////////////////////// handle throwing strings across IPC boundary with hash comparison
2021-01-05 08:53:19 +00:00
#[derive(Debug, Copy, Clone)]
#[repr(C)]
pub struct XousServerName {
2021-01-05 08:53:19 +00:00
pub name: [u8; 64],
pub length: usize,
}
2021-01-05 08:53:19 +00:00
impl hash32::Hash for XousServerName {
fn hash<H>(&self, state: &mut H)
where
H: Hasher,
{
Hash::hash(&self.name[..], state);
Hash::hash(&self.length, state)
}
}
impl XousServerName {
pub fn new() -> Self {
Default::default()
}
pub fn to_str(&self) -> &str {
core::str::from_utf8(unsafe {
core::slice::from_raw_parts(self.name.as_ptr(), self.length)
})
.unwrap()
}
}
2021-01-04 20:40:34 +00:00
impl Default for XousServerName {
fn default() -> Self {
XousServerName {
2021-01-05 08:53:19 +00:00
name: [0; 64],
length: 0,
}
}
2021-01-04 20:40:34 +00:00
}
impl PartialEq for XousServerName {
fn eq(&self, other: &Self) -> bool {
self.name[..self.length] == other.name[..other.length] && self.length == other.length
2021-01-04 20:40:34 +00:00
}
}
2021-01-04 20:40:34 +00:00
impl Eq for XousServerName {}
// 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
if b.len() > self.name.len() {
Err(core::fmt::Error)?;
}
self.length = b.len();
// Copy the string into this variable
for (dest, src) in self.name.iter_mut().zip(s.bytes()) {
*dest = src;
}
// Attempt to convert the string to UTF-8 to validate it's correct UTF-8
core::str::from_utf8(unsafe {
core::slice::from_raw_parts(self.name.as_ptr(), self.length)
})
.map_err(|_| core::fmt::Error)?;
Ok(())
}
}
// Allow a `&XousServerName` to be printed out
impl core::fmt::Display for XousServerName {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.to_str())
}
}
// Allow a `&XousServerName` to be used anywhere that expects a `&str`
impl AsRef<str> for XousServerName {
fn as_ref(&self) -> &str {
self.to_str()
}
}
//////////////////////// end server name string implementation functions
2021-01-10 22:11:15 +00:00
#[derive(Debug, Copy, Clone)]
#[repr(C)]
2021-01-04 15:34:36 +00:00
pub struct Registration {
mid: usize,
2021-01-07 13:53:08 +00:00
pub sid: SID,
2021-01-04 20:40:34 +00:00
// we use fixed-length, u8-only records to pass server names. This is different from
// your typical Rust String object; but, a key restriction on IPC calls is that the size
// of structures must be (1) statically known and (2) contain no references. Therefore
2021-01-04 20:40:34 +00:00
// the name is a pre-allocated, 64-length u8 array, and the *entire* array is the name,
// including characters after the first "null"
pub name: XousServerName,
2021-01-04 15:34:36 +00:00
pub success: bool,
}
impl Registration {
pub fn mid(&self) -> usize { self.mid }
2021-01-04 15:34:36 +00:00
pub fn get_subtype(&self) -> u16 { (self.mid & 0xFFFF) as u16 }
pub fn set_subtype(&mut self, subtype: u16) { self.mid = (self.mid & 0xFFFF_0000) | (subtype as usize); }
pub fn match_subtype(id: usize, subtype: u16) -> bool {
((id & 0xFFFF) as u16 == subtype) && ((id & 0xFFFF_0000) == (ID_REGISTER_NAME & 0xFFFF_0000))
}
2021-01-10 22:11:15 +00:00
pub fn new() -> Self {
Registration {
mid: ID_REGISTER_NAME,
sid: SID::from_u32(0,0,0,0),
2021-01-04 20:40:34 +00:00
name: XousServerName::default(),
success: false,
}
}
}
2021-01-04 15:34:36 +00:00
#[derive(Debug)]
#[repr(C)]
pub struct Lookup {
mid: usize,
2021-01-04 20:40:34 +00:00
pub cid: CID,
pub name: XousServerName,
pub success: bool,
pub authenticate_request: bool,
pub pubkey_id: [u8; 20], // 160-bit pubkey ID encoded in network order (big endian)
pub challenge: [u32; 8],
2021-01-04 15:34:36 +00:00
}
2021-01-04 20:40:34 +00:00
impl Lookup {
pub fn mid(&self) -> usize { self.mid }
pub fn new() -> Self {
Lookup {
mid: ID_LOOKUP_NAME,
cid: 0,
2021-01-04 20:40:34 +00:00
name: XousServerName::default(),
success: false,
authenticate_request: false,
pubkey_id: [0; 20],
challenge: [0; 8],
2021-01-04 15:34:36 +00:00
}
}
}
2021-01-04 15:34:36 +00:00
#[derive(Debug)]
#[repr(C)]
pub struct Authenticate {
mid: usize,
2021-01-04 20:40:34 +00:00
pub cid: CID,
pub name: XousServerName,
pub success: bool,
pub response_to_challenge: [u32; 8],
}
2021-01-04 20:40:34 +00:00
impl Authenticate {
pub fn mid(&self) -> usize { self.mid }
pub fn new() -> Self {
Authenticate {
mid: ID_AUTHENTICATE,
cid: 0,
2021-01-04 20:40:34 +00:00
name: XousServerName::default(),
success: false,
response_to_challenge: [0; 8],
2021-01-04 15:34:36 +00:00
}
}
}