Fixes to EMSI/ZModem as a result of previous enhancements to items

This commit is contained in:
2023-07-18 23:00:03 +10:00
parent c1ec4eff36
commit ee03604de3
2 changed files with 137 additions and 114 deletions

View File

@@ -35,8 +35,8 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
private const EMSI_NAK = self::EMSI_BEG.'NAKEEC3';
private const EMSI_HBT = self::EMSI_BEG.'HBTEAEE';
private const CR = "\r";
private const NL = "\n";
private const CR = "\x0d"; // \r
private const NL = "\x0a"; // \n;
private const DEL = "\x08";
/* FEATURES */
@@ -58,7 +58,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
private const EMSI_LOG_OUT = 1;
/* FREQs flags */
private const FR_NOTHANDLED = (-1);
private const FR_NOTHANDLED = -1;
private const FR_NOTAVAILABLE = 0;
private const FR_AVAILABLE = 1;
@@ -224,17 +224,19 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
'XA' // Nodelist Flags
);
// TRAF - netmail/echomail traffic size (bytes)
$makedata .= sprintf('{TRAF}{%lX %lX}',$this->send->mail_size,$this->send->size);
// TRAF - netmail & files size (bytes)
$makedata .= sprintf('{TRAF}{%lX %lX}',$this->send->mail_size,$this->send->files_size);
// MOH# - Mail On Hold - bytes waiting
$makedata .= sprintf('{MOH#}{[%lX]}',$this->send->mail_size);
$makedata .= sprintf('{MOH#}{[%lX]}',$this->send->total_size);
// EMD5 - MD5 unique string
// Transaction Number (Time in local time)
$makedata .= sprintf('{TRX#}{[%lX]}',Carbon::now()->timestamp+Carbon::now($this->node->node_timezone)->offset);
// FREQ Time - NRQ (No Requests if this is not defined)
$makedata .= sprintf('{OHFR}{Always! CM}');
$makedata .= sprintf('{TZUTC}{[%+05d]}',-10*60);
// @todo Not sure what OHFR is for
@@ -506,9 +508,9 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
*/
private function emsi_recv(int $mode): int
{
Log::debug(sprintf('%s:+ emsi_recv',self::LOGKEY));
Log::debug(sprintf('%s:+ EMSI receive handshake',self::LOGKEY));
Log::debug(sprintf('%s: - STEP 1',self::LOGKEY));
Log::debug(sprintf('%s: - STEP 1',self::LOGKEY));
/*
* Step 1
* +-+------------------------------------------------------------------+
@@ -523,7 +525,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
do {
step2:
Log::debug(sprintf('%s: - STEP 2',self::LOGKEY));
Log::debug(sprintf('%s: - STEP 2',self::LOGKEY));
/* Step 2
+-+------------------------------------------------------------------+
:2: Increment Tries :
@@ -554,7 +556,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
$this->client->buffer_flush(5);
step3:
Log::debug(sprintf('%s: - STEP 3',self::LOGKEY));
Log::debug(sprintf('%s: - STEP 3',self::LOGKEY));
/* Step 3
* +-+------------------------------------------------------------------+
* :3: T1=20 seconds :
@@ -564,7 +566,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
$t1 = $this->client->timer_set(20);
step4:
Log::debug(sprintf('%s: - STEP 4',self::LOGKEY));
Log::debug(sprintf('%s: - STEP 4',self::LOGKEY));
/* Step 4
+-+------------------------------------------------------------------+
:4: Wait for EMSI sequence until EMSI_HBT or EMSI_DAT or any of the :
@@ -610,7 +612,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
if (($ch === ord(self::CR)) || ($ch === ord(self::NL))) {
if (! strncmp($p,self::EMSI_HBT,self::EMSI_SEQ_LEN)) {
Log::debug(sprintf('%s: - Received EMSI_HBT',self::LOGKEY));
Log::debug(sprintf('%s:- Received EMSI_HBT',self::LOGKEY));
goto step3;
}
@@ -618,7 +620,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
if (! strncmp($p,self::EMSI_DAT,10)) {
Log::debug(sprintf('%s:- Received EMSI_DAT',self::LOGKEY));
Log::debug(sprintf('%s: - STEP 5',self::LOGKEY));
Log::debug(sprintf('%s: - STEP 5',self::LOGKEY));
/* Step 5
+-+------------------------------------------------------------------+
:5: Receive EMSI_DAT packet :
@@ -636,7 +638,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
$this->client->buffer_add(self::EMSI_ACK.self::CR);
$this->client->buffer_flush(5);
Log::debug(sprintf('%s: - STEP 6',self::LOGKEY));
Log::debug(sprintf('%s: - STEP 6',self::LOGKEY));
/* Step 6
+-+------------------------------------------------------------------+
:6: Received EMSI_DAT packet OK, exit. :
@@ -646,7 +648,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
return self::OK;
} else {
Log::debug(sprintf('%s: - EMSI_DAT didnt parse',self::LOGKEY));
Log::error(sprintf('%s:! EMSI_DAT didnt parse',self::LOGKEY));
goto step2;
}
@@ -657,11 +659,11 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
} else {
if (strlen($p) >= self::EMSI_BUF) {
Log::warning(sprintf('%s: ! EMSI_DAT packet too long.',self::LOGKEY));
Log::warning(sprintf('%s:! EMSI_DAT packet too long.',self::LOGKEY));
$rew = strstr($p,self::EMSI_BEG,TRUE);
if ($rew && $rew != $p) {
Log::notice(sprintf('%s: - Got EMSI_DAT at offset [%d].',self::LOGKEY,strlen($rew)));
Log::notice(sprintf('%s:- Got EMSI_DAT at offset [%d].',self::LOGKEY,strlen($rew)));
$p = substr($p,strlen($rew));
}
@@ -685,7 +687,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
*/
private function emsi_send(): int
{
Log::debug(sprintf('%s:+ EMSI send',self::LOGKEY));
Log::debug(sprintf('%s:+ EMSI transmit Handshake',self::LOGKEY));
Log::debug(sprintf('%s: - STEP 1',self::LOGKEY));
/* Step 1
@@ -721,7 +723,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
:3: T2=20 seconds :
+-+------------------------------------------------------------------+
*/
Log::debug(sprintf('%s: - STEP 3',self::LOGKEY));
Log::debug(sprintf('%s: - STEP 3',self::LOGKEY));
$t2 = $this->client->timer_set(20);
/* Step 4
@@ -739,7 +741,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
: : If any other sequence received, go to step 2. :
+-+------------------------------------------------------------------+
*/
Log::debug(sprintf('%s: - STEP 4',self::LOGKEY));
Log::debug(sprintf('%s: - STEP 4',self::LOGKEY));
while (! $this->client->timer_expired($t1)) {
$ch = $this->client->read_ch(max( 1,min($this->client->timer_rest($t1),$this->client->timer_rest($t2))));
// Log::debug(sprintf('%s: - Got (%x) {%03d} (%c)',__METHOD__,$ch,$ch,$ch));
@@ -763,7 +765,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
continue;
if (! strncmp($p,self::EMSI_DAT,10)) {
Log::warning(sprintf('%s: - Got unexpected EMSI_DAT - Argus?',self::LOGKEY));
Log::warning(sprintf('%s:! Got unexpected EMSI_DAT - Argus?',self::LOGKEY));
$this->client->buffer_add(self::EMSI_ACK);
$this->client->buffer_add(self::EMSI_ACK);
$this->client->buffer_flush(1);
@@ -771,12 +773,12 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
$t2 = $this->client->timer_set($this->client->timer_rest($t2) >> 2);
} else if (! strncmp($p,self::EMSI_REQ,self::EMSI_SEQ_LEN)) {
Log::notice(sprintf('%s: - Got EMSI_REQ - skipping...',self::LOGKEY),['p'=>$p]);
Log::notice(sprintf('%s:- Got EMSI_REQ - skipping...',self::LOGKEY),['p'=>$p]);
} else if (! strncmp($p,self::EMSI_ACK,self::EMSI_SEQ_LEN)) {
Log::debug(sprintf('%s: - Got EMSI_ACK',self::LOGKEY));
Log::debug(sprintf('%s:- Got EMSI_ACK',self::LOGKEY));
Log::debug(sprintf('%s: - STEP 5',self::LOGKEY));
Log::debug(sprintf('%s: - STEP 5',self::LOGKEY));
/* Step 5
+-+------------------------------------------------------------------+
:5: Received EMSI_ACK, exit. :
@@ -796,7 +798,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
$p .= chr($ch);
} else {
Log::warning(sprintf('%s: ! EMSI packet too long',self::LOGKEY));
Log::warning(sprintf('%s:! EMSI packet too long',self::LOGKEY));
}
}
} /* goto step4; */
@@ -825,7 +827,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
protected function protocol_init(): int
{
if ($this->DEBUG)
Log::debug(sprintf('%s:+ protocol_init',self::LOGKEY));
Log::debug(sprintf('%s:+ Starting EMSI Protocol INIT',self::LOGKEY));
$got = 0;
$tries = 0;
@@ -851,7 +853,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
while (TRUE) {
$ch = $this->client->read_ch(max( 1,min($this->client->timer_rest($t1),$this->client->timer_rest($t2))));
if ($this->DEBUG)
Log::debug(sprintf('%s: - Got [%x] (%c)',self::LOGKEY,$ch,$ch));
Log::debug(sprintf('%s:- Got [%x] (%c)',self::LOGKEY,$ch,$ch));
if (($ch != self::TIMEOUT) && ($ch < 0))
return $ch;
@@ -870,7 +872,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
if (++$tries > 10)
return self::TIMEOUT;
Log::debug(sprintf('%s: - Sending EMSI_INQ (Try %d of 10)...',self::LOGKEY,$tries));
Log::debug(sprintf('%s:- Sending EMSI_INQ (Try %d of 10)...',self::LOGKEY,$tries));
$this->client->buffer_add(self::EMSI_INQ.self::CR);
}
@@ -885,7 +887,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
if (($ch === ord(self::CR)) || ($ch === ord(self::NL))) {
if (strstr($p,self::EMSI_REQ)) {
Log::info(sprintf('%s: - Got EMSI_REQ',self::LOGKEY));
Log::info(sprintf('%s:- Got EMSI_REQ',self::LOGKEY));
if ($gotreq++)
return self::OK;
@@ -893,7 +895,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
$this->client->buffer_flush(5);
} elseif ($p && strstr($p,self::EMSI_BEG) && strstr($p,self::EMSI_ARGUS1)) {
Log::info(sprintf('%s: - Got Intro [%s]',self::LOGKEY,$p));
Log::info(sprintf('%s:- Got Intro [%s]',self::LOGKEY,$p));
}
continue;
@@ -917,7 +919,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
while (! $this->client->timer_expired($t1)) {
$ch = $this->client->read_ch(max( 1,min($this->client->timer_rest($t1),$this->client->timer_rest($t2))));
if ($this->DEBUG)
Log::debug(sprintf('%s: - Got [%x] (%c)',self::LOGKEY,$ch,$ch));
Log::debug(sprintf('%s:- Got [%x] (%c)',self::LOGKEY,$ch,$ch));
if (($ch != self::TIMEOUT) && ($ch < 0))
return $ch;
@@ -946,7 +948,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
$got = 0;
if (strstr($p, self::EMSI_INQ)) {
Log::info(sprintf('%s: - Got EMSI_REQ',self::LOGKEY));
Log::info(sprintf('%s:- Got EMSI_REQ',self::LOGKEY));
return self::OK;
}
@@ -973,14 +975,14 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
{
// @todo introduce emsi_init() to perform the job of protocol_init. Only needs to be done when we originate a session
Log::debug(sprintf('%s:+ Starting EMSI Protocol Session',self::LOGKEY));
Log::debug(sprintf('%s:+ Starting EMSI Protocol SESSION',self::LOGKEY));
$was_req = 0;
$got_req = 0;
// Outbound session
if ($this->originate) {
Log::debug(sprintf('%s: - Outbound session',self::LOGKEY));
Log::debug(sprintf('%s:- Outbound session',self::LOGKEY));
$this->optionSet(self::O_PUA);
//$emsi_lo |= ($this->is_freq_available() <= self::FR_NOTAVAILABLE ) ? self::O_NRQ : $emsi_lo;
@@ -992,11 +994,11 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
if ($rc < 0)
return (self::S_REDIAL|self::S_ADDTRY);
Log::info(sprintf('%s: - Starting outbound EMSI session to [%s]',self::LOGKEY,$this->client->address_remote));
Log::info(sprintf('%s:- Starting outbound EMSI session to [%s]',self::LOGKEY,$this->client->address_remote));
// Inbound session
} else {
Log::debug(sprintf('%s: - Inbound session',self::LOGKEY));
Log::debug(sprintf('%s:- Inbound session',self::LOGKEY));
$rc = $this->emsi_recv(self::SM_INBOUND);
if ($rc < 0) {
@@ -1005,7 +1007,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
return (self::S_REDIAL|self::S_ADDTRY);
}
Log::info(sprintf('%s: - Starting inbound EMSI session from [%s]',self::LOGKEY,$this->client->address_remote));
Log::info(sprintf('%s:- Starting inbound EMSI session from [%s]',self::LOGKEY,$this->client->address_remote));
if ($this->node->aka_authed) {
$xproto = $this->is_freq_available();
@@ -1016,7 +1018,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
foreach ($this->protocols as $p => $key) {
if ($this->node->optionGet($key)) {
Log::debug(sprintf('%s: - Remote supports [%s] (%x)',self::LOGKEY,$p,$key));
Log::debug(sprintf('%s:- Remote supports [%s] (%x)',self::LOGKEY,$p,$key));
$this->optionSet($key);
}
}
@@ -1033,7 +1035,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
// @todo Lock Node AKAs
Log::info(sprintf('%s: - We have %lu%s mail, %lu%s files',self::LOGKEY,$this->send->mail_size,'b',$this->send->files_size,'b'));
Log::info(sprintf('%s:- We have [%lu%s] mail, [%lu%s] files',self::LOGKEY,$this->send->mail_size,'b',$this->send->files_size,'b'));
$proto = $this->originate ? $this->node->optionGet(self::P_MASK) : $this->optionGet(self::P_MASK);
@@ -1084,9 +1086,9 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
$xproto = ($this->optionGet(self::O_RH1) && ($this->node->optionGet(self::O_RH1)));
$x = (substr($t,1,1) === 'H' && $xproto ) ? 'x' : '';
Log::info(sprintf('%s: = Using [%s]',self::LOGKEY,$t));
Log::info(sprintf('%s:- Using [%s]',self::LOGKEY,$t));
Log::debug(sprintf('%s: = Options: %s%s%s%s%s%s%s%s%s%s%s',
Log::debug(sprintf('%s:/ Options: %s%s%s%s%s%s%s%s%s%s%s',
self::LOGKEY,$x,$t,
($this->node->optionGet(self::O_LST)) ? '/LST' : '',
($this->node->optionGet(self::O_PWD)) ? '/PWD' : '',
@@ -1179,7 +1181,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
*/
private function wazoorecv(int $zap): bool
{
Log::debug(sprintf('%s:+ wazoorecv',self::LOGKEY));
Log::debug(sprintf('%s:+ Start WAZOO Receive',self::LOGKEY));
// @todo If the node is not defined in the DB node->address is NULL. Need to figure out how to handle those nodes.
$rc = (new Zmodem)->zmodem_receive($this->client,$zap,$this->recv,$this->node->address);