Autodetect Telnet IAC modes, and if in binary mode esc 0xff chars
All checks were successful
Create Docker Image / Build Docker Image (x86_64) (push) Successful in 37s
Create Docker Image / Build Docker Image (arm64) (push) Successful in 1m34s
Create Docker Image / Final Docker Image Manifest (push) Successful in 10s

This commit is contained in:
2025-01-30 23:18:49 +11:00
parent 52961e2403
commit b1eaec3271
3 changed files with 167 additions and 5 deletions

View File

@@ -936,6 +936,107 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
if (static::DEBUG)
Log::debug(sprintf('%s:- Got [%x] (%c)',self::LOGKEY,$ch,$ch));
// Look for Telnet IAC, if binary mode we'll need to handle IAC IAC => IAC
if ($ch === 0xff) {
Log::info(sprintf('%s:- TELNET IAC',self::LOGKEY));
$iaccmd = NULL;
// Peek for the next chars
do {
try {
$iac = $this->client->read(1,1,MSG_PEEK);
if (static::DEBUG)
Log::debug(sprintf('%s: - IAC LOOP',self::LOGKEY),['iac'=>ord($iac),'cmd'=>$iaccmd]);
switch (ord($iac)) {
// Binary Mode
case 0x00:
if ($iaccmd === 0xfb) {
Log::debug(sprintf('%s: - IAC WILL BINARY [%02x]',self::LOGKEY,ord($iac)));
// Config with DO
$this->client->send(chr(0xff).chr(0xfd).$iac,10);
} elseif ($iaccmd === 0xfd) {
Log::debug(sprintf('%s: - IAC DO BINARY [%02x]',self::LOGKEY,ord($iac)));
// Config with WILL
if (! $this->client->iac_bin) {
$this->client->send(chr(0xff).chr(0xfb).$iac,10);
$this->client->iac_bin = true;
}
}
$iaccmd = NULL;
break;
// Suppress Go Ahead
case 0x03:
if ($iaccmd === 0xfb) {
Log::debug(sprintf('%s: - IAC WILL SUPPRESS-GO-AHEAD [%02x]',self::LOGKEY,ord($iac)));
// Config with DO
$this->client->send(chr(0xff).chr(0xfd).$iac,10);
} elseif ($iaccmd === 0xfd) {
Log::debug(sprintf('%s: - IAC DO SUPPRESS-GO-AHEAD [%02x]',self::LOGKEY,ord($iac)));
// Config with WILL
$this->client->send(chr(0xff).chr(0xfb).$iac,10);
}
$iaccmd = NULL;
break;
// Will
case 0xfb:
if (static::DEBUG)
Log::debug(sprintf('%s: - IAC WILL [%02x]',self::LOGKEY,ord($iac)));
$iaccmd = ord($iac);
break;
// Do
case 0xfd:
if (static::DEBUG)
Log::debug(sprintf('%s: - IAC DO [%02x]',self::LOGKEY,ord($iac)));
$iaccmd = ord($iac);
break;
// IAC
case 0xff:
if (static::DEBUG)
Log::debug(sprintf('%s: - IAC [%02x]',self::LOGKEY,ord($iac)));
$iaccmd = ord($iac);
break;
default:
Log::alert(sprintf('%s: - IAC Unhandled [%02x]',self::LOGKEY,ord($iac)),['iac'=>$iac,'iaccmd'=>$iaccmd,'ch'=>ord($iac)]);
$ch = ord($iac);
$iac = NULL;
}
if ($iaccmd) {
$iac = ord($this->client->read_ch(10));
$ch = NULL;
} elseif (is_null($ch)) {
$ch = ord($this->client->read_ch(10));
}
} catch (SocketException $e) {
Log::debug(sprintf('%s:! SocketException: %s',self::LOGKEY,$e->getMessage()),['class'=>get_class($e),'code'=>$e->getCode()]);
$iac = NULL;
}
} while (! is_null($iac));
Log::debug(sprintf('%s:- Leaving IAC with [%02x]',self::LOGKEY,$ch),['ch'=>serialize($ch)]);
}
if (($ch != self::TIMEOUT) && ($ch < 0))
return $ch;