Windows compiles! (w/Visual Studio 2012) That's about all it does, but it's a start.

This commit is contained in:
Adam Ierymenko 2013-08-12 21:25:36 -04:00
parent 5076c75b07
commit d6414c9ff7
30 changed files with 191 additions and 87 deletions

1
.gitignore vendored
View file

@ -11,3 +11,4 @@ mac-tap/tuntap/tap.kext
/ZeroTierOne.v11.suo /ZeroTierOne.v11.suo
/ZeroTierOne.opensdf /ZeroTierOne.opensdf
/Debug_32

View file

@ -46,10 +46,7 @@
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="ext\kissdb\kissdb.c" /> <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\lz4.c" />
<ClCompile Include="ext\lz4\lz4demo.c" />
<ClCompile Include="ext\lz4\lz4hc.c" /> <ClCompile Include="ext\lz4\lz4hc.c" />
<ClCompile Include="node\Defaults.cpp" /> <ClCompile Include="node\Defaults.cpp" />
<ClCompile Include="node\Demarc.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\x509v3.h" />
<ClInclude Include="ext\bin\libcrypto\include\openssl\x509_vfy.h" /> <ClInclude Include="ext\bin\libcrypto\include\openssl\x509_vfy.h" />
<ClInclude Include="ext\kissdb\kissdb.h" /> <ClInclude Include="ext\kissdb\kissdb.h" />
<ClInclude Include="ext\lz4\bench.h" />
<ClInclude Include="ext\lz4\lz4.h" /> <ClInclude Include="ext\lz4\lz4.h" />
<ClInclude Include="ext\lz4\lz4hc.h" /> <ClInclude Include="ext\lz4\lz4hc.h" />
<ClInclude Include="node\Address.hpp" /> <ClInclude Include="node\Address.hpp" />

View file

@ -18,18 +18,9 @@
<ClCompile Include="ext\kissdb\kissdb.c"> <ClCompile Include="ext\kissdb\kissdb.c">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </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"> <ClCompile Include="ext\lz4\lz4.c">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="ext\lz4\lz4demo.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="ext\lz4\lz4hc.c"> <ClCompile Include="ext\lz4\lz4hc.c">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
@ -332,9 +323,6 @@
<ClInclude Include="ext\kissdb\kissdb.h"> <ClInclude Include="ext\kissdb\kissdb.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="ext\lz4\bench.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="ext\lz4\lz4.h"> <ClInclude Include="ext\lz4\lz4.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>

View file

@ -15,7 +15,12 @@
#include <string.h> #include <string.h>
#include <stdlib.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) #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 #ifdef KISSDB_TEST
#include <inttypes.h>
int main(int argc,char **argv) int main(int argc,char **argv)
{ {
uint64_t i,j; uint64_t i,j;

View file

@ -205,7 +205,7 @@ public:
/** /**
* @return True if this address is not zero * @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 * @return Sum of all bytes in address

View file

@ -94,7 +94,14 @@ public:
inline reference back() throw() { return data[S-1]; } inline reference back() throw() { return data[S-1]; }
inline const_reference back() const 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 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 !(*this == k); }
inline bool operator>(const Array &k) const throw() { return (k < *this); } inline bool operator>(const Array &k) const throw() { return (k < *this); }

View file

@ -110,7 +110,7 @@ public:
throw() throw()
{ {
n %= B; n %= B;
return (_field[n / 8] & (1 << (n % 8))); return ((_field[n / 8] & (1 << (n % 8))) != 0);
} }
/** /**

View file

@ -28,12 +28,15 @@
#ifndef _ZT_BUFFER_HPP #ifndef _ZT_BUFFER_HPP
#define _ZT_BUFFER_HPP #define _ZT_BUFFER_HPP
#include <string.h>
#include <stdint.h>
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include <algorithm> #include <algorithm>
#include <utility> #include <utility>
#include <string.h>
#include <stdint.h> #include "Constants.hpp"
#include "Utils.hpp" #include "Utils.hpp"
#ifdef __GNUC__ #ifdef __GNUC__

View file

@ -68,6 +68,9 @@
#ifndef __WINDOWS__ #ifndef __WINDOWS__
#define __WINDOWS__ #define __WINDOWS__
#endif #endif
#define NOMINMAX
#pragma warning(disable : 4290)
#pragma warning(disable : 4996)
#undef __UNIX_LIKE__ #undef __UNIX_LIKE__
#define ZT_PATH_SEPARATOR '\\' #define ZT_PATH_SEPARATOR '\\'
#define ZT_PATH_SEPARATOR_S "\\" #define ZT_PATH_SEPARATOR_S "\\"
@ -96,11 +99,23 @@
error_no_byte_order_defined; error_no_byte_order_defined;
#endif #endif
#ifndef ZT_OSNAME #ifndef ZT_OSNAME
error_no_ZT_OSNAME_defined; #ifdef __WINDOWS__
#define ZT_OSNAME "windows"
#else
no ZT_OSNAME defined;
#endif
#endif #endif
#ifndef ZT_ARCH #ifndef ZT_ARCH
#ifdef __WINDOWS__
#ifdef _WIN64
#define ZT_ARCH "x64"
#else
#define ZT_ARCH "x86"
#endif
#else
error_no_ZT_ARCH_defined; error_no_ZT_ARCH_defined;
#endif #endif
#endif
/** /**
* Length of a ZeroTier address in bytes * Length of a ZeroTier address in bytes

View file

@ -26,6 +26,14 @@
*/ */
#include <vector> #include <vector>
#include "Constants.hpp"
#ifdef __WINDOWS__
#include <WinSock2.h>
#include <Windows.h>
#endif
#include "Demarc.hpp" #include "Demarc.hpp"
#include "RuntimeEnvironment.hpp" #include "RuntimeEnvironment.hpp"
#include "Logger.hpp" #include "Logger.hpp"
@ -82,7 +90,7 @@ bool Demarc::has(Port p) const
throw() throw()
{ {
Mutex::Lock _l(_ports_m); Mutex::Lock _l(_ports_m);
return (_ports.count(p)); return (_ports.count(p) != 0);
} }
bool Demarc::bindLocalUdp(unsigned int localPort) bool Demarc::bindLocalUdp(unsigned int localPort)

View file

@ -29,6 +29,13 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "Constants.hpp"
#ifdef __WINDOWS__
#include <WinSock2.h>
#include <Windows.h>
#endif
#include <openssl/bn.h> #include <openssl/bn.h>
#include <openssl/obj_mac.h> #include <openssl/obj_mac.h>
#include <openssl/rand.h> #include <openssl/rand.h>
@ -129,32 +136,9 @@ const EllipticCurveKeyPair &EllipticCurveKeyPair::operator=(const EllipticCurveK
bool EllipticCurveKeyPair::generate() bool EllipticCurveKeyPair::generate()
{ {
unsigned char tmp[16384];
EC_KEY *key; EC_KEY *key;
int len; 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(); key = EC_KEY_new();
if (!key) return false; if (!key) return false;

View file

@ -173,7 +173,7 @@ public:
/** /**
* @return True if this identity has its private portion * @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 * Shortcut method to perform key agreement with another identity
@ -356,7 +356,7 @@ public:
/** /**
* @return True if this identity contains something * @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 inline bool operator==(const Identity &id) const
throw() throw()

View file

@ -28,10 +28,9 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdint.h> #include <stdint.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string> #include <string>
#include "Constants.hpp"
#include "InetAddress.hpp" #include "InetAddress.hpp"
namespace ZeroTier { namespace ZeroTier {
@ -62,13 +61,21 @@ std::string InetAddress::toString() const
switch(_sa.saddr.sa_family) { switch(_sa.saddr.sa_family) {
case AF_INET: 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))) { 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)); sprintf(buf2,"%s/%u",buf,(unsigned int)ntohs(_sa.sin.sin_port));
return std::string(buf2); return std::string(buf2);
} }
break; break;
case AF_INET6: 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))) { 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)); sprintf(buf2,"%s/%u",buf,(unsigned int)ntohs(_sa.sin6.sin6_port));
return std::string(buf2); return std::string(buf2);
} }
@ -97,12 +104,22 @@ std::string InetAddress::toIpString() const
switch(_sa.saddr.sa_family) { switch(_sa.saddr.sa_family) {
case AF_INET: 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))) if (inet_ntop(AF_INET,(const void *)&(_sa.sin.sin_addr.s_addr),buf,sizeof(buf)))
return std::string(buf); return std::string(buf);
#endif
break; break;
case AF_INET6: 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))) if (inet_ntop(AF_INET6,(const void *)&(_sa.sin6.sin6_addr.s6_addr),buf,sizeof(buf)))
return std::string(buf); return std::string(buf);
#endif
break; break;
} }

View file

@ -30,9 +30,21 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <netinet/in.h> #include <stdint.h>
#include <string> #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 { namespace ZeroTier {
/** /**

View file

@ -30,6 +30,8 @@
#include <string.h> #include <string.h>
#include <stdarg.h> #include <stdarg.h>
#include <time.h> #include <time.h>
#include "Constants.hpp"
#include "Logger.hpp" #include "Logger.hpp"
namespace ZeroTier { namespace ZeroTier {
@ -64,7 +66,12 @@ void Logger::log(const char *fmt,...)
if (_log) { if (_log) {
time_t now = time(0); time_t now = time(0);
#ifdef __WINDOWS__
ctime_s(tmp,sizeof(tmp),&now);
char *nowstr = tmp;
#else
char *nowstr = ctime_r(&now,tmp); char *nowstr = ctime_r(&now,tmp);
#endif
for(char *c=nowstr;*c;++c) { for(char *c=nowstr;*c;++c) {
if (*c == '\n') if (*c == '\n')
*c = '\0'; *c = '\0';

View file

@ -86,9 +86,15 @@ bool Network::Certificate::qualifyMembership(const Network::Certificate &mc) con
if (fabs(my - their) > delta) if (fabs(my - their) > delta)
return false; return false;
} else { } 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 my = strtoll(myField->second.c_str(),(char **)0,10);
int64_t their = strtoll(theirField->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); int64_t delta = strtoll(deltaField->second.c_str(),(char **)0,10);
#endif
if (my > their) { if (my > their) {
if ((my - their) > delta) if ((my - their) > delta)
return false; return false;

View file

@ -115,7 +115,11 @@ public:
inline uint64_t networkId() const inline uint64_t networkId() const
throw(std::invalid_argument) 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); return strtoull(get("nwid").c_str(),(char **)0,16);
#endif
} }
inline void setPeerAddress(Address &a) inline void setPeerAddress(Address &a)
@ -222,7 +226,11 @@ public:
inline uint64_t networkId() const inline uint64_t networkId() const
throw(std::invalid_argument) 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); return strtoull(get("nwid").c_str(),(char **)0,16);
#endif
} }
inline Address peerAddress() const inline Address peerAddress() const

View file

@ -29,6 +29,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <sys/stat.h>
#include <map> #include <map>
#include <set> #include <set>
#include <utility> #include <utility>
@ -37,6 +39,13 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include "Constants.hpp"
#ifdef __WINDOWS__
#include <WinSock2.h>
#include <Windows.h>
#endif
#include "Condition.hpp" #include "Condition.hpp"
#include "Node.hpp" #include "Node.hpp"
#include "Topology.hpp" #include "Topology.hpp"
@ -46,7 +55,6 @@
#include "Utils.hpp" #include "Utils.hpp"
#include "EthernetTap.hpp" #include "EthernetTap.hpp"
#include "Logger.hpp" #include "Logger.hpp"
#include "Constants.hpp"
#include "InetAddress.hpp" #include "InetAddress.hpp"
#include "Salsa20.hpp" #include "Salsa20.hpp"
#include "HMAC.hpp" #include "HMAC.hpp"
@ -68,7 +76,6 @@
#include <unistd.h> #include <unistd.h>
#include <signal.h> #include <signal.h>
#include <sys/file.h> #include <sys/file.h>
#include <sys/stat.h>
#endif #endif
#include "../version.h" #include "../version.h"

View file

@ -37,6 +37,13 @@
#include <openssl/sha.h> #include <openssl/sha.h>
#include "Constants.hpp"
#ifdef __WINDOWS__
#include <WinSock2.h>
#include <Windows.h>
#endif
#include "NodeConfig.hpp" #include "NodeConfig.hpp"
#include "RuntimeEnvironment.hpp" #include "RuntimeEnvironment.hpp"
#include "Defaults.hpp" #include "Defaults.hpp"

View file

@ -56,7 +56,7 @@ const char *Packet::errorString(ErrorCode e)
case ERROR_NONE: return "NONE"; case ERROR_NONE: return "NONE";
case ERROR_INVALID_REQUEST: return "INVALID_REQUEST"; case ERROR_INVALID_REQUEST: return "INVALID_REQUEST";
case ERROR_BAD_PROTOCOL_VERSION: return "BAD_PROTOCOL_VERSION"; 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_COLLISION: return "IDENTITY_COLLISION";
case ERROR_IDENTITY_INVALID: return "IDENTITY_INVALID"; case ERROR_IDENTITY_INVALID: return "IDENTITY_INVALID";
case ERROR_UNSUPPORTED_OPERATION: return "UNSUPPORTED_OPERATION"; case ERROR_UNSUPPORTED_OPERATION: return "UNSUPPORTED_OPERATION";

View file

@ -551,7 +551,7 @@ public:
ERROR_BAD_PROTOCOL_VERSION = 2, ERROR_BAD_PROTOCOL_VERSION = 2,
/* Unknown object queried (e.g. with WHOIS) */ /* 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 */ /* HELLO pushed an identity whose address is already claimed */
ERROR_IDENTITY_COLLISION = 4, ERROR_IDENTITY_COLLISION = 4,
@ -693,12 +693,12 @@ public:
/** /**
* @return True if packet is encrypted * @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) * @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 * Set this packet's fragmented flag
@ -715,7 +715,7 @@ public:
/** /**
* @return True if compressed (result only valid if unencrypted) * @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) * @return ZeroTier forwarding hops (0 to 7)

View file

@ -369,7 +369,7 @@ bool PacketDecoder::_doWHOIS(const RuntimeEnvironment *_r,const SharedPtr<Peer>
Packet outp(source(),_r->identity.address(),Packet::VERB_ERROR); Packet outp(source(),_r->identity.address(),Packet::VERB_ERROR);
outp.append((unsigned char)Packet::VERB_WHOIS); outp.append((unsigned char)Packet::VERB_WHOIS);
outp.append(packetId()); 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.append(payload(),ZT_ADDRESS_LENGTH);
outp.encrypt(peer->cryptKey()); outp.encrypt(peer->cryptKey());
outp.hmacSet(peer->macKey()); 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) bool PacketDecoder::_doNETWORK_CONFIG_REQUEST(const RuntimeEnvironment *_r,const SharedPtr<Peer> &peer)
{ {
char tmp[128];
try { try {
uint64_t nwid = at<uint64_t>(ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_NETWORK_ID); uint64_t nwid = at<uint64_t>(ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_NETWORK_ID);
#ifndef __WINDOWS__ #ifndef __WINDOWS__
if (_r->netconfService) { if (_r->netconfService) {
char tmp[128];
unsigned int dictLen = at<uint16_t>(ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_DICT_LEN); unsigned int dictLen = at<uint16_t>(ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_DICT_LEN);
Dictionary request; Dictionary request;

View file

@ -97,7 +97,7 @@ private:
const RuntimeEnvironment *renv; const RuntimeEnvironment *renv;
Address source; Address source;
InetAddress remoteAddress; InetAddress remoteAddress;
int localPort; Demarc::Port localPort;
unsigned int vMajor,vMinor,vRevision; unsigned int vMajor,vMinor,vRevision;
uint64_t helloPacketId; uint64_t helloPacketId;
uint64_t helloTimestamp; uint64_t helloTimestamp;

View file

@ -28,10 +28,12 @@
#ifndef _ZT_PEER_HPP #ifndef _ZT_PEER_HPP
#define _ZT_PEER_HPP #define _ZT_PEER_HPP
#include <stdint.h>
#include <algorithm> #include <algorithm>
#include <utility> #include <utility>
#include <stdexcept> #include <stdexcept>
#include <stdint.h>
#include "Address.hpp" #include "Address.hpp"
#include "Utils.hpp" #include "Utils.hpp"
#include "Identity.hpp" #include "Identity.hpp"

View file

@ -29,8 +29,15 @@
#define _ZT_RATELIMITER_HPP #define _ZT_RATELIMITER_HPP
#include <math.h> #include <math.h>
#include "Constants.hpp"
#include "Utils.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 { namespace ZeroTier {
/** /**

View file

@ -99,7 +99,7 @@ public:
with._ptr = tmp; 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; }
inline T *operator->() const throw() { return _ptr; } inline T *operator->() const throw() { return _ptr; }

View file

@ -32,6 +32,13 @@
#include <utility> #include <utility>
#include <stdexcept> #include <stdexcept>
#include "Constants.hpp"
#ifdef __WINDOWS__
#include <WinSock2.h>
#include <Windows.h>
#endif
#include "Switch.hpp" #include "Switch.hpp"
#include "Node.hpp" #include "Node.hpp"
#include "EthernetTap.hpp" #include "EthernetTap.hpp"

View file

@ -33,10 +33,12 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#ifdef _WIN32 #include "Constants.hpp"
#include <Windows.h>
#ifdef __WINDOWS__
#include <WinSock2.h> #include <WinSock2.h>
#include <WS2tcpip.h> #include <WS2tcpip.h>
#include <Windows.h>
#else #else
#include <sys/socket.h> #include <sys/socket.h>
#include <arpa/inet.h> #include <arpa/inet.h>

View file

@ -29,6 +29,7 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h> #include <stdarg.h>
#include <sys/stat.h>
#include "Constants.hpp" #include "Constants.hpp"
@ -37,19 +38,13 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h>
#include <sys/uio.h> #include <sys/uio.h>
#include <dirent.h> #include <dirent.h>
#endif #endif
#ifdef __WINDOWS__
#include <Windows.h>
#endif
#include <sys/stat.h>
#include "Utils.hpp" #include "Utils.hpp"
#include "Mutex.hpp" #include "Mutex.hpp"
#include "Salsa20.hpp"
namespace ZeroTier { 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) void Utils::getSecureRandom(void *buf,unsigned int bytes)
{ {
#ifdef __UNIX_LIKE__
static Mutex randomLock; static Mutex randomLock;
static char randbuf[32768]; static char randbuf[32768];
static unsigned int randptr = sizeof(randbuf); static unsigned int randptr = sizeof(randbuf);
#ifdef __WINDOWS__
static Salsa20 s20;
volatile bool s20Initialized = false;
#endif
Mutex::Lock _l(randomLock); Mutex::Lock _l(randomLock);
for(unsigned int i=0;i<bytes;++i) { for(unsigned int i=0;i<bytes;++i) {
if (randptr >= sizeof(randbuf)) { if (randptr >= sizeof(randbuf)) {
#ifdef __UNIX_LIKE__
int fd = ::open("/dev/urandom",O_RDONLY); int fd = ::open("/dev/urandom",O_RDONLY);
if (fd < 0) { if (fd < 0) {
fprintf(stderr,"FATAL ERROR: unable to open /dev/urandom: %s"ZT_EOL_S,strerror(errno)); 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); exit(-1);
} }
::close(fd); ::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; randptr = 0;
} }
((char *)buf)[i] = randbuf[randptr++]; ((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) 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); chmod(path,isDir ? 0700 : 0600);
#else #else
#ifdef _WIN32 #ifdef _WIN32
error need win32; // TODO: windows ACL hell...
#endif #endif
#endif #endif
} }

View file

@ -38,20 +38,20 @@
#include <vector> #include <vector>
#include <map> #include <map>
#include "Constants.hpp"
#include "../ext/lz4/lz4.h" #include "../ext/lz4/lz4.h"
#include "../ext/lz4/lz4hc.h" #include "../ext/lz4/lz4hc.h"
#ifdef __WINDOWS__ #ifdef __WINDOWS__
#include <Windows.h>
#include <WinSock2.h> #include <WinSock2.h>
#include <Windows.h>
#else #else
#include <unistd.h> #include <unistd.h>
#include <sys/time.h> #include <sys/time.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#endif #endif
#include "Constants.hpp"
/** /**
* Maximum compression/decompression block size (do not change) * Maximum compression/decompression block size (do not change)
*/ */
@ -75,7 +75,7 @@ public:
throw() throw()
{ {
#ifdef __WINDOWS__ #ifdef __WINDOWS__
DeleteFile(path); return (DeleteFile(path) != FALSE);
#else #else
return (unlink(path) == 0); return (unlink(path) == 0);
#endif #endif