netutils/websocket: separate state
authorbossjisu <jisuu.kim@samsung.com>
Thu, 6 Jul 2017 09:21:39 +0000 (02:21 -0700)
committerEunBong Song <eunb.song@samsung.com>
Wed, 30 Aug 2017 04:15:44 +0000 (21:15 -0700)
running state is splitted to run_client state and run_server state.
it's because mbed_tls function varies between client and server.
TLS code will be separated with websocket code in next update.

Change-Id: I0a7c2cff335a6b92e18cb0d1b52c1f28d4811f16
Signed-off-by: bossjisu <jisuu.kim@samsung.com>
apps/examples/websocket/websocket_main.c
apps/include/netutils/websocket.h
apps/netutils/websocket/websocket.c

index d592f35..b9e074e 100644 (file)
@@ -183,7 +183,7 @@ static void websocket_tls_debug(void *ctx, int level, const char *file, int line
        printf("%s:%04d: %s", file, line, str);
 }
 
-websocket_return_t websocket_tls_init(int param, websocket_t *data, mbedtls_ssl_config *conf, mbedtls_x509_crt *cert, mbedtls_pk_context *pkey, mbedtls_entropy_context *entropy, mbedtls_ctr_drbg_context *ctr_drbg, mbedtls_ssl_cache_context *cache)
+websocket_return_t websocket_tls_init(websocket_t *data, mbedtls_ssl_config *conf, mbedtls_x509_crt *cert, mbedtls_pk_context *pkey, mbedtls_entropy_context *entropy, mbedtls_ctr_drbg_context *ctr_drbg, mbedtls_ssl_cache_context *cache)
 {
        int r;
        const char *crt = mbedtls_test_srv_crt;
@@ -193,7 +193,7 @@ websocket_return_t websocket_tls_init(int param, websocket_t *data, mbedtls_ssl_
        size_t cacrt_len = mbedtls_test_cas_pem_len;
        size_t key_len = mbedtls_test_srv_key_len;
 
-       if (param) {
+       if (data->state == WEBSOCKET_RUN_CLIENT) {
                crt = mbedtls_test_cli_crt;
                key = mbedtls_test_cli_key;
                ca_crt = mbedtls_test_cas_pem;
@@ -243,14 +243,14 @@ websocket_return_t websocket_tls_init(int param, websocket_t *data, mbedtls_ssl_
        /* 3. Setup ssl stuff */
        printf("  . Setting up the SSL data...");
 
-       if ((r = mbedtls_ssl_config_defaults(conf, param ? MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
+       if ((r = mbedtls_ssl_config_defaults(conf, (data->state == WEBSOCKET_RUN_CLIENT)  ? MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
                printf("Error: mbedtls_ssl_config_defaults returned %d\n", r);
                return WEBSOCKET_INIT_ERROR;
        }
 
        mbedtls_ssl_conf_rng(conf, mbedtls_ctr_drbg_random, ctr_drbg);
        mbedtls_ssl_conf_dbg(conf, websocket_tls_debug, stdout);
-       if (!param) {
+       if (data->state == WEBSOCKET_RUN_SERVER) {
                mbedtls_ssl_cache_init(cache);
                mbedtls_ssl_conf_session_cache(conf, cache, mbedtls_ssl_cache_get, mbedtls_ssl_cache_set);
        }
@@ -495,13 +495,14 @@ int websocket_client(void *arg)
        websocket_cli->fd = -1;
        websocket_cli->cb = &cb;
        websocket_cli->tls_enabled = tls;
+       websocket_cli->state = WEBSOCKET_RUN_CLIENT; // websocket state assigned here temporary before easy_TLS api
 
        /* TLS init routine */
        if (tls) {
 #ifdef MBEDTLS_DEBUG_C
                mbedtls_debug_set_threshold(MBEDTLS_DEBUG_LEVEL);
 #endif
-               if ((r = websocket_tls_init(1, websocket_cli, &conf, &cert, &pkey, &entropy, &ctr_drbg, &cache)) != WEBSOCKET_SUCCESS) {
+               if ((r = websocket_tls_init(websocket_cli, &conf, &cert, &pkey, &entropy, &ctr_drbg, &cache)) != WEBSOCKET_SUCCESS) {
                        printf("fail to init TLS, error: %d\n", r);
                        goto WEB_CLI_EXIT;
                }
@@ -563,7 +564,7 @@ int websocket_client(void *arg)
        }
 
        /* wait until every message is tested. */
-       while (websocket_cli->state == WEBSOCKET_RUNNING) {
+       while (websocket_cli->state == WEBSOCKET_RUN_CLIENT) {
                /* all echo back message received */
                if (received_cnt == send_cnt) {
                        printf("all message was received well\n");
@@ -655,13 +656,14 @@ int websocket_server(void *arg)
        websocket_srv->fd = -1;
        websocket_srv->cb = &cb;
        websocket_srv->tls_enabled = tls;
+       websocket_srv->state = WEBSOCKET_RUN_SERVER; // websocket state assigned here temporary before easy_TLS api
 
        /* TLS init routine */
        if (tls) {
 #ifdef MBEDTLS_DEBUG_C
                mbedtls_debug_set_threshold(MBEDTLS_DEBUG_LEVEL);
 #endif
-               if ((r = websocket_tls_init(0, websocket_srv, &conf, &cert, &pkey, &entropy, &ctr_drbg, &cache)) != WEBSOCKET_SUCCESS) {
+               if ((r = websocket_tls_init(websocket_srv, &conf, &cert, &pkey, &entropy, &ctr_drbg, &cache)) != WEBSOCKET_SUCCESS) {
                        printf("fail to init TLS\n");
                        goto WEB_SRV_EXIT;
                }
index ad0e0d7..da6b544 100644 (file)
@@ -309,7 +309,8 @@ typedef enum {
  */
 enum websocket_state {
        WEBSOCKET_STOP,                         ///< stopped
-       WEBSOCKET_RUNNING,                      ///< running
+       WEBSOCKET_RUN_CLIENT,                   ///< client is running
+       WEBSOCKET_RUN_SERVER,                   ///< server is running
        WEBSOCKET_MAX_STATE                     ///< websocket number of max state.
 };
 
@@ -356,7 +357,7 @@ enum websocket_opcode {
  */
 typedef struct {
        int state;
-///< 0 - closed 1 - running
+///< 0 - closed / 1 - running client / 2 - running server
        int fd;
 ///< Each server or client has own socket fd
        int tls_enabled;
index 7897f58..1673b50 100644 (file)
@@ -569,7 +569,7 @@ int websocket_accept_handler(websocket_t *init_server)
                ws_srv_table[i].state = WEBSOCKET_STOP;
        }
 
-       init_server->state = WEBSOCKET_RUNNING;
+       init_server->state = WEBSOCKET_RUN_SERVER;
        while (init_server->state != WEBSOCKET_STOP) {
                FD_ZERO(&init_server_read_fds);
                FD_SET(listen_fd, &init_server_read_fds);
@@ -779,7 +779,7 @@ websocket_t *websocket_find_table(void)
                return NULL;
        }
 
-       websocket_update_state(&ws_srv_table[i], WEBSOCKET_RUNNING);
+       websocket_update_state(&ws_srv_table[i], WEBSOCKET_RUN_SERVER);
 
        return &ws_srv_table[i];
 }
@@ -796,7 +796,9 @@ websocket_return_t websocket_client_open(websocket_t *client, char *host, char *
                return WEBSOCKET_ALLOCATION_ERROR;
        }
 
-       websocket_update_state(client, WEBSOCKET_RUNNING);
+       websocket_update_state(client, WEBSOCKET_RUN_CLIENT);
+
+       //TODO : all TLS initializing code will be moved from example to here
 
        if (websocket_connect(client, host, port) != WEBSOCKET_SUCCESS) {
                r = WEBSOCKET_CONNECT_ERROR;
@@ -903,6 +905,8 @@ websocket_return_t websocket_server_open(websocket_t *init_server)
                return WEBSOCKET_ALLOCATION_ERROR;
        }
 
+       //TODO : all TLS initializing code will be moved from example to here
+
        port = init_server->tls_enabled ? 443 : 80;
 
        if (websocket_listen(&(init_server->fd), port) != WEBSOCKET_SUCCESS) {