Skip to content

Commit 8886c6b

Browse files
committed
dns: add getServers and setServers
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
1 parent 9498fd1 commit 8886c6b

4 files changed

Lines changed: 235 additions & 0 deletions

File tree

doc/api/dns.markdown

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,21 @@ The callback has arguments `(err, domains)`.
117117
On error, `err` is an `Error` object, where `err.code` is
118118
one of the error codes listed below.
119119

120+
## dns.getServers()
121+
122+
Returns an array of IP addresses as strings that are currently being used for
123+
resolution
124+
125+
## dns.setServers(servers)
126+
127+
Given an array of IP addresses as strings, set them as the servers to use for
128+
resolving
129+
130+
If you specify a port with the address it will be stripped, as the underlying
131+
library doesn't support that.
132+
133+
This will throw if you pass invalid input.
134+
120135
## Error codes
121136

122137
Each DNS query can return one of the following error codes:

lib/dns.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,55 @@ exports.resolve = function(domain, type_, callback_) {
192192
};
193193

194194

195+
exports.getServers = function() {
196+
return cares.getServers();
197+
};
198+
199+
200+
exports.setServers = function(servers) {
201+
// cache the original servers because in the event of an error setting the
202+
// servers cares won't have any servers available for resolution
203+
var orig = cares.getServers();
204+
205+
var newSet = [];
206+
207+
servers.forEach(function(serv) {
208+
var ver = isIp(serv);
209+
210+
if (ver)
211+
return newSet.push([ver, serv]);
212+
213+
var match = serv.match(/\[(.*)\](:\d+)?/);
214+
215+
// we have an IPv6 in brackets
216+
if (match) {
217+
ver = isIp(match[1]);
218+
if (ver)
219+
return newSet.push([ver, match[1]]);
220+
}
221+
222+
var s = serv.split(/:\d+$/)[0];
223+
ver = isIp(s);
224+
225+
if (ver)
226+
return newSet.push([ver, s]);
227+
228+
throw new Error('IP address is not properly formatted: ' + serv);
229+
});
230+
231+
var r = cares.setServers(newSet);
232+
233+
if (r) {
234+
// reset the servers to the old servers, because ares probably unset them
235+
cares.setServers(orig.join(','));
236+
237+
var err = cares.strerror(r);
238+
throw new Error('c-ares failed to set servers: "' + err +
239+
'" [' + servers + ']');
240+
}
241+
};
242+
243+
195244
// ERROR CODES
196245
exports.NODATA = 'ENODATA';
197246
exports.FORMERR = 'EFORMERR';

src/cares_wrap.cc

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,111 @@ static Handle<Value> GetAddrInfo(const Arguments& args) {
939939
}
940940

941941

942+
static Handle<Value> GetServers(const Arguments& args) {
943+
HandleScope scope(node_isolate);
944+
945+
Local<Array> server_array = Array::New();
946+
947+
ares_addr_node* servers;
948+
949+
int r = ares_get_servers(ares_channel, &servers);
950+
assert(r == ARES_SUCCESS);
951+
952+
ares_addr_node* cur = servers;
953+
954+
for (int i = 0; cur != NULL; ++i, cur = cur->next) {
955+
char ip[INET6_ADDRSTRLEN];
956+
957+
const void* caddr = static_cast<const void*>(&cur->addr);
958+
uv_err_t err = uv_inet_ntop(cur->family, caddr, ip, sizeof(ip));
959+
assert(err.code == UV_OK);
960+
961+
Local<String> addr = String::New(ip);
962+
server_array->Set(i, addr);
963+
}
964+
965+
ares_free_data(servers);
966+
967+
return scope.Close(server_array);
968+
}
969+
970+
971+
static Handle<Value> SetServers(const Arguments& args) {
972+
HandleScope scope(node_isolate);
973+
974+
assert(args[0]->IsArray());
975+
976+
Local<Array> arr = Local<Array>::Cast(args[0]);
977+
978+
uint32_t len = arr->Length();
979+
980+
if (len == 0) {
981+
int rv = ares_set_servers(ares_channel, NULL);
982+
return scope.Close(Integer::New(rv));
983+
}
984+
985+
ares_addr_node* servers = new ares_addr_node[len];
986+
ares_addr_node* last = NULL;
987+
988+
uv_err_t uv_ret;
989+
990+
for (uint32_t i = 0; i < len; i++) {
991+
assert(arr->Get(i)->IsArray());
992+
993+
Local<Array> elm = Local<Array>::Cast(arr->Get(i));
994+
995+
assert(elm->Get(0)->Int32Value());
996+
assert(elm->Get(1)->IsString());
997+
998+
int fam = elm->Get(0)->Int32Value();
999+
String::Utf8Value ip(elm->Get(1));
1000+
1001+
ares_addr_node* cur = &servers[i];
1002+
1003+
switch (fam) {
1004+
case 4:
1005+
cur->family = AF_INET;
1006+
uv_ret = uv_inet_pton(AF_INET, *ip, &cur->addr);
1007+
break;
1008+
case 6:
1009+
cur->family = AF_INET6;
1010+
uv_ret = uv_inet_pton(AF_INET6, *ip, &cur->addr);
1011+
break;
1012+
}
1013+
1014+
if (uv_ret.code != UV_OK)
1015+
break;
1016+
1017+
cur->next = NULL;
1018+
1019+
if (last != NULL)
1020+
last->next = cur;
1021+
1022+
last = cur;
1023+
}
1024+
1025+
int r;
1026+
1027+
if (uv_ret.code == UV_OK)
1028+
r = ares_set_servers(ares_channel, &servers[0]);
1029+
else
1030+
r = ARES_EBADSTR;
1031+
1032+
delete[] servers;
1033+
1034+
return scope.Close(Integer::New(r));
1035+
}
1036+
1037+
1038+
static Handle<Value> StrError(const Arguments& args) {
1039+
HandleScope scope;
1040+
1041+
int r = args[0]->Int32Value();
1042+
1043+
return scope.Close(String::New(ares_strerror(r)));
1044+
}
1045+
1046+
9421047
static void Initialize(Handle<Object> target) {
9431048
HandleScope scope(node_isolate);
9441049
int r;
@@ -976,6 +1081,10 @@ static void Initialize(Handle<Object> target) {
9761081
NODE_SET_METHOD(target, "getaddrinfo", GetAddrInfo);
9771082
NODE_SET_METHOD(target, "isIP", IsIP);
9781083

1084+
NODE_SET_METHOD(target, "strerror", StrError);
1085+
NODE_SET_METHOD(target, "getServers", GetServers);
1086+
NODE_SET_METHOD(target, "setServers", SetServers);
1087+
9791088
target->Set(String::NewSymbol("AF_INET"),
9801089
Integer::New(AF_INET, node_isolate));
9811090
target->Set(String::NewSymbol("AF_INET6"),

test/simple/test-dns.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
25+
var dns = require('dns');
26+
27+
var existing = dns.getServers();
28+
assert(existing.length);
29+
30+
var goog = [
31+
'8.8.8.8',
32+
'8.8.4.4',
33+
];
34+
assert.doesNotThrow(function () { dns.setServers(goog) });
35+
assert.deepEqual(dns.getServers(), goog);
36+
assert.throws(function () { dns.setServers(['foobar']) });
37+
assert.deepEqual(dns.getServers(), goog);
38+
39+
var goog6 = [
40+
'2001:4860:4860::8888',
41+
'2001:4860:4860::8844',
42+
];
43+
assert.doesNotThrow(function () { dns.setServers(goog6) });
44+
assert.deepEqual(dns.getServers(), goog6);
45+
46+
goog6.push('4.4.4.4');
47+
dns.setServers(goog6);
48+
assert.deepEqual(dns.getServers(), goog6);
49+
50+
var ports = [
51+
'4.4.4.4:53',
52+
'[2001:4860:4860::8888]:53',
53+
];
54+
var portsExpected = [
55+
'4.4.4.4',
56+
'2001:4860:4860::8888',
57+
];
58+
dns.setServers(ports);
59+
assert.deepEqual(dns.getServers(), portsExpected);
60+
61+
assert.doesNotThrow(function () { dns.setServers([]); });
62+
assert.deepEqual(dns.getServers(), []);

0 commit comments

Comments
 (0)