3 * This class contains static helper functions for our hub
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.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 $this->sessionIdLength = 32 + $this->getConfigInstance()->getConfigEntry('salt_length');
58 * Singleton getter for self instance
60 * @retuen $selfInstance An instance of this class
62 public static final function getSelfInstance () {
63 // Is the instance set
64 if (is_null(self::$selfInstance)) {
66 self::$selfInstance = new HubTools();
69 // Return own instance
70 return self::$selfInstance;
74 * Getter for session id length
76 * @return $sessionIdLength Length of session ids
78 protected final function getSessionIdLength () {
79 return $this->sessionIdLength;
83 * Resolves a session id into an ip:port combination
85 * @param $sessionId A valid session id
86 * @param $protocol Name of the used protocol: TCP/UDP
87 * @return $recipient Recipient as ip:port combination
88 * @todo Rewrite this to use DHT
90 protected function resolveIpPortBySessionId ($sessionId, $protocol) {
91 // Get a wrapper instance
92 $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('node_list_db_wrapper_class');
94 // And ask it for the session id
95 $recipient = $wrapperInstance->resolveIpPortBySessionId($sessionId, $protocol);
97 // Is the recipient invalid?
98 if ($recipient == 'invalid:invalid') {
99 // Get the instance, this might throw a NPE
100 $nodeInstance = Registry::getRegistry()->getInstance('node');
102 // Is the session id the same?
103 if ($nodeInstance->getSessionId() == $sessionId) {
104 // Then get the ip:port from it, assume TCP by default
105 $recipient = self::determineOwnExternalIp() . ':' . $nodeInstance->getConfigInstance()->getConfigEntry('node_' . strtolower($protocol) . '_listen_port');
114 * Resolves a ip:port combination into a session id
116 * @param $ipPort Ip:port combination
117 * @param $protocol Name of used protocol (TCP/UDP)
118 * @return $sessionId Valid session id
119 * @todo Rewrite this to use DHT
121 public static function resolveSessionIdByIpPort ($ipPort, $protocol) {
122 // Get a wrapper instance
123 $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('node_list_db_wrapper_class');
125 // And ask it for the session id
126 $sessionId = $wrapperInstance->resolveSessionIdByIpPort($ipPort, $protocol);
133 * Resolves given session id into an ip:port combination, if ip:port is set, it won't be translated
135 * @param $sessionId Session id or ip:port combination
136 * @param $protocol Name of the used protocol (TCP/UDP)
137 * @return $recipient Recipient as ip:port combination
138 * @throws InvalidSessionIdException If the provided session id is invalid (and no ip:port combination)
139 * @throws NoValidHostnameException If the provided hostname cannot be resolved into an IP address
141 public static function resolveSessionId ($sessionId, $protocol) {
142 // Get an own instance
143 $selfInstance = self::getSelfInstance();
145 // Default is direct ip:port
146 $recipient = $sessionId;
148 // Does it match a direct ip:port? (hint: see www.regexlib.com for the regular expression)
149 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)) {
150 // Direct ip:port found
151 $selfInstance->debugOutput('HUB-TOOLS: Direct ip:port ' . $sessionId . ' detected.');
152 } elseif (isset($selfInstance->sessionIdCache[$sessionId])) {
154 $selfInstance->debugOutput('HUB-TOOLS: Using entry from sessionIdCache[] array.');
157 $recipient = $selfInstance->sessionIdCache[$sessionId];
160 $selfInstance->debugOutput('HUB-TOOLS: sessionIdCache[' . $sessionId . ']=' . $recipient);
161 } 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)) {
162 // Hostname:port found
163 $selfInstance->debugOutput('HUB-TOOLS: hostname:port ' . $sessionId . ' detected.');
165 // Hostname:port found
166 $hostnameArray = explode(':', $sessionId);
168 // Try to resolve it and add port again
169 // @TODO Please try to encapsulate this PHP call into an own class
170 $recipient = gethostbyname($hostnameArray[0]) . ':' . $hostnameArray[1];
173 if ($recipient == $sessionId) {
174 // Resolving hostname->IP failed!
175 throw new NoValidHostnameException($hostnameArray, self::EXCEPTION_HOSTNAME_NOT_FOUND);
179 $selfInstance->debugOutput('HUB-TOOLS: hostname:port ' . $sessionId . ' resolved to ' . $recipient);
180 } elseif (preg_match('/([a-f0-9]{' . $selfInstance->getSessionIdLength() . '})/', $sessionId)) {
182 $selfInstance->debugOutput('HUB-TOOLS: Using internal session id resolver.');
184 // Resolve session id into a ip:port combination
185 $recipient = $selfInstance->resolveIpPortBySessionId($sessionId, $protocol);
188 $selfInstance->debugOutput('HUB-TOOLS: session id ' . $sessionId . ' resolved to ' . $recipient);
190 // Invalid session id
191 throw new InvalidSessionIdException($sessionId, self::EXCEPTION_SESSION_ID_IS_INVALID);
199 * Determine IP or 'external_ip' if set
201 * @return $ip The determined external ip of this node
203 public static function determineOwnExternalIp () {
204 // Is the external_ip config entry set?
205 if (FrameworkConfiguration::getSelfInstance()->getConfigEntry('external_ip') != '') {
206 // Use it as external ip
207 $ip = FrameworkConfiguration::getSelfInstance()->getConfigEntry('external_ip');
209 // Determine own external ip by connecting to my (coder) server at 188.138.90.169
210 $ip = ConsoleTools::determineExternalIp();
218 * Determine IP or 'internal_ip' if set
220 * @return $ip The determined external ip of this node
222 public static function determineOwnInternalIp () {
223 // Is the internal_ip config entry set?
224 if (FrameworkConfiguration::getSelfInstance()->getConfigEntry('allow_publish_internal_ip') == 'N') {
225 // Not allowed to publish internal IP, so use external
226 $ip = self::determineOwnExternalIp();
227 } elseif (FrameworkConfiguration::getSelfInstance()->getConfigEntry('internal_ip') != '') {
228 // Use it as internal ip
229 $ip = FrameworkConfiguration::getSelfInstance()->getConfigEntry('internal_ip');
231 // Determine own internal ip by connecting to my (coder) server at 188.138.90.169
232 $ip = ConsoleTools::acquireSelfIPAddress();