SocketCore.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #ifndef _D_SOCKET_CORE_H_
  36. #define _D_SOCKET_CORE_H_
  37. #include "common.h"
  38. #include <string>
  39. #include <utility>
  40. #ifdef HAVE_LIBSSL
  41. // for SSL
  42. # include <openssl/ssl.h>
  43. # include <openssl/err.h>
  44. #endif // HAVE_LIBSSL
  45. #ifdef HAVE_LIBGNUTLS
  46. # include <gnutls/gnutls.h>
  47. #endif // HAVE_LIBGNUTLS
  48. using namespace std;
  49. class SocketCore {
  50. friend bool operator==(const SocketCore& s1, const SocketCore& s2);
  51. friend bool operator!=(const SocketCore& s1, const SocketCore& s2);
  52. friend bool operator<(const SocketCore& s1, const SocketCore& s2);
  53. private:
  54. // socket endpoint descriptor
  55. int32_t sockfd;
  56. // reference counter for this object.
  57. int32_t use;
  58. bool blocking;
  59. bool secure;
  60. #ifdef HAVE_LIBSSL
  61. // for SSL
  62. SSL_CTX* sslCtx;
  63. SSL* ssl;
  64. #endif // HAVE_LIBSSL
  65. #ifdef HAVE_LIBGNUTLS
  66. gnutls_session_t sslSession;
  67. gnutls_certificate_credentials_t sslXcred;
  68. char* peekBuf;
  69. int32_t peekBufLength;
  70. int32_t peekBufMax;
  71. int32_t shiftPeekData(char* data, int32_t len);
  72. void addPeekData(char* data, int32_t len);
  73. int32_t gnutlsRecv(char* data, int32_t len);
  74. int32_t gnutlsPeek(char* data, int32_t len);
  75. #endif // HAVE_LIBGNUTLS
  76. void init();
  77. SocketCore(int32_t sockfd);
  78. static int error();
  79. static const char *errorMsg();
  80. static const char *errorMsg(const int err);
  81. public:
  82. SocketCore();
  83. ~SocketCore();
  84. int32_t getSockfd() const { return sockfd; }
  85. bool isOpen() const { return sockfd != -1; }
  86. /**
  87. * Creates a socket and listens form connection on it.
  88. * @param port port to listen. If 0 is specified, os automaticaly
  89. * choose avaiable port.
  90. */
  91. void beginListen(int32_t port = 0);
  92. /**
  93. * Stores host address and port of this socket to addrinfo.
  94. * @param addrinfo placeholder to store host address and port.
  95. */
  96. void getAddrInfo(pair<string, int32_t>& addrinfo) const;
  97. /**
  98. * Stores peer's address and port to peerinfo.
  99. * @param peerinfo placeholder to store peer's address and port.
  100. */
  101. void getPeerInfo(pair<string, int32_t>& peerinfo) const;
  102. /**
  103. * Accepts incoming connection on this socket.
  104. * You must call beginListen() before calling this method.
  105. * @return accepted socket. The caller must delete it after using it.
  106. */
  107. SocketCore* acceptConnection() const;
  108. /**
  109. * Connects to the server named host and the destination port is port.
  110. * This method makes socket non-blocking mode.
  111. * To make the socket blocking mode again, call setBlockingMode() after
  112. * the connection is established.
  113. * @param host hostname or ip address to connect to
  114. * @param port service port number to connect to
  115. */
  116. void establishConnection(const string& host, int32_t port);
  117. void setNonBlockingMode();
  118. /**
  119. * Makes this socket blocking mode.
  120. */
  121. void setBlockingMode();
  122. /**
  123. * Closes the connection of this socket.
  124. */
  125. void closeConnection();
  126. /**
  127. * Checks whether this socket is available for writing.
  128. * @param timeout the amount of time elapsed before the checking are timed
  129. * out.
  130. * @return true if the socket is available for writing,
  131. * otherwise returns false.
  132. */
  133. bool isWritable(int32_t timeout) const;
  134. /**
  135. * Checks whether this socket is available for reading.
  136. * @param timeout the amount of time elapsed before the checking are timed
  137. * out.
  138. * @return true if the socket is available for reading,
  139. * otherwise returns false.
  140. */
  141. bool isReadable(int32_t timeout) const;
  142. /**
  143. * Writes characters into this socket. data is a pointer pointing the first
  144. * byte of the data and len is the length of data.
  145. * This method internally calls isWritable(). The parmeter timeout is used
  146. * for this method call.
  147. * @param data data to write
  148. * @param len length of data
  149. */
  150. void writeData(const char* data, int32_t len);
  151. void writeData(const string& msg)
  152. {
  153. writeData(msg.c_str(), msg.size());
  154. }
  155. /**
  156. * Reads up to len bytes from this socket.
  157. * data is a pointer pointing the first
  158. * byte of the data, which must be allocated before this method is called.
  159. * len is the size of the allocated memory. When this method returns
  160. * successfully, len is replaced by the size of the read data.
  161. * This method internally calls isReadable(). The parameter timeout is used
  162. * for this method call.
  163. * @param data holder to store data.
  164. * @param len the maximum size data can store. This method assigns
  165. * the number of bytes read to len.
  166. */
  167. void readData(char* data, int32_t& len);
  168. /**
  169. * Reads up to len bytes from this socket, but bytes are not removed from
  170. * this socket.
  171. * This method internally calls isReadable(). The parameter timeout is used
  172. * for this method call.
  173. * @param data holder to store data.
  174. * @param len the maximum size data can store. This method assigns
  175. * the number of bytes read to len.
  176. */
  177. void peekData(char* data, int32_t& len);
  178. /**
  179. * Makes this socket secure.
  180. * If the system has not OpenSSL, then this method do nothing.
  181. * connection must be established before calling this method.
  182. */
  183. void initiateSecureConnection();
  184. bool operator==(const SocketCore& s) {
  185. return sockfd == s.sockfd;
  186. }
  187. bool operator!=(const SocketCore& s) {
  188. return !(*this == s);
  189. }
  190. bool operator<(const SocketCore& s) {
  191. return sockfd < s.sockfd;
  192. }
  193. };
  194. #endif // _D_SOCKET_CORE_H_