1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ 2 /* 3 * Copyright (c) 2008 University of Washington 4 * Copyright (c) 2011 Atishay Jain 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 2 as 8 * published by the Free Software Foundation; 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 */ 19 20 #ifndef IPV6_ADDRESS_GENERATOR_H 21 #define IPV6_ADDRESS_GENERATOR_H 22 23 #include "ns3/ipv6-address.h" 24 25 namespace ns3 { 26 27 /** 28 * \ingroup address 29 * \ingroup ipv6 30 * 31 * \brief This generator assigns addresses sequentially from a provided 32 * network address; used in topology code. It also keeps track of all 33 * addresses assigned to perform duplicate detection. 34 * 35 * Global unicast IPv6 addresses based on \RFC{4291} definition: 36 * 37 * | n bits | m bits | 128-n-m bits | 38 * +-------------------------+-----------+----------------------------+ 39 * | global routing prefix | subnet ID | interface ID | 40 * +-------------------------+-----------+----------------------------+ 41 * 42 * In this class, the first two quantities (n + m) are what is called the 43 * 'net', and the 'prefix' defines the length in bits of (n + m). 44 * 45 * The way this is expected to be used is that, after initializing the 46 * network and interfaceId to a number, a user can call NextAddress () 47 * repeatedly to obtain new interface IDs with the current network (for 48 * multiple addresses on the link) and can call NextNetwork () to increment 49 * the subnet ID. 50 * 51 * The interface ID is often an EUI-64 address derived from the MAC address, 52 * but can also be a pseudo-random value (\RFC{3041}). This implementation 53 * does not generate EUI-64-based interface IDs. 54 * 55 * \note BEWARE: this class acts as a Singleton. 56 * In other terms, two different instances of Ipv6AddressGenerator will 57 * pick IPv6 numbers from the same pool. Changing the network in one of them 58 * will also change the network in the other instances. 59 * 60 */ 61 class Ipv6AddressGenerator 62 { 63 public: 64 /** 65 * \brief Initialise the base network and interfaceId for the generator 66 * 67 * The first call to NextAddress() or GetAddress() will return the 68 * value passed in. 69 * 70 * \param net The network for the base Ipv6Address 71 * \param prefix The prefix of the base Ipv6Address 72 * \param interfaceId The base interface ID used for initialization 73 */ 74 static void Init (const Ipv6Address net, const Ipv6Prefix prefix, 75 const Ipv6Address interfaceId = "::1"); 76 77 /** 78 * \brief Get the next network according to the given Ipv6Prefix 79 * 80 * This operation is a pre-increment, meaning that the internal state 81 * is changed before returning the new network address. 82 * 83 * This also resets the interface ID to the base interface ID that was 84 * used for initialization. 85 * 86 * \param prefix The Ipv6Prefix used to set the next network 87 * \returns the IPv6 address of the next network 88 */ 89 static Ipv6Address NextNetwork (const Ipv6Prefix prefix); 90 91 /** 92 * \brief Get the current network of the given Ipv6Prefix 93 * 94 * Does not change the internal state; this just peeks at the current 95 * network 96 * 97 * \param prefix The Ipv6Prefix for the current network 98 * \returns the IPv6 address of the current network 99 */ 100 static Ipv6Address GetNetwork (const Ipv6Prefix prefix); 101 102 /** 103 * \brief Set the interfaceId for the given Ipv6Prefix 104 * 105 * \param interfaceId The interfaceId to set for the current Ipv6Prefix 106 * \param prefix The Ipv6Prefix whose address is to be set 107 */ 108 static void InitAddress (const Ipv6Address interfaceId, const Ipv6Prefix prefix); 109 110 /** 111 * \brief Allocate the next Ipv6Address for the configured network and prefix 112 * 113 * This operation is a post-increment, meaning that the first address 114 * allocated will be the one that was initially configured. 115 * 116 * \param prefix The Ipv6Prefix for the current network 117 * \returns the IPv6 address 118 */ 119 static Ipv6Address NextAddress (const Ipv6Prefix prefix); 120 121 /** 122 * \brief Get the Ipv6Address that will be allocated upon NextAddress () 123 * 124 * Does not change the internal state; just is used to peek the next 125 * address that will be allocated upon NextAddress () 126 * 127 * \param prefix The Ipv6Prefix for the current network 128 * \returns the IPv6 address 129 */ 130 static Ipv6Address GetAddress (const Ipv6Prefix prefix); 131 132 /** 133 * \brief Reset the networks and Ipv6Address to zero 134 */ 135 static void Reset (void); 136 137 /** 138 * \brief Add the Ipv6Address to the list of IPv6 entries 139 * 140 * Typically, this is used by external address allocators that want 141 * to make use of this class's ability to track duplicates. AddAllocated 142 * is always called internally for any address generated by NextAddress () 143 * 144 * \param addr The Ipv6Address to be added to the list of Ipv6 entries 145 * \returns true on success 146 */ 147 static bool AddAllocated (const Ipv6Address addr); 148 149 /** 150 * \brief Check the Ipv6Address allocation in the list of IPv6 entries 151 * 152 * \param addr The Ipv6Address to be checked in the list of Ipv4 entries 153 * \returns true if the address is already allocated 154 */ 155 static bool IsAddressAllocated (const Ipv6Address addr); 156 157 /** 158 * \brief Check if a network has already allocated addresses 159 * 160 * \param addr The Ipv6 network to be checked 161 * \param prefix The Ipv6 network prefix 162 * \returns true if the network is already allocated 163 */ 164 static bool IsNetworkAllocated (const Ipv6Address addr, const Ipv6Prefix prefix); 165 166 /** 167 * \brief Used to turn off fatal errors and assertions, for testing 168 */ 169 static void TestMode (void); 170 }; 171 172 }; // namespace ns3 173 174 #endif /* IPV6_ADDRESS_GENERATOR_H */ 175