A Discrete-Event Network Simulator
API
loopback-net-device.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008 INRIA
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18  */
19 #include "loopback-net-device.h"
20 
21 #include "ns3/channel.h"
22 #include "ns3/log.h"
23 #include "ns3/node.h"
24 #include "ns3/packet.h"
25 #include "ns3/simulator.h"
26 
27 namespace ns3
28 {
29 
30 NS_LOG_COMPONENT_DEFINE("LoopbackNetDevice");
31 
32 NS_OBJECT_ENSURE_REGISTERED(LoopbackNetDevice);
33 
34 TypeId
36 {
37  static TypeId tid = TypeId("ns3::LoopbackNetDevice")
39  .SetGroupName("Internet")
40  .AddConstructor<LoopbackNetDevice>();
41  return tid;
42 }
43 
45  : m_node(nullptr),
46  m_mtu(0xffff),
47  m_ifIndex(0),
48  m_address(Mac48Address("00:00:00:00:00:00"))
49 {
50  NS_LOG_FUNCTION(this);
51 }
52 
53 void
55  uint16_t protocol,
56  Mac48Address to,
57  Mac48Address from)
58 {
59  NS_LOG_FUNCTION(packet << " " << protocol << " " << to << " " << from);
60  NetDevice::PacketType packetType;
61  if (to == m_address)
62  {
63  packetType = NetDevice::PACKET_HOST;
64  }
65  else if (to.IsBroadcast())
66  {
67  packetType = NetDevice::PACKET_HOST;
68  }
69  else if (to.IsGroup())
70  {
71  packetType = NetDevice::PACKET_MULTICAST;
72  }
73  else
74  {
75  packetType = NetDevice::PACKET_OTHERHOST;
76  }
77  m_rxCallback(this, packet, protocol, from);
79  {
80  m_promiscCallback(this, packet, protocol, from, to, packetType);
81  }
82 }
83 
84 void
85 LoopbackNetDevice::SetIfIndex(const uint32_t index)
86 {
87  m_ifIndex = index;
88 }
89 
90 uint32_t
92 {
93  return m_ifIndex;
94 }
95 
98 {
99  return nullptr;
100 }
101 
102 void
104 {
106 }
107 
108 Address
110 {
111  return m_address;
112 }
113 
114 bool
115 LoopbackNetDevice::SetMtu(const uint16_t mtu)
116 {
117  m_mtu = mtu;
118  return true;
119 }
120 
121 uint16_t
123 {
124  return m_mtu;
125 }
126 
127 bool
129 {
130  return true;
131 }
132 
133 void
135 {
136 }
137 
138 bool
140 {
141  return true;
142 }
143 
144 Address
146 {
147  // This is typically set to all zeros rather than all ones in real systems
148  return Mac48Address("00:00:00:00:00:00");
149 }
150 
151 bool
153 {
154  // Multicast loopback will need to be supported for outgoing
155  // datagrams but this will probably be handled in multicast sockets
156  return false;
157 }
158 
159 Address
161 {
162  return Mac48Address::GetMulticast(multicastGroup);
163 }
164 
165 Address
167 {
168  return Mac48Address::GetMulticast(addr);
169 }
170 
171 bool
173 {
174  return false;
175 }
176 
177 bool
179 {
180  return false;
181 }
182 
183 bool
184 LoopbackNetDevice::Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
185 {
186  NS_LOG_FUNCTION(packet << " " << dest << " " << protocolNumber);
188  NS_ASSERT_MSG(to == GetBroadcast() || to == m_address, "Invalid destination address");
190  Seconds(0.0),
192  this,
193  packet,
194  protocolNumber,
195  to,
196  m_address);
197  return true;
198 }
199 
200 bool
202  const Address& source,
203  const Address& dest,
204  uint16_t protocolNumber)
205 {
206  NS_LOG_FUNCTION(packet << " " << source << " " << dest << " " << protocolNumber);
209  NS_ASSERT_MSG(to.IsBroadcast() || to == m_address, "Invalid destination address");
211  Seconds(0.0),
213  this,
214  packet,
215  protocolNumber,
216  to,
217  from);
218  return true;
219 }
220 
221 Ptr<Node>
223 {
224  return m_node;
225 }
226 
227 void
229 {
230  m_node = node;
231 }
232 
233 bool
235 {
236  return false;
237 }
238 
239 void
241 {
242  m_rxCallback = cb;
243 }
244 
245 void
247 {
248  m_node = nullptr;
250 }
251 
252 void
254 {
255  m_promiscCallback = cb;
256 }
257 
258 bool
260 {
261  return true;
262 }
263 
264 } // namespace ns3
a polymophic address class
Definition: address.h:101
bool IsNull() const
Check for null implementation.
Definition: callback.h:569
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
Describes an IPv6 address.
Definition: ipv6-address.h:49
Virtual network interface that loops back any data sent to it to be immediately received on the same ...
static TypeId GetTypeId()
Get the type ID.
bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber) override
void DoDispose() override
Destructor implementation.
uint32_t m_ifIndex
interface index
bool IsMulticast() const override
void Receive(Ptr< Packet > packet, uint16_t protocol, Mac48Address to, Mac48Address from)
Receive a packet from tge Loopback NetDevice.
bool IsBridge() const override
Return true if the net device is acting as a bridge.
bool NeedsArp() const override
Ptr< Node > GetNode() const override
uint16_t GetMtu() const override
Address GetMulticast(Ipv4Address multicastGroup) const override
Make and return a MAC multicast address using the provided multicast group.
Ptr< Node > m_node
the node this NetDevice is associated with
bool IsBroadcast() const override
void SetIfIndex(const uint32_t index) override
bool IsLinkUp() const override
bool IsPointToPoint() const override
Return true if the net device is on a point-to-point link.
Mac48Address m_address
NetDevice MAC address.
uint32_t GetIfIndex() const override
NetDevice::ReceiveCallback m_rxCallback
The callback used to notify higher layers that a packet has been received.
void SetReceiveCallback(NetDevice::ReceiveCallback cb) override
void AddLinkChangeCallback(Callback< void > callback) override
Address GetAddress() const override
bool SupportsSendFrom() const override
NetDevice::PromiscReceiveCallback m_promiscCallback
The callback used to notify higher layers that a packet has been received in promiscuous mode.
uint16_t m_mtu
device MTU
void SetAddress(Address address) override
Set the address of this interface.
bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber) override
void SetPromiscReceiveCallback(PromiscReceiveCallback cb) override
Ptr< Channel > GetChannel() const override
bool SetMtu(const uint16_t mtu) override
Address GetBroadcast() const override
void SetNode(Ptr< Node > node) override
an EUI-48 address
Definition: mac48-address.h:46
static Mac48Address GetMulticast(Ipv4Address address)
bool IsGroup() const
static Mac48Address ConvertFrom(const Address &address)
bool IsBroadcast() const
Network layer to device interface.
Definition: net-device.h:98
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:300
@ PACKET_HOST
Packet addressed to us.
Definition: net-device.h:301
@ PACKET_OTHERHOST
Packet addressed to someone else.
Definition: net-device.h:307
@ PACKET_MULTICAST
Packet addressed to multicast group.
Definition: net-device.h:305
uint32_t GetId() const
Definition: node.cc:117
virtual void DoDispose()
Destructor implementation.
Definition: object.cc:352
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:588
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:86
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
address
Definition: first.py:47
Every class exported by the ns3 library is enclosed in the ns3 namespace.