From 7608225ee227cef8267be1fd5a2757a4e93cffc5 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Thu, 19 Feb 2015 11:26:47 +0100 Subject: [PATCH] Implementation of isOwnAddress() basicly finished: - ... which will check if the currently saved UNL is the same as own external or internal UNL (internal only returns IP!!!). - Introduced getCurrentUniversalNodeLocator() - Updated 'core' Signed-off-by: Roland Haeder --- .../class_PackageRecipientDiscovery.php | 6 +- .../cruncher/class_CruncherStateFactory.php | 2 +- .../protocol/class_BaseProtocolHandler.php | 72 ++++++++++++++++++- .../ipv4/class_BaseIpV4ProtocolHandler.php | 17 +++++ .../ipv4/tcp/class_TcpConnectionHelper.php | 7 +- .../main/pools/peer/class_DefaultPeerPool.php | 2 +- core | 2 +- 7 files changed, 100 insertions(+), 8 deletions(-) diff --git a/application/hub/main/discovery/recipient/package/class_PackageRecipientDiscovery.php b/application/hub/main/discovery/recipient/package/class_PackageRecipientDiscovery.php index 367d21c91..8c2035736 100644 --- a/application/hub/main/discovery/recipient/package/class_PackageRecipientDiscovery.php +++ b/application/hub/main/discovery/recipient/package/class_PackageRecipientDiscovery.php @@ -84,7 +84,7 @@ class PackageRecipientDiscovery extends BaseRecipientDiscovery implements Discov * @param $decodedData Raw raw package data array * @return void * @todo Add some validation of recipient field, e.g. an Universal Node Locator is found - * @todo The if() does only check for TCP, not UDP, e.g. try to get a $handlerInstance here + * @todo Enrich both messages with recipient data */ public function discoverRawRecipients (array $decodedData) { // This must be available @@ -106,10 +106,10 @@ class PackageRecipientDiscovery extends BaseRecipientDiscovery implements Discov */ // Debug output (may flood) - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('RECIPIENT-DISCOVERY[' . __METHOD__ . ':' . __LINE__ . ']: Recipient ' . $recipient[0] . ' matches own ip (' . HubTools::determineOwnExternalAddress() . ' or ' . $this->getConfigInstance()->getServerAddress() . ')'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('RECIPIENT-DISCOVERY[' . __METHOD__ . ':' . __LINE__ . ']: Recipient matches own ip (' . HubTools::determineOwnExternalAddress() . ' or ' . $this->getConfigInstance()->getServerAddress() . ')'); } else { // Debug output (may flood) - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('RECIPIENT-DISCOVERY[' . __METHOD__ . ':' . __LINE__ . ']: Recipient ' . $recipient[0] . ' is different than own external address (' . HubTools::determineOwnExternalAddress() . ') nor internal address (' . $this->getConfigInstance()->getServerAddress() . '), need to forward (not yet implemented)!'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('RECIPIENT-DISCOVERY[' . __METHOD__ . ':' . __LINE__ . ']: Recipient is different than own external address (' . HubTools::determineOwnExternalAddress() . ') nor internal address (' . $this->getConfigInstance()->getServerAddress() . '), need to forward (not yet implemented)!'); // This package is to be delivered to someone else, so add it //$this->getListInstance()->addEntry('unl', $decodedData[NetworkPackage::PACKAGE_DATA_RECIPIENT]); diff --git a/application/hub/main/factories/states/cruncher/class_CruncherStateFactory.php b/application/hub/main/factories/states/cruncher/class_CruncherStateFactory.php index ce1963bc5..dfd3be2ba 100644 --- a/application/hub/main/factories/states/cruncher/class_CruncherStateFactory.php +++ b/application/hub/main/factories/states/cruncher/class_CruncherStateFactory.php @@ -48,7 +48,7 @@ class CruncherStateFactory extends ObjectFactory { $stateInstance = self::createObjectByConfiguredName($className, array($cruncherInstance)); // Debug message - self::createDebugInstance(__CLASS__)->debugOutput('CRUNCHER-STATE-FACTORY[' . __METHOD__ . ':' . __LINE__ . ']: Cruncher state has changed from ' . $nodeInstance->getPrintableState() . ' to ' . $stateInstance->getStateName() . '.'); + self::createDebugInstance(__CLASS__)->debugOutput('CRUNCHER-STATE-FACTORY[' . __METHOD__ . ':' . __LINE__ . ']: Cruncher state has changed from ' . $cruncherInstance->getPrintableState() . ' to ' . $stateInstance->getStateName() . '.'); // Once we have that state, set it in the cruncher instance $cruncherInstance->setStateInstance($stateInstance); diff --git a/application/hub/main/handler/protocol/class_BaseProtocolHandler.php b/application/hub/main/handler/protocol/class_BaseProtocolHandler.php index 242ca0ae5..3caf1ef79 100644 --- a/application/hub/main/handler/protocol/class_BaseProtocolHandler.php +++ b/application/hub/main/handler/protocol/class_BaseProtocolHandler.php @@ -54,7 +54,7 @@ class BaseProtocolHandler extends BaseHandler { * * @return $unlData The UNL data array */ - public final function getUniversalNodeLocatorData () { + public final function getUniversalNodeLocatorDataArray () { // Return UNL data array return $this->universalNodeLocatorData; } @@ -127,6 +127,76 @@ class BaseProtocolHandler extends BaseHandler { // Return the generic array return $unlData; } + + /** + * Gets an element from universalNodeLocatorData array + * + * @param $element Element in universalNodeLocatorData array + * @return $value Found value + */ + protected final function getUniversalNodeLocatorDataElement ($element) { + // Is the element there? + assert(isset($this->universalNodeLocatorData[$element])); + + // Return it + return $this->universalNodeLocatorData[$element]; + } + + /** + * "Getter" for currently saved UNL + * + * @return $unl Currently saved Universal Node Locator + */ + public final function getCurrentUniversalNodeLocator () { + // Construct generic UNL + $unl = sprintf('%s://%s', + $this->getUniversalNodeLocatorDataElement(UniversalNodeLocator::UNL_PART_PROTOCOL), + $this->getAddressPart() + ); + + // Return it + return $unl; + } + + /** + * Default implementation for returning address part, may not be suitable + * for IPv4/IPv6 protocol handlers. So you have to overwrite (NOT CHANGE!) this method. + * + * @return $address Address part for the final UNL + */ + public function getAddressPart () { + // Return it + return $this->getUniversalNodeLocatorDataElement(UniversalNodeLocator::UNL_PART_ADDRESS); + } + + /** + * If the found UNL (address) matches own external or internal address + * + * @return $ifMatches Whether the found UNL matches own addresss + */ + public function isOwnAddress () { + // Get current UNL (from universalNodeLocatorData array) + $currentUnl = $this->getCurrentUniversalNodeLocator(); + + // Get own external UNL + $externalUnl = HubTools::determineOwnExternalAddress(); + + // Get internal UNL + $internalUnl = HubTools::determineOwnInternalAddress(); + + // Debug message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: currentUnl=' . $currentUnl . ',externalUnl=' . $externalUnl . ',internalUnl=' . $internalUnl); + //* DIE-DEBUG: */ die(__METHOD__.':currentUnl=' . $currentUnl . ',this='.print_r($this, TRUE)); + + // Is it the same? + $ifMatches = (($currentUnl === $externalUnl) || ($currentUnl === $internalUnl)); + + // Debug message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: ifMatches=' . intval($ifMatches)); + + // Return result + return $ifMatches; + } } // [EOF] diff --git a/application/hub/main/handler/protocol/ipv4/class_BaseIpV4ProtocolHandler.php b/application/hub/main/handler/protocol/ipv4/class_BaseIpV4ProtocolHandler.php index 95ae3bd7f..88d6e8106 100644 --- a/application/hub/main/handler/protocol/ipv4/class_BaseIpV4ProtocolHandler.php +++ b/application/hub/main/handler/protocol/ipv4/class_BaseIpV4ProtocolHandler.php @@ -153,6 +153,23 @@ class BaseIpV4ProtocolHandler extends BaseProtocolHandler { //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: isValid=' . intval($isValid) . ' - EXIT!'); return $isValid; } + + /** + * This implementation uses it's parent method and combines it with the + * port part to construct a valid address:port combination. + * + * @return $address Address part for the final UNL + */ + public function getAddressPart () { + // Construct address + $address = sprintf('%s:%s', + parent::getAddressPart(), + $this->getUniversalNodeLocatorDataElement(UniversalNodeLocator::UNL_PART_PORT) + ); + + // Return it + return $address; + } } // [EOF] diff --git a/application/hub/main/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php b/application/hub/main/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php index 0c749946f..85f7c72ae 100644 --- a/application/hub/main/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php +++ b/application/hub/main/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php @@ -96,7 +96,12 @@ class TcpConnectionHelper extends BaseIpV4ConnectionHelper implements Connection $handlerInstance = ProtocolHandlerFactory::createProtocolHandlerFromPackageData($packageData); // Get UNL data - $unlData = $handlerInstance->getUniversalNodeLocatorData(); + $unlData = $handlerInstance->getUniversalNodeLocatorDataArray(); + + // Make sure it is a valid Universal Node Locator array (3 elements) + assert(isset($unlData[UniversalNodeLocator::UNL_PART_PROTOCOL])); + assert(isset($unlData[UniversalNodeLocator::UNL_PART_ADDRESS])); + assert(isset($unlData[UniversalNodeLocator::UNL_PART_PORT])); // Set handler instance $helperInstance->setHandlerInstance($handlerInstance); diff --git a/application/hub/main/pools/peer/class_DefaultPeerPool.php b/application/hub/main/pools/peer/class_DefaultPeerPool.php index 1a87fc4a2..750a3474c 100644 --- a/application/hub/main/pools/peer/class_DefaultPeerPool.php +++ b/application/hub/main/pools/peer/class_DefaultPeerPool.php @@ -224,7 +224,7 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { $handlerInstance = ProtocolHandlerFactory::createProtocolHandlerFromPackageData($packageData); // Get UNL data - $unlData = $handlerInstance->getUniversalNodeLocatorData(); + $unlData = $handlerInstance->getUniversalNodeLocatorDataArray(); // Make sure it is a valid Universal Node Locator array (3 elements) assert(isset($unlData[UniversalNodeLocator::UNL_PART_PROTOCOL])); diff --git a/core b/core index 12417a738..f4b9cbebe 160000 --- a/core +++ b/core @@ -1 +1 @@ -Subproject commit 12417a7382d7ab76dacb985819e30b57db01f1fd +Subproject commit f4b9cbebe8ad3c6be3a6292c9f227dc04dc43667 -- 2.39.5