Browse Source

Compile fixes.

Adam Ierymenko 11 years ago
parent
commit
bd749e040d
7 changed files with 106 additions and 100 deletions
  1. 1 6
      node/Peer.hpp
  2. 11 32
      node/Socket.hpp
  3. 58 13
      node/SocketManager.cpp
  4. 8 10
      node/SocketManager.hpp
  5. 17 25
      node/TcpSocket.hpp
  6. 9 13
      node/UdpSocket.hpp
  7. 2 1
      objects.mk

+ 1 - 6
node/Peer.hpp

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

+ 11 - 32
node/Socket.hpp

@@ -28,8 +28,6 @@
 #ifndef ZT_SOCKET_HPP
 #define ZT_SOCKET_HPP
 
-#include <list>
-
 #include "Constants.hpp"
 #include "InetAddress.hpp"
 #include "AtomicCounter.hpp"
@@ -49,6 +47,7 @@
 
 namespace ZeroTier {
 
+class Socket;
 class SocketManager;
 
 /**
@@ -58,8 +57,8 @@ class SocketManager;
  */
 class Socket : NonCopyable
 {
-	friend class SharedPtr<Socket>;
 	friend class SocketManager;
+	friend class SharedPtr<Socket>;
 
 public:
 	enum Type
@@ -80,20 +79,6 @@ public:
 		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
 	 *
@@ -105,25 +90,19 @@ public:
 	virtual bool send(const InetAddress &to,const void *msg,unsigned int msglen) = 0;
 
 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)
 	virtual bool notifyAvailableForRead(const SharedPtr<Socket> &self,SocketManager *sm) = 0;
 	virtual bool notifyAvailableForWrite(const SharedPtr<Socket> &self,SocketManager *sm) = 0;
 
 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__
 	SOCKET _sock;
 #else
@@ -134,6 +113,6 @@ private:
 	AtomicCounter __refCount;
 };
 
-}; // namespace ZeroTier
+} // namespace ZeroTier
 
 #endif

+ 58 - 13
node/SocketManager.cpp

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

+ 8 - 10
node/SocketManager.hpp

@@ -48,8 +48,6 @@
 #include "SharedPtr.hpp"
 #include "InetAddress.hpp"
 #include "Socket.hpp"
-#include "TcpSocket.hpp"
-#include "UdpSocket.hpp"
 #include "Mutex.hpp"
 #include "NonCopyable.hpp"
 #include "Buffer.hpp"
@@ -75,7 +73,11 @@ public:
 	 * @param arg Second argument to packetHandler()
 	 * @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();
 
@@ -169,19 +171,15 @@ private:
 #ifdef __WINDOWS__
 	SOCKET _whackSendPipe;
 	SOCKET _whackReceivePipe;
-#else
-	int _whackSendPipe;
-	int _whackReceivePipe;
-#endif
-	Mutex::Lock _whackSendPipe_m;
-
-#ifdef __WINDOWS__
 	SOCKET _tcpV4ListenSocket;
 	SOCKET _tcpV6ListenSocket;
 #else
+	int _whackSendPipe;
+	int _whackReceivePipe;
 	int _tcpV4ListenSocket;
 	int _tcpV6ListenSocket;
 #endif
+	Mutex _whackSendPipe_m;
 
 	SharedPtr<Socket> _udpV4Socket;
 	SharedPtr<Socket> _udpV6Socket;

+ 17 - 25
node/TcpSocket.hpp

@@ -30,17 +30,18 @@
 
 #include <stdint.h>
 
-#include "Socket.hpp"
+#include "InetAddress.hpp"
 #include "Mutex.hpp"
 #include "Utils.hpp"
+#include "Socket.hpp"
+
+#define ZT_TCP_SENDQ_LENGTH 4096
+#define ZT_TCP_MAX_MESSAGE_LENGTH 2048
 
 namespace ZeroTier {
 
 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
  *
@@ -69,35 +70,26 @@ class TcpSocket : public Socket
 
 public:
 	virtual ~TcpSocket();
-
-	virtual bool isOpen() const;
 	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:
-	virtual bool notifyAvailableForRead(const SharedPtr<Socket> &self,SocketManager *sm);
-	virtual bool notifyAvailableForWrite(const SharedPtr<Socket> &self,SocketManager *sm);
-
-private:
 #ifdef __WINDOWS__
-	TcpSocket(SOCKET sock,bool connecting,const InetAddress &remote) :
-#endif
-	TcpSocket(int sock,bool connecting,const InetAddress &remote) :
+	TcpSocket(SOCKET s,bool c,const InetAddress &r) :
 #else
-		Socket(ZT_SOCKET_TYPE_TCP,sock),
+	TcpSocket(int s,bool c,const InetAddress &r) :
+#endif
+		Socket(Socket::ZT_SOCKET_TYPE_TCP,s),
 		_lastReceivedData(Utils::now()),
 		_inptr(0),
 		_outptr(0),
-		_connecting(connecting),
-		_remote(remote),
-		_lock()
-	{
-	}
+		_connecting(c),
+		_remote(r),
+		_lock() {}
 
+	virtual bool notifyAvailableForRead(const SharedPtr<Socket> &self,SocketManager *sm);
+	virtual bool notifyAvailableForWrite(const SharedPtr<Socket> &self,SocketManager *sm);
+
+private:
 	unsigned char _outbuf[ZT_TCP_SENDQ_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
@@ -108,6 +100,6 @@ private:
 	Mutex _lock;
 };
 
-}; // namespace ZeroTier
+} // namespace ZeroTier
 
 #endif

+ 9 - 13
node/UdpSocket.hpp

@@ -28,7 +28,7 @@
 #ifndef ZT_UDPSOCKET_HPP
 #define ZT_UDPSOCKET_HPP
 
-#include "Socket.hpp"
+//#include "Socket.hpp"
 
 namespace ZeroTier {
 
@@ -37,7 +37,7 @@ class SocketManager;
 /**
  * Locally bound UDP socket
  */
-class TcpSocket : public Socket
+class UdpSocket : public Socket
 {
 	friend class SharedPtr<Socket>;
 	friend class SocketManager;
@@ -45,7 +45,6 @@ class TcpSocket : public Socket
 public:
 	virtual ~UdpSocket();
 
-	virtual bool isOpen() const;
 	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);
 
 protected:
-	virtual bool notifyAvailableForRead(const SharedPtr<Socket> &self,SocketManager *sm);
-	virtual bool notifyAvailableForWrite(const SharedPtr<Socket> &self,SocketManager *sm);
-
-private:
 #ifdef __WINDOWS__
-	UdpSocket(Type t,SOCKET sock) :
+	UdpSocket(Type t,SOCKET s) :
 #else
-	UdpSocket(Type t,int sock) :
+	UdpSocket(Type t,int s) :
 #endif
-		Socket(t,sock)
-	{
-	}
+		Socket(t,s) {}
+
+	virtual bool notifyAvailableForRead(const SharedPtr<Socket> &self,SocketManager *sm);
+	virtual bool notifyAvailableForWrite(const SharedPtr<Socket> &self,SocketManager *sm);
 };
 
-}; // namespace ZeroTier
+} // namespace ZeroTier
 
 #endif

+ 2 - 1
objects.mk

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