TcpSocket.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. namespace ZeroTier {
  48. TcpSocket::~TcpSocket()
  49. {
  50. #ifdef __WINDOWS__
  51. ::closesocket(_sock);
  52. #else
  53. ::close(_sock);
  54. #endif
  55. //printf("!!! TCP SOCKET DESTROYED @%.16llx to %s\r\n",(unsigned long long)this,_remote.toString().c_str());
  56. }
  57. bool TcpSocket::send(const InetAddress &to,const void *msg,unsigned int msglen)
  58. {
  59. if (msglen > ZT_SOCKET_MAX_MESSAGE_LEN)
  60. return false; // message too big
  61. if (!msglen)
  62. return true; // sanity check
  63. Mutex::Lock _l(_writeLock);
  64. bool writeInProgress = ((_outptr != 0)||(_connecting));
  65. if ((_outptr + 5 + msglen) > (unsigned int)sizeof(_outbuf))
  66. return false;
  67. _outbuf[_outptr++] = 0x17; // look like TLS data
  68. _outbuf[_outptr++] = 0x03;
  69. _outbuf[_outptr++] = 0x03; // look like TLS 1.2
  70. _outbuf[_outptr++] = (unsigned char)((msglen >> 8) & 0xff);
  71. _outbuf[_outptr++] = (unsigned char)(msglen & 0xff);
  72. for(unsigned int i=0;i<msglen;++i)
  73. _outbuf[_outptr++] = ((const unsigned char *)msg)[i];
  74. if (!writeInProgress) {
  75. // If no output was enqueued before this, try to send() it and then
  76. // start a queued write if any remains after that.
  77. int n = (int)::send(_sock,(const char *)_outbuf,_outptr,0);
  78. if (n > 0)
  79. memmove(_outbuf,_outbuf + (unsigned int)n,_outptr -= (unsigned int)n);
  80. if (_outptr) {
  81. _sm->startNotifyWrite(this);
  82. _sm->whack();
  83. }
  84. } // else just leave in _outbuf[] to get written when stream is available for write
  85. return true;
  86. }
  87. bool TcpSocket::notifyAvailableForRead(const SharedPtr<Socket> &self,SocketManager *sm)
  88. {
  89. unsigned char buf[65536];
  90. // will not be called concurrently since only SocketManager::poll() calls this
  91. int n = (int)::recv(_sock,(char *)buf,sizeof(buf),0);
  92. if (n <= 0)
  93. return false; // read error, stream probably closed
  94. unsigned int p = _inptr,pl = 0;
  95. for(int k=0;k<n;++k) {
  96. _inbuf[p++] = buf[k];
  97. if (p >= (int)sizeof(_inbuf))
  98. return false; // read overrun, packet too large or invalid
  99. if ((!pl)&&(p >= 5)) {
  100. if (_inbuf[0] == 0x17) {
  101. // fake TLS data frame, next two bytes are TLS version and are ignored
  102. pl = (((unsigned int)_inbuf[3] << 8) | (unsigned int)_inbuf[4]) + 5;
  103. } else return false; // in the future we may support fake TLS handshakes
  104. }
  105. if ((pl)&&(p >= pl)) {
  106. Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> data(_inbuf + 5,pl - 5);
  107. memmove(_inbuf,_inbuf + pl,p -= pl);
  108. try {
  109. sm->handleReceivedPacket(self,_remote,data);
  110. } catch ( ... ) {} // handlers should not throw
  111. pl = 0;
  112. }
  113. }
  114. _inptr = p;
  115. return true;
  116. }
  117. bool TcpSocket::notifyAvailableForWrite(const SharedPtr<Socket> &self,SocketManager *sm)
  118. {
  119. Mutex::Lock _l(_writeLock);
  120. if (_connecting)
  121. _connecting = false;
  122. if (_outptr) {
  123. int n = (int)::send(_sock,(const char *)_outbuf,_outptr,0);
  124. #ifdef __WINDOWS__
  125. if (n == SOCKET_ERROR) {
  126. switch(WSAGetLastError()) {
  127. case WSAEINTR:
  128. case WSAEWOULDBLOCK:
  129. break;
  130. default:
  131. return false;
  132. }
  133. #else
  134. if (n <= 0) {
  135. switch(errno) {
  136. #ifdef EAGAIN
  137. case EAGAIN:
  138. #endif
  139. #if defined(EWOULDBLOCK) && ( !defined(EAGAIN) || (EWOULDBLOCK != EAGAIN) )
  140. case EWOULDBLOCK:
  141. #endif
  142. #ifdef EINTR
  143. case EINTR:
  144. #endif
  145. break;
  146. default:
  147. return false;
  148. }
  149. #endif
  150. } else memmove(_outbuf,_outbuf + (unsigned int)n,_outptr -= (unsigned int)n);
  151. }
  152. if (!_outptr)
  153. sm->stopNotifyWrite(this);
  154. return true;
  155. }
  156. } // namespace ZeroTier