io.sockets.secure.windows: use non-blocking sockets to prevent
SSL_connect from blocking

On Windows, SSL_connect may hang forever if the server doesn't send any
data. To counteract that we temporarily set the socket non-blocking and
then call select in the wait-for-fd method.

Conflicts:
	basis/io/sockets/secure/openssl/openssl-tests.factor
db4
Björn Lindqvist 2014-04-02 17:44:19 +02:00 committed by Doug Coleman
parent 4d8f3e8a78
commit f8387a08ae
4 changed files with 26 additions and 16 deletions

View File

@ -3,13 +3,13 @@
USING: accessors alien alien.c-types alien.data alien.strings
alien.syntax arrays assocs classes.struct combinators
combinators.short-circuit continuations destructors environment
io io.backend io.binary io.buffers io.encodings.utf16n io.files
fry io io.backend io.binary io.buffers io.encodings.utf16n io.files
io.files.private io.files.types io.pathnames io.ports
io.streams.c io.streams.null io.timeouts kernel libc literals
locals make math math.bitwise namespaces sequences
specialized-arrays system threads tr windows windows.errors
windows.handles windows.kernel32 windows.shell32 windows.time
windows.types fry ;
windows.types windows.winsock ;
SPECIALIZED-ARRAY: ushort
IN: io.files.windows
@ -206,8 +206,17 @@ M: windows (wait-to-write) ( port -- )
M: windows (wait-to-read) ( port -- )
[ dup handle>> refill ] with-destructors drop ;
: make-fd-set ( socket -- fd_set )
fd_set <struct> swap 1array void* >c-array >>fd_array 1 >>fd_count ;
: select-sets ( socket event -- read-fds write-fds except-fds )
[ make-fd-set ] dip +input+ = [ f f ] [ f swap f ] if ;
CONSTANT: select-timeval S{ timeval { sec 0 } { usec 1000 } }
M: windows wait-for-fd ( handle event -- )
2drop ;
[ file>> handle>> 1 swap ] dip select-sets select-timeval
select drop yield ;
: console-app? ( -- ? ) GetConsoleWindow >boolean ;

View File

@ -4,6 +4,8 @@ kernel openssl.libcrypto openssl.libssl sequences system tools.test urls
unix.ffi ;
IN: io.sockets.secure.openssl.tests
<< os windows? [ "windows.winsock" ] [ "unix.ffi" ] if use-vocab >>
: new-ssl ( -- ssl )
SSLv23_client_method SSL_CTX_new SSL_new ;

View File

@ -1,10 +1,6 @@
USING:
accessors
alien
io.ports
io.sockets.private io.sockets.secure io.sockets.secure.openssl
kernel
openssl openssl.libcrypto openssl.libssl ;
USING: accessors alien io.ports io.sockets.private io.sockets.secure
io.sockets.secure.openssl io.sockets.windows kernel locals openssl
openssl.libcrypto openssl.libssl windows.winsock ;
IN: io.sockets.secure.windows
M: openssl ssl-supported? t ;
@ -24,8 +20,9 @@ M: secure (get-local-address) ( handle remote -- sockaddr )
M: secure parse-sockaddr addrspec>> parse-sockaddr <secure> ;
M: secure establish-connection ( client-out remote -- )
[
[ handle>> file>> <output-port> ] [ addrspec>> ] bi* establish-connection
]
[ secure-connection ] 2bi ;
M:: secure establish-connection ( client-out addrspec -- )
client-out handle>> file>> :> socket
socket FIONBIO 1 set-ioctl-socket
socket <output-port> addrspec addrspec>> establish-connection
client-out addrspec secure-connection
socket FIONBIO 0 set-ioctl-socket ;

View File

@ -159,7 +159,9 @@ M: sockaddr-in sockaddr>ip ( sockaddr -- string )
M: sockaddr-in6 sockaddr>ip ( uchar-array -- string )
addr>> [ >hex ] { } map-as 2 group [ concat ] map ":" join ;
C-TYPE: fd_set
STRUCT: fd_set
{ fd_count uint }
{ fd_array SOCKET[64] } ;
LIBRARY: winsock