Skip to content
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
11 changes: 10 additions & 1 deletion src/Protocol/BinaryDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@ private function domainNameToBinary($host)
return "\0";
}

return $this->textsToBinary(\explode('.', $host . '.'));
// break up domain name at each dot that is not preceeded by a backslash (escaped notation)
return $this->textsToBinary(
\array_map(
'stripcslashes',
\preg_split(
'/(?<!\\\\)\./',
$host . '.'
)
)
);
}
}
14 changes: 13 additions & 1 deletion src/Protocol/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,19 @@ private function readDomain($data, $consumed)
return array(null, null);
}

return array(implode('.', $labels), $consumed);
// use escaped notation for each label part, then join using dots
return array(
\implode(
'.',
\array_map(
function ($label) {
return \addcslashes($label, "\0..\40.\177");
},
$labels
)
),
$consumed
);
}

private function readLabels($data, $consumed)
Expand Down
41 changes: 41 additions & 0 deletions tests/Protocol/BinaryDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,47 @@ public function testToBinaryForResponseWithSOARecord()
$this->assertSame($expected, $data);
}

public function testToBinaryForResponseWithPTRRecordWithSpecialCharactersEscaped()
{
$data = "";
$data .= "72 62 01 00 00 01 00 01 00 00 00 00"; // header
$data .= "08 5f 70 72 69 6e 74 65 72 04 5f 74 63 70 06 64 6e 73 2d 73 64 03 6f 72 67 00"; // question: _printer._tcp.dns-sd.org
$data .= "00 0c 00 01"; // question: type PTR, class IN
$data .= "08 5f 70 72 69 6e 74 65 72 04 5f 74 63 70 06 64 6e 73 2d 73 64 03 6f 72 67 00"; // answer: _printer._tcp.dns-sd.org
$data .= "00 0c 00 01"; // answer: type PTR, class IN
$data .= "00 01 51 80"; // answer: ttl 86400
$data .= "00 2f"; // answer: rdlength 47
$data .= "14 33 72 64 2e 20 46 6c 6f 6f 72 20 43 6f 70 79 20 52 6f 6f 6d"; // answer: answer: rdata "3rd. Floor Copy Room" …
$data .= "08 5f 70 72 69 6e 74 65 72 04 5f 74 63 70 06 64 6e 73 2d 73 64 03 6f 72 67 00"; // answer: … "._printer._tcp.dns-sd.org"

$expected = $this->formatHexDump($data);

$response = new Message();
$response->id = 0x7262;
$response->rd = true;
$response->rcode = Message::RCODE_OK;

$response->questions[] = new Query(
'_printer._tcp.dns-sd.org',
Message::TYPE_PTR,
Message::CLASS_IN
);

$response->answers[] = new Record(
'_printer._tcp.dns-sd.org',
Message::TYPE_PTR,
Message::CLASS_IN,
86400,
'3rd\.\ Floor\ Copy\ Room._printer._tcp.dns-sd.org'
);

$dumper = new BinaryDumper();
$data = $dumper->toBinary($response);
$data = $this->convertBinaryToHexDump($data);

$this->assertSame($expected, $data);
}

public function testToBinaryForResponseWithMultipleAnswerRecords()
{
$data = "";
Expand Down
30 changes: 30 additions & 0 deletions tests/Protocol/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,36 @@ public function testParsePTRResponse()
$this->assertSame('google-public-dns-b.google.com', $response->answers[0]->data);
}

public function testParsePTRResponseWithSpecialCharactersEscaped()
{
$data = "";
$data .= "5d d8 81 80 00 01 00 01 00 00 00 00"; // header
$data .= "08 5f 70 72 69 6e 74 65 72 04 5f 74 63 70 06 64 6e 73 2d 73 64 03 6f 72 67 00"; // question: _printer._tcp.dns-sd.org
$data .= "00 0c 00 01"; // question: type PTR, class IN
$data .= "c0 0c"; // answer: offset pointer to rdata
$data .= "00 0c 00 01"; // answer: type PTR, class IN
$data .= "00 01 51 7f"; // answer: ttl 86399
$data .= "00 17"; // answer: rdlength 23
$data .= "14 33 72 64 2e 20 46 6c 6f 6f 72 20 43 6f 70 79 20 52 6f 6f 6d"; // answer: rdata "3rd. Floor Copy Room" …
$data .= "c0 0c"; // answer: offset pointer to rdata

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

$response = $this->parser->parseMessage($data);

$this->assertCount(1, $response->questions);
$this->assertSame('_printer._tcp.dns-sd.org', $response->questions[0]->name);
$this->assertSame(Message::TYPE_PTR, $response->questions[0]->type);
$this->assertSame(Message::CLASS_IN, $response->questions[0]->class);

$this->assertCount(1, $response->answers);
$this->assertSame('_printer._tcp.dns-sd.org', $response->answers[0]->name);
$this->assertSame(Message::TYPE_PTR, $response->answers[0]->type);
$this->assertSame(Message::CLASS_IN, $response->answers[0]->class);
$this->assertSame(86399, $response->answers[0]->ttl);
$this->assertSame('3rd\.\ Floor\ Copy\ Room._printer._tcp.dns-sd.org', $response->answers[0]->data);
}

/**
* @expectedException InvalidArgumentException
*/
Expand Down