From 99c5fae9dae76f260c641bc4411f38aad24f4ae1 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Thu, 8 May 2014 21:27:59 +0000 Subject: Make Service communicate via empty-line-delimited Dictionary objects instead of the old size prefix way. --- node/Constants.hpp | 8 +++++++ node/Service.cpp | 69 +++++++++++++++++++----------------------------------- node/Service.hpp | 30 ++++++++---------------- 3 files changed, 42 insertions(+), 65 deletions(-) (limited to 'node') diff --git a/node/Constants.hpp b/node/Constants.hpp index 782ea463..16e17aa3 100644 --- a/node/Constants.hpp +++ b/node/Constants.hpp @@ -366,6 +366,14 @@ error_no_byte_order_defined; */ #define ZT_ANTIRECURSION_HISTORY_SIZE 16 +/** + * TTL for certificates of membership on private networks + * + * This is the max delta for the timestamp field of a COM, so it's a window + * plus or minus the certificate's timestamp. In milliseconds. + */ +#define ZT_NETWORK_CERTIFICATE_TTL_WINDOW (ZT_NETWORK_AUTOCONF_DELAY * 4) + /** * How often to broadcast beacons over physical local LANs */ diff --git a/node/Service.cpp b/node/Service.cpp index b2d36c77..1e252a84 100644 --- a/node/Service.cpp +++ b/node/Service.cpp @@ -43,6 +43,7 @@ #include #include +#include "Constants.hpp" #include "Service.hpp" #include "RuntimeEnvironment.hpp" #include "Utils.hpp" @@ -91,32 +92,19 @@ bool Service::send(const Dictionary &msg) { if (_childStdin <= 0) return false; - - std::string mser = msg.toString(); - if (mser.length() > ZT_SERVICE_MAX_MESSAGE_SIZE) - return false; - - // This can technically block. We'll fix this if it ends up being a - // problem. - uint32_t len = Utils::hton((uint32_t)mser.length()); - if (write(_childStdin,&len,4) != 4) - return false; - if ((int)write(_childStdin,mser.data(),mser.length()) != (int)mser.length()) - return false; - - return true; + std::string mser(msg.toString()); + mser.append(ZT_EOL_S); + return ((long)::write(_childStdin,mser.data(),mser.length()) == (long)mser.length()); } void Service::threadMain() throw() { - char buf[131072]; + char buf[16384]; fd_set readfds,writefds,exceptfds; struct timeval tv; - - std::string stderrBuf; - std::string stdoutBuf; - unsigned int stdoutExpecting = 0; + int eolsInARow = 0; + std::string stderrBuf,stdoutBuf; while (_run) { if (_pid <= 0) { @@ -184,18 +172,18 @@ void Service::threadMain() tv.tv_sec = 1; tv.tv_usec = 0; - select(std::max(_childStdout,_childStderr)+1,&readfds,&writefds,&exceptfds,&tv); + ::select(std::max(_childStdout,_childStderr)+1,&readfds,&writefds,&exceptfds,&tv); if (!_run) { - if (_childStdin > 0) close(_childStdin); + if (_childStdin > 0) ::close(_childStdin); _childStdin = 0; - if (_childStdout > 0) close(_childStdout); - if (_childStderr > 0) close(_childStderr); + if (_childStdout > 0) ::close(_childStdout); + if (_childStderr > 0) ::close(_childStderr); return; } if ((_childStderr > 0)&&(FD_ISSET(_childStderr,&readfds))) { - int n = (int)read(_childStderr,buf,sizeof(buf)); + int n = (int)::read(_childStderr,buf,sizeof(buf)); for(int i=0;i 0)&&(FD_ISSET(_childStdout,&readfds))) { - int n = (int)read(_childStdout,buf,sizeof(buf)); + int n = (int)::read(_childStdout,buf,sizeof(buf)); for(int i=0;i= 2) { + // Two CRs in a row ends a message + try { + _handler(_arg,*this,Dictionary(stdoutBuf)); stdoutBuf = ""; - stdoutExpecting = 0; - } - } else if (stdoutBuf.length() == 4) { - stdoutExpecting = Utils::ntoh(*((const uint32_t *)stdoutBuf.data())); - stdoutBuf = ""; - if (stdoutExpecting > ZT_SERVICE_MAX_MESSAGE_SIZE) { - LOG("message size overrun from service %s: %u bytes -- restarting service",_name.c_str(),stdoutExpecting); - stdoutExpecting = 0; - kill(_pid,SIGKILL); - break; - } - } + } catch ( ... ) {} // handlers should not throw + } else stdoutBuf.push_back(buf[i]); } } } diff --git a/node/Service.hpp b/node/Service.hpp index 64ed108f..6d12cffc 100644 --- a/node/Service.hpp +++ b/node/Service.hpp @@ -34,12 +34,6 @@ #include "Constants.hpp" #include "Dictionary.hpp" #include "Thread.hpp" -#include "Mutex.hpp" - -/** - * Maximum size of a service message in bytes (sanity limit) - */ -#define ZT_SERVICE_MAX_MESSAGE_SIZE 131072 namespace ZeroTier { @@ -91,20 +85,12 @@ public: /** * @return Name of service */ - inline const char *name() const - throw() - { - return _name.c_str(); - } + inline const char *name() const throw() { return _name.c_str(); } /** * @return True if subprocess is running */ - inline bool running() const - throw() - { - return (_pid > 0); - } + inline bool running() const throw() { return (_pid > 0); } /** * Thread main method; do not call elsewhere @@ -114,15 +100,19 @@ public: private: const RuntimeEnvironment *_r; + Thread _thread; + std::string _path; std::string _name; void *_arg; void (*_handler)(void *,Service &,const Dictionary &); - long _pid; - int _childStdin; - int _childStdout; - int _childStderr; + volatile long _pid; + + volatile int _childStdin; + volatile int _childStdout; + volatile int _childStderr; + volatile bool _run; }; #endif // __WINDOWS__ -- cgit v1.2.3