timeserver: Simplify timeserver IP address checking
[framework/connectivity/connman.git] / src / timeserver.c
index 2558d99..e218fe2 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
@@ -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,24 +146,89 @@ 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_sync function recreates the timeserver
+ * __connman_timeserver_get_all function creates the timeserver
  * list which will be used to determine NTP server for time corrections.
- * It must be called everytime the default service changes, the service
- * timeserver(s) or gatway changes or the global timeserver(s) changes.
  * The service settings take priority over the global timeservers.
  */
-int __connman_timeserver_sync(struct connman_service *default_service)
+GSList *__connman_timeserver_get_all(struct connman_service *service)
 {
-       struct connman_service *service;
+       GSList *list = NULL;
        struct connman_network *network;
        char **timeservers;
        char **service_ts;
        char **service_ts_config;
        const char *service_gw;
+       char **fallback_ts;
        int index, i;
 
+       service_ts_config = connman_service_get_timeservers_config(service);
+
+       /* First add Service Timeservers.Configuration to the list */
+       for (i = 0; service_ts_config != NULL && service_ts_config[i] != NULL;
+                       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 = __connman_timeserver_add_list(list, service_ts[i]);
+
+       network = __connman_service_get_network(service);
+       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 = __connman_timeserver_add_list(list, timeservers[i]);
+
+       g_strfreev(timeservers);
+
+       fallback_ts = connman_setting_get_string_list("FallbackTimeservers");
+
+       /* Lastly add the fallback servers */
+       for (i = 0; fallback_ts != NULL && fallback_ts[i] != NULL; i++)
+               list = __connman_timeserver_add_list(list, fallback_ts[i]);
+
+       return g_slist_reverse(list);
+}
+
+/*
+ * This function must be called everytime the default service changes, the
+ * service timeserver(s) or gatway changes or the global timeserver(s) changes.
+ */
+int __connman_timeserver_sync(struct connman_service *default_service)
+{
+       struct connman_service *service;
+
        if (default_service != NULL)
                service = default_service;
        else
@@ -185,7 +240,7 @@ int __connman_timeserver_sync(struct connman_service *default_service)
        if (resolv == NULL)
                return 0;
        /*
-        * Before be start creating the new timeserver list we must stop
+        * Before we start creating the new timeserver list we must stop
         * any ongoing ntp query and server resolution.
         */
 
@@ -194,46 +249,17 @@ int __connman_timeserver_sync(struct connman_service *default_service)
        if (resolv_id > 0)
                g_resolv_cancel_lookup(resolv, resolv_id);
 
-       if (ts_list != NULL) {
-               g_slist_free_full(ts_list, g_free);
-               ts_list = NULL;
-       }
-
-       service_ts_config = connman_service_get_timeservers_config(service);
-
-       /* First add Service Timeservers.Configuration to the list */
-       for (i=0; service_ts_config != NULL && service_ts_config[i] != NULL; i++)
-               ts_list = g_slist_prepend(ts_list, g_strdup(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++)
-               ts_list = g_slist_prepend(ts_list, g_strdup(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)
-               ts_list = g_slist_prepend(ts_list, g_strdup(service_gw));
+       g_slist_free_full(ts_list, g_free);
 
-       /* Then add Global Timeservers to the list */
-       timeservers = load_timeservers();
+       ts_list = __connman_timeserver_get_all(service);
 
-       for (i=0; timeservers != NULL && timeservers[i] != NULL; i++)
-               ts_list = g_slist_prepend(ts_list, g_strdup(timeservers[i]));
+       __connman_service_timeserver_changed(service, ts_list);
 
        if (ts_list == NULL) {
                DBG("No timeservers set.");
                return 0;
        }
 
-       ts_list = g_slist_reverse(ts_list);
-
         __connman_timeserver_sync_next();
 
        return 0;
@@ -255,7 +281,7 @@ static int timeserver_start(struct connman_service *service)
                return -EINVAL;
 
        /* Stop an already ongoing resolution, if there is one */
-       if (resolv_id > 0)
+       if (resolv != NULL && resolv_id > 0)
                g_resolv_cancel_lookup(resolv, resolv_id);
 
        /* get rid of the old resolver */
@@ -265,8 +291,10 @@ static int timeserver_start(struct connman_service *service)
        }
 
        resolv = g_resolv_new(i);
-       if (resolv == NULL)
+       if (resolv == NULL) {
+               g_strfreev(nameservers);
                return -ENOMEM;
+       }
 
        if (getenv("CONNMAN_RESOLV_DEBUG"))
                g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
@@ -274,6 +302,8 @@ static int timeserver_start(struct connman_service *service)
        for (i = 0; nameservers[i] != NULL; i++)
                g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
 
+       g_strfreev(nameservers);
+
        return __connman_timeserver_sync(service);
 }
 
@@ -286,8 +316,7 @@ static void timeserver_stop()
                resolv = NULL;
        }
 
-       if (ts_list != NULL)
-               g_slist_free_full(ts_list, g_free);
+       g_slist_free_full(ts_list, g_free);
 
        ts_list = NULL;