Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | 537dc3a | 2016-02-18 19:35:26 +0100 | [diff] [blame^] | 3 | * Copyright (c) 2013-2016 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 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 22 | #ifndef NDN_UTIL_DNS_HPP |
| 23 | #define NDN_UTIL_DNS_HPP |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 24 | |
| 25 | #include "../util/time.hpp" |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 26 | |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 27 | #include <boost/asio/ip/address.hpp> |
Davide Pesavento | 537dc3a | 2016-02-18 19:35:26 +0100 | [diff] [blame^] | 28 | |
| 29 | // forward declaration |
| 30 | namespace boost { |
| 31 | namespace asio { |
| 32 | class io_service; |
| 33 | } // namespace asio |
| 34 | } // namespace boost |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 35 | |
| 36 | namespace ndn { |
| 37 | namespace dns { |
| 38 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 39 | typedef boost::asio::ip::address IpAddress; |
| 40 | typedef function<bool (const IpAddress& address)> AddressSelector; |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 41 | |
| 42 | struct AnyAddress |
| 43 | { |
| 44 | bool |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 45 | operator()(const IpAddress& address) const |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 46 | { |
| 47 | return true; |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | struct Ipv4Only |
| 52 | { |
| 53 | bool |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 54 | operator()(const IpAddress& address) const |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 55 | { |
| 56 | return address.is_v4(); |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | struct Ipv6Only |
| 61 | { |
| 62 | bool |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 63 | operator()(const IpAddress& address) const |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 64 | { |
| 65 | return address.is_v6(); |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | struct Error : public std::runtime_error |
| 70 | { |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 71 | explicit |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 72 | Error(const std::string& what) |
| 73 | : std::runtime_error(what) |
| 74 | { |
| 75 | } |
| 76 | }; |
| 77 | |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 78 | typedef function<void (const IpAddress& address)> SuccessCallback; |
| 79 | typedef function<void (const std::string& reason)> ErrorCallback; |
| 80 | |
| 81 | /** \brief Asynchronously resolve host |
| 82 | * |
| 83 | * If an address selector predicate is specified, then each resolved IP address |
| 84 | * is checked against the predicate. |
| 85 | * |
| 86 | * Available address selector predicates: |
| 87 | * |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 88 | * - dns::AnyAddress() |
| 89 | * - dns::Ipv4Address() |
| 90 | * - dns::Ipv6Address() |
Junxiao Shi | 5df5992 | 2015-09-15 03:25:11 +0000 | [diff] [blame] | 91 | * |
| 92 | * \warning Even after the DNS resolution has timed out, it's possible that |
| 93 | * \p ioService keeps running and \p onSuccess is invoked at a later time. |
| 94 | * This could cause segmentation fault if \p onSuccess is deallocated. |
| 95 | * To stop the io_service, explicitly invoke \p ioService.stop(). |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 96 | */ |
| 97 | void |
| 98 | asyncResolve(const std::string& host, |
| 99 | const SuccessCallback& onSuccess, |
| 100 | const ErrorCallback& onError, |
| 101 | boost::asio::io_service& ioService, |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 102 | const AddressSelector& addressSelector = AnyAddress(), |
| 103 | time::nanoseconds timeout = time::seconds(4)); |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 104 | |
| 105 | /** \brief Synchronously resolve host |
| 106 | * |
| 107 | * If an address selector predicate is specified, then each resolved IP address |
| 108 | * is checked against the predicate. |
| 109 | * |
| 110 | * Available address selector predicates: |
| 111 | * |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 112 | * - dns::AnyAddress() |
| 113 | * - dns::Ipv4Address() |
| 114 | * - dns::Ipv6Address() |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 115 | */ |
| 116 | IpAddress |
| 117 | syncResolve(const std::string& host, |
| 118 | boost::asio::io_service& ioService, |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 119 | const AddressSelector& addressSelector = AnyAddress()); |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 120 | |
| 121 | } // namespace dns |
| 122 | } // namespace ndn |
| 123 | |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 124 | #endif // NDN_UTIL_DNS_HPP |