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,int family)29 DualDNSResolver::DualDNSResolver(struct ev_loop *loop, int family)
30 : family_(family), 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 if (family_ == AF_UNSPEC || family_ == AF_INET) {
48 resolv4_.set_complete_cb(cb);
49 }
50 if (family_ == AF_UNSPEC || family_ == AF_INET6) {
51 resolv6_.set_complete_cb(cb);
52 }
53 }
54
resolve(const StringRef & host)55 int DualDNSResolver::resolve(const StringRef &host) {
56 int rv4 = 0, rv6 = 0;
57 if (family_ == AF_UNSPEC || family_ == AF_INET) {
58 rv4 = resolv4_.resolve(host, AF_INET);
59 }
60 if (family_ == AF_UNSPEC || family_ == AF_INET6) {
61 rv6 = resolv6_.resolve(host, AF_INET6);
62 }
63
64 if (rv4 != 0 && rv6 != 0) {
65 return -1;
66 }
67
68 return 0;
69 }
70
get_complete_cb() const71 CompleteCb DualDNSResolver::get_complete_cb() const { return complete_cb_; }
72
set_complete_cb(CompleteCb cb)73 void DualDNSResolver::set_complete_cb(CompleteCb cb) { complete_cb_ = cb; }
74
get_status(Address * result) const75 DNSResolverStatus DualDNSResolver::get_status(Address *result) const {
76 auto rv6 = resolv6_.get_status(result);
77 if (rv6 == DNSResolverStatus::OK) {
78 return DNSResolverStatus::OK;
79 }
80 auto rv4 = resolv4_.get_status(result);
81 if (rv4 == DNSResolverStatus::OK) {
82 return DNSResolverStatus::OK;
83 }
84 if (rv4 == DNSResolverStatus::RUNNING || rv6 == DNSResolverStatus::RUNNING) {
85 return DNSResolverStatus::RUNNING;
86 }
87 if (rv4 == DNSResolverStatus::ERROR || rv6 == DNSResolverStatus::ERROR) {
88 return DNSResolverStatus::ERROR;
89 }
90 return DNSResolverStatus::IDLE;
91 }
92
93 } // namespace shrpx
94