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.
14 * Copyright (C) 2007-2014 Markus Grüneis, 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 as published by
21 * the Free Software Foundation, either version 3 of the License, or
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.
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/>.
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
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.
46 #include "networkPrefix.h"
47 #include "threadUtils.hpp"
48 #include "datatypes.h"
49 #include "anytunError.h"
51 #include "routingTable.h"
52 #include "routingTree.hpp"
54 RoutingTable& gRoutingTable = RoutingTable::instance();
56 RoutingTable& RoutingTable::instance()
58 static RoutingTable instance;
62 RoutingTable::RoutingTable()
66 RoutingTable::~RoutingTable()
70 void RoutingTable::updateRouteTreeUnlocked(const NetworkPrefix& pref)
72 //Lock lock(mutex_); //deadlock
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]);
79 ipv4_bytes_type bytes(pref.to_bytes_v4());
83 RoutingTree::walk(bytes, node, length, mux);
84 } else if(type==ipv6) {
85 ipv6_bytes_type bytes(pref.to_bytes_v6());
89 RoutingTree::walk(bytes, node, length, mux);
90 } else if(type==ethernet) {
91 ethernet_bytes_type bytes(pref.to_bytes_ethernet());
95 RoutingTree::walk(bytes, node, length, mux);
97 AnytunError::throwErr() << "illegal protocol type";
99 //root_[type].print(0);
102 void RoutingTable::addRoute(const NetworkPrefix& pref, uint16_t mux)
106 network_address_type_t type=pref.getNetworkAddressType();
108 if(type==ipv4 || type==ipv6) {
109 std::pair<RoutingMap::iterator, bool> ret = routes_[type].insert(RoutingMap::value_type(pref,mux));
111 routes_[pref.getNetworkAddressType()].erase(ret.first);
112 routes_[pref.getNetworkAddressType()].insert(RoutingMap::value_type(pref,mux));
114 root_[pref.getNetworkAddressType()]=RoutingTreeNode();
115 RoutingMap::iterator it = routes_[type].begin();
116 for(; it!=routes_[pref.getNetworkAddressType()].end(); ++it) {
117 updateRouteTreeUnlocked(it->first);
119 } else if(type==ethernet) {
120 return; // TODO: add support for ethernet
122 AnytunError::throwErr() << "illegal protocol type";
127 void RoutingTable::delRoute(const NetworkPrefix& pref)
131 routes_[pref.getNetworkAddressType()].erase(routes_[pref.getNetworkAddressType()].find(pref));
134 uint16_t RoutingTable::getRoute(const NetworkAddress& addr)
137 network_address_type_t type=addr.getNetworkAddressType();
139 if(routes_[type].empty()) {
140 AnytunError::throwErr() << "no route";
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]);
155 AnytunError::throwErr() << "illegal protocol type";
160 uint16_t* RoutingTable::getOrNewRoutingTEUnlocked(const NetworkPrefix& addr)
162 RoutingMap::iterator it = routes_[addr.getNetworkAddressType()].find(addr);
163 if(it!=routes_[addr.getNetworkAddressType()].end()) {
164 return &(it->second);
167 routes_[addr.getNetworkAddressType()].insert(RoutingMap::value_type(addr, 1));
168 it = routes_[addr.getNetworkAddressType()].find(addr);
169 return &(it->second);
172 uint16_t RoutingTable::getCountUnlocked(network_address_type_t type)
174 RoutingMap::iterator it = routes_[type].begin();
176 for(; it!=routes_[type].end(); ++it) {
182 RoutingMap::iterator RoutingTable::getBeginUnlocked(network_address_type_t type)
184 return routes_[type].begin();
187 RoutingMap::iterator RoutingTable::getEndUnlocked(network_address_type_t type)
189 return routes_[type].end();
192 void RoutingTable::clear(network_address_type_t type)
195 routes_[type].clear();
198 bool RoutingTable::empty(network_address_type_t type)
201 return routes_[type].empty();
204 Mutex& RoutingTable::getMutex()