1
0
mirror of https://github.com/danog/dns.git synced 2024-11-26 20:14:51 +01:00

Fix an infinite loop when all 65536 requests are done

Also use a CSPRNG for generating random numbers (fallback to paragonie/random_compat for PHP 5)
This commit is contained in:
Bob Weinand 2016-03-25 14:41:58 +01:00
parent 78cda5692a
commit 540f851e8d
2 changed files with 10 additions and 7 deletions

View File

@ -33,7 +33,8 @@
"amphp/amp": "^1",
"amphp/cache": "^0.1",
"amphp/file": "^0.1",
"daverandom/libdns": "^1"
"daverandom/libdns": "^1",
"paragonie/random_compat": "^1"
},
"require-dev": {
"phpunit/phpunit": "^4.8",

View File

@ -21,7 +21,6 @@ class DefaultResolver implements Resolver {
private $encoder;
private $decoder;
private $arrayCache;
private $requestIdCounter;
private $pendingRequests;
private $serverIdMap;
private $serverUriMap;
@ -36,7 +35,6 @@ class DefaultResolver implements Resolver {
$this->encoder = (new EncoderFactory)->create();
$this->decoder = (new DecoderFactory)->create();
$this->arrayCache = new ArrayCache;
$this->requestIdCounter = 1;
$this->pendingRequests = [];
$this->serverIdMap = [];
$this->serverUriMap = [];
@ -159,11 +157,15 @@ class DefaultResolver implements Resolver {
$server = $this->loadExistingServer($uri) ?: $this->loadNewServer($uri);
// Get the next available request ID
if (\count($this->pendingRequests) > MAX_REQUEST_ID / 2) {
// throttle DNS requests as long as too many requests are active
return \Amp\pipe(\Amp\Pause(100), function () use ($uri, $name, $type) {
return $this->doRequest($uri, $name, $type);
});
}
do {
$requestId = $this->requestIdCounter++;
if ($this->requestIdCounter >= MAX_REQUEST_ID) {
$this->requestIdCounter = 1;
}
$requestId = random_int(0, MAX_REQUEST_ID - 1);
} while (isset($this->pendingRequests[$requestId]));
// Create question record