12c593315Sopenharmony_ci/* 22c593315Sopenharmony_ci * nghttp2 - HTTP/2 C Library 32c593315Sopenharmony_ci * 42c593315Sopenharmony_ci * Copyright (c) 2016 Tatsuhiro Tsujikawa 52c593315Sopenharmony_ci * 62c593315Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining 72c593315Sopenharmony_ci * a copy of this software and associated documentation files (the 82c593315Sopenharmony_ci * "Software"), to deal in the Software without restriction, including 92c593315Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish, 102c593315Sopenharmony_ci * distribute, sublicense, and/or sell copies of the Software, and to 112c593315Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to 122c593315Sopenharmony_ci * the following conditions: 132c593315Sopenharmony_ci * 142c593315Sopenharmony_ci * The above copyright notice and this permission notice shall be 152c593315Sopenharmony_ci * included in all copies or substantial portions of the Software. 162c593315Sopenharmony_ci * 172c593315Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 182c593315Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 192c593315Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 202c593315Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 212c593315Sopenharmony_ci * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 222c593315Sopenharmony_ci * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 232c593315Sopenharmony_ci * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 242c593315Sopenharmony_ci */ 252c593315Sopenharmony_ci#include "shrpx_dual_dns_resolver.h" 262c593315Sopenharmony_ci 272c593315Sopenharmony_cinamespace shrpx { 282c593315Sopenharmony_ci 292c593315Sopenharmony_ciDualDNSResolver::DualDNSResolver(struct ev_loop *loop, int family) 302c593315Sopenharmony_ci : family_(family), resolv4_(loop), resolv6_(loop) { 312c593315Sopenharmony_ci auto cb = [this](DNSResolverStatus, const Address *) { 322c593315Sopenharmony_ci Address result; 332c593315Sopenharmony_ci 342c593315Sopenharmony_ci auto status = this->get_status(&result); 352c593315Sopenharmony_ci switch (status) { 362c593315Sopenharmony_ci case DNSResolverStatus::ERROR: 372c593315Sopenharmony_ci case DNSResolverStatus::OK: 382c593315Sopenharmony_ci break; 392c593315Sopenharmony_ci default: 402c593315Sopenharmony_ci return; 412c593315Sopenharmony_ci } 422c593315Sopenharmony_ci 432c593315Sopenharmony_ci auto cb = this->get_complete_cb(); 442c593315Sopenharmony_ci cb(status, &result); 452c593315Sopenharmony_ci }; 462c593315Sopenharmony_ci 472c593315Sopenharmony_ci if (family_ == AF_UNSPEC || family_ == AF_INET) { 482c593315Sopenharmony_ci resolv4_.set_complete_cb(cb); 492c593315Sopenharmony_ci } 502c593315Sopenharmony_ci if (family_ == AF_UNSPEC || family_ == AF_INET6) { 512c593315Sopenharmony_ci resolv6_.set_complete_cb(cb); 522c593315Sopenharmony_ci } 532c593315Sopenharmony_ci} 542c593315Sopenharmony_ci 552c593315Sopenharmony_ciint DualDNSResolver::resolve(const StringRef &host) { 562c593315Sopenharmony_ci int rv4 = 0, rv6 = 0; 572c593315Sopenharmony_ci if (family_ == AF_UNSPEC || family_ == AF_INET) { 582c593315Sopenharmony_ci rv4 = resolv4_.resolve(host, AF_INET); 592c593315Sopenharmony_ci } 602c593315Sopenharmony_ci if (family_ == AF_UNSPEC || family_ == AF_INET6) { 612c593315Sopenharmony_ci rv6 = resolv6_.resolve(host, AF_INET6); 622c593315Sopenharmony_ci } 632c593315Sopenharmony_ci 642c593315Sopenharmony_ci if (rv4 != 0 && rv6 != 0) { 652c593315Sopenharmony_ci return -1; 662c593315Sopenharmony_ci } 672c593315Sopenharmony_ci 682c593315Sopenharmony_ci return 0; 692c593315Sopenharmony_ci} 702c593315Sopenharmony_ci 712c593315Sopenharmony_ciCompleteCb DualDNSResolver::get_complete_cb() const { return complete_cb_; } 722c593315Sopenharmony_ci 732c593315Sopenharmony_civoid DualDNSResolver::set_complete_cb(CompleteCb cb) { complete_cb_ = cb; } 742c593315Sopenharmony_ci 752c593315Sopenharmony_ciDNSResolverStatus DualDNSResolver::get_status(Address *result) const { 762c593315Sopenharmony_ci auto rv6 = resolv6_.get_status(result); 772c593315Sopenharmony_ci if (rv6 == DNSResolverStatus::OK) { 782c593315Sopenharmony_ci return DNSResolverStatus::OK; 792c593315Sopenharmony_ci } 802c593315Sopenharmony_ci auto rv4 = resolv4_.get_status(result); 812c593315Sopenharmony_ci if (rv4 == DNSResolverStatus::OK) { 822c593315Sopenharmony_ci return DNSResolverStatus::OK; 832c593315Sopenharmony_ci } 842c593315Sopenharmony_ci if (rv4 == DNSResolverStatus::RUNNING || rv6 == DNSResolverStatus::RUNNING) { 852c593315Sopenharmony_ci return DNSResolverStatus::RUNNING; 862c593315Sopenharmony_ci } 872c593315Sopenharmony_ci if (rv4 == DNSResolverStatus::ERROR || rv6 == DNSResolverStatus::ERROR) { 882c593315Sopenharmony_ci return DNSResolverStatus::ERROR; 892c593315Sopenharmony_ci } 902c593315Sopenharmony_ci return DNSResolverStatus::IDLE; 912c593315Sopenharmony_ci} 922c593315Sopenharmony_ci 932c593315Sopenharmony_ci} // namespace shrpx 94