Skip to content

Commit e273126

Browse files
authored
Rollup merge of rust-lang#147190 - joboet:sys-net-cleanup, r=hkBst,tgross35
std: `sys::net` cleanups This PR contains three improvements to the socket-based networking implementation (aa1263e is just to add the now missing `unsafe`). Best reviewed commit-by-commit.
2 parents 09e3aba + 3534594 commit e273126

File tree

10 files changed

+251
-220
lines changed

10 files changed

+251
-220
lines changed

library/std/src/os/unix/net/datagram.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ impl UnixDatagram {
159159
/// ```
160160
#[stable(feature = "unix_socket", since = "1.10.0")]
161161
pub fn unbound() -> io::Result<UnixDatagram> {
162-
let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_DGRAM)?;
162+
let inner = Socket::new(libc::AF_UNIX, libc::SOCK_DGRAM)?;
163163
Ok(UnixDatagram(inner))
164164
}
165165

library/std/src/os/unix/net/listener.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl UnixListener {
7171
#[stable(feature = "unix_socket", since = "1.10.0")]
7272
pub fn bind<P: AsRef<Path>>(path: P) -> io::Result<UnixListener> {
7373
unsafe {
74-
let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
74+
let inner = Socket::new(libc::AF_UNIX, libc::SOCK_STREAM)?;
7575
let (addr, len) = sockaddr_un(path.as_ref())?;
7676
#[cfg(any(
7777
target_os = "windows",
@@ -136,7 +136,7 @@ impl UnixListener {
136136
#[stable(feature = "unix_socket_abstract", since = "1.70.0")]
137137
pub fn bind_addr(socket_addr: &SocketAddr) -> io::Result<UnixListener> {
138138
unsafe {
139-
let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
139+
let inner = Socket::new(libc::AF_UNIX, libc::SOCK_STREAM)?;
140140
#[cfg(target_os = "linux")]
141141
const backlog: core::ffi::c_int = -1;
142142
#[cfg(not(target_os = "linux"))]

library/std/src/os/unix/net/stream.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl UnixStream {
105105
#[stable(feature = "unix_socket", since = "1.10.0")]
106106
pub fn connect<P: AsRef<Path>>(path: P) -> io::Result<UnixStream> {
107107
unsafe {
108-
let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
108+
let inner = Socket::new(libc::AF_UNIX, libc::SOCK_STREAM)?;
109109
let (addr, len) = sockaddr_un(path.as_ref())?;
110110

111111
cvt(libc::connect(inner.as_raw_fd(), (&raw const addr) as *const _, len))?;
@@ -139,7 +139,7 @@ impl UnixStream {
139139
#[stable(feature = "unix_socket_abstract", since = "1.70.0")]
140140
pub fn connect_addr(socket_addr: &SocketAddr) -> io::Result<UnixStream> {
141141
unsafe {
142-
let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
142+
let inner = Socket::new(libc::AF_UNIX, libc::SOCK_STREAM)?;
143143
cvt(libc::connect(
144144
inner.as_raw_fd(),
145145
(&raw const socket_addr.addr) as *const _,

library/std/src/sys/net/connection/socket/hermit.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,7 @@ pub fn init() {}
3737
pub struct Socket(FileDesc);
3838

3939
impl Socket {
40-
pub fn new(addr: &SocketAddr, ty: i32) -> io::Result<Socket> {
41-
let fam = match *addr {
42-
SocketAddr::V4(..) => netc::AF_INET,
43-
SocketAddr::V6(..) => netc::AF_INET6,
44-
};
45-
Socket::new_raw(fam, ty)
46-
}
47-
48-
pub fn new_raw(fam: i32, ty: i32) -> io::Result<Socket> {
40+
pub fn new(fam: i32, ty: i32) -> io::Result<Socket> {
4941
let fd = cvt(unsafe { netc::socket(fam, ty, 0) })?;
5042
Ok(Socket(unsafe { FileDesc::from_raw_fd(fd) }))
5143
}
@@ -242,11 +234,11 @@ impl Socket {
242234
None => netc::timeval { tv_sec: 0, tv_usec: 0 },
243235
};
244236

245-
setsockopt(self, netc::SOL_SOCKET, kind, timeout)
237+
unsafe { setsockopt(self, netc::SOL_SOCKET, kind, timeout) }
246238
}
247239

248240
pub fn timeout(&self, kind: i32) -> io::Result<Option<Duration>> {
249-
let raw: netc::timeval = getsockopt(self, netc::SOL_SOCKET, kind)?;
241+
let raw: netc::timeval = unsafe { getsockopt(self, netc::SOL_SOCKET, kind)? };
250242
if raw.tv_sec == 0 && raw.tv_usec == 0 {
251243
Ok(None)
252244
} else {
@@ -272,22 +264,22 @@ impl Socket {
272264
l_linger: linger.unwrap_or_default().as_secs() as libc::c_int,
273265
};
274266

275-
setsockopt(self, netc::SOL_SOCKET, netc::SO_LINGER, linger)
267+
unsafe { setsockopt(self, netc::SOL_SOCKET, netc::SO_LINGER, linger) }
276268
}
277269

278270
pub fn linger(&self) -> io::Result<Option<Duration>> {
279-
let val: netc::linger = getsockopt(self, netc::SOL_SOCKET, netc::SO_LINGER)?;
271+
let val: netc::linger = unsafe { getsockopt(self, netc::SOL_SOCKET, netc::SO_LINGER)? };
280272

281273
Ok((val.l_onoff != 0).then(|| Duration::from_secs(val.l_linger as u64)))
282274
}
283275

284276
pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
285277
let value: i32 = if nodelay { 1 } else { 0 };
286-
setsockopt(self, netc::IPPROTO_TCP, netc::TCP_NODELAY, value)
278+
unsafe { setsockopt(self, netc::IPPROTO_TCP, netc::TCP_NODELAY, value) }
287279
}
288280

289281
pub fn nodelay(&self) -> io::Result<bool> {
290-
let raw: i32 = getsockopt(self, netc::IPPROTO_TCP, netc::TCP_NODELAY)?;
282+
let raw: i32 = unsafe { getsockopt(self, netc::IPPROTO_TCP, netc::TCP_NODELAY)? };
291283
Ok(raw != 0)
292284
}
293285

@@ -304,7 +296,7 @@ impl Socket {
304296
}
305297

306298
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
307-
let raw: c_int = getsockopt(self, libc::SOL_SOCKET, libc::SO_ERROR)?;
299+
let raw: c_int = unsafe { getsockopt(self, libc::SOL_SOCKET, libc::SO_ERROR)? };
308300
if raw == 0 { Ok(None) } else { Ok(Some(io::Error::from_raw_os_error(raw as i32))) }
309301
}
310302

0 commit comments

Comments
 (0)