SocketCore.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. #endif // HAVE_LIBSSL
  44. #ifdef HAVE_LIBGNUTLS
  45. # include <gnutls/gnutls.h>
  46. #endif // HAVE_LIBGNUTLS
  47. using namespace std;
  48. class SocketCore {
  49. friend bool operator==(const SocketCore& s1, const SocketCore& s2);
  50. friend bool operator!=(const SocketCore& s1, const SocketCore& s2);
  51. friend bool operator<(const SocketCore& s1, const SocketCore& s2);
  52. private:
  53. // socket endpoint descriptor
  54. int sockfd;
  55. // reference counter for this object.
  56. int use;
  57. bool secure;
  58. #ifdef HAVE_LIBSSL
  59. // for SSL
  60. SSL_CTX* sslCtx;
  61. SSL* ssl;
  62. #endif // HAVE_LIBSSL
  63. #ifdef HAVE_LIBGNUTLS
  64. gnutls_session_t sslSession;
  65. gnutls_certificate_credentials_t sslXcred;
  66. char* peekBuf;
  67. int peekBufLength;
  68. int peekBufMax;
  69. int shiftPeekData(char* data, int len);
  70. void addPeekData(char* data, int len);
  71. int gnutlsRecv(char* data, int len);
  72. int gnutlsPeek(char* data, int len);
  73. #endif // HAVE_LIBGNUTLS
  74. void init();
  75. SocketCore(int sockfd);
  76. public:
  77. SocketCore();
  78. ~SocketCore();
  79. int getSockfd() const { return sockfd; }
  80. bool isOpen() const { return sockfd != -1; }
  81. /**
  82. * Creates a socket and listens form connection on it.
  83. * @param port port to listen. If 0 is specified, os automaticaly
  84. * choose avaiable port.
  85. */
  86. void beginListen(int port = 0);
  87. /**
  88. * Stores host address and port of this socket to addrinfo.
  89. * @param addrinfo placeholder to store host address and port.
  90. */
  91. void getAddrInfo(pair<string, int>& addrinfo) const;
  92. /**
  93. * Stores peer's address and port to peerinfo.
  94. * @param peerinfo placeholder to store peer's address and port.
  95. */
  96. void getPeerInfo(pair<string, int>& peerinfo) const;
  97. /**
  98. * Accepts incoming connection on this socket.
  99. * You must call beginListen() before calling this method.
  100. * @return accepted socket. The caller must delete it after using it.
  101. */
  102. SocketCore* acceptConnection() const;
  103. /**
  104. * Connects to the server named host and the destination port is port.
  105. * This method makes socket non-blocking mode.
  106. * To make the socket blocking mode again, call setBlockingMode() after
  107. * the connection is established.
  108. * @param host hostname or ip address to connect to
  109. * @param port service port number to connect to
  110. */
  111. void establishConnection(const string& host, int port);
  112. void setNonBlockingMode() const;
  113. /**
  114. * Makes this socket blocking mode.
  115. */
  116. void setBlockingMode() const;
  117. /**
  118. * Closes the connection of this socket.
  119. */
  120. void closeConnection();
  121. /**
  122. * Checks whether this socket is available for writing.
  123. * @param timeout the amount of time elapsed before the checking are timed
  124. * out.
  125. * @return true if the socket is available for writing,
  126. * otherwise returns false.
  127. */
  128. bool isWritable(int timeout) const;
  129. /**
  130. * Checks whether this socket is available for reading.
  131. * @param timeout the amount of time elapsed before the checking are timed
  132. * out.
  133. * @return true if the socket is available for reading,
  134. * otherwise returns false.
  135. */
  136. bool isReadable(int timeout) const;
  137. /**
  138. * Writes characters into this socket. data is a pointer pointing the first
  139. * byte of the data and len is the length of data.
  140. * This method internally calls isWritable(). The parmeter timeout is used
  141. * for this method call.
  142. * @param data data to write
  143. * @param len length of data
  144. */
  145. void writeData(const char* data, int len);
  146. void writeData(const string& msg) { writeData(msg.c_str(), msg.size()); }
  147. /**
  148. * Reads up to len bytes from this socket.
  149. * data is a pointer pointing the first
  150. * byte of the data, which must be allocated before this method is called.
  151. * len is the size of the allocated memory. When this method returns
  152. * successfully, len is replaced by the size of the read data.
  153. * This method internally calls isReadable(). The parameter timeout is used
  154. * for this method call.
  155. * @param data holder to store data.
  156. * @param len the maximum size data can store. This method assigns
  157. * the number of bytes read to len.
  158. */
  159. void readData(char* data, int& len);
  160. /**
  161. * Reads up to len bytes from this socket, but bytes are not removed from
  162. * this socket.
  163. * This method internally calls isReadable(). The parameter timeout is used
  164. * for this method call.
  165. * @param data holder to store data.
  166. * @param len the maximum size data can store. This method assigns
  167. * the number of bytes read to len.
  168. */
  169. void peekData(char* data, int& len);
  170. /**
  171. * Makes this socket secure.
  172. * If the system has not OpenSSL, then this method do nothing.
  173. * connection must be established before calling this method.
  174. */
  175. void initiateSecureConnection() ;
  176. bool operator==(const SocketCore& s) {
  177. return sockfd == s.sockfd;
  178. }
  179. bool operator!=(const SocketCore& s) {
  180. return !(*this == s);
  181. }
  182. bool operator<(const SocketCore& s) {
  183. return sockfd < s.sockfd;
  184. }
  185. };
  186. #endif // _D_SOCKET_CORE_H_