service: Simplify nameserver route adding and removing
[framework/connectivity/connman.git] / src / timeserver.c
index f39d64e..2601d36 100644 (file)
@@ -2,7 +2,7 @@
  *
  *  Connection Manager
  *
- *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
+ *  Copyright (C) 2007-2012  Intel Corporation. All rights reserved.
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License version 2 as
@@ -156,6 +156,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,29 +194,31 @@ 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);
-
-       index = connman_network_get_index(network);
-
-       service_gw = __connman_ipconfig_get_gateway_from_index(index);
-
-       /* Then add Service Gateway to the list */
-       if (service_gw != NULL)
-               list = g_slist_prepend(list, g_strdup(service_gw));
+       if (network != NULL) {
+               index = connman_network_get_index(network);
+               service_gw = __connman_ipconfig_get_gateway_from_index(index,
+                       CONNMAN_IPCONFIG_TYPE_ALL);
+
+               /* Then add Service Gateway to the list */
+               if (service_gw != NULL)
+                       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);
 
@@ -207,7 +226,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);
 }