From a1643ba07e22756d3d7a097df4c24fc035d4a819 Mon Sep 17 00:00:00 2001 From: Seonah Moon Date: Thu, 8 Apr 2021 15:11:00 +0900 Subject: [PATCH 01/16] Fix coverity issues Change-Id: Ia16714ca983ab92b60c50e40df96cd34de4f2b0e --- plugins/dns-sd/dns-sd-plugin.cpp | 6 +++--- plugins/libwebsockets/libwebsockets-plugin.cpp | 19 ++++++++++++------- tool/tool_run.cpp | 6 +++++- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/plugins/dns-sd/dns-sd-plugin.cpp b/plugins/dns-sd/dns-sd-plugin.cpp index 846293a..9ce56da 100755 --- a/plugins/dns-sd/dns-sd-plugin.cpp +++ b/plugins/dns-sd/dns-sd-plugin.cpp @@ -125,7 +125,7 @@ static void __discovered_service_set_txt_record(dns_sd_discovered_service_s *ser static void __destroy_discovered_service(dns_sd_discovered_service_s *service) { - free(service); + delete service; } /* @@ -224,9 +224,9 @@ static vine_disc_error __convert_attributes_to_txt_record(map at if (*txt_record == NULL) { ret = VINE_DISC_ERROR_OUT_OF_MEMORY; VINE_LOGE("Out of memory"); + } else { + *txt_record = memcpy(*txt_record, TXTRecordGetBytesPtr(&txt_ref), *txt_len); } - - *txt_record = memcpy(*txt_record, TXTRecordGetBytesPtr(&txt_ref), *txt_len); TXTRecordDeallocate(&txt_ref); return ret; } diff --git a/plugins/libwebsockets/libwebsockets-plugin.cpp b/plugins/libwebsockets/libwebsockets-plugin.cpp index 7874a5e..e221833 100755 --- a/plugins/libwebsockets/libwebsockets-plugin.cpp +++ b/plugins/libwebsockets/libwebsockets-plugin.cpp @@ -118,9 +118,14 @@ static void _get_peer_network_info(struct lws *wsi, char ip[], int *port) struct sockaddr_storage addr; socklen_t len = sizeof(addr); int fd; + int ret; fd = lws_get_socket_fd(wsi); - getpeername(fd, (struct sockaddr*)&addr, &len); + ret = getpeername(fd, (struct sockaddr*)&addr, &len); + if (ret < 0) { + VINE_LOGE("Cannot get name of connected peer. errno[%d]", ret); + return; + } if (addr.ss_family == AF_INET) { struct sockaddr_in *s = (struct sockaddr_in*)&addr; @@ -261,14 +266,14 @@ static void _delete_websocket_poll_fd(struct lws_pollargs *args) RET_IF(args == NULL, "args is NULL"); auto it = g_pollfds.find(args->fd); - if (it == g_pollfds.end()) { - VINE_LOGE("No fd[%d]", args->fd); + if (it != g_pollfds.end()) { + free(it->second); + g_pollfds.erase(it); + VINE_LOGI("websocket poll fd[%d] is removed.", args->fd); + } else { + VINE_LOGI("Cannot find fd[%d] but remove it from eventloop", args->fd); } - free(it->second); - g_pollfds.erase(it); - VINE_LOGI("websocket poll fd[%d] is removed.", args->fd); - if (g_callbacks.pollfd_cb) g_callbacks.pollfd_cb(VINE_DATA_PATH_POLLFD_DEL, args->fd, args->events); } diff --git a/tool/tool_run.cpp b/tool/tool_run.cpp index adcc484..79d6db2 100644 --- a/tool/tool_run.cpp +++ b/tool/tool_run.cpp @@ -143,6 +143,8 @@ static void __ip_resolved_cb(vine_session_h session, vine_service_h service, vine_service_foreach_attribute(service, __print_attr, NULL); printf("\n"); fflush(stdout); + free(service_type); + free(service_name); } static void __discovered_cb(vine_session_h session, vine_service_h service, @@ -252,6 +254,7 @@ static int _send_message_from_file(vine_dp_h dp) size = ftell(file); if (size < 0 || size >= SIZE_MAX - 1) { + fclose(file); return -1; } buf = (unsigned char *)calloc(sizeof(unsigned char), size + 1); @@ -284,7 +287,8 @@ static int _send_message(vine_dp_h dp) static int send_message(vine_dp_h dp) { - int ret; + int ret = VINE_ERROR_NONE; + if (vine_configs.file) ret = _send_message_from_file(dp); else if (vine_configs.msg) -- 2.7.4 From 1cf298ab518f5ca37cf22a4dc74aa97d5bb1cf59 Mon Sep 17 00:00:00 2001 From: Cheoleun Moon Date: Thu, 8 Apr 2021 15:58:10 +0900 Subject: [PATCH 02/16] Use delete for memory allocated by new Change-Id: I03241c8845df2bd9b0f305da4cc56577b57b9169 Signed-off-by: Cheoleun Moon --- src/vine-disc.cpp | 4 ++-- src/vine-event-loop.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/vine-disc.cpp b/src/vine-disc.cpp index 409c7ba..f76970b 100755 --- a/src/vine-disc.cpp +++ b/src/vine-disc.cpp @@ -167,13 +167,13 @@ static void __free_discovered_event(void *data) vine_discovered_event *discovered_event = (vine_discovered_event *)data; discovered_event->attributes.clear(); - free(data); + delete data; } static void __free_ip_resolved_event(void *data) { VINE_LOGD("Free ip_resolved_event[%p]", data); - free(data); + delete data; } static void __published_cb(void *plugin_handle, diff --git a/src/vine-event-loop.cpp b/src/vine-event-loop.cpp index 15e94d4..5080d92 100644 --- a/src/vine-event-loop.cpp +++ b/src/vine-event-loop.cpp @@ -161,7 +161,7 @@ void vine_event_queue_destroy(vine_event_queue_h event_fd) } if (event_fd_handle->fd >= 0) close(event_fd_handle->fd); - free(event_fd); + delete event_fd; } void vine_event_loop_get_eventfd(vine_event_queue_h event_fd, int *eventfd) -- 2.7.4 From aaab08ed6099c10269fcc9960f38bfd93bd63dbf Mon Sep 17 00:00:00 2001 From: Cheoleun Moon Date: Thu, 8 Apr 2021 16:11:21 +0900 Subject: [PATCH 03/16] Use delete for memory allocated by new (2) Change-Id: I7be8a20c08eefaf188a764a8f64a0cc26325081a Signed-off-by: Cheoleun Moon --- plugins/dns-sd/dns-sd-plugin.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/dns-sd/dns-sd-plugin.cpp b/plugins/dns-sd/dns-sd-plugin.cpp index 9ce56da..b6f744d 100755 --- a/plugins/dns-sd/dns-sd-plugin.cpp +++ b/plugins/dns-sd/dns-sd-plugin.cpp @@ -485,7 +485,7 @@ void dns_sd_deinit(void *plugin_handle) it = dns_sd_handle->sdref_map.erase(it); } - free(dns_sd_handle); + delete dns_sd_handle; } vine_disc_error dns_sd_publish(void *plugin_handle, const char *service_type, -- 2.7.4 From c2ad3578033c5c3eab6d6ec571304cb4854b3d40 Mon Sep 17 00:00:00 2001 From: Cheoleun Moon Date: Thu, 8 Apr 2021 16:19:06 +0900 Subject: [PATCH 04/16] Casting for delete Change-Id: Ifc3e1a2a78397452396ad1d6a1072c983dc73c3b Signed-off-by: Cheoleun Moon --- src/vine-disc.cpp | 4 ++-- tool/tool_run.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/vine-disc.cpp b/src/vine-disc.cpp index f76970b..53d4d4a 100755 --- a/src/vine-disc.cpp +++ b/src/vine-disc.cpp @@ -167,13 +167,13 @@ static void __free_discovered_event(void *data) vine_discovered_event *discovered_event = (vine_discovered_event *)data; discovered_event->attributes.clear(); - delete data; + delete discovered_event; } static void __free_ip_resolved_event(void *data) { VINE_LOGD("Free ip_resolved_event[%p]", data); - delete data; + delete (vine_ip_resolved_event *)data; } static void __published_cb(void *plugin_handle, diff --git a/tool/tool_run.cpp b/tool/tool_run.cpp index 79d6db2..43cf194 100644 --- a/tool/tool_run.cpp +++ b/tool/tool_run.cpp @@ -257,7 +257,7 @@ static int _send_message_from_file(vine_dp_h dp) fclose(file); return -1; } - buf = (unsigned char *)calloc(sizeof(unsigned char), size + 1); + buf = (unsigned char *)calloc(sizeof(unsigned char), size); fseek(file, 0, SEEK_SET); count = fread(buf, sizeof(unsigned char), size, file); -- 2.7.4 From 2dcf317d8d8d80c63bfcd0c6ce4949aef7aa303e Mon Sep 17 00:00:00 2001 From: Cheoleun Moon Date: Thu, 8 Apr 2021 16:36:31 +0900 Subject: [PATCH 05/16] Casting for delete (2) Change-Id: I9ae14dc0e433e2b35278a172b1a574cafac22a54 Signed-off-by: Cheoleun Moon --- src/vine-event-loop.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vine-event-loop.cpp b/src/vine-event-loop.cpp index 5080d92..bff62cc 100644 --- a/src/vine-event-loop.cpp +++ b/src/vine-event-loop.cpp @@ -161,7 +161,7 @@ void vine_event_queue_destroy(vine_event_queue_h event_fd) } if (event_fd_handle->fd >= 0) close(event_fd_handle->fd); - delete event_fd; + delete event_fd_handle; } void vine_event_loop_get_eventfd(vine_event_queue_h event_fd, int *eventfd) -- 2.7.4 From 8f5086df0ef5336298dcdf951738002c4c81f5f3 Mon Sep 17 00:00:00 2001 From: Cheoleun Moon Date: Thu, 8 Apr 2021 16:46:02 +0900 Subject: [PATCH 06/16] tests: Free after use Change-Id: I92b74905b6fc64567be9ae748f13bc1882b38074 Signed-off-by: Cheoleun Moon --- tests/unittest/vine-unittest-security.cpp | 1 + tests/verifier/vine-verifier.cpp | 1 + tests/vine-test/vine-multi-thread-test.cpp | 3 +++ tests/vine-test/vine-test.cpp | 15 ++++++++++++--- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/tests/unittest/vine-unittest-security.cpp b/tests/unittest/vine-unittest-security.cpp index 771a3d9..f8b4461 100755 --- a/tests/unittest/vine-unittest-security.cpp +++ b/tests/unittest/vine-unittest-security.cpp @@ -298,4 +298,5 @@ TEST_F(VineSecurityTest, GetPskP) EXPECT_EQ(VINE_ERROR_NONE, vine_security_get_psk(security, &psk)); + free(psk); } diff --git a/tests/verifier/vine-verifier.cpp b/tests/verifier/vine-verifier.cpp index fc10a37..bd33951 100644 --- a/tests/verifier/vine-verifier.cpp +++ b/tests/verifier/vine-verifier.cpp @@ -244,6 +244,7 @@ static void __register_service(vine_session_h session, int port) vine_session_set_registered_cb(session, __registered_cb, NULL); PRINT_IF_ERROR(vine_session_register(session, service, NULL), "vine_session_register"); + vine_service_destroy(service); } static void __server_opened_cb(vine_dp_h dp, vine_error_e result, void *user_data) diff --git a/tests/vine-test/vine-multi-thread-test.cpp b/tests/vine-test/vine-multi-thread-test.cpp index 7f9a831..1189d46 100644 --- a/tests/vine-test/vine-multi-thread-test.cpp +++ b/tests/vine-test/vine-multi-thread-test.cpp @@ -149,6 +149,9 @@ static void __discovered_cb(vine_session_h session, vine_service_h service, PRINT_IF_ERROR(ret, "vine_service_foreach_attribute"); PRINT_LOG("\n"); fflush(stdout); + + free(service_type); + free(service_name); } #define VINE_LOGGER_TIME_MESSAGE_LEN 17 diff --git a/tests/vine-test/vine-test.cpp b/tests/vine-test/vine-test.cpp index 2994cb9..4102b23 100644 --- a/tests/vine-test/vine-test.cpp +++ b/tests/vine-test/vine-test.cpp @@ -219,6 +219,9 @@ static void __ip_resolved_cb(vine_session_h session, vine_service_h service, PRINT_IF_ERROR(ret, "vine_service_foreach_attribute"); printf("\n"); fflush(stdout); + + free(service_type); + free(service_name); } static void __discovered_cb(vine_session_h session, vine_service_h service, @@ -554,8 +557,10 @@ static vine_security_h __create_security_handle(int type, bool is_server) if (vine_security_create(&s) != VINE_ERROR_NONE) return NULL; - if (vine_security_set_type(s, (vine_security_type_e)type) != VINE_ERROR_NONE) + if (vine_security_set_type(s, (vine_security_type_e)type) != VINE_ERROR_NONE) { + vine_security_destroy(s); return NULL; + } if (type >= VINE_SECURITY_TYPE_TLS) { vine_security_set_ca_path(s, ROOT_CA_PATH); @@ -612,7 +617,9 @@ static void __open_server() vine_dp_set_terminated_cb(g_server_dp, __terminated_cb, NULL); vine_dp_set_address_family(g_server_dp, (vine_address_family_e)addr_family); vine_dp_set_port(g_server_dp, port); - vine_dp_set_security(g_server_dp, __create_security_handle(security_type, true)); + vine_security_h security = __create_security_handle(security_type, true); + vine_dp_set_security(g_server_dp, security); + vine_security_destroy(security); PRINT_RESULT(vine_dp_open(g_server_dp, __opened_cb, NULL), "vine_dp_open"); } @@ -652,7 +659,9 @@ static void __connect_server() vine_dp_set_remote_ip(g_client_dp, addr_type ? VINE_ADDRESS_FAMILY_IPV6 : VINE_ADDRESS_FAMILY_IPV4, ip); PRINT_RESULT(vine_dp_set_port(g_client_dp, port), "vine_dp_set_port"); - vine_dp_set_security(g_client_dp, __create_security_handle(security_type, false)); + vine_security_h security = __create_security_handle(security_type, true); + vine_dp_set_security(g_client_dp, security); + vine_security_destroy(security); PRINT_RESULT(vine_dp_open(g_client_dp, __opened_cb, NULL), "vine_dp_open"); } -- 2.7.4 From efca060a1ec95eb05e55b14dff83d833b570eea2 Mon Sep 17 00:00:00 2001 From: Cheoleun Moon Date: Thu, 8 Apr 2021 16:56:02 +0900 Subject: [PATCH 07/16] Reduce MAX_VINE_EPOLL_EVENTS to 512 Change-Id: I73ede551a16e4fb6bda8b78904892f95919a39da Signed-off-by: Cheoleun Moon --- src/include/vine-event-loop.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/include/vine-event-loop.h b/src/include/vine-event-loop.h index 0bb8c83..4639815 100644 --- a/src/include/vine-event-loop.h +++ b/src/include/vine-event-loop.h @@ -56,6 +56,6 @@ int vine_event_loop_add_event(vine_event_queue_h event_fd, void *event_data, vine_event_handler handler, vine_event_free_handler free_func, void *user_data); -#define MAX_VINE_EPOLL_EVENTS 1024 +#define MAX_VINE_EPOLL_EVENTS 512 #endif /* __VINE_EVENT_LOOP_H__ */ -- 2.7.4 From ef4d1d09cd7d6a0031b3607b4108a9c4e320d0fc Mon Sep 17 00:00:00 2001 From: Cheoleun Moon Date: Thu, 8 Apr 2021 17:48:45 +0900 Subject: [PATCH 08/16] tests: Free after use (2) Change-Id: I59ecb802b1b4f79c3cc143b99589e4aa711b3a6a Signed-off-by: Cheoleun Moon --- tests/vine-test/vine-test.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/vine-test/vine-test.cpp b/tests/vine-test/vine-test.cpp index 4102b23..00a8cf6 100644 --- a/tests/vine-test/vine-test.cpp +++ b/tests/vine-test/vine-test.cpp @@ -252,6 +252,8 @@ static void __discovered_cb(vine_session_h session, vine_service_h service, fflush(stdout); g_service_list.push_back(service); + free(service_type); + free(service_name); } static void __print_received_data(unsigned char *buf, size_t len) -- 2.7.4 From c5e1e07f0cc98d8b7d3c0f9e8d4f45f06f9d00be Mon Sep 17 00:00:00 2001 From: Seonah Moon Date: Thu, 8 Apr 2021 18:17:04 +0900 Subject: [PATCH 09/16] Change the package name to capi-network-vine Change-Id: I3b9a7a196ba61bdb91b15dd5831d89bbbf7521e6 --- packaging/{vine.manifest => capi-network-vine.manifest} | 0 packaging/{vine.spec => capi-network-vine.spec} | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename packaging/{vine.manifest => capi-network-vine.manifest} (100%) rename packaging/{vine.spec => capi-network-vine.spec} (99%) diff --git a/packaging/vine.manifest b/packaging/capi-network-vine.manifest similarity index 100% rename from packaging/vine.manifest rename to packaging/capi-network-vine.manifest diff --git a/packaging/vine.spec b/packaging/capi-network-vine.spec similarity index 99% rename from packaging/vine.spec rename to packaging/capi-network-vine.spec index c3c5869..9650952 100755 --- a/packaging/vine.spec +++ b/packaging/capi-network-vine.spec @@ -1,6 +1,6 @@ %bcond_without lws_static %bcond_without lws_static_prebuilt -Name: vine +Name: capi-network-vine Summary: An service discovery framework Version: 1.0.0 Release: 0 -- 2.7.4 From b5c7607ef3c7dec5595390e59c3b692c548b3994 Mon Sep 17 00:00:00 2001 From: Cheoleun Moon Date: Thu, 8 Apr 2021 19:26:41 +0900 Subject: [PATCH 10/16] tests: Free after use (3) Change-Id: Ia7c27f9713fb3b23ebbad43f8876682383f21eac Signed-off-by: Cheoleun Moon --- tests/verifier/vine-verifier.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/verifier/vine-verifier.cpp b/tests/verifier/vine-verifier.cpp index bd33951..93d0937 100644 --- a/tests/verifier/vine-verifier.cpp +++ b/tests/verifier/vine-verifier.cpp @@ -388,6 +388,9 @@ static void __discovered_cb(vine_session_h session, vine_service_h service, ret = vine_session_set_ip_resolved_cb(session, service, __ip_resolved_cb, user_data); PRINT_IF_ERROR(ret, "vine_session_set_ip_resolved_cb"); } + + free(service_type); + free(service_name); } static void __start_client(bool use_tls, bool use_psk) -- 2.7.4 From eff8d1823579880ac8d88459d37384beb61d7f46 Mon Sep 17 00:00:00 2001 From: Cheoleun Moon Date: Mon, 12 Apr 2021 11:32:31 +0900 Subject: [PATCH 11/16] Use localtime_r instead of localtime Change-Id: I808747c6f88c42b2794a08bbcc2d0c0abf653eaa Signed-off-by: Cheoleun Moon --- tests/vine-test/vine-multi-thread-test.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/tests/vine-test/vine-multi-thread-test.cpp b/tests/vine-test/vine-multi-thread-test.cpp index 1189d46..86de01a 100644 --- a/tests/vine-test/vine-multi-thread-test.cpp +++ b/tests/vine-test/vine-multi-thread-test.cpp @@ -159,16 +159,19 @@ static void __get_current_time(char *buf) { int hour, min, sec, day, month; time_t now; - struct tm *local; + struct tm local; time(&now); - local = localtime(&now); + if (localtime_r(&now, &local) == NULL) { + __print_error("localtime_r() fails"); + return; + } - hour = local->tm_hour; - min = local->tm_min; - sec = local->tm_sec; - day = local->tm_mday; - month = local->tm_mon + 1; + hour = local.tm_hour; + min = local.tm_min; + sec = local.tm_sec; + day = local.tm_mday; + month = local.tm_mon + 1; snprintf(buf, VINE_LOGGER_TIME_MESSAGE_LEN, "[%02u-%02u %02u:%02u:%02u]", month, day, hour, min, sec); -- 2.7.4 From 982cf8b178a02dc3cb5c05c0c8956b9dd28190a3 Mon Sep 17 00:00:00 2001 From: Cheoleun Moon Date: Mon, 12 Apr 2021 11:33:17 +0900 Subject: [PATCH 12/16] Limits the file size to be sent to INT_MAX Change-Id: Ia8ff7e3429364f5e645dc987fcf23125199773e6 Signed-off-by: Cheoleun Moon --- tool/tool_run.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tool/tool_run.cpp b/tool/tool_run.cpp index 43cf194..c039d6f 100644 --- a/tool/tool_run.cpp +++ b/tool/tool_run.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #define RESET_COLOR "\e[m" #define MAKE_RED "\e[31m" @@ -253,7 +254,7 @@ static int _send_message_from_file(vine_dp_h dp) fseek(file, 0, SEEK_END); size = ftell(file); - if (size < 0 || size >= SIZE_MAX - 1) { + if (size < 0 || size >= INT_MAX - 1) { fclose(file); return -1; } -- 2.7.4 From 8bb5b0c897c5a320be611683f5aeefb510968255 Mon Sep 17 00:00:00 2001 From: Cheoleun Moon Date: Mon, 12 Apr 2021 12:10:19 +0900 Subject: [PATCH 13/16] Free allocated memories before strdup Change-Id: Iba469a2a10df78a19af1c6107bdea240fdce999b Signed-off-by: Cheoleun Moon --- src/vine-data-path.cpp | 2 ++ src/vine-security.cpp | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/src/vine-data-path.cpp b/src/vine-data-path.cpp index 7d56749..4a255cf 100755 --- a/src/vine-data-path.cpp +++ b/src/vine-data-path.cpp @@ -687,6 +687,8 @@ int vine_data_path_connect(vine_address_family_e addr_family, _vine_data_path_create(VINE_DATA_PATH_TYPE_CLIENT, security, ip, port, NULL, event_fd); RET_VAL_IF(dp == NULL, VINE_ERROR_OUT_OF_MEMORY, "Out of memory"); + if (dp->addr) + free(dp->addr); dp->addr = STRDUP(ip); dp->port = port; dp->connected_cb = callback; diff --git a/src/vine-security.cpp b/src/vine-security.cpp index b9b5201..57e9f80 100755 --- a/src/vine-security.cpp +++ b/src/vine-security.cpp @@ -171,6 +171,8 @@ int _vine_security_set_ca_path(vine_security_h security, const char *ca_path) RET_VAL_IF(ca_path == NULL, VINE_ERROR_INVALID_PARAMETER, "ca_path is NULL"); vine_security_s *s = (vine_security_s *)security; + if (s->ca_path) + free(s->ca_path); s->ca_path = STRDUP(ca_path); return VINE_ERROR_NONE; @@ -193,6 +195,8 @@ int _vine_security_set_cert_path(vine_security_h security, const char *cert_path RET_VAL_IF(cert_path == NULL, VINE_ERROR_INVALID_PARAMETER, "cert_path is NULL"); vine_security_s *s = (vine_security_s *)security; + if (s->cert_path) + free(s->cert_path); s->cert_path = STRDUP(cert_path); return VINE_ERROR_NONE; @@ -215,6 +219,8 @@ int _vine_security_set_private_key(vine_security_h security, const char *key_pat RET_VAL_IF(key_path == NULL, VINE_ERROR_INVALID_PARAMETER, "key_path is NULL"); vine_security_s *s = (vine_security_s *)security; + if (s->key_path) + free(s->key_path); s->key_path = STRDUP(key_path); return VINE_ERROR_NONE; @@ -237,6 +243,8 @@ int _vine_security_set_psk(vine_security_h security, const char *psk) RET_VAL_IF(psk == NULL, VINE_ERROR_INVALID_PARAMETER, "psk is NULL"); vine_security_s *s = (vine_security_s *)security; + if (s->psk) + free(s->psk); s->psk = STRDUP(psk); return VINE_ERROR_NONE; -- 2.7.4 From 7a370008d185d60a122608de7693fee731a75799 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=AC=B8=EC=84=A0=EC=95=84/Tizen=20Platform=20Lab=28SR=29/?= =?utf8?q?Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Mon, 12 Apr 2021 12:48:29 +0900 Subject: [PATCH 14/16] Some changes for PSK (#228) * Use calloc/free for auth frame * Limit PSK length to 512 Change-Id: I17afa6dc51cffb463f8ef9e44e0725b8ff4dc06e --- include/vine.h | 2 +- src/vine-auth.cpp | 4 ++-- src/vine-security.cpp | 4 ++++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/include/vine.h b/include/vine.h index 068e248..aaf2e6b 100755 --- a/include/vine.h +++ b/include/vine.h @@ -1319,7 +1319,7 @@ int vine_security_get_private_key(vine_security_h security, char **key_path); * @remarks The @psk is exchanged over a websocket connection. * @since_tizen 6.5 * @param[in] security The security handle. - * @param[in] psk The preshared key + * @param[in] psk The preshared key. This is limited to the maximum 512 characters. * @return 0 on success, otherwise a negative error value * @retval #VINE_ERROR_NONE Successful * @retval #VINE_ERROR_INVALID_PARAMTER Invalid parameter diff --git a/src/vine-auth.cpp b/src/vine-auth.cpp index 10b8b4b..049d1a3 100644 --- a/src/vine-auth.cpp +++ b/src/vine-auth.cpp @@ -25,7 +25,7 @@ unsigned char *alloc_auth_frame(unsigned char ver, int data_len, const unsigned char *data, int *size) { *size = 5 + data_len; - unsigned char *buf = new unsigned char[*size]; + unsigned char *buf = (unsigned char *)calloc(*size, sizeof(unsigned char)); if (buf == NULL) { VINE_LOGE("Out of memory"); return NULL; @@ -43,7 +43,7 @@ unsigned char *alloc_auth_frame(unsigned char ver, void release_auth_frame(unsigned char *buf) { - delete [] buf; + free(buf); } bool parse_auth_frame(unsigned char *buf, size_t buf_size, diff --git a/src/vine-security.cpp b/src/vine-security.cpp index 57e9f80..2eaccd7 100755 --- a/src/vine-security.cpp +++ b/src/vine-security.cpp @@ -20,6 +20,8 @@ #include "vine-log.h" #include "vine-utils.h" +#define VINE_SECURITY_PSK_MAX_LEN 512 + typedef struct { vine_security_type_e type; int flags; @@ -241,6 +243,8 @@ int _vine_security_set_psk(vine_security_h security, const char *psk) { RET_VAL_IF(security == NULL, VINE_ERROR_INVALID_PARAMETER, "security is NULL"); RET_VAL_IF(psk == NULL, VINE_ERROR_INVALID_PARAMETER, "psk is NULL"); + RET_VAL_IF(strlen(psk) > VINE_SECURITY_PSK_MAX_LEN, + VINE_ERROR_INVALID_PARAMETER, "psk should be less than or equal to 512"); vine_security_s *s = (vine_security_s *)security; if (s->psk) -- 2.7.4 From db73cbcab3df766ff134ffa4dcaff3fe689cfdaa Mon Sep 17 00:00:00 2001 From: Cheoleun Moon Date: Tue, 13 Apr 2021 13:41:03 +0900 Subject: [PATCH 15/16] Create disc_handle for each ip_resolving operation Change-Id: I5bb1995d2be217b5f2aa4c291387c2a5bdd29240 Signed-off-by: Cheoleun Moon --- src/vine-disc.cpp | 29 ++++++++++++++++++++++++++--- src/vine-session.cpp | 10 +++++----- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/vine-disc.cpp b/src/vine-disc.cpp index 53d4d4a..31d3f4a 100755 --- a/src/vine-disc.cpp +++ b/src/vine-disc.cpp @@ -356,6 +356,10 @@ int vine_disc_create(vine_discovery_method_e disc_method, vine_disc_h *disc) void vine_disc_destroy(vine_disc_h disc) { + vine_disc_s *disc_handle = (vine_disc_s *)disc; + if (disc_handle && disc_handle->plugin_fn && + disc_handle->plugin_fn->deinit && disc_handle->plugin_handle) + disc_handle->plugin_fn->deinit(disc_handle->plugin_handle); free(disc); } @@ -392,6 +396,7 @@ vine_error_e __vine_disc_plugin_publish(vine_disc_h disc, vine_service_h service vine_disc_s *disc_handle = (vine_disc_s *)disc; void *plugin_handle = NULL; + RET_VAL_IF(disc_handle->plugin_fn == NULL, VINE_ERROR_INVALID_OPERATION, "plugin_fn is NULL"); if (disc_handle->plugin_fn->init == NULL) { VINE_LOGE("No init() defined"); return VINE_ERROR_OPERATION_FAILED; @@ -407,6 +412,8 @@ vine_error_e __vine_disc_plugin_publish(vine_disc_h disc, vine_service_h service if (disc_handle->plugin_fn->publish == NULL) { VINE_LOGE("No publish() defined"); + disc_handle->plugin_fn->deinit(plugin_handle); + disc_handle->plugin_handle = NULL; return VINE_ERROR_OPERATION_FAILED; } @@ -417,6 +424,7 @@ vine_error_e __vine_disc_plugin_publish(vine_disc_h disc, vine_service_h service if (error != VINE_DISC_ERROR_NONE) { VINE_LOGE("Fail to publish %d", error); disc_handle->plugin_fn->deinit(plugin_handle); + disc_handle->plugin_handle = NULL; return __convert_disc_error_to_vine_error(error); } @@ -454,6 +462,7 @@ vine_error_e __vine_disc_plugin_stop_publish(vine_disc_h disc) vine_disc_error error = VINE_DISC_ERROR_NONE; vine_disc_s *disc_handle = (vine_disc_s *)disc; + RET_VAL_IF(disc_handle->plugin_fn == NULL, VINE_ERROR_INVALID_OPERATION, "plugin_fn is NULL"); if (disc_handle->plugin_fn->init == NULL) { VINE_LOGE("No init() defined"); return VINE_ERROR_OPERATION_FAILED; @@ -495,6 +504,7 @@ vine_error_e __vine_disc_plugin_subscribe(vine_disc_h disc, vine_disc_s *disc_handle = (vine_disc_s *)disc; void *plugin_handle = NULL; + RET_VAL_IF(disc_handle->plugin_fn == NULL, VINE_ERROR_INVALID_OPERATION, "plugin_fn is NULL"); if (disc_handle->plugin_fn->init == NULL) { VINE_LOGE("No init() defined"); return VINE_ERROR_OPERATION_FAILED; @@ -509,6 +519,8 @@ vine_error_e __vine_disc_plugin_subscribe(vine_disc_h disc, if (disc_handle->plugin_fn->subscribe == NULL) { VINE_LOGE("No subscribe() defined"); + disc_handle->plugin_fn->deinit(plugin_handle); + disc_handle->plugin_handle = NULL; return VINE_ERROR_OPERATION_FAILED; } error = disc_handle->plugin_fn->subscribe(plugin_handle, service_type, iface_name); @@ -553,6 +565,7 @@ vine_error_e __vine_disc_plugin_stop_subscribe(vine_disc_h disc) vine_disc_error error = VINE_DISC_ERROR_NONE; vine_disc_s *disc_handle = (vine_disc_s *)disc; + RET_VAL_IF(disc_handle->plugin_fn == NULL, VINE_ERROR_INVALID_OPERATION, "plugin_fn is NULL"); if (disc_handle->plugin_fn->init == NULL) { VINE_LOGE("No init() defined"); return VINE_ERROR_OPERATION_FAILED; @@ -591,19 +604,28 @@ vine_error_e __vine_disc_plugin_resolve_ip(vine_disc_h disc, vine_service_h serv VINE_LOGD("service[%p], disc handle[%p]", service, disc); vine_disc_s *disc_handle = (vine_disc_s *)disc; - void *plugin_handle = disc_handle->plugin_handle; - if (disc_handle->plugin_fn == NULL || disc_handle->plugin_fn->init == NULL) { + void *plugin_handle = NULL; + + RET_VAL_IF(disc_handle->plugin_fn == NULL, VINE_ERROR_INVALID_OPERATION, "plugin_fn is NULL"); + if (disc_handle->plugin_fn->init == NULL) { VINE_LOGE("No init() defined"); return VINE_ERROR_OPERATION_FAILED; } + vine_disc_error error = disc_handle->plugin_fn->init(&plugin_handle, disc); + RET_VAL_IF(error != VINE_DISC_ERROR_NONE, + __convert_disc_error_to_vine_error(error), + "Fail to init %d", error); + disc_handle->plugin_handle = plugin_handle; VINE_LOGD("plugin handle[%p]", plugin_handle); if (disc_handle->plugin_fn->resolve_ip == NULL) { VINE_LOGE("No resolve_ip() defined"); + disc_handle->plugin_fn->deinit(plugin_handle); + disc_handle->plugin_handle = NULL; return VINE_ERROR_OPERATION_FAILED; } - vine_disc_error error = disc_handle->plugin_fn->resolve_ip(plugin_handle, + error = disc_handle->plugin_fn->resolve_ip(plugin_handle, _vine_service_get_type(service), _vine_service_get_name(service), _vine_service_get_host_name(service), _vine_service_get_iface_name(service)); if (error != VINE_DISC_ERROR_NONE) { @@ -650,6 +672,7 @@ vine_error_e __vine_disc_plugin_cancel_resolve_ip(vine_disc_h disc, vine_service void *plugin_handle = disc_handle->plugin_handle; VINE_LOGD("plugin handle[%p]", plugin_handle); + RET_VAL_IF(disc_handle->plugin_fn == NULL, VINE_ERROR_INVALID_OPERATION, "plugin_fn is NULL"); if (disc_handle->plugin_fn->cancel_resolve_ip == NULL) { VINE_LOGE("No resolve_ip() defined"); return VINE_ERROR_OPERATION_FAILED; diff --git a/src/vine-session.cpp b/src/vine-session.cpp index 20528bd..5c48b5c 100755 --- a/src/vine-session.cpp +++ b/src/vine-session.cpp @@ -293,9 +293,6 @@ static void __discovered_cb(vine_disc_h disc, bool available, ret = _vine_service_create(&discovered_service, false); RET_IF(ret != VINE_ERROR_NONE, "Fail to create a service"); - ret = _vine_service_set_disc_handle(discovered_service, disc); - RET_IF(ret != VINE_ERROR_NONE, "Fail to set disc_handle"); - ret = __vine_set_discovered_service(discovered_service, service_type, service_name, host_name, port, attr, iface_name); if (ret != VINE_ERROR_NONE) { @@ -396,8 +393,11 @@ int _vine_session_set_ip_resolved_cb(vine_session_h session, vine_session_s *s = (vine_session_s *)session; vine_disc_h disc_handle; - ret = _vine_service_get_disc_handle(service, VINE_DISCOVERY_METHOD_DNS_SD, &disc_handle); - RET_VAL_IF(ret != VINE_ERROR_NONE, ret, "Fail to _vine_service_get_disc_handle"); + ret = vine_disc_create(s->disc_method, &disc_handle); + RET_VAL_IF(ret != VINE_ERROR_NONE, ret, "Fail to vine_disc_create"); + + ret = _vine_service_set_disc_handle(service, disc_handle); + RET_VAL_IF(ret != VINE_ERROR_NONE, ret, "Fail to set disc_handle"); _vine_service_set_ip_resolved_cb(service, callback, user_data); -- 2.7.4 From 46d72079745a1aaa545fdbd65098567149d767db Mon Sep 17 00:00:00 2001 From: Cheoleun Moon Date: Tue, 13 Apr 2021 13:43:21 +0900 Subject: [PATCH 16/16] Check return value of lws_get_socket_fd Change-Id: I86ae9d5f0c314af46a0f47832915b0aec325e239 Signed-off-by: Cheoleun Moon --- .gitignore | 20 ++++++++++++++++++++ plugins/libwebsockets/libwebsockets-plugin.cpp | 7 ++++++- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..88983ec --- /dev/null +++ b/.gitignore @@ -0,0 +1,20 @@ +CMakeLists.txt.user +CMakeCache.txt +CMakeFiles +CMakeScripts +Testing +Makefile +cmake_install.cmake +install_manifest.txt +compile_commands.json +CTestTestfile.cmake +_deps +vine.pc +libvine.so* +vine-unittest +third-party/libwebsockets/include/lws_config.h +build/ +.vscode/ +coding-style-guides/ +build_doc/ +Debug/ diff --git a/plugins/libwebsockets/libwebsockets-plugin.cpp b/plugins/libwebsockets/libwebsockets-plugin.cpp index e221833..fd36b20 100755 --- a/plugins/libwebsockets/libwebsockets-plugin.cpp +++ b/plugins/libwebsockets/libwebsockets-plugin.cpp @@ -121,9 +121,14 @@ static void _get_peer_network_info(struct lws *wsi, char ip[], int *port) int ret; fd = lws_get_socket_fd(wsi); + if (fd < 0) { + VINE_LOGE("lws_get_socket_fd fails[%d]", ret); + return; + } + ret = getpeername(fd, (struct sockaddr*)&addr, &len); if (ret < 0) { - VINE_LOGE("Cannot get name of connected peer. errno[%d]", ret); + VINE_LOGE("Cannot get name of connected peer. errno[%d]", errno); return; } -- 2.7.4