A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
27namespace ns3
28{
29
30NS_LOG_COMPONENT_DEFINE("LoopbackNetDevice");
31
32NS_OBJECT_ENSURE_REGISTERED(LoopbackNetDevice);
33
34TypeId
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
53void
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 || to.IsBroadcast())
62 {
63 packetType = NetDevice::PACKET_HOST;
64 }
65 else if (to.IsGroup())
66 {
67 packetType = NetDevice::PACKET_MULTICAST;
68 }
69 else
70 {
71 packetType = NetDevice::PACKET_OTHERHOST;
72 }
73 m_rxCallback(this, packet, protocol, from);
75 {
76 m_promiscCallback(this, packet, protocol, from, to, packetType);
77 }
78}
79
80void
82{
83 m_ifIndex = index;
84}
85
88{
89 return m_ifIndex;
90}
91
94{
95 return nullptr;
96}
97
98void
100{
102}
103
106{
107 return m_address;
108}
109
110bool
111LoopbackNetDevice::SetMtu(const uint16_t mtu)
112{
113 m_mtu = mtu;
114 return true;
115}
116
117uint16_t
119{
120 return m_mtu;
121}
122
123bool
125{
126 return true;
127}
128
129void
131{
132}
133
134bool
136{
137 return true;
138}
139
142{
143 // This is typically set to all zeros rather than all ones in real systems
144 return Mac48Address("00:00:00:00:00:00");
145}
146
147bool
149{
150 // Multicast loopback will need to be supported for outgoing
151 // datagrams but this will probably be handled in multicast sockets
152 return false;
153}
154
157{
158 return Mac48Address::GetMulticast(multicastGroup);
159}
160
163{
164 return Mac48Address::GetMulticast(addr);
165}
166
167bool
169{
170 return false;
171}
172
173bool
175{
176 return false;
177}
178
179bool
180LoopbackNetDevice::Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
181{
182 NS_LOG_FUNCTION(packet << " " << dest << " " << protocolNumber);
184 NS_ASSERT_MSG(to == GetBroadcast() || to == m_address, "Invalid destination address");
186 Seconds(0.0),
188 this,
189 packet,
190 protocolNumber,
191 to,
192 m_address);
193 return true;
194}
195
196bool
198 const Address& source,
199 const Address& dest,
200 uint16_t protocolNumber)
201{
202 NS_LOG_FUNCTION(packet << " " << source << " " << dest << " " << protocolNumber);
205 NS_ASSERT_MSG(to.IsBroadcast() || to == m_address, "Invalid destination address");
207 Seconds(0.0),
209 this,
210 packet,
211 protocolNumber,
212 to,
213 from);
214 return true;
215}
216
219{
220 return m_node;
221}
222
223void
225{
226 m_node = node;
227}
228
229bool
231{
232 return false;
233}
234
235void
237{
238 m_rxCallback = cb;
239}
240
241void
243{
244 m_node = nullptr;
246}
247
248void
250{
252}
253
254bool
256{
257 return true;
258}
259
260} // namespace ns3
a polymophic address class
Definition: address.h:101
Callback template class.
Definition: callback.h:438
bool IsNull() const
Check for null implementation.
Definition: callback.h:571
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:444
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
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:932
#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:1319
Every class exported by the ns3 library is enclosed in the ns3 namespace.