A small memory footprint AMQP implimentation

Dependents:   iothub_client_sample_amqp remote_monitoring simplesample_amqp

Committer:
AzureIoTClient
Date:
Fri Jul 29 15:58:39 2016 -0700
Revision:
6:641a9672db08
Parent:
0:6ae2f7bca550
Child:
17:923575db8b2d
1.0.10

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Azure.IoT Build 0:6ae2f7bca550 1 // Copyright (c) Microsoft. All rights reserved.
Azure.IoT Build 0:6ae2f7bca550 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
Azure.IoT Build 0:6ae2f7bca550 3
Azure.IoT Build 0:6ae2f7bca550 4 #include <stdlib.h>
Azure.IoT Build 0:6ae2f7bca550 5 #ifdef _CRTDBG_MAP_ALLOC
Azure.IoT Build 0:6ae2f7bca550 6 #include <crtdbg.h>
Azure.IoT Build 0:6ae2f7bca550 7 #endif
Azure.IoT Build 0:6ae2f7bca550 8 #include "azure_uamqp_c/sasl_mechanism.h"
Azure.IoT Build 0:6ae2f7bca550 9 #include "azure_uamqp_c/amqpalloc.h"
Azure.IoT Build 0:6ae2f7bca550 10
Azure.IoT Build 0:6ae2f7bca550 11 typedef struct SASL_MECHANISM_INSTANCE_TAG
Azure.IoT Build 0:6ae2f7bca550 12 {
Azure.IoT Build 0:6ae2f7bca550 13 const SASL_MECHANISM_INTERFACE_DESCRIPTION* sasl_mechanism_interface_description;
Azure.IoT Build 0:6ae2f7bca550 14 CONCRETE_SASL_MECHANISM_HANDLE concrete_sasl_mechanism_handle;
Azure.IoT Build 0:6ae2f7bca550 15 } SASL_MECHANISM_INSTANCE;
Azure.IoT Build 0:6ae2f7bca550 16
Azure.IoT Build 0:6ae2f7bca550 17 SASL_MECHANISM_HANDLE saslmechanism_create(const SASL_MECHANISM_INTERFACE_DESCRIPTION* sasl_mechanism_interface_description, void* sasl_mechanism_create_parameters)
Azure.IoT Build 0:6ae2f7bca550 18 {
Azure.IoT Build 0:6ae2f7bca550 19 SASL_MECHANISM_INSTANCE* sasl_mechanism_instance;
Azure.IoT Build 0:6ae2f7bca550 20
Azure.IoT Build 0:6ae2f7bca550 21 /* Codes_SRS_SASL_MECHANISM_01_004: [If the argument sasl_mechanism_interface_description is NULL, saslmechanism_create shall return NULL.] */
Azure.IoT Build 0:6ae2f7bca550 22 if ((sasl_mechanism_interface_description == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 23 /* Codes_SRS_SASL_MECHANISM_01_005: [If any sasl_mechanism_interface_description member is NULL, sasl_mechanism_create shall fail and return NULL.] */
Azure.IoT Build 0:6ae2f7bca550 24 (sasl_mechanism_interface_description->concrete_sasl_mechanism_create == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 25 (sasl_mechanism_interface_description->concrete_sasl_mechanism_destroy == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 26 (sasl_mechanism_interface_description->concrete_sasl_mechanism_get_init_bytes == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 27 (sasl_mechanism_interface_description->concrete_sasl_mechanism_get_mechanism_name == NULL))
Azure.IoT Build 0:6ae2f7bca550 28 {
Azure.IoT Build 0:6ae2f7bca550 29 sasl_mechanism_instance = NULL;
Azure.IoT Build 0:6ae2f7bca550 30 }
Azure.IoT Build 0:6ae2f7bca550 31 else
Azure.IoT Build 0:6ae2f7bca550 32 {
Azure.IoT Build 0:6ae2f7bca550 33 sasl_mechanism_instance = (SASL_MECHANISM_INSTANCE*)amqpalloc_malloc(sizeof(SASL_MECHANISM_INSTANCE));
Azure.IoT Build 0:6ae2f7bca550 34
Azure.IoT Build 0:6ae2f7bca550 35 if (sasl_mechanism_instance != NULL)
Azure.IoT Build 0:6ae2f7bca550 36 {
Azure.IoT Build 0:6ae2f7bca550 37 sasl_mechanism_instance->sasl_mechanism_interface_description = sasl_mechanism_interface_description;
Azure.IoT Build 0:6ae2f7bca550 38
Azure.IoT Build 0:6ae2f7bca550 39 /* Codes_SRS_SASL_MECHANISM_01_002: [In order to instantiate the concrete SASL mechanism implementation the function concrete_sasl_mechanism_create from the sasl_mechanism_interface_description shall be called, passing the sasl_mechanism_create_parameters to it.] */
Azure.IoT Build 0:6ae2f7bca550 40 sasl_mechanism_instance->concrete_sasl_mechanism_handle = sasl_mechanism_instance->sasl_mechanism_interface_description->concrete_sasl_mechanism_create((void*)sasl_mechanism_create_parameters);
Azure.IoT Build 0:6ae2f7bca550 41 if (sasl_mechanism_instance->concrete_sasl_mechanism_handle == NULL)
Azure.IoT Build 0:6ae2f7bca550 42 {
Azure.IoT Build 0:6ae2f7bca550 43 /* Codes_SRS_SASL_MECHANISM_01_003: [If the underlying concrete_sasl_mechanism_create call fails, saslmechanism_create shall return NULL.] */
Azure.IoT Build 0:6ae2f7bca550 44 amqpalloc_free(sasl_mechanism_instance);
Azure.IoT Build 0:6ae2f7bca550 45 sasl_mechanism_instance = NULL;
Azure.IoT Build 0:6ae2f7bca550 46 }
Azure.IoT Build 0:6ae2f7bca550 47 }
Azure.IoT Build 0:6ae2f7bca550 48 }
Azure.IoT Build 0:6ae2f7bca550 49
Azure.IoT Build 0:6ae2f7bca550 50 /* Codes_SRS_SASL_MECHANISM_01_001: [saslmechanism_create shall return on success a non-NULL handle to a new SASL mechanism interface.] */
Azure.IoT Build 0:6ae2f7bca550 51 return (SASL_MECHANISM_HANDLE)sasl_mechanism_instance;
Azure.IoT Build 0:6ae2f7bca550 52 }
Azure.IoT Build 0:6ae2f7bca550 53
Azure.IoT Build 0:6ae2f7bca550 54 void saslmechanism_destroy(SASL_MECHANISM_HANDLE sasl_mechanism)
Azure.IoT Build 0:6ae2f7bca550 55 {
Azure.IoT Build 0:6ae2f7bca550 56 if (sasl_mechanism != NULL)
Azure.IoT Build 0:6ae2f7bca550 57 {
Azure.IoT Build 0:6ae2f7bca550 58 SASL_MECHANISM_INSTANCE* sasl_mechanism_instance = (SASL_MECHANISM_INSTANCE*)sasl_mechanism;
Azure.IoT Build 0:6ae2f7bca550 59
Azure.IoT Build 0:6ae2f7bca550 60 /* Codes_SRS_SASL_MECHANISM_01_008: [saslmechanism_destroy shall also call the concrete_sasl_mechanism_destroy function that is member of the sasl_mechanism_interface_description argument passed to saslmechanism_create, while passing as argument to concrete_sasl_mechanism_destroy the result of the underlying concrete SASL mechanism handle.] */
Azure.IoT Build 0:6ae2f7bca550 61 sasl_mechanism_instance->sasl_mechanism_interface_description->concrete_sasl_mechanism_destroy(sasl_mechanism_instance->concrete_sasl_mechanism_handle);
Azure.IoT Build 0:6ae2f7bca550 62
Azure.IoT Build 0:6ae2f7bca550 63 /* Codes_SRS_SASL_MECHANISM_01_007: [saslmechanism_destroy shall free all resources associated with the SASL mechanism handle.] */
Azure.IoT Build 0:6ae2f7bca550 64 amqpalloc_free(sasl_mechanism_instance);
Azure.IoT Build 0:6ae2f7bca550 65 }
Azure.IoT Build 0:6ae2f7bca550 66 }
Azure.IoT Build 0:6ae2f7bca550 67
Azure.IoT Build 0:6ae2f7bca550 68 int saslmechanism_get_init_bytes(SASL_MECHANISM_HANDLE sasl_mechanism, SASL_MECHANISM_BYTES* init_bytes)
Azure.IoT Build 0:6ae2f7bca550 69 {
Azure.IoT Build 0:6ae2f7bca550 70 int result;
Azure.IoT Build 0:6ae2f7bca550 71
Azure.IoT Build 0:6ae2f7bca550 72 /* Codes_SRS_SASL_MECHANISM_01_012: [If the argument sasl_mechanism is NULL, saslmechanism_get_init_bytes shall fail and return a non-zero value.] */
Azure.IoT Build 0:6ae2f7bca550 73 if (sasl_mechanism == NULL)
Azure.IoT Build 0:6ae2f7bca550 74 {
Azure.IoT Build 0:6ae2f7bca550 75 result = __LINE__;
Azure.IoT Build 0:6ae2f7bca550 76 }
Azure.IoT Build 0:6ae2f7bca550 77 else
Azure.IoT Build 0:6ae2f7bca550 78 {
Azure.IoT Build 0:6ae2f7bca550 79 SASL_MECHANISM_INSTANCE* sasl_mechanism_instance = (SASL_MECHANISM_INSTANCE*)sasl_mechanism;
Azure.IoT Build 0:6ae2f7bca550 80
Azure.IoT Build 0:6ae2f7bca550 81 /* Codes_SRS_SASL_MECHANISM_01_010: [saslmechanism_get_init_bytes shall call the specific concrete_sasl_mechanism_get_init_bytes function specified in saslmechanism_create, passing the init_bytes argument to it.] */
Azure.IoT Build 0:6ae2f7bca550 82 if (sasl_mechanism_instance->sasl_mechanism_interface_description->concrete_sasl_mechanism_get_init_bytes(sasl_mechanism_instance->concrete_sasl_mechanism_handle, init_bytes) != 0)
Azure.IoT Build 0:6ae2f7bca550 83 {
Azure.IoT Build 0:6ae2f7bca550 84 /* Codes_SRS_SASL_MECHANISM_01_013: [If the underlying concrete_sasl_mechanism_get_init_bytes fails, saslmechanism_get_init_bytes shall fail and return a non-zero value.] */
Azure.IoT Build 0:6ae2f7bca550 85 result = __LINE__;
Azure.IoT Build 0:6ae2f7bca550 86 }
Azure.IoT Build 0:6ae2f7bca550 87 else
Azure.IoT Build 0:6ae2f7bca550 88 {
Azure.IoT Build 0:6ae2f7bca550 89 /* Codes_SRS_SASL_MECHANISM_01_011: [On success, saslmechanism_get_init_bytes shall return 0.] */
Azure.IoT Build 0:6ae2f7bca550 90 result = 0;
Azure.IoT Build 0:6ae2f7bca550 91 }
Azure.IoT Build 0:6ae2f7bca550 92 }
Azure.IoT Build 0:6ae2f7bca550 93
Azure.IoT Build 0:6ae2f7bca550 94 return result;
Azure.IoT Build 0:6ae2f7bca550 95 }
Azure.IoT Build 0:6ae2f7bca550 96
Azure.IoT Build 0:6ae2f7bca550 97 const char* saslmechanism_get_mechanism_name(SASL_MECHANISM_HANDLE sasl_mechanism)
Azure.IoT Build 0:6ae2f7bca550 98 {
Azure.IoT Build 0:6ae2f7bca550 99 const char* result;
Azure.IoT Build 0:6ae2f7bca550 100
Azure.IoT Build 0:6ae2f7bca550 101 /* Codes_SRS_SASL_MECHANISM_01_016: [If the argument sasl_mechanism is NULL, saslmechanism_get_mechanism_name shall fail and return a non-zero value.] */
Azure.IoT Build 0:6ae2f7bca550 102 if (sasl_mechanism == NULL)
Azure.IoT Build 0:6ae2f7bca550 103 {
Azure.IoT Build 0:6ae2f7bca550 104 result = NULL;
Azure.IoT Build 0:6ae2f7bca550 105 }
Azure.IoT Build 0:6ae2f7bca550 106 else
Azure.IoT Build 0:6ae2f7bca550 107 {
Azure.IoT Build 0:6ae2f7bca550 108 SASL_MECHANISM_INSTANCE* sasl_mechanism_instance = (SASL_MECHANISM_INSTANCE*)sasl_mechanism;
Azure.IoT Build 0:6ae2f7bca550 109
Azure.IoT Build 0:6ae2f7bca550 110 /* Codes_SRS_SASL_MECHANISM_01_014: [saslmechanism_get_mechanism_name shall call the specific concrete_sasl_mechanism_get_mechanism_name function specified in saslmechanism_create.] */
Azure.IoT Build 0:6ae2f7bca550 111 /* Codes_SRS_SASL_MECHANISM_01_015: [On success, saslmechanism_get_mechanism_name shall return a pointer to a string with the mechanism name.] */
Azure.IoT Build 0:6ae2f7bca550 112 /* Codes_SRS_SASL_MECHANISM_01_017: [If the underlying concrete_sasl_mechanism_get_mechanism_name fails, saslmechanism_get_mechanism_name shall return NULL.] */
Azure.IoT Build 0:6ae2f7bca550 113 result = sasl_mechanism_instance->sasl_mechanism_interface_description->concrete_sasl_mechanism_get_mechanism_name(sasl_mechanism_instance->concrete_sasl_mechanism_handle);
Azure.IoT Build 0:6ae2f7bca550 114 }
Azure.IoT Build 0:6ae2f7bca550 115
Azure.IoT Build 0:6ae2f7bca550 116 return result;
Azure.IoT Build 0:6ae2f7bca550 117 }
Azure.IoT Build 0:6ae2f7bca550 118
Azure.IoT Build 0:6ae2f7bca550 119 int saslmechanism_challenge(SASL_MECHANISM_HANDLE sasl_mechanism, const SASL_MECHANISM_BYTES* challenge_bytes, SASL_MECHANISM_BYTES* response_bytes)
Azure.IoT Build 0:6ae2f7bca550 120 {
Azure.IoT Build 0:6ae2f7bca550 121 int result;
Azure.IoT Build 0:6ae2f7bca550 122
Azure.IoT Build 0:6ae2f7bca550 123 /* Codes_SRS_SASL_MECHANISM_01_020: [If the argument sasl_mechanism is NULL, saslmechanism_challenge shall fail and return a non-zero value.] */
Azure.IoT Build 0:6ae2f7bca550 124 if (sasl_mechanism == NULL)
Azure.IoT Build 0:6ae2f7bca550 125 {
Azure.IoT Build 0:6ae2f7bca550 126 result = __LINE__;
Azure.IoT Build 0:6ae2f7bca550 127 }
Azure.IoT Build 0:6ae2f7bca550 128 else
Azure.IoT Build 0:6ae2f7bca550 129 {
Azure.IoT Build 0:6ae2f7bca550 130 /* Codes_SRS_SASL_MECHANISM_01_018: [saslmechanism_challenge shall call the specific concrete_sasl_mechanism_challenge function specified in saslmechanism_create, while passing the challenge_bytes and response_bytes arguments to it.] */
Azure.IoT Build 0:6ae2f7bca550 131 if (sasl_mechanism->sasl_mechanism_interface_description->concrete_sasl_mechanism_challenge(sasl_mechanism->concrete_sasl_mechanism_handle, challenge_bytes, response_bytes) != 0)
Azure.IoT Build 0:6ae2f7bca550 132 {
Azure.IoT Build 0:6ae2f7bca550 133 /* Codes_SRS_SASL_MECHANISM_01_021: [If the underlying concrete_sasl_mechanism_challenge fails, saslmechanism_challenge shall fail and return a non-zero value.] */
Azure.IoT Build 0:6ae2f7bca550 134 result = __LINE__;
Azure.IoT Build 0:6ae2f7bca550 135 }
Azure.IoT Build 0:6ae2f7bca550 136 else
Azure.IoT Build 0:6ae2f7bca550 137 {
Azure.IoT Build 0:6ae2f7bca550 138 /* Codes_SRS_SASL_MECHANISM_01_019: [On success, saslmechanism_challenge shall return 0.] */
Azure.IoT Build 0:6ae2f7bca550 139 result = 0;
Azure.IoT Build 0:6ae2f7bca550 140 }
Azure.IoT Build 0:6ae2f7bca550 141 }
Azure.IoT Build 0:6ae2f7bca550 142
Azure.IoT Build 0:6ae2f7bca550 143 return result;
AzureIoTClient 6:641a9672db08 144 }