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/mac48-address.h"
27
#include "ns3/ipv6.h"
28
#include "ns3/ipv6-address-generator.h"
29
30
#include "
ipv6-address-helper.h
"
31
32
namespace
ns3
33
{
34
35
NS_LOG_COMPONENT_DEFINE
(
"Ipv6AddressHelper"
);
36
37
Ipv6AddressHelper::Ipv6AddressHelper
()
38
{
39
NS_LOG_FUNCTION
(
this
);
40
Ipv6AddressGenerator::Init
(
Ipv6Address
(
"2001:db8::"
),
Ipv6Prefix
(64));
41
}
42
43
Ipv6AddressHelper::Ipv6AddressHelper
(
Ipv6Address
network,
Ipv6Prefix
prefix,
44
Ipv6Address
base)
45
{
46
NS_LOG_FUNCTION
(
this
<< network << prefix << base);
47
Ipv6AddressGenerator::Init
(network, prefix, base);
48
}
49
50
void
Ipv6AddressHelper::SetBase
(
Ipv6Address
network,
Ipv6Prefix
prefix,
51
Ipv6Address
base)
52
{
53
NS_LOG_FUNCTION
(
this
<< network << prefix << base);
54
// XXX for now we do not enforce the prefix because the underlying
55
// Ipv6AddressGenerator does not handle prefixes well that are not 64 bits
56
Ipv6AddressGenerator::Init
(network,
Ipv6Prefix
(64), base);
57
}
58
59
60
Ipv6Address
Ipv6AddressHelper::NewAddress
(
Address
addr)
61
{
62
NS_LOG_FUNCTION
(
this
<< addr);
63
if
(
Mac48Address::IsMatchingType
(addr))
64
{
65
Ipv6Address
network =
Ipv6AddressGenerator::GetNetwork
(
Ipv6Prefix
(64));
66
Ipv6Address
address =
Ipv6Address::MakeAutoconfiguredAddress
(
Mac48Address::ConvertFrom
(addr), network);
67
Ipv6AddressGenerator::AddAllocated
(address);
68
return
address;
69
}
70
else
71
{
72
NS_FATAL_ERROR
(
"Did not pass in a Mac48Address"
);
73
}
74
/* never reached */
75
return
Ipv6Address
(
"::"
);
76
}
77
78
Ipv6Address
Ipv6AddressHelper::NewAddress
(
void
)
79
{
80
NS_LOG_FUNCTION
(
this
);
81
//
82
// The way this is expected to be used is that an address and network number
83
// are initialized, and then NewAddress() is called repeatedly to allocate and
84
// get new addresses on a given subnet. The client will expect that the first
85
// address she gets back is the one she used to initialize the generator with.
86
// This implies that this operation is a post-increment.
87
//
88
return
Ipv6AddressGenerator::NextAddress
(
Ipv6Prefix
(64));
89
}
90
91
void
Ipv6AddressHelper::NewNetwork
(
Ipv6Address
network,
Ipv6Prefix
prefix)
92
{
93
NS_LOG_FUNCTION
(
this
<< network << prefix);
94
SetBase
(network,
Ipv6Prefix
(64));
95
}
96
97
void
Ipv6AddressHelper::NewNetwork
(
void
)
98
{
99
NS_LOG_FUNCTION
(
this
);
100
Ipv6AddressGenerator::NextNetwork
(
Ipv6Prefix
(64));
101
}
102
103
Ipv6InterfaceContainer
Ipv6AddressHelper::Assign
(
const
NetDeviceContainer
&c)
104
{
105
NS_LOG_FUNCTION
(
this
);
106
Ipv6InterfaceContainer
retval;
107
108
for
(uint32_t i = 0; i < c.
GetN
(); ++i)
109
{
110
Ptr<NetDevice>
device = c.
Get
(i);
111
112
Ptr<Node>
node = device->
GetNode
();
113
NS_ASSERT_MSG
(node,
"Ipv6AddressHelper::Allocate (): Bad node"
);
114
115
Ptr<Ipv6>
ipv6 = node->
GetObject
<
Ipv6
> ();
116
NS_ASSERT_MSG
(ipv6,
"Ipv6AddressHelper::Allocate (): Bad ipv6"
);
117
int32_t ifIndex = 0;
118
119
ifIndex = ipv6->
GetInterfaceForDevice
(device);
120
if
(ifIndex == -1)
121
{
122
ifIndex = ipv6->
AddInterface
(device);
123
}
124
NS_ASSERT_MSG
(ifIndex >= 0,
"Ipv6AddressHelper::Allocate (): "
125
"Interface index not found"
);
126
127
Ipv6InterfaceAddress
ipv6Addr =
Ipv6InterfaceAddress
(
NewAddress
(device->
GetAddress
()),
Ipv6Prefix
(64));
128
ipv6->
SetMetric
(ifIndex, 1);
129
ipv6->
AddAddress
(ifIndex, ipv6Addr);
130
ipv6->
SetUp
(ifIndex);
131
132
retval.
Add
(ipv6, ifIndex);
133
}
134
return
retval;
135
}
136
137
Ipv6InterfaceContainer
Ipv6AddressHelper::Assign
(
const
NetDeviceContainer
&c, std::vector<bool> withConfiguration)
138
{
139
NS_LOG_FUNCTION
(
this
);
140
Ipv6InterfaceContainer
retval;
141
for
(uint32_t i = 0; i < c.
GetN
(); ++i)
142
{
143
Ptr<NetDevice>
device = c.
Get
(i);
144
145
Ptr<Node>
node = device->
GetNode
();
146
NS_ASSERT_MSG
(node,
"Ipv6AddressHelper::Allocate (): Bad node"
);
147
148
Ptr<Ipv6>
ipv6 = node->
GetObject
<
Ipv6
> ();
149
NS_ASSERT_MSG
(ipv6,
"Ipv6AddressHelper::Allocate (): Bad ipv6"
);
150
151
int32_t ifIndex = ipv6->
GetInterfaceForDevice
(device);
152
if
(ifIndex == -1)
153
{
154
ifIndex = ipv6->
AddInterface
(device);
155
}
156
NS_ASSERT_MSG
(ifIndex >= 0,
"Ipv6AddressHelper::Allocate (): "
157
"Interface index not found"
);
158
159
ipv6->
SetMetric
(ifIndex, 1);
160
161
if
(withConfiguration.at (i))
162
{
163
Ipv6InterfaceAddress
ipv6Addr =
Ipv6InterfaceAddress
(
NewAddress
(device->
GetAddress
()),
Ipv6Prefix
(64));
164
ipv6->
AddAddress
(ifIndex, ipv6Addr);
165
}
166
167
ipv6->
SetUp
(ifIndex);
168
retval.
Add
(ipv6, ifIndex);
169
}
170
return
retval;
171
}
172
173
// Helper API that is redundant with Assign (c, false);
174
Ipv6InterfaceContainer
Ipv6AddressHelper::AssignWithoutAddress
(
const
NetDeviceContainer
&c)
175
{
176
NS_LOG_FUNCTION
(
this
);
177
std::vector<bool> withConfiguration;
178
for
(uint32_t i = 0; i < c.
GetN
(); ++i)
179
{
180
withConfiguration.push_back (
false
);
181
}
182
return
Assign
(c, withConfiguration);
183
}
184
185
}
/* namespace ns3 */
186
src
internet
helper
ipv6-address-helper.cc
Generated on Tue May 14 2013 11:08:21 for ns-3 by
1.8.1.2