From 23c65c383945cfc9c2293f45a7cbc6f1a68178ec Mon Sep 17 00:00:00 2001 From: terrafrost Date: Thu, 19 Mar 2015 07:53:19 -0500 Subject: [PATCH 1/3] SSH2: timeout improvements make it so that the timeout in the constructor behaves in the same way that timeout's set via setTimeout() do. eg. isTimeout() tells you if a timeout was thrown etc. --- phpseclib/Net/SSH2.php | 49 ++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/phpseclib/Net/SSH2.php b/phpseclib/Net/SSH2.php index d6134562..54184730 100644 --- a/phpseclib/Net/SSH2.php +++ b/phpseclib/Net/SSH2.php @@ -938,7 +938,7 @@ class Net_SSH2 $this->host = $host; $this->port = $port; - $this->connectionTimeout = $timeout; + $this->timeout = $timeout; } /** @@ -955,36 +955,24 @@ class Net_SSH2 $this->bitmap |= NET_SSH2_MASK_CONSTRUCTOR; - $timeout = $this->connectionTimeout; + $this->curTimeout = $this->timeout; + $host = $this->host . ':' . $this->port; $this->last_packet = strtok(microtime(), ' ') + strtok(''); // == microtime(true) in PHP5 $start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838 - $this->fsock = @fsockopen($this->host, $this->port, $errno, $errstr, $timeout); + $this->fsock = @fsockopen($this->host, $this->port, $errno, $errstr, $this->curTimeout); if (!$this->fsock) { user_error(rtrim("Cannot connect to $host. Error $errno. $errstr")); return false; } $elapsed = strtok(microtime(), ' ') + strtok('') - $start; - $timeout-= $elapsed; + $this->curTimeout-= $elapsed; - if ($timeout <= 0) { - user_error("Cannot connect to $host. Timeout error"); - return false; - } - - $read = array($this->fsock); - $write = $except = null; - - $sec = floor($timeout); - $usec = 1000000 * ($timeout - $sec); - - // on windows this returns a "Warning: Invalid CRT parameters detected" error - // the !count() is done as a workaround for - if (!@stream_select($read, $write, $except, $sec, $usec) && !count($read)) { - user_error("Cannot connect to $host. Banner timeout"); + if ($this->curTimeout <= 0) { + $this->is_timeout = true; return false; } @@ -1002,6 +990,29 @@ class Net_SSH2 $extra.= $temp; $temp = ''; } + + $read = array($this->fsock); + + if ($this->curTimeout) { + if ($this->curTimeout < 0) { + $this->is_timeout = true; + return false; + } + $read = array($this->fsock); + $write = $except = null; + $start = microtime(true); + $sec = floor($this->curTimeout); + $usec = 1000000 * ($this->curTimeout - $sec); + // on windows this returns a "Warning: Invalid CRT parameters detected" error + // the !count() is done as a workaround for + if (!@stream_select($read, $write, $except, $sec, $usec) && !count($read)) { + $this->is_timeout = true; + return false; + } + $elapsed = microtime(true) - $start; + $this->curTimeout-= $elapsed; + } + $temp.= fgets($this->fsock, 255); } From dfd57dfb897dd848dc7e434c36cd4885baabb73d Mon Sep 17 00:00:00 2001 From: terrafrost Date: Thu, 19 Mar 2015 22:39:43 -0500 Subject: [PATCH 2/3] SSH2: rm redundant code and make php4 compatable --- phpseclib/Net/SSH2.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/phpseclib/Net/SSH2.php b/phpseclib/Net/SSH2.php index 54184730..10ffd673 100644 --- a/phpseclib/Net/SSH2.php +++ b/phpseclib/Net/SSH2.php @@ -991,8 +991,6 @@ class Net_SSH2 $temp = ''; } - $read = array($this->fsock); - if ($this->curTimeout) { if ($this->curTimeout < 0) { $this->is_timeout = true; @@ -1000,7 +998,7 @@ class Net_SSH2 } $read = array($this->fsock); $write = $except = null; - $start = microtime(true); + $start = strtok(microtime(), ' ') + strtok(''); $sec = floor($this->curTimeout); $usec = 1000000 * ($this->curTimeout - $sec); // on windows this returns a "Warning: Invalid CRT parameters detected" error @@ -1009,7 +1007,7 @@ class Net_SSH2 $this->is_timeout = true; return false; } - $elapsed = microtime(true) - $start; + $elapsed = strtok(microtime(), ' ') + strtok('') - $start; $this->curTimeout-= $elapsed; } From 1294b08675743afc2533932ccce74d420827e6d4 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Fri, 27 Mar 2015 22:32:38 -0500 Subject: [PATCH 3/3] SSH2: rm unused $connectionTimeout variable --- phpseclib/Net/SSH2.php | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/phpseclib/Net/SSH2.php b/phpseclib/Net/SSH2.php index 10ffd673..ce5993d8 100644 --- a/phpseclib/Net/SSH2.php +++ b/phpseclib/Net/SSH2.php @@ -803,21 +803,6 @@ class Net_SSH2 */ var $port; - /** - * Timeout for initial connection - * - * Set by the constructor call. Calling setTimeout() is optional. If it's not called functions like - * exec() won't timeout unless some PHP setting forces it too. The timeout specified in the constructor, - * however, is non-optional. There will be a timeout, whether or not you set it. If you don't it'll be - * 10 seconds. It is used by fsockopen() and the initial stream_select in that function. - * - * @see Net_SSH2::Net_SSH2() - * @see Net_SSH2::_connect() - * @var Integer - * @access private - */ - var $connectionTimeout; - /** * Number of columns for terminal window size *