From: Roland Haeder Date: Fri, 16 Jan 2015 23:22:56 +0000 (+0100) Subject: Continued refacturing: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=7bd98e1f14ec24a4ad6b9c65f86729f39334cbd7;p=hub.git Continued refacturing: - Added new exception InvalidUnlException - Added very basic validation of UNLs (only regex) - Updated 'core' - TODOs.txt updated Signed-off-by: Roland Haeder --- diff --git a/application/hub/exceptions/unl/.htaccess b/application/hub/exceptions/unl/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/exceptions/unl/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/exceptions/unl/class_InvalidUnlException.php b/application/hub/exceptions/unl/class_InvalidUnlException.php new file mode 100644 index 000000000..9d71d3a8e --- /dev/null +++ b/application/hub/exceptions/unl/class_InvalidUnlException.php @@ -0,0 +1,47 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2014 Hub Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.shipsimu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class InvalidUnlException extends FrameworkException { + /** + * The super constructor for all exceptions + * + * @param $messageArray Error message array + * @param $code Error code + * @return void + */ + public function __construct (array $messageArray, $code) { + // Construct the message + $message = sprintf('[%s:%d] The protocol handler %s cannot validate recipient %s.', + $messageArray[0]->__toString(), + $this->getLine(), + $messageArray[1]->__toString(), + $messageArray[2][NetworkPackage::PACKAGE_DATA_RECIPIENT] + ); + + // Call parent exception constructor + parent::__construct($message, $code); + } +} + +// [EOF] +?> diff --git a/application/hub/exceptions/wrapper/class_NodeAlreadyRegisteredException.php b/application/hub/exceptions/wrapper/class_NodeAlreadyRegisteredException.php index 318782cb7..164ce4820 100644 --- a/application/hub/exceptions/wrapper/class_NodeAlreadyRegisteredException.php +++ b/application/hub/exceptions/wrapper/class_NodeAlreadyRegisteredException.php @@ -34,8 +34,8 @@ class NodeAlreadyRegisteredException extends FrameworkException { $message = sprintf('[%s:%d] Node already registered: session-id=%s, external-address=%s', $messageArray[0]->__toString(), $this->getLine(), - $msssageArray[1][NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_SESSION_ID], - $msssageArray[1][NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_EXTERNAL_ADDRESS] + $messageArray[1][NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_SESSION_ID], + $messageArray[1][NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_EXTERNAL_ADDRESS] ); // Call parent exception constructor diff --git a/application/hub/exceptions/wrapper/class_NodeDataMissingException.php b/application/hub/exceptions/wrapper/class_NodeDataMissingException.php index c2de8a4be..afe89e8e0 100644 --- a/application/hub/exceptions/wrapper/class_NodeDataMissingException.php +++ b/application/hub/exceptions/wrapper/class_NodeDataMissingException.php @@ -34,8 +34,8 @@ class NodeDataMissingException extends FrameworkException { $message = sprintf('[%s:%d] Node not registered/missing: session-id=%s, external-address=%s', $messageArray[0]->__toString(), $this->getLine(), - $msssageArray[1][NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_SESSION_ID], - $msssageArray[1][NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_EXTERNAL_ADDRESS] + $messageArray[1][NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_SESSION_ID], + $messageArray[1][NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_EXTERNAL_ADDRESS] ); // Call parent exception constructor diff --git a/application/hub/main/class_BaseHubSystem.php b/application/hub/main/class_BaseHubSystem.php index d63991945..0426bd28a 100644 --- a/application/hub/main/class_BaseHubSystem.php +++ b/application/hub/main/class_BaseHubSystem.php @@ -34,6 +34,7 @@ class BaseHubSystem extends BaseFrameworkSystem { const EXCEPTION_DHT_BOOTSTRAP_NOT_ACCEPTED = 0x908; const EXCEPTION_MULTIPLE_MESSAGE_SENT = 0x909; const EXCEPTION_DHT_BOOTSTRAP_NOT_ATTEMPTED = 0x90a; + const EXCEPTION_INVALID_UNL = 0x90b; // Message status codes const MESSAGE_STATUS_CODE_OKAY = 'OKAY'; diff --git a/application/hub/main/discovery/recipient/socket/class_PackageSocketDiscovery.php b/application/hub/main/discovery/recipient/socket/class_PackageSocketDiscovery.php index b36aee53a..7d2d8bfac 100644 --- a/application/hub/main/discovery/recipient/socket/class_PackageSocketDiscovery.php +++ b/application/hub/main/discovery/recipient/socket/class_PackageSocketDiscovery.php @@ -101,21 +101,30 @@ class PackageSocketDiscovery extends BaseRecipientDiscovery implements Discovera * @return $socketResource A valid socket resource or FALSE if an error occured * @throws NoListGroupException If the procol group is not found in peer list * @throws NullPointerException If listenerInstance is NULL + * @throws InvalidUnlException If the provided UNL cannot be validated by the protocol handler */ public function discoverSocket (array $packageData, $connectionType) { /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: connectionType=' . $connectionType . ' - CALLED!'); - // Assert on type + // Assert on type and recipient assert($connectionType != BaseConnectionHelper::CONNECTION_TYPE_SERVER); + assert(isset($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT])); - // Determine protocol name - $protocolName = $this->determineProtocolByPackageData($packageData); + // Determine protocol instance + $protocolInstance = $this->determineProtocolByPackageData($packageData); - // Is it the same? - assert(strtoupper($protocolName) == $packageData[NetworkPackage::PACKAGE_DATA_PROTOCOL]); + // Is it valid? + assert($protocolInstance instanceof HandleableProtocol); + + // Does the UNL validate? + if (!$protocolInstance->isValidUniversalNodeLocatorByPackageData($packageData)) { + // Not valid, throw exception + throw new InvalidUnlException(array($this, $protocolInstance, $packageData), BaseHubSystem::EXCEPTION_INVALID_UNL); + } // END - if + die(__METHOD__ . ':protocolInstance=' . $protocolInstance . ',packageData=' . print_r($packageData, TRUE)); // Get the listener instance - $listenerInstance = $this->discoverListenerInstance($protocolName, $packageData); + $listenerInstance = $this->discoverListenerInstance($protocolInstance, $packageData); // If there is no listener who wants to have that package, we simply drop it here if (is_null($listenerInstance)) { @@ -124,7 +133,7 @@ class PackageSocketDiscovery extends BaseRecipientDiscovery implements Discovera } // END - if // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('protocolName=' . $protocolName . ',packageData=' . print_r($packageData, TRUE)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('protocolInstance=' . $protocolInstance . ',packageData=' . print_r($packageData, TRUE)); /* * Now we have the listener instance, we can determine the right @@ -146,10 +155,10 @@ class PackageSocketDiscovery extends BaseRecipientDiscovery implements Discovera // Try to create a new socket resource try { // Possibly noisy debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-DISCOVERY[' . __METHOD__ . ':' . __LINE__ . ']: Trying to establish a ' . strtoupper($protocolName) . ' connection to ' . $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . ' ...'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-DISCOVERY[' . __METHOD__ . ':' . __LINE__ . ']: Trying to establish a ' . strtoupper($protocolInstance) . ' connection to ' . $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . ' ...'); // Get a socket resource from our factory (if succeeded) - $socketResource = SocketFactory::createSocketFromPackageData($packageData, $protocolName); + $socketResource = SocketFactory::createSocketFromPackageData($packageData, $protocolInstance); } catch (SocketConnectionException $e) { // The connection fails of being established, so log it away self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-DISCOVERY[' . __METHOD__ . ':' . __LINE__ . ']: Caught ' . $e->__toString() . ',message=' . $e->getMessage()); diff --git a/application/hub/main/handler/protocol/class_BaseProtocolHandler.php b/application/hub/main/handler/protocol/class_BaseProtocolHandler.php index c2421c1dd..902a4cdcd 100644 --- a/application/hub/main/handler/protocol/class_BaseProtocolHandler.php +++ b/application/hub/main/handler/protocol/class_BaseProtocolHandler.php @@ -32,6 +32,27 @@ class BaseProtocolHandler extends BaseHandler { // Call parent constructor parent::__construct($className); } + + /** + * Validates given UNL very basicly by given regular expression. You + * normally don't need/want to overwrite this method as this is a very basic + * validation only based on a regex. + * + * @param $unl Universal Node Locator to validate + * @param $regex Regular expression to use for validation without slashes + * @return $isValid Whether the UNL is valid + */ + protected final function isValidUniversalNodeLocator ($unl, $regex) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: unl=' . $unl . ',regex=' . $regex . ' - CALLED!'); + + // Very basic regex check + $isValid = (preg_match('/^' . $regex . '$/', $unl) === 1); + + // Return result + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: isValid=' . intval($isValid) . ' - EXIT!'); + return $isValid; + } } // [EOF] diff --git a/application/hub/main/handler/protocol/ipv4/class_BaseIpV4ProtocolHandler.php b/application/hub/main/handler/protocol/ipv4/class_BaseIpV4ProtocolHandler.php index 601dc7ce8..583616c65 100644 --- a/application/hub/main/handler/protocol/ipv4/class_BaseIpV4ProtocolHandler.php +++ b/application/hub/main/handler/protocol/ipv4/class_BaseIpV4ProtocolHandler.php @@ -22,6 +22,14 @@ * along with this program. If not, see . */ class BaseIpV4ProtocolHandler extends BaseProtocolHandler { + /** + * Port number + */ + private $port = 0; + + // Regular expression for validating IP:port UNLs + const UNL_REGEX = '([a-z]{1,}):\/\/\b(([01]?\d?\d|2[0-4]\d|25[0-5])\.){3}([01]?\d?\d|2[0-4]\d|25[0-5])\b:(6553[0-5]|655[0-2][0-9]\d|65[0-4](\d){2}|6[0-4](\d){3}|[1-5](\d){4}|[1-9](\d){0,3})'; + /** * Protected constructor * @@ -32,6 +40,67 @@ class BaseIpV4ProtocolHandler extends BaseProtocolHandler { // Call parent constructor parent::__construct($className); } + + /** + * Setter for port number to satify HandleableProtocol + * + * @para $port The port number + * @return void + */ + protected final function setPort ($port) { + // Set new port number + $this->port = $port; + } + + /** + * Getter for port number to satify HandleableProtocol + * + * @return $port The port number + */ + public final function getPort () { + // Return port number + return $this->port; + } + + /** + * Validates given 'recipient' if it is a valid UNL. This means that the UNL + * can be parsed by the protocol handler. + * + * @param $packageData Valid raw package data + * @return $isValid Whether the UNL can be validated + */ + public function isValidUniversalNodeLocatorByPackageData (array $packageData) { + // Debug message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: CALLED!'); + + // Is 'recipient' there? + assert(isset($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT])); + + // Is the correct handler choosen? + assert(substr($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT], 0, strlen($this->getHandlerName())) != $this->getHandlerName()); + + // Default is from generic validation + $isValid = $this->isValidUniversalNodeLocator($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT], self::UNL_REGEX); + + // Debug message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: PACKAGE_DATA_RECIPIENT=' . $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . ',isValid[' . gettype($isValid) . ']=' . intval($isValid)); + + // If this doesn't fail, continue validating the IP:port combination + if ($isValid === TRUE) { + // Okay, the basic test is passed, so reset the variable + $isValid = FALSE; + + // ... and validate IP:port, first "parse" the UNL + $unlParts = $this->parseUnl($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]); + + // Debug die + die(__METHOD__ . ':PACKAGE_DATA_RECIPIENT=' . $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . ',unlParts=' . print_r($unlParts, TRUE) . PHP_EOL); + } // END - if + + // Return result + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: isValid=' . intval($isValid) . ' - EXIT!'); + return $isValid; + } } // [EOF] diff --git a/application/hub/main/listener/tcp/class_TcpListener.php b/application/hub/main/listener/tcp/class_TcpListener.php index 560801e11..ed4403515 100644 --- a/application/hub/main/listener/tcp/class_TcpListener.php +++ b/application/hub/main/listener/tcp/class_TcpListener.php @@ -286,7 +286,6 @@ class TcpListener extends BaseListener implements Listenable { $packageData = array( NetworkPackage::PACKAGE_DATA_SENDER => $peerName . ':0', NetworkPackage::PACKAGE_DATA_RECIPIENT => $this->getSessionId(), - NetworkPackage::PACKAGE_DATA_PROTOCOL => $this->getProtocolName(), NetworkPackage::PACKAGE_DATA_STATUS => NetworkPackage::PACKAGE_STATUS_FAKED ); diff --git a/application/hub/main/package/fragmenter/class_PackageFragmenter.php b/application/hub/main/package/fragmenter/class_PackageFragmenter.php index 7e2943f50..8c341ca6e 100644 --- a/application/hub/main/package/fragmenter/class_PackageFragmenter.php +++ b/application/hub/main/package/fragmenter/class_PackageFragmenter.php @@ -443,9 +443,8 @@ class PackageFragmenter extends BaseHubSystem implements Fragmentable, Registera public function fragmentPackageArray (array $packageData, ConnectionHelper $helperInstance) { // Is this package already fragmented? if (!$this->isPackageProcessed($packageData)) { - // Remove package status and protocol, the recipient doesn't need this + // Remove package status, the recipient doesn't need this unset($packageData[NetworkPackage::PACKAGE_DATA_STATUS]); - unset($packageData[NetworkPackage::PACKAGE_DATA_PROTOCOL]); // First we need to "implode" the array $rawData = implode(NetworkPackage::PACKAGE_DATA_SEPARATOR, $packageData); diff --git a/core b/core index 08164c824..a54610167 160000 --- a/core +++ b/core @@ -1 +1 @@ -Subproject commit 08164c824a16cc37eb29ef890ded66f0fbba4354 +Subproject commit a546101674aeccd5fb335ca02e3295bffe21691a diff --git a/docs/TODOs.txt b/docs/TODOs.txt index 54a3de753..746e1f2b9 100644 --- a/docs/TODOs.txt +++ b/docs/TODOs.txt @@ -15,7 +15,7 @@ ./application/hub/interfaces/wrapper/class_NodeDhtWrapper.php:122: * @todo Add minimum/maximum age limitations ./application/hub/interfaces/wrapper/class_NodeDhtWrapper.php:132: * @todo Add timestamp to dataset instance ./application/hub/main/chains/class_PackageFilterChain.php:54: * @todo This may be slow if a message with a lot tags arrived -./application/hub/main/class_BaseHubSystem.php:576: // @TODO On some systems it is 134, on some 107? +./application/hub/main/class_BaseHubSystem.php:577: // @TODO On some systems it is 134, on some 107? ./application/hub/main/commands/console/class_HubConsoleAptProxyCommand.php:107: * @todo Should we add some more filters? ./application/hub/main/commands/console/class_HubConsoleAptProxyCommand.php:58: * @todo Try to create a AptProxyActivationTask or so ./application/hub/main/commands/console/class_HubConsoleChatCommand.php:107: * @todo Should we add some more filters? @@ -168,7 +168,7 @@ ./application/hub/main/producer/miner/blocks/class_MinerTestGenesisBlockProducer.php:86: * @todo ~5% done ./application/hub/main/recipient/dht/class_DhtRecipient.php:76: // @TODO Unfinished ./application/hub/main/recipient/self/class_SelfRecipient.php:61: // @TODO Add more checks on data -./application/hub/main/registry/socket/class_SocketRegistry.php:72: // @TODO Tested again base class, rewrite it to a generic interface! +./application/hub/main/registry/socket/class_SocketRegistry.php:75: // @TODO Tested again base class, rewrite it to a generic interface! ./application/hub/main/resolver/protocol/tcp/class_TcpProtocolResolver.php:57: * @todo 0% done ./application/hub/main/resolver/state/peer/class_PeerStateResolver.php:59: * @todo ~30% done ./application/hub/main/scanner/crawler/uploaded_list/class_CrawlerUploadedListScanner.php:52: * @todo 0% done