blob: ed15dd390dafb9fae9d0fb89821b6c203f5983b4 [file] [log] [blame]
Vince Lehman7a6bb352014-09-22 15:58:19 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2014 Regents of the University of California.
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
22#ifndef NDN_UTIL_DNS_H
23#define NDN_UTIL_DNS_H
24
25#include "../util/time.hpp"
26#include <boost/asio/ip/address.hpp>
27#include <boost/asio/io_service.hpp>
28
29namespace ndn {
30namespace dns {
31
32typedef function<bool (const boost::asio::ip::address& address)> AddressSelector;
33
34struct AnyAddress
35{
36 bool
37 operator()(const boost::asio::ip::address& address)
38 {
39 return true;
40 }
41};
42
43struct Ipv4Only
44{
45 bool
46 operator()(const boost::asio::ip::address& address)
47 {
48 return address.is_v4();
49 }
50};
51
52struct Ipv6Only
53{
54 bool
55 operator()(const boost::asio::ip::address& address)
56 {
57 return address.is_v6();
58 }
59};
60
61struct Error : public std::runtime_error
62{
63 Error(const std::string& what)
64 : std::runtime_error(what)
65 {
66 }
67};
68
69typedef boost::asio::ip::address IpAddress;
70
71typedef function<void (const IpAddress& address)> SuccessCallback;
72typedef function<void (const std::string& reason)> ErrorCallback;
73
74/** \brief Asynchronously resolve host
75 *
76 * If an address selector predicate is specified, then each resolved IP address
77 * is checked against the predicate.
78 *
79 * Available address selector predicates:
80 *
81 * - resolver::AnyAddress()
82 * - resolver::Ipv4Address()
83 * - resolver::Ipv6Address()
84 */
85void
86asyncResolve(const std::string& host,
87 const SuccessCallback& onSuccess,
88 const ErrorCallback& onError,
89 boost::asio::io_service& ioService,
90 const ndn::dns::AddressSelector& addressSelector = ndn::dns::AnyAddress(),
91 const time::nanoseconds& timeout = time::seconds(4));
92
93/** \brief Synchronously resolve host
94 *
95 * If an address selector predicate is specified, then each resolved IP address
96 * is checked against the predicate.
97 *
98 * Available address selector predicates:
99 *
100 * - resolver::AnyAddress()
101 * - resolver::Ipv4Address()
102 * - resolver::Ipv6Address()
103 */
104IpAddress
105syncResolve(const std::string& host,
106 boost::asio::io_service& ioService,
107 const ndn::dns::AddressSelector& addressSelector = ndn::dns::AnyAddress());
108
109} // namespace dns
110} // namespace ndn
111
112#endif // NDN_UTIL_DNS_H