author | Mathieu Lacage <mathieu.lacage@sophia.inria.fr> |
Fri, 16 Feb 2007 10:04:02 +0100 | |
changeset 287 | 692ddac3794c |
parent 286 | 57e6a2006962 |
child 294 | aba9a34108d2 |
permissions | -rw-r--r-- |
242 | 1 |
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
2 |
/* |
|
3 |
* Copyright (c) 2006 INRIA |
|
4 |
* All rights reserved. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or modify |
|
7 |
* it under the terms of the GNU General Public License version 2 as |
|
8 |
* published by the Free Software Foundation; |
|
9 |
* |
|
10 |
* This program is distributed in the hope that it will be useful, |
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
* GNU General Public License for more details. |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License |
|
16 |
* along with this program; if not, write to the Free Software |
|
17 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
18 |
* |
|
19 |
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> |
|
20 |
*/ |
|
21 |
#include "ns3/packet.h" |
|
287
692ddac3794c
convert old TRACE code to use new NS_DEBUG macro
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
286
diff
changeset
|
22 |
#include "ns3/debug.h" |
242 | 23 |
#include "arp.h" |
24 |
#include "arp-header.h" |
|
25 |
#include "arp-cache.h" |
|
26 |
#include "net-device.h" |
|
27 |
#include "ipv4-interface.h" |
|
28 |
#include "node.h" |
|
29 |
#include "ipv4.h" |
|
30 |
||
287
692ddac3794c
convert old TRACE code to use new NS_DEBUG macro
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
286
diff
changeset
|
31 |
NS_DEBUG_COMPONENT_DEFINE ("Arp"); |
242 | 32 |
|
33 |
namespace ns3 { |
|
34 |
||
35 |
Arp::Arp (Node *node) |
|
36 |
: m_node (node) |
|
37 |
{} |
|
38 |
||
248
a912210e52ac
fix memory leaks for simple sample code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
242
diff
changeset
|
39 |
Arp::~Arp () |
a912210e52ac
fix memory leaks for simple sample code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
242
diff
changeset
|
40 |
{ |
a912210e52ac
fix memory leaks for simple sample code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
242
diff
changeset
|
41 |
for (CacheList::const_iterator i = m_cacheList.begin (); i != m_cacheList.end (); i++) |
a912210e52ac
fix memory leaks for simple sample code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
242
diff
changeset
|
42 |
{ |
a912210e52ac
fix memory leaks for simple sample code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
242
diff
changeset
|
43 |
delete *i; |
a912210e52ac
fix memory leaks for simple sample code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
242
diff
changeset
|
44 |
} |
a912210e52ac
fix memory leaks for simple sample code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
242
diff
changeset
|
45 |
} |
a912210e52ac
fix memory leaks for simple sample code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
242
diff
changeset
|
46 |
|
242 | 47 |
Arp * |
48 |
Arp::Copy (Node *node) |
|
49 |
{ |
|
50 |
return new Arp (node); |
|
51 |
} |
|
52 |
||
53 |
ArpCache * |
|
54 |
Arp::FindCache (NetDevice *device) |
|
55 |
{ |
|
56 |
for (CacheList::const_iterator i = m_cacheList.begin (); i != m_cacheList.end (); i++) |
|
57 |
{ |
|
58 |
if ((*i)->GetDevice () == device) |
|
59 |
{ |
|
60 |
return *i; |
|
61 |
} |
|
62 |
} |
|
63 |
Ipv4Interface *interface = m_node->GetIpv4 ()->FindInterfaceForDevice (device); |
|
64 |
ArpCache * cache = new ArpCache (device, interface); |
|
286
57e6a2006962
convert use of <cassert> to "ns3/assert.h"
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
261
diff
changeset
|
65 |
NS_ASSERT (device->IsBroadcast ()); |
242 | 66 |
device->SetLinkChangeCallback (MakeCallback (&ArpCache::Flush, cache)); |
67 |
m_cacheList.push_back (cache); |
|
68 |
return cache; |
|
69 |
} |
|
70 |
||
71 |
void |
|
72 |
Arp::Receive(Packet& packet, NetDevice *device) |
|
73 |
{ |
|
74 |
ArpCache *cache = FindCache (device); |
|
75 |
ArpHeader arp; |
|
76 |
packet.Peek (arp); |
|
77 |
packet.Remove (arp); |
|
78 |
if (arp.IsRequest () && |
|
79 |
arp.GetDestinationIpv4Address () == cache->GetInterface ()->GetAddress ()) |
|
80 |
{ |
|
287
692ddac3794c
convert old TRACE code to use new NS_DEBUG macro
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
286
diff
changeset
|
81 |
NS_DEBUG ("got request from " << arp.GetSourceIpv4Address () << " -- send reply"); |
242 | 82 |
SendArpReply (cache, arp.GetSourceIpv4Address (), |
83 |
arp.GetSourceHardwareAddress ()); |
|
84 |
} |
|
85 |
else if (arp.IsReply () && |
|
86 |
arp.GetDestinationIpv4Address ().IsEqual (cache->GetInterface ()->GetAddress ()) && |
|
87 |
arp.GetDestinationHardwareAddress ().IsEqual (device->GetAddress ())) |
|
88 |
{ |
|
89 |
Ipv4Address from = arp.GetSourceIpv4Address (); |
|
90 |
ArpCache::Entry *entry = cache->Lookup (from); |
|
91 |
if (entry != 0) |
|
92 |
{ |
|
93 |
if (entry->IsWaitReply ()) |
|
94 |
{ |
|
287
692ddac3794c
convert old TRACE code to use new NS_DEBUG macro
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
286
diff
changeset
|
95 |
NS_DEBUG ("got reply from " << arp.GetSourceIpv4Address () |
242 | 96 |
<< " for waiting entry -- flush"); |
97 |
MacAddress from_mac = arp.GetSourceHardwareAddress (); |
|
98 |
Packet waiting = entry->MarkAlive (from_mac); |
|
99 |
cache->GetInterface ()->Send (waiting, arp.GetSourceIpv4Address ()); |
|
100 |
} |
|
101 |
else |
|
102 |
{ |
|
103 |
// ignore this reply which might well be an attempt |
|
104 |
// at poisening my arp cache. |
|
287
692ddac3794c
convert old TRACE code to use new NS_DEBUG macro
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
286
diff
changeset
|
105 |
NS_DEBUG ("got reply from " << arp.GetSourceIpv4Address () << |
242 | 106 |
" for non-waiting entry -- drop"); |
107 |
// XXX report packet as dropped. |
|
108 |
} |
|
109 |
} |
|
110 |
else |
|
111 |
{ |
|
287
692ddac3794c
convert old TRACE code to use new NS_DEBUG macro
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
286
diff
changeset
|
112 |
NS_DEBUG ("got reply for unknown entry -- drop"); |
242 | 113 |
// XXX report packet as dropped. |
114 |
} |
|
115 |
} |
|
116 |
} |
|
117 |
bool |
|
118 |
Arp::Lookup (Packet &packet, Ipv4Address destination, |
|
119 |
NetDevice *device, |
|
120 |
MacAddress *hardwareDestination) |
|
121 |
{ |
|
122 |
ArpCache *cache = FindCache (device); |
|
123 |
ArpCache::Entry *entry = cache->Lookup (destination); |
|
124 |
if (entry != 0) |
|
125 |
{ |
|
126 |
if (entry->IsExpired ()) |
|
127 |
{ |
|
128 |
if (entry->IsDead ()) |
|
129 |
{ |
|
287
692ddac3794c
convert old TRACE code to use new NS_DEBUG macro
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
286
diff
changeset
|
130 |
NS_DEBUG ("dead entry for " << destination << " expired -- send arp request"); |
242 | 131 |
entry->MarkWaitReply (packet); |
132 |
SendArpRequest (cache, destination); |
|
133 |
} |
|
134 |
else if (entry->IsAlive ()) |
|
135 |
{ |
|
287
692ddac3794c
convert old TRACE code to use new NS_DEBUG macro
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
286
diff
changeset
|
136 |
NS_DEBUG ("alive entry for " << destination << " expired -- send arp request"); |
242 | 137 |
entry->MarkWaitReply (packet); |
138 |
SendArpRequest (cache, destination); |
|
139 |
} |
|
140 |
else if (entry->IsWaitReply ()) |
|
141 |
{ |
|
287
692ddac3794c
convert old TRACE code to use new NS_DEBUG macro
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
286
diff
changeset
|
142 |
NS_DEBUG ("wait reply for " << destination << " expired -- drop"); |
242 | 143 |
entry->MarkDead (); |
144 |
// XXX report packet as 'dropped' |
|
145 |
} |
|
146 |
} |
|
147 |
else |
|
148 |
{ |
|
149 |
if (entry->IsDead ()) |
|
150 |
{ |
|
287
692ddac3794c
convert old TRACE code to use new NS_DEBUG macro
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
286
diff
changeset
|
151 |
NS_DEBUG ("dead entry for " << destination << " valid -- drop"); |
242 | 152 |
// XXX report packet as 'dropped' |
153 |
} |
|
154 |
else if (entry->IsAlive ()) |
|
155 |
{ |
|
287
692ddac3794c
convert old TRACE code to use new NS_DEBUG macro
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
286
diff
changeset
|
156 |
NS_DEBUG ("alive entry for " << destination << " valid -- send"); |
242 | 157 |
*hardwareDestination = entry->GetMacAddress (); |
158 |
return true; |
|
159 |
} |
|
160 |
else if (entry->IsWaitReply ()) |
|
161 |
{ |
|
287
692ddac3794c
convert old TRACE code to use new NS_DEBUG macro
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
286
diff
changeset
|
162 |
NS_DEBUG ("wait reply for " << destination << " valid -- drop previous"); |
242 | 163 |
Packet old = entry->UpdateWaitReply (packet); |
164 |
// XXX report 'old' packet as 'dropped' |
|
165 |
} |
|
166 |
} |
|
167 |
||
168 |
} |
|
169 |
else |
|
170 |
{ |
|
171 |
// This is our first attempt to transmit data to this destination. |
|
287
692ddac3794c
convert old TRACE code to use new NS_DEBUG macro
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
286
diff
changeset
|
172 |
NS_DEBUG ("no entry for " << destination << " -- send arp request"); |
242 | 173 |
entry = cache->Add (destination); |
174 |
entry->MarkWaitReply (packet); |
|
175 |
SendArpRequest (cache, destination); |
|
176 |
} |
|
177 |
return false; |
|
178 |
} |
|
179 |
||
180 |
void |
|
181 |
Arp::SendArpRequest (ArpCache const *cache, Ipv4Address to) |
|
182 |
{ |
|
183 |
ArpHeader arp; |
|
184 |
arp.SetRequest (cache->GetDevice ()->GetAddress (), |
|
185 |
cache->GetInterface ()->GetAddress (), |
|
186 |
cache->GetDevice ()->GetBroadcast (), |
|
187 |
to); |
|
188 |
Packet packet; |
|
189 |
packet.Add (arp); |
|
190 |
cache->GetDevice ()->Send (packet, cache->GetDevice ()->GetBroadcast (), 0x0806); |
|
191 |
} |
|
192 |
||
193 |
void |
|
194 |
Arp::SendArpReply (ArpCache const *cache, Ipv4Address toIp, MacAddress toMac) |
|
195 |
{ |
|
196 |
ArpHeader arp; |
|
197 |
arp.SetReply (cache->GetDevice ()->GetAddress (), |
|
198 |
cache->GetInterface ()->GetAddress (), |
|
199 |
toMac, toIp); |
|
200 |
Packet packet; |
|
201 |
packet.Add (arp); |
|
202 |
cache->GetDevice ()->Send (packet, toMac, 0x0806); |
|
203 |
} |
|
204 |
||
205 |
}//namespace ns3 |