@@ -7,7 +7,7 @@ use byteorder::{BigEndian, ByteOrder};
77use gethostname:: gethostname;
88#[ cfg( all( unix, not( target_os = "redox" ) ) ) ]
99use nix:: unistd:: sethostname;
10- use socket2:: { Domain , Socket , Type as SocketType } ;
10+ use socket2:: { Domain , Protocol , Socket , Type as SocketType } ;
1111
1212use super :: os:: convert_io_error;
1313#[ cfg( unix) ]
@@ -27,7 +27,31 @@ type RawSocket = std::os::unix::io::RawFd;
2727type RawSocket = std:: os:: windows:: raw:: SOCKET ;
2828
2929#[ cfg( unix) ]
30- use libc as c;
30+ mod c {
31+ pub use libc:: * ;
32+ // TODO: open a PR to add these constants to libc; then just use libc
33+ #[ cfg( target_os = "android" ) ]
34+ pub const AI_PASSIVE : c_int = 0x00000001 ;
35+ #[ cfg( target_os = "android" ) ]
36+ pub const AI_CANONNAME : c_int = 0x00000002 ;
37+ #[ cfg( target_os = "android" ) ]
38+ pub const AI_NUMERICHOST : c_int = 0x00000004 ;
39+ #[ cfg( target_os = "android" ) ]
40+ pub const AI_NUMERICSERV : c_int = 0x00000008 ;
41+ #[ cfg( target_os = "android" ) ]
42+ pub const AI_MASK : c_int =
43+ AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST | AI_NUMERICSERV | AI_ADDRCONFIG ;
44+ #[ cfg( target_os = "android" ) ]
45+ pub const AI_ALL : c_int = 0x00000100 ;
46+ #[ cfg( target_os = "android" ) ]
47+ pub const AI_V4MAPPED_CFG : c_int = 0x00000200 ;
48+ #[ cfg( target_os = "android" ) ]
49+ pub const AI_ADDRCONFIG : c_int = 0x00000400 ;
50+ #[ cfg( target_os = "android" ) ]
51+ pub const AI_V4MAPPED : c_int = 0x00000800 ;
52+ #[ cfg( target_os = "android" ) ]
53+ pub const AI_DEFAULT : c_int = AI_V4MAPPED_CFG | AI_ADDRCONFIG ;
54+ }
3155#[ cfg( windows) ]
3256mod c {
3357 pub use winapi:: shared:: ws2def:: * ;
@@ -92,28 +116,17 @@ impl PySocket {
92116 unsafe { Socket :: from_raw_socket ( fileno) }
93117 }
94118 } else {
95- let domain = match family {
96- c:: AF_INET => Domain :: ipv4 ( ) ,
97- c:: AF_INET6 => Domain :: ipv6 ( ) ,
98- #[ cfg( unix) ]
99- c:: AF_UNIX => Domain :: unix ( ) ,
100- _ => {
101- return Err ( vm. new_os_error ( format ! ( "Unknown address family value: {}" , family) ) )
102- }
103- } ;
119+ let sock = Socket :: new (
120+ Domain :: from ( family) ,
121+ SocketType :: from ( socket_kind) ,
122+ Some ( Protocol :: from ( proto) ) ,
123+ )
124+ . map_err ( |err| convert_sock_error ( vm, err) ) ?;
125+
104126 self . family . set ( family) ;
105- let socket_type = match socket_kind {
106- c:: SOCK_STREAM => SocketType :: stream ( ) ,
107- c:: SOCK_DGRAM => SocketType :: dgram ( ) ,
108- _ => {
109- return Err (
110- vm. new_os_error ( format ! ( "Unknown socket kind value: {}" , socket_kind) )
111- )
112- }
113- } ;
114127 self . kind . set ( socket_kind) ;
115128 self . proto . set ( proto) ;
116- Socket :: new ( domain , socket_type , None ) . map_err ( |err| convert_sock_error ( vm , err ) ) ?
129+ sock
117130 } ;
118131 self . sock . replace ( sock) ;
119132 Ok ( ( ) )
@@ -435,6 +448,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
435448 "error" => ctx. exceptions. os_error. clone( ) ,
436449 "timeout" => socket_timeout,
437450 "gaierror" => socket_gaierror,
451+ "AF_UNSPEC" => ctx. new_int( 0 ) ,
438452 "AF_INET" => ctx. new_int( c:: AF_INET ) ,
439453 "AF_INET6" => ctx. new_int( c:: AF_INET6 ) ,
440454 "SOCK_STREAM" => ctx. new_int( c:: SOCK_STREAM ) ,
@@ -446,6 +460,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
446460 "MSG_PEEK" => ctx. new_int( c:: MSG_PEEK ) ,
447461 "MSG_WAITALL" => ctx. new_int( c:: MSG_WAITALL ) ,
448462 "AI_ALL" => ctx. new_int( c:: AI_ALL ) ,
463+ "AI_PASSIVE" => ctx. new_int( c:: AI_PASSIVE ) ,
449464 "socket" => PySocket :: make_class( ctx) ,
450465 "inet_aton" => ctx. new_rustfunc( socket_inet_aton) ,
451466 "inet_ntoa" => ctx. new_rustfunc( socket_inet_ntoa) ,
0 commit comments