From 367f88d3be8c89688f7b3c8dffb8b315c0913f12 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Tue, 22 Sep 2020 20:26:13 +0200 Subject: [PATCH] Reject connection attempt when no AAAA and A records are resolved --- src/HappyEyeBallsConnectionBuilder.php | 18 +++++++++++++++++- tests/HappyEyeBallsConnectorTest.php | 15 +++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/HappyEyeBallsConnectionBuilder.php b/src/HappyEyeBallsConnectionBuilder.php index 3c7d5c8d..f755394f 100644 --- a/src/HappyEyeBallsConnectionBuilder.php +++ b/src/HappyEyeBallsConnectionBuilder.php @@ -75,6 +75,10 @@ public function connect() $that->mixIpsIntoConnectQueue($ips); + if ($that->hasBeenResolved() && $that->ipsCount === 0) { + $reject(new \RuntimeException($that->error())); + } + // start next connection attempt if not already awaiting next if ($that->nextAttemptTimer === null && $that->connectQueue) { $that->check($resolve, $reject); @@ -123,7 +127,19 @@ public function connect() public function resolve($type, $reject) { $that = $this; - return $that->resolver->resolveAll($that->host, $type)->then(null, function (\Exception $e) use ($type, $reject, $that) { + return $that->resolver->resolveAll($that->host, $type)->then(function ($ips) use ($that, $type) { + if (\count($ips) === 0) { + if ($type === Message::TYPE_A) { + $that->lastError4 = 'no records found'; + $that->lastErrorFamily = 4; + } else { + $that->lastError6 = 'no records found'; + $that->lastErrorFamily = 6; + } + } + + return $ips; + }, function (\Exception $e) use ($type, $reject, $that) { unset($that->resolverPromises[$type]); $that->resolved[$type] = true; diff --git a/tests/HappyEyeBallsConnectorTest.php b/tests/HappyEyeBallsConnectorTest.php index 6af7807a..2e4a5869 100644 --- a/tests/HappyEyeBallsConnectorTest.php +++ b/tests/HappyEyeBallsConnectorTest.php @@ -270,6 +270,21 @@ public function testCancelDuringTcpConnectionCancelsTcpConnectionIfGivenIp() $this->loop->run(); } + public function testConnectionFailsWhenNoAAAOrARecordsAreResolved() + { + $this->resolver->expects($this->exactly(2))->method('resolveAll')->withConsecutive( + array('google.com', Message::TYPE_AAAA), + array('google.com', Message::TYPE_A) + )->willReturnOnConsecutiveCalls( + $this->returnValue(Promise\resolve(array())), + $this->returnValue(Promise\resolve(array())) + ); + $this->tcp->expects($this->never())->method('connect'); + + $this->setExpectedException('RuntimeException', 'Connection to scheme://google.com:80/?hostname=google.com failed during DNS lookup: no records found'); + Block\await($this->connector->connect('scheme://google.com:80/?hostname=google.com'), $this->loop, 3); + } + /** * @internal */