X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=application%2Fhub%2Fmain%2Ftools%2Fclass_HubTools.php;h=0d8b4f0d9ce5c9e4ed3a56ba274143def0e7d2dd;hb=11418f67e82ab51b0623749de5f24d34fbe1cd8a;hp=c7063ece5f8eab7bef7e2d11c40156276a11d207;hpb=a350963ce5cca9cce90c958a34287cec1743ac55;p=hub.git diff --git a/application/hub/main/tools/class_HubTools.php b/application/hub/main/tools/class_HubTools.php index c7063ece5..0d8b4f0d9 100644 --- a/application/hub/main/tools/class_HubTools.php +++ b/application/hub/main/tools/class_HubTools.php @@ -4,7 +4,7 @@ * * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Hub Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * @@ -83,14 +83,15 @@ class HubTools extends BaseHubSystem { * Resolves a session id into an ip:port combination * * @param $sessionId A valid session id + * @param $protocol Name of the used protocol: TCP/UDP * @return $recipient Recipient as ip:port combination */ - protected function resolveIpPortBySessionId ($sessionId) { + protected function resolveIpPortBySessionId ($sessionId, $protocol) { // Get a wrapper instance $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('node_list_db_wrapper_class'); // And ask it for the session id - $recipient = $wrapperInstance->resolveIpPortBySessionId($sessionId); + $recipient = $wrapperInstance->resolveIpPortBySessionId($sessionId, $protocol); // Is the recipient invalid? if ($recipient == 'invalid:invalid') { @@ -100,7 +101,7 @@ class HubTools extends BaseHubSystem { // Is the session id the same? if ($nodeInstance->getSessionId() == $sessionId) { // Then get the ip:port from it, assume TCP by default - $recipient = self::determineOwnExternalIp() . ':' . $nodeInstance->getConfigInstance()->getConfigEntry('node_tcp_listen_port'); + $recipient = self::determineOwnExternalIp() . ':' . $nodeInstance->getConfigInstance()->getConfigEntry('node_' . strtolower($protocol) . '_listen_port'); } // END - if } // END - if @@ -108,15 +109,34 @@ class HubTools extends BaseHubSystem { return $recipient; } + /** + * Resolves a ip:port combination into a session id + * + * @param $ipPort Ip:port combination + * @param $protocol Name of used protocol (TCP/UDP) + * @return $sessionId Valid session id + */ + public static function resolveSessionIdByIpPort ($ipPort, $protocol) { + // Get a wrapper instance + $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('node_list_db_wrapper_class'); + + // And ask it for the session id + $sessionId = $wrapperInstance->resolveSessionIdByIpPort($ipPort, $protocol); + + // Return result + return $sessionId; + } + /** * Resolves given session id into an ip:port combination, if ip:port is set, it won't be translated * * @param $sessionId Session id or ip:port combination + * @param $protocol Name of the used protocol (TCP/UDP) * @return $recipient Recipient as ip:port combination * @throws InvalidSessionIdException If the provided session id is invalid (and no ip:port combination) * @throws NoValidHostnameException If the provided hostname cannot be resolved into an IP address */ - public static function resolveSessionId ($sessionId) { + public static function resolveSessionId ($sessionId, $protocol) { // Get an own instance $selfInstance = self::getSelfInstance(); @@ -133,12 +153,18 @@ class HubTools extends BaseHubSystem { // Found in cache! $recipient = $selfInstance->sessionIdCache[$sessionId]; + + // Debug message + $selfInstance->debugOutput('HUB-TOOLS: sessionIdCache[' . $sessionId . ']=' . $recipient); } elseif (preg_match('/([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}:([0-9]{3,5})/', $sessionId)) { + // Hostname:port found + $selfInstance->debugOutput('HUB-TOOLS: hostname:port ' . $sessionId . ' detected.'); + // Hostname:port found $hostnameArray = explode(':', $sessionId); // Try to resolve it and add port again - // @TODO We may want to encapsulate this PHP call into an own class + // @TODO Please try to encapsulate this PHP call into an own class $recipient = gethostbyname($hostnameArray[0]) . ':' . $hostnameArray[1]; // Is it valid? @@ -146,20 +172,23 @@ class HubTools extends BaseHubSystem { // Resolving hostname->IP failed! throw new NoValidHostnameException($hostnameArray, self::EXCEPTION_HOSTNAME_NOT_FOUND); } // END - if + + // Debug message + $selfInstance->debugOutput('HUB-TOOLS: hostname:port ' . $sessionId . ' resolved to ' . $recipient); } elseif (preg_match('/([a-f0-9]{' . $selfInstance->getSessionIdLength() . '})/', $sessionId)) { // Debug message $selfInstance->debugOutput('HUB-TOOLS: Using internal session id resolver.'); // Resolve session id into a ip:port combination - $recipient = $selfInstance->resolveIpPortBySessionId($sessionId); + $recipient = $selfInstance->resolveIpPortBySessionId($sessionId, $protocol); + + // Debug message + $selfInstance->debugOutput('HUB-TOOLS: session id ' . $sessionId . ' resolved to ' . $recipient); } else { // Invalid session id throw new InvalidSessionIdException($sessionId, self::EXCEPTION_SESSION_ID_IS_INVALID); } - // Output message - $selfInstance->debugOutput('HUB-TOOLS: Session id ' . $sessionId . ' resolved to ' . $recipient); - // Return it return $recipient; } @@ -182,6 +211,28 @@ class HubTools extends BaseHubSystem { // Return it return $ip; } + + /** + * Determine IP or 'internal_ip' if set + * + * @return $ip The determined external ip of this node + */ + public static function determineOwnInternalIp () { + // Is the internal_ip config entry set? + if (FrameworkConfiguration::getSelfInstance()->getConfigEntry('allow_publish_internal_ip') == 'N') { + // Not allowed to publish internal IP, so use external + $ip = self::determineOwnExternalIp(); + } elseif (FrameworkConfiguration::getSelfInstance()->getConfigEntry('internal_ip') != '') { + // Use it as internal ip + $ip = FrameworkConfiguration::getSelfInstance()->getConfigEntry('internal_ip'); + } else { + // Determine own internal ip by connecting to my (coder) server at 188.138.90.169 + $ip = ConsoleTools::acquireSelfIPAddress(); + } + + // Return it + return $ip; + } } // [EOF]