UNPKG

4.83 kBJavaScriptView Raw
1/*
2Copyright 2011 Timothy J Fontaine <tjfontaine@gmail.com>
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"), to deal
6in the Software without restriction, including without limitation the rights
7to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8copies of the Software, and to permit persons to whom the Software is
9furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
21*/
22
23"use strict";
24
25var clone = require('clone'),
26 consts = require('./consts'),
27 Question = require('./question'),
28 platform = require('./platform'),
29 Request = require('./request');
30
31var _queue = [];
32
33var sendQueued = function () {
34 _queue.forEach(function (request) {
35 request.start();
36 });
37 _queue = [];
38};
39
40platform.on('ready', function () {
41 sendQueued();
42});
43
44if (platform.ready) {
45 sendQueued();
46}
47
48var Resolve = module.exports = function (opts, callback) {
49 this.domain = opts.domain;
50 this.rrtype = opts.rrtype;
51 this.check_hosts = opts.check_hosts;
52 this.callback = callback;
53
54 this.buildQuestion(this.domain);
55
56 this.started = false;
57 this.current_server = undefined;
58 this.server_list = [];
59
60 this.request = undefined;
61
62 if (!platform.ready) {
63 _queue.push(this);
64 } else {
65 this.start();
66 }
67};
68
69Resolve.prototype.cancel = function () {
70 if (this.request) {
71 this.request.cancel();
72 }
73};
74
75Resolve.prototype.buildQuestion = function (name) {
76 this.question = new Question();
77 this.question.type = this.rrtype;
78 this.question.class = consts.NAME_TO_QCLASS.IN;
79 this.question.name = name;
80};
81
82Resolve.prototype.isInHosts = function () {
83 var results;
84 if (platform.hosts[this.question.name]) {
85 results = platform.hosts[this.question.name];
86 this.callback(null, results);
87 return true;
88 } else {
89 return false;
90 }
91};
92
93Resolve.prototype.start = function () {
94 var tries = 0, s, slist,
95 self = this;
96
97 if (!this.started) {
98 this.started = true;
99 this.try_edns = platform.edns;
100 this.search_path = platform.search_path.slice(0);
101
102 slist = platform.name_servers;
103
104 while (this.server_list.length < platform.attempts) {
105 s = clone(slist[tries % slist.length]);
106 s.type = 'udp';
107 this.server_list.push(s);
108 s = clone(s);
109 s.type = 'tcp';
110 this.server_list.push(s);
111 tries += 1;
112 }
113 this.server_list.reverse();
114 }
115
116 if (this.check_hosts && this.isInHosts()) {
117 return;
118 }
119
120 if (this.server_list.length === 0) {
121 this.handleTimeout();
122 } else {
123 this.current_server = this.server_list.pop();
124 this.request = Request({
125 question: this.question,
126 server: this.current_server,
127 timeout: platform.timeout,
128 try_edns: this.try_edns,
129 });
130
131 this.request.on('timeout', function () {
132 self.handleTimeout();
133 });
134
135 this.request.on('message', function (err, answer) {
136 self.handle(err, answer);
137 });
138
139 this.request.send();
140 }
141};
142
143Resolve.prototype.handle = function (err, answer) {
144 var rcode, errno;
145
146 if (answer) {
147 rcode = answer.rcode;
148 }
149
150 switch (rcode) {
151 case consts.NAME_TO_RCODE.NOERROR:
152 break;
153 case consts.NAME_TO_RCODE.NOTFOUND:
154 if (this.server_list.length > 0 && this.search_path.length > 0) {
155 this.buildQuestion([this.domain, this.search_path.pop()].join('.'));
156 } else {
157 errno = consts.NOTFOUND;
158 }
159 answer = undefined;
160 break;
161 case consts.NAME_TO_RCODE.FORMERR:
162 if (this.try_edns) {
163 this.try_edns = false;
164 this.server_list.splice(0, 1, this.current_server);
165 } else {
166 errno = consts.FORMERR;
167 }
168 answer = undefined;
169 break;
170 default:
171 errno = consts.RCODE_TO_NAME[rcode];
172 answer = undefined;
173 break;
174 }
175
176 if (errno || answer) {
177 if (errno) {
178 err = new Error('getHostByName ' + errno);
179 err.errno = errno;
180 }
181 this.callback(err, answer);
182 } else {
183 this.start();
184 }
185};
186
187Resolve.prototype.handleTimeout = function () {
188 var err;
189
190 if (this.server_list.length === 0) {
191 err = new Error('getHostByName ' + consts.TIMEOUT);
192 err.errno = consts.TIMEOUT;
193 err.request = this;
194 this.callback(err, undefined);
195 } else {
196 this.start();
197 }
198};