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  m_network = Ipv6Address ("2001:db8::");
46  m_prefix = 64;
47  m_address = Ipv6Address ("::1");
48  m_base = m_address;
49 }
50 
52  Ipv6Address base)
53 {
54  NS_LOG_FUNCTION (this << network << prefix << base);
55 
56  m_network = network;
57  m_prefix = prefix;
58  m_address = base;
59  m_base = base;
60 
61  NS_ASSERT_MSG (m_network == network.CombinePrefix (prefix),
62  "Ipv6AddressHelper: network address and prefix mismatch: " << m_network << " " << m_prefix);
63 
65  "Ipv6AddressHelper: base address and prefix mismatch: " << base << " " << m_prefix);
66 }
67 
69  Ipv6Address base)
70 {
71  NS_LOG_FUNCTION (this << network << prefix << base);
72 
73  m_network = network;
74  m_prefix = prefix;
75  m_address = base;
76  m_base = base;
77 
78  NS_ASSERT_MSG (m_network == network.CombinePrefix (prefix),
79  "Ipv6AddressHelper::SetBase(): network address and prefix mismatch: " << m_network << " " << m_prefix);
80 
82  "Ipv6AddressHelper::SetBase(): base address and prefix mismatch: " << base << " " << m_prefix);
83 }
84 
85 
87 {
88  NS_LOG_FUNCTION (this << addr);
90  {
93  return address;
94  }
95  else if (Mac48Address::IsMatchingType (addr))
96  {
99  return address;
100  }
101  else if (Mac16Address::IsMatchingType (addr))
102  {
105  return address;
106  }
107  else if (Mac8Address::IsMatchingType (addr))
108  {
111  return address;
112  }
113  else
114  {
115  NS_FATAL_ERROR ("Did not pass in a valid Mac Address (8, 16, 48 or 64 bits)");
116  }
117  /* never reached */
118  return Ipv6Address ("::");
119 }
120 
122 {
123  NS_LOG_FUNCTION (this);
124 //
125 // The way this is expected to be used is that an address and network number
126 // are initialized, and then NewAddress() is called repeatedly to allocate and
127 // get new addresses on a given subnet. The client will expect that the first
128 // address she gets back is the one she used to initialize the generator with.
129 // This implies that this operation is a post-increment.
130 
131  uint8_t netBuf[16];
132  uint8_t hostBuf[16];
133  uint8_t addrBuf[16];
134  m_network.GetBytes (netBuf);
135  m_address.GetBytes (hostBuf);
136 
138  "Ipv6AddressHelper::NewAddress(): Too many hosts in the network: " << m_address << " " << m_prefix);
139 
140  for (uint8_t i=0; i<16; i++)
141  {
142  addrBuf[i] = netBuf[i] | hostBuf[i];
143  }
144 
145  Ipv6Address addr = Ipv6Address (addrBuf);
146 
147  // Remember: hostBuf[15] is the Least Significant Byte.
148  uint16_t sum;
149  sum = static_cast<uint16_t> (hostBuf[15]) + 1;
150  hostBuf[15] += 1;
151  for (uint8_t index=0; index<15; index++)
152  {
153  if (sum > hostBuf[15-index])
154  {
155  sum = static_cast<uint16_t> (hostBuf[14-index]) + 1;
156  hostBuf[14-index] += 1;
157  }
158  else
159  {
160  break;
161  }
162  }
163  m_address = Ipv6Address (hostBuf);
164 
166  return addr;
167 }
168 
170 {
171  NS_LOG_FUNCTION (this);
172 
173  uint8_t netBuf[16];
174  uint8_t addBuf[16];
175  m_network.GetBytes (netBuf);
176 
177  uint8_t prefixIndex = (m_prefix.GetPrefixLength ()-1)/8;
178  uint8_t prefixPosition = (8-(m_prefix.GetPrefixLength ()%8))%8;
179 
180  for (uint8_t index=0; index<16; index++)
181  {
182  addBuf[index] = 0;
183  if (index==prefixIndex)
184  {
185  addBuf[index] = (1<<prefixPosition);
186  }
187  }
188 
189  uint16_t sum[16];
190  for (uint8_t index=0; index<16; index++)
191  {
192  sum[index] = static_cast<uint16_t> (netBuf[index]) + static_cast<uint16_t> (addBuf[index]);
193  netBuf[index] += addBuf[index];
194  }
195 
196  for (uint8_t index=0; index<15; index++)
197  {
198  if (sum[15-index] > netBuf[15-index])
199  {
200  sum[14-index] = static_cast<uint16_t> (netBuf[14-index]) + 1;
201  netBuf[14-index] += 1;
202  }
203  }
204 
205  m_network = Ipv6Address (netBuf);
206  m_address = m_base;
207 }
208 
210 {
211  NS_LOG_FUNCTION (this);
212  Ipv6InterfaceContainer retval;
213 
214  for (uint32_t i = 0; i < c.GetN (); ++i)
215  {
216  Ptr<NetDevice> device = c.Get (i);
217 
218  Ptr<Node> node = device->GetNode ();
219  NS_ASSERT_MSG (node, "Ipv6AddressHelper::Allocate (): Bad node");
220 
221  Ptr<Ipv6> ipv6 = node->GetObject<Ipv6> ();
222  NS_ASSERT_MSG (ipv6, "Ipv6AddressHelper::Allocate (): Bad ipv6");
223  int32_t ifIndex = 0;
224 
225  ifIndex = ipv6->GetInterfaceForDevice (device);
226  if (ifIndex == -1)
227  {
228  ifIndex = ipv6->AddInterface (device);
229  }
230  NS_ASSERT_MSG (ifIndex >= 0, "Ipv6AddressHelper::Allocate (): "
231  "Interface index not found");
232 
233  Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (NewAddress (device->GetAddress ()), Ipv6Prefix (64));
234  ipv6->SetMetric (ifIndex, 1);
235  ipv6->AddAddress (ifIndex, ipv6Addr);
236  ipv6->SetUp (ifIndex);
237 
238  retval.Add (ipv6, ifIndex);
239 
240  // Install the default traffic control configuration if the traffic
241  // control layer has been aggregated, if this is not
242  // a loopback interface, and there is no queue disc installed already
244  if (tc && DynamicCast<LoopbackNetDevice> (device) == 0 && tc->GetRootQueueDiscOnDevice (device) == 0)
245  {
246  NS_LOG_LOGIC ("Installing default traffic control configuration");
248  tcHelper.Install (device);
249  }
250  }
251  return retval;
252 }
253 
254 Ipv6InterfaceContainer Ipv6AddressHelper::Assign (const NetDeviceContainer &c, std::vector<bool> withConfiguration)
255 {
256  NS_LOG_FUNCTION (this);
257  Ipv6InterfaceContainer retval;
258  for (uint32_t i = 0; i < c.GetN (); ++i)
259  {
260  Ptr<NetDevice> device = c.Get (i);
261 
262  Ptr<Node> node = device->GetNode ();
263  NS_ASSERT_MSG (node, "Ipv6AddressHelper::Allocate (): Bad node");
264 
265  Ptr<Ipv6> ipv6 = node->GetObject<Ipv6> ();
266  NS_ASSERT_MSG (ipv6, "Ipv6AddressHelper::Allocate (): Bad ipv6");
267 
268  int32_t ifIndex = ipv6->GetInterfaceForDevice (device);
269  if (ifIndex == -1)
270  {
271  ifIndex = ipv6->AddInterface (device);
272  }
273  NS_ASSERT_MSG (ifIndex >= 0, "Ipv6AddressHelper::Allocate (): "
274  "Interface index not found");
275 
276  ipv6->SetMetric (ifIndex, 1);
277 
278  if (withConfiguration.at (i))
279  {
280  Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (NewAddress (device->GetAddress ()), Ipv6Prefix (64));
281  ipv6->AddAddress (ifIndex, ipv6Addr);
282  }
283 
284  ipv6->SetUp (ifIndex);
285  retval.Add (ipv6, ifIndex);
286 
287  // Install the default traffic control configuration if the traffic
288  // control layer has been aggregated, if this is not
289  // a loopback interface, and there is no queue disc installed already
291  if (tc && DynamicCast<LoopbackNetDevice> (device) == 0 && tc->GetRootQueueDiscOnDevice (device) == 0)
292  {
293  NS_LOG_LOGIC ("Installing default traffic control configuration");
295  tcHelper.Install (device);
296  }
297  }
298  return retval;
299 }
300 
301 // Helper API that is redundant with Assign (c, false);
303 {
304  NS_LOG_FUNCTION (this);
305  std::vector<bool> withConfiguration;
306  for (uint32_t i = 0; i < c.GetN (); ++i)
307  {
308  withConfiguration.push_back (false);
309  }
310  return Assign (c, withConfiguration);
311 }
312 
313 } /* namespace ns3 */
314 
static bool IsMatchingType(const Address &address)
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
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.
NS_ASSERT_MSG(false, "Ipv4AddressGenerator::MaskToIndex(): Impossible")
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:81
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.
void GetBytes(uint8_t buf[16]) const
Get the bytes corresponding to the address.
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:202
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
static bool IsMatchingType(const Address &address)
Check that a generic Address is compatible with Mac8Address.
Definition: mac8-address.cc:63
Ipv6Address m_address
host address
a polymophic address class
Definition: address.h:90
Ipv6InterfaceContainer Assign(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer with auto-assigned addresses.
static Ipv6Address GetZero()
Get the 0 (::) Ipv6Address.
Ipv6Prefix m_prefix
prefix length
holds a vector of ns3::NetDevice pointers
Build a set of QueueDisc objects.
static Mac48Address ConvertFrom(const Address &address)
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
Every class exported by the ns3 library is enclosed in the ns3 namespace.
address
Definition: first.py:37
uint8_t GetPrefixLength() const
Get prefix length.
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
Describes an IPv6 address.
Definition: ipv6-address.h:49
static Mac64Address ConvertFrom(const Address &address)
static bool AddAllocated(const Ipv6Address addr)
Add the Ipv6Address to the list of IPv6 entries.
uint32_t GetN(void) const
Get the number of Ptr<NetDevice> stored in this container.
Ipv6Address m_network
network address
Describes an IPv6 prefix.
Definition: ipv6-address.h:428
static TrafficControlHelper Default(void)
static bool IsMatchingType(const Address &address)
Ipv6InterfaceContainer AssignWithoutAddress(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer but do not assign any IPv6 addresses.
Ipv6Address CombinePrefix(Ipv6Prefix const &prefix)
Combine this address with a prefix.
static Mac8Address ConvertFrom(const Address &address)
Convert a generic address to a Mac8Address.
Definition: mac8-address.cc:54
Ipv6AddressHelper()
Constructor.
void NewNetwork(void)
Allocate a new network.
Ipv6Address m_base
host base address
Ipv6Address NewAddress(void)
Allocate a new Ipv6Address with interface ID equal to the next one in the underlying generator...