X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Fdhcp.c;h=f4de736a5ab45ad56b92da888d5f892f6c7b7d03;hb=230905c20905f2bc5ccf4b8fab75c1b5df2ac31d;hp=581982881d2a414502ecf529901828cb4fdf3ffa;hpb=01253f8ec8e5d7e5a6d1d392ff526ccd56fcc5dc;p=framework%2Fconnectivity%2Fconnman.git diff --git a/src/dhcp.c b/src/dhcp.c index 5819828..f4de736 100644 --- a/src/dhcp.c +++ b/src/dhcp.c @@ -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 @@ -40,7 +40,7 @@ struct connman_dhcp { dhcp_cb callback; char **nameservers; - char *timeserver; + char **timeservers; char *pac; GDHCPClient *dhcp_client; @@ -51,45 +51,79 @@ static GHashTable *network_table; static void dhcp_free(struct connman_dhcp *dhcp) { g_strfreev(dhcp->nameservers); - g_free(dhcp->timeserver); + g_strfreev(dhcp->timeservers); g_free(dhcp->pac); dhcp->nameservers = NULL; - dhcp->timeserver = NULL; + dhcp->timeservers = NULL; dhcp->pac = NULL; } -static void dhcp_invalid(struct connman_dhcp *dhcp) +/** + * dhcp_invalidate: Invalidate an existing DHCP lease + * @dhcp: pointer to the DHCP lease to invalidate. + * @callback: flag indicating whether or not to invoke the client callback + * if present. + * + * Invalidates an existing DHCP lease, optionally invoking the client + * callback. The caller may wish to avoid the client callback invocation + * when the invocation of that callback might otherwise unnecessarily upset + * service state due to the IP configuration change implied by this + * invalidation. + */ +static void dhcp_invalidate(struct connman_dhcp *dhcp, connman_bool_t callback) { struct connman_service *service; struct connman_ipconfig *ipconfig; int i; + DBG("dhcp %p callback %u", dhcp, callback); + + if (dhcp == NULL) + return; + service = __connman_service_lookup_from_network(dhcp->network); if (service == NULL) - return; + goto out; ipconfig = __connman_service_get_ip4config(service); if (ipconfig == NULL) - return; + goto out; + + __connman_6to4_remove(ipconfig); __connman_service_set_domainname(service, NULL); __connman_service_set_pac(service, NULL); - __connman_service_timeserver_remove(service, dhcp->timeserver); - for (i = 0; dhcp->nameservers[i] != NULL; i++) { - __connman_service_nameserver_remove(service, - dhcp->nameservers[i]); + if (dhcp->timeservers != NULL) { + for (i = 0; dhcp->timeservers[i] != NULL; i++) { + __connman_service_timeserver_remove(service, + dhcp->timeservers[i]); + } + } + + if (dhcp->nameservers != NULL) { + for (i = 0; dhcp->nameservers[i] != NULL; i++) { + __connman_service_nameserver_remove(service, + dhcp->nameservers[i], FALSE); + } } + __connman_ipconfig_set_dhcp_address(ipconfig, + __connman_ipconfig_get_local(ipconfig)); + DBG("last address %s", __connman_ipconfig_get_dhcp_address(ipconfig)); + + __connman_ipconfig_address_remove(ipconfig); + __connman_ipconfig_set_local(ipconfig, NULL); __connman_ipconfig_set_broadcast(ipconfig, NULL); __connman_ipconfig_set_gateway(ipconfig, NULL); __connman_ipconfig_set_prefixlen(ipconfig, 0); - if (dhcp->callback != NULL) + if (dhcp->callback != NULL && callback) dhcp->callback(dhcp->network, FALSE); +out: dhcp_free(dhcp); } @@ -105,7 +139,7 @@ static void no_lease_cb(GDHCPClient *dhcp_client, gpointer user_data) DBG("No lease available"); - dhcp_invalid(dhcp); + dhcp_invalidate(dhcp, TRUE); } static void lease_lost_cb(GDHCPClient *dhcp_client, gpointer user_data) @@ -114,7 +148,7 @@ static void lease_lost_cb(GDHCPClient *dhcp_client, gpointer user_data) DBG("Lease lost"); - dhcp_invalid(dhcp); + dhcp_invalidate(dhcp, TRUE); } static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data) @@ -123,19 +157,42 @@ static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data) DBG("Lease lost"); - dhcp_invalid(dhcp); + dhcp_invalidate(dhcp, TRUE); +} + + +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, **timeservers, *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,8 +209,15 @@ 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); + __connman_ipconfig_set_dhcp_address(ipconfig, address); + DBG("last address %s", address); + option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET); if (option != NULL) netmask = g_strdup(option->data); @@ -162,14 +226,30 @@ 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) { + ns_entries = g_list_length(option); + 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); @@ -181,26 +261,73 @@ static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data) hostname = g_strdup(option->data); option = g_dhcp_client_get_option(dhcp_client, G_DHCP_NTP_SERVER); - if (option != NULL) - dhcp->timeserver = g_strdup(option->data); + ns_entries = g_list_length(option); + timeservers = g_try_new0(char *, ns_entries + 1); + if (timeservers != NULL) { + for (i = 0, list = option; list; list = list->next, i++) + timeservers[i] = g_strdup(list->data); + timeservers[ns_entries] = NULL; + } option = g_dhcp_client_get_option(dhcp_client, 252); if (option != NULL) - dhcp->pac = g_strdup(option->data); + pac = g_strdup(option->data); - prefixlen = __connman_ipconfig_netmask_prefix_len(netmask); + __connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP); - 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); + if (ip_change == TRUE) { + __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 (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], FALSE); + } + g_strfreev(dhcp->nameservers); + } + + dhcp->nameservers = nameservers; + + for (i = 0; dhcp->nameservers != NULL && + dhcp->nameservers[i] != NULL; i++) { + __connman_service_nameserver_append(service, + dhcp->nameservers[i], FALSE); + } + } else { + g_strfreev(nameservers); } - __connman_service_timeserver_append(service, dhcp->timeserver); - __connman_service_set_pac(service, dhcp->pac); + + if (compare_string_arrays(timeservers, dhcp->timeservers) == FALSE) { + if (dhcp->timeservers != NULL) { + for (i = 0; dhcp->timeservers[i] != NULL; i++) { + __connman_service_timeserver_remove(service, + dhcp->timeservers[i]); + } + g_strfreev(dhcp->timeservers); + } + + dhcp->timeservers = timeservers; + + for (i = 0; dhcp->timeservers != NULL && + dhcp->timeservers[i] != NULL; i++) { + __connman_service_timeserver_append(service, + dhcp->timeservers[i]); + } + } else { + g_strfreev(timeservers); + } + + if (g_strcmp0(pac, dhcp->pac) != 0) { + g_free(dhcp->pac); + dhcp->pac = pac; + + __connman_service_set_pac(service, dhcp->pac); + } + __connman_service_set_domainname(service, domainname); if (domainname != NULL) @@ -209,12 +336,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); } @@ -242,7 +371,7 @@ static void ipv4ll_available_cb(GDHCPClient *dhcp_client, gpointer user_data) prefixlen = __connman_ipconfig_netmask_prefix_len(netmask); - connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP); + __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, NULL); @@ -260,6 +389,8 @@ static void dhcp_debug(const char *str, void *data) static int dhcp_request(struct connman_dhcp *dhcp) { + struct connman_service *service; + struct connman_ipconfig *ipconfig; GDHCPClient *dhcp_client; GDHCPClientError error; const char *hostname; @@ -276,6 +407,8 @@ static int dhcp_request(struct connman_dhcp *dhcp) if (getenv("CONNMAN_DHCP_DEBUG")) g_dhcp_client_set_debug(dhcp_client, dhcp_debug, "DHCP"); + g_dhcp_client_set_id(dhcp_client); + hostname = connman_utsname_get_hostname(); if (hostname != NULL) g_dhcp_client_set_send(dhcp_client, G_DHCP_HOST_NAME, hostname); @@ -307,7 +440,11 @@ static int dhcp_request(struct connman_dhcp *dhcp) dhcp->dhcp_client = dhcp_client; - return g_dhcp_client_start(dhcp_client); + service = __connman_service_lookup_from_network(dhcp->network); + ipconfig = __connman_service_get_ip4config(service); + + return g_dhcp_client_start(dhcp_client, + __connman_ipconfig_get_dhcp_address(ipconfig)); } static int dhcp_release(struct connman_dhcp *dhcp) @@ -331,9 +468,9 @@ static void remove_network(gpointer user_data) DBG("dhcp %p", dhcp); + dhcp_invalidate(dhcp, FALSE); dhcp_release(dhcp); - dhcp_free(dhcp); g_free(dhcp); } @@ -350,6 +487,8 @@ int __connman_dhcp_start(struct connman_network *network, dhcp_cb callback) dhcp->network = network; dhcp->callback = callback; + connman_network_ref(network); + g_hash_table_replace(network_table, network, dhcp); return dhcp_request(dhcp); @@ -357,17 +496,13 @@ 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) + if (network_table == NULL) return; - dhcp_release(dhcp); - - g_hash_table_remove(network_table, network); + if (g_hash_table_remove(network_table, network) == TRUE) + connman_network_unref(network); } int __connman_dhcp_init(void) @@ -385,4 +520,5 @@ void __connman_dhcp_cleanup(void) DBG(""); g_hash_table_destroy(network_table); + network_table = NULL; }