]> git.mxchange.org Git - hub.git/blob - application/hub/classes/handler/protocol/ipv4/class_BaseIpV4ProtocolHandler.php
Updated 'core' + renamed 'main' -> 'classes'.
[hub.git] / application / hub / classes / handler / protocol / ipv4 / class_BaseIpV4ProtocolHandler.php
1 <?php
2 /**
3  * A general handler for IPv4 protocols such as TCP, UDP and others.
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
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
10  *
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.
15  *
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.
20  *
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/>.
23  */
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})$/';
27
28         /**
29          * Port number
30          */
31         private $port = 0;
32
33         /**
34          * Protected constructor
35          *
36          * @param       $className      Name of the class
37          * @return      void
38          */
39         protected function __construct ($className) {
40                 // Call parent constructor
41                 parent::__construct($className);
42
43                 // Set regex
44                 $this->setRegularExpression(self::UNL_REGEX);
45         }
46
47         /**
48          * Setter for port number to satify HandleableProtocol
49          *
50          * @para        $port   The port number
51          * @return      void
52          */
53         protected final function setPort ($port) {
54                 // Set new port number
55                 $this->port = $port;
56         }
57
58         /**
59          * Getter for port number to satify HandleableProtocol
60          *
61          * @return      $port   The port number
62          */
63         public final function getPort () {
64                 // Return port number
65                 return $this->port;
66         }
67
68         /**
69          * Parses the given UNL by splitting it up in its components. The UNL ...
70          *
71          * protocol://address[:port]
72          *
73          * ... becomes:
74          *
75          * array(
76          *     'protocol' => 'value',
77          *     'address'  => 'value',
78          *     'port'     => 123
79          * )
80          *
81          * @param       $unl            Universal Node Locator (UNL) to "parse"
82          * @return      $unlData        Array with all components of the UNL
83          */
84         protected function parseUniversalNodeLocator ($unl) {
85                 // Debug message
86                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: unl=' . $unl . ' - CALLED!');
87
88                 // First generic parse
89                 $unlData = parent::parseGenericUniversalNodeLocator($unl);
90
91                 /*
92                  * Make sure the generic parts are all there. In case of a rewrite,
93                  * these assertitions will bail out on badly formed arrays.
94                  */
95                 assert(isset($unlData[UniversalNodeLocator::UNL_PART_PROTOCOL]));
96                 assert(isset($unlData[UniversalNodeLocator::UNL_PART_ADDRESS]));
97                 assert(isset($unlData[UniversalNodeLocator::UNL_PART_EXTRA]));
98
99                 // Copy 'extra' -> 'port' ...
100                 $unlData[UniversalNodeLocator::UNL_PART_PORT] = $unlData[UniversalNodeLocator::UNL_PART_EXTRA];
101
102                 // ... and drop 'extra'
103                 unset($unlData[UniversalNodeLocator::UNL_PART_EXTRA]);
104
105                 // Debug message
106                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: unlData=' . print_r($unlData, TRUE) . ' - EXIT!');
107                 return $unlData;
108         }
109
110         /**
111          * Validates given 'recipient' if it is a valid UNL. This means that the UNL
112          * can be parsed by the protocol handler.
113          *
114          * @param       $packageData    Valid raw package data
115          * @return      $isValid                Whether the UNL can be validated
116          */
117         public function isValidUniversalNodeLocatorByPackageData (array $packageData) {
118                 // Debug message
119                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: CALLED!');
120
121                 // Is 'recipient' there?
122                 assert(isset($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]));
123
124                 // Is the correct handler choosen?
125                 assert(substr($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT], 0, strlen($this->getHandlerName())) != $this->getHandlerName());
126
127                 // Default is from generic validation
128                 $isValid = $this->isValidUniversalNodeLocator($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]);
129
130                 // Debug message
131                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: PACKAGE_DATA_RECIPIENT=' . $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . ',isValid[' . gettype($isValid) . ']=' . intval($isValid));
132
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]);
137
138                         /*
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.
142                          */
143                         assert(isset($unlData[UniversalNodeLocator::UNL_PART_PORT]));
144
145                         // Set whole UNL data array
146                         $this->setUniversalNodeLocatorData($unlData);
147
148                         // Set port
149                         $this->setPort($unlData[UniversalNodeLocator::UNL_PART_PORT]);
150                 } // END - if
151
152                 // Return result
153                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: isValid=' . intval($isValid) . ' - EXIT!');
154                 return $isValid;
155         }
156
157         /**
158          * This implementation uses it's parent method and combines it with the
159          * port part to construct a valid address:port combination.
160          *
161          * @return      $address        Address part for the final UNL
162          */
163         public function getAddressPart () {
164                 // Construct address
165                 $address = sprintf('%s:%s',
166                         parent::getAddressPart(),
167                         $this->getUniversalNodeLocatorDataElement(UniversalNodeLocator::UNL_PART_PORT)
168                 );
169
170                 // Return it
171                 return $address;
172         }
173 }
174
175 // [EOF]
176 ?>