diff --git a/services/net/src/api.rs b/services/net/src/api.rs index 9b66a018c..3e226aa93 100644 --- a/services/net/src/api.rs +++ b/services/net/src/api.rs @@ -161,6 +161,20 @@ pub(crate) enum Opcode { /// etc.) StdTcpClose = 34, + /// Get the current IP address + StdGetAddress = 35, + + /// BlockingScalar call to get the current hop count of this connection + StdGetTtl = 36, + + /// BlockingScalar call to set the maximum hop count of this connection + StdSetTtl = 37, + + /// BlockingScalar call to get the NODELAY / "Nagle" value of this connection + StdGetNodelay = 38, + + /// BlockingScalar call to set the NODELAY / "Nagle" value of this connection + StdSetNodelay = 39, } #[derive(Debug, Archive, Serialize, Deserialize, Copy, Clone, Default)] @@ -225,14 +239,14 @@ pub(crate) enum NetMemResponse { #[repr(C)] pub enum NetError { - Ok = 0, - OutOfMemory = 1, + // Ok = 0, + // OutOfMemory = 1, SocketInUse = 2, - AccessDenied = 3, + // AccessDenied = 3, Invalid = 4, - Finished = 5, + // Finished = 5, LibraryError = 6, - AlreadyUsed = 7, + // AlreadyUsed = 7, TimedOut = 8, } diff --git a/services/net/src/main.rs b/services/net/src/main.rs index b61fefa7a..792d84ae5 100644 --- a/services/net/src/main.rs +++ b/services/net/src/main.rs @@ -133,6 +133,30 @@ fn parse_address(data: &[u8]) -> Option { } } +fn write_address(address: IpAddress, data: &mut [u8]) -> Option { + let mut i = data.iter_mut(); + match address { + IpAddress::Ipv4(a) => { + *i.next()? = 4; + for (dest, src) in i.zip(a.as_bytes().iter()) { + *dest = *src; + } + Some(5) + } + IpAddress::Ipv6(a) => { + *i.next()? = 6; + for (dest, src) in i.zip(a.as_bytes().iter()) { + *dest = *src; + } + Some(16) + } + _ => { + *i.next()? = 0; + Some(1) + } + } +} + fn respond_with_error(mut env: xous::MessageEnvelope, code: NetError) -> Option<()> { // If it's not a memory message, don't fill in the return information. let body = match env.body.memory_message_mut() { @@ -861,6 +885,177 @@ fn xmain() -> ! { } } + Some(Opcode::StdGetAddress) => { + let pid = msg.sender.pid(); + let connection_idx = msg.body.id() >> 16; + + let handle = if let Some(connection) = process_sockets + .entry(pid) + .or_default() + .get_mut(connection_idx) + { + if let Some(connection) = connection.take() { + connection + } else { + respond_with_error(msg, NetError::Invalid); + continue; + } + } else { + respond_with_error(msg, NetError::Invalid); + continue; + }; + + let body = match msg.body.memory_message_mut() { + Some(body) => body, + None => { + respond_with_error(msg, NetError::LibraryError); + continue; + } + }; + + let socket = sockets.get::(handle); + body.valid = xous::MemorySize::new( + write_address(socket.local_endpoint().addr, body.buf.as_slice_mut()) + .unwrap_or_default(), + ); + } + + Some(Opcode::StdGetTtl) => { + let pid = msg.sender.pid(); + let connection_idx = msg.body.id() >> 16; + // Only work with blockingscalar messages + if !msg.body.is_blocking() || msg.body.has_memory() { + respond_with_error(msg, NetError::LibraryError); + continue; + } + + let handle = if let Some(connection) = process_sockets + .entry(pid) + .or_default() + .get_mut(connection_idx) + { + if let Some(connection) = connection.take() { + connection + } else { + respond_with_error(msg, NetError::Invalid); + continue; + } + } else { + respond_with_error(msg, NetError::Invalid); + continue; + }; + + let socket = sockets.get::(handle); + xous::return_scalar(msg.sender, socket.hop_limit().unwrap_or_default() as usize) + .ok(); + } + + Some(Opcode::StdSetTtl) => { + let pid = msg.sender.pid(); + let connection_idx = msg.body.id() >> 16; + // Only work with blockingscalar messages + if !msg.body.is_blocking() || msg.body.has_memory() { + respond_with_error(msg, NetError::LibraryError); + continue; + } + + let handle = if let Some(connection) = process_sockets + .entry(pid) + .or_default() + .get_mut(connection_idx) + { + if let Some(connection) = connection.take() { + connection + } else { + respond_with_error(msg, NetError::Invalid); + continue; + } + } else { + respond_with_error(msg, NetError::Invalid); + continue; + }; + + let args = msg.body.scalar_message().unwrap(); + + let mut socket = sockets.get::(handle); + let hop_limit = if args.arg1 == 0 { + None + } else { + Some(args.arg1 as u8) + }; + socket.set_hop_limit(hop_limit); + xous::return_scalar(msg.sender, 0).ok(); + } + + Some(Opcode::StdGetNodelay) => { + let pid = msg.sender.pid(); + let connection_idx = msg.body.id() >> 16; + // Only work with blockingscalar messages + if !msg.body.is_blocking() || msg.body.has_memory() { + respond_with_error(msg, NetError::LibraryError); + continue; + } + + let handle = if let Some(connection) = process_sockets + .entry(pid) + .or_default() + .get_mut(connection_idx) + { + if let Some(connection) = connection.take() { + connection + } else { + respond_with_error(msg, NetError::Invalid); + continue; + } + } else { + respond_with_error(msg, NetError::Invalid); + continue; + }; + + let socket = sockets.get::(handle); + xous::return_scalar( + msg.sender, + if socket.nagle_enabled().is_some() { + 1 + } else { + 0 + }, + ) + .ok(); + } + + Some(Opcode::StdSetNodelay) => { + let pid = msg.sender.pid(); + let connection_idx = msg.body.id() >> 16; + // Only work with blockingscalar messages + if !msg.body.is_blocking() || msg.body.has_memory() { + respond_with_error(msg, NetError::LibraryError); + continue; + } + + let handle = if let Some(connection) = process_sockets + .entry(pid) + .or_default() + .get_mut(connection_idx) + { + if let Some(connection) = connection.take() { + connection + } else { + respond_with_error(msg, NetError::Invalid); + continue; + } + } else { + respond_with_error(msg, NetError::Invalid); + continue; + }; + + let args = msg.body.scalar_message().unwrap(); + + let mut socket = sockets.get::(handle); + socket.set_nagle_enabled(args.arg1 != 0); + xous::return_scalar(msg.sender, 0).ok(); + } + Some(Opcode::TcpConnect) => { let mut buf = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) @@ -1616,8 +1811,8 @@ fn xmain() -> ! { let mut socket; let WaitingSocket { mut env, - handle, - expiry, + handle: _, + expiry: _, } = { match connection { &mut None => continue, @@ -1662,8 +1857,8 @@ fn xmain() -> ! { let mut socket; let WaitingSocket { mut env, - handle, - expiry, + handle: _, + expiry: _, } = { match connection { &mut None => continue,