src/routing/static-router.cc
changeset 1099 1e97c5a86b24
parent 1084 0be920a99cc4
child 1101 6a34eab865da
equal deleted inserted replaced
1098:fb9d81fae2c7 1099:1e97c5a86b24
    24 
    24 
    25 NS_DEBUG_COMPONENT_DEFINE ("StaticRouter");
    25 NS_DEBUG_COMPONENT_DEFINE ("StaticRouter");
    26 
    26 
    27 namespace ns3 {
    27 namespace ns3 {
    28 
    28 
       
    29 StaticRouterLinkRecord::StaticRouterLinkRecord ()
       
    30 :
       
    31   m_linkId ("0.0.0.0"),
       
    32   m_linkData ("0.0.0.0"),
       
    33   m_linkType (Unknown),
       
    34   m_metric (0)
       
    35 {
       
    36   NS_DEBUG("StaticRouterLinkRecord::StaticRouterLinkRecord ()");
       
    37 }
       
    38 
       
    39 StaticRouterLinkRecord::StaticRouterLinkRecord (
       
    40   LinkType    linkType, 
       
    41   Ipv4Address linkId, 
       
    42   Ipv4Address linkData, 
       
    43   uint32_t    metric)
       
    44 :
       
    45   m_linkId (linkId),
       
    46   m_linkData (linkData),
       
    47   m_linkType (linkType),
       
    48   m_metric (metric)
       
    49 {
       
    50   NS_DEBUG("StaticRouterLinkRecord::StaticRouterLinkRecord (" <<
       
    51     linkType << ", " << linkId << ", " << linkData << ", " << metric << ")");
       
    52 }
       
    53 
       
    54 StaticRouterLinkRecord::~StaticRouterLinkRecord ()
       
    55 {
       
    56   NS_DEBUG("StaticRouterLinkRecord::~StaticRouterLinkRecord ()");
       
    57 }
       
    58 
       
    59   Ipv4Address
       
    60 StaticRouterLinkRecord::GetLinkId (void) const
       
    61 {
       
    62   NS_DEBUG("StaticRouterLinkRecord::GetLinkId ()");
       
    63   return m_linkId;
       
    64 }
       
    65 
       
    66   void
       
    67 StaticRouterLinkRecord::SetLinkId (Ipv4Address addr)
       
    68 {
       
    69   NS_DEBUG("StaticRouterLinkRecord::SetLinkId ()");
       
    70   m_linkId = addr;
       
    71 }
       
    72 
       
    73   Ipv4Address
       
    74 StaticRouterLinkRecord::GetLinkData (void) const
       
    75 {
       
    76   NS_DEBUG("StaticRouterLinkRecord::GetLinkData ()");
       
    77   return m_linkData;
       
    78 }
       
    79 
       
    80   void
       
    81 StaticRouterLinkRecord::SetLinkData (Ipv4Address addr)
       
    82 {
       
    83   NS_DEBUG("StaticRouterLinkRecord::SetLinkData ()");
       
    84   m_linkData = addr;
       
    85 }
       
    86 
       
    87   StaticRouterLinkRecord::LinkType
       
    88 StaticRouterLinkRecord::GetLinkType (void) const
       
    89 {
       
    90   NS_DEBUG("StaticRouterLinkRecord::GetLinkType ()");
       
    91   return m_linkType;
       
    92 }
       
    93 
       
    94   void
       
    95 StaticRouterLinkRecord::SetLinkType (
       
    96   StaticRouterLinkRecord::LinkType linkType)
       
    97 {
       
    98   NS_DEBUG("StaticRouterLinkRecord::SetLinkType ()");
       
    99   m_linkType = linkType;
       
   100 }
       
   101 
       
   102   uint32_t
       
   103 StaticRouterLinkRecord::GetMetric (void) const
       
   104 {
       
   105   NS_DEBUG("StaticRouterLinkRecord::GetMetric ()");
       
   106   return m_metric;
       
   107 }
       
   108 
       
   109   void
       
   110 StaticRouterLinkRecord::SetMetric (uint32_t metric)
       
   111 {
       
   112   NS_DEBUG("StaticRouterLinkRecord::SetMetric ()");
       
   113   m_metric = metric;
       
   114 }
       
   115 
    29 StaticRouterLSA::StaticRouterLSA()
   116 StaticRouterLSA::StaticRouterLSA()
    30   : 
   117   : 
    31   m_linkStateId("0.0.0.0"),
   118   m_linkStateId("0.0.0.0"),
    32   m_advertisingRtr("0.0.0.0"),
   119   m_advertisingRtr("0.0.0.0"),
    33   m_linkRecords(),
   120   m_linkRecords(),
    34   m_stat(StaticRouterLSA::LSA_SPF_NOT_EXPLORED)
   121   m_status(StaticRouterLSA::LSA_SPF_NOT_EXPLORED)
    35 {
   122 {
    36   NS_DEBUG("StaticRouterLSA::StaticRouterLSA ()");
   123   NS_DEBUG("StaticRouterLSA::StaticRouterLSA ()");
       
   124 }
       
   125 
       
   126 StaticRouterLSA::StaticRouterLSA (
       
   127   StaticRouterLSA::SPFStatus status,
       
   128   Ipv4Address linkStateId, 
       
   129   Ipv4Address advertisingRtr)
       
   130 :
       
   131   m_linkStateId(linkStateId),
       
   132   m_advertisingRtr(advertisingRtr),
       
   133   m_linkRecords(),
       
   134   m_status(status)
       
   135 {
       
   136   NS_DEBUG("StaticRouterLSA::StaticRouterLSA (" << status << ", " <<
       
   137     linkStateId << ", " << advertisingRtr << ")");
    37 }
   138 }
    38 
   139 
    39 StaticRouterLSA::StaticRouterLSA (StaticRouterLSA& lsa)
   140 StaticRouterLSA::StaticRouterLSA (StaticRouterLSA& lsa)
    40   : m_linkStateId(lsa.m_linkStateId), m_advertisingRtr(lsa.m_advertisingRtr),
   141   : m_linkStateId(lsa.m_linkStateId), m_advertisingRtr(lsa.m_advertisingRtr),
    41     m_stat(lsa.m_stat)
   142     m_status(lsa.m_status)
    42 {
   143 {
    43   NS_ASSERT_MSG(IsEmpty(), 
   144   NS_ASSERT_MSG(IsEmpty(), 
    44     "StaticRouterLSA::StaticRouterLSA (): Non-empty LSA in constructor");
   145     "StaticRouterLSA::StaticRouterLSA (): Non-empty LSA in constructor");
    45   CopyLinkRecords (lsa);
   146   CopyLinkRecords (lsa);
    46 }
   147 }
    47 
   148 
    48   StaticRouterLSA&
   149   StaticRouterLSA&
    49 StaticRouterLSA::operator= (StaticRouterLSA& lsa)
   150 StaticRouterLSA::operator= (const StaticRouterLSA& lsa)
    50 {
   151 {
    51   m_linkStateId = lsa.m_linkStateId;
   152   m_linkStateId = lsa.m_linkStateId;
    52   m_advertisingRtr = lsa.m_advertisingRtr;
   153   m_advertisingRtr = lsa.m_advertisingRtr;
    53   m_stat = lsa.m_stat;
   154   m_status = lsa.m_status;
    54 
   155 
    55   ClearLinkRecords ();
   156   ClearLinkRecords ();
    56   CopyLinkRecords (lsa);
   157   CopyLinkRecords (lsa);
    57   return *this;
   158   return *this;
    58 }
   159 }
    59 
   160 
    60   void
   161   void
    61 StaticRouterLSA::CopyLinkRecords (StaticRouterLSA& lsa)
   162 StaticRouterLSA::CopyLinkRecords (const StaticRouterLSA& lsa)
    62 {
   163 {
    63   for ( ListOfLinkRecords_t::iterator i = lsa.m_linkRecords.begin ();
   164   for (ListOfLinkRecords_t::const_iterator i = lsa.m_linkRecords.begin ();
    64         i != lsa.m_linkRecords.end (); 
   165        i != lsa.m_linkRecords.end (); 
    65         i++)
   166        i++)
    66     {
   167     {
    67       StaticRouterLinkRecord *pSrc = *i;
   168       StaticRouterLinkRecord *pSrc = *i;
    68       StaticRouterLinkRecord *pDst = new StaticRouterLinkRecord;
   169       StaticRouterLinkRecord *pDst = new StaticRouterLinkRecord;
    69       pDst->m_linkId = pSrc->m_linkId;
   170 
    70       pDst->m_linkData = pSrc->m_linkData;
   171       pDst->SetLinkType (pSrc->GetLinkType ());
    71       pDst->m_linkType = pSrc->m_linkType;
   172       pDst->SetLinkId (pSrc->GetLinkId ());
       
   173       pDst->SetLinkData (pSrc->GetLinkData ());
       
   174 
    72       m_linkRecords.push_back(pDst);
   175       m_linkRecords.push_back(pDst);
    73       pDst = 0;
   176       pDst = 0;
    74     }
   177     }
    75 }
   178 }
    76 
   179 
   105   m_linkRecords.push_back (lr);
   208   m_linkRecords.push_back (lr);
   106   return m_linkRecords.size ();
   209   return m_linkRecords.size ();
   107 }
   210 }
   108 
   211 
   109   uint32_t
   212   uint32_t
   110 StaticRouterLSA::GetNLinkRecords (void)
   213 StaticRouterLSA::GetNLinkRecords (void) const
   111 {
   214 {
   112   return m_linkRecords.size ();
   215   return m_linkRecords.size ();
   113 }
   216 }
   114 
   217 
   115   StaticRouterLinkRecord *
   218   StaticRouterLinkRecord *
   116 StaticRouterLSA::GetLinkRecord (uint32_t n)
   219 StaticRouterLSA::GetLinkRecord (uint32_t n) const
   117 {
   220 {
   118   uint32_t j = 0;
   221   uint32_t j = 0;
   119   for ( ListOfLinkRecords_t::iterator i = m_linkRecords.begin ();
   222   for ( ListOfLinkRecords_t::const_iterator i = m_linkRecords.begin ();
   120         i != m_linkRecords.end (); 
   223         i != m_linkRecords.end (); 
   121         i++, j++)
   224         i++, j++)
   122     {
   225     {
   123       if (j == n) 
   226       if (j == n) 
   124         {
   227         {
   127     }
   230     }
   128   NS_ASSERT_MSG(false, "StaticRouterLSA::GetLinkRecord (): invalid index");
   231   NS_ASSERT_MSG(false, "StaticRouterLSA::GetLinkRecord (): invalid index");
   129   return 0;
   232   return 0;
   130 }
   233 }
   131 
   234 
   132 
       
   133   bool
   235   bool
   134 StaticRouterLSA::IsEmpty (void)
   236 StaticRouterLSA::IsEmpty (void) const
   135 {
   237 {
   136   return m_linkRecords.size () == 0;
   238   return m_linkRecords.size () == 0;
   137 }
   239 }
   138 
   240 
       
   241   Ipv4Address
       
   242 StaticRouterLSA::GetLinkStateId (void) const
       
   243 {
       
   244   return m_linkStateId;
       
   245 }
       
   246 
       
   247   void
       
   248 StaticRouterLSA::SetLinkStateId (Ipv4Address addr)
       
   249 {
       
   250   m_linkStateId = addr;
       
   251 }
       
   252 
       
   253   Ipv4Address
       
   254 StaticRouterLSA::GetAdvertisingRouter (void) const
       
   255 {
       
   256   return m_advertisingRtr;
       
   257 }
       
   258 
       
   259   void
       
   260 StaticRouterLSA::SetAdvertisingRouter (Ipv4Address addr)
       
   261 {
       
   262   m_advertisingRtr = addr;
       
   263 }
       
   264 
       
   265   StaticRouterLSA::SPFStatus
       
   266 StaticRouterLSA::GetStatus (void) const
       
   267 {
       
   268   return m_status;
       
   269 }
       
   270 
       
   271   void
       
   272 StaticRouterLSA::SetStatus (StaticRouterLSA::SPFStatus status)
       
   273 {
       
   274   m_status = status;
       
   275 }
       
   276 
   139   void 
   277   void 
   140 StaticRouterLSA::Print (std::ostream &os)
   278 StaticRouterLSA::Print (std::ostream &os) const
   141 {
   279 {
   142   os << "m_linkStateId = " << m_linkStateId << std::endl <<
   280   os << "m_linkStateId = " << m_linkStateId << std::endl <<
   143         "m_advertisingRtr = " << m_advertisingRtr << std::endl;
   281         "m_advertisingRtr = " << m_advertisingRtr << std::endl;
   144 
   282 
   145   for ( ListOfLinkRecords_t::iterator i = m_linkRecords.begin ();
   283   for ( ListOfLinkRecords_t::const_iterator i = m_linkRecords.begin ();
   146         i != m_linkRecords.end (); 
   284         i != m_linkRecords.end (); 
   147         i++)
   285         i++)
   148     {
   286     {
   149       StaticRouterLinkRecord *p = *i;
   287       StaticRouterLinkRecord *p = *i;
   150       os << "----------" << std::endl;
   288       os << "----------" << std::endl;
   151       os << "m_linkId = " << p->m_linkId << std::endl;
   289       os << "m_linkId = " << p->GetLinkId () << std::endl;
   152       os << "m_linkData = " << p->m_linkData << std::endl;
   290       os << "m_linkData = " << p->GetLinkData () << std::endl;
   153     }
   291     }
   154 }
   292 }
   155 
   293 
   156 std::ostream& operator<< (std::ostream& os, StaticRouterLSA& lsa)
   294 std::ostream& operator<< (std::ostream& os, StaticRouterLSA& lsa)
   157 {
   295 {
   196   NS_DEBUG("StaticRouter::ClearLSAs ():  clear list");
   334   NS_DEBUG("StaticRouter::ClearLSAs ():  clear list");
   197   m_LSAs.clear();
   335   m_LSAs.clear();
   198 }
   336 }
   199 
   337 
   200   Ipv4Address
   338   Ipv4Address
   201 StaticRouter::GetRouterId (void)
   339 StaticRouter::GetRouterId (void) const
   202 {
   340 {
   203   return m_routerId;
   341   return m_routerId;
   204 }
   342 }
   205 
   343 
   206 //
   344 //
   228 // What this means is that there is going to be one advertisement with some
   366 // What this means is that there is going to be one advertisement with some
   229 // number of link records.  This means that GetNumLSAs will actually always
   367 // number of link records.  This means that GetNumLSAs will actually always
   230 // return exactly one.
   368 // return exactly one.
   231 //
   369 //
   232   StaticRouterLSA *pLSA = new StaticRouterLSA;
   370   StaticRouterLSA *pLSA = new StaticRouterLSA;
   233   pLSA->m_linkStateId = m_routerId;
   371   pLSA->SetLinkStateId (m_routerId);
   234   pLSA->m_advertisingRtr = m_routerId;
   372   pLSA->SetAdvertisingRouter (m_routerId);
   235   pLSA->m_stat = StaticRouterLSA::LSA_SPF_NOT_EXPLORED;
   373   pLSA->SetStatus (StaticRouterLSA::LSA_SPF_NOT_EXPLORED);
   236 //
   374 //
   237 // We need to ask the node for the number of net devices attached. This isn't
   375 // We need to ask the node for the number of net devices attached. This isn't
   238 // necessarily equal to the number of links to adjacent nodes (other routers)
   376 // necessarily equal to the number of links to adjacent nodes (other routers)
   239 // as the number of devices may include those for stub networks (e.g., 
   377 // as the number of devices may include those for stub networks (e.g., 
   240 // ethernets, etc.).  So we have to walk through the list of net devices and
   378 // ethernets, etc.).  So we have to walk through the list of net devices and
   313 // Now we can fill out the link records for this link.  There are always two
   451 // Now we can fill out the link records for this link.  There are always two
   314 // link records; the first is a point-to-point record describing the link and
   452 // link records; the first is a point-to-point record describing the link and
   315 // the second is a stub network record with the network number.
   453 // the second is a stub network record with the network number.
   316 //
   454 //
   317       StaticRouterLinkRecord *plr = new StaticRouterLinkRecord;
   455       StaticRouterLinkRecord *plr = new StaticRouterLinkRecord;
   318       plr->m_linkType = StaticRouterLinkRecord::PointToPoint;
   456       plr->SetLinkType (StaticRouterLinkRecord::PointToPoint);
   319       plr->m_linkId = rtrIdRemote;
   457       plr->SetLinkId (rtrIdRemote);
   320       plr->m_linkData = addrLocal;
   458       plr->SetLinkData (addrLocal);
   321       pLSA->AddLinkRecord(plr);
   459       pLSA->AddLinkRecord(plr);
   322       plr = 0;
   460       plr = 0;
   323 
   461 
   324       plr = new StaticRouterLinkRecord;
   462       plr = new StaticRouterLinkRecord;
   325       plr->m_linkType = StaticRouterLinkRecord::StubNetwork;
   463       plr->SetLinkType (StaticRouterLinkRecord::StubNetwork);
   326       plr->m_linkId = addrRemote;
   464       plr->SetLinkId (addrRemote);
   327       plr->m_linkData.Set(maskRemote.GetHostOrder());  // Frown
   465       plr->SetLinkData (Ipv4Address(maskRemote.GetHostOrder()));  // Frown
   328       pLSA->AddLinkRecord(plr);
   466       pLSA->AddLinkRecord(plr);
   329       plr = 0;
   467       plr = 0;
   330     }
   468     }
   331 //
   469 //
   332 // The LSA goes on a list of LSAs in case we want to begin exporting other
   470 // The LSA goes on a list of LSAs in case we want to begin exporting other
   335   NS_DEBUG(*pLSA);
   473   NS_DEBUG(*pLSA);
   336   return m_LSAs.size ();
   474   return m_LSAs.size ();
   337 }
   475 }
   338 
   476 
   339   uint32_t 
   477   uint32_t 
   340 StaticRouter::GetNumLSAs (void)
   478 StaticRouter::GetNumLSAs (void) const
   341 {
   479 {
   342   NS_DEBUG("StaticRouter::GetNumLSAs ()");
   480   NS_DEBUG("StaticRouter::GetNumLSAs ()");
   343   return m_LSAs.size ();
   481   return m_LSAs.size ();
   344 }
   482 }
   345 
   483 
   346 //
   484 //
   347 // Get the nth link state advertisement from this router.
   485 // Get the nth link state advertisement from this router.
   348 //
   486 //
   349   bool
   487   bool
   350 StaticRouter::GetLSA (uint32_t n, StaticRouterLSA &lsa)
   488 StaticRouter::GetLSA (uint32_t n, StaticRouterLSA &lsa) const
   351 {
   489 {
   352   NS_ASSERT_MSG(lsa.IsEmpty(), "StaticRouter::GetLSA (): Must pass empty LSA");
   490   NS_ASSERT_MSG(lsa.IsEmpty(), "StaticRouter::GetLSA (): Must pass empty LSA");
   353 //
   491 //
   354 // All of the work was done in GetNumLSAs.  All we have to do here is to
   492 // All of the work was done in GetNumLSAs.  All we have to do here is to
   355 // walk the list of link state advertisements created there and return the 
   493 // walk the list of link state advertisements created there and return the 
   356 // one the client is interested in.
   494 // one the client is interested in.
   357 //
   495 //
   358   ListOfLSAs_t::iterator i = m_LSAs.begin ();
   496   ListOfLSAs_t::const_iterator i = m_LSAs.begin ();
   359   uint32_t j = 0;
   497   uint32_t j = 0;
   360 
   498 
   361   for (; i != m_LSAs.end (); i++, j++)
   499   for (; i != m_LSAs.end (); i++, j++)
   362     {
   500     {
   363       if (j == n)
   501       if (j == n)
   374 //
   512 //
   375 // Link through the given channel and find the net device that's on the
   513 // Link through the given channel and find the net device that's on the
   376 // other end.  This only makes sense with a point-to-point channel.
   514 // other end.  This only makes sense with a point-to-point channel.
   377 //
   515 //
   378   Ptr<NetDevice>
   516   Ptr<NetDevice>
   379 StaticRouter::GetAdjacent(Ptr<NetDevice> nd, Ptr<Channel> ch)
   517 StaticRouter::GetAdjacent(Ptr<NetDevice> nd, Ptr<Channel> ch) const
   380 {
   518 {
   381 //
   519 //
   382 // Double-check that channel agrees with device that it's a point-to-point
   520 // Double-check that channel agrees with device that it's a point-to-point
   383 //
   521 //
   384   NS_ASSERT(ch->GetType () == Channel::PointToPoint);
   522   NS_ASSERT(ch->GetType () == Channel::PointToPoint);
   415 //
   553 //
   416 // Given a node and a net device, find the IPV4 interface index that 
   554 // Given a node and a net device, find the IPV4 interface index that 
   417 // corresponds to that net device.
   555 // corresponds to that net device.
   418 //
   556 //
   419   uint32_t
   557   uint32_t
   420 StaticRouter::FindIfIndexForDevice(Ptr<Node> node, Ptr<NetDevice> nd)
   558 StaticRouter::FindIfIndexForDevice(Ptr<Node> node, Ptr<NetDevice> nd) const
   421 {
   559 {
   422   Ptr<Ipv4> ipv4 = node->QueryInterface<Ipv4> (Ipv4::iid);
   560   Ptr<Ipv4> ipv4 = node->QueryInterface<Ipv4> (Ipv4::iid);
   423   NS_ASSERT_MSG(ipv4, "QI for <Ipv4> interface failed");
   561   NS_ASSERT_MSG(ipv4, "QI for <Ipv4> interface failed");
   424   for (uint32_t i = 0; i < ipv4->GetNInterfaces(); ++i )
   562   for (uint32_t i = 0; i < ipv4->GetNInterfaces(); ++i )
   425     {
   563     {