A small memory footprint AMQP implimentation

Dependents:   iothub_client_sample_amqp remote_monitoring simplesample_amqp

Committer:
AzureIoTClient
Date:
Fri Apr 21 14:50:32 2017 -0700
Revision:
23:1111ee8bcba4
Parent:
20:206846c14c80
Child:
28:add19eb7defa
1.1.13

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 #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 {
Azure.IoT Build 0:6ae2f7bca550 24 SASL_FRAME_DECODE_FRAME,
Azure.IoT Build 0:6ae2f7bca550 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 {
Azure.IoT Build 0:6ae2f7bca550 30 FRAME_CODEC_HANDLE frame_codec;
Azure.IoT Build 0:6ae2f7bca550 31
Azure.IoT Build 0:6ae2f7bca550 32 /* decode */
Azure.IoT Build 0:6ae2f7bca550 33 ON_SASL_FRAME_RECEIVED on_sasl_frame_received;
Azure.IoT Build 0:6ae2f7bca550 34 ON_SASL_FRAME_CODEC_ERROR on_sasl_frame_codec_error;
Azure.IoT Build 0:6ae2f7bca550 35 void* callback_context;
Azure.IoT Build 0:6ae2f7bca550 36 AMQPVALUE_DECODER_HANDLE decoder;
Azure.IoT Build 0:6ae2f7bca550 37 SASL_FRAME_DECODE_STATE decode_state;
Azure.IoT Build 0:6ae2f7bca550 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 {
Azure.IoT Build 0:6ae2f7bca550 43 SASL_FRAME_CODEC_INSTANCE* sasl_frame_codec_instance = (SASL_FRAME_CODEC_INSTANCE*)context;
Azure.IoT Build 0:6ae2f7bca550 44 AMQP_VALUE descriptor = amqpvalue_get_inplace_descriptor(decoded_value);
Azure.IoT Build 0:6ae2f7bca550 45
Azure.IoT Build 0:6ae2f7bca550 46 if (descriptor == NULL)
Azure.IoT Build 0:6ae2f7bca550 47 {
AzureIoTClient 23:1111ee8bcba4 48 LogError("Cannot get frame descriptor");
Azure.IoT Build 0:6ae2f7bca550 49 sasl_frame_codec_instance->decode_state = SASL_FRAME_DECODE_ERROR;
Azure.IoT Build 0:6ae2f7bca550 50
Azure.IoT Build 0:6ae2f7bca550 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.] */
Azure.IoT Build 0:6ae2f7bca550 52 sasl_frame_codec_instance->on_sasl_frame_codec_error(sasl_frame_codec_instance->callback_context);
Azure.IoT Build 0:6ae2f7bca550 53 }
Azure.IoT Build 0:6ae2f7bca550 54 else
Azure.IoT Build 0:6ae2f7bca550 55 {
AzureIoTClient 16:22a72cf8e416 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".] */
Azure.IoT Build 0:6ae2f7bca550 57 if (!is_sasl_mechanisms_type_by_descriptor(descriptor) &&
Azure.IoT Build 0:6ae2f7bca550 58 !is_sasl_init_type_by_descriptor(descriptor) &&
Azure.IoT Build 0:6ae2f7bca550 59 !is_sasl_challenge_type_by_descriptor(descriptor) &&
Azure.IoT Build 0:6ae2f7bca550 60 !is_sasl_response_type_by_descriptor(descriptor) &&
Azure.IoT Build 0:6ae2f7bca550 61 !is_sasl_outcome_type_by_descriptor(descriptor))
Azure.IoT Build 0:6ae2f7bca550 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
Azure.IoT Build 0:6ae2f7bca550 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.] */
Azure.IoT Build 0:6ae2f7bca550 67 sasl_frame_codec_instance->on_sasl_frame_codec_error(sasl_frame_codec_instance->callback_context);
Azure.IoT Build 0:6ae2f7bca550 68 }
Azure.IoT Build 0:6ae2f7bca550 69 else
Azure.IoT Build 0:6ae2f7bca550 70 {
Azure.IoT Build 0:6ae2f7bca550 71 sasl_frame_codec_instance->decoded_sasl_frame_value = decoded_value;
Azure.IoT Build 0:6ae2f7bca550 72 }
Azure.IoT Build 0:6ae2f7bca550 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 {
Azure.IoT Build 0:6ae2f7bca550 78 SASL_FRAME_CODEC_INSTANCE* sasl_frame_codec_instance = (SASL_FRAME_CODEC_INSTANCE*)context;
Azure.IoT Build 0:6ae2f7bca550 79
Azure.IoT Build 0:6ae2f7bca550 80 /* Codes_SRS_SASL_FRAME_CODEC_01_006: [Bytes 6 and 7 of the header are ignored.] */
Azure.IoT Build 0:6ae2f7bca550 81 (void)type_specific;
Azure.IoT Build 0:6ae2f7bca550 82 /* Codes_SRS_SASL_FRAME_CODEC_01_007: [The extended header is ignored.] */
Azure.IoT Build 0:6ae2f7bca550 83
Azure.IoT Build 0:6ae2f7bca550 84 /* 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 85 if ((type_specific_size + frame_body_size + 6 > MIX_MAX_FRAME_SIZE) ||
Azure.IoT Build 0:6ae2f7bca550 86 /* Codes_SRS_SASL_FRAME_CODEC_01_010: [Receipt of an empty frame is an irrecoverable error.] */
Azure.IoT Build 0:6ae2f7bca550 87 (frame_body_size == 0))
Azure.IoT Build 0:6ae2f7bca550 88 {
AzureIoTClient 23:1111ee8bcba4 89 LogError("Bad SASL frame size");
Azure.IoT Build 0:6ae2f7bca550 90
Azure.IoT Build 0:6ae2f7bca550 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.] */
Azure.IoT Build 0:6ae2f7bca550 92 sasl_frame_codec_instance->on_sasl_frame_codec_error(sasl_frame_codec_instance->callback_context);
Azure.IoT Build 0:6ae2f7bca550 93 }
Azure.IoT Build 0:6ae2f7bca550 94 else
Azure.IoT Build 0:6ae2f7bca550 95 {
Azure.IoT Build 0:6ae2f7bca550 96 switch (sasl_frame_codec_instance->decode_state)
Azure.IoT Build 0:6ae2f7bca550 97 {
Azure.IoT Build 0:6ae2f7bca550 98 default:
Azure.IoT Build 0:6ae2f7bca550 99 case SASL_FRAME_DECODE_ERROR:
Azure.IoT Build 0:6ae2f7bca550 100 break;
Azure.IoT Build 0:6ae2f7bca550 101
Azure.IoT Build 0:6ae2f7bca550 102 case SASL_FRAME_DECODE_FRAME:
Azure.IoT Build 0:6ae2f7bca550 103 sasl_frame_codec_instance->decoded_sasl_frame_value = NULL;
Azure.IoT Build 0:6ae2f7bca550 104
Azure.IoT Build 0:6ae2f7bca550 105 /* 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 106 /* Codes_SRS_SASL_FRAME_CODEC_01_048: [Receipt of an empty frame is an irrecoverable error.] */
Azure.IoT Build 0:6ae2f7bca550 107 while ((frame_body_size > 0) &&
Azure.IoT Build 0:6ae2f7bca550 108 (sasl_frame_codec_instance->decoded_sasl_frame_value == NULL) &&
Azure.IoT Build 0:6ae2f7bca550 109 (sasl_frame_codec_instance->decode_state != SASL_FRAME_DECODE_ERROR))
Azure.IoT Build 0:6ae2f7bca550 110 {
Azure.IoT Build 0:6ae2f7bca550 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.] */
Azure.IoT Build 0:6ae2f7bca550 112 if (amqpvalue_decode_bytes(sasl_frame_codec_instance->decoder, frame_body, 1) != 0)
Azure.IoT Build 0:6ae2f7bca550 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;
Azure.IoT Build 0:6ae2f7bca550 116 }
Azure.IoT Build 0:6ae2f7bca550 117 else
Azure.IoT Build 0:6ae2f7bca550 118 {
Azure.IoT Build 0:6ae2f7bca550 119 frame_body_size--;
Azure.IoT Build 0:6ae2f7bca550 120 frame_body++;
Azure.IoT Build 0:6ae2f7bca550 121 }
Azure.IoT Build 0:6ae2f7bca550 122 }
Azure.IoT Build 0:6ae2f7bca550 123
AzureIoTClient 16:22a72cf8e416 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".] */
Azure.IoT Build 0:6ae2f7bca550 125 if (frame_body_size > 0)
Azure.IoT Build 0:6ae2f7bca550 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
Azure.IoT Build 0:6ae2f7bca550 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.] */
Azure.IoT Build 0:6ae2f7bca550 131 sasl_frame_codec_instance->on_sasl_frame_codec_error(sasl_frame_codec_instance->callback_context);
Azure.IoT Build 0:6ae2f7bca550 132 }
Azure.IoT Build 0:6ae2f7bca550 133
AzureIoTClient 23:1111ee8bcba4 134 if (sasl_frame_codec_instance->decode_state != SASL_FRAME_DECODE_ERROR)
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 {
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;
Azure.IoT Build 0:6ae2f7bca550 165 }
Azure.IoT Build 0:6ae2f7bca550 166 else
Azure.IoT Build 0:6ae2f7bca550 167 {
Azure.IoT Build 0:6ae2f7bca550 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 20:206846c14c80 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 {
Azure.IoT Build 0:6ae2f7bca550 176 result->frame_codec = frame_codec;
Azure.IoT Build 0:6ae2f7bca550 177 result->on_sasl_frame_received = on_sasl_frame_received;
Azure.IoT Build 0:6ae2f7bca550 178 result->on_sasl_frame_codec_error = on_sasl_frame_codec_error;
Azure.IoT Build 0:6ae2f7bca550 179 result->callback_context = callback_context;
Azure.IoT Build 0:6ae2f7bca550 180 result->decode_state = SASL_FRAME_DECODE_FRAME;
Azure.IoT Build 0:6ae2f7bca550 181
Azure.IoT Build 0:6ae2f7bca550 182 /* 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 183 result->decoder = amqpvalue_decoder_create(amqp_value_decoded, result);
Azure.IoT Build 0:6ae2f7bca550 184 if (result->decoder == NULL)
Azure.IoT Build 0:6ae2f7bca550 185 {
Azure.IoT Build 0:6ae2f7bca550 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);
Azure.IoT Build 0:6ae2f7bca550 189 result = NULL;
Azure.IoT Build 0:6ae2f7bca550 190 }
Azure.IoT Build 0:6ae2f7bca550 191 else
Azure.IoT Build 0:6ae2f7bca550 192 {
Azure.IoT Build 0:6ae2f7bca550 193 /* 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 194 /* Codes_SRS_SASL_FRAME_CODEC_01_001: [A SASL frame has a type code of 0x01.] */
Azure.IoT Build 0:6ae2f7bca550 195 if (frame_codec_subscribe(frame_codec, FRAME_TYPE_SASL, frame_received, result) != 0)
Azure.IoT Build 0:6ae2f7bca550 196 {
Azure.IoT Build 0:6ae2f7bca550 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 20:206846c14c80 200 free(result);
Azure.IoT Build 0:6ae2f7bca550 201 result = NULL;
Azure.IoT Build 0:6ae2f7bca550 202 }
Azure.IoT Build 0:6ae2f7bca550 203 }
Azure.IoT Build 0:6ae2f7bca550 204 }
Azure.IoT Build 0:6ae2f7bca550 205 }
Azure.IoT Build 0:6ae2f7bca550 206
Azure.IoT Build 0:6ae2f7bca550 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 {
Azure.IoT Build 0:6ae2f7bca550 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 {
Azure.IoT Build 0:6ae2f7bca550 219 /* 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 220 SASL_FRAME_CODEC_INSTANCE* sasl_frame_codec_instance = (SASL_FRAME_CODEC_INSTANCE*)sasl_frame_codec;
Azure.IoT Build 0:6ae2f7bca550 221
Azure.IoT Build 0:6ae2f7bca550 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.] */
Azure.IoT Build 0:6ae2f7bca550 223 (void)frame_codec_unsubscribe(sasl_frame_codec_instance->frame_codec, FRAME_TYPE_SASL);
Azure.IoT Build 0:6ae2f7bca550 224
Azure.IoT Build 0:6ae2f7bca550 225 /* 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 226 amqpvalue_decoder_destroy(sasl_frame_codec_instance->decoder);
AzureIoTClient 20:206846c14c80 227 free(sasl_frame_codec_instance);
Azure.IoT Build 0:6ae2f7bca550 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.] */
Azure.IoT Build 0:6ae2f7bca550 232 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 233 {
Azure.IoT Build 0:6ae2f7bca550 234 int result;
Azure.IoT Build 0:6ae2f7bca550 235 SASL_FRAME_CODEC_INSTANCE* sasl_frame_codec_instance = (SASL_FRAME_CODEC_INSTANCE*)sasl_frame_codec;
Azure.IoT Build 0:6ae2f7bca550 236
Azure.IoT Build 0:6ae2f7bca550 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.] */
Azure.IoT Build 0:6ae2f7bca550 238 if ((sasl_frame_codec == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 239 (sasl_frame_value == NULL))
Azure.IoT Build 0:6ae2f7bca550 240 {
Azure.IoT Build 0:6ae2f7bca550 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__;
Azure.IoT Build 0:6ae2f7bca550 245 }
Azure.IoT Build 0:6ae2f7bca550 246 else
Azure.IoT Build 0:6ae2f7bca550 247 {
Azure.IoT Build 0:6ae2f7bca550 248 AMQP_VALUE descriptor;
Azure.IoT Build 0:6ae2f7bca550 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) ||
Azure.IoT Build 0:6ae2f7bca550 266 (sasl_frame_descriptor_ulong > SASL_OUTCOME))
Azure.IoT Build 0:6ae2f7bca550 267 {
Azure.IoT Build 0:6ae2f7bca550 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__;
Azure.IoT Build 0:6ae2f7bca550 271 }
Azure.IoT Build 0:6ae2f7bca550 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.] */
Azure.IoT Build 0:6ae2f7bca550 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 }
Azure.IoT Build 0:6ae2f7bca550 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)
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 23:1111ee8bcba4 284 LogError("SASL frame encoded size too big");
AzureIoTClient 23:1111ee8bcba4 285 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 286 }
Azure.IoT Build 0:6ae2f7bca550 287 else
Azure.IoT Build 0:6ae2f7bca550 288 {
AzureIoTClient 20:206846c14c80 289 unsigned char* sasl_frame_bytes = (unsigned char*)malloc(encoded_size);
Azure.IoT Build 0:6ae2f7bca550 290 if (sasl_frame_bytes == NULL)
Azure.IoT Build 0:6ae2f7bca550 291 {
AzureIoTClient 23:1111ee8bcba4 292 LogError("Cannot allocate SASL frame bytes");
AzureIoTClient 23:1111ee8bcba4 293 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 294 }
Azure.IoT Build 0:6ae2f7bca550 295 else
Azure.IoT Build 0:6ae2f7bca550 296 {
AzureIoTClient 6:641a9672db08 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
Azure.IoT Build 0:6ae2f7bca550 302 if (amqpvalue_encode(sasl_frame_value, encode_bytes, &payload) != 0)
Azure.IoT Build 0:6ae2f7bca550 303 {
AzureIoTClient 23:1111ee8bcba4 304 LogError("Cannot encode SASL frame value");
AzureIoTClient 23:1111ee8bcba4 305 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 306 }
Azure.IoT Build 0:6ae2f7bca550 307 else
Azure.IoT Build 0:6ae2f7bca550 308 {
Azure.IoT Build 0:6ae2f7bca550 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.] */
Azure.IoT Build 0:6ae2f7bca550 310 /* Codes_SRS_SASL_FRAME_CODEC_01_012: [Bytes 6 and 7 of the header are ignored.] */
Azure.IoT Build 0:6ae2f7bca550 311 /* Codes_SRS_SASL_FRAME_CODEC_01_013: [Implementations SHOULD set these to 0x00.] */
Azure.IoT Build 0:6ae2f7bca550 312 /* Codes_SRS_SASL_FRAME_CODEC_01_014: [The extended header is ignored.] */
Azure.IoT Build 0:6ae2f7bca550 313 /* Codes_SRS_SASL_FRAME_CODEC_01_015: [Implementations SHOULD therefore set DOFF to 0x02.] */
Azure.IoT Build 0:6ae2f7bca550 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)
Azure.IoT Build 0:6ae2f7bca550 315 {
Azure.IoT Build 0:6ae2f7bca550 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__;
Azure.IoT Build 0:6ae2f7bca550 319 }
Azure.IoT Build 0:6ae2f7bca550 320 else
Azure.IoT Build 0:6ae2f7bca550 321 {
Azure.IoT Build 0:6ae2f7bca550 322 result = 0;
Azure.IoT Build 0:6ae2f7bca550 323 }
Azure.IoT Build 0:6ae2f7bca550 324 }
Azure.IoT Build 0:6ae2f7bca550 325
AzureIoTClient 20:206846c14c80 326 free(sasl_frame_bytes);
Azure.IoT Build 0:6ae2f7bca550 327 }
Azure.IoT Build 0:6ae2f7bca550 328 }
Azure.IoT Build 0:6ae2f7bca550 329 }
Azure.IoT Build 0:6ae2f7bca550 330
Azure.IoT Build 0:6ae2f7bca550 331 return result;
Azure.IoT Build 0:6ae2f7bca550 332 }