net: don't close connection when setting fields

Due to overly liberal use of `.take()`, file handles were getting closed
when doing ordinary operations. This stops that.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2022-03-18 20:47:10 +08:00
parent 0b3fc8e98e
commit d6063d9992
2 changed files with 58 additions and 92 deletions

View file

@ -238,9 +238,10 @@ pub(crate) enum NetMemResponse {
} }
#[repr(C)] #[repr(C)]
#[derive(Debug)]
pub enum NetError { pub enum NetError {
// Ok = 0, // Ok = 0,
// OutOfMemory = 1, Unaddressable = 1,
SocketInUse = 2, SocketInUse = 2,
// AccessDenied = 3, // AccessDenied = 3,
Invalid = 4, Invalid = 4,

View file

@ -180,6 +180,9 @@ fn respond_with_error(mut env: xous::MessageEnvelope, code: NetError) -> Option<
*i.next()? = 1; *i.next()? = 1;
*i.next()? = 1; *i.next()? = 1;
*i.next()? = code as u8; *i.next()? = code as u8;
*i.next()? = 0;
*i.next()? = 0;
*i.next()? = 0;
None None
} }
@ -261,11 +264,11 @@ fn std_tcp_connect(
.connect((address, remote_port), local_port) .connect((address, remote_port), local_port)
.map_err(|e| match e { .map_err(|e| match e {
smoltcp::Error::Illegal => NetError::SocketInUse, smoltcp::Error::Illegal => NetError::SocketInUse,
smoltcp::Error::Unaddressable => NetError::Invalid, smoltcp::Error::Unaddressable => NetError::Unaddressable,
_ => NetError::LibraryError, _ => NetError::LibraryError,
}) })
{ {
log::trace!("couldn't connect"); log::trace!("couldn't connect: {:?}", e);
respond_with_error(msg, e); respond_with_error(msg, e);
return; return;
} }
@ -308,7 +311,7 @@ fn std_tcp_tx(
}; };
let mut socket = sockets.get::<TcpSocket>(*handle); let mut socket = sockets.get::<TcpSocket>(*handle);
if !socket.may_send() { if !socket.can_send() {
log::trace!("tx can't send, will retry"); log::trace!("tx can't send, will retry");
let expiry = body let expiry = body
.offset .offset
@ -465,7 +468,7 @@ fn std_tcp_peek(
#[xous::xous_main] #[xous::xous_main]
fn xmain() -> ! { fn xmain() -> ! {
log_server::init_wait().unwrap(); log_server::init_wait().unwrap();
log::set_max_level(log::LevelFilter::Trace); log::set_max_level(log::LevelFilter::Info);
log::info!("my PID is {}", xous::process::id()); log::info!("my PID is {}", xous::process::id());
let xns = xous_names::XousNames::new().unwrap(); let xns = xous_names::XousNames::new().unwrap();
@ -888,23 +891,6 @@ fn xmain() -> ! {
Some(Opcode::StdGetAddress) => { Some(Opcode::StdGetAddress) => {
let pid = msg.sender.pid(); let pid = msg.sender.pid();
let connection_idx = msg.body.id() >> 16; 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() { let body = match msg.body.memory_message_mut() {
Some(body) => body, Some(body) => body,
None => { None => {
@ -913,11 +899,19 @@ fn xmain() -> ! {
} }
}; };
let socket = sockets.get::<TcpSocket>(handle); if let Some(Some(connection)) = process_sockets
body.valid = xous::MemorySize::new( .entry(pid)
write_address(socket.local_endpoint().addr, body.buf.as_slice_mut()) .or_default()
.unwrap_or_default(), .get_mut(connection_idx)
); {
let socket = sockets.get::<TcpSocket>(*connection);
body.valid = xous::MemorySize::new(
write_address(socket.local_endpoint().addr, body.buf.as_slice_mut())
.unwrap_or_default(),
);
} else {
respond_with_error(msg, NetError::Invalid);
}
} }
Some(Opcode::StdGetTtl) => { Some(Opcode::StdGetTtl) => {
@ -929,25 +923,21 @@ fn xmain() -> ! {
continue; continue;
} }
let handle = if let Some(connection) = process_sockets if let Some(Some(connection)) = process_sockets
.entry(pid) .entry(pid)
.or_default() .or_default()
.get_mut(connection_idx) .get_mut(connection_idx)
{ {
if let Some(connection) = connection.take() { let socket = sockets.get::<TcpSocket>(*connection);
connection xous::return_scalar(
} else { msg.sender,
respond_with_error(msg, NetError::Invalid); socket.hop_limit().unwrap_or_default() as usize,
continue; )
} .ok();
} else { } else {
respond_with_error(msg, NetError::Invalid); respond_with_error(msg, NetError::Invalid);
continue; continue;
}; }
let socket = sockets.get::<TcpSocket>(handle);
xous::return_scalar(msg.sender, socket.hop_limit().unwrap_or_default() as usize)
.ok();
} }
Some(Opcode::StdSetTtl) => { Some(Opcode::StdSetTtl) => {
@ -959,32 +949,24 @@ fn xmain() -> ! {
continue; continue;
} }
let handle = if let Some(connection) = process_sockets if let Some(Some(connection)) = process_sockets
.entry(pid) .entry(pid)
.or_default() .or_default()
.get_mut(connection_idx) .get_mut(connection_idx)
{ {
if let Some(connection) = connection.take() { let mut socket = sockets.get::<TcpSocket>(*connection);
connection let args = msg.body.scalar_message().unwrap();
let hop_limit = if args.arg1 == 0 {
None
} else { } else {
respond_with_error(msg, NetError::Invalid); Some(args.arg1 as u8)
continue; };
} socket.set_hop_limit(hop_limit);
xous::return_scalar(msg.sender, 0).ok();
} else { } else {
respond_with_error(msg, NetError::Invalid); respond_with_error(msg, NetError::Invalid);
continue; 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) => { Some(Opcode::StdGetNodelay) => {
@ -996,32 +978,24 @@ fn xmain() -> ! {
continue; continue;
} }
let handle = if let Some(connection) = process_sockets if let Some(Some(connection)) = process_sockets
.entry(pid) .entry(pid)
.or_default() .or_default()
.get_mut(connection_idx) .get_mut(connection_idx)
{ {
if let Some(connection) = connection.take() { let socket = sockets.get::<TcpSocket>(*connection);
connection xous::return_scalar(
} else { msg.sender,
respond_with_error(msg, NetError::Invalid); if socket.nagle_enabled().is_some() {
continue; 1
} } else {
0
},
)
.ok();
} else { } else {
respond_with_error(msg, NetError::Invalid); 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) => { Some(Opcode::StdSetNodelay) => {
@ -1033,27 +1007,18 @@ fn xmain() -> ! {
continue; continue;
} }
let handle = if let Some(connection) = process_sockets if let Some(Some(connection)) = process_sockets
.entry(pid) .entry(pid)
.or_default() .or_default()
.get_mut(connection_idx) .get_mut(connection_idx)
{ {
if let Some(connection) = connection.take() { let mut socket = sockets.get::<TcpSocket>(*connection);
connection let args = msg.body.scalar_message().unwrap();
} else { socket.set_nagle_enabled(args.arg1 != 0);
respond_with_error(msg, NetError::Invalid); xous::return_scalar(msg.sender, 0).ok();
continue;
}
} else { } else {
respond_with_error(msg, NetError::Invalid); 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) => {
@ -1208,7 +1173,7 @@ fn xmain() -> ! {
}; };
if let Some(tcp_state) = tcp_handles.get(&connection) { if let Some(tcp_state) = tcp_handles.get(&connection) {
let mut socket = sockets.get::<TcpSocket>(tcp_state.handle); let mut socket = sockets.get::<TcpSocket>(tcp_state.handle);
if socket.may_send() { if socket.can_send() {
tcp_tx.result = match socket.send_slice(&tcp_tx.data[..tcp_tx.len as usize]) tcp_tx.result = match socket.send_slice(&tcp_tx.data[..tcp_tx.len as usize])
{ {
Ok(octets) => { Ok(octets) => {