From efca060a1ec95eb05e55b14dff83d833b570eea2 Mon Sep 17 00:00:00 2001 From: Cheoleun Moon Date: Thu, 8 Apr 2021 16:56:02 +0900 Subject: [PATCH 01/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 02/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 03/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 04/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 05/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 06/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 07/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 08/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 09/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 10/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 From a79afa77993302cf59e5056222a15e61b949afe8 Mon Sep 17 00:00:00 2001 From: Cheoleun Moon Date: Tue, 13 Apr 2021 14:28:08 +0900 Subject: [PATCH 11/16] Fix incorrect variable name Change-Id: Id5ffbaef058f40089f0bbec9737488effa1dd18c Signed-off-by: Cheoleun Moon --- plugins/libwebsockets/libwebsockets-plugin.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/libwebsockets/libwebsockets-plugin.cpp b/plugins/libwebsockets/libwebsockets-plugin.cpp index fd36b20..52894ef 100755 --- a/plugins/libwebsockets/libwebsockets-plugin.cpp +++ b/plugins/libwebsockets/libwebsockets-plugin.cpp @@ -122,7 +122,7 @@ static void _get_peer_network_info(struct lws *wsi, char ip[], int *port) fd = lws_get_socket_fd(wsi); if (fd < 0) { - VINE_LOGE("lws_get_socket_fd fails[%d]", ret); + VINE_LOGE("lws_get_socket_fd fails[%d]", fd); return; } -- 2.7.4 From 3c0dc92f56011cf75706724a2aaad5f036458171 Mon Sep 17 00:00:00 2001 From: Cheoleun Moon Date: Tue, 13 Apr 2021 16:45:26 +0900 Subject: [PATCH 12/16] Clone the found service Change-Id: If043f6eaa55c05cfeb821fd8d69d737363f1df14 Signed-off-by: Cheoleun Moon --- tests/vine-test/vine-test.cpp | 4 +++- tool/tool_run.cpp | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/vine-test/vine-test.cpp b/tests/vine-test/vine-test.cpp index 00a8cf6..4640565 100644 --- a/tests/vine-test/vine-test.cpp +++ b/tests/vine-test/vine-test.cpp @@ -251,7 +251,9 @@ static void __discovered_cb(vine_session_h session, vine_service_h service, printf("\n"); fflush(stdout); - g_service_list.push_back(service); + vine_service_h s; + vine_service_clone(service, &s); + g_service_list.push_back(s); free(service_type); free(service_name); } diff --git a/tool/tool_run.cpp b/tool/tool_run.cpp index c039d6f..343d7ea 100644 --- a/tool/tool_run.cpp +++ b/tool/tool_run.cpp @@ -152,7 +152,9 @@ static void __discovered_cb(vine_session_h session, vine_service_h service, vine_service_state_e state, void *user_data) { printf("available[%d]\n", state); - vine_session_set_ip_resolved_cb(session, service, __ip_resolved_cb, NULL); + vine_service_h s; + vine_service_clone(service, &s); + vine_session_set_ip_resolved_cb(session, s, __ip_resolved_cb, NULL); } static vine_dp_type_e _convert_dp_type(dp_type_t type) -- 2.7.4 From 565edf4d48ccae8519342a4deb5eb43222210d5f 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: Tue, 13 Apr 2021 18:28:15 +0900 Subject: [PATCH 13/16] Handle a duplicated service name (#232) * Handle a duplicated service name * Consider null character appended automatically Change-Id: If6cf46c6fe19179f7d98e09234911294fe0f0918 --- include/vine.h | 7 +++++++ plugins/dns-sd/dns-sd-plugin.cpp | 5 ++++- src/include/vine-disc-plugin.h | 1 + src/include/vine-dp.h | 1 + src/vine-disc.cpp | 2 ++ src/vine-dp.cpp | 5 +++++ tool/tool_run.cpp | 9 +++++++-- 7 files changed, 27 insertions(+), 3 deletions(-) diff --git a/include/vine.h b/include/vine.h index aaf2e6b..9ee28af 100755 --- a/include/vine.h +++ b/include/vine.h @@ -109,6 +109,10 @@ typedef enum { */ VINE_ERROR_NO_READ_DATA = VINE_ERROR|0x06, /** + * Service deregistered by a name conflict or similar problem + */ + VINE_ERROR_SERVICE_DEREGISTERED = VINE_ERROR|0x07, + /** * Rejected by peer */ //VINE_ERROR_REJECTED_BY_PEER = VINE_ERROR|0x05, @@ -374,6 +378,9 @@ int vine_session_destroy(vine_session_h session); /** * @brief Called when service is registered. + * @remarks If @a error is VINE_ERROR_SERVICE_DEREGISTERED, It means that + * @a service_name is conflicted. The callback will be invoked again + * with renamed @a service_name. * @since_tizen 6.5 * @param[in] session The session handle * @param[in] service_name The registered service name diff --git a/plugins/dns-sd/dns-sd-plugin.cpp b/plugins/dns-sd/dns-sd-plugin.cpp index b6f744d..94ed386 100755 --- a/plugins/dns-sd/dns-sd-plugin.cpp +++ b/plugins/dns-sd/dns-sd-plugin.cpp @@ -248,10 +248,13 @@ static void __register_reply(DNSServiceRef sdRef, DNSServiceFlags flags, DNSServiceErrorType errorCode, const char *name, const char *regtype, const char *domain, void *context) { - VINE_LOGD("Service Registerd. service name[%s] error[%d]", name, errorCode); + VINE_LOGD("Service Registerd. service name[%s] error[%d] flags[%d]", name, errorCode, flags); vine_dns_sd_s *dns_sd_handle = (vine_dns_sd_s *)context; vine_disc_error error = __convert_dns_sd_error_to_vine_disc_error(errorCode); + if (flags == 0) + error = VINE_DISC_ERROR_SERVICE_DEREGISTERED; + if (event_callbacks.published_cb) event_callbacks.published_cb(dns_sd_handle, name, error, dns_sd_handle->user_data); diff --git a/src/include/vine-disc-plugin.h b/src/include/vine-disc-plugin.h index 5b41bfc..3dcbd20 100755 --- a/src/include/vine-disc-plugin.h +++ b/src/include/vine-disc-plugin.h @@ -32,6 +32,7 @@ typedef enum { VINE_DISC_ERROR_OUT_OF_MEMORY, VINE_DISC_ERROR_NAME_CONFLICT, VINE_DISC_ERROR_SERVICE_NOT_RUNNING, + VINE_DISC_ERROR_SERVICE_DEREGISTERED, } vine_disc_error; typedef enum { diff --git a/src/include/vine-dp.h b/src/include/vine-dp.h index cec5ef8..f47b354 100644 --- a/src/include/vine-dp.h +++ b/src/include/vine-dp.h @@ -152,6 +152,7 @@ enum { VINE_DP_PUBSUB_SD_STATE_SUBSCRIBE = 1 << 2, }; +#define VINE_DP_PUBSUB_SERVICE_NAME_PREFIX "vine-pubsub" #define VINE_DP_PUBSUB_RANK_KEY "rank" #define VINE_DP_PUBSUB_RANK_MAX 53123 #define VINE_DP_PUBSUB_RANK_LEN 6 diff --git a/src/vine-disc.cpp b/src/vine-disc.cpp index 31d3f4a..330bcaf 100755 --- a/src/vine-disc.cpp +++ b/src/vine-disc.cpp @@ -91,6 +91,8 @@ vine_error_e __convert_disc_error_to_vine_error(vine_disc_error error) case VINE_DISC_ERROR_NAME_CONFLICT: // DNS-SD changed the service name return VINE_ERROR_NAME_CONFLICT; // TODO: To be determined if which vine error will be returned + case VINE_DISC_ERROR_SERVICE_DEREGISTERED: + return VINE_ERROR_SERVICE_DEREGISTERED; case VINE_DISC_ERROR_SERVICE_NOT_RUNNING: default: return VINE_ERROR_OPERATION_FAILED; diff --git a/src/vine-dp.cpp b/src/vine-dp.cpp index 17007e8..7fcf898 100644 --- a/src/vine-dp.cpp +++ b/src/vine-dp.cpp @@ -721,6 +721,7 @@ int DPPubSub::publish_service() vine_service_h service; int ret; char rank_str[VINE_DP_PUBSUB_RANK_LEN] = {0 , }; + char service_name[VINE_MAX_SERVICE_NAME_LEN + 1] = {0 , }; ret = vine_service_create(&service); if (ret != VINE_ERROR_NONE) @@ -731,6 +732,10 @@ int DPPubSub::publish_service() mRank = create_rank(); sprintf(rank_str, "%d", mRank); + snprintf(service_name, VINE_MAX_SERVICE_NAME_LEN, + "%s-%d", VINE_DP_PUBSUB_SERVICE_NAME_PREFIX, mRank); + + vine_service_set_name(service, service_name); vine_service_add_attribute(service, VINE_DP_PUBSUB_RANK_KEY, (const char *)rank_str); if (mSdPub == NULL) { diff --git a/tool/tool_run.cpp b/tool/tool_run.cpp index c039d6f..1cecbb5 100644 --- a/tool/tool_run.cpp +++ b/tool/tool_run.cpp @@ -114,8 +114,13 @@ static void __accepted_cb(vine_dp_h dp, vine_dp_h accepted_dp, void *user_data) static void __registered_cb(vine_session_h session, const char *service_name, vine_error_e error, void *user_data) { - printf("Service[%s] %s.\n", service_name, - error == VINE_ERROR_NONE ? "registered" : "registration failure"); + printf("Service[%s] ", service_name); + if (error == VINE_ERROR_NONE) + printf("registered. \n"); + else if (error == VINE_ERROR_SERVICE_DEREGISTERED) + printf("deregistered by a name conflict. Service name will be renamed.\n"); + else + printf("registration failure.\n"); } static bool __print_attr(const char *key, const char *value, void *user_data) -- 2.7.4 From 23739f41808c2e5f3fecf6851ae4d30cadd321d7 Mon Sep 17 00:00:00 2001 From: Cheoleun Moon Date: Tue, 13 Apr 2021 20:17:22 +0900 Subject: [PATCH 14/16] Do not use memcpy for vine_service_s Change-Id: If464cbac64927bce1347a540fbd46d48071072c2 Signed-off-by: Cheoleun Moon --- src/vine-service.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/vine-service.cpp b/src/vine-service.cpp index f0471b0..4419550 100755 --- a/src/vine-service.cpp +++ b/src/vine-service.cpp @@ -100,8 +100,20 @@ int _vine_service_clone(vine_service_h origin, vine_service_h *cloned) vine_service_s *origin_service = (vine_service_s *)origin; vine_service_s *cloned_service = new vine_service_s; - memcpy(cloned_service, origin_service, sizeof(vine_service_s)); + cloned_service->type = origin_service->type; + strncpy(cloned_service->service_type, origin_service->service_type, VINE_MAX_SERVICE_TYPE_LEN); + strncpy(cloned_service->service_name, origin_service->service_name, VINE_MAX_SERVICE_NAME_LEN); + strncpy(cloned_service->host_name, origin_service->host_name, VINE_MAX_HOST_NAME_LEN); + strncpy(cloned_service->ip, origin_service->ip, VINE_MAX_IP_LEN); + cloned_service->family = origin_service->family; + cloned_service->port = origin_service->port; cloned_service->attributes = origin_service->attributes; + strncpy(cloned_service->iface_name, origin_service->iface_name, IF_NAMESIZE); + cloned_service->state = origin_service->state; + + cloned_service->disc_handle = NULL; + cloned_service->ip_resolved_cb = NULL; + cloned_service->ip_resolved_cb_data = NULL; *cloned = cloned_service; VINE_LOGD("Cloned service[%p]", cloned_service); -- 2.7.4 From 6c000154b040b0c613e35e4362f815cbb268b71f Mon Sep 17 00:00:00 2001 From: Seonah Moon Date: Wed, 14 Apr 2021 15:54:35 +0900 Subject: [PATCH 15/16] pubsub: create a disc handle for ip_resolve() Change-Id: Ie693e2fea547fafc202ec9c5825ceff9ce4b265b --- src/vine-dp.cpp | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/vine-dp.cpp b/src/vine-dp.cpp index 7fcf898..8b5ed35 100644 --- a/src/vine-dp.cpp +++ b/src/vine-dp.cpp @@ -290,25 +290,39 @@ static void _service_discovered_cb(vine_disc_h disc, bool available, if (!user_data || !available) return; + vine_disc_h disc_handle; + int ret = vine_disc_create(VINE_DISCOVERY_METHOD_DNS_SD, &disc_handle); + RET_IF(ret != VINE_ERROR_NONE, "Fail to create a disc"); + vine_service_h service; - int ret = _vine_service_create(&service, false); - RET_IF(ret != VINE_ERROR_NONE, "Fail to create a service"); + ret = _vine_service_create(&service, false); + if (ret != VINE_ERROR_NONE) { + vine_disc_destroy(disc_handle); + return; + } auto it = attr.find(VINE_DP_PUBSUB_RANK_KEY); if (it == attr.end()) { VINE_LOGE("peer doens't have a rank."); _vine_service_destroy(service); + vine_disc_destroy(disc_handle); return; } - ret = _vine_service_set_disc_handle(service, disc); - RET_IF(ret != VINE_ERROR_NONE, "Fail to set disc_handle"); + ret = _vine_service_set_disc_handle(service, disc_handle); + if (ret != VINE_ERROR_NONE) { + VINE_LOGE("Fail to set disc_handle. error(%d)", ret); + _vine_service_destroy(service); + vine_disc_destroy(disc_handle); + return; + } ret = __vine_set_discovered_service(service, service_type, service_name, host_name, port, attr, iface_name); if (ret != VINE_ERROR_NONE) { VINE_LOGE("Fail to set a service. error(%d)", ret); _vine_service_destroy(service); + vine_disc_destroy(disc_handle); return; } @@ -318,10 +332,15 @@ static void _service_discovered_cb(vine_disc_h disc, bool available, dp->increase_init_disc_num(); } - ret = vine_disc_resolve_ip(disc, service, + ret = vine_disc_resolve_ip(disc_handle, service, _ip_resolved_cb, user_data, (vine_event_queue_h)static_cast(user_data)->get_eventfd()); - RET_IF(ret != VINE_ERROR_NONE, "Fail to vine_disc_resolve_ip"); + if (ret != VINE_ERROR_NONE) { + VINE_LOGE("Fail to resolve IP. error(%d)", ret); + _vine_service_destroy(service); + vine_disc_destroy(disc_handle); + return; + } } static void _service_published_cb(vine_disc_h disc, @@ -984,7 +1003,7 @@ int DPPubSub::compare_ip_priority(const char *peer_ip) break; } - VINE_LOGD("peer_ip[%s] found_ip[%s]", peer_ip, found_ip.c_str()); + VINE_LOGD("peer_ip[%s] found_ip[%s] subnet[%s]", peer_ip, found_ip.c_str(), subnet.c_str()); try { last = std::stoi(found_ip.substr(pos + 1)); peer_last = std::stoi(peer_ip_str.substr(pos + 1)); @@ -1046,6 +1065,7 @@ void DPPubSub::_open_timer(void *user_data) static_cast(user_data)->invoke_opened_cb(VINE_ERROR_NONE); return; } + VINE_LOGD("invoke opened_cb with an error. sd_state[%d]", sd_state); static_cast(user_data)->invoke_opened_cb(VINE_ERROR_OPERATION_FAILED); } -- 2.7.4 From b2aa1527936f3ffdd760325bde0ad514e09de25f Mon Sep 17 00:00:00 2001 From: Seonah Moon Date: Wed, 14 Apr 2021 16:31:32 +0900 Subject: [PATCH 16/16] unittest: remove unused wrapper function Change-Id: I69af0fd7e4c5bc325ebe3d3cecff06f066361b41 --- tests/unittest/CMakeLists.txt | 1 - tests/unittest/mocks/vine-mock-dl.cpp | 5 ----- 2 files changed, 6 deletions(-) diff --git a/tests/unittest/CMakeLists.txt b/tests/unittest/CMakeLists.txt index facb8bc..6bb39db 100755 --- a/tests/unittest/CMakeLists.txt +++ b/tests/unittest/CMakeLists.txt @@ -54,7 +54,6 @@ SET_TARGET_PROPERTIES(${VINE_UNITTEST} PROPERTIES --wrap=malloc,\ --wrap=strdup,\ --wrap=dlopen,\ ---wrap=dlclose,\ --wrap=dlsym") INSTALL(TARGETS ${VINE_UNITTEST} DESTINATION "${BIN_DIR}") diff --git a/tests/unittest/mocks/vine-mock-dl.cpp b/tests/unittest/mocks/vine-mock-dl.cpp index 5b425ef..e040ef9 100644 --- a/tests/unittest/mocks/vine-mock-dl.cpp +++ b/tests/unittest/mocks/vine-mock-dl.cpp @@ -52,11 +52,6 @@ void *__wrap_dlopen(const char *filename, int flags) return __vine_mock_dlopen ? __real_dlopen(filename, flags) : NULL; } -int __wrap_dlclose(void *handle) -{ - return 0; -} - void *__wrap_dlsym(void *handle, const char *symbol) { if (strcmp(symbol, DISC_PLUGIN_INIT) == 0) { -- 2.7.4