static int save_strings(OUTPROCESS_HANDLE_DATA * handleData, OUTPROCESS_MODULE_CONFIG * config) { int result; handleData->control_uri = STRING_clone(config->control_uri); if (handleData->control_uri == NULL) { result = -1; } else { handleData->message_uri = STRING_clone(config->message_uri); if (handleData->message_uri == NULL) { STRING_delete(handleData->control_uri); result = -1; } else { handleData->module_args = STRING_clone(config->outprocess_module_args); if (handleData->module_args == NULL) { STRING_delete(handleData->control_uri); STRING_delete(handleData->message_uri); result = -1; } else { result = 0; } } } return result; }
static void setup_dev_auth_emulator_generate_credentials_mocks(const char* token_scope) { STRICT_EXPECTED_CALL(STRING_new()); STRICT_EXPECTED_CALL(get_time(IGNORED_PTR_ARG)); STRICT_EXPECTED_CALL(STRING_construct(token_scope)); STRICT_EXPECTED_CALL(STRING_construct(IGNORED_PTR_ARG)) .IgnoreArgument_psz(); STRICT_EXPECTED_CALL(SASToken_Create(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG)) .IgnoreArgument_scope() .IgnoreArgument_keyName() .IgnoreArgument_expiry() .IgnoreArgument_key(); STRICT_EXPECTED_CALL(STRING_c_str(IGNORED_PTR_ARG)) .IgnoreArgument_handle(); STRICT_EXPECTED_CALL(mallocAndStrcpy_s(IGNORED_PTR_ARG, IGNORED_PTR_ARG)) .IgnoreArgument_destination() .IgnoreArgument_source(); STRICT_EXPECTED_CALL(STRING_delete(IGNORED_PTR_ARG)) .IgnoreArgument_handle(); STRICT_EXPECTED_CALL(STRING_delete(IGNORED_PTR_ARG)) .IgnoreArgument_handle(); STRICT_EXPECTED_CALL(STRING_delete(IGNORED_PTR_ARG)) .IgnoreArgument_handle(); STRICT_EXPECTED_CALL(STRING_delete(IGNORED_PTR_ARG)) .IgnoreArgument_handle(); }
static void sendHttpRequestMethodExpectedCalls() { STRICT_EXPECTED_CALL(environment_get_variable(IGNORED_PTR_ARG)).CallCannotFail(); STRICT_EXPECTED_CALL(HTTPHeaders_Alloc()); STRICT_EXPECTED_CALL(HTTPHeaders_AddHeaderNameValuePair(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG)); STRICT_EXPECTED_CALL(HTTPHeaders_AddHeaderNameValuePair(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG)); STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG)); STRICT_EXPECTED_CALL(UniqueId_Generate(IGNORED_PTR_ARG, IGNORED_NUM_ARG)); STRICT_EXPECTED_CALL(HTTPHeaders_AddHeaderNameValuePair(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG)); STRICT_EXPECTED_CALL(HTTPHeaders_AddHeaderNameValuePair(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG)); STRICT_EXPECTED_CALL(HTTPHeaders_AddHeaderNameValuePair(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG)); STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG)); //cannot fail STRICT_EXPECTED_CALL(STRING_c_str(IGNORED_PTR_ARG)); STRICT_EXPECTED_CALL(IoTHubClient_Auth_Get_SasToken(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG)); STRICT_EXPECTED_CALL(HTTPHeaders_ReplaceHeaderNameValuePair(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG)); STRICT_EXPECTED_CALL(STRING_c_str(IGNORED_PTR_ARG)); STRICT_EXPECTED_CALL(HTTPHeaders_ReplaceHeaderNameValuePair(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG)); STRICT_EXPECTED_CALL(STRING_delete(IGNORED_PTR_ARG)); //cannot fail STRICT_EXPECTED_CALL(STRING_delete(IGNORED_PTR_ARG)); //cannot fail STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG)); //cannot fail STRICT_EXPECTED_CALL(STRING_c_str(IGNORED_PTR_ARG)); STRICT_EXPECTED_CALL(HTTPAPIEX_Create(IGNORED_PTR_ARG)); STRICT_EXPECTED_CALL(IoTHubClient_Auth_Get_TrustBundle(IGNORED_PTR_ARG, IGNORED_PTR_ARG)); STRICT_EXPECTED_CALL(HTTPAPIEX_SetOption(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG)); STRICT_EXPECTED_CALL(HTTPAPIEX_ExecuteRequest(IGNORED_PTR_ARG, HTTPAPI_REQUEST_POST, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, NULL, IGNORED_PTR_ARG)); STRICT_EXPECTED_CALL(HTTPHeaders_Free(IGNORED_PTR_ARG)); //cannot fail STRICT_EXPECTED_CALL(STRING_delete(IGNORED_PTR_ARG)); //cannot fail STRICT_EXPECTED_CALL(HTTPAPIEX_Destroy(IGNORED_PTR_ARG)); //cannot fail STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG)); //cannot fail }
static int InitializeConnection(PMQTTTRANSPORT_HANDLE_DATA transportState, bool initialConnection) { int result = 0; if (!transportState->connected && !transportState->destroyCalled) { // Construct SAS token size_t secSinceEpoch = (size_t)(difftime(get_time(NULL), EPOCH_TIME_T_VALUE) + 0); size_t expiryTime = secSinceEpoch + SAS_TOKEN_DEFAULT_LIFETIME; // Not checking the success of this variable, if fail it will fail in the SASToken creation and return false; STRING_HANDLE emptyKeyName = STRING_new(); STRING_HANDLE sasToken = SASToken_Create(transportState->device_key, transportState->sasTokenSr, emptyKeyName, expiryTime); if (sasToken == NULL) { result = __LINE__; } else { MQTT_CLIENT_OPTIONS options = { 0 }; options.clientId = (char*)STRING_c_str(transportState->device_id); options.willMessage = NULL; options.username = (char*)STRING_c_str(transportState->configPassedThroughUsername); options.password = (char*)STRING_c_str(sasToken); options.keepAliveInterval = DEFAULT_MQTT_KEEPALIVE; options.useCleanSession = false; options.qualityOfServiceValue = DELIVER_AT_LEAST_ONCE; // construct address const char* hostAddress = STRING_c_str(transportState->hostAddress); const char* hostName = strstr(hostAddress, "//"); if (hostName == NULL) { hostName = hostAddress; } else { // Increment beyond the double backslash hostName += 2; } transportState->xioTransport = getIoTransportProvider(hostName, transportState->portNum); if (mqtt_client_connect(transportState->mqttClient, transportState->xioTransport, &options) != 0) { LogError("failure connecting to address %s:%d.\r\n", STRING_c_str(transportState->hostAddress), transportState->portNum); result = __LINE__; } else { transportState->connected = true; result = 0; } STRING_delete(emptyKeyName); STRING_delete(sasToken); } } return result; }
const char* IoTHubAccount_GetEventhubAccessKey(void) { char *iothub_connection_string; static char access_key[128]; if ((iothub_connection_string = IoTHubAccount_GetIoTHubConnString()) != NULL) { STRING_HANDLE iothub_connection_string_str; if((iothub_connection_string_str = STRING_construct(iothub_connection_string)) != NULL) { STRING_TOKENIZER_HANDLE tokenizer; if ((tokenizer = STRING_TOKENIZER_create(iothub_connection_string_str)) != NULL) { STRING_HANDLE tokenString; if ((tokenString = STRING_new()) != NULL) { STRING_HANDLE valueString; if ((valueString = STRING_new()) != NULL) { while ((STRING_TOKENIZER_get_next_token(tokenizer, tokenString, "=") == 0)) { char tokenValue[128]; strcpy(tokenValue, STRING_c_str(tokenString)); if (STRING_TOKENIZER_get_next_token(tokenizer, tokenString, ";") != 0) { break; } if (strcmp(tokenValue, "SharedAccessKey") == 0) { strcpy(access_key, STRING_c_str(tokenString)); break; } } STRING_delete(valueString); } STRING_delete(tokenString); } STRING_TOKENIZER_destroy(tokenizer); } STRING_delete(iothub_connection_string_str); } } return access_key; }
static void setupSAS_Create_happy_path(void) { STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG)).IgnoreArgument(1); STRICT_EXPECTED_CALL(STRING_clone(TEST_KEY_HANDLE)).SetReturn(TEST_CLONED_KEY_HANDLE); STRICT_EXPECTED_CALL(STRING_clone(TEST_URIRESOURCE_HANDLE)).SetReturn(TEST_CLONED_URIRESOURCE_HANDLE); STRICT_EXPECTED_CALL(STRING_clone(TEST_KEYNAME_HANDLE)).SetReturn(TEST_CLONED_KEYNAME_HANDLE); STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG)).IgnoreArgument(1); STRICT_EXPECTED_CALL(STRING_delete(TEST_CLONED_KEY_HANDLE)); STRICT_EXPECTED_CALL(STRING_delete(TEST_CLONED_KEYNAME_HANDLE)); STRICT_EXPECTED_CALL(STRING_delete(TEST_CLONED_URIRESOURCE_HANDLE)); }
const char* IoTHubAccount_GetSharedAccessSignature(IOTHUB_ACCOUNT_INFO_HANDLE acctHandle) { const char* result = NULL; IOTHUB_ACCOUNT_INFO* acctInfo = (IOTHUB_ACCOUNT_INFO*)acctHandle; if (acctInfo != NULL) { if (acctInfo->sharedAccessToken != NULL) { // Reuse the sharedAccessToken if it's been created already result = acctInfo->sharedAccessToken; } else { time_t currentTime = time(NULL); size_t expiry_time = (size_t)(currentTime + 3600); STRING_HANDLE accessKey = STRING_construct(acctInfo->sharedAccessKey); STRING_HANDLE iotName = STRING_construct(acctInfo->hostname); STRING_HANDLE keyName = STRING_construct(acctInfo->keyName); if (accessKey != NULL && iotName != NULL && keyName != NULL) { STRING_HANDLE sasHandle = SASToken_Create(accessKey, iotName, keyName, expiry_time); if (sasHandle == NULL) { result = NULL; } else { if (mallocAndStrcpy_s(&acctInfo->sharedAccessToken, STRING_c_str(sasHandle)) != 0) { result = NULL; } else { result = acctInfo->sharedAccessToken; } STRING_delete(sasHandle); } } STRING_delete(accessKey); STRING_delete(iotName); STRING_delete(keyName); } } else { result = NULL; } return result; }
static void create_x509_enrollment_device() { INDIVIDUAL_ENROLLMENT_HANDLE indiv_enrollment = NULL; PROVISIONING_SERVICE_CLIENT_HANDLE prov_sc_handle = prov_sc_create_from_connection_string(g_prov_conn_string); ASSERT_IS_NOT_NULL(prov_sc_handle, "Failure creating provisioning service client"); prov_sc_set_trace(prov_sc_handle, TRACING_STATUS_ON); PROV_AUTH_HANDLE auth_handle = prov_auth_create(); ASSERT_IS_NOT_NULL(auth_handle, "Failure creating auth client"); char* registration_id = prov_auth_get_registration_id(auth_handle); ASSERT_IS_NOT_NULL(registration_id, "Failure prov_auth_get_common_name"); if (prov_sc_get_individual_enrollment(prov_sc_handle, registration_id, &indiv_enrollment) != 0) { char* x509_cert = prov_auth_get_certificate(auth_handle); ASSERT_IS_NOT_NULL(x509_cert, "Failure prov_auth_get_certificate"); STRING_HANDLE base64_cert = Azure_Base64_Encode_Bytes((const unsigned char*)x509_cert, strlen(x509_cert)); ASSERT_IS_NOT_NULL(base64_cert, "Failure Azure_Base64_Encode_Bytes"); ATTESTATION_MECHANISM_HANDLE attest_handle = attestationMechanism_createWithX509ClientCert(STRING_c_str(base64_cert), NULL); ASSERT_IS_NOT_NULL(attest_handle, "Failure hsm_client_riot_get_certificate "); indiv_enrollment = individualEnrollment_create(registration_id, attest_handle); ASSERT_IS_NOT_NULL(indiv_enrollment, "Failure hsm_client_riot_get_certificate "); ASSERT_ARE_EQUAL(int, 0, prov_sc_create_or_update_individual_enrollment(prov_sc_handle, &indiv_enrollment), "Failure prov_sc_create_or_update_individual_enrollment"); STRING_delete(base64_cert); free(x509_cert); }
static STRING_HANDLE construct_url() { STRING_HANDLE result; /*Codes_SRS_BROKER_17_002: [ Broker_Create shall create a unique id. ]*/ char uuid[BROKER_GUID_SIZE]; memset(uuid, 0, BROKER_GUID_SIZE); if (UniqueId_Generate(uuid, BROKER_GUID_SIZE) != UNIQUEID_OK) { LogError("Unable to generate unique Id."); result = NULL; } else { /*Codes_SRS_BROKER_17_003: [ Broker_Create shall initialize a url consisting of "inproc://" + unique id. ]*/ result = STRING_construct(INPROC_URL_HEAD); if (result == NULL) { LogError("Unable to construct url."); } else { if (STRING_concat(result, uuid) != 0) { /*Codes_SRS_BROKER_13_003: [ This function shall return NULL if an underlying API call to the platform causes an error. ]*/ STRING_delete(result); LogError("Unable to append uuid to url."); result = NULL; } } } return result; }
void HTTPAPIEX_Destroy(HTTPAPIEX_HANDLE handle) { if (handle != NULL) { /*Codes_SRS_HTTPAPIEX_02_042: [HTTPAPIEX_Destroy shall free all the resources used by HTTAPIEX_HANDLE.]*/ size_t i; size_t vectorSize; HTTPAPIEX_HANDLE_DATA* handleData = (HTTPAPIEX_HANDLE_DATA*)handle; if (handleData->k == 2) { HTTPAPI_CloseConnection(handleData->httpHandle); HTTPAPI_Deinit(); } STRING_delete(handleData->hostName); vectorSize = VECTOR_size(handleData->savedOptions); for (i = 0; i < vectorSize; i++) { HTTPAPIEX_SAVED_OPTION*savedOption = VECTOR_element(handleData->savedOptions, i); free((void*)savedOption->optionName); free((void*)savedOption->value); } VECTOR_destroy(handleData->savedOptions); free(handle); } else { /*Codes_SRS_HTTPAPIEX_02_043: [If parameter handle is NULL then HTTPAPIEX_Destroy shall take no action.] */ } }
static VECTOR_HANDLE VECTOR_copy(VECTOR_HANDLE vector) { VECTOR_HANDLE new_vector = VECTOR_create(sizeof(STRING_HANDLE)); size_t size = VECTOR_size(vector); for (size_t index = 0; index < size && new_vector != NULL; index++) { STRING_HANDLE* str = VECTOR_element(vector, index); if (str == NULL) { VECTOR_destroy(new_vector); new_vector = NULL; } else { STRING_HANDLE new_str = STRING_clone(*str); if (new_str == NULL) { VECTOR_destroy(new_vector); new_vector = NULL; } else { if (VECTOR_push_back(new_vector, &new_str, 1) != 0) { STRING_delete(new_str); VECTOR_destroy(new_vector); new_vector = NULL; } } } } return new_vector; }
static void broker_decrement_ref(BROKER_HANDLE broker) { /*Codes_SRS_BROKER_13_058: [If `broker` is NULL the function shall do nothing.]*/ if (broker != NULL) { /*Codes_SRS_BROKER_13_111: [Otherwise, Broker_Destroy shall decrement the internal ref count of the message.]*/ /*Codes_SRS_BROKER_13_112: [If the ref count is zero then the allocated resources are freed.]*/ if (DEC_REF(BROKER_HANDLE_DATA, broker) == DEC_RETURN_ZERO) { BROKER_HANDLE_DATA* broker_data = (BROKER_HANDLE_DATA*)broker; if (singlylinkedlist_get_head_item(broker_data->modules) != NULL) { LogError("WARNING: There are still active modules attached to the broker and the broker is being destroyed."); } /* May want to do nn_shutdown first for cleanliness. */ nn_close(broker_data->publish_socket); STRING_delete(broker_data->url); singlylinkedlist_destroy(broker_data->modules); Lock_Deinit(broker_data->modules_lock); free(broker_data); } } else { LogError("broker handle is NULL"); } }
static void deinit_module(BROKER_MODULEINFO* module_info) { /*Codes_SRS_BROKER_13_057: [The function shall free all members of the MODULE_INFO object.]*/ Lock_Deinit(module_info->socket_lock); STRING_delete(module_info->quit_message_guid); free(module_info->module); }
static HTTP_HEADERS_HANDLE construct_http_headers(const PROV_SERVICE_CLIENT* prov_client, const char* etag, HTTP_CLIENT_REQUEST_TYPE request) { HTTP_HEADERS_HANDLE result; if ((result = HTTPHeaders_Alloc()) == NULL) { LogError("failure sending request"); } else { size_t secSinceEpoch = (size_t)(difftime(get_time(NULL), EPOCH_TIME_T_VALUE) + 0); size_t expiryTime = secSinceEpoch + SAS_TOKEN_DEFAULT_LIFETIME; STRING_HANDLE sas_token = SASToken_CreateString(prov_client->access_key, prov_client->provisioning_service_uri, prov_client->key_name, expiryTime); if (sas_token == NULL) { HTTPHeaders_Free(result); result = NULL; } else { if ((HTTPHeaders_AddHeaderNameValuePair(result, HEADER_KEY_USER_AGENT, HEADER_VALUE_USER_AGENT) != HTTP_HEADERS_OK) || (HTTPHeaders_AddHeaderNameValuePair(result, HEADER_KEY_ACCEPT, HEADER_VALUE_ACCEPT) != HTTP_HEADERS_OK) || ((request != HTTP_CLIENT_REQUEST_DELETE) && (HTTPHeaders_AddHeaderNameValuePair(result, HEADER_KEY_CONTENT_TYPE, HEADER_VALUE_CONTENT_TYPE) != HTTP_HEADERS_OK)) || (HTTPHeaders_AddHeaderNameValuePair(result, HEADER_KEY_AUTHORIZATION, STRING_c_str(sas_token)) != HTTP_HEADERS_OK) || ((etag != NULL) && (HTTPHeaders_AddHeaderNameValuePair(result, HEADER_KEY_IF_MATCH, etag) != HTTP_HEADERS_OK))) { LogError("failure adding header value"); HTTPHeaders_Free(result); result = NULL; } STRING_delete(sas_token); } } return result; }
static int prov_sc_get_record(PROVISIONING_SERVICE_CLIENT_HANDLE prov_client, const char* id, void** handle_ptr, HANDLE_FUNCTION_VECTOR vector, const char* path_format) { int result = 0; if (prov_client == NULL) { LogError("Invalid Provisioning Client Handle"); result = __FAILURE__; } else if (id == NULL) { LogError("Invalid id"); result = __FAILURE__; } else if (handle_ptr == NULL) { LogError("Invalid handle"); result = __FAILURE__; } else { STRING_HANDLE registration_path = create_registration_path(path_format, id); if (registration_path == NULL) { LogError("Failed to construct a registration path"); result = __FAILURE__; } else { HTTP_HEADERS_HANDLE request_headers; if ((request_headers = construct_http_headers(prov_client, NULL, HTTP_CLIENT_REQUEST_GET)) == NULL) { LogError("Failure constructing http headers"); result = __FAILURE__; } else { result = rest_call(prov_client, HTTP_CLIENT_REQUEST_GET, STRING_c_str(registration_path), request_headers, NULL); if (result == 0) { void* handle; if ((handle = vector.deserializeFromJson(prov_client->response)) == NULL) { LogError("Failure constructing new enrollment structure from json response"); result = __FAILURE__; } *handle_ptr = handle; } clear_response(prov_client); } HTTPHeaders_Free(request_headers); } STRING_delete(registration_path); } return result; }
void IoTHubTransportMqtt_Destroy(TRANSPORT_HANDLE handle) { /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_012: [IoTHubTransportMqtt_Destroy shall do nothing if parameter handle is NULL.] */ PMQTTTRANSPORT_HANDLE_DATA transportState = (PMQTTTRANSPORT_HANDLE_DATA)handle; if (transportState != NULL) { transportState->destroyCalled = true; DisconnectFromClient(transportState); //Empty the Waiting for Ack Messages. while (!DList_IsListEmpty(&transportState->waitingForAck)) { PDLIST_ENTRY currentEntry = DList_RemoveHeadList(&transportState->waitingForAck); MQTT_MESSAGE_DETAILS_LIST* mqttMsgEntry = containingRecord(currentEntry, MQTT_MESSAGE_DETAILS_LIST, entry); sendMsgComplete(mqttMsgEntry->iotHubMessageEntry, transportState, IOTHUB_BATCHSTATE_FAILED); free(mqttMsgEntry); } /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_014: [IoTHubTransportMqtt_Destroy shall free all the resources currently in use.] */ mqtt_client_deinit(transportState->mqttClient); STRING_delete(transportState->mqttEventTopic); STRING_delete(transportState->mqttMessageTopic); STRING_delete(transportState->device_id); STRING_delete(transportState->device_key); STRING_delete(transportState->sasTokenSr); STRING_delete(transportState->hostAddress); STRING_delete(transportState->configPassedThroughUsername); tickcounter_destroy(g_msgTickCounter); free(transportState); } }
static void show_platform_info() { STRING_HANDLE platform_info = platform_get_platform_info(); if (platform_info != NULL) { (void)printf("%s\r\n", STRING_c_str(platform_info)); STRING_delete(platform_info); } }
static void NodeModuleLoader_FreeModuleConfiguration(const MODULE_LOADER* loader, const void* module_configuration) { (void)loader; if (module_configuration == NULL) { //Codes_SRS_NODE_MODULE_LOADER_13_027: [ NodeModuleLoader_FreeModuleConfiguration shall do nothing if module_configuration is NULL. ] LogError("module_configuration is NULL"); } else { NODEJS_MODULE_CONFIG* config = (NODEJS_MODULE_CONFIG*)module_configuration; //Codes_SRS_NODE_MODULE_LOADER_13_028: [ NodeModuleLoader_FreeModuleConfiguration shall free the NODEJS_MODULE_CONFIG object. ] STRING_delete(config->main_path); STRING_delete(config->configuration_json); free(config); } }
static void show_sastoken_example() { STRING_HANDLE sas_token = SASToken_CreateString("key", "scope", "name", 987654321); if (sas_token == NULL) { LogError("Failed to create SAS Token.\n"); } else { STRING_delete(sas_token); } }
void HTTPAPIEX_SAS_Destroy(HTTPAPIEX_SAS_HANDLE handle) { /*Codes_SRS_HTTPAPIEXSAS_06_005: [If the parameter handle is NULL then HTTAPIEX_SAS_Destroy shall do nothing and return.]*/ if (handle) { HTTPAPIEX_SAS_STATE* state = (HTTPAPIEX_SAS_STATE*)handle; /*Codes_SRS_HTTPAPIEXSAS_06_006: [HTTAPIEX_SAS_Destroy shall deallocate any structures denoted by the parameter handle.]*/ if (state->key) { STRING_delete(state->key); } if (state->uriResource) { STRING_delete(state->uriResource); } if (state->keyName) { STRING_delete(state->keyName); } free(state); } }
static void PrintProperties(MAP_HANDLE map) { STRING_HANDLE jsonKVP = Map_ToJSON(map); if (jsonKVP != NULL) { (void)printf(" >>>Key Value Pairs Received:[%s]\r\n", STRING_c_str(jsonKVP)); STRING_delete(jsonKVP); } else { (void)printf(" >>>Key Value Pairs Received:[]\r\n"); } }
void ModuleLoader_FreeBaseConfiguration(MODULE_LOADER_BASE_CONFIGURATION* configuration) { if(configuration != NULL) { /*Codes_SRS_MODULE_LOADER_13_056: [ ModuleLoader_FreeBaseConfiguration shall free configuration->binding_path. ]*/ STRING_delete(configuration->binding_path); } else { /*Codes_SRS_MODULE_LOADER_13_055: [ ModuleLoader_FreeBaseConfiguration shall do nothing if configuration is NULL. ]*/ LogError("configuration is NULL"); } }
static void Outprocess_FreeConfiguration(void* configuration) { if (configuration == NULL) { /*Codes_SRS_OUTPROCESS_MODULE_17_003: [ If configuration is NULL this function shall do nothing. ]*/ LogError("configuration is NULL"); } else { /*Codes_SRS_OUTPROCESS_MODULE_17_004: [ This function shall delete the STRING_HANDLE represented by configuration. ]*/ STRING_delete((STRING_HANDLE)configuration); } }
SAS_TOKEN_STATUS IoTHubClient_Auth_Is_SasToken_Valid(IOTHUB_AUTHORIZATION_HANDLE handle) { SAS_TOKEN_STATUS result; if (handle == NULL) { /* Codes_SRS_IoTHub_Authorization_07_015: [ if handle is NULL, IoTHubClient_Auth_Is_SasToken_Valid shall return false. ] */ LogError("Invalid Parameter handle: %p", handle); result = SAS_TOKEN_STATUS_FAILED; } else { if (handle->cred_type == IOTHUB_CREDENTIAL_TYPE_SAS_TOKEN) { if (handle->device_sas_token == NULL) { /* Codes_SRS_IoTHub_Authorization_07_017: [ If the sas_token is NULL IoTHubClient_Auth_Is_SasToken_Valid shall return false. ] */ LogError("Failure: device_sas_toke is NULL"); result = SAS_TOKEN_STATUS_FAILED; } else { /* Codes_SRS_IoTHub_Authorization_07_018: [ otherwise IoTHubClient_Auth_Is_SasToken_Valid shall return the value returned by SASToken_Validate. ] */ STRING_HANDLE strSasToken = STRING_construct(handle->device_sas_token); if (strSasToken != NULL) { if (!SASToken_Validate(strSasToken)) { result = SAS_TOKEN_STATUS_INVALID; } else { result = SAS_TOKEN_STATUS_VALID; } STRING_delete(strSasToken); } else { LogError("Failure constructing SAS Token"); result = SAS_TOKEN_STATUS_FAILED; } } } else { /* Codes_SRS_IoTHub_Authorization_07_016: [ if credential type is not IOTHUB_CREDENTIAL_TYPE_SAS_TOKEN IoTHubClient_Auth_Is_SasToken_Valid shall return SAS_TOKEN_STATUS_VALID. ] */ result = SAS_TOKEN_STATUS_VALID; } } return result; }
HTTPAPIEX_HANDLE HTTPAPIEX_Create(const char* hostName) { HTTPAPIEX_HANDLE result; /*Codes_SRS_HTTPAPIEX_02_001: [If parameter hostName is NULL then HTTPAPIEX_Create shall return NULL.]*/ if (hostName == NULL) { LogError("invalid (NULL) parameter\r\n"); result = NULL; } else { /*Codes_SRS_HTTPAPIEX_02_005: [If creating the handle fails for any reason, then HTTAPIEX_Create shall return NULL.] */ HTTPAPIEX_HANDLE_DATA* handleData = (HTTPAPIEX_HANDLE_DATA*)malloc(sizeof(HTTPAPIEX_HANDLE_DATA)); if (handleData == NULL) { LogError("malloc failed.\r\n"); result = NULL; } else { /*Codes_SRS_HTTPAPIEX_02_002: [Parameter hostName shall be saved.]*/ handleData->hostName = STRING_construct(hostName); if (handleData->hostName == NULL) { free(handleData); LogError("unable to STRING_construct\r\n"); result = NULL; } else { /*Codes_SRS_HTTPAPIEX_02_004: [Otherwise, HTTPAPIEX_Create shall return a HTTAPIEX_HANDLE suitable for further calls to the module.] */ handleData->savedOptions = VECTOR_create(sizeof(HTTPAPIEX_SAVED_OPTION)); if (handleData->savedOptions == NULL) { STRING_delete(handleData->hostName); free(handleData); result = NULL; } else { handleData->k = -1; handleData->httpHandle = NULL; result = handleData; } } } } return result; }
static STRING_HANDLE create_registration_path(const char* path_format, const char* id) { STRING_HANDLE registration_path; if (id == NULL) { registration_path = STRING_construct(path_format); } else { STRING_HANDLE encoded_id; if ((encoded_id = URL_EncodeString(id)) == NULL) { LogError("Unable to URL encode ID"); registration_path = NULL; } else { registration_path = STRING_construct_sprintf(path_format, STRING_c_str(encoded_id)); STRING_delete(encoded_id); } } if (registration_path == NULL) { LogError("Failed constructing base path"); } else if (STRING_sprintf(registration_path, API_VERSION_QUERY_PARAM, PROVISIONING_SERVICE_API_VERSION) != 0) { LogError("Unable to add query paramters"); STRING_delete(registration_path); registration_path = NULL; } return registration_path; }
static void parseResponseJsonExpectedCalls() { STRICT_EXPECTED_CALL(BUFFER_u_char(IGNORED_PTR_ARG)); STRICT_EXPECTED_CALL(BUFFER_length(IGNORED_PTR_ARG)).CallCannotFail(); //cannot fail (returns length 0) STRICT_EXPECTED_CALL(STRING_from_byte_array(IGNORED_PTR_ARG, IGNORED_NUM_ARG)); STRICT_EXPECTED_CALL(STRING_c_str(IGNORED_PTR_ARG)); STRICT_EXPECTED_CALL(json_parse_string(IGNORED_PTR_ARG)); STRICT_EXPECTED_CALL(json_value_get_object(IGNORED_PTR_ARG)); STRICT_EXPECTED_CALL(json_object_get_value(IGNORED_PTR_ARG, IGNORED_PTR_ARG)); STRICT_EXPECTED_CALL(json_object_get_value(IGNORED_PTR_ARG, IGNORED_PTR_ARG)); STRICT_EXPECTED_CALL(json_serialize_to_string(IGNORED_PTR_ARG)); STRICT_EXPECTED_CALL(json_value_get_number(IGNORED_PTR_ARG)).CallCannotFail(); //cannot fail (returns -1, which isn't strictly failure) STRICT_EXPECTED_CALL(STRING_delete(IGNORED_PTR_ARG)); //cannot fail STRICT_EXPECTED_CALL(json_value_free(IGNORED_PTR_ARG)); //cannot fail }
static BUFFER_HANDLE createMethodPayloadJson(const char* methodName, unsigned int timeout, const char* payload) { STRING_HANDLE stringHandle; const char* stringHandle_c_str; BUFFER_HANDLE result; if ((stringHandle = STRING_construct_sprintf(RELATIVE_PATH_FMT_DEVIECMETHOD_PAYLOAD, methodName, timeout, payload)) == NULL) { LogError("STRING_construct_sprintf failed"); result = NULL; } else if ((stringHandle_c_str = STRING_c_str(stringHandle)) == NULL) { LogError("STRING_c_str failed"); STRING_delete(stringHandle); result = NULL; } else { result = BUFFER_create((const unsigned char*)stringHandle_c_str, strlen(stringHandle_c_str)); STRING_delete(stringHandle); } return result; }
HTTPAPIEX_RESULT HTTPAPIEX_SAS_ExecuteRequest(HTTPAPIEX_SAS_HANDLE sasHandle, HTTPAPIEX_HANDLE handle, HTTPAPI_REQUEST_TYPE requestType, const char* relativePath, HTTP_HEADERS_HANDLE requestHttpHeadersHandle, BUFFER_HANDLE requestContent, unsigned int* statusCode, HTTP_HEADERS_HANDLE responseHeadersHandle, BUFFER_HANDLE responseContent) { /*Codes_SRS_HTTPAPIEXSAS_06_007: [If the parameter sasHandle is NULL then HTTPAPIEX_SAS_ExecuteRequest shall simply invoke HTTPAPIEX_ExecuteRequest with the remaining parameters (following sasHandle) as its arguments and shall return immediately with the result of that call as the result of HTTPAPIEX_SAS_ExecuteRequest.]*/ if (sasHandle != NULL) { /*Codes_SRS_HTTPAPIEXSAS_06_008: [if the parameter requestHttpHeadersHandle is NULL then fallthrough.]*/ if (requestHttpHeadersHandle != NULL) { /*Codes_SRS_HTTPAPIEXSAS_06_009: [HTTPHeaders_FindHeaderValue shall be invoked with the requestHttpHeadersHandle as its first argument and the string "Authorization" as its second argument.]*/ /*Codes_SRS_HTTPAPIEXSAS_06_010: [If the return result of the invocation of HTTPHeaders_FindHeaderValue is NULL then fallthrough.]*/ if (HTTPHeaders_FindHeaderValue(requestHttpHeadersHandle, "Authorization") != NULL) { HTTPAPIEX_SAS_STATE* state = (HTTPAPIEX_SAS_STATE*)sasHandle; /*Codes_SRS_HTTPAPIEXSAS_06_018: [A value of type time_t that shall be known as currentTime is obtained from calling get_time.]*/ time_t currentTime = get_time(NULL); /*Codes_SRS_HTTPAPIEXSAS_06_019: [If the value of currentTime is (time_t)-1 is then fallthrough.]*/ if (currentTime == (time_t)-1) { LogError("Time does not appear to be working.\r\n"); } else { /*Codes_SRS_HTTPAPIEXSAS_06_011: [SASToken_Create shall be invoked.]*/ /*Codes_SRS_HTTPAPIEXSAS_06_012: [If the return result of SASToken_Create is NULL then fallthrough.]*/ size_t expiry = (size_t)(difftime(currentTime, 0) + 3600); STRING_HANDLE newSASToken = SASToken_Create(state->key, state->uriResource, state->keyName, expiry); if (newSASToken != NULL) { /*Codes_SRS_HTTPAPIEXSAS_06_013: [HTTPHeaders_ReplaceHeaderNameValuePair shall be invoked with "Authorization" as its second argument and STRING_c_str (newSASToken) as its third argument.]*/ if (HTTPHeaders_ReplaceHeaderNameValuePair(requestHttpHeadersHandle, "Authorization", STRING_c_str(newSASToken)) != HTTP_HEADERS_OK) { /*Codes_SRS_HTTPAPIEXSAS_06_014: [If the result of the invocation of HTTPHeaders_ReplaceHeaderNameValuePair is NOT HTTP_HEADERS_OK then fallthrough.]*/ LogError("Unable to replace the old SAS Token.\r\n"); } /*Codes_SRS_HTTPAPIEXSAS_06_015: [STRING_delete(newSASToken) will be invoked.]*/ STRING_delete(newSASToken); } else { LogError("Unable to create a new SAS token.\r\n"); } } } } } /*Codes_SRS_HTTPAPIEXSAS_06_016: [HTTPAPIEX_ExecuteRequest with the remaining parameters (following sasHandle) as its arguments will be invoked and the result of that call is the result of HTTPAPIEX_SAS_ExecuteRequest.]*/ return HTTPAPIEX_ExecuteRequest(handle,requestType,relativePath,requestHttpHeadersHandle,requestContent,statusCode,responseHeadersHandle,responseContent); }
void amqp_connection_destroy(AMQP_CONNECTION_HANDLE conn_handle) { // Codes_SRS_IOTHUBTRANSPORT_AMQP_CONNECTION_09_035: [If `conn_handle` is NULL, amqp_connection_destroy() shall fail and return] if (conn_handle != NULL) { AMQP_CONNECTION_INSTANCE* instance = (AMQP_CONNECTION_INSTANCE*)conn_handle; // Codes_SRS_IOTHUBTRANSPORT_AMQP_CONNECTION_09_036: [amqp_connection_destroy() shall destroy `instance->cbs_handle` if set using cbs_destroy()] if (instance->cbs_handle != NULL) { cbs_destroy(instance->cbs_handle); } // Codes_SRS_IOTHUBTRANSPORT_AMQP_CONNECTION_09_037: [amqp_connection_destroy() shall destroy `instance->session_handle` if set using session_destroy()] if (instance->session_handle != NULL) { session_destroy(instance->session_handle); } // Codes_SRS_IOTHUBTRANSPORT_AMQP_CONNECTION_09_067: [amqp_connection_destroy() shall destroy `instance->connection_handle` if set using connection_destroy()] if (instance->connection_handle != NULL) { connection_destroy(instance->connection_handle); } // Codes_SRS_IOTHUBTRANSPORT_AMQP_CONNECTION_09_038: [amqp_connection_destroy() shall destroy `instance->sasl_io` if set using xio_destroy()] if (instance->sasl_io != NULL) { xio_destroy(instance->sasl_io); } // Codes_SRS_IOTHUBTRANSPORT_AMQP_CONNECTION_09_039: [amqp_connection_destroy() shall destroy `instance->sasl_mechanism` if set using saslmechanism_destroy()] if (instance->sasl_mechanism != NULL) { saslmechanism_destroy(instance->sasl_mechanism); } // Codes_SRS_IOTHUBTRANSPORT_AMQP_CONNECTION_09_059: [amqp_connection_destroy() shall destroy `instance->iothub_host_fqdn` if set using STRING_delete()] if (instance->iothub_fqdn != NULL) { STRING_delete(instance->iothub_fqdn); } // Codes_SRS_IOTHUBTRANSPORT_AMQP_CONNECTION_09_040: [amqp_connection_destroy() shall free the memory allocated for the connection instance] free(instance); } }