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