RUNetworkConfigurator.h

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2010 Institut fuer Telematik, Karlsruher Institut fuer Technologie (KIT)
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 //
00018 
00026 #ifndef RUNetworkConfigurator_H_
00027 #define RUNetworkConfigurator_H_
00028 
00029 #include <omnetpp.h>
00030 #include <cctype>
00031 #include <vector>
00032 #include <map>
00033 #include <ctopology.h>
00034 #include <string>
00035 #include <iostream>
00036 #include "INETDefs.h"
00037 #include "IPAddress.h"
00038 #include "RoutingTable.h"
00039 #include "InterfaceTable.h"
00040 #include "IPAddressResolver.h"
00041 #include "NetworkConfigurator.h"
00042 #include "IPv4InterfaceData.h"
00043 #include "InterfaceEntry.h"
00044 
00045 using std::vector;
00046 using std::map;
00047 using std::string;
00048 using std::cerr;
00049 using std::cout;
00050 using std::endl;
00051 
00052 const int INIT_STAGES = 3;
00053 // unique hex values for different router- and AS-level node types
00054 const int TRANSIT_AS = 1;
00055 const int STUB_AS = 2;
00056 const int UNSPECIFIED = -1;
00057 const int CORE = 1;
00058 const int GW = 2;
00059 const int EDGE = 3;
00060 const int ENDSYS = 4;
00061 
00062 typedef vector<string> StringVector;
00063 
00072 struct nodeInfoRL
00073 {
00074     bool isIPNode;
00075     IInterfaceTable *ift;
00076     InterfaceEntry *defaultRouteIE;
00077     int asId, asType, routerType, moduleId;
00078     IRoutingTable *rt;
00079     IPAddress addr;
00080     bool usesDefaultRoute;
00081     cModule *module;
00082     cTopology::Node *node;
00083 
00084     nodeInfoRL() {};
00085     nodeInfoRL(cTopology::Node *node)
00086     {
00087         this->node = node;
00088         module = node->getModule();
00089         moduleId = module->getId();
00090         ift = IPAddressResolver().findInterfaceTableOf(module);
00091         rt = IPAddressResolver().findRoutingTableOf(module);
00092         isIPNode = (rt != NULL);
00093         int index = 0;
00094         string fullPath = module->getFullPath();
00095 
00096         // check if stubstring "sas" (StubAS) or "tas" (TransitAS)
00097         // is contained in fullPath
00098         if ( (index = fullPath.find("sas")) != -1 )
00099             asType = STUB_AS;
00100         else if ( (index = fullPath.find("tas")) != -1 )
00101             asType = TRANSIT_AS;
00102         else if ( (index = fullPath.find("ReaSEUnderlayNetwork")) != -1)
00103             asType = UNSPECIFIED;
00104         else {
00105             cerr << "found module that doesn't belong to Transit AS (tas) or Stub AS (sas): "<< fullPath<<endl;
00106             opp_error("found module that doesn't belong to Transit AS (tas) or Stub AS (sas)");
00107         }
00108 
00109         // set index to char position after substring "sas/tas"
00110         if (asType == STUB_AS || asType == TRANSIT_AS) {
00111             index += 3;
00112             string currentId;
00113             while (isdigit(fullPath[index]) && (index < (int) fullPath.length()))
00114                 currentId += fullPath[index++];
00115             asId = atoi(currentId.data());
00116         }
00117 
00118         if (module->getProperties()->get("CoreRouter"))
00119             routerType = CORE;
00120         else if (module->getProperties()->get("GatewayRouter"))
00121             routerType = GW;
00122         else if (module->getProperties()->get("EdgeRouter"))
00123             routerType = EDGE;
00124         else if (module->getProperties()->get("Host"))
00125             routerType = ENDSYS;
00126         else {
00127             cerr<<"found module without valid type: "<<fullPath<<endl;
00128             opp_error("found module without valid type");
00129         }
00130         //
00131         // determine default interface
00132         //
00133         if (routerType == CORE) {
00134             // find last interface that is not loopback
00135             for (int i=0; i<ift->getNumInterfaces(); i++)
00136                 if (!ift->getInterface(i)->isLoopback())
00137                     addr = ift->getInterface(i)->ipv4Data()->getIPAddress();
00138             defaultRouteIE = NULL;
00139         }
00140         else {
00141             for (int i=0; i<ift->getNumInterfaces(); i++) {
00142                 if (!ift->getInterface(i)->isLoopback()) {
00143                     // find first interface that is not loopback and is connected to
00144                     // a higher level node. Then, create default route
00145                     addr = ift->getInterface(i)->ipv4Data()->getIPAddress();
00146                     if (routerType == GW) {
00147                         if (module->gate(ift->getInterface(i)->getNodeOutputGateId())\
00148                             ->getNextGate()->getOwnerModule()->getProperties()->get("CoreRouter")) {
00149                             defaultRouteIE = ift->getInterface(i);
00150                             break;
00151                         }
00152                     }
00153                     else if (routerType == EDGE) {
00154                         if (module->gate(ift->getInterface(i)->getNodeOutputGateId())\
00155                             ->getNextGate()->getOwnerModule()->getProperties()->get("GatewayRouter")) {
00156                             defaultRouteIE = ift->getInterface(i);
00157                             break;
00158                         }
00159                     }else if (routerType == ENDSYS) {
00160                         if (module->gate(ift->getInterface(i)->getNodeOutputGateId())\
00161                             ->getNextGate()->getOwnerModule()->getProperties()->get("EdgeRouter")) {
00162                             defaultRouteIE = ift->getInterface(i);
00163                             break;
00164                         }
00165                     }
00166                 }
00167             }
00168         }
00169     };
00170 
00171 };
00172 
00173 struct edgeRouter
00174 {
00175     unsigned int edgeIP;
00176     int usedIPs;
00177     cModule *module;
00178 };
00179 
00180 typedef std::vector<nodeInfoRL> NODE_INFO_RL_VEC;
00181 typedef std::map<int, nodeInfoRL> NODE_MAP;
00182 typedef std::pair<int, nodeInfoRL> NODE_MAP_PAIR;
00183 typedef std::vector<edgeRouter> EDGE_ROUTER_VEC;
00184 
00191 struct nodeInfoAS
00192 {
00193     int id;
00194     int asType;
00195     cTopology::Node *node;
00196     cModule *module;
00197     NODE_MAP nodeMap;
00198     NODE_INFO_RL_VEC coreNode;
00199     IPAddress addr;
00200     IPAddress netmask;
00201     IPAddress subnetmask;
00202     EDGE_ROUTER_VEC edgeRouter;
00203 
00204     nodeInfoAS(cTopology::Node *node, IPAddress a, IPAddress m) {
00205         this->node = node;
00206         this->module = node->getModule();
00207         addr = a;
00208         netmask = m;
00209         int index = 0;
00210         string fullPath = node->getModule()->getFullPath();
00211 
00212         // check if stubstring "sas" (StubAS) or "tas" (TransitAS)
00213         // is contained in fullPath
00214         if ( (index = fullPath.find(".sas")) != -1 )
00215             asType = STUB_AS;
00216         else if ( (index = fullPath.find(".tas")) != -1 )
00217             asType = TRANSIT_AS;
00218         else if ( (index = fullPath.find("ReaSEUnderlayNetwork")) != -1 )
00219             asType = UNSPECIFIED;
00220         else
00221         {
00222             cerr << "found module that doesn't belong to TAS or SAS: "<< fullPath<<endl;
00223             opp_error("found module that doesn't belong to TAS or SAS");
00224         }
00225 
00226         // set index to char position after substring "sas/tas"
00227         if (asType == STUB_AS || asType == TRANSIT_AS)
00228         {
00229             index += 3;
00230             string currentId;
00231             while (isdigit(fullPath[index]) && (index < (int) fullPath.length()))
00232                 currentId += fullPath[index++];
00233             id = atoi(currentId.data());
00234         }
00235     }
00236 };
00237 
00238 
00239 typedef std::vector<nodeInfoAS> NODE_INFO_AS_VEC;
00240 
00252 class RUNetworkConfigurator : public cSimpleModule
00253 {
00254 protected:
00255     std::vector<cTopology*> rlTopology;
00256     cTopology asTopology;
00257     int noAS;
00258     int nextPow; //<! number bits for AS addressing
00259     NODE_INFO_AS_VEC asNodeVec;
00260     unsigned int IP_NET_SHIFT; //<! number of bits reserved for AS addressing
00261     uint32_t NET_MASK; //<! netmask calculated by netshift
00262 public:
00263     RUNetworkConfigurator();
00264     virtual ~RUNetworkConfigurator();
00265 
00266 protected:
00267     //
00268     // stage = 0 --> register interfaces
00269     //
00270     virtual int numInitStages() const {return 3;}
00277     virtual void initialize(int stage);
00278     virtual void handleMessage(cMessage *msg) {opp_error("message received");};
00287     void createInterASPaths();
00293     void disableStubLinks(nodeInfoRL &dst, nodeInfoRL &src);
00298     void enableStubLinks();
00305     void extractTopology();
00315     void assignAddressAndSetDefaultRoutes(nodeInfoAS &asInfo);
00325     void setIntraASRoutes(cTopology &topology, nodeInfoAS &asInfo);
00326 
00327 };
00328 namespace RUNetConf {
00329 
00341 static bool getCoreNodes(cModule *curMod, void *nullPointer);
00354 static bool getRouterLevelNodes(cModule *curMod, void *name);
00355 };
00356 #endif /*RUNetworkConfigurator_H_*/