Imported Upstream version 0.3
[anytun.git] / src / connectionList.cpp
1 /*
2  *  anytun
3  *
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.
12  *
13  *
14  *  Copyright (C) 2007-2008 Othmar Gsenger, Erwin Nindl, 
15  *                          Christian Pointner <satp@wirdorange.org>
16  *
17  *  This file is part of Anytun.
18  *
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.
22  *
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.
27  *
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/>.
30  */
31
32 #include "threadUtils.hpp"
33 #include "datatypes.h"
34 #include "keyDerivationFactory.h"
35 #include "options.h"
36 #include "packetSource.h"
37
38 #include "connectionList.h"
39
40 ConnectionList* ConnectionList::inst = NULL;
41 Mutex ConnectionList::instMutex;
42 ConnectionList& gConnectionList = ConnectionList::instance();
43
44
45 ConnectionList& ConnectionList::instance()
46 {
47   Lock lock(instMutex);
48   static instanceCleaner c;
49   if(!inst)
50     inst = new ConnectionList();
51
52   return *inst;
53 }
54
55 ConnectionList::ConnectionList()
56 {
57 }
58
59 ConnectionList::~ConnectionList()
60 {
61 // TODO: comment this in as soon as threads @ main get cleaned up properly 
62 //  Lock lock(mutex_);
63 //      ConnectionMap::iterator it;
64 //      for(it = connections_.begin(); it != connections_.end(); ++it) {
65 //     delete &it->second.kd_;
66 //     delete &it->second.seq_window_;
67 //   }
68
69
70 void ConnectionList::addConnection(ConnectionParam &conn, u_int16_t mux )
71 {
72   Lock lock(mutex_);
73
74   std::pair<ConnectionMap::iterator, bool> ret = connections_.insert(ConnectionMap::value_type(mux, conn));
75   if(!ret.second)
76   {
77     connections_.erase(ret.first);
78     connections_.insert(ConnectionMap::value_type(mux, conn));
79   }
80 }
81
82 const ConnectionMap::iterator ConnectionList::getEnd()
83 {
84     Lock lock(mutex_);
85         return connections_.end();
86 }
87
88 ConnectionMap::iterator ConnectionList::getBeginUnlocked()
89 {
90   return connections_.begin();
91 }
92
93 const ConnectionMap::iterator ConnectionList::getBegin()
94 {
95   Lock lock(mutex_);
96   return connections_.begin();
97 }
98
99
100 ConnectionMap::iterator ConnectionList::getEndUnlocked()
101 {
102   return connections_.end();
103 }
104
105 const ConnectionMap::iterator ConnectionList::getConnection(u_int16_t mux)
106 {
107         Lock lock(mutex_);
108         ConnectionMap::iterator it = connections_.find(mux);
109         return it;
110 }
111
112
113 ConnectionParam & ConnectionList::getOrNewConnectionUnlocked(u_int16_t mux)
114 {
115         ConnectionMap::iterator it = connections_.find(mux);
116         if(it!=connections_.end())
117                 return it->second;
118
119   u_int8_t key[] = {
120   'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
121   'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p'
122   };
123   
124   u_int8_t salt[] = {
125   'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
126   'i', 'j', 'k', 'l', 'm', 'n'
127   };
128
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;
137 }
138
139 void ConnectionList::clear()
140 {
141   Lock lock(mutex_);
142         connections_.clear();
143 }
144
145 bool ConnectionList::empty()
146 {
147   Lock lock(mutex_);
148         return connections_.empty();
149 }
150
151 Mutex& ConnectionList::getMutex()
152 {
153   return mutex_;
154 }