New upstream version 0.3.8
[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 #define BOOST_SYSTEM_ENABLE_DEPRECATED
48 #include <boost/system/error_code.hpp>
49
50 #include "resolver.h"
51 #include "log.h"
52
53 using ::boost::asio::ip::udp;
54 using ::boost::asio::ip::tcp;
55
56 template<class Proto>
57 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)
58 {
59   cLog.msg(Log::PRIO_ERROR) << "the resolver only supports udp and tcp";
60 }
61
62 template<>
63 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)
64 {
65   boost::this_thread::sleep(boost::posix_time::milliseconds(s * 1000));
66   gResolver.resolveUdp(addr, port, onResolve, onError, r);
67 }
68
69 template<>
70 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)
71 {
72   boost::this_thread::sleep(boost::posix_time::milliseconds(s * 1000));
73   gResolver.resolveTcp(addr, port, onResolve, onError, r);
74 }
75
76
77 template<class Proto>
78 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)
79 {
80 }
81
82 template<class Proto>
83 void ResolveHandler<Proto>::operator()(const boost::system::error_code& e, boost::asio::ip::basic_resolver_iterator<Proto> endpointIt)
84 {
85   if(boost::system::posix_error::success == e) {
86     try {
87       onResolve_(endpointIt);
88     } catch(const std::runtime_error& e) {
89       onError_(e);
90     }
91   } else {
92     cLog.msg(Log::PRIO_ERROR) << "Error while resolving '" << addr_ << "' '" << port_ << "', retrying in 10 sec.";
93     boost::thread(boost::bind(waitAndEnqueue<Proto>, 10, addr_, port_, onResolve_, onError_, resolv_addr_type_));
94   }
95 }
96
97 Resolver& gResolver = Resolver::instance();
98
99 Resolver& Resolver::instance()
100 {
101   static Resolver instance;
102   return instance;
103 }
104
105 Resolver::Resolver() : udp_resolver_(io_service_), tcp_resolver_(io_service_), thread_(NULL)
106 {
107 }
108
109 Resolver::~Resolver()
110 {
111   if(thread_) {
112     delete thread_;
113   }
114 }
115
116 void Resolver::init()
117 {
118   if(!thread_) {
119     thread_ = new boost::thread(boost::bind(&Resolver::run, this));
120   }
121 }
122
123 void Resolver::run()
124 {
125   cLog.msg(Log::PRIO_DEBUG) << "Resolver Thread started";
126
127   for(;;) {
128     try {
129       io_service_.run();
130       io_service_.reset();
131       boost::this_thread::sleep(boost::posix_time::milliseconds(250));
132     } catch(const std::runtime_error& e) {
133       cLog.msg(Log::PRIO_ERROR) << "resolver caught runtime error, restarting: " << e.what();
134     } catch(const std::exception& e) {
135       cLog.msg(Log::PRIO_ERROR) << "resolver caught exception, restarting: " << e.what();
136     }
137   }
138 }
139
140
141 void Resolver::resolveUdp(const std::string& addr, const std::string& port, UdpResolveCallback const& onResolve, ErrorCallback const& onError, ResolvAddrType r)
142 {
143   cLog.msg(Log::PRIO_DEBUG) << "trying to resolv UDP: '" << addr << "' '" << port << "'";
144
145   boost::shared_ptr<udp::resolver::query> query;
146   if(addr != "") {
147     switch(r) {
148     case IPV4_ONLY:
149       query = boost::shared_ptr<udp::resolver::query>(new udp::resolver::query(udp::v4(), addr, port));
150       break;
151     case IPV6_ONLY:
152       query = boost::shared_ptr<udp::resolver::query>(new udp::resolver::query(udp::v6(), addr, port));
153       break;
154     default:
155       query = boost::shared_ptr<udp::resolver::query>(new udp::resolver::query(addr, port));
156       break;
157     }
158   } else {
159     switch(r) {
160     case IPV4_ONLY:
161       query = boost::shared_ptr<udp::resolver::query>(new udp::resolver::query(udp::v4(), port));
162       break;
163     case IPV6_ONLY:
164       query = boost::shared_ptr<udp::resolver::query>(new udp::resolver::query(udp::v6(), port));
165       break;
166     default:
167       query = boost::shared_ptr<udp::resolver::query>(new udp::resolver::query(port));
168       break;
169     }
170   }
171   UdpResolveHandler handler(addr, port, onResolve, onError, r);
172   udp_resolver_.async_resolve(*query, handler);
173 }
174
175 void Resolver::resolveTcp(const std::string& addr, const std::string& port, TcpResolveCallback const& onResolve, ErrorCallback const& onError, ResolvAddrType r)
176 {
177   cLog.msg(Log::PRIO_DEBUG) << "trying to resolv TCP: '" << addr << "' '" << port << "'";
178
179   boost::shared_ptr<tcp::resolver::query> query;
180   if(addr != "") {
181     switch(r) {
182     case IPV4_ONLY:
183       query = boost::shared_ptr<tcp::resolver::query>(new tcp::resolver::query(tcp::v4(), addr, port));
184       break;
185     case IPV6_ONLY:
186       query = boost::shared_ptr<tcp::resolver::query>(new tcp::resolver::query(tcp::v6(), addr, port));
187       break;
188     default:
189       query = boost::shared_ptr<tcp::resolver::query>(new tcp::resolver::query(addr, port));
190       break;
191     }
192   } else {
193     switch(r) {
194     case IPV4_ONLY:
195       query = boost::shared_ptr<tcp::resolver::query>(new tcp::resolver::query(tcp::v4(), port));
196       break;
197     case IPV6_ONLY:
198       query = boost::shared_ptr<tcp::resolver::query>(new tcp::resolver::query(tcp::v6(), port));
199       break;
200     default:
201       query = boost::shared_ptr<tcp::resolver::query>(new tcp::resolver::query(port));
202       break;
203     }
204   }
205   TcpResolveHandler handler(addr, port, onResolve, onError, r);
206   tcp_resolver_.async_resolve(*query, handler);
207 }