blob: 961e5bd3d009ad8fefc8fa3b64deee0576936bce [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
63struct Error : public std::runtime_error
64{
Davide Pesaventoa2de7e42015-10-24 00:14:49 +020065 explicit
Vince Lehman7a6bb352014-09-22 15:58:19 -050066 Error(const std::string& what)
67 : std::runtime_error(what)
68 {
69 }
70};
71
Vince Lehman7a6bb352014-09-22 15:58:19 -050072typedef function<void (const IpAddress& address)> SuccessCallback;
73typedef 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 Pesaventoa2de7e42015-10-24 00:14:49 +020082 * - dns::AnyAddress()
83 * - dns::Ipv4Address()
84 * - dns::Ipv6Address()
Junxiao Shi5df59922015-09-15 03:25:11 +000085 *
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 Lehman7a6bb352014-09-22 15:58:19 -050090 */
91void
92asyncResolve(const std::string& host,
93 const SuccessCallback& onSuccess,
94 const ErrorCallback& onError,
95 boost::asio::io_service& ioService,
Davide Pesaventoa2de7e42015-10-24 00:14:49 +020096 const AddressSelector& addressSelector = AnyAddress(),
Davide Pesavento0f830802018-01-16 23:58:58 -050097 time::nanoseconds timeout = 4_s);
Vince Lehman7a6bb352014-09-22 15:58:19 -050098
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 Pesaventoa2de7e42015-10-24 00:14:49 +0200106 * - dns::AnyAddress()
107 * - dns::Ipv4Address()
108 * - dns::Ipv6Address()
Vince Lehman7a6bb352014-09-22 15:58:19 -0500109 */
110IpAddress
111syncResolve(const std::string& host,
112 boost::asio::io_service& ioService,
Davide Pesaventoa2de7e42015-10-24 00:14:49 +0200113 const AddressSelector& addressSelector = AnyAddress());
Vince Lehman7a6bb352014-09-22 15:58:19 -0500114
115} // namespace dns
116} // namespace ndn
117
Junxiao Shi25467942017-06-30 02:53:14 +0000118#endif // NDN_NET_DNS_HPP