Merge pull request #639 from terrafrost/banner-timeout

improve error handling with protocol version exchange

* terrafrost/banner-timeout:
  SSH2: rm unused $connectionTimeout variable
  SSH2: rm redundant code and make php4 compatable
  SSH2: timeout improvements
This commit is contained in:
Andreas Fischer 2015-03-30 12:37:46 +02:00
commit c0370ee91d

View File

@ -804,21 +804,6 @@ class Net_SSH2
*/ */
var $port; 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 * Number of columns for terminal window size
* *
@ -947,7 +932,7 @@ class Net_SSH2
$this->host = $host; $this->host = $host;
$this->port = $port; $this->port = $port;
$this->connectionTimeout = $timeout; $this->timeout = $timeout;
} }
/** /**
@ -964,36 +949,24 @@ class Net_SSH2
$this->bitmap |= NET_SSH2_MASK_CONSTRUCTOR; $this->bitmap |= NET_SSH2_MASK_CONSTRUCTOR;
$timeout = $this->connectionTimeout; $this->curTimeout = $this->timeout;
$host = $this->host . ':' . $this->port; $host = $this->host . ':' . $this->port;
$this->last_packet = strtok(microtime(), ' ') + strtok(''); // == microtime(true) in PHP5 $this->last_packet = strtok(microtime(), ' ') + strtok(''); // == microtime(true) in PHP5
$start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838 $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) { if (!$this->fsock) {
user_error(rtrim("Cannot connect to $host. Error $errno. $errstr")); user_error(rtrim("Cannot connect to $host. Error $errno. $errstr"));
return false; return false;
} }
$elapsed = strtok(microtime(), ' ') + strtok('') - $start; $elapsed = strtok(microtime(), ' ') + strtok('') - $start;
$timeout-= $elapsed; $this->curTimeout-= $elapsed;
if ($timeout <= 0) { if ($this->curTimeout <= 0) {
user_error("Cannot connect to $host. Timeout error"); $this->is_timeout = true;
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 <https://bugs.php.net/42682>
if (!@stream_select($read, $write, $except, $sec, $usec) && !count($read)) {
user_error("Cannot connect to $host. Banner timeout");
return false; return false;
} }
@ -1011,6 +984,27 @@ class Net_SSH2
$extra.= $temp; $extra.= $temp;
$temp = ''; $temp = '';
} }
if ($this->curTimeout) {
if ($this->curTimeout < 0) {
$this->is_timeout = true;
return false;
}
$read = array($this->fsock);
$write = $except = null;
$start = strtok(microtime(), ' ') + strtok('');
$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 <https://bugs.php.net/42682>
if (!@stream_select($read, $write, $except, $sec, $usec) && !count($read)) {
$this->is_timeout = true;
return false;
}
$elapsed = strtok(microtime(), ' ') + strtok('') - $start;
$this->curTimeout-= $elapsed;
}
$temp.= fgets($this->fsock, 255); $temp.= fgets($this->fsock, 255);
} }