Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 5946ed1 | 2015-01-19 23:41:39 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 Regents of the University of California. |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #include "dns.hpp" |
| 23 | |
| 24 | #include "scheduler.hpp" |
| 25 | |
| 26 | namespace ndn { |
| 27 | namespace dns { |
| 28 | |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 29 | class Resolver : noncopyable |
| 30 | { |
| 31 | public: |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 32 | typedef boost::asio::ip::udp protocol; |
| 33 | typedef protocol::resolver::iterator iterator; |
| 34 | typedef protocol::resolver::query query; |
| 35 | |
| 36 | public: |
| 37 | Resolver(boost::asio::io_service& ioService, |
| 38 | const AddressSelector& addressSelector) |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 39 | : m_resolver(ioService) |
| 40 | , m_addressSelector(addressSelector) |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 41 | , m_scheduler(ioService) |
| 42 | { |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 43 | BOOST_ASSERT(m_addressSelector != nullptr); |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 47 | asyncResolve(const query& q, |
| 48 | const SuccessCallback& onSuccess, |
| 49 | const ErrorCallback& onError, |
| 50 | time::nanoseconds timeout, |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 51 | const shared_ptr<Resolver>& self) |
| 52 | { |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 53 | m_onSuccess = onSuccess; |
| 54 | m_onError = onError; |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 55 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 56 | m_resolver.async_resolve(q, bind(&Resolver::onResolveResult, this, _1, _2, self)); |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 57 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 58 | m_resolveTimeout = m_scheduler.scheduleEvent(timeout, bind(&Resolver::onResolveTimeout, this, self)); |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 59 | } |
| 60 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 61 | iterator |
| 62 | syncResolve(const query& q) |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 63 | { |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 64 | return selectAddress(m_resolver.resolve(q)); |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 65 | } |
| 66 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 67 | private: |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 68 | void |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 69 | onResolveResult(const boost::system::error_code& error, |
| 70 | iterator it, const shared_ptr<Resolver>& self) |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 71 | { |
| 72 | m_scheduler.cancelEvent(m_resolveTimeout); |
Davide Pesavento | fd5de11 | 2015-10-24 01:47:10 +0200 | [diff] [blame] | 73 | // ensure the Resolver isn't destructed while callbacks are still pending, see #2653 |
| 74 | m_resolver.get_io_service().post(bind([] (const shared_ptr<Resolver>&) {}, self)); |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 75 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 76 | if (error) { |
| 77 | if (error == boost::asio::error::operation_aborted) |
| 78 | return; |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 79 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 80 | if (m_onError) |
| 81 | m_onError("Hostname cannot be resolved: " + error.message()); |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 82 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 83 | return; |
| 84 | } |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 85 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 86 | it = selectAddress(it); |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 87 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 88 | if (it != iterator() && m_onSuccess) { |
| 89 | m_onSuccess(it->endpoint().address()); |
| 90 | } |
| 91 | else if (m_onError) { |
| 92 | m_onError("No endpoints match the specified address selector"); |
| 93 | } |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 97 | onResolveTimeout(const shared_ptr<Resolver>& self) |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 98 | { |
| 99 | m_resolver.cancel(); |
Davide Pesavento | fd5de11 | 2015-10-24 01:47:10 +0200 | [diff] [blame] | 100 | // ensure the Resolver isn't destructed while callbacks are still pending, see #2653 |
| 101 | m_resolver.get_io_service().post(bind([] (const shared_ptr<Resolver>&) {}, self)); |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 102 | |
| 103 | if (m_onError) |
| 104 | m_onError("Hostname resolution timed out"); |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 105 | } |
| 106 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 107 | iterator |
| 108 | selectAddress(iterator it) const |
| 109 | { |
| 110 | while (it != iterator() && |
| 111 | !m_addressSelector(it->endpoint().address())) { |
| 112 | ++it; |
| 113 | } |
| 114 | |
| 115 | return it; |
| 116 | } |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 117 | |
| 118 | private: |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 119 | protocol::resolver m_resolver; |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 120 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 121 | AddressSelector m_addressSelector; |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 122 | SuccessCallback m_onSuccess; |
| 123 | ErrorCallback m_onError; |
| 124 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 125 | util::scheduler::Scheduler m_scheduler; |
| 126 | util::scheduler::EventId m_resolveTimeout; |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 127 | }; |
| 128 | |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 129 | void |
| 130 | asyncResolve(const std::string& host, |
| 131 | const SuccessCallback& onSuccess, |
| 132 | const ErrorCallback& onError, |
| 133 | boost::asio::io_service& ioService, |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 134 | const AddressSelector& addressSelector, |
| 135 | time::nanoseconds timeout) |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 136 | { |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 137 | auto resolver = make_shared<Resolver>(ref(ioService), addressSelector); |
| 138 | resolver->asyncResolve(Resolver::query(host, ""), onSuccess, onError, timeout, resolver); |
| 139 | // resolver will be destroyed when async operation finishes or ioService stops |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | IpAddress |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 143 | syncResolve(const std::string& host, |
| 144 | boost::asio::io_service& ioService, |
| 145 | const AddressSelector& addressSelector) |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 146 | { |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 147 | Resolver resolver(ioService, addressSelector); |
| 148 | auto it = resolver.syncResolve(Resolver::query(host, "")); |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 149 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 150 | if (it == Resolver::iterator()) { |
| 151 | BOOST_THROW_EXCEPTION(Error("No endpoints match the specified address selector")); |
| 152 | } |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 153 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 154 | return it->endpoint().address(); |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | } // namespace dns |
| 158 | } // namespace ndn |