diff options
author | Adam Ierymenko <adam.ierymenko@gmail.com> | 2014-03-17 15:14:22 -0700 |
---|---|---|
committer | Adam Ierymenko <adam.ierymenko@gmail.com> | 2014-03-17 15:14:22 -0700 |
commit | bd749e040d1e86f6a0aa20c64f2d69c1a21e2f24 (patch) | |
tree | 2d648d7ebe08b5aa865fb8a6071402ccfda70c22 | |
parent | 7e80d7e551a0a2edc3fa3e6b6eb7379863c3ac04 (diff) | |
download | infinitytier-bd749e040d1e86f6a0aa20c64f2d69c1a21e2f24.tar.gz infinitytier-bd749e040d1e86f6a0aa20c64f2d69c1a21e2f24.zip |
Compile fixes.
-rw-r--r-- | node/Peer.hpp | 7 | ||||
-rw-r--r-- | node/Socket.hpp | 43 | ||||
-rw-r--r-- | node/SocketManager.cpp | 71 | ||||
-rw-r--r-- | node/SocketManager.hpp | 18 | ||||
-rw-r--r-- | node/TcpSocket.hpp | 42 | ||||
-rw-r--r-- | node/UdpSocket.hpp | 22 | ||||
-rw-r--r-- | objects.mk | 3 |
7 files changed, 106 insertions, 100 deletions
diff --git a/node/Peer.hpp b/node/Peer.hpp index 78f6abfc..f7c81cb0 100644 --- a/node/Peer.hpp +++ b/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 }; diff --git a/node/Socket.hpp b/node/Socket.hpp index 687834a2..e8ebfeb7 100644 --- a/node/Socket.hpp +++ b/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 @@ -81,20 +80,6 @@ public: } /** - * @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 * * @param to Destination address (ignored in connected TCP sockets) @@ -105,26 +90,20 @@ 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 int _sock; @@ -134,6 +113,6 @@ private: AtomicCounter __refCount; }; -}; // namespace ZeroTier +} // namespace ZeroTier #endif diff --git a/node/SocketManager.cpp b/node/SocketManager.cpp index 87bbf066..36def1e1 100644 --- a/node/SocketManager.cpp +++ b/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); } } diff --git a/node/SocketManager.hpp b/node/SocketManager.hpp index 5ea11080..3a42897c 100644 --- a/node/SocketManager.hpp +++ b/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; diff --git a/node/TcpSocket.hpp b/node/TcpSocket.hpp index 0161fa27..e73d7ab3 100644 --- a/node/TcpSocket.hpp +++ b/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 diff --git a/node/UdpSocket.hpp b/node/UdpSocket.hpp index 9a6cf68b..5e0f961a 100644 --- a/node/UdpSocket.hpp +++ b/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 @@ -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 |