Add function body to start dns lookup 08/200508/3
authorYu <jiung.yu@samsung.com>
Tue, 26 Feb 2019 02:05:55 +0000 (11:05 +0900)
committerYu <jiung.yu@samsung.com>
Thu, 7 Mar 2019 01:05:14 +0000 (10:05 +0900)
Change-Id: Ifd4cf2d1e0e9b1ef07ee21974ec78c7bded86bb4
Signed-off-by: Yu Jiung <jiung.yu@samsung.com>
include/inm-dns-lookup.h
src/inm-dns-lookup.c

index f883cc106ffaa2a690dcdf0ac134177aae0ee76d..68e63ee6f3e2d01a86e3f52f6ed26e9fedc7131e 100644 (file)
@@ -40,6 +40,14 @@ typedef enum {
        INM_DNS_LOOKUP_ERROR_TIMEOUT,
 } inm_dns_lookup_error_e;
 
+typedef void (*inm_default_dns_lookup_callback)(
+               inm_dns_lookup_error_e err,
+               gboolean found,
+               gchar *host_name,
+               gpointer user_data);
+
+int inm_default_dns_lookup_start(inm_default_dns_lookup_callback cb, gpointer user_data);
+
 int inm_dns_lookup_init();
 int inm_dns_lookup_deinit();
 
index 4866bc9cd55660ca920ace16124bd08fec1a9e59..0d94b8a9cda4e77ebfb1eba8e78f20b2f787fb08 100644 (file)
 #include "inm-util.h"
 #include "inm-dns-lookup.h"
 
+#define ARES_DEFAULT_TIMEOUT_MS 1000
+#define DEFAULT_TEST_URL "www.tizen.org"
+
+typedef struct {
+       guint timer_source_id;
+       inm_default_dns_lookup_callback cb;
+       gpointer cb_user_data;
+} inm_dns_lookup_s;
+
+static inm_dns_lookup_s *g_default_dns_lookup_data;
+
+static inline int __create_default_dns_lookup_data()
+{
+       inm_dns_lookup_s *dns_lookup_data = NULL;
+
+       dns_lookup_data = g_try_malloc0(sizeof(inm_dns_lookup_s));
+       if (!dns_lookup_data) {
+               INM_LOGI("");
+               return -1;
+       }
+
+       g_default_dns_lookup_data = dns_lookup_data;
+       return 0;
+}
+
+static inline void __destroy_default_dns_lookup_data()
+{
+       inm_dns_lookup_s *dns_lookup_data;
+
+       __INM_FUNC_ENTER__;
+       if (!g_default_dns_lookup_data) {
+               __INM_FUNC_EXIT__;
+               return;
+       }
+       dns_lookup_data = g_default_dns_lookup_data;
+       g_default_dns_lookup_data = NULL;
+
+       REMOVE_G_SOURCE(dns_lookup_data->timer_source_id);
+
+       __INM_FUNC_EXIT__;
+       return;
+}
+
+static gboolean __default_dns_lookup_timer_cb(gpointer user_data)
+{
+       inm_dns_lookup_s *dns_lookup_data;
+
+       __INM_FUNC_ENTER__;
+       if (!g_default_dns_lookup_data) {
+               __INM_FUNC_EXIT__;
+               return FALSE;
+       }
+
+       PRINT_LOG("default dns lookup timeout");
+       dns_lookup_data = g_default_dns_lookup_data;
+       dns_lookup_data->cb(INM_DNS_LOOKUP_ERROR_TIMEOUT,
+                       FALSE,
+                       DEFAULT_TEST_URL,
+                       dns_lookup_data->cb_user_data);
+
+       /* timer source'll be freed automatically
+        * if this function returns FALSE */
+       dns_lookup_data->timer_source_id = 0;
+       __destroy_default_dns_lookup_data();
+
+       __INM_FUNC_EXIT__;
+       return FALSE;
+}
+
+int inm_default_dns_lookup_start(inm_default_dns_lookup_callback cb, gpointer user_data)
+{
+       int ret;
+
+       if (g_default_dns_lookup_data) {
+               INM_LOGI("");
+               return INM_DNS_LOOKUP_ERROR_IN_PROGRESS;
+       }
+
+       ret = __create_default_dns_lookup_data();
+       if (ret != 0) {
+               INM_LOGI("");
+               return INM_DNS_LOOKUP_ERROR_OPERATION_FAILED;
+       }
+
+       g_default_dns_lookup_data->cb = cb;
+       g_default_dns_lookup_data->cb_user_data = user_data;
+
+       g_default_dns_lookup_data->timer_source_id =
+                       g_timeout_add(ARES_DEFAULT_TIMEOUT_MS,
+                                       __default_dns_lookup_timer_cb,
+                                       NULL);
+
+       return 0;
+}
+
 int inm_dns_lookup_init()
 {
        int version;
@@ -43,5 +138,7 @@ int inm_dns_lookup_init()
 
 int inm_dns_lookup_deinit()
 {
+       __destroy_default_dns_lookup_data();
+
        return INM_DNS_LOOKUP_ERROR_NONE;
 }