A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_LOG_COMPONENT_DEFINE ("LoopbackNetDevice");
28 
29 namespace ns3 {
30 
31 NS_OBJECT_ENSURE_REGISTERED (LoopbackNetDevice);
32 
33 TypeId
35 {
36  static TypeId tid = TypeId ("ns3::LoopbackNetDevice")
37  .SetParent<NetDevice> ()
38  .AddConstructor<LoopbackNetDevice> ()
39  ;
40  return tid;
41 }
42 
44  : m_node (0),
45  m_mtu (0xffff),
46  m_ifIndex (0),
47  m_address (Mac48Address ("00:00:00:00:00:00"))
48 {
50 }
51 
52 void
53 LoopbackNetDevice::Receive (Ptr<Packet> packet, uint16_t protocol,
54  Mac48Address to, Mac48Address from)
55 {
56  NS_LOG_FUNCTION (packet << " " << protocol << " " << to << " " << from);
57  NetDevice::PacketType packetType;
58  if (to == m_address)
59  {
60  packetType = NetDevice::PACKET_HOST;
61  }
62  else if (to.IsBroadcast ())
63  {
64  packetType = NetDevice::PACKET_HOST;
65  }
66  else if (to.IsGroup ())
67  {
68  packetType = NetDevice::PACKET_MULTICAST;
69  }
70  else
71  {
72  packetType = NetDevice::PACKET_OTHERHOST;
73  }
74  m_rxCallback (this, packet, protocol, from);
75  if (!m_promiscCallback.IsNull ())
76  {
77  m_promiscCallback (this, packet, protocol, from, to, packetType);
78  }
79 }
80 
81 void
82 LoopbackNetDevice::SetIfIndex (const uint32_t index)
83 {
84  m_ifIndex = index;
85 }
86 
87 uint32_t
89 {
90  return m_ifIndex;
91 }
92 
95 {
96  return 0;
97 }
98 
99 void
101 {
103 }
104 
105 Address
107 {
108  return m_address;
109 }
110 
111 bool
112 LoopbackNetDevice::SetMtu (const uint16_t mtu)
113 {
114  m_mtu = mtu;
115  return true;
116 }
117 
118 uint16_t
120 {
121  return m_mtu;
122 }
123 
124 bool
126 {
127  return true;
128 }
129 
130 void
132 {}
133 
134 bool
136 {
137  return true;
138 }
139 
140 Address
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 
147 bool
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 
155 Address
157 {
158  return Mac48Address::GetMulticast (multicastGroup);
159 }
160 
162 {
163  return Mac48Address::GetMulticast (addr);
164 }
165 
166 bool
168 {
169  return false;
170 }
171 
172 bool
174 {
175  return false;
176 }
177 
178 bool
179 LoopbackNetDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
180 {
181  NS_LOG_FUNCTION (packet << " " << dest << " " << protocolNumber);
183  NS_ASSERT_MSG (to == GetBroadcast () || to == m_address, "Invalid destination address");
184  Simulator::ScheduleWithContext (m_node->GetId (), Seconds (0.0), &LoopbackNetDevice::Receive, this, packet, protocolNumber, to, m_address);
185  return true;
186 }
187 
188 bool
189 LoopbackNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
190 {
191  NS_LOG_FUNCTION (packet << " " << source << " " << dest << " " << protocolNumber);
193  Mac48Address from = Mac48Address::ConvertFrom (source);
194  NS_ASSERT_MSG (to.IsBroadcast () || to == m_address, "Invalid destination address");
195  Simulator::ScheduleWithContext (m_node->GetId (), Seconds (0.0), &LoopbackNetDevice::Receive, this, packet, protocolNumber, to, from);
196  return true;
197 }
198 
199 Ptr<Node>
201 {
202  return m_node;
203 }
204 
205 void
207 {
208  m_node = node;
209 }
210 
211 bool
213 {
214  return false;
215 }
216 
217 void
219 {
220  m_rxCallback = cb;
221 }
222 
223 void
225 {
226  m_node = 0;
228 }
229 
230 
231 void
233 {
234  m_promiscCallback = cb;
235 }
236 
237 bool
239 {
240  return true;
241 }
242 
243 } // namespace ns3
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
uint32_t m_ifIndex
interface index
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
Packet addressed to someone else.
Definition: net-device.h:282
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register the class in the ns-3 factory.
Definition: object-base.h:38
virtual Address GetAddress(void) const
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1018
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
virtual void SetNode(Ptr< Node > node)
bool IsBroadcast(void) const
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: object.cc:335
Mac48Address m_address
NetDevice MAC address.
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
virtual bool IsPointToPoint(void) const
Return true if the net device is on a point-to-point link.
NetDevice::ReceiveCallback m_rxCallback
The callback used to notify higher layers that a packet has been received.
virtual bool SupportsSendFrom(void) const
a polymophic address class
Definition: address.h:86
virtual void SetPromiscReceiveCallback(PromiscReceiveCallback cb)
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
static Mac48Address GetMulticast(Ipv4Address address)
uint16_t m_mtu
device MTU
virtual bool IsBroadcast(void) const
NetDevice::PromiscReceiveCallback m_promiscCallback
The callback used to notify higher layers that a packet has been received in promiscuous mode...
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
virtual bool IsLinkUp(void) const
virtual uint16_t GetMtu(void) const
static void ScheduleWithContext(uint32_t context, Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event with the given context.
Definition: simulator.h:905
static Mac48Address ConvertFrom(const Address &address)
bool IsGroup(void) const
an EUI-48 address
Definition: mac48-address.h:41
virtual uint32_t GetIfIndex(void) const
virtual bool SetMtu(const uint16_t mtu)
virtual Address GetBroadcast(void) const
#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:84
Describes an IPv6 address.
Definition: ipv6-address.h:46
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
virtual Ptr< Channel > GetChannel(void) const
uint32_t GetId(void) const
Definition: node.cc:106
Packet addressed oo us.
Definition: net-device.h:276
Network layer to device interface.
Definition: net-device.h:75
virtual void SetIfIndex(const uint32_t index)
virtual bool NeedsArp(void) const
Ptr< Node > m_node
the node this NetDevice is associated with
virtual bool IsMulticast(void) const
void Receive(Ptr< Packet > packet, uint16_t protocol, Mac48Address to, Mac48Address from)
Receive a packet from tge Loopback NetDevice.
virtual Ptr< Node > GetNode(void) const
tuple address
Definition: first.py:37
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:274
Packet addressed to multicast group.
Definition: net-device.h:280
static TypeId GetTypeId(void)
Get the type ID.
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
virtual void SetAddress(Address address)
Set the address of this interface.
virtual void AddLinkChangeCallback(Callback< void > callback)