diff --git a/Cargo.lock b/Cargo.lock index 5ddedfe75..16a9f1c75 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -150,8 +150,8 @@ dependencies = [ "lazycell", "log", "peeking_take_while", - "proc-macro2 1.0.24", - "quote 1.0.8", + "proc-macro2", + "quote", "regex", "rustc-hash", "shlex", @@ -623,9 +623,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bd53b3fde38a39a06b2e66dc282f3e86191e53bd04cc499929c15742beae3df8" dependencies = [ "once_cell", - "proc-macro2 1.0.24", - "quote 1.0.8", - "syn 1.0.67", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -767,26 +767,6 @@ dependencies = [ "xous-names", ] -[[package]] -name = "hash32" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0c35f58762feb77d74ebe43bdbc3210f09be9fe6742234d573bacc26ed92b67" -dependencies = [ - "byteorder", -] - -[[package]] -name = "hash32-derive" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ebc0efbd154a17cddc3616d83faef479c0076d871a2143c157b310cc7ca799a2" -dependencies = [ - "proc-macro2 0.3.8", - "quote 0.5.2", - "syn 0.13.11", -] - [[package]] name = "hermit-abi" version = "0.1.18" @@ -1098,9 +1078,9 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "876a53fff98e03a936a674b29568b0e605f06b29372c2489ff4de23f1949743d" dependencies = [ - "proc-macro2 1.0.24", - "quote 1.0.8", - "syn 1.0.67", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -1188,22 +1168,13 @@ version = "0.5.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" -[[package]] -name = "proc-macro2" -version = "0.3.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b06e2f335f48d24442b35a19df506a835fb3547bc3c06ef27340da9acf5cae7" -dependencies = [ - "unicode-xid 0.1.0", -] - [[package]] name = "proc-macro2" version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" dependencies = [ - "unicode-xid 0.2.1", + "unicode-xid", ] [[package]] @@ -1221,9 +1192,9 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7bcd6232fa7d7fcd15703da5c07c9f10d203ee4fdf6ecf6d7ac5056db78089a3" dependencies = [ - "proc-macro2 1.0.24", - "quote 1.0.8", - "syn 1.0.67", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -1241,22 +1212,13 @@ dependencies = [ "memchr", ] -[[package]] -name = "quote" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9949cfe66888ffe1d53e6ec9d9f3b70714083854be20fd5e271b232a017401e8" -dependencies = [ - "proc-macro2 0.3.8", -] - [[package]] name = "quote" version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "991431c3519a3f36861882da93630ce66b52918dcf1b8e2fd66b397fc96f28df" dependencies = [ - "proc-macro2 1.0.24", + "proc-macro2", ] [[package]] @@ -1471,9 +1433,9 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ddf04e93b51e6cfcc0eeee3bbf121d978303b83fbf2dc49085cff7855478fd4b" dependencies = [ - "proc-macro2 1.0.24", - "quote 1.0.8", - "syn 1.0.67", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -1482,9 +1444,9 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95a169f6bc5a81033e86ed39d0f4150e2608160b73d2b93c6e8e6a3efa873f14" dependencies = [ - "proc-macro2 1.0.24", - "quote 1.0.8", - "syn 1.0.67", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -1787,26 +1749,15 @@ dependencies = [ "quick-xml", ] -[[package]] -name = "syn" -version = "0.13.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14f9bf6292f3a61d2c716723fdb789a41bbe104168e6f496dc6497e531ea1b9b" -dependencies = [ - "proc-macro2 0.3.8", - "quote 0.5.2", - "unicode-xid 0.1.0", -] - [[package]] name = "syn" version = "1.0.67" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6498a9efc342871f91cc2d0d694c674368b4ceb40f62b65a7a08c3792935e702" dependencies = [ - "proc-macro2 1.0.24", - "quote 1.0.8", - "unicode-xid 0.2.1", + "proc-macro2", + "quote", + "unicode-xid", ] [[package]] @@ -1815,10 +1766,10 @@ version = "0.12.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b834f2d66f734cb897113e34aaff2f1ab4719ca946f9a7358dba8f8064148701" dependencies = [ - "proc-macro2 1.0.24", - "quote 1.0.8", - "syn 1.0.67", - "unicode-xid 0.2.1", + "proc-macro2", + "quote", + "syn", + "unicode-xid", ] [[package]] @@ -1958,12 +1909,6 @@ version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" -[[package]] -name = "unicode-xid" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" - [[package]] name = "unicode-xid" version = "0.2.1" @@ -2047,9 +1992,9 @@ dependencies = [ "bumpalo", "lazy_static", "log", - "proc-macro2 1.0.24", - "quote 1.0.8", - "syn 1.0.67", + "proc-macro2", + "quote", + "syn", "wasm-bindgen-shared", ] @@ -2059,7 +2004,7 @@ version = "0.2.74" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "088169ca61430fe1e58b8096c24975251700e7b1f6fd91cc9d59b04fb9b18bd4" dependencies = [ - "quote 1.0.8", + "quote", "wasm-bindgen-macro-support", ] @@ -2069,9 +2014,9 @@ version = "0.2.74" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be2241542ff3d9f241f5e2cb6dd09b37efe786df8851c54957683a49f0987a97" dependencies = [ - "proc-macro2 1.0.24", - "quote 1.0.8", - "syn 1.0.67", + "proc-macro2", + "quote", + "syn", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -2138,8 +2083,8 @@ version = "0.28.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "389d680d7bd67512dc9c37f39560224327038deb0f0e8d33f870900441b68720" dependencies = [ - "proc-macro2 1.0.24", - "quote 1.0.8", + "proc-macro2", + "quote", "xml-rs", ] @@ -2317,18 +2262,16 @@ dependencies = [ name = "xous-macros" version = "0.1.0" dependencies = [ - "proc-macro2 1.0.24", - "quote 1.0.8", + "proc-macro2", + "quote", "rand 0.7.3", - "syn 1.0.67", + "syn", ] [[package]] name = "xous-names" version = "0.1.0" dependencies = [ - "hash32", - "hash32-derive", "log", "log-server", "num-derive", @@ -2345,8 +2288,8 @@ version = "0.1.0" dependencies = [ "glob", "lazy_static", - "proc-macro2 1.0.24", - "quote 1.0.8", + "proc-macro2", + "quote", "regex", "serde_json", ] @@ -2372,8 +2315,8 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2c1e130bebaeab2f23886bf9acbaca14b092408c452543c857f66399cd6dab1" dependencies = [ - "proc-macro2 1.0.24", - "quote 1.0.8", - "syn 1.0.67", + "proc-macro2", + "quote", + "syn", "synstructure", ] diff --git a/services/xous-names/Cargo.toml b/services/xous-names/Cargo.toml index c826f1d9e..f40714452 100644 --- a/services/xous-names/Cargo.toml +++ b/services/xous-names/Cargo.toml @@ -6,8 +6,6 @@ name = "xous-names" version = "0.1.0" [dependencies] -hash32 = "0.2" -hash32-derive = "0.1.0" num-derive = {version = "0.3", default-features = false} num-traits = {version = "0.2", default-features = false} log = "0.4" diff --git a/services/xous-names/src/api.rs b/services/xous-names/src/api.rs index 72e5a91ae..93f074da7 100644 --- a/services/xous-names/src/api.rs +++ b/services/xous-names/src/api.rs @@ -1,5 +1,3 @@ -use hash32::{Hash, Hasher}; - #[allow(dead_code)] pub const AUTHENTICATE_TIMEOUT: u32 = 10_000; // time in ms that a process has to respond to an authentication request @@ -165,14 +163,14 @@ impl core::fmt::Write for XousServerName { } } -impl hash32::Hash for XousServerName { +impl std::hash::Hash for XousServerName { fn hash(&self, state: &mut H) where - H: Hasher, + H: std::hash::Hasher, { assert!(self.length < self.value.len(), "incorret length on hash!"); - Hash::hash(&self.value[..self.length as usize], state); - Hash::hash(&self.length, state) + std::hash::Hash::hash(&self.value[..self.length as usize], state); + std::hash::Hash::hash(&self.length, state) } } diff --git a/services/xous-names/src/main.rs b/services/xous-names/src/main.rs index 003aec3be..8e7740cf1 100644 --- a/services/xous-names/src/main.rs +++ b/services/xous-names/src/main.rs @@ -1,8 +1,6 @@ #![cfg_attr(target_os = "none", no_std)] #![cfg_attr(target_os = "none", no_main)] -extern crate hash32_derive; - mod api; use api::*; @@ -12,6 +10,8 @@ use xous_ipc::{String, Buffer}; use log::{error, info}; +use std::collections::HashMap; + #[cfg(any(target_os = "none", target_os = "xous"))] mod implementation { use utralib::generated::*; @@ -75,7 +75,6 @@ and we can use heap-allocated Rust primitives... */ #[derive(Debug, Copy, Clone)] struct Connection { - pub name: XousServerName, pub sid: xous::SID, pub current_conns: u32, // number of unauthenticated (inherentely trusted) connections pub max_conns: Option, // if None, unlimited connections allowed @@ -84,105 +83,85 @@ struct Connection { pub token: Option<[u32; 4]>, // a random number that must be presented to allow for disconnection for single-connection servers } #[derive(Debug)] -struct SlowMap { - pub map: [Option; 128], +struct CheckedHashMap { + pub map: HashMap::, } -impl SlowMap { +impl CheckedHashMap { pub fn new() -> Self { - SlowMap { - map: [None; 128], + CheckedHashMap { + map: HashMap::new(), } } pub fn insert(&mut self, name: XousServerName, sid: xous::SID, max_conns: Option) -> Result<(), xous::Error> { - let mut ok = false; let token = if max_conns == Some(1) { // for the special case of 1-connection servers, provision a one-time use token for disconnects Some(xous::create_server_id().expect("couldn't create token").to_array()) } else { None }; - for entry in self.map.iter_mut() { - if entry.is_none() { - *entry = Some(Connection { - name, - sid, - current_conns: 0, - max_conns, - allow_authenticate: false, // for now, we don't support authenticated connections - auth_conns: 0, - token, - }); - ok = true; + self.map.insert(name, Connection { + sid, + current_conns: 0, + max_conns, + allow_authenticate: false, // for now, we don't support authenticated connections + auth_conns: 0, + token, + }); + Ok(()) + } + pub fn remove(&mut self, sid: xous::SID) -> Option { + // remove is expensive, because we have to do a full search for the sid, which is not our usual key + // however, for security reasons, you have to let us know your sid (which is a secret) in order to delete + // your entry; whereas the human-readable name is not at all a secret + let mut removed_name: Option = None; + for (name, mapping) in self.map.iter_mut() { + if mapping.sid == sid { + removed_name = Some(*name); break; } } - if !ok { - Err(xous::Error::OutOfMemory) - } else { - Ok(()) + if let Some(name) = removed_name { + self.map.remove(&name); } - } - pub fn remove(&mut self, sid: xous::SID) -> Option { - let mut name: Option = None; - for entry in self.map.iter_mut() { - if let Some(mapping) = entry { - if mapping.sid == sid { - name = Some(mapping.name); - *entry = None; - break; - } - } - } - name + + removed_name } pub fn contains_key(&self, name: &XousServerName) -> bool { - for maybe_entry in self.map.iter() { - if let Some(entry) = maybe_entry { - if entry.name == *name { - return true - } - } - } - return false + self.map.contains_key(name) } pub fn connect(&mut self, name: &XousServerName) -> (Option<&xous::SID>, Option<[u32; 4]>) { - for maybe_entry in self.map.iter_mut() { - if let Some(entry) = maybe_entry { - if *name == entry.name { - if Some(1) == entry.max_conns { - // single-connection case - if entry.current_conns < 1 { - (*entry).current_conns = 1; - return (Some(&entry.sid), entry.token) - } else { - return (None, None) - } - } - if let Some(max) = entry.max_conns { - if entry.current_conns < max { - (*entry).current_conns += 1; - return (Some(&entry.sid), None); - } else { - return (None, None); - } - } else { - // unlimited connections allowed - (*entry).current_conns += 1; - return (Some(&entry.sid), None); - } + let maybe_entry = self.map.get_mut(name); + if let Some(entry) = maybe_entry { + if Some(1) == entry.max_conns { + // single-connection case + if entry.current_conns < 1 { + (*entry).current_conns = 1; + return (Some(&entry.sid), entry.token) + } else { + return (None, None) } } + if let Some(max) = entry.max_conns { + if entry.current_conns < max { + (*entry).current_conns += 1; + return (Some(&entry.sid), None); + } else { + return (None, None); + } + } else { + // unlimited connections allowed + (*entry).current_conns += 1; + return (Some(&entry.sid), None); + } } (None, None) } pub fn trusted_init_done(&self) -> bool { let mut trusted_done = true; - for maybe_entry in self.map.iter() { - if let Some(entry) = maybe_entry { - if let Some(max) = entry.max_conns { - if max != entry.current_conns { - trusted_done = false; - } + for (_name, entry) in self.map.iter() { + if let Some(max) = entry.max_conns { + if max != entry.current_conns { + trusted_done = false; } } } @@ -195,14 +174,12 @@ impl SlowMap { // the caller can never talk to the server again. #[allow(dead_code)] pub fn disconnect(&mut self, sid: xous::SID) -> Option { - for entry in self.map.iter_mut() { - if let Some(mapping) = entry { - if mapping.sid == sid { - if mapping.current_conns > 0 { - mapping.current_conns -= 1; - } - return Some(mapping.name); + for (name, mapping) in self.map.iter_mut() { + if mapping.sid == sid { + if mapping.current_conns > 0 { + mapping.current_conns -= 1; } + return Some(*name); } } None @@ -210,15 +187,13 @@ impl SlowMap { // this is a safer version of disconnect. we track servers that allow exactly one connection at a time // and give them a one-time-use token that a connector can use to disconnect. pub fn disconnect_with_token(&mut self, name: &XousServerName, token: [u32; 4]) -> bool { - for maybe_entry in self.map.iter_mut() { - if let Some(entry) = maybe_entry { - if let Some(old_token) = entry.token { - if (*name == entry.name) && (token == old_token) && (entry.current_conns == 1) { - (*entry).current_conns = 0; - // generate the token -- we should never re-use these! - (*entry).token = Some(xous::create_server_id().expect("couldn't create token").to_array()); - return true - } + if let Some(entry) = self.map.get_mut(name) { + if let Some(old_token) = entry.token { + if (token == old_token) && (entry.current_conns == 1) { + (*entry).current_conns = 0; + // generate the token -- we should never re-use these! + (*entry).token = Some(xous::create_server_id().expect("couldn't create token").to_array()); + return true } } } @@ -240,7 +215,7 @@ fn xmain() -> ! { // this limits the number of available servers to be requested to 128...! //let mut name_table = FnvIndexMap::::new(); - let mut name_table = SlowMap::new(); + let mut name_table = CheckedHashMap::new(); info!("started"); loop { @@ -308,10 +283,8 @@ fn xmain() -> ! { "Can't find request '{}' in table, dumping table:", name ); - for maybe_conn in name_table.map.iter() { - if let Some(connection) = maybe_conn { - log::debug!("{:?}", connection); - } + for (_name, conn) in name_table.map.iter() { + log::debug!("{:?}", conn); } d11ctimeout.hosted_delay(); response = api::Return::Failure @@ -322,10 +295,8 @@ fn xmain() -> ! { "Can't find request '{}' in table, dumping table:", name ); - for maybe_conn in name_table.map.iter() { - if let Some(connection) = maybe_conn { - log::debug!("{:?}", connection); - } + for (_name, conn) in name_table.map.iter() { + log::debug!("{:?}", conn); } // no authenticate remedy currently supported, but we'd put that code somewhere around here eventually. let (c1, c2, c3, c4) = xous::create_server_id().unwrap().to_u32();