## modules/up/src/Core/network/Prefix.cc

/* [<][>][^][v][top][bottom][index][help] */

## FUNCTIONS

This source file includes following functions.

1. Prefix
2. Prefix
3. Prefix
4. Prefix
5. Prefix
6. mask
7. numHosts
8. name
9. randomHost
10. buddy
11. neighbor
12. match

       1 //
       2 // $Id: Prefix.cc,v 1.1.1.1 2000/03/10 16:32:19 engin Exp $
       3 //
       4 // Author(s): Ramesh Govindan
       5
       6 #ifdef HAVE_CONFIG_H
       7 #include <config.h>
       8 #endif
       9
      10 extern "C" {
      11 #include <string.h>
      12 #if NEED_MEMORY_H
      13 #include <memory.h>
      14 #endif // NEED_MEMORY_H
      15
      16 #include <sys/types.h>
      17 #include <sys/socket.h>
      18 #include <netinet/in.h>
      19 #include <arpa/inet.h>
      20 }
      21
      22 #include "gnu/ACG.h"
      23 #include "gnu/RndInt.h"
      24 #include "util/Types.hh"
      25 #include "util/Trail.hh"
      26 #include "util/Handler.hh"
      27 #include "util/Buffer.hh"
      28 #include "sys/Address.hh"
      29 #include "network/Prefix.hh"
      30 #include "sched/Dispatcher.hh"
      31
      32 // Constants
      33 static const    int     maxSignedInt = 0x7fffffff;
      34
      35 // File locals
      36 ACG             *acg = NULL;
      37 RandomInteger   *rndInt = NULL;
      38
      39 Prefix::Prefix()
         /* [<][>][^][v][top][bottom][index][help] */
      40 {
      41     address = 0;
      42     maskLength = 0;
      43     (void) memset(nameString, 0, MaxPrefixNameLength);
      44 }
      45
      46 Prefix::Prefix(Address& a)
         /* [<][>][^][v][top][bottom][index][help] */
      47 {
      48     address = a.get();
      49     maskLength = 32;
      50     (void) memset(nameString, 0, MaxPrefixNameLength);
      51 }
      52
      53 Prefix::Prefix(Address& a,
         /* [<][>][^][v][top][bottom][index][help] */
      54                int l)
      55 {
      56     U32         baddr;
      57
      58     ASSERT((l >= 0) && (l <= 30));      // Note upper bound on length
      59
      60     address = a.get();
      61     maskLength = l;
      62     (void) memset(nameString, 0, MaxPrefixNameLength);
      63
      64     address &= mask();
      65     return;
      66 }
      67
      68 Prefix::Prefix(Prefix& p)
         /* [<][>][^][v][top][bottom][index][help] */
      69 {
      70     address = p.address;
      71     maskLength = p.maskLength;
      72     return;
      73 }
      74
      75 Prefix::~Prefix()
         /* [<][>][^][v][top][bottom][index][help] */
      76 {
      77     // Empty
      78 }
      79
      80 void
      81 Prefix::operator--(int j)
      82 {
      83     ASSERT(j < sizeof(address) * 8);    // NBBY?
      84     maskLength -= 1;
      85     address &= mask();
      86     return;
      87 }
      88
      89 Boolean
      90 Prefix::operator==(Prefix& pfx)
      91 {
      92     return ((maskLength == pfx.maskLength) &&
      93             (address == pfx.address));
      94 }
      95
      96 U32
      97 Prefix::mask()
         /* [<][>][^][v][top][bottom][index][help] */
      98 {
      99     return ~((0x1 << (32 - maskLength)) - 1);
     100 }
     101
     102 unsigned int
     103 Prefix::numHosts()
         /* [<][>][^][v][top][bottom][index][help] */
     104 {
     105     return ((0x1 << (32 - maskLength)) - 1);
     106 }
     107
     108 char*
     109 Prefix::name()
         /* [<][>][^][v][top][bottom][index][help] */
     110 {
     111     struct in_addr      in;
     112
     113     if (!nameString[0]) {
     114         in.s_addr = htonl(address);
     115         sprintf(nameString, "%s/%d", ::inet_ntoa(in), maskLength);
     116     }
     117     return nameString;
     118 }
     119
     120 void
     121 Prefix::randomHost(Address& addr)
         /* [<][>][^][v][top][bottom][index][help] */
     122 {
     123     unsigned int        hostNum;
     124
     125     if (acg == NULL) {
     126         dispatcher.systemClock.sync();
     127         acg = new ACG(dispatcher.systemClock.seconds());
     128         rndInt = new RandomInteger(0, maxSignedInt, acg);
     129     }
     130
     131     while (!(hostNum = rndInt->asLong(numHosts()))) {
     132         // Empty
     133     }
     134     addr.set(address + hostNum);
     135     return;
     136 }
     137
     138 void
     139 Prefix::buddy()
         /* [<][>][^][v][top][bottom][index][help] */
     140 {
     141     U32         base;
     142
     143     base = (address >> (32 - maskLength));
     144     if (base & 0x1) {
     145         base -= 1;
     146     } else {
     147         base += 1;
     148     }
     149     address = (base << (32 - maskLength));
     150     return;
     151 }
     152
     153 void
     154 Prefix::neighbor()
         /* [<][>][^][v][top][bottom][index][help] */
     155 {
     156     U32         base;
     157
     158     base = (address >> (32 - maskLength));
     159     if (base & 0x1) {
     160         base += 1;
     161     } else {
     162         base -= 1;
     163     }
     164     address = (base << (32 - maskLength));      // XXX: wrap around?
     165     return;
     166 }
     167
     168 Boolean
     169 Prefix::match(Address& a)
         /* [<][>][^][v][top][bottom][index][help] */
     170 {
     171     return ((a.get() & mask()) == address);
     172 }
     173
     174 //  Copyright (c) 1994 by the University of Southern California.
     175 //  All rights reserved.
     176 //
     177 //  Permission to use, copy, modify, and distribute this software and
     178 //  its documentation in source and binary forms for lawful
     179 //  non-commercial purposes and without fee is hereby granted, provided
     180 //  that the above copyright notice appear in all copies and that both
     181 //  the copyright notice and this permission notice appear in supporting
     182 //  documentation, and that any documentation, advertising materials,
     183 //  and other materials related to such distribution and use acknowledge
     184 //  that the software was developed by the University of Southern
     185 //  California and/or Information Sciences Institute.
     186 //  The name of the University of Southern California may not
     187 //  be used to endorse or promote products derived from this software
     188 //  without specific prior written permission.
     189 //
     190 //  THE UNIVERSITY OF SOUTHERN CALIFORNIA DOES NOT MAKE ANY REPRESENTATIONS
     191 //  ABOUT THE SUITABILITY OF THIS SOFTWARE FOR ANY PURPOSE.  THIS SOFTWARE IS
     192 //  PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
     193 //  INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
     194 //  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, TITLE, AND
     195 //  NON-INFRINGEMENT.
     196 //
     197 //  IN NO EVENT SHALL USC, OR ANY OTHER CONTRIBUTOR BE LIABLE FOR ANY
     198 //  SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES, WHETHER IN CONTRACT,
     199 //  TORT, OR OTHER FORM OF ACTION, ARISING OUT OF OR IN CONNECTION WITH,
     200 //  THE USE OR PERFORMANCE OF THIS SOFTWARE.
     201 //
     202 //  Questions concerning this software should be directed to
     203 //  scan@isi.edu.
     204 //
     205
     206

/* [<][>][^][v][top][bottom][index][help] */