## modules/up/src/Core/sys/Address.cc

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

## FUNCTIONS

This source file includes following functions.

1. Address
2. Address
3. Address
4. Address
5. set
6. get
7. name
8. inside

       1 //
       2 // $Id: Address.cc,v 1.1.1.1 2000/03/10 16:32:20 engin Exp $
       3 //
       4 // Address.cc
       5 // Author: Ramesh Govindan <govindan@isi.edu>
       6
       7 #ifdef HAVE_CONFIG_H
       8 #include <config.h>
       9 #endif
      10
      11 #include <cstdio>
      12 #include <cstdlib>
      13 #include <cerrno>
      14
      15 extern "C" {
      16 #include <string.h>
      17 #if NEED_MEMORY_H
      18 #include <memory.h>
      19 #endif // NEED_MEMORY_H
      20
      21 #include <sys/types.h>
      22 #include <sys/socket.h>
      23 #include <netinet/in.h>
      24 #include <arpa/inet.h>
      25 }
      26
      27 #include "util/Types.hh"
      28 #include "sys/Address.hh"
      29
      30 u_int masks[] ={ 0x00000000,
      31                  0x80000000, 0xC0000000, 0xE0000000, 0xF0000000,
      32                  0xF8000000, 0xFC000000, 0xFE000000, 0xFF000000,
      33                  0xFF800000, 0xFFC00000, 0xFFE00000, 0xFFF00000,
      34                  0xFFF80000, 0xFFFC0000, 0xFFFE0000, 0xFFFF0000,
      35                  0xFFFF8000, 0xFFFFC000, 0xFFFFE000, 0xFFFFF000,
      36                  0xFFFFF800, 0xFFFFFC00, 0xFFFFFE00, 0xFFFFFF00,
      37                  0xFFFFFF80, 0xFFFFFFC0, 0xFFFFFFE0, 0xFFFFFFF0,
      38                  0xFFFFFFF8, 0xFFFFFFFC, 0xFFFFFFFE, 0xFFFFFFFF
      39 };
      40
      41 // Operations on IP addresses. IP address are stored in the
      42 // address struct in host byte order, and only the name() operation
      43 // converts it into network byte order before printing it.
      44 //
      45
      46 Address::Address()
         /* [<][>][^][v][top][bottom][index][help] */
      47 {
      48     address= nameString[0]= 0;
      49 }
      50
      51 Address::Address(const char * const str) {
         /* [<][>][^][v][top][bottom][index][help] */
      52         address= ntohl(inet_addr(str));
      53         strcpy(nameString, str);
      54 }
      55
      56 Address::Address(U32 a) : address(a) {
         /* [<][>][^][v][top][bottom][index][help] */
      57         nameString[0]= 0;
      58 }
      59
      60 Address::Address(const Address& other) {
         /* [<][>][^][v][top][bottom][index][help] */
      61         address= other.address;
      62         memcpy(nameString, other.nameString, MaxAddressNameLength);
      63 }
      64
      65 Boolean
      66 Address::operator<(const Address& other) const
      67 {
      68     return (address < other.address);
      69 }
      70
      71 Boolean
      72 Address::operator==(const Address& other) const
      73 {
      74     return (address == other.address);
      75 }
      76
      77 void
      78 Address::operator=(const Address& other)
      79 {
      80     address = other.address;
      81     memcpy(nameString, other.nameString, MaxAddressNameLength);
      82 }
      83
      84 void
      85 Address::set(U32 addr)
         /* [<][>][^][v][top][bottom][index][help] */
      86 {
      87     address = addr;
      88     nameString[0]= 0;
      89 }
      90
      91 U32
      92 Address::get() const
         /* [<][>][^][v][top][bottom][index][help] */
      93 {
      94     return address;
      95 }
      96
      97 char*
      98 Address::name() const
         /* [<][>][^][v][top][bottom][index][help] */
      99 {
     100     if (!nameString[0]) {
     101         struct in_addr in;
     102
     103         in.s_addr = htonl(address);
     104         sprintf(nameString,
     105                 "%s",
     106                 ::inet_ntoa(in));
     107     }
     108     return nameString;
     109 }
     110
     111 bool Address::inside(Address &a, int len) {
         /* [<][>][^][v][top][bottom][index][help] */
     112    return (address & masks[len]) == (a.address & masks[len]);
     113 }
     114
     115 //
     116 //  Copyright (c) 1994 by the University of Southern California.
     117 //  All rights reserved.
     118 //
     119 //  Permission to use, copy, modify, and distribute this software and
     120 //  its documentation in source and binary forms for lawful
     121 //  non-commercial purposes and without fee is hereby granted, provided
     122 //  that the above copyright notice appear in all copies and that both
     123 //  the copyright notice and this permission notice appear in supporting
     124 //  documentation, and that any documentation, advertising materials,
     125 //  and other materials related to such distribution and use acknowledge
     126 //  that the software was developed by the University of Southern
     127 //  California and/or Information Sciences Institute.
     128 //  The name of the University of Southern California may not
     129 //  be used to endorse or promote products derived from this software
     130 //  without specific prior written permission.
     131 //
     132 //  THE UNIVERSITY OF SOUTHERN CALIFORNIA DOES NOT MAKE ANY REPRESENTATIONS
     133 //  ABOUT THE SUITABILITY OF THIS SOFTWARE FOR ANY PURPOSE.  THIS SOFTWARE IS
     134 //  PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
     135 //  INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
     136 //  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, TITLE, AND
     137 //  NON-INFRINGEMENT.
     138 //
     139 //  IN NO EVENT SHALL USC, OR ANY OTHER CONTRIBUTOR BE LIABLE FOR ANY
     140 //  SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES, WHETHER IN CONTRACT,
     141 //  TORT, OR OTHER FORM OF ACTION, ARISING OUT OF OR IN CONNECTION WITH,
     142 //  THE USE OR PERFORMANCE OF THIS SOFTWARE.
     143 //
     144 //  Questions concerning this software should be directed to
     145 //  info-ra@isi.edu.
     146 //

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