summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@anytun.org>2007-06-20 18:56:52 +0000
committerChristian Pointner <equinox@anytun.org>2007-06-20 18:56:52 +0000
commitd5a4ad9dddf7b057f9ccbbb0c349d37ec425fc26 (patch)
tree960a83fee909f14ff235b515f75d5cc20411d85f
parentbegin multi threading (diff)
added syslog class
added threads for receiving und sending
-rw-r--r--Makefile5
-rw-r--r--anytun.cpp39
-rw-r--r--log.cpp98
-rw-r--r--log.h126
-rw-r--r--signalController.cpp58
-rw-r--r--signalController.h2
-rw-r--r--tunDevice.cpp4
-rw-r--r--tunDevice.h2
8 files changed, 290 insertions, 44 deletions
diff --git a/Makefile b/Makefile
index 37f7ba8..6b6a876 100644
--- a/Makefile
+++ b/Makefile
@@ -31,7 +31,7 @@ OPENVPNDEPS = openvpn/tun.o \
-OBJS = anytun.o tunDevice.o buffer.o package.o cypher.o authAlgo.o PracticalSocket.o signalController.o $(OPENVPNDEPS)
+OBJS = anytun.o tunDevice.o buffer.o package.o cypher.o authAlgo.o PracticalSocket.o signalController.o log.o $(OPENVPNDEPS)
EXECUTABLE = anytun
all: $(EXECUTABLE)
@@ -60,6 +60,9 @@ signalController.o: signalController.cpp signalController.h
PracticalSocket.o: PracticalSocket.cpp PracticalSocket.h
$(C++) $(CCFLAGS) $< -c
+log.o: log.cpp log.h
+ $(C++) $(CCFLAGS) $< -c
+
anytun.o: anytun.cpp
$(C++) $(CCFLAGS) $< -c
diff --git a/anytun.cpp b/anytun.cpp
index 3047c46..b5e9646 100644
--- a/anytun.cpp
+++ b/anytun.cpp
@@ -33,6 +33,7 @@
#include "datatypes.h"
+#include "log.h"
#include "tunDevice.h"
#include "buffer.h"
#include "package.h"
@@ -40,10 +41,10 @@
#include "authAlgo.h"
#include "signalController.h"
-void* receiver(void* d)
+void* sender(void* d)
{
TunDevice* dev = reinterpret_cast<TunDevice*>(d);
-
+
Buffer buf(1600);
while(1)
{
@@ -53,24 +54,46 @@ void* receiver(void* d)
pthread_exit(NULL);
}
+void* receiver(void* d)
+{
+ TunDevice* dev = reinterpret_cast<TunDevice*>(d);
+
+ Buffer buf(1234);
+ while(1)
+ {
+ sleep(1);
+ dev->write(buf);
+ }
+ pthread_exit(NULL);
+}
+
int main(int argc, char* argv[])
{
std::cout << "anytun - secure anycast tunneling protocol" << std::endl;
+ cLog.msg(Log::PRIO_NOTICE) << "anytun started...";
+
+ SignalController sig;
+ sig.init();
- 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 senderThread;
+ pthread_create(&senderThread, NULL, sender, &dev);
pthread_t receiverThread;
pthread_create(&receiverThread, NULL, receiver, &dev);
- pthread_detach(receiverThread);
- return sig.run();;
+ int ret = sig.run();
+
+ pthread_cancel(senderThread);
+ pthread_cancel(receiverThread);
+ pthread_join(senderThread, NULL);
+ pthread_join(receiverThread, NULL);
+
+ return ret;
}
diff --git a/log.cpp b/log.cpp
new file mode 100644
index 0000000..8058183
--- /dev/null
+++ b/log.cpp
@@ -0,0 +1,98 @@
+/*
+ * 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 <iostream>
+#include <string>
+#include <syslog.h>
+
+#include "log.h"
+
+#include "threadUtils.hpp"
+
+Log* Log::inst = NULL;
+Mutex Log::instMutex;
+Log& cLog = Log::instance();
+
+LogStringBuilder::LogStringBuilder(LogStringBuilder const& src) : log(src.log), prio(src.prio)
+{
+ stream << src.stream.str();
+}
+
+LogStringBuilder::LogStringBuilder(Log& l, int p) : log(l), prio(p)
+{
+ // do something on the start of the line.
+}
+
+LogStringBuilder::~LogStringBuilder()
+{
+ Lock lock(log.mutex);
+ syslog(prio | log.getFacility(), stream.str().c_str());
+}
+
+Log& Log::instance()
+{
+ Lock lock(instMutex);
+ static instanceCleaner c;
+ if(!inst)
+ inst = new Log();
+
+ return *inst;
+}
+
+Log::Log()
+{
+ facility = LOG_DAEMON;
+ logName = "anytun";
+ open();
+}
+
+Log::~Log()
+{
+ closelog();
+}
+
+void Log::open()
+{
+ openlog(logName.c_str(), LOG_PID, facility);
+}
+
+Log& Log::setLogName(std::string newLogName)
+{
+ logName = newLogName;
+ open();
+ return *this;
+}
+
+Log& Log::setFacility(int newFacility)
+{
+ facility = newFacility;
+ open();
+ return *this;
+}
diff --git a/log.h b/log.h
new file mode 100644
index 0000000..12d0e51
--- /dev/null
+++ b/log.h
@@ -0,0 +1,126 @@
+/*
+ * 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 _LOG_H_
+#define _LOG_H_
+
+#include <string>
+#include <sstream>
+#include <syslog.h>
+
+#include "threadUtils.hpp"
+
+class Log;
+
+class LogStringBuilder
+{
+public:
+ LogStringBuilder(LogStringBuilder const& src);
+ LogStringBuilder(Log& l, int p);
+ ~LogStringBuilder();
+
+ template<class T>
+ std::ostream& operator<<(T const& value) { return stream << value; }
+
+private:
+ Log& log;
+ int prio;
+ std::stringstream stream;
+};
+
+class Log : public std::ostringstream
+{
+public:
+ static const int FAC_USER = LOG_USER;
+ static const int FAC_MAIL = LOG_MAIL;
+ static const int FAC_DAEMON = LOG_DAEMON;
+ static const int FAC_AUTH = LOG_AUTH;
+ static const int FAC_SYSLOG = LOG_SYSLOG;
+ static const int FAC_LPR = LOG_LPR;
+ static const int FAC_NEWS = LOG_NEWS;
+ static const int FAC_UUCP = LOG_UUCP;
+ static const int FAC_CRON = LOG_CRON;
+ static const int FAC_AUTHPRIV = LOG_AUTHPRIV;
+ static const int FAC_FTP = LOG_FTP;
+ static const int FAC_LOCAL0 = LOG_LOCAL0;
+ static const int FAC_LOCAL1 = LOG_LOCAL1;
+ static const int FAC_LOCAL2 = LOG_LOCAL2;
+ static const int FAC_LOCAL3 = LOG_LOCAL3;
+ static const int FAC_LOCAL4 = LOG_LOCAL4;
+ static const int FAC_LOCAL5 = LOG_LOCAL5;
+ static const int FAC_LOCAL6 = LOG_LOCAL6;
+ static const int FAC_LOCAL7 = LOG_LOCAL7;
+
+ static const int PRIO_EMERG = LOG_EMERG;
+ static const int PRIO_ALERT = LOG_ALERT;
+ static const int PRIO_CRIT = LOG_CRIT;
+ static const int PRIO_ERR = LOG_ERR;
+ static const int PRIO_WARNING = LOG_WARNING;
+ static const int PRIO_NOTICE = LOG_NOTICE;
+ static const int PRIO_INFO = LOG_INFO;
+ static const int PRIO_DEBUG = LOG_DEBUG;
+
+ static Log& instance();
+
+ Log& setLogName(std::string newLogName);
+ std::string getLogName() const { return logName; }
+ Log& setFacility(int newFacility);
+ int getFacility() const { return facility; }
+
+ LogStringBuilder msg(int prio=PRIO_INFO) { return LogStringBuilder(*this, prio); }
+
+private:
+ Log();
+ ~Log();
+ Log(const Log &l);
+ void operator=(const Log &l);
+
+ static Log* inst;
+ static Mutex instMutex;
+ class instanceCleaner {
+ public: ~instanceCleaner() {
+ if(Log::inst != 0)
+ delete Log::inst;
+ }
+ };
+ friend class instanceCleaner;
+
+ void open();
+
+ Mutex mutex;
+ friend class LogStringBuilder;
+
+ std::string logName;
+ int facility;
+};
+
+extern Log& cLog;
+
+#endif
diff --git a/signalController.cpp b/signalController.cpp
index 82dae3e..be51d59 100644
--- a/signalController.cpp
+++ b/signalController.cpp
@@ -35,46 +35,47 @@
#include "threadUtils.hpp"
#include "signalController.h"
+#include "log.h"
int SigIntHandler::handle()
{
- std::cout << "SIG-Int caught" << std::endl;
+ cLog.msg(Log::PRIO_NOTICE) << "SIG-Int caught";
return 1;
}
int SigQuitHandler::handle()
{
- std::cout << "SIG-Quit caught" << std::endl;
+ cLog.msg(Log::PRIO_NOTICE) << "SIG-Quit caught";
return 1;
}
int SigHupHandler::handle()
{
- std::cout << "SIG-Hup caught" << std::endl;
+ cLog.msg(Log::PRIO_NOTICE) << "SIG-Hup caught";
return 0;
}
int SigTermHandler::handle()
{
- std::cout << "SIG-Term caught" << std::endl;
+ cLog.msg(Log::PRIO_NOTICE) << "SIG-Term caught";
return 1;
}
int SigUsr1Handler::handle()
{
- std::cout << "SIG-Usr1 caught" << std::endl;
+ cLog.msg(Log::PRIO_NOTICE) << "SIG-Usr1 caught";
return 0;
}
int SigUsr2Handler::handle()
{
- std::cout << "SIG-Usr2 caught" << std::endl;
+ cLog.msg(Log::PRIO_NOTICE) << "SIG-Usr2 caught";
return 0;
}
@@ -91,10 +92,10 @@ void* SignalController::handle(void *s)
sigset_t signal_set;
int sigNum;
- while(1) {
+ while(1)
+ {
sigfillset(&signal_set);
sigwait(&signal_set, &sigNum);
-
{
Lock(self->sigQueueMutex);
self->sigQueue.push(sigNum);
@@ -115,7 +116,7 @@ void SignalController::init()
sigdelset(&signal_set, SIGFPE);
pthread_sigmask(SIG_BLOCK, &signal_set, NULL);
- pthread_create(&thread, NULL, handle, NULL);
+ pthread_create(&thread, NULL, handle, this);
pthread_detach(thread);
handler[SIGINT] = new SigIntHandler;
@@ -126,34 +127,27 @@ void SignalController::init()
handler[SIGUSR2] = new SigUsr2Handler;
}
-bool SignalController::sigQueueEmpty()
-{
- Lock lock(sigQueueMutex);
- return sigQueue.empty();
-}
-
int SignalController::run()
{
- while(1) {
+ 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 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;
+ int ret = it->second->handle();
+ if(ret)
+ return ret;
}
+ else
+ cLog.msg(Log::PRIO_NOTICE) << "SIG " << sigNum << " caught - ignoring";
}
return 0;
}
diff --git a/signalController.h b/signalController.h
index 5f8bc4c..4f912ef 100644
--- a/signalController.h
+++ b/signalController.h
@@ -110,8 +110,6 @@ private:
SignalController(const SignalController &s);
void operator=(const SignalController &s);
- bool sigQueueEmpty();
-
std::queue<int> sigQueue;
Mutex sigQueueMutex;
Semaphore sigQueueSem;
diff --git a/tunDevice.cpp b/tunDevice.cpp
index 2dc7971..e4cc5af 100644
--- a/tunDevice.cpp
+++ b/tunDevice.cpp
@@ -38,6 +38,7 @@ extern "C" {
}
#include "tunDevice.h"
+#include "threadUtils.hpp"
TunDevice::TunDevice(const char* dev_name, const char* ifcfg_lp, const char* ifcfg_rnmp)
@@ -119,6 +120,7 @@ short TunDevice::read(Buffer& buf)
pfd[0].events = POLLIN | POLLPRI;
pfd[0].revents = 0;
poll(pfd, 1, -1);
+ Lock lock(io_mutex_);
return read_tun(dev_, buf, buf.getLength());
}
@@ -126,7 +128,7 @@ int TunDevice::write(Buffer& buf)
{
if(!dev_)
return -1;
-
+ Lock lock(io_mutex_);
return write_tun(dev_, buf, buf.getLength());
}
diff --git a/tunDevice.h b/tunDevice.h
index 6fdcc2d..ee8b45d 100644
--- a/tunDevice.h
+++ b/tunDevice.h
@@ -32,6 +32,7 @@
#define _TUNDEVICE_H_
#include "buffer.h"
+#include "threadUtils.hpp"
class TunDevice
{
@@ -53,6 +54,7 @@ private:
void operator=(const TunDevice &src);
TunDevice(const TunDevice &src);
+ Mutex io_mutex_;
struct tuntap *dev_;
};