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