A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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/ipv4.h"
25 #include "ns3/ipv4-address-generator.h"
26 #include "ns3/simulator.h"
27 #include "ipv4-address-helper.h"
28 
29 NS_LOG_COMPONENT_DEFINE ("Ipv4AddressHelper");
30 
31 namespace ns3 {
32 
34 {
36 
37 //
38 // Set the default values to an illegal state. Do this so the client is
39 // forced to think at least briefly about what addresses get used and what
40 // is going on here.
41 //
42  m_network = 0xffffffff;
43  m_mask = 0;
44  m_address = 0xffffffff;
45  m_base = 0xffffffff;
46  m_shift = 0xffffffff;
47  m_max = 0xffffffff;
48 }
49 
51  const Ipv4Address network,
52  const Ipv4Mask mask,
53  const Ipv4Address address)
54 {
56  SetBase (network, mask, address);
57 }
58 
59 void
61  const Ipv4Address network,
62  const Ipv4Mask mask,
63  const Ipv4Address address)
64 {
66 
67  m_network = network.Get ();
68  m_mask = mask.Get ();
69  m_base = m_address = address.Get ();
70 
71 //
72 // Some quick reasonableness testing.
73 //
74  NS_ASSERT_MSG ((m_network & ~m_mask) == 0,
75  "Ipv4AddressHelper::SetBase(): Inconsistent network and mask");
76 
77 //
78 // Figure out how much to shift network numbers to get them aligned, and what
79 // the maximum allowed address is with respect to the current mask.
80 //
82  m_max = (1 << m_shift) - 2;
83 
84  NS_ASSERT_MSG (m_shift <= 32,
85  "Ipv4AddressHelper::SetBase(): Unreasonable address length");
86 
87 //
88 // Shift the network down into the normalized position.
89 //
90  m_network >>= m_shift;
91 
92  NS_LOG_LOGIC ("m_network == " << m_network);
93  NS_LOG_LOGIC ("m_mask == " << m_mask);
94  NS_LOG_LOGIC ("m_address == " << m_address);
95 }
96 
99 {
100 //
101 // The way this is expected to be used is that an address and network number
102 // are initialized, and then NewAddress() is called repeatedly to allocate and
103 // get new addresses on a given subnet. The client will expect that the first
104 // address she gets back is the one she used to initialize the generator with.
105 // This implies that this operation is a post-increment.
106 //
108  "Ipv4AddressHelper::NewAddress(): Address overflow");
109 
110  Ipv4Address addr ((m_network << m_shift) | m_address);
111  ++m_address;
112 //
113 // The Ipv4AddressGenerator allows us to keep track of the addresses we have
114 // allocated and will assert if we accidentally generate a duplicate. This
115 // avoids some really hard to debug problems.
116 //
118  return addr;
119 }
120 
123 {
125  ++m_network;
126  m_address = m_base;
127  return Ipv4Address (m_network << m_shift);
128 }
129 
132 {
134  Ipv4InterfaceContainer retval;
135  for (uint32_t i = 0; i < c.GetN (); ++i) {
136  Ptr<NetDevice> device = c.Get (i);
137 
138  Ptr<Node> node = device->GetNode ();
139  NS_ASSERT_MSG (node, "Ipv4AddressHelper::Assign(): NetDevice is not not associated "
140  "with any node -> fail");
141 
142  Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
143  NS_ASSERT_MSG (ipv4, "Ipv4AddressHelper::Assign(): NetDevice is associated"
144  " with a node without IPv4 stack installed -> fail "
145  "(maybe need to use InternetStackHelper?)");
146 
147  int32_t interface = ipv4->GetInterfaceForDevice (device);
148  if (interface == -1)
149  {
150  interface = ipv4->AddInterface (device);
151  }
152  NS_ASSERT_MSG (interface >= 0, "Ipv4AddressHelper::Assign(): "
153  "Interface index not found");
154 
156  ipv4->AddAddress (interface, ipv4Addr);
157  ipv4->SetMetric (interface, 1);
158  ipv4->SetUp (interface);
159  retval.Add (ipv4, interface);
160  }
161  return retval;
162 }
163 
164 const uint32_t N_BITS = 32;
165 
166 uint32_t
167 Ipv4AddressHelper::NumAddressBits (uint32_t maskbits) const
168 {
170  for (uint32_t i = 0; i < N_BITS; ++i)
171  {
172  if (maskbits & 1)
173  {
174  NS_LOG_LOGIC ("NumAddressBits -> " << i);
175  return i;
176  }
177  maskbits >>= 1;
178  }
179 
180  NS_ASSERT_MSG (false, "Ipv4AddressHelper::NumAddressBits(): Bad Mask");
181  return 0;
182 }
183 
184 } // namespace ns3
185 
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
uint32_t m_max
maximum allowed address
holds a vector of std::pair of Ptr and interface index.
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:210
uint32_t m_mask
network mask
uint32_t m_base
base address
uint32_t m_shift
shift, equivalent to the number of bits in the hostpart
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition: log.h:309
Ipv4Address NewAddress(void)
Increment the IP address counter used to allocate IP addresses.
uint32_t GetN(void) const
Get the number of Ptr stored in this container.
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
uint32_t Get(void) const
Get the host-order 32-bit IP address.
holds a vector of ns3::NetDevice pointers
uint32_t m_network
network address
#define NS_LOG_LOGIC(msg)
Definition: log.h:368
Access to the Ipv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:75
static bool AddAllocated(const Ipv4Address addr)
Add the Ipv4Address to the list of IPv4 entries.
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
NS_LOG_COMPONENT_DEFINE("Ipv4AddressHelper")
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
a class to store IPv4 address information on an interface
Ipv4Address NewNetwork(void)
Increment the network number and reset the IP address counter to the base value provided in the SetBa...
uint32_t Get(void) const
Get the host-order 32-bit IP mask.
void Add(Ipv4InterfaceContainer other)
Concatenate the entries in the other container with ours.
tuple address
Definition: first.py:37
Ptr< T > GetObject(void) const
Definition: object.h:361
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
uint32_t NumAddressBits(uint32_t maskbits) const
Returns the number of address bits (hostpart) for a given netmask.