mirror of
https://github.com/espressif/openthread.git
synced 2026-08-29 05:19:54 +00:00
Fixed bug with incorrect Router Id 62. (#625)
This commit is contained in:
committed by
Jonathan Hui
parent
025aa722da
commit
de579eef1c
@@ -515,7 +515,7 @@ ThreadError MeshForwarder::UpdateIp6Route(Message &aMessage)
|
||||
if (mMle.IsRoutingLocator(ip6Header.GetDestination()))
|
||||
{
|
||||
rloc16 = HostSwap16(ip6Header.GetDestination().mFields.m16[7]);
|
||||
VerifyOrExit(mMle.GetRouterId(rloc16) < Mle::kMaxRouterId, error = kThreadError_Drop);
|
||||
VerifyOrExit(mMle.IsRouterIdValid(mMle.GetRouterId(rloc16)), error = kThreadError_Drop);
|
||||
mMeshDest = rloc16;
|
||||
}
|
||||
else if ((neighbor = mMle.GetNeighbor(ip6Header.GetDestination())) != NULL)
|
||||
|
||||
@@ -1995,7 +1995,7 @@ ThreadError Mle::HandleChildIdResponse(const Message &aMessage, const Ip6::Messa
|
||||
{
|
||||
SuccessOrExit(error = mMleRouter.ProcessRouteTlv(route));
|
||||
|
||||
for (uint8_t i = 0; i < kMaxRouterId; i++)
|
||||
for (uint8_t i = 0; i <= kMaxRouterId; i++)
|
||||
{
|
||||
if (route.IsRouterIdSet(i))
|
||||
{
|
||||
|
||||
@@ -74,25 +74,26 @@ enum
|
||||
*/
|
||||
enum
|
||||
{
|
||||
kAdvertiseIntervalMin = 1, ///< ADVERTISEMENT_I_MIN (seconds)
|
||||
kAdvertiseIntervalMax = 32, ///< ADVERTISEMENT_I_MAX (seconds)
|
||||
kRouterIdReuseDelay = 100, ///< ID_REUSE_DELAY (seconds)
|
||||
kRouterIdSequencePeriod = 10, ///< ID_SEQUENCE_PERIOD (seconds)
|
||||
kMaxNeighborAge = 100, ///< MAX_NEIGHBOR_AGE (seconds)
|
||||
kMaxRouteCost = 16, ///< MAX_ROUTE_COST
|
||||
kMaxRouterId = 62, ///< MAX_ROUTER_ID
|
||||
kMaxRouters = 32, ///< MAX_ROUTERS
|
||||
kMinDowngradeNeighbors = 7, ///< MIN_DOWNGRADE_NEIGHBORS
|
||||
kNetworkIdTimeout = 120, ///< NETWORK_ID_TIMEOUT (seconds)
|
||||
kParentRouteToLeaderTimeout = 20, ///< PARENT_ROUTE_TO_LEADER_TIMEOUT (seconds)
|
||||
kRouterSelectionJitter = 120, ///< ROUTER_SELECTION_JITTER (seconds)
|
||||
kRouterDowngradeThreshold = 23, ///< ROUTER_DOWNGRADE_THRESHOLD (routers)
|
||||
kRouterUpgradeThreshold = 16, ///< ROUTER_UPGRADE_THRESHOLD (routers)
|
||||
kMaxLeaderToRouterTimeout = 90, ///< INFINITE_COST_TIMEOUT (seconds)
|
||||
kReedAdvertiseInterval = 570, ///< REED_ADVERTISEMENT_INTERVAL (seconds)
|
||||
kReedAdvertiseJitter = 60, ///< REED_ADVERTISEMENT_JITTER (seconds)
|
||||
kMleEndDeviceTimeout = 240, ///< MLE_END_DEVICE_TIMEOUT (seconds)
|
||||
kLeaderWeight = 64, ///< Default leader weight for the Thread Network Partition
|
||||
kAdvertiseIntervalMin = 1, ///< ADVERTISEMENT_I_MIN (seconds)
|
||||
kAdvertiseIntervalMax = 32, ///< ADVERTISEMENT_I_MAX (seconds)
|
||||
kRouterIdReuseDelay = 100, ///< ID_REUSE_DELAY (seconds)
|
||||
kRouterIdSequencePeriod = 10, ///< ID_SEQUENCE_PERIOD (seconds)
|
||||
kMaxNeighborAge = 100, ///< MAX_NEIGHBOR_AGE (seconds)
|
||||
kMaxRouteCost = 16, ///< MAX_ROUTE_COST
|
||||
kMaxRouterId = 62, ///< MAX_ROUTER_ID
|
||||
kInvalidRouterId = kMaxRouterId + 1, ///< Value indicating incorrect Router Id
|
||||
kMaxRouters = 32, ///< MAX_ROUTERS
|
||||
kMinDowngradeNeighbors = 7, ///< MIN_DOWNGRADE_NEIGHBORS
|
||||
kNetworkIdTimeout = 120, ///< NETWORK_ID_TIMEOUT (seconds)
|
||||
kParentRouteToLeaderTimeout = 20, ///< PARENT_ROUTE_TO_LEADER_TIMEOUT (seconds)
|
||||
kRouterSelectionJitter = 120, ///< ROUTER_SELECTION_JITTER (seconds)
|
||||
kRouterDowngradeThreshold = 23, ///< ROUTER_DOWNGRADE_THRESHOLD (routers)
|
||||
kRouterUpgradeThreshold = 16, ///< ROUTER_UPGRADE_THRESHOLD (routers)
|
||||
kMaxLeaderToRouterTimeout = 90, ///< INFINITE_COST_TIMEOUT (seconds)
|
||||
kReedAdvertiseInterval = 570, ///< REED_ADVERTISEMENT_INTERVAL (seconds)
|
||||
kReedAdvertiseJitter = 60, ///< REED_ADVERTISEMENT_JITTER (seconds)
|
||||
kMleEndDeviceTimeout = 240, ///< MLE_END_DEVICE_TIMEOUT (seconds)
|
||||
kLeaderWeight = 64, ///< Default leader weight for the Thread Network Partition
|
||||
};
|
||||
|
||||
enum
|
||||
|
||||
@@ -68,8 +68,8 @@ MleRouter::MleRouter(ThreadNetif &aThreadNetif):
|
||||
mLeaderWeight = kLeaderWeight;
|
||||
mFixedLeaderPartitionId = 0;
|
||||
mMaxChildrenAllowed = kMaxChildren;
|
||||
mRouterId = kMaxRouterId;
|
||||
mPreviousRouterId = kMaxRouterId;
|
||||
mRouterId = kInvalidRouterId;
|
||||
mPreviousRouterId = kInvalidRouterId;
|
||||
mRouterIdSequenceLastUpdated = 0;
|
||||
mRouterRoleEnabled = true;
|
||||
mRouterSelectionJitterTimeout = 0;
|
||||
@@ -94,13 +94,13 @@ void MleRouter::SetRouterRoleEnabled(bool aEnabled)
|
||||
|
||||
uint8_t MleRouter::AllocateRouterId(void)
|
||||
{
|
||||
uint8_t rval = kMaxRouterId;
|
||||
uint8_t rval = kInvalidRouterId;
|
||||
|
||||
// count available router ids
|
||||
uint8_t numAvailable = 0;
|
||||
uint8_t numAllocated = 0;
|
||||
|
||||
for (int i = 0; i < kMaxRouterId; i++)
|
||||
for (int i = 0; i <= kMaxRouterId; i++)
|
||||
{
|
||||
if (mRouters[i].mAllocated)
|
||||
{
|
||||
@@ -112,14 +112,14 @@ uint8_t MleRouter::AllocateRouterId(void)
|
||||
}
|
||||
}
|
||||
|
||||
VerifyOrExit(numAllocated < kMaxRouters && numAvailable > 0, rval = kMaxRouterId);
|
||||
VerifyOrExit(numAllocated < kMaxRouters && numAvailable > 0, rval = kInvalidRouterId);
|
||||
|
||||
// choose available router id at random
|
||||
uint8_t freeBit;
|
||||
freeBit = otPlatRandomGet() % numAvailable;
|
||||
|
||||
// allocate router id
|
||||
for (uint8_t i = 0; i < kMaxRouterId; i++)
|
||||
for (uint8_t i = 0; i <= kMaxRouterId; i++)
|
||||
{
|
||||
if (mRouters[i].mAllocated || mRouters[i].mReclaimDelay)
|
||||
{
|
||||
@@ -141,9 +141,9 @@ exit:
|
||||
|
||||
uint8_t MleRouter::AllocateRouterId(uint8_t aRouterId)
|
||||
{
|
||||
uint8_t rval = kMaxRouterId;
|
||||
uint8_t rval = kInvalidRouterId;
|
||||
|
||||
VerifyOrExit(!mRouters[aRouterId].mAllocated, rval = kMaxRouterId);
|
||||
VerifyOrExit(!mRouters[aRouterId].mAllocated, rval = kInvalidRouterId);
|
||||
|
||||
// init router state
|
||||
mRouters[aRouterId].mAllocated = true;
|
||||
@@ -167,7 +167,7 @@ ThreadError MleRouter::ReleaseRouterId(uint8_t aRouterId)
|
||||
mRouters[aRouterId].mAllocated = false;
|
||||
mRouters[aRouterId].mReclaimDelay = true;
|
||||
mRouters[aRouterId].mState = Neighbor::kStateInvalid;
|
||||
mRouters[aRouterId].mNextHop = kMaxRouterId;
|
||||
mRouters[aRouterId].mNextHop = kInvalidRouterId;
|
||||
mRouterIdSequence++;
|
||||
mRouterIdSequenceLastUpdated = Timer::GetNow();
|
||||
mAddressResolver.Remove(aRouterId);
|
||||
@@ -189,12 +189,12 @@ ThreadError MleRouter::BecomeRouter(ThreadStatusTlv::Status aStatus)
|
||||
error = kThreadError_Busy);
|
||||
VerifyOrExit(mRouterRoleEnabled && (mDeviceMode & ModeTlv::kModeFFD), error = kThreadError_InvalidState);
|
||||
|
||||
for (int i = 0; i < kMaxRouterId; i++)
|
||||
for (int i = 0; i <= kMaxRouterId; i++)
|
||||
{
|
||||
mRouters[i].mAllocated = false;
|
||||
mRouters[i].mReclaimDelay = false;
|
||||
mRouters[i].mState = Neighbor::kStateInvalid;
|
||||
mRouters[i].mNextHop = kMaxRouterId;
|
||||
mRouters[i].mNextHop = kInvalidRouterId;
|
||||
}
|
||||
|
||||
mSocket.Open(&MleRouter::HandleUdpReceive, this);
|
||||
@@ -230,12 +230,12 @@ ThreadError MleRouter::BecomeLeader(void)
|
||||
error = kThreadError_Busy);
|
||||
VerifyOrExit(mRouterRoleEnabled && (mDeviceMode & ModeTlv::kModeFFD), error = kThreadError_InvalidState);
|
||||
|
||||
for (int i = 0; i < kMaxRouterId; i++)
|
||||
for (int i = 0; i <= kMaxRouterId; i++)
|
||||
{
|
||||
mRouters[i].mAllocated = false;
|
||||
mRouters[i].mReclaimDelay = false;
|
||||
mRouters[i].mState = Neighbor::kStateInvalid;
|
||||
mRouters[i].mNextHop = kMaxRouterId;
|
||||
mRouters[i].mNextHop = kInvalidRouterId;
|
||||
}
|
||||
|
||||
mSocket.Open(&MleRouter::HandleUdpReceive, this);
|
||||
@@ -243,8 +243,8 @@ ThreadError MleRouter::BecomeLeader(void)
|
||||
mStateUpdateTimer.Start(kStateUpdatePeriod);
|
||||
mAddressResolver.Clear();
|
||||
|
||||
routerId = (mPreviousRouterId != kMaxRouterId) ? AllocateRouterId(mPreviousRouterId) : AllocateRouterId();
|
||||
VerifyOrExit(routerId < kMaxRouterId, error = kThreadError_NoBufs);
|
||||
routerId = IsRouterIdValid(mPreviousRouterId) ? AllocateRouterId(mPreviousRouterId) : AllocateRouterId();
|
||||
VerifyOrExit(IsRouterIdValid(routerId), error = kThreadError_NoBufs);
|
||||
mRouterId = static_cast<uint8_t>(routerId);
|
||||
mPreviousRouterId = mRouterId;
|
||||
|
||||
@@ -286,7 +286,7 @@ ThreadError MleRouter::HandleDetachStart(void)
|
||||
{
|
||||
ThreadError error = kThreadError_None;
|
||||
|
||||
for (int i = 0; i < kMaxRouterId; i++)
|
||||
for (int i = 0; i <= kMaxRouterId; i++)
|
||||
{
|
||||
mRouters[i].mState = Neighbor::kStateInvalid;
|
||||
}
|
||||
@@ -822,7 +822,8 @@ ThreadError MleRouter::HandleLinkAccept(const Message &aMessage, const Ip6::Mess
|
||||
mleFrameCounter.SetFrameCounter(linkFrameCounter.GetFrameCounter());
|
||||
}
|
||||
|
||||
VerifyOrExit((routerId = GetRouterId(sourceAddress.GetRloc16())) < kMaxRouterId, error = kThreadError_Parse);
|
||||
routerId = GetRouterId(sourceAddress.GetRloc16());
|
||||
VerifyOrExit(IsRouterIdValid(routerId), error = kThreadError_Parse);
|
||||
|
||||
if (routerId != mRouterId)
|
||||
{
|
||||
@@ -898,7 +899,7 @@ ThreadError MleRouter::HandleLinkAccept(const Message &aMessage, const Ip6::Mess
|
||||
LinkQualityInfo::ConvertLinkMarginToLinkQuality(linkMargin.GetLinkMargin());
|
||||
|
||||
// update routing table
|
||||
if (routerId != mRouterId && mRouters[routerId].mNextHop == kMaxRouterId)
|
||||
if (routerId != mRouterId && !IsRouterIdValid(mRouters[routerId].mNextHop))
|
||||
{
|
||||
mRouters[routerId].mNextHop = routerId;
|
||||
ResetAdvertiseInterval();
|
||||
@@ -1048,10 +1049,11 @@ uint8_t MleRouter::GetLinkCost(uint8_t aRouterId)
|
||||
{
|
||||
uint8_t rval;
|
||||
|
||||
assert(aRouterId <= kMaxRouterId);
|
||||
// kInvalidRouterId indicates non-existing next hop, hence return kMaxRouteCost for it.
|
||||
assert(aRouterId <= kInvalidRouterId);
|
||||
|
||||
VerifyOrExit(aRouterId != mRouterId &&
|
||||
aRouterId != kMaxRouterId &&
|
||||
IsRouterIdValid(aRouterId) &&
|
||||
mRouters[aRouterId].mState == Neighbor::kStateValid,
|
||||
rval = kMaxRouteCost);
|
||||
|
||||
@@ -1087,14 +1089,14 @@ ThreadError MleRouter::ProcessRouteTlv(const RouteTlv &aRoute)
|
||||
mRouterIdSequence = aRoute.GetRouterIdSequence();
|
||||
mRouterIdSequenceLastUpdated = Timer::GetNow();
|
||||
|
||||
for (uint8_t i = 0; i < kMaxRouterId; i++)
|
||||
for (uint8_t i = 0; i <= kMaxRouterId; i++)
|
||||
{
|
||||
old = mRouters[i].mAllocated;
|
||||
mRouters[i].mAllocated = aRoute.IsRouterIdSet(i);
|
||||
|
||||
if (old && !mRouters[i].mAllocated)
|
||||
{
|
||||
mRouters[i].mNextHop = kMaxRouterId;
|
||||
mRouters[i].mNextHop = kInvalidRouterId;
|
||||
mAddressResolver.Remove(i);
|
||||
}
|
||||
}
|
||||
@@ -1133,7 +1135,7 @@ bool MleRouter::IsSingleton(void)
|
||||
case kDeviceStateLeader:
|
||||
|
||||
// not a singleton if any other routers exist
|
||||
for (int i = 0; i < kMaxRouterId; i++)
|
||||
for (int i = 0; i <= kMaxRouterId; i++)
|
||||
{
|
||||
if (i != mRouterId && mRouters[i].mAllocated)
|
||||
{
|
||||
@@ -1185,7 +1187,7 @@ uint8_t MleRouter::GetActiveRouterCount(void) const
|
||||
{
|
||||
uint8_t rval = 0;
|
||||
|
||||
for (int i = 0; i < kMaxRouterId; i++)
|
||||
for (int i = 0; i <= kMaxRouterId; i++)
|
||||
{
|
||||
if (mRouters[i].mAllocated)
|
||||
{
|
||||
@@ -1247,7 +1249,7 @@ ThreadError MleRouter::HandleAdvertisement(const Message &aMessage, const Ip6::M
|
||||
|
||||
routerCount = 0;
|
||||
|
||||
for (uint8_t i = 0; i < kMaxRouterId; i++)
|
||||
for (uint8_t i = 0; i <= kMaxRouterId; i++)
|
||||
{
|
||||
if (route.IsRouterIdSet(i))
|
||||
{
|
||||
@@ -1281,7 +1283,8 @@ ThreadError MleRouter::HandleAdvertisement(const Message &aMessage, const Ip6::M
|
||||
SuccessOrExit(error = ProcessRouteTlv(route));
|
||||
}
|
||||
|
||||
VerifyOrExit((routerId = GetRouterId(sourceAddress.GetRloc16())) < kMaxRouterId, error = kThreadError_Parse);
|
||||
routerId = GetRouterId(sourceAddress.GetRloc16());
|
||||
VerifyOrExit(IsRouterIdValid(routerId), error = kThreadError_Parse);
|
||||
|
||||
router = NULL;
|
||||
|
||||
@@ -1310,7 +1313,7 @@ ThreadError MleRouter::HandleAdvertisement(const Message &aMessage, const Ip6::M
|
||||
|
||||
if ((mDeviceMode & ModeTlv::kModeFFD))
|
||||
{
|
||||
for (uint8_t i = 0, routeCount = 0; i < kMaxRouterId; i++)
|
||||
for (uint8_t i = 0, routeCount = 0; i <= kMaxRouterId; i++)
|
||||
{
|
||||
if (route.IsRouterIdSet(i) == false)
|
||||
{
|
||||
@@ -1329,7 +1332,7 @@ ThreadError MleRouter::HandleAdvertisement(const Message &aMessage, const Ip6::M
|
||||
}
|
||||
else
|
||||
{
|
||||
mRouters[GetLeaderId()].mNextHop = kMaxRouterId;
|
||||
mRouters[GetLeaderId()].mNextHop = kInvalidRouterId;
|
||||
}
|
||||
|
||||
break;
|
||||
@@ -1359,7 +1362,7 @@ ThreadError MleRouter::HandleAdvertisement(const Message &aMessage, const Ip6::M
|
||||
// check current active router number
|
||||
routerCount = 0;
|
||||
|
||||
for (uint8_t i = 0; i < kMaxRouterId; i++)
|
||||
for (uint8_t i = 0; i <= kMaxRouterId; i++)
|
||||
{
|
||||
if (route.IsRouterIdSet(i))
|
||||
{
|
||||
@@ -1425,7 +1428,7 @@ void MleRouter::UpdateRoutes(const RouteTlv &aRoute, uint8_t aRouterId)
|
||||
{
|
||||
update = false;
|
||||
|
||||
for (uint8_t i = 0, routeCount = 0; i < kMaxRouterId; i++)
|
||||
for (uint8_t i = 0, routeCount = 0; i <= kMaxRouterId; i++)
|
||||
{
|
||||
if (aRoute.IsRouterIdSet(i) == false)
|
||||
{
|
||||
@@ -1466,14 +1469,14 @@ void MleRouter::UpdateRoutes(const RouteTlv &aRoute, uint8_t aRouterId)
|
||||
}
|
||||
}
|
||||
|
||||
if (mRouters[i].mNextHop == kMaxRouterId || mRouters[i].mNextHop == aRouterId)
|
||||
if (!IsRouterIdValid(mRouters[i].mNextHop) || mRouters[i].mNextHop == aRouterId)
|
||||
{
|
||||
// route has no nexthop or nexthop is neighbor
|
||||
newCost = cost + GetLinkCost(aRouterId);
|
||||
|
||||
if (i == aRouterId)
|
||||
{
|
||||
if (mRouters[i].mNextHop == kMaxRouterId)
|
||||
if (!IsRouterIdValid(mRouters[i].mNextHop))
|
||||
{
|
||||
ResetAdvertiseInterval();
|
||||
}
|
||||
@@ -1483,7 +1486,7 @@ void MleRouter::UpdateRoutes(const RouteTlv &aRoute, uint8_t aRouterId)
|
||||
}
|
||||
else if (newCost <= kMaxRouteCost)
|
||||
{
|
||||
if (mRouters[i].mNextHop == kMaxRouterId)
|
||||
if (!IsRouterIdValid(mRouters[i].mNextHop))
|
||||
{
|
||||
ResetAdvertiseInterval();
|
||||
}
|
||||
@@ -1491,10 +1494,10 @@ void MleRouter::UpdateRoutes(const RouteTlv &aRoute, uint8_t aRouterId)
|
||||
mRouters[i].mNextHop = aRouterId;
|
||||
mRouters[i].mCost = cost;
|
||||
}
|
||||
else if (mRouters[i].mNextHop != kMaxRouterId)
|
||||
else if (IsRouterIdValid(mRouters[i].mNextHop))
|
||||
{
|
||||
ResetAdvertiseInterval();
|
||||
mRouters[i].mNextHop = kMaxRouterId;
|
||||
mRouters[i].mNextHop = kInvalidRouterId;
|
||||
mRouters[i].mCost = 0;
|
||||
mRouters[i].mLastHeard = Timer::GetNow();
|
||||
}
|
||||
@@ -1521,9 +1524,9 @@ void MleRouter::UpdateRoutes(const RouteTlv &aRoute, uint8_t aRouterId)
|
||||
|
||||
#if 1
|
||||
|
||||
for (uint8_t i = 0; i < kMaxRouterId; i++)
|
||||
for (uint8_t i = 0; i <= kMaxRouterId; i++)
|
||||
{
|
||||
if (mRouters[i].mAllocated == false || mRouters[i].mNextHop == kMaxRouterId)
|
||||
if (mRouters[i].mAllocated == false || !IsRouterIdValid(mRouters[i].mNextHop))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
@@ -1560,7 +1563,7 @@ ThreadError MleRouter::HandleParentRequest(const Message &aMessage, const Ip6::M
|
||||
VerifyOrExit(GetLeaderAge() < mNetworkIdTimeout, error = kThreadError_Drop);
|
||||
|
||||
// 3. Its current routing path cost to the Leader is infinite.
|
||||
VerifyOrExit(mRouters[GetLeaderId()].mNextHop != kMaxRouterId, error = kThreadError_Drop);
|
||||
VerifyOrExit(IsRouterIdValid(mRouters[GetLeaderId()].mNextHop), error = kThreadError_Drop);
|
||||
|
||||
macAddr.Set(aMessageInfo.GetPeerAddr());
|
||||
|
||||
@@ -1679,7 +1682,7 @@ void MleRouter::HandleStateUpdateTimer(void)
|
||||
}
|
||||
|
||||
// update router state
|
||||
for (uint8_t i = 0; i < kMaxRouterId; i++)
|
||||
for (uint8_t i = 0; i <= kMaxRouterId; i++)
|
||||
{
|
||||
if (mRouters[i].mState != Neighbor::kStateInvalid)
|
||||
{
|
||||
@@ -1687,7 +1690,7 @@ void MleRouter::HandleStateUpdateTimer(void)
|
||||
{
|
||||
mRouters[i].mState = Neighbor::kStateInvalid;
|
||||
mRouters[i].mLinkInfo.Clear();
|
||||
mRouters[i].mNextHop = kMaxRouterId;
|
||||
mRouters[i].mNextHop = kInvalidRouterId;
|
||||
mRouters[i].mLinkQualityOut = 0;
|
||||
mRouters[i].mLastHeard = Timer::GetNow();
|
||||
}
|
||||
@@ -1697,7 +1700,7 @@ void MleRouter::HandleStateUpdateTimer(void)
|
||||
{
|
||||
if (mRouters[i].mAllocated)
|
||||
{
|
||||
if (mRouters[i].mNextHop == kMaxRouterId &&
|
||||
if (!IsRouterIdValid(mRouters[i].mNextHop) &&
|
||||
(Timer::GetNow() - mRouters[i].mLastHeard) >= Timer::SecToMsec(kMaxLeaderToRouterTimeout))
|
||||
{
|
||||
ReleaseRouterId(i);
|
||||
@@ -1888,7 +1891,7 @@ ThreadError MleRouter::HandleChildIdRequest(const Message &aMessage, const Ip6::
|
||||
}
|
||||
|
||||
// Remove from router table
|
||||
for (int i = 0; i < kMaxRouterId; i++)
|
||||
for (int i = 0; i <= kMaxRouterId; i++)
|
||||
{
|
||||
if (mRouters[i].mState != Neighbor::kStateInvalid &&
|
||||
memcmp(&mRouters[i].mMacAddr, &macAddr, sizeof(mRouters[i].mMacAddr)) == 0)
|
||||
@@ -2441,7 +2444,7 @@ Neighbor *MleRouter::GetNeighbor(uint16_t aAddress)
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < kMaxRouterId; i++)
|
||||
for (int i = 0; i <= kMaxRouterId; i++)
|
||||
{
|
||||
if (mRouters[i].mState == Neighbor::kStateValid && mRouters[i].mValid.mRloc16 == aAddress)
|
||||
{
|
||||
@@ -2481,7 +2484,7 @@ Neighbor *MleRouter::GetNeighbor(const Mac::ExtAddress &aAddress)
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < kMaxRouterId; i++)
|
||||
for (int i = 0; i <= kMaxRouterId; i++)
|
||||
{
|
||||
if (mRouters[i].mState == Neighbor::kStateValid &&
|
||||
memcmp(&mRouters[i].mMacAddr, &aAddress, sizeof(mRouters[i].mMacAddr)) == 0)
|
||||
@@ -2578,7 +2581,7 @@ Neighbor *MleRouter::GetNeighbor(const Ip6::Address &aAddress)
|
||||
|
||||
VerifyOrExit(context.mContextId == 0, rval = NULL);
|
||||
|
||||
for (int i = 0; i < kMaxRouterId; i++)
|
||||
for (int i = 0; i <= kMaxRouterId; i++)
|
||||
{
|
||||
router = &mRouters[i];
|
||||
|
||||
@@ -2613,10 +2616,10 @@ uint16_t MleRouter::GetNextHop(uint16_t aDestination) const
|
||||
|
||||
routerId = GetRouterId(aDestination);
|
||||
|
||||
if (routerId < kMaxRouterId)
|
||||
if (IsRouterIdValid(routerId))
|
||||
{
|
||||
nextHopRouterId = mRouters[routerId].mNextHop;
|
||||
VerifyOrExit(nextHopRouterId != kMaxRouterId && mRouters[nextHopRouterId].mState != Neighbor::kStateInvalid, ;);
|
||||
VerifyOrExit(IsRouterIdValid(nextHopRouterId) && mRouters[nextHopRouterId].mState != Neighbor::kStateInvalid, ;);
|
||||
nextHopRloc16 = GetRloc16(mRouters[nextHopRouterId].mNextHop);
|
||||
}
|
||||
|
||||
@@ -2629,7 +2632,7 @@ uint8_t MleRouter::GetRouteCost(uint16_t aRloc16) const
|
||||
uint8_t routerId = GetRouterId(aRloc16);
|
||||
uint8_t rval;
|
||||
|
||||
VerifyOrExit(routerId < kMaxRouterId && mRouters[routerId].mNextHop != kMaxRouterId, rval = kMaxRouteCost);
|
||||
VerifyOrExit(IsRouterIdValid(routerId) && IsRouterIdValid(mRouters[routerId].mNextHop), rval = kMaxRouteCost);
|
||||
|
||||
rval = mRouters[routerId].mCost;
|
||||
|
||||
@@ -2696,7 +2699,7 @@ Router *MleRouter::GetRouters(uint8_t *aNumRouters)
|
||||
{
|
||||
if (aNumRouters != NULL)
|
||||
{
|
||||
*aNumRouters = kMaxRouterId;
|
||||
*aNumRouters = kMaxRouterId + 1;
|
||||
}
|
||||
|
||||
return mRouters;
|
||||
@@ -2766,7 +2769,7 @@ ThreadError MleRouter::GetRouterInfo(uint16_t aRouterId, otRouterInfo &aRouterIn
|
||||
routerId = static_cast<uint8_t>(aRouterId);
|
||||
}
|
||||
|
||||
VerifyOrExit(routerId <= kMaxRouterId, error = kThreadError_InvalidArgs);
|
||||
VerifyOrExit(IsRouterIdValid(routerId), error = kThreadError_InvalidArgs);
|
||||
|
||||
memcpy(&aRouterInfo.mExtAddress, &mRouters[routerId].mMacAddr, sizeof(aRouterInfo.mExtAddress));
|
||||
aRouterInfo.mAllocated = mRouters[routerId].mAllocated;
|
||||
@@ -2860,7 +2863,7 @@ ThreadError MleRouter::SendAddressSolicit(ThreadStatusTlv::Status aStatus)
|
||||
macAddr64Tlv.SetMacAddr(*mMac.GetExtAddress());
|
||||
SuccessOrExit(error = message->Append(&macAddr64Tlv, sizeof(macAddr64Tlv)));
|
||||
|
||||
if (mPreviousRouterId != kMaxRouterId)
|
||||
if (IsRouterIdValid(mPreviousRouterId))
|
||||
{
|
||||
rlocTlv.Init();
|
||||
rlocTlv.SetRloc16(GetRloc16(mPreviousRouterId));
|
||||
@@ -2965,7 +2968,7 @@ void MleRouter::HandleAddressSolicitResponse(Message &aMessage)
|
||||
|
||||
SuccessOrExit(ThreadTlv::GetTlv(aMessage, ThreadTlv::kRloc16, sizeof(rlocTlv), rlocTlv));
|
||||
VerifyOrExit(rlocTlv.IsValid(), ;);
|
||||
VerifyOrExit((routerId = GetRouterId(rlocTlv.GetRloc16())) < kMaxRouterId, ;);
|
||||
VerifyOrExit(IsRouterIdValid(routerId = GetRouterId(rlocTlv.GetRloc16())), ;);
|
||||
|
||||
SuccessOrExit(ThreadTlv::GetTlv(aMessage, ThreadTlv::kRouterMask, sizeof(routerMaskTlv), routerMaskTlv));
|
||||
VerifyOrExit(routerMaskTlv.IsValid(), ;);
|
||||
@@ -2980,7 +2983,7 @@ void MleRouter::HandleAddressSolicitResponse(Message &aMessage)
|
||||
mRouterIdSequence = routerMaskTlv.GetIdSequence();
|
||||
mRouterIdSequenceLastUpdated = Timer::GetNow();
|
||||
|
||||
for (uint8_t i = 0; i < kMaxRouterId; i++)
|
||||
for (uint8_t i = 0; i <= kMaxRouterId; i++)
|
||||
{
|
||||
old = mRouters[i].mAllocated;
|
||||
mRouters[i].mAllocated = routerMaskTlv.IsAssignedRouterIdSet(i);
|
||||
@@ -3039,7 +3042,7 @@ void MleRouter::HandleAddressSolicit(Coap::Header &aHeader, Message &aMessage, c
|
||||
ThreadExtMacAddressTlv macAddr64Tlv;
|
||||
ThreadRloc16Tlv rlocTlv;
|
||||
ThreadStatusTlv statusTlv;
|
||||
uint8_t routerId = kMaxRouterId;
|
||||
uint8_t routerId = kInvalidRouterId;
|
||||
|
||||
VerifyOrExit(aHeader.GetType() == Coap::Header::kTypeConfirmable && aHeader.GetCode() == Coap::Header::kCodePost,
|
||||
error = kThreadError_Parse);
|
||||
@@ -3053,7 +3056,7 @@ void MleRouter::HandleAddressSolicit(Coap::Header &aHeader, Message &aMessage, c
|
||||
VerifyOrExit(statusTlv.IsValid(), error = kThreadError_Parse);
|
||||
|
||||
// see if allocation already exists
|
||||
for (uint8_t i = 0; i < kMaxRouterId; i++)
|
||||
for (uint8_t i = 0; i <= kMaxRouterId; i++)
|
||||
{
|
||||
if (mRouters[i].mAllocated &&
|
||||
memcmp(&mRouters[i].mMacAddr, macAddr64Tlv.GetMacAddr(), sizeof(mRouters[i].mMacAddr)) == 0)
|
||||
@@ -3083,22 +3086,22 @@ void MleRouter::HandleAddressSolicit(Coap::Header &aHeader, Message &aMessage, c
|
||||
VerifyOrExit(rlocTlv.IsValid(), error = kThreadError_Parse);
|
||||
routerId = GetRouterId(rlocTlv.GetRloc16());
|
||||
|
||||
if (routerId >= kMaxRouterId)
|
||||
if (!IsRouterIdValid(routerId))
|
||||
{
|
||||
// requested Router ID is out of range
|
||||
routerId = kMaxRouterId;
|
||||
routerId = kInvalidRouterId;
|
||||
}
|
||||
else if (mRouters[routerId].mAllocated &&
|
||||
memcmp(&mRouters[routerId].mMacAddr, macAddr64Tlv.GetMacAddr(),
|
||||
sizeof(mRouters[routerId].mMacAddr)))
|
||||
{
|
||||
// requested Router ID is allocated to another device
|
||||
routerId = kMaxRouterId;
|
||||
routerId = kInvalidRouterId;
|
||||
}
|
||||
else if (!mRouters[routerId].mAllocated && mRouters[routerId].mReclaimDelay)
|
||||
{
|
||||
// requested Router ID is deallocated but within ID_REUSE_DELAY period
|
||||
routerId = kMaxRouterId;
|
||||
routerId = kInvalidRouterId;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -3107,7 +3110,7 @@ void MleRouter::HandleAddressSolicit(Coap::Header &aHeader, Message &aMessage, c
|
||||
}
|
||||
|
||||
// allocate new router id
|
||||
if (routerId >= kMaxRouterId)
|
||||
if (!IsRouterIdValid(routerId))
|
||||
{
|
||||
routerId = AllocateRouterId();
|
||||
}
|
||||
@@ -3116,7 +3119,7 @@ void MleRouter::HandleAddressSolicit(Coap::Header &aHeader, Message &aMessage, c
|
||||
otLogInfoMle("router id requested and provided!\n");
|
||||
}
|
||||
|
||||
if (routerId < kMaxRouterId)
|
||||
if (IsRouterIdValid(routerId))
|
||||
{
|
||||
memcpy(&mRouters[routerId].mMacAddr, macAddr64Tlv.GetMacAddr(), sizeof(mRouters[routerId].mMacAddr));
|
||||
}
|
||||
@@ -3155,10 +3158,10 @@ void MleRouter::SendAddressSolicitResponse(const Coap::Header &aRequestHeader, u
|
||||
SuccessOrExit(error = message->Append(responseHeader.GetBytes(), responseHeader.GetLength()));
|
||||
|
||||
statusTlv.Init();
|
||||
statusTlv.SetStatus((aRouterId >= kMaxRouterId) ? statusTlv.kNoAddressAvailable : statusTlv.kSuccess);
|
||||
statusTlv.SetStatus(!IsRouterIdValid(aRouterId) ? statusTlv.kNoAddressAvailable : statusTlv.kSuccess);
|
||||
SuccessOrExit(error = message->Append(&statusTlv, sizeof(statusTlv)));
|
||||
|
||||
if (aRouterId < kMaxRouterId)
|
||||
if (IsRouterIdValid(aRouterId))
|
||||
{
|
||||
rlocTlv.Init();
|
||||
rlocTlv.SetRloc16(GetRloc16(aRouterId));
|
||||
@@ -3168,7 +3171,7 @@ void MleRouter::SendAddressSolicitResponse(const Coap::Header &aRequestHeader, u
|
||||
routerMaskTlv.SetIdSequence(mRouterIdSequence);
|
||||
routerMaskTlv.ClearAssignedRouterIdMask();
|
||||
|
||||
for (uint8_t i = 0; i < kMaxRouterId; i++)
|
||||
for (uint8_t i = 0; i <= kMaxRouterId; i++)
|
||||
{
|
||||
if (mRouters[i].mAllocated)
|
||||
{
|
||||
@@ -3218,7 +3221,7 @@ void MleRouter::HandleAddressRelease(Coap::Header &aHeader, Message &aMessage,
|
||||
SuccessOrExit(error = ThreadTlv::GetTlv(aMessage, ThreadTlv::kExtMacAddress, sizeof(macAddr64Tlv), macAddr64Tlv));
|
||||
VerifyOrExit(macAddr64Tlv.IsValid(), error = kThreadError_Parse);
|
||||
|
||||
VerifyOrExit((routerId = GetRouterId(rlocTlv.GetRloc16())) < kMaxRouterId, error = kThreadError_Parse);
|
||||
VerifyOrExit(IsRouterIdValid(routerId = GetRouterId(rlocTlv.GetRloc16())), error = kThreadError_Parse);
|
||||
|
||||
router = &mRouters[routerId];
|
||||
VerifyOrExit(memcmp(&router->mMacAddr, macAddr64Tlv.GetMacAddr(), sizeof(router->mMacAddr)) == 0, ;);
|
||||
@@ -3329,7 +3332,7 @@ ThreadError MleRouter::AppendConnectivity(Message &aMessage)
|
||||
|
||||
tlv.SetActiveRouters(0);
|
||||
|
||||
for (int i = 0; i < kMaxRouterId; i++)
|
||||
for (int i = 0; i <= kMaxRouterId; i++)
|
||||
{
|
||||
if (mRouters[i].mAllocated)
|
||||
{
|
||||
@@ -3429,7 +3432,7 @@ ThreadError MleRouter::AppendRoute(Message &aMessage)
|
||||
tlv.SetRouterIdSequence(mRouterIdSequence);
|
||||
tlv.ClearRouterIdMask();
|
||||
|
||||
for (uint8_t i = 0; i < kMaxRouterId; i++)
|
||||
for (uint8_t i = 0; i <= kMaxRouterId; i++)
|
||||
{
|
||||
if (mRouters[i].mAllocated == false)
|
||||
{
|
||||
@@ -3446,7 +3449,7 @@ ThreadError MleRouter::AppendRoute(Message &aMessage)
|
||||
}
|
||||
else
|
||||
{
|
||||
if (mRouters[i].mNextHop == kMaxRouterId)
|
||||
if (!IsRouterIdValid(mRouters[i].mNextHop))
|
||||
{
|
||||
cost = 0;
|
||||
}
|
||||
@@ -3530,7 +3533,7 @@ bool MleRouter::HasOneNeighborwithComparableConnectivity(const RouteTlv &aRoute,
|
||||
uint8_t routerCount = 0;
|
||||
|
||||
// process local neighbor routers
|
||||
for (uint8_t i = 0; i < kMaxRouterId; i++)
|
||||
for (uint8_t i = 0; i <= kMaxRouterId; i++)
|
||||
{
|
||||
if (i == mRouterId)
|
||||
{
|
||||
@@ -3621,7 +3624,7 @@ uint8_t MleRouter::GetMinDowngradeNeighborRouters(void)
|
||||
uint8_t lqi;
|
||||
uint8_t routerCount = 0;
|
||||
|
||||
for (uint8_t i = 0; i < kMaxRouterId; i++)
|
||||
for (uint8_t i = 0; i <= kMaxRouterId; i++)
|
||||
{
|
||||
if (mRouters[i].mState != Neighbor::kStateValid)
|
||||
{
|
||||
|
||||
@@ -470,6 +470,17 @@ public:
|
||||
*/
|
||||
ThreadError SendLinkReject(const Ip6::Address &aDestination);
|
||||
|
||||
/**
|
||||
* This method checks if a given Router ID has correct value.
|
||||
*
|
||||
* @param[in] aRouterId The Router ID value.
|
||||
*
|
||||
* @retval TRUE If @p aRouterId is in correct range [0..62].
|
||||
* @retval FALSE If @p aRouterId is not a valid Router ID.
|
||||
*
|
||||
*/
|
||||
static bool IsRouterIdValid(uint8_t aRouterId) { return aRouterId <= kMaxRouterId; }
|
||||
|
||||
private:
|
||||
enum
|
||||
{
|
||||
@@ -562,7 +573,7 @@ private:
|
||||
|
||||
uint8_t mRouterIdSequence;
|
||||
uint32_t mRouterIdSequenceLastUpdated;
|
||||
Router mRouters[kMaxRouterId];
|
||||
Router mRouters[kMaxRouterId + 1];
|
||||
uint8_t mMaxChildrenAllowed;
|
||||
Child mChildren[kMaxChildren];
|
||||
|
||||
|
||||
@@ -605,7 +605,7 @@ private:
|
||||
kRouteCostMask = 0xf << kRouteCostOffset,
|
||||
};
|
||||
uint8_t mRouterIdSequence;
|
||||
uint8_t mRouterIdMask[BitVectorBytes(kMaxRouterId)];
|
||||
uint8_t mRouterIdMask[BitVectorBytes(kMaxRouterId + 1)];
|
||||
uint8_t mRouteData[kMaxRouters];
|
||||
} OT_TOOL_PACKED_END;
|
||||
|
||||
|
||||
@@ -462,7 +462,7 @@ public:
|
||||
|
||||
private:
|
||||
uint8_t mIdSequence;
|
||||
uint8_t mAssignedRouterIdMask[BitVectorBytes(Mle::kMaxRouterId)];
|
||||
uint8_t mAssignedRouterIdMask[BitVectorBytes(Mle::kMaxRouterId + 1)];
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user