A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
error-net-device.cc
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011 Universita' di Firenze, Italy
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: Tommaso Pecorella <tommaso.pecorella@unifi.it>
19  */
20 #include "error-net-device.h"
21 #include "error-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 ("ErrorNetDevice");
30 
31 namespace ns3 {
32 
33 NS_OBJECT_ENSURE_REGISTERED (ErrorNetDevice);
34 
35 TypeId
36 ErrorNetDevice::GetTypeId (void)
37 {
38  static TypeId tid = TypeId ("ns3::ErrorNetDevice")
39  .SetParent<NetDevice> ()
40  .AddConstructor<ErrorNetDevice> ()
41  .AddAttribute ("ReceiveErrorModel",
42  "The receiver error model used to simulate packet loss",
43  PointerValue (),
44  MakePointerAccessor (&ErrorNetDevice::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 ErrorNetDevice::ErrorNetDevice ()
54  : m_channel (0),
55  m_node (0),
56  m_mtu (0xffff),
57  m_ifIndex (0)
58 {}
59 
60 void
61 ErrorNetDevice::Receive (Ptr<Packet> packet, uint16_t protocol,
62  Mac48Address to, Mac48Address from)
63 {
64  NS_LOG_FUNCTION (packet << protocol << to << from << *packet);
65  NetDevice::PacketType packetType;
66 
67  if (m_receiveErrorModel && m_receiveErrorModel->IsCorrupt (packet) )
68  {
69  m_phyRxDropTrace (packet);
70  return;
71  }
72 
73  if (to == m_address)
74  {
75  packetType = NetDevice::PACKET_HOST;
76  }
77  else if (to.IsBroadcast ())
78  {
79  packetType = NetDevice::PACKET_HOST;
80  }
81  else if (to.IsGroup ())
82  {
83  packetType = NetDevice::PACKET_MULTICAST;
84  }
85  else
86  {
87  packetType = NetDevice::PACKET_OTHERHOST;
88  }
89  m_rxCallback (this, packet, protocol, from);
90  if (!m_promiscCallback.IsNull ())
91  {
92  m_promiscCallback (this, packet, protocol, from, to, packetType);
93  }
94 }
95 
96 void
97 ErrorNetDevice::SetChannel (Ptr<ErrorChannel> channel)
98 {
99  m_channel = channel;
100  m_channel->Add (this);
101 }
102 
103 void
105 {
106  m_receiveErrorModel = em;
107 }
108 
109 void
110 ErrorNetDevice::SetIfIndex(const uint32_t index)
111 {
112  m_ifIndex = index;
113 }
114 uint32_t
116 {
117  return m_ifIndex;
118 }
121 {
122  return m_channel;
123 }
124 void
126 {
127  m_address = Mac48Address::ConvertFrom(address);
128 }
129 Address
131 {
132  //
133  // Implicit conversion from Mac48Address to Address
134  //
135  return m_address;
136 }
137 bool
138 ErrorNetDevice::SetMtu (const uint16_t mtu)
139 {
140  m_mtu = mtu;
141  return true;
142 }
143 uint16_t
145 {
146  return m_mtu;
147 }
148 bool
150 {
151  return true;
152 }
153 void
155 {}
156 bool
158 {
159  return true;
160 }
161 Address
163 {
164  return Mac48Address ("ff:ff:ff:ff:ff:ff");
165 }
166 bool
168 {
169  return false;
170 }
171 Address
173 {
174  return Mac48Address::GetMulticast (multicastGroup);
175 }
176 
178 {
179  return Mac48Address::GetMulticast (addr);
180 }
181 
182 bool
184 {
185  return false;
186 }
187 
188 bool
190 {
191  return false;
192 }
193 
194 bool
195 ErrorNetDevice::Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
196 {
197  NS_LOG_FUNCTION (packet << dest << protocolNumber << *packet);
199  m_channel->Send (packet, protocolNumber, to, m_address, this);
200  return true;
201 }
202 bool
203 ErrorNetDevice::SendFrom(Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
204 {
205  NS_LOG_FUNCTION (packet << source << dest << protocolNumber << *packet);
207  Mac48Address from = Mac48Address::ConvertFrom (source);
208  m_channel->Send (packet, protocolNumber, to, from, this);
209  return true;
210 }
211 
212 Ptr<Node>
214 {
215  return m_node;
216 }
217 void
219 {
220  m_node = node;
221 }
222 bool
224 {
225  return false;
226 }
227 void
229 {
230  m_rxCallback = cb;
231 }
232 
233 void
235 {
236  m_channel = 0;
237  m_node = 0;
238  m_receiveErrorModel = 0;
240 }
241 
242 
243 void
245 {
246  m_promiscCallback = cb;
247 }
248 
249 bool
251 {
252  return true;
253 }
254 
255 } // namespace ns3
virtual uint32_t GetIfIndex(void) const
virtual Address GetBroadcast(void) const
virtual void SetPromiscReceiveCallback(PromiscReceiveCallback cb)
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
virtual void DoDispose(void)
Definition: object.cc:335
virtual bool SetMtu(const uint16_t mtu)
virtual bool IsBroadcast(void) const
virtual bool IsLinkUp(void) const
virtual Address GetAddress(void) const
virtual void SetIfIndex(const uint32_t index)
a polymophic address class
Definition: address.h:86
static Mac48Address GetMulticast(Ipv4Address address)
virtual bool IsPointToPoint(void) const
Return true if the net device is on a point-to-point link.
virtual bool SupportsSendFrom(void) const
virtual Ptr< Channel > GetChannel(void) const
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
static Mac48Address ConvertFrom(const Address &address)
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
virtual uint16_t GetMtu(void) const
virtual void SetNode(Ptr< Node > node)
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
void SetReceiveErrorModel(Ptr< ErrorModel > em)
an EUI-48 address
Definition: mac48-address.h:41
virtual void SetAddress(Address address)
TracedCallback< Ptr< const Packet > > m_phyRxDropTrace
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
Describes an IPv6 address.
Definition: ipv6-address.h:44
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
virtual Ptr< Node > GetNode(void) const
virtual bool NeedsArp(void) const
virtual void DoDispose(void)
virtual void AddLinkChangeCallback(Callback< void > callback)
virtual bool IsMulticast(void) const
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)