summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--anytun.cpp51
-rw-r--r--options.cpp37
-rw-r--r--options.h11
-rw-r--r--threadParam.h55
4 files changed, 114 insertions, 40 deletions
diff --git a/anytun.cpp b/anytun.cpp
index 97e70c2..2a01045 100644
--- a/anytun.cpp
+++ b/anytun.cpp
@@ -57,6 +57,8 @@
#include "syncClientSocket.h"
#include "syncCommand.h"
+#include "threadParam.h"
+
#define PAYLOAD_TYPE_TAP 0x6558
#define PAYLOAD_TYPE_TUN 0x0800
@@ -64,15 +66,6 @@
#define SESSION_KEYLEN_ENCR 16
#define SESSION_KEYLEN_SALT 14
-struct Param
-{
- Options& opt;
- TunDevice& dev;
- PacketSource& src;
- ConnectionList& cl;
- SyncQueue & queue;
-};
-
uint8_t key[] = {
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
@@ -100,7 +93,7 @@ void createConnection(const std::string & remote_host , u_int16_t remote_port, C
void encryptPacket(Packet & pack, Cypher & c, ConnectionParam & conn, void* p)
{
- Param* param = reinterpret_cast<Param*>(p);
+ ThreadParam* param = reinterpret_cast<ThreadParam*>(p);
// cypher the packet
Buffer session_key(SESSION_KEYLEN_ENCR), session_salt(SESSION_KEYLEN_SALT);
conn.kd_.generate(LABEL_SATP_ENCRYPTION, conn.seq_nr_, session_key, session_key.getLength());
@@ -178,7 +171,7 @@ bool checkPacketSeqNr(Packet & pack,ConnectionParam & conn)
void* sender(void* p)
{
- Param* param = reinterpret_cast<Param*>(p);
+ ThreadParam* param = reinterpret_cast<ThreadParam*>(p);
//TODO make Cypher selectable with command line option
// NullCypher c;
AesIcmCypher c;
@@ -222,12 +215,12 @@ void* sender(void* p)
void* syncConnector(void* p )
{
- Param* param = reinterpret_cast<Param*>(p);
+ ThreadParam* param = reinterpret_cast<ThreadParam*>(p);
SocketHandler h;
SyncClientSocket sock(h,param->cl);
// sock.EnableSSL();
- sock.Open( param->opt.getRemoteSyncAddr(), param->opt.getRemoteSyncPort());
+ sock.Open( param->connto.host, param->connto.port);
h.Add(&sock);
while (h.GetCount())
{
@@ -238,7 +231,7 @@ void* syncConnector(void* p )
void* syncListener(void* p )
{
- Param* param = reinterpret_cast<Param*>(p);
+ ThreadParam* param = reinterpret_cast<ThreadParam*>(p);
SyncSocketHandler h(param->queue);
SyncListenSocket<SyncSocket,ConnectionList> l(h,param->cl);
@@ -256,7 +249,7 @@ void* syncListener(void* p )
void* receiver(void* p)
{
- Param* param = reinterpret_cast<Param*>(p);
+ ThreadParam* param = reinterpret_cast<ThreadParam*>(p);
// NullCypher c;
AesIcmCypher c;
// NullAuthAlgo a;
@@ -344,14 +337,14 @@ int main(int argc, char* argv[])
src = new UDPPacketSource(opt.getLocalAddr(), opt.getLocalPort());
ConnectionList cl;
-
+ ConnectToList connect_to = opt.getConnectTo();
SyncQueue queue;
if(opt.getRemoteAddr() != "")
createConnection(opt.getRemoteAddr(),opt.getRemotePort(),cl,opt.getSeqWindowSize(), queue);
- struct Param p = {opt, dev, *src, cl, queue};
+ ThreadParam p(opt, 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() << "'";
@@ -364,26 +357,34 @@ int main(int argc, char* argv[])
pthread_t receiverThread;
pthread_create(&receiverThread, NULL, receiver, &p);
pthread_t syncListenerThread;
- pthread_t syncConnectorThread;
+
if ( opt.getLocalSyncPort())
pthread_create(&syncListenerThread, NULL, syncListener, &p);
- if ( opt.getRemoteSyncPort() && opt.getRemoteSyncAddr() != "")
- pthread_create(&syncConnectorThread, NULL, syncConnector, &p);
- int ret = sig.run();
+ 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);
+ pthread_create(& connectThreads.back(), NULL, syncConnector, point);
+ }
+
+ int ret = sig.run();
pthread_cancel(senderThread);
pthread_cancel(receiverThread);
if ( opt.getLocalSyncPort())
pthread_cancel(syncListenerThread);
- if ( opt.getRemoteSyncPort() && opt.getRemoteSyncAddr() != "")
- pthread_cancel(syncConnectorThread);
+ 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())
pthread_join(syncListenerThread, NULL);
- if ( opt.getRemoteSyncPort() && opt.getRemoteSyncAddr() != "")
- pthread_join(syncConnectorThread, NULL);
+
+ for( std::list<pthread_t>::iterator it = connectThreads.begin() ;it != connectThreads.end(); ++it)
+ pthread_join(*it, NULL);
delete src;
diff --git a/options.cpp b/options.cpp
index bc72f91..0bf39db 100644
--- a/options.cpp
+++ b/options.cpp
@@ -76,11 +76,13 @@
{ \
if(argc < 1 || argv[i+1][0] == '-') \
return false; \
- std::stringstream tmp; \
- tmp << argv[i+1]; \
- std::string tmp_line; \
- getline(tmp,tmp_line,','); \
- LIST.push(tmp_line); \
+ std::stringstream tmp(argv[i+1]); \
+ while (tmp.good()) \
+ { \
+ std::string tmp_line; \
+ getline(tmp,tmp_line,','); \
+ LIST.push(tmp_line); \
+ } \
argc--; \
i++; \
}
@@ -140,13 +142,13 @@ bool Options::parse(int argc, char* argv[])
while(!host_port_queue.empty())
{
std::stringstream tmp_stream(host_port_queue.front());
- std::string host;
- u_int16_t port;
- getline(tmp_stream,host,':');
+ OptionConnectTo oct;
+ getline(tmp_stream,oct.host,':');
if(!tmp_stream.good())
return false;
- tmp_stream >> port;
+ tmp_stream >> oct.port;
host_port_queue.pop();
+ connect_to_.push_back(oct);
}
return true;
}
@@ -158,10 +160,10 @@ void Options::printUsage()
// std::cout << " [-f|--config] <file> the config file" << std::endl;
std::cout << " [-s|--sender-id ] <sender id> the sender id to use" << std::endl;
std::cout << " [-i|--interface] <interface> local interface to bind to" << std::endl;
- std::cout << " [-p|--port] <port> local anycast port to bind to" << std::endl;
- std::cout << " [-S|--sync-port] <port> local unicast/sync port to bind to" << std::endl;
- std::cout << " [-R|--remote-sync-host] <hostname|ip> remote unicast/sync host" << std::endl;
- std::cout << " [-O|--remote-sync-port] <port> remote unicast/sync port to bind to" << std::endl;
+ std::cout << " [-p|--port] <port> local anycast(data) port to bind to" << std::endl;
+ std::cout << " [-S|--sync-port] <port> local unicast(sync) port to bind to" << std::endl;
+ std::cout << " [-M|--sync-hosts] <hostname|ip>:<port>[,<hostname|ip>:<port>[...]]"<< std::endl;
+ std::cout << " remote hosts to sync with" << std::endl;
std::cout << " [-r|--remote-host] <hostname|ip> remote host" << std::endl;
std::cout << " [-o|--remote-port] <port> remote port" << std::endl;
std::cout << " [-d|--dev] <name> device name" << std::endl;
@@ -181,8 +183,6 @@ void Options::printOptions()
std::cout << "local_addr='" << local_addr_ << "'" << std::endl;
std::cout << "local_port='" << local_port_ << "'" << std::endl;
std::cout << "local_sync_port='" << local_sync_port_ << "'" << std::endl;
- std::cout << "remote_sync_port='" << remote_sync_port_ << "'" << std::endl;
- std::cout << "remote_sync_addr='" << remote_sync_addr_ << "'" << std::endl;
std::cout << "remote_addr='" << remote_addr_ << "'" << std::endl;
std::cout << "remote_port='" << remote_port_ << "'" << std::endl;
std::cout << "dev_name='" << dev_name_ << "'" << std::endl;
@@ -200,6 +200,7 @@ std::string Options::getProgname()
return progname_;
}
+
Options& Options::setProgname(std::string p)
{
Lock lock(mutex);
@@ -207,6 +208,12 @@ Options& Options::setProgname(std::string p)
return *this;
}
+ConnectToList Options::getConnectTo()
+{
+ Lock lock(mutex);
+ return connect_to_;
+}
+
sender_id_t Options::getSenderId()
{
return sender_id_;
diff --git a/options.h b/options.h
index 191b303..9a4a4ed 100644
--- a/options.h
+++ b/options.h
@@ -33,6 +33,15 @@
#include "datatypes.h"
#include "threadUtils.hpp"
+#include <list>
+
+typedef struct OptionConnectTo
+{
+ std::string host;
+ uint16_t port;
+};
+
+typedef std::list<OptionConnectTo> ConnectToList;
class Options
{
@@ -77,10 +86,12 @@ public:
Options& setCypher(std::string c);
std::string getAuthAlgo();
Options& setAuthAlgo(std::string a);
+ ConnectToList getConnectTo();
private:
Mutex mutex;
+ ConnectToList connect_to_;
std::string progname_;
sender_id_t sender_id_;
std::string local_addr_;
diff --git a/threadParam.h b/threadParam.h
new file mode 100644
index 0000000..01fa2aa
--- /dev/null
+++ b/threadParam.h
@@ -0,0 +1,55 @@
+/*
+ * 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 _THREAD_PARAM_H
+#define _THREAD_PARAM__H
+
+#include "threadUtils.hpp"
+#include "datatypes.h"
+#include "options.h"
+#include "tunDevice.h"
+#include "connectionList.h"
+#include "syncQueue.h"
+
+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_)
+ {};
+ Options& opt;
+ TunDevice& dev;
+ PacketSource& src;
+ ConnectionList& cl;
+ SyncQueue & queue;
+ OptionConnectTo & connto;
+};
+
+#endif