From 925d53f646a30be4fca2b5c840c44e69142987b9 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 1 Feb 2022 18:54:07 +0800 Subject: [PATCH] TCP listener is "working" Good enough so that if you type `net server` and you hit the IP address of the device, it'll respond with something like Hello from Precursor! I have been up for 86087 ms. There's still all kinds of bugs, I'm sure. But ... I'm also not sure /why/ you would want a web server on Precursor. This is more just to make sure that the TCP stack isn't totally broken. The "web server" as it may be is quite broken and doesn't parse any headers and probably sends a bogus amount of data because I can't figure out if the content length includes some new lines or not. But really, this just isn't in scope. Works with Chrome, Firefox shows the status page and then says "connection reset" and hides all the information. Whatever, browser compatibility is well beyond my pay grade! --- services/net/Cargo.toml | 7 +- services/net/src/api.rs | 6 + services/net/src/api/tcp.rs | 16 ++ services/net/src/main.rs | 158 +++++++++++- services/net/src/protocols.rs | 4 +- services/net/src/protocols/tcp_listener.rs | 279 +++++++++++++++++++-- services/net/src/protocols/tcp_stream.rs | 126 +++++++--- services/shellchat/src/cmds/net_cmd.rs | 89 ++++++- services/status/locales/i18n.json | 2 +- services/ticktimer-server/src/main.rs | 2 +- 10 files changed, 617 insertions(+), 72 deletions(-) diff --git a/services/net/Cargo.toml b/services/net/Cargo.toml index 025ad483e..9610ba0b7 100644 --- a/services/net/Cargo.toml +++ b/services/net/Cargo.toml @@ -30,9 +30,10 @@ locales = {path = "../../locales"} pddb = {path = "../pddb"} [dependencies.smoltcp] -# ugh. have to reference `main` directly because they haven't pushed a new tag in months, and none -# of the worked examples match the actual documentation now. -# pin at a commit, at least, so we aren't building on shifting sand +# ugh. we startd pulling in smoltcp as they were just fixing a bunch of bugs and getting ready for a 0.8.0 release. +# this is the last commit that doesn't break things for Xous. They pulled in some random crate that isn't compatible +# with Xous in the commit after this -- we need to look into this and maybe send a PR so we can try to work with +# the latest smoltcp. But later...let's just get things even working first with what we have. git = "https://github.com/smoltcp-rs/smoltcp.git" rev = "35e833e33dfd3e4efc3eb7d5de06bec17c54b011" #path = "../../../smoltcp" diff --git a/services/net/src/api.rs b/services/net/src/api.rs index 979e6cf9e..04d1d7a8f 100644 --- a/services/net/src/api.rs +++ b/services/net/src/api.rs @@ -41,6 +41,8 @@ pub(crate) enum Opcode { TcpTx, TcpClose, TcpManage, + TcpListen, + TcpManageListener, // The DNS server can hook the Net crate for notifications on config updates /// Adds an Ipv4 as a DNS server @@ -290,6 +292,7 @@ impl fmt::Debug for NetIpAddr { /// spaces: this is not the right object for passing messages from a remote server /// in a potentially hostile foreign process into your local memory space. /// See XousPrivateScalarHook for that function. +#[derive(Debug)] pub(crate) struct XousScalarEndpoint { cid: Option, op: Option, @@ -304,6 +307,9 @@ impl XousScalarEndpoint { args: [None; 4] } } + pub(crate) fn get(&self) -> (Option, Option, [Option; 4]) { + (self.cid, self.op, self.args) + } pub(crate) fn set(&mut self, cid: xous::CID, op: usize, args: [Option; 4]) { self.cid = Some(cid); self.op = Some(op); diff --git a/services/net/src/api/tcp.rs b/services/net/src/api/tcp.rs index 88559263d..b6e55f918 100644 --- a/services/net/src/api/tcp.rs +++ b/services/net/src/api/tcp.rs @@ -17,6 +17,20 @@ pub(crate) struct NetTcpManage { pub(crate) mgmt_code: Option, } +#[derive(Debug, Archive, Serialize, Deserialize, Copy, Clone)] +pub(crate) struct NetTcpListen { + pub(crate) cb_sid: [u32; 4], + pub(crate) local_port: u16, + pub(crate) result: Option, +} + +#[derive(Debug, Archive, Serialize, Deserialize, Copy, Clone)] +pub(crate) struct NetTcpListenCallback { + pub(crate) ip_addr: NetIpAddr, + pub(crate) remote_port: u16, + pub(crate) local_port: u16, +} + #[derive(Debug, Archive, Serialize, Deserialize, Copy, Clone, PartialEq, Eq)] pub(crate) enum TcpMgmtCode { SetRxShutdown, @@ -26,11 +40,13 @@ pub(crate) enum TcpMgmtCode { GetTtl(u32), ErrorCheck(NetMemResponse), Flush(bool), + CloseListener, } #[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] pub(crate) enum NetTcpCallback { RxData, + ListenerActive, Drop, } diff --git a/services/net/src/main.rs b/services/net/src/main.rs index 34d77a20a..22e5c1d99 100644 --- a/services/net/src/main.rs +++ b/services/net/src/main.rs @@ -85,6 +85,7 @@ pub struct TcpConnection { remote_port: u16, local_port: u16, } +#[derive(Copy, Clone, Debug)] pub struct TcpState { handle: SocketHandle, cid: CID, @@ -178,6 +179,7 @@ fn xmain() -> ! { // tcp storage let mut tcp_handles = HashMap::::new(); + let mut tcp_listeners = HashMap::>::new(); // other link storage let neighbor_cache = NeighborCache::new(BTreeMap::new()); @@ -408,7 +410,7 @@ fn xmain() -> ! { let remote_port = tcpspec.remote_port; // initiates a new connection to a remote server consisting of an (Address:Port) tuple. - // multiple connections can exist to a server, and they are furhte differentiated by the return port + // multiple connections can exist to a server, and they are further differentiated by the return port let tcp_rx_buffer = TcpSocketBuffer::new(vec![0; TCP_BUFFER_SIZE]); let tcp_tx_buffer = TcpSocketBuffer::new(vec![0; TCP_BUFFER_SIZE]); let mut tcp_socket = TcpSocket::new(tcp_rx_buffer, tcp_tx_buffer); @@ -519,6 +521,10 @@ fn xmain() -> ! { tcpspec.mgmt_code = Some(TcpMgmtCode::Flush(false)); } } + TcpMgmtCode::CloseListener => { + // this is used for listener management, should never be sent to this routine + tcpspec.result = Some(NetMemResponse::LibraryError); + } } } else { tcpspec.result = Some(NetMemResponse::Invalid); @@ -572,6 +578,7 @@ fn xmain() -> ! { }; if let Some(tcp_state) = tcp_handles.remove(&connection) { sockets.get::(tcp_state.handle).close(); + sockets.remove(tcp_state.handle); tcpspec.result = Some(NetMemResponse::Ok); } else { tcpspec.result = Some(NetMemResponse::Invalid); @@ -581,6 +588,106 @@ fn xmain() -> ! { } buf.replace(tcpspec).unwrap(); } + Some(Opcode::TcpListen) => { + let mut buf = unsafe{Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap())}; + let mut tcpspec = buf.to_original::().unwrap(); + + let tcp_rx_buffer = TcpSocketBuffer::new(vec![0; TCP_BUFFER_SIZE]); + let tcp_tx_buffer = TcpSocketBuffer::new(vec![0; TCP_BUFFER_SIZE]); + let mut tcp_socket = TcpSocket::new(tcp_rx_buffer, tcp_tx_buffer); + match tcp_socket.listen(tcpspec.local_port) { + Ok(_) => { + if let Some(list) = tcp_listeners.get(&tcpspec.local_port) { + if list.len() > 0 { // empty check necessary because an empty vector is left if a socket went from listening->active, or was dropped + // guarantee that all TTLs are same even if we're inserting a new socket + let ttl = sockets.get::(list[0].handle).hop_limit(); + tcp_socket.set_hop_limit(ttl); + } + } + let handle = sockets.add(tcp_socket); + let sid = tcpspec.cb_sid; + let cid = xous::connect(SID::from_array(sid)).unwrap(); + let tcp_cb_state = TcpState { + handle, + cid, + shutdown_rx: false, + }; + if let Some(list) = tcp_listeners.get_mut(&tcpspec.local_port) { + list.push(tcp_cb_state); + } else { + tcp_listeners.insert(tcpspec.local_port, vec![tcp_cb_state]); + } + log::info!("creating listener on port {}", tcpspec.local_port); + tcpspec.result = Some(NetMemResponse::Ok); + } + Err(e) => { + match e { + smoltcp::Error::Illegal => { + tcpspec.result = Some(NetMemResponse::SocketInUse); + } + smoltcp::Error::Unaddressable => { + tcpspec.result = Some(NetMemResponse::Invalid); + } + _ => { + tcpspec.result = Some(NetMemResponse::LibraryError); + } + } + } + } + buf.replace(tcpspec).unwrap(); + }, + Some(Opcode::TcpManageListener) => { + let mut buf = unsafe{Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap())}; + let mut tcpspec = buf.to_original::().unwrap(); + match tcpspec.mgmt_code { + Some(TcpMgmtCode::CloseListener) => { + if let Some(listener) = tcp_listeners.get_mut(&tcpspec.local_port.unwrap()) { + if let Some(tcp_state) = listener.pop() { + sockets.get::(tcp_state.handle).close(); + log::info!("closing one listener on port {:?}", tcpspec.local_port); + sockets.remove(tcp_state.handle); + tcpspec.result = Some(NetMemResponse::Ok); + // this may leave an empty vector in the tcp_listeners structure, but I think that's OK + } + } else { + tcpspec.result = Some(NetMemResponse::Invalid); + } + } + Some(TcpMgmtCode::SetTtl(mut ttl)) => { + if let Some(listener_vec) = tcp_listeners.get_mut(&tcpspec.local_port.unwrap()) { + for listener in listener_vec.iter_mut() { + let mut socket = sockets.get::(listener.handle); + if ttl > 255 { ttl = 255; } + if ttl > 0 { + socket.set_hop_limit(Some(ttl as u8)); + } else { + socket.set_hop_limit(None); + } + } + tcpspec.result = Some(NetMemResponse::Ok); + } else { + tcpspec.result = Some(NetMemResponse::Invalid); + } + } + Some(TcpMgmtCode::GetTtl(_)) => { + if let Some(listener) = tcp_listeners.get(&tcpspec.local_port.unwrap()) { + if listener.len() > 0 { + // all listeners "should" have an identical setting, so, just return the setting of the 0th one + let socket = sockets.get::(listener[0].handle); + let value = socket.hop_limit().unwrap_or(64); // because that's what smoltcp does + tcpspec.mgmt_code = Some(TcpMgmtCode::GetTtl(value as u32)); + tcpspec.result = Some(NetMemResponse::Ok); + } else { + tcpspec.result = Some(NetMemResponse::Invalid); + } + } else { + tcpspec.result = Some(NetMemResponse::Invalid); + } + } + _ => tcpspec.result = Some(NetMemResponse::LibraryError), + } + buf.replace(tcpspec).unwrap(); + } Some(Opcode::UdpBind) => { let mut buf = unsafe{Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap())}; let udpspec = buf.to_original::().unwrap(); @@ -660,6 +767,7 @@ fn xmain() -> ! { // if the clone map is nil, close the socket, we're done None => { sockets.get::(udpstate.handle).close(); + sockets.remove(udpstate.handle); buf.replace(NetMemResponse::Ok).unwrap(); } // if the clone map has entries, promote an arbitrary map entry to the primary handle @@ -668,6 +776,7 @@ fn xmain() -> ! { // removing SIDs doesn't remove the map, so it's possible to have an empty mapping. Get rid of it, and we're done. udp_clones.remove(&udpspec.port); sockets.get::(udpstate.handle).close(); + sockets.remove(udpstate.handle); buf.replace(NetMemResponse::Ok).unwrap(); } else { // take an arbitrary key, re-insert it into the handles map. @@ -946,6 +1055,53 @@ fn xmain() -> ! { } } + // this block handles TCP listeners + // There is no lock on sending Rx messages to the listener as it is being transformed + // because all messages come from *this thread* and by definition it is not re-entrant. + // If you end up splitting the Tcp Rx task and the Tcp Listener tasks into separate threads, + // Then you must worry about the possibility of a race condition between the transition of a + // TcpListener to a regular TcpStream client (that is, an Rx packet forwarded before the + // TcpStream is fully built). + if tcp_listeners.len() > 0 { // skip the whole chunk if we don't have any tcp listeners + for (&local_port, tcp_state_vec) in tcp_listeners.iter_mut() { + let mut remove_indices = Vec::::new(); + for (index, tcp_state) in tcp_state_vec.iter().enumerate() { + let socket = sockets.get::(tcp_state.handle); + if socket.is_active() { + log::info!("Promoting a listener on port {:?} to a stream", socket.local_endpoint()); + // 1. promote this socket to a TCP rx socket (this creates a double-entry that is cleaned up in step 3) + let connection = TcpConnection { + remote: socket.remote_endpoint().addr, + remote_port: socket.remote_endpoint().port, + local_port, + }; + tcp_handles.insert(connection, *tcp_state); + + // 2. notify the listener + let note = NetTcpListenCallback { + ip_addr: NetIpAddr::from(socket.remote_endpoint().addr), + remote_port: socket.remote_endpoint().port, + local_port, + }; + log::info!("Listener active, notification sent to {}: {:?}", tcp_state.cid, note); + let buf = Buffer::into_buf(note).expect("can't transform memory message"); + buf.send(tcp_state.cid, NetTcpCallback::ListenerActive.to_u32().unwrap()).expect("can't inform callback of active status"); + remove_indices.push(index); + } + } + // 3. now cleanup and remove the ports from the listeners status + remove_indices.sort(); // this should be in ascending order, but let's do this just to be sure. + remove_indices.reverse(); + for index in remove_indices { + tcp_state_vec.remove(index); + } + // 4. At this point, the Listener is no more. However, the caller side will "renew" the + // listener if the TcpListener is being used in an `incoming` loop. + + // 5. Side note: an empty vector could be left in the tcp_listeners array. 🔥This is fine🔥. + } + } + // this block handles UDP { for (port, udpstate) in udp_handles.iter() { diff --git a/services/net/src/protocols.rs b/services/net/src/protocols.rs index 2ad760b75..a641c4ac5 100644 --- a/services/net/src/protocols.rs +++ b/services/net/src/protocols.rs @@ -5,4 +5,6 @@ pub use dns::*; pub mod ping; pub use ping::*; pub mod tcp_stream; -pub use tcp_stream::*; \ No newline at end of file +pub use tcp_stream::*; +pub mod tcp_listener; +pub use tcp_listener::*; diff --git a/services/net/src/protocols/tcp_listener.rs b/services/net/src/protocols/tcp_listener.rs index bc38c5de5..1da707d33 100644 --- a/services/net/src/protocols/tcp_listener.rs +++ b/services/net/src/protocols/tcp_listener.rs @@ -1,52 +1,281 @@ +use std::net::{IpAddr, Ipv4Addr, SocketAddr, ToSocketAddrs}; +use std::unimplemented; +use std::io; +use std::io::{Error, ErrorKind, Result}; +use std::sync::{Arc, Mutex}; +use std::sync::atomic::{AtomicU32, Ordering}; +use std::fmt; +use std::collections::VecDeque; -// this is if we need to be a server. implement this second. -pub struct TcpListener(!); +use xous::{Message, SID, CID}; +use xous_ipc::Buffer; +use crate::TcpStream; +use crate::NetConn; +use crate::api::*; +use num_traits::*; + +const LISTENER_POLL_INTERVAL_MS: usize = 250; + +pub struct TcpListener { + net: NetConn, + cb_sid: SID, + ticktimer: ticktimer_server::Ticktimer, + xns: xous_names::XousNames, + refcount: Arc, + port: u16, + nonblocking: bool, + original_notify: XousScalarEndpoint, + + // reserved for when the responder thread is "promoted" to active status + rx_buf: Arc::>>, + /// xous-specific feature that allows for more efficent Rx than blocking. Caller must set_scalar_notification() to use it. + rx_notify: Arc>, + listener_info: Arc>>, +} impl TcpListener { - pub fn bind(_: io::Result<&SocketAddr>) -> io::Result { - unsupported() + pub fn bind(maybe_socket: io::Result<&SocketAddr>) -> io::Result { + TcpListener::bind_inner(maybe_socket) } - pub fn socket_addr(&self) -> io::Result { - self.0 + pub fn bind_xous(addr: A) -> Result { + match addr.to_socket_addrs() { + Ok(socks) => { + match socks.into_iter().next() { + Some(socket_addr) => { + TcpListener::bind_inner( + Ok(&socket_addr), + ) + } + _ => Err(Error::new(ErrorKind::InvalidInput, "IP address invalid")) + } + } + _ => Err(Error::new(ErrorKind::InvalidInput, "IP address invalid")) + } } - pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> { - self.0 + pub fn bind_inner(maybe_socket: io::Result<&SocketAddr>) -> io::Result { + let socket = maybe_socket?; + let xns = xous_names::XousNames::new().unwrap(); + let net = NetConn::new(&xns).unwrap(); + let cb_sid = xous::create_server().unwrap(); + let ticktimer = ticktimer_server::Ticktimer::new().unwrap(); + + let rx_buf = Arc::new(Mutex::new(VecDeque::::new())); + rx_buf.lock().unwrap().reserve(TCP_BUFFER_SIZE); + let notify = Arc::new(Mutex::new(XousScalarEndpoint::new())); + let listener_info = Arc::new(Mutex::new(None::)); + + let _handle = crate::tcp_rx_thread( + cb_sid.clone(), + Arc::clone(&rx_buf), + Arc::clone(¬ify), + // this is not used by TcpStream + Arc::clone(&listener_info) + ); + let request = NetTcpListen { + cb_sid: cb_sid.to_array(), + local_port: socket.port(), + result: None, + }; + log::info!("Attempting to bind a listener to port {}", socket.port()); + let mut buf = Buffer::into_buf(request) + .or(Err(Error::new(ErrorKind::Other, "can't register with Net server")))?; + buf.lend_mut(net.conn(), Opcode::TcpListen.to_u32().unwrap()) + .or(Err(Error::new(ErrorKind::Other, "can't register with Net server")))?; + let ret = buf.to_original::().unwrap(); + log::info!("Got result: {:?}", ret.result); + match ret.result { + Some(NetMemResponse::Ok) => { + Ok(TcpListener { + net, + cb_sid, + ticktimer, + xns, + refcount: Arc::new(AtomicU32::new(1)), // one instance, that's me! + port: socket.port(), + rx_buf, + rx_notify: notify, + listener_info, + nonblocking: false, + original_notify: XousScalarEndpoint::new(), + }) + } + _ => { + Err(Error::new(ErrorKind::Other, "can't create Listener with Net server")) + } + } + } + pub fn set_scalar_notification(&mut self, cid: CID, op: usize, args: [Option; 4]) { + self.original_notify.set(cid, op, args); + self.rx_notify.lock().unwrap().set(cid, op, args); + } + pub fn clear_scalar_notification(&mut self) { + self.original_notify.clear(); + self.rx_notify.lock().unwrap().clear(); } - pub fn duplicate(&self) -> io::Result { - self.0 + pub fn accept(&mut self) -> io::Result<(TcpStream, SocketAddr)> { + let mut prints = 0; + loop { + let accepted = if let Some(info) = self.listener_info.lock().unwrap().take() { + // 1. Create a new TcpStream object using our existing handles + let remote = SocketAddr::new(IpAddr::from(info.ip_addr), info.remote_port); + log::info!("building stream to {:?}", remote); + let stream = TcpStream::build_from_listener( + NetConn::new(&self.xns).unwrap(), + self.cb_sid, + remote, + info.local_port, + std::mem::replace(&mut self.rx_buf, Arc::new(Mutex::new(VecDeque::::new()))), + // we keep our notifier for ourself, create a new one for the passed-on thread + Arc::new(Mutex::new(XousScalarEndpoint::new())), + ); + Some((stream, remote)) + } else { + if self.nonblocking { + return Err(Error::new(ErrorKind::WouldBlock, "accept would block")); + } + None + }; + if let Some((stream, remote)) = accepted { + log::info!("replenishing listener, notifier: {:?}", self.rx_notify); + // 2. Replenish our Listener objects and pass a TcpListen message on to the Net manager to repeat the cycle + self.listener_info = Arc::new(Mutex::new(None::)); + self.cb_sid = xous::create_server().unwrap(); + // create a new notify arc/mutex, then copy the master notification settings there + self.rx_notify = Arc::new(Mutex::new(XousScalarEndpoint::new())); + if self.original_notify.is_set() { + let (c, o, a) = self.original_notify.get(); + self.rx_notify.lock().unwrap().set(c.unwrap(), o.unwrap(), a); + } + let _handle = crate::tcp_rx_thread( + self.cb_sid.clone(), + Arc::clone(&self.rx_buf), + // we're re-using the notifier object...is this going to be a problem? + Arc::clone(&self.rx_notify), + // this is not used by TcpStream + Arc::clone(&self.listener_info) + ); + let request = NetTcpListen { + cb_sid: self.cb_sid.to_array(), + local_port: stream.socket_addr().unwrap().port(), + result: None, + }; + let mut buf = Buffer::into_buf(request) + .or(Err(Error::new(ErrorKind::Other, "can't register with Net server")))?; + buf.lend_mut(self.net.conn(), Opcode::TcpListen.to_u32().unwrap()) + .or(Err(Error::new(ErrorKind::Other, "can't register with Net server")))?; + let ret = buf.to_original::().unwrap(); + return + match ret.result { + Some(NetMemResponse::Ok) => Ok((stream, remote)), + _ => Err(Error::new(ErrorKind::Other, "can't renew Listener with Net server")) + } + } + if prints < 10 { + log::info!("accept polling..."); + prints += 1; + } + self.ticktimer.sleep_ms(LISTENER_POLL_INTERVAL_MS).unwrap(); + } } - pub fn set_ttl(&self, _: u32) -> io::Result<()> { - self.0 + pub fn local_addr(&self) -> io::Result { + Ok(SocketAddr::new( + IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), + self.port + )) + } + + pub fn set_ttl(&self, ttl: u32) -> io::Result<()> { + self.tcp_manage(TcpMgmtCode::SetTtl(ttl)).map(|_| ()) } pub fn ttl(&self) -> io::Result { - self.0 - } - - pub fn set_only_v6(&self, _: bool) -> io::Result<()> { - self.0 - } - - pub fn only_v6(&self) -> io::Result { - self.0 + match self.tcp_manage(TcpMgmtCode::GetTtl(0)) { + Ok(TcpMgmtCode::GetTtl(value)) => Ok(value), + _ => Err(Error::new(ErrorKind::Other, "Internal error")), + } } + /// I'm not really sure what this does on Xous, as we don't have a SO_ERROR value in our OS. + /// Just returning Ok(None) for now. pub fn take_error(&self) -> io::Result> { - self.0 + Ok(None) } - pub fn set_nonblocking(&self, _: bool) -> io::Result<()> { - self.0 + pub fn set_nonblocking(&mut self, setting: bool) -> io::Result<()> { + self.nonblocking = setting; + Ok(()) + } + + pub fn duplicate(&self) -> io::Result { + Err(Error::new(ErrorKind::Other, "Xous does not support cloned listeners")) + } + + fn tcp_manage(&self, code: TcpMgmtCode) -> io::Result { + // only local_port is needed + let request = NetTcpManage { + cb_sid: self.cb_sid.to_array(), + ip_addr: NetIpAddr::Ipv4([127, 0, 0, 1]), + remote_port: 0, + local_port: Some(self.port), + timeout_ms: None, + keepalive_ms: None, + result: None, + mgmt_code: Some(code), + }; + let mut buf = Buffer::into_buf(request) + .or(Err(Error::new(ErrorKind::Other, "can't manage TCP")))?; + buf.lend_mut(self.net.conn(), Opcode::TcpManageListener.to_u32().unwrap()) + .or(Err(Error::new(ErrorKind::Other, "can't manage TCP")))?; + let ret = buf.to_original::().unwrap(); + if let Some(code) = ret.mgmt_code { + Ok(code) + } else { + Err(Error::new(ErrorKind::Other, "Internal error")) + } } } impl fmt::Debug for TcpListener { fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { - self.0 + unimplemented!() } } +impl Drop for TcpListener { + fn drop(&mut self) { + if self.refcount.fetch_sub(1, Ordering::SeqCst) == 1 { + // the only thing we really need from the management structure for this call is the local port... + let request = NetTcpManage { + ip_addr: NetIpAddr::from(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1))), + remote_port: 0, // bogus port + local_port: Some(self.port), + cb_sid: self.cb_sid.to_array(), + timeout_ms: None, + keepalive_ms: None, + result: None, + mgmt_code: Some(TcpMgmtCode::CloseListener), + }; + let mut buf = Buffer::into_buf(request).expect("can't unregister with Net server"); + buf.lend_mut(self.net.conn(), Opcode::TcpManageListener.to_u32().unwrap()).expect("can't unregister with Net server"); + match buf.to_original().unwrap() { + NetMemResponse::Ok => { + let drop_cid = xous::connect(self.cb_sid).unwrap(); + xous::send_message( + drop_cid, + Message::new_blocking_scalar(NetTcpCallback::Drop.to_usize().unwrap(), 0, 0, 0, 0) + ).expect("couldn't send Drop to our repsonding server"); + unsafe{xous::disconnect(drop_cid).ok()}; // should be safe because we're the only connection and the previous was a blocking scalar + }, + _ => { + panic!("Couldn't unregister with net server"); + } + } + // now we can detroy the server id of the responder thread + xous::destroy_server(self.cb_sid).ok(); + } + } +} \ No newline at end of file diff --git a/services/net/src/protocols/tcp_stream.rs b/services/net/src/protocols/tcp_stream.rs index fa6b5f7b4..b599ee1b4 100644 --- a/services/net/src/protocols/tcp_stream.rs +++ b/services/net/src/protocols/tcp_stream.rs @@ -13,7 +13,6 @@ use xous::{CID, Message, SID, msg_blocking_scalar_unpack}; use xous_ipc::Buffer; use crate::NetConn; use crate::api::*; -//use crate::api::udp::*; use num_traits::*; use std::net::Shutdown; @@ -45,6 +44,33 @@ pub struct TcpStream { } impl TcpStream { + /// This is routine that allows a TcpListener to create a TcpStream object. The following must be observed: + /// 1. The `cb_sid`, `rx_buf` and `rx_notify` are the memory locations mapped into the already-allocated Rx thread + /// 2. These fields are not to be re-used in the Listener object that succeeds the current one + pub(crate) fn build_from_listener( + net: NetConn, + cb_sid: SID, + remote: SocketAddr, + local_port: u16, + rx_buf: Arc::>>, + rx_notify: Arc>, + ) -> TcpStream { + TcpStream { + net, + cb_sid, + socket_addr: remote, + local_port, + rx_buf, + rx_notify, + read_timeout: None, + write_timeout: None, + ticktimer: ticktimer_server::Ticktimer::new().unwrap(), + nonblocking: false, + rx_shutdown: false, + tx_shutdown: false, + rx_refcount: Arc::new(AtomicU32::new(1)), + } + } pub fn connect(maybe_socket: io::Result<&SocketAddr>) -> io::Result { TcpStream::connect_inner(maybe_socket, None, None) } @@ -85,38 +111,13 @@ impl TcpStream { rx_buf.lock().unwrap().reserve(TCP_BUFFER_SIZE); let notify = Arc::new(Mutex::new(XousScalarEndpoint::new())); - let _handle = thread::spawn({ - let cb_sid_clone = cb_sid.clone(); - let rx_buf = Arc::clone(&rx_buf); - let notify = Arc::clone(¬ify); - move || { - loop { - let msg = xous::receive_message(cb_sid_clone).unwrap(); - match FromPrimitive::from_usize(msg.body.id()) { - Some(NetTcpCallback::RxData) => { - let buffer = unsafe {Buffer::from_memory_message(msg.body.memory_message().unwrap())}; - let incoming = buffer.as_flat::().unwrap(); - log::trace!("rx data({})", incoming.len); - { // grab the lock once for better efficiency - let mut rx_locked = rx_buf.lock().unwrap(); - for &d in incoming.data[..incoming.len as usize].iter() { - rx_locked.push_back(d); - } - } - notify.lock().unwrap().notify(); // this will only notify if a destination has been set - } - Some(NetTcpCallback::Drop) => msg_blocking_scalar_unpack!(msg, _, _, _, _, { - log::debug!("Drop received, exiting Tcp handler"); - xous::return_scalar(msg.sender, 1).unwrap(); - break; - }), - None => { - log::error!("got unknown message type on Tcp callback: {:?}", msg); - } - } - } - } - }); + let _handle = tcp_rx_thread( + cb_sid.clone(), + Arc::clone(&rx_buf), + Arc::clone(¬ify), + // this is not used by TcpStream + Arc::new(Mutex::new(None::)), + ); let request = NetTcpManage { cb_sid: cb_sid.to_array(), @@ -321,8 +322,17 @@ impl TcpStream { Ok(cloned_stream) } - pub fn peek(&self, _: &mut [u8]) -> io::Result { - unimplemented!() + pub fn peek(&self, buf: &mut [u8]) -> io::Result { + if self.rx_buf.lock().unwrap().len() == 0 { + return Ok(0) + } else { + let rx_buf = self.rx_buf.lock().unwrap(); + let readlen = rx_buf.len().min(buf.len()); + for (&src, dst) in rx_buf.range(..readlen).zip(buf.iter_mut()) { + *dst = src; + } + return Ok(readlen) + } } } @@ -504,4 +514,50 @@ impl Drop for TcpStream { xous::destroy_server(self.cb_sid).ok(); } } +} + +pub(crate) fn tcp_rx_thread( + cb_sid_clone: SID, + rx_buf: Arc::>>, + notify: Arc::>, + listener: Arc::>>, +) -> std::thread::JoinHandle<()> { + thread::spawn({ + move || { + loop { + let msg = xous::receive_message(cb_sid_clone).unwrap(); + match FromPrimitive::from_usize(msg.body.id()) { + Some(NetTcpCallback::RxData) => { + let buffer = unsafe {Buffer::from_memory_message(msg.body.memory_message().unwrap())}; + let incoming = buffer.as_flat::().unwrap(); + log::trace!("rx data({})", incoming.len); + { // grab the lock once for better efficiency + let mut rx_locked = rx_buf.lock().unwrap(); + for &d in incoming.data[..incoming.len as usize].iter() { + rx_locked.push_back(d); + } + } + notify.lock().unwrap().notify(); // this will only notify if a destination has been set + } + Some(NetTcpCallback::ListenerActive) => { + let buffer = unsafe {Buffer::from_memory_message(msg.body.memory_message().unwrap())}; + let incoming = buffer.to_original::().unwrap(); + // notify the caller that it should pick up its state + log::info!("Setting the notifier structure from {:?} to Some({:?})", listener, incoming); + *listener.lock().unwrap() = Some(incoming); + log::info!("After: {:?}", listener); + notify.lock().unwrap().notify(); // this will only notify if a destination has been set + } + Some(NetTcpCallback::Drop) => msg_blocking_scalar_unpack!(msg, _, _, _, _, { + log::debug!("Drop received, exiting Tcp handler"); + xous::return_scalar(msg.sender, 1).unwrap(); + break; + }), + None => { + log::error!("got unknown message type on Tcp callback: {:?}", msg); + } + } + } + } + }) } \ No newline at end of file diff --git a/services/shellchat/src/cmds/net_cmd.rs b/services/shellchat/src/cmds/net_cmd.rs index 859b6168f..47f4eb5b8 100644 --- a/services/shellchat/src/cmds/net_cmd.rs +++ b/services/shellchat/src/cmds/net_cmd.rs @@ -4,10 +4,13 @@ use net::{Duration, XousServerId, NetPingCallback}; use xous::MessageEnvelope; use num_traits::*; use std::net::{SocketAddr, IpAddr}; +use std::io::Write; +use std::io::Read; pub struct NetCmd { udp: Option, udp_clone: Option, + tcp_listener: Option, callback_id: Option, callback_conn: u32, udp_count: u32, @@ -19,6 +22,7 @@ impl NetCmd { NetCmd { udp: None, udp_clone: None, + tcp_listener: None, callback_id: None, callback_conn: xns.request_connection_blocking(crate::SERVER_NAME_SHELLCHAT).unwrap(), udp_count: 0, @@ -30,8 +34,9 @@ impl NetCmd { #[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] pub(crate) enum NetCmdDispatch { - UdpTest1 = 0x1_0000, // we're muxing our own dispatch + ping dispatch, so we need a custom discriminant - UdpTest2 = 0x1_0001, + UdpTest1 = 0x1_0000, // we're muxing our own dispatch + ping dispatch, so we need a custom discriminant + UdpTest2 = 0x1_0001, + TcpServer = 0x2_0000, } pub const UDP_TEST_SIZE: usize = 64; @@ -54,8 +59,6 @@ impl<'a> ShellCmdApi<'a> for NetCmd { if let Some(sub_cmd) = tokens.next() { match sub_cmd { "tcpget" => { - use std::io::Write; - use std::io::Read; // note: to keep shellchat lightweight, we do a very minimal parsing of the URL. We assume it always has // a form such as: // bunniefoo.com./bunnie/test.txt @@ -111,6 +114,19 @@ impl<'a> ShellCmdApi<'a> for NetCmd { write!(ret, "Usage: tcpget bunniefoo.com/bunnie/test.txt").unwrap(); } } + "server" => { + let mut listener = net::TcpListener::bind_xous( + "127.0.0.1:80" + ).unwrap(); + listener.set_scalar_notification( + self.callback_conn, + self.callback_id.unwrap() as usize, + [Some(NetCmdDispatch::TcpServer.to_usize().unwrap()), None, None, None] + ); + // listener.set_nonblocking(true).unwrap(); // we shouldn't need to block because we only check once we're notified + self.tcp_listener = Some(listener); + write!(ret, "TCP listener started on port 80").unwrap(); + } // Testing of udp is done with netcat: // to send packets run `netcat -u 6502` on a remote host, and then type some data // to receive packets, use `netcat -u -l 6502`, on the same remote host, and it should show a packet of counts received @@ -228,7 +244,7 @@ impl<'a> ShellCmdApi<'a> for NetCmd { } - fn callback(&mut self, msg: &MessageEnvelope, _env: &mut CommonEnv) -> Result>, xous::Error> { + fn callback(&mut self, msg: &MessageEnvelope, env: &mut CommonEnv) -> Result>, xous::Error> { use core::fmt::Write; log::debug!("net callback"); @@ -298,6 +314,69 @@ impl<'a> ShellCmdApi<'a> for NetCmd { write!(ret, "Got NetCmd callback from uninitialized socket").unwrap(); } }, + Some(NetCmdDispatch::TcpServer) => { + if let Some(listener) = &mut self.tcp_listener { + match listener.accept() { + Ok((mut stream, addr)) => { + let test_string = std::format!("Hello from Precursor!\n\rI have been up for {} ms.\n\r", env.ticktimer.elapsed_ms()); + let mut request = [0u8; 1024]; + match stream.read(&mut request) { + Ok(len) => { + let r = std::string::String::from_utf8_lossy(&request[..len]); + log::info!("Request received from {:?}: {}", stream.peer_addr().unwrap(), r); + write!(ret, "{}\n\n", r).unwrap(); + let mut tokens = r.split(' '); + let mut valid = true; + if let Some(verb) = tokens.next() { + if verb != "GET" { + log::info!("Expected GET, got {}", verb); + valid = false; + } + } + if let Some(path) = tokens.next() { + if path != "/" { + log::info!("We only know /, got {}", path); + valid = false; + } + } + if valid { + // now send a page back... + let page = format!("HTTP/1.1 200 OK\r\nContent-Type: text/plain; charset=utf-8\n\rContent-Length: {}\n\rConnection: close\n\r\n\r{}\n\r", + test_string.len(), + test_string); + log::info!("Responding with: {}", page); + write!(stream, "{}", page).unwrap(); + log::info!("Flushing response to {:?}", addr); + stream.flush().unwrap(); + log::info!("Sent a response {:?}", addr); + write!(ret, "Sent 200 to {:?}", addr).unwrap(); + } else { + let errstring = "Sorry, I have only one trick, and it's pretty dumb."; + let notfound = format!("HTTP/1.1 404 Not Found\n\rConnection: close\n\rContent-Type: text/plain; charset=utf-8\n\rContent-Length: {}\n\r\n\r{}\n\r", + errstring.len(), + errstring + ); + log::info!("Responding with: {}", notfound); + write!(stream, "{}", notfound).unwrap(); + stream.flush().unwrap(); + write!(ret, "Sent 404 to {:?}", addr).unwrap(); + } + } + Err(e) => { + log::info!("Stream was opened, but no request received: {:?}", e); + write!(ret, "Socket opened but no request data received: {:?}", e).unwrap(); + } + } + // stream should close automatically as `stream` goes out of scope here and Drop is called. + } + Err(e) => { + write!(ret, "Got error on TCP accept: {:?}", e).unwrap(); + } + } + } else { + write!(ret, "Got NetCmd callback for uninitialized server").unwrap(); + } + }, None => { // rebind the scalar args to the Ping convention let op = arg1; diff --git a/services/status/locales/i18n.json b/services/status/locales/i18n.json index 59a4eb97c..98852b165 100644 --- a/services/status/locales/i18n.json +++ b/services/status/locales/i18n.json @@ -6,7 +6,7 @@ "en-audio": "Measuring..." }, "stats.disconnected": { - "ja": "接続されていません", + "ja": "接続不可", "en": "Not connected", "zh": "没有连接", "en-audio": "Not connected" diff --git a/services/ticktimer-server/src/main.rs b/services/ticktimer-server/src/main.rs index 561b55f9c..0fe3a7bb3 100644 --- a/services/ticktimer-server/src/main.rs +++ b/services/ticktimer-server/src/main.rs @@ -475,7 +475,7 @@ fn xmain() -> ! { log::info!("****************************************************************"); log::info!("Welcome to Xous {}", version::SEMVER); - log::info!("Built at {}", version::TIMESTAMP); + log::info!("Built on {}", version::TIMESTAMP); log::info!("****************************************************************"); let ticktimer_server = xous::create_server_with_address(b"ticktimer-server")