| 1 | /* |
|---|---|
| 2 | * Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | * You may not use this file except in compliance with the License. |
| 6 | * A copy of the License is located at |
| 7 | * |
| 8 | * http://aws.amazon.com/apache2.0 |
| 9 | * |
| 10 | * or in the "license" file accompanying this file. This file is distributed |
| 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | * express or implied. See the License for the specific language governing |
| 13 | * permissions and limitations under the License. |
| 14 | */ |
| 15 | #include <aws/core/Version.h> |
| 16 | #include <aws/core/utils/logging/LogMacros.h> |
| 17 | #include <aws/core/Aws.h> |
| 18 | #include <aws/core/client/CoreErrors.h> |
| 19 | #include <aws/core/utils/logging/AWSLogging.h> |
| 20 | #include <aws/core/utils/logging/DefaultLogSystem.h> |
| 21 | #include <aws/core/Globals.h> |
| 22 | #include <aws/core/external/cjson/cJSON.h> |
| 23 | #include <aws/core/monitoring/MonitoringManager.h> |
| 24 | #include <aws/core/net/Net.h> |
| 25 | #include <aws/core/config/AWSProfileConfigLoader.h> |
| 26 | |
| 27 | namespace Aws |
| 28 | { |
| 29 | static const char* ALLOCATION_TAG = "Aws_Init_Cleanup"; |
| 30 | |
| 31 | void InitAPI(const SDKOptions &options) |
| 32 | { |
| 33 | #ifdef USE_AWS_MEMORY_MANAGEMENT |
| 34 | if(options.memoryManagementOptions.memoryManager) |
| 35 | { |
| 36 | Aws::Utils::Memory::InitializeAWSMemorySystem(*options.memoryManagementOptions.memoryManager); |
| 37 | } |
| 38 | #endif // USE_AWS_MEMORY_MANAGEMENT |
| 39 | Aws::Client::CoreErrorsMapper::InitCoreErrorsMapper(); |
| 40 | if(options.loggingOptions.logLevel != Aws::Utils::Logging::LogLevel::Off) |
| 41 | { |
| 42 | if(options.loggingOptions.logger_create_fn) |
| 43 | { |
| 44 | Aws::Utils::Logging::InitializeAWSLogging(options.loggingOptions.logger_create_fn()); |
| 45 | } |
| 46 | else |
| 47 | { |
| 48 | Aws::Utils::Logging::InitializeAWSLogging( |
| 49 | Aws::MakeShared<Aws::Utils::Logging::DefaultLogSystem>(ALLOCATION_TAG, options.loggingOptions.logLevel, options.loggingOptions.defaultLogPrefix)); |
| 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 | |
| 55 | Aws::Config::InitConfigAndCredentialsCacheManager(); |
| 56 | |
| 57 | if (options.cryptoOptions.aes_CBCFactory_create_fn) |
| 58 | { |
| 59 | Aws::Utils::Crypto::SetAES_CBCFactory(options.cryptoOptions.aes_CBCFactory_create_fn()); |
| 60 | } |
| 61 | |
| 62 | if(options.cryptoOptions.aes_CTRFactory_create_fn) |
| 63 | { |
| 64 | Aws::Utils::Crypto::SetAES_CTRFactory(options.cryptoOptions.aes_CTRFactory_create_fn()); |
| 65 | } |
| 66 | |
| 67 | if(options.cryptoOptions.aes_GCMFactory_create_fn) |
| 68 | { |
| 69 | Aws::Utils::Crypto::SetAES_GCMFactory(options.cryptoOptions.aes_GCMFactory_create_fn()); |
| 70 | } |
| 71 | |
| 72 | if(options.cryptoOptions.md5Factory_create_fn) |
| 73 | { |
| 74 | Aws::Utils::Crypto::SetMD5Factory(options.cryptoOptions.md5Factory_create_fn()); |
| 75 | } |
| 76 | |
| 77 | if(options.cryptoOptions.sha256Factory_create_fn) |
| 78 | { |
| 79 | Aws::Utils::Crypto::SetSha256Factory(options.cryptoOptions.sha256Factory_create_fn()); |
| 80 | } |
| 81 | |
| 82 | if(options.cryptoOptions.sha256HMACFactory_create_fn) |
| 83 | { |
| 84 | Aws::Utils::Crypto::SetSha256HMACFactory(options.cryptoOptions.sha256HMACFactory_create_fn()); |
| 85 | } |
| 86 | |
| 87 | if (options.cryptoOptions.aes_KeyWrapFactory_create_fn) |
| 88 | { |
| 89 | Aws::Utils::Crypto::SetAES_KeyWrapFactory(options.cryptoOptions.aes_KeyWrapFactory_create_fn()); |
| 90 | } |
| 91 | |
| 92 | if(options.cryptoOptions.secureRandomFactory_create_fn) |
| 93 | { |
| 94 | Aws::Utils::Crypto::SetSecureRandomFactory(options.cryptoOptions.secureRandomFactory_create_fn()); |
| 95 | } |
| 96 | |
| 97 | Aws::Utils::Crypto::SetInitCleanupOpenSSLFlag(options.cryptoOptions.initAndCleanupOpenSSL); |
| 98 | Aws::Utils::Crypto::InitCrypto(); |
| 99 | |
| 100 | if(options.httpOptions.httpClientFactory_create_fn) |
| 101 | { |
| 102 | Aws::Http::SetHttpClientFactory(options.httpOptions.httpClientFactory_create_fn()); |
| 103 | } |
| 104 | |
| 105 | Aws::Http::SetInitCleanupCurlFlag(options.httpOptions.initAndCleanupCurl); |
| 106 | Aws::Http::SetInstallSigPipeHandlerFlag(options.httpOptions.installSigPipeHandler); |
| 107 | Aws::Http::InitHttp(); |
| 108 | Aws::InitializeEnumOverflowContainer(); |
| 109 | cJSON_Hooks hooks; |
| 110 | hooks.malloc_fn = [](size_t sz) { return Aws::Malloc("cJSON_Tag", sz); }; |
| 111 | hooks.free_fn = Aws::Free; |
| 112 | cJSON_InitHooks(&hooks); |
| 113 | Aws::Net::InitNetwork(); |
| 114 | Aws::Monitoring::InitMonitoring(options.monitoringOptions.customizedMonitoringFactory_create_fn); |
| 115 | } |
| 116 | |
| 117 | void ShutdownAPI(const SDKOptions& options) |
| 118 | { |
| 119 | Aws::Monitoring::CleanupMonitoring(); |
| 120 | Aws::Net::CleanupNetwork(); |
| 121 | Aws::CleanupEnumOverflowContainer(); |
| 122 | Aws::Http::CleanupHttp(); |
| 123 | Aws::Utils::Crypto::CleanupCrypto(); |
| 124 | |
| 125 | Aws::Config::CleanupConfigAndCredentialsCacheManager(); |
| 126 | |
| 127 | if(options.loggingOptions.logLevel != Aws::Utils::Logging::LogLevel::Off) |
| 128 | { |
| 129 | Aws::Utils::Logging::ShutdownAWSLogging(); |
| 130 | } |
| 131 | |
| 132 | Aws::Client::CoreErrorsMapper::CleanupCoreErrorsMapper(); |
| 133 | |
| 134 | #ifdef USE_AWS_MEMORY_MANAGEMENT |
| 135 | if(options.memoryManagementOptions.memoryManager) |
| 136 | { |
| 137 | Aws::Utils::Memory::ShutdownAWSMemorySystem(); |
| 138 | } |
| 139 | #endif // USE_AWS_MEMORY_MANAGEMENT |
| 140 | } |
| 141 | } |
| 142 |