src/internet/model/ipv6-address-generator.cc
changeset 10405 45c8fceae24e
parent 8798 5d8dfd7c6609
child 10968 2d29fee2b7b8
equal deleted inserted replaced
10404:eb6495608840 10405:45c8fceae24e
    26 
    26 
    27 NS_LOG_COMPONENT_DEFINE ("Ipv6AddressGenerator");
    27 NS_LOG_COMPONENT_DEFINE ("Ipv6AddressGenerator");
    28 
    28 
    29 namespace ns3 {
    29 namespace ns3 {
    30 
    30 
       
    31 /**
       
    32  * \internal
       
    33  * \ingroup address
       
    34  *
       
    35  * \brief Implementation class of Ipv6AddressGenerator
       
    36  * This generator assigns addresses sequentially from a provided
       
    37  * network address; used in topology code. It also keeps track of all
       
    38  * addresses assigned to perform duplicate detection.
       
    39  *
       
    40  */
    31 class Ipv6AddressGeneratorImpl
    41 class Ipv6AddressGeneratorImpl
    32 {
    42 {
    33 public:
    43 public:
    34   Ipv6AddressGeneratorImpl ();
    44   Ipv6AddressGeneratorImpl ();
    35   virtual ~Ipv6AddressGeneratorImpl ();
    45   virtual ~Ipv6AddressGeneratorImpl ();
    36 
    46 
       
    47   /**
       
    48    * \internal
       
    49    * \brief Initialise the base network and interfaceId for the generator
       
    50    *
       
    51    * The first call to NextAddress() or GetAddress() will return the
       
    52    * value passed in.
       
    53    *
       
    54    * \param net The network for the base Ipv6Address
       
    55    * \param prefix The prefix of the base Ipv6Address
       
    56    * \param interfaceId The base interface ID used for initialization
       
    57    */
    37   void Init (const Ipv6Address net, const Ipv6Prefix prefix,
    58   void Init (const Ipv6Address net, const Ipv6Prefix prefix,
    38              const Ipv6Address interfaceId);
    59              const Ipv6Address interfaceId);
    39 
    60 
       
    61   /**
       
    62    * \internal
       
    63    * \brief Get the next network according to the given Ipv6Prefix
       
    64    *
       
    65    * This operation is a pre-increment, meaning that the internal state
       
    66    * is changed before returning the new network address.
       
    67    *
       
    68    * This also resets the interface ID to the base interface ID that was
       
    69    * used for initialization.
       
    70    *
       
    71    * \param prefix The Ipv6Prefix used to set the next network
       
    72    * \returns the IPv6 address of the next network
       
    73    */
       
    74   Ipv6Address NextNetwork (const Ipv6Prefix prefix);
       
    75 
       
    76   /**
       
    77    * \internal
       
    78    * \brief Get the current network of the given Ipv6Prefix
       
    79    *
       
    80    * Does not change the internal state; this just peeks at the current
       
    81    * network
       
    82    *
       
    83    * \param prefix The Ipv6Prefix for the current network
       
    84    * \returns the IPv6 address of the current network
       
    85    */
    40   Ipv6Address GetNetwork (const Ipv6Prefix prefix) const;
    86   Ipv6Address GetNetwork (const Ipv6Prefix prefix) const;
    41   Ipv6Address NextNetwork (const Ipv6Prefix prefix);
    87 
    42 
    88   /**
       
    89    * \internal
       
    90    * \brief Set the interfaceId for the given Ipv6Prefix
       
    91    *
       
    92    * \param interfaceId The interfaceId to set for the current Ipv6Prefix
       
    93    * \param prefix The Ipv6Prefix whose address is to be set
       
    94    */
    43   void InitAddress (const Ipv6Address interfaceId, const Ipv6Prefix prefix);
    95   void InitAddress (const Ipv6Address interfaceId, const Ipv6Prefix prefix);
       
    96 
       
    97   /**
       
    98    * \internal
       
    99    * \brief Get the Ipv6Address that will be allocated upon NextAddress ()
       
   100    *
       
   101    * Does not change the internal state; just is used to peek the next
       
   102    * address that will be allocated upon NextAddress ()
       
   103    *
       
   104    * \param prefix The Ipv6Prefix for the current network
       
   105    * \returns the IPv6 address
       
   106    */
    44   Ipv6Address GetAddress (const Ipv6Prefix prefix) const;
   107   Ipv6Address GetAddress (const Ipv6Prefix prefix) const;
       
   108 
       
   109   /**
       
   110    * \internal
       
   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    */
    45   Ipv6Address NextAddress (const Ipv6Prefix prefix);
   119   Ipv6Address NextAddress (const Ipv6Prefix prefix);
    46 
   120 
       
   121   /**
       
   122    * \internal
       
   123    * \brief Reset the networks and Ipv6Address to zero
       
   124    */
    47   void Reset (void);
   125   void Reset (void);
       
   126 
       
   127   /**
       
   128    * \internal
       
   129    * \brief Add the Ipv6Address to the list of IPv6 entries
       
   130    *
       
   131    * Typically, this is used by external address allocators that want
       
   132    * to make use of this class's ability to track duplicates.  AddAllocated
       
   133    * is always called internally for any address generated by NextAddress ()
       
   134    *
       
   135    * \param addr The Ipv6Address to be added to the list of Ipv6 entries
       
   136    * \returns true on success
       
   137    */
    48   bool AddAllocated (const Ipv6Address addr);
   138   bool AddAllocated (const Ipv6Address addr);
    49 
   139 
       
   140   /**
       
   141    * \internal
       
   142    * \brief Used to turn off fatal errors and assertions, for testing
       
   143    */
    50   void TestMode (void);
   144   void TestMode (void);
       
   145 
    51 private:
   146 private:
    52   static const uint32_t N_BITS = 128;
   147   static const uint32_t N_BITS = 128; //!< /internal the number of bits in the address
    53   static const uint32_t MOST_SIGNIFICANT_BIT = 0x80;
   148   static const uint32_t MOST_SIGNIFICANT_BIT = 0x80; //!< /internal MSB set to 1
    54 
   149 
       
   150   /**
       
   151    * \internal
       
   152    * \brief Create an index number for the prefix
       
   153    * \param prefix the prefix to index
       
   154    * \returns an index
       
   155    */
    55   uint32_t PrefixToIndex (Ipv6Prefix prefix) const;
   156   uint32_t PrefixToIndex (Ipv6Prefix prefix) const;
    56 
   157 
       
   158   /**
       
   159    * \internal
       
   160    * \brief This class holds the state for a given network
       
   161    */
    57   class NetworkState
   162   class NetworkState
    58   {
   163   {
    59 public:
   164 public:
    60     uint8_t prefix[16];
   165     uint8_t prefix[16];  //!< /internal the network prefix
    61     uint32_t shift;
   166     uint32_t shift;      //!< /internal a shift
    62     uint8_t network[16];
   167     uint8_t network[16]; //!< /internal the network
    63     uint8_t addr[16];
   168     uint8_t addr[16];    //!< /internal the address
    64     uint8_t addrMax[16];
   169     uint8_t addrMax[16]; //!< /internal the maximum address
    65   };
   170   };
    66 
   171 
    67   NetworkState m_netTable[N_BITS];
   172   NetworkState m_netTable[N_BITS]; //!< /internal the available networks
    68 
   173 
       
   174   /**
       
   175    * \internal
       
   176    * \brief This class holds the allocated addresses
       
   177    */
    69   class Entry
   178   class Entry
    70   {
   179   {
    71 public:
   180 public:
    72     uint8_t addrLow[16];
   181     uint8_t addrLow[16];  //!< /internal the lowest allocated address
    73     uint8_t addrHigh[16];
   182     uint8_t addrHigh[16]; //!< /internal the highest allocated address
    74   };
   183   };
    75 
   184 
    76   std::list<Entry> m_entries;
   185   std::list<Entry> m_entries; //!< /internal contained of allocated addresses
    77   Ipv6Address m_base;
   186   Ipv6Address m_base; //!< /internal base address
    78   bool m_test;
   187   bool m_test; //!< /internal test mode (if true)
    79 };
   188 };
    80 
   189 
    81 Ipv6AddressGeneratorImpl::Ipv6AddressGeneratorImpl ()
   190 Ipv6AddressGeneratorImpl::Ipv6AddressGeneratorImpl ()
    82   : m_entries (),
   191   : m_entries (),
    83     m_base ("::1"),
   192     m_base ("::1"),