This commit is contained in:
Adam Ierymenko 2017-03-17 13:56:01 -07:00
commit cc883cc3d8
6 changed files with 99 additions and 41 deletions

View File

@ -54,7 +54,7 @@ ifeq ($(ZT_RULES_ENGINE_DEBUGGING),1)
endif endif
ifeq ($(ZT_DEBUG),1) ifeq ($(ZT_DEBUG),1)
DEFS+=-DZT_TRACE # DEFS+=-DZT_TRACE
override CFLAGS+=-Wall -g -O -pthread $(INCLUDES) $(DEFS) override CFLAGS+=-Wall -g -O -pthread $(INCLUDES) $(DEFS)
override CXXFLAGS+=-Wall -g -O -std=c++11 -pthread $(INCLUDES) $(DEFS) override CXXFLAGS+=-Wall -g -O -std=c++11 -pthread $(INCLUDES) $(DEFS)
override LDFLAGS+= override LDFLAGS+=
@ -96,6 +96,12 @@ endif
ifeq ($(CC_MACH),arm) ifeq ($(CC_MACH),arm)
ZT_ARCHITECTURE=3 ZT_ARCHITECTURE=3
endif endif
ifeq ($(CC_MACH),armv6)
ZT_ARCHITECTURE=3
endif
ifeq ($(CC_MACH),armv7)
ZT_ARCHITECTURE=3
endif
ifeq ($(CC_MACH),arm64) ifeq ($(CC_MACH),arm64)
ZT_ARCHITECTURE=4 ZT_ARCHITECTURE=4
endif endif
@ -104,6 +110,12 @@ ifeq ($(CC_MACH),aarch64)
endif endif
DEFS+=-DZT_BUILD_PLATFORM=1 -DZT_BUILD_ARCHITECTURE=$(ZT_ARCHITECTURE) -DZT_SOFTWARE_UPDATE_DEFAULT="\"disable\"" DEFS+=-DZT_BUILD_PLATFORM=1 -DZT_BUILD_ARCHITECTURE=$(ZT_ARCHITECTURE) -DZT_SOFTWARE_UPDATE_DEFAULT="\"disable\""
# Define some conservative CPU instruction set flags for arm32 since there's a ton of variation out there
ifeq ($(ZT_ARCHITECTURE),3)
override CFLAGS+=-march=armv6zk -mcpu=arm1176jzf-s -mfloat-abi=hard -mfpu=vfp
override CXXFLAGS+=-march=armv6zk -mcpu=arm1176jzf-s -mfloat-abi=hard -mfpu=vfp
endif
# Define this to build a static binary, which is needed to make this runnable on a few ancient Linux distros # Define this to build a static binary, which is needed to make this runnable on a few ancient Linux distros
ifeq ($(ZT_STATIC),1) ifeq ($(ZT_STATIC),1)
override LDFLAGS+=-static override LDFLAGS+=-static

View File

@ -61,15 +61,23 @@ public:
Dictionary(const char *s) Dictionary(const char *s)
{ {
Utils::scopy(_d,sizeof(_d),s); if (s) {
Utils::scopy(_d,sizeof(_d),s);
} else {
_d[0] = (char)0;
}
} }
Dictionary(const char *s,unsigned int len) Dictionary(const char *s,unsigned int len)
{ {
if (len > (C-1)) if (s) {
len = C-1; if (len > (C-1))
memcpy(_d,s,len); len = C-1;
_d[len] = (char)0; memcpy(_d,s,len);
_d[len] = (char)0;
} else {
_d[0] = (char)0;
}
} }
Dictionary(const Dictionary &d) Dictionary(const Dictionary &d)
@ -91,7 +99,12 @@ public:
*/ */
inline bool load(const char *s) inline bool load(const char *s)
{ {
return Utils::scopy(_d,sizeof(_d),s); if (s) {
return Utils::scopy(_d,sizeof(_d),s);
} else {
_d[0] = (char)0;
return true;
}
} }
/** /**

View File

@ -836,7 +836,7 @@ bool IncomingPacket::_doNETWORK_CREDENTIALS(const RuntimeEnvironment *RR,const S
bool trustEstablished = false; bool trustEstablished = false;
unsigned int p = ZT_PACKET_IDX_PAYLOAD; unsigned int p = ZT_PACKET_IDX_PAYLOAD;
while ((p < size())&&((*this)[p])) { while ((p < size())&&((*this)[p] != 0)) {
p += com.deserialize(*this,p); p += com.deserialize(*this,p);
if (com) { if (com) {
const SharedPtr<Network> network(RR->node->network(com.networkId())); const SharedPtr<Network> network(RR->node->network(com.networkId()));
@ -953,8 +953,8 @@ bool IncomingPacket::_doNETWORK_CONFIG_REQUEST(const RuntimeEnvironment *RR,cons
const uint64_t requestPacketId = packetId(); const uint64_t requestPacketId = packetId();
if (RR->localNetworkController) { if (RR->localNetworkController) {
const unsigned int metaDataLength = at<uint16_t>(ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_DICT_LEN); const unsigned int metaDataLength = (ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_DICT_LEN <= size()) ? at<uint16_t>(ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_DICT_LEN) : 0;
const char *metaDataBytes = (const char *)field(ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_DICT,metaDataLength); const char *metaDataBytes = (metaDataLength != 0) ? (const char *)field(ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_DICT,metaDataLength) : (const char *)0;
const Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> metaData(metaDataBytes,metaDataLength); const Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> metaData(metaDataBytes,metaDataLength);
RR->localNetworkController->request(nwid,(hopCount > 0) ? InetAddress() : _path->address(),requestPacketId,peer->identity(),metaData); RR->localNetworkController->request(nwid,(hopCount > 0) ? InetAddress() : _path->address(),requestPacketId,peer->identity(),metaData);
} else { } else {

View File

@ -1517,6 +1517,10 @@ void Network::_sendUpdatesToMembers(const MulticastGroup *const newMulticastGrou
Packet outp(*a,RR->identity.address(),Packet::VERB_NETWORK_CREDENTIALS); Packet outp(*a,RR->identity.address(),Packet::VERB_NETWORK_CREDENTIALS);
_config.com.serialize(outp); _config.com.serialize(outp);
outp.append((uint8_t)0x00); outp.append((uint8_t)0x00);
outp.append((uint16_t)0); // no capabilities
outp.append((uint16_t)0); // no tags
outp.append((uint16_t)0); // no revocations
outp.append((uint16_t)0); // no certificates of ownership
RR->sw->send(outp,true); RR->sw->send(outp,true);
} }
_announceMulticastGroupsTo(*a,groups); _announceMulticastGroupsTo(*a,groups);
@ -1529,6 +1533,10 @@ void Network::_sendUpdatesToMembers(const MulticastGroup *const newMulticastGrou
Packet outp(c,RR->identity.address(),Packet::VERB_NETWORK_CREDENTIALS); Packet outp(c,RR->identity.address(),Packet::VERB_NETWORK_CREDENTIALS);
_config.com.serialize(outp); _config.com.serialize(outp);
outp.append((uint8_t)0x00); outp.append((uint8_t)0x00);
outp.append((uint16_t)0); // no capabilities
outp.append((uint16_t)0); // no tags
outp.append((uint16_t)0); // no revocations
outp.append((uint16_t)0); // no certificates of ownership
RR->sw->send(outp,true); RR->sw->send(outp,true);
} }
_announceMulticastGroupsTo(c,groups); _announceMulticastGroupsTo(c,groups);

85
one.cpp
View File

@ -72,6 +72,7 @@
#include "osdep/OSUtils.hpp" #include "osdep/OSUtils.hpp"
#include "osdep/Http.hpp" #include "osdep/Http.hpp"
#include "osdep/Thread.hpp"
#include "service/OneService.hpp" #include "service/OneService.hpp"
@ -1209,6 +1210,52 @@ static void printHelp(const char *cn,FILE *out)
fprintf(out," -q - Query API (zerotier-cli)" ZT_EOL_S); fprintf(out," -q - Query API (zerotier-cli)" ZT_EOL_S);
} }
class _OneServiceRunner
{
public:
_OneServiceRunner(const char *pn,const std::string &hd,unsigned int p) : progname(pn),returnValue(0),port(p),homeDir(hd) {}
void threadMain()
throw()
{
try {
for(;;) {
zt1Service = OneService::newInstance(homeDir.c_str(),port);
switch(zt1Service->run()) {
case OneService::ONE_STILL_RUNNING: // shouldn't happen, run() won't return until done
case OneService::ONE_NORMAL_TERMINATION:
break;
case OneService::ONE_UNRECOVERABLE_ERROR:
fprintf(stderr,"%s: fatal error: %s" ZT_EOL_S,progname,zt1Service->fatalErrorMessage().c_str());
returnValue = 1;
break;
case OneService::ONE_IDENTITY_COLLISION: {
delete zt1Service;
zt1Service = (OneService *)0;
std::string oldid;
OSUtils::readFile((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret").c_str(),oldid);
if (oldid.length()) {
OSUtils::writeFile((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret.saved_after_collision").c_str(),oldid);
OSUtils::rm((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret").c_str());
OSUtils::rm((homeDir + ZT_PATH_SEPARATOR_S + "identity.public").c_str());
}
} continue; // restart!
}
break; // terminate loop -- normally we don't keep restarting
}
delete zt1Service;
zt1Service = (OneService *)0;
} catch ( ... ) {
fprintf(stderr,"%s: unexpected exception starting main OneService instance" ZT_EOL_S,progname);
returnValue = 1;
}
}
const char *progname;
unsigned int returnValue;
unsigned int port;
const std::string &homeDir;
};
#ifdef __WINDOWS__ #ifdef __WINDOWS__
int _tmain(int argc, _TCHAR* argv[]) int _tmain(int argc, _TCHAR* argv[])
#else #else
@ -1421,8 +1468,8 @@ int main(int argc,char **argv)
} else { } else {
// Running from service manager // Running from service manager
_winPokeAHole(); _winPokeAHole();
ZeroTierOneService zt1Service; ZeroTierOneService zt1WindowsService;
if (CServiceBase::Run(zt1Service) == TRUE) { if (CServiceBase::Run(zt1WindowsService) == TRUE) {
return 0; return 0;
} else { } else {
fprintf(stderr,"%s: unable to start service (try -h for help)" ZT_EOL_S,argv[0]); fprintf(stderr,"%s: unable to start service (try -h for help)" ZT_EOL_S,argv[0]);
@ -1448,35 +1495,11 @@ int main(int argc,char **argv)
} }
#endif // __UNIX_LIKE__ #endif // __UNIX_LIKE__
unsigned int returnValue = 0; _OneServiceRunner thr(argv[0],homeDir,port);
thr.threadMain();
//Thread::join(Thread::start(&thr));
for(;;) { OSUtils::rm(pidPath.c_str());
zt1Service = OneService::newInstance(homeDir.c_str(),port);
switch(zt1Service->run()) {
case OneService::ONE_STILL_RUNNING: // shouldn't happen, run() won't return until done
case OneService::ONE_NORMAL_TERMINATION:
break;
case OneService::ONE_UNRECOVERABLE_ERROR:
fprintf(stderr,"%s: fatal error: %s" ZT_EOL_S,argv[0],zt1Service->fatalErrorMessage().c_str());
returnValue = 1;
break;
case OneService::ONE_IDENTITY_COLLISION: {
delete zt1Service;
zt1Service = (OneService *)0;
std::string oldid;
OSUtils::readFile((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret").c_str(),oldid);
if (oldid.length()) {
OSUtils::writeFile((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret.saved_after_collision").c_str(),oldid);
OSUtils::rm((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret").c_str());
OSUtils::rm((homeDir + ZT_PATH_SEPARATOR_S + "identity.public").c_str());
}
} continue; // restart!
}
break; // terminate loop -- normally we don't keep restarting
}
delete zt1Service; return thr.returnValue;
zt1Service = (OneService *)0;
return returnValue;
} }

View File

@ -488,6 +488,8 @@ public:
,_updater((SoftwareUpdater *)0) ,_updater((SoftwareUpdater *)0)
,_updateAutoApply(false) ,_updateAutoApply(false)
,_primaryPort(port) ,_primaryPort(port)
,_v4TcpControlSocket((PhySocket *)0)
,_v6TcpControlSocket((PhySocket *)0)
,_lastDirectReceiveFromGlobal(0) ,_lastDirectReceiveFromGlobal(0)
#ifdef ZT_TCP_FALLBACK_RELAY #ifdef ZT_TCP_FALLBACK_RELAY
,_lastSendToGlobalV4(0) ,_lastSendToGlobalV4(0)