A small memory footprint AMQP implimentation

Dependents:   iothub_client_sample_amqp remote_monitoring simplesample_amqp

Committer:
AzureIoTClient
Date:
Fri Jul 29 15:58:39 2016 -0700
Revision:
6:641a9672db08
Parent:
0:6ae2f7bca550
Child:
17:923575db8b2d
1.0.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 #ifdef _CRTDBG_MAP_ALLOC
Azure.IoT Build 0:6ae2f7bca550 6 #include <crtdbg.h>
Azure.IoT Build 0:6ae2f7bca550 7 #endif
Azure.IoT Build 0:6ae2f7bca550 8 #include <string.h>
Azure.IoT Build 0:6ae2f7bca550 9 #include "azure_uamqp_c/sasl_anonymous.h"
Azure.IoT Build 0:6ae2f7bca550 10 #include "azure_uamqp_c/amqpalloc.h"
Azure.IoT Build 0:6ae2f7bca550 11 #include "azure_c_shared_utility/xlogging.h"
Azure.IoT Build 0:6ae2f7bca550 12
Azure.IoT Build 0:6ae2f7bca550 13 typedef struct SASL_ANONYMOUS_INSTANCE_TAG
Azure.IoT Build 0:6ae2f7bca550 14 {
AzureIoTClient 6:641a9672db08 15 unsigned char dummy;
Azure.IoT Build 0:6ae2f7bca550 16 } SASL_ANONYMOUS_INSTANCE;
Azure.IoT Build 0:6ae2f7bca550 17
Azure.IoT Build 0:6ae2f7bca550 18 /* 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.] */
Azure.IoT Build 0:6ae2f7bca550 19 static const SASL_MECHANISM_INTERFACE_DESCRIPTION saslanonymous_interface =
Azure.IoT Build 0:6ae2f7bca550 20 {
Azure.IoT Build 0:6ae2f7bca550 21 saslanonymous_create,
Azure.IoT Build 0:6ae2f7bca550 22 saslanonymous_destroy,
Azure.IoT Build 0:6ae2f7bca550 23 saslanonymous_get_init_bytes,
Azure.IoT Build 0:6ae2f7bca550 24 saslanonymous_get_mechanism_name,
Azure.IoT Build 0:6ae2f7bca550 25 saslanonymous_challenge
Azure.IoT Build 0:6ae2f7bca550 26 };
Azure.IoT Build 0:6ae2f7bca550 27
Azure.IoT Build 0:6ae2f7bca550 28 /* Codes_SRS_SASL_ANONYMOUS_01_001: [saslanonymous_create shall return on success a non-NULL handle to a new SASL anonymous mechanism.] */
Azure.IoT Build 0:6ae2f7bca550 29 CONCRETE_SASL_MECHANISM_HANDLE saslanonymous_create(void* config)
Azure.IoT Build 0:6ae2f7bca550 30 {
Azure.IoT Build 0:6ae2f7bca550 31 /* Codes_SRS_SASL_ANONYMOUS_01_003: [Since this is the ANONYMOUS SASL mechanism, config shall be ignored.] */
Azure.IoT Build 0:6ae2f7bca550 32 (void)config;
Azure.IoT Build 0:6ae2f7bca550 33
Azure.IoT Build 0:6ae2f7bca550 34 /* Codes_SRS_SASL_ANONYMOUS_01_002: [If allocating the memory needed for the saslanonymous instance fails then saslanonymous_create shall return NULL.] */
Azure.IoT Build 0:6ae2f7bca550 35 return amqpalloc_malloc(sizeof(SASL_ANONYMOUS_INSTANCE));
Azure.IoT Build 0:6ae2f7bca550 36 }
Azure.IoT Build 0:6ae2f7bca550 37
Azure.IoT Build 0:6ae2f7bca550 38 void saslanonymous_destroy(CONCRETE_SASL_MECHANISM_HANDLE sasl_mechanism_concrete_handle)
Azure.IoT Build 0:6ae2f7bca550 39 {
Azure.IoT Build 0:6ae2f7bca550 40 /* Codes_SRS_SASL_ANONYMOUS_01_005: [If the argument concrete_sasl_mechanism is NULL, saslanonymous_destroy shall do nothing.] */
Azure.IoT Build 0:6ae2f7bca550 41 if (sasl_mechanism_concrete_handle != NULL)
Azure.IoT Build 0:6ae2f7bca550 42 {
Azure.IoT Build 0:6ae2f7bca550 43 /* Codes_SRS_SASL_ANONYMOUS_01_004: [saslanonymous_destroy shall free all resources associated with the SASL mechanism.] */
Azure.IoT Build 0:6ae2f7bca550 44 amqpalloc_free(sasl_mechanism_concrete_handle);
Azure.IoT Build 0:6ae2f7bca550 45 }
Azure.IoT Build 0:6ae2f7bca550 46 }
Azure.IoT Build 0:6ae2f7bca550 47
Azure.IoT Build 0:6ae2f7bca550 48 int saslanonymous_get_init_bytes(CONCRETE_SASL_MECHANISM_HANDLE sasl_mechanism_concrete_handle, SASL_MECHANISM_BYTES* init_bytes)
Azure.IoT Build 0:6ae2f7bca550 49 {
Azure.IoT Build 0:6ae2f7bca550 50 int result;
Azure.IoT Build 0:6ae2f7bca550 51
Azure.IoT Build 0:6ae2f7bca550 52 /* Codes_SRS_SASL_ANONYMOUS_01_007: [If the any argument is NULL, saslanonymous_get_init_bytes shall return a non-zero value.] */
Azure.IoT Build 0:6ae2f7bca550 53 if ((sasl_mechanism_concrete_handle == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 54 (init_bytes == NULL))
Azure.IoT Build 0:6ae2f7bca550 55 {
Azure.IoT Build 0:6ae2f7bca550 56 result = __LINE__;
Azure.IoT Build 0:6ae2f7bca550 57 }
Azure.IoT Build 0:6ae2f7bca550 58 else
Azure.IoT Build 0:6ae2f7bca550 59 {
Azure.IoT Build 0:6ae2f7bca550 60 /* Codes_SRS_SASL_ANONYMOUS_01_012: [The bytes field of init_buffer shall be set to NULL.] */
Azure.IoT Build 0:6ae2f7bca550 61 init_bytes->bytes = NULL;
Azure.IoT Build 0:6ae2f7bca550 62 /* 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.] */
Azure.IoT Build 0:6ae2f7bca550 63 init_bytes->length = 0;
Azure.IoT Build 0:6ae2f7bca550 64
Azure.IoT Build 0:6ae2f7bca550 65 /* Codes_SRS_SASL_ANONYMOUS_01_011: [On success saslanonymous_get_init_bytes shall return zero.] */
Azure.IoT Build 0:6ae2f7bca550 66 result = 0;
Azure.IoT Build 0:6ae2f7bca550 67 }
Azure.IoT Build 0:6ae2f7bca550 68
Azure.IoT Build 0:6ae2f7bca550 69 return result;
Azure.IoT Build 0:6ae2f7bca550 70 }
Azure.IoT Build 0:6ae2f7bca550 71
Azure.IoT Build 0:6ae2f7bca550 72 const char* saslanonymous_get_mechanism_name(CONCRETE_SASL_MECHANISM_HANDLE sasl_mechanism)
Azure.IoT Build 0:6ae2f7bca550 73 {
Azure.IoT Build 0:6ae2f7bca550 74 const char* result;
Azure.IoT Build 0:6ae2f7bca550 75
Azure.IoT Build 0:6ae2f7bca550 76 /* Codes_SRS_SASL_ANONYMOUS_01_009: [If the argument concrete_sasl_mechanism is NULL, saslanonymous_get_mechanism_name shall return NULL.] */
Azure.IoT Build 0:6ae2f7bca550 77 if (sasl_mechanism == NULL)
Azure.IoT Build 0:6ae2f7bca550 78 {
Azure.IoT Build 0:6ae2f7bca550 79 result = NULL;
Azure.IoT Build 0:6ae2f7bca550 80 }
Azure.IoT Build 0:6ae2f7bca550 81 else
Azure.IoT Build 0:6ae2f7bca550 82 {
Azure.IoT Build 0:6ae2f7bca550 83 /* 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".] */
Azure.IoT Build 0:6ae2f7bca550 84 result = "ANONYMOUS";
Azure.IoT Build 0:6ae2f7bca550 85 }
Azure.IoT Build 0:6ae2f7bca550 86
Azure.IoT Build 0:6ae2f7bca550 87 return result;
Azure.IoT Build 0:6ae2f7bca550 88 }
Azure.IoT Build 0:6ae2f7bca550 89
Azure.IoT Build 0:6ae2f7bca550 90 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 91 {
Azure.IoT Build 0:6ae2f7bca550 92 int result;
Azure.IoT Build 0:6ae2f7bca550 93
Azure.IoT Build 0:6ae2f7bca550 94 (void)challenge_bytes;
Azure.IoT Build 0:6ae2f7bca550 95
Azure.IoT Build 0:6ae2f7bca550 96 /* 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.] */
Azure.IoT Build 0:6ae2f7bca550 97 if ((concrete_sasl_mechanism == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 98 (response_bytes == NULL))
Azure.IoT Build 0:6ae2f7bca550 99 {
Azure.IoT Build 0:6ae2f7bca550 100 result = __LINE__;
Azure.IoT Build 0:6ae2f7bca550 101 }
Azure.IoT Build 0:6ae2f7bca550 102 else
Azure.IoT Build 0:6ae2f7bca550 103 {
Azure.IoT Build 0:6ae2f7bca550 104 /* Codes_SRS_SASL_ANONYMOUS_01_013: [saslanonymous_challenge shall set the response_bytes buffer to NULL and 0 size as the ANONYMOUS SASL mechanism does not implement challenge/response.] */
Azure.IoT Build 0:6ae2f7bca550 105 response_bytes->bytes = NULL;
Azure.IoT Build 0:6ae2f7bca550 106 response_bytes->length = 0;
Azure.IoT Build 0:6ae2f7bca550 107
Azure.IoT Build 0:6ae2f7bca550 108 /* Codes_SRS_SASL_ANONYMOUS_01_014: [On success, saslanonymous_challenge shall return 0.] */
Azure.IoT Build 0:6ae2f7bca550 109 result = 0;
Azure.IoT Build 0:6ae2f7bca550 110 }
Azure.IoT Build 0:6ae2f7bca550 111
Azure.IoT Build 0:6ae2f7bca550 112 return result;
Azure.IoT Build 0:6ae2f7bca550 113 }
Azure.IoT Build 0:6ae2f7bca550 114
Azure.IoT Build 0:6ae2f7bca550 115 const SASL_MECHANISM_INTERFACE_DESCRIPTION* saslanonymous_get_interface(void)
Azure.IoT Build 0:6ae2f7bca550 116 {
Azure.IoT Build 0:6ae2f7bca550 117 return &saslanonymous_interface;
Azure.IoT Build 0:6ae2f7bca550 118 }