Compile fixes.

This commit is contained in:
Adam Ierymenko 2014-03-17 15:14:22 -07:00
parent 7e80d7e551
commit bd749e040d
7 changed files with 109 additions and 103 deletions

View file

@ -39,7 +39,6 @@
#include "Utils.hpp" #include "Utils.hpp"
#include "Identity.hpp" #include "Identity.hpp"
#include "Logger.hpp" #include "Logger.hpp"
#include "Demarc.hpp"
#include "RuntimeEnvironment.hpp" #include "RuntimeEnvironment.hpp"
#include "InetAddress.hpp" #include "InetAddress.hpp"
#include "Packet.hpp" #include "Packet.hpp"
@ -49,7 +48,7 @@
#include "Mutex.hpp" #include "Mutex.hpp"
// Increment if serialization has changed // Increment if serialization has changed
#define ZT_PEER_SERIALIZATION_VERSION 6 #define ZT_PEER_SERIALIZATION_VERSION 7
namespace ZeroTier { namespace ZeroTier {
@ -428,7 +427,6 @@ private:
lastSend(0), lastSend(0),
lastReceive(0), lastReceive(0),
lastFirewallOpener(0), lastFirewallOpener(0),
localPort(Demarc::ANY_PORT),
addr(), addr(),
fixed(false) fixed(false)
{ {
@ -447,7 +445,6 @@ private:
b.append(lastSend); b.append(lastSend);
b.append(lastReceive); b.append(lastReceive);
b.append(lastFirewallOpener); b.append(lastFirewallOpener);
b.append(Demarc::portToInt(localPort));
b.append((unsigned char)addr.type()); b.append((unsigned char)addr.type());
switch(addr.type()) { switch(addr.type()) {
@ -475,7 +472,6 @@ private:
lastSend = b.template at<uint64_t>(p); p += sizeof(uint64_t); lastSend = b.template at<uint64_t>(p); p += sizeof(uint64_t);
lastReceive = b.template at<uint64_t>(p); p += sizeof(uint64_t); lastReceive = b.template at<uint64_t>(p); p += sizeof(uint64_t);
lastFirewallOpener = b.template at<uint64_t>(p); p += sizeof(uint64_t); lastFirewallOpener = b.template at<uint64_t>(p); p += sizeof(uint64_t);
localPort = Demarc::intToPort(b.template at<uint64_t>(p)); p += sizeof(uint64_t);
switch ((InetAddress::AddressType)b[p++]) { switch ((InetAddress::AddressType)b[p++]) {
case InetAddress::TYPE_NULL: case InetAddress::TYPE_NULL:
@ -499,7 +495,6 @@ private:
uint64_t lastSend; uint64_t lastSend;
uint64_t lastReceive; uint64_t lastReceive;
uint64_t lastFirewallOpener; uint64_t lastFirewallOpener;
Demarc::Port localPort; // ANY_PORT if not defined (size: uint64_t)
InetAddress addr; // null InetAddress if path is undefined InetAddress addr; // null InetAddress if path is undefined
bool fixed; // do not learn address from received packets bool fixed; // do not learn address from received packets
}; };

View file

@ -28,8 +28,6 @@
#ifndef ZT_SOCKET_HPP #ifndef ZT_SOCKET_HPP
#define ZT_SOCKET_HPP #define ZT_SOCKET_HPP
#include <list>
#include "Constants.hpp" #include "Constants.hpp"
#include "InetAddress.hpp" #include "InetAddress.hpp"
#include "AtomicCounter.hpp" #include "AtomicCounter.hpp"
@ -49,6 +47,7 @@
namespace ZeroTier { namespace ZeroTier {
class Socket;
class SocketManager; class SocketManager;
/** /**
@ -58,8 +57,8 @@ class SocketManager;
*/ */
class Socket : NonCopyable class Socket : NonCopyable
{ {
friend class SharedPtr<Socket>;
friend class SocketManager; friend class SocketManager;
friend class SharedPtr<Socket>;
public: public:
enum Type enum Type
@ -80,20 +79,6 @@ public:
return _type; return _type;
} }
/**
* @return True if this is a TCP socket
*/
inline bool isTCP() const
throw()
{
return (_type == ZT_SOCKET_TYPE_TCP);
}
/**
* @return True if socket is available for sending/receiving of data
*/
virtual bool isOpen() const = 0;
/** /**
* Send a ZeroTier message packet * Send a ZeroTier message packet
* *
@ -105,25 +90,19 @@ public:
virtual bool send(const InetAddress &to,const void *msg,unsigned int msglen) = 0; virtual bool send(const InetAddress &to,const void *msg,unsigned int msglen) = 0;
protected: protected:
#ifdef __WINDOWS__
Socket(Type t,SOCKET s) :
#else
Socket(Type t,int s) :
#endif
_sock(s),
_type(t) {}
// Called only by SocketManager, should return false if socket is no longer open/valid (e.g. connection drop or other fatal error) // Called only by SocketManager, should return false if socket is no longer open/valid (e.g. connection drop or other fatal error)
virtual bool notifyAvailableForRead(const SharedPtr<Socket> &self,SocketManager *sm) = 0; virtual bool notifyAvailableForRead(const SharedPtr<Socket> &self,SocketManager *sm) = 0;
virtual bool notifyAvailableForWrite(const SharedPtr<Socket> &self,SocketManager *sm) = 0; virtual bool notifyAvailableForWrite(const SharedPtr<Socket> &self,SocketManager *sm) = 0;
private: private:
#ifdef __WINDOWS__
Socket(Type t,SOCKET sock) :
_sock(sock),
_type(t)
{
}
#else
Socket(Type t,int sock) :
_sock(sock),
_type(t)
{
}
#endif
#ifdef __WINDOWS__ #ifdef __WINDOWS__
SOCKET _sock; SOCKET _sock;
#else #else
@ -134,6 +113,6 @@ private:
AtomicCounter __refCount; AtomicCounter __refCount;
}; };
}; // namespace ZeroTier } // namespace ZeroTier
#endif #endif

View file

@ -33,6 +33,8 @@
#include <sys/types.h> #include <sys/types.h>
#include "SocketManager.hpp" #include "SocketManager.hpp"
#include "UdpSocket.hpp"
#include "TcpSocket.hpp"
#ifndef __WINDOWS__ #ifndef __WINDOWS__
#include <unistd.h> #include <unistd.h>
@ -73,7 +75,11 @@ static inline void __winpipe(SOCKET fds[2])
} }
#endif #endif
SocketManager::SocketManager(int localUdpPort,int localTcpPort,void (*packetHandler)(const SharedPtr<Socket> &,void *,const InetAddress &,const void *,unsigned int),void *arg) : SocketManager::SocketManager(
int localUdpPort,
int localTcpPort,
void (*packetHandler)(const SharedPtr<Socket> &,void *,const InetAddress &,Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> &),
void *arg) :
_whackSendPipe(INVALID_SOCKET), _whackSendPipe(INVALID_SOCKET),
_whackReceivePipe(INVALID_SOCKET), _whackReceivePipe(INVALID_SOCKET),
_tcpV4ListenSocket(INVALID_SOCKET), _tcpV4ListenSocket(INVALID_SOCKET),
@ -95,7 +101,7 @@ SocketManager::SocketManager(int localUdpPort,int localTcpPort,void (*packetHand
#else #else
{ {
int tmpfds[2]; int tmpfds[2];
if (::pipe(tmpfds,0)) if (::pipe(tmpfds))
throw std::runtime_error("pipe() failed"); throw std::runtime_error("pipe() failed");
_whackSendPipe = tmpfds[1]; _whackSendPipe = tmpfds[1];
_whackReceivePipe = tmpfds[0]; _whackReceivePipe = tmpfds[0];
@ -278,16 +284,16 @@ SocketManager::SocketManager(int localUdpPort,int localTcpPort,void (*packetHand
{ {
#ifdef __WINDOWS__ #ifdef __WINDOWS__
BOOL f; BOOL f;
f = FALSE; setsockopt(_sock,SOL_SOCKET,SO_REUSEADDR,(const char *)&f,sizeof(f)); f = FALSE; setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(const char *)&f,sizeof(f));
f = FALSE; setsockopt(_sock,IPPROTO_IP,IP_DONTFRAGMENT,(const char *)&f,sizeof(f)); f = FALSE; setsockopt(s,IPPROTO_IP,IP_DONTFRAGMENT,(const char *)&f,sizeof(f));
#else #else
int f; int f;
f = 0; setsockopt(_sock,SOL_SOCKET,SO_REUSEADDR,(void *)&f,sizeof(f)); f = 0; setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(void *)&f,sizeof(f));
#ifdef IP_DONTFRAG #ifdef IP_DONTFRAG
f = 0; setsockopt(_sock,IPPROTO_IP,IP_DONTFRAG,&f,sizeof(f)); f = 0; setsockopt(s,IPPROTO_IP,IP_DONTFRAG,&f,sizeof(f));
#endif #endif
#ifdef IP_MTU_DISCOVER #ifdef IP_MTU_DISCOVER
f = 0; setsockopt(_sock,IPPROTO_IP,IP_MTU_DISCOVER,&f,sizeof(f)); f = 0; setsockopt(s,IPPROTO_IP,IP_MTU_DISCOVER,&f,sizeof(f));
#endif #endif
#endif #endif
} }
@ -335,10 +341,10 @@ bool SocketManager::sendFirewallOpener(const InetAddress &to,int hopLimit)
{ {
if (to.isV4()) { if (to.isV4()) {
if (_udpV4Socket) if (_udpV4Socket)
return _udpV4Socket->sendWithHopLimit(to,msg,msglen,hopLimit); return ((UdpSocket *)_udpV4Socket.ptr())->sendWithHopLimit(to,"",1,hopLimit);
} else if (to.isV6()) { } else if (to.isV6()) {
if (_udpV6Socket) if (_udpV6Socket)
return _udpV6Socket->sendWithHopLimit(to,msg,msglen,hopLimit); return ((UdpSocket *)_udpV6Socket.ptr())->sendWithHopLimit(to,"",1,hopLimit);
} }
return false; return false;
} }
@ -347,6 +353,11 @@ void SocketManager::poll(unsigned long timeout)
{ {
fd_set rfds,wfds,nfds; fd_set rfds,wfds,nfds;
struct timeval tv; struct timeval tv;
#ifdef __WINDOWS__
SOCKET sockfd;
#else
int sockfd;
#endif
Mutex::Lock _l(_pollLock); Mutex::Lock _l(_pollLock);
@ -370,8 +381,38 @@ void SocketManager::poll(unsigned long timeout)
} }
if ((_tcpV4ListenSocket != INVALID_SOCKET)&&(FD_ISSET(_tcpV4ListenSocket,&rfds))) { if ((_tcpV4ListenSocket != INVALID_SOCKET)&&(FD_ISSET(_tcpV4ListenSocket,&rfds))) {
struct sockaddr_in from;
socklen_t fromlen = sizeof(from);
sockfd = accept(_tcpV4ListenSocket,(struct sockaddr *)&from,&fromlen);
#ifdef __WINDOWS__
if (sockfd != INVALID_SOCKET) {
#else
if (sockfd > 0) {
#endif
InetAddress fromia((const struct sockaddr *)&from);
Mutex::Lock _l2(_tcpSockets_m);
_tcpSockets[fromia] = SharedPtr<Socket>(new TcpSocket(sockfd,false,fromia));
_fdSetLock.lock();
FD_SET(sockfd,&_readfds);
_fdSetLock.unlock();
}
} }
if ((_tcpV6ListenSocket != INVALID_SOCKET)&&(FD_ISSET(_tcpV6ListenSocket,&rfds))) { if ((_tcpV6ListenSocket != INVALID_SOCKET)&&(FD_ISSET(_tcpV6ListenSocket,&rfds))) {
struct sockaddr_in6 from;
socklen_t fromlen = sizeof(from);
sockfd = accept(_tcpV6ListenSocket,(struct sockaddr *)&from,&fromlen);
#ifdef __WINDOWS__
if (sockfd != INVALID_SOCKET) {
#else
if (sockfd > 0) {
#endif
InetAddress fromia((const struct sockaddr *)&from);
Mutex::Lock _l2(_tcpSockets_m);
_tcpSockets[fromia] = SharedPtr<Socket>(new TcpSocket(sockfd,false,fromia));
_fdSetLock.lock();
FD_SET(sockfd,&_readfds);
_fdSetLock.unlock();
}
} }
if ((_udpV4Socket)&&(FD_ISSET(_udpV4Socket->_sock,&rfds))) if ((_udpV4Socket)&&(FD_ISSET(_udpV4Socket->_sock,&rfds)))
@ -384,15 +425,19 @@ void SocketManager::poll(unsigned long timeout)
Mutex::Lock _l2(_tcpSockets_m); Mutex::Lock _l2(_tcpSockets_m);
if (_tcpSockets.size()) { if (_tcpSockets.size()) {
ts.reserve(_tcpSockets.size()); ts.reserve(_tcpSockets.size());
for(std::map< InetAddress,SharedPtr<Socket> >::iterator s(_tcpSockets.begin());s!=_tcpSockets.end();++s) for(std::map< InetAddress,SharedPtr<Socket> >::iterator s(_tcpSockets.begin());s!=_tcpSockets.end();) {
ts.push_back(s->second); if (true) { // TODO: TCP expiration check
ts.push_back(s->second);
++s;
} else _tcpSockets.erase(s++);
}
} }
} }
for(std::vector< SharedPtr<Socket> >::iterator s(ts.begin());s!=ts.end();++s) { for(std::vector< SharedPtr<Socket> >::iterator s(ts.begin());s!=ts.end();++s) {
if (FD_ISSET((*s)->_sock,&rfds)) if (FD_ISSET((*s)->_sock,&rfds))
s->notifyAvailableForRead(*s,this); (*s)->notifyAvailableForRead(*s,this);
if (FD_ISSET((*s)->_sock,&wfds)) if (FD_ISSET((*s)->_sock,&wfds))
s->notifyAvailableForWrite(*s,this); (*s)->notifyAvailableForWrite(*s,this);
} }
} }

View file

@ -48,8 +48,6 @@
#include "SharedPtr.hpp" #include "SharedPtr.hpp"
#include "InetAddress.hpp" #include "InetAddress.hpp"
#include "Socket.hpp" #include "Socket.hpp"
#include "TcpSocket.hpp"
#include "UdpSocket.hpp"
#include "Mutex.hpp" #include "Mutex.hpp"
#include "NonCopyable.hpp" #include "NonCopyable.hpp"
#include "Buffer.hpp" #include "Buffer.hpp"
@ -75,7 +73,11 @@ public:
* @param arg Second argument to packetHandler() * @param arg Second argument to packetHandler()
* @throws std::runtime_error Could not bind local port(s) or open socket(s) * @throws std::runtime_error Could not bind local port(s) or open socket(s)
*/ */
SocketManager(int localUdpPort,int localTcpPort,void (*packetHandler)(const SharedPtr<Socket> &,void *,const InetAddress &,Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> &),void *arg); SocketManager(
int localUdpPort,
int localTcpPort,
void (*packetHandler)(const SharedPtr<Socket> &,void *,const InetAddress &,Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> &),
void *arg);
~SocketManager(); ~SocketManager();
@ -169,19 +171,15 @@ private:
#ifdef __WINDOWS__ #ifdef __WINDOWS__
SOCKET _whackSendPipe; SOCKET _whackSendPipe;
SOCKET _whackReceivePipe; SOCKET _whackReceivePipe;
#else
int _whackSendPipe;
int _whackReceivePipe;
#endif
Mutex::Lock _whackSendPipe_m;
#ifdef __WINDOWS__
SOCKET _tcpV4ListenSocket; SOCKET _tcpV4ListenSocket;
SOCKET _tcpV6ListenSocket; SOCKET _tcpV6ListenSocket;
#else #else
int _whackSendPipe;
int _whackReceivePipe;
int _tcpV4ListenSocket; int _tcpV4ListenSocket;
int _tcpV6ListenSocket; int _tcpV6ListenSocket;
#endif #endif
Mutex _whackSendPipe_m;
SharedPtr<Socket> _udpV4Socket; SharedPtr<Socket> _udpV4Socket;
SharedPtr<Socket> _udpV6Socket; SharedPtr<Socket> _udpV6Socket;

View file

@ -30,17 +30,18 @@
#include <stdint.h> #include <stdint.h>
#include "Socket.hpp" #include "InetAddress.hpp"
#include "Mutex.hpp" #include "Mutex.hpp"
#include "Utils.hpp" #include "Utils.hpp"
#include "Socket.hpp"
#define ZT_TCP_SENDQ_LENGTH 4096
#define ZT_TCP_MAX_MESSAGE_LENGTH 2048
namespace ZeroTier { namespace ZeroTier {
class SocketManager; class SocketManager;
#define ZT_TCP_SENDQ_LENGTH 16384
#define ZT_TCP_MAX_MESSAGE_LENGTH 2048
/** /**
* A TCP socket encapsulating ZeroTier packets over a TCP stream connection * A TCP socket encapsulating ZeroTier packets over a TCP stream connection
* *
@ -69,35 +70,26 @@ class TcpSocket : public Socket
public: public:
virtual ~TcpSocket(); virtual ~TcpSocket();
virtual bool isOpen() const;
virtual bool send(const InetAddress &to,const void *msg,unsigned int msglen); virtual bool send(const InetAddress &to,const void *msg,unsigned int msglen);
/**
* @return Remote TCP endpoint address
*/
inline const InetAddress &remote() const { return _remote; }
protected: protected:
#ifdef __WINDOWS__
TcpSocket(SOCKET s,bool c,const InetAddress &r) :
#else
TcpSocket(int s,bool c,const InetAddress &r) :
#endif
Socket(Socket::ZT_SOCKET_TYPE_TCP,s),
_lastReceivedData(Utils::now()),
_inptr(0),
_outptr(0),
_connecting(c),
_remote(r),
_lock() {}
virtual bool notifyAvailableForRead(const SharedPtr<Socket> &self,SocketManager *sm); virtual bool notifyAvailableForRead(const SharedPtr<Socket> &self,SocketManager *sm);
virtual bool notifyAvailableForWrite(const SharedPtr<Socket> &self,SocketManager *sm); virtual bool notifyAvailableForWrite(const SharedPtr<Socket> &self,SocketManager *sm);
private: private:
#ifdef __WINDOWS__
TcpSocket(SOCKET sock,bool connecting,const InetAddress &remote) :
#endif
TcpSocket(int sock,bool connecting,const InetAddress &remote) :
#else
Socket(ZT_SOCKET_TYPE_TCP,sock),
_lastReceivedData(Utils::now()),
_inptr(0),
_outptr(0),
_connecting(connecting),
_remote(remote),
_lock()
{
}
unsigned char _outbuf[ZT_TCP_SENDQ_LENGTH]; unsigned char _outbuf[ZT_TCP_SENDQ_LENGTH];
unsigned char _inbuf[ZT_TCP_MAX_MESSAGE_LENGTH]; unsigned char _inbuf[ZT_TCP_MAX_MESSAGE_LENGTH];
uint64_t _lastReceivedData; // updated whenever data is received, checked directly by SocketManager for stale TCP cleanup uint64_t _lastReceivedData; // updated whenever data is received, checked directly by SocketManager for stale TCP cleanup
@ -108,6 +100,6 @@ private:
Mutex _lock; Mutex _lock;
}; };
}; // namespace ZeroTier } // namespace ZeroTier
#endif #endif

View file

@ -28,7 +28,7 @@
#ifndef ZT_UDPSOCKET_HPP #ifndef ZT_UDPSOCKET_HPP
#define ZT_UDPSOCKET_HPP #define ZT_UDPSOCKET_HPP
#include "Socket.hpp" //#include "Socket.hpp"
namespace ZeroTier { namespace ZeroTier {
@ -37,7 +37,7 @@ class SocketManager;
/** /**
* Locally bound UDP socket * Locally bound UDP socket
*/ */
class TcpSocket : public Socket class UdpSocket : public Socket
{ {
friend class SharedPtr<Socket>; friend class SharedPtr<Socket>;
friend class SocketManager; friend class SocketManager;
@ -45,7 +45,6 @@ class TcpSocket : public Socket
public: public:
virtual ~UdpSocket(); virtual ~UdpSocket();
virtual bool isOpen() const;
virtual bool send(const InetAddress &to,const void *msg,unsigned int msglen); virtual bool send(const InetAddress &to,const void *msg,unsigned int msglen);
/** /**
@ -60,20 +59,17 @@ public:
bool sendWithHopLimit(const InetAddress &to,const void *msg,unsigned int msglen,int hopLimit); bool sendWithHopLimit(const InetAddress &to,const void *msg,unsigned int msglen,int hopLimit);
protected: protected:
#ifdef __WINDOWS__
UdpSocket(Type t,SOCKET s) :
#else
UdpSocket(Type t,int s) :
#endif
Socket(t,s) {}
virtual bool notifyAvailableForRead(const SharedPtr<Socket> &self,SocketManager *sm); virtual bool notifyAvailableForRead(const SharedPtr<Socket> &self,SocketManager *sm);
virtual bool notifyAvailableForWrite(const SharedPtr<Socket> &self,SocketManager *sm); virtual bool notifyAvailableForWrite(const SharedPtr<Socket> &self,SocketManager *sm);
private:
#ifdef __WINDOWS__
UdpSocket(Type t,SOCKET sock) :
#else
UdpSocket(Type t,int sock) :
#endif
Socket(t,sock)
{
}
}; };
}; // namespace ZeroTier } // namespace ZeroTier
#endif #endif

View file

@ -4,7 +4,6 @@ OBJS=\
node/C25519.o \ node/C25519.o \
node/CertificateOfMembership.o \ node/CertificateOfMembership.o \
node/Defaults.o \ node/Defaults.o \
node/Demarc.o \
node/EthernetTap.o \ node/EthernetTap.o \
node/HttpClient.o \ node/HttpClient.o \
node/Identity.o \ node/Identity.o \
@ -21,10 +20,12 @@ OBJS=\
node/Poly1305.o \ node/Poly1305.o \
node/Salsa20.o \ node/Salsa20.o \
node/Service.o \ node/Service.o \
node/SocketManager.o \
node/SoftwareUpdater.o \ node/SoftwareUpdater.o \
node/SHA512.o \ node/SHA512.o \
node/Switch.o \ node/Switch.o \
node/SysEnv.o \ node/SysEnv.o \
node/TcpSocket.o \
node/Topology.o \ node/Topology.o \
node/UdpSocket.o \ node/UdpSocket.o \
node/Utils.o node/Utils.o