A Discrete-Event Network Simulator
API
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/loopback-net-device.h"
27 #include "ns3/mac16-address.h"
28 #include "ns3/mac48-address.h"
29 #include "ns3/mac64-address.h"
30 #include "ns3/ipv6.h"
31 #include "ns3/ipv6-address-generator.h"
32 #include "ns3/traffic-control-helper.h"
33 #include "ns3/traffic-control-layer.h"
34 
35 #include "ipv6-address-helper.h"
36 
37 namespace ns3
38 {
39 
40 NS_LOG_COMPONENT_DEFINE ("Ipv6AddressHelper");
41 
43 {
44  NS_LOG_FUNCTION (this);
45  Ipv6AddressGenerator::Init (Ipv6Address ("2001:db8::"), Ipv6Prefix (64));
46 }
47 
49  Ipv6Address base)
50 {
51  NS_LOG_FUNCTION (this << network << prefix << base);
52  Ipv6AddressGenerator::Init (network, prefix, base);
53 }
54 
56  Ipv6Address base)
57 {
58  NS_LOG_FUNCTION (this << network << prefix << base);
61  Ipv6AddressGenerator::Init (network, Ipv6Prefix (64), base);
62 }
63 
64 
66 {
67  NS_LOG_FUNCTION (this << addr);
69  {
73  return address;
74  }
75  else if (Mac48Address::IsMatchingType (addr))
76  {
80  return address;
81  }
82  else if (Mac16Address::IsMatchingType (addr))
83  {
87  return address;
88  }
89  else
90  {
91  NS_FATAL_ERROR ("Did not pass in a valid Mac Address (16, 48 or 64 bits)");
92  }
93  /* never reached */
94  return Ipv6Address ("::");
95 }
96 
98 {
99  NS_LOG_FUNCTION (this);
100 //
101 // The way this is expected to be used is that an address and network number
102 // are initialized, and then NewAddress() is called repeatedly to allocate and
103 // get new addresses on a given subnet. The client will expect that the first
104 // address she gets back is the one she used to initialize the generator with.
105 // This implies that this operation is a post-increment.
106 //
108 }
109 
111 {
112  NS_LOG_FUNCTION (this);
114 }
115 
117 {
118  NS_LOG_FUNCTION (this);
119  Ipv6InterfaceContainer retval;
120 
121  for (uint32_t i = 0; i < c.GetN (); ++i)
122  {
123  Ptr<NetDevice> device = c.Get (i);
124 
125  Ptr<Node> node = device->GetNode ();
126  NS_ASSERT_MSG (node, "Ipv6AddressHelper::Allocate (): Bad node");
127 
128  Ptr<Ipv6> ipv6 = node->GetObject<Ipv6> ();
129  NS_ASSERT_MSG (ipv6, "Ipv6AddressHelper::Allocate (): Bad ipv6");
130  int32_t ifIndex = 0;
131 
132  ifIndex = ipv6->GetInterfaceForDevice (device);
133  if (ifIndex == -1)
134  {
135  ifIndex = ipv6->AddInterface (device);
136  }
137  NS_ASSERT_MSG (ifIndex >= 0, "Ipv6AddressHelper::Allocate (): "
138  "Interface index not found");
139 
140  Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (NewAddress (device->GetAddress ()), Ipv6Prefix (64));
141  ipv6->SetMetric (ifIndex, 1);
142  ipv6->AddAddress (ifIndex, ipv6Addr);
143  ipv6->SetUp (ifIndex);
144 
145  retval.Add (ipv6, ifIndex);
146 
147  // Install the default traffic control configuration if the traffic
148  // control layer has been aggregated, if this is not
149  // a loopback interface, and there is no queue disc installed already
151  if (tc && DynamicCast<LoopbackNetDevice> (device) == 0 && tc->GetRootQueueDiscOnDevice (device) == 0)
152  {
153  NS_LOG_LOGIC ("Installing default traffic control configuration");
155  tcHelper.Install (device);
156  }
157  }
158  return retval;
159 }
160 
161 Ipv6InterfaceContainer Ipv6AddressHelper::Assign (const NetDeviceContainer &c, std::vector<bool> withConfiguration)
162 {
163  NS_LOG_FUNCTION (this);
164  Ipv6InterfaceContainer retval;
165  for (uint32_t i = 0; i < c.GetN (); ++i)
166  {
167  Ptr<NetDevice> device = c.Get (i);
168 
169  Ptr<Node> node = device->GetNode ();
170  NS_ASSERT_MSG (node, "Ipv6AddressHelper::Allocate (): Bad node");
171 
172  Ptr<Ipv6> ipv6 = node->GetObject<Ipv6> ();
173  NS_ASSERT_MSG (ipv6, "Ipv6AddressHelper::Allocate (): Bad ipv6");
174 
175  int32_t ifIndex = ipv6->GetInterfaceForDevice (device);
176  if (ifIndex == -1)
177  {
178  ifIndex = ipv6->AddInterface (device);
179  }
180  NS_ASSERT_MSG (ifIndex >= 0, "Ipv6AddressHelper::Allocate (): "
181  "Interface index not found");
182 
183  ipv6->SetMetric (ifIndex, 1);
184 
185  if (withConfiguration.at (i))
186  {
187  Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (NewAddress (device->GetAddress ()), Ipv6Prefix (64));
188  ipv6->AddAddress (ifIndex, ipv6Addr);
189  }
190 
191  ipv6->SetUp (ifIndex);
192  retval.Add (ipv6, ifIndex);
193 
194  // Install the default traffic control configuration if the traffic
195  // control layer has been aggregated, if this is not
196  // a loopback interface, and there is no queue disc installed already
198  if (tc && DynamicCast<LoopbackNetDevice> (device) == 0 && tc->GetRootQueueDiscOnDevice (device) == 0)
199  {
200  NS_LOG_LOGIC ("Installing default traffic control configuration");
202  tcHelper.Install (device);
203  }
204  }
205  return retval;
206 }
207 
208 // Helper API that is redundant with Assign (c, false);
210 {
211  NS_LOG_FUNCTION (this);
212  std::vector<bool> withConfiguration;
213  for (uint32_t i = 0; i < c.GetN (); ++i)
214  {
215  withConfiguration.push_back (false);
216  }
217  return Assign (c, withConfiguration);
218 }
219 
220 } /* namespace ns3 */
221 
static bool IsMatchingType(const Address &address)
static Ipv6Address NextAddress(const Ipv6Prefix prefix)
Allocate the next Ipv6Address for the configured network and prefix.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
QueueDiscContainer Install(NetDeviceContainer c)
Keep track of a set of IPv6 interfaces.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:81
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
Introspection did not find any typical Config paths.
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.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
IPv6 address associated with an interface.
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)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
a polymophic address class
Definition: address.h:90
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.
holds a vector of ns3::NetDevice pointers
Build a set of QueueDisc objects.
static Mac48Address ConvertFrom(const Address &address)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:90
static Ipv6Address NextNetwork(const Ipv6Prefix prefix)
Get the next network according to the given Ipv6Prefix.
static void Init(const Ipv6Address net, const Ipv6Prefix prefix, const Ipv6Address interfaceId="::1")
Initialise the base network and interfaceId for the generator.
Describes an IPv6 address.
Definition: ipv6-address.h:48
static Mac64Address ConvertFrom(const Address &address)
static bool AddAllocated(const Ipv6Address addr)
Add the Ipv6Address to the list of IPv6 entries.
Describes an IPv6 prefix.
Definition: ipv6-address.h:394
static TrafficControlHelper Default(void)
tuple address
Definition: first.py:37
static bool IsMatchingType(const Address &address)
static Ipv6Address GetNetwork(const Ipv6Prefix prefix)
Get the current network of the given Ipv6Prefix.
Ipv6InterfaceContainer AssignWithoutAddress(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer but do not assign any IPv6 addresses.
Ipv6AddressHelper()
Constructor.
void NewNetwork(void)
Allocate a new network.
Ipv6Address NewAddress(void)
Allocate a new Ipv6Address with interface ID equal to the next one in the underlying generator...