core_configuration.c
Go to the documentation of this file.
1 
10 #include "core_configuration.h"
11 #include "definitions.h"
13 #include "mcl_core/mcl_assert.h"
14 #include "mcl_core/mcl_memory.h"
15 #include "string_util.h"
16 
17 #define USER_AGENT_HEADER_FORMAT "MCL/" MCL_VERSION_STRING " (%s)"
18 
20 {
21  mcl_error_t code = MCL_OK;
22 
23  MCL_DEBUG_ENTRY("mcl_core_configuration_t **configuration = <%p>", configuration);
24 
25  // Null check.
26  MCL_ASSERT_NOT_NULL(configuration, code);
27 
28  // Allocate memory for configuration handle.
29  MCL_ASSERT_CODE_MESSAGE(MCL_NULL != MCL_NEW(*configuration), MCL_OUT_OF_MEMORY, "Not enough memory to allocate new configuration.");
30 
31  (*configuration)->mindsphere_hostname = MCL_NULL;
32  (*configuration)->mindsphere_port = 0;
33  (*configuration)->mindsphere_certificate = MCL_NULL;
34  (*configuration)->proxy_hostname = MCL_NULL;
35  (*configuration)->proxy_port = 0;
36  (*configuration)->proxy_type = MCL_PROXY_UNKNOWN;
37  (*configuration)->proxy_username = MCL_NULL;
38  (*configuration)->proxy_password = MCL_NULL;
39  (*configuration)->proxy_domain = MCL_NULL;
40  (*configuration)->security_profile = MCL_SECURITY_SHARED_SECRET;
41  (*configuration)->http_request_timeout = DEFAULT_HTTP_REQUEST_TIMEOUT;
42  (*configuration)->user_agent = MCL_NULL;
43  (*configuration)->initial_access_token = MCL_NULL;
44  (*configuration)->tenant = MCL_NULL;
45  (*configuration)->credentials_load_callback.rsa = MCL_NULL;
46  (*configuration)->credentials_save_callback.rsa = MCL_NULL;
47  (*configuration)->critical_section_enter_callback = MCL_NULL;
48  (*configuration)->critical_section_leave_callback = MCL_NULL;
49  (*configuration)->register_endpoint = MCL_NULL;
50  (*configuration)->token_endpoint = MCL_NULL;
51 
53  MCL_DEBUG_LEAVE("retVal = <%d>", code);
54  return code;
55 }
56 
58 {
59  mcl_error_t code = MCL_OK;
60  mcl_size_t user_agent_length = 0;
61 
62  MCL_DEBUG_ENTRY("mcl_core_configuration_t *configuration = <%p>, E_MCL_CORE_CONFIGURATION_PARAMETER parameter = <%d>, const void *value = <%p>",
63  configuration, parameter, value);
64 
65  // Null check.
66  MCL_ASSERT_NOT_NULL(configuration, code);
67  MCL_ASSERT_NOT_NULL(value, code);
68 
69  // Set related parameter.
70  switch (parameter)
71  {
73  code = mcl_string_util_reset(value, &configuration->mindsphere_hostname);
74  break;
75 
77  configuration->mindsphere_port = *((mcl_uint16_t *) value);
78  break;
79 
81  code = mcl_string_util_reset(value, &configuration->mindsphere_certificate);
82  configuration->certificate_is_file = MCL_FALSE;
83  break;
84 
86 #if HAVE_FILE_SYSTEM_
87  code = mcl_string_util_reset(value, &configuration->mindsphere_certificate);
88  configuration->certificate_is_file = MCL_TRUE;
89 #else
90  code = MCL_NO_FILE_SUPPORT;
91 #endif
92  break;
93 
95 
96  // Validate proxy type.
98  "Invalid proxy type value.");
99  configuration->proxy_type = *(E_MCL_PROXY *) value;
100  break;
101 
103  code = mcl_string_util_reset(value, &configuration->proxy_hostname);
104  break;
105 
107  configuration->proxy_port = *((mcl_uint16_t *) value);
108  break;
109 
111  code = mcl_string_util_reset(value, &configuration->proxy_username);
112  break;
113 
115  code = mcl_string_util_reset(value, &configuration->proxy_password);
116  break;
117 
119  code = mcl_string_util_reset(value, &configuration->proxy_domain);
120  break;
121 
123 
124  // Validate security profile.
126  MCL_SECURITY_PROFILE_END > *(E_MCL_SECURITY_PROFILE *) value, MCL_INVALID_PARAMETER, "Invalid security profile value.");
127  configuration->security_profile = *(E_MCL_SECURITY_PROFILE *) value;
128  break;
129 
131 
132  // This is an obsolete parameter. Kept for backward compatibility.
133  break;
134 
136  configuration->http_request_timeout = *((mcl_uint32_t *) value);
137  break;
138 
140 
141  // Create User-Agent http header.
142  // Example MCL/4.0.0 (Custom Agent v1.0)
143  MCL_FREE(configuration->user_agent);
144  user_agent_length += 4 + (sizeof(MCL_VERSION_STRING) - 1) + 3 + mcl_string_util_strlen((const char *) value);
145  configuration->user_agent = MCL_MALLOC(user_agent_length + 1);
146 
147  if (MCL_NULL != configuration->user_agent)
148  {
149  code = mcl_string_util_snprintf(configuration->user_agent, user_agent_length + MCL_NULL_CHAR_SIZE, USER_AGENT_HEADER_FORMAT, value);
150 
151  if (MCL_OK != code)
152  {
153  MCL_FREE(configuration->user_agent);
154  }
155  }
156  else
157  {
158  code = MCL_OUT_OF_MEMORY;
159  }
160  break;
161 
163  code = mcl_string_util_reset(value, &configuration->tenant);
164  break;
165 
167  code = mcl_string_util_reset(value, &configuration->initial_access_token);
168  break;
169 
171  configuration->credentials_load_callback.rsa = (mcl_credentials_load_rsa_callback_t) value;
172  break;
173 
175  configuration->credentials_save_callback.rsa = (mcl_credentials_save_rsa_callback_t) value;
176  break;
177 
179  configuration->critical_section_enter_callback = (mcl_critical_section_enter_callback_t) value;
180  break;
181 
183  configuration->critical_section_leave_callback = (mcl_critical_section_leave_callback_t) value;
184  break;
185 
186  default:
187  code = MCL_INVALID_PARAMETER;
188  break;
189  }
190 
192  MCL_DEBUG_LEAVE("retVal = <%d>", code);
193  return code;
194 }
195 
197 {
198  mcl_error_t code = MCL_OK;
199  mcl_size_t host_name_length = 0;
200  mcl_size_t proxy_host_length = 0;
201  mcl_size_t proxy_user_name_length = 0;
202  mcl_size_t proxy_password_length = 0;
203  mcl_size_t proxy_domain_length = 0;
204  mcl_size_t user_agent_length = 0;
205 
206  MCL_DEBUG_ENTRY("core_configuration_t *configuration = <%p>", configuration);
207 
208  // Validate host name.
210  host_name_length = string_util_strnlen(configuration->mindsphere_hostname, MAXIMUM_HOST_NAME_LENGTH + 1);
211  MCL_ASSERT_CODE((0 < host_name_length) && (MAXIMUM_HOST_NAME_LENGTH >= host_name_length), MCL_INVALID_PARAMETER);
212 
213  // Check if proxy is used but do not return error if not used.
214  if (MCL_NULL != configuration->proxy_hostname)
215  {
216  // Validate proxy host name.
217  proxy_host_length = string_util_strnlen(configuration->proxy_hostname, MAXIMUM_HOST_NAME_LENGTH + 1);
218  MCL_ASSERT_CODE((0 < proxy_host_length) && (MAXIMUM_HOST_NAME_LENGTH >= proxy_host_length), MCL_INVALID_PARAMETER);
219 
220  // Check if user name is provided but do not return error if not.
221  if (MCL_NULL != configuration->proxy_username)
222  {
223  // Validate proxy user name.
224  proxy_user_name_length = string_util_strnlen(configuration->proxy_username, MAXIMUM_PROXY_USER_NAME_LENGTH + 1);
225  MCL_ASSERT_CODE((0 < proxy_user_name_length) && (MAXIMUM_PROXY_USER_NAME_LENGTH >= proxy_user_name_length), MCL_INVALID_PARAMETER);
226 
227  // Validate proxy password.
229  proxy_password_length = string_util_strnlen(configuration->proxy_password, MAXIMUM_PROXY_PASSWORD_LENGTH + 1);
230  MCL_ASSERT_CODE((0 < proxy_password_length) && (MAXIMUM_PROXY_PASSWORD_LENGTH >= proxy_password_length), MCL_INVALID_PARAMETER);
231 
232  // Validate proxy domain.
233  if (MCL_NULL != configuration->proxy_domain)
234  {
235  proxy_domain_length = string_util_strnlen(configuration->proxy_domain, MAXIMUM_PROXY_DOMAIN_LENGTH + 1);
236  MCL_ASSERT_CODE((0 < proxy_domain_length) && (MAXIMUM_PROXY_DOMAIN_LENGTH >= proxy_domain_length), MCL_INVALID_PARAMETER);
237  }
238  }
239  }
240 
241  // Validate User-Agent.
243  user_agent_length = string_util_strnlen(configuration->user_agent, MAXIMUM_USER_AGENT_LENGTH + 1);
244  MCL_ASSERT_CODE((0 < user_agent_length) && (MAXIMUM_USER_AGENT_LENGTH >= user_agent_length), MCL_INVALID_PARAMETER);
245 
246  // Validate Tenant.
248 
249  MCL_DEBUG_LEAVE("retVal = <%d>", code);
250  return code;
251 }
252 
254 {
255  MCL_DEBUG_ENTRY("mcl_core_configuration_t **configuration = <%p>", configuration);
256 
257  // Free configuration structure.
258  if ((MCL_NULL != configuration) && (MCL_NULL != *configuration))
259  {
260  MCL_FREE((*configuration)->mindsphere_hostname);
261  MCL_FREE((*configuration)->mindsphere_certificate);
262  MCL_FREE((*configuration)->proxy_hostname);
263  MCL_FREE((*configuration)->proxy_username);
264  MCL_FREE((*configuration)->proxy_password);
265  MCL_FREE((*configuration)->proxy_domain);
266  MCL_FREE((*configuration)->user_agent);
267  MCL_FREE((*configuration)->initial_access_token);
268  MCL_FREE((*configuration)->tenant);
269  MCL_FREE((*configuration)->register_endpoint);
270  MCL_FREE((*configuration)->token_endpoint);
271  MCL_FREE(*configuration);
272  }
273 
274  MCL_DEBUG_LEAVE("retVal = <%d>", MCL_OK);
275 }
276 
278 {
279  MCL_DEBUG_ENTRY("core_configuration_t *configuration = <%p>", configuration);
280 
281  // This function will be called in success case, no need to check mandatory fields.
282  MCL_INFO("Mindsphere Hostname: %s.", configuration->mindsphere_hostname);
283  MCL_INFO("Mindsphere Port: %u.", configuration->mindsphere_port);
284 
285  if (MCL_NULL != configuration->mindsphere_certificate)
286  {
287  MCL_INFO("Mindsphere Certificate: Provided.");
288  }
289  else
290  {
291  MCL_INFO("Mindsphere Certificate: Not provided.");
292  }
293 
294  if (MCL_NULL != configuration->proxy_hostname)
295  {
296  MCL_INFO("Proxy Hostname: %s.", configuration->proxy_hostname);
297  MCL_INFO("Proxy Port: %u.", configuration->proxy_port);
298 
299  switch (configuration->proxy_type)
300  {
301  case MCL_PROXY_HTTP:
302  MCL_INFO("Proxy Type: MCL_PROXY_HTTP.");
303  break;
304 
305  case MCL_PROXY_HTTP_1_0:
306  MCL_INFO("Proxy Type: MCL_PROXY_HTTP_1_0.");
307  break;
308 
309  case MCL_PROXY_SOCKS4:
310  MCL_INFO("Proxy Type: MCL_PROXY_SOCKS4.");
311  break;
312 
313  case MCL_PROXY_SOCKS5:
314  MCL_INFO("Proxy Type: MCL_PROXY_SOCKS5.");
315  break;
316 
317  case MCL_PROXY_SOCKS4A:
318  MCL_INFO("Proxy Type: MCL_PROXY_SOCKS4A.");
319  break;
320 
322  MCL_INFO("Proxy Type: MCL_PROXY_SOCKS5_HOSTNAME.");
323  break;
324 
325  default:
326  MCL_INFO("Proxy Type: MCL_PROXY_UNKNOWN.");
327  break;
328  }
329 
330  if (MCL_NULL != configuration->proxy_username)
331  {
332  MCL_INFO("Proxy Username: %s.", configuration->proxy_username);
333  MCL_INFO("Proxy Password: %s.", configuration->proxy_password);
334 
335  if (MCL_NULL != configuration->proxy_domain)
336  {
337  MCL_INFO("Proxy Domain: %s.", configuration->proxy_domain);
338  }
339  }
340  }
341  else
342  {
343  MCL_INFO("Proxy: Not used.");
344  }
345 
346  if (MCL_SECURITY_SHARED_SECRET == configuration->security_profile)
347  {
348  MCL_INFO("Security Profile: MCL_SECURITY_SHARED_SECRET.");
349  }
350  // Invalid parameter check is already done.
351  else
352  {
353  MCL_INFO("Security Profile: MCL_SECURITY_RSA_3072.");
354  }
355 
356  MCL_INFO("HTTP Request Timeout: %u seconds.", configuration->http_request_timeout);
357  MCL_INFO("User Agent: %s.", configuration->user_agent);
358 
359  if (MCL_NULL != configuration->initial_access_token)
360  {
361  MCL_INFO("Initial Access Token: Provided.");
362  }
363  else
364  {
365  MCL_INFO("Initial Access Token: Not provided.");
366  }
367 
368  MCL_INFO("Tenant: %s.", configuration->tenant);
369 
370  if ((MCL_NULL != configuration->credentials_load_callback.rsa) && (MCL_NULL != configuration->credentials_save_callback.rsa))
371  {
372  MCL_INFO("Security Information: Callback functions will be used.");
373  }
374  else
375  {
376  MCL_INFO("Security Information: Will not be saved.");
377  }
378 
379  if ((MCL_NULL != configuration->critical_section_enter_callback) && (MCL_NULL != configuration->critical_section_leave_callback))
380  {
381  MCL_INFO("Critical Section: Callback functions are provided.");
382  }
383  else
384  {
385  MCL_INFO("Critical Section: Callback functions are not provided.");
386  }
387 
388  MCL_DEBUG_LEAVE("retVal = void.");
389 }
End of proxy codes.
mcl_error_t(* mcl_critical_section_enter_callback_t)(void)
mcl_credentials_save_rsa_callback_t rsa
Callback type to save RSA key.
E_MCL_PROXY
#define MCL_FUNCTION_LEAVE_LABEL
Unknown proxy.
uint16_t mcl_uint16_t
Mindsphere port parameter as mcl_uint16_t.
Custom function for entering critical section parameter as mcl_critical_section_enter_callback_t (opt...
char * proxy_hostname
Proxy hostname. Optional.
size_t mcl_size_t
void core_configuration_log(core_configuration_t *configuration)
mcl_size_t string_util_strnlen(const char *buffer, mcl_size_t maximum_length)
Definition: string_util.c:59
Assert module header file.
The system does not support file handling.
Success.
mcl_uint16_t proxy_port
Proxy port number. Mandatory if proxy host name is set, ineffective otherwise.
#define DEFAULT_HTTP_REQUEST_TIMEOUT
Definition: definitions.h:31
Custom function for loading credentials parameter as mcl_credentials_load_callback_t.
MCL_CORE_EXPORT mcl_error_t mcl_string_util_reset(const void *value, char **target)
Definition: string_util.c:414
void mcl_core_configuration_destroy(mcl_core_configuration_t **configuration)
mcl_int32_t mcl_error_t
mcl_uint16_t mindsphere_port
Mindsphere port number.
mcl_credentials_load_callback_t credentials_load_callback
Custom function for loading credentials. If both credentials_load_callback and credentials_save_callb...
Proxy port number parameter as mcl_uint16_t. Mandatory if proxy host name is set, ineffective otherwi...
#define MCL_DEBUG_ENTRY(...)
Definition: mcl_log_util.h:115
Security profile parameter as E_MCL_SECURITY_PROFILE.
SOCKS5 proxy.
Custom function for leaving critical section parameter as mcl_critical_section_leave_callback_t (opti...
uint32_t mcl_uint32_t
mcl_credentials_save_callback_t credentials_save_callback
Custom function for saving credentials. If both credentials_load_callback and credentials_save_callba...
#define MCL_FALSE
End of security profile codes.
#define USER_AGENT_HEADER_FORMAT
String utility module header file.
MCL_CORE_EXPORT mcl_size_t mcl_string_util_strlen(const char *buffer)
Definition: string_util.c:23
mcl_credentials_load_rsa_callback_t rsa
Callback type to load RSA key.
E_MCL_CORE_CONFIGURATION_PARAMETER
mcl_error_t core_configuration_validate(core_configuration_t *configuration)
#define MCL_ASSERT_CODE_MESSAGE(condition, return_code,...)
Definition: mcl_assert.h:77
char * tenant
Tenant name.
#define MCL_NEW(p)
Definition: mcl_memory.h:55
#define MCL_NULL
struct mcl_core_configuration_t mcl_core_configuration_t
char * user_agent
User agent.
HTTP request timeout (in seconds) parameter as mcl_uint32_t. Default timeout is 300 seconds...
void(* mcl_critical_section_leave_callback_t)(void)
E_MCL_SECURITY_PROFILE security_profile
Security profile E_MCL_SECURITY_PROFILE.
#define MCL_FREE(p)
Definition: mcl_memory.h:59
SOCKS4 proxy.
mcl_uint32_t http_request_timeout
Timeout value (in seconds) for HTTP requests. Default timeout is 300 seconds.
Initial access token parameter as char*.
Definitions module header file.
Proxy host name parameter as char* (optional).
Core configuration module header file.
mcl_error_t mcl_core_configuration_set_parameter(mcl_core_configuration_t *configuration, E_MCL_CORE_CONFIGURATION_PARAMETER parameter, const void *value)
#define MAXIMUM_HOST_NAME_LENGTH
Definition: definitions.h:24
char * proxy_username
Proxy username. Optional if proxy host name is set, ineffective otherwise.
#define MCL_ASSERT_NOT_NULL(argument, return_variable)
Definition: mcl_assert.h:38
SOCKS5 hostname proxy.
E_MCL_PROXY proxy_type
Proxy type E_MCL_PROXY. Mandatory if proxy host name is set, ineffective otherwise.
Proxy domain parameter as char*. Optional if proxy host name and proxy username are set...
SOCKS4a proxy.
MCL_CORE_EXPORT mcl_error_t mcl_string_util_snprintf(char *string, mcl_size_t length, const char *format,...)
Definition: string_util.c:158
#define MCL_ASSERT_CODE(condition, return_code)
Definition: mcl_assert.h:69
mcl_critical_section_leave_callback_t critical_section_leave_callback
Custom function for leaving critical section (optional, default is NULL).
This is an obsolete parameter. Setting it will have no effect.
char * proxy_domain
Proxy domain. Optional if proxy host name and proxy username are set, ineffective otherwise...
Custom function for saving credentials parameter as mcl_credentials_save_callback_t.
Http 1.0 proxy.
#define MAXIMUM_PROXY_DOMAIN_LENGTH
Definition: definitions.h:27
mcl_error_t mcl_core_configuration_initialize(mcl_core_configuration_t **configuration)
Http proxy.
String utility module interface header file.
#define MCL_VERSION_STRING
Definition: mcl_version.h:17
Mindsphere certificate file parameter as char*. If certificate is not set (as itself or as file)...
mcl_error_t(* mcl_credentials_save_rsa_callback_t)(const char *client_id, const char *public_key, const char *private_key, const char *registration_access_token, const char *registration_uri)
Proxy password parameter as char*. Mandatory if proxy host name and proxy username are set...
Memory allocation fail.
#define MCL_NULL_CHAR_SIZE
#define MAXIMUM_PROXY_PASSWORD_LENGTH
Definition: definitions.h:26
Mindsphere hostname parameter as char*.
#define MCL_MALLOC(bytes)
Definition: mcl_memory.h:54
char * initial_access_token
Initial access token.
General invalid parameter fail.
#define MCL_DEBUG_LEAVE(...)
Definition: mcl_log_util.h:116
char * mindsphere_certificate
Mindsphere certificate. Optional. If NULL, MCL will use default CA certificate store (if provided at ...
#define MCL_TRUE
Mindsphere certificate parameter as char*. If certificate is not set (as itself or as file)...
Proxy type parameter as E_MCL_PROXY. Mandatory if proxy host name is set, ineffective otherwise...
#define MAXIMUM_USER_AGENT_LENGTH
Definition: definitions.h:28
#define MAXIMUM_PROXY_USER_NAME_LENGTH
Definition: definitions.h:25
char * proxy_password
Proxy password. Mandatory if proxy host name and proxy username are set, ineffective otherwise...
#define MCL_INFO(...)
Definition: mcl_log_util.h:126
Proxy username parameter as char*. Optional if proxy host name is set, ineffective otherwise...
mcl_critical_section_enter_callback_t critical_section_enter_callback
Custom function for entering critical section (optional, default is NULL).
char * mindsphere_hostname
Mindsphere hostname.
mcl_error_t(* mcl_credentials_load_rsa_callback_t)(char **client_id, char **public_key, char **private_key, char **registration_access_token, char **registration_uri)
E_MCL_SECURITY_PROFILE
Core configuration module interface header file.
Memory module interface header file.