Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library/std/src/os/unix/net/datagram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl UnixDatagram {
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
pub fn unbound() -> io::Result<UnixDatagram> {
let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_DGRAM)?;
let inner = Socket::new(libc::AF_UNIX, libc::SOCK_DGRAM)?;
Ok(UnixDatagram(inner))
}

Expand Down
4 changes: 2 additions & 2 deletions library/std/src/os/unix/net/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl UnixListener {
#[stable(feature = "unix_socket", since = "1.10.0")]
pub fn bind<P: AsRef<Path>>(path: P) -> io::Result<UnixListener> {
unsafe {
let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
let inner = Socket::new(libc::AF_UNIX, libc::SOCK_STREAM)?;
let (addr, len) = sockaddr_un(path.as_ref())?;
#[cfg(any(
target_os = "windows",
Expand Down Expand Up @@ -136,7 +136,7 @@ impl UnixListener {
#[stable(feature = "unix_socket_abstract", since = "1.70.0")]
pub fn bind_addr(socket_addr: &SocketAddr) -> io::Result<UnixListener> {
unsafe {
let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
let inner = Socket::new(libc::AF_UNIX, libc::SOCK_STREAM)?;
#[cfg(target_os = "linux")]
const backlog: core::ffi::c_int = -1;
#[cfg(not(target_os = "linux"))]
Expand Down
4 changes: 2 additions & 2 deletions library/std/src/os/unix/net/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl UnixStream {
#[stable(feature = "unix_socket", since = "1.10.0")]
pub fn connect<P: AsRef<Path>>(path: P) -> io::Result<UnixStream> {
unsafe {
let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
let inner = Socket::new(libc::AF_UNIX, libc::SOCK_STREAM)?;
let (addr, len) = sockaddr_un(path.as_ref())?;

cvt(libc::connect(inner.as_raw_fd(), (&raw const addr) as *const _, len))?;
Expand Down Expand Up @@ -139,7 +139,7 @@ impl UnixStream {
#[stable(feature = "unix_socket_abstract", since = "1.70.0")]
pub fn connect_addr(socket_addr: &SocketAddr) -> io::Result<UnixStream> {
unsafe {
let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
let inner = Socket::new(libc::AF_UNIX, libc::SOCK_STREAM)?;
cvt(libc::connect(
inner.as_raw_fd(),
(&raw const socket_addr.addr) as *const _,
Expand Down
10 changes: 1 addition & 9 deletions library/std/src/sys/net/connection/socket/hermit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,7 @@ pub fn init() {}
pub struct Socket(FileDesc);

impl Socket {
pub fn new(addr: &SocketAddr, ty: i32) -> io::Result<Socket> {
let fam = match *addr {
SocketAddr::V4(..) => netc::AF_INET,
SocketAddr::V6(..) => netc::AF_INET6,
};
Socket::new_raw(fam, ty)
}

pub fn new_raw(fam: i32, ty: i32) -> io::Result<Socket> {
pub fn new(fam: i32, ty: i32) -> io::Result<Socket> {
let fd = cvt(unsafe { netc::socket(fam, ty, 0) })?;
Ok(Socket(unsafe { FileDesc::from_raw_fd(fd) }))
}
Expand Down
15 changes: 11 additions & 4 deletions library/std/src/sys/net/connection/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ fn socket_addr_to_c(addr: &SocketAddr) -> (SocketAddrCRepr, c::socklen_t) {
}
}

fn addr_family(addr: &SocketAddr) -> c_int {
match addr {
SocketAddr::V4(..) => c::AF_INET,
SocketAddr::V6(..) => c::AF_INET6,
}
}

/// Converts the C socket address stored in `storage` to a Rust `SocketAddr`.
///
/// # Safety
Expand Down Expand Up @@ -364,7 +371,7 @@ impl TcpStream {
return each_addr(addr, inner);

fn inner(addr: &SocketAddr) -> io::Result<TcpStream> {
let sock = Socket::new(addr, c::SOCK_STREAM)?;
let sock = Socket::new(addr_family(addr), c::SOCK_STREAM)?;
sock.connect(addr)?;
Ok(TcpStream { inner: sock })
}
Expand All @@ -373,7 +380,7 @@ impl TcpStream {
pub fn connect_timeout(addr: &SocketAddr, timeout: Duration) -> io::Result<TcpStream> {
init();

let sock = Socket::new(addr, c::SOCK_STREAM)?;
let sock = Socket::new(addr_family(addr), c::SOCK_STREAM)?;
sock.connect_timeout(addr, timeout)?;
Ok(TcpStream { inner: sock })
}
Expand Down Expand Up @@ -535,7 +542,7 @@ impl TcpListener {
return each_addr(addr, inner);

fn inner(addr: &SocketAddr) -> io::Result<TcpListener> {
let sock = Socket::new(addr, c::SOCK_STREAM)?;
let sock = Socket::new(addr_family(addr), c::SOCK_STREAM)?;

// On platforms with Berkeley-derived sockets, this allows to quickly
// rebind a socket, without needing to wait for the OS to clean up the
Expand Down Expand Up @@ -661,7 +668,7 @@ impl UdpSocket {
return each_addr(addr, inner);

fn inner(addr: &SocketAddr) -> io::Result<UdpSocket> {
let sock = Socket::new(addr, c::SOCK_DGRAM)?;
let sock = Socket::new(addr_family(addr), c::SOCK_DGRAM)?;
let (addr, len) = socket_addr_to_c(addr);
cvt(unsafe { c::bind(sock.as_raw(), addr.as_ptr(), len as _) })?;
Ok(UdpSocket { inner: sock })
Expand Down
16 changes: 3 additions & 13 deletions library/std/src/sys/net/connection/socket/solid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,9 @@ pub fn init() {}
pub struct Socket(OwnedFd);

impl Socket {
pub fn new(addr: &SocketAddr, ty: c_int) -> io::Result<Socket> {
let fam = match *addr {
SocketAddr::V4(..) => netc::AF_INET,
SocketAddr::V6(..) => netc::AF_INET6,
};
Socket::new_raw(fam, ty)
}

pub fn new_raw(fam: c_int, ty: c_int) -> io::Result<Socket> {
unsafe {
let fd = cvt(netc::socket(fam, ty, 0))?;
Ok(Self::from_raw_fd(fd))
}
pub fn new(fam: c_int, ty: c_int) -> io::Result<Socket> {
let fd = cvt(unsafe { netc::socket(fam, ty, 0) })?;
Ok(unsafe { Self::from_raw_fd(fd) })
}

pub fn connect(&self, addr: &SocketAddr) -> io::Result<()> {
Expand Down
14 changes: 3 additions & 11 deletions library/std/src/sys/net/connection/socket/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,7 @@ pub fn cvt_gai(err: c_int) -> io::Result<()> {
}

impl Socket {
pub fn new(addr: &SocketAddr, ty: c_int) -> io::Result<Socket> {
let fam = match *addr {
SocketAddr::V4(..) => libc::AF_INET,
SocketAddr::V6(..) => libc::AF_INET6,
};
Socket::new_raw(fam, ty)
}

pub fn new_raw(fam: c_int, ty: c_int) -> io::Result<Socket> {
pub fn new(family: c_int, ty: c_int) -> io::Result<Socket> {
cfg_select! {
any(
target_os = "android",
Expand All @@ -89,7 +81,7 @@ impl Socket {
// On platforms that support it we pass the SOCK_CLOEXEC
// flag to atomically create the socket and set it as
// CLOEXEC. On Linux this was added in 2.6.27.
let fd = cvt(unsafe { libc::socket(fam, ty | libc::SOCK_CLOEXEC, 0) })?;
let fd = cvt(unsafe { libc::socket(family, ty | libc::SOCK_CLOEXEC, 0) })?;
let socket = Socket(unsafe { FileDesc::from_raw_fd(fd) });

// DragonFlyBSD, FreeBSD and NetBSD use `SO_NOSIGPIPE` as a `setsockopt`
Expand All @@ -100,7 +92,7 @@ impl Socket {
Ok(socket)
}
_ => {
let fd = cvt(unsafe { libc::socket(fam, ty, 0) })?;
let fd = cvt(unsafe { libc::socket(family, ty, 0) })?;
let fd = unsafe { FileDesc::from_raw_fd(fd) };
fd.set_cloexec()?;
let socket = Socket(fd);
Expand Down
12 changes: 2 additions & 10 deletions library/std/src/sys/net/connection/socket/wasip2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,8 @@ pub struct WasiSocket(OwnedFd);
pub struct Socket(WasiSocket);

impl Socket {
pub fn new(addr: &SocketAddr, ty: c_int) -> io::Result<Socket> {
let fam = match *addr {
SocketAddr::V4(..) => netc::AF_INET,
SocketAddr::V6(..) => netc::AF_INET6,
};
Socket::new_raw(fam, ty)
}

pub fn new_raw(fam: c_int, ty: c_int) -> io::Result<Socket> {
let fd = cvt(unsafe { netc::socket(fam, ty, 0) })?;
pub fn new(family: c_int, ty: c_int) -> io::Result<Socket> {
let fd = cvt(unsafe { netc::socket(family, ty, 0) })?;
Ok(unsafe { Self::from_raw_fd(fd) })
}

Expand Down
6 changes: 1 addition & 5 deletions library/std/src/sys/net/connection/socket/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,7 @@ pub use crate::sys::pal::winsock::{cvt, cvt_gai, cvt_r, startup as init};
pub struct Socket(OwnedSocket);

impl Socket {
pub fn new(addr: &SocketAddr, ty: c_int) -> io::Result<Socket> {
let family = match *addr {
SocketAddr::V4(..) => netc::AF_INET,
SocketAddr::V6(..) => netc::AF_INET6,
};
pub fn new(family: c_int, ty: c_int) -> io::Result<Socket> {
let socket = unsafe {
c::WSASocketW(
family,
Expand Down
Loading