diff --git a/services/net/src/api.rs b/services/net/src/api.rs index 47731a263..b790de5f8 100644 --- a/services/net/src/api.rs +++ b/services/net/src/api.rs @@ -38,6 +38,7 @@ pub(crate) enum Opcode { /// Calls for TCP implementation TcpConnect, TcpTx, + TcpClose, // The DNS server can hook the Net crate for notifications on config updates /// Adds an Ipv4 as a DNS server diff --git a/services/net/src/api/tcp.rs b/services/net/src/api/tcp.rs index c057f6529..767479abe 100644 --- a/services/net/src/api/tcp.rs +++ b/services/net/src/api/tcp.rs @@ -5,7 +5,7 @@ use com::api::NET_MTU; pub(crate) const TCP_BUFFER_SIZE: usize = NET_MTU; #[derive(Debug, Archive, Serialize, Deserialize, Copy, Clone)] -pub(crate) struct NetTcpConnect { +pub(crate) struct NetTcpManage { pub(crate) cb_sid: [u32; 4], pub(crate) ip_addr: NetIpAddr, pub(crate) remote_port: u16, @@ -40,7 +40,9 @@ pub(crate) struct NetTcpResponse { pub(crate) struct NetTcpTransmit { pub remote_addr: NetIpAddr, pub remote_port: u16, - pub local_port: u16, // this was given back in the connect, we need this to distinguish between multiple connections to the same server + // this was given back in the connect, we need this to distinguish between multiple connections to the same server + pub local_port: u16, + // this is changed to the number of bytes actually sent on return pub len: u16, pub data: [u8; TCP_BUFFER_SIZE], pub result: Option, diff --git a/services/net/src/main.rs b/services/net/src/main.rs index cbc9178a5..46ddc4544 100644 --- a/services/net/src/main.rs +++ b/services/net/src/main.rs @@ -402,7 +402,7 @@ fn xmain() -> ! { }), Some(Opcode::TcpConnect) => { let mut buf = unsafe{Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap())}; - let mut tcpspec = buf.to_original::().unwrap(); // need to define this + let mut tcpspec = buf.to_original::().unwrap(); // need to define this let address = IpAddress::from(tcpspec.ip_addr); let remote_port = tcpspec.remote_port; @@ -455,9 +455,12 @@ fn xmain() -> ! { }; if let Some(tcp_state) = tcp_handles.get(&connection) { let mut socket = sockets.get::(tcp_state.handle); - if socket.may_send() { + if socket.can_send() { tcp_tx.result = match socket.send_slice(&tcp_tx.data) { - Ok(_) => Some(NetMemResponse::Ok), + Ok(octets) => { + tcp_tx.len = octets as u16; + Some(NetMemResponse::Ok) + }, Err(_) => Some(NetMemResponse::LibraryError), } } else { @@ -469,6 +472,28 @@ fn xmain() -> ! { } buf.replace(tcp_tx).unwrap(); }, + Some(Opcode::TcpClose) => { + let mut buf = unsafe{Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap())}; + let mut tcpspec = buf.to_original::().unwrap(); // need to define this + let address = IpAddress::from(tcpspec.ip_addr); + let remote_port = tcpspec.remote_port; + if let Some(local_port) = tcpspec.local_port { + let connection = TcpConnection { + remote: IpAddress::from(tcpspec.ip_addr), + remote_port: tcpspec.remote_port, + local_port, + }; + if let Some(tcp_state) = tcp_handles.remove(&connection) { + sockets.get::(tcp_state.handle).close(); + tcpspec.result = Some(NetMemResponse::Ok); + } else { + tcpspec.result = Some(NetMemResponse::Invalid); + } + } else { + tcpspec.result = Some(NetMemResponse::Invalid); + } + buf.replace(tcpspec).unwrap(); + } Some(Opcode::UdpBind) => { let mut buf = unsafe{Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap())}; let udpspec = buf.to_original::().unwrap(); diff --git a/services/net/src/protocols/tcp_stream.rs b/services/net/src/protocols/tcp_stream.rs index 1cff58406..fccb692a3 100644 --- a/services/net/src/protocols/tcp_stream.rs +++ b/services/net/src/protocols/tcp_stream.rs @@ -89,30 +89,6 @@ impl TcpStream { unimplemented!() } - pub fn read(&self, _: &mut [u8]) -> io::Result { - unimplemented!() - } - - pub fn read_vectored(&self, _: &mut [IoSliceMut<'_>]) -> io::Result { - unimplemented!() - } - - pub fn is_read_vectored(&self) -> bool { - unimplemented!() - } - - pub fn write(&self, _: &[u8]) -> io::Result { - unimplemented!() - } - - pub fn write_vectored(&self, _: &[IoSlice<'_>]) -> io::Result { - unimplemented!() - } - - pub fn is_write_vectored(&self) -> bool { - unimplemented!() - } - pub fn peer_addr(&self) -> io::Result { unimplemented!() } @@ -167,13 +143,19 @@ impl fmt::Debug for TcpStream { unimplemented!() } } -/* + +impl Drop for TcpStream { + // do the close message here +} + impl Read for TcpStream { - + fn read(&self, _: &mut [u8]) -> io::Result { + unimplemented!() + } } impl Write for TcpStream { - - + fn write(&self, _: &[u8]) -> io::Result { + unimplemented!() + } } -*/ \ No newline at end of file