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-interface.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2005,2006,2007 INRIA
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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19
*/
20
21
#include "
ipv4-interface.h
"
22
#include "
loopback-net-device.h
"
23
#include "ns3/ipv4-address.h"
24
#include "
ipv4-l3-protocol.h
"
25
#include "
arp-l3-protocol.h
"
26
#include "
arp-cache.h
"
27
#include "ns3/net-device.h"
28
#include "ns3/log.h"
29
#include "ns3/packet.h"
30
#include "ns3/node.h"
31
#include "ns3/pointer.h"
32
33
NS_LOG_COMPONENT_DEFINE
(
"Ipv4Interface"
);
34
35
namespace
ns3 {
36
37
NS_OBJECT_ENSURE_REGISTERED
(Ipv4Interface);
38
39
TypeId
40
Ipv4Interface::GetTypeId
(
void
)
41
{
42
static
TypeId
tid =
TypeId
(
"ns3::Ipv4Interface"
)
43
.
SetParent
<
Object
> ()
44
.AddAttribute (
"ArpCache"
,
45
"The arp cache for this ipv4 interface"
,
46
PointerValue
(0),
47
MakePointerAccessor (&
Ipv4Interface::SetArpCache
,
48
&
Ipv4Interface::GetArpCache
),
49
MakePointerChecker<ArpCache> ())
50
;
51
;
52
return
tid;
53
}
54
60
Ipv4Interface::Ipv4Interface
()
61
: m_ifup (false),
62
m_forwarding (true),
63
m_metric (1),
64
m_node (0),
65
m_device (0),
66
m_cache (0)
67
{
68
NS_LOG_FUNCTION
(
this
);
69
}
70
71
Ipv4Interface::~Ipv4Interface
()
72
{
73
NS_LOG_FUNCTION
(
this
);
74
}
75
76
void
77
Ipv4Interface::DoDispose
(
void
)
78
{
79
NS_LOG_FUNCTION
(
this
);
80
m_node
= 0;
81
m_device
= 0;
82
Object::DoDispose
();
83
}
84
85
void
86
Ipv4Interface::SetNode
(
Ptr<Node>
node)
87
{
88
NS_LOG_FUNCTION
(
this
<< node);
89
m_node
= node;
90
DoSetup
();
91
}
92
93
void
94
Ipv4Interface::SetDevice
(
Ptr<NetDevice>
device)
95
{
96
NS_LOG_FUNCTION
(
this
<< device);
97
m_device
= device;
98
DoSetup
();
99
}
100
101
void
102
Ipv4Interface::DoSetup
(
void
)
103
{
104
NS_LOG_FUNCTION
(
this
);
105
if
(
m_node
== 0 ||
m_device
== 0)
106
{
107
return
;
108
}
109
if
(!
m_device
->
NeedsArp
())
110
{
111
return
;
112
}
113
Ptr<ArpL3Protocol>
arp =
m_node
->
GetObject
<
ArpL3Protocol
> ();
114
m_cache
= arp->CreateCache (
m_device
,
this
);
115
}
116
117
Ptr<NetDevice>
118
Ipv4Interface::GetDevice
(
void
)
const
119
{
120
NS_LOG_FUNCTION
(
this
);
121
return
m_device
;
122
}
123
124
void
125
Ipv4Interface::SetMetric
(uint16_t metric)
126
{
127
NS_LOG_FUNCTION
(
this
<< metric);
128
m_metric
= metric;
129
}
130
131
uint16_t
132
Ipv4Interface::GetMetric
(
void
)
const
133
{
134
NS_LOG_FUNCTION
(
this
);
135
return
m_metric
;
136
}
137
138
void
139
Ipv4Interface::SetArpCache
(
Ptr<ArpCache>
a)
140
{
141
NS_LOG_FUNCTION
(
this
<< a);
142
m_cache
= a;
143
}
144
145
Ptr<ArpCache>
146
Ipv4Interface::GetArpCache
()
const
147
{
148
NS_LOG_FUNCTION
(
this
);
149
return
m_cache
;
150
}
151
157
bool
158
Ipv4Interface::IsUp
(
void
)
const
159
{
160
NS_LOG_FUNCTION
(
this
);
161
return
m_ifup
;
162
}
163
164
bool
165
Ipv4Interface::IsDown
(
void
)
const
166
{
167
NS_LOG_FUNCTION
(
this
);
168
return
!
m_ifup
;
169
}
170
171
void
172
Ipv4Interface::SetUp
(
void
)
173
{
174
NS_LOG_FUNCTION
(
this
);
175
m_ifup
=
true
;
176
}
177
178
void
179
Ipv4Interface::SetDown
(
void
)
180
{
181
NS_LOG_FUNCTION
(
this
);
182
m_ifup
=
false
;
183
}
184
185
bool
186
Ipv4Interface::IsForwarding
(
void
)
const
187
{
188
NS_LOG_FUNCTION
(
this
);
189
return
m_forwarding
;
190
}
191
192
void
193
Ipv4Interface::SetForwarding
(
bool
val)
194
{
195
NS_LOG_FUNCTION
(
this
<< val);
196
m_forwarding
= val;
197
}
198
199
void
200
Ipv4Interface::Send
(
Ptr<Packet>
p,
Ipv4Address
dest)
201
{
202
NS_LOG_FUNCTION
(
this
<< *p << dest);
203
if
(!
IsUp
())
204
{
205
return
;
206
}
207
// Check for a loopback device
208
if
(DynamicCast<LoopbackNetDevice> (
m_device
))
209
{
210
// XXX additional checks needed here (such as whether multicast
211
// goes to loopback)?
212
m_device
->
Send
(p,
m_device
->
GetBroadcast
(),
213
Ipv4L3Protocol::PROT_NUMBER
);
214
return
;
215
}
216
// is this packet aimed at a local interface ?
217
for
(
Ipv4InterfaceAddressListCI
i =
m_ifaddrs
.begin (); i !=
m_ifaddrs
.end (); ++i)
218
{
219
if
(dest == (*i).GetLocal ())
220
{
221
Ptr<Ipv4L3Protocol>
ipv4 =
m_node
->
GetObject
<
Ipv4L3Protocol
> ();
222
223
ipv4->
Receive
(
m_device
, p,
Ipv4L3Protocol::PROT_NUMBER
,
224
m_device
->
GetBroadcast
(),
225
m_device
->
GetBroadcast
(),
226
NetDevice::PACKET_HOST
// note: linux uses PACKET_LOOPBACK here
227
);
228
return
;
229
}
230
}
231
if
(
m_device
->
NeedsArp
())
232
{
233
NS_LOG_LOGIC
(
"Needs ARP"
<<
" "
<< dest);
234
Ptr<ArpL3Protocol>
arp =
m_node
->
GetObject
<
ArpL3Protocol
> ();
235
Address
hardwareDestination;
236
bool
found =
false
;
237
if
(dest.
IsBroadcast
())
238
{
239
NS_LOG_LOGIC
(
"All-network Broadcast"
);
240
hardwareDestination =
m_device
->
GetBroadcast
();
241
found =
true
;
242
}
243
else
if
(dest.
IsMulticast
())
244
{
245
NS_LOG_LOGIC
(
"IsMulticast"
);
246
NS_ASSERT_MSG
(
m_device
->
IsMulticast
(),
247
"ArpIpv4Interface::SendTo (): Sending multicast packet over "
248
"non-multicast device"
);
249
250
hardwareDestination =
m_device
->
GetMulticast
(dest);
251
found =
true
;
252
}
253
else
254
{
255
for
(
Ipv4InterfaceAddressListCI
i =
m_ifaddrs
.begin (); i !=
m_ifaddrs
.end (); ++i)
256
{
257
if
(dest.
IsSubnetDirectedBroadcast
((*i).GetMask ()))
258
{
259
NS_LOG_LOGIC
(
"Subnetwork Broadcast"
);
260
hardwareDestination =
m_device
->
GetBroadcast
();
261
found =
true
;
262
break
;
263
}
264
}
265
if
(!found)
266
{
267
NS_LOG_LOGIC
(
"ARP Lookup"
);
268
found = arp->Lookup (p, dest,
m_device
,
m_cache
, &hardwareDestination);
269
}
270
}
271
272
if
(found)
273
{
274
NS_LOG_LOGIC
(
"Address Resolved. Send."
);
275
m_device
->
Send
(p, hardwareDestination,
276
Ipv4L3Protocol::PROT_NUMBER
);
277
}
278
}
279
else
280
{
281
NS_LOG_LOGIC
(
"Doesn't need ARP"
);
282
m_device
->
Send
(p,
m_device
->
GetBroadcast
(),
283
Ipv4L3Protocol::PROT_NUMBER
);
284
}
285
}
286
287
uint32_t
288
Ipv4Interface::GetNAddresses
(
void
)
const
289
{
290
NS_LOG_FUNCTION
(
this
);
291
return
m_ifaddrs
.size ();
292
}
293
294
bool
295
Ipv4Interface::AddAddress
(
Ipv4InterfaceAddress
addr)
296
{
297
NS_LOG_FUNCTION
(
this
<< addr);
298
m_ifaddrs
.push_back (addr);
299
return
true
;
300
}
301
302
Ipv4InterfaceAddress
303
Ipv4Interface::GetAddress
(uint32_t index)
const
304
{
305
NS_LOG_FUNCTION
(
this
<< index);
306
if
(index <
m_ifaddrs
.size ())
307
{
308
uint32_t tmp = 0;
309
for
(
Ipv4InterfaceAddressListCI
i =
m_ifaddrs
.begin (); i!=
m_ifaddrs
.end (); i++)
310
{
311
if
(tmp == index)
312
{
313
return
*i;
314
}
315
++tmp;
316
}
317
}
318
NS_ASSERT
(
false
);
// Assert if not found
319
Ipv4InterfaceAddress
addr;
320
return
(addr);
// quiet compiler
321
}
322
323
Ipv4InterfaceAddress
324
Ipv4Interface::RemoveAddress
(uint32_t index)
325
{
326
NS_LOG_FUNCTION
(
this
<< index);
327
if
(index >=
m_ifaddrs
.size ())
328
{
329
NS_ASSERT_MSG
(
false
,
"Bug in Ipv4Interface::RemoveAddress"
);
330
}
331
Ipv4InterfaceAddressListI
i =
m_ifaddrs
.begin ();
332
uint32_t tmp = 0;
333
while
(i !=
m_ifaddrs
.end ())
334
{
335
if
(tmp == index)
336
{
337
Ipv4InterfaceAddress
addr = *i;
338
m_ifaddrs
.erase (i);
339
return
addr;
340
}
341
++tmp;
342
++i;
343
}
344
NS_ASSERT_MSG
(
false
,
"Address "
<< index <<
" not found"
);
345
Ipv4InterfaceAddress
addr;
346
return
(addr);
// quiet compiler
347
}
348
349
}
// namespace ns3
350
src
internet
model
ipv4-interface.cc
Generated on Tue May 14 2013 11:08:22 for ns-3 by
1.8.1.2