X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Fdhcp.c;h=f4de736a5ab45ad56b92da888d5f892f6c7b7d03;hb=53b7e1339c05b178697e611f881594ed78c02d65;hp=b391ddf6af52154028409d11268e655f912329a4;hpb=1121ed73e9a582d8b92574b00202070a65fb150b;p=framework%2Fconnectivity%2Fconnman.git diff --git a/src/dhcp.c b/src/dhcp.c index b391ddf..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,11 +51,11 @@ 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; } @@ -84,23 +84,37 @@ static void dhcp_invalidate(struct connman_dhcp *dhcp, connman_bool_t callback) 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); @@ -109,6 +123,7 @@ static void dhcp_invalidate(struct connman_dhcp *dhcp, connman_bool_t callback) if (dhcp->callback != NULL && callback) dhcp->callback(dhcp->network, FALSE); +out: dhcp_free(dhcp); } @@ -172,7 +187,7 @@ static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data) char *address, *netmask = NULL, *gateway = NULL; const char *c_address, *c_gateway; char *domainname = NULL, *hostname = NULL; - char **nameservers, *timeserver = NULL, *pac = NULL; + char **nameservers, **timeservers, *pac = NULL; int ns_entries; struct connman_ipconfig *ipconfig; struct connman_service *service; @@ -200,6 +215,9 @@ static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data) 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); @@ -226,8 +244,7 @@ static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data) 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; + 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++) @@ -244,14 +261,19 @@ 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) - 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) pac = g_strdup(option->data); - connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP); + __connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP); if (ip_change == TRUE) { __connman_ipconfig_set_local(ipconfig, address); @@ -263,31 +285,40 @@ static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data) if (dhcp->nameservers != NULL) { for (i = 0; dhcp->nameservers[i] != NULL; i++) { __connman_service_nameserver_remove(service, - dhcp->nameservers[i]); + dhcp->nameservers[i], FALSE); } g_strfreev(dhcp->nameservers); } dhcp->nameservers = nameservers; - for (i = 0; dhcp->nameservers[i] != NULL; i++) { + for (i = 0; dhcp->nameservers != NULL && + dhcp->nameservers[i] != NULL; i++) { __connman_service_nameserver_append(service, - dhcp->nameservers[i]); + dhcp->nameservers[i], FALSE); } + } else { + g_strfreev(nameservers); } - if (g_strcmp0(timeserver, dhcp->timeserver) != 0) { - if (dhcp->timeserver != NULL) { - __connman_service_timeserver_remove(service, - dhcp->timeserver); - g_free(dhcp->timeserver); + 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->timeserver = timeserver; + dhcp->timeservers = timeservers; - if (dhcp->timeserver != NULL) + for (i = 0; dhcp->timeservers != NULL && + dhcp->timeservers[i] != NULL; i++) { __connman_service_timeserver_append(service, - dhcp->timeserver); + dhcp->timeservers[i]); + } + } else { + g_strfreev(timeservers); } if (g_strcmp0(pac, dhcp->pac) != 0) { @@ -340,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); @@ -358,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; @@ -374,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); @@ -405,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) @@ -448,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); @@ -457,7 +498,11 @@ void __connman_dhcp_stop(struct connman_network *network) { DBG(""); - g_hash_table_remove(network_table, network); + if (network_table == NULL) + return; + + if (g_hash_table_remove(network_table, network) == TRUE) + connman_network_unref(network); } int __connman_dhcp_init(void) @@ -475,4 +520,5 @@ void __connman_dhcp_cleanup(void) DBG(""); g_hash_table_destroy(network_table); + network_table = NULL; }