A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
simple-net-device.cc
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",
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
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
121 {
122  NS_LOG_FUNCTION (this);
123  return m_ifIndex;
124 }
127 {
128  NS_LOG_FUNCTION (this);
129  return m_channel;
130 }
131 void
133 {
134  NS_LOG_FUNCTION (this << address);
135  m_address = Mac48Address::ConvertFrom (address);
136 }
137 Address
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
155 {
156  NS_LOG_FUNCTION (this);
157  return m_mtu;
158 }
159 bool
161 {
162  NS_LOG_FUNCTION (this);
163  return true;
164 }
165 void
167 {
168  NS_LOG_FUNCTION (this << &callback);
169 }
170 bool
172 {
173  NS_LOG_FUNCTION (this);
174  return true;
175 }
176 Address
178 {
179  NS_LOG_FUNCTION (this);
180  return Mac48Address ("ff:ff:ff:ff:ff:ff");
181 }
182 bool
184 {
185  NS_LOG_FUNCTION (this);
186  return false;
187 }
188 Address
190 {
191  NS_LOG_FUNCTION (this << multicastGroup);
192  return Mac48Address::GetMulticast (multicastGroup);
193 }
194 
196 {
197  NS_LOG_FUNCTION (this << addr);
198  return Mac48Address::GetMulticast (addr);
199 }
200 
201 bool
203 {
204  NS_LOG_FUNCTION (this);
205  return false;
206 }
207 
208 bool
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);
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);
228  Mac48Address from = Mac48Address::ConvertFrom (source);
229  m_channel->Send (packet, protocolNumber, to, from, this);
230  return true;
231 }
232 
233 Ptr<Node>
235 {
236  NS_LOG_FUNCTION (this);
237  return m_node;
238 }
239 void
241 {
242  NS_LOG_FUNCTION (this << node);
243  m_node = node;
244 }
245 bool
247 {
248  NS_LOG_FUNCTION (this);
249  return false;
250 }
251 void
253 {
254  NS_LOG_FUNCTION (this << &cb);
255  m_rxCallback = cb;
256 }
257 
258 void
260 {
261  NS_LOG_FUNCTION (this);
262  m_channel = 0;
263  m_node = 0;
264  m_receiveErrorModel = 0;
266 }
267 
268 
269 void
271 {
272  NS_LOG_FUNCTION (this << &cb);
273  m_promiscCallback = cb;
274 }
275 
276 bool
278 {
279  NS_LOG_FUNCTION (this);
280  return true;
281 }
282 
283 } // namespace ns3
virtual Address GetAddress(void) const
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
virtual Ptr< Channel > GetChannel(void) const
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
virtual void SetIfIndex(const uint32_t index)
virtual void DoDispose(void)
Definition: object.cc:335
virtual bool IsBroadcast(void) const
virtual bool NeedsArp(void) const
virtual bool IsMulticast(void) const
a polymophic address class
Definition: address.h:86
virtual void SetNode(Ptr< Node > node)
static Mac48Address GetMulticast(Ipv4Address address)
virtual void SetPromiscReceiveCallback(PromiscReceiveCallback cb)
virtual bool SetMtu(const uint16_t mtu)
virtual bool IsPointToPoint(void) const
Return true if the net device is on a point-to-point link.
static Mac48Address ConvertFrom(const Address &address)
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
an EUI-48 address
Definition: mac48-address.h:41
virtual uint16_t GetMtu(void) const
void SetReceiveErrorModel(Ptr< ErrorModel > em)
virtual Ptr< Node > GetNode(void) const
Describes an IPv6 address.
Definition: ipv6-address.h:44
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
TracedCallback< Ptr< const Packet > > m_phyRxDropTrace
virtual void AddLinkChangeCallback(Callback< void > callback)
virtual void SetAddress(Address address)
virtual void DoDispose(void)
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
virtual Address GetBroadcast(void) const
virtual uint32_t GetIfIndex(void) const
virtual bool SupportsSendFrom(void) const
virtual bool IsLinkUp(void) const