Skip to content

Commit 3534594

Browse files
committed
std: merge address family computation
1 parent aa1263e commit 3534594

File tree

9 files changed

+26
-57
lines changed

9 files changed

+26
-57
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: 1 addition & 9 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
}

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,13 @@ fn socket_addr_to_c(addr: &SocketAddr) -> (SocketAddrCRepr, c::socklen_t) {
178178
}
179179
}
180180

181+
fn addr_family(addr: &SocketAddr) -> c_int {
182+
match addr {
183+
SocketAddr::V4(..) => c::AF_INET,
184+
SocketAddr::V6(..) => c::AF_INET6,
185+
}
186+
}
187+
181188
/// Converts the C socket address stored in `storage` to a Rust `SocketAddr`.
182189
///
183190
/// # Safety
@@ -364,7 +371,7 @@ impl TcpStream {
364371
return each_addr(addr, inner);
365372

366373
fn inner(addr: &SocketAddr) -> io::Result<TcpStream> {
367-
let sock = Socket::new(addr, c::SOCK_STREAM)?;
374+
let sock = Socket::new(addr_family(addr), c::SOCK_STREAM)?;
368375
sock.connect(addr)?;
369376
Ok(TcpStream { inner: sock })
370377
}
@@ -373,7 +380,7 @@ impl TcpStream {
373380
pub fn connect_timeout(addr: &SocketAddr, timeout: Duration) -> io::Result<TcpStream> {
374381
init();
375382

376-
let sock = Socket::new(addr, c::SOCK_STREAM)?;
383+
let sock = Socket::new(addr_family(addr), c::SOCK_STREAM)?;
377384
sock.connect_timeout(addr, timeout)?;
378385
Ok(TcpStream { inner: sock })
379386
}
@@ -535,7 +542,7 @@ impl TcpListener {
535542
return each_addr(addr, inner);
536543

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

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

663670
fn inner(addr: &SocketAddr) -> io::Result<UdpSocket> {
664-
let sock = Socket::new(addr, c::SOCK_DGRAM)?;
671+
let sock = Socket::new(addr_family(addr), c::SOCK_DGRAM)?;
665672
let (addr, len) = socket_addr_to_c(addr);
666673
cvt(unsafe { c::bind(sock.as_raw(), addr.as_ptr(), len as _) })?;
667674
Ok(UdpSocket { inner: sock })

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

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,19 +115,9 @@ pub fn init() {}
115115
pub struct Socket(OwnedFd);
116116

117117
impl Socket {
118-
pub fn new(addr: &SocketAddr, ty: c_int) -> io::Result<Socket> {
119-
let fam = match *addr {
120-
SocketAddr::V4(..) => netc::AF_INET,
121-
SocketAddr::V6(..) => netc::AF_INET6,
122-
};
123-
Socket::new_raw(fam, ty)
124-
}
125-
126-
pub fn new_raw(fam: c_int, ty: c_int) -> io::Result<Socket> {
127-
unsafe {
128-
let fd = cvt(netc::socket(fam, ty, 0))?;
129-
Ok(Self::from_raw_fd(fd))
130-
}
118+
pub fn new(fam: c_int, ty: c_int) -> io::Result<Socket> {
119+
let fd = cvt(unsafe { netc::socket(fam, ty, 0) })?;
120+
Ok(unsafe { Self::from_raw_fd(fd) })
131121
}
132122

133123
pub fn connect(&self, addr: &SocketAddr) -> io::Result<()> {

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,7 @@ pub fn cvt_gai(err: c_int) -> io::Result<()> {
6363
}
6464

6565
impl Socket {
66-
pub fn new(addr: &SocketAddr, ty: c_int) -> io::Result<Socket> {
67-
let fam = match *addr {
68-
SocketAddr::V4(..) => libc::AF_INET,
69-
SocketAddr::V6(..) => libc::AF_INET6,
70-
};
71-
Socket::new_raw(fam, ty)
72-
}
73-
74-
pub fn new_raw(fam: c_int, ty: c_int) -> io::Result<Socket> {
66+
pub fn new(family: c_int, ty: c_int) -> io::Result<Socket> {
7567
cfg_select! {
7668
any(
7769
target_os = "android",
@@ -89,7 +81,7 @@ impl Socket {
8981
// On platforms that support it we pass the SOCK_CLOEXEC
9082
// flag to atomically create the socket and set it as
9183
// CLOEXEC. On Linux this was added in 2.6.27.
92-
let fd = cvt(unsafe { libc::socket(fam, ty | libc::SOCK_CLOEXEC, 0) })?;
84+
let fd = cvt(unsafe { libc::socket(family, ty | libc::SOCK_CLOEXEC, 0) })?;
9385
let socket = Socket(unsafe { FileDesc::from_raw_fd(fd) });
9486

9587
// DragonFlyBSD, FreeBSD and NetBSD use `SO_NOSIGPIPE` as a `setsockopt`
@@ -100,7 +92,7 @@ impl Socket {
10092
Ok(socket)
10193
}
10294
_ => {
103-
let fd = cvt(unsafe { libc::socket(fam, ty, 0) })?;
95+
let fd = cvt(unsafe { libc::socket(family, ty, 0) })?;
10496
let fd = unsafe { FileDesc::from_raw_fd(fd) };
10597
fd.set_cloexec()?;
10698
let socket = Socket(fd);

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,8 @@ pub struct WasiSocket(OwnedFd);
7474
pub struct Socket(WasiSocket);
7575

7676
impl Socket {
77-
pub fn new(addr: &SocketAddr, ty: c_int) -> io::Result<Socket> {
78-
let fam = match *addr {
79-
SocketAddr::V4(..) => netc::AF_INET,
80-
SocketAddr::V6(..) => netc::AF_INET6,
81-
};
82-
Socket::new_raw(fam, ty)
83-
}
84-
85-
pub fn new_raw(fam: c_int, ty: c_int) -> io::Result<Socket> {
86-
let fd = cvt(unsafe { netc::socket(fam, ty, 0) })?;
77+
pub fn new(family: c_int, ty: c_int) -> io::Result<Socket> {
78+
let fd = cvt(unsafe { netc::socket(family, ty, 0) })?;
8779
Ok(unsafe { Self::from_raw_fd(fd) })
8880
}
8981

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,7 @@ pub use crate::sys::pal::winsock::{cvt, cvt_gai, cvt_r, startup as init};
117117
pub struct Socket(OwnedSocket);
118118

119119
impl Socket {
120-
pub fn new(addr: &SocketAddr, ty: c_int) -> io::Result<Socket> {
121-
let family = match *addr {
122-
SocketAddr::V4(..) => netc::AF_INET,
123-
SocketAddr::V6(..) => netc::AF_INET6,
124-
};
120+
pub fn new(family: c_int, ty: c_int) -> io::Result<Socket> {
125121
let socket = unsafe {
126122
c::WSASocketW(
127123
family,

0 commit comments

Comments
 (0)