Imported Upstream version 0.3.4
[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
54   return *inst;
55 }
56
57 ConnectionList::ConnectionList()
58 {
59 }
60
61 ConnectionList::~ConnectionList()
62 {
63   // TODO: comment this in as soon as threads @ main get cleaned up properly
64   //  Lock lock(mutex_);
65   //    ConnectionMap::iterator it;
66   //    for(it = connections_.begin(); it != connections_.end(); ++it) {
67   //     delete &it->second.kd_;
68   //     delete &it->second.seq_window_;
69   //   }
70 }
71
72 void ConnectionList::addConnection(ConnectionParam& conn, uint16_t mux)
73 {
74   Lock lock(mutex_);
75
76   std::pair<ConnectionMap::iterator, bool> ret = connections_.insert(ConnectionMap::value_type(mux, conn));
77   if(!ret.second) {
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(uint16_t mux)
107 {
108   Lock lock(mutex_);
109   ConnectionMap::iterator it = connections_.find(mux);
110   return it;
111 }
112
113
114 ConnectionParam& ConnectionList::getOrNewConnectionUnlocked(uint16_t mux)
115 {
116   ConnectionMap::iterator it = connections_.find(mux);
117   if(it!=connections_.end()) {
118     return it->second;
119   }
120
121   uint8_t key[] = {
122     'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
123     'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p'
124   };
125
126   uint8_t salt[] = {
127     'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
128     'i', 'j', 'k', 'l', 'm', 'n'
129   };
130
131   SeqWindow* seq= new SeqWindow(0);
132   seq_nr_t seq_nr_=0;
133   KeyDerivation* kd = KeyDerivationFactory::create(gOpt.getKdPrf());
134   kd->init(Buffer(key, sizeof(key)), Buffer(salt, sizeof(salt)));
135   ConnectionParam conn((*kd), (*seq), seq_nr_, PacketSourceEndpoint());
136   connections_.insert(ConnectionMap::value_type(mux, conn));
137   it = connections_.find(mux);
138   return it->second;
139 }
140
141 void ConnectionList::clear()
142 {
143   Lock lock(mutex_);
144   connections_.clear();
145 }
146
147 bool ConnectionList::empty()
148 {
149   Lock lock(mutex_);
150   return connections_.empty();
151 }
152
153 Mutex& ConnectionList::getMutex()
154 {
155   return mutex_;
156 }