A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ipv6-address-helper.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008-2009 Strasbourg University
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
19  */
20 
21 #include "ns3/assert.h"
22 #include "ns3/log.h"
23 #include "ns3/ptr.h"
24 #include "ns3/node.h"
25 #include "ns3/net-device.h"
26 #include "ns3/mac16-address.h"
27 #include "ns3/mac48-address.h"
28 #include "ns3/mac64-address.h"
29 #include "ns3/ipv6.h"
30 #include "ns3/ipv6-address-generator.h"
31 
32 #include "ipv6-address-helper.h"
33 
34 namespace ns3
35 {
36 
37 NS_LOG_COMPONENT_DEFINE ("Ipv6AddressHelper")
38  ;
39 
41 {
42  NS_LOG_FUNCTION (this);
43  Ipv6AddressGenerator::Init (Ipv6Address ("2001:db8::"), Ipv6Prefix (64));
44 }
45 
47  Ipv6Address base)
48 {
49  NS_LOG_FUNCTION (this << network << prefix << base);
50  Ipv6AddressGenerator::Init (network, prefix, base);
51 }
52 
54  Ipv6Address base)
55 {
56  NS_LOG_FUNCTION (this << network << prefix << base);
59  Ipv6AddressGenerator::Init (network, Ipv6Prefix (64), base);
60 }
61 
62 
64 {
65  NS_LOG_FUNCTION (this << addr);
67  {
71  return address;
72  }
73  else if (Mac48Address::IsMatchingType (addr))
74  {
78  return address;
79  }
80  else if (Mac16Address::IsMatchingType (addr))
81  {
85  return address;
86  }
87  else
88  {
89  NS_FATAL_ERROR ("Did not pass in a valid Mac Address (16, 48 or 64 bits)");
90  }
91  /* never reached */
92  return Ipv6Address ("::");
93 }
94 
96 {
97  NS_LOG_FUNCTION (this);
98 //
99 // The way this is expected to be used is that an address and network number
100 // are initialized, and then NewAddress() is called repeatedly to allocate and
101 // get new addresses on a given subnet. The client will expect that the first
102 // address she gets back is the one she used to initialize the generator with.
103 // This implies that this operation is a post-increment.
104 //
106 }
107 
109 {
110  NS_LOG_FUNCTION (this << network << prefix);
111  SetBase (network, Ipv6Prefix (64));
112 }
113 
115 {
116  NS_LOG_FUNCTION (this);
118 }
119 
121 {
122  NS_LOG_FUNCTION (this);
123  Ipv6InterfaceContainer retval;
124 
125  for (uint32_t i = 0; i < c.GetN (); ++i)
126  {
127  Ptr<NetDevice> device = c.Get (i);
128 
129  Ptr<Node> node = device->GetNode ();
130  NS_ASSERT_MSG (node, "Ipv6AddressHelper::Allocate (): Bad node");
131 
132  Ptr<Ipv6> ipv6 = node->GetObject<Ipv6> ();
133  NS_ASSERT_MSG (ipv6, "Ipv6AddressHelper::Allocate (): Bad ipv6");
134  int32_t ifIndex = 0;
135 
136  ifIndex = ipv6->GetInterfaceForDevice (device);
137  if (ifIndex == -1)
138  {
139  ifIndex = ipv6->AddInterface (device);
140  }
141  NS_ASSERT_MSG (ifIndex >= 0, "Ipv6AddressHelper::Allocate (): "
142  "Interface index not found");
143 
144  Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (NewAddress (device->GetAddress ()), Ipv6Prefix (64));
145  ipv6->SetMetric (ifIndex, 1);
146  ipv6->AddAddress (ifIndex, ipv6Addr);
147  ipv6->SetUp (ifIndex);
148 
149  retval.Add (ipv6, ifIndex);
150  }
151  return retval;
152 }
153 
154 Ipv6InterfaceContainer Ipv6AddressHelper::Assign (const NetDeviceContainer &c, std::vector<bool> withConfiguration)
155 {
156  NS_LOG_FUNCTION (this);
157  Ipv6InterfaceContainer retval;
158  for (uint32_t i = 0; i < c.GetN (); ++i)
159  {
160  Ptr<NetDevice> device = c.Get (i);
161 
162  Ptr<Node> node = device->GetNode ();
163  NS_ASSERT_MSG (node, "Ipv6AddressHelper::Allocate (): Bad node");
164 
165  Ptr<Ipv6> ipv6 = node->GetObject<Ipv6> ();
166  NS_ASSERT_MSG (ipv6, "Ipv6AddressHelper::Allocate (): Bad ipv6");
167 
168  int32_t ifIndex = ipv6->GetInterfaceForDevice (device);
169  if (ifIndex == -1)
170  {
171  ifIndex = ipv6->AddInterface (device);
172  }
173  NS_ASSERT_MSG (ifIndex >= 0, "Ipv6AddressHelper::Allocate (): "
174  "Interface index not found");
175 
176  ipv6->SetMetric (ifIndex, 1);
177 
178  if (withConfiguration.at (i))
179  {
180  Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (NewAddress (device->GetAddress ()), Ipv6Prefix (64));
181  ipv6->AddAddress (ifIndex, ipv6Addr);
182  }
183 
184  ipv6->SetUp (ifIndex);
185  retval.Add (ipv6, ifIndex);
186  }
187  return retval;
188 }
189 
190 // Helper API that is redundant with Assign (c, false);
192 {
193  NS_LOG_FUNCTION (this);
194  std::vector<bool> withConfiguration;
195  for (uint32_t i = 0; i < c.GetN (); ++i)
196  {
197  withConfiguration.push_back (false);
198  }
199  return Assign (c, withConfiguration);
200 }
201 
202 } /* namespace ns3 */
203 
static bool IsMatchingType(const Address &address)
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
Keep track of a set of IPv6 interfaces.
NS_LOG_COMPONENT_DEFINE("GrantedTimeWindowMpiInterface")
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:79
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
static Mac16Address ConvertFrom(const Address &address)
static bool IsMatchingType(const Address &address)
void Add(Ptr< Ipv6 > ipv6, uint32_t interface)
Add a couple IPv6/interface.
static Ipv6Address MakeAutoconfiguredAddress(Mac16Address addr, Ipv6Address prefix)
Make the autoconfigured IPv6 address with Mac16Address.
IPv6 address associated with an interface.
static Ipv6Address NextNetwork(const Ipv6Prefix prefix)
Get the next network according to the given Ipv6Prefix.
void SetBase(Ipv6Address network, Ipv6Prefix prefix, Ipv6Address base=Ipv6Address("::1"))
Set the base network number, network prefix, and base interface ID.
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
a polymophic address class
Definition: address.h:86
Ipv6InterfaceContainer Assign(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer with auto-assigned addresses.
uint32_t GetN(void) const
Get the number of Ptr stored in this container.
static void Init(const Ipv6Address net, const Ipv6Prefix prefix, const Ipv6Address interfaceId="::1")
Initialise the base network and interfaceId for the generator.
holds a vector of ns3::NetDevice pointers
static Mac48Address ConvertFrom(const Address &address)
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
Describes an IPv6 address.
Definition: ipv6-address.h:46
static Mac64Address ConvertFrom(const Address &address)
Describes an IPv6 prefix.
Definition: ipv6-address.h:387
static bool AddAllocated(const Ipv6Address addr)
Add the Ipv6Address to the list of IPv6 entries.
tuple address
Definition: first.py:37
static bool IsMatchingType(const Address &address)
Ipv6InterfaceContainer AssignWithoutAddress(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer but do not assign any IPv6 addresses.
Ptr< T > GetObject(void) const
Definition: object.h:361
static Ipv6Address NextAddress(const Ipv6Prefix prefix)
Allocate the next Ipv6Address for the configured network and prefix.
Ipv6AddressHelper()
Constructor.
void NewNetwork(void)
Allocate a new network.
static Ipv6Address GetNetwork(const Ipv6Prefix prefix)
Get the current network of the given Ipv6Prefix.
Ipv6Address NewAddress(void)
Allocate a new Ipv6Address with interface ID equal to the next one in the underlying generator...