A Discrete-Event Network Simulator
API
ipv4-interface.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005,2006,2007 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 
21 #include "ipv4-interface.h"
22 #include "loopback-net-device.h"
23 #include "ipv4-l3-protocol.h"
24 #include "ipv4-queue-disc-item.h"
25 #include "arp-l3-protocol.h"
26 #include "arp-cache.h"
27 #include "ns3/net-device.h"
28 #include "ns3/log.h"
29 #include "ns3/packet.h"
30 #include "ns3/node.h"
31 #include "ns3/pointer.h"
32 
33 namespace ns3 {
34 
35 NS_LOG_COMPONENT_DEFINE ("Ipv4Interface");
36 
37 NS_OBJECT_ENSURE_REGISTERED (Ipv4Interface);
38 
39 TypeId
41 {
42  static TypeId tid = TypeId ("ns3::Ipv4Interface")
43  .SetParent<Object> ()
44  .SetGroupName ("Internet")
45  .AddAttribute ("ArpCache",
46  "The arp cache for this ipv4 interface",
47  PointerValue (0),
50  MakePointerChecker<ArpCache> ())
51  ;
52  ;
53  return tid;
54 }
55 
62  : m_ifup (false),
63  m_forwarding (true),
64  m_metric (1),
65  m_node (0),
66  m_device (0),
67  m_tc (0),
68  m_cache (0)
69 {
70  NS_LOG_FUNCTION (this);
71 }
72 
74 {
75  NS_LOG_FUNCTION (this);
76 }
77 
78 void
80 {
81  NS_LOG_FUNCTION (this);
82  m_node = 0;
83  m_device = 0;
84  m_tc = 0;
85  m_cache = 0;
87 }
88 
89 void
91 {
92  NS_LOG_FUNCTION (this << node);
93  m_node = node;
94  DoSetup ();
95 }
96 
97 void
99 {
100  NS_LOG_FUNCTION (this << device);
101  m_device = device;
102  DoSetup ();
103 }
104 
105 void
107 {
108  NS_LOG_FUNCTION (this << tc);
109  m_tc = tc;
110 }
111 
112 void
114 {
115  NS_LOG_FUNCTION (this);
116  if (m_node == 0 || m_device == 0)
117  {
118  return;
119  }
120  if (!m_device->NeedsArp ())
121  {
122  return;
123  }
125  m_cache = arp->CreateCache (m_device, this);
126 }
127 
130 {
131  NS_LOG_FUNCTION (this);
132  return m_device;
133 }
134 
135 void
136 Ipv4Interface::SetMetric (uint16_t metric)
137 {
138  NS_LOG_FUNCTION (this << metric);
139  m_metric = metric;
140 }
141 
142 uint16_t
144 {
145  NS_LOG_FUNCTION (this);
146  return m_metric;
147 }
148 
149 void
151 {
152  NS_LOG_FUNCTION (this << a);
153  m_cache = a;
154 }
155 
158 {
159  NS_LOG_FUNCTION (this);
160  return m_cache;
161 }
162 
168 bool
170 {
171  NS_LOG_FUNCTION (this);
172  return m_ifup;
173 }
174 
175 bool
177 {
178  NS_LOG_FUNCTION (this);
179  return !m_ifup;
180 }
181 
182 void
184 {
185  NS_LOG_FUNCTION (this);
186  m_ifup = true;
187 }
188 
189 void
191 {
192  NS_LOG_FUNCTION (this);
193  m_ifup = false;
194 }
195 
196 bool
198 {
199  NS_LOG_FUNCTION (this);
200  return m_forwarding;
201 }
202 
203 void
205 {
206  NS_LOG_FUNCTION (this << val);
207  m_forwarding = val;
208 }
209 
210 void
212 {
213  NS_LOG_FUNCTION (this << *p << dest);
214  if (!IsUp ())
215  {
216  return;
217  }
218 
219  // Check for a loopback device, if it's the case we don't pass through
220  // traffic control layer
221  if (DynamicCast<LoopbackNetDevice> (m_device))
222  {
225  p->AddHeader (hdr);
226  m_device->Send (p, m_device->GetBroadcast (), Ipv4L3Protocol::PROT_NUMBER);
227  return;
228  }
229 
230  NS_ASSERT (m_tc != 0);
231 
232  // is this packet aimed at a local interface ?
233  for (Ipv4InterfaceAddressListCI i = m_ifaddrs.begin (); i != m_ifaddrs.end (); ++i)
234  {
235  if (dest == (*i).GetLocal ())
236  {
237  p->AddHeader (hdr);
239  m_device->GetBroadcast (),
240  m_device->GetBroadcast (),
242  return;
243  }
244  }
245  if (m_device->NeedsArp ())
246  {
247  NS_LOG_LOGIC ("Needs ARP" << " " << dest);
249  Address hardwareDestination;
250  bool found = false;
251  if (dest.IsBroadcast ())
252  {
253  NS_LOG_LOGIC ("All-network Broadcast");
254  hardwareDestination = m_device->GetBroadcast ();
255  found = true;
256  }
257  else if (dest.IsMulticast ())
258  {
259  NS_LOG_LOGIC ("IsMulticast");
260  NS_ASSERT_MSG (m_device->IsMulticast (),
261  "ArpIpv4Interface::SendTo (): Sending multicast packet over "
262  "non-multicast device");
263 
264  hardwareDestination = m_device->GetMulticast (dest);
265  found = true;
266  }
267  else
268  {
269  for (Ipv4InterfaceAddressListCI i = m_ifaddrs.begin (); i != m_ifaddrs.end (); ++i)
270  {
271  if (dest.IsSubnetDirectedBroadcast ((*i).GetMask ()))
272  {
273  NS_LOG_LOGIC ("Subnetwork Broadcast");
274  hardwareDestination = m_device->GetBroadcast ();
275  found = true;
276  break;
277  }
278  }
279  if (!found)
280  {
281  NS_LOG_LOGIC ("ARP Lookup");
282  found = arp->Lookup (p, hdr, dest, m_device, m_cache, &hardwareDestination);
283  }
284  }
285 
286  if (found)
287  {
288  NS_LOG_LOGIC ("Address Resolved. Send.");
289  m_tc->Send (m_device, Create<Ipv4QueueDiscItem> (p, hardwareDestination, Ipv4L3Protocol::PROT_NUMBER, hdr));
290  }
291  }
292  else
293  {
294  NS_LOG_LOGIC ("Doesn't need ARP");
295  m_tc->Send (m_device, Create<Ipv4QueueDiscItem> (p, m_device->GetBroadcast (), Ipv4L3Protocol::PROT_NUMBER, hdr));
296  }
297 }
298 
299 uint32_t
301 {
302  NS_LOG_FUNCTION (this);
303  return m_ifaddrs.size ();
304 }
305 
306 bool
308 {
309  NS_LOG_FUNCTION (this << addr);
310  m_ifaddrs.push_back (addr);
311  return true;
312 }
313 
315 Ipv4Interface::GetAddress (uint32_t index) const
316 {
317  NS_LOG_FUNCTION (this << index);
318  if (index < m_ifaddrs.size ())
319  {
320  uint32_t tmp = 0;
321  for (Ipv4InterfaceAddressListCI i = m_ifaddrs.begin (); i!= m_ifaddrs.end (); i++)
322  {
323  if (tmp == index)
324  {
325  return *i;
326  }
327  ++tmp;
328  }
329  }
330  NS_ASSERT (false); // Assert if not found
332  return (addr); // quiet compiler
333 }
334 
337 {
338  NS_LOG_FUNCTION (this << index);
339  if (index >= m_ifaddrs.size ())
340  {
341  NS_ASSERT_MSG (false, "Bug in Ipv4Interface::RemoveAddress");
342  }
344  uint32_t tmp = 0;
345  while (i != m_ifaddrs.end ())
346  {
347  if (tmp == index)
348  {
349  Ipv4InterfaceAddress addr = *i;
350  m_ifaddrs.erase (i);
351  return addr;
352  }
353  ++tmp;
354  ++i;
355  }
356  NS_ASSERT_MSG (false, "Address " << index << " not found");
358  return (addr); // quiet compiler
359 }
360 
363 {
364  NS_LOG_FUNCTION(this << address);
365 
366  if (address == address.GetLoopback())
367  {
368  NS_LOG_WARN ("Cannot remove loopback address.");
369  return Ipv4InterfaceAddress();
370  }
371 
372  for(Ipv4InterfaceAddressListI it = m_ifaddrs.begin(); it != m_ifaddrs.end(); it++)
373  {
374  if((*it).GetLocal() == address)
375  {
376  Ipv4InterfaceAddress ifAddr = *it;
377  m_ifaddrs.erase(it);
378  return ifAddr;
379  }
380  }
381  return Ipv4InterfaceAddress();
382 }
383 
384 } // namespace ns3
385 
void SetDown(void)
Disable this interface.
void SetForwarding(bool val)
Ipv4Interface()
By default, Ipv4 interface are created in the "down" state with no IP addresses.
void Send(Ptr< Packet > p, const Ipv4Header &hdr, Ipv4Address dest)
#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:44
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:455
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
Ptr< ArpCache > GetArpCache() const
bool IsMulticast(void) const
Ipv4InterfaceAddress RemoveAddress(uint32_t index)
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:339
bool AddAddress(Ipv4InterfaceAddress address)
Packet addressed oo us.
Definition: net-device.h:540
Ptr< NetDevice > m_device
The associated NetDevice.
Ptr< ArpCache > m_cache
ARP cache.
void SetNode(Ptr< Node > node)
Set node associated with interface.
a polymophic address class
Definition: address.h:90
std::list< Ipv4InterfaceAddress >::const_iterator Ipv4InterfaceAddressListCI
Container Iterator for the Ipv4InterfaceAddresses.
bool m_forwarding
Forwarding state.
bool IsSubnetDirectedBroadcast(Ipv4Mask const &mask) const
Generate subnet-directed broadcast address corresponding to mask.
Packet header for IPv4.
Definition: ipv4-header.h:31
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: pointer.h:220
static TypeId GetTypeId(void)
Get the type ID.
void SetTrafficControl(Ptr< TrafficControlLayer > tc)
Set the TrafficControlLayer.
bool IsBroadcast(void) const
Ptr< TrafficControlLayer > m_tc
The associated TrafficControlLayer.
void SetDevice(Ptr< NetDevice > device)
Set the NetDevice.
Ipv4InterfaceAddressList m_ifaddrs
Address list.
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:252
Ipv4InterfaceAddress GetAddress(uint32_t index) const
virtual ~Ipv4Interface()
bool IsDown(void) const
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Hold objects of type Ptr.
Definition: pointer.h:36
std::list< Ipv4InterfaceAddress >::iterator Ipv4InterfaceAddressListI
Const Container Iterator for the Ipv4InterfaceAddresses.
static Ipv4Address GetLoopback(void)
Ptr< Node > m_node
The associated node.
uint16_t GetMetric(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:90
uint16_t m_metric
Interface metric.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
void SetArpCache(Ptr< ArpCache > arpCache)
Set ARP cache used by this interface.
a class to store IPv4 address information on an interface
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:228
Ptr< NetDevice > GetDevice(void) const
bool m_ifup
The state of this interface.
An implementation of the ARP protocol.
void SetMetric(uint16_t metric)
A base class which provides memory management and object aggregation.
Definition: object.h:87
tuple address
Definition: first.py:37
void DoSetup(void)
Initialize interface.
virtual void DoDispose(void)
Destructor implementation.
uint32_t GetNAddresses(void) const
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:826
bool IsForwarding(void) const
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:257
static const uint16_t PROT_NUMBER
Protocol number (0x0800)
bool IsUp(void) const
These are IP interface states and may be distinct from NetDevice states, such as found in real implem...
void SetUp(void)
Enable this interface.