Moved InvalidSocketException from hub project, added determineExternalIp()
authorRoland Häder <roland@mxchange.org>
Wed, 8 Jul 2009 16:53:34 +0000 (16:53 +0000)
committerRoland Häder <roland@mxchange.org>
Wed, 8 Jul 2009 16:53:34 +0000 (16:53 +0000)
.gitattributes
inc/classes/exceptions/socket/.htaccess [new file with mode: 0644]
inc/classes/exceptions/socket/class_InvalidSocketException.php [new file with mode: 0644]
inc/classes/main/console/class_ConsoleTools.php

index 8a0a0b17b3817d0fc4c5d2cf48abeaa5373d22a1..fcbfa7645ab8189a2faca3e73d1557104e4c7ffa 100644 (file)
@@ -99,6 +99,8 @@ inc/classes/exceptions/main/class_VariableIsNotSetException.php -text
 inc/classes/exceptions/result/.htaccess -text
 inc/classes/exceptions/result/class_InvalidDatabaseResultException.php -text
 inc/classes/exceptions/result/class_ResultUpdateException.php -text
 inc/classes/exceptions/result/.htaccess -text
 inc/classes/exceptions/result/class_InvalidDatabaseResultException.php -text
 inc/classes/exceptions/result/class_ResultUpdateException.php -text
+inc/classes/exceptions/socket/.htaccess -text
+inc/classes/exceptions/socket/class_InvalidSocketException.php -text
 inc/classes/exceptions/template/.htaccess -text
 inc/classes/exceptions/template/class_BasePathIsEmptyException.php -text
 inc/classes/exceptions/template/class_BasePathIsNoDirectoryException.php -text
 inc/classes/exceptions/template/.htaccess -text
 inc/classes/exceptions/template/class_BasePathIsEmptyException.php -text
 inc/classes/exceptions/template/class_BasePathIsNoDirectoryException.php -text
diff --git a/inc/classes/exceptions/socket/.htaccess b/inc/classes/exceptions/socket/.htaccess
new file mode 100644 (file)
index 0000000..3a42882
--- /dev/null
@@ -0,0 +1 @@
+Deny from all
diff --git a/inc/classes/exceptions/socket/class_InvalidSocketException.php b/inc/classes/exceptions/socket/class_InvalidSocketException.php
new file mode 100644 (file)
index 0000000..79b3609
--- /dev/null
@@ -0,0 +1,48 @@
+<?php
+/**
+ * This exception is thrown when the socket resource is invalid or an error
+ * occurs while socket initialization phase.
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.ship-simu.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 InvalidSocketException extends FrameworkException {
+       /**
+        * A Constructor for this exception
+        *
+        * @param               $messageArray   Error message array
+        * @param               $code                   Error code
+        * @return      void
+        */
+       public function __construct(array $messageData, $code) {
+               // Construct the message
+               $message = sprintf("[%s:] Invalid socket (type %s != resource). errno=%s, errstr=%s",
+                       $messageData[0]->__toString(),
+                       $messageData[1],
+                       $messageData[2],
+                       $messageData[3]
+               );
+
+               // Call parent exception constructor
+               parent::__construct($message, $code);
+       }
+}
+
+// [EOF]
+?>
index 2dfe18ff9da8feef36ceb7f16330fe52c51e0e0c..876f61ca68d638a7bd2e99fa2871619d413ff6e5 100644 (file)
@@ -116,6 +116,41 @@ class ConsoleTools extends BaseFrameworkSystem {
                // Return the IP address
                return $ip;
        }
                // Return the IP address
                return $ip;
        }
+
+       /**
+        * Determines own remote IP address (e.g. can be used to probe if we are
+        * reachable from outside by determining external IP and then connect to it.
+        * This is accomblished by connecting to the IP of www.ship-simu.org which
+        * should default to 217.172.186.31.
+        *
+        * This method is taken and lightly rewritten from a user comment on php.net:
+        * http://de.php.net/manual/en/function.socket-create.php#49368
+        *
+        * @param       $dest                           IP destination we should connect to
+        * @param       $port                           Port destination we should connect to
+        * @return      $externalAddress        The determined external IP address
+        * @throws      InvalidSocketException  If socket initialization wents wrong
+        */
+       public function determineExternalIp ($dest = '217.172.186.31', $port = 80) {
+               // First get a socket
+               $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
+               if (!is_resource($socket)) {
+                       // Throw InvalidSocketException
+                       throw new InvalidSocketException (array($this, gettype($socket), 0, 'unknown'));
+               } // END - if
+
+               // Connect to the destination
+               socket_connect($socket, $dest, $port);
+
+               // Get the socket address (our external IP) and port (ignored)
+               socket_getsockname($socket, $externalAddress, $ourPort);
+
+               // Close the socket
+               socket_close($socket);
+
+               // Return determined external IP
+               return $externalAddress;
+       }
 }
 
 // [EOF]
 }
 
 // [EOF]