A small memory footprint AMQP implimentation
Dependents: iothub_client_sample_amqp remote_monitoring simplesample_amqp
sasl_frame_codec.c@20:206846c14c80, 2017-03-10 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Fri Mar 10 11:47:49 2017 -0800
- Revision:
- 20:206846c14c80
- Parent:
- 19:000ab4e6a2c1
- Child:
- 23:1111ee8bcba4
1.1.9
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" |
Azure.IoT Build | 0:6ae2f7bca550 | 10 | #include "azure_uamqp_c/sasl_frame_codec.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 11 | #include "azure_uamqp_c/frame_codec.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 12 | #include "azure_uamqp_c/amqpvalue.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 13 | #include "azure_uamqp_c/amqp_definitions.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 14 | |
Azure.IoT Build | 0:6ae2f7bca550 | 15 | /* Requirements implemented by design or by other modules */ |
Azure.IoT Build | 0:6ae2f7bca550 | 16 | /* Codes_SRS_SASL_FRAME_CODEC_01_011: [A SASL frame has a type code of 0x01.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 17 | /* 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 | 18 | |
Azure.IoT Build | 0:6ae2f7bca550 | 19 | #define MIX_MAX_FRAME_SIZE 512 |
Azure.IoT Build | 0:6ae2f7bca550 | 20 | |
Azure.IoT Build | 0:6ae2f7bca550 | 21 | typedef enum SASL_FRAME_DECODE_STATE_TAG |
Azure.IoT Build | 0:6ae2f7bca550 | 22 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 23 | SASL_FRAME_DECODE_FRAME, |
Azure.IoT Build | 0:6ae2f7bca550 | 24 | SASL_FRAME_DECODE_ERROR |
Azure.IoT Build | 0:6ae2f7bca550 | 25 | } SASL_FRAME_DECODE_STATE; |
Azure.IoT Build | 0:6ae2f7bca550 | 26 | |
Azure.IoT Build | 0:6ae2f7bca550 | 27 | typedef struct SASL_FRAME_CODEC_INSTANCE_TAG |
Azure.IoT Build | 0:6ae2f7bca550 | 28 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 29 | FRAME_CODEC_HANDLE frame_codec; |
Azure.IoT Build | 0:6ae2f7bca550 | 30 | |
Azure.IoT Build | 0:6ae2f7bca550 | 31 | /* decode */ |
Azure.IoT Build | 0:6ae2f7bca550 | 32 | ON_SASL_FRAME_RECEIVED on_sasl_frame_received; |
Azure.IoT Build | 0:6ae2f7bca550 | 33 | ON_SASL_FRAME_CODEC_ERROR on_sasl_frame_codec_error; |
Azure.IoT Build | 0:6ae2f7bca550 | 34 | void* callback_context; |
Azure.IoT Build | 0:6ae2f7bca550 | 35 | AMQPVALUE_DECODER_HANDLE decoder; |
Azure.IoT Build | 0:6ae2f7bca550 | 36 | SASL_FRAME_DECODE_STATE decode_state; |
Azure.IoT Build | 0:6ae2f7bca550 | 37 | AMQP_VALUE decoded_sasl_frame_value; |
Azure.IoT Build | 0:6ae2f7bca550 | 38 | } SASL_FRAME_CODEC_INSTANCE; |
Azure.IoT Build | 0:6ae2f7bca550 | 39 | |
Azure.IoT Build | 0:6ae2f7bca550 | 40 | static void amqp_value_decoded(void* context, AMQP_VALUE decoded_value) |
Azure.IoT Build | 0:6ae2f7bca550 | 41 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 42 | SASL_FRAME_CODEC_INSTANCE* sasl_frame_codec_instance = (SASL_FRAME_CODEC_INSTANCE*)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 43 | AMQP_VALUE descriptor = amqpvalue_get_inplace_descriptor(decoded_value); |
Azure.IoT Build | 0:6ae2f7bca550 | 44 | |
Azure.IoT Build | 0:6ae2f7bca550 | 45 | if (descriptor == NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 46 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 47 | sasl_frame_codec_instance->decode_state = SASL_FRAME_DECODE_ERROR; |
Azure.IoT Build | 0:6ae2f7bca550 | 48 | |
Azure.IoT Build | 0:6ae2f7bca550 | 49 | /* 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.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 50 | sasl_frame_codec_instance->on_sasl_frame_codec_error(sasl_frame_codec_instance->callback_context); |
Azure.IoT Build | 0:6ae2f7bca550 | 51 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 52 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 53 | { |
AzureIoTClient | 16:22a72cf8e416 | 54 | /* 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".] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 55 | if (!is_sasl_mechanisms_type_by_descriptor(descriptor) && |
Azure.IoT Build | 0:6ae2f7bca550 | 56 | !is_sasl_init_type_by_descriptor(descriptor) && |
Azure.IoT Build | 0:6ae2f7bca550 | 57 | !is_sasl_challenge_type_by_descriptor(descriptor) && |
Azure.IoT Build | 0:6ae2f7bca550 | 58 | !is_sasl_response_type_by_descriptor(descriptor) && |
Azure.IoT Build | 0:6ae2f7bca550 | 59 | !is_sasl_outcome_type_by_descriptor(descriptor)) |
Azure.IoT Build | 0:6ae2f7bca550 | 60 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 61 | sasl_frame_codec_instance->decode_state = SASL_FRAME_DECODE_ERROR; |
Azure.IoT Build | 0:6ae2f7bca550 | 62 | |
Azure.IoT Build | 0:6ae2f7bca550 | 63 | /* 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.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 64 | sasl_frame_codec_instance->on_sasl_frame_codec_error(sasl_frame_codec_instance->callback_context); |
Azure.IoT Build | 0:6ae2f7bca550 | 65 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 66 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 67 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 68 | sasl_frame_codec_instance->decoded_sasl_frame_value = decoded_value; |
Azure.IoT Build | 0:6ae2f7bca550 | 69 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 70 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 71 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 72 | |
Azure.IoT Build | 0:6ae2f7bca550 | 73 | 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 | 74 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 75 | SASL_FRAME_CODEC_INSTANCE* sasl_frame_codec_instance = (SASL_FRAME_CODEC_INSTANCE*)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 76 | |
Azure.IoT Build | 0:6ae2f7bca550 | 77 | /* Codes_SRS_SASL_FRAME_CODEC_01_006: [Bytes 6 and 7 of the header are ignored.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 78 | (void)type_specific; |
Azure.IoT Build | 0:6ae2f7bca550 | 79 | /* Codes_SRS_SASL_FRAME_CODEC_01_007: [The extended header is ignored.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 80 | |
Azure.IoT Build | 0:6ae2f7bca550 | 81 | /* Codes_SRS_SASL_FRAME_CODEC_01_008: [The maximum size of a SASL frame is defined by MIN-MAX-FRAME-SIZE.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 82 | if ((type_specific_size + frame_body_size + 6 > MIX_MAX_FRAME_SIZE) || |
Azure.IoT Build | 0:6ae2f7bca550 | 83 | /* Codes_SRS_SASL_FRAME_CODEC_01_010: [Receipt of an empty frame is an irrecoverable error.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 84 | (frame_body_size == 0)) |
Azure.IoT Build | 0:6ae2f7bca550 | 85 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 86 | /* error */ |
Azure.IoT Build | 0:6ae2f7bca550 | 87 | |
Azure.IoT Build | 0:6ae2f7bca550 | 88 | /* 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.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 89 | sasl_frame_codec_instance->on_sasl_frame_codec_error(sasl_frame_codec_instance->callback_context); |
Azure.IoT Build | 0:6ae2f7bca550 | 90 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 91 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 92 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 93 | switch (sasl_frame_codec_instance->decode_state) |
Azure.IoT Build | 0:6ae2f7bca550 | 94 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 95 | default: |
Azure.IoT Build | 0:6ae2f7bca550 | 96 | case SASL_FRAME_DECODE_ERROR: |
Azure.IoT Build | 0:6ae2f7bca550 | 97 | /* error */ |
Azure.IoT Build | 0:6ae2f7bca550 | 98 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 99 | |
Azure.IoT Build | 0:6ae2f7bca550 | 100 | case SASL_FRAME_DECODE_FRAME: |
Azure.IoT Build | 0:6ae2f7bca550 | 101 | sasl_frame_codec_instance->decoded_sasl_frame_value = NULL; |
Azure.IoT Build | 0:6ae2f7bca550 | 102 | |
Azure.IoT Build | 0:6ae2f7bca550 | 103 | /* Codes_SRS_SASL_FRAME_CODEC_01_039: [sasl_frame_codec shall decode the sasl-frame value as a described type.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 104 | /* Codes_SRS_SASL_FRAME_CODEC_01_048: [Receipt of an empty frame is an irrecoverable error.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 105 | while ((frame_body_size > 0) && |
Azure.IoT Build | 0:6ae2f7bca550 | 106 | (sasl_frame_codec_instance->decoded_sasl_frame_value == NULL) && |
Azure.IoT Build | 0:6ae2f7bca550 | 107 | (sasl_frame_codec_instance->decode_state != SASL_FRAME_DECODE_ERROR)) |
Azure.IoT Build | 0:6ae2f7bca550 | 108 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 109 | /* 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.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 110 | if (amqpvalue_decode_bytes(sasl_frame_codec_instance->decoder, frame_body, 1) != 0) |
Azure.IoT Build | 0:6ae2f7bca550 | 111 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 112 | sasl_frame_codec_instance->decode_state = SASL_FRAME_DECODE_ERROR; |
Azure.IoT Build | 0:6ae2f7bca550 | 113 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 114 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 115 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 116 | frame_body_size--; |
Azure.IoT Build | 0:6ae2f7bca550 | 117 | frame_body++; |
Azure.IoT Build | 0:6ae2f7bca550 | 118 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 119 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 120 | |
AzureIoTClient | 16:22a72cf8e416 | 121 | /* 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".] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 122 | if (frame_body_size > 0) |
Azure.IoT Build | 0:6ae2f7bca550 | 123 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 124 | sasl_frame_codec_instance->decode_state = SASL_FRAME_DECODE_ERROR; |
Azure.IoT Build | 0:6ae2f7bca550 | 125 | |
Azure.IoT Build | 0:6ae2f7bca550 | 126 | /* 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.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 127 | sasl_frame_codec_instance->on_sasl_frame_codec_error(sasl_frame_codec_instance->callback_context); |
Azure.IoT Build | 0:6ae2f7bca550 | 128 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 129 | |
Azure.IoT Build | 0:6ae2f7bca550 | 130 | if (sasl_frame_codec_instance->decode_state == SASL_FRAME_DECODE_ERROR) |
Azure.IoT Build | 0:6ae2f7bca550 | 131 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 132 | /* error */ |
Azure.IoT Build | 0:6ae2f7bca550 | 133 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 134 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 135 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 136 | /* Codes_SRS_SASL_FRAME_CODEC_01_041: [Once the sasl frame is decoded, the callback on_sasl_frame_received shall be called.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 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.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 138 | sasl_frame_codec_instance->on_sasl_frame_received(sasl_frame_codec_instance->callback_context, sasl_frame_codec_instance->decoded_sasl_frame_value); |
Azure.IoT Build | 0:6ae2f7bca550 | 139 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 140 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 141 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 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 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 147 | PAYLOAD* payload = (PAYLOAD*)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 148 | (void)memcpy((unsigned char*)payload->bytes + payload->length, bytes, length); |
Azure.IoT Build | 0:6ae2f7bca550 | 149 | payload->length += length; |
Azure.IoT Build | 0:6ae2f7bca550 | 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 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 155 | SASL_FRAME_CODEC_INSTANCE* result; |
Azure.IoT Build | 0:6ae2f7bca550 | 156 | |
Azure.IoT Build | 0:6ae2f7bca550 | 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.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 158 | if ((frame_codec == NULL) || |
Azure.IoT Build | 0:6ae2f7bca550 | 159 | (on_sasl_frame_received == NULL) || |
Azure.IoT Build | 0:6ae2f7bca550 | 160 | (on_sasl_frame_codec_error == NULL)) |
Azure.IoT Build | 0:6ae2f7bca550 | 161 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 162 | result = NULL; |
Azure.IoT Build | 0:6ae2f7bca550 | 163 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 164 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 165 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 166 | /* 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 | 20:206846c14c80 | 167 | result = (SASL_FRAME_CODEC_INSTANCE*)malloc(sizeof(SASL_FRAME_CODEC_INSTANCE)); |
Azure.IoT Build | 0:6ae2f7bca550 | 168 | if (result != NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 169 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 170 | result->frame_codec = frame_codec; |
Azure.IoT Build | 0:6ae2f7bca550 | 171 | result->on_sasl_frame_received = on_sasl_frame_received; |
Azure.IoT Build | 0:6ae2f7bca550 | 172 | result->on_sasl_frame_codec_error = on_sasl_frame_codec_error; |
Azure.IoT Build | 0:6ae2f7bca550 | 173 | result->callback_context = callback_context; |
Azure.IoT Build | 0:6ae2f7bca550 | 174 | result->decode_state = SASL_FRAME_DECODE_FRAME; |
Azure.IoT Build | 0:6ae2f7bca550 | 175 | |
Azure.IoT Build | 0:6ae2f7bca550 | 176 | /* Codes_SRS_SASL_FRAME_CODEC_01_022: [sasl_frame_codec_create shall create a decoder to be used for decoding SASL values.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 177 | result->decoder = amqpvalue_decoder_create(amqp_value_decoded, result); |
Azure.IoT Build | 0:6ae2f7bca550 | 178 | if (result->decoder == NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 179 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 180 | /* Codes_SRS_SASL_FRAME_CODEC_01_023: [If creating the decoder fails, sasl_frame_codec_create shall fail and return NULL.] */ |
AzureIoTClient | 20:206846c14c80 | 181 | free(result); |
Azure.IoT Build | 0:6ae2f7bca550 | 182 | result = NULL; |
Azure.IoT Build | 0:6ae2f7bca550 | 183 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 184 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 185 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 186 | /* Codes_SRS_SASL_FRAME_CODEC_01_020: [sasl_frame_codec_create shall subscribe for SASL frames with the given frame_codec.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 187 | /* Codes_SRS_SASL_FRAME_CODEC_01_001: [A SASL frame has a type code of 0x01.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 188 | if (frame_codec_subscribe(frame_codec, FRAME_TYPE_SASL, frame_received, result) != 0) |
Azure.IoT Build | 0:6ae2f7bca550 | 189 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 190 | /* Codes_SRS_SASL_FRAME_CODEC_01_021: [If subscribing for SASL frames fails, sasl_frame_codec_create shall fail and return NULL.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 191 | amqpvalue_decoder_destroy(result->decoder); |
AzureIoTClient | 20:206846c14c80 | 192 | free(result); |
Azure.IoT Build | 0:6ae2f7bca550 | 193 | result = NULL; |
Azure.IoT Build | 0:6ae2f7bca550 | 194 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 195 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 196 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 197 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 198 | |
Azure.IoT Build | 0:6ae2f7bca550 | 199 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 200 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 201 | |
Azure.IoT Build | 0:6ae2f7bca550 | 202 | void sasl_frame_codec_destroy(SASL_FRAME_CODEC_HANDLE sasl_frame_codec) |
Azure.IoT Build | 0:6ae2f7bca550 | 203 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 204 | /* Codes_SRS_SASL_FRAME_CODEC_01_026: [If sasl_frame_codec is NULL, sasl_frame_codec_destroy shall do nothing.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 205 | if (sasl_frame_codec != NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 206 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 207 | /* Codes_SRS_SASL_FRAME_CODEC_01_025: [sasl_frame_codec_destroy shall free all resources associated with the sasl_frame_codec instance.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 208 | SASL_FRAME_CODEC_INSTANCE* sasl_frame_codec_instance = (SASL_FRAME_CODEC_INSTANCE*)sasl_frame_codec; |
Azure.IoT Build | 0:6ae2f7bca550 | 209 | |
Azure.IoT Build | 0:6ae2f7bca550 | 210 | /* 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.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 211 | (void)frame_codec_unsubscribe(sasl_frame_codec_instance->frame_codec, FRAME_TYPE_SASL); |
Azure.IoT Build | 0:6ae2f7bca550 | 212 | |
Azure.IoT Build | 0:6ae2f7bca550 | 213 | /* Codes_SRS_SASL_FRAME_CODEC_01_028: [The decoder created in sasl_frame_codec_create shall be destroyed by sasl_frame_codec_destroy.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 214 | amqpvalue_decoder_destroy(sasl_frame_codec_instance->decoder); |
AzureIoTClient | 20:206846c14c80 | 215 | free(sasl_frame_codec_instance); |
Azure.IoT Build | 0:6ae2f7bca550 | 216 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 217 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 218 | |
Azure.IoT Build | 0:6ae2f7bca550 | 219 | /* 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.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 220 | int sasl_frame_codec_encode_frame(SASL_FRAME_CODEC_HANDLE sasl_frame_codec, const AMQP_VALUE sasl_frame_value, ON_BYTES_ENCODED on_bytes_encoded, void* callback_context) |
Azure.IoT Build | 0:6ae2f7bca550 | 221 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 222 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 223 | SASL_FRAME_CODEC_INSTANCE* sasl_frame_codec_instance = (SASL_FRAME_CODEC_INSTANCE*)sasl_frame_codec; |
Azure.IoT Build | 0:6ae2f7bca550 | 224 | |
Azure.IoT Build | 0:6ae2f7bca550 | 225 | /* 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.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 226 | if ((sasl_frame_codec == NULL) || |
Azure.IoT Build | 0:6ae2f7bca550 | 227 | (sasl_frame_value == NULL)) |
Azure.IoT Build | 0:6ae2f7bca550 | 228 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 229 | /* 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 | 19:000ab4e6a2c1 | 230 | result = __FAILURE__; |
Azure.IoT Build | 0:6ae2f7bca550 | 231 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 232 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 233 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 234 | AMQP_VALUE descriptor; |
Azure.IoT Build | 0:6ae2f7bca550 | 235 | uint64_t sasl_frame_descriptor_ulong; |
Azure.IoT Build | 0:6ae2f7bca550 | 236 | size_t encoded_size; |
Azure.IoT Build | 0:6ae2f7bca550 | 237 | |
Azure.IoT Build | 0:6ae2f7bca550 | 238 | if (((descriptor = amqpvalue_get_inplace_descriptor(sasl_frame_value)) == NULL) || |
Azure.IoT Build | 0:6ae2f7bca550 | 239 | (amqpvalue_get_ulong(descriptor, &sasl_frame_descriptor_ulong) != 0) || |
AzureIoTClient | 16:22a72cf8e416 | 240 | /* 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".] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 241 | (sasl_frame_descriptor_ulong < SASL_MECHANISMS) || |
Azure.IoT Build | 0:6ae2f7bca550 | 242 | (sasl_frame_descriptor_ulong > SASL_OUTCOME)) |
Azure.IoT Build | 0:6ae2f7bca550 | 243 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 244 | /* 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 | 19:000ab4e6a2c1 | 245 | result = __FAILURE__; |
Azure.IoT Build | 0:6ae2f7bca550 | 246 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 247 | /* 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.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 248 | /* 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.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 249 | else if ((amqpvalue_get_encoded_size(sasl_frame_value, &encoded_size) != 0) || |
Azure.IoT Build | 0:6ae2f7bca550 | 250 | /* 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 | 251 | (encoded_size > MIX_MAX_FRAME_SIZE - 8)) |
Azure.IoT Build | 0:6ae2f7bca550 | 252 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 253 | /* 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 | 19:000ab4e6a2c1 | 254 | result = __FAILURE__; |
Azure.IoT Build | 0:6ae2f7bca550 | 255 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 256 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 257 | { |
AzureIoTClient | 20:206846c14c80 | 258 | unsigned char* sasl_frame_bytes = (unsigned char*)malloc(encoded_size); |
Azure.IoT Build | 0:6ae2f7bca550 | 259 | if (sasl_frame_bytes == NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 260 | { |
AzureIoTClient | 19:000ab4e6a2c1 | 261 | result = __FAILURE__; |
Azure.IoT Build | 0:6ae2f7bca550 | 262 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 263 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 264 | { |
AzureIoTClient | 6:641a9672db08 | 265 | PAYLOAD payload; |
AzureIoTClient | 6:641a9672db08 | 266 | |
AzureIoTClient | 6:641a9672db08 | 267 | payload.bytes = sasl_frame_bytes; |
AzureIoTClient | 6:641a9672db08 | 268 | payload.length = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 269 | |
Azure.IoT Build | 0:6ae2f7bca550 | 270 | if (amqpvalue_encode(sasl_frame_value, encode_bytes, &payload) != 0) |
Azure.IoT Build | 0:6ae2f7bca550 | 271 | { |
AzureIoTClient | 19:000ab4e6a2c1 | 272 | result = __FAILURE__; |
Azure.IoT Build | 0:6ae2f7bca550 | 273 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 274 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 275 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 276 | /* 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.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 277 | /* Codes_SRS_SASL_FRAME_CODEC_01_012: [Bytes 6 and 7 of the header are ignored.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 278 | /* Codes_SRS_SASL_FRAME_CODEC_01_013: [Implementations SHOULD set these to 0x00.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 279 | /* Codes_SRS_SASL_FRAME_CODEC_01_014: [The extended header is ignored.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 280 | /* Codes_SRS_SASL_FRAME_CODEC_01_015: [Implementations SHOULD therefore set DOFF to 0x02.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 281 | if (frame_codec_encode_frame(sasl_frame_codec_instance->frame_codec, FRAME_TYPE_SASL, &payload, 1, NULL, 0, on_bytes_encoded, callback_context) != 0) |
Azure.IoT Build | 0:6ae2f7bca550 | 282 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 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 | 19:000ab4e6a2c1 | 284 | result = __FAILURE__; |
Azure.IoT Build | 0:6ae2f7bca550 | 285 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 286 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 287 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 288 | result = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 289 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 290 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 291 | |
AzureIoTClient | 20:206846c14c80 | 292 | free(sasl_frame_bytes); |
Azure.IoT Build | 0:6ae2f7bca550 | 293 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 294 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 295 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 296 | |
Azure.IoT Build | 0:6ae2f7bca550 | 297 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 298 | } |