dns.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2023 Regents of the University of California.
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 "ndn-cxx/net/dns.hpp"
24 
25 #include <boost/asio/ip/udp.hpp>
26 #include <boost/asio/post.hpp>
27 
28 namespace ndn::dns {
29 
30 class Resolver : noncopyable
31 {
32 public:
33  Resolver(boost::asio::io_context& ioCtx,
34  const AddressSelector& addressSelector)
35  : m_resolver(ioCtx)
36  , m_addressSelector(addressSelector)
37  , m_scheduler(ioCtx)
38  {
39  BOOST_ASSERT(m_addressSelector != nullptr);
40  }
41 
42  void
43  asyncResolve(const std::string& host,
44  const SuccessCallback& onSuccess,
45  const ErrorCallback& onError,
46  time::nanoseconds timeout,
47  const shared_ptr<Resolver>& self)
48  {
49  m_onSuccess = onSuccess;
50  m_onError = onError;
51 
52  m_resolver.async_resolve(host, "", [=] (auto&&... args) {
53  onResolveResult(std::forward<decltype(args)>(args)..., self);
54  });
55 
56  m_resolveTimeout = m_scheduler.schedule(timeout, [=] { onResolveTimeout(self); });
57  }
58 
59 private:
60  void
61  onResolveResult(const boost::system::error_code& error,
62  const boost::asio::ip::udp::resolver::results_type& results,
63  const shared_ptr<Resolver>& self)
64  {
65  m_resolveTimeout.cancel();
66 
67  // ensure the Resolver isn't destructed while callbacks are still pending, see #2653
68  boost::asio::post(m_resolver.get_executor(), [self] {});
69 
70  if (error) {
71  if (m_onError && error != boost::asio::error::operation_aborted) {
72  m_onError("Hostname could not be resolved: " + error.message());
73  }
74  return;
75  }
76 
77  for (const auto& entry : results) {
78  if (m_addressSelector(entry.endpoint().address())) {
79  if (m_onSuccess) {
80  m_onSuccess(entry.endpoint().address());
81  }
82  return;
83  }
84  }
85 
86  if (m_onError) {
87  m_onError("No endpoints match the specified address selector");
88  }
89  }
90 
91  void
92  onResolveTimeout(const shared_ptr<Resolver>& self)
93  {
94  m_resolver.cancel();
95 
96  // ensure the Resolver isn't destructed while callbacks are still pending, see #2653
97  boost::asio::post(m_resolver.get_executor(), [self] {});
98 
99  if (m_onError) {
100  m_onError("Hostname resolution timed out");
101  }
102  }
103 
104 private:
105  boost::asio::ip::udp::resolver m_resolver;
106 
107  AddressSelector m_addressSelector;
108  SuccessCallback m_onSuccess;
109  ErrorCallback m_onError;
110 
111  Scheduler m_scheduler;
112  scheduler::EventId m_resolveTimeout;
113 };
114 
115 void
116 asyncResolve(const std::string& host,
117  const SuccessCallback& onSuccess,
118  const ErrorCallback& onError,
119  boost::asio::io_context& ioCtx,
120  const AddressSelector& addressSelector,
121  time::nanoseconds timeout)
122 {
123  auto resolver = make_shared<Resolver>(ioCtx, addressSelector);
124  resolver->asyncResolve(host, onSuccess, onError, timeout, resolver);
125  // resolver will be destroyed when async operation finishes or ioCtx stops
126 }
127 
128 } // namespace ndn::dns
A handle for a scheduled event.
Definition: scheduler.hpp:62
Generic time-based event scheduler.
Definition: scheduler.hpp:138
Definition: dns.cpp:28
std::function< bool(const boost::asio::ip::address &)> AddressSelector
Definition: dns.hpp:35
void asyncResolve(const std::string &host, const SuccessCallback &onSuccess, const ErrorCallback &onError, boost::asio::io_context &ioCtx, const AddressSelector &addressSelector, time::nanoseconds timeout)
Asynchronously resolve host.
Definition: dns.cpp:116
std::function< void(const boost::asio::ip::address &address)> SuccessCallback
Definition: dns.hpp:64
std::function< void(const std::string &reason)> ErrorCallback
Definition: dns.hpp:65
::boost::chrono::nanoseconds nanoseconds
Definition: time.hpp:54