TcpSocket.cpp 5.0 KB

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