aodv-rt-table.cc

00001 /*
00002 Copyright (c) 1997, 1998 Carnegie Mellon University.  All Rights
00003 Reserved. 
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007 
00008 1. Redistributions of source code must retain the above copyright notice,
00009 this list of conditions and the following disclaimer.
00010 2. Redistributions in binary form must reproduce the above copyright notice,
00011 this list of conditions and the following disclaimer in the documentation
00012 and/or other materials provided with the distribution.
00013 3. The name of the author may not be used to endorse or promote products
00014 derived from this software without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00017 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00018 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00019 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00020 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00021 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00022 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00023 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00024 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00025 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 The AODV code developed by the CMU/MONARCH group was optimized and tuned by Samir Das and Mahesh Marina, University of Cincinnati. The work was partially done in Sun Microsystems.
00028 */
00029 
00037 #include "aodv-rt-table.h"
00038 
00039 /*---------------------------------------
00040  *                                      |
00041  *      Aodv_rt_entry                   |
00042  *                                      |
00043  ---------------------------------------*/
00044 
00045 Aodv_rt_entry::Aodv_rt_entry()
00046 {
00047         rt_req_timeout = 0.0;
00048         rt_req_cnt = 0;
00049 
00050         memset(rt_dst_, 0, MRCL_ADDRESS_MAX_LEN);
00051         rt_seqno_ = 0;
00052         rt_hops_ = rt_last_hop_count_ = INFINITY2;
00053         memset(nexthop_, 0, MRCL_ADDRESS_MAX_LEN);
00054         rt_pclist_ = 0;
00055         expire_ = 0.0;
00056         rt_flags_ = RTF_DOWN;
00057 
00058         for (int i=0; i < MAX_HISTORY; i++) {
00059                 rt_disc_latency_[i] = 0.0;
00060         }
00061         hist_indx_ = 0;
00062         rt_req_last_ttl_ = 0;
00063 
00064         rt_nblist_ = 0;;
00065         next_ = 0;
00066         prev_ = 0;
00067 }
00068 
00069 Aodv_rt_entry::~Aodv_rt_entry()
00070 {
00071         AddrList *cur = rt_nblist_;
00072         while(cur)
00073         {
00074                 AddrList *tmp = cur;
00075                 cur = cur->next;
00076                 delete tmp;
00077         }
00078         cur = rt_pclist_;
00079         while(cur)
00080         {
00081                 AddrList *tmp = cur;
00082                 cur = cur->next;
00083                 delete tmp;
00084         }
00085 }
00086 
00087 void Aodv_rt_entry::nb_insert(char *addr)
00088 {
00089         AddrList *a = new AddrList;
00090         MrclAddress::storeAddr(a->addr, addr);
00091         a->expire = 0;
00092         a->next = rt_nblist_;
00093         a->prev = 0;
00094         if(rt_nblist_)
00095                 rt_nblist_->prev = a;
00096         rt_nblist_ = a;
00097 }
00098 
00099 AddrList *Aodv_rt_entry::nb_lookup(char *addr)
00100 {
00101         for(AddrList *cur = rt_nblist_; cur; cur = cur->next)
00102         {
00103                 if(MrclAddress::areEqual(addr, cur->addr))
00104                         return cur;
00105         }
00106         return 0;
00107 }
00108 
00109 void Aodv_rt_entry::pc_insert(char *addr)
00110 {
00111         AddrList *a = new AddrList;
00112         MrclAddress::storeAddr(a->addr, addr);
00113         a->expire = 0;
00114         a->next = rt_pclist_;
00115         a->prev = 0;
00116         if(rt_pclist_)
00117                 rt_pclist_->prev = a;
00118         rt_pclist_ = a;
00119         char add[20];
00120         int ip;
00121         memcpy(&ip, a->addr + sizeof(int), sizeof(int));
00122         sprintf(add,"%d.%d.%d.%d", (ip & 0xff000000)>>24,(ip & 0x00ff0000)>>16, (ip & 0x0000ff00)>>8, (ip & 0x000000ff));
00123 }
00124 
00125 AddrList *Aodv_rt_entry::pc_lookup(char *addr)
00126 {
00127         for(AddrList *cur = rt_pclist_; cur; cur = cur->next)
00128         {
00129                 if(MrclAddress::areEqual(addr, cur->addr))
00130                         return cur;
00131         }
00132         return 0;
00133 }
00134 
00135 void Aodv_rt_entry::pc_delete(char *addr)
00136 {
00137         AddrList *a = pc_lookup(addr);
00138         if(!a)
00139                 return;
00140         if(a == rt_pclist_)
00141         {
00142                 rt_pclist_ = a->next;
00143                 if(rt_pclist_)
00144                         rt_pclist_->prev = 0;
00145         }
00146         else
00147         {
00148                 a->prev->next = a->next;
00149                 if(a->next)
00150                         a->next->prev = a->prev;
00151         }
00152         delete a;
00153 }
00154 
00155 void Aodv_rt_entry::pc_show()
00156 {
00157         printf("Current Address list:\n");
00158         AddrList *cur = rt_pclist_;
00159         char addr[20];
00160         int ip;
00161         while(cur)
00162         {
00163                 memcpy(&ip, cur->addr + sizeof(int), sizeof(int));
00164                 sprintf(addr,"%d.%d.%d.%d", (ip & 0xff000000)>>24,(ip & 0x00ff0000)>>16, (ip & 0x0000ff00)>>8, (ip & 0x000000ff));
00165                 printf("\t%s id %d will expire at %f next %p prev %p this %p\n", addr, cur->id, cur->expire, cur->next, cur->prev, cur);
00166                 cur = cur->next;
00167         }
00168 }
00169 
00170 
00171 void Aodv_rt_entry::pc_delete()
00172 {
00173         AddrList *cur = rt_pclist_;
00174         while(cur)
00175         {
00176                 AddrList *tmp = cur;
00177                 cur = cur->next;
00178                 delete tmp;
00179         }
00180         rt_pclist_ = 0;
00181 }
00182 
00183 int Aodv_rt_entry::pc_empty()
00184 {
00185         return (rt_pclist_ == 0);
00186 }
00187 
00188 Aodv_rt_entry *Aodv_rt_entry::getNext()
00189 {
00190         return next_;
00191 }
00192 
00193 Aodv_rt_entry *Aodv_rt_entry::getPrev()
00194 {
00195         return prev_;
00196 }
00197 
00198 void Aodv_rt_entry::setNext(Aodv_rt_entry *e)
00199 {
00200         next_ = e;
00201 }
00202 
00203 void Aodv_rt_entry::setPrev(Aodv_rt_entry *e)
00204 {
00205         prev_ = e;
00206 }
00207 
00208 void Aodv_rt_entry::setDst(char *addr)
00209 {
00210         MrclAddress::storeAddr(rt_dst_, addr);
00211 }
00212 
00213 char *Aodv_rt_entry::getDst()
00214 {
00215         return rt_dst_;
00216 }
00217 
00218 void Aodv_rt_entry::setSeqno(int sn)
00219 {
00220         rt_seqno_ = sn;
00221 }
00222 
00223 void Aodv_rt_entry::incrSeqno()
00224 {
00225         rt_seqno_++;
00226 }
00227 
00228 
00229 int Aodv_rt_entry::getSeqno()
00230 {
00231         return rt_seqno_;
00232 }
00233 
00234 void Aodv_rt_entry::setHops(int h)
00235 {
00236         rt_hops_ = h;
00237 }
00238 
00239 int Aodv_rt_entry::getHops()
00240 {
00241         return rt_hops_;
00242 }
00243 
00244 void Aodv_rt_entry::setLastHopCount(int lh)
00245 {
00246         rt_last_hop_count_ = lh;
00247 }
00248 
00249 int Aodv_rt_entry::getLastHopCount()
00250 {
00251         return rt_last_hop_count_;
00252 }
00253 
00254 void Aodv_rt_entry::setNexthop(char *addr)
00255 {
00256         MrclAddress::storeAddr(nexthop_, addr);
00257 }
00258 
00259 void Aodv_rt_entry::resetNexthop()
00260 {
00261         memset(nexthop_, 0, MRCL_ADDRESS_MAX_LEN);
00262 }
00263 
00264 char *Aodv_rt_entry::getNexthop()
00265 {
00266         return nexthop_;
00267 }
00268 
00269 void Aodv_rt_entry::setExpire(double e)
00270 {
00271         expire_ = e;
00272 }
00273 
00274 double Aodv_rt_entry::getExpire()
00275 {
00276         return expire_;
00277 }
00278 
00279 void Aodv_rt_entry::setFlags(char f)
00280 {
00281         rt_flags_ = f;
00282 }
00283 
00284 char Aodv_rt_entry::getFlags()
00285 {
00286         return rt_flags_;
00287 }
00288 
00289 void Aodv_rt_entry::setReqLastTtl(int lt)
00290 {
00291         rt_req_last_ttl_ = lt;
00292 }
00293 
00294 int Aodv_rt_entry::getReqLastTtl()
00295 {
00296         return rt_req_last_ttl_;
00297 }
00298 
00299 void Aodv_rt_entry::setDiscLatency(int i, double val)
00300 {
00301         if(i < 0 || i >= MAX_HISTORY)
00302                 return;
00303         rt_disc_latency_[i] = val;
00304 }
00305 
00306 double Aodv_rt_entry::getDiscLatency(int i)
00307 {
00308         if(i < 0 || i >= MAX_HISTORY)
00309                 return 0.0;
00310         return rt_disc_latency_[i];
00311 }
00312 
00313 void Aodv_rt_entry::setHistIndx(char val)
00314 {
00315         hist_indx_ = val;
00316 }
00317 
00318 char Aodv_rt_entry::getHistIndx()
00319 {
00320         return hist_indx_;
00321 }
00322 
00323 
00324 /*---------------------------------------
00325  *                                      |
00326  *      Aodv_rtable                     |
00327  *                                      |
00328  ---------------------------------------*/
00329 
00330 Aodv_rtable::Aodv_rtable() : rt_head_(0)
00331 {
00332 }
00333 
00334 Aodv_rt_entry *Aodv_rtable::head()
00335 {
00336         return rt_head_;
00337 }
00338 
00339 Aodv_rt_entry *Aodv_rtable::rt_add(char *addr)
00340 {
00341         if(rt_lookup(addr) != 0)
00342         {
00343                 fprintf(stderr, "Aodv_rtable::rt_add error\n");
00344                 exit(1);
00345         }
00346         Aodv_rt_entry *e = new Aodv_rt_entry;
00347         e->setDst(addr);
00348         e->setNext(rt_head_);
00349         if(rt_head_)
00350                 rt_head_->setPrev(e);
00351         rt_head_ = e;
00352         return(e);
00353 }
00354 
00355 void Aodv_rtable::rt_delete(char *addr)
00356 {
00357         Aodv_rt_entry *e = rt_lookup(addr);
00358         if(!e)
00359                 return;
00360         if(e == rt_head_)
00361         {
00362                 rt_head_ = e->getNext();
00363                 if(rt_head_)
00364                         rt_head_->setPrev(0);
00365         }
00366         else
00367         {
00368                 e->getPrev()->setNext(e->getNext());
00369                 if(e->getNext())
00370                         e->getNext()->setPrev(e->getPrev());
00371         }
00372 }
00373 
00374 Aodv_rt_entry *Aodv_rtable::rt_lookup(char *addr)
00375 {
00376         for(Aodv_rt_entry *cur = rt_head_; cur; cur = cur->getNext())
00377         {
00378                 if(MrclAddress::areEqual(addr, cur->getDst()))
00379                         return cur;
00380         }
00381         return 0;
00382 }
00383 
00384 Aodv_rt_entry *Aodv_rtable::next(Aodv_rt_entry *addr)
00385 {
00386         for(Aodv_rt_entry *cur = rt_head_; cur; cur = cur->getNext())
00387         {
00388                 if(cur==addr)
00389                         return (cur->getNext());
00390         }
00391         return 0;
00392 }
00393 

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