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); |
| 73 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame^] | 74 | if (error) { |
| 75 | if (error == boost::asio::error::operation_aborted) |
| 76 | return; |
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 (m_onError) |
| 79 | m_onError("Hostname cannot be resolved: " + error.message()); |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 80 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame^] | 81 | return; |
| 82 | } |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 83 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame^] | 84 | it = selectAddress(it); |
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 | if (it != iterator() && m_onSuccess) { |
| 87 | m_onSuccess(it->endpoint().address()); |
| 88 | } |
| 89 | else if (m_onError) { |
| 90 | m_onError("No endpoints match the specified address selector"); |
| 91 | } |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame^] | 95 | onResolveTimeout(const shared_ptr<Resolver>& self) |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 96 | { |
| 97 | m_resolver.cancel(); |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame^] | 98 | |
| 99 | if (m_onError) |
| 100 | m_onError("Hostname resolution timed out"); |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 101 | } |
| 102 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame^] | 103 | iterator |
| 104 | selectAddress(iterator it) const |
| 105 | { |
| 106 | while (it != iterator() && |
| 107 | !m_addressSelector(it->endpoint().address())) { |
| 108 | ++it; |
| 109 | } |
| 110 | |
| 111 | return it; |
| 112 | } |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 113 | |
| 114 | private: |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame^] | 115 | protocol::resolver m_resolver; |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 116 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame^] | 117 | AddressSelector m_addressSelector; |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 118 | SuccessCallback m_onSuccess; |
| 119 | ErrorCallback m_onError; |
| 120 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame^] | 121 | util::scheduler::Scheduler m_scheduler; |
| 122 | util::scheduler::EventId m_resolveTimeout; |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 123 | }; |
| 124 | |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 125 | void |
| 126 | asyncResolve(const std::string& host, |
| 127 | const SuccessCallback& onSuccess, |
| 128 | const ErrorCallback& onError, |
| 129 | boost::asio::io_service& ioService, |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame^] | 130 | const AddressSelector& addressSelector, |
| 131 | time::nanoseconds timeout) |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 132 | { |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame^] | 133 | auto resolver = make_shared<Resolver>(ref(ioService), addressSelector); |
| 134 | resolver->asyncResolve(Resolver::query(host, ""), onSuccess, onError, timeout, resolver); |
| 135 | // resolver will be destroyed when async operation finishes or ioService stops |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | IpAddress |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame^] | 139 | syncResolve(const std::string& host, |
| 140 | boost::asio::io_service& ioService, |
| 141 | const AddressSelector& addressSelector) |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 142 | { |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame^] | 143 | Resolver resolver(ioService, addressSelector); |
| 144 | auto it = resolver.syncResolve(Resolver::query(host, "")); |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 145 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame^] | 146 | if (it == Resolver::iterator()) { |
| 147 | BOOST_THROW_EXCEPTION(Error("No endpoints match the specified address selector")); |
| 148 | } |
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 | return it->endpoint().address(); |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | } // namespace dns |
| 154 | } // namespace ndn |