You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Socket.cxx 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #ifdef WIN32
  22. //#include <io.h>
  23. #include <winsock2.h>
  24. #include <ws2tcpip.h>
  25. #define errorNumber WSAGetLastError()
  26. #define SHUT_RD SD_RECEIVE
  27. #define SHUT_WR SD_SEND
  28. #define SHUT_RDWR SD_BOTH
  29. #else
  30. #define errorNumber errno
  31. #define closesocket close
  32. #include <sys/socket.h>
  33. #endif
  34. #include <unistd.h>
  35. #include <signal.h>
  36. #include <fcntl.h>
  37. #include <errno.h>
  38. #include <network/Socket.h>
  39. using namespace network;
  40. // -=- Socket initialisation
  41. static bool socketsInitialised = false;
  42. void network::initSockets() {
  43. if (socketsInitialised)
  44. return;
  45. #ifdef WIN32
  46. WORD requiredVersion = MAKEWORD(2,0);
  47. WSADATA initResult;
  48. if (WSAStartup(requiredVersion, &initResult) != 0)
  49. throw SocketException("unable to initialise Winsock2", errorNumber);
  50. #else
  51. signal(SIGPIPE, SIG_IGN);
  52. #endif
  53. socketsInitialised = true;
  54. }
  55. bool network::isSocketListening(int sock)
  56. {
  57. int listening = 0;
  58. socklen_t listening_size = sizeof(listening);
  59. if (getsockopt(sock, SOL_SOCKET, SO_ACCEPTCONN,
  60. (char *)&listening, &listening_size) < 0)
  61. return false;
  62. return listening != 0;
  63. }
  64. Socket::Socket(int fd)
  65. : instream(0), outstream(0),
  66. isShutdown_(false), queryConnection(false)
  67. {
  68. initSockets();
  69. setFd(fd);
  70. }
  71. Socket::Socket()
  72. : instream(0), outstream(0),
  73. isShutdown_(false), queryConnection(false)
  74. {
  75. initSockets();
  76. }
  77. Socket::~Socket()
  78. {
  79. if (instream && outstream)
  80. closesocket(getFd());
  81. delete instream;
  82. delete outstream;
  83. }
  84. // if shutdown() is overridden then the override MUST call on to here
  85. void Socket::shutdown()
  86. {
  87. isShutdown_ = true;
  88. ::shutdown(getFd(), SHUT_RDWR);
  89. }
  90. bool Socket::isShutdown() const
  91. {
  92. return isShutdown_;
  93. }
  94. // Was there a "?" in the ConnectionFilter used to accept this Socket?
  95. void Socket::setRequiresQuery()
  96. {
  97. queryConnection = true;
  98. }
  99. bool Socket::requiresQuery() const
  100. {
  101. return queryConnection;
  102. }
  103. void Socket::setFd(int fd)
  104. {
  105. #ifndef WIN32
  106. // - By default, close the socket on exec()
  107. fcntl(fd, F_SETFD, FD_CLOEXEC);
  108. #endif
  109. instream = new rdr::FdInStream(fd);
  110. outstream = new rdr::FdOutStream(fd);
  111. isShutdown_ = false;
  112. }
  113. SocketListener::SocketListener(int fd)
  114. : fd(fd), filter(0)
  115. {
  116. initSockets();
  117. }
  118. SocketListener::SocketListener()
  119. : fd(-1), filter(0)
  120. {
  121. initSockets();
  122. }
  123. SocketListener::~SocketListener()
  124. {
  125. if (fd != -1)
  126. closesocket(fd);
  127. }
  128. void SocketListener::shutdown()
  129. {
  130. #ifdef WIN32
  131. closesocket(fd);
  132. fd = -1;
  133. #else
  134. ::shutdown(fd, SHUT_RDWR);
  135. #endif
  136. }
  137. Socket* SocketListener::accept() {
  138. int new_sock = -1;
  139. // Accept an incoming connection
  140. if ((new_sock = ::accept(fd, 0, 0)) < 0)
  141. throw SocketException("unable to accept new connection", errorNumber);
  142. // Create the socket object & check connection is allowed
  143. Socket* s = createSocket(new_sock);
  144. if (filter && !filter->verifyConnection(s)) {
  145. delete s;
  146. return NULL;
  147. }
  148. return s;
  149. }
  150. void SocketListener::listen(int sock)
  151. {
  152. // - Set it to be a listening socket
  153. if (::listen(sock, 5) < 0) {
  154. int e = errorNumber;
  155. closesocket(sock);
  156. throw SocketException("unable to set socket to listening mode", e);
  157. }
  158. fd = sock;
  159. }