summaryrefslogtreecommitdiffstats
path: root/src/shrpx_dual_dns_resolver.cc
blob: 8c6c5c9b308f1142a5d57f13b7ecc49eb72151ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * nghttp2 - HTTP/2 C Library
 *
 * Copyright (c) 2016 Tatsuhiro Tsujikawa
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#include "shrpx_dual_dns_resolver.h"

namespace shrpx {

DualDNSResolver::DualDNSResolver(struct ev_loop *loop, int family)
    : family_(family), resolv4_(loop), resolv6_(loop) {
  auto cb = [this](DNSResolverStatus, const Address *) {
    Address result;

    auto status = this->get_status(&result);
    switch (status) {
    case DNSResolverStatus::ERROR:
    case DNSResolverStatus::OK:
      break;
    default:
      return;
    }

    auto cb = this->get_complete_cb();
    cb(status, &result);
  };

  if (family_ == AF_UNSPEC || family_ == AF_INET) {
    resolv4_.set_complete_cb(cb);
  }
  if (family_ == AF_UNSPEC || family_ == AF_INET6) {
    resolv6_.set_complete_cb(cb);
  }
}

int DualDNSResolver::resolve(const StringRef &host) {
  int rv4 = 0, rv6 = 0;
  if (family_ == AF_UNSPEC || family_ == AF_INET) {
    rv4 = resolv4_.resolve(host, AF_INET);
  }
  if (family_ == AF_UNSPEC || family_ == AF_INET6) {
    rv6 = resolv6_.resolve(host, AF_INET6);
  }

  if (rv4 != 0 && rv6 != 0) {
    return -1;
  }

  return 0;
}

CompleteCb DualDNSResolver::get_complete_cb() const { return complete_cb_; }

void DualDNSResolver::set_complete_cb(CompleteCb cb) { complete_cb_ = cb; }

DNSResolverStatus DualDNSResolver::get_status(Address *result) const {
  auto rv6 = resolv6_.get_status(result);
  if (rv6 == DNSResolverStatus::OK) {
    return DNSResolverStatus::OK;
  }
  auto rv4 = resolv4_.get_status(result);
  if (rv4 == DNSResolverStatus::OK) {
    return DNSResolverStatus::OK;
  }
  if (rv4 == DNSResolverStatus::RUNNING || rv6 == DNSResolverStatus::RUNNING) {
    return DNSResolverStatus::RUNNING;
  }
  if (rv4 == DNSResolverStatus::ERROR || rv6 == DNSResolverStatus::ERROR) {
    return DNSResolverStatus::ERROR;
  }
  return DNSResolverStatus::IDLE;
}

} // namespace shrpx