A small memory footprint AMQP implimentation

Dependents:   iothub_client_sample_amqp remote_monitoring simplesample_amqp

Committer:
AzureIoTClient
Date:
Thu Oct 04 09:16:13 2018 -0700
Revision:
47:365a93fdb5bb
Parent:
28:add19eb7defa
1.2.10

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Azure.IoT Build 0:6ae2f7bca550 1 // Copyright (c) Microsoft. All rights reserved.
Azure.IoT Build 0:6ae2f7bca550 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
Azure.IoT Build 0:6ae2f7bca550 3
Azure.IoT Build 0:6ae2f7bca550 4 #include <stdlib.h>
Azure.IoT Build 0:6ae2f7bca550 5 #include <string.h>
AzureIoTClient 20:206846c14c80 6 #include "azure_c_shared_utility/optimize_size.h"
AzureIoTClient 20:206846c14c80 7 #include "azure_c_shared_utility/gballoc.h"
AzureIoTClient 27:d74f1cea23e1 8 #include "azure_c_shared_utility/xlogging.h"
Azure.IoT Build 0:6ae2f7bca550 9 #include "azure_uamqp_c/sasl_anonymous.h"
Azure.IoT Build 0:6ae2f7bca550 10
Azure.IoT Build 0:6ae2f7bca550 11 typedef struct SASL_ANONYMOUS_INSTANCE_TAG
Azure.IoT Build 0:6ae2f7bca550 12 {
AzureIoTClient 28:add19eb7defa 13 unsigned char dummy;
Azure.IoT Build 0:6ae2f7bca550 14 } SASL_ANONYMOUS_INSTANCE;
Azure.IoT Build 0:6ae2f7bca550 15
AzureIoTClient 23:1111ee8bcba4 16 /* Codes_SRS_SASL_ANONYMOUS_01_001: [`saslanonymous_create` shall return on success a non-NULL handle to a new SASL anonymous mechanism.]*/
AzureIoTClient 23:1111ee8bcba4 17 static CONCRETE_SASL_MECHANISM_HANDLE saslanonymous_create(void* config)
Azure.IoT Build 0:6ae2f7bca550 18 {
AzureIoTClient 23:1111ee8bcba4 19 CONCRETE_SASL_MECHANISM_HANDLE result;
Azure.IoT Build 0:6ae2f7bca550 20
AzureIoTClient 28:add19eb7defa 21 /* Codes_SRS_SASL_ANONYMOUS_01_003: [Since this is the ANONYMOUS SASL mechanism, `config` shall be ignored.]*/
AzureIoTClient 28:add19eb7defa 22 (void)config;
Azure.IoT Build 0:6ae2f7bca550 23
AzureIoTClient 23:1111ee8bcba4 24 result = malloc(sizeof(SASL_ANONYMOUS_INSTANCE));
AzureIoTClient 23:1111ee8bcba4 25 if (result == NULL)
AzureIoTClient 23:1111ee8bcba4 26 {
AzureIoTClient 23:1111ee8bcba4 27 /* Codes_SRS_SASL_ANONYMOUS_01_002: [If allocating the memory needed for the SASL anonymous instance fails then `saslanonymous_create` shall return NULL.] */
AzureIoTClient 23:1111ee8bcba4 28 LogError("Cannot allocate memory for SASL anonymous instance");
AzureIoTClient 23:1111ee8bcba4 29 }
AzureIoTClient 23:1111ee8bcba4 30
AzureIoTClient 23:1111ee8bcba4 31 return result;
Azure.IoT Build 0:6ae2f7bca550 32 }
Azure.IoT Build 0:6ae2f7bca550 33
AzureIoTClient 23:1111ee8bcba4 34 static void saslanonymous_destroy(CONCRETE_SASL_MECHANISM_HANDLE sasl_mechanism_concrete_handle)
Azure.IoT Build 0:6ae2f7bca550 35 {
AzureIoTClient 28:add19eb7defa 36 /* Codes_SRS_SASL_ANONYMOUS_01_005: [If the argument `concrete_sasl_mechanism` is NULL, `saslanonymous_destroy` shall do nothing.]*/
AzureIoTClient 23:1111ee8bcba4 37 if (sasl_mechanism_concrete_handle == NULL)
AzureIoTClient 23:1111ee8bcba4 38 {
AzureIoTClient 23:1111ee8bcba4 39 LogError("NULL sasl_mechanism_concrete_handle");
AzureIoTClient 23:1111ee8bcba4 40 }
AzureIoTClient 23:1111ee8bcba4 41 else
AzureIoTClient 23:1111ee8bcba4 42 {
AzureIoTClient 28:add19eb7defa 43 /* Codes_SRS_SASL_ANONYMOUS_01_004: [`saslanonymous_destroy` shall free all resources associated with the SASL mechanism.] */
AzureIoTClient 28:add19eb7defa 44 free(sasl_mechanism_concrete_handle);
AzureIoTClient 28:add19eb7defa 45 }
Azure.IoT Build 0:6ae2f7bca550 46 }
Azure.IoT Build 0:6ae2f7bca550 47
AzureIoTClient 23:1111ee8bcba4 48 static int saslanonymous_get_init_bytes(CONCRETE_SASL_MECHANISM_HANDLE sasl_mechanism_concrete_handle, SASL_MECHANISM_BYTES* init_bytes)
Azure.IoT Build 0:6ae2f7bca550 49 {
AzureIoTClient 28:add19eb7defa 50 int result;
Azure.IoT Build 0:6ae2f7bca550 51
AzureIoTClient 28:add19eb7defa 52 /* Codes_SRS_SASL_ANONYMOUS_01_007: [If any argument is NULL, `saslanonymous_get_init_bytes` shall return a non-zero value.]*/
AzureIoTClient 28:add19eb7defa 53 if ((sasl_mechanism_concrete_handle == NULL) ||
AzureIoTClient 28:add19eb7defa 54 (init_bytes == NULL))
AzureIoTClient 28:add19eb7defa 55 {
AzureIoTClient 23:1111ee8bcba4 56 LogError("Bad arguments: sasl_mechanism_concrete_handle = %p, init_bytes = %p",
AzureIoTClient 23:1111ee8bcba4 57 sasl_mechanism_concrete_handle, init_bytes);
AzureIoTClient 23:1111ee8bcba4 58 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 59 }
AzureIoTClient 28:add19eb7defa 60 else
AzureIoTClient 28:add19eb7defa 61 {
AzureIoTClient 28:add19eb7defa 62 /* Codes_SRS_SASL_ANONYMOUS_01_012: [The bytes field of `init_buffer` shall be set to NULL.] */
AzureIoTClient 28:add19eb7defa 63 init_bytes->bytes = NULL;
AzureIoTClient 28:add19eb7defa 64 /* Codes_SRS_SASL_ANONYMOUS_01_006: [`saslanonymous_get_init_bytes` shall validate the `concrete_sasl_mechanism` argument and set the length of the `init_bytes` argument to be zero.] */
AzureIoTClient 28:add19eb7defa 65 init_bytes->length = 0;
Azure.IoT Build 0:6ae2f7bca550 66
AzureIoTClient 28:add19eb7defa 67 /* Codes_SRS_SASL_ANONYMOUS_01_011: [On success `saslanonymous_get_init_bytes` shall return zero.] */
AzureIoTClient 28:add19eb7defa 68 result = 0;
AzureIoTClient 28:add19eb7defa 69 }
Azure.IoT Build 0:6ae2f7bca550 70
AzureIoTClient 28:add19eb7defa 71 return result;
Azure.IoT Build 0:6ae2f7bca550 72 }
Azure.IoT Build 0:6ae2f7bca550 73
AzureIoTClient 23:1111ee8bcba4 74 static const char* saslanonymous_get_mechanism_name(CONCRETE_SASL_MECHANISM_HANDLE sasl_mechanism)
Azure.IoT Build 0:6ae2f7bca550 75 {
AzureIoTClient 28:add19eb7defa 76 const char* result;
Azure.IoT Build 0:6ae2f7bca550 77
AzureIoTClient 28:add19eb7defa 78 /* Codes_SRS_SASL_ANONYMOUS_01_009: [If the argument `concrete_sasl_mechanism` is NULL, `saslanonymous_get_mechanism_name` shall return NULL.] */
AzureIoTClient 28:add19eb7defa 79 if (sasl_mechanism == NULL)
AzureIoTClient 28:add19eb7defa 80 {
AzureIoTClient 23:1111ee8bcba4 81 LogError("NULL sasl_mechanism");
AzureIoTClient 23:1111ee8bcba4 82 result = NULL;
AzureIoTClient 28:add19eb7defa 83 }
AzureIoTClient 28:add19eb7defa 84 else
AzureIoTClient 28:add19eb7defa 85 {
AzureIoTClient 28:add19eb7defa 86 /* Codes_SRS_SASL_ANONYMOUS_01_008: [`saslanonymous_get_mechanism_name` shall validate the argument `concrete_sasl_mechanism` and on success it shall return a pointer to the string `ANONYMOUS`.] */
AzureIoTClient 28:add19eb7defa 87 result = "ANONYMOUS";
AzureIoTClient 28:add19eb7defa 88 }
Azure.IoT Build 0:6ae2f7bca550 89
AzureIoTClient 28:add19eb7defa 90 return result;
Azure.IoT Build 0:6ae2f7bca550 91 }
Azure.IoT Build 0:6ae2f7bca550 92
AzureIoTClient 23:1111ee8bcba4 93 static int saslanonymous_challenge(CONCRETE_SASL_MECHANISM_HANDLE concrete_sasl_mechanism, const SASL_MECHANISM_BYTES* challenge_bytes, SASL_MECHANISM_BYTES* response_bytes)
Azure.IoT Build 0:6ae2f7bca550 94 {
AzureIoTClient 28:add19eb7defa 95 int result;
Azure.IoT Build 0:6ae2f7bca550 96
AzureIoTClient 28:add19eb7defa 97 (void)challenge_bytes;
Azure.IoT Build 0:6ae2f7bca550 98
AzureIoTClient 28:add19eb7defa 99 /* Codes_SRS_SASL_ANONYMOUS_01_015: [If the `concrete_sasl_mechanism` or `response_bytes` argument is NULL then `saslanonymous_challenge` shall fail and return a non-zero value.] */
AzureIoTClient 28:add19eb7defa 100 if ((concrete_sasl_mechanism == NULL) ||
AzureIoTClient 28:add19eb7defa 101 (response_bytes == NULL))
AzureIoTClient 28:add19eb7defa 102 {
AzureIoTClient 23:1111ee8bcba4 103 LogError("Bad arguments: concrete_sasl_mechanism = %p, response_bytes = %p",
AzureIoTClient 23:1111ee8bcba4 104 concrete_sasl_mechanism, response_bytes);
AzureIoTClient 23:1111ee8bcba4 105 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 106 }
AzureIoTClient 28:add19eb7defa 107 else
AzureIoTClient 28:add19eb7defa 108 {
AzureIoTClient 28:add19eb7defa 109 /* Codes_SRS_SASL_ANONYMOUS_01_013: [`saslanonymous_challenge` shall set the `buffer` field to NULL and `size` to 0 in the `response_bytes` argument as the ANONYMOUS SASL mechanism does not implement challenge/response.] */
AzureIoTClient 28:add19eb7defa 110 response_bytes->bytes = NULL;
AzureIoTClient 28:add19eb7defa 111 response_bytes->length = 0;
Azure.IoT Build 0:6ae2f7bca550 112
AzureIoTClient 28:add19eb7defa 113 /* Codes_SRS_SASL_ANONYMOUS_01_014: [On success, `saslanonymous_challenge` shall return 0.] */
AzureIoTClient 28:add19eb7defa 114 result = 0;
AzureIoTClient 28:add19eb7defa 115 }
Azure.IoT Build 0:6ae2f7bca550 116
AzureIoTClient 28:add19eb7defa 117 return result;
Azure.IoT Build 0:6ae2f7bca550 118 }
Azure.IoT Build 0:6ae2f7bca550 119
AzureIoTClient 23:1111ee8bcba4 120 /* Codes_SRS_SASL_ANONYMOUS_01_010: [`saslanonymous_get_interface` shall return a pointer to a `SASL_MECHANISM_INTERFACE_DESCRIPTION` structure that contains pointers to the functions: `saslanonymous_create`, `saslanonymous_destroy`, `saslanonymous_get_init_bytes`, `saslanonymous_get_mechanism_name`, `saslanonymous_challenge`.] */
AzureIoTClient 23:1111ee8bcba4 121 static const SASL_MECHANISM_INTERFACE_DESCRIPTION saslanonymous_interface =
AzureIoTClient 23:1111ee8bcba4 122 {
AzureIoTClient 23:1111ee8bcba4 123 saslanonymous_create,
AzureIoTClient 23:1111ee8bcba4 124 saslanonymous_destroy,
AzureIoTClient 23:1111ee8bcba4 125 saslanonymous_get_init_bytes,
AzureIoTClient 23:1111ee8bcba4 126 saslanonymous_get_mechanism_name,
AzureIoTClient 23:1111ee8bcba4 127 saslanonymous_challenge
AzureIoTClient 23:1111ee8bcba4 128 };
AzureIoTClient 23:1111ee8bcba4 129
Azure.IoT Build 0:6ae2f7bca550 130 const SASL_MECHANISM_INTERFACE_DESCRIPTION* saslanonymous_get_interface(void)
Azure.IoT Build 0:6ae2f7bca550 131 {
AzureIoTClient 28:add19eb7defa 132 return &saslanonymous_interface;
Azure.IoT Build 0:6ae2f7bca550 133 }