00001 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ 00002 /* 00003 * Copyright (c) 2008 University of Washington 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License version 2 as 00007 * published by the Free Software Foundation; 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 */ 00018 00019 #ifndef IPV4_ADDRESS_HELPER_H 00020 #define IPV4_ADDRESS_HELPER_H 00021 00022 #include "ns3/ipv4-address.h" 00023 #include "net-device-container.h" 00024 #include "ipv4-interface-container.h" 00025 00026 namespace ns3 { 00027 00028 /** 00029 * @brief A helper class to make life easier while doing simple IPv4 address 00030 * assignment in scripts. 00031 * 00032 * This class is a very simple IPv4 address generator. You can think of it 00033 * as a simple local number incrementer. It has no notion that IP addresses 00034 * are part of a global address space. If you have a complicated address 00035 * assignment situation you may want to look at the Ipv4AddressGenerator which 00036 * does recognize that IP address and netowrk number generation is part of a 00037 * global problem. Ipv4AddressHelper is a simple class to make simple problems 00038 * easy to handle. 00039 * 00040 * We do call into the global address generator to make sure that there are 00041 * no duplicate addresses generated. 00042 * 00043 * @see Ipv4AddressGenerator 00044 */ 00045 class Ipv4AddressHelper 00046 { 00047 public: 00048 /** 00049 * @brief Construct a helper class to make life easier while doing simple IPv4 00050 * address assignment in scripts. 00051 */ 00052 Ipv4AddressHelper (); 00053 00054 /** 00055 * @brief Set the base network mumber, network mask and base address. 00056 * 00057 * The address helper allocates IP addresses based on a given network number 00058 * and mask combination along with an initial IP address. 00059 * 00060 * For example, if you want to use a /24 prefix with an initial network number 00061 * of 192.168.1 (corresponding to a mask of 255.255.255.0) and you want to 00062 * start allocating IP addresses out of that network beginning at 192.168.1.3, 00063 * you would call 00064 * 00065 * SetBase ("192.168.1.0", "255.255.255.0", "0.0.0.3"); 00066 * 00067 * If you don't care about the initial address it defaults to "0.0.0.1" in 00068 * which case you can simply use, 00069 * 00070 * SetBase ("192.168.1.0", "255.255.255.0"); 00071 * 00072 * and the first address generated will be 192.168.1.1. 00073 * 00074 * @param network The Ipv4Address containing the initial network number to 00075 * use during allocation. The bits outside the network mask are not used. 00076 * @param mask The Ipv4Mask containing one bits in each bit position of the 00077 * network number. 00078 * @param base An optional Ipv4Address containing the initial address used for 00079 * IP address alloction. Will be combined (ORed) with the network number to 00080 * generate the first IP address. Defaults to 0.0.0.1. 00081 * @returns Nothing. 00082 */ 00083 void SetBase (Ipv4Address network, Ipv4Mask mask, 00084 Ipv4Address base = "0.0.0.1"); 00085 00086 /** 00087 * @brief Increment the network number and reset the IP address counter to 00088 * the base value provided in the SetBase method. 00089 * 00090 * The address helper allocates IP addresses based on a given network number 00091 * and initial IP address. In order to separate the network number and IP 00092 * address parts, SetBase was given an initial network number value, a network 00093 * mask and an initial address base. 00094 * 00095 * This method increments the network number and resets the IP address 00096 * counter to the last base value used. For example, if the network number was 00097 * set to 192.168.0.0 with a mask of 255.255.255.0 and a base address of 00098 * 0.0.0.3 in the SetBase call; a call to NewNetwork will increment the 00099 * network number counter resulting in network numbers incrementing as 00100 * 192.168.1.0, 192.168.2.0, etc. After each network number increment, the 00101 * IP address counter is reset to the initial value specified in SetBase. In 00102 * this case, that would be 0.0.0.3. so if you were to call NewAddress after 00103 * the increment that resulted in a network number of 192.168.2.0, the 00104 * allocated addresses returned by NewAddress would be 192.168.2.3, 00105 * 192.168.2.4, etc. 00106 * 00107 * @returns The value of the incremented network number that will be used in 00108 * following address allocations. 00109 * @see SetBase 00110 * @see NewAddress 00111 */ 00112 Ipv4Address NewNetwork (void); 00113 00114 /** 00115 * @brief Increment the IP address counter used to allocate IP addresses 00116 * 00117 * The address helper allocates IP addresses based on a given network number 00118 * and initial IP address. In order to separate the network number and IP 00119 * address parts, SetBase was given an initial network number value, a network 00120 * mask and an initial address base. 00121 * 00122 * This method increments IP address counter. A check is made to ensure that 00123 * the address returned will not overflow the number of bits allocated to IP 00124 * addresses in SetBase (the number of address bits is defined by the number 00125 * of mask bits that are not '1'). 00126 * 00127 * For example, if the network number was set to 192.168.0.0 with a mask of 00128 * 255.255.255.0 and a base address of 0.0.0.3 in SetBase, the next call to 00129 * NewAddress will return 192.168.1.3. The NewAddress method 00130 * has post-increment semantics. A following NewAddress would return 00131 * 192.168.0.4, etc., until the 253rd call which would assert due to an address 00132 * overflow. 00133 * 00134 * @returns The value of the newly allocated IP address. 00135 * @see SetBase 00136 * @see NewNetwork 00137 */ 00138 Ipv4Address NewAddress (void); 00139 00140 /** 00141 * @brief Assign IP addresses to the net devices specified in the container 00142 * based on the current network prefix and address base. 00143 * 00144 * The address helper allocates IP addresses based on a given network number 00145 * and initial IP address. In order to separate the network number and IP 00146 * address parts, SetBase was given an initial value and a network mask. 00147 * The one bits of this mask define the prefix category from which the helper 00148 * will allocate new network numbers. An initial value for the network 00149 * numbers was provided in the base parameter of the SetBase method in the 00150 * bits corresponding to positions in the mask that were 1. An initial value 00151 * for the IP address counter was also provided in the base parameter in the 00152 * bits corresponding to positions in the mask that were 0. 00153 * 00154 * This method gets new addresses for each net device in the container. For 00155 * each net device in the container, the helper finds the associated node and 00156 * looks up the Ipv4 interface corresponding to the net device. It then sets 00157 * the Ipv4Address and mask in the interface to the appropriate values. If 00158 * the addresses overflow the number of bits allocated for them by the network 00159 * mask in the SetBase method, the system will NS_ASSERT and halt. 00160 * 00161 * @param c The NetDeviceContainer holding the collection of net devices we 00162 * are asked to assign Ipv4 addresses to. 00163 * 00164 * @returns Nothing 00165 * @see SetBase 00166 * @see NewNetwork 00167 */ 00168 Ipv4InterfaceContainer Assign (const NetDeviceContainer &c); 00169 00170 private: 00171 uint32_t NumAddressBits (uint32_t maskbits) const; 00172 00173 uint32_t m_network; 00174 uint32_t m_mask; 00175 uint32_t m_address; 00176 uint32_t m_base; 00177 uint32_t m_shift; 00178 uint32_t m_max; 00179 }; 00180 00181 }; // namespace ns3 00182 00183 #endif /* IPV4_ADDRESS_HELPER_H */