#include <OSPFInterfaceState.h>
Inheritance diagram for OSPF::InterfaceState:
Public Member Functions | |
virtual | ~InterfaceState () |
virtual void | ProcessEvent (Interface *intf, Interface::InterfaceEventType event)=0 |
virtual Interface::InterfaceStateType | GetState (void) const =0 |
Protected Member Functions | |
void | ChangeState (Interface *intf, InterfaceState *newState, InterfaceState *currentState) |
void | CalculateDesignatedRouter (Interface *intf) |
void OSPF::InterfaceState::CalculateDesignatedRouter | ( | Interface * | intf | ) | [protected] |
00098 { 00099 OSPF::RouterID routerID = intf->parentArea->GetRouter ()->GetRouterID (); 00100 OSPF::DesignatedRouterID currentDesignatedRouter = intf->designatedRouter; 00101 OSPF::DesignatedRouterID currentBackupRouter = intf->backupDesignatedRouter; 00102 00103 unsigned int neighborCount = intf->neighboringRouters.size (); 00104 unsigned char repeatCount = 0; 00105 unsigned int i; 00106 00107 OSPF::DesignatedRouterID declaredBackup; 00108 unsigned char declaredBackupPriority; 00109 OSPF::RouterID declaredBackupID; 00110 bool backupDeclared; 00111 00112 OSPF::DesignatedRouterID declaredDesignatedRouter; 00113 unsigned char declaredDesignatedRouterPriority; 00114 OSPF::RouterID declaredDesignatedRouterID; 00115 bool designatedRouterDeclared; 00116 00117 do { 00118 // calculating backup designated router 00119 declaredBackup = OSPF::NullDesignatedRouterID; 00120 declaredBackupPriority = 0; 00121 declaredBackupID = OSPF::NullRouterID; 00122 backupDeclared = false; 00123 00124 OSPF::DesignatedRouterID highestRouter = OSPF::NullDesignatedRouterID; 00125 unsigned char highestPriority = 0; 00126 OSPF::RouterID highestID = OSPF::NullRouterID; 00127 00128 for (i = 0; i < neighborCount; i++) { 00129 OSPF::Neighbor* neighbor = intf->neighboringRouters[i]; 00130 unsigned char neighborPriority = neighbor->GetPriority (); 00131 00132 if (neighbor->GetState () < OSPF::Neighbor::TwoWayState) { 00133 continue; 00134 } 00135 if (neighborPriority == 0) { 00136 continue; 00137 } 00138 00139 OSPF::RouterID neighborID = neighbor->GetNeighborID (); 00140 OSPF::DesignatedRouterID neighborsDesignatedRouter = neighbor->GetDesignatedRouter (); 00141 OSPF::DesignatedRouterID neighborsBackupDesignatedRouter = neighbor->GetBackupDesignatedRouter (); 00142 00143 if (neighborsDesignatedRouter.routerID != neighborID) { 00144 if (neighborsBackupDesignatedRouter.routerID == neighborID) { 00145 if ((neighborPriority > declaredBackupPriority) || 00146 ((neighborPriority == declaredBackupPriority) && 00147 (neighborID > declaredBackupID))) 00148 { 00149 declaredBackup = neighborsBackupDesignatedRouter; 00150 declaredBackupPriority = neighborPriority; 00151 declaredBackupID = neighborID; 00152 backupDeclared = true; 00153 } 00154 } 00155 if (!backupDeclared) { 00156 if ((neighborPriority > highestPriority) || 00157 ((neighborPriority == highestPriority) && 00158 (neighborID > highestID))) 00159 { 00160 highestRouter.routerID = neighborID; 00161 highestRouter.ipInterfaceAddress = neighbor->GetAddress (); 00162 highestPriority = neighborPriority; 00163 highestID = neighborID; 00164 } 00165 } 00166 } 00167 } 00168 // also include the router itself in the calculations 00169 if (intf->routerPriority > 0) { 00170 if (currentDesignatedRouter.routerID != routerID) { 00171 if (currentBackupRouter.routerID == routerID) { 00172 if ((intf->routerPriority > declaredBackupPriority) || 00173 ((intf->routerPriority == declaredBackupPriority) && 00174 (routerID > declaredBackupID))) 00175 { 00176 declaredBackup.routerID = routerID; 00177 declaredBackup.ipInterfaceAddress = intf->interfaceAddressRange.address; 00178 declaredBackupPriority = intf->routerPriority; 00179 declaredBackupID = routerID; 00180 backupDeclared = true; 00181 } 00182 00183 } 00184 if (!backupDeclared) { 00185 if ((intf->routerPriority > highestPriority) || 00186 ((intf->routerPriority == highestPriority) && 00187 (routerID > highestID))) 00188 { 00189 declaredBackup.routerID = routerID; 00190 declaredBackup.ipInterfaceAddress = intf->interfaceAddressRange.address; 00191 declaredBackupPriority = intf->routerPriority; 00192 declaredBackupID = routerID; 00193 backupDeclared = true; 00194 } else { 00195 declaredBackup = highestRouter; 00196 declaredBackupPriority = highestPriority; 00197 declaredBackupID = highestID; 00198 backupDeclared = true; 00199 } 00200 } 00201 } 00202 } 00203 00204 // calculating backup designated router 00205 declaredDesignatedRouter = OSPF::NullDesignatedRouterID; 00206 declaredDesignatedRouterPriority = 0; 00207 declaredDesignatedRouterID = OSPF::NullRouterID; 00208 designatedRouterDeclared = false; 00209 00210 for (i = 0; i < neighborCount; i++) { 00211 OSPF::Neighbor* neighbor = intf->neighboringRouters[i]; 00212 unsigned char neighborPriority = neighbor->GetPriority (); 00213 00214 if (neighbor->GetState () < OSPF::Neighbor::TwoWayState) { 00215 continue; 00216 } 00217 if (neighborPriority == 0) { 00218 continue; 00219 } 00220 00221 OSPF::RouterID neighborID = neighbor->GetNeighborID (); 00222 OSPF::DesignatedRouterID neighborsDesignatedRouter = neighbor->GetDesignatedRouter (); 00223 OSPF::DesignatedRouterID neighborsBackupDesignatedRouter = neighbor->GetBackupDesignatedRouter (); 00224 00225 if (neighborsDesignatedRouter.routerID == neighborID) { 00226 if ((neighborPriority > declaredDesignatedRouterPriority) || 00227 ((neighborPriority == declaredDesignatedRouterPriority) && 00228 (neighborID > declaredDesignatedRouterID))) 00229 { 00230 declaredDesignatedRouter = neighborsDesignatedRouter; 00231 declaredDesignatedRouterPriority = neighborPriority; 00232 declaredDesignatedRouterID = neighborID; 00233 designatedRouterDeclared = true; 00234 } 00235 } 00236 } 00237 // also include the router itself in the calculations 00238 if (intf->routerPriority > 0) { 00239 if (currentDesignatedRouter.routerID == routerID) { 00240 if ((intf->routerPriority > declaredDesignatedRouterPriority) || 00241 ((intf->routerPriority == declaredDesignatedRouterPriority) && 00242 (routerID > declaredDesignatedRouterID))) 00243 { 00244 declaredDesignatedRouter.routerID = routerID; 00245 declaredDesignatedRouter.ipInterfaceAddress = intf->interfaceAddressRange.address; 00246 declaredDesignatedRouterPriority = intf->routerPriority; 00247 declaredDesignatedRouterID = routerID; 00248 designatedRouterDeclared = true; 00249 } 00250 00251 } 00252 } 00253 if (!designatedRouterDeclared) { 00254 declaredDesignatedRouter = declaredBackup; 00255 declaredDesignatedRouterPriority = declaredBackupPriority; 00256 declaredDesignatedRouterID = declaredBackupID; 00257 designatedRouterDeclared = true; 00258 } 00259 00260 // if the router is any kind of DR or is no longer one of them, then repeat 00261 if (((declaredDesignatedRouter.routerID != OSPF::NullRouterID) && 00262 ((currentDesignatedRouter.routerID == routerID) && 00263 (declaredDesignatedRouter.routerID != routerID)) || 00264 ((currentDesignatedRouter.routerID != routerID) && 00265 (declaredDesignatedRouter.routerID == routerID))) || 00266 ((declaredBackup.routerID != OSPF::NullRouterID) && 00267 ((currentBackupRouter.routerID == routerID) && 00268 (declaredBackup.routerID != routerID)) || 00269 ((currentBackupRouter.routerID != routerID) && 00270 (declaredBackup.routerID == routerID)))) 00271 { 00272 currentDesignatedRouter = declaredDesignatedRouter; 00273 currentBackupRouter = declaredBackup; 00274 repeatCount++; 00275 } else { 00276 repeatCount += 2; 00277 } 00278 00279 } while (repeatCount < 2); 00280 00281 OSPF::RouterID routersOldDesignatedRouterID = intf->designatedRouter.routerID; 00282 OSPF::RouterID routersOldBackupID = intf->backupDesignatedRouter.routerID; 00283 00284 intf->designatedRouter = declaredDesignatedRouter; 00285 intf->backupDesignatedRouter = declaredBackup; 00286 00287 bool wasBackupDesignatedRouter = (routersOldBackupID == routerID); 00288 bool wasDesignatedRouter = (routersOldDesignatedRouterID == routerID); 00289 bool wasOther = (intf->GetState () == OSPF::Interface::NotDesignatedRouterState); 00290 bool wasWaiting = (!wasBackupDesignatedRouter && !wasDesignatedRouter && !wasOther); 00291 bool isBackupDesignatedRouter = (declaredBackup.routerID == routerID); 00292 bool isDesignatedRouter = (declaredDesignatedRouter.routerID == routerID); 00293 bool isOther = (!isBackupDesignatedRouter && !isDesignatedRouter); 00294 00295 if (wasBackupDesignatedRouter) { 00296 if (isDesignatedRouter) { 00297 ChangeState (intf, new OSPF::InterfaceStateDesignatedRouter, this); 00298 } 00299 if (isOther) { 00300 ChangeState (intf, new OSPF::InterfaceStateNotDesignatedRouter, this); 00301 } 00302 } 00303 if (wasDesignatedRouter) { 00304 if (isBackupDesignatedRouter) { 00305 ChangeState (intf, new OSPF::InterfaceStateBackup, this); 00306 } 00307 if (isOther) { 00308 ChangeState (intf, new OSPF::InterfaceStateNotDesignatedRouter, this); 00309 } 00310 } 00311 if (wasOther) { 00312 if (isDesignatedRouter) { 00313 ChangeState (intf, new OSPF::InterfaceStateDesignatedRouter, this); 00314 } 00315 if (isBackupDesignatedRouter) { 00316 ChangeState (intf, new OSPF::InterfaceStateBackup, this); 00317 } 00318 } 00319 if (wasWaiting) { 00320 if (isDesignatedRouter) { 00321 ChangeState (intf, new OSPF::InterfaceStateDesignatedRouter, this); 00322 } 00323 if (isBackupDesignatedRouter) { 00324 ChangeState (intf, new OSPF::InterfaceStateBackup, this); 00325 } 00326 if (isOther) { 00327 ChangeState (intf, new OSPF::InterfaceStateNotDesignatedRouter, this); 00328 } 00329 } 00330 00331 for (i = 0; i < neighborCount; i++) { 00332 if ((intf->interfaceType == OSPF::Interface::NBMA) && 00333 ((!wasBackupDesignatedRouter && isBackupDesignatedRouter) || 00334 (!wasDesignatedRouter && isDesignatedRouter))) 00335 { 00336 if (intf->neighboringRouters[i]->GetPriority () == 0) { 00337 intf->neighboringRouters[i]->ProcessEvent (OSPF::Neighbor::Start); 00338 } 00339 } 00340 if ((declaredDesignatedRouter.routerID != routersOldDesignatedRouterID) || 00341 (declaredBackup.routerID != routersOldBackupID)) 00342 { 00343 if (intf->neighboringRouters[i]->GetState () >= OSPF::Neighbor::TwoWayState) { 00344 intf->neighboringRouters[i]->ProcessEvent (OSPF::Neighbor::IsAdjacencyOK); 00345 } 00346 } 00347 } 00348 }
void OSPF::InterfaceState::ChangeState | ( | Interface * | intf, | |
InterfaceState * | newState, | |||
InterfaceState * | currentState | |||
) | [protected] |
00011 { 00012 OSPF::Interface::InterfaceStateType oldState = currentState->GetState (); 00013 OSPF::Interface::InterfaceStateType nextState = newState->GetState (); 00014 OSPF::Interface::OSPFInterfaceType intfType = intf->GetType (); 00015 bool rebuildRoutingTable = false; 00016 00017 intf->ChangeState (newState, currentState); 00018 00019 if ((oldState == OSPF::Interface::DownState) || 00020 (nextState == OSPF::Interface::DownState) || 00021 (oldState == OSPF::Interface::LoopbackState) || 00022 (nextState == OSPF::Interface::LoopbackState) || 00023 (oldState == OSPF::Interface::DesignatedRouterState) || 00024 (nextState == OSPF::Interface::DesignatedRouterState) || 00025 ((intfType == OSPF::Interface::PointToPoint) && 00026 ((oldState == OSPF::Interface::PointToPointState) || 00027 (nextState == OSPF::Interface::PointToPointState))) || 00028 (((intfType == OSPF::Interface::Broadcast) || 00029 (intfType == OSPF::Interface::NBMA)) && 00030 ((oldState == OSPF::Interface::WaitingState) || 00031 (nextState == OSPF::Interface::WaitingState)))) 00032 { 00033 OSPF::RouterLSA* routerLSA = intf->GetArea ()->FindRouterLSA (intf->GetArea ()->GetRouter ()->GetRouterID ()); 00034 00035 if (routerLSA != NULL) { 00036 long sequenceNumber = routerLSA->getHeader ().getLsSequenceNumber (); 00037 if (sequenceNumber == MAX_SEQUENCE_NUMBER) { 00038 routerLSA->getHeader ().setLsAge (MAX_AGE); 00039 intf->GetArea ()->FloodLSA (routerLSA); 00040 routerLSA->IncrementInstallTime (); 00041 } else { 00042 OSPF::RouterLSA* newLSA = intf->GetArea ()->OriginateRouterLSA (); 00043 00044 newLSA->getHeader ().setLsSequenceNumber (sequenceNumber + 1); 00045 newLSA->getHeader ().setLsChecksum (0); // TODO: calculate correct LS checksum 00046 rebuildRoutingTable |= routerLSA->Update (newLSA); 00047 delete newLSA; 00048 00049 intf->GetArea ()->FloodLSA (routerLSA); 00050 } 00051 } else { // (lsa == NULL) -> This must be the first time any interface is up... 00052 OSPF::RouterLSA* newLSA = intf->GetArea ()->OriginateRouterLSA (); 00053 00054 rebuildRoutingTable |= intf->GetArea ()->InstallRouterLSA (newLSA); 00055 00056 routerLSA = intf->GetArea ()->FindRouterLSA (intf->GetArea ()->GetRouter ()->GetRouterID ()); 00057 00058 intf->GetArea ()->SetSPFTreeRoot (routerLSA); 00059 intf->GetArea ()->FloodLSA (newLSA); 00060 delete newLSA; 00061 } 00062 } 00063 00064 if (nextState == OSPF::Interface::DesignatedRouterState) { 00065 OSPF::NetworkLSA* newLSA = intf->GetArea ()->OriginateNetworkLSA (intf); 00066 if (newLSA != NULL) { 00067 rebuildRoutingTable |= intf->GetArea ()->InstallNetworkLSA (newLSA); 00068 00069 intf->GetArea ()->FloodLSA (newLSA); 00070 delete newLSA; 00071 } else { // no neighbors on the network -> old NetworkLSA must be flushed 00072 OSPF::NetworkLSA* oldLSA = intf->GetArea ()->FindNetworkLSA (ULongFromIPv4Address (intf->GetAddressRange ().address)); 00073 00074 if (oldLSA != NULL) { 00075 oldLSA->getHeader ().setLsAge (MAX_AGE); 00076 intf->GetArea ()->FloodLSA (oldLSA); 00077 oldLSA->IncrementInstallTime (); 00078 } 00079 } 00080 } 00081 00082 if (oldState == OSPF::Interface::DesignatedRouterState) { 00083 OSPF::NetworkLSA* networkLSA = intf->GetArea ()->FindNetworkLSA (ULongFromIPv4Address (intf->GetAddressRange ().address)); 00084 00085 if (networkLSA != NULL) { 00086 networkLSA->getHeader ().setLsAge (MAX_AGE); 00087 intf->GetArea ()->FloodLSA (networkLSA); 00088 networkLSA->IncrementInstallTime (); 00089 } 00090 } 00091 00092 if (rebuildRoutingTable) { 00093 intf->GetArea ()->GetRouter ()->RebuildRoutingTable (); 00094 } 00095 }
virtual Interface::InterfaceStateType OSPF::InterfaceState::GetState | ( | void | ) | const [pure virtual] |
virtual void OSPF::InterfaceState::ProcessEvent | ( | Interface * | intf, | |
Interface::InterfaceEventType | event | |||
) | [pure virtual] |