diff options
-rw-r--r-- | src/Makefile | 1 | ||||
-rwxr-xr-x | src/configure | 8 | ||||
-rw-r--r-- | src/posix/signalHandler.hpp | 153 | ||||
-rw-r--r-- | src/signalController.cpp | 152 | ||||
-rw-r--r-- | src/signalController.h | 92 | ||||
-rw-r--r-- | src/win32/signalHandler.hpp | 111 |
6 files changed, 276 insertions, 241 deletions
diff --git a/src/Makefile b/src/Makefile index a53336e..cf5822c 100644 --- a/src/Makefile +++ b/src/Makefile @@ -178,6 +178,7 @@ anyrtpproxy: anytun distclean: cleanall find . -name *.o -exec rm -f {} \; rm -f config.sub config.guess + rm -f signalHandler.hpp rm -f tunDevice.cpp rm -f include.mk diff --git a/src/configure b/src/configure index 24dcd4e..1f5f024 100755 --- a/src/configure +++ b/src/configure @@ -144,13 +144,17 @@ fi rm -f include.mk case $TARGET in Linux) - rm -rf tunDevice.cpp + rm -f tunDevice.cpp ln -sf linux/tunDevice.cpp + rm -f signalHandler.hpp + ln -sf posix/signalHandler.hpp echo "loading Linux specific TUN Device" ;; OpenBSD|FreeBSD|NetBSD|GNU/kFreeBSD) - rm -rf tunDevice.cpp + rm -f tunDevice.cpp ln -sf bsd/tunDevice.cpp + rm -f signalHandler.hpp + ln -sf posix/signalHandler.hpp echo "loading BSD specific TUN Device" CXXFLAGS=$CXXFLAGS' -I/usr/local/include' LDFLAGS=$LDFLAGS' -L/usr/local/lib' diff --git a/src/posix/signalHandler.hpp b/src/posix/signalHandler.hpp new file mode 100644 index 0000000..92a66bd --- /dev/null +++ b/src/posix/signalHandler.hpp @@ -0,0 +1,153 @@ +/* + * anytun + * + * The secure anycast tunneling protocol (satp) defines a protocol used + * for communication between any combination of unicast and anycast + * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel + * mode and allows tunneling of every ETHER TYPE protocol (e.g. + * ethernet, ip, arp ...). satp directly includes cryptography and + * message authentication based on the methodes used by SRTP. It is + * intended to deliver a generic, scaleable and secure solution for + * tunneling and relaying of packets of any protocol. + * + * + * Copyright (C) 2007-2009 Othmar Gsenger, Erwin Nindl, + * Christian Pointner <satp@wirdorange.org> + * + * This file is part of Anytun. + * + * Anytun is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * Anytun is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with anytun. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef ANYTUN_signalHandler_h_INCLUDED +#define ANYTUN_signalHandler_h_INCLUDED + +#include <csignal> +#include <boost/thread.hpp> +#include <boost/bind.hpp> + +class SigIntHandler : public SignalHandler +{ +public: + SigIntHandler() : SignalHandler(SIGINT) {} + int handle() + { + cLog.msg(Log::PRIO_NOTICE) << "SIG-Int caught, exiting"; + return 1; + } +}; + +class SigQuitHandler : public SignalHandler +{ +public: + SigQuitHandler() : SignalHandler(SIGQUIT) {} + int handle() + { + cLog.msg(Log::PRIO_NOTICE) << "SIG-Quit caught, exiting"; + return 1; + } +}; + +class SigHupHandler : public SignalHandler +{ +public: + SigHupHandler() : SignalHandler(SIGHUP) {} + int handle() + { + cLog.msg(Log::PRIO_NOTICE) << "SIG-Hup caught"; + return 0; + } +}; + +class SigUsr1Handler : public SignalHandler +{ +public: + SigUsr1Handler() : SignalHandler(SIGUSR1) {} + int handle() + { + cLog.msg(Log::PRIO_NOTICE) << "SIG-Term caught, exiting"; + return 1; + } +}; + +class SigUsr2Handler : public SignalHandler +{ +public: + SigUsr2Handler() : SignalHandler(SIGUSR2) {} + int handle() + { + cLog.msg(Log::PRIO_NOTICE) << "SIG-Usr1 caught"; + return 0; + } +}; + +class SigTermHandler : public SignalHandler +{ +public: + SigTermHandler() : SignalHandler(SIGTERM) {} + int handle() + { + cLog.msg(Log::PRIO_NOTICE) << "SIG-Usr2 caught"; + return 0; + } +}; + +void handle() +{ + sigset_t signal_set; + int sigNum; + int err = 0; + while(1) { + sigfillset(&signal_set); + err = sigwait(&signal_set, &sigNum); + if (err) { + if (err != EINTR && errno != EINTR ) { + cLog.msg(Log::PRIO_ERROR) << "sigwait failed with error: \"" << AnytunErrno(errno) << "\" SignalHandling will be disabled"; + break; + } + } else { + gSignalController.inject(sigNum); + } + } +} + +void registerSignalHandler(SignalController& ctrl) +{ + sigset_t signal_set; + + sigfillset(&signal_set); + sigdelset(&signal_set, SIGCHLD); + sigdelset(&signal_set, SIGSEGV); + sigdelset(&signal_set, SIGBUS); + sigdelset(&signal_set, SIGFPE); + +#if defined(BOOST_HAS_PTHREADS) + pthread_sigmask(SIG_BLOCK, &signal_set, NULL); +#else +#error The signalhandler works only with pthreads +#endif + + boost::thread(boost::bind(handle)); + + ctrl.handler[SIGINT] = new SigIntHandler; + ctrl.handler[SIGQUIT] = new SigQuitHandler; + ctrl.handler[SIGHUP] = new SigHupHandler; + ctrl.handler[SIGTERM] = new SigTermHandler; + ctrl.handler[SIGUSR1] = new SigUsr1Handler; + ctrl.handler[SIGUSR2] = new SigUsr2Handler; + + cLog.msg(Log::PRIO_DEBUG) << "signal handlers are now registered"; +} + +#endif diff --git a/src/signalController.cpp b/src/signalController.cpp index 00b9bd2..d35e785 100644 --- a/src/signalController.cpp +++ b/src/signalController.cpp @@ -38,12 +38,6 @@ #include "anytunError.h" #include "threadUtils.hpp" -#ifndef _MSC_VER -#include <csignal> -#include <boost/bind.hpp> -#else -#include <windows.h> -#endif SignalController* SignalController::inst = NULL; Mutex SignalController::instMutex; @@ -66,85 +60,8 @@ int SigErrorHandler::handle(const std::string& msg) return 0; } -#ifndef _MSC_VER - -int SigIntHandler::handle() -{ - cLog.msg(Log::PRIO_NOTICE) << "SIG-Int caught, exiting"; - - return 1; -} - -int SigQuitHandler::handle() -{ - cLog.msg(Log::PRIO_NOTICE) << "SIG-Quit caught, exiting"; - - return 1; -} - -int SigHupHandler::handle() -{ - cLog.msg(Log::PRIO_NOTICE) << "SIG-Hup caught"; - - return 0; -} - -int SigTermHandler::handle() -{ - cLog.msg(Log::PRIO_NOTICE) << "SIG-Term caught, exiting"; - - return 1; -} - -int SigUsr1Handler::handle() -{ - cLog.msg(Log::PRIO_NOTICE) << "SIG-Usr1 caught"; - - return 0; -} - -int SigUsr2Handler::handle() -{ - cLog.msg(Log::PRIO_NOTICE) << "SIG-Usr2 caught"; - - return 0; -} -#else -int CtrlCHandler::handle() -{ - cLog.msg(Log::PRIO_NOTICE) << "CTRL-C Event received, exitting"; - - return 1; -} - -int CtrlBreakHandler::handle() -{ - cLog.msg(Log::PRIO_NOTICE) << "CTRL-Break Event received, ignoring"; - - return 0; -} - -int CtrlCloseHandler::handle() -{ - cLog.msg(Log::PRIO_NOTICE) << "Close Event received, exitting"; - - return 1; -} - -int CtrlLogoffHandler::handle() -{ - cLog.msg(Log::PRIO_NOTICE) << "LogOff Event received, exitting"; - - return 1; -} - -int CtrlShutdownHandler::handle() -{ - cLog.msg(Log::PRIO_NOTICE) << "Shutdown Event received, exitting"; - - return 1; -} -#endif +// use system specific signal handler +#include "signalHandler.hpp" SignalController::~SignalController() { @@ -152,72 +69,9 @@ SignalController::~SignalController() delete it->second; } -#ifndef _MSC_VER -void SignalController::handle() -{ - sigset_t signal_set; - int sigNum; - int err = 0; - while(1) - { - sigfillset(&signal_set); - err = sigwait(&signal_set, &sigNum); - if (err) - { - if (err != EINTR && errno != EINTR ) - { - cLog.msg(Log::PRIO_ERROR) << "sigwait failed with error: \"" << AnytunErrno(errno) << "\" SignalHandling will be disabled"; - break; - } - } else { - inject(sigNum); - } - } -} -#else -bool SignalController::handle(DWORD ctrlType) -{ - gSignalController.inject(ctrlType); - return true; -} -#endif - void SignalController::init() { -#ifndef _MSC_VER - sigset_t signal_set; - - sigfillset(&signal_set); - sigdelset(&signal_set, SIGCHLD); - sigdelset(&signal_set, SIGSEGV); - sigdelset(&signal_set, SIGBUS); - sigdelset(&signal_set, SIGFPE); - -#if defined(BOOST_HAS_PTHREADS) - pthread_sigmask(SIG_BLOCK, &signal_set, NULL); -#else -#error The signalhandler works only with pthreads -#endif - - boost::thread(boost::bind(&SignalController::handle, this)); - - handler[SIGINT] = new SigIntHandler; - handler[SIGQUIT] = new SigQuitHandler; - handler[SIGHUP] = new SigHupHandler; - handler[SIGTERM] = new SigTermHandler; - handler[SIGUSR1] = new SigUsr1Handler; - handler[SIGUSR2] = new SigUsr2Handler; -#else - if(!SetConsoleCtrlHandler((PHANDLER_ROUTINE)SignalController::handle, true)) - AnytunError::throwErr() << "Error on SetConsoleCtrlhandler: " << AnytunErrno(GetLastError()); - - handler[CTRL_C_EVENT] = new CtrlCHandler; - handler[CTRL_BREAK_EVENT] = new CtrlBreakHandler; - handler[CTRL_CLOSE_EVENT] = new CtrlCloseHandler; - handler[CTRL_LOGOFF_EVENT] = new CtrlLogoffHandler; - handler[CTRL_SHUTDOWN_EVENT] = new CtrlShutdownHandler; -#endif - + registerSignalHandler(*this); handler[SIGERROR] = new SigErrorHandler; } diff --git a/src/signalController.h b/src/signalController.h index 6108bea..fdf5034 100644 --- a/src/signalController.h +++ b/src/signalController.h @@ -38,10 +38,6 @@ #include "threadUtils.hpp" -#ifndef _MSC_VER -#include <csignal> -#endif - #define SIGERROR -1 class SignalHandler @@ -67,96 +63,10 @@ public: int handle(const std::string& msg); }; -#ifndef _MSC_VER -class SigIntHandler : public SignalHandler -{ -public: - SigIntHandler() : SignalHandler(SIGINT) {} - int handle(); -}; - -class SigQuitHandler : public SignalHandler -{ -public: - SigQuitHandler() : SignalHandler(SIGQUIT) {} - int handle(); -}; - -class SigHupHandler : public SignalHandler -{ -public: - SigHupHandler() : SignalHandler(SIGHUP) {} - int handle(); -}; - -class SigUsr1Handler : public SignalHandler -{ -public: - SigUsr1Handler() : SignalHandler(SIGUSR1) {} - int handle(); -}; - -class SigUsr2Handler : public SignalHandler -{ -public: - SigUsr2Handler() : SignalHandler(SIGUSR2) {} - int handle(); -}; - -class SigTermHandler : public SignalHandler -{ -public: - SigTermHandler() : SignalHandler(SIGTERM) {} - int handle(); -}; - -#else - -class CtrlCHandler : public SignalHandler -{ -public: - CtrlCHandler() : SignalHandler(CTRL_C_EVENT) {} - int handle(); -}; - -class CtrlBreakHandler : public SignalHandler -{ -public: - CtrlBreakHandler() : SignalHandler(CTRL_BREAK_EVENT) {} - int handle(); -}; - -class CtrlCloseHandler : public SignalHandler -{ -public: - CtrlCloseHandler() : SignalHandler(CTRL_BREAK_EVENT) {} - int handle(); -}; - -class CtrlLogoffHandler : public SignalHandler -{ -public: - CtrlLogoffHandler() : SignalHandler(CTRL_BREAK_EVENT) {} - int handle(); -}; - -class CtrlShutdownHandler : public SignalHandler -{ -public: - CtrlShutdownHandler() : SignalHandler(CTRL_BREAK_EVENT) {} - int handle(); -}; -#endif - class SignalController { public: static SignalController& instance(); -#ifndef _MSC_VER - void handle(); -#else - static bool handle(DWORD ctrlType); -#endif void init(); int run(); @@ -186,6 +96,8 @@ private: Semaphore sigQueueSem; HandlerMap handler; + + friend void registerSignalHandler(SignalController& ctrl); }; extern SignalController& gSignalController; diff --git a/src/win32/signalHandler.hpp b/src/win32/signalHandler.hpp new file mode 100644 index 0000000..f272f1c --- /dev/null +++ b/src/win32/signalHandler.hpp @@ -0,0 +1,111 @@ +/* + * anytun + * + * The secure anycast tunneling protocol (satp) defines a protocol used + * for communication between any combination of unicast and anycast + * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel + * mode and allows tunneling of every ETHER TYPE protocol (e.g. + * ethernet, ip, arp ...). satp directly includes cryptography and + * message authentication based on the methodes used by SRTP. It is + * intended to deliver a generic, scaleable and secure solution for + * tunneling and relaying of packets of any protocol. + * + * + * Copyright (C) 2007-2009 Othmar Gsenger, Erwin Nindl, + * Christian Pointner <satp@wirdorange.org> + * + * This file is part of Anytun. + * + * Anytun is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * Anytun is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with anytun. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef ANYTUN_signalHandler_h_INCLUDED +#define ANYTUN_signalHandler_h_INCLUDED + +#include <windows.h> + +class CtrlCHandler : public SignalHandler +{ +public: + CtrlCHandler() : SignalHandler(CTRL_C_EVENT) {} + int handle() + { + cLog.msg(Log::PRIO_NOTICE) << "CTRL-C Event received, exitting"; + return 1; + } +}; + +class CtrlBreakHandler : public SignalHandler +{ +public: + CtrlBreakHandler() : SignalHandler(CTRL_BREAK_EVENT) {} + int handle() + { + cLog.msg(Log::PRIO_NOTICE) << "CTRL-Break Event received, ignoring"; + return 0; + } +}; + +class CtrlCloseHandler : public SignalHandler +{ +public: + CtrlCloseHandler() : SignalHandler(CTRL_BREAK_EVENT) {} + int handle() + { + cLog.msg(Log::PRIO_NOTICE) << "Close Event received, exitting"; + return 1; + } +}; + +class CtrlLogoffHandler : public SignalHandler +{ +public: + CtrlLogoffHandler() : SignalHandler(CTRL_BREAK_EVENT) {} + int handle() + { + cLog.msg(Log::PRIO_NOTICE) << "LogOff Event received, exitting"; + return 1; + } +}; + +class CtrlShutdownHandler : public SignalHandler +{ +public: + CtrlShutdownHandler() : SignalHandler(CTRL_BREAK_EVENT) {} + int handle() + { + cLog.msg(Log::PRIO_NOTICE) << "Shutdown Event received, exitting"; + return 1; + } +}; + +bool handle(DWORD ctrlType) +{ + gSignalController.inject(ctrlType); + return true; +} + +void registerSignalHandler(SignalController& ctrl) +{ + if(!SetConsoleCtrlHandler((PHANDLER_ROUTINE)handle, true)) + AnytunError::throwErr() << "Error on SetConsoleCtrlhandler: " << AnytunErrno(GetLastError()); + + handler[CTRL_C_EVENT] = new CtrlCHandler; + handler[CTRL_BREAK_EVENT] = new CtrlBreakHandler; + handler[CTRL_CLOSE_EVENT] = new CtrlCloseHandler; + handler[CTRL_LOGOFF_EVENT] = new CtrlLogoffHandler; + handler[CTRL_SHUTDOWN_EVENT] = new CtrlShutdownHandler; +} + +#endif |