Skip to content

Commit 4a191c1

Browse files
authored
Merge pull request #59 from othillo/ptr
support PTR
2 parents 9c8225e + 71cb1be commit 4a191c1

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

src/Protocol/Parser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public function parseAnswer(Message $message)
166166
$rdata = inet_ntop($ip);
167167
}
168168

169-
if (Message::TYPE_CNAME === $type) {
169+
if (Message::TYPE_CNAME === $type || Message::TYPE_PTR === $type) {
170170
list($bodyLabels, $consumed) = $this->readLabels($message->data, $consumed);
171171

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

tests/Protocol/ParserTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,52 @@ public function testParseResponseWithTwoAnswers()
272272
$this->assertSame('193.223.78.152', $response->answers[1]->data);
273273
}
274274

275+
public function testParsePTRResponse()
276+
{
277+
$data = "";
278+
$data .= "5d d8 81 80 00 01 00 01 00 00 00 00"; // header
279+
$data .= "01 34 01 34 01 38 01 38 07 69 6e"; // question: 4.4.8.8.in-addr.arpa
280+
$data .= "2d 61 64 64 72 04 61 72 70 61 00"; // question (continued)
281+
$data .= "00 0c 00 01"; // question: type PTR, class IN
282+
$data .= "c0 0c"; // answer: offset pointer to rdata
283+
$data .= "00 0c 00 01"; // answer: type PTR, class IN
284+
$data .= "00 01 51 7f"; // answer: ttl 86399
285+
$data .= "00 20"; // answer: rdlength 32
286+
$data .= "13 67 6f 6f 67 6c 65 2d 70 75 62 6c 69 63 2d 64"; // answer: rdata google-public-dns-b.google.com.
287+
$data .= "6e 73 2d 62 06 67 6f 6f 67 6c 65 03 63 6f 6d 00";
288+
289+
$data = $this->convertTcpDumpToBinary($data);
290+
291+
$response = $this->parser->parseMessage($data);
292+
293+
$header = $response->header;
294+
$this->assertSame(0x5dd8, $header->get('id'));
295+
$this->assertSame(1, $header->get('qdCount'));
296+
$this->assertSame(1, $header->get('anCount'));
297+
$this->assertSame(0, $header->get('nsCount'));
298+
$this->assertSame(0, $header->get('arCount'));
299+
$this->assertSame(1, $header->get('qr'));
300+
$this->assertSame(Message::OPCODE_QUERY, $header->get('opcode'));
301+
$this->assertSame(0, $header->get('aa'));
302+
$this->assertSame(0, $header->get('tc'));
303+
$this->assertSame(1, $header->get('rd'));
304+
$this->assertSame(1, $header->get('ra'));
305+
$this->assertSame(0, $header->get('z'));
306+
$this->assertSame(Message::RCODE_OK, $header->get('rcode'));
307+
308+
$this->assertCount(1, $response->questions);
309+
$this->assertSame('4.4.8.8.in-addr.arpa', $response->questions[0]['name']);
310+
$this->assertSame(Message::TYPE_PTR, $response->questions[0]['type']);
311+
$this->assertSame(Message::CLASS_IN, $response->questions[0]['class']);
312+
313+
$this->assertCount(1, $response->answers);
314+
$this->assertSame('4.4.8.8.in-addr.arpa', $response->answers[0]->name);
315+
$this->assertSame(Message::TYPE_PTR, $response->answers[0]->type);
316+
$this->assertSame(Message::CLASS_IN, $response->answers[0]->class);
317+
$this->assertSame(86399, $response->answers[0]->ttl);
318+
$this->assertSame('google-public-dns-b.google.com', $response->answers[0]->data);
319+
}
320+
275321
/**
276322
* @expectedException InvalidArgumentException
277323
*/

0 commit comments

Comments
 (0)