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_dual_dns_resolver.h"
26
27 namespace shrpx {
28
DualDNSResolver(struct ev_loop * loop)29 DualDNSResolver::DualDNSResolver(struct ev_loop *loop)
30 : resolv4_(loop), resolv6_(loop) {
31 auto cb = [this](DNSResolverStatus, const Address *) {
32 Address result;
33
34 auto status = this->get_status(&result);
35 switch (status) {
36 case DNSResolverStatus::ERROR:
37 case DNSResolverStatus::OK:
38 break;
39 default:
40 return;
41 }
42
43 auto cb = this->get_complete_cb();
44 cb(status, &result);
45 };
46
47 resolv4_.set_complete_cb(cb);
48 resolv6_.set_complete_cb(cb);
49 }
50
resolve(const StringRef & host)51 int DualDNSResolver::resolve(const StringRef &host) {
52 int rv4, rv6;
53 rv4 = resolv4_.resolve(host, AF_INET);
54 rv6 = resolv6_.resolve(host, AF_INET6);
55
56 if (rv4 != 0 && rv6 != 0) {
57 return -1;
58 }
59
60 return 0;
61 }
62
get_complete_cb() const63 CompleteCb DualDNSResolver::get_complete_cb() const { return complete_cb_; }
64
set_complete_cb(CompleteCb cb)65 void DualDNSResolver::set_complete_cb(CompleteCb cb) { complete_cb_ = cb; }
66
get_status(Address * result) const67 DNSResolverStatus DualDNSResolver::get_status(Address *result) const {
68 auto rv6 = resolv6_.get_status(result);
69 if (rv6 == DNSResolverStatus::OK) {
70 return DNSResolverStatus::OK;
71 }
72 auto rv4 = resolv4_.get_status(result);
73 if (rv4 == DNSResolverStatus::OK) {
74 return DNSResolverStatus::OK;
75 }
76 if (rv4 == DNSResolverStatus::RUNNING || rv6 == DNSResolverStatus::RUNNING) {
77 return DNSResolverStatus::RUNNING;
78 }
79 if (rv4 == DNSResolverStatus::ERROR || rv6 == DNSResolverStatus::ERROR) {
80 return DNSResolverStatus::ERROR;
81 }
82 return DNSResolverStatus::IDLE;
83 }
84
85 } // namespace shrpx
86