dhcp: Remove extra dhcp_release call
[framework/connectivity/connman.git] / src / dhcp.c
index 5819828..f1e3096 100644 (file)
@@ -73,6 +73,8 @@ static void dhcp_invalid(struct connman_dhcp *dhcp)
        if (ipconfig == NULL)
                return;
 
+       __connman_6to4_remove(ipconfig);
+
        __connman_service_set_domainname(service, NULL);
        __connman_service_set_pac(service, NULL);
        __connman_service_timeserver_remove(service, dhcp->timeserver);
@@ -126,16 +128,39 @@ static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
        dhcp_invalid(dhcp);
 }
 
+
+static gboolean compare_string_arrays(char **array_a, char **array_b)
+{
+       int i;
+
+       if (array_a == NULL || array_b == NULL)
+               return FALSE;
+
+       if (g_strv_length(array_a) != g_strv_length(array_b))
+               return FALSE;
+
+       for (i = 0; array_a[i] != NULL &&
+                            array_b[i] != NULL; i++) {
+               if (g_strcmp0(array_a[i], array_b[i]) != 0)
+                       return FALSE;
+       }
+
+       return TRUE;
+}
+
 static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
 {
        struct connman_dhcp *dhcp = user_data;
        GList *list, *option = NULL;
-       char *address, *netmask = NULL, *gateway = NULL, *net = NULL;
+       char *address, *netmask = NULL, *gateway = NULL;
+       const char *c_address, *c_gateway;
        char *domainname = NULL, *hostname = NULL;
+       char **nameservers, *timeserver = NULL, *pac = NULL;
        int ns_entries;
        struct connman_ipconfig *ipconfig;
        struct connman_service *service;
-       unsigned char prefixlen;
+       unsigned char prefixlen, c_prefixlen;
+       gboolean ip_change;
        int i;
 
        DBG("Lease available");
@@ -152,6 +177,10 @@ static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
                return;
        }
 
+       c_address = __connman_ipconfig_get_local(ipconfig);
+       c_gateway = __connman_ipconfig_get_gateway(ipconfig);
+       c_prefixlen = __connman_ipconfig_get_prefixlen(ipconfig);
+
        address = g_dhcp_client_get_address(dhcp_client);
 
        option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
@@ -162,14 +191,31 @@ static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
        if (option != NULL)
                gateway = g_strdup(option->data);
 
+       prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
+
+       DBG("c_address %s", c_address);
+
+       if (address != NULL && c_address != NULL &&
+                                       g_strcmp0(address, c_address) != 0)
+               ip_change = TRUE;
+       else if (gateway != NULL && c_gateway != NULL &&
+                                       g_strcmp0(gateway, c_gateway) != 0)
+               ip_change = TRUE;
+       else if (prefixlen != c_prefixlen)
+               ip_change = TRUE;
+       else if (c_address == NULL || c_gateway == NULL)
+               ip_change = TRUE;
+       else
+               ip_change = FALSE;
+
        option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
        for (ns_entries = 0, list = option; list; list = list->next)
                ns_entries += 1;
-       dhcp->nameservers = g_try_new0(char *, ns_entries + 1);
-       if (dhcp->nameservers) {
+       nameservers = g_try_new0(char *, ns_entries + 1);
+       if (nameservers != NULL) {
                for (i = 0, list = option; list; list = list->next, i++)
-                       dhcp->nameservers[i] = g_strdup(list->data);
-               dhcp->nameservers[ns_entries] = NULL;
+                       nameservers[i] = g_strdup(list->data);
+               nameservers[ns_entries] = NULL;
        }
 
        option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
@@ -182,25 +228,58 @@ static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
 
        option = g_dhcp_client_get_option(dhcp_client, G_DHCP_NTP_SERVER);
        if (option != NULL)
-               dhcp->timeserver = g_strdup(option->data);
+               timeserver = g_strdup(option->data);
 
        option = g_dhcp_client_get_option(dhcp_client, 252);
        if (option != NULL)
-               dhcp->pac = g_strdup(option->data);
-
-       prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
+               pac = g_strdup(option->data);
 
        connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP);
-       __connman_ipconfig_set_local(ipconfig, address);
-       __connman_ipconfig_set_prefixlen(ipconfig, prefixlen);
-       __connman_ipconfig_set_gateway(ipconfig, gateway);
 
-       for (i = 0; dhcp->nameservers[i] != NULL; i++) {
-               __connman_service_nameserver_append(service,
-                                       dhcp->nameservers[i]);
+       if (ip_change == TRUE) {
+               __connman_ipconfig_set_local(ipconfig, address);
+               __connman_ipconfig_set_prefixlen(ipconfig, prefixlen);
+               __connman_ipconfig_set_gateway(ipconfig, gateway);
+       }
+
+       if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
+               if (dhcp->nameservers != NULL) {
+                       for (i = 0; dhcp->nameservers[i] != NULL; i++) {
+                               __connman_service_nameserver_remove(service,
+                                                       dhcp->nameservers[i]);
+                       }
+                       g_strfreev(dhcp->nameservers);
+               }
+
+               dhcp->nameservers = nameservers;
+
+               for (i = 0; dhcp->nameservers[i] != NULL; i++) {
+                       __connman_service_nameserver_append(service,
+                                                       dhcp->nameservers[i]);
+               }
+       }
+
+       if (g_strcmp0(timeserver, dhcp->timeserver) != 0) {
+               if (dhcp->timeserver != NULL) {
+                       __connman_service_timeserver_remove(service,
+                                                       dhcp->timeserver);
+                       g_free(dhcp->timeserver);
+               }
+
+               dhcp->timeserver = timeserver;
+
+               if (dhcp->timeserver != NULL)
+                       __connman_service_timeserver_append(service,
+                                                       dhcp->timeserver);
+       }
+
+       if (g_strcmp0(pac, dhcp->pac) != 0) {
+               g_free(dhcp->pac);
+               dhcp->pac = pac;
+
+               __connman_service_set_pac(service, dhcp->pac);
        }
-       __connman_service_timeserver_append(service, dhcp->timeserver);
-       __connman_service_set_pac(service, dhcp->pac);
+
        __connman_service_set_domainname(service, domainname);
 
        if (domainname != NULL)
@@ -209,12 +288,14 @@ static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
        if (hostname != NULL)
                __connman_utsname_set_hostname(hostname);
 
-       dhcp_valid(dhcp);
+       if (ip_change == TRUE)
+               dhcp_valid(dhcp);
+
+       __connman_6to4_probe(service);
 
        g_free(address);
        g_free(netmask);
        g_free(gateway);
-       g_free(net);
        g_free(domainname);
        g_free(hostname);
 }
@@ -357,16 +438,8 @@ int __connman_dhcp_start(struct connman_network *network, dhcp_cb callback)
 
 void __connman_dhcp_stop(struct connman_network *network)
 {
-       struct connman_dhcp *dhcp;
-
        DBG("");
 
-       dhcp = g_hash_table_lookup(network_table, network);
-       if (dhcp == NULL)
-               return;
-
-       dhcp_release(dhcp);
-
        g_hash_table_remove(network_table, network);
 }