Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 71d41d9

Browse files
committed
add TcpListener support for HermitCore
Add basic support of TcpListerner for HermitCore. In addition, revise TcpStream to support peer_addr.
1 parent 1fb612b commit 71d41d9

File tree

1 file changed

+111
-46
lines changed

1 file changed

+111
-46
lines changed

src/libstd/sys/hermit/net.rs

Lines changed: 111 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
use crate::convert::TryFrom;
22
use crate::fmt;
33
use crate::io::{self, ErrorKind, IoSlice, IoSliceMut};
4-
use crate::net::{Ipv4Addr, Ipv6Addr, Shutdown, SocketAddr};
4+
use crate::net::{IpAddr, Ipv4Addr, Ipv6Addr, Shutdown, SocketAddr};
55
use crate::str;
6+
use crate::sync::Arc;
67
use crate::sys::hermit::abi;
8+
use crate::sys::hermit::abi::IpAddress::{Ipv4, Ipv6};
79
use crate::sys::{unsupported, Void};
10+
use crate::sys_common::AsInner;
811
use crate::time::Duration;
912

1013
/// Checks whether the HermitCore's socket interface has been started already, and
@@ -17,14 +20,37 @@ pub fn init() -> io::Result<()> {
1720
Ok(())
1821
}
1922

20-
pub struct TcpStream(abi::Handle);
23+
#[derive(Debug, Clone)]
24+
pub struct Socket(abi::Handle);
25+
26+
impl Socket {
27+
fn new(handle: abi::Handle) -> Socket {
28+
Socket(handle)
29+
}
30+
}
31+
32+
impl AsInner<abi::Handle> for Socket {
33+
fn as_inner(&self) -> &abi::Handle {
34+
&self.0
35+
}
36+
}
37+
38+
impl Drop for Socket {
39+
fn drop(&mut self) {
40+
let _ = abi::tcpstream::close(self.0);
41+
}
42+
}
43+
44+
45+
#[derive(Clone)]
46+
pub struct TcpStream(Arc<Socket>);
2147

2248
impl TcpStream {
2349
pub fn connect(addr: io::Result<&SocketAddr>) -> io::Result<TcpStream> {
2450
let addr = addr?;
2551

2652
match abi::tcpstream::connect(addr.ip().to_string().as_bytes(), addr.port(), None) {
27-
Ok(handle) => Ok(TcpStream(handle)),
53+
Ok(handle) => Ok(TcpStream(Arc::new(Socket(handle)))),
2854
_ => {
2955
Err(io::Error::new(ErrorKind::Other, "Unable to initiate a connection on a socket"))
3056
}
@@ -37,39 +63,42 @@ impl TcpStream {
3763
saddr.port(),
3864
Some(duration.as_millis() as u64),
3965
) {
40-
Ok(handle) => Ok(TcpStream(handle)),
66+
Ok(handle) => Ok(TcpStream(Arc::new(Socket(handle)))),
4167
_ => {
4268
Err(io::Error::new(ErrorKind::Other, "Unable to initiate a connection on a socket"))
4369
}
4470
}
4571
}
4672

4773
pub fn set_read_timeout(&self, duration: Option<Duration>) -> io::Result<()> {
48-
abi::tcpstream::set_read_timeout(self.0, duration.map(|d| d.as_millis() as u64))
74+
abi::tcpstream::set_read_timeout(*self.0.as_inner(), duration.map(|d| d.as_millis() as u64))
4975
.map_err(|_| io::Error::new(ErrorKind::Other, "Unable to set timeout value"))
5076
}
5177

5278
pub fn set_write_timeout(&self, duration: Option<Duration>) -> io::Result<()> {
53-
abi::tcpstream::set_write_timeout(self.0, duration.map(|d| d.as_millis() as u64))
54-
.map_err(|_| io::Error::new(ErrorKind::Other, "Unable to set timeout value"))
79+
abi::tcpstream::set_write_timeout(
80+
*self.0.as_inner(),
81+
duration.map(|d| d.as_millis() as u64),
82+
)
83+
.map_err(|_| io::Error::new(ErrorKind::Other, "Unable to set timeout value"))
5584
}
5685

5786
pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
58-
let duration = abi::tcpstream::get_read_timeout(self.0)
87+
let duration = abi::tcpstream::get_read_timeout(*self.0.as_inner())
5988
.map_err(|_| io::Error::new(ErrorKind::Other, "Unable to determine timeout value"))?;
6089

6190
Ok(duration.map(|d| Duration::from_millis(d)))
6291
}
6392

6493
pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
65-
let duration = abi::tcpstream::get_write_timeout(self.0)
94+
let duration = abi::tcpstream::get_write_timeout(*self.0.as_inner())
6695
.map_err(|_| io::Error::new(ErrorKind::Other, "Unable to determine timeout value"))?;
6796

6897
Ok(duration.map(|d| Duration::from_millis(d)))
6998
}
7099

71100
pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
72-
abi::tcpstream::peek(self.0, buf)
101+
abi::tcpstream::peek(*self.0.as_inner(), buf)
73102
.map_err(|_| io::Error::new(ErrorKind::Other, "set_nodelay failed"))
74103
}
75104

@@ -81,18 +110,11 @@ impl TcpStream {
81110
let mut size: usize = 0;
82111

83112
for i in ioslice.iter_mut() {
84-
let mut pos: usize = 0;
85-
86-
while pos < i.len() {
87-
let ret = abi::tcpstream::read(self.0, &mut i[pos..])
88-
.map_err(|_| io::Error::new(ErrorKind::Other, "Unable to read on socket"))?;
89-
90-
if ret == 0 {
91-
return Ok(size);
92-
} else {
93-
size += ret;
94-
pos += ret;
95-
}
113+
let ret = abi::tcpstream::read(*self.0.as_inner(), &mut i[0..])
114+
.map_err(|_| io::Error::new(ErrorKind::Other, "Unable to read on socket"))?;
115+
116+
if ret != 0 {
117+
size += ret;
96118
}
97119
}
98120

@@ -112,7 +134,7 @@ impl TcpStream {
112134
let mut size: usize = 0;
113135

114136
for i in ioslice.iter() {
115-
size += abi::tcpstream::write(self.0, i)
137+
size += abi::tcpstream::write(*self.0.as_inner(), i)
116138
.map_err(|_| io::Error::new(ErrorKind::Other, "Unable to write on socket"))?;
117139
}
118140

@@ -125,42 +147,64 @@ impl TcpStream {
125147
}
126148

127149
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
128-
Err(io::Error::new(ErrorKind::Other, "peer_addr isn't supported"))
150+
let (ipaddr, port) = abi::tcpstream::peer_addr(*self.0.as_inner())
151+
.map_err(|_| io::Error::new(ErrorKind::Other, "peer_addr failed"))?;
152+
153+
let saddr = match ipaddr {
154+
Ipv4(ref addr) => SocketAddr::new(
155+
IpAddr::V4(Ipv4Addr::new(addr.0[0], addr.0[1], addr.0[2], addr.0[3])),
156+
port,
157+
),
158+
Ipv6(ref addr) => SocketAddr::new(
159+
IpAddr::V6(Ipv6Addr::new(
160+
((addr.0[0] as u16) << 8) | addr.0[1] as u16,
161+
((addr.0[2] as u16) << 8) | addr.0[3] as u16,
162+
((addr.0[4] as u16) << 8) | addr.0[5] as u16,
163+
((addr.0[6] as u16) << 8) | addr.0[7] as u16,
164+
((addr.0[8] as u16) << 8) | addr.0[9] as u16,
165+
((addr.0[10] as u16) << 8) | addr.0[11] as u16,
166+
((addr.0[12] as u16) << 8) | addr.0[13] as u16,
167+
((addr.0[14] as u16) << 8) | addr.0[15] as u16)),
168+
port,
169+
),
170+
_ => {
171+
return Err(io::Error::new(ErrorKind::Other, "peer_addr failed"));
172+
},
173+
};
174+
175+
Ok(saddr)
129176
}
130177

131178
pub fn socket_addr(&self) -> io::Result<SocketAddr> {
132179
Err(io::Error::new(ErrorKind::Other, "socket_addr isn't supported"))
133180
}
134181

135182
pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
136-
abi::tcpstream::shutdown(self.0, how as i32)
183+
abi::tcpstream::shutdown(*self.0.as_inner(), how as i32)
137184
.map_err(|_| io::Error::new(ErrorKind::Other, "unable to shutdown socket"))
138185
}
139186

140187
pub fn duplicate(&self) -> io::Result<TcpStream> {
141-
let handle = abi::tcpstream::duplicate(self.0)
142-
.map_err(|_| io::Error::new(ErrorKind::Other, "unable to duplicate stream"))?;
143-
144-
Ok(TcpStream(handle))
188+
Ok(self.clone())
145189
}
146190

147191
pub fn set_nodelay(&self, mode: bool) -> io::Result<()> {
148-
abi::tcpstream::set_nodelay(self.0, mode)
192+
abi::tcpstream::set_nodelay(*self.0.as_inner(), mode)
149193
.map_err(|_| io::Error::new(ErrorKind::Other, "set_nodelay failed"))
150194
}
151195

152196
pub fn nodelay(&self) -> io::Result<bool> {
153-
abi::tcpstream::nodelay(self.0)
197+
abi::tcpstream::nodelay(*self.0.as_inner())
154198
.map_err(|_| io::Error::new(ErrorKind::Other, "nodelay failed"))
155199
}
156200

157201
pub fn set_ttl(&self, tll: u32) -> io::Result<()> {
158-
abi::tcpstream::set_tll(self.0, tll)
202+
abi::tcpstream::set_tll(*self.0.as_inner(), tll)
159203
.map_err(|_| io::Error::new(ErrorKind::Other, "unable to set TTL"))
160204
}
161205

162206
pub fn ttl(&self) -> io::Result<u32> {
163-
abi::tcpstream::get_tll(self.0)
207+
abi::tcpstream::get_tll(*self.0.as_inner())
164208
.map_err(|_| io::Error::new(ErrorKind::Other, "unable to get TTL"))
165209
}
166210

@@ -169,40 +213,61 @@ impl TcpStream {
169213
}
170214

171215
pub fn set_nonblocking(&self, mode: bool) -> io::Result<()> {
172-
abi::tcpstream::set_nonblocking(self.0, mode)
216+
abi::tcpstream::set_nonblocking(*self.0.as_inner(), mode)
173217
.map_err(|_| io::Error::new(ErrorKind::Other, "unable to set blocking mode"))
174218
}
175219
}
176220

177-
impl Drop for TcpStream {
178-
fn drop(&mut self) {
179-
let _ = abi::tcpstream::close(self.0);
180-
}
181-
}
182-
183221
impl fmt::Debug for TcpStream {
184222
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
185223
Ok(())
186224
}
187225
}
188226

189-
pub struct TcpListener(abi::Handle);
227+
#[derive(Clone)]
228+
pub struct TcpListener(SocketAddr);
190229

191230
impl TcpListener {
192-
pub fn bind(_: io::Result<&SocketAddr>) -> io::Result<TcpListener> {
193-
Err(io::Error::new(ErrorKind::Other, "not supported"))
231+
pub fn bind(addr: io::Result<&SocketAddr>) -> io::Result<TcpListener> {
232+
let addr = addr?;
233+
234+
Ok(TcpListener(*addr))
194235
}
195236

196237
pub fn socket_addr(&self) -> io::Result<SocketAddr> {
197-
Err(io::Error::new(ErrorKind::Other, "not supported"))
238+
Ok(self.0)
198239
}
199240

200241
pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
201-
Err(io::Error::new(ErrorKind::Other, "not supported"))
242+
let (handle, ipaddr, port) = abi::tcplistener::accept(self.0.port())
243+
.map_err(|_| io::Error::new(ErrorKind::Other, "accept failed"))?;
244+
let saddr = match ipaddr {
245+
Ipv4(ref addr) => SocketAddr::new(
246+
IpAddr::V4(Ipv4Addr::new(addr.0[0], addr.0[1], addr.0[2], addr.0[3])),
247+
port,
248+
),
249+
Ipv6(ref addr) => SocketAddr::new(
250+
IpAddr::V6(Ipv6Addr::new(
251+
((addr.0[0] as u16) << 8) | addr.0[1] as u16,
252+
((addr.0[2] as u16) << 8) | addr.0[3] as u16,
253+
((addr.0[4] as u16) << 8) | addr.0[5] as u16,
254+
((addr.0[6] as u16) << 8) | addr.0[7] as u16,
255+
((addr.0[8] as u16) << 8) | addr.0[9] as u16,
256+
((addr.0[10] as u16) << 8) | addr.0[11] as u16,
257+
((addr.0[12] as u16) << 8) | addr.0[13] as u16,
258+
((addr.0[14] as u16) << 8) | addr.0[15] as u16)),
259+
port,
260+
),
261+
_ => {
262+
return Err(io::Error::new(ErrorKind::Other, "accept failed"));
263+
},
264+
};
265+
266+
Ok((TcpStream(Arc::new(Socket(handle))), saddr))
202267
}
203268

204269
pub fn duplicate(&self) -> io::Result<TcpListener> {
205-
Err(io::Error::new(ErrorKind::Other, "not supported"))
270+
Ok(self.clone())
206271
}
207272

208273
pub fn set_ttl(&self, _: u32) -> io::Result<()> {

0 commit comments

Comments
 (0)