mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +00:00
initial tcplistener draft
This commit is contained in:
parent
54ce057f09
commit
a555f0380e
4 changed files with 302 additions and 22 deletions
|
|
@ -211,13 +211,13 @@ pub(crate) enum Opcode {
|
|||
|
||||
/// Close the UDP connection. The connection ID is specified in the upper 16 bits
|
||||
/// of the opcode. This may be any kind of message (scalar, blockingscalar, memory,
|
||||
/// etc.)
|
||||
/// etc.)
|
||||
StdUdpClose = 41,
|
||||
|
||||
/// Receives data from the specified UDP Connection.
|
||||
///
|
||||
/// - The UDP connection number (fd) is passed in the upper 16 bits of the opcode.
|
||||
/// # Argumesnts
|
||||
/// # Arguments
|
||||
/// The arguments are passed as follows:
|
||||
/// u8 array
|
||||
/// -------|---------
|
||||
|
|
@ -280,6 +280,73 @@ pub(crate) enum Opcode {
|
|||
/// 0 | 1 indicating error, 0 indicating success
|
||||
/// 1 | Error code (only valid on error)
|
||||
StdUdpTx = 43,
|
||||
|
||||
/// Create a listener on the designated socket
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// u8 array
|
||||
/// -------|---------
|
||||
/// offset | Contents
|
||||
/// =======|=========
|
||||
/// 0 | port number, low byte
|
||||
/// 1 | port number, high byte
|
||||
/// 2 | address type -- 4 = ipv4, 6 = ipv6
|
||||
/// ... | remaining bytes are the address
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// u8 array
|
||||
/// -------|---------
|
||||
/// offset | Contents
|
||||
/// =======|=========
|
||||
/// 0 | 0 indicating success
|
||||
/// 1 | Connection index
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// u8 array
|
||||
/// -------|---------
|
||||
/// offset | Contents
|
||||
/// =======|=========
|
||||
/// 0 | 1 indicating error
|
||||
/// 1 | Error code
|
||||
StdTcpListen = 44,
|
||||
|
||||
/// Converts incoming TCP connection Listener to a TcpStream object.
|
||||
///
|
||||
/// - The TCP connection number (fd) is passed in the upper 16 bits of the opcode.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// The arguments are passed as follows:
|
||||
/// u8 array
|
||||
/// -------|---------
|
||||
/// offset | Contents
|
||||
/// =======|=========
|
||||
/// 0 | 0 indicates nonblocking; 1 indicates blocking
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// The return buffer is the `buf` parameter, which is split as follows:
|
||||
/// u8 array
|
||||
/// -------|---------
|
||||
/// offset | Contents
|
||||
/// =======|=========
|
||||
/// 0 | 0 indicates success; 1 indicates error
|
||||
/// 1 | connection index (lsb)
|
||||
/// 2 | connection index (msb)
|
||||
/// 3 | address type -- 4 = ipv4, 6 = ipv6, 0 = invalid/err
|
||||
/// 4 | LSB ip address
|
||||
/// ...
|
||||
/// 7 | MSB ipv4 address
|
||||
/// ...
|
||||
/// 19| MSW ipv6 address
|
||||
/// 20| remote port:u16, low byte
|
||||
/// 21| remote port:u16, high byte
|
||||
///
|
||||
/// - The `valid` and `offset` parameters are not used.
|
||||
StdTcpAccept = 45,
|
||||
}
|
||||
|
||||
#[derive(Debug, Archive, Serialize, Deserialize, Copy, Clone, Default)]
|
||||
|
|
|
|||
|
|
@ -9,6 +9,8 @@ mod std_glue;
|
|||
use std_glue::*;
|
||||
mod std_udp;
|
||||
use std_udp::*;
|
||||
mod std_tcplistener;
|
||||
use std_tcplistener::*;
|
||||
|
||||
use com::api::{ComIntSources, Ipv4Conf, NET_MTU};
|
||||
use num_traits::*;
|
||||
|
|
@ -25,7 +27,7 @@ use byteorder::{ByteOrder, NetworkEndian};
|
|||
use smoltcp::iface::{Interface, InterfaceBuilder, NeighborCache, Routes};
|
||||
use smoltcp::phy::{Device, Medium};
|
||||
use smoltcp::socket::{IcmpEndpoint, IcmpPacketMetadata, IcmpSocket, IcmpSocketBuffer, SocketSet};
|
||||
use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv4Address, Ipv4Cidr};
|
||||
use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv4Address, Ipv4Cidr, IpEndpoint};
|
||||
use smoltcp::wire::{Icmpv4Packet, Icmpv4Repr, Icmpv6Packet, Icmpv6Repr};
|
||||
|
||||
use core::num::NonZeroU64;
|
||||
|
|
@ -98,6 +100,12 @@ struct WaitingSocket {
|
|||
expiry: Option<NonZeroU64>,
|
||||
}
|
||||
|
||||
struct AcceptingSocket {
|
||||
env: xous::MessageEnvelope,
|
||||
handle: SocketHandle,
|
||||
fd: usize,
|
||||
}
|
||||
|
||||
pub struct UdpStdState {
|
||||
pub msg: xous::MessageEnvelope,
|
||||
pub handle: SocketHandle,
|
||||
|
|
@ -183,6 +191,9 @@ fn xmain() -> ! {
|
|||
)>,
|
||||
> = Vec::new();
|
||||
|
||||
// When a client issues an Accept request, it gets placed here for later processing.
|
||||
let mut tcp_accept_waiting: Vec<Option<AcceptingSocket>> = Vec::new();
|
||||
|
||||
// When a UDP client opens a socket, an entry is automatically created here to accumulate
|
||||
// incoming UDP socket data.
|
||||
let mut udp_rx_waiting: Vec<Option<UdpStdState>> = Vec::new();
|
||||
|
|
@ -552,6 +563,41 @@ fn xmain() -> ! {
|
|||
}
|
||||
}
|
||||
|
||||
Some(Opcode::StdTcpListen) => {
|
||||
let pid = msg.sender.pid();
|
||||
|
||||
std_tcp_listen(
|
||||
msg,
|
||||
&mut sockets,
|
||||
process_sockets.entry(pid).or_default(),
|
||||
);
|
||||
xous::try_send_message(
|
||||
net_conn,
|
||||
Message::new_scalar(
|
||||
Opcode::NetPump.to_usize().unwrap(),
|
||||
0, 0, 0, 0
|
||||
),
|
||||
).ok();
|
||||
}
|
||||
|
||||
Some(Opcode::StdTcpAccept) => {
|
||||
let pid = msg.sender.pid();
|
||||
|
||||
std_tcp_accept(
|
||||
msg,
|
||||
&mut sockets,
|
||||
&mut tcp_accept_waiting,
|
||||
process_sockets.entry(pid).or_default(),
|
||||
);
|
||||
xous::try_send_message(
|
||||
net_conn,
|
||||
Message::new_scalar(
|
||||
Opcode::NetPump.to_usize().unwrap(),
|
||||
0, 0, 0, 0
|
||||
),
|
||||
).ok();
|
||||
}
|
||||
|
||||
Some(Opcode::StdGetAddress) => {
|
||||
let pid = msg.sender.pid();
|
||||
let connection_idx = msg.body.id() >> 16;
|
||||
|
|
@ -1060,11 +1106,11 @@ fn xmain() -> ! {
|
|||
if let Some(connection) = connection.take() {
|
||||
connection
|
||||
} else {
|
||||
udp_failure(msg, NetError::Invalid);
|
||||
std_failure(msg, NetError::Invalid);
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
udp_failure(msg, NetError::Invalid);
|
||||
std_failure(msg, NetError::Invalid);
|
||||
continue;
|
||||
};
|
||||
sockets.get::<UdpSocket>(handle).close();
|
||||
|
|
@ -1438,6 +1484,31 @@ fn xmain() -> ! {
|
|||
response_data[1] = sent_octets as u32;
|
||||
}
|
||||
|
||||
// this handles TCP std listeners
|
||||
for connection in tcp_accept_waiting.iter_mut() {
|
||||
let ep: IpEndpoint;
|
||||
let AcceptingSocket {
|
||||
mut env,
|
||||
handle: _,
|
||||
fd,
|
||||
} = match connection {
|
||||
&mut None => continue,
|
||||
Some(s) => {
|
||||
let socket = sockets.get::<TcpSocket>(s.handle);
|
||||
if socket.is_active() {
|
||||
ep = socket.remote_endpoint();
|
||||
connection.take().unwrap()
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
};
|
||||
let body = env.body.memory_message_mut().unwrap();
|
||||
let buf = body.buf.as_slice_mut::<u8>();
|
||||
|
||||
tcp_accept_success(buf, fd as u16, ep);
|
||||
}
|
||||
|
||||
// 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.
|
||||
|
|
@ -1537,7 +1608,7 @@ fn xmain() -> ! {
|
|||
|
||||
// If it can't receive, then the only explanation was that it timed out
|
||||
if !socket.can_recv() {
|
||||
udp_failure(msg, NetError::TimedOut);
|
||||
std_failure(msg, NetError::TimedOut);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
@ -1555,7 +1626,7 @@ fn xmain() -> ! {
|
|||
}
|
||||
Err(e) => {
|
||||
log::error!("unable to receive: {:?}", e);
|
||||
udp_failure(msg, NetError::LibraryError);
|
||||
std_failure(msg, NetError::LibraryError);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
|
@ -1571,7 +1642,7 @@ fn xmain() -> ! {
|
|||
}
|
||||
Err(e) => {
|
||||
log::error!("unable to receive: {:?}", e);
|
||||
udp_failure(msg, NetError::LibraryError);
|
||||
std_failure(msg, NetError::LibraryError);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
142
services/net/src/std_tcplistener.rs
Normal file
142
services/net/src/std_tcplistener.rs
Normal file
|
|
@ -0,0 +1,142 @@
|
|||
use crate::*;
|
||||
use smoltcp::wire::{IpEndpoint, IpAddress};
|
||||
|
||||
pub(crate) fn std_tcp_listen(
|
||||
mut msg: xous::MessageEnvelope,
|
||||
sockets: &mut SocketSet,
|
||||
our_sockets: &mut Vec<Option<SocketHandle>>,
|
||||
) {
|
||||
// Ignore nonblocking and scalar messages
|
||||
let body = match msg.body.memory_message_mut() {
|
||||
Some(b) => b,
|
||||
None => {
|
||||
log::trace!("invalid message type");
|
||||
std_failure(msg, NetError::LibraryError);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let bytes = body.buf.as_slice::<u8>();
|
||||
let local_port = u16::from_le_bytes([bytes[0], bytes[1]]);
|
||||
let address = match parse_address(&bytes[2..]) {
|
||||
Some(addr) => addr,
|
||||
None => {
|
||||
log::trace!("couldn't parse address");
|
||||
std_failure(msg, NetError::LibraryError);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
if let Err(e) = tcp_socket
|
||||
.listen(local_port)
|
||||
.map_err(|e| match e {
|
||||
smoltcp::Error::Illegal => NetError::SocketInUse,
|
||||
smoltcp::Error::Unaddressable => NetError::Unaddressable,
|
||||
_ => NetError::LibraryError,
|
||||
})
|
||||
{
|
||||
log::trace!("couldn't listen: {:?}", e);
|
||||
std_failure(msg, e);
|
||||
return;
|
||||
}
|
||||
|
||||
let handle = sockets.add(tcp_socket);
|
||||
|
||||
// Add the socket into our process' list of sockets, and pass the index back as the `fd` parameter for future reference.
|
||||
let fd = insert_or_append(our_sockets, handle) as u8;
|
||||
|
||||
let body = msg.body.memory_message_mut().unwrap();
|
||||
let bfr = body.buf.as_slice_mut::<u8>();
|
||||
log::trace!("successfully connected: {} -> {:?}:{}", fd, address, local_port);
|
||||
bfr[0] = 0;
|
||||
bfr[1] = fd;
|
||||
}
|
||||
|
||||
pub(crate) fn std_tcp_accept(
|
||||
mut msg: xous::MessageEnvelope,
|
||||
sockets: &mut SocketSet,
|
||||
tcp_accept_waiting: &mut Vec<Option<AcceptingSocket>>,
|
||||
our_sockets: &Vec<Option<SocketHandle>>,
|
||||
) {
|
||||
let fd = (msg.body.id() >> 16) & 0xffff;
|
||||
let body = match msg.body.memory_message_mut() {
|
||||
Some(body) => body,
|
||||
None => {
|
||||
std_failure(msg, NetError::LibraryError);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// this is not used
|
||||
body.valid = None;
|
||||
|
||||
let handle = match our_sockets.get(fd) {
|
||||
Some(Some(val)) => val,
|
||||
_ => {
|
||||
std_failure(msg, NetError::Invalid);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let args = body.buf.as_slice::<u8>();
|
||||
let nonblocking = args[0] == 0;
|
||||
|
||||
let socket = sockets.get::<TcpSocket>(*handle);
|
||||
|
||||
if socket.is_active() {
|
||||
log::trace!("accept did not block; immediately returning TcpSocket");
|
||||
let buf = body.buf.as_slice_mut::<u8>();
|
||||
tcp_accept_success(buf, fd as u16, socket.remote_endpoint());
|
||||
return;
|
||||
}
|
||||
|
||||
if nonblocking {
|
||||
std_failure(msg, NetError::WouldBlock);
|
||||
return;
|
||||
}
|
||||
log::trace!("TCP listener added to accept queue");
|
||||
|
||||
// Adding the message to the udp_rx_waiting list prevents it from going out of scope and
|
||||
// thus prevents the .drop() method from being called. Since messages are returned to the sender
|
||||
// in the .drop() method, this keeps the caller blocked for the lifetime of the message.
|
||||
insert_or_append(
|
||||
tcp_accept_waiting,
|
||||
AcceptingSocket {
|
||||
env: msg, // <-- msg is inserted into the tcp_accept_waiting vector, thus preventing the lend_mut from returning.
|
||||
handle: *handle,
|
||||
fd,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
pub(crate) fn tcp_accept_success(buf: &mut [u8], fd: u16, ep: IpEndpoint) {
|
||||
log::debug!("tcp accept: {:?}", ep);
|
||||
buf[0] = 0;
|
||||
let fd_arr = fd.to_le_bytes();
|
||||
buf[1] = fd_arr[0];
|
||||
buf[2] = fd_arr[1];
|
||||
match ep.addr {
|
||||
IpAddress::Ipv4(a) => {
|
||||
buf[3] = 4;
|
||||
for (&s, d) in a.0.iter().zip(buf[4..8].iter_mut()) {
|
||||
*d = s;
|
||||
}
|
||||
}
|
||||
IpAddress::Ipv6(a) => {
|
||||
buf[3] = 6;
|
||||
for (&s, d) in a.0.iter().zip(buf[4..20].iter_mut()) {
|
||||
*d = s;
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
buf[3] = 0; // this is the invalid/error type
|
||||
}
|
||||
}
|
||||
let port = ep.port.to_le_bytes();
|
||||
buf[20] = port[0];
|
||||
buf[21] = port[1];
|
||||
}
|
||||
|
|
@ -19,7 +19,7 @@ pub(crate) fn std_udp_bind(
|
|||
Some(b) => b,
|
||||
None => {
|
||||
log::trace!("invalid message type");
|
||||
udp_failure(msg, NetError::LibraryError);
|
||||
std_failure(msg, NetError::LibraryError);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
|
@ -30,7 +30,7 @@ pub(crate) fn std_udp_bind(
|
|||
Some(addr) => addr,
|
||||
None => {
|
||||
log::trace!("couldn't parse address");
|
||||
udp_failure(msg, NetError::LibraryError);
|
||||
std_failure(msg, NetError::LibraryError);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
|
@ -51,7 +51,7 @@ pub(crate) fn std_udp_bind(
|
|||
})
|
||||
{
|
||||
log::trace!("couldn't connect: {:?}", e);
|
||||
udp_failure(msg, e);
|
||||
std_failure(msg, e);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -78,7 +78,7 @@ pub(crate) fn std_udp_rx(
|
|||
let body = match msg.body.memory_message_mut() {
|
||||
Some(body) => body,
|
||||
None => {
|
||||
udp_failure(msg, NetError::LibraryError);
|
||||
std_failure(msg, NetError::LibraryError);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
|
@ -89,7 +89,7 @@ pub(crate) fn std_udp_rx(
|
|||
let handle = match our_sockets.get(connection_handle_index) {
|
||||
Some(Some(val)) => val,
|
||||
_ => {
|
||||
udp_failure(msg, NetError::Invalid);
|
||||
std_failure(msg, NetError::Invalid);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
|
@ -120,7 +120,7 @@ pub(crate) fn std_udp_rx(
|
|||
}
|
||||
Err(e) => {
|
||||
log::error!("unable to receive: {:?}", e);
|
||||
udp_failure(msg, NetError::LibraryError);
|
||||
std_failure(msg, NetError::LibraryError);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
|
@ -131,14 +131,14 @@ pub(crate) fn std_udp_rx(
|
|||
}
|
||||
Err(e) => {
|
||||
log::error!("unable to receive: {:?}", e);
|
||||
udp_failure(msg, NetError::LibraryError);
|
||||
std_failure(msg, NetError::LibraryError);
|
||||
}
|
||||
}
|
||||
};
|
||||
return;
|
||||
}
|
||||
if nonblocking {
|
||||
udp_failure(msg, NetError::WouldBlock);
|
||||
std_failure(msg, NetError::WouldBlock);
|
||||
return;
|
||||
}
|
||||
log::trace!("UDP socket was not ready to receive, adding it to list of waiting messages");
|
||||
|
|
@ -166,14 +166,14 @@ pub(crate) fn std_udp_tx(
|
|||
let body = match msg.body.memory_message_mut() {
|
||||
Some(body) => body,
|
||||
None => {
|
||||
udp_failure(msg, NetError::LibraryError);
|
||||
std_failure(msg, NetError::LibraryError);
|
||||
return;
|
||||
}
|
||||
};
|
||||
let handle = match our_sockets.get(connection_handle_index) {
|
||||
Some(Some(val)) => val,
|
||||
_ => {
|
||||
udp_failure(msg, NetError::Invalid);
|
||||
std_failure(msg, NetError::Invalid);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
|
@ -185,7 +185,7 @@ pub(crate) fn std_udp_tx(
|
|||
Some(addr) => addr,
|
||||
None => {
|
||||
log::trace!("couldn't parse address");
|
||||
udp_failure(msg, NetError::LibraryError);
|
||||
std_failure(msg, NetError::LibraryError);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
|
@ -199,7 +199,7 @@ pub(crate) fn std_udp_tx(
|
|||
}
|
||||
Err(_e) => {
|
||||
// the only type of error returned from smoltcp in this case is if the destination is not addressible.
|
||||
udp_failure(msg, NetError::Unaddressable);
|
||||
std_failure(msg, NetError::Unaddressable);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
@ -236,8 +236,8 @@ pub(crate) fn udp_rx_success(buf: &mut [u8], rx: &[u8], ep: IpEndpoint) {
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) fn udp_failure(mut env: xous::MessageEnvelope, code: NetError) -> Option<()> {
|
||||
log::trace!("udp_failure: {:?}", code);
|
||||
pub(crate) fn std_failure(mut env: xous::MessageEnvelope, code: NetError) -> Option<()> {
|
||||
log::trace!("std_failure: {:?}", code);
|
||||
// If it's not a memory message, don't fill in the return information.
|
||||
let body = match env.body.memory_message_mut() {
|
||||
None => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue