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 
19 namespace Aws
20 {
21     static const char* ALLOCATION_TAG = "Aws_Init_Cleanup";
22 
InitAPI(const SDKOptions & options)23     void InitAPI(const SDKOptions &options)
24     {
25 #ifdef USE_AWS_MEMORY_MANAGEMENT
26         if(options.memoryManagementOptions.memoryManager)
27         {
28             Aws::Utils::Memory::InitializeAWSMemorySystem(*options.memoryManagementOptions.memoryManager);
29         }
30 #endif // USE_AWS_MEMORY_MANAGEMENT
31         Aws::InitializeCrt();
32         Aws::Client::CoreErrorsMapper::InitCoreErrorsMapper();
33         if(options.loggingOptions.logLevel != Aws::Utils::Logging::LogLevel::Off)
34         {
35             if(options.loggingOptions.logger_create_fn)
36             {
37                 Aws::Utils::Logging::InitializeAWSLogging(options.loggingOptions.logger_create_fn());
38             }
39             else
40             {
41                 Aws::Utils::Logging::InitializeAWSLogging(
42                         Aws::MakeShared<Aws::Utils::Logging::DefaultLogSystem>(ALLOCATION_TAG, options.loggingOptions.logLevel, options.loggingOptions.defaultLogPrefix));
43             }
44             if(options.loggingOptions.crt_logger_create_fn)
45             {
46                 Aws::Utils::Logging::InitializeCRTLogging(options.loggingOptions.crt_logger_create_fn());
47             }
48             else
49             {
50                 Aws::Utils::Logging::InitializeCRTLogging(
51                         Aws::MakeShared<Aws::Utils::Logging::DefaultCRTLogSystem>(ALLOCATION_TAG, options.loggingOptions.logLevel));
52             }
53             // For users to better debugging in case multiple versions of SDK installed
54             AWS_LOGSTREAM_INFO(ALLOCATION_TAG, "Initiate AWS SDK for C++ with Version:" << Aws::String(Aws::Version::GetVersionString()));
55         }
56 
57         Aws::Config::InitConfigAndCredentialsCacheManager();
58 
59         if (options.ioOptions.clientBootstrap_create_fn)
60         {
61             Aws::SetDefaultClientBootstrap(options.ioOptions.clientBootstrap_create_fn());
62         }
63         else
64         {
65             Aws::Crt::Io::EventLoopGroup eventLoopGroup;
66             Aws::Crt::Io::DefaultHostResolver defaultHostResolver(eventLoopGroup, 8, 30);
67             auto clientBootstrap = Aws::MakeShared<Aws::Crt::Io::ClientBootstrap>(ALLOCATION_TAG, eventLoopGroup, defaultHostResolver);
68             clientBootstrap->EnableBlockingShutdown();
69             Aws::SetDefaultClientBootstrap(clientBootstrap);
70         }
71 
72         if (options.ioOptions.tlsConnectionOptions_create_fn)
73         {
74             Aws::SetDefaultTlsConnectionOptions(options.ioOptions.tlsConnectionOptions_create_fn());
75         }
76         else
77         {
78             Aws::Crt::Io::TlsContextOptions tlsCtxOptions = Aws::Crt::Io::TlsContextOptions::InitDefaultClient();
79             Aws::Crt::Io::TlsContext tlsContext(tlsCtxOptions, Aws::Crt::Io::TlsMode::CLIENT);
80             auto tlsConnectionOptions = Aws::MakeShared<Aws::Crt::Io::TlsConnectionOptions>(ALLOCATION_TAG, tlsContext.NewConnectionOptions());
81             Aws::SetDefaultTlsConnectionOptions(tlsConnectionOptions);
82         }
83 
84         if (options.cryptoOptions.aes_CBCFactory_create_fn)
85         {
86             Aws::Utils::Crypto::SetAES_CBCFactory(options.cryptoOptions.aes_CBCFactory_create_fn());
87         }
88 
89         if(options.cryptoOptions.aes_CTRFactory_create_fn)
90         {
91             Aws::Utils::Crypto::SetAES_CTRFactory(options.cryptoOptions.aes_CTRFactory_create_fn());
92         }
93 
94         if(options.cryptoOptions.aes_GCMFactory_create_fn)
95         {
96             Aws::Utils::Crypto::SetAES_GCMFactory(options.cryptoOptions.aes_GCMFactory_create_fn());
97         }
98 
99         if(options.cryptoOptions.md5Factory_create_fn)
100         {
101             Aws::Utils::Crypto::SetMD5Factory(options.cryptoOptions.md5Factory_create_fn());
102         }
103 
104         if(options.cryptoOptions.sha1Factory_create_fn)
105         {
106             Aws::Utils::Crypto::SetSha1Factory(options.cryptoOptions.sha1Factory_create_fn());
107         }
108 
109         if(options.cryptoOptions.sha256Factory_create_fn)
110         {
111             Aws::Utils::Crypto::SetSha256Factory(options.cryptoOptions.sha256Factory_create_fn());
112         }
113 
114         if(options.cryptoOptions.sha256HMACFactory_create_fn)
115         {
116             Aws::Utils::Crypto::SetSha256HMACFactory(options.cryptoOptions.sha256HMACFactory_create_fn());
117         }
118 
119         if (options.cryptoOptions.aes_KeyWrapFactory_create_fn)
120         {
121             Aws::Utils::Crypto::SetAES_KeyWrapFactory(options.cryptoOptions.aes_KeyWrapFactory_create_fn());
122         }
123 
124         if(options.cryptoOptions.secureRandomFactory_create_fn)
125         {
126             Aws::Utils::Crypto::SetSecureRandomFactory(options.cryptoOptions.secureRandomFactory_create_fn());
127         }
128 
129         Aws::Utils::Crypto::SetInitCleanupOpenSSLFlag(options.cryptoOptions.initAndCleanupOpenSSL);
130         Aws::Utils::Crypto::InitCrypto();
131 
132         if(options.httpOptions.httpClientFactory_create_fn)
133         {
134             Aws::Http::SetHttpClientFactory(options.httpOptions.httpClientFactory_create_fn());
135         }
136 
137         Aws::Http::SetInitCleanupCurlFlag(options.httpOptions.initAndCleanupCurl);
138         Aws::Http::SetInstallSigPipeHandlerFlag(options.httpOptions.installSigPipeHandler);
139         Aws::Http::InitHttp();
140         Aws::InitializeEnumOverflowContainer();
141         cJSON_Hooks hooks;
142         hooks.malloc_fn = [](size_t sz) { return Aws::Malloc("cJSON_Tag", sz); };
143         hooks.free_fn = Aws::Free;
144         cJSON_InitHooks(&hooks);
145         Aws::Net::InitNetwork();
146         Aws::Internal::InitEC2MetadataClient();
147         Aws::Monitoring::InitMonitoring(options.monitoringOptions.customizedMonitoringFactory_create_fn);
148     }
149 
ShutdownAPI(const SDKOptions & options)150     void ShutdownAPI(const SDKOptions& options)
151     {
152         Aws::Monitoring::CleanupMonitoring();
153         Aws::Internal::CleanupEC2MetadataClient();
154         Aws::Net::CleanupNetwork();
155         Aws::CleanupEnumOverflowContainer();
156         Aws::Http::CleanupHttp();
157         Aws::Utils::Crypto::CleanupCrypto();
158 
159         Aws::Config::CleanupConfigAndCredentialsCacheManager();
160 
161         if(options.loggingOptions.logLevel != Aws::Utils::Logging::LogLevel::Off)
162         {
163             Aws::Utils::Logging::ShutdownCRTLogging();
164             Aws::Utils::Logging::ShutdownAWSLogging();
165         }
166 
167         Aws::Client::CoreErrorsMapper::CleanupCoreErrorsMapper();
168         Aws::CleanupCrt();
169 #ifdef USE_AWS_MEMORY_MANAGEMENT
170         if(options.memoryManagementOptions.memoryManager)
171         {
172             Aws::Utils::Memory::ShutdownAWSMemorySystem();
173         }
174 #endif // USE_AWS_MEMORY_MANAGEMENT
175     }
176 }
177