Demo using MBED TLS

Dependencies:   EthernetInterface NTPClient iothub_amqp_transport iothub_client mbed-rtos mbed

Fork of iothub_client_sample_amqp by Azure IoT

Committer:
markrad
Date:
Thu Jan 05 00:20:03 2017 +0000
Revision:
58:f50b97b08851
Sample using MBED TLS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
markrad 58:f50b97b08851 1 // Copyright (c) Microsoft. All rights reserved.
markrad 58:f50b97b08851 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
markrad 58:f50b97b08851 3
markrad 58:f50b97b08851 4 #include "azure_c_shared_utility/connection_string_parser.h"
markrad 58:f50b97b08851 5 #include "azure_c_shared_utility/map.h"
markrad 58:f50b97b08851 6 #include "azure_c_shared_utility/strings.h"
markrad 58:f50b97b08851 7 #include "azure_c_shared_utility/string_tokenizer.h"
markrad 58:f50b97b08851 8 #include <stdbool.h>
markrad 58:f50b97b08851 9 #include "azure_c_shared_utility/xlogging.h"
markrad 58:f50b97b08851 10
markrad 58:f50b97b08851 11 /* Codes_SRS_CONNECTIONSTRINGPARSER_01_001: [connectionstringparser_parse shall parse all key value pairs from the connection_string passed in as argument and return a new map that holds the key/value pairs.] */
markrad 58:f50b97b08851 12 MAP_HANDLE connectionstringparser_parse(STRING_HANDLE connection_string)
markrad 58:f50b97b08851 13 {
markrad 58:f50b97b08851 14 MAP_HANDLE result;
markrad 58:f50b97b08851 15
markrad 58:f50b97b08851 16 if (connection_string == NULL)
markrad 58:f50b97b08851 17 {
markrad 58:f50b97b08851 18 /* Codes_SRS_CONNECTIONSTRINGPARSER_01_002: [If connection_string is NULL then connectionstringparser_parse shall fail and return NULL.] */
markrad 58:f50b97b08851 19 result = NULL;
markrad 58:f50b97b08851 20 LogError("NULL connection string passed to tokenizer.\r\n");
markrad 58:f50b97b08851 21 }
markrad 58:f50b97b08851 22 else
markrad 58:f50b97b08851 23 {
markrad 58:f50b97b08851 24 /* Codes_SRS_CONNECTIONSTRINGPARSER_01_003: [connectionstringparser_parse shall create a STRING tokenizer to be used for parsing the connection string, by calling STRING_TOKENIZER_create.] */
markrad 58:f50b97b08851 25 /* Codes_SRS_CONNECTIONSTRINGPARSER_01_004: [connectionstringparser_parse shall start scanning at the beginning of the connection string.] */
markrad 58:f50b97b08851 26 STRING_TOKENIZER_HANDLE tokenizer = STRING_TOKENIZER_create(connection_string);
markrad 58:f50b97b08851 27 if (tokenizer == NULL)
markrad 58:f50b97b08851 28 {
markrad 58:f50b97b08851 29 /* Codes_SRS_CONNECTIONSTRINGPARSER_01_015: [If STRING_TOKENIZER_create fails, connectionstringparser_parse shall fail and return NULL.] */
markrad 58:f50b97b08851 30 result = NULL;
markrad 58:f50b97b08851 31 LogError("Error creating STRING tokenizer.\r\n");
markrad 58:f50b97b08851 32 }
markrad 58:f50b97b08851 33 else
markrad 58:f50b97b08851 34 {
markrad 58:f50b97b08851 35 /* Codes_SRS_CONNECTIONSTRINGPARSER_01_016: [2 STRINGs shall be allocated in order to hold the to be parsed key and value tokens.] */
markrad 58:f50b97b08851 36 STRING_HANDLE token_key_string = STRING_new();
markrad 58:f50b97b08851 37 if (token_key_string == NULL)
markrad 58:f50b97b08851 38 {
markrad 58:f50b97b08851 39 /* Codes_SRS_CONNECTIONSTRINGPARSER_01_017: [If allocating the STRINGs fails connectionstringparser_parse shall fail and return NULL.] */
markrad 58:f50b97b08851 40 result = NULL;
markrad 58:f50b97b08851 41 LogError("Error creating key token STRING.\r\n");
markrad 58:f50b97b08851 42 }
markrad 58:f50b97b08851 43 else
markrad 58:f50b97b08851 44 {
markrad 58:f50b97b08851 45 STRING_HANDLE token_value_string = STRING_new();
markrad 58:f50b97b08851 46 if (token_value_string == NULL)
markrad 58:f50b97b08851 47 {
markrad 58:f50b97b08851 48 /* Codes_SRS_CONNECTIONSTRINGPARSER_01_017: [If allocating the STRINGs fails connectionstringparser_parse shall fail and return NULL.] */
markrad 58:f50b97b08851 49 result = NULL;
markrad 58:f50b97b08851 50 LogError("Error creating value token STRING.\r\n");
markrad 58:f50b97b08851 51 }
markrad 58:f50b97b08851 52 else
markrad 58:f50b97b08851 53 {
markrad 58:f50b97b08851 54 result = Map_Create(NULL);
markrad 58:f50b97b08851 55 if (result == NULL)
markrad 58:f50b97b08851 56 {
markrad 58:f50b97b08851 57 /* Codes_SRS_CONNECTIONSTRINGPARSER_01_018: [If creating the result map fails, then connectionstringparser_parse shall return NULL.] */
markrad 58:f50b97b08851 58 LogError("Error creating Map\r\n");
markrad 58:f50b97b08851 59 }
markrad 58:f50b97b08851 60 else
markrad 58:f50b97b08851 61 {
markrad 58:f50b97b08851 62 /* Codes_SRS_CONNECTIONSTRINGPARSER_01_005: [The following actions shall be repeated until parsing is complete:] */
markrad 58:f50b97b08851 63 /* Codes_SRS_CONNECTIONSTRINGPARSER_01_006: [connectionstringparser_parse shall find a token (the key of the key/value pair) delimited by the `=` character, by calling STRING_TOKENIZER_get_next_token.] */
markrad 58:f50b97b08851 64 /* Codes_SRS_CONNECTIONSTRINGPARSER_01_007: [If STRING_TOKENIZER_get_next_token fails, parsing shall be considered complete.] */
markrad 58:f50b97b08851 65 while (STRING_TOKENIZER_get_next_token(tokenizer, token_key_string, "=") == 0)
markrad 58:f50b97b08851 66 {
markrad 58:f50b97b08851 67 bool is_error = false;
markrad 58:f50b97b08851 68
markrad 58:f50b97b08851 69 /* Codes_SRS_CONNECTIONSTRINGPARSER_01_008: [connectionstringparser_parse shall find a token (the value of the key/value pair) delimited by the `;` character, by calling STRING_TOKENIZER_get_next_token.] */
markrad 58:f50b97b08851 70 if (STRING_TOKENIZER_get_next_token(tokenizer, token_value_string, ";") != 0)
markrad 58:f50b97b08851 71 {
markrad 58:f50b97b08851 72 /* Codes_SRS_CONNECTIONSTRINGPARSER_01_009: [If STRING_TOKENIZER_get_next_token fails, connectionstringparser_parse shall fail and return NULL (freeing the allocated result map).] */
markrad 58:f50b97b08851 73 is_error = true;
markrad 58:f50b97b08851 74 LogError("Error reading value token from the connection string.\r\n");
markrad 58:f50b97b08851 75 }
markrad 58:f50b97b08851 76 else
markrad 58:f50b97b08851 77 {
markrad 58:f50b97b08851 78 /* Codes_SRS_CONNECTIONSTRINGPARSER_01_011: [The C strings for the key and value shall be extracted from the previously parsed STRINGs by using STRING_c_str.] */
markrad 58:f50b97b08851 79 const char* token = STRING_c_str(token_key_string);
markrad 58:f50b97b08851 80 /* Codes_SRS_CONNECTIONSTRINGPARSER_01_013: [If STRING_c_str fails then connectionstringparser_parse shall fail and return NULL (freeing the allocated result map).] */
markrad 58:f50b97b08851 81 if ((token == NULL) ||
markrad 58:f50b97b08851 82 /* Codes_SRS_CONNECTIONSTRINGPARSER_01_019: [If the key length is zero then connectionstringparser_parse shall fail and return NULL (freeing the allocated result map).] */
markrad 58:f50b97b08851 83 (strlen(token) == 0))
markrad 58:f50b97b08851 84 {
markrad 58:f50b97b08851 85 is_error = true;
markrad 58:f50b97b08851 86 LogError("The key token is NULL or empty.\r\n");
markrad 58:f50b97b08851 87 }
markrad 58:f50b97b08851 88 else
markrad 58:f50b97b08851 89 {
markrad 58:f50b97b08851 90 /* Codes_SRS_CONNECTIONSTRINGPARSER_01_011: [The C strings for the key and value shall be extracted from the previously parsed STRINGs by using STRING_c_str.] */
markrad 58:f50b97b08851 91 const char* value = STRING_c_str(token_value_string);
markrad 58:f50b97b08851 92 if (value == NULL)
markrad 58:f50b97b08851 93 {
markrad 58:f50b97b08851 94 /* Codes_SRS_CONNECTIONSTRINGPARSER_01_013: [If STRING_c_str fails then connectionstringparser_parse shall fail and return NULL (freeing the allocated result map).] */
markrad 58:f50b97b08851 95 is_error = true;
markrad 58:f50b97b08851 96 LogError("Could not get C string for value token.\r\n");
markrad 58:f50b97b08851 97 }
markrad 58:f50b97b08851 98 else
markrad 58:f50b97b08851 99 {
markrad 58:f50b97b08851 100 /* Codes_SRS_CONNECTIONSTRINGPARSER_01_010: [The key and value shall be added to the result map by using Map_Add.] */
markrad 58:f50b97b08851 101 if (Map_Add(result, token, value) != 0)
markrad 58:f50b97b08851 102 {
markrad 58:f50b97b08851 103 /* Codes_SRS_CONNECTIONSTRINGPARSER_01_012: [If Map_Add fails connectionstringparser_parse shall fail and return NULL (freeing the allocated result map).] */
markrad 58:f50b97b08851 104 is_error = true;
markrad 58:f50b97b08851 105 LogError("Could not add the key/value pair to the result map.\r\n");
markrad 58:f50b97b08851 106 }
markrad 58:f50b97b08851 107 }
markrad 58:f50b97b08851 108 }
markrad 58:f50b97b08851 109 }
markrad 58:f50b97b08851 110
markrad 58:f50b97b08851 111 if (is_error)
markrad 58:f50b97b08851 112 {
markrad 58:f50b97b08851 113 LogError("Error parsing connection string.\r\n");
markrad 58:f50b97b08851 114 Map_Destroy(result);
markrad 58:f50b97b08851 115 result = NULL;
markrad 58:f50b97b08851 116 break;
markrad 58:f50b97b08851 117 }
markrad 58:f50b97b08851 118 }
markrad 58:f50b97b08851 119 }
markrad 58:f50b97b08851 120
markrad 58:f50b97b08851 121 STRING_delete(token_value_string);
markrad 58:f50b97b08851 122 }
markrad 58:f50b97b08851 123
markrad 58:f50b97b08851 124 STRING_delete(token_key_string);
markrad 58:f50b97b08851 125 }
markrad 58:f50b97b08851 126
markrad 58:f50b97b08851 127 /* Codes_SRS_CONNECTIONSTRINGPARSER_01_014: [After the parsing is complete the previously allocated STRINGs and STRING tokenizer shall be freed by calling STRING_TOKENIZER_destroy.] */
markrad 58:f50b97b08851 128 STRING_TOKENIZER_destroy(tokenizer);
markrad 58:f50b97b08851 129 }
markrad 58:f50b97b08851 130 }
markrad 58:f50b97b08851 131
markrad 58:f50b97b08851 132 return result;
markrad 58:f50b97b08851 133 }