summaryrefslogtreecommitdiff
path: root/node
diff options
context:
space:
mode:
authorAdam Ierymenko <adam.ierymenko@zerotier.com>2013-08-12 21:25:36 -0400
committerAdam Ierymenko <adam.ierymenko@zerotier.com>2013-08-12 21:25:36 -0400
commitd6414c9ff74e3f8076a1e94d3c6074366119900e (patch)
treeb0620f1f1610b51b386964d6758b5787b6cb7d6f /node
parent5076c75b079fe02c843ca85dc20821e468b2d7b8 (diff)
downloadinfinitytier-d6414c9ff74e3f8076a1e94d3c6074366119900e.tar.gz
infinitytier-d6414c9ff74e3f8076a1e94d3c6074366119900e.zip
Windows compiles! (w/Visual Studio 2012) That's about all it does, but it's a start.
Diffstat (limited to 'node')
-rw-r--r--node/Address.hpp2
-rw-r--r--node/Array.hpp9
-rw-r--r--node/BloomFilter.hpp2
-rw-r--r--node/Buffer.hpp7
-rw-r--r--node/Constants.hpp17
-rw-r--r--node/Demarc.cpp10
-rw-r--r--node/EllipticCurveKeyPair.cpp30
-rw-r--r--node/Identity.hpp4
-rw-r--r--node/InetAddress.cpp21
-rw-r--r--node/InetAddress.hpp14
-rw-r--r--node/Logger.cpp7
-rw-r--r--node/Network.cpp6
-rw-r--r--node/Network.hpp8
-rw-r--r--node/Node.cpp11
-rw-r--r--node/NodeConfig.cpp7
-rw-r--r--node/Packet.cpp2
-rw-r--r--node/Packet.hpp8
-rw-r--r--node/PacketDecoder.cpp4
-rw-r--r--node/PacketDecoder.hpp2
-rw-r--r--node/Peer.hpp4
-rw-r--r--node/RateLimiter.hpp7
-rw-r--r--node/SharedPtr.hpp2
-rw-r--r--node/Switch.cpp7
-rw-r--r--node/UdpSocket.cpp6
-rw-r--r--node/Utils.cpp47
-rw-r--r--node/Utils.hpp8
26 files changed, 182 insertions, 70 deletions
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