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

Enable parsing of answers without question for mDNS #15

Closed
wants to merge 8 commits into from
Closed
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ var Packet = function () {

## History

###### 0.1.1 - October 5, 2014

- Fixing NPM tagging issue...

###### 0.1.0 - October 2, 2014

- Added TLSA support
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "native-dns-packet",
"version": "0.1.0",
"version": "0.1.1",
"authors": [
"Timothy J Fontaine <[email protected]> (http://atxconsulting.com)",
"Greg Slepak <[email protected]> (https://twitter.com/taoeffect)",
Expand Down
42 changes: 27 additions & 15 deletions packet.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,8 @@ function writeHeader(buff, packet) {
val += (packet.header.res3 << 4) & 0x10;
val += packet.header.rcode & 0xF;
buff.writeUInt16BE(val & 0xFFFF);
assert(packet.question.length == 1, 'DNS requires one question');
// aren't used
buff.writeUInt16BE(1);
// question offset 4
buff.writeUInt16BE(packet.question.length & 0xFFFF);
// answer offset 6
buff.writeUInt16BE(packet.answer.length & 0xFFFF);
// authority offset 8
Expand Down Expand Up @@ -233,7 +232,6 @@ function writeQuestion(buff, val, label_index) {
namePack(val.name, buff, label_index);
buff.writeUInt16BE(val.type & 0xFFFF);
buff.writeUInt16BE(val.class & 0xFFFF);
return WRITE_RESOURCE_RECORD;
}

function writeResource(buff, val, label_index, rdata) {
Expand Down Expand Up @@ -411,14 +409,21 @@ Packet.write = function(buff, packet) {
switch (state) {
case WRITE_HEADER:
state = writeHeader(buff, packet);
count = 0;
break;
case WRITE_TRUNCATE:
state = writeTruncate(buff, packet, section, last_resource);
break;
case WRITE_QUESTION:
state = writeQuestion(buff, packet.question[0], label_index);
section = 'answer';
count = 0;
if(count === packet.question.length) {
state = WRITE_RESOURCE_RECORD;
section = 'answer';
count = 0;
}
else {
writeQuestion(buff, packet.question[count], label_index);
count += 1
}
break;
case WRITE_RESOURCE_RECORD:
last_resource = buff.tell();
Expand Down Expand Up @@ -521,13 +526,20 @@ function parseHeader(msg, packet) {
}

function parseQuestion(msg, packet) {
var val = {};
val.name = nameUnpack(msg);
val.type = msg.readUInt16BE();
val.class = msg.readUInt16BE();
packet.question[0] = val;
assert(packet.question.length === 1);
// TODO handle qdcount > 1 in practice no one sends this
if (packet.header.opcode === 0 && // Standard query
packet.header.qr === 1 && // Response
packet.question.length === 0) { // Empty question can occur in mDNS
return PARSE_RESOURCE_RECORD;
}
var qnum = packet.question.length;
while (qnum > 0) {
var val = {};
val.name = nameUnpack(msg);
val.type = msg.readUInt16BE();
val.class = msg.readUInt16BE();
packet.question[packet.question.length - qnum] = val;
qnum -= 1;
}
return PARSE_RESOURCE_RECORD;
}

Expand Down Expand Up @@ -678,7 +690,7 @@ var
PARSE_OPT = consts.NAME_TO_QTYPE.OPT,
PARSE_SPF = consts.NAME_TO_QTYPE.SPF,
PARSE_TLSA = consts.NAME_TO_QTYPE.TLSA;


Packet.parse = function(msg) {
var state,
Expand Down
Binary file added test/fixtures/chromecast.bin
Binary file not shown.
45 changes: 45 additions & 0 deletions test/fixtures/chromecast.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{ header:
{ id: 0,
qr: 0,
opcode: 0,
aa: 0,
tc: 0,
rd: 0,
ra: 0,
res1: 0,
res2: 0,
res3: 0,
rcode: 0 },
question:
[ { name: '_googlecast._tcp.local', type: 12, class: 32769 },
{ name: 'TV i Vardagsrummet._googlecast._tcp.local',
type: 33,
class: 32769 },
{ name: 'TV i Vardagsrummet._googlecast._tcp.local',
type: 16,
class: 32769 },
{ name: 'SkC$rm i GC$strummet._googlecast._tcp.local',
type: 33,
class: 32769 },
{ name: 'SkC$rm i GC$strummet._googlecast._tcp.local',
type: 16,
class: 32769 },
{ name: 'TV i Vardagsrummet.local', type: 1, class: 32769 },
{ name: 'TV i Vardagsrummet.local', type: 28, class: 32769 },
{ name: 'SkC$rm i GC$strummet.local', type: 1, class: 32769 },
{ name: 'SkC$rm i GC$strummet.local', type: 28, class: 32769 } ],
answer:
[ { name: '_googlecast._tcp.local',
type: 12,
class: 1,
ttl: 2477,
data: 'TV i Vardagsrummet._googlecast._tcp.local' },
{ name: '_googlecast._tcp.local',
type: 12,
class: 1,
ttl: 2473,
data: 'SkC$rm i GC$strummet._googlecast._tcp.local' } ],
authority: [],
additional: [],
edns_options: [],
payload: undefined }
Binary file added test/fixtures/mdns.readynas.bin
Binary file not shown.
83 changes: 83 additions & 0 deletions test/fixtures/mdns.readynas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{ header:
{ id: 0,
qr: 1,
opcode: 0,
aa: 1,
tc: 0,
rd: 0,
ra: 0,
res1: 0,
res2: 0,
res3: 0,
rcode: 0 },
question: [],
answer:
[ { name: '_services._dns-sd._udp.local',
type: 12,
class: 1,
ttl: 3599,
data: '_transmissionserver._tcp.local' },
{ name: '_services._dns-sd._udp.local',
type: 12,
class: 1,
ttl: 3599,
data: '_ssh._tcp.local' },
{ name: '_services._dns-sd._udp.local',
type: 12,
class: 1,
ttl: 3599,
data: '_https._tcp.local' },
{ name: '_services._dns-sd._udp.local',
type: 12,
class: 1,
ttl: 3599,
data: '_workstation._tcp.local' },
{ name: '_services._dns-sd._udp.local',
type: 12,
class: 1,
ttl: 3599,
data: '_afpovertcp._tcp.local' },
{ name: '_services._dns-sd._udp.local',
type: 12,
class: 1,
ttl: 3599,
data: '_readynas._tcp.local' },
{ name: '_services._dns-sd._udp.local',
type: 12,
class: 1,
ttl: 3599,
data: '_ftp._tcp.local' },
{ name: '_services._dns-sd._udp.local',
type: 12,
class: 1,
ttl: 3599,
data: '_pdl-datastream._tcp.local' },
{ name: '_services._dns-sd._udp.local',
type: 12,
class: 1,
ttl: 3599,
data: '_sftp-ssh._tcp.local' },
{ name: '_services._dns-sd._udp.local',
type: 12,
class: 1,
ttl: 3599,
data: '_googlecast._tcp.local' },
{ name: '_services._dns-sd._udp.local',
type: 12,
class: 1,
ttl: 3599,
data: '_smb._tcp.local' },
{ name: '_services._dns-sd._udp.local',
type: 12,
class: 1,
ttl: 3599,
data: '_udisks-ssh._tcp.local' },
{ name: '_services._dns-sd._udp.local',
type: 12,
class: 1,
ttl: 3599,
data: '_http._tcp.local' } ],
authority: [],
additional: [],
edns_options: [],
payload: undefined }