1
0
mirror of https://github.com/danog/dns.git synced 2025-01-23 05:51:11 +01:00

Check for errors when writing to socket

Fix for #3
This commit is contained in:
Chris Wright 2014-07-18 00:18:58 +01:00
parent 0cf046c2c5
commit 6fcf8b4d07
2 changed files with 23 additions and 8 deletions

View File

@ -179,14 +179,16 @@ class Client
private function sendRequest($request)
{
$packet = $this->requestBuilder->buildRequest($request['id'], $request['name'], $request['type']);
fwrite($this->socket, $packet);
$bytesWritten = fwrite($this->socket, $packet);
if ($bytesWritten < strlen($packet)) {
$this->completeRequest($request, null, ResolutionErrors::ERR_REQUEST_SEND_FAILED);
return;
}
$request['timeout_id'] = $this->reactor->once(function() use($request) {
unset($this->pendingRequestsByNameAndType[$request['name']][$request['type']]);
foreach ($request['lookups'] as $id => $lookup) {
$this->completePendingLookup($id, null, ResolutionErrors::ERR_SERVER_TIMEOUT);
}
$this->completeRequest($request, null, ResolutionErrors::ERR_SERVER_TIMEOUT);
}, $this->requestTimeout);
if ($this->readWatcherId === null) {
@ -245,9 +247,7 @@ class Client
$this->cache->store($name, $addr, $type, $ttl);
}
foreach ($request['lookups'] as $id => $lookup) {
$this->completePendingLookup($id, $addr, $type);
}
$this->completeRequest($request, $addr, $type);
} else {
foreach ($request['lookups'] as $id => $lookup) {
$this->processPendingLookup($id);
@ -271,6 +271,20 @@ class Client
unset($this->pendingLookups[$id]);
}
/**
* Complete all lookups in a request
*
* @param array $request
* @param string|null $addr
* @param int $type
*/
private function completeRequest($request, $addr, $type)
{
foreach ($request['lookups'] as $id => $lookup) {
$this->completePendingLookup($id, $addr, $type);
}
}
/**
* Send a request to the server
*

View File

@ -7,4 +7,5 @@ class ResolutionErrors
const ERR_INVALID_NAME = 1;
const ERR_NO_RECORD = 2;
const ERR_SERVER_TIMEOUT = 3;
const ERR_REQUEST_SEND_FAILED = 4;
}