+++ /dev/null
-<?php
-/**
- * A general handler for IPv4 protocols such as TCP, UDP and others.
- *
- * @author Roland Haeder <webmaster@shipsimu.org>
- * @version 0.0.0
- * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team
- * @license GNU GPL 3.0 or any newer version
- * @link http://www.shipsimu.org
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-class BaseIpV4ProtocolHandler extends BaseProtocolHandler {
- // Regular expression for validating IP:port UNLs
- 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})$/';
-
- /**
- * Port number
- */
- private $port = 0;
-
- /**
- * Protected constructor
- *
- * @param $className Name of the class
- * @return void
- */
- protected function __construct ($className) {
- // Call parent constructor
- parent::__construct($className);
-
- // Set regex
- $this->setRegularExpression(self::UNL_REGEX);
- }
-
- /**
- * Setter for port number to satify HandleableProtocol
- *
- * @para $port The port number
- * @return void
- */
- protected final function setPort ($port) {
- // Set new port number
- $this->port = $port;
- }
-
- /**
- * Getter for port number to satify HandleableProtocol
- *
- * @return $port The port number
- */
- public final function getPort () {
- // Return port number
- return $this->port;
- }
-
- /**
- * Parses the given UNL by splitting it up in its components. The UNL ...
- *
- * protocol://address[:port]
- *
- * ... becomes:
- *
- * array(
- * 'protocol' => 'value',
- * 'address' => 'value',
- * 'port' => 123
- * )
- *
- * @param $unl Universal Node Locator (UNL) to "parse"
- * @return $unlData Array with all components of the UNL
- */
- protected function parseUniversalNodeLocator ($unl) {
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: unl=' . $unl . ' - CALLED!');
-
- // First generic parse
- $unlData = parent::parseGenericUniversalNodeLocator($unl);
-
- /*
- * Make sure the generic parts are all there. In case of a rewrite,
- * these assertitions will bail out on badly formed arrays.
- */
- assert(isset($unlData[UniversalNodeLocator::UNL_PART_PROTOCOL]));
- assert(isset($unlData[UniversalNodeLocator::UNL_PART_ADDRESS]));
- assert(isset($unlData[UniversalNodeLocator::UNL_PART_EXTRA]));
-
- // Copy 'extra' -> 'port' ...
- $unlData[UniversalNodeLocator::UNL_PART_PORT] = $unlData[UniversalNodeLocator::UNL_PART_EXTRA];
-
- // ... and drop 'extra'
- unset($unlData[UniversalNodeLocator::UNL_PART_EXTRA]);
-
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: unlData=' . print_r($unlData, TRUE) . ' - EXIT!');
- return $unlData;
- }
-
- /**
- * Validates given 'recipient' if it is a valid UNL. This means that the UNL
- * can be parsed by the protocol handler.
- *
- * @param $packageData Valid raw package data
- * @return $isValid Whether the UNL can be validated
- */
- public function isValidUniversalNodeLocatorByPackageData (array $packageData) {
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: CALLED!');
-
- // Is 'recipient' there?
- assert(isset($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]));
-
- // Is the correct handler choosen?
- assert(substr($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT], 0, strlen($this->getHandlerName())) != $this->getHandlerName());
-
- // Default is from generic validation
- $isValid = $this->isValidUniversalNodeLocator($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]);
-
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: PACKAGE_DATA_RECIPIENT=' . $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . ',isValid[' . gettype($isValid) . ']=' . intval($isValid));
-
- // If this doesn't fail, continue validating the IP:port combination
- if ($isValid === TRUE) {
- // ... and validate IP:port, first "parse" the UNL
- $unlData = $this->parseUniversalNodeLocator($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]);
-
- /*
- * Make sure the extra field 'port' is there. This may look
- * superflious but in case of a rewrite this assert will stop at
- * badly formated arrays.
- */
- assert(isset($unlData[UniversalNodeLocator::UNL_PART_PORT]));
-
- // Set whole UNL data array
- $this->setUniversalNodeLocatorData($unlData);
-
- // Set port
- $this->setPort($unlData[UniversalNodeLocator::UNL_PART_PORT]);
- } // END - if
-
- // Return result
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: isValid=' . intval($isValid) . ' - EXIT!');
- return $isValid;
- }
-
- /**
- * This implementation uses it's parent method and combines it with the
- * port part to construct a valid address:port combination.
- *
- * @return $address Address part for the final UNL
- */
- public function getAddressPart () {
- // Construct address
- $address = sprintf('%s:%s',
- parent::getAddressPart(),
- $this->getUniversalNodeLocatorDataElement(UniversalNodeLocator::UNL_PART_PORT)
- );
-
- // Return it
- return $address;
- }
-}
-
-// [EOF]
-?>