added pad function. Learning lots of cool things

This commit is contained in:
Daplie 2017-01-14 12:58:35 -07:00
parent fc6aee5ddc
commit 234f3c4c93
4 changed files with 38 additions and 11 deletions

View File

@ -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?

View File

@ -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);

View File

@ -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;

View File

@ -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