* @version 0.0.0
* @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 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
'.print_r($recipient, TRUE).'' . PHP_EOL); // 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 * @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) { // Get an own instance $selfInstance = self::getSelfInstance(); // Default is direct ip:port $recipient = $sessionId; // Does it match a direct ip:port? (hint: see www.regexlib.com for the regular expression) if (preg_match('/((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])):([0-9]{3,5})/', $sessionId)) { // Direct ip:port found self::createDebugInstance(__CLASS__)->debugOutput('HUB-TOOLS[' . __METHOD__ . ':' . __LINE__ . ']: Direct ip:port ' . $sessionId . ' detected.'); } elseif (isset($selfInstance->sessionIdCache[$sessionId])) { // Debug message self::createDebugInstance(__CLASS__)->debugOutput('HUB-TOOLS[' . __METHOD__ . ':' . __LINE__ . ']: Using entry from sessionIdCache[] array.'); // Found in cache! $recipient = $selfInstance->sessionIdCache[$sessionId]; // Debug message self::createDebugInstance(__CLASS__)->debugOutput('HUB-TOOLS[' . __METHOD__ . ':' . __LINE__ . ']: 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 self::createDebugInstance(__CLASS__)->debugOutput('HUB-TOOLS[' . __METHOD__ . ':' . __LINE__ . ']: hostname:port ' . $sessionId . ' detected.'); // Hostname:port found $hostnameArray = explode(':', $sessionId); /* * Try to resolve it and add port again * @TODO Please try to encapsulate this PHP call into an own class */ $recipient = gethostbyname($hostnameArray[0]) . ':' . $hostnameArray[1]; // Is it valid? if ($recipient == $sessionId) { // Resolving hostname->IP failed! throw new NoValidHostnameException($hostnameArray, self::EXCEPTION_HOSTNAME_NOT_FOUND); } // END - if // Debug message self::createDebugInstance(__CLASS__)->debugOutput('HUB-TOOLS[' . __METHOD__ . ':' . __LINE__ . ']: hostname:port ' . $sessionId . ' resolved to ' . $recipient); } elseif (preg_match('/([a-f0-9]{' . $selfInstance->getSessionIdLength() . '})/', $sessionId)) { // Debug message self::createDebugInstance(__CLASS__)->debugOutput('HUB-TOOLS[' . __METHOD__ . ':' . __LINE__ . ']: Using internal session id resolver.'); // Resolve session id into a ip:port combination $recipient = $selfInstance->resolveIpPortBySessionId($sessionId); // Debug message self::createDebugInstance(__CLASS__)->debugOutput('HUB-TOOLS[' . __METHOD__ . ':' . __LINE__ . ']: Session id ' . $sessionId . ' resolved to ' . $recipient); } else { // Invalid session id throw new InvalidSessionIdException($sessionId, self::EXCEPTION_SESSION_ID_IS_INVALID); } // Return it return $recipient; } /** * Determine IP or 'external_ip' if set * * @return $ip The determined external ip of this node */ public static function determineOwnExternalIp () { // Is the external_ip config entry set? if (FrameworkConfiguration::getSelfInstance()->getConfigEntry('external_ip') != '') { // Use it as external ip $ip = FrameworkConfiguration::getSelfInstance()->getConfigEntry('external_ip'); } else { // Determine own external ip by connecting to my (coder) server at 188.138.90.169 $ip = ConsoleTools::determineExternalIp(); } // 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] ?>