Skip to content

Commit

Permalink
Add the ability to limit address resolution by type
Browse files Browse the repository at this point in the history
Allows restricting to A or AAAA only via the ClientConnectContext.

Relates to #35, but doesn't fully fix it. Further work needs to be done
to avoid IPv6 automatically when IPv6 support is not available.
  • Loading branch information
Robert Goldsmith authored and kelunik committed Sep 12, 2017
1 parent 2d76d7f commit 840e95f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 17 additions & 0 deletions lib/ClientConnectContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace Amp\Socket;

use Amp\Dns\Record;
use function Amp\Socket\Internal\normalizeBindToOption;

final class ClientConnectContext {
private $bindTo = null;
private $connectTimeout = 10000;
private $maxAttempts = 2;
private $typeRestriction = null;

public function withBindTo(string $bindTo = null): self {
$bindTo = normalizeBindToOption($bindTo);
Expand Down Expand Up @@ -52,6 +54,21 @@ public function getMaxAttempts(): int {
return $this->maxAttempts;
}

public function withDnsTypeRestriction(int $type = null): self {
if ($type !== null && $type !== Record::AAAA && $type !== Record::A) {
throw new \Error("Invalid resolver type restriction");
}

$clone = clone $this;
$clone->typeRestriction = $type;

return $clone;
}

public function getDnsTypeRestriction() {
return $this->typeRestriction;
}

public function toStreamContextArray(): array {
$options = [];

Expand Down
2 changes: 1 addition & 1 deletion lib/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function connect(string $uri, ClientConnectContext $socketContext = null, Cancel
$uris = [$uri];
} else {
// Host is not an IP address, so resolve the domain name.
$records = yield Dns\resolve($host);
$records = yield Dns\resolve($host, $socketContext->getDnsTypeRestriction());
foreach ($records as $record) {
/** @var Dns\Record $record */
if ($record->getType() === Dns\Record::AAAA) {
Expand Down

0 comments on commit 840e95f

Please sign in to comment.