TcpSocket.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef ZT_TCPSOCKET_HPP
  28. #define ZT_TCPSOCKET_HPP
  29. #include <stdint.h>
  30. #include "InetAddress.hpp"
  31. #include "Mutex.hpp"
  32. #include "Utils.hpp"
  33. #include "Socket.hpp"
  34. #define ZT_TCP_SENDQ_LENGTH 4096
  35. #define ZT_TCP_MAX_MESSAGE_LENGTH 2048
  36. namespace ZeroTier {
  37. class SocketManager;
  38. /**
  39. * A TCP socket encapsulating ZeroTier packets over a TCP stream connection
  40. *
  41. * This implements a simple packet encapsulation that is designed to look like
  42. * a TLS connection. It's not a TLS connection, but it sends TLS format record
  43. * headers. It could be extended in the future to implement a fake TLS
  44. * handshake.
  45. *
  46. * At the moment, each packet is just made to look like TLS application data:
  47. * <[1] TLS content type> - currently 0x17 for "application data"
  48. * <[1] TLS major version> - currently 0x03 for TLS 1.2
  49. * <[1] TLS minor version> - currently 0x03 for TLS 1.2
  50. * <[2] payload length> - 16-bit length of payload in bytes
  51. * <[...] payload> - Message payload
  52. *
  53. * The primary purpose of TCP sockets is to work over ports like HTTPS(443),
  54. * allowing users behind particularly fascist firewalls to at least reach
  55. * ZeroTier's supernodes. UDP is the preferred method of communication as
  56. * encapsulating L2 and L3 protocols over TCP is inherently inefficient
  57. * due to double-ACKs. So TCP is only used as a fallback.
  58. */
  59. class TcpSocket : public Socket
  60. {
  61. friend class SharedPtr<Socket>;
  62. friend class SocketManager;
  63. public:
  64. virtual ~TcpSocket();
  65. virtual bool send(const InetAddress &to,const void *msg,unsigned int msglen);
  66. protected:
  67. #ifdef __WINDOWS__
  68. TcpSocket(SOCKET s,bool c,const InetAddress &r) :
  69. #else
  70. TcpSocket(int s,bool c,const InetAddress &r) :
  71. #endif
  72. Socket(Socket::ZT_SOCKET_TYPE_TCP,s),
  73. _lastReceivedData(Utils::now()),
  74. _inptr(0),
  75. _outptr(0),
  76. _connecting(c),
  77. _remote(r),
  78. _lock() {}
  79. virtual bool notifyAvailableForRead(const SharedPtr<Socket> &self,SocketManager *sm);
  80. virtual bool notifyAvailableForWrite(const SharedPtr<Socket> &self,SocketManager *sm);
  81. private:
  82. unsigned char _outbuf[ZT_TCP_SENDQ_LENGTH];
  83. unsigned char _inbuf[ZT_TCP_MAX_MESSAGE_LENGTH];
  84. uint64_t _lastReceivedData; // updated whenever data is received, checked directly by SocketManager for stale TCP cleanup
  85. unsigned int _inptr;
  86. unsigned int _outptr;
  87. bool _connecting; // manipulated directly by SocketManager, true if connect() is in progress
  88. InetAddress _remote;
  89. Mutex _lock;
  90. };
  91. } // namespace ZeroTier
  92. #endif