2021-06-29 10:43:29 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Classes\FTN;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Illuminate\Support\Arr;
|
|
|
|
use Illuminate\Support\Collection;
|
2022-01-15 02:06:15 +00:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2021-06-29 10:43:29 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use Symfony\Component\HttpFoundation\File\File;
|
|
|
|
|
|
|
|
use App\Classes\FTN as FTNBase;
|
2021-09-11 13:32:10 +00:00
|
|
|
use App\Models\{Address,Setup,Software,System,Zone};
|
2021-06-29 10:43:29 +00:00
|
|
|
|
2021-08-24 13:42:03 +00:00
|
|
|
class Packet extends FTNBase implements \Iterator, \Countable
|
2021-06-29 10:43:29 +00:00
|
|
|
{
|
|
|
|
private const LOGKEY = 'PKT';
|
|
|
|
|
|
|
|
private const HEADER_LEN = 0x3a;
|
|
|
|
private const VERSION_OFFSET = 0x12;
|
|
|
|
private const BLOCKSIZE = 1024;
|
|
|
|
private const PACKED_MSG_HEADER_LEN = 0x22;
|
|
|
|
|
|
|
|
// V2 Packet Header (2/2e/2+)
|
|
|
|
private const v2header = [
|
|
|
|
'onode' => [0x00,'v',2], // Originating Node
|
|
|
|
'dnode' => [0x02,'v',2], // Destination Node
|
|
|
|
'y' => [0x04,'v',2], // Year
|
|
|
|
'm' => [0x06,'v',2], // Month
|
|
|
|
'd' => [0x08,'v',2], // Day
|
|
|
|
'H' => [0x0a,'v',2], // Hour
|
|
|
|
'M' => [0x0c,'v',2], // Minute
|
|
|
|
'S' => [0x0e,'v',2], // Second
|
|
|
|
'baud' => [0x10,'v',2], // Baud
|
|
|
|
'pktver' => [0x12,'v',2], // Packet Version
|
|
|
|
'onet' => [0x14,'v',2], // Originating Net (0xffff when origPoint !=0 2+)
|
|
|
|
'dnet' => [0x16,'v',2], // Destination Net
|
|
|
|
'prodcode-lo' => [0x18,'C',1],
|
|
|
|
'prodrev-maj' => [0x19,'C',1], // Product Version Major (serialNum 2)
|
|
|
|
'password' => [0x1a,'Z8',8], // Packet Password
|
|
|
|
'qozone' => [0x22,'v',2],
|
|
|
|
'qdzone' => [0x24,'v',2],
|
|
|
|
'filler' => [0x26,'v',2], // Reserved (auxnet 2+ - contains Orignet if Origin is a point) fsc-0048.001
|
2021-06-29 13:23:59 +00:00
|
|
|
'capvalid' => [0x28,'n',2], // fsc-0039.004 (Not used 2) (copy of 0x2c)
|
2021-06-29 10:43:29 +00:00
|
|
|
'prodcode-hi' => [0x2a,'C',1], // (Not used 2)
|
|
|
|
'prodrev-min' => [0x2b,'C',1], // (Not used 2)
|
2021-06-29 13:23:59 +00:00
|
|
|
'capword' => [0x2c,'v',2], // fsc-0039.001 (Not used 2)
|
2021-06-29 10:43:29 +00:00
|
|
|
'ozone' => [0x2e,'v',2], // Originating Zone (Not used 2)
|
|
|
|
'dzone' => [0x30,'v',2], // Destination Zone (Not used 2)
|
|
|
|
'opoint' => [0x32,'v',2], // Originating Point (Not used 2)
|
|
|
|
'dpoint' => [0x34,'v',2], // Destination Point (Not used 2)
|
2021-07-15 14:54:23 +00:00
|
|
|
'proddata' => [0x36,'a4',4], // ProdData (Not used 2) // FSC-39/FSC-48
|
2021-06-29 10:43:29 +00:00
|
|
|
];
|
|
|
|
|
2021-07-15 14:54:23 +00:00
|
|
|
private array $header; // Packet Header
|
|
|
|
|
|
|
|
public File $file; // Packet filename
|
|
|
|
public Collection $messages; // Messages in the Packet
|
2021-08-13 13:46:48 +00:00
|
|
|
public Collection $errors; // Messages that fail validation
|
2021-07-18 12:10:21 +00:00
|
|
|
private string $name; // Packet name
|
2023-01-25 05:26:10 +00:00
|
|
|
public bool $use_cache = FALSE; // Use a cache for messages.
|
2021-08-24 13:42:03 +00:00
|
|
|
private int $index; // Our array index
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of messages in this packet
|
|
|
|
*/
|
|
|
|
public function count(): int
|
|
|
|
{
|
|
|
|
return $this->messages->count();
|
|
|
|
}
|
|
|
|
|
2022-01-15 02:06:15 +00:00
|
|
|
public function current(): Message
|
2021-08-24 13:42:03 +00:00
|
|
|
{
|
2022-01-15 02:06:15 +00:00
|
|
|
return $this->use_cache ? unserialize(Cache::pull($this->key())) : $this->messages->get($this->index);
|
2021-08-24 13:42:03 +00:00
|
|
|
}
|
|
|
|
|
2022-01-15 02:06:15 +00:00
|
|
|
public function key(): mixed
|
2021-08-24 13:42:03 +00:00
|
|
|
{
|
2022-01-15 02:06:15 +00:00
|
|
|
return $this->use_cache ? $this->messages->get($this->index) : $this->index;
|
2021-08-24 13:42:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function next(): void
|
|
|
|
{
|
|
|
|
$this->index++;
|
|
|
|
}
|
|
|
|
|
2022-01-15 02:06:15 +00:00
|
|
|
public function rewind(): void
|
2021-08-24 13:42:03 +00:00
|
|
|
{
|
|
|
|
$this->index = 0;
|
|
|
|
}
|
|
|
|
|
2022-01-15 02:06:15 +00:00
|
|
|
public function valid(): bool
|
2021-08-24 13:42:03 +00:00
|
|
|
{
|
2022-01-15 02:06:15 +00:00
|
|
|
return (! is_null($this->key())) && ($this->use_cache ? Cache::has($this->key()) : $this->messages->has($this->key()));
|
2021-08-24 13:42:03 +00:00
|
|
|
}
|
2021-07-15 14:54:23 +00:00
|
|
|
|
2022-01-20 06:54:02 +00:00
|
|
|
/**
|
|
|
|
* @param Address|NULL $oo Origin Address
|
|
|
|
* @param Address|NULL $o Destination Address
|
|
|
|
*/
|
|
|
|
public function __construct(Address $oo=NULL,Address $o=NULL)
|
2021-06-29 10:43:29 +00:00
|
|
|
{
|
|
|
|
$this->messages = collect();
|
2021-08-13 13:46:48 +00:00
|
|
|
$this->errors = collect();
|
2021-07-15 14:54:23 +00:00
|
|
|
$this->domain = NULL;
|
|
|
|
|
|
|
|
// If we are creating an outbound packet, we need to set our header
|
2022-01-20 11:47:44 +00:00
|
|
|
if ($oo && $o) {
|
|
|
|
$this->name = sprintf('%08x',timew());
|
|
|
|
Log::debug(sprintf('%s:Creating packet [%s]',self::LOGKEY,$this->name));
|
2022-01-20 06:54:02 +00:00
|
|
|
$this->newHeader($oo,$o);
|
2022-01-20 11:47:44 +00:00
|
|
|
}
|
2021-07-15 14:54:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-13 13:29:55 +00:00
|
|
|
* Process a packet file
|
2021-07-15 14:54:23 +00:00
|
|
|
*
|
2022-11-13 13:29:55 +00:00
|
|
|
* @param mixed $f
|
|
|
|
* @param string $name
|
|
|
|
* @param int $size
|
2021-11-25 10:22:36 +00:00
|
|
|
* @param System|null $system
|
2022-01-15 02:06:15 +00:00
|
|
|
* @param bool $use_cache
|
2021-07-15 14:54:23 +00:00
|
|
|
* @return Packet
|
|
|
|
* @throws InvalidPacketException
|
|
|
|
*/
|
2022-11-13 13:29:55 +00:00
|
|
|
|
2023-01-25 05:26:10 +00:00
|
|
|
public static function process(mixed $f,string $name,int $size,System $system=NULL,bool $use_cache=FALSE): self
|
2021-07-15 14:54:23 +00:00
|
|
|
{
|
2022-11-13 13:29:55 +00:00
|
|
|
Log::debug(sprintf('%s:+ Opening Packet [%s] with size [%d]',self::LOGKEY,$name,$size));
|
2021-07-15 14:54:23 +00:00
|
|
|
|
2022-11-13 13:29:55 +00:00
|
|
|
$read_ptr = 0;
|
2021-07-15 14:54:23 +00:00
|
|
|
|
|
|
|
// PKT Header
|
|
|
|
$header = fread($f,self::HEADER_LEN);
|
2022-11-13 13:29:55 +00:00
|
|
|
$read_ptr += strlen($header);
|
2021-07-15 14:54:23 +00:00
|
|
|
|
|
|
|
// Could not read header
|
|
|
|
if (strlen($header) != self::HEADER_LEN)
|
|
|
|
throw new InvalidPacketException(sprintf('Length of header [%d] too short'.strlen($header)));
|
|
|
|
|
|
|
|
// Not a type 2 packet
|
|
|
|
$version = Arr::get(unpack('vv',substr($header,self::VERSION_OFFSET)),'v');
|
|
|
|
if ($version != 2)
|
|
|
|
throw new InvalidPacketException('Not a type 2 packet: '.$version);
|
|
|
|
|
|
|
|
$o = new self;
|
2022-01-15 02:06:15 +00:00
|
|
|
$o->use_cache = $use_cache;
|
2022-11-13 13:29:55 +00:00
|
|
|
$o->name = $name;
|
2021-07-15 14:54:23 +00:00
|
|
|
$o->header = unpack(self::unpackheader(self::v2header),$header);
|
|
|
|
|
|
|
|
$x = fread($f,2);
|
2022-11-13 13:29:55 +00:00
|
|
|
$read_ptr += strlen($x);
|
2021-07-15 14:54:23 +00:00
|
|
|
|
|
|
|
// End of Packet?
|
|
|
|
if (strlen($x) == 2 and $x == "\00\00")
|
|
|
|
return new self;
|
|
|
|
|
|
|
|
// Messages start with 02H 00H
|
|
|
|
if (strlen($x) == 2 AND $x != "\02\00")
|
|
|
|
throw new InvalidPacketException('Not a valid packet: '.bin2hex($x));
|
2021-06-29 10:43:29 +00:00
|
|
|
|
2021-07-15 14:54:23 +00:00
|
|
|
// No message attached
|
|
|
|
else if (! strlen($x))
|
|
|
|
throw new InvalidPacketException('No message in packet: '.bin2hex($x));
|
|
|
|
|
2021-11-24 11:34:40 +00:00
|
|
|
$o->zone = $system?->zones->firstWhere('zone_id',$o->fz);
|
|
|
|
|
2021-08-29 13:58:12 +00:00
|
|
|
// If zone is null, we'll take the zone from the packet
|
|
|
|
if (! $o->zone)
|
|
|
|
$o->zone = Zone::where('zone_id',$o->fz)->where('default',TRUE)->single();
|
|
|
|
|
2021-07-15 14:54:23 +00:00
|
|
|
$buf_ptr = 0;
|
|
|
|
$message = '';
|
|
|
|
$readbuf = '';
|
2021-08-26 12:01:16 +00:00
|
|
|
$last = '';
|
2021-07-15 14:54:23 +00:00
|
|
|
|
|
|
|
while ($buf_ptr || (! feof($f) && ($readbuf=fread($f,self::BLOCKSIZE)))) {
|
2022-11-15 11:01:05 +00:00
|
|
|
if (! $buf_ptr)
|
|
|
|
$read_ptr += strlen($readbuf); // Could use ftell()
|
2022-11-13 13:29:55 +00:00
|
|
|
|
2021-08-26 12:01:16 +00:00
|
|
|
if (strlen($message) < self::PACKED_MSG_HEADER_LEN) {
|
2021-07-15 14:54:23 +00:00
|
|
|
$addchars = self::PACKED_MSG_HEADER_LEN-strlen($message);
|
|
|
|
$message .= substr($readbuf,$buf_ptr,$addchars);
|
|
|
|
$buf_ptr += $addchars;
|
2021-08-13 13:46:48 +00:00
|
|
|
|
|
|
|
// If our buffer wasnt big enough...
|
|
|
|
if ($buf_ptr >= strlen($readbuf)) {
|
|
|
|
$buf_ptr = 0;
|
|
|
|
continue;
|
|
|
|
}
|
2021-07-15 14:54:23 +00:00
|
|
|
}
|
|
|
|
|
2021-08-26 12:01:16 +00:00
|
|
|
// Take 2 chars from the buffer and check if we have our end packet signature
|
|
|
|
if ($last && ($buf_ptr == 0)) {
|
|
|
|
$last .= substr($readbuf,0,2);
|
|
|
|
|
|
|
|
if (($end=strpos($last,"\x00\x02\x00",$buf_ptr)) !== FALSE) {
|
2021-08-29 13:58:12 +00:00
|
|
|
$o->parseMessage(substr($message,0,$end-2));
|
2021-08-26 12:01:16 +00:00
|
|
|
$last = '';
|
|
|
|
$message = '';
|
|
|
|
$buf_ptr = 1+$end;
|
|
|
|
|
|
|
|
// Loop to rebuild our header for the next message
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$last = '';
|
|
|
|
}
|
|
|
|
|
2021-07-15 14:54:23 +00:00
|
|
|
if (($end=strpos($readbuf,"\x00\x02\x00",$buf_ptr)) === FALSE) {
|
2022-11-15 11:01:05 +00:00
|
|
|
// Just in case our packet break is at the end of the buffer
|
2021-08-26 12:01:16 +00:00
|
|
|
$last = substr($readbuf,-2);
|
|
|
|
|
2022-11-13 13:29:55 +00:00
|
|
|
if ((str_contains($last,"\x00")) && ($size-$read_ptr > 2)) {
|
2021-08-26 12:01:16 +00:00
|
|
|
$message .= substr($readbuf,$buf_ptr);
|
|
|
|
$buf_ptr = 0;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$last = '';
|
2021-07-15 14:54:23 +00:00
|
|
|
$end = strpos($readbuf,"\x00\x00\x00",$buf_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if we have found the end of the packet, if not read more.
|
2022-11-13 13:29:55 +00:00
|
|
|
if ($end === FALSE && ($read_ptr < $size)) {
|
2021-07-15 14:54:23 +00:00
|
|
|
$message .= substr($readbuf,$buf_ptr);
|
|
|
|
$buf_ptr = 0;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$message .= substr($readbuf,$buf_ptr,$end-$buf_ptr);
|
|
|
|
$buf_ptr = $end+3;
|
|
|
|
|
|
|
|
if ($buf_ptr >= strlen($readbuf))
|
|
|
|
$buf_ptr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for the next message
|
2021-08-29 13:58:12 +00:00
|
|
|
$o->parseMessage($message);
|
2021-07-15 14:54:23 +00:00
|
|
|
$message = '';
|
2021-06-29 10:43:29 +00:00
|
|
|
}
|
2021-07-15 14:54:23 +00:00
|
|
|
|
2021-07-30 14:35:52 +00:00
|
|
|
// If our message is still set, then we have an unprocessed message
|
|
|
|
if ($message)
|
2021-08-29 13:58:12 +00:00
|
|
|
$o->parseMessage($message);
|
2021-07-30 14:35:52 +00:00
|
|
|
|
2021-07-15 14:54:23 +00:00
|
|
|
return $o;
|
2021-06-29 10:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function __get($key)
|
|
|
|
{
|
|
|
|
switch ($key) {
|
|
|
|
// From Addresses
|
|
|
|
case 'fz': return Arr::get($this->header,'ozone');
|
|
|
|
case 'fn': return Arr::get($this->header,'onet');
|
|
|
|
case 'ff': return Arr::get($this->header,'onode');
|
|
|
|
case 'fp': return Arr::get($this->header,'opoint');
|
|
|
|
case 'fd': return Arr::get($this->header,'odomain');
|
|
|
|
|
|
|
|
// To Addresses
|
|
|
|
case 'tz': return Arr::get($this->header,'dzone');
|
|
|
|
case 'tn': return Arr::get($this->header,'dnet');
|
|
|
|
case 'tf': return Arr::get($this->header,'dnode');
|
|
|
|
case 'tp': return Arr::get($this->header,'dpoint');
|
|
|
|
case 'td': return Arr::get($this->header,'ddomain');
|
|
|
|
|
|
|
|
case 'date':
|
|
|
|
return Carbon::create(
|
|
|
|
Arr::get($this->header,'y'),
|
|
|
|
Arr::get($this->header,'m')+1,
|
|
|
|
Arr::get($this->header,'d'),
|
|
|
|
Arr::get($this->header,'H'),
|
|
|
|
Arr::get($this->header,'M'),
|
|
|
|
Arr::get($this->header,'S')
|
|
|
|
);
|
|
|
|
|
|
|
|
case 'capability':
|
2021-06-29 13:23:59 +00:00
|
|
|
return Arr::get($this->header,'capword') == Arr::get($this->header,'capvalid') ? sprintf('%016b',Arr::get($this->header,'capword')) : 'FTS-1';
|
2021-06-29 10:43:29 +00:00
|
|
|
|
|
|
|
case 'password':
|
|
|
|
return Arr::get($this->header,$key);
|
|
|
|
|
|
|
|
case 'fftn':
|
2021-08-18 14:20:34 +00:00
|
|
|
case 'fftn_o':
|
2021-06-29 10:43:29 +00:00
|
|
|
case 'tftn':
|
2021-08-18 14:20:34 +00:00
|
|
|
case 'tftn_o':
|
2021-06-29 10:43:29 +00:00
|
|
|
return parent::__get($key);
|
|
|
|
|
|
|
|
case 'software':
|
|
|
|
$code = Arr::get($this->header,'prodcode-hi')<<8|Arr::get($this->header,'prodcode-lo');
|
|
|
|
Software::unguard();
|
|
|
|
$o = Software::singleOrNew(['code'=>$code,'type'=>Software::SOFTWARE_TOSSER]);
|
|
|
|
Software::reguard();
|
|
|
|
|
|
|
|
return $o;
|
|
|
|
|
|
|
|
case 'software_ver':
|
|
|
|
return sprintf('%d.%d',Arr::get($this->header,'prodrev-maj'),Arr::get($this->header,'prodrev-min'));
|
|
|
|
|
|
|
|
// Packet Type
|
|
|
|
case 'type':
|
|
|
|
if ((Arr::get($this->header,'onet') == 0xffff) && (Arr::get($this->header,'opoint') != 0) && Arr::get($this->header,'filler'))
|
|
|
|
return '2+';
|
|
|
|
elseif (Arr::get($this->header,'prodrev-maj') && ! Arr::get($this->header,'capword'))
|
|
|
|
return '2';
|
|
|
|
else
|
|
|
|
return '2e';
|
|
|
|
|
2021-07-18 12:10:21 +00:00
|
|
|
// Packet name:
|
|
|
|
case 'name':
|
|
|
|
return $this->{$key};
|
|
|
|
|
2021-06-29 10:43:29 +00:00
|
|
|
default:
|
|
|
|
throw new \Exception('Unknown key: '.$key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 14:54:23 +00:00
|
|
|
/**
|
|
|
|
* Return the packet
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2021-06-29 10:43:29 +00:00
|
|
|
public function __toString(): string
|
|
|
|
{
|
2021-09-08 12:07:00 +00:00
|
|
|
$return = $this->createHeader();
|
2021-06-29 10:43:29 +00:00
|
|
|
|
2021-09-08 12:07:00 +00:00
|
|
|
foreach ($this->messages as $o) {
|
|
|
|
if ($o->packed)
|
|
|
|
$return .= "\02\00".(string)$o;
|
2021-07-17 05:48:07 +00:00
|
|
|
}
|
2021-06-29 10:43:29 +00:00
|
|
|
|
2021-09-08 12:07:00 +00:00
|
|
|
$return .= "\00\00";
|
|
|
|
|
2021-06-29 10:43:29 +00:00
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create our message packet header
|
|
|
|
*/
|
|
|
|
private function createHeader(): string
|
|
|
|
{
|
|
|
|
try {
|
2021-09-08 12:07:00 +00:00
|
|
|
$a = pack(collect(self::v2header)->merge(['password' => [0x1a,'a8',8]])->pluck(1)->join(''),
|
2021-06-29 10:43:29 +00:00
|
|
|
$this->ff,
|
|
|
|
$this->tf,
|
2021-07-15 14:54:23 +00:00
|
|
|
Arr::get($this->header,'y'),
|
|
|
|
Arr::get($this->header,'m'),
|
|
|
|
Arr::get($this->header,'d'),
|
|
|
|
Arr::get($this->header,'H'),
|
|
|
|
Arr::get($this->header,'M'),
|
|
|
|
Arr::get($this->header,'S'),
|
|
|
|
Arr::get($this->header,'baud',0),
|
|
|
|
Arr::get($this->header,'pktver',2),
|
|
|
|
$this->fn, // @todo if point, this needs to be 0xff
|
2021-06-29 10:43:29 +00:00
|
|
|
$this->tn,
|
2021-07-15 14:54:23 +00:00
|
|
|
Arr::get($this->header,'prodcode-lo',(Setup::PRODUCT_ID & 0xff)),
|
|
|
|
Arr::get($this->header,'prodrev-maj',Setup::PRODUCT_VERSION_MAJ),
|
|
|
|
$this->password,
|
2021-06-29 10:43:29 +00:00
|
|
|
$this->fz,
|
|
|
|
$this->tz,
|
2021-07-15 14:54:23 +00:00
|
|
|
Arr::get($this->header,'filler',''),
|
|
|
|
Arr::get($this->header,'capvalid',1<<0),
|
|
|
|
Arr::get($this->header,'prodcode-hi',(Setup::PRODUCT_ID >> 8) & 0xff),
|
|
|
|
Arr::get($this->header,'prodrev-min',Setup::PRODUCT_VERSION_MIN),
|
|
|
|
Arr::get($this->header,'capword',1<<0),
|
|
|
|
$this->fz,
|
|
|
|
$this->tz,
|
|
|
|
$this->fp,
|
|
|
|
$this->tp,
|
|
|
|
Arr::get($this->header,'proddata','AB8D'),
|
2021-06-29 10:43:29 +00:00
|
|
|
);
|
|
|
|
|
2021-07-15 14:54:23 +00:00
|
|
|
return $a;
|
2021-06-29 10:43:29 +00:00
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return $e->getMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 14:54:23 +00:00
|
|
|
/**
|
|
|
|
* Add a netmail message to this packet
|
|
|
|
*
|
|
|
|
* @param Message $o
|
|
|
|
*/
|
2021-07-30 14:35:52 +00:00
|
|
|
public function addMail(Message $o): void
|
2021-06-29 10:43:29 +00:00
|
|
|
{
|
|
|
|
$this->messages->push($o);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-15 14:54:23 +00:00
|
|
|
* When creating a new packet, set the header.
|
2021-06-29 10:43:29 +00:00
|
|
|
*
|
2022-01-20 06:54:02 +00:00
|
|
|
* @param Address $oo
|
2021-07-17 05:48:07 +00:00
|
|
|
* @param Address $o
|
2021-06-29 10:43:29 +00:00
|
|
|
*/
|
2022-01-20 06:54:02 +00:00
|
|
|
private function newHeader(Address $oo,Address $o): void
|
2021-06-29 10:43:29 +00:00
|
|
|
{
|
2021-07-15 14:54:23 +00:00
|
|
|
$date = Carbon::now();
|
|
|
|
|
|
|
|
// Create Header
|
|
|
|
$this->header = [
|
2022-01-20 06:54:02 +00:00
|
|
|
'onode' => $oo->node_id, // Originating Node
|
2021-08-20 14:33:41 +00:00
|
|
|
'dnode' => $o->node_id, // Destination Node
|
|
|
|
'y' => $date->format('Y'), // Year
|
|
|
|
'm' => $date->format('m')-1, // Month
|
|
|
|
'd' => $date->format('d'), // Day
|
|
|
|
'H' => $date->format('H'), // Hour
|
|
|
|
'M' => $date->format('i'), // Minute
|
|
|
|
'S' => $date->format('s'), // Second
|
2022-01-20 06:54:02 +00:00
|
|
|
'onet' => $oo->host_id ?: $oo->region_id, // Originating Net (0xffff when origPoint !=0 2+)
|
2021-08-20 14:33:41 +00:00
|
|
|
'dnet' => $o->host_id ?: $o->region_id, // Destination Net
|
2021-07-15 14:54:23 +00:00
|
|
|
'password' => $o->session('pktpass'), // Packet Password
|
2022-01-20 06:54:02 +00:00
|
|
|
'qozone' => $oo->zone->zone_id,
|
2021-07-15 14:54:23 +00:00
|
|
|
'qdzone' => $o->zone->zone_id,
|
2022-01-20 06:54:02 +00:00
|
|
|
'ozone' => $oo->zone->zone_id, // Originating Zone (Not used 2)
|
2021-08-20 14:33:41 +00:00
|
|
|
'dzone' => $o->zone->zone_id, // Destination Zone (Not used 2)
|
2022-01-20 06:54:02 +00:00
|
|
|
'opoint' => $oo->point_id, // Originating Point (Not used 2)
|
2021-08-20 14:33:41 +00:00
|
|
|
'dpoint' => $o->point_id, // Destination Point (Not used 2)
|
2021-07-15 14:54:23 +00:00
|
|
|
];
|
|
|
|
}
|
2021-06-29 10:43:29 +00:00
|
|
|
|
2021-07-15 14:54:23 +00:00
|
|
|
/**
|
|
|
|
* Parse a message in a mail packet
|
|
|
|
*
|
|
|
|
* @param string $message
|
2021-09-11 13:32:10 +00:00
|
|
|
* @throws InvalidPacketException|\Exception
|
2021-07-15 14:54:23 +00:00
|
|
|
*/
|
2021-08-29 13:58:12 +00:00
|
|
|
private function parseMessage(string $message): void
|
2021-07-15 14:54:23 +00:00
|
|
|
{
|
2021-12-29 02:44:27 +00:00
|
|
|
Log::info(sprintf('%s:Processing message [%d] bytes',self::LOGKEY,strlen($message)));
|
|
|
|
|
2021-08-29 13:58:12 +00:00
|
|
|
$msg = Message::parseMessage($message,$this->zone);
|
2021-07-19 14:26:12 +00:00
|
|
|
|
|
|
|
// If the message is invalid, we'll ignore it
|
2021-09-11 13:32:10 +00:00
|
|
|
if ($msg->errors) {
|
2022-01-15 02:06:15 +00:00
|
|
|
Log::info(sprintf('%s:- Message [%s] has errors',self::LOGKEY,$msg->msgid));
|
2021-12-29 02:44:27 +00:00
|
|
|
|
2021-09-11 13:32:10 +00:00
|
|
|
// If the from address doenst exist, we'll create a new entry
|
2022-12-03 05:00:38 +00:00
|
|
|
if ($msg->errors->messages()->has('to') && $msg->tzone) {
|
2022-11-06 03:40:03 +00:00
|
|
|
try {
|
2023-01-01 03:30:15 +00:00
|
|
|
// @todo Need to work out the correct region for the host_id
|
2022-11-06 03:40:03 +00:00
|
|
|
Address::unguard();
|
|
|
|
$ao = Address::firstOrNew([
|
|
|
|
'zone_id' => $msg->tzone->id,
|
|
|
|
'region_id' => 0,
|
|
|
|
'host_id' => $msg->tn,
|
|
|
|
'node_id' => $msg->tf,
|
|
|
|
'point_id' => $msg->tp,
|
2023-01-01 14:05:44 +00:00
|
|
|
'active' => TRUE,
|
2022-11-06 03:40:03 +00:00
|
|
|
]);
|
|
|
|
Address::reguard();
|
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
2022-11-11 11:57:40 +00:00
|
|
|
Log::error(sprintf('%s:! Error finding/creating TO address [%s] for message',self::LOGKEY,$msg->tboss),['error'=>$e->getMessage()]);
|
2022-11-06 03:40:03 +00:00
|
|
|
$this->errors->push($msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$ao->role = Address::NODE_UNKNOWN;
|
|
|
|
|
|
|
|
System::unguard();
|
|
|
|
$so = System::firstOrCreate([
|
|
|
|
'name' => 'Discovered System',
|
|
|
|
'sysop' => 'Unknown',
|
|
|
|
'location' => '',
|
|
|
|
'active' => TRUE,
|
|
|
|
]);
|
|
|
|
System::reguard();
|
|
|
|
|
|
|
|
$so->addresses()->save($ao);
|
|
|
|
|
|
|
|
Log::alert(sprintf('%s: - To FTN is not defined, creating new entry for [%s] (%d)',self::LOGKEY,$msg->tboss,$ao->id));
|
|
|
|
}
|
|
|
|
|
2022-12-03 05:00:38 +00:00
|
|
|
if ($msg->errors->messages()->has('from') && $msg->tzone) {
|
2021-09-12 12:09:45 +00:00
|
|
|
try {
|
2023-01-01 03:30:15 +00:00
|
|
|
// @todo Need to work out the correct region for the host_id
|
2021-09-12 12:09:45 +00:00
|
|
|
Address::unguard();
|
|
|
|
$ao = Address::firstOrNew([
|
|
|
|
'zone_id' => $msg->fzone->id,
|
|
|
|
'region_id' => 0,
|
|
|
|
'host_id' => $msg->fn,
|
|
|
|
'node_id' => $msg->ff,
|
|
|
|
'point_id' => $msg->fp,
|
2023-01-01 14:05:44 +00:00
|
|
|
'active'=> TRUE,
|
2021-09-12 12:09:45 +00:00
|
|
|
]);
|
|
|
|
Address::reguard();
|
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
2022-11-11 11:57:40 +00:00
|
|
|
Log::error(sprintf('%s:! Error finding/creating FROM address [%s] for message',self::LOGKEY,$msg->fboss),['error'=>$e->getMessage()]);
|
2021-09-11 13:32:10 +00:00
|
|
|
$this->errors->push($msg);
|
|
|
|
return;
|
|
|
|
}
|
2021-08-24 13:42:03 +00:00
|
|
|
|
2022-01-24 11:56:13 +00:00
|
|
|
$ao->role = Address::NODE_UNKNOWN;
|
2021-09-12 12:09:45 +00:00
|
|
|
|
2021-09-11 13:32:10 +00:00
|
|
|
System::unguard();
|
|
|
|
$so = System::firstOrCreate([
|
|
|
|
'name' => 'Discovered System',
|
|
|
|
'sysop' => 'Unknown',
|
|
|
|
'location' => '',
|
|
|
|
'active' => TRUE,
|
|
|
|
]);
|
|
|
|
System::reguard();
|
|
|
|
|
|
|
|
$so->addresses()->save($ao);
|
|
|
|
|
2021-09-12 12:09:45 +00:00
|
|
|
Log::alert(sprintf('%s: - From FTN is not defined, creating new entry for [%s] (%d)',self::LOGKEY,$msg->fboss,$ao->id));
|
2022-11-06 03:40:03 +00:00
|
|
|
}
|
2021-09-12 12:09:45 +00:00
|
|
|
|
2022-11-06 03:40:03 +00:00
|
|
|
if ($msg->errors->messages()->has('user_from') || $msg->errors->messages()->has('user_to')) {
|
2021-09-11 13:32:10 +00:00
|
|
|
Log::error(sprintf('%s:! Skipping message [%s] due to errors (%s)...',self::LOGKEY,$msg->msgid,join(',',$msg->errors->messages()->keys())));
|
|
|
|
$this->errors->push($msg);
|
|
|
|
return;
|
2021-08-24 13:42:03 +00:00
|
|
|
}
|
2021-07-19 14:26:12 +00:00
|
|
|
}
|
2021-09-11 13:32:10 +00:00
|
|
|
|
2022-01-15 02:06:15 +00:00
|
|
|
if ($this->use_cache) {
|
|
|
|
$key = urlencode($msg->msgid ?: sprintf('%s %s',$msg->fftn,Carbon::now()->timestamp));
|
2023-01-25 05:26:10 +00:00
|
|
|
if (! Cache::forever($key,serialize($msg)))
|
|
|
|
throw new \Exception(sprintf('Caching failed for key [%s]?',$key));
|
|
|
|
|
2021-09-11 13:32:10 +00:00
|
|
|
$this->messages->push($key);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$this->messages->push($msg);
|
|
|
|
}
|
2021-06-29 10:43:29 +00:00
|
|
|
}
|
|
|
|
}
|