From: Björn Lindqvist Date: Wed, 2 Apr 2014 15:44:19 +0000 (+0200) Subject: Merge: X-Git-Tag: 0.97~707 X-Git-Url: https://gitweb.factorcode.org/gitweb.cgi?p=factor.git;a=commitdiff_plain;h=f8387a08aede7494388ca03fa17eac9480bdae91 Merge: 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 --- diff --git a/basis/io/files/windows/windows.factor b/basis/io/files/windows/windows.factor index 066da7d46e..b3f4e866bf 100755 --- a/basis/io/files/windows/windows.factor +++ b/basis/io/files/windows/windows.factor @@ -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 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 ; diff --git a/basis/io/sockets/secure/openssl/openssl-tests.factor b/basis/io/sockets/secure/openssl/openssl-tests.factor index 525df80170..6ee13c4ced 100644 --- a/basis/io/sockets/secure/openssl/openssl-tests.factor +++ b/basis/io/sockets/secure/openssl/openssl-tests.factor @@ -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 ; diff --git a/basis/io/sockets/secure/windows/windows.factor b/basis/io/sockets/secure/windows/windows.factor index a4887bd14b..1f9a2dc8b1 100644 --- a/basis/io/sockets/secure/windows/windows.factor +++ b/basis/io/sockets/secure/windows/windows.factor @@ -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 ; -M: secure establish-connection ( client-out remote -- ) - [ - [ handle>> file>> ] [ 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 addrspec addrspec>> establish-connection + client-out addrspec secure-connection + socket FIONBIO 0 set-ioctl-socket ; diff --git a/basis/windows/winsock/winsock.factor b/basis/windows/winsock/winsock.factor index 029003a762..acd235577e 100644 --- a/basis/windows/winsock/winsock.factor +++ b/basis/windows/winsock/winsock.factor @@ -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