int prov_transport_common_mqtt_close(PROV_DEVICE_TRANSPORT_HANDLE handle) { int result; if (handle == NULL) { /* Tests_PROV_TRANSPORT_MQTT_COMMON_07_011: [ If handle is NULL, prov_transport_common_mqtt_close shall return a non-zero value. ] */ LogError("Invalid parameter specified handle: %p", handle); result = __FAILURE__; } else { PROV_TRANSPORT_MQTT_INFO* mqtt_info = (PROV_TRANSPORT_MQTT_INFO*)handle; BUFFER_delete(mqtt_info->ek); mqtt_info->ek = NULL; BUFFER_delete(mqtt_info->srk); mqtt_info->srk = NULL; free(mqtt_info->registration_id); mqtt_info->registration_id = NULL; /* Tests_PROV_TRANSPORT_MQTT_COMMON_07_012: [ prov_transport_common_mqtt_close shall close all connection associated with mqtt communication. ] */ if (mqtt_client_disconnect(mqtt_info->mqtt_client, NULL, NULL) == 0) { mqtt_client_dowork(mqtt_info->mqtt_client); } xio_destroy(mqtt_info->transport_io); mqtt_info->transport_io = NULL; /* Tests_PROV_TRANSPORT_MQTT_COMMON_07_013: [ On success prov_transport_common_mqtt_close shall return a zero value. ] */ mqtt_info->mqtt_state = MQTT_STATE_IDLE; result = 0; } return result; }
void tlsio_schannel_destroy(CONCRETE_IO_HANDLE tls_io) { if (tls_io != NULL) { TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; if (tls_io_instance->credential_handle_allocated) { (void)FreeCredentialHandle(&tls_io_instance->credential_handle); tls_io_instance->credential_handle_allocated = false; } if (tls_io_instance->received_bytes != NULL) { free(tls_io_instance->received_bytes); } if (tls_io_instance->x509_schannel_handle != NULL) { x509_schannel_destroy(tls_io_instance->x509_schannel_handle); } xio_destroy(tls_io_instance->socket_io); free(tls_io_instance->host_name); free(tls_io); } }
void headerdetectio_destroy(CONCRETE_IO_HANDLE header_detect_io) { if (header_detect_io != NULL) { HEADER_DETECT_IO_INSTANCE* header_detect_io_instance = (HEADER_DETECT_IO_INSTANCE*)header_detect_io; (void)headerdetectio_close(header_detect_io, NULL, NULL); xio_destroy(header_detect_io_instance->underlying_io); amqpalloc_free(header_detect_io); } }
static void DisconnectFromClient(PMQTTTRANSPORT_HANDLE_DATA transportState) { /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_013: [If the parameter subscribe is true then IoTHubTransportMqtt_Destroy shall call IoTHubTransportMqtt_Unsubscribe.] */ if (transportState->subscribed) { IoTHubTransportMqtt_Unsubscribe(transportState); } (void)mqtt_client_disconnect(transportState->mqttClient); xio_destroy(transportState->xioTransport); transportState->connected = false; transportState->currPacketState = DISCONNECT_TYPE; }
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); } }
static int create_sasl_components(AMQP_CONNECTION_INSTANCE* instance) { int result; SASL_MECHANISM_HANDLE sasl_mechanism; XIO_HANDLE sasl_io; // Codes_SRS_IOTHUBTRANSPORT_AMQP_CONNECTION_09_012: [`instance->sasl_mechanism` shall be created using saslmechanism_create()] if ((sasl_mechanism = saslmechanism_create(saslmssbcbs_get_interface(), NULL)) == NULL) { // Codes_SRS_IOTHUBTRANSPORT_AMQP_CONNECTION_09_013: [If saslmechanism_create() fails, amqp_connection_create() shall fail and return NULL] LogError("Failed creating the SASL mechanism (saslmechanism_create failed)"); result = __FAILURE__; } else { // Codes_SRS_IOTHUBTRANSPORT_AMQP_CONNECTION_09_014: [A SASLCLIENTIO_CONFIG shall be set with `instance->underlying_io_transport` and `instance->sasl_mechanism`] SASLCLIENTIO_CONFIG sasl_client_config; sasl_client_config.sasl_mechanism = sasl_mechanism; sasl_client_config.underlying_io = instance->underlying_io_transport; // Codes_SRS_IOTHUBTRANSPORT_AMQP_CONNECTION_09_015: [`instance->sasl_io` shall be created using xio_create() passing saslclientio_get_interface_description() and the SASLCLIENTIO_CONFIG instance] if ((sasl_io = xio_create(saslclientio_get_interface_description(), &sasl_client_config)) == NULL) { // Codes_SRS_IOTHUBTRANSPORT_AMQP_CONNECTION_09_016: [If xio_create() fails, amqp_connection_create() shall fail and return NULL] LogError("Failed creating the SASL I/O (xio_create failed)"); saslmechanism_destroy(sasl_mechanism); result = __FAILURE__; } // Codes_SRS_IOTHUBTRANSPORT_AMQP_CONNECTION_09_017: [The sasl_io "logtrace" option shall be set using xio_setoption(), passing `instance->is_trace_on`] else if (xio_setoption(sasl_io, SASL_IO_OPTION_LOG_TRACE, (const void*)&instance->is_trace_on) != RESULT_OK) { LogError("Failed setting the SASL I/O logging trace option (xio_setoption failed)"); xio_destroy(sasl_io); saslmechanism_destroy(sasl_mechanism); result = __FAILURE__; } else { instance->sasl_mechanism = sasl_mechanism; instance->sasl_io = sasl_io; result = RESULT_OK; } } 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; /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_013: [If the parameter subscribe is true then IoTHubTransportMqtt_Destroy shall call IoTHubTransportMqtt_Unsubscribe.] */ if (transportState->subscribed) { IoTHubTransportMqtt_Unsubscribe(handle); } //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); } (void)mqtt_client_disconnect(transportState->mqttClient); xio_destroy(transportState->xioTransport); /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_014: [IoTHubTransportMqtt_Destroy shall free all the resources currently in use.] */ transportState->connected = false; transportState->currPacketState = DISCONNECT_TYPE; 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); } }
void tlsio_wolfssl_destroy(CONCRETE_IO_HANDLE tls_io) { if (tls_io != NULL) { TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; if (tls_io_instance->socket_io_read_bytes != NULL) { free(tls_io_instance->socket_io_read_bytes); } if (tls_io_instance->certificate != NULL) { free(tls_io_instance->certificate); tls_io_instance->certificate = NULL; } wolfSSL_CTX_free(tls_io_instance->ssl_context); xio_destroy(tls_io_instance->socket_io); free(tls_io); } }
static int create_connection(PROV_TRANSPORT_MQTT_INFO* mqtt_info) { int result; MQTT_CLIENT_OPTIONS options = { 0 }; char* username_info; if ((username_info = construct_username(mqtt_info)) == NULL) { LogError("Failure creating username info"); result = __FAILURE__; } else if (construct_transport(mqtt_info)) { LogError("Failure constructing transport"); free(username_info); result = __FAILURE__; } else { (void)mqtt_client_set_trace(mqtt_info->mqtt_client, mqtt_info->log_trace, false); options.username = username_info; options.clientId = mqtt_info->registration_id; options.useCleanSession = 1; options.log_trace = mqtt_info->log_trace; options.qualityOfServiceValue = DELIVER_AT_LEAST_ONCE; if (mqtt_client_connect(mqtt_info->mqtt_client, mqtt_info->transport_io, &options) != 0) { xio_destroy(mqtt_info->transport_io); mqtt_info->transport_io = NULL; LogError("Failure connecting to mqtt server"); result = __FAILURE__; } else { result = 0; } free(username_info); } return result; }
int main(int argc, char** argv) { int result; (void)argc, argv; amqpalloc_set_memory_tracing_enabled(true); if (platform_init() != 0) { result = -1; } else { XIO_HANDLE sasl_io; CONNECTION_HANDLE connection; SESSION_HANDLE session; LINK_HANDLE link; MESSAGE_SENDER_HANDLE message_sender; MESSAGE_HANDLE message; size_t last_memory_used = 0; /* create SASL PLAIN handler */ SASL_PLAIN_CONFIG sasl_plain_config = { EH_KEY_NAME, EH_KEY, NULL }; SASL_MECHANISM_HANDLE sasl_mechanism_handle = saslmechanism_create(saslplain_get_interface(), &sasl_plain_config); XIO_HANDLE tls_io; /* create the TLS IO */ TLSIO_CONFIG tls_io_config = { EH_HOST, 5671 }; const IO_INTERFACE_DESCRIPTION* tlsio_interface = platform_get_default_tlsio(); tls_io = xio_create(tlsio_interface, &tls_io_config); /* create the SASL client IO using the TLS IO */ SASLCLIENTIO_CONFIG sasl_io_config; sasl_io_config.underlying_io = tls_io; sasl_io_config.sasl_mechanism = sasl_mechanism_handle; sasl_io = xio_create(saslclientio_get_interface_description(), &sasl_io_config); /* create the connection, session and link */ connection = connection_create(sasl_io, EH_HOST, "some", NULL, NULL); session = session_create(connection, NULL, NULL); session_set_incoming_window(session, 2147483647); session_set_outgoing_window(session, 65536); AMQP_VALUE source = messaging_create_source("ingress"); AMQP_VALUE target = messaging_create_target("amqps://" EH_HOST "/" EH_NAME); link = link_create(session, "sender-link", role_sender, source, target); link_set_snd_settle_mode(link, sender_settle_mode_unsettled); (void)link_set_max_message_size(link, 65536); amqpvalue_destroy(source); amqpvalue_destroy(target); message = message_create(); unsigned char hello[] = { 'H', 'e', 'l', 'l', 'o' }; BINARY_DATA binary_data; binary_data.bytes = hello; binary_data.length = sizeof(hello); message_add_body_amqp_data(message, binary_data); /* create a message sender */ message_sender = messagesender_create(link, NULL, NULL); if (messagesender_open(message_sender) == 0) { uint32_t i; #if _WIN32 unsigned long startTime = (unsigned long)GetTickCount64(); #endif for (i = 0; i < msg_count; i++) { (void)messagesender_send(message_sender, message, on_message_send_complete, message); } message_destroy(message); while (true) { size_t current_memory_used; size_t maximum_memory_used; connection_dowork(connection); current_memory_used = amqpalloc_get_current_memory_used(); maximum_memory_used = amqpalloc_get_maximum_memory_used(); if (current_memory_used != last_memory_used) { (void)printf("Current memory usage:%lu (max:%lu)\r\n", (unsigned long)current_memory_used, (unsigned long)maximum_memory_used); last_memory_used = current_memory_used; } if (sent_messages == msg_count) { break; } } #if _WIN32 unsigned long endTime = (unsigned long)GetTickCount64(); (void)printf("Send %zu messages in %lu ms: %.02f msgs/sec\r\n", msg_count, (endTime - startTime), (float)msg_count / ((float)(endTime - startTime) / 1000)); #endif } messagesender_destroy(message_sender); link_destroy(link); session_destroy(session); connection_destroy(connection); xio_destroy(sasl_io); xio_destroy(tls_io); saslmechanism_destroy(sasl_mechanism_handle); platform_deinit(); (void)printf("Max memory usage:%lu\r\n", (unsigned long)amqpalloc_get_maximum_memory_used()); (void)printf("Current memory usage:%lu\r\n", (unsigned long)amqpalloc_get_current_memory_used()); result = 0; } #ifdef _CRTDBG_MAP_ALLOC _CrtDumpMemoryLeaks(); #endif return result; }
static PROV_TRANSPORT_IO_INFO* amqp_transport_ws_io(const char* fqdn, SASL_MECHANISM_HANDLE* sasl_mechanism, const HTTP_PROXY_OPTIONS* proxy_info) { PROV_TRANSPORT_IO_INFO* result; HTTP_PROXY_IO_CONFIG proxy_config; TLSIO_CONFIG tls_io_config; WSIO_CONFIG ws_io_config; const IO_INTERFACE_DESCRIPTION* ws_io_interface; const IO_INTERFACE_DESCRIPTION* tlsio_interface; if ((ws_io_interface = wsio_get_interface_description()) == NULL) { /* Codes_PROV_TRANSPORT_AMQP_WS_CLIENT_07_013: [ If any failure is encountered amqp_transport_ws_io shall return NULL ] */ LogError("wsio_get_interface_description return NULL IO Interface"); result = NULL; } else if ((tlsio_interface = platform_get_default_tlsio()) == NULL) { /* Codes_PROV_TRANSPORT_AMQP_WS_CLIENT_07_013: [ If any failure is encountered amqp_transport_ws_io shall return NULL ] */ LogError("platform_get_default_tlsio return NULL IO Interface"); result = NULL; } else { memset(&tls_io_config, 0, sizeof(TLSIO_CONFIG)); ws_io_config.hostname = fqdn; ws_io_config.port = PROV_AMQP_WS_PORT_NUM; ws_io_config.protocol = PROV_AMQP_WS_PROTOCOL_NAME; ws_io_config.resource_name = PROV_AMQP_WS_RELATIVE_PATH; ws_io_config.underlying_io_interface = tlsio_interface; ws_io_config.underlying_io_parameters = &tls_io_config; tls_io_config.hostname = fqdn; tls_io_config.port = PROV_AMQP_WS_PORT_NUM; if (proxy_info != NULL) { /* Codes_PROV_TRANSPORT_AMQP_WS_CLIENT_07_012: [ If proxy_info is not NULL, amqp_transport_ws_io shall construct a HTTP_PROXY_IO_CONFIG object and assign it to TLSIO_CONFIG underlying_io_parameters ] */ proxy_config.hostname = tls_io_config.hostname; proxy_config.port = proxy_info->port; proxy_config.proxy_hostname = proxy_info->host_address; proxy_config.proxy_port = proxy_info->port; proxy_config.username = proxy_info->username; proxy_config.password = proxy_info->password; tls_io_config.underlying_io_interface = http_proxy_io_get_interface_description(); tls_io_config.underlying_io_parameters = &proxy_config; } /* Codes_PROV_TRANSPORT_AMQP_WS_CLIENT_07_014: [ On success amqp_transport_ws_io shall return an allocated PROV_TRANSPORT_IO_INFO structure. ] */ if ((result = (PROV_TRANSPORT_IO_INFO*)malloc(sizeof(PROV_TRANSPORT_IO_INFO))) == NULL) { /* Codes_PROV_TRANSPORT_AMQP_WS_CLIENT_07_013: [ If any failure is encountered amqp_transport_ws_io shall return NULL ] */ LogError("failure allocating prov_transport info"); result = NULL; } else { memset(result, 0, sizeof(PROV_TRANSPORT_IO_INFO)); /* Codes_PROV_TRANSPORT_AMQP_WS_CLIENT_07_015: [ amqp_transport_ws_io shall allocate a PROV_TRANSPORT_IO_INFO transfer_handle by calling xio_create with the ws_io_interface. ] */ result->transport_handle = xio_create(ws_io_interface, &ws_io_config); if (result->transport_handle == NULL) { /* Codes_PROV_TRANSPORT_AMQP_WS_CLIENT_07_013: [ If any failure is encountered amqp_transport_ws_io shall return NULL ] */ LogError("failed calling xio_create on underlying io"); free(result); result = NULL; } else { if (sasl_mechanism != NULL) { const IO_INTERFACE_DESCRIPTION* saslio_interface; SASLCLIENTIO_CONFIG sasl_io_config; sasl_io_config.underlying_io = result->transport_handle; sasl_io_config.sasl_mechanism = *sasl_mechanism; saslio_interface = saslclientio_get_interface_description(); if (saslio_interface == NULL) { /* Codes_PROV_TRANSPORT_AMQP_WS_CLIENT_07_013: [ If any failure is encountered amqp_transport_ws_io shall return NULL ] */ LogError("failed calling xio_create on underlying io"); xio_destroy(result->transport_handle); free(result); result = NULL; } else { /* Codes_PROV_TRANSPORT_AMQP_WS_CLIENT_07_016: [ amqp_transport_ws_io shall allocate a PROV_TRANSPORT_IO_INFO sasl_handle by calling xio_create with the saslio_interface. ] */ result->sasl_handle = xio_create(saslio_interface, &sasl_io_config); if (result->sasl_handle == NULL) { /* Codes_PROV_TRANSPORT_AMQP_WS_CLIENT_07_013: [ If any failure is encountered amqp_transport_ws_io shall return NULL ] */ LogError("failed calling xio_create on sasl client interface"); xio_destroy(result->transport_handle); free(result); result = NULL; } } } } } } return result; }
int main(int argc, char** argv) { int result; (void)argc, argv; amqpalloc_set_memory_tracing_enabled(true); if (platform_init() != 0) { result = -1; } else { CONNECTION_HANDLE connection; SESSION_HANDLE session; LINK_HANDLE link; MESSAGE_SENDER_HANDLE message_sender; MESSAGE_HANDLE message; size_t last_memory_used = 0; /* create socket IO */ XIO_HANDLE socket_io; SOCKETIO_CONFIG socketio_config = { "localhost", 5672, NULL }; socket_io = xio_create(socketio_get_interface_description(), &socketio_config); /* create the connection, session and link */ connection = connection_create(socket_io, "localhost", "some", NULL, NULL); session = session_create(connection, NULL, NULL); session_set_incoming_window(session, 2147483647); session_set_outgoing_window(session, 65536); AMQP_VALUE source = messaging_create_source("ingress"); AMQP_VALUE target = messaging_create_target("localhost/ingress"); link = link_create(session, "sender-link", role_sender, source, target); link_set_snd_settle_mode(link, sender_settle_mode_settled); (void)link_set_max_message_size(link, 65536); amqpvalue_destroy(source); amqpvalue_destroy(target); message = message_create(); unsigned char hello[] = { 'H', 'e', 'l', 'l', 'o' }; BINARY_DATA binary_data; binary_data.bytes = hello; binary_data.length = sizeof(hello); message_add_body_amqp_data(message, binary_data); /* create a message sender */ message_sender = messagesender_create(link, NULL, NULL); if (messagesender_open(message_sender) == 0) { uint32_t i; #if _WIN32 unsigned long startTime = (unsigned long)GetTickCount64(); #endif for (i = 0; i < msg_count; i++) { (void)messagesender_send(message_sender, message, on_message_send_complete, message); } message_destroy(message); while (true) { size_t current_memory_used; size_t maximum_memory_used; connection_dowork(connection); current_memory_used = amqpalloc_get_current_memory_used(); maximum_memory_used = amqpalloc_get_maximum_memory_used(); if (current_memory_used != last_memory_used) { (void)printf("Current memory usage:%lu (max:%lu)\r\n", (unsigned long)current_memory_used, (unsigned long)maximum_memory_used); last_memory_used = current_memory_used; } if (sent_messages == msg_count) { break; } } #if _WIN32 unsigned long endTime = (unsigned long)GetTickCount64(); (void)printf("Send %zu messages in %lu ms: %.02f msgs/sec\r\n", msg_count, (endTime - startTime), (float)msg_count / ((float)(endTime - startTime) / 1000)); #endif } messagesender_destroy(message_sender); link_destroy(link); session_destroy(session); connection_destroy(connection); xio_destroy(socket_io); platform_deinit(); (void)printf("Max memory usage:%lu\r\n", (unsigned long)amqpalloc_get_maximum_memory_used()); (void)printf("Current memory usage:%lu\r\n", (unsigned long)amqpalloc_get_current_memory_used()); result = 0; } #ifdef _CRTDBG_MAP_ALLOC _CrtDumpMemoryLeaks(); #endif return result; }
static int construct_transport(PROV_TRANSPORT_MQTT_INFO* mqtt_info) { int result; if (mqtt_info->transport_io == NULL) { HTTP_PROXY_OPTIONS* transport_proxy; if (mqtt_info->proxy_option.host_address != NULL) { transport_proxy = &mqtt_info->proxy_option; } else { transport_proxy = NULL; } if ((mqtt_info->transport_io = mqtt_info->transport_io_cb(mqtt_info->hostname, transport_proxy)) == NULL) { LogError("Failure calling transport_io callback"); result = __FAILURE__; } else { if (mqtt_info->certificate != NULL && xio_setoption(mqtt_info->transport_io, OPTION_TRUSTED_CERT, mqtt_info->certificate) != 0) { LogError("Failure setting trusted certs"); result = __FAILURE__; xio_destroy(mqtt_info->transport_io); mqtt_info->transport_io = NULL; } else if (mqtt_info->hsm_type == TRANSPORT_HSM_TYPE_X509) { if (mqtt_info->x509_cert != NULL && mqtt_info->private_key != NULL) { if (xio_setoption(mqtt_info->transport_io, OPTION_X509_ECC_CERT, mqtt_info->x509_cert) != 0) { LogError("Failure setting x509 cert on xio"); xio_destroy(mqtt_info->transport_io); mqtt_info->transport_io = NULL; result = __FAILURE__; } else if (xio_setoption(mqtt_info->transport_io, OPTION_X509_ECC_KEY, mqtt_info->private_key) != 0) { LogError("Failure setting x509 key on xio"); xio_destroy(mqtt_info->transport_io); mqtt_info->transport_io = NULL; result = __FAILURE__; } else { result = 0; } } else { LogError("x509 certificate is NULL"); xio_destroy(mqtt_info->transport_io); mqtt_info->transport_io = NULL; result = __FAILURE__; } } else { result = 0; } } } else { result = 0; } return result; }
int main(int argc, char** argv) { int result; (void)argc; (void)argv; if (platform_init() != 0) { result = -1; } else { XIO_HANDLE sasl_io; CONNECTION_HANDLE connection; SESSION_HANDLE session; LINK_HANDLE link; MESSAGE_SENDER_HANDLE message_sender; MESSAGE_HANDLE message; size_t last_memory_used = 0; /* create SASL PLAIN handler */ SASL_MECHANISM_HANDLE sasl_mechanism_handle = saslmechanism_create(saslmssbcbs_get_interface(), NULL); XIO_HANDLE tls_io; STRING_HANDLE sas_key_name; STRING_HANDLE sas_key_value; STRING_HANDLE resource_uri; STRING_HANDLE encoded_resource_uri; STRING_HANDLE sas_token; BUFFER_HANDLE buffer; TLSIO_CONFIG tls_io_config = { EH_HOST, 5671 }; const IO_INTERFACE_DESCRIPTION* tlsio_interface; SASLCLIENTIO_CONFIG sasl_io_config; time_t currentTime; size_t expiry_time; CBS_HANDLE cbs; AMQP_VALUE source; AMQP_VALUE target; unsigned char hello[] = { 'H', 'e', 'l', 'l', 'o' }; BINARY_DATA binary_data; gballoc_init(); /* create the TLS IO */ tlsio_interface = platform_get_default_tlsio(); tls_io = xio_create(tlsio_interface, &tls_io_config); /* create the SASL client IO using the TLS IO */ sasl_io_config.underlying_io = tls_io; sasl_io_config.sasl_mechanism = sasl_mechanism_handle; sasl_io = xio_create(saslclientio_get_interface_description(), &sasl_io_config); /* create the connection, session and link */ connection = connection_create(sasl_io, EH_HOST, "some", NULL, NULL); session = session_create(connection, NULL, NULL); session_set_incoming_window(session, 2147483647); session_set_outgoing_window(session, 65536); /* Construct a SAS token */ sas_key_name = STRING_construct(EH_KEY_NAME); /* unfortunately SASToken wants an encoded key - this should be fixed at a later time */ buffer = BUFFER_create((unsigned char*)EH_KEY, strlen(EH_KEY)); sas_key_value = Base64_Encoder(buffer); BUFFER_delete(buffer); resource_uri = STRING_construct("sb://" EH_HOST "/" EH_NAME "/publishers/" EH_PUBLISHER); encoded_resource_uri = URL_EncodeString(STRING_c_str(resource_uri)); /* Make a token that expires in one hour */ currentTime = time(NULL); expiry_time = (size_t)(difftime(currentTime, 0) + 3600); sas_token = SASToken_Create(sas_key_value, encoded_resource_uri, sas_key_name, expiry_time); cbs = cbs_create(session); if (cbs_open_async(cbs, on_cbs_open_complete, cbs, on_cbs_error, cbs) == 0) { (void)cbs_put_token_async(cbs, "servicebus.windows.net:sastoken", "sb://" EH_HOST "/" EH_NAME "/publishers/" EH_PUBLISHER, STRING_c_str(sas_token), on_cbs_put_token_complete, cbs); while (!auth) { size_t current_memory_used; size_t maximum_memory_used; connection_dowork(connection); current_memory_used = gballoc_getCurrentMemoryUsed(); maximum_memory_used = gballoc_getMaximumMemoryUsed(); if (current_memory_used != last_memory_used) { (void)printf("Current memory usage:%lu (max:%lu)\r\n", (unsigned long)current_memory_used, (unsigned long)maximum_memory_used); last_memory_used = current_memory_used; } } } STRING_delete(sas_token); STRING_delete(sas_key_name); STRING_delete(sas_key_value); STRING_delete(resource_uri); STRING_delete(encoded_resource_uri); source = messaging_create_source("ingress"); target = messaging_create_target("amqps://" EH_HOST "/" EH_NAME); link = link_create(session, "sender-link", role_sender, source, target); link_set_snd_settle_mode(link, sender_settle_mode_settled); (void)link_set_max_message_size(link, 65536); amqpvalue_destroy(source); amqpvalue_destroy(target); message = message_create(); binary_data.bytes = hello; binary_data.length = sizeof(hello); message_add_body_amqp_data(message, binary_data); /* create a message sender */ message_sender = messagesender_create(link, NULL, NULL); if (messagesender_open(message_sender) == 0) { uint32_t i; bool keep_running = true; tickcounter_ms_t start_time; TICK_COUNTER_HANDLE tick_counter = tickcounter_create(); if (tickcounter_get_current_ms(tick_counter, &start_time) != 0) { (void)printf("Error getting start time\r\n"); } else { for (i = 0; i < msg_count; i++) { (void)messagesender_send(message_sender, message, on_message_send_complete, message); } message_destroy(message); while (keep_running) { size_t current_memory_used; size_t maximum_memory_used; connection_dowork(connection); current_memory_used = gballoc_getCurrentMemoryUsed(); maximum_memory_used = gballoc_getMaximumMemoryUsed(); if (current_memory_used != last_memory_used) { (void)printf("Current memory usage:%lu (max:%lu)\r\n", (unsigned long)current_memory_used, (unsigned long)maximum_memory_used); last_memory_used = current_memory_used; } if (sent_messages == msg_count) { break; } } { tickcounter_ms_t end_time; if (tickcounter_get_current_ms(tick_counter, &end_time) != 0) { (void)printf("Error getting end time\r\n"); } else { (void)printf("Send %u messages in %lu ms: %.02f msgs/sec\r\n", (unsigned int)msg_count, (unsigned long)(end_time - start_time), (float)msg_count / ((float)(end_time - start_time) / 1000)); } } } } messagesender_destroy(message_sender); link_destroy(link); session_destroy(session); connection_destroy(connection); xio_destroy(sasl_io); xio_destroy(tls_io); saslmechanism_destroy(sasl_mechanism_handle); platform_deinit(); (void)printf("Max memory usage:%lu\r\n", (unsigned long)gballoc_getCurrentMemoryUsed()); (void)printf("Current memory usage:%lu\r\n", (unsigned long)gballoc_getMaximumMemoryUsed()); gballoc_deinit(); result = 0; } return result; }
void tls_server_io_schannel_destroy(CONCRETE_IO_HANDLE tls_io) { if (tls_io != NULL) { LIST_ITEM_HANDLE first_pending_io; TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; if (tls_io_instance->credential_handle_allocated) { (void)FreeCredentialHandle(&tls_io_instance->credential_handle); tls_io_instance->credential_handle_allocated = false; } if (tls_io_instance->received_bytes != NULL) { free(tls_io_instance->received_bytes); } if (tls_io_instance->x509_schannel_handle != NULL) { x509_schannel_destroy(tls_io_instance->x509_schannel_handle); } if (tls_io_instance->x509certificate != NULL) { free(tls_io_instance->x509certificate); } if (tls_io_instance->x509privatekey != NULL) { free(tls_io_instance->x509privatekey); } xio_destroy(tls_io_instance->socket_io); free(tls_io_instance->host_name); first_pending_io = singlylinkedlist_get_head_item(tls_io_instance->pending_io_list); while (first_pending_io != NULL) { PENDING_SEND* pending_send = (PENDING_SEND*)singlylinkedlist_item_get_value(first_pending_io); if (pending_send == NULL) { LogError("Failure: retrieving socket from list"); indicate_error(tls_io_instance); break; } else { if (pending_send->on_send_complete != NULL) { pending_send->on_send_complete(pending_send->on_send_complete_context, IO_SEND_CANCELLED); } if (singlylinkedlist_remove(tls_io_instance->pending_io_list, first_pending_io) != 0) { LogError("Failure: removing pending IO from list"); } } } if (tls_io_instance->cert_context != NULL) { if (!CertFreeCertificateContext(tls_io_instance->cert_context)) { LogError("Failure freeing certificate context"); } } singlylinkedlist_destroy(tls_io_instance->pending_io_list); free(tls_io); } }
CONCRETE_IO_HANDLE tls_server_io_schannel_create(void* io_create_parameters) { TLS_SERVER_IO_CONFIG* tls_server_io_config = (TLS_SERVER_IO_CONFIG*)io_create_parameters; TLS_IO_INSTANCE* result; if (tls_server_io_config == NULL) { LogError("invalid argument detected: void* io_create_parameters = %p", tls_server_io_config); result = NULL; } else { result = (TLS_IO_INSTANCE*)malloc(sizeof(TLS_IO_INSTANCE)); if (result == NULL) { LogError("malloc failed"); } else { HCERTSTORE cert_store_handle; const IO_INTERFACE_DESCRIPTION* underlying_io_interface; void* io_interface_parameters; result->on_bytes_received = NULL; result->on_io_open_complete = NULL; result->on_io_close_complete = NULL; result->on_io_error = NULL; result->on_io_open_complete_context = NULL; result->on_io_close_complete_context = NULL; result->on_bytes_received_context = NULL; result->on_io_error_context = NULL; result->credential_handle_allocated = false; result->x509_schannel_handle = NULL; result->needed_bytes = 0; /* A cert store called "My" has to be available for use ... This is because this is only used in a test now ... */ cert_store_handle = CertOpenStore(CERT_STORE_PROV_SYSTEM, X509_ASN_ENCODING, 0, CERT_SYSTEM_STORE_LOCAL_MACHINE, L"MY"); if (cert_store_handle == NULL) { result->cert_context = NULL; LogError("Error opening store for server."); } else { result->cert_context = CertFindCertificateInStore(cert_store_handle, X509_ASN_ENCODING, 0, CERT_FIND_SUBJECT_STR_A, "localhost", // use appropriate subject name NULL ); if (!CertCloseStore(cert_store_handle, 0)) { LogError("Error closing store."); } } underlying_io_interface = tls_server_io_config->underlying_io_interface; io_interface_parameters = tls_server_io_config->underlying_io_parameters; if (underlying_io_interface == NULL) { LogError("socketio_get_interface_description failed"); free(result->host_name); free(result); result = NULL; } else { result->socket_io = xio_create(underlying_io_interface, io_interface_parameters); if (result->socket_io == NULL) { LogError("xio_create failed"); free(result->host_name); free(result); result = NULL; } else { result->pending_io_list = singlylinkedlist_create(); if (result->pending_io_list == NULL) { LogError("Failed creating pending IO list."); xio_destroy(result->socket_io); free(result->host_name); free(result); result = NULL; } else { result->received_bytes = NULL; result->received_byte_count = 0; result->buffer_size = 0; result->tlsio_state = TLS_SERVER_IO_STATE_NOT_OPEN; result->x509certificate = NULL; result->x509privatekey = NULL; result->x509_schannel_handle = NULL; } } } } } return result; }