rtable.cc

Go to the documentation of this file.
00001 /* -*-  Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
00002 /*
00003  * Copyright (c) 1997 Regents of the University of California.
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. All advertising materials mentioning features or use of this software
00015  *    must display the following acknowledgement:
00016  *  This product includes software developed by the Computer Systems
00017  *  Engineering Group at Lawrence Berkeley Laboratory.
00018  * 4. Neither the name of the University nor of the Laboratory may be used
00019  *    to endorse or promote products derived from this software without
00020  *    specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00023  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00028  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00031  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00032  * SUCH DAMAGE.
00033  */
00034 /* Ported from CMU/Monarch's code, nov'98 -Padma.*/
00035 
00036 #include <string.h>
00037 #include <stdlib.h>
00038 #include <stdio.h>
00039 #include <assert.h>
00040 #include "rtable.h"
00041 
00042 static int rtent_trich(const void *a, const void *b) {
00043   nsaddr_t ia = ((const rtable_ent *) a)->dst;
00044   nsaddr_t ib = ((const rtable_ent *) b)->dst;
00045   if (ia > ib) return 1;
00046   if (ib > ia) return -1;
00047   return 0;
00048 }
00049 
00050 RoutingTable::RoutingTable() {
00051   // Let's start with a ten element maxelts.
00052   elts = 0;
00053   maxelts = 10;
00054   rtab = new rtable_ent[maxelts];
00055 }
00056 
00057 void
00058 RoutingTable::AddEntry(const rtable_ent &ent)
00059 {
00060   rtable_ent *it;
00061   //DEBUG
00062   assert(ent.metric <= BIG);
00063 
00064   if ((it = (rtable_ent*) bsearch(&ent, rtab, elts, sizeof(rtable_ent), 
00065                  rtent_trich))) {
00066     bcopy(&ent,it,sizeof(rtable_ent));
00067     return;
00068     /*
00069     if (it->seqnum < ent.seqnum || it->metric > (ent.metric+em)) {
00070       bcopy(&ent,it,sizeof(rtable_ent));
00071       it->metric += em;
00072       return NEW_ROUTE_SUCCESS_OLDENT;
00073     } else {
00074       return NEW_ROUTE_METRIC_TOO_HIGH;
00075     }
00076     */
00077   }
00078 
00079   if (elts == maxelts) {
00080     rtable_ent *tmp = rtab;
00081     maxelts *= 2;
00082     rtab = new rtable_ent[maxelts];
00083     bcopy(tmp, rtab, elts*sizeof(rtable_ent));
00084     delete tmp;
00085   }
00086   
00087   int max;
00088   
00089   for (max=0;max<elts;max++) {
00090       if (ent.dst < rtab[max].dst) {
00091           break;
00092       }
00093   }
00094   // Copy all the further out guys out yet another.
00095   // bcopy does not seem to quite work on sunos???
00096   
00097   //bcopy(&rtab[max], &rtab[max+1], sizeof(rtable_ent)*(elts-max));
00098   
00099   //if (elts) {
00100   int i = elts-1;
00101   while (i >= max) {
00102       rtab[i+1] = rtab[i];
00103       i--;
00104   }
00105   //}
00106   bcopy(&ent, &rtab[max], sizeof(rtable_ent));
00107   elts++;
00108 
00109   return;
00110 }
00111 
00112 void 
00113 RoutingTable::InitLoop() {
00114   ctr = 0;
00115 }
00116 
00117 rtable_ent *
00118 RoutingTable::NextLoop() {
00119   if (ctr >= elts)
00120     return 0;
00121 
00122   return &rtab[ctr++];
00123 }
00124 
00125 // Only valid (duh) as long as no new routes are added
00126 int 
00127 RoutingTable::RemainingLoop() {
00128   return elts-ctr;
00129 }
00130 
00131 rtable_ent *
00132 RoutingTable::GetEntry(nsaddr_t dest) {
00133   rtable_ent ent;
00134   ent.dst = dest;
00135   return (rtable_ent *) bsearch(&ent, rtab, elts, sizeof(rtable_ent), 
00136                 rtent_trich);
00137 }

Generated on Tue Mar 6 16:47:50 2007 for ns2 Network Simulator 2.29 by  doxygen 1.4.6