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 
34 TypeId
36 {
37  static TypeId tid = TypeId ("ns3::LoopbackNetDevice")
38  .SetParent<NetDevice> ()
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 {
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
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)
Definition: log.h:345
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:278
virtual Address GetAddress(void) const
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1014
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
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:336
Mac48Address m_address
NetDevice MAC address.
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition: log.h:309
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:904
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)
Definition: assert.h:86
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
NS_LOG_COMPONENT_DEFINE("LoopbackNetDevice")
virtual Ptr< Channel > GetChannel(void) const
uint32_t GetId(void) const
Definition: node.cc:104
Packet addressed oo us.
Definition: net-device.h:272
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:270
Packet addressed to multicast group.
Definition: net-device.h:276
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:611
virtual void SetAddress(Address address)
Set the address of this interface.
virtual void AddLinkChangeCallback(Callback< void > callback)