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
GetTypeId(void)40 MockNetDevice::GetTypeId (void)
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),
49 MakeBooleanAccessor (&MockNetDevice::m_pointToPointMode),
50 MakeBooleanChecker ())
51 ;
52 return tid;
53 }
54
MockNetDevice()55 MockNetDevice::MockNetDevice ()
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
Receive(Ptr<Packet> packet,uint16_t protocol,Address to,Address from,NetDevice::PacketType packetType)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
SetIfIndex(const uint32_t index)82 MockNetDevice::SetIfIndex (const uint32_t index)
83 {
84 NS_LOG_FUNCTION (this << index);
85 m_ifIndex = index;
86 }
87
88 uint32_t
GetIfIndex(void) const89 MockNetDevice::GetIfIndex (void) const
90 {
91 NS_LOG_FUNCTION (this);
92 return m_ifIndex;
93 }
94
95 Ptr<Channel>
GetChannel(void) const96 MockNetDevice::GetChannel (void) const
97 {
98 NS_LOG_FUNCTION (this);
99 return 0;
100 }
101
102 void
SetAddress(Address address)103 MockNetDevice::SetAddress (Address address)
104 {
105 NS_LOG_FUNCTION (this << address);
106 m_address = address;
107 }
108
109 Address
GetAddress(void) const110 MockNetDevice::GetAddress (void) const
111 {
112 NS_LOG_FUNCTION (this);
113 return m_address;
114 }
115
116 bool
SetMtu(const uint16_t mtu)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
GetMtu(void) const125 MockNetDevice::GetMtu (void) const
126 {
127 NS_LOG_FUNCTION (this);
128 return m_mtu;
129 }
130
131 bool
IsLinkUp(void) const132 MockNetDevice::IsLinkUp (void) const
133 {
134 NS_LOG_FUNCTION (this);
135 return m_linkUp;
136 }
137
138 void
AddLinkChangeCallback(Callback<void> callback)139 MockNetDevice::AddLinkChangeCallback (Callback<void> callback)
140 {
141 NS_LOG_FUNCTION (this << &callback);
142 m_linkChangeCallbacks.ConnectWithoutContext (callback);
143 }
144
145 bool
IsBroadcast(void) const146 MockNetDevice::IsBroadcast (void) const
147 {
148 NS_LOG_FUNCTION (this);
149 if (m_pointToPointMode)
150 {
151 return false;
152 }
153 if (Mac64Address::IsMatchingType (m_address))
154 {
155 return false;
156 }
157 if (Mac8Address::IsMatchingType (m_address))
158 {
159 return false;
160 }
161
162 return true;
163 }
164
165 Address
GetBroadcast(void) const166 MockNetDevice::GetBroadcast (void) const
167 {
168 NS_LOG_FUNCTION (this);
169
170 Address address;
171
172 if (Mac48Address::IsMatchingType (m_address))
173 {
174 address = Mac48Address::GetBroadcast ();
175 }
176 else if (Mac16Address::IsMatchingType (m_address))
177 {
178 address = Mac16Address::GetBroadcast ();
179 }
180
181 return address;
182 }
183
184 bool
IsMulticast(void) const185 MockNetDevice::IsMulticast (void) const
186 {
187 NS_LOG_FUNCTION (this);
188 if (m_pointToPointMode)
189 {
190 return false;
191 }
192 if (Mac64Address::IsMatchingType (m_address))
193 {
194 return false;
195 }
196 if (Mac8Address::IsMatchingType (m_address))
197 {
198 return false;
199 }
200
201 return true;
202 }
203
204 Address
GetMulticast(Ipv4Address multicastGroup) const205 MockNetDevice::GetMulticast (Ipv4Address multicastGroup) const
206 {
207 NS_LOG_FUNCTION (this << multicastGroup);
208
209 Address address;
210
211 if (Mac48Address::IsMatchingType (m_address))
212 {
213 address = Mac48Address::GetMulticast (multicastGroup);
214 }
215 else if (Mac16Address::IsMatchingType (m_address))
216 {
217 address = Mac16Address::GetMulticast (Ipv6Address::MakeIpv4MappedAddress (multicastGroup));
218 }
219
220 return address;
221 }
222
GetMulticast(Ipv6Address addr) const223 Address MockNetDevice::GetMulticast (Ipv6Address addr) const
224 {
225 NS_LOG_FUNCTION (this << addr);
226 Address address;
227
228 if (Mac48Address::IsMatchingType (m_address))
229 {
230 address = Mac48Address::GetMulticast (addr);
231 }
232 else if (Mac16Address::IsMatchingType (m_address))
233 {
234 address = Mac16Address::GetMulticast (addr);
235 }
236
237 return address;
238 }
239
240 bool
IsPointToPoint(void) const241 MockNetDevice::IsPointToPoint (void) const
242 {
243 NS_LOG_FUNCTION (this);
244 if (m_pointToPointMode)
245 {
246 return true;
247 }
248 return false;
249 }
250
251 bool
IsBridge(void) const252 MockNetDevice::IsBridge (void) const
253 {
254 NS_LOG_FUNCTION (this);
255 return false;
256 }
257
258 bool
Send(Ptr<Packet> packet,const Address & dest,uint16_t protocolNumber)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
SendFrom(Ptr<Packet> p,const Address & source,const Address & dest,uint16_t protocolNumber)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>
GetNode(void) const285 MockNetDevice::GetNode (void) const
286 {
287 NS_LOG_FUNCTION (this);
288 return m_node;
289 }
290 void
SetNode(Ptr<Node> node)291 MockNetDevice::SetNode (Ptr<Node> node)
292 {
293 NS_LOG_FUNCTION (this << node);
294 m_node = node;
295 }
296
297 bool
NeedsArp(void) const298 MockNetDevice::NeedsArp (void) const
299 {
300 NS_LOG_FUNCTION (this);
301 if (m_pointToPointMode)
302 {
303 return false;
304 }
305 return true;
306 }
307
308 void
SetReceiveCallback(NetDevice::ReceiveCallback cb)309 MockNetDevice::SetReceiveCallback (NetDevice::ReceiveCallback cb)
310 {
311 NS_LOG_FUNCTION (this << &cb);
312 m_rxCallback = cb;
313 }
314
315 void
DoDispose(void)316 MockNetDevice::DoDispose (void)
317 {
318 NS_LOG_FUNCTION (this);
319 m_node = 0;
320 m_rxCallback.Nullify ();
321 m_promiscCallback.Nullify ();
322 m_sendCallback.Nullify ();
323 NetDevice::DoDispose ();
324 }
325
326 void
SetPromiscReceiveCallback(PromiscReceiveCallback cb)327 MockNetDevice::SetPromiscReceiveCallback (PromiscReceiveCallback cb)
328 {
329 NS_LOG_FUNCTION (this << &cb);
330 m_promiscCallback = cb;
331 }
332
333 bool
SupportsSendFrom(void) const334 MockNetDevice::SupportsSendFrom (void) const
335 {
336 NS_LOG_FUNCTION (this);
337 return true;
338 }
339
340 void
SetSendCallback(PromiscReceiveCallback cb)341 MockNetDevice::SetSendCallback (PromiscReceiveCallback cb)
342 {
343 NS_LOG_FUNCTION (this << &cb);
344 m_sendCallback = cb;
345 }
346
347
348 } // namespace ns3
349