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,int family)39 DNSTracker::DNSTracker(struct ev_loop *loop, int family)
40 : loop_(loop), family_(family) {
41 ev_timer_init(&gc_timer_, gccb, 0., 12_h);
42 gc_timer_.data = this;
43 }
44
~DNSTracker()45 DNSTracker::~DNSTracker() {
46 ev_timer_stop(loop_, &gc_timer_);
47
48 for (auto &p : ents_) {
49 auto &qlist = p.second.qlist;
50 while (!qlist.empty()) {
51 auto head = qlist.head;
52 qlist.remove(head);
53 head->status = DNSResolverStatus::ERROR;
54 head->in_qlist = false;
55 // TODO Not sure we should call callback here, or it is even be
56 // safe to do that.
57 }
58 }
59 }
60
make_entry(std::unique_ptr<DualDNSResolver> resolv,ImmutableString host,DNSResolverStatus status,const Address * result)61 ResolverEntry DNSTracker::make_entry(std::unique_ptr<DualDNSResolver> resolv,
62 ImmutableString host,
63 DNSResolverStatus status,
64 const Address *result) {
65 auto &dnsconf = get_config()->dns;
66
67 auto ent = ResolverEntry{};
68 ent.resolv = std::move(resolv);
69 ent.host = std::move(host);
70 ent.status = status;
71 switch (status) {
72 case DNSResolverStatus::ERROR:
73 case DNSResolverStatus::OK:
74 ent.expiry = ev_now(loop_) + dnsconf.timeout.cache;
75 break;
76 default:
77 break;
78 }
79 if (result) {
80 ent.result = *result;
81 }
82 return ent;
83 }
84
update_entry(ResolverEntry & ent,std::unique_ptr<DualDNSResolver> resolv,DNSResolverStatus status,const Address * result)85 void DNSTracker::update_entry(ResolverEntry &ent,
86 std::unique_ptr<DualDNSResolver> resolv,
87 DNSResolverStatus status, const Address *result) {
88 auto &dnsconf = get_config()->dns;
89
90 ent.resolv = std::move(resolv);
91 ent.status = status;
92 switch (status) {
93 case DNSResolverStatus::ERROR:
94 case DNSResolverStatus::OK:
95 ent.expiry = ev_now(loop_) + dnsconf.timeout.cache;
96 break;
97 default:
98 break;
99 }
100 if (result) {
101 ent.result = *result;
102 }
103 }
104
resolve(Address * result,DNSQuery * dnsq)105 DNSResolverStatus DNSTracker::resolve(Address *result, DNSQuery *dnsq) {
106 int rv;
107
108 auto it = ents_.find(dnsq->host);
109
110 if (it == std::end(ents_)) {
111 if (LOG_ENABLED(INFO)) {
112 LOG(INFO) << "DNS entry not found for " << dnsq->host;
113 }
114
115 auto resolv = std::make_unique<DualDNSResolver>(loop_, family_);
116 auto host_copy =
117 ImmutableString{std::begin(dnsq->host), std::end(dnsq->host)};
118 auto host = StringRef{host_copy};
119
120 rv = resolv->resolve(host);
121 if (rv != 0) {
122 if (LOG_ENABLED(INFO)) {
123 LOG(INFO) << "Name lookup failed for " << host;
124 }
125
126 ents_.emplace(host, make_entry(nullptr, std::move(host_copy),
127 DNSResolverStatus::ERROR, nullptr));
128
129 start_gc_timer();
130
131 return DNSResolverStatus::ERROR;
132 }
133
134 switch (resolv->get_status(result)) {
135 case DNSResolverStatus::ERROR:
136 if (LOG_ENABLED(INFO)) {
137 LOG(INFO) << "Name lookup failed for " << host;
138 }
139
140 ents_.emplace(host, make_entry(nullptr, std::move(host_copy),
141 DNSResolverStatus::ERROR, nullptr));
142
143 start_gc_timer();
144
145 return DNSResolverStatus::ERROR;
146 case DNSResolverStatus::OK:
147 if (LOG_ENABLED(INFO)) {
148 LOG(INFO) << "Name lookup succeeded: " << host << " -> "
149 << util::numeric_name(&result->su.sa, result->len);
150 }
151
152 ents_.emplace(host, make_entry(nullptr, std::move(host_copy),
153 DNSResolverStatus::OK, result));
154
155 start_gc_timer();
156
157 return DNSResolverStatus::OK;
158 case DNSResolverStatus::RUNNING: {
159 auto p = ents_.emplace(host,
160 make_entry(std::move(resolv), std::move(host_copy),
161 DNSResolverStatus::RUNNING, nullptr));
162
163 start_gc_timer();
164
165 auto &ent = (*p.first).second;
166
167 add_to_qlist(ent, dnsq);
168
169 return DNSResolverStatus::RUNNING;
170 }
171 default:
172 assert(0);
173 }
174 }
175
176 auto &ent = (*it).second;
177
178 if (ent.status != DNSResolverStatus::RUNNING && ent.expiry < ev_now(loop_)) {
179 if (LOG_ENABLED(INFO)) {
180 LOG(INFO) << "DNS entry found for " << dnsq->host
181 << ", but it has been expired";
182 }
183
184 auto resolv = std::make_unique<DualDNSResolver>(loop_, family_);
185 auto host = StringRef{ent.host};
186
187 rv = resolv->resolve(host);
188 if (rv != 0) {
189 if (LOG_ENABLED(INFO)) {
190 LOG(INFO) << "Name lookup failed for " << host;
191 }
192
193 update_entry(ent, nullptr, DNSResolverStatus::ERROR, nullptr);
194
195 return DNSResolverStatus::ERROR;
196 }
197
198 switch (resolv->get_status(result)) {
199 case DNSResolverStatus::ERROR:
200 if (LOG_ENABLED(INFO)) {
201 LOG(INFO) << "Name lookup failed for " << host;
202 }
203
204 update_entry(ent, nullptr, DNSResolverStatus::ERROR, nullptr);
205
206 return DNSResolverStatus::ERROR;
207 case DNSResolverStatus::OK:
208 if (LOG_ENABLED(INFO)) {
209 LOG(INFO) << "Name lookup succeeded: " << host << " -> "
210 << util::numeric_name(&result->su.sa, result->len);
211 }
212
213 update_entry(ent, nullptr, DNSResolverStatus::OK, result);
214
215 return DNSResolverStatus::OK;
216 case DNSResolverStatus::RUNNING:
217 update_entry(ent, std::move(resolv), DNSResolverStatus::RUNNING, nullptr);
218 add_to_qlist(ent, dnsq);
219
220 return DNSResolverStatus::RUNNING;
221 default:
222 assert(0);
223 }
224 }
225
226 switch (ent.status) {
227 case DNSResolverStatus::RUNNING:
228 if (LOG_ENABLED(INFO)) {
229 LOG(INFO) << "Waiting for name lookup complete for " << dnsq->host;
230 }
231 ent.qlist.append(dnsq);
232 dnsq->in_qlist = true;
233 return DNSResolverStatus::RUNNING;
234 case DNSResolverStatus::ERROR:
235 if (LOG_ENABLED(INFO)) {
236 LOG(INFO) << "Name lookup failed for " << dnsq->host << " (cached)";
237 }
238 return DNSResolverStatus::ERROR;
239 case DNSResolverStatus::OK:
240 if (LOG_ENABLED(INFO)) {
241 LOG(INFO) << "Name lookup succeeded (cached): " << dnsq->host << " -> "
242 << util::numeric_name(&ent.result.su.sa, ent.result.len);
243 }
244 if (result) {
245 memcpy(result, &ent.result, sizeof(*result));
246 }
247 return DNSResolverStatus::OK;
248 default:
249 assert(0);
250 abort();
251 }
252 }
253
add_to_qlist(ResolverEntry & ent,DNSQuery * dnsq)254 void DNSTracker::add_to_qlist(ResolverEntry &ent, DNSQuery *dnsq) {
255 auto loop = loop_;
256 ent.resolv->set_complete_cb(
257 [&ent, loop](DNSResolverStatus status, const Address *result) {
258 auto &qlist = ent.qlist;
259 while (!qlist.empty()) {
260 auto head = qlist.head;
261 qlist.remove(head);
262 head->status = status;
263 head->in_qlist = false;
264 auto cb = head->cb;
265 cb(status, result);
266 }
267
268 auto &dnsconf = get_config()->dns;
269
270 ent.resolv.reset();
271 ent.status = status;
272 ent.expiry = ev_now(loop) + dnsconf.timeout.cache;
273 if (ent.status == DNSResolverStatus::OK) {
274 ent.result = *result;
275 }
276 });
277 ent.qlist.append(dnsq);
278 dnsq->in_qlist = true;
279 }
280
cancel(DNSQuery * dnsq)281 void DNSTracker::cancel(DNSQuery *dnsq) {
282 if (!dnsq->in_qlist) {
283 return;
284 }
285
286 auto it = ents_.find(dnsq->host);
287 if (it == std::end(ents_)) {
288 return;
289 }
290
291 auto &ent = (*it).second;
292 ent.qlist.remove(dnsq);
293 dnsq->in_qlist = false;
294 }
295
start_gc_timer()296 void DNSTracker::start_gc_timer() {
297 if (ev_is_active(&gc_timer_)) {
298 return;
299 }
300
301 ev_timer_again(loop_, &gc_timer_);
302 }
303
gc()304 void DNSTracker::gc() {
305 if (LOG_ENABLED(INFO)) {
306 LOG(INFO) << "Starting removing expired DNS cache entries";
307 }
308
309 auto now = ev_now(loop_);
310 for (auto it = std::begin(ents_); it != std::end(ents_);) {
311 auto &ent = (*it).second;
312 if (ent.expiry >= now) {
313 ++it;
314 continue;
315 }
316
317 it = ents_.erase(it);
318 }
319
320 if (ents_.empty()) {
321 ev_timer_stop(loop_, &gc_timer_);
322 }
323 }
324
325 } // namespace shrpx
326