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 - 2011 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 BaseFrameworkSystem {
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 getInstance () {
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 * @return $recipient Recipient as ip:port combination
88 protected function resolveIpPortBySessionId ($sessionId) {
89 // Get a wrapper instance
90 $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('node_list_db_wrapper_class');
92 // And ask it for the session id
93 $recipient = $wrapperInstance->resolveIpPortBySessionId($sessionId);
100 * Resolves given session id into an ip:port combination, if ip:port is set, it won't be translated
102 * @param $sessionId Session id or ip:port combination
103 * @return $recipient Recipient as ip:port combination
104 * @throws InvalidSessionIdException If the provided session id is invalid (and no ip:port combination)
105 * @throws NoValidHostnameException If the provided hostname cannot be resolved into an IP address
107 public static function resolveSessionId ($sessionId) {
108 // Get an own instance
109 $selfInstance = self::getInstance();
111 // Default is direct ip:port
112 $recipient = $sessionId;
114 // Does it match a direct ip:port? (hint: see www.regexlib.com for the regular expression)
115 if (!preg_match('/((?:2[0-5]{2}|1\d{2}|[1-9]\d|[1-9])\.(?:(?:2[0-5]{2}|1\d{2}|[1-9]\d|\d)\.){2}(?:2[0-5]{2}|1\d{2}|[1-9]\d|\d)):(\d|[1-9]\d|[1-9]\d{2,3}|[1-5]\d{4}|6[0-4]\d{3}|654\d{2}|655[0-2]\d|6553[0-5])/', $sessionId)) {
117 if (isset($selfInstance->sessionIdCache[$sessionId])) {
119 $selfInstance->debugOutput('HUB-TOOLS: Using entry from sessionIdCache[] array.');
122 $recipient = $selfInstance->sessionIdCache[$sessionId];
123 } elseif (preg_match('/([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}/', $sessionId)) {
124 // Hostname:port found
125 $hostnameArray = explode(':', $sessionId);
127 // Try to resolve it and add port again
128 // @TODO We may want to encapsulate this PHP call into an own class
129 $recipient = gethostbyname($hostnameArray[0]) . ':' . $hostnameArray[1];
132 if ($recipient == $sessionId) {
133 // Resolving hostname->IP failed!
134 throw new NoValidHostnameException($hostnameArray, self::EXCEPTION_HOSTNAME_NOT_FOUND);
136 } elseif (!preg_match('/([a-f0-9]{' . $selfInstance->getSessionIdLength() . '})/', $sessionId)) {
137 // Invalid session id
138 throw new InvalidSessionIdException($sessionId, self::EXCEPTION_SESSION_ID_IS_INVALID);
141 $selfInstance->debugOutput('HUB-TOOLS: Using internal resolver.');
144 $recipient = $selfInstance->resolveIpPortBySessionId($sessionId);
149 $selfInstance->debugOutput('HUB-TOOLS: Session id ' . $sessionId . ' resolved to ' . $recipient);