A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv4-address-helper.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 University of Washington
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18#ifndef IPV4_ADDRESS_HELPER_H
19#define IPV4_ADDRESS_HELPER_H
20
22
23#include "ns3/ipv4-address.h"
24#include "ns3/net-device-container.h"
25
26namespace ns3
27{
28
29/**
30 * \ingroup ipv4Helpers
31 *
32 * @brief A helper class to make life easier while doing simple IPv4 address
33 * assignment in scripts.
34 *
35 * This class is a very simple IPv4 address generator. You can think of it
36 * as a simple local number incrementer. It has no notion that IP addresses
37 * are part of a global address space. If you have a complicated address
38 * assignment situation you may want to look at the Ipv4AddressGenerator which
39 * does recognize that IP address and network number generation is part of a
40 * global problem. Ipv4AddressHelper is a simple class to make simple problems
41 * easy to handle.
42 *
43 * We do call into the global address generator to make sure that there are
44 * no duplicate addresses generated.
45 *
46 * @see Ipv4AddressGenerator
47 */
49{
50 public:
51 /**
52 * @brief Construct a helper class to make life easier while doing simple IPv4
53 * address assignment in scripts.
54 */
56
57 /**
58 * @brief Construct a helper class to make life easier while doing simple IPv4
59 * address assignment in scripts. This version sets the base and mask
60 * in the constructor
61 * @param network the network part
62 * @param mask the address mask
63 * @param base the host part to start from
64 */
65 Ipv4AddressHelper(Ipv4Address network, Ipv4Mask mask, Ipv4Address base = "0.0.0.1");
66
67 /**
68 * @brief Set the base network number, network mask and base address.
69 *
70 * The address helper allocates IP addresses based on a given network number
71 * and mask combination along with an initial IP address.
72 *
73 * For example, if you want to use a /24 prefix with an initial network number
74 * of 192.168.1 (corresponding to a mask of 255.255.255.0) and you want to
75 * start allocating IP addresses out of that network beginning at 192.168.1.3,
76 * you would call
77 *
78 * SetBase ("192.168.1.0", "255.255.255.0", "0.0.0.3");
79 *
80 * If you don't care about the initial address it defaults to "0.0.0.1" in
81 * which case you can simply use,
82 *
83 * SetBase ("192.168.1.0", "255.255.255.0");
84 *
85 * and the first address generated will be 192.168.1.1.
86 *
87 * @param network The Ipv4Address containing the initial network number to
88 * use during allocation. The bits outside the network mask are not used.
89 * @param mask The Ipv4Mask containing one bits in each bit position of the
90 * network number.
91 * @param base An optional Ipv4Address containing the initial address used for
92 * IP address allocation. Will be combined (ORed) with the network number to
93 * generate the first IP address. Defaults to 0.0.0.1.
94 */
95 void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base = "0.0.0.1");
96
97 /**
98 * @brief Increment the network number and reset the IP address counter to
99 * the base value provided in the SetBase method.
100 *
101 * The address helper allocates IP addresses based on a given network number
102 * and initial IP address. In order to separate the network number and IP
103 * address parts, SetBase was given an initial network number value, a network
104 * mask and an initial address base.
105 *
106 * This method increments the network number and resets the IP address
107 * counter to the last base value used. For example, if the network number was
108 * set to 192.168.0.0 with a mask of 255.255.255.0 and a base address of
109 * 0.0.0.3 in the SetBase call; a call to NewNetwork will increment the
110 * network number counter resulting in network numbers incrementing as
111 * 192.168.1.0, 192.168.2.0, etc. After each network number increment, the
112 * IP address counter is reset to the initial value specified in SetBase. In
113 * this case, that would be 0.0.0.3. so if you were to call NewAddress after
114 * the increment that resulted in a network number of 192.168.2.0, the
115 * allocated addresses returned by NewAddress would be 192.168.2.3,
116 * 192.168.2.4, etc.
117 *
118 * @returns The value of the incremented network number that will be used in
119 * following address allocations.
120 * @see SetBase
121 * @see NewAddress
122 */
124
125 /**
126 * @brief Increment the IP address counter used to allocate IP addresses
127 *
128 * The address helper allocates IP addresses based on a given network number
129 * and initial IP address. In order to separate the network number and IP
130 * address parts, SetBase was given an initial network number value, a network
131 * mask and an initial address base.
132 *
133 * This method increments IP address counter. A check is made to ensure that
134 * the address returned will not overflow the number of bits allocated to IP
135 * addresses in SetBase (the number of address bits is defined by the number
136 * of mask bits that are not '1').
137 *
138 * For example, if the network number was set to 192.168.0.0 with a mask of
139 * 255.255.255.0 and a base address of 0.0.0.3 in SetBase, the next call to
140 * NewAddress will return 192.168.1.3. The NewAddress method
141 * has post-increment semantics. A following NewAddress would return
142 * 192.168.0.4, etc., until the 253rd call which would assert due to an address
143 * overflow.
144 *
145 * @returns The value of the newly allocated IP address.
146 * @see SetBase
147 * @see NewNetwork
148 */
150
151 /**
152 * @brief Assign IP addresses to the net devices specified in the container
153 * based on the current network prefix and address base.
154 *
155 * The address helper allocates IP addresses based on a given network number
156 * and initial IP address. In order to separate the network number and IP
157 * address parts, SetBase was given an initial value and a network mask.
158 * The one bits of this mask define the prefix category from which the helper
159 * will allocate new network numbers. An initial value for the network
160 * numbers was provided in the base parameter of the SetBase method in the
161 * bits corresponding to positions in the mask that were 1. An initial value
162 * for the IP address counter was also provided in the base parameter in the
163 * bits corresponding to positions in the mask that were 0.
164 *
165 * This method gets new addresses for each net device in the container. For
166 * each net device in the container, the helper finds the associated node and
167 * looks up the Ipv4 interface corresponding to the net device. It then sets
168 * the Ipv4Address and mask in the interface to the appropriate values. If
169 * the addresses overflow the number of bits allocated for them by the network
170 * mask in the SetBase method, the system will NS_ASSERT and halt.
171 *
172 * @param c The NetDeviceContainer holding the collection of net devices we
173 * are asked to assign Ipv4 addresses to.
174 *
175 * @returns A container holding the added NetDevices
176 * @see SetBase
177 * @see NewNetwork
178 */
180
181 private:
182 /**
183 * \brief Returns the number of address bits (hostpart) for a given netmask
184 * \param maskbits the netmask
185 * \returns the number of bits in the hostpart
186 */
187 uint32_t NumAddressBits(uint32_t maskbits) const;
188
189 uint32_t m_network; //!< network address
190 uint32_t m_mask; //!< network mask
191 uint32_t m_address; //!< address
192 uint32_t m_base; //!< base address
193 uint32_t m_shift; //!< shift, equivalent to the number of bits in the hostpart
194 uint32_t m_max; //!< maximum allowed address
195};
196
197} // namespace ns3
198
199#endif /* IPV4_ADDRESS_HELPER_H */
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
uint32_t m_shift
shift, equivalent to the number of bits in the hostpart
uint32_t m_mask
network mask
uint32_t m_network
network address
uint32_t m_base
base address
Ipv4Address NewAddress()
Increment the IP address counter used to allocate IP addresses.
uint32_t m_max
maximum allowed address
Ipv4AddressHelper()
Construct a helper class to make life easier while doing simple IPv4 address assignment in scripts.
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.
Ipv4Address NewNetwork()
Increment the network number and reset the IP address counter to the base value provided in the SetBa...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
holds a vector of std::pair of Ptr<Ipv4> and interface index.
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:257
holds a vector of ns3::NetDevice pointers
Every class exported by the ns3 library is enclosed in the ns3 namespace.