浏览代码

More cleanup.

Adam Ierymenko 8 年之前
父节点
当前提交
f23a43fb81
共有 3 个文件被更改,包括 80 次插入131 次删除
  1. 9 27
      node/Utils.cpp
  2. 40 102
      node/Utils.hpp
  3. 31 2
      service/SoftwareUpdater.cpp

+ 9 - 27
node/Utils.cpp

@@ -55,6 +55,15 @@ namespace ZeroTier {
 
 
 const char Utils::HEXCHARS[16] = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' };
 const char Utils::HEXCHARS[16] = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' };
 
 
+// Crazy hack to force memory to be securely zeroed in spite of the best efforts of optimizing compilers.
+static void _Utils_doBurn(volatile uint8_t *ptr,unsigned int len)
+{
+	volatile uint8_t *const end = ptr + len;
+	while (ptr != end) *(ptr++) = (uint8_t)0;
+}
+static void (*volatile _Utils_doBurn_ptr)(volatile uint8_t *,unsigned int) = _Utils_doBurn;
+void Utils::burn(void *ptr,unsigned int len) { (_Utils_doBurn_ptr)((volatile uint8_t *)ptr,len); }
+
 static unsigned long _Utils_itoa(unsigned long n,char *s)
 static unsigned long _Utils_itoa(unsigned long n,char *s)
 {
 {
 	if (n == 0)
 	if (n == 0)
@@ -76,15 +85,6 @@ char *Utils::decimal(unsigned long n,char s[24])
 	return s;
 	return s;
 }
 }
 
 
-// Crazy hack to force memory to be securely zeroed in spite of the best efforts of optimizing compilers.
-static void _Utils_doBurn(volatile uint8_t *ptr,unsigned int len)
-{
-	volatile uint8_t *const end = ptr + len;
-	while (ptr != end) *(ptr++) = (uint8_t)0;
-}
-static void (*volatile _Utils_doBurn_ptr)(volatile uint8_t *,unsigned int) = _Utils_doBurn;
-void Utils::burn(void *ptr,unsigned int len) { (_Utils_doBurn_ptr)((volatile uint8_t *)ptr,len); }
-
 void Utils::getSecureRandom(void *buf,unsigned int bytes)
 void Utils::getSecureRandom(void *buf,unsigned int bytes)
 {
 {
 	static Mutex globalLock;
 	static Mutex globalLock;
@@ -171,22 +171,4 @@ void Utils::getSecureRandom(void *buf,unsigned int bytes)
 #endif // __WINDOWS__ or not
 #endif // __WINDOWS__ or not
 }
 }
 
 
-bool Utils::scopy(char *dest,unsigned int len,const char *src)
-{
-	if (!len)
-		return false; // sanity check
-	if (!src) {
-		*dest = (char)0;
-		return true;
-	}
-	char *end = dest + len;
-	while ((*dest++ = *src++)) {
-		if (dest == end) {
-			*(--dest) = (char)0;
-			return false;
-		}
-	}
-	return true;
-}
-
 } // namespace ZeroTier
 } // namespace ZeroTier

+ 40 - 102
node/Utils.hpp

@@ -228,7 +228,6 @@ public:
 	 * @param saveptr Pointer to a char * for temporary reentrant storage
 	 * @param saveptr Pointer to a char * for temporary reentrant storage
 	 */
 	 */
 	static inline char *stok(char *str,const char *delim,char **saveptr)
 	static inline char *stok(char *str,const char *delim,char **saveptr)
-		throw()
 	{
 	{
 #ifdef __WINDOWS__
 #ifdef __WINDOWS__
 		return strtok_s(str,delim,saveptr);
 		return strtok_s(str,delim,saveptr);
@@ -237,30 +236,11 @@ public:
 #endif
 #endif
 	}
 	}
 
 
-	// String to number converters -- defined here to permit portability
-	// ifdefs for platforms that lack some of the strtoXX functions.
-	static inline unsigned int strToUInt(const char *s)
-		throw()
-	{
-		return (unsigned int)strtoul(s,(char **)0,10);
-	}
-	static inline int strToInt(const char *s)
-		throw()
-	{
-		return (int)strtol(s,(char **)0,10);
-	}
-	static inline unsigned long strToULong(const char *s)
-		throw()
-	{
-		return strtoul(s,(char **)0,10);
-	}
-	static inline long strToLong(const char *s)
-		throw()
-	{
-		return strtol(s,(char **)0,10);
-	}
+	static inline unsigned int strToUInt(const char *s) { return (unsigned int)strtoul(s,(char **)0,10); }
+	static inline int strToInt(const char *s) { return (int)strtol(s,(char **)0,10); }
+	static inline unsigned long strToULong(const char *s) { return strtoul(s,(char **)0,10); }
+	static inline long strToLong(const char *s) { return strtol(s,(char **)0,10); }
 	static inline unsigned long long strToU64(const char *s)
 	static inline unsigned long long strToU64(const char *s)
-		throw()
 	{
 	{
 #ifdef __WINDOWS__
 #ifdef __WINDOWS__
 		return (unsigned long long)_strtoui64(s,(char **)0,10);
 		return (unsigned long long)_strtoui64(s,(char **)0,10);
@@ -269,7 +249,6 @@ public:
 #endif
 #endif
 	}
 	}
 	static inline long long strTo64(const char *s)
 	static inline long long strTo64(const char *s)
-		throw()
 	{
 	{
 #ifdef __WINDOWS__
 #ifdef __WINDOWS__
 		return (long long)_strtoi64(s,(char **)0,10);
 		return (long long)_strtoi64(s,(char **)0,10);
@@ -277,28 +256,11 @@ public:
 		return strtoll(s,(char **)0,10);
 		return strtoll(s,(char **)0,10);
 #endif
 #endif
 	}
 	}
-	static inline unsigned int hexStrToUInt(const char *s)
-		throw()
-	{
-		return (unsigned int)strtoul(s,(char **)0,16);
-	}
-	static inline int hexStrToInt(const char *s)
-		throw()
-	{
-		return (int)strtol(s,(char **)0,16);
-	}
-	static inline unsigned long hexStrToULong(const char *s)
-		throw()
-	{
-		return strtoul(s,(char **)0,16);
-	}
-	static inline long hexStrToLong(const char *s)
-		throw()
-	{
-		return strtol(s,(char **)0,16);
-	}
+	static inline unsigned int hexStrToUInt(const char *s) { return (unsigned int)strtoul(s,(char **)0,16); }
+	static inline int hexStrToInt(const char *s) { return (int)strtol(s,(char **)0,16); }
+	static inline unsigned long hexStrToULong(const char *s) { return strtoul(s,(char **)0,16); }
+	static inline long hexStrToLong(const char *s) { return strtol(s,(char **)0,16); }
 	static inline unsigned long long hexStrToU64(const char *s)
 	static inline unsigned long long hexStrToU64(const char *s)
-		throw()
 	{
 	{
 #ifdef __WINDOWS__
 #ifdef __WINDOWS__
 		return (unsigned long long)_strtoui64(s,(char **)0,16);
 		return (unsigned long long)_strtoui64(s,(char **)0,16);
@@ -307,7 +269,6 @@ public:
 #endif
 #endif
 	}
 	}
 	static inline long long hexStrTo64(const char *s)
 	static inline long long hexStrTo64(const char *s)
-		throw()
 	{
 	{
 #ifdef __WINDOWS__
 #ifdef __WINDOWS__
 		return (long long)_strtoi64(s,(char **)0,16);
 		return (long long)_strtoi64(s,(char **)0,16);
@@ -315,11 +276,6 @@ public:
 		return strtoll(s,(char **)0,16);
 		return strtoll(s,(char **)0,16);
 #endif
 #endif
 	}
 	}
-	static inline double strToDouble(const char *s)
-		throw()
-	{
-		return strtod(s,(char **)0);
-	}
 
 
 	/**
 	/**
 	 * Perform a safe C string copy, ALWAYS null-terminating the result
 	 * Perform a safe C string copy, ALWAYS null-terminating the result
@@ -332,7 +288,23 @@ public:
 	 * @param src Source string (if NULL, dest will receive a zero-length string and true is returned)
 	 * @param src Source string (if NULL, dest will receive a zero-length string and true is returned)
 	 * @return True on success, false on overflow (buffer will still be 0-terminated)
 	 * @return True on success, false on overflow (buffer will still be 0-terminated)
 	 */
 	 */
-	static bool scopy(char *dest,unsigned int len,const char *src);
+	static inline bool scopy(char *dest,unsigned int len,const char *src)
+	{
+		if (!len)
+			return false; // sanity check
+		if (!src) {
+			*dest = (char)0;
+			return true;
+		}
+		char *end = dest + len;
+		while ((*dest++ = *src++)) {
+			if (dest == end) {
+				*(--dest) = (char)0;
+				return false;
+			}
+		}
+		return true;
+	}
 
 
 	/**
 	/**
 	 * Count the number of bits set in an integer
 	 * Count the number of bits set in an integer
@@ -378,14 +350,13 @@ public:
 	}
 	}
 
 
 	// Byte swappers for big/little endian conversion
 	// Byte swappers for big/little endian conversion
-	static inline uint8_t hton(uint8_t n) throw() { return n; }
-	static inline int8_t hton(int8_t n) throw() { return n; }
-	static inline uint16_t hton(uint16_t n) throw() { return htons(n); }
-	static inline int16_t hton(int16_t n) throw() { return (int16_t)htons((uint16_t)n); }
-	static inline uint32_t hton(uint32_t n) throw() { return htonl(n); }
-	static inline int32_t hton(int32_t n) throw() { return (int32_t)htonl((uint32_t)n); }
+	static inline uint8_t hton(uint8_t n) { return n; }
+	static inline int8_t hton(int8_t n) { return n; }
+	static inline uint16_t hton(uint16_t n) { return htons(n); }
+	static inline int16_t hton(int16_t n) { return (int16_t)htons((uint16_t)n); }
+	static inline uint32_t hton(uint32_t n) { return htonl(n); }
+	static inline int32_t hton(int32_t n) { return (int32_t)htonl((uint32_t)n); }
 	static inline uint64_t hton(uint64_t n)
 	static inline uint64_t hton(uint64_t n)
-		throw()
 	{
 	{
 #if __BYTE_ORDER == __LITTLE_ENDIAN
 #if __BYTE_ORDER == __LITTLE_ENDIAN
 #if defined(__GNUC__) && (!defined(__OpenBSD__))
 #if defined(__GNUC__) && (!defined(__OpenBSD__))
@@ -406,16 +377,15 @@ public:
 		return n;
 		return n;
 #endif
 #endif
 	}
 	}
-	static inline int64_t hton(int64_t n) throw() { return (int64_t)hton((uint64_t)n); }
-
-	static inline uint8_t ntoh(uint8_t n) throw() { return n; }
-	static inline int8_t ntoh(int8_t n) throw() { return n; }
-	static inline uint16_t ntoh(uint16_t n) throw() { return ntohs(n); }
-	static inline int16_t ntoh(int16_t n) throw() { return (int16_t)ntohs((uint16_t)n); }
-	static inline uint32_t ntoh(uint32_t n) throw() { return ntohl(n); }
-	static inline int32_t ntoh(int32_t n) throw() { return (int32_t)ntohl((uint32_t)n); }
+	static inline int64_t hton(int64_t n) { return (int64_t)hton((uint64_t)n); }
+
+	static inline uint8_t ntoh(uint8_t n) { return n; }
+	static inline int8_t ntoh(int8_t n) { return n; }
+	static inline uint16_t ntoh(uint16_t n) { return ntohs(n); }
+	static inline int16_t ntoh(int16_t n) { return (int16_t)ntohs((uint16_t)n); }
+	static inline uint32_t ntoh(uint32_t n) { return ntohl(n); }
+	static inline int32_t ntoh(int32_t n) { return (int32_t)ntohl((uint32_t)n); }
 	static inline uint64_t ntoh(uint64_t n)
 	static inline uint64_t ntoh(uint64_t n)
-		throw()
 	{
 	{
 #if __BYTE_ORDER == __LITTLE_ENDIAN
 #if __BYTE_ORDER == __LITTLE_ENDIAN
 #if defined(__GNUC__) && !defined(__OpenBSD__)
 #if defined(__GNUC__) && !defined(__OpenBSD__)
@@ -436,39 +406,7 @@ public:
 		return n;
 		return n;
 #endif
 #endif
 	}
 	}
-	static inline int64_t ntoh(int64_t n) throw() { return (int64_t)ntoh((uint64_t)n); }
-
-	/**
-	 * Compare Peer version tuples
-	 *
-	 * @return -1, 0, or 1 based on whether first tuple is less than, equal to, or greater than second
-	 */
-	static inline int compareVersion(unsigned int maj1,unsigned int min1,unsigned int rev1,unsigned int b1,unsigned int maj2,unsigned int min2,unsigned int rev2,unsigned int b2)
-	{
-		if (maj1 > maj2)
-			return 1;
-		else if (maj1 < maj2)
-			return -1;
-		else {
-			if (min1 > min2)
-				return 1;
-			else if (min1 < min2)
-				return -1;
-			else {
-				if (rev1 > rev2)
-					return 1;
-				else if (rev1 < rev2)
-					return -1;
-				else {
-					if (b1 > b2)
-						return 1;
-					else if (b1 < b2)
-						return -1;
-					else return 0;
-				}
-			}
-		}
-	}
+	static inline int64_t ntoh(int64_t n) { return (int64_t)ntoh((uint64_t)n); }
 
 
 	/**
 	/**
 	 * Hexadecimal characters 0-f
 	 * Hexadecimal characters 0-f

+ 31 - 2
service/SoftwareUpdater.cpp

@@ -57,6 +57,35 @@
 
 
 namespace ZeroTier {
 namespace ZeroTier {
 
 
+static int _compareVersion(unsigned int maj1,unsigned int min1,unsigned int rev1,unsigned int b1,unsigned int maj2,unsigned int min2,unsigned int rev2,unsigned int b2)
+{
+	if (maj1 > maj2) {
+		return 1;
+	} else if (maj1 < maj2) {
+		return -1;
+	} else {
+		if (min1 > min2) {
+			return 1;
+		} else if (min1 < min2) {
+			return -1;
+		} else {
+			if (rev1 > rev2) {
+				return 1;
+			} else if (rev1 < rev2) {
+				return -1;
+			} else {
+				if (b1 > b2) {
+					return 1;
+				} else if (b1 < b2) {
+					return -1;
+				} else {
+					return 0;
+				}
+			}
+		}
+	}
+}
+
 SoftwareUpdater::SoftwareUpdater(Node &node,const std::string &homePath) :
 SoftwareUpdater::SoftwareUpdater(Node &node,const std::string &homePath) :
 	_node(node),
 	_node(node),
 	_lastCheckTime(0),
 	_lastCheckTime(0),
@@ -170,7 +199,7 @@ void SoftwareUpdater::handleSoftwareUpdateUserMessage(uint64_t origin,const void
 									const unsigned int dvMin = (unsigned int)OSUtils::jsonInt(d->second.meta[ZT_SOFTWARE_UPDATE_JSON_VERSION_MINOR],0);
 									const unsigned int dvMin = (unsigned int)OSUtils::jsonInt(d->second.meta[ZT_SOFTWARE_UPDATE_JSON_VERSION_MINOR],0);
 									const unsigned int dvRev = (unsigned int)OSUtils::jsonInt(d->second.meta[ZT_SOFTWARE_UPDATE_JSON_VERSION_REVISION],0);
 									const unsigned int dvRev = (unsigned int)OSUtils::jsonInt(d->second.meta[ZT_SOFTWARE_UPDATE_JSON_VERSION_REVISION],0);
 									const unsigned int dvBld = (unsigned int)OSUtils::jsonInt(d->second.meta[ZT_SOFTWARE_UPDATE_JSON_VERSION_BUILD],0);
 									const unsigned int dvBld = (unsigned int)OSUtils::jsonInt(d->second.meta[ZT_SOFTWARE_UPDATE_JSON_VERSION_BUILD],0);
-									if (Utils::compareVersion(dvMaj,dvMin,dvRev,dvBld,bestVMaj,bestVMin,bestVRev,bestVBld) > 0) {
+									if (_compareVersion(dvMaj,dvMin,dvRev,dvBld,bestVMaj,bestVMin,bestVRev,bestVBld) > 0) {
 										latest = &(d->second.meta);
 										latest = &(d->second.meta);
 										bestVMaj = dvMaj;
 										bestVMaj = dvMaj;
 										bestVMin = dvMin;
 										bestVMin = dvMin;
@@ -194,7 +223,7 @@ void SoftwareUpdater::handleSoftwareUpdateUserMessage(uint64_t origin,const void
 					} else { // VERB_LATEST
 					} else { // VERB_LATEST
 
 
 						if ((origin == ZT_SOFTWARE_UPDATE_SERVICE)&&
 						if ((origin == ZT_SOFTWARE_UPDATE_SERVICE)&&
-							  (Utils::compareVersion(rvMaj,rvMin,rvRev,rvBld,ZEROTIER_ONE_VERSION_MAJOR,ZEROTIER_ONE_VERSION_MINOR,ZEROTIER_ONE_VERSION_REVISION,ZEROTIER_ONE_VERSION_BUILD) > 0)&&
+							  (_compareVersion(rvMaj,rvMin,rvRev,rvBld,ZEROTIER_ONE_VERSION_MAJOR,ZEROTIER_ONE_VERSION_MINOR,ZEROTIER_ONE_VERSION_REVISION,ZEROTIER_ONE_VERSION_BUILD) > 0)&&
 							  (OSUtils::jsonString(req[ZT_SOFTWARE_UPDATE_JSON_UPDATE_SIGNED_BY],"") == ZT_SOFTWARE_UPDATE_SIGNING_AUTHORITY)) {
 							  (OSUtils::jsonString(req[ZT_SOFTWARE_UPDATE_JSON_UPDATE_SIGNED_BY],"") == ZT_SOFTWARE_UPDATE_SIGNING_AUTHORITY)) {
 							const unsigned long len = (unsigned long)OSUtils::jsonInt(req[ZT_SOFTWARE_UPDATE_JSON_UPDATE_SIZE],0);
 							const unsigned long len = (unsigned long)OSUtils::jsonInt(req[ZT_SOFTWARE_UPDATE_JSON_UPDATE_SIZE],0);
 							const std::string hash = OSUtils::jsonBinFromHex(req[ZT_SOFTWARE_UPDATE_JSON_UPDATE_HASH]);
 							const std::string hash = OSUtils::jsonBinFromHex(req[ZT_SOFTWARE_UPDATE_JSON_UPDATE_HASH]);