Platform.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. #include "Platform.h"
  36. #include <stdlib.h> /* _fmode */
  37. #include <fcntl.h> /* _O_BINARY */
  38. #include <locale.h> // For setlocale, LC_*
  39. #include <iostream>
  40. #ifdef HAVE_OPENSSL
  41. # include <openssl/err.h>
  42. # include <openssl/ssl.h>
  43. # include "libssl_compat.h"
  44. #endif // HAVE_OPENSSL
  45. #ifdef HAVE_LIBGCRYPT
  46. # include <gcrypt.h>
  47. #endif // HAVE_LIBGCRYPT
  48. #ifdef HAVE_LIBGNUTLS
  49. # include <gnutls/gnutls.h>
  50. #endif // HAVE_LIBGNUTLS
  51. #ifdef ENABLE_ASYNC_DNS
  52. # include <ares.h>
  53. #endif // ENABLE_ASYNC_DNS
  54. #ifdef HAVE_LIBSSH2
  55. # include <libssh2.h>
  56. #endif // HAVE_LIBSSH2
  57. #include "a2netcompat.h"
  58. #include "DlAbortEx.h"
  59. #include "message.h"
  60. #include "fmt.h"
  61. #include "console.h"
  62. #include "OptionParser.h"
  63. #include "prefs.h"
  64. #ifdef HAVE_LIBGMP
  65. # include "a2gmp.h"
  66. #endif // HAVE_LIBGMP
  67. #include "LogFactory.h"
  68. #include "util.h"
  69. #include "SocketCore.h"
  70. namespace aria2 {
  71. #ifdef HAVE_LIBGNUTLS
  72. namespace {
  73. void gnutls_log_callback(int level, const char* str)
  74. {
  75. using namespace aria2;
  76. // GnuTLS adds a newline. Drop it.
  77. std::string msg(str);
  78. msg.resize(msg.size() - 1);
  79. A2_LOG_DEBUG(fmt("GnuTLS: <%d> %s", level, msg.c_str()));
  80. }
  81. } // namespace
  82. #endif // HAVE_LIBGNUTLS
  83. bool Platform::initialized_ = false;
  84. #if OPENSSL_VERSION_NUMBER >= 0x30000000L
  85. OSSL_PROVIDER* Platform::legacy_provider_ = nullptr;
  86. OSSL_PROVIDER* Platform::default_provider_ = nullptr;
  87. #endif // OPENSSL_VERSION_NUMBER >= 0x30000000L
  88. Platform::Platform() { setUp(); }
  89. Platform::~Platform() { tearDown(); }
  90. bool Platform::setUp()
  91. {
  92. if (initialized_) {
  93. return false;
  94. }
  95. initialized_ = true;
  96. #ifdef HAVE_LIBGMP
  97. global::initGmp();
  98. #endif // HAVE_LIBGMP
  99. #ifdef ENABLE_NLS
  100. setlocale(LC_CTYPE, "");
  101. setlocale(LC_MESSAGES, "");
  102. bindtextdomain(PACKAGE, LOCALEDIR);
  103. textdomain(PACKAGE);
  104. #endif // ENABLE_NLS
  105. #ifdef HAVE_OPENSSL
  106. # if OPENSSL_VERSION_NUMBER >= 0x30000000L
  107. // RC4 is in the legacy provider.
  108. legacy_provider_ = OSSL_PROVIDER_load(nullptr, "legacy");
  109. if (!legacy_provider_) {
  110. throw DL_ABORT_EX("OSSL_PROVIDER_load 'legacy' failed.");
  111. }
  112. default_provider_ = OSSL_PROVIDER_load(nullptr, "default");
  113. if (!default_provider_) {
  114. throw DL_ABORT_EX("OSSL_PROVIDER_load 'default' failed.");
  115. }
  116. # elif !OPENSSL_101_API
  117. // for SSL initialization
  118. SSL_load_error_strings();
  119. SSL_library_init();
  120. // Need this to "decrypt" p12 files.
  121. OpenSSL_add_all_algorithms();
  122. # endif // !OPENSSL_101_API
  123. #endif // HAVE_OPENSSL
  124. #ifdef HAVE_LIBGCRYPT
  125. if (!gcry_check_version("1.2.4")) {
  126. throw DL_ABORT_EX("gcry_check_version() failed.");
  127. }
  128. gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
  129. gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
  130. #endif // HAVE_LIBGCRYPT
  131. #ifdef HAVE_LIBGNUTLS
  132. {
  133. int r = gnutls_global_init();
  134. if (r != GNUTLS_E_SUCCESS) {
  135. throw DL_ABORT_EX(
  136. fmt("gnutls_global_init() failed, cause:%s", gnutls_strerror(r)));
  137. }
  138. gnutls_global_set_log_function(gnutls_log_callback);
  139. gnutls_global_set_log_level(0);
  140. }
  141. #endif // HAVE_LIBGNUTLS
  142. #ifdef CARES_HAVE_ARES_LIBRARY_INIT
  143. int aresErrorCode;
  144. if ((aresErrorCode = ares_library_init(ARES_LIB_INIT_ALL)) != 0) {
  145. global::cerr()->printf("ares_library_init() failed:%s\n",
  146. ares_strerror(aresErrorCode));
  147. }
  148. #endif // CARES_HAVE_ARES_LIBRARY_INIT
  149. #ifdef HAVE_LIBSSH2
  150. {
  151. auto rv = libssh2_init(0);
  152. if (rv != 0) {
  153. throw DL_ABORT_EX(fmt("libssh2_init() failed, code: %d", rv));
  154. }
  155. }
  156. #endif // HAVE_LIBSSH2
  157. #ifdef HAVE_WINSOCK2_H
  158. WSADATA wsaData;
  159. memset(reinterpret_cast<char*>(&wsaData), 0, sizeof(wsaData));
  160. if (WSAStartup(MAKEWORD(1, 1), &wsaData)) {
  161. throw DL_ABORT_EX(MSG_WINSOCK_INIT_FAILD);
  162. }
  163. #endif // HAVE_WINSOCK2_H
  164. #ifdef __MINGW32__
  165. (void)_setmode(_fileno(stdin), _O_BINARY);
  166. (void)_setmode(_fileno(stdout), _O_BINARY);
  167. (void)_setmode(_fileno(stderr), _O_BINARY);
  168. #endif // __MINGW32__
  169. return true;
  170. }
  171. bool Platform::tearDown()
  172. {
  173. if (!initialized_) {
  174. return false;
  175. }
  176. initialized_ = false;
  177. SocketCore::setClientTLSContext(nullptr);
  178. SocketCore::setServerTLSContext(nullptr);
  179. #ifdef HAVE_OPENSSL
  180. # if OPENSSL_VERSION_NUMBER >= 0x30000000L
  181. if (default_provider_) {
  182. OSSL_PROVIDER_unload(default_provider_);
  183. }
  184. if (legacy_provider_) {
  185. OSSL_PROVIDER_unload(legacy_provider_);
  186. }
  187. # endif // OPENSSL_VERSION_NUMBER >= 0x30000000L
  188. #endif // HAVE_OPENSSL
  189. #ifdef HAVE_LIBGNUTLS
  190. gnutls_global_deinit();
  191. #endif // HAVE_LIBGNUTLS
  192. #ifdef CARES_HAVE_ARES_LIBRARY_CLEANUP
  193. ares_library_cleanup();
  194. #endif // CARES_HAVE_ARES_LIBRARY_CLEANUP
  195. #ifdef HAVE_LIBSSH2
  196. libssh2_exit();
  197. #endif // HAVE_LIBSSH2
  198. #ifdef HAVE_WINSOCK2_H
  199. WSACleanup();
  200. #endif // HAVE_WINSOCK2_H
  201. // Deletes statically allocated resources. This is done to
  202. // distinguish memory leak from them. This is handy to use
  203. // valgrind.
  204. OptionParser::deleteInstance();
  205. option::deletePrefResource();
  206. return true;
  207. }
  208. bool Platform::isInitialized() { return initialized_; }
  209. } // namespace aria2