Skip to content

Commit 77f8d0a

Browse files
committed
Support parsing NS records
1 parent 62b913b commit 77f8d0a

4 files changed

Lines changed: 30 additions & 2 deletions

File tree

examples/11-query-any.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
case Message::TYPE_AAAA:
2929
$type = 'AAAA';
3030
break;
31+
case Message::TYPE_NS:
32+
$type = 'NS';
33+
break;
3134
case Message::TYPE_PTR:
3235
$type = 'PTR';
3336
break;

src/Model/Record.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Record
3535
* IPv4 address string, for example "192.168.1.1".
3636
* - AAAA:
3737
* IPv6 address string, for example "::1".
38-
* - CNAME / PTR:
38+
* - CNAME / PTR / NS:
3939
* The hostname without trailing dot, for example "reactphp.org".
4040
* - Any other unknown type:
4141
* An opaque binary string containing the RDATA as transported in the DNS

src/Protocol/Parser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public function parseAnswer(Message $message)
164164
$consumed += $rdLength;
165165

166166
$rdata = inet_ntop($ip);
167-
} elseif (Message::TYPE_CNAME === $type || Message::TYPE_PTR === $type) {
167+
} elseif (Message::TYPE_CNAME === $type || Message::TYPE_PTR === $type || Message::TYPE_NS === $type) {
168168
list($bodyLabels, $consumed) = $this->readLabels($message->data, $consumed);
169169

170170
$rdata = implode('.', $bodyLabels);

tests/Protocol/ParserTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,31 @@ public function testParseResponseWithTwoAnswers()
298298
$this->assertSame('193.223.78.152', $response->answers[1]->data);
299299
}
300300

301+
public function testParseNSResponse()
302+
{
303+
$data = "";
304+
$data .= "04 69 67 6f 72 02 69 6f 00"; // answer: igor.io
305+
$data .= "00 02 00 01"; // answer: type NS, class IN
306+
$data .= "00 01 51 80"; // answer: ttl 86400
307+
$data .= "00 07"; // answer: rdlength 7
308+
$data .= "05 68 65 6c 6c 6f 00"; // answer: rdata hello
309+
310+
$data = $this->convertTcpDumpToBinary($data);
311+
312+
$response = new Message();
313+
$response->header->set('anCount', 1);
314+
$response->data = $data;
315+
316+
$this->parser->parseAnswer($response);
317+
318+
$this->assertCount(1, $response->answers);
319+
$this->assertSame('igor.io', $response->answers[0]->name);
320+
$this->assertSame(Message::TYPE_NS, $response->answers[0]->type);
321+
$this->assertSame(Message::CLASS_IN, $response->answers[0]->class);
322+
$this->assertSame(86400, $response->answers[0]->ttl);
323+
$this->assertSame('hello', $response->answers[0]->data);
324+
}
325+
301326
public function testParsePTRResponse()
302327
{
303328
$data = "";

0 commit comments

Comments
 (0)