Refine code based on commnets

Change code layout
Change hostname_len type to size_t
Fix various issues

Signed-off-by: Xiaokang Qian <xiaokang.qian@arm.com>
diff --git a/library/ssl_client.c b/library/ssl_client.c
index 2c5f664..8080e3e 100644
--- a/library/ssl_client.c
+++ b/library/ssl_client.c
@@ -54,7 +54,6 @@
 {
     unsigned char *p = buf;
     size_t hostname_len;
-    size_t cmp_hostname_len;
 
     *olen = 0;
 
@@ -65,25 +64,8 @@
         ( "client hello, adding server name extension: %s",
           ssl->hostname ) );
 
-    ssl->session_negotiate->hostname_mismatch = 0;
     hostname_len = strlen( ssl->hostname );
 
-    cmp_hostname_len = hostname_len < ssl->session_negotiate->hostname_len ?
-                       hostname_len : ssl->session_negotiate->hostname_len;
-
-    if( hostname_len != ssl->session_negotiate->hostname_len ||
-        memcmp( ssl->hostname, ssl->session_negotiate->hostname, cmp_hostname_len ) )
-        ssl->session_negotiate->hostname_mismatch = 1;
-
-    if( ssl->session_negotiate->hostname == NULL )
-    {
-        ssl->session_negotiate->hostname = mbedtls_calloc( 1, hostname_len );
-        if( ssl->session_negotiate->hostname == NULL )
-            return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
-        memcpy(ssl->session_negotiate->hostname, ssl->hostname, hostname_len);
-    }
-    ssl->session_negotiate->hostname_len = hostname_len;
-
     MBEDTLS_SSL_CHK_BUF_PTR( p, end, hostname_len + 9 );
 
     /*
@@ -888,6 +870,34 @@
         }
     }
 
+#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
+    defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
+    if( ssl->handshake->resume )
+    {
+        if( ssl->hostname != NULL && ssl->session_negotiate->hostname != NULL )
+        {
+            if( strcmp( ssl->hostname, ssl->session_negotiate->hostname ) )
+            {
+                MBEDTLS_SSL_DEBUG_MSG( 1,
+                ( "hostname mismatch the session ticket, should not resume " ) );
+                return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
+            }
+        }
+        else if( ssl->session_negotiate->hostname != NULL )
+        {
+                MBEDTLS_SSL_DEBUG_MSG( 1,
+                ( "hostname missed, should not resume " ) );
+                return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
+        }
+    }
+    else
+    {
+        mbedtls_ssl_session_set_hostname( ssl->session_negotiate,
+                                          ssl->hostname );
+    }
+#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
+          MBEDTLS_SSL_SERVER_NAME_INDICATION */
+
     return( 0 );
 }
 /*