00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
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
00145 for(MRS_RouteEntry *cur = routes_; cur; cur = cur->next)
00146 {
00147
00148
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
00169 return(cur->next_hop->getAddr());
00170 }
00171
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
00181 for(MRS_RouteEntry *cur = routes_; cur; cur = cur->next)
00182 {
00183
00184
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
00206 }
00207
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
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
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
00262 getRoute(mrhdr->daddr(), p, 0);
00263 if (mrhdr->getModuleRoute(mrhdr->moduleRouteCount() - 1)==getId())
00264 {
00265
00266 mrhdr->clearModuleRoute();
00267 char *nextHop = getNextHop(p);
00268 if(nextHop)
00269 {
00270 IPAddress::storeAddr(mrhdr->nexthop(), nextHop);
00271
00272 ch->next_hop() = *((nsaddr_t*)(mrhdr->nexthop()+sizeof(int)));
00273
00274 }
00275 for(MRS_RouteEntry *cur = routes_; cur; cur = cur->next)
00276 {
00277
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
00299 printf("[FORWARDING2] IP %d sendUp to another routing module\n", myaddr);
00300
00301 sendUp(p);
00302 }
00303 }
00304 else
00305 {
00306
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 }