A Discrete-Event Network Simulator
API
ipv4-address-helper.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 University of Washington
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18 
19 #include "ns3/assert.h"
20 #include "ns3/log.h"
21 #include "ns3/ptr.h"
22 #include "ns3/node.h"
23 #include "ns3/net-device.h"
24 #include "ns3/loopback-net-device.h"
25 #include "ns3/ipv4.h"
26 #include "ns3/ipv4-address-generator.h"
27 #include "ns3/simulator.h"
28 #include "ns3/traffic-control-helper.h"
29 #include "ns3/traffic-control-layer.h"
30 #include "ipv4-address-helper.h"
31 
32 namespace ns3 {
33 
34 NS_LOG_COMPONENT_DEFINE ("Ipv4AddressHelper");
35 
37 {
39 
40 //
41 // Set the default values to an illegal state. Do this so the client is
42 // forced to think at least briefly about what addresses get used and what
43 // is going on here.
44 //
45  m_network = 0xffffffff;
46  m_mask = 0;
47  m_address = 0xffffffff;
48  m_base = 0xffffffff;
49  m_shift = 0xffffffff;
50  m_max = 0xffffffff;
51 }
52 
54  const Ipv4Address network,
55  const Ipv4Mask mask,
56  const Ipv4Address address)
57 {
59  SetBase (network, mask, address);
60 }
61 
62 void
64  const Ipv4Address network,
65  const Ipv4Mask mask,
66  const Ipv4Address address)
67 {
69 
70  m_network = network.Get ();
71  m_mask = mask.Get ();
72  m_base = m_address = address.Get ();
73 
74 //
75 // Some quick reasonableness testing.
76 //
77  NS_ASSERT_MSG ((m_network & ~m_mask) == 0,
78  "Ipv4AddressHelper::SetBase(): Inconsistent network and mask");
79 
80 //
81 // Figure out how much to shift network numbers to get them aligned, and what
82 // the maximum allowed address is with respect to the current mask.
83 //
85  m_max = (1 << m_shift) - 2;
86 
87  NS_ASSERT_MSG (m_shift <= 32,
88  "Ipv4AddressHelper::SetBase(): Unreasonable address length");
89 
90 //
91 // Shift the network down into the normalized position.
92 //
93  m_network >>= m_shift;
94 
95  NS_LOG_LOGIC ("m_network == " << m_network);
96  NS_LOG_LOGIC ("m_mask == " << m_mask);
97  NS_LOG_LOGIC ("m_address == " << m_address);
98 }
99 
102 {
103 //
104 // The way this is expected to be used is that an address and network number
105 // are initialized, and then NewAddress() is called repeatedly to allocate and
106 // get new addresses on a given subnet. The client will expect that the first
107 // address she gets back is the one she used to initialize the generator with.
108 // This implies that this operation is a post-increment.
109 //
111  "Ipv4AddressHelper::NewAddress(): Address overflow");
112 
113  Ipv4Address addr ((m_network << m_shift) | m_address);
114  ++m_address;
115 //
116 // The Ipv4AddressGenerator allows us to keep track of the addresses we have
117 // allocated and will assert if we accidentally generate a duplicate. This
118 // avoids some really hard to debug problems.
119 //
121  return addr;
122 }
123 
126 {
128  ++m_network;
129  m_address = m_base;
130  return Ipv4Address (m_network << m_shift);
131 }
132 
135 {
137  Ipv4InterfaceContainer retval;
138  for (uint32_t i = 0; i < c.GetN (); ++i) {
139  Ptr<NetDevice> device = c.Get (i);
140 
141  Ptr<Node> node = device->GetNode ();
142  NS_ASSERT_MSG (node, "Ipv4AddressHelper::Assign(): NetDevice is not not associated "
143  "with any node -> fail");
144 
145  Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
146  NS_ASSERT_MSG (ipv4, "Ipv4AddressHelper::Assign(): NetDevice is associated"
147  " with a node without IPv4 stack installed -> fail "
148  "(maybe need to use InternetStackHelper?)");
149 
150  int32_t interface = ipv4->GetInterfaceForDevice (device);
151  if (interface == -1)
152  {
153  interface = ipv4->AddInterface (device);
154  }
155  NS_ASSERT_MSG (interface >= 0, "Ipv4AddressHelper::Assign(): "
156  "Interface index not found");
157 
159  ipv4->AddAddress (interface, ipv4Addr);
160  ipv4->SetMetric (interface, 1);
161  ipv4->SetUp (interface);
162  retval.Add (ipv4, interface);
163 
164  // Install the default traffic control configuration if the traffic
165  // control layer has been aggregated, if this is not
166  // a loopback interface, and there is no queue disc installed already
168  if (tc && DynamicCast<LoopbackNetDevice> (device) == 0 && tc->GetRootQueueDiscOnDevice (device) == 0)
169  {
170  NS_LOG_LOGIC ("Installing default traffic control configuration");
172  tcHelper.Install (device);
173  }
174  }
175  return retval;
176 }
177 
178 const uint32_t N_BITS = 32;
179 
180 uint32_t
181 Ipv4AddressHelper::NumAddressBits (uint32_t maskbits) const
182 {
184  for (uint32_t i = 0; i < N_BITS; ++i)
185  {
186  if (maskbits & 1)
187  {
188  NS_LOG_LOGIC ("NumAddressBits -> " << i);
189  return i;
190  }
191  maskbits >>= 1;
192  }
193 
194  NS_ASSERT_MSG (false, "Ipv4AddressHelper::NumAddressBits(): Bad Mask");
195  return 0;
196 }
197 
198 } // namespace ns3
199 
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
uint32_t m_max
maximum allowed address
QueueDiscContainer Install(NetDeviceContainer c)
holds a vector of std::pair of Ptr<Ipv4> and interface index.
NS_ASSERT_MSG(false, "Ipv4AddressGenerator::MaskToIndex(): Impossible")
Introspection did not find any typical Config paths.
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:258
uint32_t m_mask
network mask
void Add(const Ipv4InterfaceContainer &other)
Concatenate the entries in the other container with ours.
uint32_t m_base
base address
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
uint32_t m_shift
shift, equivalent to the number of bits in the hostpart
static bool AddAllocated(const Ipv4Address addr)
Add the Ipv4Address to the list of IPv4 entries.
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Ipv4Address NewAddress(void)
Increment the IP address counter used to allocate IP addresses.
Ipv4AddressHelper()
Construct a helper class to make life easier while doing simple IPv4 address assignment in scripts...
const uint32_t N_BITS
number of bits in a IPv4 address
holds a vector of ns3::NetDevice pointers
uint32_t m_network
network address
Build a set of QueueDisc objects.
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:76
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
Every class exported by the ns3 library is enclosed in the ns3 namespace.
address
Definition: first.py:37
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
uint32_t NumAddressBits(uint32_t maskbits) const
Returns the number of address bits (hostpart) for a given netmask.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
uint32_t Get(void) const
Get the host-order 32-bit IP mask.
a class to store IPv4 address information on an interface
uint32_t GetN(void) const
Get the number of Ptr<NetDevice> stored in this container.
Ipv4Address NewNetwork(void)
Increment the network number and reset the IP address counter to the base value provided in the SetBa...
static TrafficControlHelper Default(void)
uint32_t Get(void) const
Get the host-order 32-bit IP address.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.