From 9e69344d60e911b67e5f7f2b38e10e12c55e4bb6 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sat, 23 Jun 2012 17:16:42 -0500 Subject: [PATCH] - better timeout handling (thanks moizhb) --- phpseclib/Net/SSH2.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/phpseclib/Net/SSH2.php b/phpseclib/Net/SSH2.php index 376b5a75..bddf96ce 100644 --- a/phpseclib/Net/SSH2.php +++ b/phpseclib/Net/SSH2.php @@ -758,11 +758,31 @@ class Net_SSH2 { 61 => 'NET_SSH2_MSG_USERAUTH_INFO_RESPONSE') ); + $start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838 $this->fsock = @fsockopen($host, $port, $errno, $errstr, $timeout); if (!$this->fsock) { user_error(rtrim("Cannot connect to $host. Error $errno. $errstr"), E_USER_NOTICE); return; } + $elapsed = strtok(microtime(), ' ') + strtok('') - $start; + + if ($timeout - $elapsed <= 0) { + user_error(rtrim("Cannot connect to $host. Timeout error"), E_USER_NOTICE); + return; + } + + $read = array($this->fsock); + $write = $except = NULL; + + stream_set_blocking($this->fsock, false); + + // on windows this returns a "Warning: Invalid CRT parameters detected" error + if (!@stream_select($read, $write, $except, $timeout - $elapsed)) { + user_error(rtrim("Cannot connect to $host. Banner timeout"), E_USER_NOTICE); + return; + } + + stream_set_blocking($this->fsock, true); /* According to the SSH2 specs,