A small memory footprint AMQP implimentation

Dependents:   iothub_client_sample_amqp remote_monitoring simplesample_amqp

Revision:
24:2c59c2d43ebf
Parent:
23:1111ee8bcba4
Child:
28:add19eb7defa
--- a/sasl_plain.c	Fri Apr 21 14:50:32 2017 -0700
+++ b/sasl_plain.c	Mon May 08 10:50:45 2017 -0700
@@ -14,23 +14,13 @@
 	uint32_t init_bytes_length;
 } SASL_PLAIN_INSTANCE;
 
-static const SASL_MECHANISM_INTERFACE_DESCRIPTION saslplain_interface =
-{
-	/* Codes_SRS_SASL_PLAIN_01_015: [**saslplain_get_interface shall return a pointer to a SASL_MECHANISM_INTERFACE_DESCRIPTION  structure that contains pointers to the functions: saslplain_create, saslplain_destroy, saslplain_get_init_bytes, saslplain_get_mechanism_name, saslplain_challenge.] */
-	saslplain_create,
-	saslplain_destroy,
-	saslplain_get_init_bytes,
-	saslplain_get_mechanism_name,
-	saslplain_challenge
-};
-
 CONCRETE_SASL_MECHANISM_HANDLE saslplain_create(void* config)
 {
 	SASL_PLAIN_INSTANCE* result;
 
 	if (config == NULL)
 	{
-		/* Codes_SRS_SASL_PLAIN_01_003: [If the config argument is NULL, then saslplain_create shall fail and return NULL.] */
+		/* Codes_SRS_SASL_PLAIN_01_003: [If the `config` argument is NULL, then `saslplain_create` shall fail and return NULL.] */
         LogError("NULL config");
 		result = NULL;
 	}
@@ -38,11 +28,12 @@
 	{
 		SASL_PLAIN_CONFIG* sasl_plain_config = (SASL_PLAIN_CONFIG*)config;
 
-		/* Codes_SRS_SASL_PLAIN_01_004: [If either the authcid or passwd member of the config structure is NULL, then saslplain_create shall fail and return NULL.] */
+		/* Codes_SRS_SASL_PLAIN_01_025: [ `authzid` shall be optional. ]*/
 		if ((sasl_plain_config->authcid == NULL) ||
 			(sasl_plain_config->passwd == NULL))
 		{
-            LogError("Bad configuration: authcid = %p, passwd = %p",
+			/* Codes_SRS_SASL_PLAIN_01_004: [If either the `authcid` or `passwd` member of the `config` structure is NULL, then `saslplain_create` shall fail and return NULL.] */
+			LogError("Bad configuration: authcid = %p, passwd = %p",
                 sasl_plain_config->authcid, sasl_plain_config->passwd);
             result = NULL;
 		}
@@ -65,16 +56,20 @@
 			}
 			else
 			{
-				/* Codes_SRS_SASL_PLAIN_01_001: [saslplain_create shall return on success a non-NULL handle to a new SASL plain mechanism.] */
+				/* Codes_SRS_SASL_PLAIN_01_001: [`saslplain_create` shall return on success a non-NULL handle to a new SASL plain mechanism.] */
 				result = (SASL_PLAIN_INSTANCE*)malloc(sizeof(SASL_PLAIN_INSTANCE));
-				/* Codes_SRS_SASL_PLAIN_01_002: [If allocating the memory needed for the saslplain instance fails then saslplain_create shall return NULL.] */
-				if (result != NULL)
+				if (result == NULL)
+				{
+					/* Codes_SRS_SASL_PLAIN_01_002: [If allocating the memory needed for the saslplain instance fails then `saslplain_create` shall return NULL.] */
+					LogError("Cannot allocate memory for SASL plain instance");
+				}
+				else
 				{
 					/* Ignore UTF8 for now */
 					result->init_bytes = (unsigned char*)malloc(authzid_length + authcid_length + passwd_length + 2);
 					if (result->init_bytes == NULL)
 					{
-						/* Codes_SRS_SASL_PLAIN_01_002: [If allocating the memory needed for the saslplain instance fails then saslplain_create shall return NULL.] */
+						/* Codes_SRS_SASL_PLAIN_01_002: [If allocating the memory needed for the saslplain instance fails then `saslplain_create` shall return NULL.] */
                         LogError("Cannot allocate init bytes");
                         free(result);
 						result = NULL;
@@ -111,11 +106,12 @@
 {
     if (sasl_mechanism_concrete_handle == NULL)
     {
-        LogError("NULL sasl_mechanism_concrete_handle");
+		/* Codes_SRS_SASL_PLAIN_01_006: [If the argument `concrete_sasl_mechanism` is NULL, `saslplain_destroy` shall do nothing.] */
+		LogError("NULL sasl_mechanism_concrete_handle");
     }
     else
     {
-		/* Codes_SRS_SASL_PLAIN_01_005: [saslplain_destroy shall free all resources associated with the SASL mechanism.] */
+		/* Codes_SRS_SASL_PLAIN_01_005: [`saslplain_destroy` shall free all resources associated with the SASL mechanism.] */
 		SASL_PLAIN_INSTANCE* sasl_plain_instance = (SASL_PLAIN_INSTANCE*)sasl_mechanism_concrete_handle;
 		if (sasl_plain_instance->init_bytes != NULL)
 		{
@@ -133,7 +129,8 @@
 	if ((sasl_mechanism_concrete_handle == NULL) ||
 		(init_bytes == NULL))
 	{
-        LogError("Bad arguments: sasl_mechanism_concrete_handle = %p, init_bytes = %p",
+		/* Codes_SRS_SASL_PLAIN_01_009: [If any argument is NULL, `saslplain_get_init_bytes` shall return a non-zero value.] */
+		LogError("Bad arguments: sasl_mechanism_concrete_handle = %p, init_bytes = %p",
             sasl_mechanism_concrete_handle, init_bytes);
         result = __FAILURE__;
 	}
@@ -141,10 +138,11 @@
 	{
 		SASL_PLAIN_INSTANCE* sasl_plain_instance = (SASL_PLAIN_INSTANCE*)sasl_mechanism_concrete_handle;
 
+		/* Codes_SRS_SASL_PLAIN_01_007: [`saslplain_get_init_bytes` shall construct the initial bytes per the RFC 4616.] */
 		init_bytes->bytes = sasl_plain_instance->init_bytes;
 		init_bytes->length = sasl_plain_instance->init_bytes_length;
 
-		/* Codes_SRS_SASL_PLAIN_01_008: [On success saslplain_get_init_bytes shall return zero.] */
+		/* Codes_SRS_SASL_PLAIN_01_008: [On success `saslplain_get_init_bytes` shall return zero.] */
 		result = 0;
 	}
 
@@ -157,13 +155,13 @@
 
 	if (sasl_mechanism == NULL)
 	{
-		/* Codes_SRS_SASL_PLAIN_01_011: [If the argument concrete_sasl_mechanism is NULL, saslplain_get_mechanism_name shall return NULL.] */
+		/* Codes_SRS_SASL_PLAIN_01_011: [If the argument `concrete_sasl_mechanism` is NULL, `saslplain_get_mechanism_name` shall return NULL.] */
         LogError("NULL sasl_mechanism");
         result = NULL;
 	}
 	else
 	{
-		/* Codes_SRS_SASL_PLAIN_01_010: [saslplain_get_mechanism_name shall validate the argument concrete_sasl_mechanism and on success it shall return a pointer to the string "PLAIN".] */
+		/* Codes_SRS_SASL_PLAIN_01_010: [`saslplain_get_mechanism_name` shall validate the argument `concrete_sasl_mechanism` and on success it shall return a pointer to the string "PLAIN".] */
 		result = "PLAIN";
 	}
 
@@ -176,7 +174,7 @@
 
 	(void)challenge_bytes;
 
-	/* Codes_SRS_SASL_PLAIN_01_014: [If the concrete_sasl_mechanism or response_bytes argument is NULL then saslplain_challenge shall fail and return a non-zero value.] */
+	/* Codes_SRS_SASL_PLAIN_01_014: [If the `concrete_sasl_mechanism` or `response_bytes` argument is NULL then `saslplain_challenge` shall fail and return a non-zero value.] */
 	if ((concrete_sasl_mechanism == NULL) ||
 		(response_bytes == NULL))
 	{
@@ -186,17 +184,27 @@
 	}
 	else
 	{
-		/* Codes_SRS_SASL_PLAIN_01_012: [saslplain_challenge shall set the response_bytes buffer to NULL and 0 size as the PLAIN SASL mechanism does not implement challenge/response.] */
+		/* Codes_SRS_SASL_PLAIN_01_012: [`saslplain_challenge` shall set the `response_bytes` buffer to NULL and 0 size as the PLAIN SASL mechanism does not implement challenge/response.] */
 		response_bytes->bytes = NULL;
 		response_bytes->length = 0;
 
-		/* Codes_SRS_SASL_PLAIN_01_013: [On success, saslplain_challenge shall return 0.] */
+		/* Codes_SRS_SASL_PLAIN_01_013: [On success, `saslplain_challenge` shall return 0.] */
 		result = 0;
 	}
 
 	return result;
 }
 
+static const SASL_MECHANISM_INTERFACE_DESCRIPTION saslplain_interface =
+{
+	/* Codes_SRS_SASL_PLAIN_01_015: [**`saslplain_get_interface` shall return a pointer to a `SASL_MECHANISM_INTERFACE_DESCRIPTION` structure that contains pointers to the functions: `saslplain_create`, `saslplain_destroy`, `saslplain_get_init_bytes`, `saslplain_get_mechanism_name`, `saslplain_challenge`.] */
+	saslplain_create,
+	saslplain_destroy,
+	saslplain_get_init_bytes,
+	saslplain_get_mechanism_name,
+	saslplain_challenge
+};
+
 const SASL_MECHANISM_INTERFACE_DESCRIPTION* saslplain_get_interface(void)
 {
 	return &saslplain_interface;