added ipv6 routing support
[anytun.git] / src / routingTable.cpp
blobb3aae6e67ff676ce9d6a6b2687757bada23542b2
1 /*
2 * anytun
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.
14 * Copyright (C) 2007-2008 Othmar Gsenger, Erwin Nindl,
15 * Christian Pointner <satp@wirdorange.org>
17 * This file is part of Anytun.
19 * Anytun is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License version 3 as
21 * published by the Free Software Foundation.
23 * Anytun is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with anytun. If not, see <http://www.gnu.org/licenses/>.
31 #include "networkPrefix.h"
32 #include "threadUtils.hpp"
33 #include "datatypes.h"
35 #include "routingTable.h"
37 RoutingTable* RoutingTable::inst = NULL;
38 Mutex RoutingTable::instMutex;
39 RoutingTable& gRoutingTable = RoutingTable::instance();
42 RoutingTable& RoutingTable::instance()
44 Lock lock(instMutex);
45 static instanceCleaner c;
46 if(!inst)
47 inst = new RoutingTable();
49 return *inst;
52 RoutingTable::RoutingTable()
56 RoutingTable::~RoutingTable()
60 void RoutingTable::addRoute(const NetworkPrefix & pref,u_int16_t mux )
62 if ( pref.getNetworkAddressType()!=ipv4 && pref.getNetworkAddressType() != ipv6)
63 return; //TODO add ETHERNET support
64 Lock lock(mutex_);
67 std::pair<RoutingMap::iterator, bool> ret = routes_[pref.getNetworkAddressType()].insert(RoutingMap::value_type(pref,mux));
68 if(!ret.second)
70 routes_[pref.getNetworkAddressType()].erase(ret.first);
71 routes_[pref.getNetworkAddressType()].insert(RoutingMap::value_type(pref,mux));
76 void RoutingTable::delRoute(const NetworkPrefix & pref )
78 Lock lock(mutex_);
80 routes_[pref.getNetworkAddressType()].erase(routes_[pref.getNetworkAddressType()].find(pref));
83 u_int16_t RoutingTable::getRoute(const NetworkAddress & addr)
85 Lock lock(mutex_);
86 if (routes_[addr.getNetworkAddressType()].empty())
87 return 0;
88 //TODO Routing algorithem isnt working!!!
89 NetworkPrefix prefix(addr,128);
90 RoutingMap::iterator it = routes_[addr.getNetworkAddressType()].lower_bound(prefix);
91 // it--;
92 if (it!=routes_[addr.getNetworkAddressType()].end())
93 return it->second;
94 it=routes_[addr.getNetworkAddressType()].begin();
95 return it->second;
98 u_int16_t* RoutingTable::getOrNewRoutingTEUnlocked(const NetworkPrefix & addr)
100 RoutingMap::iterator it = routes_[addr.getNetworkAddressType()].find(addr);
101 if(it!=routes_[addr.getNetworkAddressType()].end())
102 return &(it->second);
104 routes_[addr.getNetworkAddressType()].insert(RoutingMap::value_type(addr, 1));
105 it = routes_[addr.getNetworkAddressType()].find(addr);
106 return &(it->second);
109 u_int16_t RoutingTable::getCountUnlocked(network_address_type_t type)
111 RoutingMap::iterator it = routes_[type].begin();
112 u_int16_t routes=0;
113 for (;it!=routes_[type].end();++it)
114 routes++;
115 return routes;
118 RoutingMap::iterator RoutingTable::getBeginUnlocked(network_address_type_t type)
120 return routes_[type].begin();
123 RoutingMap::iterator RoutingTable::getEndUnlocked(network_address_type_t type)
125 return routes_[type].end();
128 void RoutingTable::clear(network_address_type_t type)
130 Lock lock(mutex_);
131 routes_[type].clear();
134 bool RoutingTable::empty(network_address_type_t type)
136 Lock lock(mutex_);
137 return routes_[type].empty();
140 Mutex& RoutingTable::getMutex()
142 return mutex_;