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.
14 * Copyright (C) 2007-2014 Markus Grüneis, Othmar Gsenger, Erwin Nindl,
15 * Christian Pointner <satp@wirdorange.org>
17 * This file is part of Anytun.
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
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.
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/>.
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
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.
46 #include <boost/bind.hpp>
47 #define BOOST_SYSTEM_ENABLE_DEPRECATED
48 #include <boost/system/error_code.hpp>
53 using ::boost::asio::ip::udp;
54 using ::boost::asio::ip::tcp;
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)
59 cLog.msg(Log::PRIO_ERROR) << "the resolver only supports udp and tcp";
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)
65 boost::this_thread::sleep(boost::posix_time::milliseconds(s * 1000));
66 gResolver.resolveUdp(addr, port, onResolve, onError, r);
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)
72 boost::this_thread::sleep(boost::posix_time::milliseconds(s * 1000));
73 gResolver.resolveTcp(addr, port, onResolve, onError, r);
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)
83 void ResolveHandler<Proto>::operator()(const boost::system::error_code& e, boost::asio::ip::basic_resolver_iterator<Proto> endpointIt)
85 if(boost::system::posix_error::success == e) {
87 onResolve_(endpointIt);
88 } catch(const std::runtime_error& e) {
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_));
97 Resolver& gResolver = Resolver::instance();
99 Resolver& Resolver::instance()
101 static Resolver instance;
105 Resolver::Resolver() : udp_resolver_(io_service_), tcp_resolver_(io_service_), thread_(NULL)
109 Resolver::~Resolver()
116 void Resolver::init()
119 thread_ = new boost::thread(boost::bind(&Resolver::run, this));
125 cLog.msg(Log::PRIO_DEBUG) << "Resolver Thread started";
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();
141 void Resolver::resolveUdp(const std::string& addr, const std::string& port, UdpResolveCallback const& onResolve, ErrorCallback const& onError, ResolvAddrType r)
143 cLog.msg(Log::PRIO_DEBUG) << "trying to resolv UDP: '" << addr << "' '" << port << "'";
145 boost::shared_ptr<udp::resolver::query> query;
149 query = boost::shared_ptr<udp::resolver::query>(new udp::resolver::query(udp::v4(), addr, port));
152 query = boost::shared_ptr<udp::resolver::query>(new udp::resolver::query(udp::v6(), addr, port));
155 query = boost::shared_ptr<udp::resolver::query>(new udp::resolver::query(addr, port));
161 query = boost::shared_ptr<udp::resolver::query>(new udp::resolver::query(udp::v4(), port));
164 query = boost::shared_ptr<udp::resolver::query>(new udp::resolver::query(udp::v6(), port));
167 query = boost::shared_ptr<udp::resolver::query>(new udp::resolver::query(port));
171 UdpResolveHandler handler(addr, port, onResolve, onError, r);
172 udp_resolver_.async_resolve(*query, handler);
175 void Resolver::resolveTcp(const std::string& addr, const std::string& port, TcpResolveCallback const& onResolve, ErrorCallback const& onError, ResolvAddrType r)
177 cLog.msg(Log::PRIO_DEBUG) << "trying to resolv TCP: '" << addr << "' '" << port << "'";
179 boost::shared_ptr<tcp::resolver::query> query;
183 query = boost::shared_ptr<tcp::resolver::query>(new tcp::resolver::query(tcp::v4(), addr, port));
186 query = boost::shared_ptr<tcp::resolver::query>(new tcp::resolver::query(tcp::v6(), addr, port));
189 query = boost::shared_ptr<tcp::resolver::query>(new tcp::resolver::query(addr, port));
195 query = boost::shared_ptr<tcp::resolver::query>(new tcp::resolver::query(tcp::v4(), port));
198 query = boost::shared_ptr<tcp::resolver::query>(new tcp::resolver::query(tcp::v6(), port));
201 query = boost::shared_ptr<tcp::resolver::query>(new tcp::resolver::query(port));
205 TcpResolveHandler handler(addr, port, onResolve, onError, r);
206 tcp_resolver_.async_resolve(*query, handler);