]> git.mxchange.org Git - hub.git/commitdiff
Continued refacturing:
authorRoland Haeder <roland@mxchange.org>
Fri, 16 Jan 2015 23:22:56 +0000 (00:22 +0100)
committerRoland Haeder <roland@mxchange.org>
Fri, 16 Jan 2015 23:23:45 +0000 (00:23 +0100)
- Added new exception InvalidUnlException
- Added very basic validation of UNLs (only regex)
- Updated 'core'
- TODOs.txt updated

Signed-off-by: Roland Haeder <roland@mxchange.org>
12 files changed:
application/hub/exceptions/unl/.htaccess [new file with mode: 0644]
application/hub/exceptions/unl/class_InvalidUnlException.php [new file with mode: 0644]
application/hub/exceptions/wrapper/class_NodeAlreadyRegisteredException.php
application/hub/exceptions/wrapper/class_NodeDataMissingException.php
application/hub/main/class_BaseHubSystem.php
application/hub/main/discovery/recipient/socket/class_PackageSocketDiscovery.php
application/hub/main/handler/protocol/class_BaseProtocolHandler.php
application/hub/main/handler/protocol/ipv4/class_BaseIpV4ProtocolHandler.php
application/hub/main/listener/tcp/class_TcpListener.php
application/hub/main/package/fragmenter/class_PackageFragmenter.php
core
docs/TODOs.txt

diff --git a/application/hub/exceptions/unl/.htaccess b/application/hub/exceptions/unl/.htaccess
new file mode 100644 (file)
index 0000000..3a42882
--- /dev/null
@@ -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 (file)
index 0000000..9d71d3a
--- /dev/null
@@ -0,0 +1,47 @@
+<?php
+/**
+ * This exception is thrown when a protocol handler cannot validate given package data's recipient (UNL).
+ *
+ * @author             Roland Haeder <webmaster@shipsimu.org>
+ * @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 <http://www.gnu.org/licenses/>.
+ */
+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]
+?>
index 318782cb70daa6d68b18a6dbe56ceca01bd1f99a..164ce4820d6b59295c51d5ee955e7f231d08bb3a 100644 (file)
@@ -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
index c2de8a4be7876b5605a6bde70881b1bcffb91d98..afe89e8e04862409f7933f6ba5a7deedb1d65d86 100644 (file)
@@ -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
index d6399194594514328ea6aa2332296f3dc8b96c34..0426bd28a33b75e6b129d93db9dda0595dce27eb 100644 (file)
@@ -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';
index b36aee53aedfb3a88942d11a19d094caea0592d8..7d2d8bfac9c6b44f2e8e95e46c82aa8d40c24504 100644 (file)
@@ -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());
index c2421c1dd0fee41014a0da7250a97329e4cb10ab..902a4cdcd5f74464ca8c3f3ca81dec985d3ed883 100644 (file)
@@ -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]
index 601dc7ce8935b341bc0e2408265619876b1ba0f3..583616c65bfdfa7a200f8cad8e73562159c94e58 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 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]
index 560801e1160b8750ed6b13e350a974224e3ab426..ed4403515feca878427a48b6451782aa98c5fde9 100644 (file)
@@ -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
                        );
 
index 7e2943f50e7f2bca8de998bdc8c3a24d151e8d34..8c341ca6ec873a37cbbf86284dc22a71d778238d 100644 (file)
@@ -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 08164c824a16cc37eb29ef890ded66f0fbba4354..a546101674aeccd5fb335ca02e3295bffe21691a 160000 (submodule)
--- a/core
+++ b/core
@@ -1 +1 @@
-Subproject commit 08164c824a16cc37eb29ef890ded66f0fbba4354
+Subproject commit a546101674aeccd5fb335ca02e3295bffe21691a
index 54a3de7530452e73d333f9c843414cb0b5a8c3c4..746e1f2b9289038e3e45ac4d00f61f5f4c0a905f 100644 (file)
@@ -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?
 ./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