diff options
-rwxr-xr-x | .gitignore | 1 | ||||
-rw-r--r-- | ZeroTierOne.vcxproj | 4 | ||||
-rw-r--r-- | ZeroTierOne.vcxproj.filters | 12 | ||||
-rw-r--r-- | ext/kissdb/kissdb.c | 9 | ||||
-rw-r--r-- | node/Address.hpp | 2 | ||||
-rw-r--r-- | node/Array.hpp | 9 | ||||
-rw-r--r-- | node/BloomFilter.hpp | 2 | ||||
-rw-r--r-- | node/Buffer.hpp | 7 | ||||
-rw-r--r-- | node/Constants.hpp | 17 | ||||
-rw-r--r-- | node/Demarc.cpp | 10 | ||||
-rw-r--r-- | node/EllipticCurveKeyPair.cpp | 30 | ||||
-rw-r--r-- | node/Identity.hpp | 4 | ||||
-rw-r--r-- | node/InetAddress.cpp | 21 | ||||
-rw-r--r-- | node/InetAddress.hpp | 14 | ||||
-rw-r--r-- | node/Logger.cpp | 7 | ||||
-rw-r--r-- | node/Network.cpp | 6 | ||||
-rw-r--r-- | node/Network.hpp | 8 | ||||
-rw-r--r-- | node/Node.cpp | 11 | ||||
-rw-r--r-- | node/NodeConfig.cpp | 7 | ||||
-rw-r--r-- | node/Packet.cpp | 2 | ||||
-rw-r--r-- | node/Packet.hpp | 8 | ||||
-rw-r--r-- | node/PacketDecoder.cpp | 4 | ||||
-rw-r--r-- | node/PacketDecoder.hpp | 2 | ||||
-rw-r--r-- | node/Peer.hpp | 4 | ||||
-rw-r--r-- | node/RateLimiter.hpp | 7 | ||||
-rw-r--r-- | node/SharedPtr.hpp | 2 | ||||
-rw-r--r-- | node/Switch.cpp | 7 | ||||
-rw-r--r-- | node/UdpSocket.cpp | 6 | ||||
-rw-r--r-- | node/Utils.cpp | 47 | ||||
-rw-r--r-- | node/Utils.hpp | 8 |
30 files changed, 191 insertions, 87 deletions
@@ -11,3 +11,4 @@ mac-tap/tuntap/tap.kext /ZeroTierOne.v11.suo /ZeroTierOne.opensdf +/Debug_32 diff --git a/ZeroTierOne.vcxproj b/ZeroTierOne.vcxproj index 866c9d50..35dd52ed 100644 --- a/ZeroTierOne.vcxproj +++ b/ZeroTierOne.vcxproj @@ -46,10 +46,7 @@ </ItemDefinitionGroup> <ItemGroup> <ClCompile Include="ext\kissdb\kissdb.c" /> - <ClCompile Include="ext\lz4\bench.c" /> - <ClCompile Include="ext\lz4\fuzzer.c" /> <ClCompile Include="ext\lz4\lz4.c" /> - <ClCompile Include="ext\lz4\lz4demo.c" /> <ClCompile Include="ext\lz4\lz4hc.c" /> <ClCompile Include="node\Defaults.cpp" /> <ClCompile Include="node\Demarc.cpp" /> @@ -151,7 +148,6 @@ <ClInclude Include="ext\bin\libcrypto\include\openssl\x509v3.h" /> <ClInclude Include="ext\bin\libcrypto\include\openssl\x509_vfy.h" /> <ClInclude Include="ext\kissdb\kissdb.h" /> - <ClInclude Include="ext\lz4\bench.h" /> <ClInclude Include="ext\lz4\lz4.h" /> <ClInclude Include="ext\lz4\lz4hc.h" /> <ClInclude Include="node\Address.hpp" /> diff --git a/ZeroTierOne.vcxproj.filters b/ZeroTierOne.vcxproj.filters index 72620a51..c4c2344e 100644 --- a/ZeroTierOne.vcxproj.filters +++ b/ZeroTierOne.vcxproj.filters @@ -18,18 +18,9 @@ <ClCompile Include="ext\kissdb\kissdb.c"> <Filter>Source Files</Filter> </ClCompile> - <ClCompile Include="ext\lz4\bench.c"> - <Filter>Source Files</Filter> - </ClCompile> - <ClCompile Include="ext\lz4\fuzzer.c"> - <Filter>Source Files</Filter> - </ClCompile> <ClCompile Include="ext\lz4\lz4.c"> <Filter>Source Files</Filter> </ClCompile> - <ClCompile Include="ext\lz4\lz4demo.c"> - <Filter>Source Files</Filter> - </ClCompile> <ClCompile Include="ext\lz4\lz4hc.c"> <Filter>Source Files</Filter> </ClCompile> @@ -332,9 +323,6 @@ <ClInclude Include="ext\kissdb\kissdb.h"> <Filter>Header Files</Filter> </ClInclude> - <ClInclude Include="ext\lz4\bench.h"> - <Filter>Header Files</Filter> - </ClInclude> <ClInclude Include="ext\lz4\lz4.h"> <Filter>Header Files</Filter> </ClInclude> diff --git a/ext/kissdb/kissdb.c b/ext/kissdb/kissdb.c index 1fb03cd5..ab33a5b3 100644 --- a/ext/kissdb/kissdb.c +++ b/ext/kissdb/kissdb.c @@ -15,7 +15,12 @@ #include <string.h> #include <stdlib.h> -#include <inttypes.h> +#include <stdint.h> + +#ifdef _WIN32 +#define fseeko _fseeki64 +#define ftello _ftelli64 +#endif #define KISSDB_HEADER_SIZE ((sizeof(uint64_t) * 3) + 4) @@ -322,6 +327,8 @@ int KISSDB_Iterator_next(KISSDB_Iterator *dbi,void *kbuf,void *vbuf) #ifdef KISSDB_TEST +#include <inttypes.h> + int main(int argc,char **argv) { uint64_t i,j; diff --git a/node/Address.hpp b/node/Address.hpp index fce4f20c..8baf9dbe 100644 --- a/node/Address.hpp +++ b/node/Address.hpp @@ -205,7 +205,7 @@ public: /** * @return True if this address is not zero */ - inline operator bool() const throw() { return (_a); } + inline operator bool() const throw() { return (_a != 0); } /** * @return Sum of all bytes in address diff --git a/node/Array.hpp b/node/Array.hpp index d0fe10ec..d48c2f52 100644 --- a/node/Array.hpp +++ b/node/Array.hpp @@ -94,7 +94,14 @@ public: inline reference back() throw() { return data[S-1]; } inline const_reference back() const throw() { return data[S-1]; } - inline bool operator==(const Array &k) const throw() { return std::equal(begin(),end(),k.begin()); } + inline bool operator==(const Array &k) const throw() + { + for(unsigned long i=0;i<S;++i) { + if (data[i] != k.data[i]) + return false; + } + return true; + } inline bool operator<(const Array &k) const throw() { return std::lexicographical_compare(begin(),end(),k.begin(),k.end()); } inline bool operator!=(const Array &k) const throw() { return !(*this == k); } inline bool operator>(const Array &k) const throw() { return (k < *this); } diff --git a/node/BloomFilter.hpp b/node/BloomFilter.hpp index ef48c9f0..f3071c0a 100644 --- a/node/BloomFilter.hpp +++ b/node/BloomFilter.hpp @@ -110,7 +110,7 @@ public: throw() { n %= B; - return (_field[n / 8] & (1 << (n % 8))); + return ((_field[n / 8] & (1 << (n % 8))) != 0); } /** diff --git a/node/Buffer.hpp b/node/Buffer.hpp index 14dd05c3..91bc1027 100644 --- a/node/Buffer.hpp +++ b/node/Buffer.hpp @@ -28,12 +28,15 @@ #ifndef _ZT_BUFFER_HPP #define _ZT_BUFFER_HPP +#include <string.h> +#include <stdint.h> + #include <stdexcept> #include <string> #include <algorithm> #include <utility> -#include <string.h> -#include <stdint.h> + +#include "Constants.hpp" #include "Utils.hpp" #ifdef __GNUC__ diff --git a/node/Constants.hpp b/node/Constants.hpp index da1195b3..20ec0a6b 100644 --- a/node/Constants.hpp +++ b/node/Constants.hpp @@ -68,6 +68,9 @@ #ifndef __WINDOWS__ #define __WINDOWS__ #endif +#define NOMINMAX +#pragma warning(disable : 4290) +#pragma warning(disable : 4996) #undef __UNIX_LIKE__ #define ZT_PATH_SEPARATOR '\\' #define ZT_PATH_SEPARATOR_S "\\" @@ -96,11 +99,23 @@ error_no_byte_order_defined; #endif #ifndef ZT_OSNAME -error_no_ZT_OSNAME_defined; +#ifdef __WINDOWS__ +#define ZT_OSNAME "windows" +#else +no ZT_OSNAME defined; +#endif #endif #ifndef ZT_ARCH +#ifdef __WINDOWS__ +#ifdef _WIN64 +#define ZT_ARCH "x64" +#else +#define ZT_ARCH "x86" +#endif +#else error_no_ZT_ARCH_defined; #endif +#endif /** * Length of a ZeroTier address in bytes diff --git a/node/Demarc.cpp b/node/Demarc.cpp index b935ce53..9c78fb65 100644 --- a/node/Demarc.cpp +++ b/node/Demarc.cpp @@ -26,6 +26,14 @@ */ #include <vector> + +#include "Constants.hpp" + +#ifdef __WINDOWS__ +#include <WinSock2.h> +#include <Windows.h> +#endif + #include "Demarc.hpp" #include "RuntimeEnvironment.hpp" #include "Logger.hpp" @@ -82,7 +90,7 @@ bool Demarc::has(Port p) const throw() { Mutex::Lock _l(_ports_m); - return (_ports.count(p)); + return (_ports.count(p) != 0); } bool Demarc::bindLocalUdp(unsigned int localPort) diff --git a/node/EllipticCurveKeyPair.cpp b/node/EllipticCurveKeyPair.cpp index d2e2920a..047b2b4f 100644 --- a/node/EllipticCurveKeyPair.cpp +++ b/node/EllipticCurveKeyPair.cpp @@ -29,6 +29,13 @@ #include <stdlib.h> #include <string.h> +#include "Constants.hpp" + +#ifdef __WINDOWS__ +#include <WinSock2.h> +#include <Windows.h> +#endif + #include <openssl/bn.h> #include <openssl/obj_mac.h> #include <openssl/rand.h> @@ -129,32 +136,9 @@ const EllipticCurveKeyPair &EllipticCurveKeyPair::operator=(const EllipticCurveK bool EllipticCurveKeyPair::generate() { - unsigned char tmp[16384]; EC_KEY *key; int len; - // Make sure OpenSSL libcrypto has sufficient randomness (on most - // platforms it auto-seeds, so this is a sanity check). - if (!RAND_status()) { -#if defined(__APPLE__) || defined(__linux__) || defined(linux) || defined(__LINUX__) || defined(__linux) - FILE *rf = fopen("/dev/urandom","r"); - if (rf) { - fread(tmp,sizeof(tmp),1,rf); - fclose(rf); - } else { - fprintf(stderr,"FATAL: could not open /dev/urandom\n"); - exit(-1); - } - RAND_seed(tmp,sizeof(tmp)); -#else -#ifdef _WIN32 - error need win32; -#else - error; -#endif -#endif - } - key = EC_KEY_new(); if (!key) return false; diff --git a/node/Identity.hpp b/node/Identity.hpp index a970d7f7..22e60fde 100644 --- a/node/Identity.hpp +++ b/node/Identity.hpp @@ -173,7 +173,7 @@ public: /** * @return True if this identity has its private portion */ - inline bool hasPrivate() const throw() { return (_keyPair); } + inline bool hasPrivate() const throw() { return (_keyPair != (EllipticCurveKeyPair *)0); } /** * Shortcut method to perform key agreement with another identity @@ -356,7 +356,7 @@ public: /** * @return True if this identity contains something */ - inline operator bool() const throw() { return (_publicKey.size()); } + inline operator bool() const throw() { return (_publicKey.size() != 0); } inline bool operator==(const Identity &id) const throw() diff --git a/node/InetAddress.cpp b/node/InetAddress.cpp index 79efbaf2..24ec1f62 100644 --- a/node/InetAddress.cpp +++ b/node/InetAddress.cpp @@ -28,10 +28,9 @@ #include <stdio.h> #include <string.h> #include <stdint.h> -#include <netinet/in.h> -#include <arpa/inet.h> #include <string> +#include "Constants.hpp" #include "InetAddress.hpp" namespace ZeroTier { @@ -62,13 +61,21 @@ std::string InetAddress::toString() const switch(_sa.saddr.sa_family) { case AF_INET: +#ifdef __WINDOWS__ + if (inet_ntop(AF_INET,(PVOID)&(_sa.sin.sin_addr.s_addr),buf,sizeof(buf))) { +#else if (inet_ntop(AF_INET,(const void *)&(_sa.sin.sin_addr.s_addr),buf,sizeof(buf))) { +#endif sprintf(buf2,"%s/%u",buf,(unsigned int)ntohs(_sa.sin.sin_port)); return std::string(buf2); } break; case AF_INET6: +#ifdef __WINDOWS__ + if (inet_ntop(AF_INET6,(PVOID)&(_sa.sin6.sin6_addr.s6_addr),buf,sizeof(buf))) { +#else if (inet_ntop(AF_INET6,(const void *)&(_sa.sin6.sin6_addr.s6_addr),buf,sizeof(buf))) { +#endif sprintf(buf2,"%s/%u",buf,(unsigned int)ntohs(_sa.sin6.sin6_port)); return std::string(buf2); } @@ -97,12 +104,22 @@ std::string InetAddress::toIpString() const switch(_sa.saddr.sa_family) { case AF_INET: +#ifdef __WINDOWS__ + if (inet_ntop(AF_INET,(PVOID)&(_sa.sin.sin_addr.s_addr),buf,sizeof(buf))) + return std::string(buf); +#else if (inet_ntop(AF_INET,(const void *)&(_sa.sin.sin_addr.s_addr),buf,sizeof(buf))) return std::string(buf); +#endif break; case AF_INET6: +#ifdef __WINDOWS__ + if (inet_ntop(AF_INET6,(PVOID)&(_sa.sin6.sin6_addr.s6_addr),buf,sizeof(buf))) + return std::string(buf); +#else if (inet_ntop(AF_INET6,(const void *)&(_sa.sin6.sin6_addr.s6_addr),buf,sizeof(buf))) return std::string(buf); +#endif break; } diff --git a/node/InetAddress.hpp b/node/InetAddress.hpp index 42079274..6f21cc7f 100644 --- a/node/InetAddress.hpp +++ b/node/InetAddress.hpp @@ -30,9 +30,21 @@ #include <stdlib.h> #include <string.h> -#include <netinet/in.h> +#include <stdint.h> + #include <string> +#include "Constants.hpp" + +#ifdef __WINDOWS__ +#include <WinSock2.h> +#include <WS2tcpip.h> +#include <Windows.h> +#else +#include <netinet/in.h> +#include <arpa/inet.h> +#endif + namespace ZeroTier { /** diff --git a/node/Logger.cpp b/node/Logger.cpp index 7bed5990..71b4893d 100644 --- a/node/Logger.cpp +++ b/node/Logger.cpp @@ -30,6 +30,8 @@ #include <string.h> #include <stdarg.h> #include <time.h> + +#include "Constants.hpp" #include "Logger.hpp" namespace ZeroTier { @@ -64,7 +66,12 @@ void Logger::log(const char *fmt,...) if (_log) { time_t now = time(0); +#ifdef __WINDOWS__ + ctime_s(tmp,sizeof(tmp),&now); + char *nowstr = tmp; +#else char *nowstr = ctime_r(&now,tmp); +#endif for(char *c=nowstr;*c;++c) { if (*c == '\n') *c = '\0'; diff --git a/node/Network.cpp b/node/Network.cpp index 3593b732..314c9feb 100644 --- a/node/Network.cpp +++ b/node/Network.cpp @@ -86,9 +86,15 @@ bool Network::Certificate::qualifyMembership(const Network::Certificate &mc) con if (fabs(my - their) > delta) return false; } else { +#ifdef __WINDOWS__ + int64_t my = _strtoi64(myField->second.c_str(),(char **)0,10); + int64_t their = _strtoi64(theirField->second.c_str(),(char **)0,10); + int64_t delta = _strtoi64(deltaField->second.c_str(),(char **)0,10); +#else int64_t my = strtoll(myField->second.c_str(),(char **)0,10); int64_t their = strtoll(theirField->second.c_str(),(char **)0,10); int64_t delta = strtoll(deltaField->second.c_str(),(char **)0,10); +#endif if (my > their) { if ((my - their) > delta) return false; diff --git a/node/Network.hpp b/node/Network.hpp index e80d2e65..f263cd5d 100644 --- a/node/Network.hpp +++ b/node/Network.hpp @@ -115,7 +115,11 @@ public: inline uint64_t networkId() const throw(std::invalid_argument) { +#ifdef __WINDOWS__ + return _strtoui64(get("nwid").c_str(),(char **)0,16); +#else return strtoull(get("nwid").c_str(),(char **)0,16); +#endif } inline void setPeerAddress(Address &a) @@ -222,7 +226,11 @@ public: inline uint64_t networkId() const throw(std::invalid_argument) { +#ifdef __WINDOWS__ + return _strtoui64(get("nwid").c_str(),(char **)0,16); +#else return strtoull(get("nwid").c_str(),(char **)0,16); +#endif } inline Address peerAddress() const diff --git a/node/Node.cpp b/node/Node.cpp index f01f4659..dd6b50a2 100644 --- a/node/Node.cpp +++ b/node/Node.cpp @@ -29,6 +29,8 @@ #include <stdlib.h> #include <string.h> #include <errno.h> +#include <sys/stat.h> + #include <map> #include <set> #include <utility> @@ -37,6 +39,13 @@ #include <vector> #include <string> +#include "Constants.hpp" + +#ifdef __WINDOWS__ +#include <WinSock2.h> +#include <Windows.h> +#endif + #include "Condition.hpp" #include "Node.hpp" #include "Topology.hpp" @@ -46,7 +55,6 @@ #include "Utils.hpp" #include "EthernetTap.hpp" #include "Logger.hpp" -#include "Constants.hpp" #include "InetAddress.hpp" #include "Salsa20.hpp" #include "HMAC.hpp" @@ -68,7 +76,6 @@ #include <unistd.h> #include <signal.h> #include <sys/file.h> -#include <sys/stat.h> #endif #include "../version.h" diff --git a/node/NodeConfig.cpp b/node/NodeConfig.cpp index 427f963b..2cfd0cae 100644 --- a/node/NodeConfig.cpp +++ b/node/NodeConfig.cpp @@ -37,6 +37,13 @@ #include <openssl/sha.h> +#include "Constants.hpp" + +#ifdef __WINDOWS__ +#include <WinSock2.h> +#include <Windows.h> +#endif + #include "NodeConfig.hpp" #include "RuntimeEnvironment.hpp" #include "Defaults.hpp" diff --git a/node/Packet.cpp b/node/Packet.cpp index 94d9164b..e287ae23 100644 --- a/node/Packet.cpp +++ b/node/Packet.cpp @@ -56,7 +56,7 @@ const char *Packet::errorString(ErrorCode e) case ERROR_NONE: return "NONE"; case ERROR_INVALID_REQUEST: return "INVALID_REQUEST"; case ERROR_BAD_PROTOCOL_VERSION: return "BAD_PROTOCOL_VERSION"; - case ERROR_NOT_FOUND: return "NOT_FOUND"; + case ERROR_OBJ_NOT_FOUND: return "OBJECT_NOT_FOUND"; case ERROR_IDENTITY_COLLISION: return "IDENTITY_COLLISION"; case ERROR_IDENTITY_INVALID: return "IDENTITY_INVALID"; case ERROR_UNSUPPORTED_OPERATION: return "UNSUPPORTED_OPERATION"; diff --git a/node/Packet.hpp b/node/Packet.hpp index d4dda497..3ee8fb53 100644 --- a/node/Packet.hpp +++ b/node/Packet.hpp @@ -551,7 +551,7 @@ public: ERROR_BAD_PROTOCOL_VERSION = 2, /* Unknown object queried (e.g. with WHOIS) */ - ERROR_NOT_FOUND = 3, + ERROR_OBJ_NOT_FOUND = 3, /* HELLO pushed an identity whose address is already claimed */ ERROR_IDENTITY_COLLISION = 4, @@ -693,12 +693,12 @@ public: /** * @return True if packet is encrypted */ - inline bool encrypted() const { return (((unsigned char)(*this)[ZT_PACKET_IDX_FLAGS] & ZT_PROTO_FLAG_ENCRYPTED)); } + inline bool encrypted() const { return (((unsigned char)(*this)[ZT_PACKET_IDX_FLAGS] & ZT_PROTO_FLAG_ENCRYPTED) != 0); } /** * @return True if packet is fragmented (expect fragments) */ - inline bool fragmented() const { return (((unsigned char)(*this)[ZT_PACKET_IDX_FLAGS] & ZT_PROTO_FLAG_FRAGMENTED)); } + inline bool fragmented() const { return (((unsigned char)(*this)[ZT_PACKET_IDX_FLAGS] & ZT_PROTO_FLAG_FRAGMENTED) != 0); } /** * Set this packet's fragmented flag @@ -715,7 +715,7 @@ public: /** * @return True if compressed (result only valid if unencrypted) */ - inline bool compressed() const { return (((unsigned char)(*this)[ZT_PACKET_IDX_VERB] & ZT_PROTO_VERB_FLAG_COMPRESSED)); } + inline bool compressed() const { return (((unsigned char)(*this)[ZT_PACKET_IDX_VERB] & ZT_PROTO_VERB_FLAG_COMPRESSED) != 0); } /** * @return ZeroTier forwarding hops (0 to 7) diff --git a/node/PacketDecoder.cpp b/node/PacketDecoder.cpp index 1481711b..14f0d502 100644 --- a/node/PacketDecoder.cpp +++ b/node/PacketDecoder.cpp @@ -369,7 +369,7 @@ bool PacketDecoder::_doWHOIS(const RuntimeEnvironment *_r,const SharedPtr<Peer> Packet outp(source(),_r->identity.address(),Packet::VERB_ERROR); outp.append((unsigned char)Packet::VERB_WHOIS); outp.append(packetId()); - outp.append((unsigned char)Packet::ERROR_NOT_FOUND); + outp.append((unsigned char)Packet::ERROR_OBJ_NOT_FOUND); outp.append(payload(),ZT_ADDRESS_LENGTH); outp.encrypt(peer->cryptKey()); outp.hmacSet(peer->macKey()); @@ -612,11 +612,11 @@ bool PacketDecoder::_doNETWORK_MEMBERSHIP_CERTIFICATE(const RuntimeEnvironment * bool PacketDecoder::_doNETWORK_CONFIG_REQUEST(const RuntimeEnvironment *_r,const SharedPtr<Peer> &peer) { - char tmp[128]; try { uint64_t nwid = at<uint64_t>(ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_NETWORK_ID); #ifndef __WINDOWS__ if (_r->netconfService) { + char tmp[128]; unsigned int dictLen = at<uint16_t>(ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_DICT_LEN); Dictionary request; diff --git a/node/PacketDecoder.hpp b/node/PacketDecoder.hpp index fc5a0c66..1a713ade 100644 --- a/node/PacketDecoder.hpp +++ b/node/PacketDecoder.hpp @@ -97,7 +97,7 @@ private: const RuntimeEnvironment *renv; Address source; InetAddress remoteAddress; - int localPort; + Demarc::Port localPort; unsigned int vMajor,vMinor,vRevision; uint64_t helloPacketId; uint64_t helloTimestamp; diff --git a/node/Peer.hpp b/node/Peer.hpp index 339b9e1a..b9ade9b1 100644 --- a/node/Peer.hpp +++ b/node/Peer.hpp @@ -28,10 +28,12 @@ #ifndef _ZT_PEER_HPP #define _ZT_PEER_HPP +#include <stdint.h> + #include <algorithm> #include <utility> #include <stdexcept> -#include <stdint.h> + #include "Address.hpp" #include "Utils.hpp" #include "Identity.hpp" diff --git a/node/RateLimiter.hpp b/node/RateLimiter.hpp index 4ee0cec7..69d47d0a 100644 --- a/node/RateLimiter.hpp +++ b/node/RateLimiter.hpp @@ -29,8 +29,15 @@ #define _ZT_RATELIMITER_HPP #include <math.h> + +#include "Constants.hpp" #include "Utils.hpp" +#ifdef __WINDOWS__ +#define fmin(a,b) (((a) <= (b)) ? (a) : (b)) +#define fmax(a,b) (((a) >= (b)) ? (a) : (b)) +#endif + namespace ZeroTier { /** diff --git a/node/SharedPtr.hpp b/node/SharedPtr.hpp index 198804a4..834d0a2e 100644 --- a/node/SharedPtr.hpp +++ b/node/SharedPtr.hpp @@ -99,7 +99,7 @@ public: with._ptr = tmp; } - inline operator bool() const throw() { return (_ptr); } + inline operator bool() const throw() { return (_ptr != (T *)0); } inline T &operator*() const throw() { return *_ptr; } inline T *operator->() const throw() { return _ptr; } diff --git a/node/Switch.cpp b/node/Switch.cpp index d5c62994..04984f48 100644 --- a/node/Switch.cpp +++ b/node/Switch.cpp @@ -32,6 +32,13 @@ #include <utility> #include <stdexcept> +#include "Constants.hpp" + +#ifdef __WINDOWS__ +#include <WinSock2.h> +#include <Windows.h> +#endif + #include "Switch.hpp" #include "Node.hpp" #include "EthernetTap.hpp" diff --git a/node/UdpSocket.cpp b/node/UdpSocket.cpp index dfe2b11d..2f59a03c 100644 --- a/node/UdpSocket.cpp +++ b/node/UdpSocket.cpp @@ -33,10 +33,12 @@ #include <sys/types.h> #include <sys/stat.h> -#ifdef _WIN32 -#include <Windows.h> +#include "Constants.hpp" + +#ifdef __WINDOWS__ #include <WinSock2.h> #include <WS2tcpip.h> +#include <Windows.h> #else #include <sys/socket.h> #include <arpa/inet.h> diff --git a/node/Utils.cpp b/node/Utils.cpp index e8561b4c..4f04fb0c 100644 --- a/node/Utils.cpp +++ b/node/Utils.cpp @@ -29,6 +29,7 @@ #include <string.h> #include <stdlib.h> #include <stdarg.h> +#include <sys/stat.h> #include "Constants.hpp" @@ -37,19 +38,13 @@ #include <errno.h> #include <fcntl.h> #include <sys/types.h> -#include <sys/stat.h> #include <sys/uio.h> #include <dirent.h> #endif -#ifdef __WINDOWS__ -#include <Windows.h> -#endif - -#include <sys/stat.h> - #include "Utils.hpp" #include "Mutex.hpp" +#include "Salsa20.hpp" namespace ZeroTier { @@ -390,14 +385,18 @@ unsigned int Utils::unhex(const char *hex,void *buf,unsigned int len) void Utils::getSecureRandom(void *buf,unsigned int bytes) { -#ifdef __UNIX_LIKE__ static Mutex randomLock; static char randbuf[32768]; static unsigned int randptr = sizeof(randbuf); +#ifdef __WINDOWS__ + static Salsa20 s20; + volatile bool s20Initialized = false; +#endif Mutex::Lock _l(randomLock); for(unsigned int i=0;i<bytes;++i) { if (randptr >= sizeof(randbuf)) { +#ifdef __UNIX_LIKE__ int fd = ::open("/dev/urandom",O_RDONLY); if (fd < 0) { fprintf(stderr,"FATAL ERROR: unable to open /dev/urandom: %s"ZT_EOL_S,strerror(errno)); @@ -408,18 +407,32 @@ void Utils::getSecureRandom(void *buf,unsigned int bytes) exit(-1); } ::close(fd); +#else +#ifdef __WINDOWS__ + if (!s20Initialized) { + s20Initialized = true; + char ktmp[32]; + char ivtmp[8]; + for(int i=0;i<32;++i) ktmp[i] = (char)rand(); + for(int i=0;i<8;++i) ivtmp[i] = (char)rand(); + double now = Utils::nowf(); + memcpy(ktmp,&now,sizeof(now)); + DWORD tmp = GetCurrentProcessId(); + memcpy(ktmp + sizeof(double),&tmp,sizeof(tmp)); + tmp = GetTickCount(); + memcpy(ktmp + sizeof(double) + sizeof(DWORD),&tmp,sizeof(tmp)); + s20.init(ktmp,256,ivtmp); + for(int i=0;i<sizeof(randbuf);++i) randbuf[i] = (char)rand(); + } + s20.encrypt(randbuf,randbuf,sizeof(randbuf)); +#else +no getSecureRandom() implementation; +#endif +#endif randptr = 0; } ((char *)buf)[i] = randbuf[randptr++]; } - -#else // !__UNIX_LIKE__ -#ifdef __WINDOWS__ - probably use windows capi...; -#else // !__WINDOWS__ - no getSecureRandom() implementation! -#endif // __WINDOWS__ -#endif // __UNIX_LIKE__ } void Utils::lockDownFile(const char *path,bool isDir) @@ -428,7 +441,7 @@ void Utils::lockDownFile(const char *path,bool isDir) chmod(path,isDir ? 0700 : 0600); #else #ifdef _WIN32 - error need win32; + // TODO: windows ACL hell... #endif #endif } diff --git a/node/Utils.hpp b/node/Utils.hpp index 00564ae6..9d565636 100644 --- a/node/Utils.hpp +++ b/node/Utils.hpp @@ -38,20 +38,20 @@ #include <vector> #include <map> +#include "Constants.hpp" + #include "../ext/lz4/lz4.h" #include "../ext/lz4/lz4hc.h" #ifdef __WINDOWS__ -#include <Windows.h> #include <WinSock2.h> +#include <Windows.h> #else #include <unistd.h> #include <sys/time.h> #include <arpa/inet.h> #endif -#include "Constants.hpp" - /** * Maximum compression/decompression block size (do not change) */ @@ -75,7 +75,7 @@ public: throw() { #ifdef __WINDOWS__ - DeleteFile(path); + return (DeleteFile(path) != FALSE); #else return (unlink(path) == 0); #endif |