blob: 79d9a1057552c823fed8618b95797fd83eee958e [file] [log] [blame]
Vince Lehman7a6bb352014-09-22 15:58:19 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi25467942017-06-30 02:53:14 +00003 * Copyright (c) 2013-2017 Regents of the University of California.
Vince Lehman7a6bb352014-09-22 15:58:19 -05004 *
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 Shi25467942017-06-30 02:53:14 +000022#ifndef NDN_NET_DNS_HPP
23#define NDN_NET_DNS_HPP
Vince Lehman7a6bb352014-09-22 15:58:19 -050024
25#include "../util/time.hpp"
Davide Pesaventoa2de7e42015-10-24 00:14:49 +020026
Vince Lehman7a6bb352014-09-22 15:58:19 -050027#include <boost/asio/ip/address.hpp>
Davide Pesavento537dc3a2016-02-18 19:35:26 +010028
29// forward declaration
30namespace boost {
31namespace asio {
32class io_service;
33} // namespace asio
34} // namespace boost
Vince Lehman7a6bb352014-09-22 15:58:19 -050035
36namespace ndn {
37namespace dns {
38
Davide Pesaventoa2de7e42015-10-24 00:14:49 +020039typedef boost::asio::ip::address IpAddress;
40typedef function<bool (const IpAddress& address)> AddressSelector;
Vince Lehman7a6bb352014-09-22 15:58:19 -050041
42struct AnyAddress
43{
44 bool
Davide Pesaventoa2de7e42015-10-24 00:14:49 +020045 operator()(const IpAddress& address) const
Vince Lehman7a6bb352014-09-22 15:58:19 -050046 {
47 return true;
48 }
49};
50
51struct Ipv4Only
52{
53 bool
Davide Pesaventoa2de7e42015-10-24 00:14:49 +020054 operator()(const IpAddress& address) const
Vince Lehman7a6bb352014-09-22 15:58:19 -050055 {
56 return address.is_v4();
57 }
58};
59
60struct Ipv6Only
61{
62 bool
Davide Pesaventoa2de7e42015-10-24 00:14:49 +020063 operator()(const IpAddress& address) const
Vince Lehman7a6bb352014-09-22 15:58:19 -050064 {
65 return address.is_v6();
66 }
67};
68
69struct Error : public std::runtime_error
70{
Davide Pesaventoa2de7e42015-10-24 00:14:49 +020071 explicit
Vince Lehman7a6bb352014-09-22 15:58:19 -050072 Error(const std::string& what)
73 : std::runtime_error(what)
74 {
75 }
76};
77
Vince Lehman7a6bb352014-09-22 15:58:19 -050078typedef function<void (const IpAddress& address)> SuccessCallback;
79typedef 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 Pesaventoa2de7e42015-10-24 00:14:49 +020088 * - dns::AnyAddress()
89 * - dns::Ipv4Address()
90 * - dns::Ipv6Address()
Junxiao Shi5df59922015-09-15 03:25:11 +000091 *
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 Lehman7a6bb352014-09-22 15:58:19 -050096 */
97void
98asyncResolve(const std::string& host,
99 const SuccessCallback& onSuccess,
100 const ErrorCallback& onError,
101 boost::asio::io_service& ioService,
Davide Pesaventoa2de7e42015-10-24 00:14:49 +0200102 const AddressSelector& addressSelector = AnyAddress(),
103 time::nanoseconds timeout = time::seconds(4));
Vince Lehman7a6bb352014-09-22 15:58:19 -0500104
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 Pesaventoa2de7e42015-10-24 00:14:49 +0200112 * - dns::AnyAddress()
113 * - dns::Ipv4Address()
114 * - dns::Ipv6Address()
Vince Lehman7a6bb352014-09-22 15:58:19 -0500115 */
116IpAddress
117syncResolve(const std::string& host,
118 boost::asio::io_service& ioService,
Davide Pesaventoa2de7e42015-10-24 00:14:49 +0200119 const AddressSelector& addressSelector = AnyAddress());
Vince Lehman7a6bb352014-09-22 15:58:19 -0500120
121} // namespace dns
122} // namespace ndn
123
Junxiao Shi25467942017-06-30 02:53:14 +0000124#endif // NDN_NET_DNS_HPP