8000 Improve error messages for failed TCP/IP connections with errno/errstr by clue · Pull Request #265 · reactphp/socket · GitHub
[go: up one dir, main page]

Skip to content

Improve error messages for failed TCP/IP connections with errno/errstr #265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/TcpConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,20 @@ public function connect($uri)
// The following hack looks like the only way to
// detect connection refused errors with PHP's stream sockets.
if (false === \stream_socket_get_name($stream, true)) {
\fclose($stream);
// actual socket errno and errstr can only be retrieved when ext-sockets is available (see tests)
// @codeCoverageIgnoreStart
if (\function_exists('socket_import_stream')) {
$socket = \socket_import_stream($stream);
$errno = \socket_get_option($socket, \SOL_SOCKET, \SO_ERROR);
$errstr = \socket_strerror($errno);
} else {
$errno = \defined('SOCKET_ECONNREFUSED') ? \SOCKET_ECONNRESET : 111;
$errstr = 'Connection refused?';
}
// @codeCoverageIgnoreEnd

$reject(new \RuntimeException('Connection to ' . $uri . ' failed: Connection refused'));
\fclose($stream);
$reject(new \RuntimeException('Connection to ' . $uri . ' failed: ' . $errstr, $errno));
} else {
$resolve(new Connection($stream, $loop));
}
Expand Down
33 changes: 33 additions & 0 deletions tests/TcpConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,39 @@ public function connectionToTcpServerShouldFailIfFileDescriptorsAreExceeded()
Block\await($connector->connect('127.0.0.1:9999'), $loop, self::TIMEOUT);
}

/** @test */
public function connectionToInvalidNetworkShouldFailWithUnreachableError()
{
if (!defined('SOCKET_ENETUNREACH') || !function_exists('socket_import_stream')) {
$this->markTestSkipped('Test requires ext-socket on PHP 5.4+');
}

// try to find an unreachable network by trying a couple of private network addresses
$errno = 0; $errstr = '';
for ($i = 0; $i < 20; ++$i) {
$address = 'tcp://192.168.' . mt_rand(0, 255) . '.' . mt_rand(1, 254) . ':8123';
$client = @stream_socket_client($address, $errno, $errstr, 0.1 * $i);
if ($errno === SOCKET_ENETUNREACH) {
break;
}
}
if ($client || $errno !== SOCKET_ENETUNREACH) {
$this->markTestSkipped('Expected error ' . SOCKET_ENETUNREACH . ' but got ' . $errno . ' (' . $errstr . ') for ' . $address);
}

$loop = Factory::create();
$connector = new TcpConnector($loop);

$promise = $connector->connect($address);

$this->setExpectedException(
'RuntimeException',
'Connection to ' . $address . ' failed: ' . socket_strerror(SOCKET_ENETUNREACH),
SOCKET_ENETUNREACH
);
Block\await($promise, $loop, self::TIMEOUT);
}

/** @test */
public function connectionToTcpServerShouldSucceedWithRemoteAdressSameAsTarget()
{
Expand Down
0