3 * A general handler for IPv4 protocols such as TCP, UDP and others.
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 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 BaseIpV4ProtocolHandler extends BaseProtocolHandler {
25 // Regular expression for validating IP:port UNLs
26 const UNL_REGEX = '/^([a-z]{1,}):\/\/\b(([01]?\d?\d|2[0-4]\d|25[0-5])\.){3}([01]?\d?\d|2[0-4]\d|25[0-5])\b:(6553[0-5]|655[0-2][0-9]\d|65[0-4](\d){2}|6[0-4](\d){3}|[1-5](\d){4}|[1-9](\d){0,3})$/';
34 * Protected constructor
36 * @param $className Name of the class
39 protected function __construct ($className) {
40 // Call parent constructor
41 parent::__construct($className);
44 $this->setRegularExpression(self::UNL_REGEX);
48 * Setter for port number to satify HandleableProtocol
50 * @para $port The port number
53 protected final function setPort ($port) {
54 // Set new port number
59 * Getter for port number to satify HandleableProtocol
61 * @return $port The port number
63 public final function getPort () {
69 * Parses the given UNL by splitting it up in its components. The UNL ...
71 * protocol://address[:port]
76 * 'protocol' => 'value',
77 * 'address' => 'value',
81 * @param $unl Universal Node Locator (UNL) to "parse"
82 * @return $unlData Array with all components of the UNL
84 protected function parseUniversalNodeLocator ($unl) {
86 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: unl=' . $unl . ' - CALLED!');
88 // First generic parse
89 $unlData = parent::parseGenericUniversalNodeLocator($unl);
92 * Make sure the generic parts are all there. In case of a rewrite,
93 * these assertitions will bail out on badly formed arrays.
95 assert(isset($unlData[UniversalNodeLocator::UNL_PART_PROTOCOL]));
96 assert(isset($unlData[UniversalNodeLocator::UNL_PART_ADDRESS]));
97 assert(isset($unlData[UniversalNodeLocator::UNL_PART_EXTRA]));
99 // Copy 'extra' -> 'port' ...
100 $unlData[UniversalNodeLocator::UNL_PART_PORT] = $unlData[UniversalNodeLocator::UNL_PART_EXTRA];
102 // ... and drop 'extra'
103 unset($unlData[UniversalNodeLocator::UNL_PART_EXTRA]);
106 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: unlData=' . print_r($unlData, TRUE) . ' - EXIT!');
111 * Validates given 'recipient' if it is a valid UNL. This means that the UNL
112 * can be parsed by the protocol handler.
114 * @param $packageData Valid raw package data
115 * @return $isValid Whether the UNL can be validated
117 public function isValidUniversalNodeLocatorByPackageData (array $packageData) {
119 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: CALLED!');
121 // Is 'recipient' there?
122 assert(isset($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]));
124 // Is the correct handler choosen?
125 assert(substr($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT], 0, strlen($this->getHandlerName())) != $this->getHandlerName());
127 // Default is from generic validation
128 $isValid = $this->isValidUniversalNodeLocator($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]);
131 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: PACKAGE_DATA_RECIPIENT=' . $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . ',isValid[' . gettype($isValid) . ']=' . intval($isValid));
133 // If this doesn't fail, continue validating the IP:port combination
134 if ($isValid === TRUE) {
135 // ... and validate IP:port, first "parse" the UNL
136 $unlData = $this->parseUniversalNodeLocator($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]);
139 * Make sure the extra field 'port' is there. This may look
140 * superflious but in case of a rewrite this assert will stop at
141 * badly formated arrays.
143 assert(isset($unlData[UniversalNodeLocator::UNL_PART_PORT]));
145 // Set whole UNL data array
146 $this->setUniversalNodeLocatorData($unlData);
149 $this->setPort($unlData[UniversalNodeLocator::UNL_PART_PORT]);
153 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: isValid=' . intval($isValid) . ' - EXIT!');
158 * This implementation uses it's parent method and combines it with the
159 * port part to construct a valid address:port combination.
161 * @return $address Address part for the final UNL
163 public function getAddressPart () {
165 $address = sprintf('%s:%s',
166 parent::getAddressPart(),
167 $this->getUniversalNodeLocatorDataElement(UniversalNodeLocator::UNL_PART_PORT)