Index: azure_iot_hub_f767zi/trunk/azure_iot_sdk/c-utility/adapters/tlsio_wolfssl.c
===================================================================
--- azure_iot_hub_f767zi/trunk/azure_iot_sdk/c-utility/adapters/tlsio_wolfssl.c	(revision 464)
+++ azure_iot_hub_f767zi/trunk/azure_iot_sdk/c-utility/adapters/tlsio_wolfssl.c	(revision 471)
@@ -52,10 +52,9 @@
     char* x509privatekey;
     int wolfssl_device_id;
-    char* hostname;
-    bool ignore_host_name_check;
+    size_t socket_reads;
 } TLS_IO_INSTANCE;
 
 STATIC_VAR_UNUSED const char* const OPTION_WOLFSSL_SET_DEVICE_ID = "SetDeviceId";
-static const size_t SOCKET_READ_LIMIT = 5;
+static const size_t SOCKET_READ_LIMIT = 10000; // 10,000 ms ?
 
 /*this function will clone an option given by name and value*/
@@ -106,21 +105,4 @@
             }
         }
-        #ifdef INVALID_DEVID
-        else if(strcmp(name, OPTION_WOLFSSL_SET_DEVICE_ID) == 0 )
-        {
-             int* value_clone;
-
-             if ((value_clone = malloc(sizeof(int))) == NULL)
-             {
-                 LogError("unable to clone device id option");
-             }
-             else
-             {
-                 *value_clone = *(int*)value;
-             }
-
-             result = value_clone;
-        }
-        #endif
         else
         {
@@ -144,6 +126,5 @@
         if ((strcmp(name, OPTION_TRUSTED_CERT) == 0) ||
             (strcmp(name, SU_OPTION_X509_CERT) == 0) ||
-            (strcmp(name, SU_OPTION_X509_PRIVATE_KEY) == 0) ||
-            (strcmp(name, OPTION_WOLFSSL_SET_DEVICE_ID) == 0))
+            (strcmp(name, SU_OPTION_X509_PRIVATE_KEY) == 0))
         {
             free((void*)value);
@@ -203,15 +184,4 @@
                 result = NULL;
             }
-            #ifdef INVALID_DEVID
-            else if (
-                (tls_io_instance->wolfssl_device_id != INVALID_DEVID) &&
-                (OptionHandler_AddOption(result, OPTION_WOLFSSL_SET_DEVICE_ID, &tls_io_instance->wolfssl_device_id) != OPTIONHANDLER_OK)
-                )
-            {
-                LogError("unable to save deviceid option");
-                OptionHandler_Destroy(result);
-                result = NULL;
-            }
-            #endif
             else
             {
@@ -287,10 +257,10 @@
         int res;
         tls_io_instance->tlsio_state = TLSIO_STATE_IN_HANDSHAKE;
+        tls_io_instance->socket_reads = 0;
 
         res = wolfSSL_connect(tls_io_instance->ssl);
         if (res != SSL_SUCCESS)
         {
-            // Error codes explained in https://www.wolfssl.com/docs/wolfssl-manual/appendix-c/
-            LogError("WolfSSL connect failed (%d)", wolfSSL_get_error(tls_io_instance->ssl, res));
+            LogError("WolfSSL connect failed");
             indicate_open_complete(tls_io_instance, IO_OPEN_ERROR);
             tls_io_instance->tlsio_state = TLSIO_STATE_ERROR;
@@ -384,15 +354,18 @@
         TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context;
         unsigned char* new_socket_io_read_bytes;
-        size_t socket_reads = 0;
 
         AZURE_UNREFERENCED_PARAMETER(ssl);
-        while (tls_io_instance->socket_io_read_byte_count == 0 && socket_reads < SOCKET_READ_LIMIT)
-        {
+        if (tls_io_instance->socket_io_read_byte_count == 0)
+        {
+            if (tls_io_instance->socket_reads >= SOCKET_READ_LIMIT) {
+                return WOLFSSL_CBIO_ERR_TIMEOUT;
+            }
             xio_dowork(tls_io_instance->socket_io);
-            if (tls_io_instance->tlsio_state != TLSIO_STATE_IN_HANDSHAKE)
-            {
-                break;
-            }
-            socket_reads++;
+            if (tls_io_instance->tlsio_state == TLSIO_STATE_IN_HANDSHAKE)
+            {
+                tls_io_instance->socket_reads++;
+                ThreadAPI_Sleep(1);
+                return 0;
+            }
         }
 
@@ -443,4 +416,16 @@
 }
 
+static void on_send_complete(void* context, IO_SEND_RESULT send_result)
+{
+	TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context;
+	if ((tls_io_instance == NULL) || (tls_io_instance->on_send_complete == NULL))
+		return;
+
+	tls_io_instance->on_send_complete(tls_io_instance->on_send_complete_callback_context, send_result);
+
+	tls_io_instance->on_send_complete = NULL;
+	tls_io_instance->on_send_complete_callback_context = NULL;
+}
+
 static int on_io_send(WOLFSSL *ssl, char *buf, int sz, void *context)
 {
@@ -450,10 +435,10 @@
     TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context;
 
-    if (xio_send(tls_io_instance->socket_io, buf, sz, tls_io_instance->on_send_complete, tls_io_instance->on_send_complete_callback_context) != 0)
+    if (xio_send(tls_io_instance->socket_io, buf, sz, on_send_complete, tls_io_instance) != 0)
     {
         LogError("Failed sending bytes through underlying IO");
         tls_io_instance->tlsio_state = TLSIO_STATE_ERROR;
         indicate_error(tls_io_instance);
-        result = WOLFSSL_CBIO_ERR_GENERAL;
+        result = -1;
     }
     else
@@ -469,5 +454,7 @@
     AZURE_UNREFERENCED_PARAMETER(ssl);
     TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context;
-    if (tls_io_instance->tlsio_state != TLSIO_STATE_IN_HANDSHAKE)
+    if (tls_io_instance->tlsio_state == TLSIO_STATE_OPEN) {
+    }
+    else if (tls_io_instance->tlsio_state != TLSIO_STATE_IN_HANDSHAKE)
     {
         LogInfo("on_handshake_done called when not in IN_HANDSHAKE state");
@@ -569,29 +556,8 @@
 }
 
-static int enable_domain_check(TLS_IO_INSTANCE* tls_io_instance)
-{
-    int result = 0;
-
-    if (!tls_io_instance->ignore_host_name_check)
-    {
-        if (wolfSSL_check_domain_name(tls_io_instance->ssl, tls_io_instance->hostname) != WOLFSSL_SUCCESS)
-        {
-            result = MU_FAILURE;
-        }
-    }
-
-    return result;
-}
-
 static int prepare_wolfssl_open(TLS_IO_INSTANCE* tls_io_instance)
 {
     int result;
-
-    if (enable_domain_check(tls_io_instance))
-    {
-        LogError("Failed to configure domain name verification");
-        result = MU_FAILURE;
-    }
-    else if (add_certificate_to_store(tls_io_instance) != 0)
+    if (add_certificate_to_store(tls_io_instance) != 0)
     {
         LogError("Failed to add certificates to store");
@@ -607,4 +573,11 @@
         result = MU_FAILURE;
     }
+#ifdef INVALID_DEVID
+    else if (tls_io_instance->wolfssl_device_id != INVALID_DEVID && wolfSSL_SetDevId(tls_io_instance->ssl, tls_io_instance->wolfssl_device_id) != WOLFSSL_SUCCESS)
+    {
+        LogError("Failure setting device id");
+        result = MU_FAILURE;
+    }
+#endif
     else
     {
@@ -653,10 +626,4 @@
             {
                 LogError("Cannot create the wolfSSL context");
-                free(result);
-                result = NULL;
-            }
-            else if (mallocAndStrcpy_s(&result->hostname, tls_io_config->hostname) != 0)
-            {
-                LogError("Failed copying the target hostname.");
                 free(result);
                 result = NULL;
@@ -691,5 +658,4 @@
                     LogError("Failed getting socket IO interface description.");
                     wolfSSL_CTX_free(result->ssl_context);
-                    free(result->hostname);
                     free(result);
                     result = NULL;
@@ -702,5 +668,4 @@
                         LogError("Failure connecting to underlying socket_io");
                         wolfSSL_CTX_free(result->ssl_context);
-                        free(result->hostname);
                         free(result);
                         result = NULL;
@@ -710,5 +675,4 @@
                         LogError("Failure connecting to underlying socket_io");
                         wolfSSL_CTX_free(result->ssl_context);
-                        free(result->hostname);
                         free(result);
                         result = NULL;
@@ -753,5 +717,4 @@
 
         xio_destroy(tls_io_instance->socket_io);
-        free(tls_io_instance->hostname);
         free(tls_io);
     }
@@ -803,8 +766,176 @@
             else
             {
-                // The state can get changed in the on_underlying_io_open_complete
-                if (tls_io_instance->tlsio_state != TLSIO_STATE_OPEN)
+                result = 0;
+            }
+        }
+    }
+
+    return result;
+}
+
+int tlsio_wolfssl_close(CONCRETE_IO_HANDLE tls_io, ON_IO_CLOSE_COMPLETE on_io_close_complete, void* callback_context)
+{
+    int result = 0;
+
+    if (tls_io == NULL)
+    {
+        LogError("NULL tls_io handle.");
+        result = MU_FAILURE;
+    }
+    else
+    {
+        TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io;
+
+        if ((tls_io_instance->tlsio_state == TLSIO_STATE_NOT_OPEN) ||
+            (tls_io_instance->tlsio_state == TLSIO_STATE_CLOSING))
+        {
+            LogError("Close called while not open.");
+            result = MU_FAILURE;
+        }
+        else
+        {
+            tls_io_instance->tlsio_state = TLSIO_STATE_CLOSING;
+            tls_io_instance->on_io_close_complete = on_io_close_complete;
+            tls_io_instance->on_io_close_complete_context = callback_context;
+
+            if (xio_close(tls_io_instance->socket_io, on_underlying_io_close_complete, tls_io_instance) != 0)
+            {
+                LogError("xio_close failed.");
+                result = MU_FAILURE;
+            }
+            else
+            {
+                result = 0;
+            }
+        }
+    }
+
+    return result;
+}
+
+int tlsio_wolfssl_send(CONCRETE_IO_HANDLE tls_io, const void* buffer, size_t size, ON_SEND_COMPLETE on_send_complete, void* callback_context)
+{
+    int result;
+
+    if (tls_io == NULL || buffer == NULL || size == 0)
+    {
+        LogError("Invalid parameter specified tls_io: %p, buffer: %p, size: %ul", tls_io, buffer, (unsigned int)size);
+        result = MU_FAILURE;
+    }
+    else
+    {
+        TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io;
+
+        if (tls_io_instance->tlsio_state != TLSIO_STATE_OPEN)
+        {
+            LogError("send called while not open");
+            result = MU_FAILURE;
+        }
+        if (tls_io_instance->on_send_complete != NULL)
+        {
+            LogError("Error writing data");
+            result = MU_FAILURE;
+        }
+        else
+        {
+            tls_io_instance->on_send_complete = on_send_complete;
+            tls_io_instance->on_send_complete_callback_context = callback_context;
+
+            int res = wolfSSL_write(tls_io_instance->ssl, buffer, size);
+            if ((res < 0) || ((size_t)res != size)) // Best way I can think of to safely compare an int to a size_t
+            {
+                LogError("Error writing data through WolfSSL");
+                result = MU_FAILURE;
+            }
+            else
+            {
+                result = 0;
+            }
+        }
+    }
+
+    return result;
+}
+
+void tlsio_wolfssl_dowork(CONCRETE_IO_HANDLE tls_io)
+{
+    if (tls_io == NULL)
+    {
+        LogError("NULL tls_io");
+    }
+    else
+    {
+        TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io;
+
+        if ((tls_io_instance->tlsio_state != TLSIO_STATE_NOT_OPEN) &&
+            (tls_io_instance->tlsio_state != TLSIO_STATE_ERROR))
+        {
+            if (tls_io_instance->tlsio_state != TLSIO_STATE_OPENING_UNDERLYING_IO)
+                decode_ssl_received_bytes(tls_io_instance);
+            xio_dowork(tls_io_instance->socket_io);
+        }
+    }
+}
+
+
+static int process_option(char** destination, const char* name, const char* value)
+{
+    int result;
+    if (*destination != NULL)
+    {
+        free(*destination);
+        *destination = NULL;
+    }
+    if (mallocAndStrcpy_s(destination, value) != 0)
+    {
+        LogError("unable to process option %s",name);
+        result = MU_FAILURE;
+    }
+    else
+    {
+        result = 0;
+    }
+    return result;
+}
+
+int tlsio_wolfssl_setoption(CONCRETE_IO_HANDLE tls_io, const char* optionName, const void* value)
+{
+    int result;
+
+    if (tls_io == NULL || optionName == NULL)
+    {
+        LogError("Bad arguments, tls_io = %p, optionName = %p", tls_io, optionName);
+        result = MU_FAILURE;
+    }
+    else
+    {
+        TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io;
+
+        if (strcmp(OPTION_TRUSTED_CERT, optionName) == 0)
+        {
+            result  = process_option(&tls_io_instance->certificate, optionName, value);
+        }
+        else if (strcmp(SU_OPTION_X509_CERT, optionName) == 0 || strcmp(OPTION_X509_ECC_CERT, optionName) == 0)
+        {
+            result = process_option(&tls_io_instance->x509certificate, optionName, value);
+        }
+        else if (strcmp(SU_OPTION_X509_PRIVATE_KEY, optionName) == 0 || strcmp(OPTION_X509_ECC_KEY, optionName) == 0)
+        {
+            result = process_option(&tls_io_instance->x509privatekey, optionName, value);
+        }
+        else if (strcmp(optionName, OPTION_SET_TLS_RENEGOTIATION) == 0)
+        {
+            // No need to do anything for WolfSSL
+            result = 0;
+        }
+#ifdef INVALID_DEVID
+        else if (strcmp(OPTION_WOLFSSL_SET_DEVICE_ID, optionName) == 0)
+        {
+            int device_id = *((int *)value);
+            if (tls_io_instance->ssl != NULL)
+            {
+                if (tls_io_instance->ssl != NULL && wolfSSL_SetDevId(tls_io_instance->ssl, device_id) != WOLFSSL_SUCCESS)
                 {
-                    LogError("Failed to connect to server.  The certificates may not be correct.");
+                    LogError("Failure setting device id on ssl");
                     result = MU_FAILURE;
                 }
@@ -814,174 +945,7 @@
                 }
             }
-        }
-    }
-
-    return result;
-}
-
-int tlsio_wolfssl_close(CONCRETE_IO_HANDLE tls_io, ON_IO_CLOSE_COMPLETE on_io_close_complete, void* callback_context)
-{
-    int result = 0;
-
-    if (tls_io == NULL)
-    {
-        LogError("NULL tls_io handle.");
-        result = MU_FAILURE;
-    }
-    else
-    {
-        TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io;
-
-        if ((tls_io_instance->tlsio_state == TLSIO_STATE_NOT_OPEN) ||
-            (tls_io_instance->tlsio_state == TLSIO_STATE_CLOSING))
-        {
-            LogError("Close called while not open.");
-            result = MU_FAILURE;
-        }
-        else
-        {
-            tls_io_instance->tlsio_state = TLSIO_STATE_CLOSING;
-            tls_io_instance->on_io_close_complete = on_io_close_complete;
-            tls_io_instance->on_io_close_complete_context = callback_context;
-
-            if (xio_close(tls_io_instance->socket_io, on_underlying_io_close_complete, tls_io_instance) != 0)
-            {
-                LogError("xio_close failed.");
-                result = MU_FAILURE;
-            }
-            else
-            {
-                result = 0;
-            }
-        }
-    }
-
-    return result;
-}
-
-int tlsio_wolfssl_send(CONCRETE_IO_HANDLE tls_io, const void* buffer, size_t size, ON_SEND_COMPLETE on_send_complete, void* callback_context)
-{
-    int result;
-
-    if (tls_io == NULL || buffer == NULL || size == 0)
-    {
-        LogError("Invalid parameter specified tls_io: %p, buffer: %p, size: %ul", tls_io, buffer, (unsigned int)size);
-        result = MU_FAILURE;
-    }
-    else
-    {
-        TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io;
-
-        if (tls_io_instance->tlsio_state != TLSIO_STATE_OPEN)
-        {
-            LogError("send called while not open");
-            result = MU_FAILURE;
-        }
-        else
-        {
-            tls_io_instance->on_send_complete = on_send_complete;
-            tls_io_instance->on_send_complete_callback_context = callback_context;
-
-            int res = wolfSSL_write(tls_io_instance->ssl, buffer, size);
-            if ((res < 0) || ((size_t)res != size)) // Best way I can think of to safely compare an int to a size_t
-            {
-                LogError("Error writing data through WolfSSL");
-                result = MU_FAILURE;
-            }
-            else
-            {
-                result = 0;
-            }
-        }
-    }
-
-    return result;
-}
-
-void tlsio_wolfssl_dowork(CONCRETE_IO_HANDLE tls_io)
-{
-    if (tls_io == NULL)
-    {
-        LogError("NULL tls_io");
-    }
-    else
-    {
-        TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io;
-
-        if ((tls_io_instance->tlsio_state != TLSIO_STATE_NOT_OPEN) &&
-            (tls_io_instance->tlsio_state != TLSIO_STATE_ERROR))
-        {
-            decode_ssl_received_bytes(tls_io_instance);
-            xio_dowork(tls_io_instance->socket_io);
-        }
-    }
-}
-
-
-static int process_option(char** destination, const char* name, const char* value)
-{
-
-    (void) name;
-    
-    int result;
-    if (*destination != NULL)
-    {
-        free(*destination);
-        *destination = NULL;
-    }
-    if (mallocAndStrcpy_s(destination, value) != 0)
-    {
-        LogError("unable to process option %s",name);
-        result = MU_FAILURE;
-    }
-    else
-    {
-        result = 0;
-    }
-    return result;
-}
-
-int tlsio_wolfssl_setoption(CONCRETE_IO_HANDLE tls_io, const char* optionName, const void* value)
-{
-    int result;
-
-    if (tls_io == NULL || optionName == NULL)
-    {
-        LogError("Bad arguments, tls_io = %p, optionName = %p", tls_io, optionName);
-        result = MU_FAILURE;
-    }
-    else
-    {
-        TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io;
-
-        if (strcmp(OPTION_TRUSTED_CERT, optionName) == 0)
-        {
-            result  = process_option(&tls_io_instance->certificate, optionName, value);
-        }
-        else if (strcmp(SU_OPTION_X509_CERT, optionName) == 0 || strcmp(OPTION_X509_ECC_CERT, optionName) == 0)
-        {
-            result = process_option(&tls_io_instance->x509certificate, optionName, value);
-        }
-        else if (strcmp(SU_OPTION_X509_PRIVATE_KEY, optionName) == 0 || strcmp(OPTION_X509_ECC_KEY, optionName) == 0)
-        {
-            result = process_option(&tls_io_instance->x509privatekey, optionName, value);
-        }
-        else if (strcmp(optionName, OPTION_SET_TLS_RENEGOTIATION) == 0)
-        {
-            // No need to do anything for WolfSSL
-            result = 0;
-        }
-#ifdef INVALID_DEVID
-        else if (strcmp(OPTION_WOLFSSL_SET_DEVICE_ID, optionName) == 0)
-        {
-            int device_id = *((int *)value);
-            if (tls_io_instance->ssl != NULL && wolfSSL_SetDevId(tls_io_instance->ssl, device_id) != WOLFSSL_SUCCESS)
-            {
-                LogError("Failure setting device id on ssl");
-                result = MU_FAILURE;
-            }
-            else
-            {
-                // Save the device Id even if ssl object not yet created.
+            else
+            {
+                // Save the id till we create the ssl object
                 tls_io_instance->wolfssl_device_id = device_id;
                 result = 0;
@@ -989,10 +953,4 @@
         }
 #endif
-        else if (strcmp("ignore_host_name_check", optionName) == 0)
-        {
-            bool* server_name_check = (bool*)value;
-            tls_io_instance->ignore_host_name_check = *server_name_check;
-            result = 0;
-        }
         else
         {
