5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable {
26 * Stacker for chunks with final EOP
28 const STACKER_NAME_CHUNKS_WITH_FINAL_EOP = 'final_chunks';
29 const STACKER_NAME_CHUNKS_WITHOUT_FINAL = 'pending_chunks';
30 const STACKER_NAME_ASSEMBLED_RAW_DATA = 'chunk_raw_data';
38 const CHUNK_SPLITS_INDEX_HASH = 0;
39 const CHUNK_SPLITS_INDEX_SERIAL = 1;
40 const CHUNK_SPLITS_INDEX_RAW_DATA = 2;
43 * The final array for assembling the original package back together
45 private $finalPackageChunks = array();
48 * Array of chunk hashes
50 private $chunkHashes = array();
53 * Raw EOP chunk data in an array:
56 * 1 = Hash of last chunk
58 private $eopChunk = array();
63 private $rawPackageData = '';
66 * Fragmenter instance, needs to be set here again
68 private $fragmenterInstance = NULL;
71 * Protected constructor
75 protected function __construct () {
76 // Call parent constructor
77 parent::__construct(__CLASS__);
80 $this->setHandlerName('chunk');
85 // Get a fragmenter instance for later verification of serial numbers (e.g. if all are received)
86 $fragmenterInstance = FragmenterFactory::createFragmenterInstance('package');
88 // Set it in this handler
89 $this->fragmenterInstance = $fragmenterInstance;
93 * Creates an instance of this class
95 * @return $handlerInstance An instance of a chunk Handler class
97 public final static function createChunkHandler () {
99 $handlerInstance = new ChunkHandler();
101 // Get a FIFO stacker
102 $stackInstance = ObjectFactory::createObjectByConfiguredName('chunk_handler_stacker_class');
105 $stackInstance->initStacks(array(
106 self::STACKER_NAME_CHUNKS_WITH_FINAL_EOP,
107 self::STACKER_NAME_CHUNKS_WITHOUT_FINAL,
108 self::STACKER_NAME_ASSEMBLED_RAW_DATA
111 // Set the stacker in this handler
112 $handlerInstance->setStackInstance($stackInstance);
114 // Get a crypto instance ...
115 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
117 // ... and set it in this handler
118 $handlerInstance->setCryptoInstance($cryptoInstance);
120 // Return the prepared instance
121 return $handlerInstance;
125 * Initializes the handler
129 private function initHandler () {
131 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Initializing handler ...');
133 // Init finalPackageChunks
134 $this->finalPackageChunks = array(
135 // Array for package content
136 'content' => array(),
137 // ... and for the hashes
139 // ... marker for that the final array is complete for assembling all chunks
140 'is_complete' => FALSE,
141 // ... steps done to assemble all chunks
142 'assemble_steps' => 0,
146 $this->chunkHashes = array();
149 $this->eopChunk = array(
156 * Checks whether the hash generated from package content is the same ("valid") as given
158 * @param $chunkSplits An array from a splitted chunk
159 * @return $isValid Whether the hash is "valid"
161 private function isChunkHashValid (array $chunkSplits) {
163 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: chunkSplits=' . print_r($chunkSplits, TRUE));
165 // Assert on some elements
166 assert(isset($chunkSplits[self::CHUNK_SPLITS_INDEX_RAW_DATA]));
167 assert(isset($chunkSplits[self::CHUNK_SPLITS_INDEX_HASH]));
169 // Now hash the raw data again
170 $chunkHash = $this->getCryptoInstance()->hashString($chunkSplits[self::CHUNK_SPLITS_INDEX_RAW_DATA], $chunkSplits[self::CHUNK_SPLITS_INDEX_HASH], FALSE);
173 $isValid = ($chunkSplits[self::CHUNK_SPLITS_INDEX_HASH] === $chunkHash);
176 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: chunkHash=' . $chunkHash . ',isValid=' . intval($isValid));
183 * Checks whether the given serial number is valid
185 * @param $serialNumber A serial number from a chunk
186 * @return $isValid Whether the serial number is valid
188 private function isSerialNumberValid ($serialNumber) {
190 $isValid = ((strlen($serialNumber) == PackageFragmenter::MAX_SERIAL_LENGTH) && ($this->hexval($serialNumber, FALSE) === $serialNumber));
197 * Adds the chunk to the final array which will be used for the final step
198 * which will be to assemble all chunks back to the original package content
199 * and for the final hash check.
201 * This method may throw an exception if a chunk with the same serial number
202 * has already been added to avoid mixing chunks from different packages.
204 * @param $chunkSplits An array from a splitted chunk
207 private function addChunkToFinalArray (array $chunkSplits) {
208 // Is the serial number (index 1) already been added?
209 if (isset($this->finalPackageChunks[$chunkSplits[self::CHUNK_SPLITS_INDEX_SERIAL]])) {
210 // Then throw an exception
211 throw new ChunkAlreadyAssembledException(array($this, $chunkSplits), self::EXCEPTION_CHUNK_ALREADY_ASSEMBLED);
215 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: serialNumber=' . $chunkSplits[self::CHUNK_SPLITS_INDEX_SERIAL] . ',hash=' . $chunkSplits[self::CHUNK_SPLITS_INDEX_HASH]);
217 // Add the chunk data (index 2) to the final array and use the serial number as index
218 $this->finalPackageChunks['content'][$chunkSplits[self::CHUNK_SPLITS_INDEX_SERIAL]] = $chunkSplits[self::CHUNK_SPLITS_INDEX_RAW_DATA];
220 // ... and the hash as well
221 $this->finalPackageChunks['hashes'][$chunkSplits[self::CHUNK_SPLITS_INDEX_SERIAL]] = $chunkSplits[self::CHUNK_SPLITS_INDEX_HASH];
225 * Marks the final array as completed, do only this if you really have all
226 * chunks together including EOP and "hash chunk".
230 private function markFinalArrayAsCompleted () {
232 * As for now, just set the array element. If any further steps are
233 * being added, this should always be the last step.
235 $this->finalPackageChunks['is_complete'] = TRUE;
239 * Sorts the chunks array by using the serial number as a sorting key. In
240 * most situations a call of ksort() is enough to accomblish this. So this
241 * method may only call ksort() on the chunks array.
243 * This method sorts 'content' and 'hashes' so both must have used the
244 * serial numbers as array indexes.
248 private function sortChunksArray () {
249 // Sort 'content' first
250 ksort($this->finalPackageChunks['content']);
253 ksort($this->finalPackageChunks['hashes']);
257 * Prepares the package assemble by removing last chunks (last shall be
258 * hash chunk, pre-last shall be EOP chunk) and verify that all serial
259 * numbers are valid (same as PackageFragmenter class would generate).
263 private function preparePackageAssmble () {
264 // Make sure both arrays have same count (this however should always be TRUE)
265 assert(count($this->finalPackageChunks['hashes']) == count($this->finalPackageChunks['content']));
266 //* DIE: */ exit(__METHOD__ . ':finalPackageChunks='.print_r($this->finalPackageChunks['content'], TRUE));
269 * Remove last element (hash chunk) from 'hashes'. This hash will never
270 * be needed, so ignore it.
272 array_pop($this->finalPackageChunks['hashes']);
274 // ... and from 'content' as well but save it for later use
275 $this->chunkHashes = explode(PackageFragmenter::CHUNK_HASH_SEPARATOR, substr(array_pop($this->finalPackageChunks['content']), strlen(PackageFragmenter::HASH_CHUNK_IDENTIFIER)));
277 // Remove EOP chunk and keep a copy of it
278 array_pop($this->finalPackageChunks['hashes']);
279 $this->eopChunk = explode(PackageFragmenter::CHUNK_HASH_SEPARATOR, substr(array_pop($this->finalPackageChunks['content']), strlen(PackageFragmenter::END_OF_PACKAGE_IDENTIFIER)));
281 // Verify all serial numbers
282 $this->verifyChunkSerialNumbers();
286 * Verifies all chunk serial numbers by using a freshly initialized
287 * fragmenter instance. Do ALWAYS sort the array and array_pop() the hash
288 * chunk before calling this method to avoid re-requests of many chunks.
292 private function verifyChunkSerialNumbers () {
294 //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: finalPackageChunks=' . print_r($this->finalPackageChunks, TRUE));
297 $finalHash = $this->generateFinalHash(implode('', $this->finalPackageChunks['content']));
299 // Reset the serial number generator
300 $this->fragmenterInstance->resetSerialNumber($finalHash);
302 // "Walk" through all (content) chunks
303 foreach ($this->finalPackageChunks['content'] as $serialNumber => $content) {
304 // Get next serial number
305 $nextSerial = $this->fragmenterInstance->getNextHexSerialNumber($finalHash);
308 //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: serialNumber=' . $serialNumber . ',nextSerial=' . $nextSerial);
310 // Is it not the same? Then re-request it
311 if ($serialNumber != $nextSerial) {
312 // This is invalid, so remove it
313 unset($this->finalPackageChunks['content'][$serialNumber]);
314 unset($this->finalPackageChunks['hashes'][$serialNumber]);
316 // And re-request it with valid serial number (and hash chunk)
317 $this->rerequestChunkBySerialNumber($nextSerial);
323 * Assembles and verifies ("final check") chunks back together to the
324 * original package (raw data for the start). This method should only be
325 * called AFTER the EOP and final-chunk chunk have been removed.
329 private function assembleAllChunksToPackage () {
330 // If chunkHashes is not filled, don't continue
331 assert(count($this->chunkHashes) > 0);
333 // Init raw package data string
334 $this->rawPackageData = '';
336 // That went well, so start assembling all chunks
337 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Handling ' . count($this->finalPackageChunks['content']) . ' entries ...');
338 foreach ($this->finalPackageChunks['content'] as $serialNumber => $content) {
339 // Assert on 'hash' entry (must always be set)
340 assert(isset($this->finalPackageChunks['hashes'][$serialNumber]));
343 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: serialNumber=' . $serialNumber . ',hashes=' . $this->finalPackageChunks['hashes'][$serialNumber] . ' - validating ...');
344 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('finalPackageChunks=' . print_r($this->finalPackageChunks, TRUE) . 'chunkHashes=' . print_r($this->chunkHashes, TRUE));
346 // Is this chunk valid? This should be the case
347 assert($this->isChunkHashValid(array(
348 self::CHUNK_SPLITS_INDEX_HASH => $this->finalPackageChunks['hashes'][$serialNumber],
349 self::CHUNK_SPLITS_INDEX_RAW_DATA => $content
352 // ... and is also in the hash chunk?
353 assert(in_array($this->finalPackageChunks['hashes'][$serialNumber], $this->chunkHashes));
355 // Verification okay, add it to the raw data
356 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Adding ' . strlen($content) . ' bytes as raw package data ...');
357 $this->rawPackageData .= $content;
361 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: eopChunk[1]=' . $this->eopChunk[1] . ',index=' . (count($this->chunkHashes) - 2) . ',chunkHashes='.print_r($this->chunkHashes, TRUE));
363 // The last chunk hash must match with the one from eopChunk[1]
364 assert($this->eopChunk[1] == $this->chunkHashes[count($this->chunkHashes) - 2]);
368 * Generate final hash if EOP chunk is found, else an assert will happen.
370 * @param $rawPackageData Raw package data
371 * @return $finalHash Final hash if EOP chunk is found
373 private function generateFinalHash ($rawPackageData) {
374 // Make sure the raw package data is given
375 assert((is_string($rawPackageData)) && (!empty($rawPackageData)));
377 // Make sure the EOP chunk is set
378 assert((isset($this->eopChunk[0])) && (isset($this->eopChunk[1])));
379 assert((is_string($this->eopChunk[0])) && (!empty($this->eopChunk[0])));
382 $finalHash = $this->getCryptoInstance()->hashString($rawPackageData, $this->eopChunk[0], FALSE);
389 * Verifies the finally assembled raw package data by comparing it against
394 private function verifyRawPackageData () {
395 // Generate final hash
396 $finalHash = $this->generateFinalHash($this->rawPackageData);
399 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: eopChunk[1]=' . $this->eopChunk[1] . ',finalHash=' . $finalHash);
400 assert($finalHash == $this->eopChunk[0]);
404 * Checks whether the final (last) chunk is valid
406 * @param $chunks An array with chunks and (hopefully) a valid final chunk
407 * @return $isValid Whether the final (last) chunk is valid
409 private function isValidFinalChunk (array $chunks) {
410 // Default is all fine
413 // Split the (possible) EOP chunk
414 $chunkSplits = explode(PackageFragmenter::CHUNK_DATA_HASH_SEPARATOR, $chunks[count($chunks) - 1]);
416 // Make sure chunks with only 3 elements are parsed (for details see ChunkHandler)
417 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('eopChunk=' . $chunks[count($chunks) - 1] . ',chunkSplits=' . print_r($chunkSplits, TRUE));
418 assert(count($chunkSplits) == 3);
420 // Validate final chunk
421 if (substr($chunkSplits[ChunkHandler::CHUNK_SPLITS_INDEX_RAW_DATA], 0, strlen(PackageFragmenter::END_OF_PACKAGE_IDENTIFIER)) != PackageFragmenter::END_OF_PACKAGE_IDENTIFIER) {
424 } elseif (substr_count($chunkSplits[ChunkHandler::CHUNK_SPLITS_INDEX_RAW_DATA], PackageFragmenter::CHUNK_HASH_SEPARATOR) != 1) {
425 // CHUNK_HASH_SEPARATOR shall only be found once
434 * Adds all chunks if the last one verifies as a 'final chunk'.
436 * @param $chunks An array with chunks, the last one should be a 'final'
438 * @throws FinalChunkVerificationException If the final chunk does not start with 'EOP:'
440 public function addAllChunksWithFinal (array $chunks) {
441 // Try to validate the final chunk
443 // Validate final chunk
444 $this->isValidFinalChunk($chunks);
445 } catch (AssertionException $e) {
446 // Last chunk is not valid
447 throw new FinalChunkVerificationException(array($this, $chunks, $e), BaseListener::EXCEPTION_FINAL_CHUNK_VERIFICATION);
450 // Do we have some pending chunks (no final)?
451 while (!$this->getStackInstance()->isStackEmpty(self::STACKER_NAME_CHUNKS_WITHOUT_FINAL)) {
452 // Then get it first and add it before the EOP chunks
453 array_unshift($chunks, $this->getStackInstance()->popNamed(self::STACKER_NAME_CHUNKS_WITHOUT_FINAL));
456 // Add all chunks to the FIFO stacker
457 foreach ($chunks as $chunk) {
459 $this->getStackInstance()->pushNamed(self::STACKER_NAME_CHUNKS_WITH_FINAL_EOP, $chunk);
464 * Adds all chunks and wait for more (e.g. incomplete transmission)
466 * @param $chunks An array with chunks, the last one should be a 'final'
469 public function addAllChunksWait (array $chunks) {
470 // Add all chunks to the FIFO stacker
471 foreach ($chunks as $chunk) {
473 $this->getStackInstance()->pushNamed(self::STACKER_NAME_CHUNKS_WITHOUT_FINAL, $chunk);
478 * Checks whether unhandled chunks are available
480 * @return $unhandledChunks Whether unhandled chunks are left
482 public function ifUnhandledChunksWithFinalAvailable () {
483 // Simply check if the stacker is not empty
484 $unhandledChunks = $this->getStackInstance()->isStackEmpty(self::STACKER_NAME_CHUNKS_WITH_FINAL_EOP) === FALSE;
487 return $unhandledChunks;
491 * Handles available chunks by processing one-by-one (not all together,
492 * this would slow-down the whole application) with the help of an
497 public function handleAvailableChunksWithFinal () {
498 // First check if there are undhandled chunks available
499 assert($this->ifUnhandledChunksWithFinalAvailable());
501 // Get an entry from the stacker
502 $chunk = $this->getStackInstance()->popNamed(self::STACKER_NAME_CHUNKS_WITH_FINAL_EOP);
504 // Split the string with proper separator character
505 $chunkSplits = explode(PackageFragmenter::CHUNK_DATA_HASH_SEPARATOR, $chunk);
508 * Make sure three elements are always found:
513 assert(count($chunkSplits) == 3);
515 // Is the generated hash from data same ("valid") as given hash?
516 if (!$this->isChunkHashValid($chunkSplits)) {
518 self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Chunk content is not validating against given hash.');
520 // Re-request this chunk (trust the hash in index # 0)
521 $this->rerequestChunkBySplitsArray($chunkSplits);
523 // Don't process this chunk
527 // Is the serial number valid (chars 0-9, length equals PackageFragmenter::MAX_SERIAL_LENGTH)?
528 if (!$this->isSerialNumberValid($chunkSplits[self::CHUNK_SPLITS_INDEX_SERIAL])) {
530 self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Chunk serial number ' . $chunkSplits[self::CHUNK_SPLITS_INDEX_SERIAL] . ' for hash ' . $chunkSplits[self::CHUNK_SPLITS_INDEX_HASH] . ' is invalid.');
532 // Re-request this chunk
533 $this->rerequestChunkBySplitsArray($chunkSplits);
535 // Don't process this chunk
540 * It is now known that (as long as the hash algorithm has no
541 * collisions) the content is the same as the sender sends it to this
544 * And also the serial number is valid (basicly) at this point. Now the
545 * chunk can be added to the final array.
547 $this->addChunkToFinalArray($chunkSplits);
549 // Is the stack now empty?
550 if ($this->getStackInstance()->isStackEmpty(self::STACKER_NAME_CHUNKS_WITH_FINAL_EOP)) {
551 // Then mark the final array as complete
552 $this->markFinalArrayAsCompleted();
557 * Checks whether unassembled chunks are available (ready) in final array
559 * @return $unassembledChunksAvailable Whether unassembled chunks are available
561 public function ifUnassembledChunksAvailable () {
562 // For now do only check the array element 'is_complete'
563 $unassembledChunksAvailable = ($this->finalPackageChunks['is_complete'] === TRUE);
566 return $unassembledChunksAvailable;
570 * Assembles all chunks (except EOP and "hash chunk") back together to the original package data.
572 * This is done by the following steps:
574 * 1) Sort the final array with ksort(). This will bring the "hash
575 * chunk" up to the last array index and the EOP chunk to the
576 * pre-last array index
577 * 2) Assemble all chunks except two last (see above step)
578 * 3) While so, do the final check on all hashes
579 * 4) If the package is assembled back together, hash it again for
580 * the very final verification.
584 public function assembleChunksFromFinalArray () {
585 // Make sure the final array is really completed
586 assert($this->ifUnassembledChunksAvailable());
589 $this->finalPackageChunks['assemble_steps']++;
592 switch ($this->finalPackageChunks['assemble_steps']) {
593 case 1: // Sort the chunks array (the serial number shall act as a sorting key)
594 $this->sortChunksArray();
597 case 2: // Prepare the assemble by removing last two indexes
598 $this->preparePackageAssmble();
601 case 3: // Assemble all chunks back together to the original package
602 $this->assembleAllChunksToPackage();
605 case 4: // Verify the raw data by hashing it again
606 $this->verifyRawPackageData();
609 case 5: // Re-initialize handler to reset it to the old state
610 $this->initHandler();
613 default: // Invalid step found
614 self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Invalid step ' . $this->finalPackageChunks['assemble_steps'] . ' detected.');
620 * Checks whether the raw package data has been assembled back together.
621 * This can be safely assumed when rawPackageData is not empty and the
622 * collection of all chunks is FALSE (because initHandler() will reset it).
624 * @return $isRawPackageDataAvailable Whether raw package data is available
626 public function ifRawPackageDataIsAvailable () {
628 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: this->rawPackageData()=' . strlen($this->rawPackageData) . ',ifUnassembledChunksAvailable()=' . intval($this->ifUnassembledChunksAvailable()));
629 $isRawPackageDataAvailable = ((!empty($this->rawPackageData)) && (!$this->ifUnassembledChunksAvailable()));
632 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: isRawPackageDataAvailable=' . intval($isRawPackageDataAvailable));
633 return $isRawPackageDataAvailable;
637 * Handles the finally assembled raw package data by feeding it into another
638 * stacker for further decoding/processing.
642 public function handledAssembledRawPackageData () {
643 // Assert to make sure that there is raw package data available
644 assert($this->ifRawPackageDataIsAvailable());
646 // Then feed it into the next stacker
647 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Pushing ' . strlen($this->rawPackageData) . ' bytes to stack ' . self::STACKER_NAME_ASSEMBLED_RAW_DATA . ' ...');
648 $this->getStackInstance()->pushNamed(self::STACKER_NAME_ASSEMBLED_RAW_DATA, $this->rawPackageData);
651 $this->rawPackageData = '';