Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame^] | 3 | * Copyright (c) 2013-2017 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" |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame^] | 23 | #include "../util/scheduler.hpp" |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 24 | |
Davide Pesavento | 537dc3a | 2016-02-18 19:35:26 +0100 | [diff] [blame] | 25 | #include <boost/asio/io_service.hpp> |
| 26 | #include <boost/asio/ip/udp.hpp> |
| 27 | |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 28 | namespace ndn { |
| 29 | namespace dns { |
| 30 | |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 31 | class Resolver : noncopyable |
| 32 | { |
| 33 | public: |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 34 | typedef boost::asio::ip::udp protocol; |
| 35 | typedef protocol::resolver::iterator iterator; |
| 36 | typedef protocol::resolver::query query; |
| 37 | |
| 38 | public: |
| 39 | Resolver(boost::asio::io_service& ioService, |
| 40 | const AddressSelector& addressSelector) |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 41 | : m_resolver(ioService) |
| 42 | , m_addressSelector(addressSelector) |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 43 | , m_scheduler(ioService) |
| 44 | { |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 45 | BOOST_ASSERT(m_addressSelector != nullptr); |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 49 | asyncResolve(const query& q, |
| 50 | const SuccessCallback& onSuccess, |
| 51 | const ErrorCallback& onError, |
| 52 | time::nanoseconds timeout, |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 53 | const shared_ptr<Resolver>& self) |
| 54 | { |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 55 | m_onSuccess = onSuccess; |
| 56 | m_onError = onError; |
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_resolver.async_resolve(q, bind(&Resolver::onResolveResult, this, _1, _2, self)); |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 59 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 60 | m_resolveTimeout = m_scheduler.scheduleEvent(timeout, bind(&Resolver::onResolveTimeout, this, self)); |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 61 | } |
| 62 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 63 | iterator |
| 64 | syncResolve(const query& q) |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 65 | { |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 66 | return selectAddress(m_resolver.resolve(q)); |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 67 | } |
| 68 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 69 | private: |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 70 | void |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 71 | onResolveResult(const boost::system::error_code& error, |
| 72 | iterator it, const shared_ptr<Resolver>& self) |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 73 | { |
| 74 | m_scheduler.cancelEvent(m_resolveTimeout); |
Davide Pesavento | fd5de11 | 2015-10-24 01:47:10 +0200 | [diff] [blame] | 75 | // ensure the Resolver isn't destructed while callbacks are still pending, see #2653 |
| 76 | m_resolver.get_io_service().post(bind([] (const shared_ptr<Resolver>&) {}, self)); |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 77 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 78 | if (error) { |
| 79 | if (error == boost::asio::error::operation_aborted) |
| 80 | return; |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 81 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 82 | if (m_onError) |
| 83 | m_onError("Hostname cannot be resolved: " + error.message()); |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 84 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 85 | return; |
| 86 | } |
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 | it = selectAddress(it); |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 89 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 90 | if (it != iterator() && m_onSuccess) { |
| 91 | m_onSuccess(it->endpoint().address()); |
| 92 | } |
| 93 | else if (m_onError) { |
| 94 | m_onError("No endpoints match the specified address selector"); |
| 95 | } |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 99 | onResolveTimeout(const shared_ptr<Resolver>& self) |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 100 | { |
| 101 | m_resolver.cancel(); |
Davide Pesavento | fd5de11 | 2015-10-24 01:47:10 +0200 | [diff] [blame] | 102 | // ensure the Resolver isn't destructed while callbacks are still pending, see #2653 |
| 103 | m_resolver.get_io_service().post(bind([] (const shared_ptr<Resolver>&) {}, self)); |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 104 | |
| 105 | if (m_onError) |
| 106 | m_onError("Hostname resolution timed out"); |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 107 | } |
| 108 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 109 | iterator |
| 110 | selectAddress(iterator it) const |
| 111 | { |
| 112 | while (it != iterator() && |
| 113 | !m_addressSelector(it->endpoint().address())) { |
| 114 | ++it; |
| 115 | } |
| 116 | |
| 117 | return it; |
| 118 | } |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 119 | |
| 120 | private: |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 121 | protocol::resolver m_resolver; |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 122 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 123 | AddressSelector m_addressSelector; |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 124 | SuccessCallback m_onSuccess; |
| 125 | ErrorCallback m_onError; |
| 126 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 127 | util::scheduler::Scheduler m_scheduler; |
| 128 | util::scheduler::EventId m_resolveTimeout; |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 129 | }; |
| 130 | |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 131 | void |
| 132 | asyncResolve(const std::string& host, |
| 133 | const SuccessCallback& onSuccess, |
| 134 | const ErrorCallback& onError, |
| 135 | boost::asio::io_service& ioService, |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 136 | const AddressSelector& addressSelector, |
| 137 | time::nanoseconds timeout) |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 138 | { |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 139 | auto resolver = make_shared<Resolver>(ref(ioService), addressSelector); |
| 140 | resolver->asyncResolve(Resolver::query(host, ""), onSuccess, onError, timeout, resolver); |
| 141 | // resolver will be destroyed when async operation finishes or ioService stops |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | IpAddress |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 145 | syncResolve(const std::string& host, |
| 146 | boost::asio::io_service& ioService, |
| 147 | const AddressSelector& addressSelector) |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 148 | { |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 149 | Resolver resolver(ioService, addressSelector); |
| 150 | auto it = resolver.syncResolve(Resolver::query(host, "")); |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 151 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 152 | if (it == Resolver::iterator()) { |
| 153 | BOOST_THROW_EXCEPTION(Error("No endpoints match the specified address selector")); |
| 154 | } |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 155 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 156 | return it->endpoint().address(); |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | } // namespace dns |
| 160 | } // namespace ndn |