diff options
-rw-r--r-- | anytun.cpp | 51 | ||||
-rw-r--r-- | connectionList.cpp | 3 | ||||
-rw-r--r-- | connectionParam.h | 1 | ||||
-rw-r--r-- | options.cpp | 66 | ||||
-rw-r--r-- | options.h | 20 | ||||
-rw-r--r-- | threadParam.h | 6 |
6 files changed, 91 insertions, 56 deletions
@@ -88,7 +88,7 @@ void createConnection(const std::string & remote_host, u_int16_t remote_port, Co SeqWindow * seq= new SeqWindow(seqSize); seq_nr_t seq_nr_=0; - KeyDerivation * kd = KeyDerivationFactory::create("aes-ctr"); // TODO: get value from options + KeyDerivation * kd = KeyDerivationFactory::create(gOpt.getKdPrf()); kd->init(Buffer(key, sizeof(key)), Buffer(salt, sizeof(salt))); cLog.msg(Log::PRIO_NOTICE) << "added connection remote host " << remote_host << ":" << remote_port; ConnectionParam connparam ( (*kd), (*seq), seq_nr_, remote_host, remote_port); @@ -129,8 +129,8 @@ void* sender(void* p) { ThreadParam* param = reinterpret_cast<ThreadParam*>(p); - std::auto_ptr<Cipher> c(CipherFactory::create(param->opt.getCipher())); -// std::auto_ptr<AuthAlgo> a(AuthAlgoFactory::create(param->opt.getAuthAlgo()) ); + std::auto_ptr<Cipher> c(CipherFactory::create(gOpt.getCipher())); +// std::auto_ptr<AuthAlgo> a(AuthAlgoFactory::create(gOpt.getAuthAlgo()) ); PlainPacket plain_packet(MAX_PACKET_LENGTH); EncryptedPacket encrypted_packet(MAX_PACKET_LENGTH); @@ -181,9 +181,9 @@ void* sender(void* p) c->setSalt(session_salt); // encrypt packet - c->encrypt(plain_packet, encrypted_packet, conn.seq_nr_, param->opt.getSenderId()); + c->encrypt(plain_packet, encrypted_packet, conn.seq_nr_, gOpt.getSenderId()); - encrypted_packet.setHeader(conn.seq_nr_, param->opt.getSenderId(), mux); + encrypted_packet.setHeader(conn.seq_nr_, gOpt.getSenderId(), mux); conn.seq_nr_++; // TODO: activate authentication @@ -221,7 +221,7 @@ void* syncListener(void* p ) SyncSocketHandler h(param->queue); SyncListenSocket<SyncSocket,ConnectionList> l(h,param->cl); - if (l.Bind(param->opt.getLocalSyncPort())) + if (l.Bind(gOpt.getLocalSyncPort())) pthread_exit(NULL); Utility::ResolveLocal(); // resolve local hostname @@ -236,8 +236,8 @@ void* receiver(void* p) { ThreadParam* param = reinterpret_cast<ThreadParam*>(p); - std::auto_ptr<Cipher> c( CipherFactory::create(param->opt.getCipher()) ); -// std::auto_ptr<AuthAlgo> a( AuthAlgoFactory::create(param->opt.getAuthAlgo()) ); + std::auto_ptr<Cipher> c( CipherFactory::create(gOpt.getCipher()) ); +// std::auto_ptr<AuthAlgo> a( AuthAlgoFactory::create(gOpt.getAuthAlgo()) ); EncryptedPacket encrypted_packet(MAX_PACKET_LENGTH); PlainPacket plain_packet(MAX_PACKET_LENGTH); @@ -266,10 +266,10 @@ void* receiver(void* p) // autodetect peer - if(param->opt.getRemoteAddr() == "" && param->cl.empty()) + if(gOpt.getRemoteAddr() == "" && param->cl.empty()) { cLog.msg(Log::PRIO_NOTICE) << "autodetected remote host " << remote_host << ":" << remote_port; - createConnection(remote_host, remote_port, param->cl,param->opt.getSeqWindowSize(),param->queue); + createConnection(remote_host, remote_port, param->cl, gOpt.getSeqWindowSize(),param->queue); } // TODO: Add multi connection support here @@ -343,33 +343,32 @@ bool initLibGCrypt() int main(int argc, char* argv[]) { std::cout << "anytun - secure anycast tunneling protocol" << std::endl; - Options opt; - if(!opt.parse(argc, argv)) + if(!gOpt.parse(argc, argv)) { - opt.printUsage(); + gOpt.printUsage(); exit(-1); } cLog.msg(Log::PRIO_NOTICE) << "anytun started..."; SignalController sig; sig.init(); - std::string dev_type(opt.getDevType()); - TunDevice dev(opt.getDevName().c_str(), dev_type=="" ? NULL : dev_type.c_str(), opt.getIfconfigParamLocal().c_str(), opt.getIfconfigParamRemoteNetmask().c_str()); + std::string dev_type(gOpt.getDevType()); + TunDevice dev(gOpt.getDevName().c_str(), dev_type=="" ? NULL : dev_type.c_str(), gOpt.getIfconfigParamLocal().c_str(), gOpt.getIfconfigParamRemoteNetmask().c_str()); PacketSource* src; - if(opt.getLocalAddr() == "") - src = new UDPPacketSource(opt.getLocalPort()); + if(gOpt.getLocalAddr() == "") + src = new UDPPacketSource(gOpt.getLocalPort()); else - src = new UDPPacketSource(opt.getLocalAddr(), opt.getLocalPort()); + src = new UDPPacketSource(gOpt.getLocalAddr(), gOpt.getLocalPort()); ConnectionList cl; - ConnectToList connect_to = opt.getConnectTo(); + ConnectToList connect_to = gOpt.getConnectTo(); SyncQueue queue; - if(opt.getRemoteAddr() != "") - createConnection(opt.getRemoteAddr(),opt.getRemotePort(),cl,opt.getSeqWindowSize(), queue); + if(gOpt.getRemoteAddr() != "") + createConnection(gOpt.getRemoteAddr(),gOpt.getRemotePort(),cl,gOpt.getSeqWindowSize(), queue); - ThreadParam p(opt, dev, *src, cl, queue,*(new OptionConnectTo())); + ThreadParam p(dev, *src, cl, queue,*(new OptionConnectTo())); cLog.msg(Log::PRIO_NOTICE) << "dev created (opened)"; cLog.msg(Log::PRIO_NOTICE) << "dev opened - actual name is '" << p.dev.getActualName() << "'"; @@ -385,14 +384,14 @@ int main(int argc, char* argv[]) pthread_create(&receiverThread, NULL, receiver, &p); pthread_t syncListenerThread; - if ( opt.getLocalSyncPort()) + if ( gOpt.getLocalSyncPort()) pthread_create(&syncListenerThread, NULL, syncListener, &p); std::list<pthread_t> connectThreads; for(ConnectToList::iterator it = connect_to.begin() ;it != connect_to.end(); ++it) { connectThreads.push_back(pthread_t()); - ThreadParam * point = new ThreadParam(opt, dev, *src, cl, queue,*it); + ThreadParam * point = new ThreadParam(dev, *src, cl, queue,*it); pthread_create(& connectThreads.back(), NULL, syncConnector, point); } @@ -400,14 +399,14 @@ int main(int argc, char* argv[]) pthread_cancel(senderThread); pthread_cancel(receiverThread); - if ( opt.getLocalSyncPort()) + if ( gOpt.getLocalSyncPort()) pthread_cancel(syncListenerThread); for( std::list<pthread_t>::iterator it = connectThreads.begin() ;it != connectThreads.end(); ++it) pthread_cancel(*it); pthread_join(senderThread, NULL); pthread_join(receiverThread, NULL); - if ( opt.getLocalSyncPort()) + if ( gOpt.getLocalSyncPort()) pthread_join(syncListenerThread, NULL); for( std::list<pthread_t>::iterator it = connectThreads.begin() ;it != connectThreads.end(); ++it) diff --git a/connectionList.cpp b/connectionList.cpp index 713009b..f3bb129 100644 --- a/connectionList.cpp +++ b/connectionList.cpp @@ -31,6 +31,7 @@ #include "threadUtils.hpp" #include "datatypes.h" #include "keyDerivationFactory.h" +#include "options.h" #include "connectionList.h" @@ -92,7 +93,7 @@ ConnectionParam & ConnectionList::getOrNewConnectionUnlocked(u_int16_t mux) SeqWindow * seq= new SeqWindow(0); seq_nr_t seq_nr_=0; - KeyDerivation * kd = KeyDerivationFactory::create("aes-ctr"); // TODO: get value from options + KeyDerivation * kd = KeyDerivationFactory::create(gOpt.getKdPrf()); kd->init(Buffer(key, sizeof(key)), Buffer(salt, sizeof(salt))); ConnectionParam conn ( (*kd), (*seq), seq_nr_, "", 0); connections_.insert(ConnectionMap::value_type(mux, conn)); diff --git a/connectionParam.h b/connectionParam.h index af89935..1002b23 100644 --- a/connectionParam.h +++ b/connectionParam.h @@ -31,7 +31,6 @@ #ifndef _CONNECTIONPARAM_H_ #define _CONNECTIONPARAM_H_ -#include "options.h" #include "keyDerivation.h" #include "cipher.h" #include "authAlgo.h" diff --git a/options.cpp b/options.cpp index 8bc4e77..ba010fd 100644 --- a/options.cpp +++ b/options.cpp @@ -36,6 +36,44 @@ #include "datatypes.h" #include "options.h" +Options* Options::inst = NULL; +Mutex Options::instMutex; +Options& gOpt = Options::instance(); + +Options& Options::instance() +{ + Lock lock(instMutex); + static instanceCleaner c; + if(!inst) + inst = new Options(); + + return *inst; +} + +Options::Options() +{ + progname_ = "anytun"; + sender_id_ = 0; + local_addr_ = ""; + local_port_ = 4444; + local_sync_port_ = 0; + remote_sync_port_ = 0; + remote_sync_addr_ = ""; + remote_addr_ = ""; + remote_port_ = 4444; + dev_name_ = "tap"; + dev_type_ = ""; + ifconfig_param_local_ = "192.168.200.1"; + ifconfig_param_remote_netmask_ = "255.255.255.0"; + seq_window_size_ = 100; + cipher_ = "aes-ctr"; + kd_prf_ = "aes-ctr"; + auth_algo_ = "sha1"; +} + +Options::~Options() +{ +} #define PARSE_BOOL_PARAM(SHORT, LONG, VALUE) \ else if(str == SHORT || str == LONG) \ @@ -71,7 +109,7 @@ i+=2; \ } -#define PARSE_CSLIST_PARAM(SHORT, LONG, LIST) \ +#define PARSE_CSLIST_PARAM(SHORT, LONG, LIST) \ else if(str == SHORT || str == LONG) \ { \ if(argc < 1 || argv[i+1][0] == '-') \ @@ -87,27 +125,6 @@ i++; \ } -Options::Options() -{ - progname_ = "anytun"; - sender_id_ = 0; - local_addr_ = ""; - local_port_ = 4444; - local_sync_port_ = 0; - remote_sync_port_ = 0; - remote_sync_addr_ = ""; - remote_addr_ = ""; - remote_port_ = 4444; - dev_name_ = "tap"; - dev_type_ = ""; - ifconfig_param_local_ = "192.168.200.1"; - ifconfig_param_remote_netmask_ = "255.255.255.0"; - seq_window_size_ = 100; - cipher_ = "aes-ctr"; - kd_prf_ = "aes-ctr"; - auth_algo_ = "sha1"; -} - bool Options::parse(int argc, char* argv[]) { Lock lock(mutex); @@ -137,10 +154,13 @@ bool Options::parse(int argc, char* argv[]) PARSE_SCALAR_PARAM("-c","--cipher", cipher_) PARSE_SCALAR_PARAM("-k","--kd-prf", kd_prf_) PARSE_SCALAR_PARAM("-a","--auth-algo", auth_algo_) - PARSE_SCALAR_CSLIST("-M","--sync-hosts", host_port_queue) + PARSE_CSLIST_PARAM("-M","--sync-hosts", host_port_queue) else return false; } + + if(cipher_ == "null") + kd_prf_ = "null"; while(!host_port_queue.empty()) { std::stringstream tmp_stream(host_port_queue.front()); @@ -46,7 +46,8 @@ typedef std::list<OptionConnectTo> ConnectToList; class Options { public: - Options(); + static Options& instance(); + bool parse(int argc, char* argv[]); void printUsage(); void printOptions(); @@ -91,6 +92,21 @@ public: ConnectToList getConnectTo(); private: + Options(); + ~Options(); + Options(const Options &l); + void operator=(const Options &l); + + static Options* inst; + static Mutex instMutex; + class instanceCleaner { + public: ~instanceCleaner() { + if(Options::inst != 0) + delete Options::inst; + } + }; + friend class instanceCleaner; + Mutex mutex; ConnectToList connect_to_; @@ -114,4 +130,6 @@ private: std::string auth_algo_; }; +extern Options& gOpt; + #endif diff --git a/threadParam.h b/threadParam.h index 11b7c72..584396e 100644 --- a/threadParam.h +++ b/threadParam.h @@ -33,7 +33,6 @@ #include "threadUtils.hpp" #include "datatypes.h" -#include "options.h" #include "tunDevice.h" #include "connectionList.h" #include "syncQueue.h" @@ -41,10 +40,9 @@ class ThreadParam { public: - ThreadParam(Options& opt_,TunDevice& dev_,PacketSource& src_,ConnectionList& cl_,SyncQueue & queue_,OptionConnectTo & connto_) - : opt(opt_),dev(dev_),src(src_),cl(cl_),queue(queue_),connto(connto_) + ThreadParam(TunDevice& dev_,PacketSource& src_,ConnectionList& cl_,SyncQueue & queue_,OptionConnectTo & connto_) + : dev(dev_),src(src_),cl(cl_),queue(queue_),connto(connto_) {}; - Options& opt; TunDevice& dev; PacketSource& src; ConnectionList& cl; |