Code cleanup, and fix some unsafe pointer handling in Network.

This commit is contained in:
Adam Ierymenko 2014-10-29 13:57:37 -07:00
parent f65b48d447
commit 95f421024a
10 changed files with 86 additions and 97 deletions

View file

@ -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 ( ... ) {

View file

@ -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);

View file

@ -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;
};

View file

@ -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;

View file

@ -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())