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
loopback-net-device.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2008 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
#include "
loopback-net-device.h
"
21
#include "ns3/log.h"
22
#include "ns3/simulator.h"
23
#include "ns3/channel.h"
24
#include "ns3/node.h"
25
#include "ns3/packet.h"
26
27
NS_LOG_COMPONENT_DEFINE
(
"LoopbackNetDevice"
);
28
29
namespace
ns3 {
30
31
NS_OBJECT_ENSURE_REGISTERED
(LoopbackNetDevice);
32
33
TypeId
34
LoopbackNetDevice::GetTypeId
(
void
)
35
{
36
static
TypeId
tid =
TypeId
(
"ns3::LoopbackNetDevice"
)
37
.
SetParent
<
NetDevice
> ()
38
.AddConstructor<LoopbackNetDevice> ()
39
;
40
return
tid;
41
}
42
43
LoopbackNetDevice::LoopbackNetDevice
()
44
: m_node (0),
45
m_mtu (0xffff),
46
m_ifIndex (0),
47
m_address (
Mac48Address
(
"00:00:00:00:00:00"
))
48
{
49
NS_LOG_FUNCTION_NOARGS
();
50
}
51
52
void
53
LoopbackNetDevice::Receive
(
Ptr<Packet>
packet, uint16_t protocol,
54
Mac48Address
to,
Mac48Address
from)
55
{
56
NS_LOG_FUNCTION
(packet <<
" "
<< protocol <<
" "
<< to <<
" "
<< from);
57
NetDevice::PacketType
packetType;
58
if
(to ==
m_address
)
59
{
60
packetType =
NetDevice::PACKET_HOST
;
61
}
62
else
if
(to.
IsBroadcast
())
63
{
64
packetType =
NetDevice::PACKET_HOST
;
65
}
66
else
if
(to.
IsGroup
())
67
{
68
packetType =
NetDevice::PACKET_MULTICAST
;
69
}
70
else
71
{
72
packetType =
NetDevice::PACKET_OTHERHOST
;
73
}
74
m_rxCallback
(
this
, packet, protocol, from);
75
if
(!
m_promiscCallback
.
IsNull
())
76
{
77
m_promiscCallback
(
this
, packet, protocol, from, to, packetType);
78
}
79
}
80
81
void
82
LoopbackNetDevice::SetIfIndex
(
const
uint32_t index)
83
{
84
m_ifIndex
= index;
85
}
86
87
uint32_t
88
LoopbackNetDevice::GetIfIndex
(
void
)
const
89
{
90
return
m_ifIndex
;
91
}
92
93
Ptr<Channel>
94
LoopbackNetDevice::GetChannel
(
void
)
const
95
{
96
return
0;
97
}
98
99
void
100
LoopbackNetDevice::SetAddress
(
Address
address)
101
{
102
m_address
=
Mac48Address::ConvertFrom
(address);
103
}
104
105
Address
106
LoopbackNetDevice::GetAddress
(
void
)
const
107
{
108
return
m_address
;
109
}
110
111
bool
112
LoopbackNetDevice::SetMtu
(
const
uint16_t mtu)
113
{
114
m_mtu
= mtu;
115
return
true
;
116
}
117
118
uint16_t
119
LoopbackNetDevice::GetMtu
(
void
)
const
120
{
121
return
m_mtu
;
122
}
123
124
bool
125
LoopbackNetDevice::IsLinkUp
(
void
)
const
126
{
127
return
true
;
128
}
129
130
void
131
LoopbackNetDevice::AddLinkChangeCallback
(
Callback<void>
callback)
132
{}
133
134
bool
135
LoopbackNetDevice::IsBroadcast
(
void
)
const
136
{
137
return
true
;
138
}
139
140
Address
141
LoopbackNetDevice::GetBroadcast
(
void
)
const
142
{
143
// This is typically set to all zeros rather than all ones in real systems
144
return
Mac48Address
(
"00:00:00:00:00:00"
);
145
}
146
147
bool
148
LoopbackNetDevice::IsMulticast
(
void
)
const
149
{
150
// Multicast loopback will need to be supported for outgoing
151
// datagrams but this will probably be handled in multicast sockets
152
return
false
;
153
}
154
155
Address
156
LoopbackNetDevice::GetMulticast
(
Ipv4Address
multicastGroup)
const
157
{
158
return
Mac48Address::GetMulticast
(multicastGroup);
159
}
160
161
Address
LoopbackNetDevice::GetMulticast
(
Ipv6Address
addr)
const
162
{
163
return
Mac48Address::GetMulticast
(addr);
164
}
165
166
bool
167
LoopbackNetDevice::IsPointToPoint
(
void
)
const
168
{
169
return
false
;
170
}
171
172
bool
173
LoopbackNetDevice::IsBridge
(
void
)
const
174
{
175
return
false
;
176
}
177
178
bool
179
LoopbackNetDevice::Send
(
Ptr<Packet>
packet,
const
Address
& dest, uint16_t protocolNumber)
180
{
181
NS_LOG_FUNCTION
(packet <<
" "
<< dest <<
" "
<< protocolNumber);
182
Mac48Address
to =
Mac48Address::ConvertFrom
(dest);
183
NS_ASSERT_MSG
(to ==
GetBroadcast
() || to ==
m_address
,
"Invalid destination address"
);
184
Simulator::ScheduleWithContext
(
m_node
->
GetId
(),
Seconds
(0.0), &
LoopbackNetDevice::Receive
,
this
, packet, protocolNumber, to,
m_address
);
185
return
true
;
186
}
187
188
bool
189
LoopbackNetDevice::SendFrom
(
Ptr<Packet>
packet,
const
Address
& source,
const
Address
& dest, uint16_t protocolNumber)
190
{
191
NS_LOG_FUNCTION
(packet <<
" "
<< source <<
" "
<< dest <<
" "
<< protocolNumber);
192
Mac48Address
to =
Mac48Address::ConvertFrom
(dest);
193
Mac48Address
from =
Mac48Address::ConvertFrom
(source);
194
NS_ASSERT_MSG
(to.
IsBroadcast
() || to ==
m_address
,
"Invalid destination address"
);
195
Simulator::ScheduleWithContext
(
m_node
->
GetId
(),
Seconds
(0.0), &
LoopbackNetDevice::Receive
,
this
, packet, protocolNumber, to, from);
196
return
true
;
197
}
198
199
Ptr<Node>
200
LoopbackNetDevice::GetNode
(
void
)
const
201
{
202
return
m_node
;
203
}
204
205
void
206
LoopbackNetDevice::SetNode
(
Ptr<Node>
node)
207
{
208
m_node
= node;
209
}
210
211
bool
212
LoopbackNetDevice::NeedsArp
(
void
)
const
213
{
214
return
false
;
215
}
216
217
void
218
LoopbackNetDevice::SetReceiveCallback
(
NetDevice::ReceiveCallback
cb)
219
{
220
m_rxCallback
= cb;
221
}
222
223
void
224
LoopbackNetDevice::DoDispose
(
void
)
225
{
226
m_node
= 0;
227
NetDevice::DoDispose
();
228
}
229
230
231
void
232
LoopbackNetDevice::SetPromiscReceiveCallback
(
PromiscReceiveCallback
cb)
233
{
234
m_promiscCallback
= cb;
235
}
236
237
bool
238
LoopbackNetDevice::SupportsSendFrom
(
void
)
const
239
{
240
return
true
;
241
}
242
243
}
// namespace ns3
src
internet
model
loopback-net-device.cc
Generated on Tue Nov 13 2012 10:32:15 for ns-3 by
1.8.1.2