Permalink
Browse files

c-ares based dns module

  • Loading branch information...
1 parent e9a116f commit dc1f4ebd4478b9fe32039d79a553fac01efafcf5 @blazzy blazzy committed with ry Apr 6, 2010
Showing with 809 additions and 0 deletions.
  1. +121 −0 lib/dns_cares.js
  2. +11 −0 src/node.cc
  3. +660 −0 src/node_cares.cc
  4. +16 −0 src/node_cares.h
  5. +1 −0 wscript
View
@@ -0,0 +1,121 @@
+var dns = process.binding('cares');
+
+
+var watchers = {};
+var activeWatchers = {};
+
+
+var timer = new process.Timer();
+
+timer.callback = function () {
+ for (var socket in activeWatchers) {
+ var s = parseInt(socket);
+ channel.processFD( watchers[socket].read ? s : dns.SOCKET_BAD
+ , watchers[socket].write ? s : dns.SOCKET_BAD
+ );
+ }
+ updateTimer();
+}
+
+
+function updateTimer() {
+ timer.stop();
+
+ for (var socket in activeWatchers) { // if !empty(activeWatchers)
+ var max = 20000;
+ var timeout = channel.timeout(max);
+
+ timer.start(timeout, 0);
+
+ return;
+ }
+}
+
+
+var channel = new dns.Channel({SOCK_STATE_CB: function (socket, read, write) {
+ var watcher;
+
+ if (socket in watchers) {
+ watcher = watchers[socket].watcher;
+ } else {
+ watcher = new process.IOWatcher();
+ watchers[socket] = { read: read
+ , write: write
+ , watcher: watcher
+ };
+
+ watcher.callback = function(read, write) {
+ channel.processFD( read ? socket : dns.SOCKET_BAD
+ , write ? socket : dns.SOCKET_BAD
+ );
+ updateTimer();
+ }
+ }
+
+ watcher.set(socket, read == 1, write == 1);
+
+ if (!(read || write)) {
+ watcher.stop();
+ delete activeWatchers[socket];
+ return;
+ } else {
+ watcher.start();
+ activeWatchers[socket] = watcher;
+ }
+
+ updateTimer();
+}});
+
+
+
+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 + '"');
+ }
+}
+
+
+exports.resolve4 = function(domain, callback) { channel.query(domain, dns.A, callback) };
+exports.resolve6 = function(domain, callback) { channel.query(domain, dns.AAAA, callback) };
+exports.resolveTxt = function(domain, callback) { channel.query(domain, dns.TXT, callback) };
+exports.resolveSrv = function(domain, callback) { channel.query(domain, dns.SRV, callback) };
+exports.reverse = function(domain, callback) { channel.query(domain, dns.PTR, callback) };
+exports.resolveNs = function(domain, callback) { channel.query(domain, dns.NS, callback) };
+
+
+var resolveMap = {
+ 'A' : exports.resolve4,
+ 'AAAA': exports.resolve6,
+ 'TXT' : exports.resolveTxt,
+ 'SRV' : exports.resolveSrv,
+ 'PTR' : exports.resolvePtr,
+ 'NS' : exports.resolveNs,
+};
+
+// ERROR CODES
+exports.NODATA = dns.NODATA;
+exports.FORMERR = dns.FORMERR;
+exports.BADRESP = dns.BADRESP;
+exports.NOTFOUND = dns.NOTFOUND;
+exports.BADNAME = dns.BADNAME;
+exports.TIMEOUT = dns.TIMEOUT;
+exports.CONNREFUSED = dns.CONNREFUSED;
+exports.NOMEM = dns.NOMEM;
+exports.DESTRUCTION = dns.DESTRUCTION;
+
+exports.NOTIMP = dns.NOTIMP;
+exports.EREFUSED = dns.EREFUSED;
+exports.SERVFAIL = dns.SERVFAIL;
View
@@ -19,6 +19,7 @@
#include <node_net2.h>
#include <node_events.h>
#include <node_dns.h>
+#include <node_cares.h>
#include <node_net.h>
#include <node_file.h>
#include <node_idle_watcher.h>
@@ -1194,6 +1195,15 @@ static Handle<Value> Binding(const Arguments& args) {
binding_cache->Set(module, exports);
}
+ } else if (!strcmp(*module_v, "cares")) {
+ if (binding_cache->Has(module)) {
+ exports = binding_cache->Get(module)->ToObject();
+ } else {
+ exports = Object::New();
+ Cares::Initialize(exports);
+ binding_cache->Set(module, exports);
+ }
+
} else if (!strcmp(*module_v, "fs")) {
if (binding_cache->Has(module)) {
exports = binding_cache->Get(module)->ToObject();
@@ -1266,6 +1276,7 @@ static Handle<Value> Binding(const Arguments& args) {
exports->Set(String::New("buffer"), String::New(native_buffer));
exports->Set(String::New("child_process"),String::New(native_child_process));
exports->Set(String::New("dns"), String::New(native_dns));
+ exports->Set(String::New("dns_cares"), String::New(native_dns_cares));
exports->Set(String::New("events"), String::New(native_events));
exports->Set(String::New("file"), String::New(native_file));
exports->Set(String::New("fs"), String::New(native_fs));
Oops, something went wrong.

0 comments on commit dc1f4eb

Please sign in to comment.