From 682189a994aae8bca898d56426200a28fd50ad95 Mon Sep 17 00:00:00 2001 From: kang Date: Thu, 7 Sep 2017 09:18:58 +0900 Subject: [PATCH] apps/websocket_sample bug fix send input parameter between threads, and make receive wait time longer --- apps/examples/webclient/webclient_main.c | 48 ++++++++--- apps/examples/webserver/webserver_main.c | 46 +++++++---- apps/examples/websocket/websocket_main.c | 96 ++++++++++++++++++---- external/include/protocols/webclient.h | 2 +- external/include/protocols/webserver/http_server.h | 2 +- external/include/protocols/websocket.h | 3 +- .../src/ip_adapter/tizenrt/caipnwmonitor.c | 4 +- external/webclient/webclient.c | 1 + external/webserver/http.c | 3 +- external/websocket/websocket.c | 1 + os/net/Kconfig | 4 +- 11 files changed, 158 insertions(+), 52 deletions(-) mode change 100755 => 100644 apps/examples/webclient/webclient_main.c mode change 100755 => 100644 apps/examples/webserver/webserver_main.c diff --git a/apps/examples/webclient/webclient_main.c b/apps/examples/webclient/webclient_main.c old mode 100755 new mode 100644 index f9716f9..9647b24 --- a/apps/examples/webclient/webclient_main.c +++ b/apps/examples/webclient/webclient_main.c @@ -83,11 +83,22 @@ #define WEBCLIENT_SCHED_POLICY SCHED_RR #define WEBCLIENT_BUF_SIZE 4600 +#define WEBCLIENT_FREE_INPUT(node, size) \ + do { \ + int m = 0; \ + for (; m < size; m++) { \ + free(node->argv[m]); \ + } \ + free(node->argv); \ + free(node); \ + } while (0) /**************************************************************************** * Private Data ****************************************************************************/ + + struct webclient_input { int argc; char **argv; @@ -227,6 +238,7 @@ int webclient_init_request(void *arg, struct http_client_request_t *request) int argc, i; char **argv; char *p, *q; + int ret = -1; struct webclient_input *input; input = arg; @@ -238,7 +250,7 @@ int webclient_init_request(void *arg, struct http_client_request_t *request) memset(request, 0, sizeof(struct http_client_request_t)); if (argc < 3) { - return -1; + goto exit; } if (!strcmp(argv[1], "GET")) { @@ -251,11 +263,17 @@ int webclient_init_request(void *arg, struct http_client_request_t *request) request->method = WGET_MODE_DELETE; } else { dump_webclient_usage(); - return -1; + goto exit; } /* argument2 is url. */ - request->url = argv[2]; + request->url = (char *)malloc(strlen(argv[2]) + 1); + if (!request->url) { + goto exit; + } + strcpy(request->url, argv[2]); + request->url[strlen(argv[2])] = '\0'; + #ifdef CONFIG_NET_SECURITY_TLS if (!strncmp(request->url, "https", 5)) { g_https = 1; @@ -264,13 +282,13 @@ int webclient_init_request(void *arg, struct http_client_request_t *request) if (!strncmp(request->url, "http", 4)) { g_https = 0; } else { - return -1; + goto exit; } for (i = 3; i < argc; i++) { p = argv[i]; if ((q = strchr(p, '=')) == NULL) { - return -1; + goto exit; } *q++ = '\0'; @@ -285,21 +303,25 @@ int webclient_init_request(void *arg, struct http_client_request_t *request) if (t > 0 && t <= WEBCLIENT_CONF_MAX_ENTITY_SIZE) { request->entity = (char *)malloc(t); if (request->entity == NULL) { - return -1; + goto exit; } g_testentity = 1; memset(request->entity, '1', t); } else { printf("entity is too big\n"); - return -1; + goto exit; } } else { - return -1; + goto exit; } } request->buflen = WEBCLIENT_BUF_SIZE; - return 0; + ret = 0; +exit: + WEBCLIENT_FREE_INPUT(input, input->argc); + + return ret; } pthread_addr_t webclient_cb(void *arg) @@ -411,6 +433,7 @@ int webclient_main(int argc, char *argv[]) } input->argv = (char **)malloc(sizeof(char *) * argc); if (!input->argv) { + free(input); printf(" malloc argv fail\n"); return 0; } @@ -419,13 +442,18 @@ int webclient_main(int argc, char *argv[]) int i = 0; for (; i < argc; i++) { input->argv[i] = (char *)malloc(sizeof(char) * (strlen(argv[i]) + 1)); + if (!input->argv[i]) { + WEBCLIENT_FREE_INPUT(input, i); + return -1; + } strcpy(input->argv[i], argv[i]); } status = pthread_create(&tid, &attr, webclient_cb, input); - if (status < 0) { printf("fail to start webclient\n"); + WEBCLIENT_FREE_INPUT(input, argc); + g_running = 0; return -1; } diff --git a/apps/examples/webserver/webserver_main.c b/apps/examples/webserver/webserver_main.c old mode 100755 new mode 100644 index ad51f83..c170c4d --- a/apps/examples/webserver/webserver_main.c +++ b/apps/examples/webserver/webserver_main.c @@ -69,6 +69,15 @@ #define WEBSERVER_STACK_SIZE (1024 * 8) #define WEBSERVER_SCHED_PRI 100 #define WEBSERVER_SCHED_POLICY SCHED_RR +#define WEBSERVER_FREE_INPUT(node, size) \ + do { \ + int m = 0; \ + for (; m < size; m++) { \ + free(node->argv[m]); \ + } \ + free(node->argv); \ + free(node); \ + } while (0) struct webserver_input { int argc; @@ -338,11 +347,11 @@ void ws_server_on_msg_cb(websocket_context_ptr ctx, const websocket_on_msg_arg * void print_webserver_usage(void) { printf("\n webserver usage:\n"); - printf(" $ webserver [operation] [option]\n"); - printf("\n [operation] : %%s (webserver start or stop)\n"); - printf("\n [option] : %%s default:require (require, optional, none)\n"); + printf(" $ webserver OPERATION OPTION\n"); + printf("\n OPERATION : %%s (webserver start or stop)\n"); + printf("\n OPTION : %%s default:require (require, optional, none)\n"); printf("\n example:\n"); - printf(" $ webserver start\n"); + printf(" $ webserver start none\n"); } @@ -385,16 +394,13 @@ pthread_addr_t httptest_cb(void *arg) struct ssl_config_t ssl_config; int auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED; #endif - int i = 0; struct webserver_input *input = arg; - - if (!input && (input->argc != 2 || input->argc != 3)) { - print_webserver_usage(); - goto release; - } - if (!strcmp(input->argv[1], "start")) { #ifdef CONFIG_NET_SECURITY_TLS + if (input->argc != 3) { + print_webserver_usage(); + goto release; + } if (strcmp(input->argv[2], "required") == 0) { auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED; } else if (strcmp(input->argv[2], "optional") == 0) { @@ -402,11 +408,16 @@ pthread_addr_t httptest_cb(void *arg) } else if (strcmp(input->argv[2], "none") == 0) { auth_mode = MBEDTLS_SSL_VERIFY_NONE; } else { + print_webserver_usage(); goto release; } #endif goto start; } else if (!strcmp(input->argv[1], "stop")) { + if (input->argc != 2) { + print_webserver_usage(); + goto release; + } goto stop; } else { print_webserver_usage(); @@ -476,11 +487,7 @@ stop: printf("webserver end\n"); release: - for (; i < input->argc; i++) { - free(input->argv[i]); - } - free(input); - + WEBSERVER_FREE_INPUT(input, input->argc); return NULL; } @@ -501,6 +508,7 @@ int webserver_main(int argc, char *argv[]) input->argv = (char **)malloc(sizeof(char *) * argc); if (!input->argv) { printf(" malloc argv fail\n"); + free(input); return -1; } @@ -508,9 +516,12 @@ int webserver_main(int argc, char *argv[]) int i = 0; for (; i < argc; i++) { input->argv[i] = (char *)malloc(sizeof(char) * (strlen(argv[i]) + 1)); + if (!input->argv[i]) { + WEBSERVER_FREE_INPUT(input, i); + return -1; + } strcpy(input->argv[i], argv[i]); } - status = pthread_attr_init(&attr); if (status != 0) { printf("fail to start webserver\n"); @@ -525,6 +536,7 @@ int webserver_main(int argc, char *argv[]) status = pthread_create(&tid, &attr, httptest_cb, input); if (status < 0) { printf("fail to start webserver\n"); + WEBSERVER_FREE_INPUT(input, input->argc); return -1; } pthread_setname_np(tid, "webserver"); diff --git a/apps/examples/websocket/websocket_main.c b/apps/examples/websocket/websocket_main.c index 1d96acb..521bbcd 100644 --- a/apps/examples/websocket/websocket_main.c +++ b/apps/examples/websocket/websocket_main.c @@ -135,7 +135,7 @@ #define WEBSOCKET_EXAMPLE_STACKSIZE (1024 * 10) -/* +/* * TLS debug configure (0 ~ 5) * * This configuration is good to debug TLS handshake state. But, more than @@ -163,6 +163,16 @@ " $ websocket server 1\n" \ " $ websocket client 127.0.0.1 443 0 1 100 10\n" +struct options_s{ + int mode; + int tls_mode; + char server_port[8]; + char server_ip[20]; + char path[32]; + int size; + int num; +}; + /**************************************************************************** * Private Data ****************************************************************************/ @@ -180,7 +190,13 @@ 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(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; @@ -245,6 +261,8 @@ websocket_return_t websocket_tls_init(websocket_t *data, mbedtls_ssl_config *con return WEBSOCKET_INIT_ERROR; } + /* ToDo: to access web browser it should be a ssl_verify none mode*/ + mbedtls_ssl_conf_authmode(conf, MBEDTLS_SSL_VERIFY_NONE); mbedtls_ssl_conf_rng(conf, mbedtls_ctr_drbg_random, ctr_drbg); mbedtls_ssl_conf_dbg(conf, websocket_tls_debug, stdout); if (data->state == WEBSOCKET_RUN_SERVER) { @@ -426,13 +444,13 @@ int websocket_client(void *arg) { int i; int r = WEBSOCKET_SUCCESS; - char **argv = arg; + struct options_s *opt = arg; char *addr = NULL; char *port = NULL; char *path = NULL; - int tls = atoi(argv[3]); - int size = atoi(argv[4]); - int send_cnt = atoi(argv[5]); + int tls = opt->tls_mode; + int size = opt->size; + int send_cnt = opt->num; websocket_frame_t *tx_frame = NULL; websocket_t *websocket_cli = NULL; char *test_message = NULL; @@ -470,17 +488,17 @@ int websocket_client(void *arg) return -1; } - addr = calloc(1, (strlen(argv[0]) + 1)); - port = calloc(1, (strlen(argv[1]) + 1)); - path = calloc(1, (strlen(argv[2]) + 1)); + addr = calloc(1, (strlen(opt->server_ip) + 1)); + port = calloc(1, (strlen(opt->server_port) + 1)); + path = calloc(1, (strlen(opt->path) + 1)); if (addr == NULL || port == NULL || path == NULL) { printf("fail to allocate memory\n"); goto WEB_CLI_EXIT; } - strncpy(addr, argv[0], strlen(argv[0])); - strncpy(port, argv[1], strlen(argv[1])); - strncpy(path, argv[2], strlen(argv[2])); + strncpy(addr, opt->server_ip, strlen(opt->server_ip)); + strncpy(port, opt->server_port, strlen(opt->server_port)); + strncpy(path, opt->path, strlen(opt->path)); received_cnt = 0; websocket_cli = calloc(1, sizeof(websocket_t)); @@ -603,6 +621,9 @@ WEB_CLI_EXIT: if (test_message) { free(test_message); } + if (opt) { + free(opt); + } g_wcenabled = 0; @@ -613,8 +634,8 @@ WEB_CLI_EXIT: int websocket_server(void *arg) { int r; - char **argv = arg; - int tls = atoi(argv[0]); + struct options_s *opt = arg; + int tls = opt->tls_mode; static websocket_cb_t cb = { recv_cb, /* recv callback */ send_cb, /* send callback */ @@ -625,6 +646,9 @@ int websocket_server(void *arg) echoback_on_msg_cb /* recv message callback */ }; + if (opt) { + free(opt); // it is not used anymore + } if (tls != 0 && tls != 1) { printf("wrong tls option\n %s\n", WEBSOCKET_USAGE); return WEBSOCKET_INIT_ERROR; @@ -699,23 +723,59 @@ int websocket_main(int argc, char *argv[]) int status; pthread_attr_t attr; pthread_t tid; + struct options_s *input; + if (argc < 3) { + goto error_with_input; + } + + /* Parsing input parameters*/ + input = (struct options_s *)malloc(sizeof(struct options_s)); + if (!input) { + printf("memory alloc error\n"); + return -1; + } + memset(input->server_ip, 0, 20); + memset(input->path, 0, 32); + + if (strncmp(argv[1], "server", strlen("server") + 1) == 0) { + if (argc != 3) { + goto error_with_input; + } + input->tls_mode = atoi(argv[2]); + } + else if(strncmp(argv[1], "client", strlen("client") + 1) == 0) { + if (argc != 8) { + goto error_with_input; + } + + strncpy(input->server_ip, argv[2], 20); + strncpy(input->server_port, argv[3], 8); + strncpy(input->path, argv[4], 32); + input->tls_mode = atoi(argv[5]); + input->size = atoi(argv[6]); + input->num = atoi(argv[7]); + } + else { + goto error_with_input; + } if ((status = pthread_attr_init(&attr)) != 0) { printf("fail to init thread\n"); return -1; } + pthread_attr_setstacksize(&attr, WEBSOCKET_EXAMPLE_STACKSIZE); pthread_attr_setschedpolicy(&attr, WEBSOCKET_SCHED_POLICY); if (memcmp(argv[1], "client", strlen("client")) == 0 && argc == 8) { - if ((status = pthread_create(&tid, &attr, (pthread_startroutine_t)websocket_client, (void *)(argv + 2))) != 0) { + if ((status = pthread_create(&tid, &attr, (pthread_startroutine_t)websocket_client, (void *)input)) != 0) { printf("fail to create thread\n"); return -1; } pthread_setname_np(tid, "websocket client"); pthread_detach(tid); } else if (memcmp(argv[1], "server", strlen("server")) == 0 && argc == 3) { - if ((status = pthread_create(&tid, &attr, (pthread_startroutine_t)websocket_server, (void *)(argv + 2))) != 0) { + if ((status = pthread_create(&tid, &attr, (pthread_startroutine_t)websocket_server, (void *)input)) != 0) { printf("fail to create thread\n"); return -1; } @@ -726,4 +786,8 @@ int websocket_main(int argc, char *argv[]) return -1; } return 0; + +error_with_input: + printf("%s", WEBSOCKET_USAGE); + return -1; } diff --git a/external/include/protocols/webclient.h b/external/include/protocols/webclient.h index cb0ce59..e677ffe 100644 --- a/external/include/protocols/webclient.h +++ b/external/include/protocols/webclient.h @@ -72,7 +72,7 @@ #define WEBCLIENT_CONF_MAX_PHRASE_SIZE 50 #define WEBCLIENT_CONF_MAX_MESSAGE_SIZE 2100 -#define WEBCLIENT_CONF_TIMEOUT_MSEC 5000 +#define WEBCLIENT_CONF_TIMEOUT_MSEC 50000 #define WEBCLIENT_CONF_MIN_TLS_MEMORY 100000 #define WEBCLIENT_CONF_HANDSHAKE_RETRY 3 diff --git a/external/include/protocols/webserver/http_server.h b/external/include/protocols/webserver/http_server.h index 76d04a1..a24c2f1 100644 --- a/external/include/protocols/webserver/http_server.h +++ b/external/include/protocols/webserver/http_server.h @@ -90,7 +90,7 @@ #define HTTP_CONF_MAX_CLIENT 16 #define HTTP_CONF_CLIENT_STACKSIZE 8192 #define HTTP_CONF_MIN_TLS_MEMORY 80000 -#define HTTP_CONF_SOCKET_TIMEOUT_MSEC 5000 +#define HTTP_CONF_SOCKET_TIMEOUT_MSEC 50000 #define HTTP_CONF_SERVER_MQ_MAX_MSG 10 #define HTTP_CONF_SERVER_MQ_PRIO 50 #define HTTP_CONF_SERVER_SIGWAKEUP 18 diff --git a/external/include/protocols/websocket.h b/external/include/protocols/websocket.h index 9f9a784..c217f0b 100644 --- a/external/include/protocols/websocket.h +++ b/external/include/protocols/websocket.h @@ -103,7 +103,8 @@ /** * @brief Websocket socket input timeout value, msec. */ -#define WEBSOCKET_SOCK_RCV_TIMEOUT (5 * 1000) //mili second + #define WEBSOCKET_SOCK_RCV_TIMEOUT (60 * 1000) //mili second + /** * @brief Websocket accept server select() timeout value, msec. */ diff --git a/external/iotivity/iotivity_1.2-rel/resource/csdk/connectivity/src/ip_adapter/tizenrt/caipnwmonitor.c b/external/iotivity/iotivity_1.2-rel/resource/csdk/connectivity/src/ip_adapter/tizenrt/caipnwmonitor.c index c169a29..c6c35a5 100644 --- a/external/iotivity/iotivity_1.2-rel/resource/csdk/connectivity/src/ip_adapter/tizenrt/caipnwmonitor.c +++ b/external/iotivity/iotivity_1.2-rel/resource/csdk/connectivity/src/ip_adapter/tizenrt/caipnwmonitor.c @@ -323,12 +323,12 @@ u_arraylist_t *CAFindInterfaceChange() if(len <= 0) return NULL; if(buf[0] == 'd' && buf[1] == 'e' && buf[2] == 'l') { - printf("[pkbuild] call if to adapter (down)\n\n\n\n\n\n"); + printf("Receive the event(IF is down)\n"); CARemoveNetworkMonitorList(0); CAIPPassNetworkChangesToAdapter(CA_INTERFACE_DOWN); } else if(buf[0] == 'g' && buf[1] == 'e' && buf[2] == 'n') { - printf("[pkbuild] call if to adapter (up)\n\n\n\n\n\n\n"); + printf("Receive the event(IF is UP)\n"); iflist = CAIPGetInterfaceInformation(0); if (!iflist) { OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno)); diff --git a/external/webclient/webclient.c b/external/webclient/webclient.c index 5329e4e..40afc32 100644 --- a/external/webclient/webclient.c +++ b/external/webclient/webclient.c @@ -860,6 +860,7 @@ static int wget_socket_connect(struct wget_s *ws) /* Set send and receive timeout values */ tv.tv_sec = WEBCLIENT_CONF_TIMEOUT_MSEC / 1000; tv.tv_usec = (WEBCLIENT_CONF_TIMEOUT_MSEC % 1000) * 1000; + nvdbg("webclient recv timeout(%d.%d)sec\n", tv.tv_sec, tv.tv_usec); if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&tv, sizeof(struct timeval)) < 0) { ndbg("ERROR: setsockopt failed\n"); diff --git a/external/webserver/http.c b/external/webserver/http.c index 0311a49..5a39e18 100644 --- a/external/webserver/http.c +++ b/external/webserver/http.c @@ -122,7 +122,7 @@ pthread_addr_t http_server_handler(pthread_addr_t arg) sock_fd = accept(*(volatile int *)&server->listen_fd, (struct sockaddr *)&client_addr, &addrlen); - if (sock_fd < 0 && errno != EWOULDBLOCK) { + if (sock_fd < 0 && (errno != EWOULDBLOCK && errno != ETIMEDOUT)) { HTTP_LOGE("Error: Accept client error!!\n"); continue; } @@ -130,6 +130,7 @@ pthread_addr_t http_server_handler(pthread_addr_t arg) if (sock_fd > 0) { tv.tv_sec = HTTP_CONF_SOCKET_TIMEOUT_MSEC / 1000; tv.tv_usec = (HTTP_CONF_SOCKET_TIMEOUT_MSEC % 1000) * 1000; + HTTP_LOGD("Set timeout to socket (%d.%d)sec\n", tv.tv_sec, tv.tv_usec); if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&tv, sizeof(struct timeval)) < 0) { HTTP_LOGE("Error: Fail to setsockopt\n"); diff --git a/external/websocket/websocket.c b/external/websocket/websocket.c index afc0b8b..143d104 100644 --- a/external/websocket/websocket.c +++ b/external/websocket/websocket.c @@ -131,6 +131,7 @@ websocket_return_t websocket_config_socket(int fd) tv.tv_sec = (WEBSOCKET_SOCK_RCV_TIMEOUT / 1000); tv.tv_usec = ((WEBSOCKET_SOCK_RCV_TIMEOUT % 1000) * 1000); + WEBSOCKET_DEBUG("set socket option(recv timeout sec:%d usec:%d)\n", tv.tv_sec, tv.tv_usec); if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (FAR const void *)&tv, (socklen_t) sizeof(struct timeval)) == -1) { WEBSOCKET_DEBUG("setsockopt failed\n"); return WEBSOCKET_SOCKET_ERROR; diff --git a/os/net/Kconfig b/os/net/Kconfig index 4f510bc..d2b3e82 100644 --- a/os/net/Kconfig +++ b/os/net/Kconfig @@ -39,7 +39,7 @@ endif #NET_LWIP menu "Driver buffer configuration" config NET_MULTIBUFFER - bool "Use multiple device-side I/O buffers" + bool #"Use multiple device-side I/O buffers" default n ---help--- If this configuration is selected, then the @@ -118,8 +118,6 @@ source "$EXTERNALDIR/tftpc/Kconfig.protocol" source "$EXTERNALDIR/telnetd/Kconfig.protocol" source "$EXTERNALDIR/smtp/Kconfig.protocol" -source "$EXTERNALDIR/slsi_wifi/Kconfig" - source "../framework/src/mqtt/Kconfig.protocol" config NET_SECURITY_TLS -- 2.7.4