Skip to content

Commit

Permalink
Added binary mode
Browse files Browse the repository at this point in the history
to be able to handle byte-streams
  • Loading branch information
MyIgel committed Aug 8, 2015
1 parent 5168590 commit 73a4b33
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 4 deletions.
93 changes: 90 additions & 3 deletions Bestnetwork/Telnet/TelnetClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* Matthias Blaser <[email protected]>
*
* Extended by Christian Hammers <[email protected]>
*
* and Igor Scheller <[email protected]>
*/
class TelnetClient {

Expand All @@ -24,20 +24,25 @@ class TelnetClient {

protected $socket = NULL;
protected $buffer = NULL;
protected $globalBuffer = NULL;
protected $prompt;
protected $errPrompt;
protected $errNo;
protected $errStr;

protected $NULL;
protected $CR;
protected $DC1;
protected $WILL;
protected $WONT;
protected $DO;
protected $DONT;
protected $IAC;

protected $globalBuffer = '';
/**
* @var bool Is binary mode enabled?
*/
protected $binaryMode = false;

/**
* Constructor. Initialises host, port and timeout parameters
Expand Down Expand Up @@ -129,15 +134,34 @@ public function disconnect(){
* @param string $command Command to execute
* @param null|string $prompt
* @param null|string $errPrompt
* @return string Command result
* @return string|bool Command result or true in binary mode
* @throws TelnetException
*/
public function execute( $command, $prompt = NULL, $errPrompt = NULL ){
if($this->binaryMode){
$this->executeBlind($command);
return true;
}

$this->write($command);
$this->read($prompt, $errPrompt);
return $this->getBuffer();
}

/**
* Executes the given command without output
*
* @param string $command
* @param bool $addNewLine
*/
public function executeBlind($command, $addNewLine = true){
$buffer = $this->buffer;

$this->write($command, $addNewLine);

$this->buffer = $buffer;
}

/**
* Attempts login to remote host.
* This method is a wrapper for lower level private methods and should be
Expand Down Expand Up @@ -204,6 +228,22 @@ public function clearBuffer(){
$this->buffer = '';
}

/**
* Returns true if binary mode is enabled
*/
public function getBinaryMode(){
return $this->binaryMode;
}

/**
* Enables the binary mode
*
* @param bool $binaryMode
*/
public function setBinaryMode( $binaryMode = true ){
$this->binaryMode = $binaryMode;
}

/**
* Reads characters from the socket and adds them to command buffer.
* Handles telnet control characters. Stops when prompt is encountered.
Expand Down Expand Up @@ -260,6 +300,46 @@ protected function read( $prompt = NULL, $errPrompt = NULL ){
}

}while( $c != $this->NULL || $c != $this->DC1 );

return '';
}

/**
* Reads the given amount of bytes
*
* @param int $count
* @return string
* @throws TelnetException
*/
public function readBytes( $count ){
if( !$this->socket ){
throw new TelnetException('Telnet connection closed');
}

// clear the buffer
$this->clearBuffer();

while( $count ){
$c = $this->getc();

if( $c === false ){
throw new TelnetException('Couldn\'t find the requested "' . $count . '" bytes, it was not in the data returned from server: ' . $this->buffer);
}

// Interpreted As Command
if( $c == $this->IAC && !$this->binaryMode ){
if( $this->negotiateTelnetOptions() ){
continue;
}
}

// append current char to global buffer
$this->buffer .= $c;

$count -= strlen($c);
}

return $this->buffer;
}

/**
Expand Down Expand Up @@ -312,9 +392,16 @@ public function getGlobalBuffer(){
/**
* Telnet control character magic
*
* @returns bool
* @throws TelnetException
*/
protected function negotiateTelnetOptions(){
if( $this->binaryMode ){
$b = $this->getGlobalBuffer();
$this->buffer .= substr($b, -1);
return;
}

$c = $this->getc();

if( $c != $this->IAC ){
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
{
"name": "Christian Hammers",
"email": "[email protected]"
},
{
"name": "Igor Scheller",
"email": "[email protected]"
}
],
"require": {
Expand All @@ -40,4 +44,3 @@
}
}
}

0 comments on commit 73a4b33

Please sign in to comment.