A Discrete-Event Network Simulator
API
mock-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) 2020 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 "mock-net-device.h"
21 #include "ns3/node.h"
22 #include "ns3/packet.h"
23 #include "ns3/log.h"
24 #include "ns3/pointer.h"
25 #include "ns3/trace-source-accessor.h"
26 #include "ns3/boolean.h"
27 #include "ns3/simulator.h"
28 #include "ns3/channel.h"
29 #include "ns3/mac64-address.h"
30 #include "ns3/mac48-address.h"
31 #include "ns3/mac16-address.h"
32 #include "ns3/mac8-address.h"
33 
34 namespace ns3 {
35 
36 NS_LOG_COMPONENT_DEFINE ("MockNetDevice");
37 NS_OBJECT_ENSURE_REGISTERED (MockNetDevice);
38 
39 TypeId
41 {
42  static TypeId tid = TypeId ("ns3::MockNetDevice")
43  .SetParent<NetDevice> ()
44  .SetGroupName("Network")
45  .AddConstructor<MockNetDevice> ()
46  .AddAttribute ("PointToPointMode",
47  "The device is configured in Point to Point mode",
48  BooleanValue (false),
51  ;
52  return tid;
53 }
54 
56  : m_node (0),
57  m_mtu (0xffff),
58  m_ifIndex (0),
59  m_linkUp (true)
60 {
61  NS_LOG_FUNCTION (this);
62 }
63 
64 void
65 MockNetDevice::Receive (Ptr<Packet> packet, uint16_t protocol,
66  Address to, Address from, NetDevice::PacketType packetType)
67 {
68  NS_LOG_FUNCTION (this << packet << protocol << to << from);
69 
70  if (packetType != NetDevice::PACKET_OTHERHOST)
71  {
72  m_rxCallback (this, packet, protocol, from);
73  }
74 
75  if (!m_promiscCallback.IsNull ())
76  {
77  m_promiscCallback (this, packet, protocol, from, to, packetType);
78  }
79 }
80 
81 void
82 MockNetDevice::SetIfIndex (const uint32_t index)
83 {
84  NS_LOG_FUNCTION (this << index);
85  m_ifIndex = index;
86 }
87 
88 uint32_t
90 {
91  NS_LOG_FUNCTION (this);
92  return m_ifIndex;
93 }
94 
97 {
98  NS_LOG_FUNCTION (this);
99  return 0;
100 }
101 
102 void
104 {
105  NS_LOG_FUNCTION (this << address);
106  m_address = address;
107 }
108 
109 Address
111 {
112  NS_LOG_FUNCTION (this);
113  return m_address;
114 }
115 
116 bool
117 MockNetDevice::SetMtu (const uint16_t mtu)
118 {
119  NS_LOG_FUNCTION (this << mtu);
120  m_mtu = mtu;
121  return true;
122 }
123 
124 uint16_t
126 {
127  NS_LOG_FUNCTION (this);
128  return m_mtu;
129 }
130 
131 bool
133 {
134  NS_LOG_FUNCTION (this);
135  return m_linkUp;
136 }
137 
138 void
140 {
141  NS_LOG_FUNCTION (this << &callback);
143 }
144 
145 bool
147 {
148  NS_LOG_FUNCTION (this);
149  if (m_pointToPointMode)
150  {
151  return false;
152  }
154  {
155  return false;
156  }
158  {
159  return false;
160  }
161 
162  return true;
163 }
164 
165 Address
167 {
168  NS_LOG_FUNCTION (this);
169 
171 
173  {
175  }
177  {
179  }
180 
181  return address;
182 }
183 
184 bool
186 {
187  NS_LOG_FUNCTION (this);
188  if (m_pointToPointMode)
189  {
190  return false;
191  }
193  {
194  return false;
195  }
197  {
198  return false;
199  }
200 
201  return true;
202 }
203 
204 Address
206 {
207  NS_LOG_FUNCTION (this << multicastGroup);
208 
210 
212  {
213  address = Mac48Address::GetMulticast (multicastGroup);
214  }
216  {
218  }
219 
220  return address;
221 }
222 
224 {
225  NS_LOG_FUNCTION (this << addr);
227 
229  {
231  }
233  {
235  }
236 
237  return address;
238 }
239 
240 bool
242 {
243  NS_LOG_FUNCTION (this);
244  if (m_pointToPointMode)
245  {
246  return true;
247  }
248  return false;
249 }
250 
251 bool
253 {
254  NS_LOG_FUNCTION (this);
255  return false;
256 }
257 
258 bool
259 MockNetDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
260 {
261  NS_LOG_FUNCTION (this << packet << dest << protocolNumber);
262 
263  return SendFrom (packet, m_address, dest, protocolNumber);
264 }
265 
266 bool
267 MockNetDevice::SendFrom (Ptr<Packet> p, const Address& source, const Address& dest, uint16_t protocolNumber)
268 {
269  NS_LOG_FUNCTION (this << p << source << dest << protocolNumber);
270  if (p->GetSize () > GetMtu ())
271  {
272  return false;
273  }
274 
275  if (!m_sendCallback.IsNull ())
276  {
277  m_sendCallback (this, p, protocolNumber, source, dest, NetDevice::PACKET_HOST);
278  }
279 
280  return true;
281 }
282 
283 
284 Ptr<Node>
286 {
287  NS_LOG_FUNCTION (this);
288  return m_node;
289 }
290 void
292 {
293  NS_LOG_FUNCTION (this << node);
294  m_node = node;
295 }
296 
297 bool
299 {
300  NS_LOG_FUNCTION (this);
301  if (m_pointToPointMode)
302  {
303  return false;
304  }
305  return true;
306 }
307 
308 void
310 {
311  NS_LOG_FUNCTION (this << &cb);
312  m_rxCallback = cb;
313 }
314 
315 void
317 {
318  NS_LOG_FUNCTION (this);
319  m_node = 0;
324 }
325 
326 void
328 {
329  NS_LOG_FUNCTION (this << &cb);
330  m_promiscCallback = cb;
331 }
332 
333 bool
335 {
336  NS_LOG_FUNCTION (this);
337  return true;
338 }
339 
340 void
342 {
343  NS_LOG_FUNCTION (this << &cb);
344  m_sendCallback = cb;
345 }
346 
347 
348 } // namespace ns3
static bool IsMatchingType(const Address &address)
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
AttributeValue implementation for Boolean.
Definition: boolean.h:36
Packet addressed to someone else.
Definition: net-device.h:304
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
void Receive(Ptr< Packet > packet, uint16_t protocol, Address to, Address from, NetDevice::PacketType packetType)
Pretend that a packet has been received from a connected Channel.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
static bool IsMatchingType(const Address &address)
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: boolean.h:85
static TypeId GetTypeId(void)
Get the type ID.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
virtual bool NeedsArp(void) const
void SetSendCallback(PromiscReceiveCallback cb)
Add a callback to be invoked when the MockNetDevice has a packet to "send".
static bool IsMatchingType(const Address &address)
Check that a generic Address is compatible with Mac8Address.
Definition: mac8-address.cc:63
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
virtual uint16_t GetMtu(void) const
virtual bool IsPointToPoint(void) const
Return true if the net device is on a point-to-point link.
a polymophic address class
Definition: address.h:90
NetDevice::PromiscReceiveCallback m_sendCallback
Send callback.
uint16_t m_mtu
MTU.
virtual uint32_t GetIfIndex(void) const
virtual Address GetAddress(void) const
static Mac48Address GetMulticast(Ipv4Address address)
virtual bool IsLinkUp(void) const
virtual bool IsMulticast(void) const
virtual Ptr< Channel > GetChannel(void) const
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
uint32_t m_ifIndex
Interface index.
static Mac48Address GetBroadcast(void)
virtual Address GetBroadcast(void) const
virtual bool IsBroadcast(void) const
Address m_address
MAC address.
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
virtual bool SupportsSendFrom(void) const
static Mac16Address GetBroadcast(void)
TracedCallback m_linkChangeCallbacks
List of callbacks to fire if the link changes state (up or down).
Every class exported by the ns3 library is enclosed in the ns3 namespace.
address
Definition: first.py:44
virtual void SetNode(Ptr< Node > node)
bool m_linkUp
Flag indicating whether or not the link is up.
This device assumes 48-bit mac addressing; there is also the possibility to add an ErrorModel if you ...
Ptr< Node > m_node
Node this netDevice is associated to.
Describes an IPv6 address.
Definition: ipv6-address.h:49
bool m_pointToPointMode
Enabling this will disable Broadcast and Arp.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
Packet addressed oo us.
Definition: net-device.h:298
Network layer to device interface.
Definition: net-device.h:95
virtual void SetPromiscReceiveCallback(PromiscReceiveCallback cb)
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
virtual void DoDispose(void)
Destructor implementation.
void Nullify(void)
Discard the implementation, set it to null.
Definition: callback.h:1391
virtual void SetIfIndex(const uint32_t index)
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:296
static Mac16Address GetMulticast(Ipv6Address address)
Returns the multicast address associated with an IPv6 address according to RFC 4944 Section 9...
static bool IsMatchingType(const Address &address)
NetDevice::PromiscReceiveCallback m_promiscCallback
Promiscuous receive callback.
virtual Ptr< Node > GetNode(void) const
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1386
virtual bool SetMtu(const uint16_t mtu)
static Ipv6Address MakeIpv4MappedAddress(Ipv4Address addr)
Make the Ipv4-mapped IPv6 address.
virtual void AddLinkChangeCallback(Callback< void > callback)
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
NetDevice::ReceiveCallback m_rxCallback
Receive callback.
virtual void SetAddress(Address address)
Set the address of this interface.
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).