prototype address and network allocation
authorCraig Dowell <craigdo@ee.washington.edu>
Fri, 05 Oct 2007 16:00:04 -0700
changeset 1835 ccd0fc4d9bf8
parent 1834 0b4a24131a7a
child 1836 618a749157e3
prototype address and network allocation
tutorial/ipv4-address-extended.cc
tutorial/ipv4-address-extended.h
tutorial/testipv4.cc
tutorial/wscript
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tutorial/ipv4-address-extended.cc	Fri Oct 05 16:00:04 2007 -0700
@@ -0,0 +1,177 @@
+/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2007 University of Washington
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include "ns3/assert.h"
+#include "ns3/log.h"
+#include "ns3/simulation-singleton.h"
+#include "ipv4-address-extended.h"
+
+NS_LOG_COMPONENT_DEFINE("Ipv4AddressEx");
+
+namespace ns3 {
+
+class Ipv4NetworkManager
+{
+public:
+  Ipv4NetworkManager ();
+  virtual ~Ipv4NetworkManager ();
+
+  Ipv4Address Allocate (const Ipv4Mask mask);
+
+private:
+  static const uint32_t N_BITS = 32;
+  class State
+  {
+  public:
+    uint32_t mask;
+    uint32_t network;
+  };
+  State m_state[N_BITS];
+};
+
+Ipv4NetworkManager::Ipv4NetworkManager () 
+{
+  NS_LOG_FUNCTION;
+
+  uint32_t mask = 0;
+
+  for (uint32_t i = 0; i < N_BITS; ++i)
+    {
+      m_state[i].mask = mask;
+      mask >>= 1;
+      mask |= 0x80000000;
+      m_state[i].network = 0;
+      NS_LOG_LOGIC ("m_state[" << i << "]");
+      NS_LOG_LOGIC ("mask = " << std::hex << m_state[i].mask);
+      NS_LOG_LOGIC ("network = " << std::hex << m_state[i].network);
+    }
+}
+
+Ipv4NetworkManager::~Ipv4NetworkManager ()
+{
+  NS_LOG_FUNCTION;
+}
+
+Ipv4Address
+Ipv4NetworkManager::Allocate (const Ipv4Mask mask)
+{
+  NS_LOG_FUNCTION;
+
+  uint32_t bits = mask.GetHostOrder ();
+
+  for (uint32_t i = 0; i < N_BITS; ++i)
+    {
+      if (bits & 1)
+        {
+          uint32_t nBits = N_BITS - i;
+          NS_ASSERT(nBits >= 0 && nBits < N_BITS);
+          ++m_state[nBits].network;
+          return Ipv4Address (m_state[nBits].network << i);
+        }
+      bits >>= 1;
+    }
+  NS_ASSERT_MSG(false, "Impossible");
+  return Ipv4Address (bits);
+}
+
+class Ipv4AddressManager
+{
+public:
+  Ipv4AddressManager ();
+  virtual ~Ipv4AddressManager ();
+
+  Ipv4Address Allocate (const Ipv4Mask mask, const Ipv4Address network);
+
+private:
+  static const uint32_t N_BITS = 32;
+  class State
+  {
+  public:
+    uint32_t mask;
+    uint32_t address;
+  };
+  State m_state[N_BITS];
+};
+
+Ipv4AddressManager::Ipv4AddressManager () 
+{
+  NS_LOG_FUNCTION;
+
+  uint32_t mask = 0;
+
+  for (uint32_t i = 0; i < N_BITS; ++i)
+    {
+      m_state[i].mask = mask;
+      mask >>= 1;
+      mask |= 0x80000000;
+      m_state[i].address = 0;
+      NS_LOG_LOGIC ("m_state[" << i << "]");
+      NS_LOG_LOGIC ("mask = " << std::hex << m_state[i].mask);
+      NS_LOG_LOGIC ("address = " << std::hex << m_state[i].address);
+    }
+}
+
+Ipv4AddressManager::~Ipv4AddressManager ()
+{
+  NS_LOG_FUNCTION;
+}
+
+Ipv4Address
+Ipv4AddressManager::Allocate (const Ipv4Mask mask, const Ipv4Address network)
+{
+  NS_LOG_FUNCTION;
+
+  uint32_t bits = mask.GetHostOrder ();
+  uint32_t net = network.GetHostOrder ();
+
+  for (uint32_t i = 0; i < N_BITS; ++i)
+    {
+      if (bits & 1)
+        {
+          uint32_t nBits = N_BITS - i;
+          NS_ASSERT(nBits >= 0 && nBits < N_BITS);
+          ++m_state[nBits].address;
+          NS_ASSERT_MSG((m_state[nBits].mask & m_state[nBits].address) == 0, 
+            "Ipv4AddressManager::Allocate(): Overflow");
+          return Ipv4Address (net | m_state[nBits].address);
+        }
+      bits >>= 1;
+    }
+  NS_ASSERT_MSG(false, "Impossible");
+  return Ipv4Address (bits);
+}
+
+Ipv4Address
+Ipv4AddressEx::AllocateAddress (const Ipv4Mask mask, const Ipv4Address network)
+{
+  NS_LOG_FUNCTION;
+
+  return SimulationSingleton<Ipv4AddressManager>::Get ()->
+    Allocate (mask, network);
+}
+
+Ipv4Address
+Ipv4AddressEx::AllocateNetwork (const Ipv4Mask mask)
+{
+  NS_LOG_FUNCTION;
+
+  return SimulationSingleton<Ipv4NetworkManager>::Get ()->
+    Allocate (mask);
+}
+
+}; // namespace ns3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tutorial/ipv4-address-extended.h	Fri Oct 05 16:00:04 2007 -0700
@@ -0,0 +1,39 @@
+/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2007 University of Washington
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#ifndef IPV4_ADDRESS_EXTENDED_H
+#define IPV4_ADDRESS_EXTENDED_H
+
+#include <stdint.h>
+#include <ostream>
+
+#include "ns3/ipv4-address.h"
+
+namespace ns3 {
+
+class Ipv4AddressEx : public Ipv4Address {
+public:
+  static Ipv4Address AllocateAddress (const Ipv4Mask mask, 
+    const Ipv4Address network);
+
+  static Ipv4Address AllocateNetwork (const Ipv4Mask mask);
+};
+
+}; // namespace ns3
+
+#endif /* IPV4_ADDRESS_EXTENDED_H */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tutorial/testipv4.cc	Fri Oct 05 16:00:04 2007 -0700
@@ -0,0 +1,57 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include "ns3/log.h"
+#include "ipv4-address-extended.h"
+
+NS_LOG_COMPONENT_DEFINE ("TestIpv4");
+
+using namespace ns3;
+
+int 
+main (int argc, char *argv[])
+{
+  LogComponentEnable ("TestIpv4", LOG_LEVEL_ALL);
+
+  NS_LOG_INFO ("Test Ipv4");
+
+  Ipv4Mask mask1 ("255.0.0.0");
+
+  for (uint32_t i = 0; i < 10; ++i)
+    {
+      Ipv4Address network = Ipv4AddressEx::AllocateNetwork (mask1);
+      Ipv4Address address = Ipv4AddressEx::AllocateAddress (mask1, network);
+      NS_LOG_INFO ("address = " << address);
+    }
+
+  Ipv4Mask mask2 ("255.255.0.0");
+
+  for (uint32_t i = 0; i < 10; ++i)
+    {
+      Ipv4Address network = Ipv4AddressEx::AllocateNetwork (mask2);
+      Ipv4Address address = Ipv4AddressEx::AllocateAddress (mask2, network);
+      NS_LOG_INFO ("address = " << address);
+    }
+
+  Ipv4Mask mask3 ("255.255.255.0");
+
+  for (uint32_t i = 0; i < 10; ++i)
+    {
+      Ipv4Address network = Ipv4AddressEx::AllocateNetwork (mask3);
+      Ipv4Address address = Ipv4AddressEx::AllocateAddress (mask3, network);
+      NS_LOG_INFO ("address = " << address);
+    }
+}
--- a/tutorial/wscript	Wed Oct 03 13:34:06 2007 -0700
+++ b/tutorial/wscript	Fri Oct 05 16:00:04 2007 -0700
@@ -20,3 +20,7 @@
     obj = bld.create_ns3_program('tutorial-naive-dumbbell',
         ['core'])
     obj.source = 'tutorial-naive-dumbbell.cc'
+
+    obj = bld.create_ns3_program('testipv4',
+        ['core'])
+    obj.source = ['testipv4.cc', 'ipv4-address-extended.cc']