timeserver: Simplify timeserver IP address checking
[framework/connectivity/connman.git] / src / timeserver.c
index 62e050e..e218fe2 100644 (file)
@@ -115,9 +115,6 @@ static void resolv_result(GResolvResultStatus status, char **results, gpointer u
 void __connman_timeserver_sync_next()
 {
        char *server;
-       int ret;
-       struct addrinfo hints;
-       struct addrinfo *addr;
 
        __connman_ntp_stop();
 
@@ -129,15 +126,8 @@ void __connman_timeserver_sync_next()
 
        ts_list = g_slist_delete_link(ts_list, ts_list);
 
-       memset(&hints, 0, sizeof(struct addrinfo));
-       hints.ai_flags = AI_NUMERICHOST;
-       addr = NULL;
-
-       ret = getaddrinfo(server, NULL, &hints, &addr);
-       freeaddrinfo(addr);
-
        /* if its a IP , directly query it. */
-       if (ret == 0) {
+       if (connman_inet_check_ipaddress(server) > 0) {
                DBG("Using timeservers %s", server);
 
                __connman_ntp_start(server);
@@ -156,6 +146,23 @@ void __connman_timeserver_sync_next()
        return;
 }
 
+GSList *__connman_timeserver_add_list(GSList *server_list,
+               const char *timeserver)
+{
+       GSList *list = server_list;
+
+       if (timeserver == NULL)
+               return server_list;
+
+       while (list != NULL) {
+               char *existing_server = list->data;
+               if (strcmp(timeserver, existing_server) == 0)
+                       return server_list;
+               list = g_slist_next(list);
+       }
+       return g_slist_prepend(server_list, g_strdup(timeserver));
+}
+
 /*
  * __connman_timeserver_get_all function creates the timeserver
  * list which will be used to determine NTP server for time corrections.
@@ -177,13 +184,14 @@ GSList *__connman_timeserver_get_all(struct connman_service *service)
        /* First add Service Timeservers.Configuration to the list */
        for (i = 0; service_ts_config != NULL && service_ts_config[i] != NULL;
                        i++)
-               list = g_slist_prepend(list, g_strdup(service_ts_config[i]));
+               list = __connman_timeserver_add_list(list,
+                               service_ts_config[i]);
 
        service_ts = connman_service_get_timeservers(service);
 
        /* First add Service Timeservers via DHCP to the list */
        for (i = 0; service_ts != NULL && service_ts[i] != NULL; i++)
-               list = g_slist_prepend(list, g_strdup(service_ts[i]));
+               list = __connman_timeserver_add_list(list, service_ts[i]);
 
        network = __connman_service_get_network(service);
        if (network != NULL) {
@@ -193,14 +201,14 @@ GSList *__connman_timeserver_get_all(struct connman_service *service)
 
                /* Then add Service Gateway to the list */
                if (service_gw != NULL)
-                       list = g_slist_prepend(list, g_strdup(service_gw));
+                       list = __connman_timeserver_add_list(list, service_gw);
        }
 
        /* Then add Global Timeservers to the list */
        timeservers = load_timeservers();
 
        for (i = 0; timeservers != NULL && timeservers[i] != NULL; i++)
-               list = g_slist_prepend(list, g_strdup(timeservers[i]));
+               list = __connman_timeserver_add_list(list, timeservers[i]);
 
        g_strfreev(timeservers);
 
@@ -208,7 +216,7 @@ GSList *__connman_timeserver_get_all(struct connman_service *service)
 
        /* Lastly add the fallback servers */
        for (i = 0; fallback_ts != NULL && fallback_ts[i] != NULL; i++)
-               list = g_slist_prepend(list, g_strdup(fallback_ts[i]));
+               list = __connman_timeserver_add_list(list, fallback_ts[i]);
 
        return g_slist_reverse(list);
 }