From 74f2885ade3fb44784e1fbc2fd0575f69728077c Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Thu, 5 Mar 2009 23:47:47 +0000 Subject: changed packetSource to use gResolver --- src/anytun.cpp | 53 ++++++++++++++++++++++++------------------------ src/anytun.vcproj | 4 ++-- src/packetSource.cpp | 28 +++++++++++++++---------- src/packetSource.h | 10 ++++++++- src/signalController.cpp | 3 --- src/signalController.h | 4 ---- 6 files changed, 55 insertions(+), 47 deletions(-) diff --git a/src/anytun.cpp b/src/anytun.cpp index a299a7b..ef46a5e 100644 --- a/src/anytun.cpp +++ b/src/anytun.cpp @@ -49,9 +49,7 @@ #include "cipherFactory.h" #include "authAlgoFactory.h" #include "keyDerivationFactory.h" -#ifndef NO_SIGNALCONTROLLER #include "signalController.h" -#endif #ifdef WIN_SERVICE #include "win32/winService.h" #endif @@ -325,6 +323,28 @@ void receiver(TunDevice* dev, PacketSource* src) } } +void startSendRecvThreads(PrivInfo& privs, TunDevice* dev, PacketSource* src) +{ + src->waitUntilReady(); + +#ifndef NO_DAEMON + if(gOpt.getChrootDir() != "") { + try { + do_chroot(gOpt.getChrootDir()); + } + catch(const std::runtime_error& e) { + cLog.msg(Log::PRIO_WARNING) << "ignroing chroot error: " << e.what(); + } + } +#ifndef NO_PRIVDROP + privs.drop(); +#endif +#endif + + boost::thread(boost::bind(sender, dev, src)); + boost::thread(boost::bind(receiver, dev, src)); +} + #ifdef WIN_SERVICE int main(int argc, char* argv[]) { @@ -411,10 +431,8 @@ int main(int argc, char* argv[]) } #endif -#ifndef NO_SIGNALCONTROLLER // this has to be called before the first thread is started gSignalController.init(); -#endif gResolver.init(); #ifndef NO_CRYPT @@ -474,34 +492,17 @@ int main(int argc, char* argv[]) } #endif -#ifndef NO_DAEMON - if(gOpt.getChrootDir() != "") { - try { - do_chroot(gOpt.getChrootDir()); - } - catch(const std::runtime_error& e) { - cLog.msg(Log::PRIO_WARNING) << "ignroing chroot error: " << e.what(); - } - } -#ifndef NO_PRIVDROP - privs.drop(); -#endif -#endif - - boost::thread senderThread(boost::bind(sender, &dev, src)); -#if defined(WIN_SERVICE) || !defined(NO_SIGNALCONTROLLER) - boost::thread receiverThread(boost::bind(receiver, &dev, src)); -#endif + // wait for packet source to finish in a seperate thread in order + // to be still able to process signals while waiting + boost::thread(boost::bind(startSendRecvThreads, privs, &dev, src)); #if defined(WIN_SERVICE) int ret = 0; gWinService.waitForStop(); -#elif !defined(NO_SIGNALCONTROLLER) - int ret = gSignalController.run(); #else - receiver(dev, *src); - int ret = 0; + int ret = gSignalController.run(); #endif + // TODO cleanup threads here! /* pthread_cancel(senderThread); diff --git a/src/anytun.vcproj b/src/anytun.vcproj index 57b141a..ea6a923 100644 --- a/src/anytun.vcproj +++ b/src/anytun.vcproj @@ -189,7 +189,7 @@ Name="VCCLCompilerTool" AdditionalOptions="/I "C:\Program Files\boost\boost_1_35_0\"" Optimization="0" - PreprocessorDefinitions="LOG_FILE;LOG_STDOUT;LOG_WINEVENTLOG;WIN_SERVICE;USE_SSL_CRYPTO;NO_DAEMON;NO_EXEC;NO_SIGNALCONTROLLER;WIN32_LEAN_AND_MEAN;BOOST_ALL_DYN_LINK" + PreprocessorDefinitions="LOG_FILE;LOG_STDOUT;LOG_WINEVENTLOG;WIN_SERVICE;USE_SSL_CRYPTO;NO_DAEMON;NO_EXEC;WIN32_LEAN_AND_MEAN;BOOST_ALL_DYN_LINK" MinimalRebuild="true" BasicRuntimeChecks="3" RuntimeLibrary="3" @@ -264,7 +264,7 @@ /> +#include #include "datatypes.h" #include "packetSource.h" #include "log.h" +#include "resolver.h" +#include "options.h" + +void PacketSource::waitUntilReady() +{ + ready_sem_.down(); +} UDPPacketSource::UDPPacketSource(std::string port) : sock_(io_service_) { - proto::resolver resolver(io_service_); - proto::resolver::query query(port); - proto::endpoint e = *resolver.resolve(query); - cLog.msg(Log::PRIO_NOTICE) << "openeing socket: " << e; - sock_.open(e.protocol()); - sock_.bind(e); + gResolver.resolveUdp("", port, boost::bind(&UDPPacketSource::onResolve, this, _1), gOpt.getResolvAddrType()); } UDPPacketSource::UDPPacketSource(std::string localaddr, std::string port) : sock_(io_service_) { - proto::resolver resolver(io_service_); - proto::resolver::query query(localaddr, port); - proto::endpoint e = *resolver.resolve(query); - cLog.msg(Log::PRIO_NOTICE) << "openeing socket: " << e; + gResolver.resolveUdp(localaddr, port, boost::bind(&UDPPacketSource::onResolve, this, _1), gOpt.getResolvAddrType()); +} + +void UDPPacketSource::onResolve(const boost::asio::ip::udp::endpoint& e) +{ + cLog.msg(Log::PRIO_NOTICE) << "opening socket: " << e; sock_.open(e.protocol()); - sock_.bind(e); + sock_.bind(e); + ready_sem_.up(); } u_int32_t UDPPacketSource::recv(u_int8_t* buf, u_int32_t len, PacketSourceEndpoint& remote) diff --git a/src/packetSource.h b/src/packetSource.h index ce6e997..94c6c45 100644 --- a/src/packetSource.h +++ b/src/packetSource.h @@ -35,8 +35,9 @@ #include #include "datatypes.h" +#include "threadUtils.hpp" -// TODO: fix this +// TODO: fix this when other packetSource types are introduced typedef boost::asio::ip::udp::endpoint PacketSourceEndpoint; class PacketSource @@ -46,6 +47,11 @@ public: virtual u_int32_t recv(u_int8_t* buf, u_int32_t len, PacketSourceEndpoint& remote) = 0; virtual void send(u_int8_t* buf, u_int32_t len, PacketSourceEndpoint remote) = 0; + + void waitUntilReady(); + +protected: + Semaphore ready_sem_; }; class UDPPacketSource : public PacketSource @@ -59,6 +65,8 @@ public: 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 boost::asio::ip::udp::endpoint& e); + private: boost::asio::io_service io_service_; diff --git a/src/signalController.cpp b/src/signalController.cpp index 2ae5791..e8d231e 100644 --- a/src/signalController.cpp +++ b/src/signalController.cpp @@ -29,8 +29,6 @@ * along with anytun. If not, see . */ -#ifndef NO_SIGNALCONTROLLER - #include #include @@ -241,4 +239,3 @@ int SignalController::run() return 0; } -#endif diff --git a/src/signalController.h b/src/signalController.h index 1f9cae3..ba46de0 100644 --- a/src/signalController.h +++ b/src/signalController.h @@ -32,8 +32,6 @@ #ifndef _SIGNAL_CONTROLLER_H_ #define _SIGNAL_CONTROLLER_H_ -#ifndef NO_SIGNALCONTROLLER - #include #include @@ -188,5 +186,3 @@ private: extern SignalController& gSignalController; #endif - -#endif -- cgit v1.2.3