00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "ns3/log.h"
00022 #include "ipv4-static-routing.h"
00023 #include "ns3/packet.h"
00024
00025 NS_LOG_COMPONENT_DEFINE ("Ipv4StaticRouting");
00026
00027 namespace ns3 {
00028
00029 Ipv4StaticRouting::Ipv4StaticRouting ()
00030 : m_defaultRoute (0), m_defaultMulticastRoute (0)
00031 {
00032 NS_LOG_FUNCTION_NOARGS ();
00033 }
00034
00035 void
00036 Ipv4StaticRouting::AddHostRouteTo (Ipv4Address dest,
00037 Ipv4Address nextHop,
00038 uint32_t interface)
00039 {
00040 NS_LOG_FUNCTION_NOARGS ();
00041 Ipv4Route *route = new Ipv4Route ();
00042 *route = Ipv4Route::CreateHostRouteTo (dest, nextHop, interface);
00043 m_hostRoutes.push_back (route);
00044 }
00045
00046 void
00047 Ipv4StaticRouting::AddHostRouteTo (Ipv4Address dest,
00048 uint32_t interface)
00049 {
00050 NS_LOG_FUNCTION_NOARGS ();
00051 Ipv4Route *route = new Ipv4Route ();
00052 *route = Ipv4Route::CreateHostRouteTo (dest, interface);
00053 m_hostRoutes.push_back (route);
00054 }
00055
00056 void
00057 Ipv4StaticRouting::AddNetworkRouteTo (Ipv4Address network,
00058 Ipv4Mask networkMask,
00059 Ipv4Address nextHop,
00060 uint32_t interface)
00061 {
00062 NS_LOG_FUNCTION_NOARGS ();
00063 Ipv4Route *route = new Ipv4Route ();
00064 *route = Ipv4Route::CreateNetworkRouteTo (network,
00065 networkMask,
00066 nextHop,
00067 interface);
00068 m_networkRoutes.push_back (route);
00069 }
00070
00071 void
00072 Ipv4StaticRouting::AddNetworkRouteTo (Ipv4Address network,
00073 Ipv4Mask networkMask,
00074 uint32_t interface)
00075 {
00076 NS_LOG_FUNCTION_NOARGS ();
00077 Ipv4Route *route = new Ipv4Route ();
00078 *route = Ipv4Route::CreateNetworkRouteTo (network,
00079 networkMask,
00080 interface);
00081 m_networkRoutes.push_back (route);
00082 }
00083
00084 void
00085 Ipv4StaticRouting::SetDefaultRoute (Ipv4Address nextHop,
00086 uint32_t interface)
00087 {
00088 NS_LOG_FUNCTION_NOARGS ();
00089 Ipv4Route *route = new Ipv4Route ();
00090 *route = Ipv4Route::CreateDefaultRoute (nextHop, interface);
00091 delete m_defaultRoute;
00092 m_defaultRoute = route;
00093 }
00094
00095 void
00096 Ipv4StaticRouting::AddMulticastRoute(Ipv4Address origin,
00097 Ipv4Address group,
00098 uint32_t inputInterface,
00099 std::vector<uint32_t> outputInterfaces)
00100 {
00101 NS_LOG_FUNCTION_NOARGS ();
00102 Ipv4MulticastRoute *route = new Ipv4MulticastRoute ();
00103 *route = Ipv4MulticastRoute::CreateMulticastRoute (origin, group,
00104 inputInterface, outputInterfaces);
00105 m_multicastRoutes.push_back (route);
00106 }
00107
00108 void
00109 Ipv4StaticRouting::SetDefaultMulticastRoute(uint32_t outputInterface)
00110 {
00111 NS_LOG_FUNCTION_NOARGS ();
00112 Ipv4Address origin = Ipv4Address::GetAny ();
00113 Ipv4Address group = Ipv4Address::GetAny ();
00114 uint32_t inputInterface = Ipv4RoutingProtocol::IF_INDEX_ANY;
00115
00116 std::vector<uint32_t> outputInterfaces (1);
00117 outputInterfaces[0] = outputInterface;
00118
00119 Ipv4MulticastRoute *route = new Ipv4MulticastRoute ();
00120 *route = Ipv4MulticastRoute::CreateMulticastRoute (origin, group,
00121 inputInterface, outputInterfaces);
00122
00123 delete m_defaultMulticastRoute;
00124 m_defaultMulticastRoute = route;
00125 }
00126
00127 uint32_t
00128 Ipv4StaticRouting::GetNMulticastRoutes (void) const
00129 {
00130 NS_LOG_FUNCTION_NOARGS ();
00131 return m_multicastRoutes.size () + m_defaultMulticastRoute ? 1 : 0;
00132 }
00133
00134 Ipv4MulticastRoute *
00135 Ipv4StaticRouting::GetMulticastRoute (uint32_t index) const
00136 {
00137 NS_LOG_FUNCTION_NOARGS ();
00138 NS_ASSERT_MSG(index < m_multicastRoutes.size (),
00139 "Ipv4StaticRouting::GetMulticastRoute (): Index out of range");
00140
00141
00142
00143
00144
00145
00146
00147 if (index == 0 && m_defaultMulticastRoute != 0)
00148 {
00149 return m_defaultMulticastRoute;
00150 }
00151
00152
00153
00154
00155
00156
00157
00158
00159 if (m_defaultMulticastRoute != 0)
00160 {
00161 NS_ASSERT(index > 0);
00162 index--;
00163 }
00164
00165 if (index < m_multicastRoutes.size ())
00166 {
00167 uint32_t tmp = 0;
00168 for (MulticastRoutesCI i = m_multicastRoutes.begin ();
00169 i != m_multicastRoutes.end ();
00170 i++)
00171 {
00172 if (tmp == index)
00173 {
00174 return *i;
00175 }
00176 tmp++;
00177 }
00178 }
00179 return 0;
00180 }
00181
00182 Ipv4MulticastRoute *
00183 Ipv4StaticRouting::GetDefaultMulticastRoute () const
00184 {
00185 NS_LOG_FUNCTION_NOARGS ();
00186 if (m_defaultMulticastRoute != 0)
00187 {
00188 return m_defaultMulticastRoute;
00189 }
00190 return 0;
00191 }
00192
00193 bool
00194 Ipv4StaticRouting::RemoveMulticastRoute(Ipv4Address origin,
00195 Ipv4Address group,
00196 uint32_t inputInterface)
00197 {
00198 NS_LOG_FUNCTION_NOARGS ();
00199 for (MulticastRoutesI i = m_multicastRoutes.begin ();
00200 i != m_multicastRoutes.end ();
00201 i++)
00202 {
00203 Ipv4MulticastRoute *route = *i;
00204 if (origin == route->GetOrigin () &&
00205 group == route->GetGroup () &&
00206 inputInterface == route->GetInputInterface ())
00207 {
00208 delete *i;
00209 m_multicastRoutes.erase (i);
00210 return true;
00211 }
00212 }
00213 return false;
00214 }
00215
00216 void
00217 Ipv4StaticRouting::RemoveMulticastRoute(uint32_t index)
00218 {
00219 NS_LOG_FUNCTION_NOARGS ();
00220
00221
00222
00223
00224
00225
00226
00227 if (index == 0 && m_defaultMulticastRoute != 0)
00228 {
00229 delete m_defaultMulticastRoute;
00230 m_defaultMulticastRoute = 0;
00231 }
00232
00233
00234
00235
00236
00237
00238
00239
00240 if (m_defaultMulticastRoute != 0)
00241 {
00242 NS_ASSERT(index > 0);
00243 index--;
00244 }
00245
00246 uint32_t tmp = 0;
00247 for (MulticastRoutesI i = m_multicastRoutes.begin ();
00248 i != m_multicastRoutes.end ();
00249 i++)
00250 {
00251 if (tmp == index)
00252 {
00253 delete *i;
00254 m_multicastRoutes.erase (i);
00255 return;
00256 }
00257 tmp++;
00258 }
00259 }
00260
00261 Ipv4Route *
00262 Ipv4StaticRouting::LookupStatic (Ipv4Address dest)
00263 {
00264 NS_LOG_FUNCTION_NOARGS ();
00265 for (HostRoutesCI i = m_hostRoutes.begin ();
00266 i != m_hostRoutes.end ();
00267 i++)
00268 {
00269 NS_ASSERT ((*i)->IsHost ());
00270 if ((*i)->GetDest ().IsEqual (dest))
00271 {
00272 return (*i);
00273 }
00274 }
00275 for (NetworkRoutesI j = m_networkRoutes.begin ();
00276 j != m_networkRoutes.end ();
00277 j++)
00278 {
00279 NS_ASSERT ((*j)->IsNetwork ());
00280 Ipv4Mask mask = (*j)->GetDestNetworkMask ();
00281 Ipv4Address entry = (*j)->GetDestNetwork ();
00282 if (mask.IsMatch (dest, entry))
00283 {
00284 return (*j);
00285 }
00286 }
00287 if (m_defaultRoute != 0)
00288 {
00289 NS_ASSERT (m_defaultRoute->IsDefault ());
00290 return m_defaultRoute;
00291 }
00292 return 0;
00293 }
00294
00295 Ipv4MulticastRoute *
00296 Ipv4StaticRouting::LookupStatic (
00297 Ipv4Address origin,
00298 Ipv4Address group,
00299 uint32_t ifIndex)
00300 {
00301 NS_LOG_FUNCTION_NOARGS ();
00302
00303
00304
00305
00306 Ipv4Address wildcard = Ipv4Address::GetAny ();
00307
00308 for (MulticastRoutesI i = m_multicastRoutes.begin ();
00309 i != m_multicastRoutes.end ();
00310 i++)
00311 {
00312 Ipv4MulticastRoute *route = *i;
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322 if (origin == route->GetOrigin () && group == route->GetGroup ())
00323 {
00324 if (ifIndex == Ipv4RoutingProtocol::IF_INDEX_ANY ||
00325 ifIndex == route->GetInputInterface ())
00326 {
00327 return *i;
00328 }
00329 }
00330 }
00331
00332
00333
00334
00335
00336
00337 if (ifIndex != Ipv4RoutingProtocol::IF_INDEX_ANY)
00338 {
00339 return 0;
00340 }
00341
00342
00343
00344
00345
00346
00347
00348
00349 for (MulticastRoutesI i = m_multicastRoutes.begin ();
00350 i != m_multicastRoutes.end ();
00351 i++)
00352 {
00353 Ipv4MulticastRoute *route = *i;
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363 if (route->GetOrigin () == wildcard && group == route->GetGroup ())
00364 {
00365 return *i;
00366 }
00367 }
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377 for (MulticastRoutesI i = m_multicastRoutes.begin ();
00378 i != m_multicastRoutes.end ();
00379 i++)
00380 {
00381 Ipv4MulticastRoute *route = *i;
00382
00383 if (route->GetOrigin () == wildcard && route->GetGroup () == wildcard)
00384 {
00385 return *i;
00386 }
00387 }
00388
00389
00390
00391
00392 if (m_defaultMulticastRoute != 0)
00393 {
00394 return m_defaultMulticastRoute;
00395 }
00396
00397 return 0;
00398 }
00399
00400 uint32_t
00401 Ipv4StaticRouting::GetNRoutes (void)
00402 {
00403 NS_LOG_FUNCTION_NOARGS ();
00404 uint32_t n = 0;
00405 if (m_defaultRoute != 0)
00406 {
00407 n++;
00408 }
00409 n += m_hostRoutes.size ();
00410 n += m_networkRoutes.size ();
00411 return n;
00412 }
00413
00414 Ipv4Route *
00415 Ipv4StaticRouting::GetDefaultRoute ()
00416 {
00417 NS_LOG_FUNCTION_NOARGS ();
00418 if (m_defaultRoute != 0)
00419 {
00420 return m_defaultRoute;
00421 }
00422 else
00423 {
00424 return 0;
00425 }
00426 }
00427
00428 Ipv4Route *
00429 Ipv4StaticRouting::GetRoute (uint32_t index)
00430 {
00431 NS_LOG_FUNCTION_NOARGS ();
00432 if (index == 0 && m_defaultRoute != 0)
00433 {
00434 return m_defaultRoute;
00435 }
00436 if (index > 0 && m_defaultRoute != 0)
00437 {
00438 index--;
00439 }
00440 if (index < m_hostRoutes.size ())
00441 {
00442 uint32_t tmp = 0;
00443 for (HostRoutesCI i = m_hostRoutes.begin ();
00444 i != m_hostRoutes.end ();
00445 i++)
00446 {
00447 if (tmp == index)
00448 {
00449 return *i;
00450 }
00451 tmp++;
00452 }
00453 }
00454 index -= m_hostRoutes.size ();
00455 uint32_t tmp = 0;
00456 for (NetworkRoutesI j = m_networkRoutes.begin ();
00457 j != m_networkRoutes.end ();
00458 j++)
00459 {
00460 if (tmp == index)
00461 {
00462 return *j;
00463 }
00464 tmp++;
00465 }
00466 NS_ASSERT (false);
00467
00468 return 0;
00469 }
00470 void
00471 Ipv4StaticRouting::RemoveRoute (uint32_t index)
00472 {
00473 NS_LOG_FUNCTION_NOARGS ();
00474 if (index == 0 && m_defaultRoute != 0)
00475 {
00476 delete m_defaultRoute;
00477 m_defaultRoute = 0;
00478 }
00479 if (index > 0 && m_defaultRoute != 0)
00480 {
00481 index--;
00482 }
00483 if (index < m_hostRoutes.size ())
00484 {
00485 uint32_t tmp = 0;
00486 for (HostRoutesI i = m_hostRoutes.begin ();
00487 i != m_hostRoutes.end ();
00488 i++)
00489 {
00490 if (tmp == index)
00491 {
00492 delete *i;
00493 m_hostRoutes.erase (i);
00494 return;
00495 }
00496 tmp++;
00497 }
00498 }
00499 index -= m_hostRoutes.size ();
00500 uint32_t tmp = 0;
00501 for (NetworkRoutesI j = m_networkRoutes.begin ();
00502 j != m_networkRoutes.end ();
00503 j++)
00504 {
00505 if (tmp == index)
00506 {
00507 delete *j;
00508 m_networkRoutes.erase (j);
00509 return;
00510 }
00511 tmp++;
00512 }
00513 NS_ASSERT (false);
00514 }
00515
00516 bool
00517 Ipv4StaticRouting::RequestRoute (
00518 uint32_t ifIndex,
00519 Ipv4Header const &ipHeader,
00520 Ptr<Packet> packet,
00521 RouteReplyCallback routeReply)
00522 {
00523 NS_LOG_FUNCTION (this << ifIndex << &ipHeader << packet << &routeReply);
00524
00525 NS_LOG_LOGIC ("source = " << ipHeader.GetSource ());
00526
00527 NS_LOG_LOGIC ("destination = " << ipHeader.GetDestination ());
00528
00529 if (ipHeader.GetDestination ().IsMulticast ())
00530 {
00531 NS_LOG_LOGIC ("Multicast destination");
00532
00533 Ipv4MulticastRoute *mRoute = LookupStatic(ipHeader.GetSource (),
00534 ipHeader.GetDestination (), ifIndex);
00535
00536 if (mRoute)
00537 {
00538 NS_LOG_LOGIC ("Multicast route found");
00539
00540 for (uint32_t i = 0; i < mRoute->GetNOutputInterfaces (); ++i)
00541 {
00542 Ptr<Packet> p = packet->Copy ();
00543 Ipv4Header h = ipHeader;
00544 Ipv4Route route =
00545 Ipv4Route::CreateHostRouteTo(h.GetDestination (),
00546 mRoute->GetOutputInterface(i));
00547 NS_LOG_LOGIC ( "Send via interface " <<
00548 mRoute->GetOutputInterface(i));
00549 routeReply (true, route, p, h);
00550 }
00551 return true;
00552 }
00553 return false;
00554 }
00555
00556
00557
00558 NS_LOG_LOGIC ("Unicast destination");
00559 Ipv4Route *route = LookupStatic (ipHeader.GetDestination ());
00560 if (route != 0)
00561 {
00562 routeReply (true, *route, packet, ipHeader);
00563 return true;
00564 }
00565 else
00566 {
00567 return false;
00568
00569 }
00570 }
00571
00572 bool
00573 Ipv4StaticRouting::RequestIfIndex (Ipv4Address destination, uint32_t& ifIndex)
00574 {
00575 NS_LOG_FUNCTION (this << destination << &ifIndex);
00576
00577
00578
00579
00580 if (destination.IsMulticast ())
00581 {
00582 NS_LOG_LOGIC ("Multicast destination");
00583
00584 Ipv4MulticastRoute *mRoute = LookupStatic(Ipv4Address::GetAny (),
00585 destination, Ipv4RoutingProtocol::IF_INDEX_ANY);
00586
00587 if (mRoute)
00588 {
00589 NS_LOG_LOGIC ("Multicast route found");
00590
00591 if (mRoute->GetNOutputInterfaces () != 1)
00592 {
00593 NS_LOG_LOGIC ("Route is to multiple interfaces. Ignoring.");
00594 return false;
00595 }
00596
00597 ifIndex = mRoute->GetOutputInterface(0);
00598 NS_LOG_LOGIC ("Found ifIndex " << ifIndex);
00599 return true;
00600 }
00601 return false;
00602 }
00603
00604
00605
00606 NS_LOG_LOGIC ("Unicast destination");
00607 Ipv4Route *route = LookupStatic (destination);
00608 if (route)
00609 {
00610 ifIndex = route->GetInterface ();
00611 return true;
00612 }
00613 else
00614 {
00615 return false;
00616 }
00617 }
00618
00619 void
00620 Ipv4StaticRouting::DoDispose (void)
00621 {
00622 NS_LOG_FUNCTION_NOARGS ();
00623 for (HostRoutesI i = m_hostRoutes.begin ();
00624 i != m_hostRoutes.end ();
00625 i = m_hostRoutes.erase (i))
00626 {
00627 delete (*i);
00628 }
00629 for (NetworkRoutesI j = m_networkRoutes.begin ();
00630 j != m_networkRoutes.end ();
00631 j = m_networkRoutes.erase (j))
00632 {
00633 delete (*j);
00634 }
00635 if (m_defaultRoute != 0)
00636 {
00637 delete m_defaultRoute;
00638 m_defaultRoute = 0;
00639 }
00640 for (MulticastRoutesI i = m_multicastRoutes.begin ();
00641 i != m_multicastRoutes.end ();
00642 i = m_multicastRoutes.erase (i))
00643 {
00644 delete (*i);
00645 }
00646 if (m_defaultMulticastRoute != 0)
00647 {
00648 delete m_defaultMulticastRoute;
00649 m_defaultMulticastRoute = 0;
00650 }
00651 Ipv4RoutingProtocol::DoDispose ();
00652 }
00653
00654 }