summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/anytun.cpp53
-rw-r--r--src/anytun.vcproj4
-rw-r--r--src/packetSource.cpp28
-rw-r--r--src/packetSource.h10
-rw-r--r--src/signalController.cpp3
-rw-r--r--src/signalController.h4
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 &quot;C:\Program Files\boost\boost_1_35_0\&quot;"
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 @@
/>
<Tool
Name="VCCLCompilerTool"
- 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"
RuntimeLibrary="2"
UsePrecompiledHeader="0"
WarningLevel="3"
diff --git a/src/packetSource.cpp b/src/packetSource.cpp
index 2541894..5542ee9 100644
--- a/src/packetSource.cpp
+++ b/src/packetSource.cpp
@@ -30,29 +30,35 @@
*/
#include <boost/asio.hpp>
+#include <boost/bind.hpp>
#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 <boost/asio.hpp>
#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 <http://www.gnu.org/licenses/>.
*/
-#ifndef NO_SIGNALCONTROLLER
-
#include <map>
#include <iostream>
@@ -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 <map>
#include <queue>
@@ -188,5 +186,3 @@ private:
extern SignalController& gSignalController;
#endif
-
-#endif