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 - 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 BaseIpV4ProtocolHandler extends BaseProtocolHandler {
30 // Regular expression for validating IP:port UNLs
31 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);
45 * Setter for port number to satify HandleableProtocol
47 * @para $port The port number
50 protected final function setPort ($port) {
51 // Set new port number
56 * Getter for port number to satify HandleableProtocol
58 * @return $port The port number
60 public final function getPort () {
66 * Validates given 'recipient' if it is a valid UNL. This means that the UNL
67 * can be parsed by the protocol handler.
69 * @param $packageData Valid raw package data
70 * @return $isValid Whether the UNL can be validated
72 public function isValidUniversalNodeLocatorByPackageData (array $packageData) {
74 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: CALLED!');
76 // Is 'recipient' there?
77 assert(isset($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]));
79 // Is the correct handler choosen?
80 assert(substr($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT], 0, strlen($this->getHandlerName())) != $this->getHandlerName());
82 // Default is from generic validation
83 $isValid = $this->isValidUniversalNodeLocator($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT], self::UNL_REGEX);
86 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: PACKAGE_DATA_RECIPIENT=' . $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . ',isValid[' . gettype($isValid) . ']=' . intval($isValid));
88 // If this doesn't fail, continue validating the IP:port combination
89 if ($isValid === TRUE) {
90 // Okay, the basic test is passed, so reset the variable
93 // ... and validate IP:port, first "parse" the UNL
94 $unlParts = $this->parseUnl($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]);
97 die(__METHOD__ . ':PACKAGE_DATA_RECIPIENT=' . $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . ',unlParts=' . print_r($unlParts, TRUE) . PHP_EOL);
101 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: isValid=' . intval($isValid) . ' - EXIT!');