2011-03-10 09:54:52 +01:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2010-04-08 03:25:37 +02:00
|
|
|
var dns = process.binding('cares');
|
2010-09-16 23:58:04 +02:00
|
|
|
var net = process.binding('net');
|
2010-09-17 09:15:44 +02:00
|
|
|
var IOWatcher = process.binding('io_watcher').IOWatcher;
|
2010-04-08 03:25:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
var watchers = {};
|
|
|
|
var activeWatchers = {};
|
2011-07-05 00:56:56 +02:00
|
|
|
var Timer = process.binding('timer').Timer;
|
2010-04-08 03:25:37 +02:00
|
|
|
|
2010-09-17 09:15:44 +02:00
|
|
|
var timer = new Timer();
|
2010-04-08 03:25:37 +02:00
|
|
|
|
2011-06-08 04:35:50 +02:00
|
|
|
timer.ontimeout = function() {
|
2010-04-12 18:57:24 +02:00
|
|
|
var sockets = Object.keys(activeWatchers);
|
|
|
|
for (var i = 0, l = sockets.length; i < l; i++) {
|
|
|
|
var socket = sockets[i];
|
2010-10-07 05:05:23 +02:00
|
|
|
var s = parseInt(socket, 10);
|
2010-12-02 02:43:30 +01:00
|
|
|
channel.processFD(watchers[socket].read ? s : dns.SOCKET_BAD,
|
2010-10-30 21:52:53 +02:00
|
|
|
watchers[socket].write ? s : dns.SOCKET_BAD);
|
2010-04-08 03:25:37 +02:00
|
|
|
}
|
|
|
|
updateTimer();
|
2010-10-07 05:05:23 +02:00
|
|
|
};
|
2010-04-08 03:25:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
function updateTimer() {
|
|
|
|
timer.stop();
|
|
|
|
|
2010-04-12 18:57:24 +02:00
|
|
|
// Were just checking to see if activeWatchers is empty or not
|
2010-09-23 14:14:16 +02:00
|
|
|
if (0 === Object.keys(activeWatchers).length) return;
|
|
|
|
var max = 20000;
|
|
|
|
var timeout = channel.timeout(max);
|
|
|
|
timer.start(timeout, 0);
|
2010-12-02 02:43:30 +01:00
|
|
|
}
|
2010-04-08 03:25:37 +02:00
|
|
|
|
|
|
|
|
2010-12-02 02:43:30 +01:00
|
|
|
var channel = new dns.Channel({SOCK_STATE_CB: function(socket, read, write) {
|
2011-02-07 23:39:36 +01:00
|
|
|
var watcher, fd;
|
|
|
|
|
|
|
|
if (process.platform == 'win32') {
|
|
|
|
fd = process.binding('os').openOSHandle(socket);
|
|
|
|
} else {
|
|
|
|
fd = socket;
|
|
|
|
}
|
2010-04-08 03:25:37 +02:00
|
|
|
|
|
|
|
if (socket in watchers) {
|
|
|
|
watcher = watchers[socket].watcher;
|
|
|
|
} else {
|
2010-09-17 09:15:44 +02:00
|
|
|
watcher = new IOWatcher();
|
2010-12-02 02:43:30 +01:00
|
|
|
watchers[socket] = { read: read,
|
|
|
|
write: write,
|
|
|
|
watcher: watcher };
|
2010-04-08 03:25:37 +02:00
|
|
|
|
|
|
|
watcher.callback = function(read, write) {
|
2010-12-02 02:43:30 +01:00
|
|
|
channel.processFD(read ? socket : dns.SOCKET_BAD,
|
2010-10-30 21:52:53 +02:00
|
|
|
write ? socket : dns.SOCKET_BAD);
|
2010-04-08 03:25:37 +02:00
|
|
|
updateTimer();
|
2010-10-07 05:05:23 +02:00
|
|
|
};
|
2010-12-02 02:43:30 +01:00
|
|
|
}
|
2010-04-08 03:25:37 +02:00
|
|
|
|
2010-10-30 21:52:53 +02:00
|
|
|
watcher.stop();
|
2010-04-08 03:25:37 +02:00
|
|
|
|
|
|
|
if (!(read || write)) {
|
|
|
|
delete activeWatchers[socket];
|
|
|
|
return;
|
|
|
|
} else {
|
2011-02-07 23:39:36 +01:00
|
|
|
watcher.set(fd, read == 1, write == 1);
|
2010-04-08 03:25:37 +02:00
|
|
|
watcher.start();
|
|
|
|
activeWatchers[socket] = watcher;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateTimer();
|
|
|
|
}});
|
|
|
|
|
2010-12-02 02:43:30 +01:00
|
|
|
exports.resolve = function(domain, type_, callback_) {
|
2010-02-20 01:41:08 +01:00
|
|
|
var type, callback;
|
|
|
|
if (typeof(type_) == 'string') {
|
|
|
|
type = type_;
|
|
|
|
callback = callback_;
|
|
|
|
} else {
|
|
|
|
type = 'A';
|
|
|
|
callback = arguments[1];
|
2009-12-06 09:23:38 +01:00
|
|
|
}
|
2010-01-07 02:28:15 +01:00
|
|
|
|
|
|
|
var resolveFunc = resolveMap[type];
|
|
|
|
|
|
|
|
if (typeof(resolveFunc) == 'function') {
|
2010-02-20 01:41:08 +01:00
|
|
|
resolveFunc(domain, callback);
|
2010-01-07 02:28:15 +01:00
|
|
|
} else {
|
2010-02-20 01:41:08 +01:00
|
|
|
throw new Error('Unknown type "' + type + '"');
|
2010-01-07 02:28:15 +01:00
|
|
|
}
|
2010-10-07 05:05:23 +02:00
|
|
|
};
|
2010-01-07 02:28:15 +01:00
|
|
|
|
2009-12-06 09:23:38 +01:00
|
|
|
|
2010-08-12 18:51:23 +02:00
|
|
|
function familyToSym(family) {
|
|
|
|
if (family !== dns.AF_INET && family !== dns.AF_INET6) {
|
|
|
|
family = (family === 6) ? dns.AF_INET6 : dns.AF_INET;
|
|
|
|
}
|
|
|
|
return family;
|
2010-12-02 02:43:30 +01:00
|
|
|
}
|
2010-08-12 18:51:23 +02:00
|
|
|
|
|
|
|
|
2010-12-02 02:43:30 +01:00
|
|
|
exports.getHostByName = function(domain, family/*=4*/, callback) {
|
2010-08-12 18:51:23 +02:00
|
|
|
if (typeof family === 'function') { callback = family; family = null; }
|
|
|
|
channel.getHostByName(domain, familyToSym(family), callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-12-02 02:43:30 +01:00
|
|
|
exports.getHostByAddr = function(address, family/*=4*/, callback) {
|
2010-08-12 18:51:23 +02:00
|
|
|
if (typeof family === 'function') { callback = family; family = null; }
|
|
|
|
channel.getHostByAddr(address, familyToSym(family), callback);
|
2010-04-08 03:25:37 +02:00
|
|
|
};
|
2009-12-06 09:23:38 +01:00
|
|
|
|
2010-06-30 06:20:32 +02:00
|
|
|
|
2010-04-08 03:25:37 +02:00
|
|
|
// Easy DNS A/AAAA look up
|
2010-08-09 02:53:55 +02:00
|
|
|
// lookup(domain, [family,] callback)
|
2010-12-02 02:43:30 +01:00
|
|
|
exports.lookup = function(domain, family, callback) {
|
2010-10-30 22:31:51 +02:00
|
|
|
// parse arguments
|
2010-08-09 02:53:55 +02:00
|
|
|
if (arguments.length === 2) {
|
|
|
|
callback = family;
|
|
|
|
family = undefined;
|
|
|
|
} else if (family && family !== 4 && family !== 6) {
|
2010-10-07 05:05:23 +02:00
|
|
|
family = parseInt(family, 10);
|
2010-08-09 02:53:55 +02:00
|
|
|
if (family === dns.AF_INET) {
|
|
|
|
family = 4;
|
|
|
|
} else if (family === dns.AF_INET6) {
|
|
|
|
family = 6;
|
|
|
|
} else if (family !== 4 && family !== 6) {
|
|
|
|
throw new Error('invalid argument: "family" must be 4 or 6');
|
|
|
|
}
|
|
|
|
}
|
2010-10-30 22:31:51 +02:00
|
|
|
|
2010-08-09 02:53:55 +02:00
|
|
|
if (!domain) {
|
|
|
|
callback(null, null, family === 6 ? 6 : 4);
|
|
|
|
return;
|
|
|
|
}
|
2010-10-30 22:31:51 +02:00
|
|
|
|
2010-09-16 23:58:04 +02:00
|
|
|
var matchedFamily = net.isIP(domain);
|
2010-08-09 02:53:55 +02:00
|
|
|
if (matchedFamily) {
|
|
|
|
callback(null, domain, matchedFamily);
|
2010-10-30 22:31:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (/\w\.local\.?$/.test(domain)) {
|
|
|
|
// ANNOYING: In the case of mDNS domains use NSS in the thread pool.
|
|
|
|
// I wish c-ares had better support.
|
2010-12-02 02:43:30 +01:00
|
|
|
process.binding('net').getaddrinfo(domain, 4, function(err, domains4) {
|
2010-10-30 22:31:51 +02:00
|
|
|
callback(err, domains4[0], 4);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (family) {
|
|
|
|
// resolve names for explicit address family
|
|
|
|
var af = familyToSym(family);
|
2010-12-02 02:43:30 +01:00
|
|
|
channel.getHostByName(domain, af, function(err, domains) {
|
2010-10-30 22:31:51 +02:00
|
|
|
if (!err && domains && domains.length) {
|
|
|
|
if (family !== net.isIP(domains[0])) {
|
|
|
|
callback(new Error('not found'), []);
|
|
|
|
} else {
|
|
|
|
callback(null, domains[0], family);
|
|
|
|
}
|
2010-08-09 02:53:55 +02:00
|
|
|
} else {
|
2010-10-30 22:31:51 +02:00
|
|
|
callback(err, []);
|
2010-08-09 02:53:55 +02:00
|
|
|
}
|
2010-10-30 22:31:51 +02:00
|
|
|
});
|
2010-12-02 02:43:30 +01:00
|
|
|
return;
|
2010-04-08 03:25:37 +02:00
|
|
|
}
|
2010-10-30 22:31:51 +02:00
|
|
|
|
|
|
|
// first resolve names for v4 and if that fails, try v6
|
2010-12-02 02:43:30 +01:00
|
|
|
channel.getHostByName(domain, dns.AF_INET, function(err, domains4) {
|
2010-10-30 22:31:51 +02:00
|
|
|
if (domains4 && domains4.length) {
|
|
|
|
callback(null, domains4[0], 4);
|
|
|
|
} else {
|
2010-12-02 02:43:30 +01:00
|
|
|
channel.getHostByName(domain, dns.AF_INET6, function(err, domains6) {
|
2010-10-30 22:31:51 +02:00
|
|
|
if (domains6 && domains6.length) {
|
|
|
|
callback(null, domains6[0], 6);
|
|
|
|
} else {
|
|
|
|
callback(err, []);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2010-04-08 03:25:37 +02:00
|
|
|
};
|
2009-12-06 09:23:38 +01:00
|
|
|
|
|
|
|
|
2010-12-02 02:43:30 +01:00
|
|
|
exports.resolve4 = function(domain, callback) {
|
2010-08-09 02:53:55 +02:00
|
|
|
channel.query(domain, dns.A, callback);
|
|
|
|
};
|
2010-12-02 02:43:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
exports.resolve6 = function(domain, callback) {
|
2010-08-09 02:53:55 +02:00
|
|
|
channel.query(domain, dns.AAAA, callback);
|
|
|
|
};
|
2010-12-02 02:43:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
exports.resolveMx = function(domain, callback) {
|
2010-08-09 02:53:55 +02:00
|
|
|
channel.query(domain, dns.MX, callback);
|
|
|
|
};
|
2010-12-02 02:43:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
exports.resolveTxt = function(domain, callback) {
|
2010-08-09 02:53:55 +02:00
|
|
|
channel.query(domain, dns.TXT, callback);
|
|
|
|
};
|
2010-12-02 02:43:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
exports.resolveSrv = function(domain, callback) {
|
2010-08-09 02:53:55 +02:00
|
|
|
channel.query(domain, dns.SRV, callback);
|
|
|
|
};
|
2010-12-02 02:43:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
exports.reverse = function(domain, callback) {
|
2010-08-09 02:53:55 +02:00
|
|
|
channel.query(domain, dns.PTR, callback);
|
|
|
|
};
|
2010-12-02 02:43:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
exports.resolveNs = function(domain, callback) {
|
2010-08-09 02:53:55 +02:00
|
|
|
channel.query(domain, dns.NS, callback);
|
|
|
|
};
|
2010-12-02 02:43:30 +01:00
|
|
|
|
|
|
|
|
2010-11-16 21:50:24 +01:00
|
|
|
exports.resolveCname = function(domain, callback) {
|
|
|
|
channel.query(domain, dns.CNAME, callback);
|
|
|
|
};
|
2010-01-07 02:28:15 +01:00
|
|
|
|
2010-12-02 02:43:30 +01:00
|
|
|
var resolveMap = { A: exports.resolve4,
|
|
|
|
AAAA: exports.resolve6,
|
|
|
|
MX: exports.resolveMx,
|
|
|
|
TXT: exports.resolveTxt,
|
|
|
|
SRV: exports.resolveSrv,
|
2011-05-12 06:43:24 +02:00
|
|
|
PTR: exports.reverse,
|
2010-12-02 02:43:30 +01:00
|
|
|
NS: exports.resolveNs,
|
|
|
|
CNAME: exports.resolveCname };
|
2010-04-08 03:25:37 +02:00
|
|
|
|
|
|
|
// ERROR CODES
|
2010-12-02 02:43:30 +01:00
|
|
|
exports.NODATA = dns.NODATA;
|
|
|
|
exports.FORMERR = dns.FORMERR;
|
|
|
|
exports.BADRESP = dns.BADRESP;
|
|
|
|
exports.NOTFOUND = dns.NOTFOUND;
|
|
|
|
exports.BADNAME = dns.BADNAME;
|
|
|
|
exports.TIMEOUT = dns.TIMEOUT;
|
2010-04-08 03:25:37 +02:00
|
|
|
exports.CONNREFUSED = dns.CONNREFUSED;
|
2010-12-02 02:43:30 +01:00
|
|
|
exports.NOMEM = dns.NOMEM;
|
2010-04-08 03:25:37 +02:00
|
|
|
exports.DESTRUCTION = dns.DESTRUCTION;
|
2010-12-02 02:43:30 +01:00
|
|
|
exports.NOTIMP = dns.NOTIMP;
|
|
|
|
exports.EREFUSED = dns.EREFUSED;
|
|
|
|
exports.SERVFAIL = dns.SERVFAIL;
|