Skip to content
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

Disable IPv6 if it's not route-able #37

Closed
wants to merge 1 commit into from
Closed
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
20 changes: 19 additions & 1 deletion lib/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ function listen(string $uri, ServerListenContext $socketContext = null, ServerTl
* @return Promise<\Amp\Socket\ClientSocket>
*/
function connect(string $uri, ClientConnectContext $socketContext = null, CancellationToken $token = null): Promise {
return call(function () use ($uri, $socketContext, $token) {
static $useIPv6 = true;

return call(function () use ($uri, $socketContext, $token, &$useIPv6) {
$socketContext = $socketContext ?? new ClientConnectContext;
$token = $token ?? new NullCancellationToken;
$attempt = 0;
Expand All @@ -80,13 +82,21 @@ function connect(string $uri, ClientConnectContext $socketContext = null, Cancel
foreach ($records as $record) {
/** @var Dns\Record $record */
if ($record->getType() === Dns\Record::AAAA) {
if (!$useIPv6) {
continue;
}

$uris[] = \sprintf("%s://[%s]:%d", $scheme, $record->getValue(), $port);
} else {
$uris[] = \sprintf("%s://%s:%d", $scheme, $record->getValue(), $port);
}
}
}

if (!$uris) {
throw new Dns\NoRecordException("No IPv4 records available for {$uri} and IPv6 is unavailable.");
}

$flags = \STREAM_CLIENT_CONNECT | \STREAM_CLIENT_ASYNC_CONNECT;
$timeout = $socketContext->getConnectTimeout();

Expand All @@ -99,6 +109,14 @@ function connect(string $uri, ClientConnectContext $socketContext = null, Cancel
$context = \stream_context_create($socketContext->toStreamContextArray());

if (!$socket = @\stream_socket_client($builtUri, $errno, $errstr, null, $flags, $context)) {
if ($errno === 101 && $useIPv6) { // Network is unreachable
$useIPv6 = false;

// If IPv6 addresses are not routeable, remove support for IPv6 and retry.
// See https://github.com/amphp/socket/issues/35.
return connect($uri, $socketContext, $token);
}

throw new ConnectException(\sprintf(
"Connection to %s failed: [Error #%d] %s",
$uri,
Expand Down