@@ -87,7 +87,9 @@ use std::os::windows::io::{AsSocket, BorrowedSocket, OwnedSocket};
87
87
use futures_io:: { AsyncRead , AsyncWrite } ;
88
88
use futures_lite:: stream:: { self , Stream } ;
89
89
use futures_lite:: { future, pin, ready} ;
90
- use socket2:: { Domain , Protocol , SockAddr , Socket , Type } ;
90
+
91
+ use rustix:: io as rio;
92
+ use rustix:: net as rn;
91
93
92
94
use crate :: reactor:: { Reactor , Source } ;
93
95
@@ -1386,10 +1388,15 @@ impl Async<TcpStream> {
1386
1388
/// # std::io::Result::Ok(()) });
1387
1389
/// ```
1388
1390
pub async fn connect < A : Into < SocketAddr > > ( addr : A ) -> io:: Result < Async < TcpStream > > {
1389
- // Begin async connect .
1391
+ // Figure out how to handle this address .
1390
1392
let addr = addr. into ( ) ;
1391
- let domain = Domain :: for_address ( addr) ;
1392
- let socket = connect ( addr. into ( ) , domain, Some ( Protocol :: TCP ) ) ?;
1393
+ let ( domain, sock_addr) = match addr {
1394
+ SocketAddr :: V4 ( v4) => ( rn:: AddressFamily :: INET , rn:: SocketAddrAny :: V4 ( v4) ) ,
1395
+ SocketAddr :: V6 ( v6) => ( rn:: AddressFamily :: INET6 , rn:: SocketAddrAny :: V6 ( v6) ) ,
1396
+ } ;
1397
+
1398
+ // Begin async connect.
1399
+ let socket = connect ( sock_addr, domain, Some ( rn:: ipproto:: TCP ) ) ?;
1393
1400
let stream = Async :: new ( TcpStream :: from ( socket) ) ?;
1394
1401
1395
1402
// The stream becomes writable when connected.
@@ -1718,7 +1725,11 @@ impl Async<UnixStream> {
1718
1725
/// ```
1719
1726
pub async fn connect < P : AsRef < Path > > ( path : P ) -> io:: Result < Async < UnixStream > > {
1720
1727
// Begin async connect.
1721
- let socket = connect ( SockAddr :: unix ( path) ?, Domain :: UNIX , None ) ?;
1728
+ let socket = connect (
1729
+ rn:: SocketAddrUnix :: new ( path. as_ref ( ) ) ?. into ( ) ,
1730
+ rn:: AddressFamily :: UNIX ,
1731
+ None ,
1732
+ ) ?;
1722
1733
let stream = Async :: new ( UnixStream :: from ( socket) ) ?;
1723
1734
1724
1735
// The stream becomes writable when connected.
@@ -1928,8 +1939,11 @@ async fn optimistic(fut: impl Future<Output = io::Result<()>>) -> io::Result<()>
1928
1939
. await
1929
1940
}
1930
1941
1931
- fn connect ( addr : SockAddr , domain : Domain , protocol : Option < Protocol > ) -> io:: Result < Socket > {
1932
- let sock_type = Type :: STREAM ;
1942
+ fn connect (
1943
+ addr : rn:: SocketAddrAny ,
1944
+ domain : rn:: AddressFamily ,
1945
+ protocol : Option < rn:: Protocol > ,
1946
+ ) -> io:: Result < rustix:: fd:: OwnedFd > {
1933
1947
#[ cfg( any(
1934
1948
target_os = "android" ,
1935
1949
target_os = "dragonfly" ,
@@ -1940,10 +1954,13 @@ fn connect(addr: SockAddr, domain: Domain, protocol: Option<Protocol>) -> io::Re
1940
1954
target_os = "netbsd" ,
1941
1955
target_os = "openbsd"
1942
1956
) ) ]
1943
- // If we can, set nonblocking at socket creation for unix
1944
- let sock_type = sock_type. nonblocking ( ) ;
1945
- // This automatically handles cloexec on unix, no_inherit on windows and nosigpipe on macos
1946
- let socket = Socket :: new ( domain, sock_type, protocol) ?;
1957
+ let socket = rn:: socket_with (
1958
+ domain,
1959
+ rn:: SocketType :: STREAM ,
1960
+ rn:: SocketFlags :: CLOEXEC | rn:: SocketFlags :: NONBLOCK ,
1961
+ protocol,
1962
+ ) ?;
1963
+
1947
1964
#[ cfg( not( any(
1948
1965
target_os = "android" ,
1949
1966
target_os = "dragonfly" ,
@@ -1954,14 +1971,62 @@ fn connect(addr: SockAddr, domain: Domain, protocol: Option<Protocol>) -> io::Re
1954
1971
target_os = "netbsd" ,
1955
1972
target_os = "openbsd"
1956
1973
) ) ) ]
1957
- // If the current platform doesn't support nonblocking at creation, enable it after creation
1958
- socket. set_nonblocking ( true ) ?;
1959
- match socket. connect ( & addr) {
1974
+ let socket = {
1975
+ #[ cfg( not( any(
1976
+ target_os = "macos" ,
1977
+ target_os = "ios" ,
1978
+ target_os = "tvos" ,
1979
+ target_os = "watchos" ,
1980
+ windows,
1981
+ target_os = "aix" ,
1982
+ target_os = "haiku"
1983
+ ) ) ) ]
1984
+ let flags = rn:: SocketFlags :: CLOEXEC ;
1985
+ #[ cfg( any(
1986
+ target_os = "macos" ,
1987
+ target_os = "ios" ,
1988
+ target_os = "tvos" ,
1989
+ target_os = "watchos" ,
1990
+ windows,
1991
+ target_os = "aix" ,
1992
+ target_os = "haiku"
1993
+ ) ) ]
1994
+ let flags = rn:: SocketFlags :: empty ( ) ;
1995
+
1996
+ // Create the socket.
1997
+ let socket = rn:: socket_with ( domain, rn:: SocketFlags :: STREAM , flags, protocol) ?;
1998
+
1999
+ // Set cloexec if necessary.
2000
+ #[ cfg( any(
2001
+ target_os = "macos" ,
2002
+ target_os = "ios" ,
2003
+ target_os = "tvos" ,
2004
+ target_os = "watchos"
2005
+ ) ) ]
2006
+ rio:: fcntl_setfd ( & socket, rio:: fcntl_getfd ( & socket) ? | rio:: FdFlags :: CLOEXEC ) ?;
2007
+
2008
+ // Set non-blocking mode.
2009
+ rio:: ioctl_fionbio ( & socket, true ) ?;
2010
+
2011
+ socket
2012
+ } ;
2013
+
2014
+ // Set nosigpipe if necessary.
2015
+ #[ cfg( any(
2016
+ target_os = "macos" ,
2017
+ target_os = "ios" ,
2018
+ target_os = "tvos" ,
2019
+ target_os = "watchos" ,
2020
+ target_os = "freebsd"
2021
+ ) ) ]
2022
+ rn:: sockopt:: set_socket_nosigpipe ( & socket, true ) ?;
2023
+
2024
+ match rn:: connect_any ( & socket, & addr) {
1960
2025
Ok ( _) => { }
1961
2026
#[ cfg( unix) ]
1962
- Err ( err ) if err . raw_os_error ( ) == Some ( rustix :: io :: Errno :: INPROGRESS . raw_os_error ( ) ) => { }
1963
- Err ( err ) if err . kind ( ) == io :: ErrorKind :: WouldBlock => { }
1964
- Err ( err) => return Err ( err) ,
2027
+ Err ( rio :: Errno :: INPROGRESS ) => { }
2028
+ Err ( rio :: Errno :: AGAIN ) => { }
2029
+ Err ( err) => return Err ( err. into ( ) ) ,
1965
2030
}
1966
2031
Ok ( socket)
1967
2032
}
0 commit comments