-
Notifications
You must be signed in to change notification settings - Fork 29.6k
/
dns.js
355 lines (289 loc) Β· 9.26 KB
/
dns.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
'use strict';
const net = require('net');
const util = require('util');
const cares = process.binding('cares_wrap');
const uv = process.binding('uv');
const GetAddrInfoReqWrap = cares.GetAddrInfoReqWrap;
const GetNameInfoReqWrap = cares.GetNameInfoReqWrap;
const isIp = net.isIP;
function errnoException(err, syscall, hostname) {
// FIXME(bnoordhuis) Remove this backwards compatibility shite and pass
// the true error to the user. ENOTFOUND is not even a proper POSIX error!
if (err === uv.UV_EAI_MEMORY ||
err === uv.UV_EAI_NODATA ||
err === uv.UV_EAI_NONAME) {
err = 'ENOTFOUND';
}
var ex = null;
if (typeof err === 'string') { // c-ares error code.
ex = new Error(syscall + ' ' + err + (hostname ? ' ' + hostname : ''));
ex.code = err;
ex.errno = err;
ex.syscall = syscall;
} else {
ex = util._errnoException(err, syscall);
}
if (hostname) {
ex.hostname = hostname;
}
return ex;
}
// c-ares invokes a callback either synchronously or asynchronously,
// but the dns API should always invoke a callback asynchronously.
//
// This function makes sure that the callback is invoked asynchronously.
// It returns a function that invokes the callback within nextTick().
//
// To avoid invoking unnecessary nextTick(), `immediately` property of
// returned function should be set to true after c-ares returned.
//
// Usage:
//
// function someAPI(callback) {
// callback = makeAsync(callback);
// channel.someAPI(..., callback);
// callback.immediately = true;
// }
function makeAsync(callback) {
if (typeof callback !== 'function') {
return callback;
}
return function asyncCallback() {
if (asyncCallback.immediately) {
// The API already returned, we can invoke the callback immediately.
callback.apply(null, arguments);
} else {
process.nextTick(callMakeAsyncCbNT, callback, arguments);
}
};
}
function callMakeAsyncCbNT(callback, args) {
callback.apply(null, args);
}
function onlookup(err, addresses) {
if (err) {
return this.callback(errnoException(err, 'getaddrinfo', this.hostname));
}
if (this.family) {
this.callback(null, addresses[0], this.family);
} else {
this.callback(null, addresses[0], addresses[0].indexOf(':') >= 0 ? 6 : 4);
}
}
function onlookupall(err, addresses) {
var results = [];
if (err) {
return this.callback(errnoException(err, 'getaddrinfo', this.hostname));
}
for (var i = 0; i < addresses.length; i++) {
results.push({
address: addresses[i],
family: this.family || (addresses[i].indexOf(':') >= 0 ? 6 : 4)
});
}
this.callback(null, results);
}
// Easy DNS A/AAAA look up
// lookup(hostname, [options,] callback)
exports.lookup = function lookup(hostname, options, callback) {
var hints = 0;
var family = -1;
var all = false;
// Parse arguments
if (hostname && typeof hostname !== 'string') {
throw new TypeError('invalid arguments: ' +
'hostname must be a string or falsey');
} else if (typeof options === 'function') {
callback = options;
family = 0;
} else if (typeof callback !== 'function') {
throw new TypeError('invalid arguments: callback must be passed');
} else if (options !== null && typeof options === 'object') {
hints = options.hints >>> 0;
family = options.family >>> 0;
all = options.all === true;
if (hints !== 0 &&
hints !== exports.ADDRCONFIG &&
hints !== exports.V4MAPPED &&
hints !== (exports.ADDRCONFIG | exports.V4MAPPED)) {
throw new TypeError('invalid argument: hints must use valid flags');
}
// FIXME(indutny): V4MAPPED on FreeBSD results in EAI_BADFLAGS, because
// the libc does not support it
if (process.platform === 'freebsd' && family !== 6)
hints &= ~exports.V4MAPPED;
} else {
family = options >>> 0;
}
if (family !== 0 && family !== 4 && family !== 6)
throw new TypeError('invalid argument: family must be 4 or 6');
callback = makeAsync(callback);
if (!hostname) {
if (all) {
callback(null, []);
} else {
callback(null, null, family === 6 ? 6 : 4);
}
return {};
}
var matchedFamily = net.isIP(hostname);
if (matchedFamily) {
if (all) {
callback(null, [{address: hostname, family: matchedFamily}]);
} else {
callback(null, hostname, matchedFamily);
}
return {};
}
var req = new GetAddrInfoReqWrap();
req.callback = callback;
req.family = family;
req.hostname = hostname;
req.oncomplete = all ? onlookupall : onlookup;
var err = cares.getaddrinfo(req, hostname, family, hints);
if (err) {
callback(errnoException(err, 'getaddrinfo', hostname));
return {};
}
callback.immediately = true;
return req;
};
function onlookupservice(err, host, service) {
if (err)
return this.callback(errnoException(err, 'getnameinfo', this.host));
this.callback(null, host, service);
}
// lookupService(address, port, callback)
exports.lookupService = function(host, port, callback) {
if (arguments.length !== 3)
throw new Error('invalid arguments');
if (cares.isIP(host) === 0)
throw new TypeError('host needs to be a valid IP address');
callback = makeAsync(callback);
var req = new GetNameInfoReqWrap();
req.callback = callback;
req.host = host;
req.port = port;
req.oncomplete = onlookupservice;
var err = cares.getnameinfo(req, host, port);
if (err) throw errnoException(err, 'getnameinfo', host);
callback.immediately = true;
return req;
};
function onresolve(err, result) {
if (err)
this.callback(errnoException(err, this.bindingName, this.hostname));
else
this.callback(null, result);
}
function resolver(bindingName) {
var binding = cares[bindingName];
return function query(name, callback) {
if (typeof name !== 'string') {
throw new Error('Name must be a string');
} else if (typeof callback !== 'function') {
throw new Error('Callback must be a function');
}
callback = makeAsync(callback);
var req = {
bindingName: bindingName,
callback: callback,
hostname: name,
oncomplete: onresolve
};
var err = binding(req, name);
if (err) throw errnoException(err, bindingName);
callback.immediately = true;
return req;
}
}
var resolveMap = {};
exports.resolve4 = resolveMap.A = resolver('queryA');
exports.resolve6 = resolveMap.AAAA = resolver('queryAaaa');
exports.resolveCname = resolveMap.CNAME = resolver('queryCname');
exports.resolveMx = resolveMap.MX = resolver('queryMx');
exports.resolveNs = resolveMap.NS = resolver('queryNs');
exports.resolveTxt = resolveMap.TXT = resolver('queryTxt');
exports.resolveSrv = resolveMap.SRV = resolver('querySrv');
exports.resolveNaptr = resolveMap.NAPTR = resolver('queryNaptr');
exports.resolveSoa = resolveMap.SOA = resolver('querySoa');
exports.reverse = resolveMap.PTR = resolver('getHostByAddr');
exports.resolve = function(hostname, type_, callback_) {
var resolver, callback;
if (typeof type_ === 'string') {
resolver = resolveMap[type_];
callback = callback_;
} else if (typeof type_ === 'function') {
resolver = exports.resolve4;
callback = type_;
} else {
throw new Error('Type must be a string');
}
if (typeof resolver === 'function') {
return resolver(hostname, callback);
} else {
throw new Error('Unknown type "' + type_ + '"');
}
};
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 + ']');
}
};
// uv_getaddrinfo flags
exports.ADDRCONFIG = cares.AI_ADDRCONFIG;
exports.V4MAPPED = cares.AI_V4MAPPED;
// ERROR CODES
exports.NODATA = 'ENODATA';
exports.FORMERR = 'EFORMERR';
exports.SERVFAIL = 'ESERVFAIL';
exports.NOTFOUND = 'ENOTFOUND';
exports.NOTIMP = 'ENOTIMP';
exports.REFUSED = 'EREFUSED';
exports.BADQUERY = 'EBADQUERY';
exports.ADNAME = 'EADNAME';
exports.BADFAMILY = 'EBADFAMILY';
exports.BADRESP = 'EBADRESP';
exports.CONNREFUSED = 'ECONNREFUSED';
exports.TIMEOUT = 'ETIMEOUT';
exports.EOF = 'EOF';
exports.FILE = 'EFILE';
exports.NOMEM = 'ENOMEM';
exports.DESTRUCTION = 'EDESTRUCTION';
exports.BADSTR = 'EBADSTR';
exports.BADFLAGS = 'EBADFLAGS';
exports.NONAME = 'ENONAME';
exports.BADHINTS = 'EBADHINTS';
exports.NOTINITIALIZED = 'ENOTINITIALIZED';
exports.LOADIPHLPAPI = 'ELOADIPHLPAPI';
exports.ADDRGETNETWORKPARAMS = 'EADDRGETNETWORKPARAMS';
exports.CANCELLED = 'ECANCELLED';