Skip to content

Commit

Permalink
dns: add getServers and setServers
Browse files Browse the repository at this point in the history
getServers returns an array of ips that are currently being used for
resolution

setServers takes an array of ips that are to be used for resolution,
this will throw if there's invalid input but preserve the original
configuration
  • Loading branch information
tjfontaine committed May 14, 2013
1 parent 9498fd1 commit 8886c6b
Show file tree
Hide file tree
Showing 4 changed files with 235 additions and 0 deletions.
15 changes: 15 additions & 0 deletions doc/api/dns.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,21 @@ The callback has arguments `(err, domains)`.
On error, `err` is an `Error` object, where `err.code` is
one of the error codes listed below.

## dns.getServers()

Returns an array of IP addresses as strings that are currently being used for
resolution

## dns.setServers(servers)

Given an array of IP addresses as strings, set them as the servers to use for
resolving

If you specify a port with the address it will be stripped, as the underlying
library doesn't support that.

This will throw if you pass invalid input.

## Error codes

Each DNS query can return one of the following error codes:
Expand Down
49 changes: 49 additions & 0 deletions lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,55 @@ exports.resolve = function(domain, type_, callback_) {
};


exports.getServers = function() {
return cares.getServers();
};


exports.setServers = function(servers) {
// cache the original servers because in the event of an error setting the
// servers cares won't have any servers available for resolution
var orig = cares.getServers();

var newSet = [];

servers.forEach(function(serv) {
var ver = isIp(serv);

if (ver)
return newSet.push([ver, serv]);

var match = serv.match(/\[(.*)\](:\d+)?/);

// we have an IPv6 in brackets
if (match) {
ver = isIp(match[1]);
if (ver)
return newSet.push([ver, match[1]]);
}

var s = serv.split(/:\d+$/)[0];
ver = isIp(s);

if (ver)
return newSet.push([ver, s]);

throw new Error('IP address is not properly formatted: ' + serv);
});

var r = cares.setServers(newSet);

if (r) {
// reset the servers to the old servers, because ares probably unset them
cares.setServers(orig.join(','));

var err = cares.strerror(r);
throw new Error('c-ares failed to set servers: "' + err +
'" [' + servers + ']');
}
};


// ERROR CODES
exports.NODATA = 'ENODATA';
exports.FORMERR = 'EFORMERR';
Expand Down
109 changes: 109 additions & 0 deletions src/cares_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,111 @@ static Handle<Value> GetAddrInfo(const Arguments& args) {
}


static Handle<Value> GetServers(const Arguments& args) {
HandleScope scope(node_isolate);

Local<Array> server_array = Array::New();

ares_addr_node* servers;

int r = ares_get_servers(ares_channel, &servers);
assert(r == ARES_SUCCESS);

ares_addr_node* cur = servers;

for (int i = 0; cur != NULL; ++i, cur = cur->next) {
char ip[INET6_ADDRSTRLEN];

const void* caddr = static_cast<const void*>(&cur->addr);
uv_err_t err = uv_inet_ntop(cur->family, caddr, ip, sizeof(ip));
assert(err.code == UV_OK);

Local<String> addr = String::New(ip);
server_array->Set(i, addr);
}

ares_free_data(servers);

return scope.Close(server_array);
}


static Handle<Value> SetServers(const Arguments& args) {
HandleScope scope(node_isolate);

assert(args[0]->IsArray());

Local<Array> arr = Local<Array>::Cast(args[0]);

uint32_t len = arr->Length();

if (len == 0) {
int rv = ares_set_servers(ares_channel, NULL);
return scope.Close(Integer::New(rv));
}

ares_addr_node* servers = new ares_addr_node[len];
ares_addr_node* last = NULL;

uv_err_t uv_ret;

for (uint32_t i = 0; i < len; i++) {
assert(arr->Get(i)->IsArray());

Local<Array> elm = Local<Array>::Cast(arr->Get(i));

assert(elm->Get(0)->Int32Value());
assert(elm->Get(1)->IsString());

int fam = elm->Get(0)->Int32Value();
String::Utf8Value ip(elm->Get(1));

ares_addr_node* cur = &servers[i];

switch (fam) {
case 4:
cur->family = AF_INET;
uv_ret = uv_inet_pton(AF_INET, *ip, &cur->addr);
break;
case 6:
cur->family = AF_INET6;
uv_ret = uv_inet_pton(AF_INET6, *ip, &cur->addr);
break;
}

if (uv_ret.code != UV_OK)
break;

cur->next = NULL;

if (last != NULL)
last->next = cur;

last = cur;
}

int r;

if (uv_ret.code == UV_OK)
r = ares_set_servers(ares_channel, &servers[0]);
else
r = ARES_EBADSTR;

delete[] servers;

return scope.Close(Integer::New(r));
}


static Handle<Value> StrError(const Arguments& args) {
HandleScope scope;

int r = args[0]->Int32Value();

return scope.Close(String::New(ares_strerror(r)));
}


static void Initialize(Handle<Object> target) {
HandleScope scope(node_isolate);
int r;
Expand Down Expand Up @@ -976,6 +1081,10 @@ static void Initialize(Handle<Object> target) {
NODE_SET_METHOD(target, "getaddrinfo", GetAddrInfo);
NODE_SET_METHOD(target, "isIP", IsIP);

NODE_SET_METHOD(target, "strerror", StrError);
NODE_SET_METHOD(target, "getServers", GetServers);
NODE_SET_METHOD(target, "setServers", SetServers);

target->Set(String::NewSymbol("AF_INET"),
Integer::New(AF_INET, node_isolate));
target->Set(String::NewSymbol("AF_INET6"),
Expand Down
62 changes: 62 additions & 0 deletions test/simple/test-dns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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.

var common = require('../common');
var assert = require('assert');

var dns = require('dns');

var existing = dns.getServers();
assert(existing.length);

var goog = [
'8.8.8.8',
'8.8.4.4',
];
assert.doesNotThrow(function () { dns.setServers(goog) });
assert.deepEqual(dns.getServers(), goog);
assert.throws(function () { dns.setServers(['foobar']) });
assert.deepEqual(dns.getServers(), goog);

var goog6 = [
'2001:4860:4860::8888',
'2001:4860:4860::8844',
];
assert.doesNotThrow(function () { dns.setServers(goog6) });
assert.deepEqual(dns.getServers(), goog6);

goog6.push('4.4.4.4');
dns.setServers(goog6);
assert.deepEqual(dns.getServers(), goog6);

var ports = [
'4.4.4.4:53',
'[2001:4860:4860::8888]:53',
];
var portsExpected = [
'4.4.4.4',
'2001:4860:4860::8888',
];
dns.setServers(ports);
assert.deepEqual(dns.getServers(), portsExpected);

assert.doesNotThrow(function () { dns.setServers([]); });
assert.deepEqual(dns.getServers(), []);

0 comments on commit 8886c6b

Please sign in to comment.