From c42340fe60da0a8695df2ccbaf6eeb066f5b6159 Mon Sep 17 00:00:00 2001 From: Deon George Date: Fri, 31 Jan 2025 17:42:21 +1100 Subject: [PATCH] Telnet Binary Send --- app/Classes/Protocol/EMSI.php | 2 +- app/Classes/Sock/SocketClient.php | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/app/Classes/Protocol/EMSI.php b/app/Classes/Protocol/EMSI.php index c8d8bd1..8e948b9 100644 --- a/app/Classes/Protocol/EMSI.php +++ b/app/Classes/Protocol/EMSI.php @@ -960,7 +960,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface Log::debug(sprintf('%s: - IAC DO BINARY [%02x]',self::LOGKEY,ord($iac))); // Config with WILL - if ($this->client->iac_bin) { + if (! $this->client->iac_bin) { $this->client->send(chr(0xff).chr(0xfb).$iac,10); $this->client->iac_bin = true; } diff --git a/app/Classes/Sock/SocketClient.php b/app/Classes/Sock/SocketClient.php index becf1c4..b645307 100644 --- a/app/Classes/Sock/SocketClient.php +++ b/app/Classes/Sock/SocketClient.php @@ -433,13 +433,17 @@ final class SocketClient { if ($flags !== MSG_PEEK) $this->rx_buf = substr($this->rx_buf,strlen($result)); + Log::debug(sprintf('%s:*** ',self::LOGKEY),['iac_bin'=>$this->iac_bin]); + // In case we are in Telnet Binary Mode if ($this->iac_bin) { if (self::DEBUG) - Log::debug(sprintf('%s:- Telnet IAC Binary Mode, looking for ff ff',self::LOGKEY)); + Log::debug(sprintf('%s:- Telnet IAC Binary Mode, looking for ff ff',self::LOGKEY),['result'=>hex_dump($result)]); // if the last char is ff, we need to get the next char if (str_ends_with($result,"\xff")) { + Log::debug(sprintf('%s: - We have a hit',self::LOGKEY)); + // If we have it in our buffer, just get it if ($this->rx_left) { $result .= substr($this->rx_buf,0,1); @@ -449,14 +453,14 @@ final class SocketClient { } else { $this->rx_buf = $result; $len++; - $result = NULL; + $result = ''; } } if (strlen($result) > 1) $result = str_replace("\xff\xff","\xff",$result); - if ($result) + if (strlen($result)) return $result; } else @@ -464,7 +468,7 @@ final class SocketClient { } if (self::DEBUG) - Log::debug(sprintf('%s:- Buffer doesnt have [%d] chars, it only has [%d]',self::LOGKEY,$len,strlen($this->rx_buf))); + Log::debug(sprintf('%s:- Buffer doesnt have [%d] chars, it only has [%d], or it ends with 0xff',self::LOGKEY,$len,strlen($this->rx_buf)),['rx_buf'=>hex_dump($this->rx_buf)]); if ($timeout && (! $this->hasData($timeout))) throw new SocketException(SocketException::SOCKET_TIMEOUT,$timeout); @@ -591,6 +595,11 @@ final class SocketClient { if (self::DEBUG) Log::debug(sprintf('%s:- Sending [%d] chars [%s]',self::LOGKEY,strlen($message),Str::limit($message,15))); + if ($this->iac_bin) { + Log::debug(sprintf('%s:- IAC_BIN mode, looking for 0xff',self::LOGKEY)); + $message = str_replace("\xff","\xff\xff",$message); + } + switch ($this->type) { case SOCK_STREAM: return socket_write($this->connection,$message,strlen($message));