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