From 7b58b80d9b4c757caef0ea718b3abc58de14114f Mon Sep 17 00:00:00 2001 From: Jonathan Hui Date: Fri, 26 Aug 2016 23:24:55 -0700 Subject: [PATCH] Remove globals from ip6_routes.cpp. --- src/core/net/ip6.cpp | 3 ++- src/core/net/ip6.hpp | 2 ++ src/core/net/ip6_routes.cpp | 25 +++++++++++++------------ src/core/net/ip6_routes.hpp | 24 ++++++++++++++++++------ 4 files changed, 35 insertions(+), 19 deletions(-) diff --git a/src/core/net/ip6.cpp b/src/core/net/ip6.cpp index 2188a6405..91f46c055 100644 --- a/src/core/net/ip6.cpp +++ b/src/core/net/ip6.cpp @@ -48,6 +48,7 @@ namespace Thread { namespace Ip6 { Ip6::Ip6(void): + mRoutes(*this), mIcmp(*this), mUdp(*this), mForwardingEnabled(false), @@ -530,7 +531,7 @@ ThreadError Ip6::ForwardMessage(Message &message, MessageInfo &messageInfo) // on-link global address ; } - else if ((interfaceId = Routes::Lookup(messageInfo.GetPeerAddr(), messageInfo.GetSockAddr())) > 0) + else if ((interfaceId = mRoutes.Lookup(messageInfo.GetPeerAddr(), messageInfo.GetSockAddr())) > 0) { // route ; diff --git a/src/core/net/ip6.hpp b/src/core/net/ip6.hpp index 7a10e7b88..2f42f858e 100644 --- a/src/core/net/ip6.hpp +++ b/src/core/net/ip6.hpp @@ -42,6 +42,7 @@ #include #include #include +#include #include #include #include @@ -322,6 +323,7 @@ public: */ int8_t GetOnLinkNetif(const Address &aAddress); + Routes mRoutes; Icmp mIcmp; Udp mUdp; diff --git a/src/core/net/ip6_routes.cpp b/src/core/net/ip6_routes.cpp index 4c59aae1e..52470b8ad 100644 --- a/src/core/net/ip6_routes.cpp +++ b/src/core/net/ip6_routes.cpp @@ -38,24 +38,25 @@ #include namespace Thread { - -extern Ip6::Ip6 *sIp6; - namespace Ip6 { -static Route *sRoutes = NULL; +Routes::Routes(Ip6 &aIp6): + mRoutes(NULL), + mIp6(aIp6) +{ +} ThreadError Routes::Add(Route &aRoute) { ThreadError error = kThreadError_None; - for (Route *cur = sRoutes; cur; cur = cur->mNext) + for (Route *cur = mRoutes; cur; cur = cur->mNext) { VerifyOrExit(cur != &aRoute, error = kThreadError_Busy); } - aRoute.mNext = sRoutes; - sRoutes = &aRoute; + aRoute.mNext = mRoutes; + mRoutes = &aRoute; exit: return error; @@ -63,13 +64,13 @@ exit: ThreadError Routes::Remove(Route &aRoute) { - if (&aRoute == sRoutes) + if (&aRoute == mRoutes) { - sRoutes = aRoute.mNext; + mRoutes = aRoute.mNext; } else { - for (Route *cur = sRoutes; cur; cur = cur->mNext) + for (Route *cur = mRoutes; cur; cur = cur->mNext) { if (cur->mNext == &aRoute) { @@ -90,7 +91,7 @@ int8_t Routes::Lookup(const Address &aSource, const Address &aDestination) uint8_t prefixMatch; int8_t rval = -1; - for (Route *cur = sRoutes; cur; cur = cur->mNext) + for (Route *cur = mRoutes; cur; cur = cur->mNext) { prefixMatch = cur->mPrefix.PrefixMatch(aDestination); @@ -113,7 +114,7 @@ int8_t Routes::Lookup(const Address &aSource, const Address &aDestination) rval = cur->mInterfaceId; } - for (Netif *netif = sIp6->GetNetifList(); netif; netif = netif->GetNext()) + for (Netif *netif = mIp6.GetNetifList(); netif; netif = netif->GetNext()) { if (netif->RouteLookup(aSource, aDestination, &prefixMatch) == kThreadError_None && static_cast(prefixMatch) > maxPrefixMatch) diff --git a/src/core/net/ip6_routes.hpp b/src/core/net/ip6_routes.hpp index c83dc0c4a..550f5fa83 100644 --- a/src/core/net/ip6_routes.hpp +++ b/src/core/net/ip6_routes.hpp @@ -68,7 +68,15 @@ class Routes { public: /** - * This static method adds an IPv6 route. + * This constructor initializes the object. + * + * @param[in] aIp6 A reference to the IPv6 network object. + * + */ + Routes(Ip6 &aIp6); + + /** + * This method adds an IPv6 route. * * @param[in] aRoute A reference to the IPv6 route. * @@ -76,10 +84,10 @@ public: * @retval kThreadError_Busy The route was already added. * */ - static ThreadError Add(Route &aRoute); + ThreadError Add(Route &aRoute); /** - * This static method removes an IPv6 route. + * This method removes an IPv6 route. * * @param[in] aRoute A reference to the IPv6 route. * @@ -87,10 +95,10 @@ public: * @retval kThreadError_InvalidArgs The route was not added. * */ - static ThreadError Remove(Route &aRoute); + ThreadError Remove(Route &aRoute); /** - * This static method performs source-destination route lookup. + * This method performs source-destination route lookup. * * @param[in] aSource The IPv6 source address. * @param[in] aDestination The IPv6 destination address. @@ -98,7 +106,11 @@ public: * @returns The interface identifier for the best route or -1 if no route is available. * */ - static int8_t Lookup(const Address &aSource, const Address &aDestination); + int8_t Lookup(const Address &aSource, const Address &aDestination); + +private: + Route *mRoutes; + Ip6 &mIp6; }; /**