Imported Upstream version 0.3.5
[anytun.git] / src / routingTable.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 methods 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-2014 Markus Grüneis, 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  *  In addition, as a special exception, the copyright holders give
33  *  permission to link the code of portions of this program with the
34  *  OpenSSL library under certain conditions as described in each
35  *  individual source file, and distribute linked combinations
36  *  including the two.
37  *  You must obey the GNU General Public License in all respects
38  *  for all of the code used other than OpenSSL.  If you modify
39  *  file(s) with this exception, you may extend this exception to your
40  *  version of the file(s), but you are not obligated to do so.  If you
41  *  do not wish to do so, delete this exception statement from your
42  *  version.  If you delete this exception statement from all source
43  *  files in the program, then also delete it here.
44  */
45
46 #include "networkPrefix.h"
47 #include "threadUtils.hpp"
48 #include "datatypes.h"
49 #include "anytunError.h"
50
51 #include "routingTable.h"
52 #include "routingTree.hpp"
53
54 RoutingTable& gRoutingTable = RoutingTable::instance();
55
56 RoutingTable& RoutingTable::instance()
57 {
58   static RoutingTable instance;
59   return instance;
60 }
61
62 RoutingTable::RoutingTable()
63 {
64 }
65
66 RoutingTable::~RoutingTable()
67 {
68 }
69
70 void RoutingTable::updateRouteTreeUnlocked(const NetworkPrefix& pref)
71 {
72   //Lock lock(mutex_); //deadlock
73
74   uint8_t length=pref.getNetworkPrefixLength();
75   network_address_type_t type=pref.getNetworkAddressType();
76   uint16_t mux = routes_[pref.getNetworkAddressType()].find(pref)->second;
77   RoutingTreeNode* node = &(root_[type]);
78   if(type==ipv4) {
79     ipv4_bytes_type bytes(pref.to_bytes_v4());
80     if(length>32) {
81       length=32;
82     }
83     RoutingTree::walk(bytes, node, length, mux);
84   } else if(type==ipv6) {
85     ipv6_bytes_type bytes(pref.to_bytes_v6());
86     if(length>128) {
87       length=128;
88     }
89     RoutingTree::walk(bytes, node, length, mux);
90   } else if(type==ethernet) {
91     ethernet_bytes_type bytes(pref.to_bytes_ethernet());
92     if(length>48) {
93       length=48;
94     }
95     RoutingTree::walk(bytes, node, length, mux);
96   } else {
97     AnytunError::throwErr() << "illegal protocol type";
98   }
99   //root_[type].print(0);
100 }
101
102 void RoutingTable::addRoute(const NetworkPrefix& pref, uint16_t mux)
103 {
104   Lock lock(mutex_);
105
106   network_address_type_t type=pref.getNetworkAddressType();
107
108   if(type==ipv4 || type==ipv6) {
109     std::pair<RoutingMap::iterator, bool> ret = routes_[type].insert(RoutingMap::value_type(pref,mux));
110     if(!ret.second) {
111       routes_[pref.getNetworkAddressType()].erase(ret.first);
112       routes_[pref.getNetworkAddressType()].insert(RoutingMap::value_type(pref,mux));
113     }
114     root_[pref.getNetworkAddressType()]=RoutingTreeNode();
115     RoutingMap::iterator it = routes_[type].begin();
116     for(; it!=routes_[pref.getNetworkAddressType()].end(); ++it) {
117       updateRouteTreeUnlocked(it->first);
118     }
119   } else if(type==ethernet) {
120     return; // TODO: add support for ethernet
121   } else {
122     AnytunError::throwErr() << "illegal protocol type";
123   }
124 }
125
126
127 void RoutingTable::delRoute(const NetworkPrefix& pref)
128 {
129   Lock lock(mutex_);
130
131   routes_[pref.getNetworkAddressType()].erase(routes_[pref.getNetworkAddressType()].find(pref));
132 }
133
134 uint16_t RoutingTable::getRoute(const NetworkAddress& addr)
135 {
136   Lock lock(mutex_);
137   network_address_type_t type=addr.getNetworkAddressType();
138
139   if(routes_[type].empty()) {
140     AnytunError::throwErr() << "no route";
141   }
142
143   if(type==ipv4) {
144     ipv4_bytes_type bytes(addr.to_bytes_v4());
145     return RoutingTree::find(bytes, root_[type]);
146   } else if(type==ipv6) {
147     ipv6_bytes_type bytes(addr.to_bytes_v6());
148     return RoutingTree::find(bytes, root_[type]);
149   } else if(type==ethernet) {
150     //TODO Our model wont fit to ethernet addresses well.
151     // maybe use hashmap or something like that instead
152     ethernet_bytes_type bytes(addr.to_bytes_ethernet());
153     return RoutingTree::find(bytes, root_[type]);
154   } else {
155     AnytunError::throwErr() << "illegal protocol type";
156   }
157   return 0;
158 }
159
160 uint16_t* RoutingTable::getOrNewRoutingTEUnlocked(const NetworkPrefix& addr)
161 {
162   RoutingMap::iterator it = routes_[addr.getNetworkAddressType()].find(addr);
163   if(it!=routes_[addr.getNetworkAddressType()].end()) {
164     return &(it->second);
165   }
166
167   routes_[addr.getNetworkAddressType()].insert(RoutingMap::value_type(addr, 1));
168   it = routes_[addr.getNetworkAddressType()].find(addr);
169   return &(it->second);
170 }
171
172 uint16_t RoutingTable::getCountUnlocked(network_address_type_t type)
173 {
174   RoutingMap::iterator it = routes_[type].begin();
175   uint16_t routes=0;
176   for(; it!=routes_[type].end(); ++it) {
177     routes++;
178   }
179   return routes;
180 }
181
182 RoutingMap::iterator RoutingTable::getBeginUnlocked(network_address_type_t type)
183 {
184   return routes_[type].begin();
185 }
186
187 RoutingMap::iterator RoutingTable::getEndUnlocked(network_address_type_t type)
188 {
189   return routes_[type].end();
190 }
191
192 void RoutingTable::clear(network_address_type_t type)
193 {
194   Lock lock(mutex_);
195   routes_[type].clear();
196 }
197
198 bool RoutingTable::empty(network_address_type_t type)
199 {
200   Lock lock(mutex_);
201   return routes_[type].empty();
202 }
203
204 Mutex& RoutingTable::getMutex()
205 {
206   return mutex_;
207 }