]> git.mxchange.org Git - hub.git/blob - application/hub/main/handler/protocol/ipv4/class_BaseIpV4ProtocolHandler.php
66dd83a4003628b83702750ee5d7f3c0e25a6757
[hub.git] / application / hub / main / 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 - 2014 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         /**
26          * Port number
27          */
28         private $port = 0;
29
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})';
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
44         /**
45          * Setter for port number to satify HandleableProtocol
46          *
47          * @para        $port   The port number
48          * @return      void
49          */
50         protected final function setPort ($port) {
51                 // Set new port number
52                 $this->port = $port;
53         }
54
55         /**
56          * Getter for port number to satify HandleableProtocol
57          *
58          * @return      $port   The port number
59          */
60         public final function getPort () {
61                 // Return port number
62                 return $this->port;
63         }
64
65         /**
66          * Validates given 'recipient' if it is a valid UNL. This means that the UNL
67          * can be parsed by the protocol handler.
68          *
69          * @param       $packageData    Valid raw package data
70          * @return      $isValid                Whether the UNL can be validated
71          */
72         public function isValidUniversalNodeLocatorByPackageData (array $packageData) {
73                 // Debug message
74                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: CALLED!');
75
76                 // Is 'recipient' there?
77                 assert(isset($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]));
78
79                 // Is the correct handler choosen?
80                 assert(substr($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT], 0, strlen($this->getHandlerName())) != $this->getHandlerName());
81
82                 // Default is from generic validation
83                 $isValid = $this->isValidUniversalNodeLocator($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT], self::UNL_REGEX);
84
85                 // Debug message
86                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: PACKAGE_DATA_RECIPIENT=' . $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . ',isValid[' . gettype($isValid) . ']=' . intval($isValid));
87
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
91                         $isValid = FALSE;
92
93                         // ... and validate IP:port, first "parse" the UNL
94                         $unlParts = $this->parseUniversalNodeLocator($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]);
95
96                         // Debug die
97                         die(__METHOD__ . ':PACKAGE_DATA_RECIPIENT=' . $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . ',unlParts=' . print_r($unlParts, TRUE) . PHP_EOL);
98                 } // END - if
99
100                 // Return result
101                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: isValid=' . intval($isValid) . ' - EXIT!');
102                 return $isValid;
103         }
104 }
105
106 // [EOF]
107 ?>