From ff9f5aee4b685b8c705bd39d5768eec75a50c5c2 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Fri, 8 Jan 2010 03:48:56 +0000 Subject: UDPPacketSource now opens one socket for each resolved endpoint --- src/anytun-config.cpp | 2 +- src/anytun.cpp | 2 +- src/packetSource.cpp | 34 ++++++++++++++++++++++++++-------- src/packetSource.h | 6 ++++-- src/resolver.cpp | 10 +++++----- src/resolver.h | 8 ++++---- src/syncServer.cpp | 2 +- src/syncServer.h | 2 +- 8 files changed, 43 insertions(+), 23 deletions(-) diff --git a/src/anytun-config.cpp b/src/anytun-config.cpp index 4ef4112..f69e883 100644 --- a/src/anytun-config.cpp +++ b/src/anytun-config.cpp @@ -88,7 +88,7 @@ void createConnection(const PacketSourceEndpoint & remote_end, ConnectionList & sem.up(); } -void createConnectionResolver(const PacketSourceResolverIt& it, ConnectionList & cl, u_int16_t seqSize, SyncQueue & queue, mux_t mux, Semaphore& sem) +void createConnectionResolver(PacketSourceResolverIt& it, ConnectionList & cl, u_int16_t seqSize, SyncQueue & queue, mux_t mux, Semaphore& sem) { createConnection(*it, cl, seqSize, queue, mux, sem); } diff --git a/src/anytun.cpp b/src/anytun.cpp index 200b33d..bdd5b65 100644 --- a/src/anytun.cpp +++ b/src/anytun.cpp @@ -97,7 +97,7 @@ void createConnection(const PacketSourceEndpoint& remote_end, window_size_t seqS #endif } -void createConnectionResolver(const PacketSourceResolverIt& it, window_size_t seqSize, mux_t mux) +void createConnectionResolver(PacketSourceResolverIt& it, window_size_t seqSize, mux_t mux) { createConnection(*it, seqSize, mux); } diff --git a/src/packetSource.cpp b/src/packetSource.cpp index c9a0412..7c66d0f 100644 --- a/src/packetSource.cpp +++ b/src/packetSource.cpp @@ -45,18 +45,36 @@ void PacketSource::waitUntilReady() ready_sem_.down(); } -UDPPacketSource::UDPPacketSource(std::string localaddr, std::string port) : sock_(io_service_) +UDPPacketSource::UDPPacketSource(std::string localaddr, std::string port) { gResolver.resolveUdp(localaddr, port, boost::bind(&UDPPacketSource::onResolve, this, _1), boost::bind(&UDPPacketSource::onError, this, _1), gOpt.getResolvAddrType()); } -void UDPPacketSource::onResolve(const PacketSourceResolverIt& it) +UDPPacketSource::~UDPPacketSource() { - PacketSourceEndpoint e = *it; + std::list::iterator it = sockets_.begin(); + for(;it != sockets_.end(); ++it) + delete *it; +} + +void UDPPacketSource::onResolve(PacketSourceResolverIt& it) +{ + while(it != PacketSourceResolverIt()) { + PacketSourceEndpoint e = *it; + cLog.msg(Log::PRIO_NOTICE) << "opening socket: " << e; + + proto::socket* sock = new proto::socket(io_service_); + sock->open(e.protocol()); + if(e.protocol() == proto::v6()) { + boost::asio::ip::v6_only option(true); + sock->set_option(option); + } + sock->bind(e); + sockets_.push_back(sock); + + it++; + } - cLog.msg(Log::PRIO_NOTICE) << "opening socket: " << e; - sock_.open(e.protocol()); - sock_.bind(e); ready_sem_.up(); } @@ -67,11 +85,11 @@ void UDPPacketSource::onError(const std::runtime_error& e) u_int32_t UDPPacketSource::recv(u_int8_t* buf, u_int32_t len, PacketSourceEndpoint& remote) { - return static_cast(sock_.receive_from(boost::asio::buffer(buf, len), remote)); + return static_cast(sockets_.front()->receive_from(boost::asio::buffer(buf, len), remote)); } void UDPPacketSource::send(u_int8_t* buf, u_int32_t len, PacketSourceEndpoint remote) { - sock_.send_to(boost::asio::buffer(buf, len), remote); + sockets_.front()->send_to(boost::asio::buffer(buf, len), remote); } diff --git a/src/packetSource.h b/src/packetSource.h index f388517..ffc14bf 100644 --- a/src/packetSource.h +++ b/src/packetSource.h @@ -34,6 +34,7 @@ #define ANYTUN_packetSource_h_INCLUDED #include +#include #include "datatypes.h" #include "threadUtils.hpp" @@ -62,17 +63,18 @@ public: typedef boost::asio::ip::udp proto; UDPPacketSource(std::string localaddr, std::string port); + ~UDPPacketSource(); u_int32_t recv(u_int8_t* buf, u_int32_t len, PacketSourceEndpoint& remote); void send(u_int8_t* buf, u_int32_t len, PacketSourceEndpoint remote); - void onResolve(const PacketSourceResolverIt& it); + void onResolve(PacketSourceResolverIt& it); void onError(const std::runtime_error& e); private: boost::asio::io_service io_service_; - proto::socket sock_; + std::list sockets_; }; #endif diff --git a/src/resolver.cpp b/src/resolver.cpp index 36b2c2e..55371d0 100644 --- a/src/resolver.cpp +++ b/src/resolver.cpp @@ -40,20 +40,20 @@ using ::boost::asio::ip::udp; using ::boost::asio::ip::tcp; template -void waitAndEnqueue(u_int32_t s, const std::string& addr, const std::string& port, boost::function)> const& onResolve, ErrorCallback const& onError, ResolvAddrType r) +void waitAndEnqueue(u_int32_t s, const std::string& addr, const std::string& port, boost::function)> 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)> const& onResolve, ErrorCallback const& onError, ResolvAddrType r) +void waitAndEnqueue(u_int32_t s, const std::string& addr, const std::string& port, boost::function)> const& onResolve, ErrorCallback const& onError, ResolvAddrType r) { boost::this_thread::sleep(boost::posix_time::milliseconds(s * 1000)); gResolver.resolveUdp(addr, port, onResolve, onError, r); } template<> -void waitAndEnqueue(u_int32_t s, const std::string& addr, const std::string& port, boost::function)> const& onResolve, ErrorCallback const& onError, ResolvAddrType r) +void waitAndEnqueue(u_int32_t s, const std::string& addr, const std::string& port, boost::function)> const& onResolve, ErrorCallback const& onError, ResolvAddrType r) { boost::this_thread::sleep(boost::posix_time::milliseconds(s * 1000)); gResolver.resolveTcp(addr, port, onResolve, onError, r); @@ -61,12 +61,12 @@ void waitAndEnqueue(u_int32_t s, const std::string& addr, const std::string& por template -ResolveHandler::ResolveHandler(const std::string& addr, const std::string& port, boost::function)> const& onResolve, ErrorCallback const& onError, ResolvAddrType r) : addr_(addr), port_(port), onResolve_(onResolve), onError_(onError), resolv_addr_type_(r) +ResolveHandler::ResolveHandler(const std::string& addr, const std::string& port, boost::function)> const& onResolve, ErrorCallback const& onError, ResolvAddrType r) : addr_(addr), port_(port), onResolve_(onResolve), onError_(onError), resolv_addr_type_(r) { } template -void ResolveHandler::operator()(const boost::system::error_code& e, const boost::asio::ip::basic_resolver_iterator endpointIt) +void ResolveHandler::operator()(const boost::system::error_code& e, boost::asio::ip::basic_resolver_iterator endpointIt) { if(boost::system::posix_error::success == e) { try { diff --git a/src/resolver.h b/src/resolver.h index ea4a57b..35c2bc4 100644 --- a/src/resolver.h +++ b/src/resolver.h @@ -40,16 +40,16 @@ #include "datatypes.h" #include "threadUtils.hpp" -typedef boost::function UdpResolveCallback; -typedef boost::function TcpResolveCallback; +typedef boost::function UdpResolveCallback; +typedef boost::function TcpResolveCallback; typedef boost::function ErrorCallback; template class ResolveHandler { public: - ResolveHandler(const std::string& addr, const std::string& port, boost::function)> const& onResolve, ErrorCallback const& onError, ResolvAddrType r = ANY); - void operator()(const boost::system::error_code& e, const boost::asio::ip::basic_resolver_iterator); + ResolveHandler(const std::string& addr, const std::string& port, boost::function)> const& onResolve, ErrorCallback const& onError, ResolvAddrType r = ANY); + void operator()(const boost::system::error_code& e, boost::asio::ip::basic_resolver_iterator); private: std::string addr_; diff --git a/src/syncServer.cpp b/src/syncServer.cpp index ecf9fb4..893ceba 100644 --- a/src/syncServer.cpp +++ b/src/syncServer.cpp @@ -42,7 +42,7 @@ SyncServer::SyncServer(std::string localaddr, std::string port, ConnectCallback gResolver.resolveTcp(localaddr, port, boost::bind(&SyncServer::onResolve, this, _1), boost::bind(&SyncServer::onResolvError, this, _1)); } -void SyncServer::onResolve(const SyncTcpConnection::proto::resolver::iterator& it) +void SyncServer::onResolve(SyncTcpConnection::proto::resolver::iterator& it) { SyncTcpConnection::proto::endpoint e = *it; diff --git a/src/syncServer.h b/src/syncServer.h index dc007d6..3157d15 100644 --- a/src/syncServer.h +++ b/src/syncServer.h @@ -50,7 +50,7 @@ class SyncServer { public: SyncServer(std::string localaddr, std::string port, ConnectCallback onConnect); - void onResolve(const SyncTcpConnection::proto::resolver::iterator& it); + void onResolve(SyncTcpConnection::proto::resolver::iterator& it); void onResolvError(const std::runtime_error& e); void run(); -- cgit v1.2.3