diff options
Diffstat (limited to 'node')
-rw-r--r-- | node/Network.cpp | 10 | ||||
-rw-r--r-- | node/Node.cpp | 14 | ||||
-rw-r--r-- | node/NodeConfig.hpp | 5 | ||||
-rw-r--r-- | node/RuntimeEnvironment.hpp | 12 | ||||
-rw-r--r-- | node/SharedPtr.hpp | 14 |
5 files changed, 29 insertions, 26 deletions
diff --git a/node/Network.cpp b/node/Network.cpp index e11d7d1a..2b9d59e1 100644 --- a/node/Network.cpp +++ b/node/Network.cpp @@ -535,15 +535,11 @@ void Network::threadMain() void Network::_CBhandleTapData(void *arg,const MAC &from,const MAC &to,unsigned int etherType,const Buffer<4096> &data) { - if ((!((Network *)arg)->_enabled)||(((Network *)arg)->status() != NETWORK_OK)) + SharedPtr<Network> network((Network *)arg,true); + if ((!network)||(!network->_enabled)||(network->status() != NETWORK_OK)) return; - - const RuntimeEnvironment *RR = ((Network *)arg)->RR; - if (RR->shutdownInProgress) - return; - try { - RR->sw->onLocalEthernet(SharedPtr<Network>((Network *)arg),from,to,etherType,data); + network->RR->sw->onLocalEthernet(network,from,to,etherType,data); } catch (std::exception &exc) { TRACE("unexpected exception handling local packet: %s",exc.what()); } catch ( ... ) { diff --git a/node/Node.cpp b/node/Node.cpp index 0758535e..58395b2d 100644 --- a/node/Node.cpp +++ b/node/Node.cpp @@ -99,8 +99,6 @@ struct _NodeImpl RuntimeEnvironment *RR = &renv; LOG("terminating: %s",reasonForTerminationStr.c_str()); - renv.shutdownInProgress = true; - running = false; #ifndef __WINDOWS__ @@ -109,9 +107,9 @@ struct _NodeImpl delete renv.updater; renv.updater = (SoftwareUpdater *)0; delete renv.nc; renv.nc = (NodeConfig *)0; // shut down all networks, close taps, etc. delete renv.topology; renv.topology = (Topology *)0; // now we no longer need routing info - delete renv.sw; renv.sw = (Switch *)0; // order matters less from here down delete renv.mc; renv.mc = (Multicaster *)0; delete renv.antiRec; renv.antiRec = (AntiRecursion *)0; + delete renv.sw; renv.sw = (Switch *)0; // order matters less from here down delete renv.http; renv.http = (HttpClient *)0; delete renv.prng; renv.prng = (CMWC4096 *)0; delete renv.log; renv.log = (Logger *)0; // but stop logging last of all @@ -271,16 +269,12 @@ Node::~Node() static void _CBztTraffic(const SharedPtr<Socket> &fromSock,void *arg,const InetAddress &from,Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> &data) { - const RuntimeEnvironment *RR = (const RuntimeEnvironment *)arg; - if ((RR->sw)&&(!RR->shutdownInProgress)) - RR->sw->onRemotePacket(fromSock,from,data); + ((const RuntimeEnvironment *)arg)->sw->onRemotePacket(fromSock,from,data); } static void _cbHandleGetRootTopology(void *arg,int code,const std::string &url,const std::string &body) { RuntimeEnvironment *RR = (RuntimeEnvironment *)arg; - if (RR->shutdownInProgress) - return; if ((code != 200)||(body.length() == 0)) { TRACE("failed to retrieve %s",url.c_str()); @@ -391,9 +385,9 @@ Node::ReasonForTermination Node::run() } RR->http = new HttpClient(); - RR->antiRec = new AntiRecursion(); - RR->mc = new Multicaster(RR); RR->sw = new Switch(RR); + RR->mc = new Multicaster(RR); + RR->antiRec = new AntiRecursion(); RR->topology = new Topology(RR); try { RR->nc = new NodeConfig(RR); diff --git a/node/NodeConfig.hpp b/node/NodeConfig.hpp index e858d6f7..498d43db 100644 --- a/node/NodeConfig.hpp +++ b/node/NodeConfig.hpp @@ -145,7 +145,7 @@ public: inline bool hasNetwork(uint64_t nwid) { Mutex::Lock _l(_networks_m); - return (_networks.count(nwid) > 0); + return (_networks.find(nwid) != _networks.end()); } /** @@ -163,12 +163,15 @@ public: return tapDevs; } +private: void _readLocalConfig(); void _writeLocalConfig(); const RuntimeEnvironment *RR; + Dictionary _localConfig; // persisted as local.conf Mutex _localConfig_m; + std::map< uint64_t,SharedPtr<Network> > _networks; // persisted in networks.d/ Mutex _networks_m; }; diff --git a/node/RuntimeEnvironment.hpp b/node/RuntimeEnvironment.hpp index 1061c452..360524b9 100644 --- a/node/RuntimeEnvironment.hpp +++ b/node/RuntimeEnvironment.hpp @@ -69,7 +69,6 @@ public: homePath(), identity(), initialized(false), - shutdownInProgress(false), tcpTunnelingEnabled(false), timeOfLastResynchronize(0), timeOfLastPacketReceived(0), @@ -79,9 +78,9 @@ public: log((Logger *)0), prng((CMWC4096 *)0), http((HttpClient *)0), - antiRec((AntiRecursion *)0), - mc((Multicaster *)0), sw((Switch *)0), + mc((Multicaster *)0), + antiRec((AntiRecursion *)0), topology((Topology *)0), nc((NodeConfig *)0), node((Node *)0), @@ -101,9 +100,6 @@ public: // Are we initialized? volatile bool initialized; - // Indicates that we are shutting down -- this is hacky, want to factor out - volatile bool shutdownInProgress; - // Are we in outgoing TCP failover mode? volatile bool tcpTunnelingEnabled; @@ -130,9 +126,9 @@ public: Logger *log; // null if logging is disabled CMWC4096 *prng; HttpClient *http; - AntiRecursion *antiRec; - Multicaster *mc; Switch *sw; + Multicaster *mc; + AntiRecursion *antiRec; Topology *topology; NodeConfig *nc; Node *node; diff --git a/node/SharedPtr.hpp b/node/SharedPtr.hpp index dcf26035..c1a7db70 100644 --- a/node/SharedPtr.hpp +++ b/node/SharedPtr.hpp @@ -64,6 +64,20 @@ public: ++obj->__refCount; } + SharedPtr(T *obj,bool runAwayFromZombies) + throw() : + _ptr(obj) + { + // HACK: this is used in "handlers" to take ownership of naked pointers, + // an ugly pattern that really ought to be factored out. + if (runAwayFromZombies) { + if ((int)(++obj->__refCount) < 2) { + --obj->__refCount; + _ptr = (T *)0; + } + } else ++obj->__refCount; + } + SharedPtr(const SharedPtr &sp) throw() : _ptr(sp._getAndInc()) |