mrcl-routing-static.cc

00001 /*
00002  * Copyright (c) 2008 Regents of the SIGNET lab, University of Padova.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University of Padova (SIGNET lab) nor the 
00014  *    names of its contributors may be used to endorse or promote products 
00015  *    derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00018  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
00019  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
00020  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
00021  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00022  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00023  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00024  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
00025  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
00026  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00027  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include"mrcl-routing-static.h"
00031 #include"routing-module.h"
00032 #include<hop-count-metric.h>
00033 #include<ip.h>
00034 
00035 static class MrclRoutingStaticClass : public TclClass {
00036 public:
00037         MrclRoutingStaticClass() : TclClass("Routing/MrclRoutingStatic") {}
00038         TclObject* create(int, const char*const*) {
00039                 return (new MrclRoutingStatic());
00040         }
00041 } class_mrcl_routing_static;
00042 
00043 MrclRoutingStatic::MrclRoutingStatic() : tail_(0), routes_(0)
00044 {
00045         m_ = new HopCountMetric*;
00046         m_[0] = new HopCountMetric();
00047         m_[0]->setHopCount(1);
00048 }
00049 
00050 
00051 MrclRoutingStatic::~MrclRoutingStatic()
00052 {
00053         delete m_[0];
00054         delete m_;
00055 }
00056 
00057 
00058 int MrclRoutingStatic::command(int argc, const char*const* argv)
00059 {
00060         Tcl& tcl = Tcl::instance();
00061 //      if(argc == 2)
00062 //      {
00063 //              if (strcasecmp(argv[1],"addr")==0)
00064 //              {
00065 //                      tcl.resultf("%d", ipAddr_);
00066 //                      return TCL_OK;
00067 //              }
00068 //      }
00069 //      else if (argc == 3)
00070 //      {
00071 //              if (strcmp(argv[1],"setAddr")==0)
00072 //              {
00073 //                      IPAddress *addr = (IPAddress *)(TclObject::lookup(argv[2]));
00074 //                      if (addr==0)
00075 //                      {
00076 //                              printf("AodvModule::command(%s), error in address lookup\n", argv[1]);
00077 //                              return TCL_ERROR;
00078 //                      }
00079 //                      addr_ = addr;
00080 //                      return TCL_OK;
00081 //              }
00082 //              else if (strcasecmp (argv[1], "metric") == 0)
00083 //              {
00084 //                      SandboxMetric *m = new SandboxMetric();
00085 //                      SandboxMetric **mp = new SandboxMetric*[1];
00086 //                      mp[0] = m;
00087 //                      myMetric_ = mp;
00088 //                      myMetric_[0]->setValue(atof(argv[2]));
00089 //                      Packet *p;
00090 //                      if (debug_>5) printf("IPmodule %d has metric %f\n", ipAddr_, myMetric_[0]->value(p));
00091 //                      return TCL_OK;
00092 //              }
00093 //      }
00094         if (argc == 6) 
00095         {
00096                 if (strcasecmp (argv[1], "addRoute") == 0)
00097                 {
00098                         IPAddress *net = (IPAddress *)(TclObject::lookup(argv[2]));
00099                         IPAddress *mask = (IPAddress *)(TclObject::lookup(argv[3]));
00100                         IPAddress *nextHop = (IPAddress *)(TclObject::lookup(argv[4]));
00101                         if ((net==0)||(mask==0)||(nextHop==0))
00102                         {
00103                                 printf("MrclRoutingStatic::command(%s), error in address lookup\n", argv[1]);
00104                                 return TCL_ERROR;
00105                         }
00106                         int module = atoi(argv[5]);
00107                         MRS_RouteEntry  *r = new MRS_RouteEntry;
00108                         r->net = net;
00109                         r->mask = mask;
00110                         r->module = module;
00111                         r->next_hop = nextHop;
00112                         r->next = 0;
00113                         if(tail_)
00114                         {
00115                                 tail_->next = r;
00116                         }
00117                         else
00118                         {
00119                                 routes_ = r;
00120                         }
00121                         tail_ = r;
00122                         int len = net->strlen() + 2;
00123                         char *neta = new char[len];
00124                         net->toString(neta, len);
00125                         len = mask->strlen() + 2;
00126                         char *maska = new char[len];
00127                         mask->toString(maska, len);
00128                         len = nextHop->strlen() + 2;
00129                         char *nexthopa = new char[len];
00130                         nextHop->toString(nexthopa, len);
00131                         printf("Add Route: net %s mask %s nexthop %s\n", neta, maska, nexthopa);
00132                         return TCL_OK;
00133                 }
00134         }
00135         return (MrclRouting::command(argc,argv));
00136 }
00137 
00138 
00139 char* MrclRoutingStatic::getNextHop(Packet *p)
00140 {
00141         hdr_ip *iph = HDR_IP(p);
00142         hdr_cmn *ch = HDR_CMN(p);
00143         RoutingHdr *rhdr = HDR_ROUTING(p);
00144         //printf("[MrclRoutingStatic::getNextHop] Ip %d Try to find nexthop for %d:\n", ipAddr_, iph->daddr());
00145         for(MRS_RouteEntry *cur = routes_; cur; cur = cur->next)
00146         {
00147                 //printf("try with %d -> ", cur->next_hop.getIpAddr());
00148 //              if((iph->daddr() & cur->mask->getIpAddr()) == cur->net.getIpAddr())
00149                 int len = cur->net->strlen() + 2;
00150                 char *neta = new char[len];
00151                 cur->net->toString(neta, len);
00152                 len = cur->mask->strlen() + 2;
00153                 char *maska = new char[len];
00154                 cur->mask->toString(maska, len);
00155                 len = cur->next_hop->strlen() +2;
00156                 char *nexthopa = new char[len];
00157                 cur->next_hop->toString(nexthopa, len);
00158                 char addr[20];
00159                 int ip;
00160                 memcpy(&ip, rhdr->daddr() + sizeof(int), sizeof(int));
00161                 sprintf(addr,"%d.%d.%d.%d", (ip & 0xff000000)>>24,(ip & 0x00ff0000)>>16, (ip & 0x0000ff00)>>8, (ip & 0x000000ff));
00162                 printf("STATIC-getnetxhop\n");
00163                 printf("\tckecking cur %p net %s mask %s addr %s netxhop %s\n", cur, neta, maska, addr, nexthopa);
00164                 printf("\tcur->mak->AND(a)=%d cur->net->INT()=%d\n", cur->mask->AND(rhdr->daddr()), cur->net->INT());
00165 
00166                 if (cur->mask->AND(rhdr->daddr()) == cur->net->INT())
00167                 {
00168                         //printf("GOT IT\n");
00169                         return(cur->next_hop->getAddr());
00170                 }
00171                 //printf("NO\n");
00172         }
00173         drop(p, 1, STATICROUTE_NO_ROUTE);
00174 }
00175 
00176 
00177 int MrclRoutingStatic::canIReach(char *a, Metric ***m)
00178 {
00179         printf("begin\n");
00180         //printf("[MrclRoutingStatic::canIReach] Ip %d Try to find nexthop for %d:\n", ipAddr_, ((IPAddress *)a)->getIpAddr());
00181         for(MRS_RouteEntry *cur = routes_; cur; cur = cur->next)
00182         {
00183                 //printf("try with %d -> ", cur->next_hop.getIpAddr());
00184 //              if((((IPAddress *)a)->getIpAddr() & cur->mask.getIpAddr()) == cur->net.getIpAddr())
00185 
00186                 int len = cur->net->strlen() + 2;
00187                 char *neta = new char[len];
00188                 cur->net->toString(neta, len);
00189                 len = cur->mask->strlen() + 2;
00190                 char *maska = new char[len];
00191                 cur->mask->toString(maska, len);
00192                 char addr[20];
00193                 int ip;
00194                 memcpy(&ip, a + sizeof(int), sizeof(int));
00195                 sprintf(addr,"%d.%d.%d.%d", (ip & 0xff000000)>>24,(ip & 0x00ff0000)>>16, (ip & 0x0000ff00)>>8, (ip & 0x000000ff));
00196                 printf("ckecking cur %p net %s mask %s addr %s\n", cur, neta, maska, addr);
00197                 printf("cur->mak->AND(a)=%d cur->net->INT()=%d\n", cur->mask->AND(a), cur->net->INT());
00198                 if ( cur->mask->AND(a) == cur->net->INT())
00199                 {
00200                         Packet *p;
00201                         printf("I CAN REACH %f\n", m_[0]->value(p));
00202                         *m = (Metric **)m_;
00203                         return(1);
00204                 }
00205                 //printf("NO\n");
00206         }
00207         //printf("Can not reach such a destination\n");
00208         return(0);
00209 }
00210 
00211 
00212 void MrclRoutingStatic::forward(Packet *p)
00213 {
00214         RoutingHdr* mrhdr = HDR_ROUTING(p);
00215         hdr_ip *iph = HDR_IP(p);
00216         hdr_cmn *ch = HDR_CMN(p);
00217         char destAddr[MRCL_ADDRESS_MAX_LEN];
00218         char myaddr[MRCL_ADDRESS_MAX_LEN];
00219         int ip;
00220         sprintf(destAddr,"%d.%d.%d.%d", (iph->daddr() & 0xff000000)>>24,(iph->daddr() & 0x00ff0000)>>16, (iph->daddr() & 0x0000ff00)>>8, (iph->daddr() & 0x000000ff));
00221         memcpy(&ip, getAddress(0)->getAddr() + sizeof(int), sizeof(int));
00222         sprintf(myaddr,"%d.%d.%d.%d", (ip & 0xff000000)>>24,(ip & 0x00ff0000)>>16, (ip & 0x0000ff00)>>8, (ip & 0x000000ff));
00223         printf("[MrclRoutingStatic::forward] Ip %d Try to find nexthop for %d (%s):\n", myaddr, iph->daddr(), destAddr);
00224 
00225         if (isMyAddress(mrhdr->daddr()))
00226         {
00227                 printf("My packet %d -> send up\n", ch->uid());
00228                 sendUp(p);
00229         }
00230         else
00231         {
00232                 resolve(p);
00233         }
00234 }
00235 
00236 void MrclRoutingStatic::resolve(Packet* p)
00237 {
00238         // packet not for me -> try to solve it
00239         RoutingHdr* mrhdr = HDR_ROUTING(p);
00240         hdr_ip *iph = HDR_IP(p);
00241         hdr_cmn *ch = HDR_CMN(p);
00242         char destAddr[MRCL_ADDRESS_MAX_LEN];
00243         char myaddr[MRCL_ADDRESS_MAX_LEN];
00244         int ip;
00245         sprintf(destAddr,"%d.%d.%d.%d", (iph->daddr() & 0xff000000)>>24,(iph->daddr() & 0x00ff0000)>>16, (iph->daddr() & 0x0000ff00)>>8, (iph->daddr() & 0x000000ff));
00246         memcpy(&ip, getAddress(0)->getAddr() + sizeof(int), sizeof(int));
00247         sprintf(myaddr,"%d.%d.%d.%d", (ip & 0xff000000)>>24,(ip & 0x00ff0000)>>16, (ip & 0x0000ff00)>>8, (ip & 0x000000ff));
00248         printf("[MrclRoutingStatic::resolve] Ip %d Try to find nexthop for %d (%s):\n", myaddr, iph->daddr(), destAddr);
00249         
00250         int nRoute = getRoute(mrhdr->daddr(), p, 0);
00251         if (nRoute>0)
00252         {
00253                 // packet can be forwarded
00254                 printf("IP %s Discovered routes %d:\n", myaddr, nRoute);
00255                 printf("0 to reach %s through module %d\n", mrhdr->daddr(), mrhdr->getModuleRoute(mrhdr->moduleRouteCount() - 1));
00256                 for(int i=1; i<nRoute; i++)
00257                 {
00258                         getRoute(mrhdr->daddr(), p, i);
00259                         printf("\t%d to reach %s through module %d\n", i, mrhdr->daddr(), mrhdr->getModuleRoute(mrhdr->moduleRouteCount() - 1));
00260                 }
00261                 // forward to the 1st one
00262                 getRoute(mrhdr->daddr(), p, 0); // re-set packet header parameters
00263                 if (mrhdr->getModuleRoute(mrhdr->moduleRouteCount() - 1)==getId())
00264                 {
00265                         // this module can forward the packet directly
00266                         mrhdr->clearModuleRoute();
00267                         char *nextHop = getNextHop(p);
00268                         if(nextHop)
00269                         {
00270                                 IPAddress::storeAddr(mrhdr->nexthop(), nextHop);
00271 //                              ch->next_hop() = (nsaddr_t)(mrhdr->nexthop()+sizeof(int)); COMPILER ERROR ON 64 BIT MACHINES 
00272                                 ch->next_hop() = *((nsaddr_t*)(mrhdr->nexthop()+sizeof(int)));
00273                                 // here the packet has to be solved from the leaf module
00274                         }
00275                         for(MRS_RouteEntry *cur = routes_; cur; cur = cur->next)
00276                         {
00277 //                              if((dest.getIpAddr() & cur->mask.getIpAddr()) == cur->net.getIpAddr())
00278                                 if ( cur->mask->AND(mrhdr->daddr()) == cur->net->INT())
00279                                 {
00280                                         printf("[FORWARDING1] IP %d to %s module %d nexthop %d\n\n", myaddr, destAddr, cur->module, cur->next_hop->INT());
00281                                         hdr_ip *iph = HDR_IP(p);
00282                                         hdr_cmn *ch = HDR_CMN(p);
00283                                         int id = 0;
00284                                         for(MRS_RouteEntry *cur = routes_; cur; cur = cur->next)
00285                                         {
00286                                                 if((iph->daddr() & cur->mask->INT()) == cur->net->INT())
00287                                                 {
00288                                                         id = cur->module;
00289                                                 }
00290                                         }
00291                                         sendDown(id,p);
00292                                         return;
00293                                 }
00294                         }
00295                 }
00296                 else
00297                 {
00298                         // this packet has to be forwarded to other routing module
00299                         printf("[FORWARDING2] IP %d sendUp to another routing module\n", myaddr);
00300                 //                              mrhdr->setSendup();
00301                         sendUp(p);
00302                 }
00303         }
00304         else
00305         {
00306                 // packet can not be forwarded
00307                 drop(p, STATICROUTE_NO_ROUTE_VERBOSITY, STATICROUTE_NO_ROUTE);
00308         }
00309 }
00310 
00311 
00312 
00313 
00314 int MrclRoutingStatic::controlPacket(Packet *p)
00315 {
00316         return(FALSE);
00317 }

Generated on Wed Nov 26 15:47:28 2008 for NS-MIRACLE library by  doxygen 1.5.2