mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-17 08:40:52 +00:00
net: add functions for setting ttl, nodelay
Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
0876e21129
commit
0b3fc8e98e
2 changed files with 218 additions and 9 deletions
|
|
@ -161,6 +161,20 @@ pub(crate) enum Opcode {
|
||||||
/// etc.)
|
/// etc.)
|
||||||
StdTcpClose = 34,
|
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)]
|
#[derive(Debug, Archive, Serialize, Deserialize, Copy, Clone, Default)]
|
||||||
|
|
@ -225,14 +239,14 @@ pub(crate) enum NetMemResponse {
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub enum NetError {
|
pub enum NetError {
|
||||||
Ok = 0,
|
// Ok = 0,
|
||||||
OutOfMemory = 1,
|
// OutOfMemory = 1,
|
||||||
SocketInUse = 2,
|
SocketInUse = 2,
|
||||||
AccessDenied = 3,
|
// AccessDenied = 3,
|
||||||
Invalid = 4,
|
Invalid = 4,
|
||||||
Finished = 5,
|
// Finished = 5,
|
||||||
LibraryError = 6,
|
LibraryError = 6,
|
||||||
AlreadyUsed = 7,
|
// AlreadyUsed = 7,
|
||||||
TimedOut = 8,
|
TimedOut = 8,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -133,6 +133,30 @@ fn parse_address(data: &[u8]) -> Option<smoltcp::wire::IpAddress> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn write_address(address: IpAddress, data: &mut [u8]) -> Option<usize> {
|
||||||
|
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<()> {
|
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.
|
// If it's not a memory message, don't fill in the return information.
|
||||||
let body = match env.body.memory_message_mut() {
|
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::<TcpSocket>(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::<TcpSocket>(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::<TcpSocket>(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::<TcpSocket>(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::<TcpSocket>(handle);
|
||||||
|
socket.set_nagle_enabled(args.arg1 != 0);
|
||||||
|
xous::return_scalar(msg.sender, 0).ok();
|
||||||
|
}
|
||||||
|
|
||||||
Some(Opcode::TcpConnect) => {
|
Some(Opcode::TcpConnect) => {
|
||||||
let mut buf = unsafe {
|
let mut buf = unsafe {
|
||||||
Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap())
|
Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap())
|
||||||
|
|
@ -1616,8 +1811,8 @@ fn xmain() -> ! {
|
||||||
let mut socket;
|
let mut socket;
|
||||||
let WaitingSocket {
|
let WaitingSocket {
|
||||||
mut env,
|
mut env,
|
||||||
handle,
|
handle: _,
|
||||||
expiry,
|
expiry: _,
|
||||||
} = {
|
} = {
|
||||||
match connection {
|
match connection {
|
||||||
&mut None => continue,
|
&mut None => continue,
|
||||||
|
|
@ -1662,8 +1857,8 @@ fn xmain() -> ! {
|
||||||
let mut socket;
|
let mut socket;
|
||||||
let WaitingSocket {
|
let WaitingSocket {
|
||||||
mut env,
|
mut env,
|
||||||
handle,
|
handle: _,
|
||||||
expiry,
|
expiry: _,
|
||||||
} = {
|
} = {
|
||||||
match connection {
|
match connection {
|
||||||
&mut None => continue,
|
&mut None => continue,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue