AccessNet.cc

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
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 
00024 #include <vector>
00025 #include <iostream>
00026 
00027 #include <omnetpp.h>
00028 
00029 #include <IRoutingTable.h>
00030 #include <IInterfaceTable.h>
00031 #include <IPAddressResolver.h>
00032 #include <IPv4InterfaceData.h>
00033 
00034 #include "AccessNet.h"
00035 
00036 Define_Module(AccessNet);
00037 
00038 std::ostream& operator<<(std::ostream& os, NodeInfo& n)
00039 {
00040     os << IPAddress(n.IPAddress);
00041     return os;
00042 }
00043 
00044 void AccessNet::initialize(int stage)
00045 {
00046     if(stage != MIN_STAGE_UNDERLAY + 1)
00047         return;
00048 
00049     router.module = getParentModule();
00050     router.interfaceTable = IPAddressResolver().interfaceTableOf(getParentModule());
00051     router.routingTable = IPAddressResolver().routingTableOf(getParentModule());
00052     router.IPAddress = IPAddressResolver().addressOf(getParentModule()).get4().getInt();
00053 
00054     // statistics
00055     lifetimeVector.setName("Terminal Lifetime");
00056 
00057     WATCH_VECTOR(overlayTerminal);
00058 
00059     lastIP = 0;
00060 
00061     updateDisplayString();
00062 }
00063 
00064 void AccessNet::handleMessage(cMessage* msg)
00065 {
00066     error("this module doesn't handle messages, it runs only in initialize()");
00067 }
00068 
00069 int AccessNet::addOverlayNode(cModule* node, bool migrate)
00070 {
00071     Enter_Method("addOverlayNode()");
00072 
00073     TerminalInfo terminal;
00074     terminal.module = node;
00075     terminal.interfaceTable = IPAddressResolver().interfaceTableOf(node);
00076     terminal.remoteInterfaceTable = router.interfaceTable;
00077     terminal.routingTable = IPAddressResolver().routingTableOf(node);
00078     terminal.PPPInterface = node->getSubmodule("ppp", 0);
00079     terminal.createdAt = simTime();
00080 
00081     // find unassigned ip address:
00082     //   Start at last given address, check if next address is valid and free.
00083     bool ip_test = false;
00084     for (uint32 ipOffset = lastIP + 1; ipOffset != lastIP; ipOffset++) {
00085         if ( ipOffset == 0x10000) {
00086             // Netmask = 255.255.0.0, so roll over if offset = 2**16
00087             ipOffset = 0;
00088             continue;
00089         }
00090 
00091         uint32_t ip = router.IPAddress + ipOffset;
00092 
00093         // Check if IP is valid:
00094         //   Reject x.y.z.0 or x.y.z.255 or x.y.255.z
00095         if ( ((ip & 0xff) == 0) || ((ip & 0xff) == 0xff)
00096              || ((ip & 0xff00) == 0xff00) ) {
00097             continue;
00098         }
00099 
00100         // Check if IP is free
00101         ip_test = true;
00102         for (uint32_t i = 0; i < overlayTerminal.size(); i++) {
00103             if (overlayTerminal[i].IPAddress == ip) {
00104                 ip_test = false;
00105                 break;
00106             }
00107         }
00108 
00109         // found valid IP
00110         if (ip_test) {
00111             terminal.IPAddress = ip;
00112             lastIP = ipOffset;
00113             break;
00114         }
00115     }
00116     if (!ip_test)
00117         opp_error ("Error creating node: No available IP in access net!");
00118 
00119     // update ip display string
00120     if (ev.isGUI()) {
00121         const char* ip_disp = const_cast<char*>
00122             (IPAddress(terminal.IPAddress).str().c_str());
00123         terminal.module->getDisplayString().insertTag("t", 0);
00124         terminal.module->getDisplayString().setTagArg("t", 0, ip_disp);
00125         terminal.module->getDisplayString().setTagArg("t", 1, "l");
00126     }
00127 
00128 
00129     //
00130     // Create new remote ppp interface module for this terminal
00131     //
00132 
00133     // create ppp interface module
00134 
00135     int k = 1;
00136     while ( router.module->findSubmodule("ppp", k) != -1 )
00137         k++;
00138 
00139     cModuleType* pppInterfaceModuleType = cModuleType::get("inet.linklayer.ppp.PPPInterface");
00140     terminal.remotePPPInterface = pppInterfaceModuleType->
00141         create("ppp", router.module, 0, k);
00142 
00143 
00144     //
00145     // Connect all gates
00146     //
00147 
00148     // connect terminal to access router and vice versa
00149     cGate* routerInGate = firstUnusedGate(router.module, "pppg", cGate::INPUT);
00150     cGate* routerOutGate = firstUnusedGate(router.module, "pppg", cGate::OUTPUT);
00151 
00152     cChannelType* channelType = cChannelType::find( channelTypeStr.c_str() );
00153     if (!channelType) opp_error("Could not find Channel Type. Most likely "
00154                     "parameter channelTypes does not match the channels defined "
00155                     "in channels.ned");
00156 
00157     terminal.module->gate("pppg$o", 0)->connectTo(routerInGate,
00158                                 channelType->create(channelTypeStr.c_str()));
00159     routerOutGate->connectTo(terminal.module->gate("pppg$i", 0),
00160                              channelType->create(channelTypeStr.c_str()));
00161 
00162     // connect ppp interface module to router module and vice versa
00163     routerInGate->connectTo(terminal.remotePPPInterface->gate("phys$i"));
00164     terminal.remotePPPInterface->gate("phys$o")->connectTo(routerOutGate);
00165 
00166     // connect ppp interface module to network layer module and vice versa
00167     cModule* netwModule = router.module->getSubmodule("networkLayer");
00168 
00169     cGate* netwInGate = firstUnusedGate(netwModule, "ifIn");
00170     cGate* netwOutGate = firstUnusedGate(netwModule, "ifOut");
00171 
00172     netwOutGate->connectTo(terminal.remotePPPInterface->gate("netwIn"));
00173     terminal.remotePPPInterface->gate("netwOut")->connectTo(netwInGate);
00174 
00175     // connect network layer module to ip and arp modules
00176     cModule* ipModule = router.module->getSubmodule("networkLayer")->
00177         getSubmodule("ip");
00178 
00179     cGate* ipIn = firstUnusedGate(ipModule, "queueIn");
00180     netwInGate->connectTo(ipIn);
00181 
00182 #ifdef _ORIG_INET
00183     cModule* arpModule = router.module->getSubmodule("networkLayer")->getSubmodule("arp"); //comment out for speed-hack
00184 
00185     cGate* arpOut = firstUnusedGate(arpModule, "nicOut"); //comment out for speed-hack
00186 
00187     //cGate* ipOut = firstUnusedGate(ipModule, "queueOut"); //comment out for speed-hack
00188     cGate* ipOut = ipModule->gate("queueOut");
00189 
00190     arpOut->connectTo(netwOutGate);     //comment out for speed-hack
00191 #endif
00192 
00193     //
00194     // Start ppp interface modules
00195     //
00196     terminal.remotePPPInterface->finalizeParameters();
00197     terminal.remotePPPInterface->setDisplayString("i=block/ifcard");
00198     terminal.remotePPPInterface->buildInside();
00199     terminal.remotePPPInterface->scheduleStart(simTime());
00200     terminal.remotePPPInterface->callInitialize();
00201 
00202     if ( !migrate) {
00203         // we are already in stage 4 and need to call initialize
00204         // for all previous stages manually
00205         for (int i=0; i < MAX_STAGE_UNDERLAY + 1; i++) {
00206             terminal.module->callInitialize(i);
00207         }
00208     }
00209 
00210     terminal.remoteInterfaceEntry = router.interfaceTable->getInterface(
00211         router.interfaceTable->getNumInterfaces() - 1);
00212     terminal.interfaceEntry = terminal.interfaceTable->getInterfaceByName("ppp0");
00213 
00214 
00215     //
00216     // Fill in interface table.
00217     //
00218 
00219     // router
00220     IPv4InterfaceData* interfaceData = new IPv4InterfaceData;
00221     interfaceData->setIPAddress(router.IPAddress);
00222     interfaceData->setNetmask(IPAddress::ALLONES_ADDRESS);
00223     terminal.remoteInterfaceEntry->setIPv4Data(interfaceData);
00224 
00225     // terminal
00226     terminal.interfaceEntry->ipv4Data()->setIPAddress(IPAddress(terminal.IPAddress));
00227     terminal.interfaceEntry->ipv4Data()->setNetmask(IPAddress::ALLONES_ADDRESS);
00228 
00229     //
00230     // Fill in routing table.
00231     //
00232 
00233     // router
00234     IPRoute* re = new IPRoute();
00235     re->setHost(IPAddress(terminal.IPAddress));
00236     re->setNetmask(IPAddress(IPAddress::ALLONES_ADDRESS));
00237     re->setInterface(terminal.remoteInterfaceEntry);
00238     re->setType(IPRoute::DIRECT);
00239     re->setSource(IPRoute::MANUAL);
00240     router.routingTable->addRoute(re);
00241     terminal.remoteRoutingEntry = re;
00242 
00243     // terminal
00244     IPRoute* te = new IPRoute();
00245     te->setHost(IPAddress::UNSPECIFIED_ADDRESS);
00246     te->setNetmask(IPAddress::UNSPECIFIED_ADDRESS);
00247     te->setGateway(router.IPAddress);
00248     te->setInterface(terminal.interfaceEntry);
00249     te->setType(IPRoute::REMOTE);
00250     te->setSource(IPRoute::MANUAL);
00251     terminal.routingTable->addRoute(te);
00252     terminal.routingEntry = te;
00253 
00254 
00255     // append module to overlay terminal vector
00256     overlayTerminal.push_back(terminal);
00257     int ID = terminal.module->getId();
00258 
00259     updateDisplayString();
00260 
00261     return ID;
00262 }
00263 
00264 int AccessNet::getRandomNodeId()
00265 {
00266     Enter_Method("getRandomNodeId()");
00267 
00268     return overlayTerminal[intuniform(0, overlayTerminal.size() - 1)].module->getId();
00269 }
00270 
00271 cModule* AccessNet::removeOverlayNode(int ID)
00272 {
00273     Enter_Method("removeOverlayNode()");
00274 
00275     cModule* node = NULL;
00276     TerminalInfo terminal;
00277     int index;
00278 
00279     for(unsigned int i=0; i<overlayTerminal.size(); i++) {
00280         if(overlayTerminal[i].module->getId() == ID) {
00281             terminal = overlayTerminal[i];
00282             node = terminal.module;
00283             index = i;
00284         }
00285     }
00286 
00287     if(node == NULL) return NULL;
00288 
00289     cModule* ppp = terminal.remotePPPInterface;
00290 
00291     // disconnect terminal
00292     node->gate("pppg$o", 0)->disconnect();
00293     node->gate("pppg$i", 0)->getPreviousGate()->disconnect();
00294 
00295     // disconnect ip and arp modules
00296     ppp->gate("netwIn")->getPathStartGate()->disconnect();
00297     ppp->gate("netwOut")->getNextGate()->disconnect();
00298 
00299     // remove associated ppp interface module
00300     ppp->callFinish();
00301     ppp->deleteModule();
00302 
00303     // remove associated interface table entry
00304     router.interfaceTable->deleteInterface(terminal.remoteInterfaceEntry);
00305 
00306     // remove routing entries
00307     terminal.routingTable->deleteRoute(terminal.routingEntry);
00308     router.routingTable->deleteRoute(terminal.remoteRoutingEntry);
00309 
00310     // statistics
00311     lifetimeVector.record(simTime() - overlayTerminal[index].createdAt);
00312 
00313     // remove terminal from overlay terminal vector
00314     overlayTerminal.erase(overlayTerminal.begin() + index);
00315 
00316     updateDisplayString();
00317 
00318     return node;
00319 }
00320 
00321 cModule* AccessNet::getOverlayNode(int ID)
00322 {
00323     Enter_Method("getOverlayNode()");
00324 
00325     cModule* node = NULL;
00326 
00327     for(unsigned int i=0; i<overlayTerminal.size(); i++) {
00328         if(overlayTerminal[i].module->getId() == ID)
00329             node = overlayTerminal[i].module;
00330     }
00331     return node;
00332 }
00333 
00334 void AccessNet::updateDisplayString()
00335 {
00336     if (ev.isGUI()) {
00337         char buf[80];
00338         if ( overlayTerminal.size() == 1 ) {
00339             sprintf(buf, "1 terminal connected");
00340         } else {
00341             sprintf(buf, "%i terminals connected", overlayTerminal.size());
00342         }
00343         getDisplayString().setTagArg("t", 0, buf);
00344         getDisplayString().setTagArg("t", 2, "blue");
00345     }
00346 }
00347 
00348 cGate* firstUnusedGate(cModule* owner, const char* name, cGate::Type type)
00349 {
00350     int index;
00351     for (index = 0; index < owner->gateSize(name); index++) {
00352         cGate *gate = type == cGate::NONE ? owner->gate(name, index) : owner->gateHalf(name, type, index);
00353         if (!gate->isConnectedOutside()) {
00354             return gate;
00355         }
00356     }
00357 
00358     owner->setGateSize(name, index + 2);
00359     return type == cGate::NONE ? owner->gate(name, index + 1) : owner->gateHalf(name, type, index + 1);
00360 }
00361 

Generated on Wed Mar 25 16:32:47 2009 for OverSim by  doxygen 1.5.5