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