blob: 9357613e17a3c1aa8ae0c839e283b4ce0ac7ee97 [file] [log] [blame]
Vince Lehman7a6bb352014-09-22 15:58:19 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento4d0d0962017-12-19 22:23:14 -05002/*
Davide Pesavento0f830802018-01-16 23:58:58 -05003 * Copyright (c) 2013-2018 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
Davide Pesavento4d0d0962017-12-19 22:23:14 -050025#include "asio-fwd.hpp"
Vince Lehman7a6bb352014-09-22 15:58:19 -050026#include "../util/time.hpp"
Davide Pesaventoa2de7e42015-10-24 00:14:49 +020027
Vince Lehman7a6bb352014-09-22 15:58:19 -050028#include <boost/asio/ip/address.hpp>
Davide Pesavento537dc3a2016-02-18 19:35:26 +010029
Vince Lehman7a6bb352014-09-22 15:58:19 -050030namespace ndn {
31namespace dns {
32
Davide Pesaventoa2de7e42015-10-24 00:14:49 +020033typedef boost::asio::ip::address IpAddress;
34typedef function<bool (const IpAddress& address)> AddressSelector;
Vince Lehman7a6bb352014-09-22 15:58:19 -050035
36struct AnyAddress
37{
38 bool
Davide Pesaventoa2de7e42015-10-24 00:14:49 +020039 operator()(const IpAddress& address) const
Vince Lehman7a6bb352014-09-22 15:58:19 -050040 {
41 return true;
42 }
43};
44
45struct Ipv4Only
46{
47 bool
Davide Pesaventoa2de7e42015-10-24 00:14:49 +020048 operator()(const IpAddress& address) const
Vince Lehman7a6bb352014-09-22 15:58:19 -050049 {
50 return address.is_v4();
51 }
52};
53
54struct Ipv6Only
55{
56 bool
Davide Pesaventoa2de7e42015-10-24 00:14:49 +020057 operator()(const IpAddress& address) const
Vince Lehman7a6bb352014-09-22 15:58:19 -050058 {
59 return address.is_v6();
60 }
61};
62
Junxiao Shi68b53852018-07-25 13:56:38 -060063class Error : public std::runtime_error
Vince Lehman7a6bb352014-09-22 15:58:19 -050064{
Junxiao Shi68b53852018-07-25 13:56:38 -060065public:
66 using std::runtime_error::runtime_error;
Vince Lehman7a6bb352014-09-22 15:58:19 -050067};
68
Vince Lehman7a6bb352014-09-22 15:58:19 -050069typedef function<void (const IpAddress& address)> SuccessCallback;
70typedef function<void (const std::string& reason)> ErrorCallback;
71
72/** \brief Asynchronously resolve host
73 *
74 * If an address selector predicate is specified, then each resolved IP address
75 * is checked against the predicate.
76 *
77 * Available address selector predicates:
78 *
Davide Pesaventoa2de7e42015-10-24 00:14:49 +020079 * - dns::AnyAddress()
80 * - dns::Ipv4Address()
81 * - dns::Ipv6Address()
Junxiao Shi5df59922015-09-15 03:25:11 +000082 *
83 * \warning Even after the DNS resolution has timed out, it's possible that
84 * \p ioService keeps running and \p onSuccess is invoked at a later time.
85 * This could cause segmentation fault if \p onSuccess is deallocated.
86 * To stop the io_service, explicitly invoke \p ioService.stop().
Vince Lehman7a6bb352014-09-22 15:58:19 -050087 */
88void
89asyncResolve(const std::string& host,
90 const SuccessCallback& onSuccess,
91 const ErrorCallback& onError,
92 boost::asio::io_service& ioService,
Davide Pesaventoa2de7e42015-10-24 00:14:49 +020093 const AddressSelector& addressSelector = AnyAddress(),
Davide Pesavento0f830802018-01-16 23:58:58 -050094 time::nanoseconds timeout = 4_s);
Vince Lehman7a6bb352014-09-22 15:58:19 -050095
96/** \brief Synchronously resolve host
97 *
98 * If an address selector predicate is specified, then each resolved IP address
99 * is checked against the predicate.
100 *
101 * Available address selector predicates:
102 *
Davide Pesaventoa2de7e42015-10-24 00:14:49 +0200103 * - dns::AnyAddress()
104 * - dns::Ipv4Address()
105 * - dns::Ipv6Address()
Vince Lehman7a6bb352014-09-22 15:58:19 -0500106 */
107IpAddress
108syncResolve(const std::string& host,
109 boost::asio::io_service& ioService,
Davide Pesaventoa2de7e42015-10-24 00:14:49 +0200110 const AddressSelector& addressSelector = AnyAddress());
Vince Lehman7a6bb352014-09-22 15:58:19 -0500111
112} // namespace dns
113} // namespace ndn
114
Junxiao Shi25467942017-06-30 02:53:14 +0000115#endif // NDN_NET_DNS_HPP