From 5fff05a76f7ba49531e66840a367fb3d6b46054c Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Mon, 9 Oct 2017 12:39:46 -0600 Subject: [PATCH] WIP add cp, add server --- bin/debug.js | 100 +++++++++++++++++++++++++++++++++++++------------- bin/debugd.js | 45 +++++++++++++++++++++++ 2 files changed, 120 insertions(+), 25 deletions(-) create mode 100644 bin/debugd.js diff --git a/bin/debug.js b/bin/debug.js index 18d8a5b..d65727c 100644 --- a/bin/debug.js +++ b/bin/debug.js @@ -1,3 +1,8 @@ +#!/usr/bin/env node + +// node bin/debug.js ../dig.js/blar.DOEsNteXiST.AJ.OnEal.DAplIE.me.any.0.bin +// node bin/debugd.js ../dig.js/blar.DOEsNteXiST.AJ.OnEal.DAplIE.me.any.0.bin +// dig @localhost -p 65053 ANY DOEsNteXiST.AJ.OnEal.DAplIE.me (function () { 'use strict'; @@ -17,6 +22,10 @@ console.log('// HEADER //'); console.log('//////////////////'); console.log(''); +console.log(''); +console.log('byte index 0x00'); +console.log(''); + console.log('id (int)', dv.getUint16(0)); var flags = dv.getUint16(2, false); @@ -45,9 +54,14 @@ console.log('ancount', ancount); console.log('nscount', nscount); console.log('arcount', arcount); -function unpackQuestion(dv, total, len) { +function unpackQuestionLabels(opts) { + var dv = opts.dv; + //var origTotal = opts.total; + var total = opts.total; + var len = opts.len; var qnames = []; var labelLen; + while (true) { if (total >= len) { break; @@ -58,29 +72,46 @@ function unpackQuestion(dv, total, len) { break; } if (192 === labelLen) { - console.log('ERROR: Compression pointer found'); - console.log(dv.getUint8(total, false)); - console.log(dv.getUint8(dv.getUint8(total + 0, false), false)); - console.log(dv.getUint8(dv.getUint8(total + 1, false), false)); - console.log(dv.getUint8(dv.getUint8(total + 2, false), false)); + var pointer = dv.getUint8(total, false); + console.log('Found a pointer to' + + ' 0x' + pointer.toString(16) + ' (' + pointer + ')' + + ' at byte index' + + ' 0x' + leftPad(total.toString(16)) + ' (' + total + ')' + + ' which back-references:' + ); + console.log('-4', dv.getUint8(pointer - 4, false)); + console.log('-3', dv.getUint8(pointer - 3, false)); + console.log('-2', dv.getUint8(pointer - 2, false)); + console.log('-1', dv.getUint8(pointer - 1, false)); + console.log(' 0', dv.getUint8(pointer + 0, false)); + console.log('+1', dv.getUint8(pointer + 1, false)); + console.log('+2', dv.getUint8(pointer + 2, false)); + console.log('bytes near the pointer itself (not what it points to) are:'); + console.log('-2', dv.getUint8(total - 2, false)); + console.log('-1', dv.getUint8(total - 1, false)); + console.log(' 0', dv.getUint8(total + 0, false)); + console.log('+1', dv.getUint8(total + 1, false)); + console.log('+2', dv.getUint8(total + 2, false)); + console.log('+3', dv.getUint8(total + 3, false)); + console.log('+4', dv.getUint8(total + 4, false)); + //total += 1; + opts.cp = true; + qnames.push(unpackQuestionLabels({ dv: dv, total: pointer, len: len }).qname); + total += 1; break; + } else { + var i; + var label = ''; + console.log('label len', labelLen); + for (i = 0; i < labelLen; i += 1) { + //console.log('total:', total, i); + label += String.fromCharCode(dv.getUint8(total + i, false)); + } + total += label.length; + console.log('label:', label); + qnames.push(label); } - var i; - var label = ''; - console.log('label len', labelLen); - for (i = 0; i < labelLen; i += 1) { - label += String.fromCharCode(dv.getUint8(total + i, false)); - } - total += label.length; - console.log('label:', label); - qnames.push(label); } - // leading length and (potentially) trailing null - console.log('QNAME (len ' + (qnames.join('.').length + 1 + (labelLen ? 0 : 1)) + '):', qnames.join('.')); - console.log('QTYPE (len 2):', dv.getUint16(total, false)); - total += 2; - console.log('QCLASS (len 2):', dv.getUint16(total, false)); - total += 2; return { total: total @@ -88,8 +119,23 @@ function unpackQuestion(dv, total, len) { }; } +function unpackQuestion(dv, total, len) { + + var result = unpackQuestionLabels({ dv: dv, total: total, len: len }); + + // leading length and (potentially) trailing null + console.log('QNAME (len ' + (result.total - total) + '):', result.qname, result.cp ? '(pointer)' : ''); + console.log('QTYPE (len 2):', dv.getUint16(result.total, false)); + result.total += 2; + console.log('QCLASS (len 2):', dv.getUint16(result.total, false)); + result.total += 2; + + return result; +} + var q = { total: 12 }; function mapChar(ch) { return JSON.stringify(String.fromCharCode(ch)) + '(' + ch + ')'; } +function leftPad(ch) { ch = ch.toString(); while (ch.length < 2) { ch = '0' + ch; } return ch; } console.log(''); @@ -99,7 +145,7 @@ console.log('//////////////////'); console.log(''); for (count = 0; count < qdcount; count += 1) { console.log(''); - console.log('Question ' + (count + 1) + ' of ' + qdcount + ' (byte index: ' + q.total + ')'); + console.log('Question ' + (count + 1) + ' of ' + qdcount + ' (byte index: 0x' + leftPad(q.total.toString(16)) + ' ' + q.total + ')'); q = unpackQuestion(dv, q.total, ab.byteLength); console.log(''); } @@ -111,7 +157,7 @@ console.log('//////////////////'); console.log(''); for (count = 0; count < ancount; count += 1) { console.log(''); - console.log('Answer', count + 1, 'of', ancount + ' (byte index: ' + q.total + ')'); + console.log('Answer', count + 1, 'of', ancount + ' (byte index: 0x' + q.total.toString(16) + ' ' + q.total + ')'); q = unpackQuestion(dv, q.total, ab.byteLength); console.log(''); } @@ -124,7 +170,7 @@ console.log('//////////////////'); console.log(''); for (count = 0; count < nscount; count += 1) { console.log(''); - console.log('Authority', count + 1, 'of', nscount + ' (byte index: ' + q.total + ')'); + console.log('Authority', count + 1, 'of', nscount + ' (byte index: 0x' + q.total.toString(16) + ' ' + q.total + ')'); q = unpackQuestion(dv, q.total, ab.byteLength); console.log('ttl (len 4):', dv.getUint32(q.total, false)); q.total += 4; @@ -144,10 +190,14 @@ console.log('//////////////////'); console.log(''); for (count = 0; count < arcount; count += 1) { console.log(''); - console.log('Additional', count + 1, 'of', arcount + ' (byte index: ' + q.total + ')'); + console.log('Additional', count + 1, 'of', arcount + ' (byte index: 0x' + q.total.toString(16) + ' ' + q.total + ')'); q = unpackQuestion(dv, q.total, ab.byteLength); console.log(''); } +console.log(''); +console.log('total bytes', dv.byteLength); +console.log('total bytes read', q.total); +console.log(''); }()); diff --git a/bin/debugd.js b/bin/debugd.js new file mode 100644 index 0000000..c8ae8be --- /dev/null +++ b/bin/debugd.js @@ -0,0 +1,45 @@ +#!/usr/bin/env +(function () { +'use strict'; + +var cli = { port: 65053, address: null, udp6: false, bin: process.argv[2] }; +var dgram = require('dgram'); +var server = dgram.createSocket({ + type: cli.udp6 ? 'udp6' : 'udp4' +, reuseAddr: true +}); +var handlers = {}; +var bin = require('fs').readFileSync(cli.bin, null); + +handlers.onMessage = function (nb, rinfo) { + console.log('[DEBUG] got a message'); + + // replace the id to match + bin[0] = nb[0]; + bin[1] = nb[1]; + server.send(bin, rinfo.port, rinfo.address, function () { + console.log('[DEBUG] sent response'); + }); +}; + +handlers.onListening = function () { + /*jshint validthis:true*/ + var server = this; + + if (cli.mdns || '224.0.0.251' === cli.nameserver) { + server.setBroadcast(true); + server.addMembership(cli.nameserver); + } + + console.log(''); + console.log('Bound and Listening:'); + console.log(server.address().address + '#' + server.address().port + ' (' + server.type + ')'); +}; + +server.bind({ + port: cli.port +, address: cli.address +}); +server.on('listening', handlers.onListening); + +}());