Skip to content

Commit 2d970a9

Browse files
authored
Merge pull request #106 from clue-labs/mx
Support parsing MX records
2 parents b1df561 + 573a218 commit 2d970a9

4 files changed

Lines changed: 47 additions & 1 deletion

File tree

examples/11-query-any.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@
4343
$type = 'TXT';
4444
$data = implode('', $data);
4545
break;
46+
case Message::TYPE_MX:
47+
// MX records contain "priority" and "target", only dump its values here
48+
$type = 'MX';
49+
$data = implode(' ', $data);
50+
break;
4651
default:
4752
// unknown type uses HEX format
4853
$type = 'Type ' . $answer->type;

src/Model/Record.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ class Record
4848
* suggests using key-value pairs such as `["name=test","version=1"]`, but
4949
* interpretation of this is not enforced and left up to consumers of this
5050
* library (used for DNS-SD/Zeroconf and others).
51+
* - MX:
52+
* Mail server priority (UINT16) and target hostname without trailing dot,
53+
* for example `{"priority":10,"target":"mx.example.com"}`.
54+
* The payload data uses an associative array with fixed keys "priority"
55+
* (also commonly referred to as weight or preference) and "target" (also
56+
* referred to as exchange). If a response message contains multiple
57+
* records of this type, targets should be sorted by priority (lowest
58+
* first) - this is left up to consumers of this library (used for SMTP).
5159
* - Any other unknown type:
5260
* An opaque binary string containing the RDATA as transported in the DNS
5361
* record. For forwards compatibility, you should not rely on this format
@@ -56,7 +64,7 @@ class Record
5664
* considered a BC break. See the format definition of known types above
5765
* for more details.
5866
*
59-
* @var string|string[]
67+
* @var string|string[]|array
6068
*/
6169
public $data;
6270

src/Protocol/Parser.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ public function parseAnswer(Message $message)
177177
$consumed += $len + 1;
178178
$remaining -= $len + 1;
179179
}
180+
} elseif (Message::TYPE_MX === $type) {
181+
list($priority) = array_values(unpack('n', substr($message->data, $consumed, 2)));
182+
list($bodyLabels, $consumed) = $this->readLabels($message->data, $consumed + 2);
183+
184+
$rdata = array(
185+
'priority' => $priority,
186+
'target' => implode('.', $bodyLabels)
187+
);
180188
} else {
181189
// unknown types simply parse rdata as an opaque binary string
182190
$rdata = substr($message->data, $consumed, $rdLength);

tests/Protocol/ParserTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,31 @@ public function testParseTXTResponseMultiple()
306306
$this->assertSame(array('hello', 'world'), $response->answers[0]->data);
307307
}
308308

309+
public function testParseMXResponse()
310+
{
311+
$data = "";
312+
$data .= "04 69 67 6f 72 02 69 6f 00"; // answer: igor.io
313+
$data .= "00 0f 00 01"; // answer: type MX, class IN
314+
$data .= "00 01 51 80"; // answer: ttl 86400
315+
$data .= "00 09"; // answer: rdlength 9
316+
$data .= "00 0a 05 68 65 6c 6c 6f 00"; // answer: rdata priority 10: hello
317+
318+
$data = $this->convertTcpDumpToBinary($data);
319+
320+
$response = new Message();
321+
$response->header->set('anCount', 1);
322+
$response->data = $data;
323+
324+
$this->parser->parseAnswer($response);
325+
326+
$this->assertCount(1, $response->answers);
327+
$this->assertSame('igor.io', $response->answers[0]->name);
328+
$this->assertSame(Message::TYPE_MX, $response->answers[0]->type);
329+
$this->assertSame(Message::CLASS_IN, $response->answers[0]->class);
330+
$this->assertSame(86400, $response->answers[0]->ttl);
331+
$this->assertSame(array('priority' => 10, 'target' => 'hello'), $response->answers[0]->data);
332+
}
333+
309334
public function testParseResponseWithTwoAnswers()
310335
{
311336
$data = "";

0 commit comments

Comments
 (0)