NETWORK ATTACKS FRAMEWORK  1.0.0
A NETwork Attacks framework. Making network attacks impact evaluation easier!
ManetRoutingBase.cc
Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2008 by Alfonso Ariza                                   *
00003  *   aarizaq@uma.es                                                        *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00022 
00023 #include "ManetRoutingBase.h"
00024 #include "UDPPacket.h"
00025 #include "IPv4Datagram.h"
00026 #include "IPv4ControlInfo.h"
00027 #include "IPv4InterfaceData.h"
00028 #include "IPv6ControlInfo.h"
00029 #include "Ieee802Ctrl_m.h"
00030 #include "RoutingTableAccess.h"
00031 #include "InterfaceTableAccess.h"
00032 #include "Coord.h"
00033 #include "ControlInfoBreakLink_m.h"
00034 #include "Ieee80211Frame_m.h"
00035 #include "ICMPAccess.h"
00036 #include "IMobility.h"
00037 #include "Ieee80211MgmtAP.h"
00038 
00039 #define IP_DEF_TTL 32
00040 #define UDP_HDR_LEN 8
00041 
00042 simsignal_t ManetRoutingBase::mobilityStateChangedSignal = SIMSIGNAL_NULL;
00043 ManetRoutingBase::GlobalRouteMap *ManetRoutingBase::globalRouteMap = NULL;
00044 bool ManetRoutingBase::createInternalStore = false;
00045 
00046 
00047 ManetRoutingBase::ManetRoutingBase()
00048 {
00049 #ifdef WITH_80211MESH
00050     locator = NULL;
00051 #endif
00052     isRegistered = false;
00053     regPosition = false;
00054     mac_layer_ = false;
00055     commonPtr = NULL;
00056     routesVector = NULL;
00057     interfaceVector = new InterfaceVector;
00058     staticNode = false;
00059     colaborativeProtocol = NULL;
00060     arp = NULL;
00061     isGateway = false;
00062     proxyAddress.clear();
00063     addressGroupVector.clear();
00064     inAddressGroup.clear();
00065 
00066 }
00067 
00068 
00069 bool ManetRoutingBase::isThisInterfaceRegistered(InterfaceEntry *ie)
00070 {
00071     if (!isRegistered)
00072         opp_error("Manet routing protocol is not register");
00073     for (unsigned int i=0; i<interfaceVector->size(); i++)
00074     {
00075         if ((*interfaceVector)[i].interfacePtr==ie)
00076             return true;
00077     }
00078     return false;
00079 }
00080 
00081 void ManetRoutingBase::registerRoutingModule()
00082 {
00083     InterfaceEntry *ie;
00084     const char *name;
00085     /* Set host parameters */
00086     isRegistered = true;
00087     int  num_80211 = 0;
00088     inet_rt = RoutingTableAccess().getIfExists();
00089     inet_ift = InterfaceTableAccess().get();
00090     nb = NotificationBoardAccess().get();
00091 
00092     if (routesVector)
00093         routesVector->clear();
00094 
00095     if (par("useICMP"))
00096     {
00097         icmpModule = ICMPAccess().getIfExists();
00098     }
00099     sendToICMP = false;
00100 
00101     cProperties *props = getParentModule()->getProperties();
00102     mac_layer_ = props && props->getAsBool("macRouting");
00103     usetManetLabelRouting = par("usetManetLabelRouting");
00104 
00105     const char *interfaces = par("interfaces");
00106     cStringTokenizer tokenizerInterfaces(interfaces);
00107     const char *token;
00108     const char *prefixName;
00109     if (!mac_layer_)
00110     {
00111         while ((token = tokenizerInterfaces.nextToken()) != NULL)
00112         {
00113             if ((prefixName = strstr(token, "prefix")) != NULL)
00114             {
00115                 const char *leftparenp = strchr(prefixName, '(');
00116                 const char *rightparenp = strchr(prefixName, ')');
00117                 std::string interfacePrefix;
00118                 interfacePrefix.assign(leftparenp + 1, rightparenp - leftparenp - 1);
00119                 for (int i = 0; i < inet_ift->getNumInterfaces(); i++)
00120                 {
00121                     ie = inet_ift->getInterface(i);
00122                     name = ie->getName();
00123                     if ((strstr(name, interfacePrefix.c_str()) != NULL) && !isThisInterfaceRegistered(ie))
00124                     {
00125                         InterfaceIdentification interface;
00126                         interface.interfacePtr = ie;
00127                         interface.index = i;
00128                         num_80211++;
00129                         interfaceVector->push_back(interface);
00130                     }
00131                 }
00132             }
00133             else
00134             {
00135                 for (int i = 0; i < inet_ift->getNumInterfaces(); i++)
00136                 {
00137                     ie = inet_ift->getInterface(i);
00138                     name = ie->getName();
00139                     if (strcmp(name, token) == 0 && !isThisInterfaceRegistered(ie))
00140                     {
00141                         InterfaceIdentification interface;
00142                         interface.interfacePtr = ie;
00143                         interface.index = i;
00144                         num_80211++;
00145                         interfaceVector->push_back(interface);
00146                     }
00147                 }
00148             }
00149         }
00150     }
00151     else
00152     {
00153         cModule *mod = getParentModule()->getParentModule();
00154         char *interfaceName = new char[strlen(mod->getFullName()) + 1];
00155         char *d = interfaceName;
00156         for (const char *s = mod->getFullName(); *s; s++)
00157             if (isalnum(*s))
00158                 *d++ = *s;
00159         *d = '\0';
00160 
00161         for (int i = 0; i < inet_ift->getNumInterfaces(); i++)
00162         {
00163             ie = inet_ift->getInterface(i);
00164             name = ie->getName();
00165             if (strcmp(name, interfaceName) == 0 && !isThisInterfaceRegistered(ie))
00166             {
00167                 InterfaceIdentification interface;
00168                 interface.interfacePtr = ie;
00169                 interface.index = i;
00170                 num_80211++;
00171                 interfaceVector->push_back(interface);
00172             }
00173         }
00174     }
00175     const char *exclInterfaces = par("excludedInterfaces");
00176     cStringTokenizer tokenizerExcluded(exclInterfaces);
00177     if (tokenizerExcluded.hasMoreTokens())
00178     {
00179         while ((token = tokenizerExcluded.nextToken())!=NULL)
00180         {
00181             for (unsigned int i = 0; i<interfaceVector->size(); i++)
00182             {
00183                 name = (*interfaceVector)[i].interfacePtr->getName();
00184                 if (strcmp(token, name)==0)
00185                 {
00186                     interfaceVector->erase(interfaceVector->begin()+i);
00187                     break;
00188                 }
00189             }
00190         }
00191     }
00192 
00193     if (inet_rt)
00194         routerId = ManetAddress(inet_rt->getRouterId());
00195 
00196     if (interfaceVector->size()==0)
00197         opp_error("Manet routing protocol has found no interfaces that can be used for routing.");
00198     if (mac_layer_)
00199         hostAddress = ManetAddress(interfaceVector->front().interfacePtr->getMacAddress());
00200     else
00201         hostAddress = ManetAddress(interfaceVector->front().interfacePtr->ipv4Data()->getIPAddress());
00202     // One enabled network interface (in total)
00203     // clear routing entries related to wlan interfaces and autoassign ip adresses
00204     bool manetPurgeRoutingTables = (bool) par("manetPurgeRoutingTables");
00205     if (manetPurgeRoutingTables && !mac_layer_)
00206     {
00207         IPv4Route *entry;
00208         // clean the route table wlan interface entry
00209         for (int i=inet_rt->getNumRoutes()-1; i>=0; i--)
00210         {
00211             entry = inet_rt->getRoute(i);
00212             const InterfaceEntry *ie = entry->getInterface();
00213             if (strstr(ie->getName(), "wlan")!=NULL)
00214             {
00215                 inet_rt->deleteRoute(entry);
00216             }
00217         }
00218     }
00219     if (par("autoassignAddress") && !mac_layer_)
00220     {
00221         IPv4Address AUTOASSIGN_ADDRESS_BASE(par("autoassignAddressBase").stringValue());
00222         if (AUTOASSIGN_ADDRESS_BASE.getInt() == 0)
00223             opp_error("Auto assignment need autoassignAddressBase to be set");
00224         IPv4Address myAddr(AUTOASSIGN_ADDRESS_BASE.getInt() + uint32(getParentModule()->getId()));
00225         for (int k=0; k<inet_ift->getNumInterfaces(); k++)
00226         {
00227             InterfaceEntry *ie = inet_ift->getInterface(k);
00228             if (strstr(ie->getName(), "wlan")!=NULL)
00229             {
00230                 ie->ipv4Data()->setIPAddress(myAddr);
00231                 ie->ipv4Data()->setNetmask(IPv4Address::ALLONES_ADDRESS); // full address must match for local delivery
00232             }
00233         }
00234     }
00235     // register LL-MANET-Routers
00236     if (!mac_layer_)
00237     {
00238         for (unsigned int i = 0; i<interfaceVector->size(); i++)
00239         {
00240             (*interfaceVector)[i].interfacePtr->ipv4Data()->joinMulticastGroup(IPv4Address::LL_MANET_ROUTERS);
00241         }
00242         arp = ArpAccess().get();
00243     }
00244     nb->subscribe(this,NF_L2_AP_DISASSOCIATED);
00245     nb->subscribe(this,NF_L2_AP_ASSOCIATED);
00246 
00247 #ifdef WITH_80211MESH
00248     locator = LocatorModuleAccess().getIfExists();
00249     if (locator)
00250     {
00251         InterfaceEntry *ie = getInterfaceWlanByAddress();
00252         if (locator->getIpAddress().isUnspecified() && ie->ipv4Data())
00253             locator->setIpAddress(ie->ipv4Data()->getIPAddress());
00254         if (locator->getMacAddress().isUnspecified())
00255             locator->setMacAddress(ie->getMacAddress());
00256         nb->subscribe(this,NF_LOCATOR_ASSOC);
00257         nb->subscribe(this,NF_LOCATOR_DISASSOC);
00258     }
00259 #endif
00260 
00261     if (par("PublicRoutingTables").boolValue())
00262     {
00263         setInternalStore(true);
00264         if (globalRouteMap == NULL)
00265         {
00266             globalRouteMap = new GlobalRouteMap;
00267         }
00268 
00269         GlobalRouteMap::iterator it = globalRouteMap->find(getAddress());
00270         if (it == globalRouteMap->end())
00271         {
00272             ProtocolRoutingData data;
00273             ProtocolsRoutes vect;
00274             data.isProactive = isProactive();
00275             data.routesVector = routesVector;
00276             vect.push_back(data);
00277             globalRouteMap->insert(std::make_pair<ManetAddress,ProtocolsRoutes>(getAddress(),vect));
00278         }
00279         else
00280         {
00281             ProtocolRoutingData data;
00282             data.isProactive = isProactive();
00283             data.routesVector = routesVector;
00284             it->second.push_back(data);
00285         }
00286     }
00287 
00288 
00289  //   WATCH_MAP(*routesVector);
00290 }
00291 
00292 ManetRoutingBase::~ManetRoutingBase()
00293 {
00294     delete interfaceVector;
00295     if (routesVector)
00296     {
00297         delete routesVector;
00298         routesVector = NULL;
00299     }
00300     proxyAddress.clear();
00301     addressGroupVector.clear();
00302     inAddressGroup.clear();
00303 
00304     if (globalRouteMap)
00305     {
00306         GlobalRouteMap::iterator it = globalRouteMap->find(getAddress());
00307         if (it != globalRouteMap->end())
00308             globalRouteMap->erase(it);
00309         if (globalRouteMap->empty())
00310         {
00311             delete globalRouteMap;
00312             globalRouteMap = NULL;
00313         }
00314     }
00315 }
00316 
00317 bool ManetRoutingBase::isLocalAddress(const ManetAddress& dest) const
00318 {
00319     if (!isRegistered)
00320         opp_error("Manet routing protocol is not register");
00321     //if (!mac_layer_)
00322     if (dest.getType() == ManetAddress::IPv4_ADDRESS)
00323         return inet_rt->isLocalAddress(dest.getIPv4());
00324     InterfaceEntry *ie;
00325     for (int i = 0; i < inet_ift->getNumInterfaces(); i++)
00326     {
00327         ie = inet_ift->getInterface(i);
00328         ManetAddress add(ie->getMacAddress());
00329         if (add==dest) return true;
00330     }
00331     return false;
00332 }
00333 
00334 bool ManetRoutingBase::isMulticastAddress(const ManetAddress& dest) const
00335 {
00336     if (!isRegistered)
00337         opp_error("Manet routing protocol is not register");
00338     //if (mac_layer_)
00339     if (dest.getType() == ManetAddress::MAC_ADDRESS)
00340         return dest.getMAC() == MACAddress::BROADCAST_ADDRESS;
00341     else
00342         return dest.getIPv4() == IPv4Address::ALLONES_ADDRESS;
00343 }
00344 
00345 void ManetRoutingBase::linkLayerFeeback()
00346 {
00347     if (!isRegistered)
00348         opp_error("Manet routing protocol is not register");
00349     nb->subscribe(this, NF_LINK_BREAK);
00350 }
00351 
00352 void ManetRoutingBase::linkPromiscuous()
00353 {
00354     if (!isRegistered)
00355         opp_error("Manet routing protocol is not register");
00356     nb->subscribe(this, NF_LINK_PROMISCUOUS);
00357 }
00358 
00359 void ManetRoutingBase::linkFullPromiscuous()
00360 {
00361     if (!isRegistered)
00362         opp_error("Manet routing protocol is not register");
00363     nb->subscribe(this, NF_LINK_FULL_PROMISCUOUS);
00364 }
00365 
00366 void ManetRoutingBase::registerPosition()
00367 {
00368     if (!isRegistered)
00369         opp_error("Manet routing protocol is not register");
00370     regPosition = true;
00371     mobilityStateChangedSignal = registerSignal("mobilityStateChanged");
00372     cModule *mod;
00373     for (mod = getParentModule(); mod != 0; mod = mod->getParentModule()) {
00374             cProperties *properties = mod->getProperties();
00375             if (properties && properties->getAsBool("node"))
00376                 break;
00377     }
00378     if (mod)
00379         mod->subscribe(mobilityStateChangedSignal, this);
00380     else
00381         getParentModule()->subscribe(mobilityStateChangedSignal, this);
00382 }
00383 
00384 void ManetRoutingBase::processLinkBreak(const cObject *details) {return;}
00385 void ManetRoutingBase::processLinkBreakManagement(const cObject *details) {return;}
00386 void ManetRoutingBase::processPromiscuous(const cObject *details) {return;}
00387 void ManetRoutingBase::processFullPromiscuous(const cObject *details) {return;}
00388 void ManetRoutingBase::processLocatorAssoc(const cObject *details) {return;}
00389 void ManetRoutingBase::processLocatorDisAssoc(const cObject *details) {return;}
00390 
00391 
00392 void ManetRoutingBase::sendToIpOnIface(cPacket *msg, int srcPort, const ManetAddress& destAddr, int destPort, int ttl, double delay, InterfaceEntry  *ie)
00393 {
00394     if (!isRegistered)
00395         opp_error("Manet routing protocol is not register");
00396 
00397     if (destAddr.getType() == ManetAddress::MAC_ADDRESS)
00398     {
00399         Ieee802Ctrl *ctrl = new Ieee802Ctrl;
00400         //TODO ctrl->setEtherType(...);
00401         MACAddress macadd = destAddr.getMAC();
00402         ctrl->setDest(macadd);
00403 
00404         if (ie == NULL)
00405             ie = interfaceVector->back().interfacePtr;
00406 
00407         if (macadd == MACAddress::BROADCAST_ADDRESS)
00408         {
00409             for (unsigned int i = 0; i<interfaceVector->size()-1; i++)
00410             {
00411 // It's necessary to duplicate the the control info message and include the information relative to the interface
00412                 Ieee802Ctrl *ctrlAux = ctrl->dup();
00413                 ie = (*interfaceVector)[i].interfacePtr;
00414                 cPacket *msgAux = msg->dup();
00415 // Set the control info to the duplicate packet
00416                 if (ie)
00417                     ctrlAux->setInputPort(ie->getInterfaceId());
00418                 msgAux->setControlInfo(ctrlAux);
00419                 sendDelayed(msgAux, delay, "to_ip");
00420 
00421             }
00422             ie = interfaceVector->back().interfacePtr;
00423         }
00424 
00425         if (ie)
00426             ctrl->setInputPort(ie->getInterfaceId());
00427         msg->setControlInfo(ctrl);
00428         sendDelayed(msg, delay, "to_ip");
00429         return;
00430     }
00431 
00432     UDPPacket *udpPacket = new UDPPacket(msg->getName());
00433     udpPacket->setByteLength(UDP_HDR_LEN);
00434     udpPacket->encapsulate(msg);
00435     //IPvXAddress srcAddr = interfaceWlanptr->ipv4Data()->getIPAddress();
00436 
00437     if (ttl==0)
00438     {
00439         // delete and return
00440         delete msg;
00441         return;
00442     }
00443     // set source and destination port
00444     udpPacket->setSourcePort(srcPort);
00445     udpPacket->setDestinationPort(destPort);
00446 
00447     if (destAddr.getType() == ManetAddress::IPv4_ADDRESS)
00448     {
00449         // send to IPv4
00450         IPv4Address add(destAddr.getIPv4());
00451         IPv4Address  srcadd;
00452 
00453 // If found interface We use the address of interface
00454         if (ie)
00455             srcadd = ie->ipv4Data()->getIPAddress();
00456         else
00457             srcadd = hostAddress.getIPv4();
00458 
00459         EV << "Sending app packet " << msg->getName() << " over IPv4." << " from " <<
00460         srcadd.str() << " to " << add.str() << "\n";
00461         IPv4ControlInfo *ipControlInfo = new IPv4ControlInfo();
00462         ipControlInfo->setDestAddr(add);
00463         //ipControlInfo->setProtocol(IP_PROT_UDP);
00464         ipControlInfo->setProtocol(IP_PROT_MANET);
00465 
00466         ipControlInfo->setTimeToLive(ttl);
00467         udpPacket->setControlInfo(ipControlInfo);
00468 
00469         if (ie!=NULL)
00470             ipControlInfo->setInterfaceId(ie->getInterfaceId());
00471 
00472         if ((add == IPv4Address::ALLONES_ADDRESS || add == IPv4Address::LL_MANET_ROUTERS) && ie == NULL)
00473         {
00474 // In this case we send a broadcast packet per interface
00475             for (unsigned int i = 0; i<interfaceVector->size()-1; i++)
00476             {
00477                 ie = (*interfaceVector)[i].interfacePtr;
00478                 srcadd = ie->ipv4Data()->getIPAddress();
00479 // It's necessary to duplicate the the control info message and include the information relative to the interface
00480                 IPv4ControlInfo *ipControlInfoAux = new IPv4ControlInfo(*ipControlInfo);
00481                 if (ipControlInfoAux->getOrigDatagram())
00482                     delete ipControlInfoAux->removeOrigDatagram();
00483                 ipControlInfoAux->setInterfaceId(ie->getInterfaceId());
00484                 ipControlInfoAux->setSrcAddr(srcadd);
00485                 UDPPacket *udpPacketAux = udpPacket->dup();
00486 // Set the control info to the duplicate udp packet
00487                 udpPacketAux->setControlInfo(ipControlInfoAux);
00488                 sendDelayed(udpPacketAux, delay, "to_ip");
00489             }
00490             ie = interfaceVector->back().interfacePtr;
00491             srcadd = ie->ipv4Data()->getIPAddress();
00492             ipControlInfo->setInterfaceId(ie->getInterfaceId());
00493         }
00494         ipControlInfo->setSrcAddr(srcadd);
00495         sendDelayed(udpPacket, delay, "to_ip");
00496     }
00497     else if (destAddr.getType() == ManetAddress::IPv6_ADDRESS)
00498     {
00499         // send to IPv6
00500         EV << "Sending app packet " << msg->getName() << " over IPv6.\n";
00501         IPv6ControlInfo *ipControlInfo = new IPv6ControlInfo();
00502         // ipControlInfo->setProtocol(IP_PROT_UDP);
00503         ipControlInfo->setProtocol(IP_PROT_MANET);
00504         ipControlInfo->setSrcAddr(hostAddress.getIPv6());
00505         ipControlInfo->setDestAddr(destAddr.getIPv6());
00506         ipControlInfo->setHopLimit(ttl);
00507         // ipControlInfo->setInterfaceId(udpCtrl->InterfaceId()); FIXME extend IPv6 with this!!!
00508         udpPacket->setControlInfo(ipControlInfo);
00509         sendDelayed(udpPacket, delay, "to_ip");
00510     }
00511     else
00512     {
00513         throw cRuntimeError("Unaccepted ManetAddress type: %d", destAddr.getType());
00514     }
00515     // totalSend++;
00516 }
00517 
00518 void ManetRoutingBase::sendToIp(cPacket *msg, int srcPort, const ManetAddress& destAddr, int destPort, int ttl, double delay, const ManetAddress &iface)
00519 {
00520     if (!isRegistered)
00521         opp_error("Manet routing protocol is not register");
00522 
00523     InterfaceEntry  *ie = NULL;
00524     if (!iface.isUnspecified())
00525         ie = getInterfaceWlanByAddress(iface); // The user want to use a pre-defined interface
00526 
00527     sendToIpOnIface(msg, srcPort, destAddr, destPort, ttl, delay, ie);
00528 }
00529 
00530 void ManetRoutingBase::sendToIp(cPacket *msg, int srcPort, const ManetAddress& destAddr, int destPort, int ttl, double delay, int index)
00531 {
00532     if (!isRegistered)
00533         opp_error("Manet routing protocol is not register");
00534 
00535     InterfaceEntry  *ie = NULL;
00536     if (index!=-1)
00537         ie = getInterfaceEntry(index); // The user want to use a pre-defined interface
00538 
00539     sendToIpOnIface(msg, srcPort, destAddr, destPort, ttl, delay, ie);
00540 }
00541 
00542 
00543 void ManetRoutingBase::omnet_chg_rte(const struct in_addr &dst, const struct in_addr &gtwy, const struct in_addr &netm,
00544                                       short int hops, bool del_entry)
00545 {
00546     omnet_chg_rte(dst.s_addr, gtwy.s_addr, netm.s_addr, hops, del_entry);
00547 }
00548 
00549 void ManetRoutingBase::omnet_chg_rte(const struct in_addr &dst, const struct in_addr &gtwy, const struct in_addr &netm,
00550                                       short int hops, bool del_entry, const struct in_addr &iface)
00551 {
00552     omnet_chg_rte(dst.s_addr, gtwy.s_addr, netm.s_addr, hops, del_entry, iface.s_addr);
00553 }
00554 
00555 bool ManetRoutingBase::omnet_exist_rte(struct in_addr dst)
00556 {
00557     ManetAddress add = omnet_exist_rte(dst.s_addr);
00558     if (add.isUnspecified()) return false;
00559     else if (add.getIPv4() == IPv4Address::ALLONES_ADDRESS) return false;
00560     else return true;
00561 }
00562 
00563 void ManetRoutingBase::omnet_chg_rte(const ManetAddress &dst, const ManetAddress &gtwy, const ManetAddress &netm, short int hops, bool del_entry, const ManetAddress &iface)
00564 {
00565     if (!isRegistered)
00566         opp_error("Manet routing protocol is not register");
00567 
00568     /* Add route to kernel routing table ... */
00569     IPv4Address desAddress(dst.getIPv4());
00570     if (!createInternalStore && routesVector)
00571     {
00572         delete routesVector;
00573         routesVector = NULL;
00574     }
00575     else if (createInternalStore && routesVector)
00576     {
00577         RouteMap::iterator it = routesVector->find(dst);
00578         if (it != routesVector->end())
00579             routesVector->erase(it);
00580         if (!del_entry)
00581         {
00582             /*
00583             ManetAddress dest=dst;
00584             ManetAddress next=gtwy;
00585             if (mac_layer_)
00586             {
00587                 dest.setAddresType(ManetAddress::MAC);
00588                 next.setAddresType(ManetAddress::MAC);
00589             }
00590             else
00591             {
00592                 dest.setAddresType(ManetAddress::IPV4);
00593                 next.setAddresType(ManetAddress::IPV4);
00594             }*/
00595             routesVector->insert(std::make_pair<ManetAddress,ManetAddress>(dst, gtwy));
00596         }
00597     }
00598 
00599     if (mac_layer_)
00600         return;
00601 
00602     bool found = false;
00603     IPv4Route *oldentry = NULL;
00604     for (int i=inet_rt->getNumRoutes(); i>0; --i)
00605     {
00606         IPv4Route *e = inet_rt->getRoute(i-1);
00607         if (desAddress == e->getDestination())
00608         {
00609             if (del_entry && !found)
00610             {
00611                 if (!inet_rt->deleteRoute(e))
00612                     opp_error("Aodv omnet_chg_rte can't delete route entry");
00613             }
00614             else
00615             {
00616                 found = true;
00617                 oldentry = e;
00618             }
00619         }
00620     }
00621 
00622 #ifdef WITH_80211MESH
00623     if (locator && locator->isApIp(desAddress) && del_entry)
00624     {
00625         std::vector<IPv4Address> list;
00626         locator->getApListIp(desAddress,list);
00627         for (unsigned int i = 0; i<list.size(); i++)
00628         {
00629             for (int i=inet_rt->getNumRoutes(); i>0; --i)
00630             {
00631                 IPv4Route *e = inet_rt->getRoute(i-1);
00632                 if (list[i] == e->getDestination())
00633                 {
00634                     if (!inet_rt->deleteRoute(e))
00635                         opp_error("Aodv omnet_chg_rte can't delete route entry");
00636                     else
00637                         break;
00638                 }
00639             }
00640         }
00641     }
00642 #endif
00643     if (del_entry)
00644         return;
00645 
00646     IPv4Address netmask(netm.getIPv4());
00647     IPv4Address gateway(gtwy.getIPv4());
00648 
00649     // The default mask is for manet routing is  IPv4Address::ALLONES_ADDRESS
00650     if (netm.isUnspecified())
00651         netmask = IPv4Address::ALLONES_ADDRESS;
00652 
00653     InterfaceEntry *ie = getInterfaceWlanByAddress(iface);
00654     IPv4Route::RouteSource routeSource = usetManetLabelRouting ? IPv4Route::MANET : IPv4Route::MANET2;
00655 
00656     if (found)
00657     {
00658         if (oldentry->getDestination() == desAddress
00659                 && oldentry->getNetmask() == netmask
00660                 && oldentry->getGateway() == gateway
00661                 && oldentry->getMetric() == hops
00662                 && oldentry->getInterface() == ie
00663                 && oldentry->getSource() == routeSource)
00664             return;
00665         inet_rt->deleteRoute(oldentry);
00666     }
00667 
00668     IPv4Route *entry = new IPv4Route();
00669 
00671     entry->setDestination(desAddress);
00673     entry->setNetmask(netmask);
00675     entry->setGateway(gateway);
00677     entry->setMetric(hops);
00679 
00680     entry->setInterface(ie);
00681 
00684     entry->setSource(routeSource);
00685     inet_rt->addRoute(entry);
00686 #ifdef WITH_80211MESH
00687     if (locator && locator->isApIp(desAddress))
00688     {
00689         std::vector<IPv4Address> list;
00690         locator->getApListIp(desAddress,list);
00691         for (unsigned int i = 0; i<list.size(); i++)
00692         {
00693             IPv4Route *e = inet_rt->findBestMatchingRoute(list[i]);
00694             if (e && e->getDestination() == list[i])
00695             {
00696                 if (e->getGateway() == gateway && e->getMetric() == hops && e->getInterface() == ie)
00697                     continue;
00698             }
00699 
00700             IPv4Route *entry = new IPv4Route();
00701 
00703             entry->setDestination(list[i]);
00705             entry->setNetmask(netmask);
00707             entry->setGateway(gateway);
00709             entry->setMetric(hops);
00711 
00712             entry->setInterface(ie);
00713 
00715             entry->setType(routeType);
00718             entry->setSource(routeSource);
00719             inet_rt->addRoute(entry);
00720         }
00721     }
00722 #endif
00723 }
00724 
00725 // This methods use the nic index to identify the output nic.
00726 void ManetRoutingBase::omnet_chg_rte(const struct in_addr &dst, const struct in_addr &gtwy, const struct in_addr &netm,
00727                                       short int hops, bool del_entry, int index)
00728 {
00729     omnet_chg_rte(dst.s_addr, gtwy.s_addr, netm.s_addr, hops, del_entry, index);
00730 }
00731 
00732 
00733 void ManetRoutingBase::omnet_chg_rte(const ManetAddress &dst, const ManetAddress &gtwy, const ManetAddress &netm, short int hops, bool del_entry, int index)
00734 {
00735     if (!isRegistered)
00736         opp_error("Manet routing protocol is not register");
00737 
00738     /* Add route to kernel routing table ... */
00739     IPv4Address desAddress(dst.getIPv4());
00740     if (!createInternalStore && routesVector)
00741     {
00742          delete routesVector;
00743          routesVector = NULL;
00744     }
00745     else if (createInternalStore && routesVector)
00746     {
00747          RouteMap::iterator it = routesVector->find(dst);
00748          if (it != routesVector->end())
00749              routesVector->erase(it);
00750          if (!del_entry)
00751          {
00752              routesVector->insert(std::make_pair<ManetAddress,ManetAddress>(dst, gtwy));
00753          }
00754     }
00755     if (mac_layer_)
00756         return;
00757     bool found = false;
00758     IPv4Route *oldentry = NULL;
00759     for (int i=inet_rt->getNumRoutes(); i>0; --i)
00760     {
00761         IPv4Route *e = inet_rt->getRoute(i-1);
00762         if (desAddress == e->getDestination())
00763         {
00764             if (del_entry && !found)
00765             {
00766                 if (!inet_rt->deleteRoute(e))
00767                     opp_error("Aodv omnet_chg_rte can't delete route entry");
00768             }
00769             else
00770             {
00771                 found = true;
00772                 oldentry = e;
00773             }
00774         }
00775     }
00776 
00777 #ifdef WITH_80211MESH
00778     if (locator && locator->isApIp(desAddress) && del_entry)
00779     {
00780         std::vector<IPv4Address> list;
00781         locator->getApListIp(desAddress, list);
00782         for (unsigned int i = 0; i < list.size(); i++)
00783         {
00784             for (int i = inet_rt->getNumRoutes(); i > 0; --i)
00785             {
00786                 IPv4Route *e = inet_rt->getRoute(i - 1);
00787                 if (list[i] == e->getDestination())
00788                 {
00789                     if (!inet_rt->deleteRoute(e))
00790                         opp_error("Aodv omnet_chg_rte can't delete route entry");
00791                     else
00792                         break;
00793                 }
00794             }
00795         }
00796     }
00797 #endif
00798     if (del_entry)
00799         return;
00800 
00801     IPv4Address netmask(netm.getIPv4());
00802     IPv4Address gateway(gtwy.getIPv4());
00803     if (netm.isUnspecified())
00804         netmask = IPv4Address::ALLONES_ADDRESS;
00805 
00806     InterfaceEntry *ie = getInterfaceEntry(index);
00807     IPv4Route::RouteSource routeSource = usetManetLabelRouting ? IPv4Route::MANET : IPv4Route::MANET2;
00808 
00809     if (found)
00810     {
00811         if (oldentry->getDestination() == desAddress
00812                 && oldentry->getNetmask() == netmask
00813                 && oldentry->getGateway() == gateway
00814                 && oldentry->getMetric() == hops
00815                 && oldentry->getInterface() == ie
00816                 && oldentry->getSource() == routeSource)
00817             return;
00818         inet_rt->deleteRoute(oldentry);
00819     }
00820 
00821     IPv4Route *entry = new IPv4Route();
00822 
00824     entry->setDestination(desAddress);
00826     entry->setNetmask(netmask);
00828     entry->setGateway(gateway);
00830     entry->setMetric(hops);
00832 
00833     entry->setInterface(getInterfaceEntry(index));
00834 
00837 
00838     if (usetManetLabelRouting)
00839         entry->setSource(IPv4Route::MANET);
00840     else
00841         entry->setSource(IPv4Route::MANET2);
00842 
00843         inet_rt->addRoute(entry);
00844 
00845 #ifdef WITH_80211MESH
00846     if (locator && locator->isApIp(desAddress))
00847     {
00848         std::vector<IPv4Address> list;
00849         locator->getApListIp(desAddress, list);
00850         for (unsigned int i = 0; i < list.size(); i++)
00851         {
00852             IPv4Route *e = inet_rt->findBestMatchingRoute(list[i]);
00853             if (e && e->getDestination() == list[i])
00854             {
00855                 if (e->getGateway() == gateway && e->getMetric() == hops && e->getInterface() == ie)
00856                     continue;
00857             }
00858 
00859             e = new IPv4Route();
00860 
00862             e->setDestination(list[i]);
00864             e->setNetmask(netmask);
00866             e->setGateway(gateway);
00868             e->setMetric(hops);
00870 
00871             e->setInterface(ie);
00872 
00874             e->setType(entry->getType());
00877             e->setSource(entry->getSource());
00878             inet_rt->addRoute(e);
00879         }
00880     }
00881 #endif
00882 }
00883 
00884 
00885 //
00886 // Check if it exists in the ip4 routing table the address dst
00887 // if it doesn't exist return ALLONES_ADDRESS
00888 //
00889 ManetAddress ManetRoutingBase::omnet_exist_rte(ManetAddress dst)
00890 {
00891     if (!isRegistered)
00892         opp_error("Manet routing protocol is not register");
00893 
00894     /* Add route to kernel routing table ... */
00895     IPv4Address desAddress(dst.getIPv4());
00896     const IPv4Route *e = NULL;
00897     if (mac_layer_)
00898         return ManetAddress::ZERO;
00899     for (int i=inet_rt->getNumRoutes(); i>0; --i)
00900     {
00901         e = inet_rt->getRoute(i-1);
00902         if (desAddress == e->getDestination())
00903             return ManetAddress(e->getGateway());
00904     }
00905     return ManetAddress(IPv4Address::ALLONES_ADDRESS);
00906 }
00907 
00908 //
00909 // Erase all the entries in the routing table
00910 //
00911 void ManetRoutingBase::omnet_clean_rte()
00912 {
00913     if (!isRegistered)
00914         opp_error("Manet routing protocol is not register");
00915 
00916     IPv4Route *entry;
00917     if (mac_layer_)
00918         return;
00919     // clean the route table wlan interface entry
00920     for (int i=inet_rt->getNumRoutes()-1; i>=0; i--)
00921     {
00922         entry = inet_rt->getRoute(i);
00923         if (strstr(entry->getInterface()->getName(), "wlan")!=NULL)
00924         {
00925             inet_rt->deleteRoute(entry);
00926         }
00927     }
00928 }
00929 
00930 //
00931 // generic receiveChangeNotification, the protocols must implemet processLinkBreak and processPromiscuous only
00932 //
00933 void ManetRoutingBase::receiveChangeNotification(int category, const cObject *details)
00934 {
00935     Enter_Method("Manet llf");
00936     if (!isRegistered)
00937         opp_error("Manet routing protocol is not register");
00938     if (category == NF_LINK_BREAK)
00939     {
00940         if (details==NULL)
00941             return;
00942         Ieee80211DataFrame *frame = dynamic_cast<Ieee80211DataFrame *>(const_cast<cObject*>(details));
00943         if (frame)
00944         {
00945             cPacket *pktAux = frame->getEncapsulatedPacket();
00946             if (!mac_layer_ && pktAux != NULL)
00947             {
00948                 cPacket *pkt = pktAux->dup();
00949                 ControlInfoBreakLink *add = new ControlInfoBreakLink;
00950                 add->setDest(frame->getReceiverAddress());
00951                 pkt->setControlInfo(add);
00952                 processLinkBreak(pkt);
00953                 delete pkt;
00954             }
00955             else
00956                 processLinkBreak(details);
00957         }
00958         else
00959         {
00960             Ieee80211ManagementFrame *frame = dynamic_cast<Ieee80211ManagementFrame *>(const_cast<cObject*>(details));
00961             if (frame)
00962                 processLinkBreakManagement(details);
00963         }
00964 
00965     }
00966     else if (category == NF_LINK_PROMISCUOUS)
00967     {
00968         processPromiscuous(details);
00969     }
00970     else if (category == NF_LINK_FULL_PROMISCUOUS)
00971     {
00972         processFullPromiscuous(details);
00973     }
00974     else if(category == NF_L2_AP_DISASSOCIATED || category == NF_L2_AP_ASSOCIATED)
00975     {
00976         Ieee80211MgmtAP::NotificationInfoSta *infoSta = dynamic_cast<Ieee80211MgmtAP::NotificationInfoSta *>(const_cast<cObject*> (details));
00977         if (infoSta)
00978         {
00979             ManetAddress addr;
00980             if (!mac_layer_ && arp)
00981                 addr = ManetAddress(arp->getInverseAddressResolution(infoSta->getStaAddress()));
00982             else
00983                 addr = ManetAddress(infoSta->getStaAddress());
00984             // sanity check
00985             for (unsigned int i = 0; i< proxyAddress.size(); i++)
00986             {
00987                  if (proxyAddress[i].address == addr)
00988                  {
00989                      proxyAddress.erase(proxyAddress.begin()+i);
00990                      break;
00991                  }
00992             }
00993             if (category == NF_L2_AP_ASSOCIATED)
00994             {
00995                 ManetProxyAddress p;
00996                 p.address = addr;
00997                 p.mask = mac_layer_ ? ManetAddress(MACAddress::BROADCAST_ADDRESS) : ManetAddress(IPv4Address::ALLONES_ADDRESS);
00998                 proxyAddress.push_back(p);
00999             }
01000         }
01001     }
01002 #ifdef WITH_80211MESH
01003     else if(category == NF_LOCATOR_ASSOC)
01004         processLocatorAssoc(details);
01005     else if(category == NF_LOCATOR_DISASSOC)
01006         processLocatorDisAssoc(details);
01007 #endif
01008 }
01009 
01010 void ManetRoutingBase::receiveSignal(cComponent *source, simsignal_t signalID, cObject *obj)
01011 {
01012     if (signalID == mobilityStateChangedSignal)
01013     {
01014         IMobility *mobility = check_and_cast<IMobility*>(obj);
01015         curPosition = mobility->getCurrentPosition();
01016         curSpeed = mobility->getCurrentSpeed();
01017         posTimer = simTime();
01018     }
01019 }
01020 
01021 /*
01022   Replacement for gettimeofday(), used for timers.
01023   The timeval should only be interpreted as number of seconds and
01024   fractions of seconds since the start of the simulation.
01025 */
01026 int ManetRoutingBase::gettimeofday(struct timeval *tv, struct timezone *tz)
01027 {
01028     double current_time;
01029     double tmp;
01030     /* Timeval is required, timezone is ignored */
01031     if (!tv)
01032         return -1;
01033     current_time = SIMTIME_DBL(simTime());
01034     tv->tv_sec = (long)current_time; /* Remove decimal part */
01035     tmp = (current_time - tv->tv_sec) * 1000000;
01036     tv->tv_usec = (long)(tmp+0.5);
01037     if (tv->tv_usec>1000000)
01038     {
01039         tv->tv_sec++;
01040         tv->tv_usec -= 1000000;
01041     }
01042     return 0;
01043 }
01044 
01045 //
01046 // Get the index of interface with the same address that add
01047 //
01048 int ManetRoutingBase::getWlanInterfaceIndexByAddress(ManetAddress add)
01049 {
01050     if (!isRegistered)
01051         opp_error("Manet routing protocol is not register");
01052 
01053     if (add.isUnspecified())
01054         return interfaceVector->front().index;
01055 
01056     for (unsigned int i=0; i<interfaceVector->size(); i++)
01057     {
01058         //if (mac_layer_)
01059         if (add.getType() == ManetAddress::MAC_ADDRESS)
01060         {
01061             if ((*interfaceVector)[i].interfacePtr->getMacAddress() == add.getMAC())
01062                 return (*interfaceVector)[i].index;
01063         }
01064         else
01065         {
01066             if ((*interfaceVector)[i].interfacePtr->ipv4Data()->getIPAddress() == add.getIPv4())
01067                 return (*interfaceVector)[i].index;
01068         }
01069     }
01070     return -1;
01071 }
01072 
01073 //
01074 // Get the interface with the same address that add
01075 //
01076 InterfaceEntry *ManetRoutingBase::getInterfaceWlanByAddress(ManetAddress add) const
01077 {
01078     if (!isRegistered)
01079         opp_error("Manet routing protocol is not register");
01080 
01081     if (add.isUnspecified())
01082         return interfaceVector->front().interfacePtr;
01083 
01084     for (unsigned int i=0; i<interfaceVector->size(); i++)
01085     {
01086         //if (mac_layer_)
01087         if (add.getType() == ManetAddress::MAC_ADDRESS)
01088         {
01089             if ((*interfaceVector)[i].interfacePtr->getMacAddress() == add.getMAC())
01090                 return (*interfaceVector)[i].interfacePtr;
01091         }
01092         else
01093         {
01094             if ((*interfaceVector)[i].interfacePtr->ipv4Data()->getIPAddress() == add.getIPv4())
01095                 return (*interfaceVector)[i].interfacePtr;
01096         }
01097     }
01098     return NULL;
01099 }
01100 
01101 //
01102 // Get the index used in the general interface table
01103 //
01104 int ManetRoutingBase::getWlanInterfaceIndex(int i) const
01105 {
01106     if (!isRegistered)
01107         opp_error("Manet routing protocol is not register");
01108 
01109     if (i >= 0 && i < (int) interfaceVector->size())
01110         return (*interfaceVector)[i].index;
01111     else
01112         return -1;
01113 }
01114 
01115 //
01116 // Get the i-esime wlan interface
01117 //
01118 InterfaceEntry *ManetRoutingBase::getWlanInterfaceEntry(int i) const
01119 {
01120     if (!isRegistered)
01121         opp_error("Manet routing protocol is not register");
01122 
01123     if (i >= 0 && i < (int)interfaceVector->size())
01124         return (*interfaceVector)[i].interfacePtr;
01125     else
01126         return NULL;
01127 }
01128 
01129 
01131 //
01132 // Access to node position
01133 //
01135 const Coord& ManetRoutingBase::getPosition()
01136 {
01137     if (!regPosition)
01138         error("this node doesn't have activated the register position");
01139     return curPosition;
01140 }
01141 
01142 double ManetRoutingBase::getSpeed()
01143 {
01144     if (!regPosition)
01145         error("this node doesn't have activated the register position");
01146     return curSpeed.length();
01147 }
01148 
01149 const Coord& ManetRoutingBase::getDirection()
01150 {
01151     if (!regPosition)
01152         error("this node doesn't have activated the register position");
01153     return curSpeed;
01154 }
01155 
01156 void ManetRoutingBase::setInternalStore(bool i)
01157 {
01158     createInternalStore = i;
01159     if (!createInternalStore)
01160     {
01161         delete routesVector;
01162         routesVector = NULL;
01163     }
01164     else
01165     {
01166         if (routesVector==NULL)
01167             routesVector = new RouteMap;
01168     }
01169 }
01170 
01171 
01172 ManetAddress ManetRoutingBase::getNextHopInternal(const ManetAddress &dest)
01173 {
01174     if (routesVector==NULL)
01175         return ManetAddress::ZERO;
01176     if (routesVector->empty())
01177         return ManetAddress::ZERO;
01178     RouteMap::iterator it = routesVector->find(dest);
01179     if (it!=routesVector->end())
01180         return it->second;
01181     return ManetAddress::ZERO;
01182 }
01183 
01184 bool ManetRoutingBase::setRoute(const ManetAddress & destination, const ManetAddress &nextHop, const int &ifaceIndex, const int &hops, const ManetAddress &mask)
01185 {
01186     if (!isRegistered)
01187         opp_error("Manet routing protocol is not register");
01188 
01189     /* Add route to kernel routing table ... */
01190     IPv4Address desAddress(destination.getIPv4());
01191     bool del_entry = (nextHop.isUnspecified());
01192 
01193     if (!createInternalStore && routesVector)
01194     {
01195          delete routesVector;
01196          routesVector = NULL;
01197     }
01198     else if (createInternalStore && routesVector)
01199     {
01200          //FIXME netmask not stored in internal routesVector, only stored in inet routing table
01201          // Is the netmask always ALLONES? If yes, do remove mask parameter...
01202          RouteMap::iterator it = routesVector->find(destination);
01203          if (it != routesVector->end())
01204              routesVector->erase(it);
01205          if (!del_entry)
01206          {
01207              routesVector->insert(std::make_pair<ManetAddress,ManetAddress>(destination, nextHop));
01208          }
01209     }
01210 
01211     if (mac_layer_)
01212         return true;
01213 
01214     if (ifaceIndex>=getNumInterfaces())
01215         return false;
01216 
01217     bool found = false;
01218     IPv4Route *oldentry = NULL;
01219 
01220     //TODO the entries with ALLONES netmasks stored at the begin of inet route entry vector,
01221     // let optimise next search!
01222     for (int i=inet_rt->getNumRoutes(); i>0; --i)
01223     {
01224         IPv4Route *e = inet_rt->getRoute(i-1);
01225         if (desAddress == e->getDestination())     // FIXME netmask checking?
01226         {
01227             if (del_entry && !found)    // FIXME The 'found' never set to true when 'del_entry' is true
01228             {
01229                 if (!inet_rt->deleteRoute(e))
01230                     opp_error("ManetRoutingBase::setRoute can't delete route entry");
01231             }
01232             else
01233             {
01234                 found = true;
01235                 oldentry = e;
01236             }
01237         }
01238     }
01239 
01240 #ifdef WITH_80211MESH
01241     if (locator && locator->isApIp(desAddress) && del_entry)
01242     {
01243         std::vector<IPv4Address> list;
01244         locator->getApListIp(desAddress, list);
01245         for (unsigned int i = 0; i < list.size(); i++)
01246         {
01247             for (int i = inet_rt->getNumRoutes(); i > 0; --i)
01248             {
01249                 IPv4Route *e = inet_rt->getRoute(i - 1);
01250                 if (list[i] == e->getDestination())
01251                 {
01252                     if (!inet_rt->deleteRoute(e))
01253                         opp_error("Aodv omnet_chg_rte can't delete route entry");
01254                     else
01255                         break;
01256                 }
01257             }
01258         }
01259     }
01260 #endif
01261     if (del_entry)
01262         return true;
01263 
01264     IPv4Address netmask(mask.getIPv4());
01265     IPv4Address gateway(nextHop.getIPv4());
01266     if (mask.isUnspecified())
01267         netmask = IPv4Address::ALLONES_ADDRESS;
01268     InterfaceEntry *ie = getInterfaceEntry(ifaceIndex);
01269 
01270     if (found)
01271     {
01272         if (oldentry->getDestination() == desAddress
01273                 && oldentry->getNetmask() == netmask
01274                 && oldentry->getGateway() == gateway
01275                 && oldentry->getMetric() == hops
01276                 && oldentry->getInterface() == ie
01277                 && oldentry->getSource() == IPv4Route::MANUAL)
01278             return true;
01279         inet_rt->deleteRoute(oldentry);
01280     }
01281 
01282     IPv4Route *entry = new IPv4Route();
01283 
01285     entry->setDestination(desAddress);
01287     entry->setNetmask(netmask);
01289     entry->setGateway(gateway);
01291     entry->setMetric(hops);
01293 
01294     entry->setInterface(ie);
01295 
01298     IPv4Route::RouteSource routeSource = usetManetLabelRouting ? IPv4Route::MANET : IPv4Route::MANET2;
01299     entry->setSource(routeSource);
01300 
01301     inet_rt->addRoute(entry);
01302 
01303 #ifdef WITH_80211MESH
01304     if (locator && locator->isApIp(desAddress))
01305     {
01306         std::vector<IPv4Address> list;
01307         locator->getApListIp(desAddress, list);
01308         for (unsigned int i = 0; i < list.size(); i++)
01309         {
01310             IPv4Route *e = inet_rt->findBestMatchingRoute(list[i]);
01311             if (e && e->getDestination() == list[i])
01312             {
01313                 if (e->getGateway() == gateway && e->getMetric() == hops && e->getInterface() == ie)
01314                     continue;
01315             }
01316 
01317             e = new IPv4Route();
01318 
01320             e->setDestination(list[i]);
01322             e->setNetmask(netmask);
01324             e->setGateway(gateway);
01326             e->setMetric(hops);
01328 
01329             e->setInterface(ie);
01330 
01332             e->setType(entry->getType());
01335             e->setSource(entry->getSource());
01336             inet_rt->addRoute(e);
01337         }
01338     }
01339 #endif
01340     return true;
01341 
01342 }
01343 
01344 bool ManetRoutingBase::setRoute(const ManetAddress & destination, const ManetAddress &nextHop, const char *ifaceName, const int &hops, const ManetAddress &mask)
01345 {
01346     if (!isRegistered)
01347         opp_error("Manet routing protocol is not register");
01348 
01349     /* Add route to kernel routing table ... */
01350     int index;
01351     for (index = 0; index < getNumInterfaces(); index++)
01352     {
01353         if (strcmp(ifaceName, getInterfaceEntry(index)->getName())==0) break;
01354     }
01355     if (index>=getNumInterfaces())
01356         return false;
01357     return setRoute(destination, nextHop, index, hops, mask);
01358 };
01359 
01360 void ManetRoutingBase::sendICMP(cPacket *pkt)
01361 {
01362     if (pkt==NULL)
01363         return;
01364 
01365     if (icmpModule==NULL || !sendToICMP)
01366     {
01367         delete pkt;
01368         return;
01369     }
01370 
01371     if (mac_layer_)
01372     {
01373         // The packet is encapsulated in a Ieee802 frame
01374         cPacket *pktAux = pkt->decapsulate();
01375         if (pktAux)
01376         {
01377             delete pkt;
01378             pkt = pktAux;
01379         }
01380     }
01381 
01382     IPv4Datagram *datagram = dynamic_cast<IPv4Datagram*>(pkt);
01383     if (datagram==NULL)
01384     {
01385         delete pkt;
01386         return;
01387     }
01388     // don't send ICMP error messages for multicast messages
01389     if (datagram->getDestAddress().isMulticast())
01390     {
01391         EV << "won't send ICMP error messages for multicast message " << datagram << endl;
01392         delete pkt;
01393         return;
01394     }
01395     // check source address
01396     if (datagram->getSrcAddress().isUnspecified() && par("setICMPSourceAddress"))
01397         datagram->setSrcAddress(inet_ift->getInterface(0)->ipv4Data()->getIPAddress());
01398     EV << "issuing ICMP Destination Unreachable for packets waiting in queue for failed route discovery.\n";
01399     icmpModule->sendErrorMessage(datagram, ICMP_DESTINATION_UNREACHABLE, 0);
01400 }
01401 // The address group allows to implement the anycast. Any address in the group is valid for the route
01402 // Address group methods
01403 //
01404 
01405 
01406 int  ManetRoutingBase::getNumAddressInAGroups(int group)
01407 {
01408     if ((int)addressGroupVector.size()<=group)
01409         return -1;
01410     return addressGroupVector[group].size();
01411 }
01412 
01413 void ManetRoutingBase::addInAddressGroup(const ManetAddress& addr, int group)
01414 {
01415     AddressGroup addressGroup;
01416     if ((int)addressGroupVector.size()<=group)
01417     {
01418         while ((int)addressGroupVector.size()<=group)
01419         {
01420             AddressGroup addressGroup;
01421             addressGroupVector.push_back(addressGroup);
01422         }
01423     }
01424     else
01425     {
01426         if (addressGroupVector[group].count(addr)>0)
01427             return;
01428     }
01429     addressGroupVector[group].insert(addr);
01430     // check if the node is already in the group
01431     if (isLocalAddress(addr))
01432     {
01433         for (unsigned int i=0; i<inAddressGroup.size(); i++)
01434         {
01435             if (inAddressGroup[i]==group)
01436                 return;
01437         }
01438         inAddressGroup.push_back(group);
01439     }
01440 }
01441 
01442 bool ManetRoutingBase::delInAddressGroup(const ManetAddress& addr, int group)
01443 {
01444     if ((int)addressGroupVector.size()<=group)
01445         return false;
01446     if (addressGroupVector[group].count(addr)==0)
01447         return false;
01448 
01449     addressGroupVector[group].erase(addr);
01450     // check if the node is already in the group
01451     if (isLocalAddress(addr))
01452     {
01453         // check if other address is in the group
01454         for (AddressGroup::iterator it = addressGroupVector[group].begin(); it!=addressGroupVector[group].end(); it++)
01455             if (isLocalAddress(*it)) return true;
01456         for (unsigned int i=0; i<inAddressGroup.size(); i++)
01457         {
01458             if (inAddressGroup[i]==group)
01459             {
01460                 inAddressGroup.erase(inAddressGroup.begin()+i);
01461                 return true;
01462             }
01463         }
01464     }
01465     return true;
01466 }
01467 
01468 bool ManetRoutingBase::findInAddressGroup(const ManetAddress& addr, int group)
01469 {
01470     if ((int)addressGroupVector.size()<=group)
01471         return false;
01472     if (addressGroupVector[group].count(addr)>0)
01473         return true;
01474     return false;
01475 }
01476 
01477 bool ManetRoutingBase::findAddressAndGroup(const ManetAddress& addr, int &group)
01478 {
01479     if (addressGroupVector.empty())
01480         return false;
01481     for (unsigned int i=0; i<addressGroupVector.size(); i++)
01482     {
01483         if (findInAddressGroup(addr, i))
01484         {
01485             group = i;
01486             return true;
01487         }
01488     }
01489     return false;
01490 }
01491 
01492 bool ManetRoutingBase::isInAddressGroup(int group)
01493 {
01494     if (inAddressGroup.empty())
01495         return false;
01496     for (unsigned int i=0; i<inAddressGroup.size(); i++)
01497         if (group==inAddressGroup[i])
01498             return true;
01499     return false;
01500 }
01501 
01502 bool ManetRoutingBase::getAddressGroup(AddressGroup &addressGroup, int group)
01503 {
01504     if ((int)addressGroupVector.size()<=group)
01505         return false;
01506     addressGroup = addressGroupVector[group];
01507     return true;
01508 }
01509 
01510 bool ManetRoutingBase::getAddressGroup(std::vector<ManetAddress> &addressGroup, int group)
01511 {
01512     if ((int)addressGroupVector.size()<=group)
01513         return false;
01514     addressGroup.clear();
01515     for (AddressGroup::iterator it=addressGroupVector[group].begin(); it!=addressGroupVector[group].end(); it++)
01516         addressGroup.push_back(*it);
01517     return true;
01518 }
01519 
01520 
01521 bool ManetRoutingBase::isAddressInProxyList(const ManetAddress & addr)
01522 {
01523     if (proxyAddress.empty())
01524         return false;
01525     for (unsigned int i = 0; i < proxyAddress.size(); i++)
01526     {
01527         //if ((addr & proxyAddress[i].mask) == proxyAddress[i].address)
01528         if (addr == proxyAddress[i].address)   //FIXME
01529             return true;
01530     }
01531     return false;
01532 }
01533 
01534 void ManetRoutingBase::setAddressInProxyList(const ManetAddress & addr,const ManetAddress & mask)
01535 {
01536     // search if exist
01537     for (unsigned int i = 0; i < proxyAddress.size(); i++)
01538     {
01539         if ((addr == proxyAddress[i].address) && (mask == proxyAddress[i].mask))
01540             return;
01541     }
01542     ManetProxyAddress val;
01543     val.address = addr;
01544     val.mask = mask;
01545     proxyAddress.push_back(val);
01546 }
01547 
01548 bool ManetRoutingBase::getAddressInProxyList(int i,ManetAddress &addr, ManetAddress &mask)
01549 {
01550     if (i< 0 || i >= (int)proxyAddress.size())
01551         return false;
01552     addr = proxyAddress[i].address;
01553     mask = proxyAddress[i].mask;
01554     return true;
01555 }
01556 
01557 
01558 bool ManetRoutingBase::addressIsForUs(const ManetAddress &addr) const
01559 {
01560     if (isLocalAddress(addr))
01561         return true;
01562     if (proxyAddress.empty())
01563         return false;
01564     for (unsigned int i = 0; i < proxyAddress.size(); i++)
01565     {
01566         //if ((addr & proxyAddress[i].mask) == proxyAddress[i].address)
01567         if (addr == proxyAddress[i].address)   //FIXME
01568             return true;
01569     }
01570     return false;
01571 }
01572 
01573 bool ManetRoutingBase::getAp(const ManetAddress &destination, ManetAddress& accesPointAddr) const
01574 {
01575 #ifdef WITH_80211MESH
01576     if (locator == NULL)
01577         return false;
01578     if (isInMacLayer())
01579     {
01580         MACAddress macAddr = locator->getLocatorMacToMac(MACAddress(destination.getLo()));
01581         if (macAddr.isUnspecified())
01582             return false;
01583         accesPointAddr = macAddr.getInt();
01584     }
01585     else
01586     {
01587         IPv4Address ipAddr = locator->getLocatorIpToIp(IPv4Address(destination.getLo()));
01588         if (ipAddr.isUnspecified())
01589             return false;
01590         accesPointAddr = ipAddr.getInt();
01591     }
01592     return true;
01593 #else
01594     return false;
01595 #endif
01596 }
01597 
01598 void ManetRoutingBase::getApList(const MACAddress & dest,std::vector<MACAddress>& list)
01599 {
01600     list.clear();
01601 #ifdef WITH_80211MESH
01602     if (locator)
01603     {
01604         MACAddress ap = locator->getLocatorMacToMac(dest);
01605         if (!ap.isUnspecified())
01606             locator->getApList(ap,list);
01607         else
01608             locator->getApList(dest,list);
01609     }
01610 #endif
01611     list.push_back(dest);
01612 }
01613 
01614 void ManetRoutingBase::getApListIp(const IPv4Address &dest,std::vector<IPv4Address>& list)
01615 {
01616     list.clear();
01617 #ifdef WITH_80211MESH
01618     if (locator)
01619     {
01620         IPv4Address ap = locator->getLocatorIpToIp(dest);
01621         if (!ap.isUnspecified())
01622             locator->getApListIp(ap,list);
01623         else
01624             locator->getApListIp(dest,list);
01625     }
01626 #endif
01627     list.push_back(dest);
01628 }
01629 
01630 void ManetRoutingBase::getListRelatedAp(const ManetAddress & add, std::vector<ManetAddress>& list)
01631 {
01632     //if (mac_layer_)
01633     if (add.getType() == ManetAddress::MAC_ADDRESS)
01634     {
01635         std::vector<MACAddress> listAux;
01636         getApList(add.getMAC(), listAux);
01637         list.clear();
01638         for (unsigned int i = 0; i < listAux.size(); i++)
01639         {
01640             list.push_back(ManetAddress(listAux[i]));
01641         }
01642     }
01643     else
01644     {
01645         std::vector<IPv4Address> listAux;
01646         getApListIp(add.getIPv4(), listAux);
01647         list.clear();
01648         for (unsigned int i = 0; i < listAux.size(); i++)
01649         {
01650             list.push_back(ManetAddress(listAux[i]));
01651         }
01652     }
01653 }
01654 
01655 bool ManetRoutingBase::isAp() const
01656 {
01657 #ifdef WITH_80211MESH
01658     if (!locator)
01659         return false;
01660     if (mac_layer_)
01661         return locator->isThisAp();
01662     else
01663         return locator->isThisApIp();
01664 #else
01665     return false;
01666 #endif
01667 }
01668 
01669 bool ManetRoutingBase::getRouteFromGlobal(const ManetAddress &src, const ManetAddress &dest, std::vector<ManetAddress> &route)
01670 {
01671     if (!createInternalStore || globalRouteMap == NULL)
01672         return false;
01673     ManetAddress next = src;
01674     route.clear();
01675     route.push_back(src);
01676     while (1)
01677     {
01678         GlobalRouteMap::iterator it = globalRouteMap->find(next);
01679         if (it==globalRouteMap->end())
01680             return false;
01681         if (it->second.empty())
01682             return false;
01683 
01684         if (it->second.size() == 1)
01685         {
01686             RouteMap *rt = it->second[0].routesVector;
01687             RouteMap::iterator it2 = rt->find(dest);
01688             if (it2 == rt->end())
01689                 return false;
01690             if (it2->second == dest)
01691             {
01692                 route.push_back(dest);
01693                 return true;
01694             }
01695             else
01696             {
01697                 route.push_back(it2->second);
01698                 next = it2->second;
01699             }
01700         }
01701         else
01702         {
01703             if (it->second.size() > 2)
01704                 throw cRuntimeError("Number of routing protocols bigger that 2");
01705             // if several protocols, search before in the proactive
01706             RouteMap *rt;
01707             if (it->second[0].isProactive)
01708                 rt = it->second[0].routesVector;
01709             else
01710                 rt = it->second[1].routesVector;
01711             RouteMap::iterator it2 = rt->find(dest);
01712             if (it2 == rt->end())
01713             {
01714                 // search in the reactive
01715 
01716                 if (it->second[0].isProactive)
01717                     rt = it->second[1].routesVector;
01718                 else
01719                     rt = it->second[0].routesVector;
01720                 it2 = rt->find(dest);
01721                 if (it2 == rt->end())
01722                     return false;
01723             }
01724             if (it2->second == dest)
01725             {
01726                 route.push_back(dest);
01727                 return true;
01728             }
01729             else
01730             {
01731                 route.push_back(it2->second);
01732                 next = it2->second;
01733             }
01734         }
01735     }
01736 }
01737 
 All Classes Files Functions Variables Typedefs Enumerator Defines