Imported Upstream version 0.3.4
[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 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 #include "networkPrefix.h"
33 #include "threadUtils.hpp"
34 #include "datatypes.h"
35 #include "anytunError.h"
36
37 #include "routingTable.h"
38 #include "routingTree.hpp"
39
40 RoutingTable* RoutingTable::inst = NULL;
41 Mutex RoutingTable::instMutex;
42 RoutingTable& gRoutingTable = RoutingTable::instance();
43
44
45 RoutingTable& RoutingTable::instance()
46 {
47   Lock lock(instMutex);
48   static instanceCleaner c;
49   if(!inst) {
50     inst = new RoutingTable();
51   }
52
53   return *inst;
54 }
55
56 RoutingTable::RoutingTable()
57 {
58 }
59
60 RoutingTable::~RoutingTable()
61 {
62 }
63
64 void RoutingTable::updateRouteTreeUnlocked(const NetworkPrefix& pref)
65 {
66   //Lock lock(mutex_); //deadlock
67
68   uint8_t length=pref.getNetworkPrefixLength();
69   network_address_type_t type=pref.getNetworkAddressType();
70   uint16_t mux = routes_[pref.getNetworkAddressType()].find(pref)->second;
71   RoutingTreeNode* node = &(root_[type]);
72   if(type==ipv4) {
73     ipv4_bytes_type bytes(pref.to_bytes_v4());
74     if(length>32) {
75       length=32;
76     }
77     RoutingTree::walk(bytes, node, length, mux);
78   } else if(type==ipv6) {
79     ipv6_bytes_type bytes(pref.to_bytes_v6());
80     if(length>128) {
81       length=128;
82     }
83     RoutingTree::walk(bytes, node, length, mux);
84   } else if(type==ethernet) {
85     ethernet_bytes_type bytes(pref.to_bytes_ethernet());
86     if(length>48) {
87       length=48;
88     }
89     RoutingTree::walk(bytes, node, length, mux);
90   } else {
91     AnytunError::throwErr() << "illegal protocol type";
92   }
93   //root_[type].print(0);
94 }
95
96 void RoutingTable::addRoute(const NetworkPrefix& pref, uint16_t mux)
97 {
98   Lock lock(mutex_);
99
100   network_address_type_t type=pref.getNetworkAddressType();
101
102   if(type==ipv4 || type==ipv6) {
103     std::pair<RoutingMap::iterator, bool> ret = routes_[type].insert(RoutingMap::value_type(pref,mux));
104     if(!ret.second) {
105       routes_[pref.getNetworkAddressType()].erase(ret.first);
106       routes_[pref.getNetworkAddressType()].insert(RoutingMap::value_type(pref,mux));
107     }
108     root_[pref.getNetworkAddressType()]=RoutingTreeNode();
109     RoutingMap::iterator it = routes_[type].begin();
110     for(; it!=routes_[pref.getNetworkAddressType()].end(); ++it) {
111       updateRouteTreeUnlocked(it->first);
112     }
113   } else if(type==ethernet) {
114     return; // TODO: add support for ethernet
115   } else {
116     AnytunError::throwErr() << "illegal protocol type";
117   }
118 }
119
120
121 void RoutingTable::delRoute(const NetworkPrefix& pref)
122 {
123   Lock lock(mutex_);
124
125   routes_[pref.getNetworkAddressType()].erase(routes_[pref.getNetworkAddressType()].find(pref));
126 }
127
128 uint16_t RoutingTable::getRoute(const NetworkAddress& addr)
129 {
130   Lock lock(mutex_);
131   network_address_type_t type=addr.getNetworkAddressType();
132
133   if(routes_[type].empty()) {
134     AnytunError::throwErr() << "no route";
135   }
136
137   if(type==ipv4) {
138     ipv4_bytes_type bytes(addr.to_bytes_v4());
139     return RoutingTree::find(bytes, root_[type]);
140   } else if(type==ipv6) {
141     ipv6_bytes_type bytes(addr.to_bytes_v6());
142     return RoutingTree::find(bytes, root_[type]);
143   } else if(type==ethernet) {
144     //TODO Our model wont fit to ethernet addresses well.
145     // maybe use hashmap or something like that instead
146     ethernet_bytes_type bytes(addr.to_bytes_ethernet());
147     return RoutingTree::find(bytes, root_[type]);
148   } else {
149     AnytunError::throwErr() << "illegal protocol type";
150   }
151   return 0;
152 }
153
154 uint16_t* RoutingTable::getOrNewRoutingTEUnlocked(const NetworkPrefix& addr)
155 {
156   RoutingMap::iterator it = routes_[addr.getNetworkAddressType()].find(addr);
157   if(it!=routes_[addr.getNetworkAddressType()].end()) {
158     return &(it->second);
159   }
160
161   routes_[addr.getNetworkAddressType()].insert(RoutingMap::value_type(addr, 1));
162   it = routes_[addr.getNetworkAddressType()].find(addr);
163   return &(it->second);
164 }
165
166 uint16_t RoutingTable::getCountUnlocked(network_address_type_t type)
167 {
168   RoutingMap::iterator it = routes_[type].begin();
169   uint16_t routes=0;
170   for(; it!=routes_[type].end(); ++it) {
171     routes++;
172   }
173   return routes;
174 }
175
176 RoutingMap::iterator RoutingTable::getBeginUnlocked(network_address_type_t type)
177 {
178   return routes_[type].begin();
179 }
180
181 RoutingMap::iterator RoutingTable::getEndUnlocked(network_address_type_t type)
182 {
183   return routes_[type].end();
184 }
185
186 void RoutingTable::clear(network_address_type_t type)
187 {
188   Lock lock(mutex_);
189   routes_[type].clear();
190 }
191
192 bool RoutingTable::empty(network_address_type_t type)
193 {
194   Lock lock(mutex_);
195   return routes_[type].empty();
196 }
197
198 Mutex& RoutingTable::getMutex()
199 {
200   return mutex_;
201 }