A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ipv4-interface.cc
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 "ns3/ipv4-address.h"
24 #include "ipv4-l3-protocol.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 NS_LOG_COMPONENT_DEFINE ("Ipv4Interface");
34 
35 namespace ns3 {
36 
37 NS_OBJECT_ENSURE_REGISTERED (Ipv4Interface);
38 
39 TypeId
40 Ipv4Interface::GetTypeId (void)
41 {
42  static TypeId tid = TypeId ("ns3::Ipv4Interface")
43  .SetParent<Object> ()
44  .AddAttribute ("ArpCache",
45  "The arp cache for this ipv4 interface",
46  PointerValue (0),
47  MakePointerAccessor (&Ipv4Interface::SetArpCache,
49  MakePointerChecker<ArpCache> ())
50  ;
51  ;
52  return tid;
53 }
54 
61  : m_ifup (false),
62  m_forwarding (true),
63  m_metric (1),
64  m_node (0),
65  m_device (0),
66  m_cache (0)
67 {
68  NS_LOG_FUNCTION (this);
69 }
70 
71 Ipv4Interface::~Ipv4Interface ()
72 {
73  NS_LOG_FUNCTION (this);
74 }
75 
76 void
78 {
79  NS_LOG_FUNCTION (this);
80  m_node = 0;
81  m_device = 0;
83 }
84 
85 void
86 Ipv4Interface::SetNode (Ptr<Node> node)
87 {
88  NS_LOG_FUNCTION (this << node);
89  m_node = node;
90  DoSetup ();
91 }
92 
93 void
94 Ipv4Interface::SetDevice (Ptr<NetDevice> device)
95 {
96  NS_LOG_FUNCTION (this << device);
97  m_device = device;
98  DoSetup ();
99 }
100 
101 void
102 Ipv4Interface::DoSetup (void)
103 {
104  NS_LOG_FUNCTION (this);
105  if (m_node == 0 || m_device == 0)
106  {
107  return;
108  }
109  if (!m_device->NeedsArp ())
110  {
111  return;
112  }
113  Ptr<ArpL3Protocol> arp = m_node->GetObject<ArpL3Protocol> ();
114  m_cache = arp->CreateCache (m_device, this);
115 }
116 
117 Ptr<NetDevice>
119 {
120  NS_LOG_FUNCTION (this);
121  return m_device;
122 }
123 
124 void
125 Ipv4Interface::SetMetric (uint16_t metric)
126 {
127  NS_LOG_FUNCTION (this << metric);
128  m_metric = metric;
129 }
130 
131 uint16_t
133 {
134  NS_LOG_FUNCTION (this);
135  return m_metric;
136 }
137 
138 void
139 Ipv4Interface::SetArpCache (Ptr<ArpCache> a)
140 {
141  NS_LOG_FUNCTION (this << a);
142  m_cache = a;
143 }
144 
145 Ptr<ArpCache>
147 {
148  NS_LOG_FUNCTION (this);
149  return m_cache;
150 }
151 
157 bool
159 {
160  NS_LOG_FUNCTION (this);
161  return m_ifup;
162 }
163 
164 bool
166 {
167  NS_LOG_FUNCTION (this);
168  return !m_ifup;
169 }
170 
171 void
173 {
174  NS_LOG_FUNCTION (this);
175  m_ifup = true;
176 }
177 
178 void
180 {
181  NS_LOG_FUNCTION (this);
182  m_ifup = false;
183 }
184 
185 bool
187 {
188  NS_LOG_FUNCTION (this);
189  return m_forwarding;
190 }
191 
192 void
194 {
195  NS_LOG_FUNCTION (this << val);
196  m_forwarding = val;
197 }
198 
199 void
201 {
202  NS_LOG_FUNCTION (this << *p << dest);
203  if (!IsUp ())
204  {
205  return;
206  }
207  // Check for a loopback device
208  if (DynamicCast<LoopbackNetDevice> (m_device))
209  {
210  // XXX additional checks needed here (such as whether multicast
211  // goes to loopback)?
212  m_device->Send (p, m_device->GetBroadcast (),
213  Ipv4L3Protocol::PROT_NUMBER);
214  return;
215  }
216  // is this packet aimed at a local interface ?
217  for (Ipv4InterfaceAddressListCI i = m_ifaddrs.begin (); i != m_ifaddrs.end (); ++i)
218  {
219  if (dest == (*i).GetLocal ())
220  {
221  Ptr<Ipv4L3Protocol> ipv4 = m_node->GetObject<Ipv4L3Protocol> ();
222 
223  ipv4->Receive (m_device, p, Ipv4L3Protocol::PROT_NUMBER,
224  m_device->GetBroadcast (),
225  m_device->GetBroadcast (),
226  NetDevice::PACKET_HOST // note: linux uses PACKET_LOOPBACK here
227  );
228  return;
229  }
230  }
231  if (m_device->NeedsArp ())
232  {
233  NS_LOG_LOGIC ("Needs ARP" << " " << dest);
234  Ptr<ArpL3Protocol> arp = m_node->GetObject<ArpL3Protocol> ();
235  Address hardwareDestination;
236  bool found = false;
237  if (dest.IsBroadcast ())
238  {
239  NS_LOG_LOGIC ("All-network Broadcast");
240  hardwareDestination = m_device->GetBroadcast ();
241  found = true;
242  }
243  else if (dest.IsMulticast ())
244  {
245  NS_LOG_LOGIC ("IsMulticast");
246  NS_ASSERT_MSG (m_device->IsMulticast (),
247  "ArpIpv4Interface::SendTo (): Sending multicast packet over "
248  "non-multicast device");
249 
250  hardwareDestination = m_device->GetMulticast (dest);
251  found = true;
252  }
253  else
254  {
255  for (Ipv4InterfaceAddressListCI i = m_ifaddrs.begin (); i != m_ifaddrs.end (); ++i)
256  {
257  if (dest.IsSubnetDirectedBroadcast ((*i).GetMask ()))
258  {
259  NS_LOG_LOGIC ("Subnetwork Broadcast");
260  hardwareDestination = m_device->GetBroadcast ();
261  found = true;
262  break;
263  }
264  }
265  if (!found)
266  {
267  NS_LOG_LOGIC ("ARP Lookup");
268  found = arp->Lookup (p, dest, m_device, m_cache, &hardwareDestination);
269  }
270  }
271 
272  if (found)
273  {
274  NS_LOG_LOGIC ("Address Resolved. Send.");
276  int bytes = p->PeekPacketTag(bt);
277  Ptr<NetDevice> device = m_device;
278  //if not control packet, send on TX device
279  if (!bytes && m_node->IsCognitiveRadio()) {
280  device = m_node->GetDevice(TRANSMITTER_RADIO);
281  }
282  device->Send (p, hardwareDestination,
283  Ipv4L3Protocol::PROT_NUMBER);
284  }
285  }
286  else
287  {
288  NS_LOG_LOGIC ("Doesn't need ARP");
289  m_device->Send (p, m_device->GetBroadcast (),
290  Ipv4L3Protocol::PROT_NUMBER);
291  }
292 }
293 
294 uint32_t
296 {
297  NS_LOG_FUNCTION (this);
298  return m_ifaddrs.size ();
299 }
300 
301 bool
303 {
304  NS_LOG_FUNCTION (this << addr);
305  m_ifaddrs.push_back (addr);
306  return true;
307 }
308 
310 Ipv4Interface::GetAddress (uint32_t index) const
311 {
312  NS_LOG_FUNCTION (this << index);
313  if (index < m_ifaddrs.size ())
314  {
315  uint32_t tmp = 0;
316  for (Ipv4InterfaceAddressListCI i = m_ifaddrs.begin (); i!= m_ifaddrs.end (); i++)
317  {
318  if (tmp == index)
319  {
320  return *i;
321  }
322  ++tmp;
323  }
324  }
325  NS_ASSERT (false); // Assert if not found
327  return (addr); // quiet compiler
328 }
329 
332 {
333  NS_LOG_FUNCTION (this << index);
334  if (index >= m_ifaddrs.size ())
335  {
336  NS_ASSERT_MSG (false, "Bug in Ipv4Interface::RemoveAddress");
337  }
338  Ipv4InterfaceAddressListI i = m_ifaddrs.begin ();
339  uint32_t tmp = 0;
340  while (i != m_ifaddrs.end ())
341  {
342  if (tmp == index)
343  {
344  Ipv4InterfaceAddress addr = *i;
345  m_ifaddrs.erase (i);
346  return addr;
347  }
348  ++tmp;
349  ++i;
350  }
351  NS_ASSERT_MSG (false, "Address " << index << " not found");
353  return (addr); // quiet compiler
354 }
355 
356 } // namespace ns3
357 
void SetForwarding(bool val)
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
void Send(Ptr< Packet > p, Ipv4Address dest)
#define NS_ASSERT(condition)
Definition: assert.h:64
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
Ptr< ArpCache > GetArpCache() const
bool IsMulticast(void) const
Ipv4InterfaceAddress RemoveAddress(uint32_t index)
virtual void DoDispose(void)
Definition: object.cc:335
bool AddAddress(Ipv4InterfaceAddress address)
a polymophic address class
Definition: address.h:86
bool IsSubnetDirectedBroadcast(Ipv4Mask const &mask) const
Generate subnet-directed broadcast address corresponding to mask.
bool PeekPacketTag(Tag &tag) const
Definition: packet.cc:881
bool IsBroadcast(void) const
Ptr< NetDevice > GetDevice(uint32_t index) const
Definition: node.cc:133
#define NS_LOG_LOGIC(msg)
Definition: log.h:334
Ipv4InterfaceAddress GetAddress(uint32_t index) const
bool IsDown(void) const
Implement the Ipv4 layer.
uint16_t GetMetric(void) const
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
a class to store IPv4 address information on an interface
Ptr< NetDevice > GetDevice(void) const
An implementation of the ARP protocol.
void SetMetric(uint16_t metric)
virtual void DoDispose(void)
Ptr< T > GetObject(void) const
Definition: object.h:332
uint32_t GetNAddresses(void) const
bool IsForwarding(void) const
bool IsUp(void) const