added OnConnect Callback
[anytun.git] / src / connectionList.cpp
blobe72fd200923190cf736ef94bd7c9c7286249c25c
1 /*
2 * anytun
4 * The secure anycast tunneling protocol (satp) defines a protocol used
5 * for communication between any combination of unicast and anycast
6 * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel
7 * mode and allows tunneling of every ETHER TYPE protocol (e.g.
8 * ethernet, ip, arp ...). satp directly includes cryptography and
9 * message authentication based on the methodes used by SRTP. It is
10 * intended to deliver a generic, scaleable and secure solution for
11 * tunneling and relaying of packets of any protocol.
14 * Copyright (C) 2007-2008 Othmar Gsenger, Erwin Nindl,
15 * Christian Pointner <satp@wirdorange.org>
17 * This file is part of Anytun.
19 * Anytun is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License version 3 as
21 * published by the Free Software Foundation.
23 * Anytun is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with anytun. If not, see <http://www.gnu.org/licenses/>.
32 #include "threadUtils.hpp"
33 #include "datatypes.h"
34 #include "keyDerivationFactory.h"
35 #include "options.h"
37 #include "connectionList.h"
39 ConnectionList* ConnectionList::inst = NULL;
40 Mutex ConnectionList::instMutex;
41 ConnectionList& gConnectionList = ConnectionList::instance();
44 ConnectionList& ConnectionList::instance()
46 Lock lock(instMutex);
47 static instanceCleaner c;
48 if(!inst)
49 inst = new ConnectionList();
51 return *inst;
54 ConnectionList::ConnectionList()
58 ConnectionList::~ConnectionList()
60 /* Lock lock(mutex_);
61 ConnectionMap::iterator it;
62 for(it = connections_.begin(); it != connections_.end(); ++it)
64 //delete &it->second.kd_;
69 void ConnectionList::addConnection(ConnectionParam &conn, u_int16_t mux )
71 Lock lock(mutex_);
73 std::pair<ConnectionMap::iterator, bool> ret = connections_.insert(ConnectionMap::value_type(mux, conn));
74 if(!ret.second)
76 connections_.erase(ret.first);
77 connections_.insert(ConnectionMap::value_type(mux, conn));
81 const ConnectionMap::iterator ConnectionList::getEnd()
83 return connections_.end();
86 ConnectionMap::iterator ConnectionList::getBeginUnlocked()
88 return connections_.begin();
91 ConnectionMap::iterator ConnectionList::getEndUnlocked()
93 return connections_.end();
96 const ConnectionMap::iterator ConnectionList::getConnection(u_int16_t mux)
98 Lock lock(mutex_);
99 ConnectionMap::iterator it = connections_.find(mux);
100 return it;
104 ConnectionParam & ConnectionList::getOrNewConnectionUnlocked(u_int16_t mux)
106 ConnectionMap::iterator it = connections_.find(mux);
107 if(it!=connections_.end())
108 return it->second;
110 uint8_t key[] = {
111 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
112 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p'
115 uint8_t salt[] = {
116 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
117 'i', 'j', 'k', 'l', 'm', 'n'
120 SeqWindow * seq= new SeqWindow(0);
121 seq_nr_t seq_nr_=0;
122 KeyDerivation * kd = KeyDerivationFactory::create(gOpt.getKdPrf());
123 kd->init(Buffer(key, sizeof(key)), Buffer(salt, sizeof(salt)));
124 ConnectionParam conn ( (*kd), (*seq), seq_nr_, "", 0);
125 connections_.insert(ConnectionMap::value_type(mux, conn));
126 it = connections_.find(mux);
127 return it->second;
130 void ConnectionList::clear()
132 Lock lock(mutex_);
133 connections_.clear();
136 bool ConnectionList::empty()
138 Lock lock(mutex_);
139 return connections_.empty();
142 Mutex& ConnectionList::getMutex()
144 return mutex_;