You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

240 lines
6.0 KiB

15 years ago
var dns = process.binding('cares');
var net = process.binding('net');
var IOWatcher = process.binding('io_watcher').IOWatcher;
15 years ago
var watchers = {};
var activeWatchers = {};
var Timer = process.binding('timer').Timer;
15 years ago
var timer = new Timer();
15 years ago
14 years ago
timer.callback = function() {
var sockets = Object.keys(activeWatchers);
for (var i = 0, l = sockets.length; i < l; i++) {
var socket = sockets[i];
var s = parseInt(socket, 10);
14 years ago
channel.processFD(watchers[socket].read ? s : dns.SOCKET_BAD,
watchers[socket].write ? s : dns.SOCKET_BAD);
15 years ago
}
updateTimer();
};
15 years ago
function updateTimer() {
timer.stop();
// Were just checking to see if activeWatchers is empty or not
14 years ago
if (0 === Object.keys(activeWatchers).length) return;
var max = 20000;
var timeout = channel.timeout(max);
timer.start(timeout, 0);
14 years ago
}
15 years ago
14 years ago
var channel = new dns.Channel({SOCK_STATE_CB: function(socket, read, write) {
15 years ago
var watcher;
if (socket in watchers) {
watcher = watchers[socket].watcher;
} else {
watcher = new IOWatcher();
14 years ago
watchers[socket] = { read: read,
write: write,
watcher: watcher };
15 years ago
watcher.callback = function(read, write) {
14 years ago
channel.processFD(read ? socket : dns.SOCKET_BAD,
write ? socket : dns.SOCKET_BAD);
15 years ago
updateTimer();
};
14 years ago
}
15 years ago
watcher.stop();
15 years ago
if (!(read || write)) {
delete activeWatchers[socket];
return;
} else {
watcher.set(socket, read == 1, write == 1);
15 years ago
watcher.start();
activeWatchers[socket] = watcher;
}
updateTimer();
}});
14 years ago
exports.resolve = function(domain, type_, callback_) {
var type, callback;
if (typeof(type_) == 'string') {
type = type_;
callback = callback_;
} else {
type = 'A';
callback = arguments[1];
}
var resolveFunc = resolveMap[type];
if (typeof(resolveFunc) == 'function') {
resolveFunc(domain, callback);
} else {
throw new Error('Unknown type "' + type + '"');
}
};
function familyToSym(family) {
if (family !== dns.AF_INET && family !== dns.AF_INET6) {
family = (family === 6) ? dns.AF_INET6 : dns.AF_INET;
}
return family;
14 years ago
}
14 years ago
exports.getHostByName = function(domain, family/*=4*/, callback) {
if (typeof family === 'function') { callback = family; family = null; }
channel.getHostByName(domain, familyToSym(family), callback);
};
14 years ago
exports.getHostByAddr = function(address, family/*=4*/, callback) {
if (typeof family === 'function') { callback = family; family = null; }
channel.getHostByAddr(address, familyToSym(family), callback);
15 years ago
};
15 years ago
// Easy DNS A/AAAA look up
// lookup(domain, [family,] callback)
14 years ago
exports.lookup = function(domain, family, callback) {
// parse arguments
if (arguments.length === 2) {
callback = family;
family = undefined;
} else if (family && family !== 4 && family !== 6) {
family = parseInt(family, 10);
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');
}
}
if (!domain) {
callback(null, null, family === 6 ? 6 : 4);
return;
}
var matchedFamily = net.isIP(domain);
if (matchedFamily) {
callback(null, domain, matchedFamily);
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.
14 years ago
process.binding('net').getaddrinfo(domain, 4, function(err, domains4) {
callback(err, domains4[0], 4);
});
return;
}
if (family) {
// resolve names for explicit address family
var af = familyToSym(family);
14 years ago
channel.getHostByName(domain, af, function(err, domains) {
if (!err && domains && domains.length) {
if (family !== net.isIP(domains[0])) {
callback(new Error('not found'), []);
} else {
callback(null, domains[0], family);
}
} else {
callback(err, []);
}
});
14 years ago
return;
15 years ago
}
// first resolve names for v4 and if that fails, try v6
14 years ago
channel.getHostByName(domain, dns.AF_INET, function(err, domains4) {
if (domains4 && domains4.length) {
callback(null, domains4[0], 4);
} else {
14 years ago
channel.getHostByName(domain, dns.AF_INET6, function(err, domains6) {
if (domains6 && domains6.length) {
callback(null, domains6[0], 6);
} else {
callback(err, []);
}
});
}
});
15 years ago
};
14 years ago
exports.resolve4 = function(domain, callback) {
channel.query(domain, dns.A, callback);
};
14 years ago
exports.resolve6 = function(domain, callback) {
channel.query(domain, dns.AAAA, callback);
};
14 years ago
exports.resolveMx = function(domain, callback) {
channel.query(domain, dns.MX, callback);
};
14 years ago
exports.resolveTxt = function(domain, callback) {
channel.query(domain, dns.TXT, callback);
};
14 years ago
exports.resolveSrv = function(domain, callback) {
channel.query(domain, dns.SRV, callback);
};
14 years ago
exports.reverse = function(domain, callback) {
channel.query(domain, dns.PTR, callback);
};
14 years ago
exports.resolveNs = function(domain, callback) {
channel.query(domain, dns.NS, callback);
};
14 years ago
exports.resolveCname = function(domain, callback) {
channel.query(domain, dns.CNAME, callback);
};
14 years ago
var resolveMap = { A: exports.resolve4,
AAAA: exports.resolve6,
MX: exports.resolveMx,
TXT: exports.resolveTxt,
SRV: exports.resolveSrv,
PTR: exports.resolvePtr,
NS: exports.resolveNs,
CNAME: exports.resolveCname };
15 years ago
// ERROR CODES
14 years ago
exports.NODATA = dns.NODATA;
exports.FORMERR = dns.FORMERR;
exports.BADRESP = dns.BADRESP;
exports.NOTFOUND = dns.NOTFOUND;
exports.BADNAME = dns.BADNAME;
exports.TIMEOUT = dns.TIMEOUT;
15 years ago
exports.CONNREFUSED = dns.CONNREFUSED;
14 years ago
exports.NOMEM = dns.NOMEM;
15 years ago
exports.DESTRUCTION = dns.DESTRUCTION;
14 years ago
exports.NOTIMP = dns.NOTIMP;
exports.EREFUSED = dns.EREFUSED;
exports.SERVFAIL = dns.SERVFAIL;