Add internal APIs to create/destroy remote service 77/262577/4 accepted/tizen/unified/20210819.123206 submit/tizen/20210817.044230 submit/tizen/20210818.091254
authorSeonah Moon <seonah1.moon@samsung.com>
Fri, 13 Aug 2021 09:09:19 +0000 (18:09 +0900)
committerSeonah Moon <seonah1.moon@samsung.com>
Fri, 13 Aug 2021 09:30:02 +0000 (18:30 +0900)
Add new APIs for handling a remove service to make browse() and resolve() operate independently.

- Added APIs:
dnssd_create_remote_service()
dnssd_destroy_remote_service()

- how to test
1. Register service
 $ dns-sd -R test _http._tcp local 12345
2. Resolve service using dns-sd-internal-test
 $ dns-sd-internal-test
 1(_http._tcp, test, wlan0) > 5

Change-Id: I7aa95913d6be359db65d4948a67b49711e3e44e2

include/dns-sd-internal.h
packaging/capi-network-nsd.spec
src/dns-sd/dns-sd.c
tests/dns-sd-internal-test.c

index 78185b9c88f8df345b51c7520d946d6e417154a9..aac2c90b849b7805203b310eb3c5b685f20d6742 100644 (file)
@@ -186,6 +186,49 @@ int dnssd_resolve_service(dnssd_service_h service,
  */
 int dnssd_cancel_resolve_service(dnssd_service_h service);
 
+
+/**
+ * @brief Creates a DNSSD remote service handle.
+ * @details @a dnssd_service is used for dnssd_resolve_service() only.
+ * @since_tizen 6.5
+ * @remarks You must release @a dnssd_service using dnssd_destroy_remote_service().
+ * @param[in] service_type The DNSSD service type. It is expressed as type
+ *            followed by protocol, separated by a dot(e.g. "_ftp._tcp"). It
+ *            must begin with an underscore, followed by 1-15 characters which
+ *            may be letters, digits, or hyphens. The transport protocol must be
+ *            "_tcp" or "_udp". New service types should be registered at
+ *            http://www.dns-sd.org/ServiceTypes.html.
+ * @param[in] service_name The name of DNSSD remote service
+ * @param[in] interface The interface name
+ * @param[out] dnssd_service The DNSSD remote handle
+ * @return @c 0 on success,
+ *         otherwise negative error value
+ * @retval #DNSSD_ERROR_NONE Successful
+ * @retval #DNSSD_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #DNSSD_ERROR_OUT_OF_MEMORY Out of memory
+ * @retval #DNSSD_ERROR_NOT_SUPPORTED Not Supported
+ * @retval #DNSSD_ERROR_NOT_INITIALIZED Not Initialized
+ * @see dnssd_destroy_remote_service()
+ * @pre This API needs dnssd_initialize() before use
+ */
+int dnssd_create_remote_service(const char *service_type, const char *service_name, const char *interfac, dnssd_service_h *dnssd_service);
+
+/**
+ * @brief Destroys the DNSSD remote service handle.
+ * @since_tizen 6.5
+ * @remarks You must destroy only remote services created using dnssd_create_remote_service().
+ * @param[in] dnssd_service The DNSSD remote service handle
+ * @return @c 0 on success,
+ *         otherwise negative error value
+ * @retval #DNSSD_ERROR_NONE Successful
+ * @retval #DNSSD_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #DNSSD_ERROR_NOT_SUPPORTED Not Supported
+ * @retval #DNSSD_ERROR_NOT_INITIALIZED Not Initialized
+ * @see dnssd_create_remote_service()
+ * @pre This API needs dnssd_create_remote_service() before use.
+ */
+int dnssd_destroy_remote_service(dnssd_service_h dnssd_service);
+
 #ifdef __cplusplus
 }
 #endif
index 45a1511530f90202b65ecb54e8ef8d853908e0a0..9c5fe33662e841ca6b68f8fbb31fb7d1448e0d36 100644 (file)
@@ -1,6 +1,6 @@
 Name:       capi-network-nsd
 Summary:    A Network Service Discovery libraries in Native API
-Version:    0.1.5
+Version:    0.1.6
 Release:    1
 Group:      System/Network
 License:    Apache-2.0
index c63ddfaefe49270edaa2b3585eb8bd3fd944beed..74f7656ef1235de7b27d1e39c49558912866e077 100644 (file)
@@ -1188,14 +1188,8 @@ static void __dnssd_getaddrinfo_reply_cb(DNSServiceRef sd_ref,
                        local_handle->flags);
 
        dnssd_handle = __dnssd_get_struct_from_handle(found->browse_handler);
-       if (dnssd_handle == NULL) {
-               DNSSD_LOGD("Invalid browse handle");
-               DNSSD_UNLOCK;
-               __DNSSD_LOG_FUNC_EXIT__;
-               return;
-       }
-       DNSSD_LOGD("GetAddrInfo Callback set for [%p]",
-                       dnssd_handle);
+       if (dnssd_handle)
+               DNSSD_LOGD("GetAddrInfo Callback set for [%p]", dnssd_handle);
 
        __update_found_address(address, found);
 
@@ -2047,6 +2041,123 @@ EXPORT_API int dnssd_service_get_all_txt_record(dnssd_service_h dnssd_service,
                return res;
        }
 
+       DNSSD_UNLOCK;
+       __DNSSD_LOG_FUNC_EXIT__;
+
+       return DNSSD_ERROR_NONE;
+}
+
+EXPORT_API int dnssd_create_remote_service(const char *service_type,
+               const char *service_name, const char *interface,
+               dnssd_service_h *dnssd_service)
+{
+       __DNSSD_LOG_FUNC_ENTER__;
+       dnssd_handle_s *remote_handle;
+       dnssd_found_data_s *found = NULL;
+       unsigned int handler;
+
+       CHECK_FEATURE_SUPPORTED(NETWORK_SERVICE_DISCOVERY_FEATURE);
+       DNSSD_LOCK;
+
+       CHECK_INITIALIZED();
+
+       if (dnssd_service == NULL || service_type == NULL
+                       || service_name == NULL
+                       || __dnssd_get_struct_from_handle(*dnssd_service) != NULL) {
+               DNSSD_LOGE("Invalid Parameter");
+               DNSSD_UNLOCK;
+               __DNSSD_LOG_FUNC_EXIT__;
+               return DNSSD_ERROR_INVALID_PARAMETER;
+       }
+
+       remote_handle = (dnssd_handle_s *)g_try_malloc0(FOUND_SIZE);
+       if (remote_handle == NULL) {
+               DNSSD_LOGE("Failed to Allocate Memory"); //LCOV_EXCL_LINE
+               DNSSD_UNLOCK;
+               __DNSSD_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
+               return DNSSD_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
+       }
+
+       handler = (uintptr_t)remote_handle & 0xffffffff;
+       *dnssd_service = handler;
+       remote_handle->service_handler = handler;
+       remote_handle->op_type = DNSSD_TYPE_FOUND_NOT_RESOLVED;
+       remote_handle->service_type = g_strdup(service_type);
+       g_strlcpy(remote_handle->domain, "local", sizeof(remote_handle->domain));
+       remote_handle->flags = 0;
+       remote_handle->watch_id = 0;
+
+       found = GET_FOUND_DATA_P(remote_handle);
+       if (interface) {
+               unsigned int if_index;
+               if ((if_index = if_nametoindex(interface)) == 0) {
+                       DNSSD_LOGE("Invalid interface name");
+                       g_free(remote_handle->service_type);
+                       g_free(remote_handle);
+                       DNSSD_UNLOCK;
+                       __DNSSD_LOG_FUNC_EXIT__;
+                       return DNSSD_ERROR_INVALID_PARAMETER;
+               }
+               found->if_index = if_index;
+       } else {
+               found->if_index = kDNSServiceInterfaceIndexAny;
+       }
+       found->service_name = g_strdup(service_name);
+
+       DNSSD_LOGD("New handle created [%p]->[%u] type %s", remote_handle,
+                       *dnssd_service, remote_handle->service_type);
+
+       dnssd_handle_list = g_slist_prepend(dnssd_handle_list, remote_handle);
+
+       DNSSD_UNLOCK;
+       __DNSSD_LOG_FUNC_EXIT__;
+       return DNSSD_ERROR_NONE;
+}
+
+EXPORT_API int dnssd_destroy_remote_service(dnssd_service_h dnssd_service)
+{
+       __DNSSD_LOG_FUNC_ENTER__;
+       dnssd_handle_s *remote_handle = NULL;
+
+       CHECK_FEATURE_SUPPORTED(NETWORK_SERVICE_DISCOVERY_FEATURE);
+       DNSSD_LOCK;
+
+       CHECK_INITIALIZED();
+
+       remote_handle = __dnssd_get_struct_from_handle(dnssd_service);
+       if (remote_handle == NULL) {
+               DNSSD_LOGD("Service Handler not found");
+               __DNSSD_LOG_FUNC_EXIT__;
+               return DNSSD_ERROR_SERVICE_NOT_FOUND;
+       }
+
+       if (remote_handle->op_type != DNSSD_TYPE_FOUND &&
+                       remote_handle->op_type != DNSSD_TYPE_FOUND_NOT_RESOLVED) {
+               DNSSD_LOGE("Invalid handle type");
+               DNSSD_UNLOCK;
+               return DNSSD_ERROR_INVALID_PARAMETER;
+       }
+
+       DNSSD_LOGD("Destroy handle: [%p]->[%u]", remote_handle, dnssd_service);
+
+       dnssd_handle_list = g_slist_remove(dnssd_handle_list, remote_handle);
+       g_free(remote_handle->service_type);
+       dnssd_found_data_s *found = GET_FOUND_DATA_P(remote_handle);
+       if (!found) {
+               DNSSD_LOGE("Cannot find found data.");
+               DNSSD_UNLOCK;
+               return DNSSD_ERROR_INVALID_PARAMETER;
+       }
+
+       if (remote_handle->watch_id > 0)
+               __remove_service_getaddrinfo_socket(remote_handle);
+
+       g_free(found->service_name);
+       g_free(found->host);
+       g_free(remote_handle);
+       remote_handle = NULL;
+
+       DNSSD_LOGD("g_slist length [%d]", g_slist_length(dnssd_handle_list));
        DNSSD_UNLOCK;
        __DNSSD_LOG_FUNC_EXIT__;
        return DNSSD_ERROR_NONE;
index f70a227d1c2988a0d1059120de1599a2fb4f1ac4..a88ed2631abc1b0b1270cc4059cfcd02e04ef57d 100644 (file)
@@ -181,6 +181,53 @@ static void _browse_cb(dnssd_service_state_e service_state,
        printf("\n");
 }
 
+int test_dnssd_create_remote_service()
+{
+       int rv = -1;
+
+       dnssd_service_h service = 0;
+       char service_type[255];
+       char service_name[255];
+       char interface[255];
+       printf("Create DNS Service\n");
+       test_get_user_string("Enter type:(Example : _http._tcp, "
+                       "_printer._tcp etc)", service_type, 255);
+       test_get_user_string("Enter name:", service_name, 255);
+       test_get_user_string("Enter interface name:", interface, 255);
+
+       rv = dnssd_create_remote_service(service_type, service_name, interface, &service);
+       if (rv != DNSSD_ERROR_NONE) {
+               printf("Failed to create DNS SD Service, error [%s]\n",
+                               dnssd_error_to_string(rv));
+               return 0;
+       }
+
+       printf("Successfully created DNS SD Service[%u]\n", service);
+
+       return 1;
+}
+
+int test_dnssd_destroy_remote_service()
+{
+       dnssd_service_h service = 0;
+       int rv;
+
+       printf("Destroy Remote Service\n");
+       printf("Enter Remote Service Handler:\n");
+       rv = scanf("%u", &service);
+
+       rv = dnssd_destroy_remote_service(service);
+       if (rv != DNSSD_ERROR_NONE) {
+               printf("Failed to destroy service, error [%s]\n",
+                               dnssd_error_to_string(rv));
+               return 0;
+       }
+       printf("Successfully destroy service\n");
+
+
+       return 1;
+}
+
 int test_dnssd_browse_service()
 {
        dnssd_service_h service = 0;
@@ -219,13 +266,36 @@ int test_dnssd_cancel_browse_service()
        return 1;
 }
 
+int test_dnssd_resolve_service()
+{
+       dnssd_service_h service = 0;
+       int rv;
+
+       printf("Resolve Remote Service\n");
+       printf("Enter Remote Service Handler:\n");
+       rv = scanf("%u", &service);
+
+       rv = dnssd_resolve_service(service, _resolve_cb, NULL);
+       if (rv != DNSSD_ERROR_NONE) {
+               printf("Failed to resolve service, error [%s]\n",
+                               dnssd_error_to_string(rv));
+               return 0;
+       }
+       printf("Successfully resolve service\n");
+
+       return 1;
+}
+
 static void MenuScreen()
 {
        printf("_____________________________________\n");
        printf("|_____________MENU-SCREEN___________|\n");
        printf("| 0 - EXIT                          |\n");
-       printf("| 1 - Browse DNS Service            |\n");
-       printf("| 2 - Cancel Browse DNS Service     |\n");
+       printf("| 1 - Create Remote Service         |\n");
+       printf("| 2 - Destroy Remote Service        |\n");
+       printf("| 3 - Browse DNS Service            |\n");
+       printf("| 4 - Cancel Browse DNS Service     |\n");
+       printf("| 5 - Resolve Service               |\n");
        printf("|___________________________________|\n");
 }
 
@@ -253,11 +323,20 @@ int test_thread(GIOChannel *source, GIOCondition condition, gpointer data)
 
        switch (a[0]) {
        case '1':
-               rv = test_dnssd_browse_service();
+               rv = test_dnssd_create_remote_service();
                break;
        case '2':
+               rv = test_dnssd_destroy_remote_service();
+               break;
+       case '3':
+               rv = test_dnssd_browse_service();
+               break;
+       case '4':
                rv = test_dnssd_cancel_browse_service();
                break;
+       case '5':
+               rv = test_dnssd_resolve_service();
+               break;
        }
 
        if (rv == 1)