TcpSocket.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. namespace ZeroTier {
  35. class SocketManager;
  36. /**
  37. * A TCP socket encapsulating ZeroTier packets over a TCP stream connection
  38. *
  39. * This implements a simple packet encapsulation that is designed to look like
  40. * a TLS connection. It's not a TLS connection, but it sends TLS format record
  41. * headers. It could be extended in the future to implement a fake TLS
  42. * handshake.
  43. *
  44. * At the moment, each packet is just made to look like TLS application data:
  45. * <[1] TLS content type> - currently 0x17 for "application data"
  46. * <[1] TLS major version> - currently 0x03 for TLS 1.2
  47. * <[1] TLS minor version> - currently 0x03 for TLS 1.2
  48. * <[2] payload length> - 16-bit length of payload in bytes
  49. * <[...] payload> - Message payload
  50. *
  51. * The primary purpose of TCP sockets is to work over ports like HTTPS(443),
  52. * allowing users behind particularly fascist firewalls to at least reach
  53. * ZeroTier's supernodes. UDP is the preferred method of communication as
  54. * encapsulating L2 and L3 protocols over TCP is inherently inefficient
  55. * due to double-ACKs. So TCP is only used as a fallback.
  56. */
  57. class TcpSocket : public Socket
  58. {
  59. friend class SharedPtr<Socket>;
  60. friend class SocketManager;
  61. public:
  62. virtual ~TcpSocket();
  63. virtual bool send(const InetAddress &to,const void *msg,unsigned int msglen);
  64. protected:
  65. #ifdef __WINDOWS__
  66. TcpSocket(SocketManager *sm,SOCKET s,bool c,const InetAddress &r) :
  67. #else
  68. TcpSocket(SocketManager *sm,int s,bool c,const InetAddress &r) :
  69. #endif
  70. Socket(Socket::ZT_SOCKET_TYPE_TCP,s),
  71. _lastActivity(Utils::now()),
  72. _sm(sm),
  73. _outbuf((unsigned char *)0),
  74. _outptr(0),
  75. _outbufsize(0),
  76. _inptr(0),
  77. _connecting(c),
  78. _remote(r) {}
  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 _inbuf[ZT_SOCKET_MAX_MESSAGE_LEN];
  83. uint64_t _lastActivity; // updated whenever data is received, checked directly by SocketManager for stale TCP cleanup
  84. SocketManager *_sm;
  85. unsigned char *_outbuf;
  86. unsigned int _outptr;
  87. unsigned int _outbufsize;
  88. unsigned int _inptr;
  89. bool _connecting; // manipulated directly by SocketManager, true if connect() is in progress
  90. InetAddress _remote;
  91. Mutex _writeLock;
  92. };
  93. } // namespace ZeroTier
  94. #endif