From 9f9798e2c818a133aacc5b0c7b4fc6a6d1d3fddf Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Wed, 20 Jun 2007 01:09:52 +0000 Subject: begin multi threading added signal controller (not working yet) --- Makefile | 7 ++- anytun.cpp | 45 +++++++------ signalController.cpp | 159 ++++++++++++++++++++++++++++++++++++++++++++++ signalController.h | 123 ++++++++++++++++++++++++++++++++++++ threadUtils.hpp | 174 +++++++++++++++++++++++++++++++++++++++++++++++++++ threadutils.hpp | 174 --------------------------------------------------- 6 files changed, 488 insertions(+), 194 deletions(-) create mode 100644 signalController.cpp create mode 100644 signalController.h create mode 100644 threadUtils.hpp delete mode 100644 threadutils.hpp diff --git a/Makefile b/Makefile index 03af359..37f7ba8 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ CFLAGS = -g -Wall C++ = g++ CCFLAGS = -g -Wall LD = g++ -LDFLAGS = -g -O2 -ldl +LDFLAGS = -g -O2 -ldl -lpthread OPENVPNDEPS = openvpn/tun.o \ openvpn/error.o \ @@ -31,7 +31,7 @@ OPENVPNDEPS = openvpn/tun.o \ -OBJS = anytun.o tunDevice.o buffer.o package.o cypher.o authAlgo.o PracticalSocket.o $(OPENVPNDEPS) +OBJS = anytun.o tunDevice.o buffer.o package.o cypher.o authAlgo.o PracticalSocket.o signalController.o $(OPENVPNDEPS) EXECUTABLE = anytun all: $(EXECUTABLE) @@ -54,6 +54,9 @@ cypher.o: cypher.cpp cypher.h buffer.h authAlgo.o: authAlgo.cpp authAlgo.h buffer.h $(C++) $(CCFLAGS) $< -c +signalController.o: signalController.cpp signalController.h + $(C++) $(CCFLAGS) $< -c + PracticalSocket.o: PracticalSocket.cpp PracticalSocket.h $(C++) $(CCFLAGS) $< -c diff --git a/anytun.cpp b/anytun.cpp index ba6c688..3047c46 100644 --- a/anytun.cpp +++ b/anytun.cpp @@ -38,30 +38,39 @@ #include "package.h" #include "cypher.h" #include "authAlgo.h" +#include "signalController.h" -int main(int argc, char* argv[]) +void* receiver(void* d) { - std::cout << "anytun - secure anycast tunneling protocol" << std::endl; + TunDevice* dev = reinterpret_cast(d); - TunDevice* dev; -// dev = new TunDevice("tun", "192.168.200.1", "192.168.201.1"); - dev = new TunDevice("tap", "192.168.202.1", "255.255.255.0"); -// dev = new TunDevice("tun17", "192.168.200.1", "192.168.201.1"); - std::cout << "dev created (opened)" << std::endl; - std::cout << "dev opened - actual name is '" << dev->getActualName() << "'" << std::endl; - std::cout << "dev type is '" << dev->getType() << "'" << std::endl; - - Buffer inBuf(2000); - int len; - do + Buffer buf(1600); + while(1) { - len = dev->read(inBuf); + int len = dev->read(buf); std::cout << "read " << len << " bytes" << std::endl; } - while(len); + pthread_exit(NULL); +} - delete dev; - std::cout << "dev destroyed" << std::endl; +int main(int argc, char* argv[]) +{ + std::cout << "anytun - secure anycast tunneling protocol" << std::endl; + + SignalController sig; + sig.init(); + +// TunDevice dev("tun", "192.168.200.1", "192.168.201.1"); + TunDevice dev("tap", "192.168.202.1", "255.255.255.0"); +// TunDevice dev("tun17", "192.168.200.1", "192.168.201.1"); + + std::cout << "dev created (opened)" << std::endl; + std::cout << "dev opened - actual name is '" << dev.getActualName() << "'" << std::endl; + std::cout << "dev type is '" << dev.getType() << "'" << std::endl; + + pthread_t receiverThread; + pthread_create(&receiverThread, NULL, receiver, &dev); + pthread_detach(receiverThread); - return 0; + return sig.run();; } diff --git a/signalController.cpp b/signalController.cpp new file mode 100644 index 0000000..82dae3e --- /dev/null +++ b/signalController.cpp @@ -0,0 +1,159 @@ +/* + * 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 anytun.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program 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 this program (see the file COPYING included with this + * distribution); if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include + +#include + +#include "threadUtils.hpp" +#include "signalController.h" + + +int SigIntHandler::handle() +{ + std::cout << "SIG-Int caught" << std::endl; + + return 1; +} + +int SigQuitHandler::handle() +{ + std::cout << "SIG-Quit caught" << std::endl; + + return 1; +} + +int SigHupHandler::handle() +{ + std::cout << "SIG-Hup caught" << std::endl; + + return 0; +} + +int SigTermHandler::handle() +{ + std::cout << "SIG-Term caught" << std::endl; + + return 1; +} + +int SigUsr1Handler::handle() +{ + std::cout << "SIG-Usr1 caught" << std::endl; + + return 0; +} + +int SigUsr2Handler::handle() +{ + std::cout << "SIG-Usr2 caught" << std::endl; + + return 0; +} + +SignalController::~SignalController() +{ + for(HandlerMap::iterator it = handler.begin(); it != handler.end(); ++it) + delete it->second; +} + +void* SignalController::handle(void *s) +{ + SignalController* self = reinterpret_cast(s); + sigset_t signal_set; + int sigNum; + + while(1) { + sigfillset(&signal_set); + sigwait(&signal_set, &sigNum); + + { + Lock(self->sigQueueMutex); + self->sigQueue.push(sigNum); + } + self->sigQueueSem.up(); + } + pthread_exit(NULL); +} + +void SignalController::init() +{ + sigset_t signal_set; + + sigfillset(&signal_set); + sigdelset(&signal_set, SIGCHLD); + sigdelset(&signal_set, SIGSEGV); + sigdelset(&signal_set, SIGBUS); + sigdelset(&signal_set, SIGFPE); + pthread_sigmask(SIG_BLOCK, &signal_set, NULL); + + pthread_create(&thread, NULL, handle, NULL); + pthread_detach(thread); + + handler[SIGINT] = new SigIntHandler; + handler[SIGQUIT] = new SigQuitHandler; + handler[SIGHUP] = new SigHupHandler; + handler[SIGTERM] = new SigTermHandler; + handler[SIGUSR1] = new SigUsr1Handler; + handler[SIGUSR2] = new SigUsr2Handler; +} + +bool SignalController::sigQueueEmpty() +{ + Lock lock(sigQueueMutex); + return sigQueue.empty(); +} + +int SignalController::run() +{ + while(1) { + sigQueueSem.down(); + while(!sigQueueEmpty()) + { + int sigNum; + { + Lock lock(sigQueueMutex); + sigNum = sigQueue.front(); + sigQueue.pop(); + } + HandlerMap::iterator it = handler.find(sigNum); + if(it != handler.end()) + { + int ret = it->second->handle(); + if(ret) + return ret; + } + else + std::cout << "SIG " << sigNum << " caught - ignoring" << std::endl; + } + } + return 0; +} diff --git a/signalController.h b/signalController.h new file mode 100644 index 0000000..5f8bc4c --- /dev/null +++ b/signalController.h @@ -0,0 +1,123 @@ +/* + * 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 anytun.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program 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 this program (see the file COPYING included with this + * distribution); if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _SIGNAL_CONTROLLER_H_ +#define _SIGNAL_CONTROLLER_H_ + +#include +#include +#include + +#include "threadUtils.hpp" + +class SignalHandler +{ +public: + virtual ~SignalHandler() {} + + virtual int handle() { return 0; } + +protected: + SignalHandler(int s) : sigNum(s) {} + +private: + int sigNum; + friend class SignalController; +}; + +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(); +}; + +class SignalController +{ +public: + SignalController() {} + ~SignalController(); + static void* handle(void* s); + + void init(); + int run(); + +private: + typedef std::map HandlerMap; + + SignalController(const SignalController &s); + void operator=(const SignalController &s); + + bool sigQueueEmpty(); + + std::queue sigQueue; + Mutex sigQueueMutex; + Semaphore sigQueueSem; + + pthread_t thread; + HandlerMap handler; +}; + +#endif diff --git a/threadUtils.hpp b/threadUtils.hpp new file mode 100644 index 0000000..ad45f68 --- /dev/null +++ b/threadUtils.hpp @@ -0,0 +1,174 @@ +/* + * 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 anytun.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program 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 this program (see the file COPYING included with this + * distribution); if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _THREADUTILS_HPP_ +#define _THREADUTILS_HPP_ + +#include +#include + +class Mutex +{ +public: + Mutex() + { + if(pthread_mutex_init(&mutex,NULL)) + throw std::runtime_error("can't create mutex"); + } + + ~Mutex() + { + pthread_mutex_destroy(&mutex); + } + +private: + Mutex(const Mutex& src); + void operator=(const Mutex& src); + + void lock() + { + if(pthread_mutex_lock(&mutex)) + throw std::runtime_error("can't lock mutex"); + } + + void unlock() + { + if(pthread_mutex_unlock(&mutex)) + throw std::runtime_error("can't unlock mutex"); + } + friend class Lock; + friend class Condition; + pthread_mutex_t mutex; +}; + + +class Lock +{ +public: + Lock(Mutex &m) : mutex(m) + { + mutex.lock(); + } + + ~Lock() + { + mutex.unlock(); + } + +private: + Lock(const Lock& src); + void operator=(const Lock& src); + + Mutex &mutex; +}; + +class Condition +{ +public: + Condition() + { + if(pthread_cond_init(&cond, NULL)) + throw std::runtime_error("can't create condition"); + } + + ~Condition() + { + pthread_cond_destroy(&cond); + } + + void wait() + { + mutex.lock(); + if(pthread_cond_wait(&cond, &mutex.mutex)) + { + mutex.unlock(); + throw std::runtime_error("error on waiting for condition"); + } + mutex.unlock(); + } + + void signal() + { + mutex.lock(); + if(pthread_cond_signal(&cond)) + { + mutex.unlock(); + throw std::runtime_error("can't signal condition"); + } + mutex.unlock(); + } + + void broadcast() + { + mutex.lock(); + if(pthread_cond_broadcast(&cond)) + { + mutex.unlock(); + throw std::runtime_error("can't broadcast condition"); + } + mutex.unlock(); + } + +private: + pthread_cond_t cond; + Mutex mutex; +}; + +class Semaphore +{ +public: + Semaphore(unsigned int initVal=0) + { + if(sem_init(&sem, 0, initVal)) + throw std::runtime_error("can't create semaphore"); + } + + ~Semaphore() + { + sem_destroy(&sem); + } + + void down() + { + if(sem_wait(&sem)) + throw std::runtime_error("error on semaphore down"); + } + + void up() + { + if(sem_post(&sem)) + throw std::runtime_error("error on semaphore up"); + } + +private: + sem_t sem; +}; + +#endif diff --git a/threadutils.hpp b/threadutils.hpp deleted file mode 100644 index ad45f68..0000000 --- a/threadutils.hpp +++ /dev/null @@ -1,174 +0,0 @@ -/* - * 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 anytun.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 - * as published by the Free Software Foundation. - * - * This program 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 this program (see the file COPYING included with this - * distribution); if not, write to the Free Software Foundation, Inc., - * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#ifndef _THREADUTILS_HPP_ -#define _THREADUTILS_HPP_ - -#include -#include - -class Mutex -{ -public: - Mutex() - { - if(pthread_mutex_init(&mutex,NULL)) - throw std::runtime_error("can't create mutex"); - } - - ~Mutex() - { - pthread_mutex_destroy(&mutex); - } - -private: - Mutex(const Mutex& src); - void operator=(const Mutex& src); - - void lock() - { - if(pthread_mutex_lock(&mutex)) - throw std::runtime_error("can't lock mutex"); - } - - void unlock() - { - if(pthread_mutex_unlock(&mutex)) - throw std::runtime_error("can't unlock mutex"); - } - friend class Lock; - friend class Condition; - pthread_mutex_t mutex; -}; - - -class Lock -{ -public: - Lock(Mutex &m) : mutex(m) - { - mutex.lock(); - } - - ~Lock() - { - mutex.unlock(); - } - -private: - Lock(const Lock& src); - void operator=(const Lock& src); - - Mutex &mutex; -}; - -class Condition -{ -public: - Condition() - { - if(pthread_cond_init(&cond, NULL)) - throw std::runtime_error("can't create condition"); - } - - ~Condition() - { - pthread_cond_destroy(&cond); - } - - void wait() - { - mutex.lock(); - if(pthread_cond_wait(&cond, &mutex.mutex)) - { - mutex.unlock(); - throw std::runtime_error("error on waiting for condition"); - } - mutex.unlock(); - } - - void signal() - { - mutex.lock(); - if(pthread_cond_signal(&cond)) - { - mutex.unlock(); - throw std::runtime_error("can't signal condition"); - } - mutex.unlock(); - } - - void broadcast() - { - mutex.lock(); - if(pthread_cond_broadcast(&cond)) - { - mutex.unlock(); - throw std::runtime_error("can't broadcast condition"); - } - mutex.unlock(); - } - -private: - pthread_cond_t cond; - Mutex mutex; -}; - -class Semaphore -{ -public: - Semaphore(unsigned int initVal=0) - { - if(sem_init(&sem, 0, initVal)) - throw std::runtime_error("can't create semaphore"); - } - - ~Semaphore() - { - sem_destroy(&sem); - } - - void down() - { - if(sem_wait(&sem)) - throw std::runtime_error("error on semaphore down"); - } - - void up() - { - if(sem_post(&sem)) - throw std::runtime_error("error on semaphore up"); - } - -private: - sem_t sem; -}; - -#endif -- cgit v1.2.3