diff --git a/app/Classes/FTN/Message.php b/app/Classes/FTN/Message.php index 7cb2b22..c9dec33 100644 --- a/app/Classes/FTN/Message.php +++ b/app/Classes/FTN/Message.php @@ -208,7 +208,22 @@ class Message extends FTNBase { $o = new self($domain); - $o->header = unpack(self::unpackheader(self::header),substr($msg,0,self::HEADER_LEN)); + try { + $o->header = unpack(self::unpackheader(self::header),substr($msg,0,self::HEADER_LEN)); + + } catch (\Exception $e) { + Log::error(sprintf('%s:! Error bad packet header',self::LOGKEY)); + $validator = Validator::make([ + 'header' => substr($msg,0,self::HEADER_LEN), + ],[ + 'header' => [function ($attribute,$value,$fail) use ($e) { return $fail($e->getMessage()); }] + ]); + + if ($validator->fails()) + $o->errors = $validator; + + return $o; + } $ptr = 0; // To User @@ -577,7 +592,7 @@ class Message extends FTNBase if ($aos->has($ftn)) $ao = $aos->get($ftn); else - $aos->put($ftn,$ao=(bool)Address::findFTN($ftn)); + $aos->put($ftn,($ao=(Address::findFTN($ftn))?->id)); if (! $ao) { Log::alert(sprintf('%s:! Undefined Node [%s] in %s.',self::LOGKEY,$ftn,$type)); diff --git a/app/Classes/FTN/Packet.php b/app/Classes/FTN/Packet.php index af8b6e4..9705b6e 100644 --- a/app/Classes/FTN/Packet.php +++ b/app/Classes/FTN/Packet.php @@ -154,16 +154,10 @@ class Packet extends FTNBase implements \Iterator, \Countable $buf_ptr = 0; $message = ''; $readbuf = ''; + $last = ''; while ($buf_ptr || (! feof($f) && ($readbuf=fread($f,self::BLOCKSIZE)))) { - // A message header is atleast 0x22 chars long - if (strlen($readbuf) < self::PACKED_MSG_HEADER_LEN) { - $message .= $readbuf; - $buf_ptr = 0; - - continue; - - } elseif (strlen($message) < self::PACKED_MSG_HEADER_LEN) { + if (strlen($message) < self::PACKED_MSG_HEADER_LEN) { $addchars = self::PACKED_MSG_HEADER_LEN-strlen($message); $message .= substr($readbuf,$buf_ptr,$addchars); $buf_ptr += $addchars; @@ -175,8 +169,35 @@ class Packet extends FTNBase implements \Iterator, \Countable } } - // If we didnt find a packet end, perhaps there are no more + // 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) { + $o->parseMessage(substr($message,0,$end-2),$domain); + $last = ''; + $message = ''; + $buf_ptr = 1+$end; + + // Loop to rebuild our header for the next message + continue; + } + + $last = ''; + } + if (($end=strpos($readbuf,"\x00\x02\x00",$buf_ptr)) === FALSE) { + // In case our packet break is at the end of the buffer + $last = substr($readbuf,-2); + + if ((str_contains($last,"\x00")) && ($fstat['size']-ftell($f) > 2)) { + $message .= substr($readbuf,$buf_ptr); + $buf_ptr = 0; + + continue; + } + + $last = ''; $end = strpos($readbuf,"\x00\x00\x00",$buf_ptr); }