SocketCore.h 9.5 KB

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