prototype address and network allocation
This commit is contained in:
177
tutorial/ipv4-address-extended.cc
Normal file
177
tutorial/ipv4-address-extended.cc
Normal file
@@ -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
|
||||
39
tutorial/ipv4-address-extended.h
Normal file
39
tutorial/ipv4-address-extended.h
Normal file
@@ -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 */
|
||||
57
tutorial/testipv4.cc
Normal file
57
tutorial/testipv4.cc
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -20,3 +20,7 @@ def build(bld):
|
||||
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']
|
||||
|
||||
Reference in New Issue
Block a user