mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
net: add StdTcp calls
These calls have been tested to work to create tcp connections. Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
e91705c0ae
commit
89442ff4cc
2 changed files with 1119 additions and 377 deletions
|
|
@ -1,26 +1,28 @@
|
|||
pub(crate) mod udp;
|
||||
#[allow(unused_imports)] // needed to keep hosted mode quiet, since the Udp implementation is a bodge
|
||||
// needed to keep hosted mode quiet, since the Udp implementation is a bodge
|
||||
#[allow(unused_imports)]
|
||||
pub(crate) use udp::*;
|
||||
pub(crate) mod ping;
|
||||
pub(crate) use ping::*;
|
||||
pub(crate) mod tcp;
|
||||
#[allow(unused_imports)] // needed to keep hosted mode quiet, since the Tcp implementation is a bodge
|
||||
pub(crate) use tcp::*;
|
||||
pub use ping::NetPingCallback;
|
||||
// needed to keep hosted mode quiet, since the Tcp implementation is a bodge
|
||||
#[allow(unused_imports)]
|
||||
pub(crate) use tcp::*;
|
||||
|
||||
use com::SsidRecord;
|
||||
use rkyv::{Archive, Deserialize, Serialize};
|
||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
|
||||
use smoltcp::wire::IpAddress;
|
||||
use std::convert::TryInto;
|
||||
use std::fmt;
|
||||
use std::fmt::Debug;
|
||||
use std::io::Write;
|
||||
use com::SsidRecord;
|
||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
|
||||
|
||||
// republish this so we can decode the icmpv4 error codes
|
||||
pub use smoltcp::wire::Icmpv4DstUnreachable;
|
||||
|
||||
pub(crate) const SERVER_NAME_NET: &str = "_Middleware Network Server_";
|
||||
pub(crate) const SERVER_NAME_NET: &str = "_Middleware Network Server_";
|
||||
#[allow(dead_code)]
|
||||
pub const AP_DICT_NAME: &'static str = "wlan.networks";
|
||||
|
||||
|
|
@ -30,54 +32,121 @@ pub const MIN_EC_REV: u32 = 0x00_09_06_00;
|
|||
|
||||
/// Dispatch opcodes to the Net crate main loop.
|
||||
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
|
||||
#[repr(C)]
|
||||
pub(crate) enum Opcode {
|
||||
/// Calls for UDP implementation
|
||||
UdpBind,
|
||||
UdpClose,
|
||||
UdpTx,
|
||||
UdpSetTtl,
|
||||
UdpGetTtl,
|
||||
UdpBind = 0,
|
||||
UdpClose = 1,
|
||||
UdpTx = 2,
|
||||
UdpSetTtl = 3,
|
||||
UdpGetTtl = 4,
|
||||
|
||||
/// Calls for TCP implementation
|
||||
TcpConnect,
|
||||
TcpTx,
|
||||
TcpClose,
|
||||
TcpManage,
|
||||
TcpListen,
|
||||
TcpManageListener,
|
||||
TcpConnect = 5,
|
||||
TcpTx = 6,
|
||||
TcpClose = 7,
|
||||
TcpManage = 8,
|
||||
TcpListen = 9,
|
||||
TcpManageListener = 10,
|
||||
|
||||
// The DNS server can hook the Net crate for notifications on config updates
|
||||
/// Adds an Ipv4 as a DNS server
|
||||
DnsHookAddIpv4,
|
||||
DnsHookAddIpv4 = 11,
|
||||
/// Adds an Ipv6 as a DNS server. Separate messages because max scalar arg is 128 bits.
|
||||
DnsHookAddIpv6,
|
||||
DnsHookAddIpv6 = 12,
|
||||
/// Called on IP config update -- clears all DNS servers.
|
||||
DnsHookAllClear,
|
||||
DnsUnhookAll,
|
||||
DnsHookAllClear = 13,
|
||||
DnsUnhookAll = 14,
|
||||
|
||||
/// Ping stack
|
||||
Ping,
|
||||
PingSetTtl,
|
||||
PingGetTtl,
|
||||
PingSetTimeout,
|
||||
PingGetTimeout,
|
||||
Ping = 15,
|
||||
PingSetTtl = 16,
|
||||
PingGetTtl = 17,
|
||||
PingSetTimeout = 18,
|
||||
PingGetTimeout = 19,
|
||||
|
||||
/// Link Management,
|
||||
GetIpv4Config,
|
||||
Reset,
|
||||
SubscribeWifiStats,
|
||||
UnsubWifiStats,
|
||||
FetchSsidList,
|
||||
ConnMgrStartStop,
|
||||
GetIpv4Config = 20,
|
||||
Reset = 21,
|
||||
SubscribeWifiStats = 22,
|
||||
UnsubWifiStats = 23,
|
||||
FetchSsidList = 24,
|
||||
ConnMgrStartStop = 25,
|
||||
|
||||
/// [Internal] com llio interrupt callback
|
||||
ComInterrupt,
|
||||
ComInterrupt = 26,
|
||||
/// [Internal] run the network stack code
|
||||
NetPump,
|
||||
NetPump = 27,
|
||||
/// Suspend/resume callback
|
||||
SuspendResume,
|
||||
SuspendResume = 28,
|
||||
/// Quit the server
|
||||
Quit
|
||||
Quit = 29,
|
||||
|
||||
/// Create a connection to the target address
|
||||
///
|
||||
/// # 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
|
||||
///
|
||||
/// u16 array
|
||||
/// -------|---------
|
||||
/// offset | Contents
|
||||
/// =======|=========
|
||||
/// 0 | 0 indicating success
|
||||
/// 1 | Connection index
|
||||
/// 2 | local port
|
||||
/// 3 | remote port
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// u8 array
|
||||
/// -------|---------
|
||||
/// offset | Contents
|
||||
/// =======|=========
|
||||
/// 0 | 1 indicating error
|
||||
/// 1 | 1 indicating error (duplicated)
|
||||
/// 2 | 1 indicating error (duplicated)
|
||||
/// 3 | 1 indicating error (duplicated)
|
||||
/// 4 | Error code
|
||||
StdTcpConnect = 30,
|
||||
|
||||
/// Transmit data to the specified TCP Connection
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// The connection ID is OR-ed into the top 16 bits of the opcode. The payload
|
||||
/// is pointed to by the data buffer, and the `Valid` and Offset` flags indicate
|
||||
/// which regions inside the memory are valid.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// u32 array
|
||||
/// -------|---------
|
||||
/// offset | Contents
|
||||
/// =======|=========
|
||||
/// 0 | 0 if no error, 1 if error
|
||||
/// 1 | Number of bytes transferred, or code of the error
|
||||
///
|
||||
StdTcpTx = 31,
|
||||
|
||||
/// Receives data from the specified TCP Connection. The TCP connection number is
|
||||
/// passed in the upper 16 bits of the opcode, and the number of received bytes
|
||||
/// is returned as part of the `Valid` parameter.
|
||||
StdTcpRx = 32,
|
||||
|
||||
/// Close the TCP 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.)
|
||||
StdTcpClose = 33,
|
||||
}
|
||||
|
||||
#[derive(Debug, Archive, Serialize, Deserialize, Copy, Clone, Default)]
|
||||
|
|
@ -94,7 +163,7 @@ pub enum XousServerId {
|
|||
PrivateSid([u32; 4]),
|
||||
/// A name that needs to be looked up with XousNames. Easier to implement, but less secure as it requires
|
||||
/// an open connection slot that could be abused by other processes.
|
||||
ServerName(xous_ipc::String::<64>),
|
||||
ServerName(xous_ipc::String<64>),
|
||||
}
|
||||
|
||||
/// These opcodes are reserved for private SIDs shared from a DNS server to
|
||||
|
|
@ -127,6 +196,7 @@ pub(crate) enum NetCallback {
|
|||
}
|
||||
|
||||
#[derive(Debug, Archive, Serialize, Deserialize, Copy, Clone, PartialEq, Eq)]
|
||||
#[repr(C, u16)]
|
||||
pub(crate) enum NetMemResponse {
|
||||
Ok,
|
||||
Sent(u16),
|
||||
|
|
@ -139,9 +209,21 @@ pub(crate) enum NetMemResponse {
|
|||
AlreadyUsed,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
pub enum NetError {
|
||||
Ok = 0,
|
||||
OutOfMemory = 1,
|
||||
SocketInUse = 2,
|
||||
AccessDenied = 3,
|
||||
Invalid = 4,
|
||||
Finished = 5,
|
||||
LibraryError = 6,
|
||||
AlreadyUsed = 7,
|
||||
}
|
||||
|
||||
/////// a bunch of structures are re-derived here so we can infer `rkyv` traits on them
|
||||
#[derive(Debug, Archive, Serialize, Deserialize, Copy, Clone)]
|
||||
pub (crate) struct NetSocketAddr {
|
||||
pub(crate) struct NetSocketAddr {
|
||||
pub(crate) addr: NetIpAddr,
|
||||
pub(crate) port: u16,
|
||||
}
|
||||
|
|
@ -162,24 +244,16 @@ pub enum NetIpAddr {
|
|||
impl From<SocketAddr> for NetIpAddr {
|
||||
fn from(other: SocketAddr) -> NetIpAddr {
|
||||
match other {
|
||||
SocketAddr::V4(sav4) => {
|
||||
NetIpAddr::Ipv4(sav4.ip().octets())
|
||||
},
|
||||
SocketAddr::V6(sav6) => {
|
||||
NetIpAddr::Ipv6(sav6.ip().octets())
|
||||
}
|
||||
SocketAddr::V4(sav4) => NetIpAddr::Ipv4(sav4.ip().octets()),
|
||||
SocketAddr::V6(sav6) => NetIpAddr::Ipv6(sav6.ip().octets()),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl From<IpAddress> for NetIpAddr {
|
||||
fn from(other: IpAddress) -> NetIpAddr {
|
||||
match other {
|
||||
IpAddress::Ipv4(ipv4) => {
|
||||
NetIpAddr::Ipv4(ipv4.0)
|
||||
},
|
||||
IpAddress::Ipv6(ipv6) => {
|
||||
NetIpAddr::Ipv6(ipv6.0)
|
||||
},
|
||||
IpAddress::Ipv4(ipv4) => NetIpAddr::Ipv4(ipv4.0),
|
||||
IpAddress::Ipv6(ipv6) => NetIpAddr::Ipv6(ipv6.0),
|
||||
_ => {
|
||||
panic!("Invalid IpAddress")
|
||||
}
|
||||
|
|
@ -189,24 +263,16 @@ impl From<IpAddress> for NetIpAddr {
|
|||
impl From<IpAddr> for NetIpAddr {
|
||||
fn from(other: IpAddr) -> NetIpAddr {
|
||||
match other {
|
||||
IpAddr::V4(ipv4) => {
|
||||
NetIpAddr::Ipv4(ipv4.octets())
|
||||
},
|
||||
IpAddr::V6(ipv6) => {
|
||||
NetIpAddr::Ipv6(ipv6.octets())
|
||||
}
|
||||
IpAddr::V4(ipv4) => NetIpAddr::Ipv4(ipv4.octets()),
|
||||
IpAddr::V6(ipv6) => NetIpAddr::Ipv6(ipv6.octets()),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl From<NetIpAddr> for IpAddr {
|
||||
fn from(other: NetIpAddr) -> IpAddr {
|
||||
match other {
|
||||
NetIpAddr::Ipv4(octets) => {
|
||||
IpAddr::V4(Ipv4Addr::from(octets))
|
||||
}
|
||||
NetIpAddr::Ipv6(octets) => {
|
||||
IpAddr::V6(Ipv6Addr::from(octets))
|
||||
}
|
||||
NetIpAddr::Ipv4(octets) => IpAddr::V4(Ipv4Addr::from(octets)),
|
||||
NetIpAddr::Ipv6(octets) => IpAddr::V6(Ipv6Addr::from(octets)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -216,19 +282,16 @@ impl From<NetIpAddr> for IpAddress {
|
|||
NetIpAddr::Ipv4([a, b, c, d]) => {
|
||||
IpAddress::Ipv4(smoltcp::wire::Ipv4Address::new(a, b, c, d))
|
||||
}
|
||||
NetIpAddr::Ipv6(ipv6) => {
|
||||
IpAddress::Ipv6(smoltcp::wire::Ipv6Address::new(
|
||||
u16::from_be_bytes(ipv6[0..1].try_into().unwrap()),
|
||||
u16::from_be_bytes(ipv6[2..3].try_into().unwrap()),
|
||||
u16::from_be_bytes(ipv6[4..5].try_into().unwrap()),
|
||||
u16::from_be_bytes(ipv6[6..7].try_into().unwrap()),
|
||||
u16::from_be_bytes(ipv6[8..9].try_into().unwrap()),
|
||||
u16::from_be_bytes(ipv6[10..11].try_into().unwrap()),
|
||||
u16::from_be_bytes(ipv6[12..13].try_into().unwrap()),
|
||||
u16::from_be_bytes(ipv6[14..15].try_into().unwrap()),
|
||||
)
|
||||
)
|
||||
}
|
||||
NetIpAddr::Ipv6(ipv6) => IpAddress::Ipv6(smoltcp::wire::Ipv6Address::new(
|
||||
u16::from_be_bytes(ipv6[0..1].try_into().unwrap()),
|
||||
u16::from_be_bytes(ipv6[2..3].try_into().unwrap()),
|
||||
u16::from_be_bytes(ipv6[4..5].try_into().unwrap()),
|
||||
u16::from_be_bytes(ipv6[6..7].try_into().unwrap()),
|
||||
u16::from_be_bytes(ipv6[8..9].try_into().unwrap()),
|
||||
u16::from_be_bytes(ipv6[10..11].try_into().unwrap()),
|
||||
u16::from_be_bytes(ipv6[12..13].try_into().unwrap()),
|
||||
u16::from_be_bytes(ipv6[14..15].try_into().unwrap()),
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -243,7 +306,7 @@ pub fn ipaddress_to_ipaddr(other: IpAddress) -> IpAddr {
|
|||
let octets = ipv6.0;
|
||||
IpAddr::V6(Ipv6Addr::from(octets))
|
||||
}
|
||||
_ => unimplemented!()
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -253,21 +316,30 @@ impl fmt::Display for NetIpAddr {
|
|||
NetIpAddr::Ipv4(octets) => {
|
||||
// Fast Path: if there's no alignment stuff, write directly to the buffer
|
||||
if fmt.precision().is_none() && fmt.width().is_none() {
|
||||
write!(fmt, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3])
|
||||
write!(
|
||||
fmt,
|
||||
"{}.{}.{}.{}",
|
||||
octets[0], octets[1], octets[2], octets[3]
|
||||
)
|
||||
} else {
|
||||
const IPV4_BUF_LEN: usize = 15; // Long enough for the longest possible IPv4 address
|
||||
let mut buf = [0u8; IPV4_BUF_LEN];
|
||||
let mut buf_slice = &mut buf[..];
|
||||
|
||||
// Note: The call to write should never fail, hence the unwrap
|
||||
write!(buf_slice, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3]).unwrap();
|
||||
write!(
|
||||
buf_slice,
|
||||
"{}.{}.{}.{}",
|
||||
octets[0], octets[1], octets[2], octets[3]
|
||||
)
|
||||
.unwrap();
|
||||
let len = IPV4_BUF_LEN - buf_slice.len();
|
||||
|
||||
// This unsafe is OK because we know what is being written to the buffer
|
||||
let buf = unsafe { std::str::from_utf8_unchecked(&buf[..len]) };
|
||||
fmt.pad(buf)
|
||||
}
|
||||
},
|
||||
}
|
||||
NetIpAddr::Ipv6(ip) => ip.fmt(fmt),
|
||||
}
|
||||
}
|
||||
|
|
@ -307,7 +379,7 @@ impl XousScalarEndpoint {
|
|||
XousScalarEndpoint {
|
||||
cid: None,
|
||||
op: None,
|
||||
args: [None; 4]
|
||||
args: [None; 4],
|
||||
}
|
||||
}
|
||||
pub(crate) fn get(&self) -> (Option<xous::CID>, Option<usize>, [Option<usize>; 4]) {
|
||||
|
|
@ -333,11 +405,11 @@ impl XousScalarEndpoint {
|
|||
cid,
|
||||
xous::Message::new_scalar(
|
||||
op,
|
||||
if let Some(a) = self.args[0] {a} else {0},
|
||||
if let Some(a) = self.args[1] {a} else {0},
|
||||
if let Some(a) = self.args[2] {a} else {0},
|
||||
if let Some(a) = self.args[3] {a} else {0},
|
||||
)
|
||||
self.args[0].unwrap_or_default(),
|
||||
self.args[1].unwrap_or_default(),
|
||||
self.args[2].unwrap_or_default(),
|
||||
self.args[3].unwrap_or_default(),
|
||||
),
|
||||
) {
|
||||
Ok(_) => (),
|
||||
Err(e) => {
|
||||
|
|
@ -356,18 +428,63 @@ impl XousScalarEndpoint {
|
|||
log::trace!("custom args");
|
||||
if let Some(cid) = self.cid {
|
||||
if let Some(op) = self.op {
|
||||
log::trace!("ca: {} {} cust{:?} self{:?} 0:{}", cid, op, custom, self.args,
|
||||
if let Some(b) = custom[0] {b as usize} else { if let Some(a) = self.args[0] {a} else {0} }
|
||||
log::trace!(
|
||||
"ca: {} {} cust{:?} self{:?} 0:{}",
|
||||
cid,
|
||||
op,
|
||||
custom,
|
||||
self.args,
|
||||
if let Some(b) = custom[0] {
|
||||
b as usize
|
||||
} else {
|
||||
if let Some(a) = self.args[0] {
|
||||
a
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
);
|
||||
match xous::send_message(
|
||||
cid,
|
||||
xous::Message::new_scalar(
|
||||
op,
|
||||
if let Some(b) = custom[0] {b as usize} else { if let Some(a) = self.args[0] {a} else {0} },
|
||||
if let Some(b) = custom[1] {b as usize} else { if let Some(a) = self.args[1] {a} else {0} },
|
||||
if let Some(b) = custom[2] {b as usize} else { if let Some(a) = self.args[2] {a} else {0} },
|
||||
if let Some(b) = custom[3] {b as usize} else { if let Some(a) = self.args[3] {a} else {0} },
|
||||
)
|
||||
if let Some(b) = custom[0] {
|
||||
b as usize
|
||||
} else {
|
||||
if let Some(a) = self.args[0] {
|
||||
a
|
||||
} else {
|
||||
0
|
||||
}
|
||||
},
|
||||
if let Some(b) = custom[1] {
|
||||
b as usize
|
||||
} else {
|
||||
if let Some(a) = self.args[1] {
|
||||
a
|
||||
} else {
|
||||
0
|
||||
}
|
||||
},
|
||||
if let Some(b) = custom[2] {
|
||||
b as usize
|
||||
} else {
|
||||
if let Some(a) = self.args[2] {
|
||||
a
|
||||
} else {
|
||||
0
|
||||
}
|
||||
},
|
||||
if let Some(b) = custom[3] {
|
||||
b as usize
|
||||
} else {
|
||||
if let Some(a) = self.args[3] {
|
||||
a
|
||||
} else {
|
||||
0
|
||||
}
|
||||
},
|
||||
),
|
||||
) {
|
||||
Ok(_) => (),
|
||||
Err(e) => {
|
||||
|
|
@ -380,7 +497,6 @@ impl XousScalarEndpoint {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/// This is a "generic" object for registering a hook between a remote, potentially
|
||||
/// untrusted server and my process. The SID filled into the hook should be a
|
||||
/// "one time" (or perhaps better phrased as "single purpose") SID. In other words, it's
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue