Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 4d0d096 | 2017-12-19 22:23:14 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2013-2018 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 | |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 22 | #ifndef NDN_NET_DNS_HPP |
| 23 | #define NDN_NET_DNS_HPP |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 24 | |
Davide Pesavento | 4d0d096 | 2017-12-19 22:23:14 -0500 | [diff] [blame] | 25 | #include "asio-fwd.hpp" |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 26 | #include "../util/time.hpp" |
Davide Pesavento | a2de7e4 | 2015-10-24 00:14:49 +0200 | [diff] [blame] | 27 | |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 28 | #include <boost/asio/ip/address.hpp> |
Davide Pesavento | 537dc3a | 2016-02-18 19:35:26 +0100 | [diff] [blame] | 29 | |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 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(), |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 97 | time::nanoseconds timeout = 4_s); |
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 | |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 118 | #endif // NDN_NET_DNS_HPP |