A small memory footprint AMQP implimentation
Dependents: iothub_client_sample_amqp remote_monitoring simplesample_amqp
sasl_frame_codec.c@30:0407b2db334c, 2017-07-28 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Fri Jul 28 09:52:29 2017 -0700
- Revision:
- 30:0407b2db334c
- Parent:
- 28:add19eb7defa
1.1.20
Who changed what in which revision?
User | Revision | Line number | New 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 | #include <stdint.h> |
Azure.IoT Build | 0:6ae2f7bca550 | 6 | #include <stddef.h> |
Azure.IoT Build | 0:6ae2f7bca550 | 7 | #include <string.h> |
AzureIoTClient | 20:206846c14c80 | 8 | #include "azure_c_shared_utility/gballoc.h" |
AzureIoTClient | 19:000ab4e6a2c1 | 9 | #include "azure_c_shared_utility/optimize_size.h" |
AzureIoTClient | 23:1111ee8bcba4 | 10 | #include "azure_c_shared_utility/xlogging.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 11 | #include "azure_uamqp_c/sasl_frame_codec.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 12 | #include "azure_uamqp_c/frame_codec.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 13 | #include "azure_uamqp_c/amqpvalue.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 14 | #include "azure_uamqp_c/amqp_definitions.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 15 | |
Azure.IoT Build | 0:6ae2f7bca550 | 16 | /* Requirements implemented by design or by other modules */ |
Azure.IoT Build | 0:6ae2f7bca550 | 17 | /* Codes_SRS_SASL_FRAME_CODEC_01_011: [A SASL frame has a type code of 0x01.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 18 | /* Codes_SRS_SASL_FRAME_CODEC_01_016: [The maximum size of a SASL frame is defined by MIN-MAX-FRAME-SIZE.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 19 | |
Azure.IoT Build | 0:6ae2f7bca550 | 20 | #define MIX_MAX_FRAME_SIZE 512 |
Azure.IoT Build | 0:6ae2f7bca550 | 21 | |
Azure.IoT Build | 0:6ae2f7bca550 | 22 | typedef enum SASL_FRAME_DECODE_STATE_TAG |
Azure.IoT Build | 0:6ae2f7bca550 | 23 | { |
AzureIoTClient | 28:add19eb7defa | 24 | SASL_FRAME_DECODE_FRAME, |
AzureIoTClient | 28:add19eb7defa | 25 | SASL_FRAME_DECODE_ERROR |
Azure.IoT Build | 0:6ae2f7bca550 | 26 | } SASL_FRAME_DECODE_STATE; |
Azure.IoT Build | 0:6ae2f7bca550 | 27 | |
Azure.IoT Build | 0:6ae2f7bca550 | 28 | typedef struct SASL_FRAME_CODEC_INSTANCE_TAG |
Azure.IoT Build | 0:6ae2f7bca550 | 29 | { |
AzureIoTClient | 28:add19eb7defa | 30 | FRAME_CODEC_HANDLE frame_codec; |
Azure.IoT Build | 0:6ae2f7bca550 | 31 | |
AzureIoTClient | 28:add19eb7defa | 32 | /* decode */ |
AzureIoTClient | 28:add19eb7defa | 33 | ON_SASL_FRAME_RECEIVED on_sasl_frame_received; |
AzureIoTClient | 28:add19eb7defa | 34 | ON_SASL_FRAME_CODEC_ERROR on_sasl_frame_codec_error; |
AzureIoTClient | 28:add19eb7defa | 35 | void* callback_context; |
AzureIoTClient | 28:add19eb7defa | 36 | AMQPVALUE_DECODER_HANDLE decoder; |
AzureIoTClient | 28:add19eb7defa | 37 | SASL_FRAME_DECODE_STATE decode_state; |
AzureIoTClient | 28:add19eb7defa | 38 | AMQP_VALUE decoded_sasl_frame_value; |
Azure.IoT Build | 0:6ae2f7bca550 | 39 | } SASL_FRAME_CODEC_INSTANCE; |
Azure.IoT Build | 0:6ae2f7bca550 | 40 | |
Azure.IoT Build | 0:6ae2f7bca550 | 41 | static void amqp_value_decoded(void* context, AMQP_VALUE decoded_value) |
Azure.IoT Build | 0:6ae2f7bca550 | 42 | { |
AzureIoTClient | 28:add19eb7defa | 43 | SASL_FRAME_CODEC_INSTANCE* sasl_frame_codec_instance = (SASL_FRAME_CODEC_INSTANCE*)context; |
AzureIoTClient | 28:add19eb7defa | 44 | AMQP_VALUE descriptor = amqpvalue_get_inplace_descriptor(decoded_value); |
Azure.IoT Build | 0:6ae2f7bca550 | 45 | |
AzureIoTClient | 28:add19eb7defa | 46 | if (descriptor == NULL) |
AzureIoTClient | 28:add19eb7defa | 47 | { |
AzureIoTClient | 23:1111ee8bcba4 | 48 | LogError("Cannot get frame descriptor"); |
AzureIoTClient | 28:add19eb7defa | 49 | sasl_frame_codec_instance->decode_state = SASL_FRAME_DECODE_ERROR; |
Azure.IoT Build | 0:6ae2f7bca550 | 50 | |
AzureIoTClient | 28:add19eb7defa | 51 | /* Codes_SRS_SASL_FRAME_CODEC_01_049: [If any error occurs while decoding a frame, the decoder shall call the on_sasl_frame_codec_error and pass to it the callback_context, both of those being the ones given to sasl_frame_codec_create.] */ |
AzureIoTClient | 28:add19eb7defa | 52 | sasl_frame_codec_instance->on_sasl_frame_codec_error(sasl_frame_codec_instance->callback_context); |
AzureIoTClient | 28:add19eb7defa | 53 | } |
AzureIoTClient | 28:add19eb7defa | 54 | else |
AzureIoTClient | 28:add19eb7defa | 55 | { |
AzureIoTClient | 28:add19eb7defa | 56 | /* Codes_SRS_SASL_FRAME_CODEC_01_009: [The frame body of a SASL frame MUST contain exactly one AMQP type, whose type encoding MUST have provides="sasl-frame".] */ |
AzureIoTClient | 28:add19eb7defa | 57 | if (!is_sasl_mechanisms_type_by_descriptor(descriptor) && |
AzureIoTClient | 28:add19eb7defa | 58 | !is_sasl_init_type_by_descriptor(descriptor) && |
AzureIoTClient | 28:add19eb7defa | 59 | !is_sasl_challenge_type_by_descriptor(descriptor) && |
AzureIoTClient | 28:add19eb7defa | 60 | !is_sasl_response_type_by_descriptor(descriptor) && |
AzureIoTClient | 28:add19eb7defa | 61 | !is_sasl_outcome_type_by_descriptor(descriptor)) |
AzureIoTClient | 28:add19eb7defa | 62 | { |
AzureIoTClient | 23:1111ee8bcba4 | 63 | LogError("Not a SASL frame"); |
AzureIoTClient | 23:1111ee8bcba4 | 64 | sasl_frame_codec_instance->decode_state = SASL_FRAME_DECODE_ERROR; |
Azure.IoT Build | 0:6ae2f7bca550 | 65 | |
AzureIoTClient | 28:add19eb7defa | 66 | /* Codes_SRS_SASL_FRAME_CODEC_01_049: [If any error occurs while decoding a frame, the decoder shall call the on_sasl_frame_codec_error and pass to it the callback_context, both of those being the ones given to sasl_frame_codec_create.] */ |
AzureIoTClient | 28:add19eb7defa | 67 | sasl_frame_codec_instance->on_sasl_frame_codec_error(sasl_frame_codec_instance->callback_context); |
AzureIoTClient | 28:add19eb7defa | 68 | } |
AzureIoTClient | 28:add19eb7defa | 69 | else |
AzureIoTClient | 28:add19eb7defa | 70 | { |
AzureIoTClient | 28:add19eb7defa | 71 | sasl_frame_codec_instance->decoded_sasl_frame_value = decoded_value; |
AzureIoTClient | 28:add19eb7defa | 72 | } |
AzureIoTClient | 28:add19eb7defa | 73 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 74 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 75 | |
Azure.IoT Build | 0:6ae2f7bca550 | 76 | static void frame_received(void* context, const unsigned char* type_specific, uint32_t type_specific_size, const unsigned char* frame_body, uint32_t frame_body_size) |
Azure.IoT Build | 0:6ae2f7bca550 | 77 | { |
AzureIoTClient | 28:add19eb7defa | 78 | SASL_FRAME_CODEC_INSTANCE* sasl_frame_codec_instance = (SASL_FRAME_CODEC_INSTANCE*)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 79 | |
AzureIoTClient | 28:add19eb7defa | 80 | /* Codes_SRS_SASL_FRAME_CODEC_01_006: [Bytes 6 and 7 of the header are ignored.] */ |
AzureIoTClient | 28:add19eb7defa | 81 | (void)type_specific; |
AzureIoTClient | 28:add19eb7defa | 82 | /* Codes_SRS_SASL_FRAME_CODEC_01_007: [The extended header is ignored.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 83 | |
AzureIoTClient | 28:add19eb7defa | 84 | /* Codes_SRS_SASL_FRAME_CODEC_01_008: [The maximum size of a SASL frame is defined by MIN-MAX-FRAME-SIZE.] */ |
AzureIoTClient | 28:add19eb7defa | 85 | if ((type_specific_size + frame_body_size + 6 > MIX_MAX_FRAME_SIZE) || |
AzureIoTClient | 28:add19eb7defa | 86 | /* Codes_SRS_SASL_FRAME_CODEC_01_010: [Receipt of an empty frame is an irrecoverable error.] */ |
AzureIoTClient | 28:add19eb7defa | 87 | (frame_body_size == 0)) |
AzureIoTClient | 28:add19eb7defa | 88 | { |
AzureIoTClient | 23:1111ee8bcba4 | 89 | LogError("Bad SASL frame size"); |
Azure.IoT Build | 0:6ae2f7bca550 | 90 | |
AzureIoTClient | 28:add19eb7defa | 91 | /* Codes_SRS_SASL_FRAME_CODEC_01_049: [If any error occurs while decoding a frame, the decoder shall call the on_sasl_frame_codec_error and pass to it the callback_context, both of those being the ones given to sasl_frame_codec_create.] */ |
AzureIoTClient | 28:add19eb7defa | 92 | sasl_frame_codec_instance->on_sasl_frame_codec_error(sasl_frame_codec_instance->callback_context); |
AzureIoTClient | 28:add19eb7defa | 93 | } |
AzureIoTClient | 28:add19eb7defa | 94 | else |
AzureIoTClient | 28:add19eb7defa | 95 | { |
AzureIoTClient | 28:add19eb7defa | 96 | switch (sasl_frame_codec_instance->decode_state) |
AzureIoTClient | 28:add19eb7defa | 97 | { |
AzureIoTClient | 28:add19eb7defa | 98 | default: |
AzureIoTClient | 28:add19eb7defa | 99 | case SASL_FRAME_DECODE_ERROR: |
AzureIoTClient | 28:add19eb7defa | 100 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 101 | |
AzureIoTClient | 28:add19eb7defa | 102 | case SASL_FRAME_DECODE_FRAME: |
AzureIoTClient | 28:add19eb7defa | 103 | sasl_frame_codec_instance->decoded_sasl_frame_value = NULL; |
Azure.IoT Build | 0:6ae2f7bca550 | 104 | |
AzureIoTClient | 28:add19eb7defa | 105 | /* Codes_SRS_SASL_FRAME_CODEC_01_039: [sasl_frame_codec shall decode the sasl-frame value as a described type.] */ |
AzureIoTClient | 28:add19eb7defa | 106 | /* Codes_SRS_SASL_FRAME_CODEC_01_048: [Receipt of an empty frame is an irrecoverable error.] */ |
AzureIoTClient | 28:add19eb7defa | 107 | while ((frame_body_size > 0) && |
AzureIoTClient | 28:add19eb7defa | 108 | (sasl_frame_codec_instance->decoded_sasl_frame_value == NULL) && |
AzureIoTClient | 28:add19eb7defa | 109 | (sasl_frame_codec_instance->decode_state != SASL_FRAME_DECODE_ERROR)) |
AzureIoTClient | 28:add19eb7defa | 110 | { |
AzureIoTClient | 28:add19eb7defa | 111 | /* Codes_SRS_SASL_FRAME_CODEC_01_040: [Decoding the sasl-frame type shall be done by feeding the bytes to the decoder create in sasl_frame_codec_create.] */ |
AzureIoTClient | 28:add19eb7defa | 112 | if (amqpvalue_decode_bytes(sasl_frame_codec_instance->decoder, frame_body, 1) != 0) |
AzureIoTClient | 28:add19eb7defa | 113 | { |
AzureIoTClient | 23:1111ee8bcba4 | 114 | LogError("Could not decode SASL frame AMQP value"); |
AzureIoTClient | 23:1111ee8bcba4 | 115 | sasl_frame_codec_instance->decode_state = SASL_FRAME_DECODE_ERROR; |
AzureIoTClient | 28:add19eb7defa | 116 | } |
AzureIoTClient | 28:add19eb7defa | 117 | else |
AzureIoTClient | 28:add19eb7defa | 118 | { |
AzureIoTClient | 28:add19eb7defa | 119 | frame_body_size--; |
AzureIoTClient | 28:add19eb7defa | 120 | frame_body++; |
AzureIoTClient | 28:add19eb7defa | 121 | } |
AzureIoTClient | 28:add19eb7defa | 122 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 123 | |
AzureIoTClient | 28:add19eb7defa | 124 | /* Codes_SRS_SASL_FRAME_CODEC_01_009: [The frame body of a SASL frame MUST contain exactly one AMQP type, whose type encoding MUST have provides="sasl-frame".] */ |
AzureIoTClient | 28:add19eb7defa | 125 | if (frame_body_size > 0) |
AzureIoTClient | 28:add19eb7defa | 126 | { |
AzureIoTClient | 23:1111ee8bcba4 | 127 | LogError("More than one AMQP value detected in SASL frame"); |
AzureIoTClient | 23:1111ee8bcba4 | 128 | sasl_frame_codec_instance->decode_state = SASL_FRAME_DECODE_ERROR; |
Azure.IoT Build | 0:6ae2f7bca550 | 129 | |
AzureIoTClient | 28:add19eb7defa | 130 | /* Codes_SRS_SASL_FRAME_CODEC_01_049: [If any error occurs while decoding a frame, the decoder shall call the on_sasl_frame_codec_error and pass to it the callback_context, both of those being the ones given to sasl_frame_codec_create.] */ |
AzureIoTClient | 28:add19eb7defa | 131 | sasl_frame_codec_instance->on_sasl_frame_codec_error(sasl_frame_codec_instance->callback_context); |
AzureIoTClient | 28:add19eb7defa | 132 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 133 | |
AzureIoTClient | 28:add19eb7defa | 134 | if (sasl_frame_codec_instance->decode_state != SASL_FRAME_DECODE_ERROR) |
AzureIoTClient | 28:add19eb7defa | 135 | { |
AzureIoTClient | 28:add19eb7defa | 136 | /* Codes_SRS_SASL_FRAME_CODEC_01_041: [Once the sasl frame is decoded, the callback on_sasl_frame_received shall be called.] */ |
AzureIoTClient | 28:add19eb7defa | 137 | /* Codes_SRS_SASL_FRAME_CODEC_01_042: [The decoded sasl-frame value and the context passed in sasl_frame_codec_create shall be passed to on_sasl_frame_received.] */ |
AzureIoTClient | 28:add19eb7defa | 138 | sasl_frame_codec_instance->on_sasl_frame_received(sasl_frame_codec_instance->callback_context, sasl_frame_codec_instance->decoded_sasl_frame_value); |
AzureIoTClient | 28:add19eb7defa | 139 | } |
AzureIoTClient | 28:add19eb7defa | 140 | break; |
AzureIoTClient | 28:add19eb7defa | 141 | } |
AzureIoTClient | 28:add19eb7defa | 142 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 143 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 144 | |
Azure.IoT Build | 0:6ae2f7bca550 | 145 | static int encode_bytes(void* context, const unsigned char* bytes, size_t length) |
Azure.IoT Build | 0:6ae2f7bca550 | 146 | { |
AzureIoTClient | 28:add19eb7defa | 147 | PAYLOAD* payload = (PAYLOAD*)context; |
AzureIoTClient | 28:add19eb7defa | 148 | (void)memcpy((unsigned char*)payload->bytes + payload->length, bytes, length); |
AzureIoTClient | 28:add19eb7defa | 149 | payload->length += length; |
AzureIoTClient | 28:add19eb7defa | 150 | return 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 151 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 152 | |
Azure.IoT Build | 0:6ae2f7bca550 | 153 | SASL_FRAME_CODEC_HANDLE sasl_frame_codec_create(FRAME_CODEC_HANDLE frame_codec, ON_SASL_FRAME_RECEIVED on_sasl_frame_received, ON_SASL_FRAME_CODEC_ERROR on_sasl_frame_codec_error, void* callback_context) |
Azure.IoT Build | 0:6ae2f7bca550 | 154 | { |
AzureIoTClient | 28:add19eb7defa | 155 | SASL_FRAME_CODEC_INSTANCE* result; |
Azure.IoT Build | 0:6ae2f7bca550 | 156 | |
AzureIoTClient | 28:add19eb7defa | 157 | /* Codes_SRS_SASL_FRAME_CODEC_01_019: [If any of the arguments frame_codec, on_sasl_frame_received or on_sasl_frame_codec_error is NULL, sasl_frame_codec_create shall return NULL.] */ |
AzureIoTClient | 28:add19eb7defa | 158 | if ((frame_codec == NULL) || |
AzureIoTClient | 28:add19eb7defa | 159 | (on_sasl_frame_received == NULL) || |
AzureIoTClient | 28:add19eb7defa | 160 | (on_sasl_frame_codec_error == NULL)) |
AzureIoTClient | 28:add19eb7defa | 161 | { |
AzureIoTClient | 23:1111ee8bcba4 | 162 | LogError("Bad arguments: frame_codec = %p, on_sasl_frame_received = %p, on_sasl_frame_codec_error = %p", |
AzureIoTClient | 23:1111ee8bcba4 | 163 | frame_codec, on_sasl_frame_received, on_sasl_frame_codec_error); |
AzureIoTClient | 23:1111ee8bcba4 | 164 | result = NULL; |
AzureIoTClient | 28:add19eb7defa | 165 | } |
AzureIoTClient | 28:add19eb7defa | 166 | else |
AzureIoTClient | 28:add19eb7defa | 167 | { |
AzureIoTClient | 28:add19eb7defa | 168 | /* Codes_SRS_SASL_FRAME_CODEC_01_018: [sasl_frame_codec_create shall create an instance of an sasl_frame_codec and return a non-NULL handle to it.] */ |
AzureIoTClient | 28:add19eb7defa | 169 | result = (SASL_FRAME_CODEC_INSTANCE*)malloc(sizeof(SASL_FRAME_CODEC_INSTANCE)); |
AzureIoTClient | 23:1111ee8bcba4 | 170 | if (result == NULL) |
AzureIoTClient | 23:1111ee8bcba4 | 171 | { |
AzureIoTClient | 23:1111ee8bcba4 | 172 | LogError("Cannot allocate memory for SASL frame codec"); |
AzureIoTClient | 23:1111ee8bcba4 | 173 | } |
AzureIoTClient | 23:1111ee8bcba4 | 174 | else |
AzureIoTClient | 23:1111ee8bcba4 | 175 | { |
AzureIoTClient | 28:add19eb7defa | 176 | result->frame_codec = frame_codec; |
AzureIoTClient | 28:add19eb7defa | 177 | result->on_sasl_frame_received = on_sasl_frame_received; |
AzureIoTClient | 28:add19eb7defa | 178 | result->on_sasl_frame_codec_error = on_sasl_frame_codec_error; |
AzureIoTClient | 28:add19eb7defa | 179 | result->callback_context = callback_context; |
AzureIoTClient | 28:add19eb7defa | 180 | result->decode_state = SASL_FRAME_DECODE_FRAME; |
Azure.IoT Build | 0:6ae2f7bca550 | 181 | |
AzureIoTClient | 28:add19eb7defa | 182 | /* Codes_SRS_SASL_FRAME_CODEC_01_022: [sasl_frame_codec_create shall create a decoder to be used for decoding SASL values.] */ |
AzureIoTClient | 28:add19eb7defa | 183 | result->decoder = amqpvalue_decoder_create(amqp_value_decoded, result); |
AzureIoTClient | 28:add19eb7defa | 184 | if (result->decoder == NULL) |
AzureIoTClient | 28:add19eb7defa | 185 | { |
AzureIoTClient | 28:add19eb7defa | 186 | /* Codes_SRS_SASL_FRAME_CODEC_01_023: [If creating the decoder fails, sasl_frame_codec_create shall fail and return NULL.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 187 | LogError("Cannot create AMQP value decoder"); |
AzureIoTClient | 23:1111ee8bcba4 | 188 | free(result); |
AzureIoTClient | 28:add19eb7defa | 189 | result = NULL; |
AzureIoTClient | 28:add19eb7defa | 190 | } |
AzureIoTClient | 28:add19eb7defa | 191 | else |
AzureIoTClient | 28:add19eb7defa | 192 | { |
AzureIoTClient | 28:add19eb7defa | 193 | /* Codes_SRS_SASL_FRAME_CODEC_01_020: [sasl_frame_codec_create shall subscribe for SASL frames with the given frame_codec.] */ |
AzureIoTClient | 28:add19eb7defa | 194 | /* Codes_SRS_SASL_FRAME_CODEC_01_001: [A SASL frame has a type code of 0x01.] */ |
AzureIoTClient | 28:add19eb7defa | 195 | if (frame_codec_subscribe(frame_codec, FRAME_TYPE_SASL, frame_received, result) != 0) |
AzureIoTClient | 28:add19eb7defa | 196 | { |
AzureIoTClient | 28:add19eb7defa | 197 | /* Codes_SRS_SASL_FRAME_CODEC_01_021: [If subscribing for SASL frames fails, sasl_frame_codec_create shall fail and return NULL.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 198 | LogError("Cannot subscribe for SASL frames"); |
AzureIoTClient | 23:1111ee8bcba4 | 199 | amqpvalue_decoder_destroy(result->decoder); |
AzureIoTClient | 28:add19eb7defa | 200 | free(result); |
AzureIoTClient | 28:add19eb7defa | 201 | result = NULL; |
AzureIoTClient | 28:add19eb7defa | 202 | } |
AzureIoTClient | 28:add19eb7defa | 203 | } |
AzureIoTClient | 28:add19eb7defa | 204 | } |
AzureIoTClient | 28:add19eb7defa | 205 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 206 | |
AzureIoTClient | 28:add19eb7defa | 207 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 208 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 209 | |
Azure.IoT Build | 0:6ae2f7bca550 | 210 | void sasl_frame_codec_destroy(SASL_FRAME_CODEC_HANDLE sasl_frame_codec) |
Azure.IoT Build | 0:6ae2f7bca550 | 211 | { |
AzureIoTClient | 28:add19eb7defa | 212 | /* Codes_SRS_SASL_FRAME_CODEC_01_026: [If sasl_frame_codec is NULL, sasl_frame_codec_destroy shall do nothing.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 213 | if (sasl_frame_codec == NULL) |
AzureIoTClient | 23:1111ee8bcba4 | 214 | { |
AzureIoTClient | 23:1111ee8bcba4 | 215 | LogError("NULL sasl_frame_codec"); |
AzureIoTClient | 23:1111ee8bcba4 | 216 | } |
AzureIoTClient | 23:1111ee8bcba4 | 217 | else |
AzureIoTClient | 23:1111ee8bcba4 | 218 | { |
AzureIoTClient | 28:add19eb7defa | 219 | /* Codes_SRS_SASL_FRAME_CODEC_01_025: [sasl_frame_codec_destroy shall free all resources associated with the sasl_frame_codec instance.] */ |
AzureIoTClient | 28:add19eb7defa | 220 | SASL_FRAME_CODEC_INSTANCE* sasl_frame_codec_instance = (SASL_FRAME_CODEC_INSTANCE*)sasl_frame_codec; |
Azure.IoT Build | 0:6ae2f7bca550 | 221 | |
AzureIoTClient | 28:add19eb7defa | 222 | /* Codes_SRS_SASL_FRAME_CODEC_01_027: [sasl_frame_codec_destroy shall unsubscribe from receiving SASL frames from the frame_codec that was passed to sasl_frame_codec_create.] */ |
AzureIoTClient | 28:add19eb7defa | 223 | (void)frame_codec_unsubscribe(sasl_frame_codec_instance->frame_codec, FRAME_TYPE_SASL); |
Azure.IoT Build | 0:6ae2f7bca550 | 224 | |
AzureIoTClient | 28:add19eb7defa | 225 | /* Codes_SRS_SASL_FRAME_CODEC_01_028: [The decoder created in sasl_frame_codec_create shall be destroyed by sasl_frame_codec_destroy.] */ |
AzureIoTClient | 28:add19eb7defa | 226 | amqpvalue_decoder_destroy(sasl_frame_codec_instance->decoder); |
AzureIoTClient | 28:add19eb7defa | 227 | free(sasl_frame_codec_instance); |
AzureIoTClient | 28:add19eb7defa | 228 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 229 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 230 | |
Azure.IoT Build | 0:6ae2f7bca550 | 231 | /* Codes_SRS_SASL_FRAME_CODEC_01_029: [sasl_frame_codec_encode_frame shall encode the frame header and sasl_frame_value AMQP value in a SASL frame and on success it shall return 0.] */ |
AzureIoTClient | 30:0407b2db334c | 232 | int sasl_frame_codec_encode_frame(SASL_FRAME_CODEC_HANDLE sasl_frame_codec, AMQP_VALUE sasl_frame_value, ON_BYTES_ENCODED on_bytes_encoded, void* callback_context) |
Azure.IoT Build | 0:6ae2f7bca550 | 233 | { |
AzureIoTClient | 28:add19eb7defa | 234 | int result; |
AzureIoTClient | 28:add19eb7defa | 235 | SASL_FRAME_CODEC_INSTANCE* sasl_frame_codec_instance = (SASL_FRAME_CODEC_INSTANCE*)sasl_frame_codec; |
Azure.IoT Build | 0:6ae2f7bca550 | 236 | |
AzureIoTClient | 28:add19eb7defa | 237 | /* Codes_SRS_SASL_FRAME_CODEC_01_030: [If sasl_frame_codec or sasl_frame_value is NULL, sasl_frame_codec_encode_frame shall fail and return a non-zero value.] */ |
AzureIoTClient | 28:add19eb7defa | 238 | if ((sasl_frame_codec == NULL) || |
AzureIoTClient | 28:add19eb7defa | 239 | (sasl_frame_value == NULL)) |
AzureIoTClient | 28:add19eb7defa | 240 | { |
AzureIoTClient | 28:add19eb7defa | 241 | /* Codes_SRS_SASL_FRAME_CODEC_01_034: [If any error occurs during encoding, sasl_frame_codec_encode_frame shall fail and return a non-zero value.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 242 | LogError("Bad arguments: sasl_frame_codec = %p, sasl_frame_value = %p", |
AzureIoTClient | 23:1111ee8bcba4 | 243 | sasl_frame_codec, sasl_frame_value); |
AzureIoTClient | 23:1111ee8bcba4 | 244 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 245 | } |
AzureIoTClient | 28:add19eb7defa | 246 | else |
AzureIoTClient | 28:add19eb7defa | 247 | { |
AzureIoTClient | 28:add19eb7defa | 248 | AMQP_VALUE descriptor; |
AzureIoTClient | 28:add19eb7defa | 249 | uint64_t sasl_frame_descriptor_ulong; |
AzureIoTClient | 23:1111ee8bcba4 | 250 | size_t encoded_size; |
Azure.IoT Build | 0:6ae2f7bca550 | 251 | |
AzureIoTClient | 23:1111ee8bcba4 | 252 | if ((descriptor = amqpvalue_get_inplace_descriptor(sasl_frame_value)) == NULL) |
AzureIoTClient | 23:1111ee8bcba4 | 253 | { |
AzureIoTClient | 23:1111ee8bcba4 | 254 | /* Codes_SRS_SASL_FRAME_CODEC_01_034: [If any error occurs during encoding, sasl_frame_codec_encode_frame shall fail and return a non-zero value.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 255 | LogError("Cannot get SASL frame descriptor AMQP value"); |
AzureIoTClient | 23:1111ee8bcba4 | 256 | result = __FAILURE__; |
AzureIoTClient | 23:1111ee8bcba4 | 257 | } |
AzureIoTClient | 23:1111ee8bcba4 | 258 | else if (amqpvalue_get_ulong(descriptor, &sasl_frame_descriptor_ulong) != 0) |
AzureIoTClient | 23:1111ee8bcba4 | 259 | { |
AzureIoTClient | 23:1111ee8bcba4 | 260 | /* Codes_SRS_SASL_FRAME_CODEC_01_034: [If any error occurs during encoding, sasl_frame_codec_encode_frame shall fail and return a non-zero value.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 261 | LogError("Cannot get SASL frame descriptor ulong"); |
AzureIoTClient | 23:1111ee8bcba4 | 262 | result = __FAILURE__; |
AzureIoTClient | 23:1111ee8bcba4 | 263 | } |
AzureIoTClient | 23:1111ee8bcba4 | 264 | /* Codes_SRS_SASL_FRAME_CODEC_01_047: [The frame body of a SASL frame MUST contain exactly one AMQP type, whose type encoding MUST have provides="sasl-frame".] */ |
AzureIoTClient | 23:1111ee8bcba4 | 265 | else if ((sasl_frame_descriptor_ulong < SASL_MECHANISMS) || |
AzureIoTClient | 28:add19eb7defa | 266 | (sasl_frame_descriptor_ulong > SASL_OUTCOME)) |
AzureIoTClient | 28:add19eb7defa | 267 | { |
AzureIoTClient | 28:add19eb7defa | 268 | /* Codes_SRS_SASL_FRAME_CODEC_01_034: [If any error occurs during encoding, sasl_frame_codec_encode_frame shall fail and return a non-zero value.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 269 | LogError("Bad SASL frame descriptor"); |
AzureIoTClient | 23:1111ee8bcba4 | 270 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 271 | } |
AzureIoTClient | 28:add19eb7defa | 272 | /* Codes_SRS_SASL_FRAME_CODEC_01_032: [The payload frame size shall be computed based on the encoded size of the sasl_frame_value and its fields.] */ |
AzureIoTClient | 28:add19eb7defa | 273 | /* Codes_SRS_SASL_FRAME_CODEC_01_033: [The encoded size of the sasl_frame_value and its fields shall be obtained by calling amqpvalue_get_encoded_size.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 274 | else if (amqpvalue_get_encoded_size(sasl_frame_value, &encoded_size) != 0) |
AzureIoTClient | 23:1111ee8bcba4 | 275 | { |
AzureIoTClient | 23:1111ee8bcba4 | 276 | /* Codes_SRS_SASL_FRAME_CODEC_01_034: [If any error occurs during encoding, sasl_frame_codec_encode_frame shall fail and return a non-zero value.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 277 | LogError("Cannot get SASL frame encoded size"); |
AzureIoTClient | 23:1111ee8bcba4 | 278 | result = __FAILURE__; |
AzureIoTClient | 23:1111ee8bcba4 | 279 | } |
AzureIoTClient | 28:add19eb7defa | 280 | /* Codes_SRS_SASL_FRAME_CODEC_01_016: [The maximum size of a SASL frame is defined by MIN-MAX-FRAME-SIZE.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 281 | else if (encoded_size > MIX_MAX_FRAME_SIZE - 8) |
AzureIoTClient | 28:add19eb7defa | 282 | { |
AzureIoTClient | 28:add19eb7defa | 283 | /* Codes_SRS_SASL_FRAME_CODEC_01_034: [If any error occurs during encoding, sasl_frame_codec_encode_frame shall fail and return a non-zero value.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 284 | LogError("SASL frame encoded size too big"); |
AzureIoTClient | 23:1111ee8bcba4 | 285 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 286 | } |
AzureIoTClient | 28:add19eb7defa | 287 | else |
AzureIoTClient | 28:add19eb7defa | 288 | { |
AzureIoTClient | 28:add19eb7defa | 289 | unsigned char* sasl_frame_bytes = (unsigned char*)malloc(encoded_size); |
AzureIoTClient | 28:add19eb7defa | 290 | if (sasl_frame_bytes == NULL) |
AzureIoTClient | 28:add19eb7defa | 291 | { |
AzureIoTClient | 23:1111ee8bcba4 | 292 | LogError("Cannot allocate SASL frame bytes"); |
AzureIoTClient | 23:1111ee8bcba4 | 293 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 294 | } |
AzureIoTClient | 28:add19eb7defa | 295 | else |
AzureIoTClient | 28:add19eb7defa | 296 | { |
AzureIoTClient | 28:add19eb7defa | 297 | PAYLOAD payload; |
AzureIoTClient | 6:641a9672db08 | 298 | |
AzureIoTClient | 6:641a9672db08 | 299 | payload.bytes = sasl_frame_bytes; |
AzureIoTClient | 6:641a9672db08 | 300 | payload.length = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 301 | |
AzureIoTClient | 28:add19eb7defa | 302 | if (amqpvalue_encode(sasl_frame_value, encode_bytes, &payload) != 0) |
AzureIoTClient | 28:add19eb7defa | 303 | { |
AzureIoTClient | 23:1111ee8bcba4 | 304 | LogError("Cannot encode SASL frame value"); |
AzureIoTClient | 23:1111ee8bcba4 | 305 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 306 | } |
AzureIoTClient | 28:add19eb7defa | 307 | else |
AzureIoTClient | 28:add19eb7defa | 308 | { |
AzureIoTClient | 28:add19eb7defa | 309 | /* Codes_SRS_SASL_FRAME_CODEC_01_031: [sasl_frame_codec_encode_frame shall encode the frame header and its contents by using frame_codec_encode_frame.] */ |
AzureIoTClient | 28:add19eb7defa | 310 | /* Codes_SRS_SASL_FRAME_CODEC_01_012: [Bytes 6 and 7 of the header are ignored.] */ |
AzureIoTClient | 28:add19eb7defa | 311 | /* Codes_SRS_SASL_FRAME_CODEC_01_013: [Implementations SHOULD set these to 0x00.] */ |
AzureIoTClient | 28:add19eb7defa | 312 | /* Codes_SRS_SASL_FRAME_CODEC_01_014: [The extended header is ignored.] */ |
AzureIoTClient | 28:add19eb7defa | 313 | /* Codes_SRS_SASL_FRAME_CODEC_01_015: [Implementations SHOULD therefore set DOFF to 0x02.] */ |
AzureIoTClient | 28:add19eb7defa | 314 | if (frame_codec_encode_frame(sasl_frame_codec_instance->frame_codec, FRAME_TYPE_SASL, &payload, 1, NULL, 0, on_bytes_encoded, callback_context) != 0) |
AzureIoTClient | 28:add19eb7defa | 315 | { |
AzureIoTClient | 28:add19eb7defa | 316 | /* Codes_SRS_SASL_FRAME_CODEC_01_034: [If any error occurs during encoding, sasl_frame_codec_encode_frame shall fail and return a non-zero value.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 317 | LogError("Cannot encode SASL frame"); |
AzureIoTClient | 23:1111ee8bcba4 | 318 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 319 | } |
AzureIoTClient | 28:add19eb7defa | 320 | else |
AzureIoTClient | 28:add19eb7defa | 321 | { |
AzureIoTClient | 28:add19eb7defa | 322 | result = 0; |
AzureIoTClient | 28:add19eb7defa | 323 | } |
AzureIoTClient | 28:add19eb7defa | 324 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 325 | |
AzureIoTClient | 28:add19eb7defa | 326 | free(sasl_frame_bytes); |
AzureIoTClient | 28:add19eb7defa | 327 | } |
AzureIoTClient | 28:add19eb7defa | 328 | } |
AzureIoTClient | 28:add19eb7defa | 329 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 330 | |
AzureIoTClient | 28:add19eb7defa | 331 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 332 | } |