1 /*
2 * nghttp2 - HTTP/2 C Library
3 *
4 * Copyright (c) 2016 Tatsuhiro Tsujikawa
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25 #include "shrpx_dns_tracker.h"
26 #include "shrpx_config.h"
27 #include "shrpx_log.h"
28 #include "util.h"
29
30 namespace shrpx {
31
32 namespace {
gccb(struct ev_loop * loop,ev_timer * w,int revents)33 void gccb(struct ev_loop *loop, ev_timer *w, int revents) {
34 auto dns_tracker = static_cast<DNSTracker *>(w->data);
35 dns_tracker->gc();
36 }
37 } // namespace
38
DNSTracker(struct ev_loop * loop)39 DNSTracker::DNSTracker(struct ev_loop *loop) : loop_(loop) {
40 ev_timer_init(&gc_timer_, gccb, 0., 12_h);
41 gc_timer_.data = this;
42 }
43
~DNSTracker()44 DNSTracker::~DNSTracker() {
45 ev_timer_stop(loop_, &gc_timer_);
46
47 for (auto &p : ents_) {
48 auto &qlist = p.second.qlist;
49 while (!qlist.empty()) {
50 auto head = qlist.head;
51 qlist.remove(head);
52 head->status = DNSResolverStatus::ERROR;
53 head->in_qlist = false;
54 // TODO Not sure we should call callback here, or it is even be
55 // safe to do that.
56 }
57 }
58 }
59
make_entry(std::unique_ptr<DualDNSResolver> resolv,ImmutableString host,DNSResolverStatus status,const Address * result)60 ResolverEntry DNSTracker::make_entry(std::unique_ptr<DualDNSResolver> resolv,
61 ImmutableString host,
62 DNSResolverStatus status,
63 const Address *result) {
64 auto &dnsconf = get_config()->dns;
65
66 auto ent = ResolverEntry{};
67 ent.resolv = std::move(resolv);
68 ent.host = std::move(host);
69 ent.status = status;
70 switch (status) {
71 case DNSResolverStatus::ERROR:
72 case DNSResolverStatus::OK:
73 ent.expiry = ev_now(loop_) + dnsconf.timeout.cache;
74 break;
75 default:
76 break;
77 }
78 if (result) {
79 ent.result = *result;
80 }
81 return ent;
82 }
83
update_entry(ResolverEntry & ent,std::unique_ptr<DualDNSResolver> resolv,DNSResolverStatus status,const Address * result)84 void DNSTracker::update_entry(ResolverEntry &ent,
85 std::unique_ptr<DualDNSResolver> resolv,
86 DNSResolverStatus status, const Address *result) {
87 auto &dnsconf = get_config()->dns;
88
89 ent.resolv = std::move(resolv);
90 ent.status = status;
91 switch (status) {
92 case DNSResolverStatus::ERROR:
93 case DNSResolverStatus::OK:
94 ent.expiry = ev_now(loop_) + dnsconf.timeout.cache;
95 break;
96 default:
97 break;
98 }
99 if (result) {
100 ent.result = *result;
101 }
102 }
103
resolve(Address * result,DNSQuery * dnsq)104 DNSResolverStatus DNSTracker::resolve(Address *result, DNSQuery *dnsq) {
105 int rv;
106
107 auto it = ents_.find(dnsq->host);
108
109 if (it == std::end(ents_)) {
110 if (LOG_ENABLED(INFO)) {
111 LOG(INFO) << "DNS entry not found for " << dnsq->host;
112 }
113
114 auto resolv = std::make_unique<DualDNSResolver>(loop_);
115 auto host_copy =
116 ImmutableString{std::begin(dnsq->host), std::end(dnsq->host)};
117 auto host = StringRef{host_copy};
118
119 rv = resolv->resolve(host);
120 if (rv != 0) {
121 if (LOG_ENABLED(INFO)) {
122 LOG(INFO) << "Name lookup failed for " << host;
123 }
124
125 ents_.emplace(host, make_entry(nullptr, std::move(host_copy),
126 DNSResolverStatus::ERROR, nullptr));
127
128 start_gc_timer();
129
130 return DNSResolverStatus::ERROR;
131 }
132
133 switch (resolv->get_status(result)) {
134 case DNSResolverStatus::ERROR:
135 if (LOG_ENABLED(INFO)) {
136 LOG(INFO) << "Name lookup failed for " << host;
137 }
138
139 ents_.emplace(host, make_entry(nullptr, std::move(host_copy),
140 DNSResolverStatus::ERROR, nullptr));
141
142 start_gc_timer();
143
144 return DNSResolverStatus::ERROR;
145 case DNSResolverStatus::OK:
146 if (LOG_ENABLED(INFO)) {
147 LOG(INFO) << "Name lookup succeeded: " << host << " -> "
148 << util::numeric_name(&result->su.sa, result->len);
149 }
150
151 ents_.emplace(host, make_entry(nullptr, std::move(host_copy),
152 DNSResolverStatus::OK, result));
153
154 start_gc_timer();
155
156 return DNSResolverStatus::OK;
157 case DNSResolverStatus::RUNNING: {
158 auto p = ents_.emplace(host,
159 make_entry(std::move(resolv), std::move(host_copy),
160 DNSResolverStatus::RUNNING, nullptr));
161
162 start_gc_timer();
163
164 auto &ent = (*p.first).second;
165
166 add_to_qlist(ent, dnsq);
167
168 return DNSResolverStatus::RUNNING;
169 }
170 default:
171 assert(0);
172 }
173 }
174
175 auto &ent = (*it).second;
176
177 if (ent.status != DNSResolverStatus::RUNNING && ent.expiry < ev_now(loop_)) {
178 if (LOG_ENABLED(INFO)) {
179 LOG(INFO) << "DNS entry found for " << dnsq->host
180 << ", but it has been expired";
181 }
182
183 auto resolv = std::make_unique<DualDNSResolver>(loop_);
184 auto host = StringRef{ent.host};
185
186 rv = resolv->resolve(host);
187 if (rv != 0) {
188 if (LOG_ENABLED(INFO)) {
189 LOG(INFO) << "Name lookup failed for " << host;
190 }
191
192 update_entry(ent, nullptr, DNSResolverStatus::ERROR, nullptr);
193
194 return DNSResolverStatus::ERROR;
195 }
196
197 switch (resolv->get_status(result)) {
198 case DNSResolverStatus::ERROR:
199 if (LOG_ENABLED(INFO)) {
200 LOG(INFO) << "Name lookup failed for " << host;
201 }
202
203 update_entry(ent, nullptr, DNSResolverStatus::ERROR, nullptr);
204
205 return DNSResolverStatus::ERROR;
206 case DNSResolverStatus::OK:
207 if (LOG_ENABLED(INFO)) {
208 LOG(INFO) << "Name lookup succeeded: " << host << " -> "
209 << util::numeric_name(&result->su.sa, result->len);
210 }
211
212 update_entry(ent, nullptr, DNSResolverStatus::OK, result);
213
214 return DNSResolverStatus::OK;
215 case DNSResolverStatus::RUNNING:
216 update_entry(ent, std::move(resolv), DNSResolverStatus::RUNNING, nullptr);
217 add_to_qlist(ent, dnsq);
218
219 return DNSResolverStatus::RUNNING;
220 default:
221 assert(0);
222 }
223 }
224
225 switch (ent.status) {
226 case DNSResolverStatus::RUNNING:
227 if (LOG_ENABLED(INFO)) {
228 LOG(INFO) << "Waiting for name lookup complete for " << dnsq->host;
229 }
230 ent.qlist.append(dnsq);
231 dnsq->in_qlist = true;
232 return DNSResolverStatus::RUNNING;
233 case DNSResolverStatus::ERROR:
234 if (LOG_ENABLED(INFO)) {
235 LOG(INFO) << "Name lookup failed for " << dnsq->host << " (cached)";
236 }
237 return DNSResolverStatus::ERROR;
238 case DNSResolverStatus::OK:
239 if (LOG_ENABLED(INFO)) {
240 LOG(INFO) << "Name lookup succeeded (cached): " << dnsq->host << " -> "
241 << util::numeric_name(&ent.result.su.sa, ent.result.len);
242 }
243 if (result) {
244 memcpy(result, &ent.result, sizeof(*result));
245 }
246 return DNSResolverStatus::OK;
247 default:
248 assert(0);
249 abort();
250 }
251 }
252
add_to_qlist(ResolverEntry & ent,DNSQuery * dnsq)253 void DNSTracker::add_to_qlist(ResolverEntry &ent, DNSQuery *dnsq) {
254 auto loop = loop_;
255 ent.resolv->set_complete_cb(
256 [&ent, loop](DNSResolverStatus status, const Address *result) {
257 auto &qlist = ent.qlist;
258 while (!qlist.empty()) {
259 auto head = qlist.head;
260 qlist.remove(head);
261 head->status = status;
262 head->in_qlist = false;
263 auto cb = head->cb;
264 cb(status, result);
265 }
266
267 auto &dnsconf = get_config()->dns;
268
269 ent.resolv.reset();
270 ent.status = status;
271 ent.expiry = ev_now(loop) + dnsconf.timeout.cache;
272 if (ent.status == DNSResolverStatus::OK) {
273 ent.result = *result;
274 }
275 });
276 ent.qlist.append(dnsq);
277 dnsq->in_qlist = true;
278 }
279
cancel(DNSQuery * dnsq)280 void DNSTracker::cancel(DNSQuery *dnsq) {
281 if (!dnsq->in_qlist) {
282 return;
283 }
284
285 auto it = ents_.find(dnsq->host);
286 if (it == std::end(ents_)) {
287 return;
288 }
289
290 auto &ent = (*it).second;
291 ent.qlist.remove(dnsq);
292 dnsq->in_qlist = false;
293 }
294
start_gc_timer()295 void DNSTracker::start_gc_timer() {
296 if (ev_is_active(&gc_timer_)) {
297 return;
298 }
299
300 ev_timer_again(loop_, &gc_timer_);
301 }
302
gc()303 void DNSTracker::gc() {
304 if (LOG_ENABLED(INFO)) {
305 LOG(INFO) << "Starting removing expired DNS cache entries";
306 }
307
308 auto now = ev_now(loop_);
309 for (auto it = std::begin(ents_); it != std::end(ents_);) {
310 auto &ent = (*it).second;
311 if (ent.expiry >= now) {
312 ++it;
313 continue;
314 }
315
316 it = ents_.erase(it);
317 }
318
319 if (ents_.empty()) {
320 ev_timer_stop(loop_, &gc_timer_);
321 }
322 }
323
324 } // namespace shrpx
325