Left: | ||
Right: |
OLD | NEW |
---|---|
1 // -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- | 1 // -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- |
2 // | 2 // |
3 // Copyright (c) 2008 University of Washington | 3 // Copyright (c) 2008 University of Washington |
4 // | 4 // |
5 // This program is free software; you can redistribute it and/or modify | 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 | 6 // it under the terms of the GNU General Public License version 2 as |
7 // published by the Free Software Foundation; | 7 // published by the Free Software Foundation; |
8 // | 8 // |
9 // This program is distributed in the hope that it will be useful, | 9 // This program is distributed in the hope that it will be useful, |
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 // GNU General Public License for more details. | 12 // GNU General Public License for more details. |
13 // | 13 // |
14 // You should have received a copy of the GNU General Public License | 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 | 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 | 16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 // | 17 // |
18 | 18 |
19 #include "ns3/log.h" | 19 #include "ns3/log.h" |
20 #include "ns3/object.h" | 20 #include "ns3/object.h" |
21 #include "ipv4-global-routing.h" | |
22 #include "ns3/packet.h" | 21 #include "ns3/packet.h" |
23 #include "ns3/node.h" | 22 #include "ns3/node.h" |
23 #include "ns3/ipv4-route.h" | |
24 #include "ns3/ipv4-routing-table-entry.h" | |
25 #include "ipv4-global-routing.h" | |
24 | 26 |
25 NS_LOG_COMPONENT_DEFINE ("Ipv4GlobalRouting"); | 27 NS_LOG_COMPONENT_DEFINE ("Ipv4GlobalRouting"); |
26 | 28 |
27 namespace ns3 { | 29 namespace ns3 { |
28 | 30 |
29 NS_OBJECT_ENSURE_REGISTERED (Ipv4GlobalRouting); | 31 NS_OBJECT_ENSURE_REGISTERED (Ipv4GlobalRouting); |
30 | 32 |
31 TypeId· | 33 TypeId· |
32 Ipv4GlobalRouting::GetTypeId (void) | 34 Ipv4GlobalRouting::GetTypeId (void) |
33 {· | 35 {· |
34 static TypeId tid = TypeId ("ns3::Ipv4GlobalRouting") | 36 static TypeId tid = TypeId ("ns3::Ipv4GlobalRouting") |
35 .SetParent<Object> () | 37 .SetParent<Object> () |
36 ; | 38 ; |
37 return tid; | 39 return tid; |
38 } | 40 } |
39 | 41 |
40 Ipv4GlobalRouting::Ipv4GlobalRouting ()· | 42 Ipv4GlobalRouting::Ipv4GlobalRouting ()· |
41 { | 43 { |
42 NS_LOG_FUNCTION_NOARGS (); | 44 NS_LOG_FUNCTION_NOARGS (); |
43 } | 45 } |
44 | 46 |
47 Ipv4GlobalRouting::~Ipv4GlobalRouting () | |
48 { | |
49 NS_LOG_FUNCTION_NOARGS (); | |
50 } | |
51 | |
45 void· | 52 void· |
46 Ipv4GlobalRouting::AddHostRouteTo (Ipv4Address dest,· | 53 Ipv4GlobalRouting::AddHostRouteTo (Ipv4Address dest,· |
47 Ipv4Address nextHop,· | 54 Ipv4Address nextHop,· |
48 uint32_t interface) | 55 uint32_t interface) |
49 { | 56 { |
50 NS_LOG_FUNCTION (dest << nextHop << interface); | 57 NS_LOG_FUNCTION (dest << nextHop << interface); |
51 Ipv4Route *route = new Ipv4Route (); | 58 Ipv4RoutingTableEntry *route = new Ipv4RoutingTableEntry (); |
52 *route = Ipv4Route::CreateHostRouteTo (dest, nextHop, interface); | 59 *route = Ipv4RoutingTableEntry::CreateHostRouteTo (dest, nextHop, interface); |
53 m_hostRoutes.push_back (route); | 60 m_hostRoutes.push_back (route); |
54 } | 61 } |
55 | 62 |
56 void· | 63 void· |
57 Ipv4GlobalRouting::AddHostRouteTo (Ipv4Address dest,· | 64 Ipv4GlobalRouting::AddHostRouteTo (Ipv4Address dest,· |
58 uint32_t interface) | 65 uint32_t interface) |
59 { | 66 { |
60 NS_LOG_FUNCTION (dest << interface); | 67 NS_LOG_FUNCTION (dest << interface); |
61 Ipv4Route *route = new Ipv4Route (); | 68 Ipv4RoutingTableEntry *route = new Ipv4RoutingTableEntry (); |
62 *route = Ipv4Route::CreateHostRouteTo (dest, interface); | 69 *route = Ipv4RoutingTableEntry::CreateHostRouteTo (dest, interface); |
63 m_hostRoutes.push_back (route); | 70 m_hostRoutes.push_back (route); |
64 } | 71 } |
65 | 72 |
66 void· | 73 void· |
67 Ipv4GlobalRouting::AddNetworkRouteTo (Ipv4Address network,· | 74 Ipv4GlobalRouting::AddNetworkRouteTo (Ipv4Address network,· |
68 Ipv4Mask networkMask,· | 75 Ipv4Mask networkMask,· |
69 Ipv4Address nextHop,· | 76 Ipv4Address nextHop,· |
70 uint32_t interface) | 77 uint32_t interface) |
71 { | 78 { |
72 NS_LOG_FUNCTION (network << networkMask << nextHop << interface); | 79 NS_LOG_FUNCTION (network << networkMask << nextHop << interface); |
73 Ipv4Route *route = new Ipv4Route (); | 80 Ipv4RoutingTableEntry *route = new Ipv4RoutingTableEntry (); |
74 *route = Ipv4Route::CreateNetworkRouteTo (network, | 81 *route = Ipv4RoutingTableEntry::CreateNetworkRouteTo (network, |
75 networkMask, | 82 networkMask, |
76 nextHop, | 83 nextHop, |
77 interface); | 84 interface); |
78 m_networkRoutes.push_back (route); | 85 m_networkRoutes.push_back (route); |
79 } | 86 } |
80 | 87 |
81 void· | 88 void· |
82 Ipv4GlobalRouting::AddNetworkRouteTo (Ipv4Address network,· | 89 Ipv4GlobalRouting::AddNetworkRouteTo (Ipv4Address network,· |
83 Ipv4Mask networkMask,· | 90 Ipv4Mask networkMask,· |
84 uint32_t interface) | 91 uint32_t interface) |
85 { | 92 { |
86 NS_LOG_FUNCTION (network << networkMask << interface); | 93 NS_LOG_FUNCTION (network << networkMask << interface); |
87 Ipv4Route *route = new Ipv4Route (); | 94 Ipv4RoutingTableEntry *route = new Ipv4RoutingTableEntry (); |
88 *route = Ipv4Route::CreateNetworkRouteTo (network, | 95 *route = Ipv4RoutingTableEntry::CreateNetworkRouteTo (network, |
89 networkMask, | 96 networkMask, |
90 interface); | 97 interface); |
91 m_networkRoutes.push_back (route); | 98 m_networkRoutes.push_back (route); |
92 } | 99 } |
93 | 100 |
94 Ipv4Route * | 101 Ptr<Ipv4Route> |
95 Ipv4GlobalRouting::LookupGlobal (Ipv4Address dest) | 102 Ipv4GlobalRouting::LookupGlobal (Ipv4Address dest) |
96 { | 103 { |
97 NS_LOG_FUNCTION_NOARGS (); | 104 NS_LOG_FUNCTION_NOARGS (); |
105 Ptr<Ipv4Route> rtentry = 0; | |
106 | |
98 for (HostRoutesCI i = m_hostRoutes.begin ();· | 107 for (HostRoutesCI i = m_hostRoutes.begin ();· |
99 i != m_hostRoutes.end ();· | 108 i != m_hostRoutes.end ();· |
100 i++)· | 109 i++)· |
101 { | 110 { |
102 NS_ASSERT ((*i)->IsHost ()); | 111 NS_ASSERT ((*i)->IsHost ()); |
103 if ((*i)->GetDest ().IsEqual (dest))· | 112 if ((*i)->GetDest ().IsEqual (dest))· |
104 { | 113 { |
105 NS_LOG_LOGIC ("Found global host route" << *i);· | 114 NS_LOG_LOGIC ("Found global host route" << *i);· |
106 return (*i); | 115 // XXX in the future, do not store these internally as Ipv4RoutingTabl eEntry* |
116 Ptr<Ipv4> ipv4 = m_node->GetObject<Ipv4> (); | |
117 Ipv4RoutingTableEntry* route = (*i); | |
118 rtentry = Create<Ipv4Route> (); | |
119 rtentry->SetDestination (route->GetDest ()); | |
120 // XXX handle multi-address case | |
121 rtentry->SetSource (ipv4->GetAddress (route->GetInterface(), 0).GetLoc al ()); | |
122 rtentry->SetGateway (route->GetGateway ()); | |
123 uint32_t interfaceIdx = route->GetInterface (); | |
124 rtentry->SetOutputDevice (ipv4->GetNetDevice (interfaceIdx)); | |
125 return rtentry; | |
107 } | 126 } |
108 } | 127 } |
109 for (NetworkRoutesI j = m_networkRoutes.begin ();· | 128 for (NetworkRoutesI j = m_networkRoutes.begin ();· |
110 j != m_networkRoutes.end ();· | 129 j != m_networkRoutes.end ();· |
111 j++)· | 130 j++)· |
112 { | 131 { |
113 NS_ASSERT ((*j)->IsNetwork ()); | 132 NS_ASSERT ((*j)->IsNetwork ()); |
114 Ipv4Mask mask = (*j)->GetDestNetworkMask (); | 133 Ipv4Mask mask = (*j)->GetDestNetworkMask (); |
115 Ipv4Address entry = (*j)->GetDestNetwork (); | 134 Ipv4Address entry = (*j)->GetDestNetwork (); |
116 if (mask.IsMatch (dest, entry))· | 135 if (mask.IsMatch (dest, entry))· |
117 { | 136 { |
118 NS_LOG_LOGIC ("Found global network route" << *j);· | 137 NS_LOG_LOGIC ("Found global network route" << *j);· |
119 return (*j); | 138 Ptr<Ipv4> ipv4 = m_node->GetObject<Ipv4> (); |
faker.moatamri
2009/05/15 07:55:26
Looks like a copy and paste code, can we do it in
| |
139 Ipv4RoutingTableEntry* route = (*j); | |
140 rtentry = Create<Ipv4Route> (); | |
141 rtentry->SetDestination (route->GetDest ()); | |
142 // XXX handle multi-address case | |
faker.moatamri
2009/05/15 07:55:26
Should we put a TODO in here and create a bug in t
| |
143 rtentry->SetSource (ipv4->GetAddress (route->GetInterface(), 0).GetLoc al ()); | |
144 rtentry->SetGateway (route->GetGateway ()); | |
145 uint32_t interfaceIdx = route->GetInterface (); | |
146 rtentry->SetOutputDevice (ipv4->GetNetDevice (interfaceIdx)); | |
147 return rtentry; | |
120 } | 148 } |
121 } | 149 } |
122 return 0; | 150 return 0; |
123 } | 151 } |
124 | 152 |
125 uint32_t· | 153 uint32_t· |
126 Ipv4GlobalRouting::GetNRoutes (void) | 154 Ipv4GlobalRouting::GetNRoutes (void) |
127 { | 155 { |
128 NS_LOG_FUNCTION_NOARGS (); | 156 NS_LOG_FUNCTION_NOARGS (); |
129 uint32_t n = 0; | 157 uint32_t n = 0; |
130 n += m_hostRoutes.size (); | 158 n += m_hostRoutes.size (); |
131 n += m_networkRoutes.size (); | 159 n += m_networkRoutes.size (); |
132 return n; | 160 return n; |
133 } | 161 } |
134 | 162 |
135 Ipv4Route * | 163 Ipv4RoutingTableEntry * |
136 Ipv4GlobalRouting::GetRoute (uint32_t index) | 164 Ipv4GlobalRouting::GetRoute (uint32_t index) |
137 { | 165 { |
138 NS_LOG_FUNCTION (index); | 166 NS_LOG_FUNCTION (index); |
139 if (index < m_hostRoutes.size ()) | 167 if (index < m_hostRoutes.size ()) |
140 { | 168 { |
141 uint32_t tmp = 0; | 169 uint32_t tmp = 0; |
142 for (HostRoutesCI i = m_hostRoutes.begin ();· | 170 for (HostRoutesCI i = m_hostRoutes.begin ();· |
143 i != m_hostRoutes.end ();· | 171 i != m_hostRoutes.end ();· |
144 i++)· | 172 i++)· |
145 { | 173 { |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
200 delete *j; | 228 delete *j; |
201 m_networkRoutes.erase (j); | 229 m_networkRoutes.erase (j); |
202 NS_LOG_LOGIC ("Done removing network route " << index << "; network ro ute remaining size = " << m_networkRoutes.size()); | 230 NS_LOG_LOGIC ("Done removing network route " << index << "; network ro ute remaining size = " << m_networkRoutes.size()); |
203 return; | 231 return; |
204 } | 232 } |
205 tmp++; | 233 tmp++; |
206 } | 234 } |
207 NS_ASSERT (false); | 235 NS_ASSERT (false); |
208 } | 236 } |
209 | 237 |
210 bool | |
211 Ipv4GlobalRouting::RequestRoute ( | |
212 uint32_t interface, | |
213 Ipv4Header const &ipHeader, | |
214 Ptr<Packet> packet, | |
215 RouteReplyCallback routeReply) | |
216 { | |
217 NS_LOG_FUNCTION (this << interface << &ipHeader << packet << &routeReply); | |
218 | |
219 NS_LOG_LOGIC ("source = " << ipHeader.GetSource ()); | |
220 | |
221 NS_LOG_LOGIC ("destination = " << ipHeader.GetDestination ()); | |
222 | |
223 if (ipHeader.GetDestination ().IsMulticast ()) | |
224 { | |
225 NS_LOG_LOGIC ("Multicast destination-- returning false"); | |
226 return false; // Let other routing protocols try to handle this | |
227 } | |
228 | |
229 // This is a unicast packet. Check to see if we have a route for it. | |
230 // | |
231 NS_LOG_LOGIC ("Unicast destination- looking up"); | |
232 Ipv4Route *route = LookupGlobal (ipHeader.GetDestination ()); | |
233 if (route != 0) | |
234 { | |
235 routeReply (true, *route, packet, ipHeader); | |
236 return true; | |
237 } | |
238 else | |
239 { | |
240 return false; // Let other routing protocols try to handle this | |
241 // route request. | |
242 } | |
243 } | |
244 | |
245 bool | |
246 Ipv4GlobalRouting::RequestInterface (Ipv4Address destination, uint32_t& interfac e) | |
247 { | |
248 NS_LOG_FUNCTION (this << destination << &interface); | |
249 // | |
250 // First, see if this is a multicast packet we have a route for. If we | |
251 // have a route, then send the packet down each of the specified interfaces. | |
252 // | |
253 if (destination.IsMulticast ()) | |
254 { | |
255 NS_LOG_LOGIC ("Multicast destination-- returning false"); | |
256 return false; // Let other routing protocols try to handle this | |
257 } | |
258 // | |
259 // See if this is a unicast packet we have a route for. | |
260 // | |
261 NS_LOG_LOGIC ("Unicast destination- looking up"); | |
262 Ipv4Route *route = LookupGlobal (destination); | |
263 if (route) | |
264 { | |
265 interface = route->GetInterface (); | |
266 return true; | |
267 } | |
268 else | |
269 { | |
270 return false; | |
271 } | |
272 } | |
273 | |
274 void | 238 void |
275 Ipv4GlobalRouting::DoDispose (void) | 239 Ipv4GlobalRouting::DoDispose (void) |
276 { | 240 { |
277 NS_LOG_FUNCTION_NOARGS (); | 241 NS_LOG_FUNCTION_NOARGS (); |
278 for (HostRoutesI i = m_hostRoutes.begin ();· | 242 for (HostRoutesI i = m_hostRoutes.begin ();· |
279 i != m_hostRoutes.end ();· | 243 i != m_hostRoutes.end ();· |
280 i = m_hostRoutes.erase (i))· | 244 i = m_hostRoutes.erase (i))· |
281 { | 245 { |
282 delete (*i); | 246 delete (*i); |
283 } | 247 } |
284 for (NetworkRoutesI j = m_networkRoutes.begin ();· | 248 for (NetworkRoutesI j = m_networkRoutes.begin ();· |
285 j != m_networkRoutes.end ();· | 249 j != m_networkRoutes.end ();· |
286 j = m_networkRoutes.erase (j))· | 250 j = m_networkRoutes.erase (j))· |
287 { | 251 { |
288 delete (*j); | 252 delete (*j); |
289 } | 253 } |
290 Ipv4RoutingProtocol::DoDispose (); | 254 Ipv4RoutingProtocol::DoDispose (); |
291 } | 255 } |
292 | 256 |
257 Ptr<Ipv4Route> | |
258 Ipv4GlobalRouting::RouteOutput (const Ipv4Header &header, uint32_t oif, Socket:: SocketErrno &errno) | |
259 {······ | |
260 | |
261 // | |
262 // First, see if this is a multicast packet we have a route for. If we | |
263 // have a route, then send the packet down each of the specified interfaces. | |
264 // | |
265 if (header.GetDestination().IsMulticast ()) | |
266 { | |
267 NS_LOG_LOGIC ("Multicast destination-- returning false"); | |
268 return 0; // Let other routing protocols try to handle this | |
269 } | |
270 // | |
271 // See if this is a unicast packet we have a route for. | |
272 // | |
273 NS_LOG_LOGIC ("Unicast destination- looking up"); | |
274 Ptr<Ipv4Route> rtentry = LookupGlobal (header.GetDestination()); | |
275 if (rtentry) | |
276 { | |
277 errno = Socket::ERROR_NOTERROR; | |
278 } | |
279 else | |
280 { | |
281 errno = Socket::ERROR_NOROUTETOHOST; | |
282 } | |
283 return rtentry; | |
284 } | |
285 | |
286 bool· | |
287 Ipv4GlobalRouting::RouteInput (Ptr<const Packet> p, const Ipv4Header &ipHeader, Ptr<const NetDevice> idev, UnicastForwardCallback u cb, MulticastForwardCallback mcb, | |
288 LocalDeliverCallback lcb, ErrorCallback ecb)· | |
289 {· | |
290 | |
291 NS_LOG_FUNCTION (this << p << ipHeader << ipHeader.GetSource () << ipHeader.Ge tDestination () << idev); | |
292 | |
293 if (ipHeader.GetDestination ().IsMulticast ()) | |
294 { | |
295 NS_LOG_LOGIC ("Multicast destination-- returning false"); | |
296 return false; // Let other routing protocols try to handle this | |
297 } | |
298 | |
299 // This is a unicast packet. Check to see if we have a route for it. | |
300 // | |
301 NS_LOG_LOGIC ("Unicast destination- looking up"); | |
302 Ptr<Ipv4Route> rtentry = LookupGlobal (ipHeader.GetDestination ()); | |
303 if (rtentry != 0) | |
304 { | |
305 NS_LOG_LOGIC ("Found unicast destination- calling unicast callback"); | |
306 ucb (rtentry, p, ipHeader); | |
307 return true; | |
308 } | |
309 else | |
310 { | |
311 NS_LOG_LOGIC ("Did not find unicast destination- returning false"); | |
312 return false; // Let other routing protocols try to handle this | |
313 // route request. | |
314 } | |
315 } | |
316 | |
317 void | |
318 Ipv4GlobalRouting::SetNode (Ptr<Node> node) | |
319 { | |
320 NS_LOG_FUNCTION_NOARGS (); | |
321 m_node = node; | |
322 } | |
323 | |
324 Ptr<Node> | |
325 Ipv4GlobalRouting::GetNode (void) const | |
326 { | |
327 NS_LOG_FUNCTION_NOARGS (); | |
328 return m_node; | |
329 } | |
330 | |
331 | |
332 | |
293 }//namespace ns3 | 333 }//namespace ns3 |
OLD | NEW |