New upstream version 0.3.6
[anytun.git] / src / resolver.cpp
1 /*
2  *  anytun
3  *
4  *  The secure anycast tunneling protocol (satp) defines a protocol used
5  *  for communication between any combination of unicast and anycast
6  *  tunnel endpoints.  It has less protocol overhead than IPSec in Tunnel
7  *  mode and allows tunneling of every ETHER TYPE protocol (e.g.
8  *  ethernet, ip, arp ...). satp directly includes cryptography and
9  *  message authentication based on the methods used by SRTP.  It is
10  *  intended to deliver a generic, scaleable and secure solution for
11  *  tunneling and relaying of packets of any protocol.
12  *
13  *
14  *  Copyright (C) 2007-2014 Markus Grüneis, Othmar Gsenger, Erwin Nindl,
15  *                          Christian Pointner <satp@wirdorange.org>
16  *
17  *  This file is part of Anytun.
18  *
19  *  Anytun is free software: you can redistribute it and/or modify
20  *  it under the terms of the GNU General Public License as published by
21  *  the Free Software Foundation, either version 3 of the License, or
22  *  any later version.
23  *
24  *  Anytun is distributed in the hope that it will be useful,
25  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
26  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  *  GNU General Public License for more details.
28  *
29  *  You should have received a copy of the GNU General Public License
30  *  along with Anytun.  If not, see <http://www.gnu.org/licenses/>.
31  *
32  *  In addition, as a special exception, the copyright holders give
33  *  permission to link the code of portions of this program with the
34  *  OpenSSL library under certain conditions as described in each
35  *  individual source file, and distribute linked combinations
36  *  including the two.
37  *  You must obey the GNU General Public License in all respects
38  *  for all of the code used other than OpenSSL.  If you modify
39  *  file(s) with this exception, you may extend this exception to your
40  *  version of the file(s), but you are not obligated to do so.  If you
41  *  do not wish to do so, delete this exception statement from your
42  *  version.  If you delete this exception statement from all source
43  *  files in the program, then also delete it here.
44  */
45
46 #include <boost/bind.hpp>
47 #include <boost/system/error_code.hpp>
48
49 #include "resolver.h"
50 #include "log.h"
51
52 using ::boost::asio::ip::udp;
53 using ::boost::asio::ip::tcp;
54
55 template<class Proto>
56 void waitAndEnqueue(uint32_t s, const std::string& addr, const std::string& port, boost::function<void(boost::asio::ip::basic_resolver_iterator<Proto>)> const& onResolve, ErrorCallback const& onError, ResolvAddrType r)
57 {
58   cLog.msg(Log::PRIO_ERROR) << "the resolver only supports udp and tcp";
59 }
60
61 template<>
62 void waitAndEnqueue(uint32_t s, const std::string& addr, const std::string& port, boost::function<void(boost::asio::ip::basic_resolver_iterator<udp>)> const& onResolve, ErrorCallback const& onError, ResolvAddrType r)
63 {
64   boost::this_thread::sleep(boost::posix_time::milliseconds(s * 1000));
65   gResolver.resolveUdp(addr, port, onResolve, onError, r);
66 }
67
68 template<>
69 void waitAndEnqueue(uint32_t s, const std::string& addr, const std::string& port, boost::function<void(boost::asio::ip::basic_resolver_iterator<tcp>)> const& onResolve, ErrorCallback const& onError, ResolvAddrType r)
70 {
71   boost::this_thread::sleep(boost::posix_time::milliseconds(s * 1000));
72   gResolver.resolveTcp(addr, port, onResolve, onError, r);
73 }
74
75
76 template<class Proto>
77 ResolveHandler<Proto>::ResolveHandler(const std::string& addr, const std::string& port, boost::function<void(boost::asio::ip::basic_resolver_iterator<Proto>)> const& onResolve, ErrorCallback const& onError, ResolvAddrType r) : addr_(addr), port_(port), onResolve_(onResolve), onError_(onError), resolv_addr_type_(r)
78 {
79 }
80
81 template<class Proto>
82 void ResolveHandler<Proto>::operator()(const boost::system::error_code& e, boost::asio::ip::basic_resolver_iterator<Proto> endpointIt)
83 {
84   if(boost::system::posix_error::success == e) {
85     try {
86       onResolve_(endpointIt);
87     } catch(const std::runtime_error& e) {
88       onError_(e);
89     }
90   } else {
91     cLog.msg(Log::PRIO_ERROR) << "Error while resolving '" << addr_ << "' '" << port_ << "', retrying in 10 sec.";
92     boost::thread(boost::bind(waitAndEnqueue<Proto>, 10, addr_, port_, onResolve_, onError_, resolv_addr_type_));
93   }
94 }
95
96 Resolver& gResolver = Resolver::instance();
97
98 Resolver& Resolver::instance()
99 {
100   static Resolver instance;
101   return instance;
102 }
103
104 Resolver::Resolver() : udp_resolver_(io_service_), tcp_resolver_(io_service_), thread_(NULL)
105 {
106 }
107
108 Resolver::~Resolver()
109 {
110   if(thread_) {
111     delete thread_;
112   }
113 }
114
115 void Resolver::init()
116 {
117   if(!thread_) {
118     thread_ = new boost::thread(boost::bind(&Resolver::run, this));
119   }
120 }
121
122 void Resolver::run()
123 {
124   cLog.msg(Log::PRIO_DEBUG) << "Resolver Thread started";
125
126   for(;;) {
127     try {
128       io_service_.run();
129       io_service_.reset();
130       boost::this_thread::sleep(boost::posix_time::milliseconds(250));
131     } catch(const std::runtime_error& e) {
132       cLog.msg(Log::PRIO_ERROR) << "resolver caught runtime error, restarting: " << e.what();
133     } catch(const std::exception& e) {
134       cLog.msg(Log::PRIO_ERROR) << "resolver caught exception, restarting: " << e.what();
135     }
136   }
137 }
138
139
140 void Resolver::resolveUdp(const std::string& addr, const std::string& port, UdpResolveCallback const& onResolve, ErrorCallback const& onError, ResolvAddrType r)
141 {
142   cLog.msg(Log::PRIO_DEBUG) << "trying to resolv UDP: '" << addr << "' '" << port << "'";
143
144   boost::shared_ptr<udp::resolver::query> query;
145   if(addr != "") {
146     switch(r) {
147     case IPV4_ONLY:
148       query = boost::shared_ptr<udp::resolver::query>(new udp::resolver::query(udp::v4(), addr, port));
149       break;
150     case IPV6_ONLY:
151       query = boost::shared_ptr<udp::resolver::query>(new udp::resolver::query(udp::v6(), addr, port));
152       break;
153     default:
154       query = boost::shared_ptr<udp::resolver::query>(new udp::resolver::query(addr, port));
155       break;
156     }
157   } else {
158     switch(r) {
159     case IPV4_ONLY:
160       query = boost::shared_ptr<udp::resolver::query>(new udp::resolver::query(udp::v4(), port));
161       break;
162     case IPV6_ONLY:
163       query = boost::shared_ptr<udp::resolver::query>(new udp::resolver::query(udp::v6(), port));
164       break;
165     default:
166       query = boost::shared_ptr<udp::resolver::query>(new udp::resolver::query(port));
167       break;
168     }
169   }
170   UdpResolveHandler handler(addr, port, onResolve, onError, r);
171   udp_resolver_.async_resolve(*query, handler);
172 }
173
174 void Resolver::resolveTcp(const std::string& addr, const std::string& port, TcpResolveCallback const& onResolve, ErrorCallback const& onError, ResolvAddrType r)
175 {
176   cLog.msg(Log::PRIO_DEBUG) << "trying to resolv TCP: '" << addr << "' '" << port << "'";
177
178   boost::shared_ptr<tcp::resolver::query> query;
179   if(addr != "") {
180     switch(r) {
181     case IPV4_ONLY:
182       query = boost::shared_ptr<tcp::resolver::query>(new tcp::resolver::query(tcp::v4(), addr, port));
183       break;
184     case IPV6_ONLY:
185       query = boost::shared_ptr<tcp::resolver::query>(new tcp::resolver::query(tcp::v6(), addr, port));
186       break;
187     default:
188       query = boost::shared_ptr<tcp::resolver::query>(new tcp::resolver::query(addr, port));
189       break;
190     }
191   } else {
192     switch(r) {
193     case IPV4_ONLY:
194       query = boost::shared_ptr<tcp::resolver::query>(new tcp::resolver::query(tcp::v4(), port));
195       break;
196     case IPV6_ONLY:
197       query = boost::shared_ptr<tcp::resolver::query>(new tcp::resolver::query(tcp::v6(), port));
198       break;
199     default:
200       query = boost::shared_ptr<tcp::resolver::query>(new tcp::resolver::query(port));
201       break;
202     }
203   }
204   TcpResolveHandler handler(addr, port, onResolve, onError, r);
205   tcp_resolver_.async_resolve(*query, handler);
206 }