src/node/ipv4-address.cc
changeset 234 6124bda39cb3
child 256 8978ff0dc351
equal deleted inserted replaced
233:6b60d7b27ae4 234:6124bda39cb3
       
     1 /* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
       
     2 /*
       
     3  * Copyright (c) 2005 INRIA
       
     4  * All rights reserved.
       
     5  *
       
     6  * This program is free software; you can redistribute it and/or modify
       
     7  * it under the terms of the GNU General Public License version 2 as
       
     8  * published by the Free Software Foundation;
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program; if not, write to the Free Software
       
    17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    18  *
       
    19  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
       
    20  */
       
    21 
       
    22 #include "ipv4-address.h"
       
    23 
       
    24 namespace ns3 {
       
    25 
       
    26 #define ASCII_DOT (0x2e)
       
    27 #define ASCII_ZERO (0x30)
       
    28 
       
    29 static uint32_t 
       
    30 AsciiToIpv4Host (char const *address)
       
    31 {
       
    32   uint32_t host = 0;
       
    33   while (true) {
       
    34     uint8_t byte = 0;
       
    35     while (*address != ASCII_DOT &&
       
    36            *address != 0) {
       
    37       byte *= 10;
       
    38       byte += *address - ASCII_ZERO;
       
    39       address++;
       
    40     }
       
    41     host <<= 8;
       
    42     host |= byte;
       
    43     if (*address == 0) {
       
    44       break;
       
    45     }
       
    46     address++;
       
    47   }
       
    48   return host;
       
    49 }
       
    50 
       
    51 }//namespace ns3
       
    52 
       
    53 namespace ns3 {
       
    54 
       
    55 Ipv4Mask::Ipv4Mask ()
       
    56   : m_mask (0x66666666)
       
    57 {}
       
    58 
       
    59 Ipv4Mask::Ipv4Mask (uint32_t mask)
       
    60   : m_mask (mask)
       
    61 {}
       
    62 Ipv4Mask::Ipv4Mask (char const *mask)
       
    63 {
       
    64   m_mask = AsciiToIpv4Host (mask);
       
    65 }
       
    66 
       
    67 bool 
       
    68 Ipv4Mask::IsEqual (Ipv4Mask other) const
       
    69 {
       
    70   if (other.m_mask == m_mask) {
       
    71     return true;
       
    72   } else {
       
    73     return false;
       
    74   }
       
    75 }
       
    76 
       
    77 
       
    78 bool 
       
    79 Ipv4Mask::IsMatch (Ipv4Address a, Ipv4Address b) const
       
    80 {
       
    81   if ((a.GetHostOrder () & m_mask) == (b.GetHostOrder () & m_mask)) {
       
    82     return true;
       
    83   } else {
       
    84     return false;
       
    85   }
       
    86 }
       
    87 
       
    88 uint32_t 
       
    89 Ipv4Mask::GetHostOrder (void) const
       
    90 {
       
    91   return m_mask;
       
    92 }
       
    93 void 
       
    94 Ipv4Mask::SetHostOrder (uint32_t value)
       
    95 {
       
    96   m_mask = value;
       
    97 }
       
    98 
       
    99 void 
       
   100 Ipv4Mask::Print (std::ostream *os) const
       
   101 {
       
   102   *os << ((m_mask >> 24) & 0xff) << "."
       
   103       << ((m_mask >> 16) & 0xff) << "."
       
   104       << ((m_mask >> 8) & 0xff) << "."
       
   105       << ((m_mask >> 0) & 0xff);
       
   106 }
       
   107 
       
   108 
       
   109 Ipv4Mask
       
   110 Ipv4Mask::GetLoopback (void)
       
   111 {
       
   112   static Ipv4Mask loopback = Ipv4Mask ("255.0.0.0");
       
   113   return loopback;
       
   114 }
       
   115 Ipv4Mask
       
   116 Ipv4Mask::GetZero (void)
       
   117 {
       
   118   static Ipv4Mask zero = Ipv4Mask ("0.0.0.0");
       
   119   return zero;
       
   120 }
       
   121 
       
   122 Ipv4Address::Ipv4Address ()
       
   123   : m_address (0x66666666)
       
   124 {}
       
   125 Ipv4Address::Ipv4Address (uint32_t address)
       
   126 {
       
   127   m_address = address;
       
   128 }
       
   129 Ipv4Address::Ipv4Address (char const *address)
       
   130 {
       
   131   m_address = AsciiToIpv4Host (address);
       
   132 }
       
   133 
       
   134 bool 
       
   135 Ipv4Address::IsEqual (Ipv4Address other) const
       
   136 {
       
   137   if (other.m_address == m_address) {
       
   138     return true;
       
   139   } else {
       
   140     return false;
       
   141   }
       
   142 }
       
   143 
       
   144 bool 
       
   145 Ipv4Address::IsMulticast (void)
       
   146 {
       
   147   // XXX
       
   148   return false;
       
   149 }
       
   150 
       
   151 uint32_t
       
   152 Ipv4Address::GetHostOrder (void) const
       
   153 {
       
   154   return m_address;
       
   155 }
       
   156 void 
       
   157 Ipv4Address::SetHostOrder (uint32_t ip)
       
   158 {
       
   159   m_address = ip;
       
   160 }
       
   161 void
       
   162 Ipv4Address::Serialize (uint8_t buf[4]) const
       
   163 {
       
   164   buf[0] = (m_address >> 24) & 0xff;
       
   165   buf[1] = (m_address >> 16) & 0xff;
       
   166   buf[2] = (m_address >> 8) & 0xff;
       
   167   buf[3] = (m_address >> 0) & 0xff;
       
   168 }
       
   169 
       
   170 void 
       
   171 Ipv4Address::Print (std::ostream *os) const
       
   172 {
       
   173   *os << ((m_address >> 24) & 0xff) << "."
       
   174       << ((m_address >> 16) & 0xff) << "."
       
   175       << ((m_address >> 8) & 0xff) << "."
       
   176       << ((m_address >> 0) & 0xff);
       
   177 }
       
   178 
       
   179 
       
   180 
       
   181 Ipv4Address 
       
   182 Ipv4Address::GetZero (void)
       
   183 {
       
   184   static Ipv4Address zero ("0.0.0.0");
       
   185   return zero;
       
   186 }
       
   187 Ipv4Address 
       
   188 Ipv4Address::GetAny (void)
       
   189 {
       
   190   static Ipv4Address any ("0.0.0.0");
       
   191   return any;
       
   192 }
       
   193 Ipv4Address 
       
   194 Ipv4Address::GetBroadcast (void)
       
   195 {
       
   196   static Ipv4Address broadcast ("255.255.255.255");
       
   197   return broadcast;
       
   198 }
       
   199 Ipv4Address 
       
   200 Ipv4Address::GetLoopback (void)
       
   201 {
       
   202   Ipv4Address loopback ("127.0.0.1");
       
   203   return loopback;
       
   204 }
       
   205 
       
   206 bool operator == (Ipv4Address const &a, Ipv4Address const &b)
       
   207 {
       
   208   return a.IsEqual (b);
       
   209 }
       
   210 bool operator != (Ipv4Address const &a, Ipv4Address const &b)
       
   211 {
       
   212   return !a.IsEqual (b);
       
   213 }
       
   214 size_t Ipv4AddressHash::operator()(Ipv4Address const &x) const 
       
   215 { 
       
   216   return x.GetHostOrder ();
       
   217 }
       
   218 
       
   219 std::ostream& operator<< (std::ostream& os, Ipv4Address const& address)
       
   220 {
       
   221   address.Print (&os);
       
   222   return os;
       
   223 }
       
   224 std::ostream& operator<< (std::ostream& os, Ipv4Mask const& mask)
       
   225 {
       
   226   mask.Print (&os);
       
   227   return os;
       
   228 }
       
   229 
       
   230 
       
   231 }; // namespace ns3