moved rtp stuff to anyrtpproxy and removed it from anytun
[anytun.git] / src / connectionList.cpp
bloba0d4abfc44fba70ed0642202b0e92a24ec2108d6
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"
36 #include "packetSource.h"
38 #include "connectionList.h"
40 ConnectionList* ConnectionList::inst = NULL;
41 Mutex ConnectionList::instMutex;
42 ConnectionList& gConnectionList = ConnectionList::instance();
45 ConnectionList& ConnectionList::instance()
47 Lock lock(instMutex);
48 static instanceCleaner c;
49 if(!inst)
50 inst = new ConnectionList();
52 return *inst;
55 ConnectionList::ConnectionList()
59 ConnectionList::~ConnectionList()
61 /* Lock lock(mutex_);
62 ConnectionMap::iterator it;
63 for(it = connections_.begin(); it != connections_.end(); ++it)
65 //delete &it->second.kd_;
70 void ConnectionList::addConnection(ConnectionParam &conn, u_int16_t mux )
72 Lock lock(mutex_);
74 std::pair<ConnectionMap::iterator, bool> ret = connections_.insert(ConnectionMap::value_type(mux, conn));
75 if(!ret.second)
77 connections_.erase(ret.first);
78 connections_.insert(ConnectionMap::value_type(mux, conn));
82 const ConnectionMap::iterator ConnectionList::getEnd()
84 Lock lock(mutex_);
85 return connections_.end();
88 ConnectionMap::iterator ConnectionList::getBeginUnlocked()
90 return connections_.begin();
93 const ConnectionMap::iterator ConnectionList::getBegin()
95 Lock lock(mutex_);
96 return connections_.begin();
100 ConnectionMap::iterator ConnectionList::getEndUnlocked()
102 return connections_.end();
105 const ConnectionMap::iterator ConnectionList::getConnection(u_int16_t mux)
107 Lock lock(mutex_);
108 ConnectionMap::iterator it = connections_.find(mux);
109 return it;
113 ConnectionParam & ConnectionList::getOrNewConnectionUnlocked(u_int16_t mux)
115 ConnectionMap::iterator it = connections_.find(mux);
116 if(it!=connections_.end())
117 return it->second;
119 u_int8_t key[] = {
120 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
121 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p'
124 u_int8_t salt[] = {
125 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
126 'i', 'j', 'k', 'l', 'm', 'n'
129 SeqWindow * seq= new SeqWindow(0);
130 seq_nr_t seq_nr_=0;
131 KeyDerivation * kd = KeyDerivationFactory::create(gOpt.getKdPrf());
132 kd->init(Buffer(key, sizeof(key)), Buffer(salt, sizeof(salt)));
133 ConnectionParam conn ( (*kd), (*seq), seq_nr_, PacketSourceEndpoint());
134 connections_.insert(ConnectionMap::value_type(mux, conn));
135 it = connections_.find(mux);
136 return it->second;
139 void ConnectionList::clear()
141 Lock lock(mutex_);
142 connections_.clear();
145 bool ConnectionList::empty()
147 Lock lock(mutex_);
148 return connections_.empty();
151 Mutex& ConnectionList::getMutex()
153 return mutex_;