Skip to content

Commit

Permalink
Handle CNAME resource records correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveRandom committed Jun 17, 2014
1 parent 5a5610a commit 22c9004
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 13 deletions.
1 change: 1 addition & 0 deletions lib/Addr/AddressModes.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ class AddressModes
const INET4_ADDR = 1;
const INET6_ADDR = 2;
const PREFER_INET6 = 4;
const CNAME = 8;
}
43 changes: 37 additions & 6 deletions lib/Addr/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,24 +198,40 @@ private function onSocketReadable()
{
$packet = fread($this->socket, 512);

$response = $this->responseInterpreter->interpret($packet);
if ($response === null) {
// Decode the response and clean up the pending requests list
$decoded = $this->responseInterpreter->decode($packet);
if ($decoded === null) {
return;
}

list($id, $addr, $ttl) = $response;
list($id, $response) = $decoded;
$request = $this->pendingRequestsById[$id];
$type = $request['type'];
$name = $request['name'];

$this->reactor->cancel($request['timeout_id']);
unset($this->pendingRequestsById[$id], $this->pendingRequestsByNameAndType[$name][$type]);
unset($this->pendingRequestsById[$id], $this->pendingRequestsByNameAndType[$name][$request['type']]);
if (!$this->pendingRequestsById) {
$this->reactor->cancel($this->readWatcherId);
$this->readWatcherId = null;
}

if ($addr !== null) {
// Interpret the response and make sure we have at least one resource record
$interpreted = $this->responseInterpreter->interpret($response, $request['type']);
if ($interpreted === null) {
foreach ($request['lookups'] as $id => $lookup) {
$this->processPendingLookup($id);
}

return;
}

// Distribute the result to the appropriate lookup routine
list($type, $addr, $ttl) = $interpreted;
if ($type === AddressModes::CNAME) {
foreach ($request['lookups'] as $id => $lookup) {
$this->redirectPendingLookup($id, $addr);
}
} else if ($addr !== null) {
if ($request['cache_store']) {
call_user_func($request['cache_store'], $name, $addr, $type, $ttl);
}
Expand Down Expand Up @@ -277,6 +293,21 @@ private function processPendingLookup($id)
}
}

/**
* Redirect a lookup to search for another name
*
* @param int $id
* @param string $name
*/
private function redirectPendingLookup($id, $name)
{
array_unshift($this->pendingLookups[$id]['requests'], $this->pendingLookups[$id]['last_type']);
$this->pendingLookups[$id]['last_type'] = null;
$this->pendingLookups[$id]['name'] = $name;

$this->processPendingLookup($id);
}

/**
* Resolve a name from a server
*
Expand Down
48 changes: 41 additions & 7 deletions lib/Addr/ResponseInterpreter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Addr;

use LibDNS\Decoder\Decoder,
LibDNS\Messages\MessageTypes;
LibDNS\Messages\Message,
LibDNS\Messages\MessageTypes,
LibDNS\Records\ResourceTypes;

class ResponseInterpreter
{
Expand All @@ -23,12 +25,12 @@ public function __construct(Decoder $decoder)
}

/**
* Extract the message ID and response data from a DNS response packet
* Attempt to decode a data packet to a DNS response message
*
* @param string $packet
* @return array|null
* @return Message|null
*/
public function interpret($packet)
public function decode($packet)
{
try {
$message = $this->decoder->decode($packet);
Expand All @@ -40,13 +42,45 @@ public function interpret($packet)
return null;
}

return [$message->getID(), $message];
}

/**
* Extract the message ID and response data from a DNS response packet
*
* @param Message $message
* @param int $expectedType
* @return array|null
*/
public function interpret($message, $expectedType)
{
static $typeMap = [
AddressModes::INET4_ADDR => ResourceTypes::A,
AddressModes::INET6_ADDR => ResourceTypes::AAAA,
];

$answers = $message->getAnswerRecords();
if (!count($answers)) {
return [$message->getID(), null];
return null;
}

/** @var \LibDNS\Records\Resource $record */
$record = $answers->getRecordByIndex(0);
return [$message->getID(), (string)$record->getData(), $record->getTTL()];
$cname = null;
foreach ($answers as $record) {
switch ($record->getType()) {
case $typeMap[$expectedType]:
return [$expectedType, (string)$record->getData(), $record->getTTL()];

case ResourceTypes::CNAME:
$cname = (string)$record->getData();
break;
}
}

if ($cname) {
return [AddressModes::CNAME, $cname, null];
}

return null;
}
}

0 comments on commit 22c9004

Please sign in to comment.