summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile7
-rw-r--r--anytun.cpp45
-rw-r--r--signalController.cpp159
-rw-r--r--signalController.h123
-rw-r--r--threadUtils.hpp (renamed from threadutils.hpp)0
5 files changed, 314 insertions, 20 deletions
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<TunDevice*>(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 <satp@wirdorange.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 <csignal>
+#include <map>
+
+#include <iostream>
+
+#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<SignalController*>(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 <satp@wirdorange.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 <csignal>
+#include <map>
+#include <queue>
+
+#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<int, SignalHandler*> HandlerMap;
+
+ SignalController(const SignalController &s);
+ void operator=(const SignalController &s);
+
+ bool sigQueueEmpty();
+
+ std::queue<int> sigQueue;
+ Mutex sigQueueMutex;
+ Semaphore sigQueueSem;
+
+ pthread_t thread;
+ HandlerMap handler;
+};
+
+#endif
diff --git a/threadutils.hpp b/threadUtils.hpp
index ad45f68..ad45f68 100644
--- a/threadutils.hpp
+++ b/threadUtils.hpp