SocketCore.h 8.1 KB

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