From 234f3c4c93d09df97c5cff05c5ee9e79281f76c0 Mon Sep 17 00:00:00 2001 From: Daplie Date: Sat, 14 Jan 2017 12:58:35 -0700 Subject: [PATCH] added pad function. Learning lots of cool things --- howto.md | 5 +++++ listen.js | 25 ++++++++++++++++++++++- node_modules/dns-js/lib/bufferconsumer.js | 18 ++++++++-------- node_modules/dns-js/lib/dnspacket.js | 1 - 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/howto.md b/howto.md index 40b37ea..2e0e487 100644 --- a/howto.md +++ b/howto.md @@ -63,6 +63,11 @@ RangeError: Index out of range ``` which is located in the node.js buffer module. The API is [here](https://nodejs.org/api/buffer.html). +However, the error we are working with will most likely be dealt with by parsing through the binary +and putting it in a format that is acceptable to a custom buffer, since the current buffer.js does +doesn't seem to do the trick. + +Binary When can a Buffer overflow problem occur in js? diff --git a/listen.js b/listen.js index fafbe9c..2695957 100644 --- a/listen.js +++ b/listen.js @@ -11,12 +11,30 @@ var dns = require('dns-js'); var broadcast = '224.0.0.251'; // mdns var port = 5353; // mdns +// ex: pad('11111', 8, '0') +function pad(str, len, ch) { + + while (str.length < len) { + str = ch + str; + } + + return str; +} + socket.on('message', function (message, rinfo) { console.log('Received %d bytes from %s:%d\n', message.length, rinfo.address, rinfo.port); //console.log(msg.toString('utf8')); -message.forEach(parseInt(byte.toString('hex'), 16).toString(2)); + + message.forEach(function(byte){ + + console.log(pad(byte.toString(2), 8,'0')); + + }); + + console.log('got here'); console.log(message.toString('hex')); + console.log(message.toString('ascii')); var packets; try { @@ -31,10 +49,15 @@ message.forEach(parseInt(byte.toString('hex'), 16).toString(2)); if (!Array.isArray(packets)) { packets = [packets]; } require('./cloud-respond.js').respond(socket, packets, rinfo); + + // console.log(packets); + // console.log('\n'); }); socket.bind(port, function () { + console.log('***********************************') console.log('bound on', port); + console.log('***********************************') console.log('bound on', this.address()); socket.setBroadcast(true); diff --git a/node_modules/dns-js/lib/bufferconsumer.js b/node_modules/dns-js/lib/bufferconsumer.js index 0f4308d..46e1214 100644 --- a/node_modules/dns-js/lib/bufferconsumer.js +++ b/node_modules/dns-js/lib/bufferconsumer.js @@ -42,15 +42,15 @@ BufferConsumer.prototype.slice = function (length) { return v; } else { - // if ((this._offset + length) > this.length) { - // debug('Buffer owerflow. Slice beyond buffer.', { - // offset: this._offset, - // length: length, - // bufferLength: this.length - // }); - // debug('so far', this); - // throw new Error('Buffer overflow'); - // } + if ((this._offset + length) > this.length) { + debug('Buffer owerflow. Slice beyond buffer.', { + offset: this._offset, + length: length, + bufferLength: this.length + }); + debug('so far', this); + throw new Error('Buffer overflow'); + } v = this.buffer.slice(this._offset, this._offset + length); this._offset += length; return v; diff --git a/node_modules/dns-js/lib/dnspacket.js b/node_modules/dns-js/lib/dnspacket.js index a7ba91f..f29c554 100644 --- a/node_modules/dns-js/lib/dnspacket.js +++ b/node_modules/dns-js/lib/dnspacket.js @@ -225,7 +225,6 @@ function each(section /*[,filter], callback*/) { }); } - /** * Serialize this DNSPacket into an Buffer for sending over UDP. * @returns {Buffer} A Node.js Buffer