author | Tom Henderson <tomh@tomh.org> |
Sun, 18 Mar 2007 14:06:51 -0700 | |
changeset 345 | 47b41507a45a |
parent 321 | 452a9cdad112 |
child 463 | c2082308e01a |
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" |
345
47b41507a45a
move channel.cc channel.h to node directory; merge ns-3-tracing from mathieu
Tom Henderson <tomh@tomh.org>
parents:
321
diff
changeset
|
23 |
#include "ns3/empty-trace-resolver.h" |
242 | 24 |
#include "arp.h" |
25 |
#include "arp-header.h" |
|
26 |
#include "arp-cache.h" |
|
27 |
#include "net-device.h" |
|
28 |
#include "ipv4-interface.h" |
|
29 |
#include "node.h" |
|
30 |
#include "ipv4.h" |
|
31 |
||
287
692ddac3794c
convert old TRACE code to use new NS_DEBUG macro
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
286
diff
changeset
|
32 |
NS_DEBUG_COMPONENT_DEFINE ("Arp"); |
242 | 33 |
|
34 |
namespace ns3 { |
|
35 |
||
294
aba9a34108d2
remove ArpL3Protocol object
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
287
diff
changeset
|
36 |
const uint16_t Arp::PROT_NUMBER = 0x0806; |
aba9a34108d2
remove ArpL3Protocol object
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
287
diff
changeset
|
37 |
|
242 | 38 |
Arp::Arp (Node *node) |
294
aba9a34108d2
remove ArpL3Protocol object
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
287
diff
changeset
|
39 |
: L3Protocol (PROT_NUMBER, 0/* XXX: correct version number ? */ ), |
aba9a34108d2
remove ArpL3Protocol object
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
287
diff
changeset
|
40 |
m_node (node) |
242 | 41 |
{} |
42 |
||
248
a912210e52ac
fix memory leaks for simple sample code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
242
diff
changeset
|
43 |
Arp::~Arp () |
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 |
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
|
46 |
{ |
a912210e52ac
fix memory leaks for simple sample code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
242
diff
changeset
|
47 |
delete *i; |
a912210e52ac
fix memory leaks for simple sample code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
242
diff
changeset
|
48 |
} |
a912210e52ac
fix memory leaks for simple sample code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
242
diff
changeset
|
49 |
} |
a912210e52ac
fix memory leaks for simple sample code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
242
diff
changeset
|
50 |
|
242 | 51 |
Arp * |
294
aba9a34108d2
remove ArpL3Protocol object
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
287
diff
changeset
|
52 |
Arp::Copy (Node *node) const |
242 | 53 |
{ |
54 |
return new Arp (node); |
|
55 |
} |
|
56 |
||
345
47b41507a45a
move channel.cc channel.h to node directory; merge ns-3-tracing from mathieu
Tom Henderson <tomh@tomh.org>
parents:
321
diff
changeset
|
57 |
TraceResolver * |
47b41507a45a
move channel.cc channel.h to node directory; merge ns-3-tracing from mathieu
Tom Henderson <tomh@tomh.org>
parents:
321
diff
changeset
|
58 |
Arp::CreateTraceResolver (TraceContext const &context) |
47b41507a45a
move channel.cc channel.h to node directory; merge ns-3-tracing from mathieu
Tom Henderson <tomh@tomh.org>
parents:
321
diff
changeset
|
59 |
{ |
47b41507a45a
move channel.cc channel.h to node directory; merge ns-3-tracing from mathieu
Tom Henderson <tomh@tomh.org>
parents:
321
diff
changeset
|
60 |
return new EmptyTraceResolver (context); |
47b41507a45a
move channel.cc channel.h to node directory; merge ns-3-tracing from mathieu
Tom Henderson <tomh@tomh.org>
parents:
321
diff
changeset
|
61 |
} |
47b41507a45a
move channel.cc channel.h to node directory; merge ns-3-tracing from mathieu
Tom Henderson <tomh@tomh.org>
parents:
321
diff
changeset
|
62 |
|
242 | 63 |
ArpCache * |
64 |
Arp::FindCache (NetDevice *device) |
|
65 |
{ |
|
66 |
for (CacheList::const_iterator i = m_cacheList.begin (); i != m_cacheList.end (); i++) |
|
67 |
{ |
|
68 |
if ((*i)->GetDevice () == device) |
|
69 |
{ |
|
70 |
return *i; |
|
71 |
} |
|
72 |
} |
|
73 |
Ipv4Interface *interface = m_node->GetIpv4 ()->FindInterfaceForDevice (device); |
|
74 |
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
|
75 |
NS_ASSERT (device->IsBroadcast ()); |
242 | 76 |
device->SetLinkChangeCallback (MakeCallback (&ArpCache::Flush, cache)); |
77 |
m_cacheList.push_back (cache); |
|
78 |
return cache; |
|
79 |
} |
|
80 |
||
81 |
void |
|
294
aba9a34108d2
remove ArpL3Protocol object
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
287
diff
changeset
|
82 |
Arp::Receive(Packet& packet, NetDevice &device) |
242 | 83 |
{ |
294
aba9a34108d2
remove ArpL3Protocol object
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
287
diff
changeset
|
84 |
ArpCache *cache = FindCache (&device); |
242 | 85 |
ArpHeader arp; |
86 |
packet.Peek (arp); |
|
87 |
packet.Remove (arp); |
|
88 |
if (arp.IsRequest () && |
|
89 |
arp.GetDestinationIpv4Address () == cache->GetInterface ()->GetAddress ()) |
|
90 |
{ |
|
321
452a9cdad112
improve arp debugging output
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
303
diff
changeset
|
91 |
NS_DEBUG ("node="<<m_node->GetId () <<", got request from " << |
452a9cdad112
improve arp debugging output
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
303
diff
changeset
|
92 |
arp.GetSourceIpv4Address () << " -- send reply"); |
242 | 93 |
SendArpReply (cache, arp.GetSourceIpv4Address (), |
94 |
arp.GetSourceHardwareAddress ()); |
|
95 |
} |
|
96 |
else if (arp.IsReply () && |
|
97 |
arp.GetDestinationIpv4Address ().IsEqual (cache->GetInterface ()->GetAddress ()) && |
|
294
aba9a34108d2
remove ArpL3Protocol object
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
287
diff
changeset
|
98 |
arp.GetDestinationHardwareAddress ().IsEqual (device.GetAddress ())) |
242 | 99 |
{ |
100 |
Ipv4Address from = arp.GetSourceIpv4Address (); |
|
101 |
ArpCache::Entry *entry = cache->Lookup (from); |
|
102 |
if (entry != 0) |
|
103 |
{ |
|
104 |
if (entry->IsWaitReply ()) |
|
105 |
{ |
|
321
452a9cdad112
improve arp debugging output
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
303
diff
changeset
|
106 |
NS_DEBUG ("node="<<m_node->GetId ()<<", got reply from " << |
452a9cdad112
improve arp debugging output
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
303
diff
changeset
|
107 |
arp.GetSourceIpv4Address () |
242 | 108 |
<< " for waiting entry -- flush"); |
109 |
MacAddress from_mac = arp.GetSourceHardwareAddress (); |
|
110 |
Packet waiting = entry->MarkAlive (from_mac); |
|
111 |
cache->GetInterface ()->Send (waiting, arp.GetSourceIpv4Address ()); |
|
112 |
} |
|
113 |
else |
|
114 |
{ |
|
115 |
// ignore this reply which might well be an attempt |
|
116 |
// at poisening my arp cache. |
|
321
452a9cdad112
improve arp debugging output
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
303
diff
changeset
|
117 |
NS_DEBUG ("node="<<m_node->GetId ()<<", got reply from " << |
452a9cdad112
improve arp debugging output
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
303
diff
changeset
|
118 |
arp.GetSourceIpv4Address () << |
452a9cdad112
improve arp debugging output
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
303
diff
changeset
|
119 |
" for non-waiting entry -- drop"); |
242 | 120 |
// XXX report packet as dropped. |
121 |
} |
|
122 |
} |
|
123 |
else |
|
124 |
{ |
|
321
452a9cdad112
improve arp debugging output
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
303
diff
changeset
|
125 |
NS_DEBUG ("node="<<m_node->GetId ()<<", got reply for unknown entry -- drop"); |
242 | 126 |
// XXX report packet as dropped. |
127 |
} |
|
128 |
} |
|
129 |
} |
|
130 |
bool |
|
131 |
Arp::Lookup (Packet &packet, Ipv4Address destination, |
|
132 |
NetDevice *device, |
|
133 |
MacAddress *hardwareDestination) |
|
134 |
{ |
|
135 |
ArpCache *cache = FindCache (device); |
|
136 |
ArpCache::Entry *entry = cache->Lookup (destination); |
|
137 |
if (entry != 0) |
|
138 |
{ |
|
139 |
if (entry->IsExpired ()) |
|
140 |
{ |
|
141 |
if (entry->IsDead ()) |
|
142 |
{ |
|
321
452a9cdad112
improve arp debugging output
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
303
diff
changeset
|
143 |
NS_DEBUG ("node="<<m_node->GetId ()<< |
452a9cdad112
improve arp debugging output
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
303
diff
changeset
|
144 |
", dead entry for " << destination << " expired -- send arp request"); |
242 | 145 |
entry->MarkWaitReply (packet); |
146 |
SendArpRequest (cache, destination); |
|
147 |
} |
|
148 |
else if (entry->IsAlive ()) |
|
149 |
{ |
|
321
452a9cdad112
improve arp debugging output
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
303
diff
changeset
|
150 |
NS_DEBUG ("node="<<m_node->GetId ()<< |
452a9cdad112
improve arp debugging output
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
303
diff
changeset
|
151 |
", alive entry for " << destination << " expired -- send arp request"); |
242 | 152 |
entry->MarkWaitReply (packet); |
153 |
SendArpRequest (cache, destination); |
|
154 |
} |
|
155 |
else if (entry->IsWaitReply ()) |
|
156 |
{ |
|
321
452a9cdad112
improve arp debugging output
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
303
diff
changeset
|
157 |
NS_DEBUG ("node="<<m_node->GetId ()<< |
452a9cdad112
improve arp debugging output
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
303
diff
changeset
|
158 |
", wait reply for " << destination << " expired -- drop"); |
242 | 159 |
entry->MarkDead (); |
160 |
// XXX report packet as 'dropped' |
|
161 |
} |
|
162 |
} |
|
163 |
else |
|
164 |
{ |
|
165 |
if (entry->IsDead ()) |
|
166 |
{ |
|
321
452a9cdad112
improve arp debugging output
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
303
diff
changeset
|
167 |
NS_DEBUG ("node="<<m_node->GetId ()<< |
452a9cdad112
improve arp debugging output
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
303
diff
changeset
|
168 |
", dead entry for " << destination << " valid -- drop"); |
242 | 169 |
// XXX report packet as 'dropped' |
170 |
} |
|
171 |
else if (entry->IsAlive ()) |
|
172 |
{ |
|
321
452a9cdad112
improve arp debugging output
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
303
diff
changeset
|
173 |
NS_DEBUG ("node="<<m_node->GetId ()<< |
452a9cdad112
improve arp debugging output
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
303
diff
changeset
|
174 |
", alive entry for " << destination << " valid -- send"); |
242 | 175 |
*hardwareDestination = entry->GetMacAddress (); |
176 |
return true; |
|
177 |
} |
|
178 |
else if (entry->IsWaitReply ()) |
|
179 |
{ |
|
321
452a9cdad112
improve arp debugging output
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
303
diff
changeset
|
180 |
NS_DEBUG ("node="<<m_node->GetId ()<< |
452a9cdad112
improve arp debugging output
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
303
diff
changeset
|
181 |
", wait reply for " << destination << " valid -- drop previous"); |
242 | 182 |
Packet old = entry->UpdateWaitReply (packet); |
183 |
// XXX report 'old' packet as 'dropped' |
|
184 |
} |
|
185 |
} |
|
186 |
||
187 |
} |
|
188 |
else |
|
189 |
{ |
|
190 |
// This is our first attempt to transmit data to this destination. |
|
321
452a9cdad112
improve arp debugging output
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
303
diff
changeset
|
191 |
NS_DEBUG ("node="<<m_node->GetId ()<< |
452a9cdad112
improve arp debugging output
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
303
diff
changeset
|
192 |
", no entry for " << destination << " -- send arp request"); |
242 | 193 |
entry = cache->Add (destination); |
194 |
entry->MarkWaitReply (packet); |
|
195 |
SendArpRequest (cache, destination); |
|
196 |
} |
|
197 |
return false; |
|
198 |
} |
|
199 |
||
200 |
void |
|
201 |
Arp::SendArpRequest (ArpCache const *cache, Ipv4Address to) |
|
202 |
{ |
|
203 |
ArpHeader arp; |
|
204 |
arp.SetRequest (cache->GetDevice ()->GetAddress (), |
|
205 |
cache->GetInterface ()->GetAddress (), |
|
206 |
cache->GetDevice ()->GetBroadcast (), |
|
207 |
to); |
|
208 |
Packet packet; |
|
209 |
packet.Add (arp); |
|
294
aba9a34108d2
remove ArpL3Protocol object
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
287
diff
changeset
|
210 |
cache->GetDevice ()->Send (packet, cache->GetDevice ()->GetBroadcast (), PROT_NUMBER); |
242 | 211 |
} |
212 |
||
213 |
void |
|
214 |
Arp::SendArpReply (ArpCache const *cache, Ipv4Address toIp, MacAddress toMac) |
|
215 |
{ |
|
216 |
ArpHeader arp; |
|
217 |
arp.SetReply (cache->GetDevice ()->GetAddress (), |
|
218 |
cache->GetInterface ()->GetAddress (), |
|
219 |
toMac, toIp); |
|
220 |
Packet packet; |
|
221 |
packet.Add (arp); |
|
294
aba9a34108d2
remove ArpL3Protocol object
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
287
diff
changeset
|
222 |
cache->GetDevice ()->Send (packet, toMac, PROT_NUMBER); |
242 | 223 |
} |
224 |
||
225 |
}//namespace ns3 |