A Discrete-Event Network Simulator
API
loopback-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) 2008 INRIA
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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include "loopback-net-device.h"
21 #include "ns3/log.h"
22 #include "ns3/simulator.h"
23 #include "ns3/channel.h"
24 #include "ns3/node.h"
25 #include "ns3/packet.h"
26 
27 namespace ns3 {
28 
29 NS_LOG_COMPONENT_DEFINE ("LoopbackNetDevice");
30 
31 NS_OBJECT_ENSURE_REGISTERED (LoopbackNetDevice);
32 
33 TypeId
35 {
36  static TypeId tid = TypeId ("ns3::LoopbackNetDevice")
37  .SetParent<NetDevice> ()
38  .SetGroupName ("Internet")
39  .AddConstructor<LoopbackNetDevice> ()
40  ;
41  return tid;
42 }
43 
45  : m_node (0),
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
54 LoopbackNetDevice::Receive (Ptr<Packet> packet, uint16_t protocol,
55  Mac48Address to, Mac48Address from)
56 {
57  NS_LOG_FUNCTION (packet << " " << protocol << " " << to << " " << from);
58  NetDevice::PacketType packetType;
59  if (to == m_address)
60  {
61  packetType = NetDevice::PACKET_HOST;
62  }
63  else if (to.IsBroadcast ())
64  {
65  packetType = NetDevice::PACKET_HOST;
66  }
67  else if (to.IsGroup ())
68  {
69  packetType = NetDevice::PACKET_MULTICAST;
70  }
71  else
72  {
73  packetType = NetDevice::PACKET_OTHERHOST;
74  }
75  m_rxCallback (this, packet, protocol, from);
76  if (!m_promiscCallback.IsNull ())
77  {
78  m_promiscCallback (this, packet, protocol, from, to, packetType);
79  }
80 }
81 
82 void
83 LoopbackNetDevice::SetIfIndex (const uint32_t index)
84 {
85  m_ifIndex = index;
86 }
87 
88 uint32_t
90 {
91  return m_ifIndex;
92 }
93 
96 {
97  return 0;
98 }
99 
100 void
102 {
104 }
105 
106 Address
108 {
109  return m_address;
110 }
111 
112 bool
113 LoopbackNetDevice::SetMtu (const uint16_t mtu)
114 {
115  m_mtu = mtu;
116  return true;
117 }
118 
119 uint16_t
121 {
122  return m_mtu;
123 }
124 
125 bool
127 {
128  return true;
129 }
130 
131 void
133 {}
134 
135 bool
137 {
138  return true;
139 }
140 
141 Address
143 {
144  // This is typically set to all zeros rather than all ones in real systems
145  return Mac48Address ("00:00:00:00:00:00");
146 }
147 
148 bool
150 {
151  // Multicast loopback will need to be supported for outgoing
152  // datagrams but this will probably be handled in multicast sockets
153  return false;
154 }
155 
156 Address
158 {
159  return Mac48Address::GetMulticast (multicastGroup);
160 }
161 
163 {
164  return Mac48Address::GetMulticast (addr);
165 }
166 
167 bool
169 {
170  return false;
171 }
172 
173 bool
175 {
176  return false;
177 }
178 
179 bool
180 LoopbackNetDevice::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");
185  Simulator::ScheduleWithContext (m_node->GetId (), Seconds (0.0), &LoopbackNetDevice::Receive, this, packet, protocolNumber, to, m_address);
186  return true;
187 }
188 
189 bool
190 LoopbackNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
191 {
192  NS_LOG_FUNCTION (packet << " " << source << " " << dest << " " << protocolNumber);
194  Mac48Address from = Mac48Address::ConvertFrom (source);
195  NS_ASSERT_MSG (to.IsBroadcast () || to == m_address, "Invalid destination address");
196  Simulator::ScheduleWithContext (m_node->GetId (), Seconds (0.0), &LoopbackNetDevice::Receive, this, packet, protocolNumber, to, from);
197  return true;
198 }
199 
200 Ptr<Node>
202 {
203  return m_node;
204 }
205 
206 void
208 {
209  m_node = node;
210 }
211 
212 bool
214 {
215  return false;
216 }
217 
218 void
220 {
221  m_rxCallback = cb;
222 }
223 
224 void
226 {
227  m_node = 0;
229 }
230 
231 
232 void
234 {
235  m_promiscCallback = cb;
236 }
237 
238 bool
240 {
241  return true;
242 }
243 
244 } // namespace ns3
ns3::TypeId
a unique identifier for an interface.
Definition: type-id.h:59
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
loopback-net-device.h
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
ns3::LoopbackNetDevice::SupportsSendFrom
virtual bool SupportsSendFrom(void) const
Definition: loopback-net-device.cc:239
ns3::Mac48Address::GetMulticast
static Mac48Address GetMulticast(Ipv4Address address)
Definition: mac48-address.cc:191
ns3::Simulator::ScheduleWithContext
static void ScheduleWithContext(uint32_t context, Time const &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:571
ns3::LoopbackNetDevice::NeedsArp
virtual bool NeedsArp(void) const
Definition: loopback-net-device.cc:213
ns3::Mac48Address::IsGroup
bool IsGroup(void) const
Definition: mac48-address.cc:164
ns3::LoopbackNetDevice::SetIfIndex
virtual void SetIfIndex(const uint32_t index)
Definition: loopback-net-device.cc:83
ns3::Node::GetId
uint32_t GetId(void) const
Definition: node.cc:109
ns3::Callback< void >
ns3::LoopbackNetDevice::Send
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
Definition: loopback-net-device.cc:180
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::LoopbackNetDevice::IsMulticast
virtual bool IsMulticast(void) const
Definition: loopback-net-device.cc:149
ns3::Callback::IsNull
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1386
ns3::LoopbackNetDevice::DoDispose
virtual void DoDispose(void)
Destructor implementation.
Definition: loopback-net-device.cc:225
ns3::Ipv4Address
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
ns3::Ipv6Address
Describes an IPv6 address.
Definition: ipv6-address.h:50
ns3::NetDevice::PACKET_HOST
@ PACKET_HOST
Packet addressed oo us.
Definition: net-device.h:298
ns3::Mac48Address
an EUI-48 address
Definition: mac48-address.h:44
ns3::LoopbackNetDevice::GetNode
virtual Ptr< Node > GetNode(void) const
Definition: loopback-net-device.cc:201
ns3::LoopbackNetDevice::SetNode
virtual void SetNode(Ptr< Node > node)
Definition: loopback-net-device.cc:207
ns3::LoopbackNetDevice::GetChannel
virtual Ptr< Channel > GetChannel(void) const
Definition: loopback-net-device.cc:95
ns3::LoopbackNetDevice::IsBroadcast
virtual bool IsBroadcast(void) const
Definition: loopback-net-device.cc:136
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
ns3::LoopbackNetDevice::SetReceiveCallback
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
Definition: loopback-net-device.cc:219
ns3::LoopbackNetDevice::GetMtu
virtual uint16_t GetMtu(void) const
Definition: loopback-net-device.cc:120
ns3::LoopbackNetDevice::m_node
Ptr< Node > m_node
the node this NetDevice is associated with
Definition: loopback-net-device.h:103
ns3::LoopbackNetDevice::SendFrom
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
Definition: loopback-net-device.cc:190
ns3::LoopbackNetDevice::Receive
void Receive(Ptr< Packet > packet, uint16_t protocol, Mac48Address to, Mac48Address from)
Receive a packet from tge Loopback NetDevice.
Definition: loopback-net-device.cc:54
ns3::Ptr< Packet >
ns3::NetDevice::PACKET_OTHERHOST
@ PACKET_OTHERHOST
Packet addressed to someone else.
Definition: net-device.h:304
ns3::Address
a polymophic address class
Definition: address.h:91
ns3::Mac48Address::ConvertFrom
static Mac48Address ConvertFrom(const Address &address)
Definition: mac48-address.cc:126
ns3::Mac48Address::IsBroadcast
bool IsBroadcast(void) const
Definition: mac48-address.cc:158
ns3::LoopbackNetDevice::AddLinkChangeCallback
virtual void AddLinkChangeCallback(Callback< void > callback)
Definition: loopback-net-device.cc:132
ns3::LoopbackNetDevice::SetAddress
virtual void SetAddress(Address address)
Set the address of this interface.
Definition: loopback-net-device.cc:101
first.address
address
Definition: first.py:44
NS_ASSERT_MSG
#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:88
ns3::LoopbackNetDevice::GetBroadcast
virtual Address GetBroadcast(void) const
Definition: loopback-net-device.cc:142
ns3::LoopbackNetDevice::GetTypeId
static TypeId GetTypeId(void)
Get the type ID.
Definition: loopback-net-device.cc:34
ns3::LoopbackNetDevice::IsBridge
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
Definition: loopback-net-device.cc:174
ns3::LoopbackNetDevice::m_mtu
uint16_t m_mtu
device MTU
Definition: loopback-net-device.h:104
ns3::LoopbackNetDevice::SetMtu
virtual bool SetMtu(const uint16_t mtu)
Definition: loopback-net-device.cc:113
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::LoopbackNetDevice::IsLinkUp
virtual bool IsLinkUp(void) const
Definition: loopback-net-device.cc:126
ns3::LoopbackNetDevice::GetAddress
virtual Address GetAddress(void) const
Definition: loopback-net-device.cc:107
ns3::LoopbackNetDevice::m_ifIndex
uint32_t m_ifIndex
interface index
Definition: loopback-net-device.h:105
ns3::LoopbackNetDevice::IsPointToPoint
virtual bool IsPointToPoint(void) const
Return true if the net device is on a point-to-point link.
Definition: loopback-net-device.cc:168
ns3::LoopbackNetDevice::m_rxCallback
NetDevice::ReceiveCallback m_rxCallback
The callback used to notify higher layers that a packet has been received.
Definition: loopback-net-device.h:96
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Definition: log-macros-enabled.h:244
ns3::LoopbackNetDevice::GetIfIndex
virtual uint32_t GetIfIndex(void) const
Definition: loopback-net-device.cc:89
ns3::LoopbackNetDevice::GetMulticast
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
Definition: loopback-net-device.cc:157
ns3::NetDevice::PacketType
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:297
ns3::LoopbackNetDevice::m_address
Mac48Address m_address
NetDevice MAC address.
Definition: loopback-net-device.h:106
ns3::Object::DoDispose
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
ns3::NetDevice
Network layer to device interface.
Definition: net-device.h:96
ns3::LoopbackNetDevice::LoopbackNetDevice
LoopbackNetDevice()
Definition: loopback-net-device.cc:44
ns3::LoopbackNetDevice
Virtual network interface that loops back any data sent to it to be immediately received on the same ...
Definition: loopback-net-device.h:43
ns3::LoopbackNetDevice::m_promiscCallback
NetDevice::PromiscReceiveCallback m_promiscCallback
The callback used to notify higher layers that a packet has been received in promiscuous mode.
Definition: loopback-net-device.h:101
ns3::LoopbackNetDevice::SetPromiscReceiveCallback
virtual void SetPromiscReceiveCallback(PromiscReceiveCallback cb)
Definition: loopback-net-device.cc:233
ns3::NetDevice::PACKET_MULTICAST
@ PACKET_MULTICAST
Packet addressed to multicast group.
Definition: net-device.h:302