Aws.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/core/Version.h>
  6. #include <aws/core/utils/logging/LogMacros.h>
  7. #include <aws/core/Aws.h>
  8. #include <aws/core/client/CoreErrors.h>
  9. #include <aws/core/utils/logging/AWSLogging.h>
  10. #include <aws/core/utils/logging/CRTLogging.h>
  11. #include <aws/core/utils/logging/DefaultLogSystem.h>
  12. #include <aws/core/Globals.h>
  13. #include <aws/core/external/cjson/cJSON.h>
  14. #include <aws/core/monitoring/MonitoringManager.h>
  15. #include <aws/core/net/Net.h>
  16. #include <aws/core/config/AWSProfileConfigLoader.h>
  17. #include <aws/core/internal/AWSHttpResourceClient.h>
  18. namespace Aws
  19. {
  20. static const char* ALLOCATION_TAG = "Aws_Init_Cleanup";
  21. void InitAPI(const SDKOptions &options)
  22. {
  23. #ifdef USE_AWS_MEMORY_MANAGEMENT
  24. if(options.memoryManagementOptions.memoryManager)
  25. {
  26. Aws::Utils::Memory::InitializeAWSMemorySystem(*options.memoryManagementOptions.memoryManager);
  27. }
  28. #endif // USE_AWS_MEMORY_MANAGEMENT
  29. Aws::InitializeCrt();
  30. Aws::Client::CoreErrorsMapper::InitCoreErrorsMapper();
  31. if(options.loggingOptions.logLevel != Aws::Utils::Logging::LogLevel::Off)
  32. {
  33. if(options.loggingOptions.logger_create_fn)
  34. {
  35. Aws::Utils::Logging::InitializeAWSLogging(options.loggingOptions.logger_create_fn());
  36. }
  37. else
  38. {
  39. Aws::Utils::Logging::InitializeAWSLogging(
  40. Aws::MakeShared<Aws::Utils::Logging::DefaultLogSystem>(ALLOCATION_TAG, options.loggingOptions.logLevel, options.loggingOptions.defaultLogPrefix));
  41. }
  42. if(options.loggingOptions.crt_logger_create_fn)
  43. {
  44. Aws::Utils::Logging::InitializeCRTLogging(options.loggingOptions.crt_logger_create_fn());
  45. }
  46. else
  47. {
  48. Aws::Utils::Logging::InitializeCRTLogging(
  49. Aws::MakeShared<Aws::Utils::Logging::DefaultCRTLogSystem>(ALLOCATION_TAG, options.loggingOptions.logLevel));
  50. }
  51. // For users to better debugging in case multiple versions of SDK installed
  52. AWS_LOGSTREAM_INFO(ALLOCATION_TAG, "Initiate AWS SDK for C++ with Version:" << Aws::String(Aws::Version::GetVersionString()));
  53. }
  54. Aws::Config::InitConfigAndCredentialsCacheManager();
  55. if (options.ioOptions.clientBootstrap_create_fn)
  56. {
  57. Aws::SetDefaultClientBootstrap(options.ioOptions.clientBootstrap_create_fn());
  58. }
  59. else
  60. {
  61. Aws::Crt::Io::EventLoopGroup eventLoopGroup;
  62. Aws::Crt::Io::DefaultHostResolver defaultHostResolver(eventLoopGroup, 8, 30);
  63. auto clientBootstrap = Aws::MakeShared<Aws::Crt::Io::ClientBootstrap>(ALLOCATION_TAG, eventLoopGroup, defaultHostResolver);
  64. clientBootstrap->EnableBlockingShutdown();
  65. Aws::SetDefaultClientBootstrap(clientBootstrap);
  66. }
  67. if (options.ioOptions.tlsConnectionOptions_create_fn)
  68. {
  69. Aws::SetDefaultTlsConnectionOptions(options.ioOptions.tlsConnectionOptions_create_fn());
  70. }
  71. else
  72. {
  73. Aws::Crt::Io::TlsContextOptions tlsCtxOptions = Aws::Crt::Io::TlsContextOptions::InitDefaultClient();
  74. Aws::Crt::Io::TlsContext tlsContext(tlsCtxOptions, Aws::Crt::Io::TlsMode::CLIENT);
  75. auto tlsConnectionOptions = Aws::MakeShared<Aws::Crt::Io::TlsConnectionOptions>(ALLOCATION_TAG, tlsContext.NewConnectionOptions());
  76. Aws::SetDefaultTlsConnectionOptions(tlsConnectionOptions);
  77. }
  78. if (options.cryptoOptions.aes_CBCFactory_create_fn)
  79. {
  80. Aws::Utils::Crypto::SetAES_CBCFactory(options.cryptoOptions.aes_CBCFactory_create_fn());
  81. }
  82. if(options.cryptoOptions.aes_CTRFactory_create_fn)
  83. {
  84. Aws::Utils::Crypto::SetAES_CTRFactory(options.cryptoOptions.aes_CTRFactory_create_fn());
  85. }
  86. if(options.cryptoOptions.aes_GCMFactory_create_fn)
  87. {
  88. Aws::Utils::Crypto::SetAES_GCMFactory(options.cryptoOptions.aes_GCMFactory_create_fn());
  89. }
  90. if(options.cryptoOptions.md5Factory_create_fn)
  91. {
  92. Aws::Utils::Crypto::SetMD5Factory(options.cryptoOptions.md5Factory_create_fn());
  93. }
  94. if(options.cryptoOptions.sha1Factory_create_fn)
  95. {
  96. Aws::Utils::Crypto::SetSha1Factory(options.cryptoOptions.sha1Factory_create_fn());
  97. }
  98. if(options.cryptoOptions.sha256Factory_create_fn)
  99. {
  100. Aws::Utils::Crypto::SetSha256Factory(options.cryptoOptions.sha256Factory_create_fn());
  101. }
  102. if(options.cryptoOptions.sha256HMACFactory_create_fn)
  103. {
  104. Aws::Utils::Crypto::SetSha256HMACFactory(options.cryptoOptions.sha256HMACFactory_create_fn());
  105. }
  106. if (options.cryptoOptions.aes_KeyWrapFactory_create_fn)
  107. {
  108. Aws::Utils::Crypto::SetAES_KeyWrapFactory(options.cryptoOptions.aes_KeyWrapFactory_create_fn());
  109. }
  110. if(options.cryptoOptions.secureRandomFactory_create_fn)
  111. {
  112. Aws::Utils::Crypto::SetSecureRandomFactory(options.cryptoOptions.secureRandomFactory_create_fn());
  113. }
  114. Aws::Utils::Crypto::SetInitCleanupOpenSSLFlag(options.cryptoOptions.initAndCleanupOpenSSL);
  115. Aws::Utils::Crypto::InitCrypto();
  116. if(options.httpOptions.httpClientFactory_create_fn)
  117. {
  118. Aws::Http::SetHttpClientFactory(options.httpOptions.httpClientFactory_create_fn());
  119. }
  120. Aws::Http::SetInitCleanupCurlFlag(options.httpOptions.initAndCleanupCurl);
  121. Aws::Http::SetInstallSigPipeHandlerFlag(options.httpOptions.installSigPipeHandler);
  122. Aws::Http::SetCompliantRfc3986Encoding(options.httpOptions.compliantRfc3986Encoding);
  123. Aws::Http::InitHttp();
  124. Aws::InitializeEnumOverflowContainer();
  125. cJSON_AS4CPP_Hooks hooks;
  126. hooks.malloc_fn = [](size_t sz) { return Aws::Malloc("cJSON_AS4CPP_Tag", sz); };
  127. hooks.free_fn = Aws::Free;
  128. cJSON_AS4CPP_InitHooks(&hooks);
  129. Aws::Net::InitNetwork();
  130. Aws::Internal::InitEC2MetadataClient();
  131. Aws::Monitoring::InitMonitoring(options.monitoringOptions.customizedMonitoringFactory_create_fn);
  132. }
  133. void ShutdownAPI(const SDKOptions& options)
  134. {
  135. Aws::Monitoring::CleanupMonitoring();
  136. Aws::Internal::CleanupEC2MetadataClient();
  137. Aws::Net::CleanupNetwork();
  138. Aws::CleanupEnumOverflowContainer();
  139. Aws::Http::CleanupHttp();
  140. Aws::Utils::Crypto::CleanupCrypto();
  141. Aws::Config::CleanupConfigAndCredentialsCacheManager();
  142. Aws::Client::CoreErrorsMapper::CleanupCoreErrorsMapper();
  143. Aws::CleanupCrt();
  144. if (options.loggingOptions.logLevel != Aws::Utils::Logging::LogLevel::Off)
  145. {
  146. Aws::Utils::Logging::ShutdownCRTLogging();
  147. Aws::Utils::Logging::ShutdownAWSLogging();
  148. }
  149. #ifdef USE_AWS_MEMORY_MANAGEMENT
  150. if(options.memoryManagementOptions.memoryManager)
  151. {
  152. Aws::Utils::Memory::ShutdownAWSMemorySystem();
  153. }
  154. #endif // USE_AWS_MEMORY_MANAGEMENT
  155. }
  156. }