src/internet/model/ipv6-address-generator.h
author Adrian S Tam <adrian.sw.tam@gmail.com>
Wed, 07 Dec 2011 11:22:10 -0500
changeset 7619 b4dee6307aa7
parent 7599 33588b2b9557
child 10158 971f362648c3
permissions -rw-r--r--
Nagle's algorithm in TCP (closes bug 1039)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7599
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
     1
/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
     2
/*
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
     3
 * Copyright (c) 2008 University of Washington
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
     4
 * Copyright (c) 2011 Atishay Jain
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
     5
 *
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
     6
 * This program is free software; you can redistribute it and/or modify
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
     7
 * it under the terms of the GNU General Public License version 2 as
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
     8
 * published by the Free Software Foundation;
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
     9
 *
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    13
 * GNU General Public License for more details.
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    14
 *
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    16
 * along with this program; if not, write to the Free Software
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    18
 */
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    19
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    20
#ifndef IPV6_ADDRESS_GENERATOR_H
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    21
#define IPV6_ADDRESS_GENERATOR_H
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    22
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    23
#include "ns3/ipv6-address.h"
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    24
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    25
namespace ns3 {
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    26
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    27
/**
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    28
 * \ingroup address
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    29
 *
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    30
 * \brief This generator assigns addresses sequentially from a provided
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    31
 * network address; used in topology code. It also keeps track of all
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    32
 * addresses assigned to perform duplicate detection.
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    33
 *
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    34
 * Global unicast IPv6 addresses based on RFC 4291 definition:
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    35
 *
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    36
 *     |         n bits          |   m bits  |       128-n-m bits         |
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    37
 *     +-------------------------+-----------+----------------------------+
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    38
 *     | global routing prefix   | subnet ID |       interface ID         |
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    39
 *     +-------------------------+-----------+----------------------------+
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    40
 *
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    41
 * In this class, the first two quantities (n + m) are what is called the
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    42
 * 'net', and the 'prefix' defines the length in bits of (n + m).
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    43
 *
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    44
 * The way this is expected to be used is that, after initializing the
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    45
 * network and interfaceId to a number, a user can call NextAddress ()
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    46
 * repeatedly to obtain new interface IDs with the current network (for
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    47
 * multiple addresses on the link) and can call NextNetwork () to increment
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    48
 * the subnet ID.
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    49
 *
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    50
 * The interface ID is often an EUI-64 address derived from the MAC address,
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    51
 * but can also be a pseudo-random value (RFC 3041).  This implementation
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    52
 * does not generate EUI-64-based interface IDs.
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    53
 */
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    54
class Ipv6AddressGenerator
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    55
{
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    56
public:
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    57
  /**
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    58
   * \brief Initialise the base network and interfaceId for the generator
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    59
   *
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    60
   * The first call to NextAddress() or GetAddress() will return the
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    61
   * value passed in.
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    62
   *
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    63
   * \param net The network for the base Ipv6Address
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    64
   * \param prefix The prefix of the base Ipv6Address
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    65
   * \param interfaceId The base interface ID used for initialization
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    66
   */
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    67
  static void Init (const Ipv6Address net, const Ipv6Prefix prefix,
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    68
                    const Ipv6Address interfaceId = "::1");
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    69
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    70
  /**
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    71
   * \brief Get the next network acoording to the given Ipv6Prefix
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    72
   *
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    73
   * This operation is a pre-increment, meaning that the internal state
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    74
   * is changed before returning the new network address.
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    75
   *
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    76
   * This also resets the interface ID to the base interface ID that was
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    77
   * used for initialization.
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    78
   *
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    79
   * \param prefix The Ipv6Prefix used to set the next network
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    80
   */
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    81
  static Ipv6Address NextNetwork (const Ipv6Prefix prefix);
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    82
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    83
  /**
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    84
   * \brief Get the current network of the given Ipv6Prefix
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    85
   *
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    86
   * Does not change the internal state; this just peeks at the current
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    87
   * network
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    88
   *
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    89
   * \param prefix The Ipv6Prefix for the current network
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    90
   */
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    91
  static Ipv6Address GetNetwork (const Ipv6Prefix prefix);
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    92
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    93
  /**
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    94
   * \brief Set the interfaceId for the given Ipv6Prefix
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    95
   *
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    96
   * \param interfaceId The interfaceId to set for the current Ipv6Prefix
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    97
   * \param prefix The Ipv6Prefix whose address is to be set
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    98
   */
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
    99
  static void InitAddress (const Ipv6Address interfaceId, const Ipv6Prefix prefix);
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   100
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   101
  /**
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   102
   * \brief Allocate the next Ipv6Address for the configured network and prefix
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   103
   *
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   104
   * This operation is a post-increment, meaning that the first address
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   105
   * allocated will be the one that was initially configured.
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   106
   * .
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   107
   * \param prefix The Ipv6Prefix for the current network
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   108
   */
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   109
  static Ipv6Address NextAddress (const Ipv6Prefix prefix);
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   110
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   111
  /**
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   112
   * \brief Get the Ipv6Address that will be allocated upon NextAddress()
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   113
   *
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   114
   * Does not change the internal state; just is used to peek the next
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   115
   * address that will be allocated upon NextAddress ()
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   116
   *
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   117
   * \param prefix The Ipv6Prefix for the current network
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   118
   */
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   119
  static Ipv6Address GetAddress (const Ipv6Prefix prefix);
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   120
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   121
  /**
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   122
   * \brief Reset the networks and Ipv6Address to zero
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   123
   */
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   124
  static void Reset (void);
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   125
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   126
  /**
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   127
   * \brief Add the Ipv6Address to the list of IPv6 entries
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   128
   *
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   129
   * Typically, this is used by external address allocators that want
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   130
   * to make use of this class's ability to track duplicates.  AddAllocated
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   131
   * is always called internally for any address generated by NextAddress()
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   132
   *
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   133
   * \param addr The Ipv6Address to be added to the list of Ipv6 entries
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   134
   */
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   135
  static bool AddAllocated (const Ipv6Address addr);
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   136
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   137
  /**
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   138
   * \brief Used to turn off fatal errors and assertions, for testing
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   139
   */
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   140
  static void TestMode (void);
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   141
};
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   142
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   143
}; // namespace ns3
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   144
33588b2b9557 IPv6 address generator
Atishay Jain <atishayjain25@gmail.com>
parents:
diff changeset
   145
#endif /* IPV6_ADDRESS_GENERATOR_H */