TcpSocket.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #include <stdio.h>
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include <fcntl.h>
  31. #include <time.h>
  32. #include <errno.h>
  33. #include <sys/types.h>
  34. #include "Constants.hpp"
  35. #include "TcpSocket.hpp"
  36. #include "SocketManager.hpp"
  37. #ifdef __WINDOWS__
  38. #include <WinSock2.h>
  39. #include <WS2tcpip.h>
  40. #include <Windows.h>
  41. #else
  42. #include <unistd.h>
  43. #include <sys/socket.h>
  44. #include <arpa/inet.h>
  45. #include <signal.h>
  46. #endif
  47. #define ZT_TCP_MAX_SENDQ_LENGTH (ZT_SOCKET_MAX_MESSAGE_LEN * 8)
  48. namespace ZeroTier {
  49. TcpSocket::~TcpSocket()
  50. {
  51. #ifdef __WINDOWS__
  52. ::closesocket(_sock);
  53. #else
  54. ::close(_sock);
  55. #endif
  56. if (_outbuf)
  57. ::free(_outbuf);
  58. //printf("!!! TCP SOCKET DESTROYED @%.16llx to %s\r\n",(unsigned long long)this,_remote.toString().c_str());
  59. }
  60. bool TcpSocket::send(const InetAddress &to,const void *msg,unsigned int msglen)
  61. {
  62. if (msglen > ZT_SOCKET_MAX_MESSAGE_LEN)
  63. return false; // message too big
  64. if (!msglen)
  65. return true; // sanity check
  66. Mutex::Lock _l(_writeLock);
  67. bool writeInProgress = ((_outptr != 0)||(_connecting));
  68. // Ensure that _outbuf is large enough
  69. unsigned int newptr = _outptr + 5 + msglen;
  70. if (newptr > _outbufsize) {
  71. unsigned int newbufsize = _outbufsize;
  72. while (newbufsize < newptr)
  73. newbufsize += ZT_SOCKET_MAX_MESSAGE_LEN;
  74. if (newbufsize > ZT_TCP_MAX_SENDQ_LENGTH)
  75. return false; // cannot send, outbuf full
  76. unsigned char *newbuf = (unsigned char *)::malloc(newbufsize);
  77. if (!newbuf)
  78. return false; // cannot send, no memory
  79. _outbufsize = newbufsize;
  80. if (_outbuf) {
  81. memcpy(newbuf,_outbuf,_outptr);
  82. ::free(_outbuf);
  83. }
  84. _outbuf = newbuf;
  85. }
  86. _outbuf[_outptr++] = 0x17; // look like TLS data
  87. _outbuf[_outptr++] = 0x03;
  88. _outbuf[_outptr++] = 0x03; // look like TLS 1.2
  89. _outbuf[_outptr++] = (unsigned char)((msglen >> 8) & 0xff);
  90. _outbuf[_outptr++] = (unsigned char)(msglen & 0xff);
  91. for(unsigned int i=0;i<msglen;++i)
  92. _outbuf[_outptr++] = ((const unsigned char *)msg)[i];
  93. if (!writeInProgress) {
  94. // If no output was enqueued before this, try to send() it and then
  95. // start a queued write if any remains after that.
  96. int n = (int)::send(_sock,(const char *)_outbuf,_outptr,0);
  97. if (n > 0)
  98. memmove(_outbuf,_outbuf + (unsigned int)n,_outptr -= (unsigned int)n);
  99. if (_outptr) {
  100. _sm->startNotifyWrite(this);
  101. _sm->whack();
  102. }
  103. } // else just leave in _outbuf[] to get written when stream is available for write
  104. return true;
  105. }
  106. bool TcpSocket::notifyAvailableForRead(const SharedPtr<Socket> &self,SocketManager *sm)
  107. {
  108. unsigned char buf[65536];
  109. // will not be called concurrently since only SocketManager::poll() calls this
  110. int n = (int)::recv(_sock,(char *)buf,sizeof(buf),0);
  111. if (n <= 0)
  112. return false; // read error, stream probably closed
  113. unsigned int p = _inptr,pl = 0;
  114. for(int k=0;k<n;++k) {
  115. _inbuf[p++] = buf[k];
  116. if (p >= (int)sizeof(_inbuf))
  117. return false; // read overrun, packet too large or invalid
  118. if ((!pl)&&(p >= 5)) {
  119. if (_inbuf[0] == 0x17) {
  120. // fake TLS data frame, next two bytes are TLS version and are ignored
  121. pl = (((unsigned int)_inbuf[3] << 8) | (unsigned int)_inbuf[4]) + 5;
  122. } else return false; // in the future we may support fake TLS handshakes
  123. }
  124. if ((pl)&&(p >= pl)) {
  125. Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> data(_inbuf + 5,pl - 5);
  126. sm->handleReceivedPacket(self,_remote,data);
  127. memmove(_inbuf,_inbuf + pl,p - pl);
  128. p -= pl;
  129. pl = 0;
  130. }
  131. }
  132. _inptr = p;
  133. return true;
  134. }
  135. bool TcpSocket::notifyAvailableForWrite(const SharedPtr<Socket> &self,SocketManager *sm)
  136. {
  137. Mutex::Lock _l(_writeLock);
  138. if (_connecting)
  139. _connecting = false;
  140. if (_outptr) {
  141. int n = (int)::send(_sock,(const char *)_outbuf,_outptr,0);
  142. #ifdef __WINDOWS__
  143. if (n == SOCKET_ERROR) {
  144. switch(WSAGetLastError()) {
  145. case WSAEINTR:
  146. case WSAEWOULDBLOCK:
  147. break;
  148. default:
  149. return false;
  150. }
  151. #else
  152. if (n <= 0) {
  153. switch(errno) {
  154. #ifdef EAGAIN
  155. case EAGAIN:
  156. #endif
  157. #if defined(EWOULDBLOCK) && ( !defined(EAGAIN) || (EWOULDBLOCK != EAGAIN) )
  158. case EWOULDBLOCK:
  159. #endif
  160. #ifdef EINTR
  161. case EINTR:
  162. #endif
  163. break;
  164. default:
  165. return false;
  166. }
  167. #endif
  168. } else memmove(_outbuf,_outbuf + (unsigned int)n,_outptr -= (unsigned int)n);
  169. }
  170. if (!_outptr)
  171. sm->stopNotifyWrite(this);
  172. return true;
  173. }
  174. } // namespace ZeroTier