From: Roland Häder Date: Wed, 8 Jul 2009 16:53:34 +0000 (+0000) Subject: Moved InvalidSocketException from hub project, added determineExternalIp() X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=08ebb6a4213421fff6bde3ff172c8416954db878;p=core.git Moved InvalidSocketException from hub project, added determineExternalIp() --- diff --git a/.gitattributes b/.gitattributes index 8a0a0b17..fcbfa764 100644 --- a/.gitattributes +++ b/.gitattributes @@ -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/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 diff --git a/inc/classes/exceptions/socket/.htaccess b/inc/classes/exceptions/socket/.htaccess new file mode 100644 index 00000000..3a428827 --- /dev/null +++ b/inc/classes/exceptions/socket/.htaccess @@ -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 index 00000000..79b3609d --- /dev/null +++ b/inc/classes/exceptions/socket/class_InvalidSocketException.php @@ -0,0 +1,48 @@ + + * @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 . + */ +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] +?> diff --git a/inc/classes/main/console/class_ConsoleTools.php b/inc/classes/main/console/class_ConsoleTools.php index 2dfe18ff..876f61ca 100644 --- a/inc/classes/main/console/class_ConsoleTools.php +++ b/inc/classes/main/console/class_ConsoleTools.php @@ -116,6 +116,41 @@ class ConsoleTools extends BaseFrameworkSystem { // 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]