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
simple-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 "
simple-net-device.h
"
21
#include "
simple-channel.h
"
22
#include "ns3/node.h"
23
#include "ns3/packet.h"
24
#include "ns3/log.h"
25
#include "ns3/pointer.h"
26
#include "ns3/error-model.h"
27
#include "ns3/trace-source-accessor.h"
28
29
NS_LOG_COMPONENT_DEFINE
(
"SimpleNetDevice"
);
30
31
namespace
ns3 {
32
33
NS_OBJECT_ENSURE_REGISTERED
(SimpleNetDevice);
34
35
TypeId
36
SimpleNetDevice::GetTypeId
(
void
)
37
{
38
static
TypeId
tid =
TypeId
(
"ns3::SimpleNetDevice"
)
39
.
SetParent
<
NetDevice
> ()
40
.AddConstructor<SimpleNetDevice> ()
41
.AddAttribute (
"ReceiveErrorModel"
,
42
"The receiver error model used to simulate packet loss"
,
43
PointerValue
(),
44
MakePointerAccessor (&
SimpleNetDevice::m_receiveErrorModel
),
45
MakePointerChecker<ErrorModel> ())
46
.AddTraceSource (
"PhyRxDrop"
,
47
"Trace source indicating a packet has been dropped by the device during reception"
,
48
MakeTraceSourceAccessor
(&
SimpleNetDevice::m_phyRxDropTrace
))
49
;
50
return
tid;
51
}
52
53
SimpleNetDevice::SimpleNetDevice
()
54
: m_channel (0),
55
m_node (0),
56
m_mtu (0xffff),
57
m_ifIndex (0)
58
{
59
NS_LOG_FUNCTION
(
this
);
60
}
61
62
void
63
SimpleNetDevice::Receive
(
Ptr<Packet>
packet, uint16_t protocol,
64
Mac48Address
to,
Mac48Address
from)
65
{
66
NS_LOG_FUNCTION
(
this
<< packet << protocol << to << from);
67
NetDevice::PacketType
packetType;
68
69
if
(
m_receiveErrorModel
&&
m_receiveErrorModel
->
IsCorrupt
(packet) )
70
{
71
m_phyRxDropTrace
(packet);
72
return
;
73
}
74
75
if
(to ==
m_address
)
76
{
77
packetType =
NetDevice::PACKET_HOST
;
78
}
79
else
if
(to.
IsBroadcast
())
80
{
81
packetType =
NetDevice::PACKET_HOST
;
82
}
83
else
if
(to.
IsGroup
())
84
{
85
packetType =
NetDevice::PACKET_MULTICAST
;
86
}
87
else
88
{
89
packetType =
NetDevice::PACKET_OTHERHOST
;
90
}
91
m_rxCallback
(
this
, packet, protocol, from);
92
if
(!
m_promiscCallback
.
IsNull
())
93
{
94
m_promiscCallback
(
this
, packet, protocol, from, to, packetType);
95
}
96
}
97
98
void
99
SimpleNetDevice::SetChannel
(
Ptr<SimpleChannel>
channel)
100
{
101
NS_LOG_FUNCTION
(
this
<< channel);
102
m_channel
= channel;
103
m_channel
->
Add
(
this
);
104
}
105
106
void
107
SimpleNetDevice::SetReceiveErrorModel
(
Ptr<ErrorModel>
em)
108
{
109
NS_LOG_FUNCTION
(
this
<< em);
110
m_receiveErrorModel
= em;
111
}
112
113
void
114
SimpleNetDevice::SetIfIndex
(
const
uint32_t index)
115
{
116
NS_LOG_FUNCTION
(
this
<< index);
117
m_ifIndex
= index;
118
}
119
uint32_t
120
SimpleNetDevice::GetIfIndex
(
void
)
const
121
{
122
NS_LOG_FUNCTION
(
this
);
123
return
m_ifIndex
;
124
}
125
Ptr<Channel>
126
SimpleNetDevice::GetChannel
(
void
)
const
127
{
128
NS_LOG_FUNCTION
(
this
);
129
return
m_channel
;
130
}
131
void
132
SimpleNetDevice::SetAddress
(
Address
address)
133
{
134
NS_LOG_FUNCTION
(
this
<< address);
135
m_address
=
Mac48Address::ConvertFrom
(address);
136
}
137
Address
138
SimpleNetDevice::GetAddress
(
void
)
const
139
{
140
//
141
// Implicit conversion from Mac48Address to Address
142
//
143
NS_LOG_FUNCTION
(
this
);
144
return
m_address
;
145
}
146
bool
147
SimpleNetDevice::SetMtu
(
const
uint16_t mtu)
148
{
149
NS_LOG_FUNCTION
(
this
<< mtu);
150
m_mtu
= mtu;
151
return
true
;
152
}
153
uint16_t
154
SimpleNetDevice::GetMtu
(
void
)
const
155
{
156
NS_LOG_FUNCTION
(
this
);
157
return
m_mtu
;
158
}
159
bool
160
SimpleNetDevice::IsLinkUp
(
void
)
const
161
{
162
NS_LOG_FUNCTION
(
this
);
163
return
true
;
164
}
165
void
166
SimpleNetDevice::AddLinkChangeCallback
(
Callback<void>
callback)
167
{
168
NS_LOG_FUNCTION
(
this
<< &callback);
169
}
170
bool
171
SimpleNetDevice::IsBroadcast
(
void
)
const
172
{
173
NS_LOG_FUNCTION
(
this
);
174
return
true
;
175
}
176
Address
177
SimpleNetDevice::GetBroadcast
(
void
)
const
178
{
179
NS_LOG_FUNCTION
(
this
);
180
return
Mac48Address
(
"ff:ff:ff:ff:ff:ff"
);
181
}
182
bool
183
SimpleNetDevice::IsMulticast
(
void
)
const
184
{
185
NS_LOG_FUNCTION
(
this
);
186
return
false
;
187
}
188
Address
189
SimpleNetDevice::GetMulticast
(
Ipv4Address
multicastGroup)
const
190
{
191
NS_LOG_FUNCTION
(
this
<< multicastGroup);
192
return
Mac48Address::GetMulticast
(multicastGroup);
193
}
194
195
Address
SimpleNetDevice::GetMulticast
(
Ipv6Address
addr)
const
196
{
197
NS_LOG_FUNCTION
(
this
<< addr);
198
return
Mac48Address::GetMulticast
(addr);
199
}
200
201
bool
202
SimpleNetDevice::IsPointToPoint
(
void
)
const
203
{
204
NS_LOG_FUNCTION
(
this
);
205
return
false
;
206
}
207
208
bool
209
SimpleNetDevice::IsBridge
(
void
)
const
210
{
211
NS_LOG_FUNCTION
(
this
);
212
return
false
;
213
}
214
215
bool
216
SimpleNetDevice::Send
(
Ptr<Packet>
packet,
const
Address
& dest, uint16_t protocolNumber)
217
{
218
NS_LOG_FUNCTION
(
this
<< packet << dest << protocolNumber);
219
Mac48Address
to =
Mac48Address::ConvertFrom
(dest);
220
m_channel
->
Send
(packet, protocolNumber, to,
m_address
,
this
);
221
return
true
;
222
}
223
bool
224
SimpleNetDevice::SendFrom
(
Ptr<Packet>
packet,
const
Address
& source,
const
Address
& dest, uint16_t protocolNumber)
225
{
226
NS_LOG_FUNCTION
(
this
<< packet << source << dest << protocolNumber);
227
Mac48Address
to =
Mac48Address::ConvertFrom
(dest);
228
Mac48Address
from =
Mac48Address::ConvertFrom
(source);
229
m_channel
->
Send
(packet, protocolNumber, to, from,
this
);
230
return
true
;
231
}
232
233
Ptr<Node>
234
SimpleNetDevice::GetNode
(
void
)
const
235
{
236
NS_LOG_FUNCTION
(
this
);
237
return
m_node
;
238
}
239
void
240
SimpleNetDevice::SetNode
(
Ptr<Node>
node)
241
{
242
NS_LOG_FUNCTION
(
this
<< node);
243
m_node
= node;
244
}
245
bool
246
SimpleNetDevice::NeedsArp
(
void
)
const
247
{
248
NS_LOG_FUNCTION
(
this
);
249
return
false
;
250
}
251
void
252
SimpleNetDevice::SetReceiveCallback
(
NetDevice::ReceiveCallback
cb)
253
{
254
NS_LOG_FUNCTION
(
this
<< &cb);
255
m_rxCallback
= cb;
256
}
257
258
void
259
SimpleNetDevice::DoDispose
(
void
)
260
{
261
NS_LOG_FUNCTION
(
this
);
262
m_channel
= 0;
263
m_node
= 0;
264
m_receiveErrorModel
= 0;
265
NetDevice::DoDispose
();
266
}
267
268
269
void
270
SimpleNetDevice::SetPromiscReceiveCallback
(
PromiscReceiveCallback
cb)
271
{
272
NS_LOG_FUNCTION
(
this
<< &cb);
273
m_promiscCallback
= cb;
274
}
275
276
bool
277
SimpleNetDevice::SupportsSendFrom
(
void
)
const
278
{
279
NS_LOG_FUNCTION
(
this
);
280
return
true
;
281
}
282
283
}
// namespace ns3
src
network
utils
simple-net-device.cc
Generated on Tue May 14 2013 11:08:31 for ns-3 by
1.8.1.2