Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simpler internal API #49

Merged
merged 3 commits into from
Mar 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/Model/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace React\Dns\Model;

use React\Dns\Query\Query;
use React\Dns\Model\Record;

class Message
{
const TYPE_A = 1;
Expand All @@ -25,6 +28,55 @@ class Message
const RCODE_NOT_IMPLEMENTED = 4;
const RCODE_REFUSED = 5;

/**
* Creates a new request message for the given query
*
* @param Query $query
* @return self
*/
public static function createRequestForQuery(Query $query)
{
$request = new Message();
$request->header->set('id', self::generateId());
$request->header->set('rd', 1);
$request->questions[] = (array) $query;
$request->prepare();

return $request;
}

/**
* Creates a new response message for the given query with the given answer records
*
* @param Query $query
* @param Record[] $answers
* @return self
*/
public static function createResponseWithAnswersForQuery(Query $query, array $answers)
{
$response = new Message();
$response->header->set('id', self::generateId());
$response->header->set('qr', 1);
$response->header->set('opcode', Message::OPCODE_QUERY);
$response->header->set('rd', 1);
$response->header->set('rcode', Message::RCODE_OK);

$response->questions[] = (array) $query;

foreach ($answers as $record) {
$response->answers[] = $record;
}

$response->prepare();

return $response;
}

private static function generateId()
{
return mt_rand(0, 0xffff);
}

public $data = '';

public $header;
Expand Down
26 changes: 26 additions & 0 deletions src/Protocol/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use React\Dns\Model\Message;
use React\Dns\Model\Record;
use InvalidArgumentException;

/**
* DNS protocol parser
Expand All @@ -12,7 +13,32 @@
*/
class Parser
{
/**
* Parses the given raw binary message into a Message object
*
* @param string $data
* @throws InvalidArgumentException
* @return Message
*/
public function parseMessage($data)
{
$message = new Message();
if ($this->parse($data, $message) !== $message) {
throw new InvalidArgumentException('Unable to parse binary message');
}

return $message;
}

/**
* @deprecated unused, exists for BC only
*/
public function parseChunk($data, Message $message)
{
return $this->parse($data, $message);
}

private function parse($data, Message $message)
{
$message->data .= $data;

Expand Down
30 changes: 9 additions & 21 deletions src/Query/CachedExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace React\Dns\Query;

use React\Dns\Model\Message;
use React\Dns\Model\Record;

class CachedExecutor implements ExecutorInterface
{
Expand All @@ -18,15 +17,14 @@ public function __construct(ExecutorInterface $executor, RecordCache $cache)

public function query($nameserver, Query $query)
{
$that = $this;
$executor = $this->executor;
$cache = $this->cache;

return $this->cache
->lookup($query)
->then(
function ($cachedRecords) use ($that, $query) {
return $that->buildResponse($query, $cachedRecords);
function ($cachedRecords) use ($query) {
return Message::createResponseWithAnswersForQuery($query, $cachedRecords);
},
function () use ($executor, $cache, $nameserver, $query) {
return $executor
Expand All @@ -39,27 +37,17 @@ function () use ($executor, $cache, $nameserver, $query) {
);
}

/**
* @deprecated unused, exists for BC only
*/
public function buildResponse(Query $query, array $cachedRecords)
{
$response = new Message();

$response->header->set('id', $this->generateId());
$response->header->set('qr', 1);
$response->header->set('opcode', Message::OPCODE_QUERY);
$response->header->set('rd', 1);
$response->header->set('rcode', Message::RCODE_OK);

$response->questions[] = new Record($query->name, $query->type, $query->class);

foreach ($cachedRecords as $record) {
$response->answers[] = $record;
}

$response->prepare();

return $response;
return Message::createResponseWithAnswersForQuery($query, $cachedRecords);
}

/**
* @deprecated unused, exists for BC only
*/
protected function generateId()
{
return mt_rand(0, 0xffff);
Expand Down
33 changes: 17 additions & 16 deletions src/Query/Executor.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,20 @@ public function __construct(LoopInterface $loop, Parser $parser, BinaryDumper $d

public function query($nameserver, Query $query)
{
$request = $this->prepareRequest($query);
$request = Message::createRequestForQuery($query);

$queryData = $this->dumper->toBinary($request);
$transport = strlen($queryData) > 512 ? 'tcp' : 'udp';

return $this->doQuery($nameserver, $transport, $queryData, $query->name);
}

/**
* @deprecated unused, exists for BC only
*/
public function prepareRequest(Query $query)
{
$request = new Message();
$request->header->set('id', $this->generateId());
$request->header->set('rd', 1);
$request->questions[] = (array) $query;
$request->prepare();

return $request;
return Message::createRequestForQuery($query);
}

public function doQuery($nameserver, $transport, $queryData, $name)
Expand All @@ -63,7 +60,6 @@ public function doQuery($nameserver, $transport, $queryData, $name)
$parser = $this->parser;
$loop = $this->loop;

$response = new Message();
$deferred = new Deferred(function ($resolve, $reject) use (&$timer, &$conn, $name) {
$reject(new CancellationException(sprintf('DNS query for %s has been cancelled', $name)));

Expand Down Expand Up @@ -108,17 +104,19 @@ public function doQuery($nameserver, $transport, $queryData, $name)
return $deferred->promise();
}

$conn->on('data', function ($data) use ($retryWithTcp, $conn, $parser, $response, $transport, $deferred, $timer) {
$responseReady = $parser->parseChunk($data, $response);

if (!$responseReady) {
return;
}

$conn->on('data', function ($data) use ($retryWithTcp, $conn, $parser, $transport, $deferred, $timer) {
if ($timer !== null) {
$timer->cancel();
}

try {
$response = $parser->parseMessage($data);
} catch (\Exception $e) {
$conn->end();
$deferred->reject($e);
return;
}

if ($response->header->isTruncated()) {
if ('tcp' === $transport) {
$deferred->reject(new BadServerException('The server set the truncated bit although we issued a TCP request'));
Expand All @@ -138,6 +136,9 @@ public function doQuery($nameserver, $transport, $queryData, $name)
return $deferred->promise();
}

/**
* @deprecated unused, exists for BC only
*/
protected function generateId()
{
return mt_rand(0, 0xffff);
Expand Down
29 changes: 29 additions & 0 deletions tests/Model/MessageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace React\Tests\Dns\Model;

use React\Dns\Query\Query;
use React\Dns\Model\Message;

class MessageTest extends \PHPUnit_Framework_TestCase
{
public function testCreateRequestDesiresRecusion()
{
$query = new Query('igor.io', Message::TYPE_A, Message::CLASS_IN, 1345656451);
$request = Message::createRequestForQuery($query);

$this->assertTrue($request->header->isQuery());
$this->assertSame(1, $request->header->get('rd'));
}

public function testCreateResponseWithNoAnswers()
{
$query = new Query('igor.io', Message::TYPE_A, Message::CLASS_IN, 1345656451);
$answers = array();
$request = Message::createResponseWithAnswersForQuery($query, $answers);

$this->assertFalse($request->header->isQuery());
$this->assertTrue($request->header->isResponse());
$this->assertEquals(0, $request->header->get('anCount'));
}
}
46 changes: 26 additions & 20 deletions tests/Protocol/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

class ParserTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->parser = new Parser();
}

/**
* @dataProvider provideConvertTcpDumpToBinary
*/
Expand Down Expand Up @@ -34,10 +39,7 @@ public function testParseRequest()

$data = $this->convertTcpDumpToBinary($data);

$request = new Message();

$parser = new Parser();
$parser->parseChunk($data, $request);
$request = $this->parser->parseMessage($data);

$header = $request->header;
$this->assertSame(0x7262, $header->get('id'));
Expand Down Expand Up @@ -74,10 +76,7 @@ public function testParseResponse()

$data = $this->convertTcpDumpToBinary($data);

$response = new Message();

$parser = new Parser();
$parser->parseChunk($data, $response);
$response = $this->parser->parseMessage($data);

$header = $response->header;
$this->assertSame(0x7262, $header->get('id'));
Expand Down Expand Up @@ -121,8 +120,7 @@ public function testParseQuestionWithTwoQuestions()
$request->header->set('qdCount', 2);
$request->data = $data;

$parser = new Parser();
$parser->parseQuestion($request);
$this->parser->parseQuestion($request);

$this->assertCount(2, $request->questions);
$this->assertSame('igor.io', $request->questions[0]['name']);
Expand All @@ -148,8 +146,7 @@ public function testParseAnswerWithInlineData()
$response->header->set('anCount', 1);
$response->data = $data;

$parser = new Parser();
$parser->parseAnswer($response);
$this->parser->parseAnswer($response);

$this->assertCount(1, $response->answers);
$this->assertSame('igor.io', $response->answers[0]->name);
Expand All @@ -174,10 +171,7 @@ public function testParseResponseWithCnameAndOffsetPointers()

$data = $this->convertTcpDumpToBinary($data);

$response = new Message();

$parser = new Parser();
$parser->parseChunk($data, $response);
$response = $this->parser->parseMessage($data);

$this->assertCount(1, $response->questions);
$this->assertSame('mail.google.com', $response->questions[0]['name']);
Expand Down Expand Up @@ -212,10 +206,7 @@ public function testParseResponseWithTwoAnswers()

$data = $this->convertTcpDumpToBinary($data);

$response = new Message();

$parser = new Parser();
$parser->parseChunk($data, $response);
$response = $this->parser->parseMessage($data);

$this->assertCount(1, $response->questions);
$this->assertSame('io.whois-servers.net', $response->questions[0]['name']);
Expand All @@ -237,6 +228,21 @@ public function testParseResponseWithTwoAnswers()
$this->assertSame('193.223.78.152', $response->answers[1]->data);
}

/**
* @expectedException InvalidArgumentException
*/
public function testParseIncomplete()
{
$data = "";
$data .= "72 62 01 00 00 01 00 00 00 00 00 00"; // header
$data .= "04 69 67 6f 72 02 69 6f 00"; // question: igor.io
//$data .= "00 01 00 01"; // question: type A, class IN

$data = $this->convertTcpDumpToBinary($data);

$this->parser->parseMessage($data);
}

private function convertTcpDumpToBinary($input)
{
// sudo ngrep -d en1 -x port 53
Expand Down
Loading