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
ipv4-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 University of Washington
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
19
#include "ns3/assert.h"
20
#include "ns3/log.h"
21
#include "ns3/ptr.h"
22
#include "ns3/node.h"
23
#include "ns3/net-device.h"
24
#include "ns3/ipv4.h"
25
#include "ns3/ipv4-address-generator.h"
26
#include "ns3/simulator.h"
27
#include "
ipv4-address-helper.h
"
28
29
NS_LOG_COMPONENT_DEFINE
(
"Ipv4AddressHelper"
);
30
31
namespace
ns3 {
32
33
Ipv4AddressHelper::Ipv4AddressHelper
()
34
{
35
NS_LOG_FUNCTION_NOARGS
();
36
37
//
38
// Set the default values to an illegal state. Do this so the client is
39
// forced to think at least briefly about what addresses get used and what
40
// is going on here.
41
//
42
m_network
= 0xffffffff;
43
m_mask
= 0;
44
m_address
= 0xffffffff;
45
m_base
= 0xffffffff;
46
m_shift
= 0xffffffff;
47
m_max
= 0xffffffff;
48
}
49
50
Ipv4AddressHelper::Ipv4AddressHelper
(
51
const
Ipv4Address
network,
52
const
Ipv4Mask
mask,
53
const
Ipv4Address
address)
54
{
55
NS_LOG_FUNCTION_NOARGS
();
56
SetBase
(network, mask, address);
57
}
58
59
void
60
Ipv4AddressHelper::SetBase
(
61
const
Ipv4Address
network,
62
const
Ipv4Mask
mask,
63
const
Ipv4Address
address)
64
{
65
NS_LOG_FUNCTION_NOARGS
();
66
67
m_network
= network.
Get
();
68
m_mask
= mask.
Get
();
69
m_base
=
m_address
= address.
Get
();
70
71
//
72
// Some quick reasonableness testing.
73
//
74
NS_ASSERT_MSG
((
m_network
& ~
m_mask
) == 0,
75
"Ipv4AddressHelper::SetBase(): Inconsistent network and mask"
);
76
77
//
78
// Figure out how much to shift network numbers to get them aligned, and what
79
// the maximum allowed address is with respect to the current mask.
80
//
81
m_shift
=
NumAddressBits
(
m_mask
);
82
m_max
= (1 <<
m_shift
) - 2;
83
84
NS_ASSERT_MSG
(
m_shift
<= 32,
85
"Ipv4AddressHelper::SetBase(): Unreasonable address length"
);
86
87
//
88
// Shift the network down into the normalized position.
89
//
90
m_network
>>=
m_shift
;
91
92
NS_LOG_LOGIC
(
"m_network == "
<<
m_network
);
93
NS_LOG_LOGIC
(
"m_mask == "
<<
m_mask
);
94
NS_LOG_LOGIC
(
"m_address == "
<<
m_address
);
95
}
96
97
Ipv4Address
98
Ipv4AddressHelper::NewAddress
(
void
)
99
{
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
//
107
NS_ASSERT_MSG
(
m_address
<=
m_max
,
108
"Ipv4AddressHelper::NewAddress(): Address overflow"
);
109
110
Ipv4Address
addr ((
m_network
<<
m_shift
) |
m_address
);
111
++
m_address
;
112
//
113
// The Ipv4AddressGenerator allows us to keep track of the addresses we have
114
// allocated and will assert if we accidentally generate a duplicate. This
115
// avoids some really hard to debug problems.
116
//
117
Ipv4AddressGenerator::AddAllocated
(addr);
118
return
addr;
119
}
120
121
Ipv4Address
122
Ipv4AddressHelper::NewNetwork
(
void
)
123
{
124
NS_LOG_FUNCTION_NOARGS
();
125
++
m_network
;
126
m_address
=
m_base
;
127
return
Ipv4Address
(
m_network
<<
m_shift
);
128
}
129
130
Ipv4InterfaceContainer
131
Ipv4AddressHelper::Assign
(
const
NetDeviceContainer
&c)
132
{
133
NS_LOG_FUNCTION_NOARGS
();
134
Ipv4InterfaceContainer
retval;
135
for
(uint32_t i = 0; i < c.
GetN
(); ++i) {
136
Ptr<NetDevice>
device = c.
Get
(i);
137
138
Ptr<Node>
node = device->
GetNode
();
139
NS_ASSERT_MSG
(node,
"Ipv4AddressHelper::Assign(): NetDevice is not not associated "
140
"with any node -> fail"
);
141
142
Ptr<Ipv4>
ipv4 = node->
GetObject
<
Ipv4
> ();
143
NS_ASSERT_MSG
(ipv4,
"Ipv4AddressHelper::Assign(): NetDevice is associated"
144
" with a node without IPv4 stack installed -> fail "
145
"(maybe need to use InternetStackHelper?)"
);
146
147
int32_t
interface
= ipv4->GetInterfaceForDevice (device);
148
if
(interface == -1)
149
{
150
interface
= ipv4->AddInterface (device);
151
}
152
NS_ASSERT_MSG
(interface >= 0,
"Ipv4AddressHelper::Assign(): "
153
"Interface index not found"
);
154
155
Ipv4InterfaceAddress
ipv4Addr =
Ipv4InterfaceAddress
(
NewAddress
(),
m_mask
);
156
ipv4->
AddAddress
(interface, ipv4Addr);
157
ipv4->
SetMetric
(interface, 1);
158
ipv4->
SetUp
(interface);
159
retval.
Add
(ipv4, interface);
160
}
161
return
retval;
162
}
163
164
const
uint32_t
N_BITS
= 32;
165
166
uint32_t
167
Ipv4AddressHelper::NumAddressBits
(uint32_t maskbits)
const
168
{
169
NS_LOG_FUNCTION_NOARGS
();
170
for
(uint32_t i = 0; i <
N_BITS
; ++i)
171
{
172
if
(maskbits & 1)
173
{
174
NS_LOG_LOGIC
(
"NumAddressBits -> "
<< i);
175
return
i;
176
}
177
maskbits >>= 1;
178
}
179
180
NS_ASSERT_MSG
(
false
,
"Ipv4AddressHelper::NumAddressBits(): Bad Mask"
);
181
return
0;
182
}
183
184
}
// namespace ns3
185
src
internet
helper
ipv4-address-helper.cc
Generated on Tue Oct 9 2012 16:45:37 for ns-3 by
1.8.1.2