Simplified logging and fix BINKP issue when receiving less than the blksize

This commit is contained in:
Deon George
2021-08-17 23:49:39 +10:00
parent 5bf612e5b4
commit 978843b5e3
7 changed files with 285 additions and 253 deletions

View File

@@ -16,6 +16,8 @@ use App\Traits\CRC as CRCTrait;
final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
{
private const LOGKEY = 'Z--';
use CRCTrait;
/* ZModem constants */
@@ -204,9 +206,13 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
{
// If our parent returns a PID, we've forked
if (! parent::onConnect($client)) {
Log::withContext(['pid'=>getmypid()]);
$this->session(self::SESSION_ZMODEM,$client);
$this->client->close();
Log::info(sprintf('%s: = End - Connection closed [%s]',__METHOD__,$client->address_remote));
Log::info(sprintf('%s:= onConnect - Connection closed [%s]',self::LOGKEY,$client->address_remote));
exit(0);
}
return NULL;
@@ -235,7 +241,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
break;
default:
Log::error(sprintf('%s: ! Strange cancap [%d]',__METHOD__,$canzap));
Log::error(sprintf('%s: ! init Strange cancap [%d]',self::LOGKEY,$canzap));
}
/* Maximum block size -- by protocol, may be reduced by window size later */
@@ -256,6 +262,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
public function protocol_init(): int
{
// Not used
return 0;
}
/**
@@ -291,7 +298,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
*/
public function zmodem_receive(SocketClient $client,int $canzap,Receive $recv,Address $ao): int
{
Log::debug(sprintf('%s: + Start [%d]',__METHOD__,$canzap));
Log::debug(sprintf('%s:+ zmodem_receive [%d]',self::LOGKEY,$canzap));
$opts = $this->init($client,$canzap);
@@ -303,17 +310,17 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
switch ($rc=$this->ls_zrecvfinfo(self::ZRINIT,($this->ls_Protocol&self::LSZ_OPTFIRSTBATCH) ? 1 : 0)) {
case self::ZFIN:
Log::debug(sprintf('%s: = ZFIN after INIT, empty batch',__METHOD__));
Log::debug(sprintf('%s:= zmodem_receive ZFIN after INIT, empty batch',self::LOGKEY));
$this->ls_zdonereceiver();
return self::LSZ_OK;
case self::ZFILE:
Log::debug(sprintf('%s: = ZFILE after INIT',__METHOD__));
Log::debug(sprintf('%s: = zmodem_receive ZFILE after INIT',self::LOGKEY));
break;
default:
Log::error(sprintf('%s: ! Something strange after init [%d]',__METHOD__,$rc));
Log::error(sprintf('%s:! zmodem_receive Something strange after init [%d]',self::LOGKEY,$rc));
$this->ls_zabort();
$this->ls_zdonereceiver();
@@ -324,59 +331,59 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
while (TRUE) {
switch ($rc) {
case self::ZFIN:
Log::debug(sprintf('%s: = ZFIN',__METHOD__));
Log::debug(sprintf('%s:= zmodem_receive ZFIN',self::LOGKEY));
$this->ls_zdonereceiver();
return self::LSZ_OK;
case self::ZFILE:
if (! $this->recv->to_get) {
Log::error(sprintf('%s: ! No files to get?',__METHOD__));
Log::error(sprintf('%s: ! zmodem_receive No files to get?',self::LOGKEY));
$frame = self::ZSKIP;
} else {
switch ($this->recv->open($ao)) {
case self::FOP_SKIP:
Log::info(sprintf('%s: = Skip this file [%s]',__METHOD__,$this->recv->name));
Log::info(sprintf('%s: = zmodem_receive Skip this file [%s]',self::LOGKEY,$this->recv->name));
$frame = self::ZSKIP;
break;
case self::FOP_SUSPEND:
Log::info(sprintf('%s: = Suspend this file [%s]',__METHOD__,$this->recv->name));
Log::info(sprintf('%s: = zmodem_receive Suspend this file [%s]',self::LOGKEY,$this->recv->name));
$frame = self::ZFERR;
break;
case self::FOP_CONT:
case self::FOP_OK:
Log::info(sprintf('%s: = Receving [%s] from [%d]',__METHOD__,$this->recv->name,$this->recv->filepos));
Log::info(sprintf('%s: = zmodem_receive Receving [%s] from [%d]',self::LOGKEY,$this->recv->name,$this->recv->filepos));
$frame = self::ZRINIT;
switch (($rc=$this->ls_zrecvfile($recv->filepos))) {
case self::ZFERR:
Log::debug(sprintf('%s: = ZFERR',__METHOD__));
Log::debug(sprintf('%s: = zmodem_receive ZFERR',self::LOGKEY));
$this->recv->close();
$frame = self::ZFERR;
break;
case self::ZSKIP:
Log::debug(sprintf('%s: = ZSKIP',__METHOD__));
Log::debug(sprintf('%s: = zmodem_receive ZSKIP',self::LOGKEY));
$this->recv->close();
$frame = self::ZSKIP;
break;
case self::LSZ_OK:
Log::debug(sprintf('%s: = OK',__METHOD__));
Log::debug(sprintf('%s: = zmodem_receive OK',self::LOGKEY));
$this->recv->close();
break;
default:
Log::error(sprintf('%s: ! OTHER [%d]',__METHOD__,$rc));
Log::error(sprintf('%s:! zmodem_receive OTHER [%d]',self::LOGKEY,$rc));
$this->recv->close();
return self::LSZ_ERROR;
@@ -389,7 +396,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
break;
case self::ZABORT:
Log::debug(sprintf('%s: = ZABORT',__METHOD__));
Log::debug(sprintf('%s:= zmodem_receive ZABORT',self::LOGKEY));
$this->ls_zabort();
$this->ls_zdonereceiver();
@@ -397,7 +404,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
return self::LSZ_ERROR;
default:
Log::error(sprintf('%s: ? Something strange [%d]',__METHOD__,$rc));
Log::error(sprintf('%s:? zmodem_receive Something strange [%d]',self::LOGKEY,$rc));
$this->ls_zabort();
$this->ls_zdonereceiver();
@@ -447,7 +454,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
break;
default:
Log::error(sprintf('%s: ? Something strange [%d]',__METHOD__,$rc));
Log::error(sprintf('%s: ? zmodem_senddone Something strange [%d]',self::LOGKEY,$rc));
if ($rc < 0)
return $rc;
@@ -457,7 +464,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
} while ($trys < 10);
Log::error(sprintf('%s: ? Something strange or timeeout [%d]',__METHOD__,$rc));
Log::error(sprintf('%s:? zmodem_senddone Something strange or timeeout [%d]',self::LOGKEY,$rc));
return $rc;
}
@@ -470,7 +477,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
*/
public function zmodem_sendinit(SocketClient $client,int $canzap): int
{
Log::debug(sprintf('%s: + Start [%d]',__METHOD__,$canzap));
Log::debug(sprintf('%s:+ zmodem_sendinit [%d]',self::LOGKEY,$canzap));
$this->init($client,$canzap);
@@ -487,7 +494,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
($this->ls_Protocol&self::LSZ_OPTESCAPEALL) ? ',ESCALL' : ''
));
Log::debug(sprintf('%s: = End [%x]',__METHOD__,$rc));
Log::debug(sprintf('%s:= zmodem_sendinit [%x]',self::LOGKEY,$rc));
return $rc;
}
@@ -499,7 +506,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
*/
public function zmodem_sendfile(Send $send): int
{
Log::debug(sprintf('%s: + Start',__METHOD__));
Log::debug(sprintf('%s:+ zmodem_sendfile',self::LOGKEY));
while ($send->total_count && $send->open()) {
try {
@@ -524,7 +531,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
return $rc;
} catch (\Exception $e) {
Log::error(sprintf('%s: ! Error [%s]',__METHOD__,$e->getMessage()));
Log::error(sprintf('%s: ! Error [%s]',self::LOGKEY,$e->getMessage()));
}
}
@@ -840,7 +847,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
*/
private function ls_zdonereceiver(): int
{
Log::debug(sprintf('%s: + Start',__METHOD__));
Log::debug(sprintf('%s:+ ls_zdonereceiver',self::LOGKEY));
$trys = 0;
$retransmit = 1;
@@ -866,12 +873,12 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
case self::XON|0x80:
case self::XOFF|0x80:
if ($this->DEBUG)
Log::debug(sprintf('%s: - XON/XOFF, skip it',__METHOD__));
Log::debug(sprintf('%s: - ls_zdonereceiver XON/XOFF, skip it',self::LOGKEY));
break;
case self::ZPAD:
if ($this->DEBUG)
Log::debug(sprintf('%s: - ZPAD',__METHOD__));
Log::debug(sprintf('%s: - ls_zdonereceiver ZPAD',self::LOGKEY));
if (($rc=$this->ls_zrecvhdr($this->ls_rxHdr,$this->ls_HeaderTimeout)) < 0)
return $rc;
@@ -884,7 +891,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
break;
default:
Log::error(sprintf('%s: ? Something strange [%d]',__METHOD__,$rc));
Log::error(sprintf('%s: ? ls_zdonereceiver Something strange [%d]',self::LOGKEY,$rc));
if ($rc < 0)
return $rc;
@@ -894,7 +901,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
} while ($trys < 10);
Log::error(sprintf('%s: ? Something strange [%d]',__METHOD__,$rc));
Log::error(sprintf('%s:? ls_zdonereceiver Something strange [%d]',self::LOGKEY,$rc));
return $rc;
}
@@ -907,7 +914,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
*/
private function ls_zinitsender(int $window,string $attstr)
{
Log::debug(sprintf('%s: + Start',__METHOD__));
Log::debug(sprintf('%s:+ ls_zinitsender',self::LOGKEY));
$trys = 0;
@@ -936,7 +943,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
switch ($rc=$this->ls_zrecvhdr($this->ls_rxHdr,$this->ls_HeaderTimeout)) {
/* Ok, We got RINIT! */
case self::ZRINIT:
Log::debug(sprintf('%s: - ZRINIT',__METHOD__));
Log::debug(sprintf('%s: - ls_zinitsender ZRINIT',self::LOGKEY));
$this->rxOptions= (($this->ls_rxHdr[self::LSZ_F1]&0xff)<<8)|($this->ls_rxHdr[self::LSZ_F0]&0xff);
@@ -967,7 +974,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
if ($window && (! $this->ls_txWinSize || ($this->ls_txWinSize > $window)))
$this->ls_txWinSize = $window;
Log::debug(sprintf('%s: - ZRINIT OK - effproto [%08x], winsize [%d]',__METHOD__,$this->ls_Protocol,$this->ls_txWinSize));
Log::debug(sprintf('%s: - ls_zinitsender ZRINIT OK - effproto [%08x], winsize [%d]',self::LOGKEY,$this->ls_Protocol,$this->ls_txWinSize));
/* Ok, now we could calculate real max frame size and initial block size */
if ($this->ls_txWinSize && $this->ls_MaxBlockSize>$this->ls_txWinSize) {
@@ -1001,7 +1008,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
if ($this->ls_txCurBlockSize>$this->ls_MaxBlockSize)
$this->ls_txCurBlockSize=$this->ls_MaxBlockSize;
Log::debug(sprintf('%s: - ZRINIT OK - block sizes Max [%d] Current [%d]',__METHOD__,$this->ls_MaxBlockSize,$this->ls_txCurBlockSize));
Log::debug(sprintf('%s: - ls_zinitsender ZRINIT OK - block sizes Max [%d] Current [%d]',self::LOGKEY,$this->ls_MaxBlockSize,$this->ls_txCurBlockSize));
/* Send ZSINIT, if we need it */
if ($attstr || (! ($this->rxOptions&self::LSZ_RXWNTESCCTL) && ($this->ls_Protocol&self::LSZ_OPTESCAPEALL)))
@@ -1012,7 +1019,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
/* Return number to peer, he is paranoid */
case self::ZCHALLENGE:
Log::debug(sprintf('%s: - CHALLENGE',__METHOD__));
Log::debug(sprintf('%s: - ls_zinitsender CHALLENGE',self::LOGKEY));
if (($rc=$this->ls_zsendhhdr(ZACK,$this->ls_rxHdr)) < 0)
return $rc;
@@ -1028,7 +1035,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
/* ZFIN from previous session? Or may be real one? */
case self::ZFIN:
Log::debug(sprintf('%s: - ZFIN [%d]',__METHOD__,$zfins));
Log::debug(sprintf('%s: - ls_zinitsender ZFIN [%d]',self::LOGKEY,$zfins));
if (++$zfins == self::LSZ_TRUSTZFINS)
return self::LSZ_ERROR;
@@ -1037,20 +1044,20 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
/* Please, resend */
case self::LSZ_BADCRC:
Log::debug(sprintf('%s: - LSZ_BADCRC',__METHOD__));
Log::debug(sprintf('%s: - ls_zinitsender LSZ_BADCRC',self::LOGKEY));
/* We don't support it! */
case self::ZCOMMAND:
Log::debug(sprintf('%s: - ZCOMMAND',__METHOD__));
Log::debug(sprintf('%s: - ls_zinitsender ZCOMMAND',self::LOGKEY));
$this->ls_zsendhhdr(ZNAK,$this->ls_storelong(0));
/* Abort this session -- we trust in ABORT! */
case self::ZABORT:
Log::debug(sprintf('%s: - ZABORT',__METHOD__));
Log::debug(sprintf('%s:- ls_zinitsender ZABORT',self::LOGKEY));
return self::LSZ_ERROR;
default:
Log::error(sprintf('%s: ? Something strange [%d]',__METHOD__,$rc));
Log::error(sprintf('%s: ? ls_zinitsender Something strange [%d]',self::LOGKEY,$rc));
if ($rc < 0)
return $rc;
@@ -1058,7 +1065,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
} while ($trys < 10);
Log::error(sprintf('%s: ? Something strange or timeout [%d]',__METHOD__,$rc));
Log::error(sprintf('%s:? ls_zinitsender Something strange or timeout [%d]',self::LOGKEY,$rc));
return $rc;
}
@@ -1074,7 +1081,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
private function ls_zrecvdata16(string &$data,int &$len,int $timeout): int
{
if ($this->DEBUG)
Log::debug('+ Start',['m'=>__METHOD__,'d'=>$data]);
Log::debug(sprintf('%s:+ ls_zrecvdata16',self::LOGKEY),['d'=>$data]);
$got = 0; /* Bytes total got */
$crc = 0; /* Received CRC */
@@ -1083,7 +1090,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
while ($rcvdata && (($c = $this->ls_readzdle($timeout)) >= 0)) {
if ($this->DEBUG)
Log::debug(sprintf(' - got [%x] (%c)',$c,($c<31 ? 32 : $c)),['m'=>__METHOD__,'c'=>serialize($c)]);
Log::debug(sprintf('%s: - ls_zrecvdata16 got [%x] (%c)',self::LOGKEY,$c,($c<31 ? 32 : $c)),['c'=>serialize($c)]);
if ($c < 256) {
$data .= chr($c&0xff);
@@ -1110,7 +1117,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
/* We finish loop by error in ls_readzdle() */
if ($rcvdata) {
Log::error(sprintf('%s: ? Something strange or timeout [%d]',__METHOD__,$rc));
Log::error(sprintf('%s:? ls_zrecvdata16 Something strange or timeout [%d]',self::LOGKEY,$rc));
return $c;
}
@@ -1129,7 +1136,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
$incrc = crc16($data.chr($frametype));
if ($this->DEBUG)
Log::debug(sprintf('CRC%d got %08x - calculated %08x',16,$incrc,$crc),['m'=>__METHOD__]);
Log::debug(sprintf('%s: - ls_zrecvdata16 CRC%d got %08x - calculated %08x',self::LOGKEY,16,$incrc,$crc),['m'=>__METHOD__]);
if ($incrc != $crc)
return self::LSZ_BADCRC;
@@ -1137,7 +1144,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
$len = $got;
if ($this->DEBUG)
Log::debug('= End',['m'=>__METHOD__,'frametype'=>$frametype]);
Log::debug(sprintf('%s:= ls_zrecvdata16 - frametype [%d]',self::LOGKEY,$frametype));
return $frametype;
}
@@ -1153,7 +1160,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
private function ls_zrecvdata32(string &$data,int &$len,int $timeout): int
{
if ($this->DEBUG)
Log::debug('+ Start',['m'=>__METHOD__,'d'=>$data]);
Log::debug(sprintf('%s:+ ls_zrecvdata32',self::LOGKEY),['d'=>$data]);
$got = 0; /* Bytes total got */
$crc = 0; /* Received CRC */
@@ -1162,7 +1169,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
while ($rcvdata && (($c=$this->ls_readzdle($timeout)) >= 0)) {
if ($this->DEBUG)
Log::debug(sprintf(' - got [%x] (%c)',$c,($c<31 ? 32 : $c)),['m'=>__METHOD__,'c'=>serialize($c)]);
Log::debug(sprintf('%s: - ls_zrecvdata32 got [%x] (%c)',self::LOGKEY,$c,($c<31 ? 32 : $c)),['c'=>serialize($c)]);
if ($c < 256) {
$data .= chr($c&0xff);
@@ -1189,7 +1196,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
/* We finish loop by error in ls_readzdle() */
if ($rcvdata) {
Log::error(sprintf('%s: ? Something strange or timeout [%d]',__METHOD__,$rc));
Log::error(sprintf('%s:? ls_zrecvdata32 Something strange or timeout [%d]',self::LOGKEY,$rc));
return $c;
}
@@ -1214,7 +1221,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
$incrc = crc32($data.chr($frametype));
if ($this->DEBUG)
Log::debug(sprintf('CRC%d got %08x - calculated %08x',32,$incrc,$crc),['m'=>__METHOD__]);
Log::debug(sprintf('%s: - ls_zrecvdata32 CRC%d got %08x - calculated %08x',self::LOGKEY,32,$incrc,$crc));
if ($incrc != $crc)
return self::LSZ_BADCRC;
@@ -1222,7 +1229,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
$len = $got;
if ($this->DEBUG)
Log::debug('= End',['m'=>__METHOD__,'frametype'=>$frametype]);
Log::debug(sprintf('%s:= ls_zrecvdata32 - frametype [%d]',self::LOGKEY,$frametype));
return $frametype;
}
@@ -1236,7 +1243,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
*/
private function ls_zrecvfile(int $pos): int
{
Log::debug('+ Start',['m'=>__METHOD__,'pos'=>$pos]);
Log::debug(sprintf('%s:+ ls_zrecvfile - pos [%d]',self::LOGKEY,$pos));
$needzdata = 1;
$len = 0;
@@ -1254,7 +1261,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
$needzdata = 1;
case self::ZCRCG:
Log::debug(sprintf('%s: - ZCRC%s, [%d] bytes at [%d]',__METHOD__,($rc==self::ZCRCE ? 'E' : 'G'),$len,$rxpos));
Log::debug(sprintf('%s: - ls_zrecvfile ZCRC%s, [%d] bytes at [%d]',self::LOGKEY,($rc==self::ZCRCE ? 'E' : 'G'),$len,$rxpos));
$rxpos += $len;
@@ -1269,7 +1276,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
$needzdata = 1;
case self::ZCRCQ:
Log::debug(sprintf('%s: - ZCRC%s, [%d] bytes at [%d]',__METHOD__,($rc==self::ZCRCW ? 'W' : 'Q'),$len,$rxpos));
Log::debug(sprintf('%s: - ls_zrecvfile ZCRC%s, [%d] bytes at [%d]',self::LOGKEY,($rc==self::ZCRCW ? 'W' : 'Q'),$len,$rxpos));
$rxpos += $len;
@@ -1296,7 +1303,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
break;
default:
Log::error(sprintf('%s: ? Something strange [%d]',__METHOD__,$rc));
Log::error(sprintf('%s: ? ls_zrecvfile Something strange [%d]',self::LOGKEY,$rc));
if ($rc < 0)
return $rc;
@@ -1313,13 +1320,13 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
/* We need new position -- ZDATA (and may be ZEOF) */
} else {
Log::debug(sprintf('%s: - Want ZDATA/ZEOF at [%d]',__METHOD__,$rxpos));
Log::debug(sprintf('%s: - ls_zrecvfile Want ZDATA/ZEOF at [%d]',self::LOGKEY,$rxpos));
if (($rc=$this->ls_zrecvnewpos($rxpos,$newpos)) < 0)
return $rc;
if ($newpos != $rxpos) {
Log::error(sprintf('%s: - Bad new position [%d] in [%d]',__METHOD__,$newpos,$rc));
Log::error(sprintf('%s: - ls_zrecvfile Bad new position [%d] in [%d]',self::LOGKEY,$newpos,$rc));
if ($this->ls_rxAttnStr) {
$this->client->buffer_add($this->ls_rxAttnStr);
@@ -1333,7 +1340,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
} else {
if ($rc == self::ZEOF) {
Log::debug(sprintf('%s: - ZEOF',__METHOD__));
Log::debug(sprintf('%s: - ls_zrecvfile ZEOF',self::LOGKEY));
if (($rc=$this->ls_zsendhhdr(self::ZRINIT,$this->ls_storelong(0))) < 0)
return $rc;
@@ -1341,7 +1348,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
return self::LSZ_OK;
}
Log::debug(sprintf('%s: - ZDATA',__METHOD__));
Log::debug(sprintf('%s: - ls_zrecvfile ZDATA',self::LOGKEY));
$needzdata = 0;
}
}
@@ -1362,7 +1369,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
*/
private function ls_zrecvfinfo(int $frame,int $first): int
{
Log::debug(sprintf('%s: + Start - Frame [%d], First [%d]',__METHOD__,$frame,$first));
Log::debug(sprintf('%s:+ ls_zrecvfinfo - Frame [%d], First [%d]',self::LOGKEY,$frame,$first));
$trys = 0;
$retransmit = ($frame != self::ZRINIT);
@@ -1391,7 +1398,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
return $rc;
}
Log::debug(sprintf('%s: - ZRINIT',__METHOD__));
Log::debug(sprintf('%s: - ls_zrecvfinfo ZRINIT',self::LOGKEY));
$txHdr = [];
$txHdr[self::LSZ_P0] = ($win&0xff);
@@ -1409,22 +1416,22 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
switch (($rc=$this->ls_zrecvhdr($this->ls_rxHdr,$this->ls_HeaderTimeout))) {
/* Send ZRINIT again */
case self::ZRQINIT:
Log::debug(sprintf('%s: - ZRQINIT',__METHOD__));
Log::debug(sprintf('%s: - ls_zrecvfinfo ZRQINIT',self::LOGKEY));
/* We will trust in first ZFIN after ZRQINIT */
$first = 1;
case self::ZNAK:
Log::debug(sprintf('%s: - ZNAK',__METHOD__));
Log::debug(sprintf('%s: - ls_zrecvfinfo ZNAK',self::LOGKEY));
case self::LSZ_TIMEOUT:
Log::debug(sprintf('%s: - LSZ_TIMEOUT',__METHOD__));
Log::debug(sprintf('%s: - ls_zrecvfinfo LSZ_TIMEOUT',self::LOGKEY));
$retransmit = 1;
break;
/* He want to set some options */
case self::ZSINIT:
Log::debug(sprintf('%s: - ZSINIT',__METHOD__));
Log::debug(sprintf('%s: - ls_zrecvfinfo ZSINIT',self::LOGKEY));
if (($rc=$this->ls_zrecvcrcw($this->rxbuf,$len)) < 0)
return $rc;
@@ -1452,7 +1459,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
/* Ok, File started! */
case self::ZFILE:
Log::debug(sprintf('%s: - ZFILE',__METHOD__));
Log::debug(sprintf('%s: - ls_zrecvfinfo ZFILE',self::LOGKEY));
if (($rc=$this->ls_zrecvcrcw($this->rxbuf,$len)) < 0)
return $rc;
@@ -1471,7 +1478,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
$filesleft, // @todo Should track this
$bytesleft) < 2)
{
Log::error(sprintf('%s: ! File info is corrupted [%s]',__METHOD__,$this->rxbuf));
Log::error(sprintf('%s: ! ls_zrecvfinfo File info is corrupted [%s]',self::LOGKEY,$this->rxbuf));
$filesleft = -1;
} else {
@@ -1489,7 +1496,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
/* ZFIN from previous session? Or may be real one? */
case self::ZFIN:
Log::debug(sprintf('%s: - ZFIN [%d], first [%d]',__METHOD__,$zfins,$first));
Log::debug(sprintf('%s: - ls_zrecvfinfo ZFIN [%d], first [%d]',self::LOGKEY,$zfins,$first));
if ($first || (++$zfins == self::LSZ_TRUSTZFINS))
return self::ZFIN;
@@ -1498,11 +1505,11 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
/* Abort this session -- we trust in ABORT! */
case self::ZABORT:
Log::debug(sprintf('%s: - ZABORT',__METHOD__));
Log::debug(sprintf('%s:- ls_zrecvfinfo ZABORT',self::LOGKEY));
return self::ZABORT;
case self::LSZ_BADCRC:
Log::debug(sprintf('%s: - BADCRC',__METHOD__));
Log::debug(sprintf('%s: - ls_zrecvfinfo BADCRC',self::LOGKEY));
$this->ls_zsendhhdr(self::ZNAK,$this->ls_storelong(0));
$retransmit = 1;
@@ -1510,7 +1517,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
break;
default:
Log::error(sprintf('%s: ? Something strange [%d]',__METHOD__,$rc));
Log::error(sprintf('%s: ? ls_zrecvfinfo Something strange [%d]',self::LOGKEY,$rc));
if ($rc < 0)
return $rc;
@@ -1520,14 +1527,14 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
} while ($trys < 10);
Log::error(sprintf('%s: ? Something strange or timeout',__METHOD__));
Log::error(sprintf('%s:? ls_zrecvfinfo Something strange or timeout',self::LOGKEY));
return self::LSZ_TIMEOUT;
}
private function ls_zrecvhdr(array &$hdr,int $timeout): int
{
if ($this->DEBUG)
Log::debug(sprintf('%s: + Start with [%d] timeout',__METHOD__,$timeout));
Log::debug(sprintf('%s:+ ls_zrecvhdr with [%d] timeout',self::LOGKEY,$timeout));
$state = self::rhInit;
$readmode = self::rm7BIT;
@@ -1544,7 +1551,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
if ($state == self::rhInit) {
if ($this->DEBUG)
Log::debug(sprintf('%s: - Init State',__METHOD__));
Log::debug(sprintf('%s: - ls_zrecvhdr Init State',self::LOGKEY));
$frametype = self::LSZ_ERROR;
$crc = 0;
@@ -1558,7 +1565,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
while ($rc = $this->client->hasData($timeout)) {
if ($this->DEBUG)
Log::debug(sprintf('%s: - hasData - readmode [%d] - Garbage [%d]',__METHOD__,$readmode,$garbage));
Log::debug(sprintf('%s: - ls_zrecvhdr hasData - readmode [%d] - Garbage [%d]',self::LOGKEY,$readmode,$garbage));
switch ($readmode) {
case self::rm8BIT:
@@ -1576,7 +1583,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
}
if ($this->DEBUG)
Log::debug(sprintf('%s: - %s [%x] (%c)',__METHOD__,$readmode,$c,$c));
Log::debug(sprintf('%s: - ls_zrecvhdr %s [%x] (%c)',self::LOGKEY,$readmode,$c,$c));
/* Here is error */
if ($c < 0)
@@ -1586,7 +1593,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
$c &= 0xff;
if ($this->DEBUG)
Log::debug(sprintf('%s: = %x',__METHOD__,$c),['state'=>$state]);
Log::debug(sprintf('%s: = ls_zrecvhdr %x (%d)',self::LOGKEY,$c,$state));
switch ($state) {
case self::rhInit:
@@ -1694,7 +1701,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
case self::rhBYTE:
if ($this->DEBUG)
Log::debug(sprintf('%s: - [%02x] (%d)',__METHOD__,$c,$got));
Log::debug(sprintf('%s: - ls_zrecvhdr [%02x] (%d)',self::LOGKEY,$c,$got));
$hdr[$got] = $c;
@@ -1707,7 +1714,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
case self::rhCRC:
if ($this->DEBUG)
Log::debug(sprintf('%s: - [%02x] (%d|%d)',__METHOD__,$c,$crcgot+1,$got));
Log::debug(sprintf('%s: - ls_zrecvhdr [%02x] (%d|%d)',self::LOGKEY,$c,$crcgot+1,$got));
if ($crcl == 2) {
$crc <<= 8;
@@ -1724,7 +1731,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
if ($crcl == 2) {
if (($this->ls_Protocol&self::LSZ_OPTCRC32) && ($readmode != self::rmHEX))
Log::error(sprintf('%s: - was CRC32, got CRC16 binary header',__METHOD__));
Log::error(sprintf('%s: - ls_zrecvhdr was CRC32, got CRC16 binary header',self::LOGKEY));
$crc &= 0xffff;
@@ -1733,14 +1740,14 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
} else {
if (! ($this->ls_Protocol&self::LSZ_OPTCRC32))
Log::error(sprintf('s: - was CRC16, got CRC32 binary header',__METHOD__));
Log::error(sprintf('%s: - ls_zrecvhdr was CRC16, got CRC32 binary header',self::LOGKEY));
$incrc = $this->CRC32_FINISH($incrc);
$this->ls_Protocol |= self::LSZ_OPTCRC32;
}
if ($this->DEBUG)
Log::debug(sprintf('%s: - CRC%d got %08x - calculated %08x',__METHOD__,(($crcl==2) ? 16 : 32),$incrc,$crc));
Log::debug(sprintf('%s: - ls_zrecvhdr CRC%d got %08x - calculated %08x',self::LOGKEY,(($crcl==2) ? 16 : 32),$incrc,$crc));
if ($incrc != $crc)
return self::LSZ_BADCRC;
@@ -1765,7 +1772,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
case self::CR:
case self::CR|0x80:
if ($this->DEBUG)
Log::debug(sprintf('%s: ? rhCR, ignoring any remaining chars [%d]',__METHOD__,$c));
Log::debug(sprintf('%s:? ls_zrecvhdr rhCR, ignoring any remaining chars [%d]',self::LOGKEY,$c));
/* At this point, most implementations ignore checking for the remaining chars */
return $frametype;
@@ -1779,12 +1786,12 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
case self::XON:
case self::XON|0x80:
if ($this->DEBUG)
Log::debug(sprintf('%s: ? rhCR, got XON without CR/LF [%d]',__METHOD__,$c));
Log::debug(sprintf('%s:? ls_zrecvhdr rhCR, got XON without CR/LF [%d]',self::LOGKEY,$c));
return $frametype;
default:
Log::error(sprintf('%s: ? Something strange [%d]',__METHOD__,$c));
Log::error(sprintf('%s:? ls_zrecvhdr Something strange [%d]',self::LOGKEY,$c));
return self::LSZ_BADCRC;
}
@@ -1800,7 +1807,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
break;
default:
Log::error(sprintf('%s: ? Something strange [%d]',__METHOD__,$c));
Log::error(sprintf('%s:? ls_zrecvhdr Something strange [%d]',self::LOGKEY,$c));
return self::LSZ_BADCRC;
}
@@ -1810,7 +1817,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
$state = self::rhInit;
if ($this->DEBUG)
Log::debug(sprintf('%s: rhXON',__METHOD__));
Log::debug(sprintf('%s: - ls_zrecvhdr rhXON',self::LOGKEY));
switch ($c) {
case self::ZPAD:
@@ -1825,7 +1832,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
return $frametype;
default:
Log::error(sprintf('%s: ! rhXON unexpcted [%d]',__METHOD__,$c));
Log::error(sprintf('%s:! ls_zrecvhdr rhXON unexpcted [%d]',self::LOGKEY,$c));
return self::LSZ_BADCRC;
}
@@ -1833,7 +1840,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
}
}
Log::error(sprintf('%s: ? Something strange or timeout [%d]',__METHOD__,$rc));
Log::error(sprintf('%s:? ls_zrecvhdr Something strange or timeout [%d]',self::LOGKEY,$rc));
return $rc;
}
@@ -1855,17 +1862,17 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
return self::LSZ_OK;
case self::LSZ_BADCRC:
Log::debug(sprintf('%s: - got BADCRC',__METHOD__));
Log::debug(sprintf('%s: - ls_zrecvcrcw got BADCRC',self::LOGKEY));
$this->ls_zsendhhdr(self::ZNAK,$this->ls_storelong(0));
return 1;
case self::LSZ_TIMEOUT:
Log::debug(sprintf('%s: - got TIMEOUT',__METHOD__));
Log::debug(sprintf('%s: - ls_zrecvcrcw got TIMEOUT',self::LOGKEY));
break;
default:
Log::error(sprintf('%s: ? Something strange [%d]',__METHOD__,$rc));
Log::error(sprintf('%s: ? ls_zrecvcrcw Something strange [%d]',self::LOGKEY,$rc));
if ($rc < 0)
return $rc;
@@ -1916,7 +1923,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
break;
default:
Log::error(sprintf('%s: ? Something strange [%d]',__METHOD__,$rc));
Log::error(sprintf('%s: ? ls_zrecvnewpos Something strange [%d]',self::LOGKEY,$rc));
if ($rc < 0)
return $rc;
@@ -1926,7 +1933,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
} while (++$trys < 10);
Log::error(sprintf('%s: ? Something strange or timeout [%d]',__METHOD__,$rc));
Log::error(sprintf('%s:? ls_zrecvnewpos Something strange or timeout [%d]',self::LOGKEY,$rc));
return self::LSZ_TIMEOUT;
}
@@ -1940,11 +1947,11 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
*/
private function ls_zrpos(Send $send,int $newpos): int
{
Log::debug(sprintf('%s: + Start, newpos [%d]',__METHOD__,$newpos));
Log::debug(sprintf('%s:+ ls_zrpos, newpos [%d]',self::LOGKEY,$newpos));
if ($newpos == $this->ls_txLastRepos) {
if (++$this->ls_txReposCount > 10) {
Log::error(sprintf('%s: ! ZRPOS to [%ld] limit reached',__METHOD__,$newpos));
Log::error(sprintf('%s:! ZRPOS to [%ld] limit reached',self::LOGKEY,$newpos));
return self::LSZ_ERROR;
}
@@ -1958,7 +1965,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
$this->ls_txLastACK = $newpos;
if (! $send->seek($newpos)) {
Log::error(sprintf('%s: ! ZRPOS to [%ld] seek error',__METHOD__,$send->filepos));
Log::error(sprintf('%s:! ZRPOS to [%ld] seek error',self::LOGKEY,$send->filepos));
return self::LSZ_ERROR;
}
@@ -1980,11 +1987,11 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
*/
private function ls_zsenddata(string $data,int $frame): int
{
Log::debug(sprintf('%s: + Start [%d] (%d)',__METHOD__,strlen($data),$frame));
Log::debug(sprintf('%s:+ ls_zsenddata [%d] (%d)',self::LOGKEY,strlen($data),$frame));
if ($this->ls_Protocol&self::LSZ_OPTCRC32) {
if ($this->DEBUG)
Log::debug(sprintf('%s: - CRC32',__METHOD__));
Log::debug(sprintf('%s: - ls_zsenddata CRC32',self::LOGKEY));
for ($n=0;$n<strlen($data);$n++)
$this->ls_sendchar(ord($data[$n]));
@@ -2013,7 +2020,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
} else {
if ($this->DEBUG)
Log::debug(sprintf('%s: - CRC16',__METHOD__));
Log::debug(sprintf('%s: - ls_zsenddata CRC16',self::LOGKEY));
for ($n=0;$n<strlen($data);$n++) {
$this->ls_sendchar(ord($data[$n]));
@@ -2052,7 +2059,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
*/
private function ls_zsendbhdr(int $frametype,array $hdr): int
{
Log::debug(sprintf('%s: + Start',__METHOD__));
Log::debug(sprintf('%s:+ ls_zsendbhdr',self::LOGKEY));
$crc = $this->LSZ_INIT_CRC();
@@ -2113,7 +2120,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
*/
private function ls_zsendfile(Send $send,int $sernum,int $fileleft,int $bytesleft): int
{
Log::debug(sprintf('%s: + Start [%s]',__METHOD__,$send->name));
Log::debug(sprintf('%s:+ ls_zsendfile [%s]',self::LOGKEY,$send->name));
$trys = 0;
$needack = 0;
@@ -2121,7 +2128,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
switch (($rc = $this->ls_zsendfinfo($send,$sernum,$send->filepos,$fileleft,$bytesleft))) {
/* Ok, It's OK! */
case self::ZRPOS:
Log::debug(sprintf('%s: - ZRPOS to [%d]',__METHOD__,$send->filepos));
Log::debug(sprintf('%s: - ls_zsendfile ZRPOS to [%d]',self::LOGKEY,$send->filepos));
break;
/* Skip it */
@@ -2129,13 +2136,13 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
/* Suspend it */
case self::ZFERR:
// @todo Mark the file as skipped
Log::debug(sprintf('%s: - ZSKIP/ZFERR',__METHOD__));
Log::debug(sprintf('%s: - ls_zsendfile ZSKIP/ZFERR',self::LOGKEY));
return $rc;
case self::ZABORT:
/* Session is aborted */
case self::ZFIN:
Log::debug(sprintf('%s: - ZABORT/ZFIN',__METHOD__));
Log::debug(sprintf('%s: - ls_zsendfile ZABORT/ZFIN',self::LOGKEY));
$this->ls_zsendhhdr(self::ZFIN,$this->ls_storelong(0));
return self::LSZ_ERROR;
@@ -2144,7 +2151,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
if ($rc < 0)
return $rc;
Log::debug(sprintf('%s: - Strange answer on ZFILE: %d',__METHOD__,$rc));
Log::debug(sprintf('%s:- ls_zsendfile Strange answer on ZFILE [%d]',self::LOGKEY,$rc));
return self::LSZ_ERROR;
}
@@ -2161,7 +2168,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
/* We need to send ZDATA if previous frame was ZCRCW
Also, frame will be ZCRCW, if it is after RPOS */
if ($frame == self::ZCRCW) {
Log::debug(sprintf('%s: - send ZDATA at [%d]',__METHOD__,$send->filepos));
Log::debug(sprintf('%s: - ls_zsendfile send ZDATA at [%d]',self::LOGKEY,$send->filepos));
if (($rc=$this->ls_zsendbhdr(self::ZDATA,$this->ls_storelong($send->filepos))) < 0)
return $rc;
@@ -2172,7 +2179,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
$txbuf = $send->read($this->ls_txCurBlockSize);
} catch (\Exception $e) {
Log::error(sprintf('%s: Read error'));
Log::error(sprintf('%s:! ls_zsendfile Read error',self::LOGKEY));
return self::LSZ_ERROR;
}
@@ -2219,28 +2226,28 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
switch (($rc=$this->ls_zrecvhdr($this->ls_rxHdr,$needack ? $this->ls_HeaderTimeout : 0))) { // @todo set timeout to 5 for debugging wtih POP
/* They don't need this file */
case self::ZSKIP:
Log::debug(sprintf('%s: - ZSKIP',__METHOD__));
Log::debug(sprintf('%s: - ls_zsendfile ZSKIP',self::LOGKEY));
/* Problems occured -- suspend file */
case self::ZFERR:
Log::debug(sprintf('%s: - ZFERR',__METHOD__));
Log::debug(sprintf('%s: - ls_zsendfile ZFERR',self::LOGKEY));
return $rc;
/* Ok, position ACK */
case self::ZACK:
$this->ls_txLastACK = $this->ls_fetchlong($this->ls_rxHdr);
Log::debug(sprintf('%s: - ZACK',__METHOD__),['ls_rxHdr'=>$this->ls_rxHdr,'ls_txLastACK'=>$this->ls_txLastACK,'ls_rxHdr'=>$this->ls_rxHdr]);
Log::debug(sprintf('%s: - ls_zsendfile ZACK',self::LOGKEY),['ls_rxHdr'=>$this->ls_rxHdr,'ls_txLastACK'=>$this->ls_txLastACK,'ls_rxHdr'=>$this->ls_rxHdr]);
break;
/* Repos */
case self::ZRPOS:
Log::debug(sprintf('%s: - ZRPOS',__METHOD__));
Log::debug(sprintf('%s: - ZRPOS',self::LOGKEY));
if (($rc=$this->ls_zrpos($send,$this->ls_fetchlong($this->ls_rxHdr))) < 0)
return $rc;
Log::debug(sprintf('%s: - ZRPOS [%d]',__METHOD__,$rc));
Log::debug(sprintf('%s: - ZRPOS [%d]',self::LOGKEY,$rc));
/* Force to retransmit ZDATA */
$frame = self::ZCRCW;
@@ -2265,7 +2272,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
break;
default:
Log::error(sprintf('%s: ? Something strange [%d]',__METHOD__,$rc));
Log::error(sprintf('%s: ? ls_zsendfile Something strange [%d]',self::LOGKEY,$rc));
if ($rc < 0)
return $rc;
@@ -2324,7 +2331,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
/* ACK for data -- it lost! */
case self::ZACK:
Log::debug(sprintf('%s: - ZACK after EOF',__METHOD__));
Log::debug(sprintf('%s: - ls_zsendfile ZACK after EOF',self::LOGKEY));
$this->ls_txLastACK = $this->ls_fetchlong($this->ls_rxHdr);
break;
@@ -2344,7 +2351,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
break;
default:
Log::error(sprintf('%s: ? Something strange after ZEOF [%d]',__METHOD__,$rc));
Log::error(sprintf('%s: ? ls_zsendfile Something strange after ZEOF [%d]',self::LOGKEY,$rc));
if ($rc < 0)
return $rc;
@@ -2355,14 +2362,14 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
} while ($send->feof() && $trys < 10);
if ($send->feof()) {
Log::error(sprintf('%s: ! To many tries waiting for ZEOF ACK',__METHOD__));
Log::error(sprintf('%s:! ls_zsendfile To many tries waiting for ZEOF ACK',self::LOGKEY));
return self::LSZ_ERROR;
}
}
}
Log::error(sprintf('%s: ? Something strange or timeout [%d]',__METHOD__,$rc));
Log::error(sprintf('%s:? ls_zsendfile Something strange or timeout [%d]',self::LOGKEY,$rc));
return $rc;
}