Skip to content
Snippets Groups Projects
Commit 7dea65c3 authored by Deucе's avatar Deucе :ok_hand_tone4:
Browse files

Fixes for async lookups

- Add a unique number to event names for each request
  Prevents responses from being acepted by multiple queries
- Don't set Socket.dnsObject for synchronous lookups
- OPcode of 2 is used for status replies (ie: NXDOMAIN)
- Be more careful construct result array in request()
parent b47b9b45
No related branches found
No related tags found
No related merge requests found
...@@ -55,9 +55,10 @@ function DNS(synchronous, servers) { ...@@ -55,9 +55,10 @@ function DNS(synchronous, servers) {
var sock = new Socket(SOCK_DGRAM, "dns", server.indexOf(':') >= 0); var sock = new Socket(SOCK_DGRAM, "dns", server.indexOf(':') >= 0);
sock.bind(); sock.bind();
sock.connect(server, 53); sock.connect(server, 53);
if (!this.synchronous) if (!this.synchronous) {
sock.cbID = sock.on('read', handle_response); sock.cbID = sock.on('read', handle_response);
sock.dnsObject = this; sock.dnsObject = this;
}
this.sockets.push(sock); this.sockets.push(sock);
}, this); }, this);
...@@ -394,7 +395,7 @@ DNS.prototype.handle_response = function(sock) { ...@@ -394,7 +395,7 @@ DNS.prototype.handle_response = function(sock) {
if (!ret.response) if (!ret.response)
return null; return null;
ret.opcode = (ascii(resp[2]) & 0x1e) >> 1; ret.opcode = (ascii(resp[2]) & 0x1e) >> 1;
if (ret.opcode !== 0) if (ret.opcode !== 0 && ret.opcode !== 2)
return null; return null;
ret.authoritative = !!(ascii(resp[2]) & (1<<5)); ret.authoritative = !!(ascii(resp[2]) & (1<<5));
ret.truncation = !!(ascii(resp[2]) & (1<<6)); ret.truncation = !!(ascii(resp[2]) & (1<<6));
...@@ -595,6 +596,7 @@ DNS.prototype.resolve = function(host, callback, thisObj) ...@@ -595,6 +596,7 @@ DNS.prototype.resolve = function(host, callback, thisObj)
this.sockets.forEach(function(sock) { this.sockets.forEach(function(sock) {
ctx.unique_id += '.'+sock.local_port; ctx.unique_id += '.'+sock.local_port;
}); });
ctx.unique_id += this.increment_id().toString();
if (host === undefined) if (host === undefined)
throw new Error('No host specified'); throw new Error('No host specified');
...@@ -652,7 +654,7 @@ DNS.prototype.resolve = function(host, callback, thisObj) ...@@ -652,7 +654,7 @@ DNS.prototype.resolve = function(host, callback, thisObj)
function handle_response(resp) { function handle_response(resp) {
var rectype; var rectype;
if (resp !== undefined) { if (resp !== null && resp !== undefined) {
switch(resp.queries[0].type) { switch(resp.queries[0].type) {
case DNS.types.A: case DNS.types.A:
rectype = 'A'; rectype = 'A';
...@@ -665,9 +667,10 @@ DNS.prototype.resolve = function(host, callback, thisObj) ...@@ -665,9 +667,10 @@ DNS.prototype.resolve = function(host, callback, thisObj)
if (rectype === undefined) if (rectype === undefined)
return; return;
this[rectype].addrs = []; if (this[rectype].addrs === undefined)
this[rectype].addrs = [];
if (resp !== undefined && resp.answers !== undefined) { if (resp !== null && resp.answers !== undefined) {
resp.answers.forEach(function(ans) { resp.answers.forEach(function(ans) {
if (resp.queries[0].type != ans.type || resp.queries[0].class != ans.class) if (resp.queries[0].type != ans.type || resp.queries[0].class != ans.class)
return; return;
...@@ -692,10 +695,6 @@ DNS.prototype.resolveTypeClass = function(host, type, class, callback, thisObj) ...@@ -692,10 +695,6 @@ DNS.prototype.resolveTypeClass = function(host, type, class, callback, thisObj)
var respAAAA; var respAAAA;
var ret; var ret;
this.sockets.forEach(function(sock) {
ctx.unique_id += '.'+sock.local_port;
});
if (host === undefined) if (host === undefined)
throw new Error('No host specified'); throw new Error('No host specified');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment