A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
loopback-net-device.cc
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
34 LoopbackNetDevice::GetTypeId (void)
35 {
36  static TypeId tid = TypeId ("ns3::LoopbackNetDevice")
37  .SetParent<NetDevice> ()
38  .AddConstructor<LoopbackNetDevice> ()
39  ;
40  return tid;
41 }
42 
43 LoopbackNetDevice::LoopbackNetDevice ()
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 {
102  m_address = Mac48Address::ConvertFrom (address);
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)
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
virtual Address GetAddress(void) const
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
virtual void SetNode(Ptr< Node > node)
bool IsBroadcast(void) const
virtual void DoDispose(void)
Definition: object.cc:335
#define NS_LOG_FUNCTION_NOARGS()
Definition: log.h:275
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
virtual bool IsPointToPoint(void) const
Return true if the net device is on a point-to-point link.
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)
virtual bool IsBroadcast(void) const
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)
Definition: simulator.h:900
static Mac48Address ConvertFrom(const Address &address)
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:44
virtual void DoDispose(void)
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
virtual Ptr< Channel > GetChannel(void) const
uint32_t GetId(void) const
Definition: node.cc:105
virtual void SetIfIndex(const uint32_t index)
virtual bool NeedsArp(void) const
virtual bool IsMulticast(void) const
virtual Ptr< Node > GetNode(void) const
virtual void SetAddress(Address address)
virtual void AddLinkChangeCallback(Callback< void > callback)