Imported Upstream version 0.3.2
[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-2009 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 as published by
21  *  the Free Software Foundation, either version 3 of the License, or
22  *  any later version.
23  *
24  *  Anytun is distributed in the hope that it will be useful,
25  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
26  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  *  GNU General Public License for more details.
28  *
29  *  You should have received a copy of the GNU General Public License
30  *  along with anytun.  If not, see <http://www.gnu.org/licenses/>.
31  */
32
33 #include "threadUtils.hpp"
34 #include "datatypes.h"
35 #include "keyDerivationFactory.h"
36 #include "options.h"
37 #include "packetSource.h"
38
39 #include "connectionList.h"
40
41 ConnectionList* ConnectionList::inst = NULL;
42 Mutex ConnectionList::instMutex;
43 ConnectionList& gConnectionList = ConnectionList::instance();
44
45
46 ConnectionList& ConnectionList::instance()
47 {
48   Lock lock(instMutex);
49   static instanceCleaner c;
50   if(!inst)
51     inst = new ConnectionList();
52
53   return *inst;
54 }
55
56 ConnectionList::ConnectionList()
57 {
58 }
59
60 ConnectionList::~ConnectionList()
61 {
62 // TODO: comment this in as soon as threads @ main get cleaned up properly 
63 //  Lock lock(mutex_);
64 //      ConnectionMap::iterator it;
65 //      for(it = connections_.begin(); it != connections_.end(); ++it) {
66 //     delete &it->second.kd_;
67 //     delete &it->second.seq_window_;
68 //   }
69
70
71 void ConnectionList::addConnection(ConnectionParam &conn, u_int16_t mux )
72 {
73   Lock lock(mutex_);
74
75   std::pair<ConnectionMap::iterator, bool> ret = connections_.insert(ConnectionMap::value_type(mux, conn));
76   if(!ret.second)
77   {
78     connections_.erase(ret.first);
79     connections_.insert(ConnectionMap::value_type(mux, conn));
80   }
81 }
82
83 const ConnectionMap::iterator ConnectionList::getEnd()
84 {
85     Lock lock(mutex_);
86         return connections_.end();
87 }
88
89 ConnectionMap::iterator ConnectionList::getBeginUnlocked()
90 {
91   return connections_.begin();
92 }
93
94 const ConnectionMap::iterator ConnectionList::getBegin()
95 {
96   Lock lock(mutex_);
97   return connections_.begin();
98 }
99
100
101 ConnectionMap::iterator ConnectionList::getEndUnlocked()
102 {
103   return connections_.end();
104 }
105
106 const ConnectionMap::iterator ConnectionList::getConnection(u_int16_t mux)
107 {
108         Lock lock(mutex_);
109         ConnectionMap::iterator it = connections_.find(mux);
110         return it;
111 }
112
113
114 ConnectionParam & ConnectionList::getOrNewConnectionUnlocked(u_int16_t mux)
115 {
116         ConnectionMap::iterator it = connections_.find(mux);
117         if(it!=connections_.end())
118                 return it->second;
119
120   u_int8_t key[] = {
121   'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
122   'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p'
123   };
124   
125   u_int8_t salt[] = {
126   'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
127   'i', 'j', 'k', 'l', 'm', 'n'
128   };
129
130   SeqWindow * seq= new SeqWindow(0);
131   seq_nr_t seq_nr_=0;
132   KeyDerivation * kd = KeyDerivationFactory::create(gOpt.getKdPrf());
133   kd->init(Buffer(key, sizeof(key)), Buffer(salt, sizeof(salt)));
134   ConnectionParam conn ((*kd), (*seq), seq_nr_, PacketSourceEndpoint());
135         connections_.insert(ConnectionMap::value_type(mux, conn));
136         it = connections_.find(mux);
137         return it->second;
138 }
139
140 void ConnectionList::clear()
141 {
142   Lock lock(mutex_);
143         connections_.clear();
144 }
145
146 bool ConnectionList::empty()
147 {
148   Lock lock(mutex_);
149         return connections_.empty();
150 }
151
152 Mutex& ConnectionList::getMutex()
153 {
154   return mutex_;
155 }