summaryrefslogtreecommitdiff
path: root/lib/dns.js
diff options
context:
space:
mode:
authorTimothy J Fontaine <tjfontaine@gmail.com>2013-05-08 12:56:08 -0700
committerTimothy J Fontaine <tjfontaine@gmail.com>2013-05-14 14:15:24 -0700
commit8886c6bf624e6627f68a10c5f4c6399bb5257cdf (patch)
tree1bb2c2e010b8e0891f92c002989fcf98bb8d00f9 /lib/dns.js
parent9498fd15c70c2daf386dda13c43d7a2d984f0892 (diff)
downloadnode-new-8886c6bf624e6627f68a10c5f4c6399bb5257cdf.tar.gz
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
Diffstat (limited to 'lib/dns.js')
-rw-r--r--lib/dns.js49
1 files changed, 49 insertions, 0 deletions
diff --git a/lib/dns.js b/lib/dns.js
index 10214790de..e9611a3a71 100644
--- a/lib/dns.js
+++ b/lib/dns.js
@@ -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';