forked from agnat/node_mdns
-
Notifications
You must be signed in to change notification settings - Fork 6
/
resolver_sequence_tasks.js
194 lines (176 loc) · 5.42 KB
/
resolver_sequence_tasks.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
var dns_sd = require('./dns_sd')
, util = require('util')
, MDNSService = require('./mdns_service').MDNSService
;
exports.DNSServiceResolve = function DNSServiceResolve(options) {
options = options || {};
options.flags = options.flags || 0;
options.unwrapTxtRecord =
'unwrapTxtRecord' in options ? options.unwrapTxtRecord : true;
return function DNSServiceResolve(service, next) {
try {
var resolver = new MDNSService();
function on_resolver_done(sdRef, flags, iface, errorCode, fullname,
hosttarget, port, txtRecord, context)
{
try {
var error = dns_sd.buildException(errorCode);
if ( ! error && service.interfaceIndex === iface) {
if (fullname) service.fullname = fullname;
if (hosttarget) service.host = hosttarget;
if (port) service.port = port;
// all services have a TXT record. ignore the empty ones.
if (txtRecord.length > 1) {
service.rawTxtRecord = txtRecord;
if (options.unwrapTxtRecord) {
service.txtRecord = dns_sd.txtRecordBufferToObject(txtRecord)
}
}
}
resolver.stop();
next(error);
} catch (ex) {
resolver.stop();
next(ex);
}
}
dns_sd.DNSServiceResolve(resolver.serviceRef, options.flags,
service.interfaceIndex, service.name, '' + service.type,
service.replyDomain, on_resolver_done, null);
resolver.start();
} catch (ex) {
resolver.stop();
next(ex);
}
};
}
exports.DNSServiceGetAddrInfo = function DNSServiceGetAddrInfo(options) {
options = options || {};
var family_flags = 0;
if ('families' in options) {
if (options.families.indexOf(4) !== -1) {
family_flags |= dns_sd.kDNSServiceProtocol_IPv4;
}
if (options.families.indexOf(6) !== -1) {
family_flags |= dns_sd.kDNSServiceProtocol_IPv6;
}
}
return function DNSServiceGetAddrInfo(service, next) {
try {
var adr_getter = new MDNSService()
, addresses = []
;
function on_get_addr_info_done(sdRef, flags, iface, errorCode, hostname,
address, context)
{
try {
var error = dns_sd.buildException(errorCode);
if (error) {
adr_getter.stop()
next(error);
} else {
if (iface === service.interfaceIndex) {
addresses.push(address);
}
if ( ! (dns_sd.kDNSServiceFlagsMoreComing & flags)) {
service.addresses = addresses;
adr_getter.stop()
next();
}
}
} catch (ex) {
adr_getter.stop();
next(ex);
}
}
dns_sd.DNSServiceGetAddrInfo(
adr_getter.serviceRef, 0, service.interfaceIndex, family_flags,
service.host, on_get_addr_info_done, null);
adr_getter.start();
} catch (ex) {
adr_getter.stop();
next(ex);
}
}
}
var _getaddrinfo;
try {
var cares = process.binding('cares_wrap');
_getaddrinfo = function getaddrinfo_v06x(host, family, cb) {
var wrap = cares.getaddrinfo(host, family);
if ( ! wrap) {
throw errnoException(process._errno || global.errno, 'getaddrinfo');
}
wrap.oncomplete = function(addresses) {
var errno = process._errno || global.errno;
if (addresses) {
cb(undefined, addresses);
} else if (errno === 'ENOENT') {
cb(undefined, []);
} else {
cb(errnoException(errno, 'getaddrinfo'));
}
}
};
} catch (ex) {
_getaddrinfo = process.binding('net').getaddrinfo;
}
exports.getaddrinfo = function getaddrinfo(options) {
options = options || {};
var families = options.families || [4, 6];
return function getaddrinfo(service, next) {
var last_error
, counter = 0
;
// XXX in older versions of node a zero family value is not supported
families.forEach(function(family) {
_getaddrinfo(service.host, family, function(error, addresses) {
if (error) {
last_error = error
} else {
service.addresses = (service.addresses || []).concat(addresses);
}
if (++counter === families.length) {
next(last_error);
}
});
});
}
}
function unique(array) {
var o = {} , p , r = [] ;
array.forEach(function(e) { o[e] = undefined });
for (p in o) { r.push(p) }
return r;
}
exports.makeAddressesUnique = function makeAddressesUnique() {
return function makeAddressesUnique(service, next) {
service.addresses = unique(service.addresses);
next();
}
}
exports.filterAddresses = function filterAddresses(filter_function) {
return function filterAddresses(service, next) {
service.addresses = (service.addresses || []).filter(filter_function);
next();
}
}
exports.logService = function logService() {
return function logService(service, next) {
console.log(service);
next();
}
}
function errnoException(errorno, syscall) {
// TODO make this more compatible with ErrnoException from src/node.cc
// Once all of Node is using this function the ErrnoException from
// src/node.cc should be removed.
var e = new Error(syscall + ' ' + errorno);
// For backwards compatibility. libuv returns ENOENT on NXDOMAIN.
if (errorno == 'ENOENT') {
errorno = 'ENOTFOUND'
}
e.errno = e.code = errorno;
e.syscall = syscall;
return e;
}