A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
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
Ipv6AddressHelper::Ipv6AddressHelper
()
40
{
41
NS_LOG_FUNCTION
(
this
);
42
Ipv6AddressGenerator::Init
(
Ipv6Address
(
"2001:db8::"
),
Ipv6Prefix
(64));
43
}
44
45
Ipv6AddressHelper::Ipv6AddressHelper
(
Ipv6Address
network,
Ipv6Prefix
prefix,
46
Ipv6Address
base)
47
{
48
NS_LOG_FUNCTION
(
this
<< network << prefix << base);
49
Ipv6AddressGenerator::Init
(network, prefix, base);
50
}
51
52
void
Ipv6AddressHelper::SetBase
(
Ipv6Address
network,
Ipv6Prefix
prefix,
53
Ipv6Address
base)
54
{
55
NS_LOG_FUNCTION
(
this
<< network << prefix << base);
58
Ipv6AddressGenerator::Init
(network,
Ipv6Prefix
(64), base);
59
}
60
61
62
Ipv6Address
Ipv6AddressHelper::NewAddress
(
Address
addr)
63
{
64
NS_LOG_FUNCTION
(
this
<< addr);
65
if
(
Mac64Address::IsMatchingType
(addr))
66
{
67
Ipv6Address
network =
Ipv6AddressGenerator::GetNetwork
(
Ipv6Prefix
(64));
68
Ipv6Address
address
=
Ipv6Address::MakeAutoconfiguredAddress
(
Mac64Address::ConvertFrom
(addr), network);
69
Ipv6AddressGenerator::AddAllocated
(address);
70
return
address
;
71
}
72
else
if
(
Mac48Address::IsMatchingType
(addr))
73
{
74
Ipv6Address
network =
Ipv6AddressGenerator::GetNetwork
(
Ipv6Prefix
(64));
75
Ipv6Address
address
=
Ipv6Address::MakeAutoconfiguredAddress
(
Mac48Address::ConvertFrom
(addr), network);
76
Ipv6AddressGenerator::AddAllocated
(address);
77
return
address
;
78
}
79
else
if
(
Mac16Address::IsMatchingType
(addr))
80
{
81
Ipv6Address
network =
Ipv6AddressGenerator::GetNetwork
(
Ipv6Prefix
(64));
82
Ipv6Address
address
=
Ipv6Address::MakeAutoconfiguredAddress
(
Mac16Address::ConvertFrom
(addr), network);
83
Ipv6AddressGenerator::AddAllocated
(address);
84
return
address
;
85
}
86
else
87
{
88
NS_FATAL_ERROR
(
"Did not pass in a valid Mac Address (16, 48 or 64 bits)"
);
89
}
90
/* never reached */
91
return
Ipv6Address
(
"::"
);
92
}
93
94
Ipv6Address
Ipv6AddressHelper::NewAddress
(
void
)
95
{
96
NS_LOG_FUNCTION
(
this
);
97
//
98
// The way this is expected to be used is that an address and network number
99
// are initialized, and then NewAddress() is called repeatedly to allocate and
100
// get new addresses on a given subnet. The client will expect that the first
101
// address she gets back is the one she used to initialize the generator with.
102
// This implies that this operation is a post-increment.
103
//
104
return
Ipv6AddressGenerator::NextAddress
(
Ipv6Prefix
(64));
105
}
106
107
void
Ipv6AddressHelper::NewNetwork
(
Ipv6Address
network,
Ipv6Prefix
prefix)
108
{
109
NS_LOG_FUNCTION
(
this
<< network << prefix);
110
SetBase
(network,
Ipv6Prefix
(64));
111
}
112
113
void
Ipv6AddressHelper::NewNetwork
(
void
)
114
{
115
NS_LOG_FUNCTION
(
this
);
116
Ipv6AddressGenerator::NextNetwork
(
Ipv6Prefix
(64));
117
}
118
119
Ipv6InterfaceContainer
Ipv6AddressHelper::Assign
(
const
NetDeviceContainer
&c)
120
{
121
NS_LOG_FUNCTION
(
this
);
122
Ipv6InterfaceContainer
retval;
123
124
for
(uint32_t i = 0; i < c.
GetN
(); ++i)
125
{
126
Ptr<NetDevice>
device = c.
Get
(i);
127
128
Ptr<Node>
node = device->
GetNode
();
129
NS_ASSERT_MSG
(node,
"Ipv6AddressHelper::Allocate (): Bad node"
);
130
131
Ptr<Ipv6>
ipv6 = node->
GetObject
<
Ipv6
> ();
132
NS_ASSERT_MSG
(ipv6,
"Ipv6AddressHelper::Allocate (): Bad ipv6"
);
133
int32_t ifIndex = 0;
134
135
ifIndex = ipv6->
GetInterfaceForDevice
(device);
136
if
(ifIndex == -1)
137
{
138
ifIndex = ipv6->
AddInterface
(device);
139
}
140
NS_ASSERT_MSG
(ifIndex >= 0,
"Ipv6AddressHelper::Allocate (): "
141
"Interface index not found"
);
142
143
Ipv6InterfaceAddress
ipv6Addr =
Ipv6InterfaceAddress
(
NewAddress
(device->
GetAddress
()),
Ipv6Prefix
(64));
144
ipv6->
SetMetric
(ifIndex, 1);
145
ipv6->
AddAddress
(ifIndex, ipv6Addr);
146
ipv6->
SetUp
(ifIndex);
147
148
retval.
Add
(ipv6, ifIndex);
149
}
150
return
retval;
151
}
152
153
Ipv6InterfaceContainer
Ipv6AddressHelper::Assign
(
const
NetDeviceContainer
&c, std::vector<bool> withConfiguration)
154
{
155
NS_LOG_FUNCTION
(
this
);
156
Ipv6InterfaceContainer
retval;
157
for
(uint32_t i = 0; i < c.
GetN
(); ++i)
158
{
159
Ptr<NetDevice>
device = c.
Get
(i);
160
161
Ptr<Node>
node = device->
GetNode
();
162
NS_ASSERT_MSG
(node,
"Ipv6AddressHelper::Allocate (): Bad node"
);
163
164
Ptr<Ipv6>
ipv6 = node->
GetObject
<
Ipv6
> ();
165
NS_ASSERT_MSG
(ipv6,
"Ipv6AddressHelper::Allocate (): Bad ipv6"
);
166
167
int32_t ifIndex = ipv6->
GetInterfaceForDevice
(device);
168
if
(ifIndex == -1)
169
{
170
ifIndex = ipv6->
AddInterface
(device);
171
}
172
NS_ASSERT_MSG
(ifIndex >= 0,
"Ipv6AddressHelper::Allocate (): "
173
"Interface index not found"
);
174
175
ipv6->
SetMetric
(ifIndex, 1);
176
177
if
(withConfiguration.at (i))
178
{
179
Ipv6InterfaceAddress
ipv6Addr =
Ipv6InterfaceAddress
(
NewAddress
(device->
GetAddress
()),
Ipv6Prefix
(64));
180
ipv6->
AddAddress
(ifIndex, ipv6Addr);
181
}
182
183
ipv6->
SetUp
(ifIndex);
184
retval.
Add
(ipv6, ifIndex);
185
}
186
return
retval;
187
}
188
189
// Helper API that is redundant with Assign (c, false);
190
Ipv6InterfaceContainer
Ipv6AddressHelper::AssignWithoutAddress
(
const
NetDeviceContainer
&c)
191
{
192
NS_LOG_FUNCTION
(
this
);
193
std::vector<bool> withConfiguration;
194
for
(uint32_t i = 0; i < c.
GetN
(); ++i)
195
{
196
withConfiguration.push_back (
false
);
197
}
198
return
Assign
(c, withConfiguration);
199
}
200
201
}
/* namespace ns3 */
202
src
internet
helper
ipv6-address-helper.cc
Generated on Fri Aug 30 2013 01:42:50 for ns-3 by
1.8.1.2