]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/handler/protocol/ipv4/class_BaseIpV4ProtocolHandler.php
Continued refacturing:
[hub.git] / application / hub / main / handler / protocol / ipv4 / class_BaseIpV4ProtocolHandler.php
index 601dc7ce8935b341bc0e2408265619876b1ba0f3..583616c65bfdfa7a200f8cad8e73562159c94e58 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 class BaseIpV4ProtocolHandler extends BaseProtocolHandler {
+       /**
+        * Port number
+        */
+       private $port = 0;
+
+       // 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})';
+
        /**
         * Protected constructor
         *
@@ -32,6 +40,67 @@ class BaseIpV4ProtocolHandler extends BaseProtocolHandler {
                // Call parent constructor
                parent::__construct($className);
        }
+
+       /**
+        * 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;
+       }
+
+       /**
+        * 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], self::UNL_REGEX);
+
+               // 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) {
+                       // Okay, the basic test is passed, so reset the variable
+                       $isValid = FALSE;
+
+                       // ... and validate IP:port, first "parse" the UNL
+                       $unlParts = $this->parseUnl($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]);
+
+                       // Debug die
+                       die(__METHOD__ . ':PACKAGE_DATA_RECIPIENT=' . $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . ',unlParts=' . print_r($unlParts, TRUE) . PHP_EOL);
+               } // END - if
+
+               // Return result
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: isValid=' . intval($isValid) . ' - EXIT!');
+               return $isValid;
+       }
 }
 
 // [EOF]