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