summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/anytun.cpp7
-rw-r--r--src/packetSource.cpp10
-rw-r--r--src/packetSource.h1
-rw-r--r--src/resolver.cpp29
-rw-r--r--src/resolver.h13
5 files changed, 38 insertions, 22 deletions
diff --git a/src/anytun.cpp b/src/anytun.cpp
index 99f1560..5715d5a 100644
--- a/src/anytun.cpp
+++ b/src/anytun.cpp
@@ -97,6 +97,11 @@ void createConnection(const PacketSourceEndpoint & remote_end, window_size_t seq
#endif
}
+void createConnectionError(const std::exception& e)
+{
+ gSignalController.inject(SIGERROR, e.what());
+}
+
#ifndef ANYTUN_NOSYNC
void syncConnector(const OptionHost& connto)
{
@@ -463,7 +468,7 @@ int main(int argc, char* argv[])
src = new UDPPacketSource(gOpt.getLocalAddr(), gOpt.getLocalPort());
if(gOpt.getRemoteAddr() != "")
- gResolver.resolveUdp(gOpt.getRemoteAddr(), gOpt.getRemotePort(), boost::bind(createConnection, _1, gOpt.getSeqWindowSize(), gOpt.getMux()), gOpt.getResolvAddrType());
+ gResolver.resolveUdp(gOpt.getRemoteAddr(), gOpt.getRemotePort(), boost::bind(createConnection, _1, gOpt.getSeqWindowSize(), gOpt.getMux()), boost::bind(createConnectionError, _1), gOpt.getResolvAddrType());
HostList connect_to = gOpt.getRemoteSyncHosts();
#ifndef NO_ROUTING
diff --git a/src/packetSource.cpp b/src/packetSource.cpp
index 5542ee9..e291175 100644
--- a/src/packetSource.cpp
+++ b/src/packetSource.cpp
@@ -37,6 +37,7 @@
#include "log.h"
#include "resolver.h"
#include "options.h"
+#include "signalController.h"
void PacketSource::waitUntilReady()
{
@@ -45,12 +46,12 @@ void PacketSource::waitUntilReady()
UDPPacketSource::UDPPacketSource(std::string port) : sock_(io_service_)
{
- gResolver.resolveUdp("", port, boost::bind(&UDPPacketSource::onResolve, this, _1), gOpt.getResolvAddrType());
+ gResolver.resolveUdp("", port, boost::bind(&UDPPacketSource::onResolve, this, _1), boost::bind(&UDPPacketSource::onError, this, _1), gOpt.getResolvAddrType());
}
UDPPacketSource::UDPPacketSource(std::string localaddr, std::string port) : sock_(io_service_)
{
- gResolver.resolveUdp(localaddr, port, boost::bind(&UDPPacketSource::onResolve, this, _1), gOpt.getResolvAddrType());
+ gResolver.resolveUdp(localaddr, port, boost::bind(&UDPPacketSource::onResolve, this, _1), boost::bind(&UDPPacketSource::onError, this, _1), gOpt.getResolvAddrType());
}
void UDPPacketSource::onResolve(const boost::asio::ip::udp::endpoint& e)
@@ -61,6 +62,11 @@ void UDPPacketSource::onResolve(const boost::asio::ip::udp::endpoint& e)
ready_sem_.up();
}
+void UDPPacketSource::onError(const std::runtime_error& e)
+{
+ gSignalController.inject(SIGERROR, e.what());
+}
+
u_int32_t UDPPacketSource::recv(u_int8_t* buf, u_int32_t len, PacketSourceEndpoint& remote)
{
return static_cast<u_int32_t>(sock_.receive_from(boost::asio::buffer(buf, len), remote));
diff --git a/src/packetSource.h b/src/packetSource.h
index 94c6c45..6909125 100644
--- a/src/packetSource.h
+++ b/src/packetSource.h
@@ -66,6 +66,7 @@ public:
void send(u_int8_t* buf, u_int32_t len, PacketSourceEndpoint remote);
void onResolve(const boost::asio::ip::udp::endpoint& e);
+ void onError(const std::runtime_error& e);
private:
diff --git a/src/resolver.cpp b/src/resolver.cpp
index 5e2d862..06db1b4 100644
--- a/src/resolver.cpp
+++ b/src/resolver.cpp
@@ -34,34 +34,33 @@
#include "resolver.h"
#include "log.h"
-#include "signalController.h"
using ::boost::asio::ip::udp;
using ::boost::asio::ip::tcp;
template<class Proto>
-void waitAndEnqueue(u_int32_t s, const std::string& addr, const std::string& port, boost::function<void(boost::asio::ip::basic_endpoint<Proto>)> const& onResolve, ResolvAddrType r)
+void waitAndEnqueue(u_int32_t s, const std::string& addr, const std::string& port, boost::function<void(boost::asio::ip::basic_endpoint<Proto>)> const& onResolve, ErrorCallback const& onError, ResolvAddrType r)
{
cLog.msg(Log::PRIO_ERROR) << "the resolver only supports udp and tcp";
}
template<>
-void waitAndEnqueue(u_int32_t s, const std::string& addr, const std::string& port, boost::function<void(boost::asio::ip::basic_endpoint<udp>)> const& onResolve, ResolvAddrType r)
+void waitAndEnqueue(u_int32_t s, const std::string& addr, const std::string& port, boost::function<void(boost::asio::ip::basic_endpoint<udp>)> const& onResolve, ErrorCallback const& onError, ResolvAddrType r)
{
boost::this_thread::sleep(boost::posix_time::milliseconds(s * 1000));
- gResolver.resolveUdp(addr, port, onResolve, r);
+ gResolver.resolveUdp(addr, port, onResolve, onError, r);
}
template<>
-void waitAndEnqueue(u_int32_t s, const std::string& addr, const std::string& port, boost::function<void(boost::asio::ip::basic_endpoint<tcp>)> const& onResolve, ResolvAddrType r)
+void waitAndEnqueue(u_int32_t s, const std::string& addr, const std::string& port, boost::function<void(boost::asio::ip::basic_endpoint<tcp>)> const& onResolve, ErrorCallback const& onError, ResolvAddrType r)
{
boost::this_thread::sleep(boost::posix_time::milliseconds(s * 1000));
- gResolver.resolveTcp(addr, port, onResolve, r);
+ gResolver.resolveTcp(addr, port, onResolve, onError, r);
}
template<class Proto>
-ResolveHandler<Proto>::ResolveHandler(const std::string& addr, const std::string& port, boost::function<void(boost::asio::ip::basic_endpoint<Proto>)> const& onResolve, ResolvAddrType r) : addr_(addr), port_(port), callback_(onResolve), resolv_addr_type_(r)
+ResolveHandler<Proto>::ResolveHandler(const std::string& addr, const std::string& port, boost::function<void(boost::asio::ip::basic_endpoint<Proto>)> const& onResolve, ErrorCallback const& onError, ResolvAddrType r) : addr_(addr), port_(port), onResolve_(onResolve), onError_(onError), resolv_addr_type_(r)
{
}
@@ -70,15 +69,15 @@ void ResolveHandler<Proto>::operator()(const boost::system::error_code& e, const
{
if(boost::system::posix_error::success == e) {
try {
- callback_(*endpointIt);
+ onResolve_(*endpointIt);
}
catch(const std::runtime_error& e)
{
- gSignalController.inject(SIGERROR, e.what());
+ onError_(e);
}
} else {
- cLog.msg(Log::PRIO_ERROR) << "Error while resolving '" << addr_ << "' '" << port_ << "', retrying in 10 sec.";
- boost::thread(boost::bind(waitAndEnqueue<Proto>, 10, addr_, port_, callback_, resolv_addr_type_));
+ cLog.msg(Log::PRIO_ERROR) << "Error while resolving '" << addr_ << "' '" << port_ << "', retrying in 10 sec.";
+ boost::thread(boost::bind(waitAndEnqueue<Proto>, 10, addr_, port_, onResolve_, onError_, resolv_addr_type_));
}
}
@@ -134,7 +133,7 @@ void Resolver::run()
}
-void Resolver::resolveUdp(const std::string& addr, const std::string& port, boost::function<void (udp::endpoint)> const& onResolve, ResolvAddrType r)
+void Resolver::resolveUdp(const std::string& addr, const std::string& port, UdpResolveCallback const& onResolve, ErrorCallback const& onError, ResolvAddrType r)
{
cLog.msg(Log::PRIO_DEBUG) << "trying to resolv UDP: '" << addr << "' '" << port << "'";
@@ -153,11 +152,11 @@ void Resolver::resolveUdp(const std::string& addr, const std::string& port, boos
default: query = std::auto_ptr<udp::resolver::query>(new udp::resolver::query(port)); break;
}
}
- UdpResolveHandler handler(addr, port, onResolve, r);
+ UdpResolveHandler handler(addr, port, onResolve, onError, r);
udp_resolver_.async_resolve(*query, handler);
}
-void Resolver::resolveTcp(const std::string& addr, const std::string& port, boost::function<void (tcp::endpoint)> const& onResolve, ResolvAddrType r)
+void Resolver::resolveTcp(const std::string& addr, const std::string& port, TcpResolveCallback const& onResolve, ErrorCallback const& onError, ResolvAddrType r)
{
cLog.msg(Log::PRIO_DEBUG) << "trying to resolv TCP: '" << addr << "' '" << port << "'";
@@ -176,6 +175,6 @@ void Resolver::resolveTcp(const std::string& addr, const std::string& port, boos
default: query = std::auto_ptr<tcp::resolver::query>(new tcp::resolver::query(port)); break;
}
}
- TcpResolveHandler handler(addr, port, onResolve, r);
+ TcpResolveHandler handler(addr, port, onResolve, onError, r);
tcp_resolver_.async_resolve(*query, handler);
}
diff --git a/src/resolver.h b/src/resolver.h
index 8a64738..b5553bc 100644
--- a/src/resolver.h
+++ b/src/resolver.h
@@ -39,17 +39,22 @@
#include "datatypes.h"
#include "threadUtils.hpp"
+typedef boost::function<void (boost::asio::ip::udp::endpoint)> UdpResolveCallback;
+typedef boost::function<void (boost::asio::ip::tcp::endpoint)> TcpResolveCallback;
+typedef boost::function<void (std::runtime_error const&)> ErrorCallback;
+
template<class Proto>
class ResolveHandler
{
public:
- ResolveHandler(const std::string& addr, const std::string& port, boost::function<void (boost::asio::ip::basic_endpoint<Proto>)> const& onResolve, ResolvAddrType r = ANY);
+ ResolveHandler(const std::string& addr, const std::string& port, boost::function<void (boost::asio::ip::basic_endpoint<Proto>)> const& onResolve, ErrorCallback const& onError, ResolvAddrType r = ANY);
void operator()(const boost::system::error_code& e, const boost::asio::ip::basic_resolver_iterator<Proto>);
private:
std::string addr_;
std::string port_;
- boost::function<void (boost::asio::ip::basic_endpoint<Proto>)> callback_;
+ boost::function<void (boost::asio::ip::basic_endpoint<Proto>)> onResolve_;
+ ErrorCallback onError_;
ResolvAddrType resolv_addr_type_;
};
@@ -64,8 +69,8 @@ public:
void init();
void run();
- void resolveUdp(const std::string& addr, const std::string& port, boost::function<void (boost::asio::ip::udp::endpoint)> const& onResolve, ResolvAddrType r = ANY);
- void resolveTcp(const std::string& addr, const std::string& port, boost::function<void (boost::asio::ip::tcp::endpoint)> const& onResolve, ResolvAddrType r = ANY);
+ void resolveUdp(const std::string& addr, const std::string& port, UdpResolveCallback const& onResolve, ErrorCallback const& onError, ResolvAddrType r = ANY);
+ void resolveTcp(const std::string& addr, const std::string& port, TcpResolveCallback const& onResolve, ErrorCallback const& onError, ResolvAddrType r = ANY);
private:
Resolver();