![]() |
User Manual, Developers Guide and API Documentation |
![]() |
00001 /******************************************************************************* 00002 * This file is part of openWNS (open Wireless Network Simulator) 00003 * _____________________________________________________________________________ 00004 * 00005 * Copyright (C) 2004-2007 00006 * Chair of Communication Networks (ComNets) 00007 * Kopernikusstr. 5, D-52074 Aachen, Germany 00008 * phone: ++49-241-80-27910, 00009 * fax: ++49-241-80-22242 00010 * email: info@openwns.org 00011 * www: http://www.openwns.org 00012 * _____________________________________________________________________________ 00013 * 00014 * openWNS is free software; you can redistribute it and/or modify it under the 00015 * terms of the GNU Lesser General Public License version 2 as published by the 00016 * Free Software Foundation; 00017 * 00018 * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY 00019 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 00020 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 00021 * details. 00022 * 00023 * You should have received a copy of the GNU Lesser General Public License 00024 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00025 * 00026 ******************************************************************************/ 00027 00028 #include <IP/Routing.hpp> 00029 00030 #include <IP/Forwarding.hpp> 00031 00032 #include <WNS/module/Base.hpp> 00033 #include <WNS/Assure.hpp> 00034 #include <WNS/node/component/Component.hpp> 00035 #include <WNS/pyconfig/View.hpp> 00036 00037 #include <iomanip> 00038 #include <sstream> 00039 00040 using namespace ip; 00041 using namespace wns::service::nl; 00042 00043 STATIC_FACTORY_REGISTER_WITH_CREATOR( 00044 Routing, 00045 wns::ldk::FunctionalUnit, 00046 "ip.routing", 00047 wns::ldk::FUNConfigCreator); 00048 00049 wns::service::nl::Address Routing::defaultGateway = wns::service::nl::Address("0.0.0.0"); 00050 00051 Routing::Routing(wns::ldk::fun::FUN* fun, const wns::pyconfig::View& _pyco) : 00052 wns::ldk::CommandTypeSpecifier<>(fun), 00053 wns::ldk::HasReceptor<>(), 00054 wns::ldk::HasConnector<>(), 00055 wns::ldk::HasDeliverer<>(), 00056 wns::ldk::Processor<Routing>(), 00057 wns::Cloneable<Routing>(), 00058 ipHeaderReader(NULL), 00059 pyco(_pyco), 00060 log(_pyco.get("logger")) 00061 { 00062 for (int ii=0; ii < getConfig().len("routingTable"); ++ii) 00063 { 00064 wns::pyconfig::View re_conf = getConfig().get<wns::pyconfig::View>("routingTable", ii); 00065 container::RoutingTableEntry route = container::RoutingTableEntry(re_conf); 00066 00067 rt.insert(rt.end(), route); 00068 } 00069 } 00070 00071 Routing::~Routing() 00072 { 00073 } 00074 00075 void 00076 Routing::onFUNCreated() 00077 { 00078 ipHeaderReader = getFUN()->getCommandReader("ip.ipHeader"); 00079 assure(ipHeaderReader, "No reader for the IP Header available!"); 00080 } // onFUNCreated 00081 00082 void 00083 Routing::addRoute(const wns::service::nl::Address& netAddress, 00084 const wns::service::nl::Address& gateway, 00085 const wns::service::nl::Address& netMask, 00086 std::string dllName) 00087 { 00088 container::RoutingTableEntry route; 00089 route.netaddress = netAddress; 00090 route.gateway = gateway; 00091 route.netmask = netMask; 00092 route.dllName = dllName; 00093 00094 rt.insert(rt.begin(), route); 00095 } 00096 00097 void 00098 Routing::processOutgoing(const wns::ldk::CompoundPtr& compound) 00099 { 00100 if (!forwardViaRoutingTable(compound)) 00101 { 00102 MESSAGE_SINGLE(NORMAL, log, "No route to host!"); 00103 } 00104 } 00105 00106 void 00107 Routing::processIncoming(const wns::ldk::CompoundPtr&) 00108 { 00109 } 00110 00111 void 00112 Routing::setDLLs(container::DataLinkContainer _dlls) 00113 { 00114 this->dlls = _dlls; 00115 } 00116 00117 bool 00118 Routing::forwardViaRoutingTable(const wns::ldk::CompoundPtr& compound) 00119 { 00120 assure(ipHeaderReader, "No reader for the IP Header available!"); 00121 IPCommand* ipHeader = ipHeaderReader->readCommand<IPCommand>(compound->getCommandPool()); 00122 00123 Address destAddress = ipHeader->peer.destination; 00124 00125 #ifndef WNS_NO_LOGGING 00126 printRoutingTable(destAddress); 00127 #endif 00128 00129 for(container::RoutingTable::const_iterator it=rt.begin(); it!=rt.end(); ++it) 00130 { 00131 Address netAddress = it->netaddress; 00132 Address netmask = it->netmask; 00133 00134 if (netAddress == (destAddress&netmask)) 00135 { 00136 if (it->gateway == defaultGateway) 00137 { 00138 ipHeader->local.nextHop = destAddress; 00139 } 00140 else 00141 { 00142 ipHeader->local.nextHop = it->gateway; 00143 } 00144 00145 ipHeader->local.dllName = it->dllName; 00146 00147 container::DataLink dll = dlls.find(ipHeader->local.dllName); 00148 00149 ipHeader->local.arpZone = dll.arpZone; 00150 00151 // If IP destination is directly reachable 00152 // local.routingDone is true 00153 // rc->local.routingDone; 00154 return true; 00155 } 00156 } 00157 return false; 00158 } 00159 00160 void 00161 Routing::printRoutingTable(const wns::service::nl::Address& highlight) const 00162 { 00163 MESSAGE_BEGIN(NORMAL, log, m, ""); 00164 m << "" << std::setw(18) << std::left << "Destination" 00165 << std::setw(18) << std::left << "Gateway" 00166 << std::setw(18) << std::left << "Genmask" 00167 << "Iface"; 00168 MESSAGE_END(); 00169 00170 bool theFirstHitIsTheDeepest = false; 00171 00172 for (container::RoutingTable::const_iterator it=rt.begin(); it!=rt.end(); ++it) 00173 { 00174 MESSAGE_BEGIN(NORMAL, log, m, ""); 00175 m << "" << std::setw(18) << std::left << Address(it->netaddress) 00176 << std::setw(18) << std::left<< Address(it->gateway) 00177 << std::setw(18) << std::left<< Address(it->netmask) 00178 << it->dllName; 00179 00180 if (highlight != defaultGateway && !theFirstHitIsTheDeepest) 00181 { 00182 if ( (highlight & it->netmask) == it->netaddress) 00183 { 00184 m << " ***HIT***"; 00185 theFirstHitIsTheDeepest = true; 00186 } 00187 } 00188 MESSAGE_END(); 00189 } 00190 } 00191 00192 wns::pyconfig::View 00193 Routing::getConfig() 00194 { 00195 return pyco; 00196 } 00197 00198 /*void 00199 Routing::setDLLService(Address address, wns::service::dll::UnicastDataTransmission* dll) 00200 { 00201 dlls[address] = dll; 00202 }*/ 00203 00204 00205
1.5.5