3 * This class contains static helper functions for our hub
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2014 Hub Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class HubTools extends BaseHubSystem {
25 // Constants for exceptions
26 const EXCEPTION_SESSION_ID_IS_INVALID = 0x200;
27 const EXCEPTION_HOSTNAME_NOT_FOUND = 0x201;
30 * Cache for session ids
32 private $sessionIdCache = array();
35 * Length for session id (should be 32+salt_length
37 private $sessionIdLength = 0;
42 private static $selfInstance = NULL;
45 * Protected constructor
49 protected function __construct () {
50 // Call parent constructor
51 parent::__construct(__CLASS__);
54 $dhtInstance = DhtObjectFactory::createDhtInstance('node');
57 $this->setDhtInstance($dhtInstance);
60 $this->sessionIdLength = 32 + $this->getConfigInstance()->getConfigEntry('salt_length');
64 * Singleton getter for self instance
66 * @retuen $selfInstance An instance of this class
68 public static final function getSelfInstance () {
69 // Is the instance set
70 if (is_null(self::$selfInstance)) {
72 self::$selfInstance = new HubTools();
75 // Return own instance
76 return self::$selfInstance;
80 * Getter for session id length
82 * @return $sessionIdLength Length of session ids
84 protected final function getSessionIdLength () {
85 return $this->sessionIdLength;
89 * Resolves a session id into an universal node locator. The opposite method
90 * is resolveSessionIdByUniversalNodeLocator()
92 * @param $sessionId A valid session id
93 * @return $recipientUniversalNodeLocator Recipient as universal node locator
95 protected function resolveUniversalNodeLocatorBySessionId ($sessionId) {
97 $recipientUniversalNodeLocator = 'invalid:invalid';
99 // And ask it for universal node locator by given session id
100 $recipient = $this->getDhtInstance()->findNodeLocalBySessionId($sessionId);
102 // Is the recipient valid?
103 if ((isset($recipient[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_EXTERNAL_IP])) && (isset($recipient[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_LISTEN_PORT]))) {
105 $recipientUniversalNodeLocator = $recipient[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_EXTERNAL_IP] . ':' . $recipient[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_LISTEN_PORT];
107 // Get the instance, this might throw a NPE
108 $nodeInstance = NodeObjectFactory::createNodeInstance();
110 // Is the session id the same?
111 if ($nodeInstance->getSessionId() == $sessionId) {
112 // Then get the universal node locator from it, assume TCP by default
113 $recipientUniversalNodeLocator = self::determineOwnExternalIp() . ':' . $nodeInstance->getConfigInstance()->getConfigEntry('node_listen_port');
118 return $recipientUniversalNodeLocator;
122 * Resolves a universal node locator into a session id. The "opposite" method
123 * is resolveUniversalNodeLocatorBySessionId().
125 * @param $unl Universal node locator
126 * @return $sessionId Valid session id
128 public static function resolveSessionIdByUniversalNodeLocator ($unl) {
129 // Get an own instance
130 $selfInstance = self::getSelfInstance();
132 // And ask it for session id by given universal node locator
133 $recipient = $selfInstance->getDhtInstance()->findNodeByUniversalNodeLocator($unl);
134 die(__METHOD__.':recipient=<pre>'.print_r($recipient, TRUE).'</pre>' . PHP_EOL);
141 * Resolves given session id into an universal node locator, if universal node locator is set, it won't be translated
143 * @param $address Session id or universal node locator
144 * @return $recipient Recipient as universal node locator
145 * @throws InvalidSessionIdException If the provided session id is invalid (and no universal node locator)
146 * @throws NoValidHostnameException If the provided hostname cannot be resolved into an IP address
148 public static function resolveSessionId ($address) {
149 // Get an own instance
150 $selfInstance = self::getSelfInstance();
152 // Default is direct universal node locator
153 $recipient = $address;
155 // Does it match a direct universal node locator? (hint: see www.regexlib.com for the regular expression)
156 if (preg_match('/([a-z0-9]{3,10})\/\/:([a-z0-9\.]{5,})/', $address)) {
157 // @TODO ((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})
158 // Direct universal node locator found
159 self::createDebugInstance(__CLASS__)->debugOutput('HUB-TOOLS[' . __METHOD__ . ':' . __LINE__ . ']: Direct universal node locator ' . $address . ' detected.');
160 } elseif (isset($selfInstance->sessionIdCache[$address])) {
162 self::createDebugInstance(__CLASS__)->debugOutput('HUB-TOOLS[' . __METHOD__ . ':' . __LINE__ . ']: Using entry from sessionIdCache[] array.');
165 $recipient = $selfInstance->sessionIdCache[$address];
168 self::createDebugInstance(__CLASS__)->debugOutput('HUB-TOOLS[' . __METHOD__ . ':' . __LINE__ . ']: sessionIdCache[' . $address . ']=' . $recipient);
169 } elseif (preg_match('/([a-f0-9]{' . $selfInstance->getSessionIdLength() . '})/', $address)) {
171 self::createDebugInstance(__CLASS__)->debugOutput('HUB-TOOLS[' . __METHOD__ . ':' . __LINE__ . ']: Using internal session id resolver.');
173 // Resolve session id into an universal node locator
174 $recipient = $selfInstance->resolveUniversalNodeLocatorBySessionId($address);
177 self::createDebugInstance(__CLASS__)->debugOutput('HUB-TOOLS[' . __METHOD__ . ':' . __LINE__ . ']: Session id ' . $address . ' resolved to ' . $recipient);
179 // Invalid session id/UNL
180 throw new InvalidSessionIdException($address, self::EXCEPTION_SESSION_ID_IS_INVALID);
188 * Determine IP or 'external_ip' if set
190 * @return $ip The determined external ip of this node
192 public static function determineOwnExternalIp () {
193 // Is the external_ip config entry set?
194 if (FrameworkConfiguration::getSelfInstance()->getConfigEntry('external_ip') != '') {
195 // Use it as external ip
196 $ip = FrameworkConfiguration::getSelfInstance()->getConfigEntry('external_ip');
198 // Determine own external ip by connecting to my (coder) server at 188.138.90.169
199 $ip = ConsoleTools::determineExternalIp();
207 * Determine IP or 'internal_ip' if set
209 * @return $ip The determined external ip of this node
211 public static function determineOwnInternalIp () {
212 // Is the internal_ip config entry set?
213 if (FrameworkConfiguration::getSelfInstance()->getConfigEntry('allow_publish_internal_ip') == 'N') {
214 // Not allowed to publish internal IP, so use external
215 $ip = self::determineOwnExternalIp();
216 } elseif (FrameworkConfiguration::getSelfInstance()->getConfigEntry('internal_ip') != '') {
217 // Use it as internal ip
218 $ip = FrameworkConfiguration::getSelfInstance()->getConfigEntry('internal_ip');
220 // Determine own internal ip by connecting to my (coder) server at 188.138.90.169
221 $ip = ConsoleTools::acquireSelfIPAddress();