*
* Connection Manager
*
- * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
+ * Copyright (C) 2007-2013 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
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
+#include <net/ethernet.h>
+
+#ifndef IPV6_MIN_MTU
+#define IPV6_MIN_MTU 1280
+#endif
+
+#include <connman/ipconfig.h>
+#include <include/setting.h>
+
+#include <gdhcp/gdhcp.h>
#include <glib.h>
#include "connman.h"
+#define RATE_LIMIT_INTERVAL 60 /* delay between successive attempts */
+
struct connman_dhcp {
- gint refcount;
- int index;
- enum connman_dhcp_state state;
+ struct connman_ipconfig *ipconfig;
+ struct connman_network *network;
+ dhcp_cb callback;
+ gpointer user_data;
+
+ char **nameservers;
+ char **timeservers;
+ char *pac;
+
+ unsigned int timeout;
- struct connman_element *element;
+ GDHCPClient *ipv4ll_client;
+ GDHCPClient *dhcp_client;
+ char *ipv4ll_debug_prefix;
+ char *dhcp_debug_prefix;
- struct connman_dhcp_driver *driver;
- void *driver_data;
+ bool ipv4ll_running;
};
-/**
- * connman_dhcp_ref:
- * @dhcp: DHCP structure
- *
- * Increase reference counter of DHCP
- */
-struct connman_dhcp *connman_dhcp_ref(struct connman_dhcp *dhcp)
+static GHashTable *ipconfig_table;
+
+static void dhcp_free(struct connman_dhcp *dhcp)
{
- g_atomic_int_inc(&dhcp->refcount);
+#if defined TIZEN_EXT
+ DBG("dhcp_free [%p]", dhcp);
+#endif
+ g_strfreev(dhcp->nameservers);
+ g_strfreev(dhcp->timeservers);
+ g_free(dhcp->pac);
+
+ dhcp->nameservers = NULL;
+ dhcp->timeservers = NULL;
+ dhcp->pac = NULL;
- return dhcp;
+ g_free(dhcp);
+#if defined TIZEN_EXT
+ dhcp = NULL;
+#endif
}
-/**
- * connman_dhcp_unref:
- * @dhcp: DHCP structure
- *
- * Decrease reference counter of DHCP
- */
-void connman_dhcp_unref(struct connman_dhcp *dhcp)
+static void ipv4ll_stop_client(struct connman_dhcp *dhcp)
{
- if (g_atomic_int_dec_and_test(&dhcp->refcount) == TRUE)
- g_free(dhcp);
+#if defined TIZEN_EXT
+ DBG("dhcp [%p] ipv4ll_client [%p]", dhcp, dhcp->ipv4ll_client);
+#endif
+ if (!dhcp->ipv4ll_client)
+ return;
+
+ g_dhcp_client_stop(dhcp->ipv4ll_client);
+ g_dhcp_client_unref(dhcp->ipv4ll_client);
+ dhcp->ipv4ll_client = NULL;
+ dhcp->ipv4ll_running = false;
+
+ g_free(dhcp->ipv4ll_debug_prefix);
+ dhcp->ipv4ll_debug_prefix = NULL;
}
-/**
- * connman_dhcp_get_index:
- * @dhcp: DHCP structure
- *
- * Get network index of DHCP
- */
-int connman_dhcp_get_index(struct connman_dhcp *dhcp)
+static bool apply_dhcp_invalidate_on_network(struct connman_dhcp *dhcp)
{
- return dhcp->index;
+ struct connman_service *service;
+ int i;
+
+ if (!dhcp->network)
+ return true;
+
+ service = connman_service_lookup_from_network(dhcp->network);
+ if (!service) {
+ connman_error("Can not lookup service");
+ return false;
+ }
+
+ __connman_service_set_domainname(service, NULL);
+ __connman_ipconfig_set_proxy_autoconfig(dhcp->ipconfig, NULL);
+
+ if (dhcp->timeservers) {
+ for (i = 0; dhcp->timeservers[i]; i++) {
+ __connman_service_timeserver_remove(service,
+ dhcp->timeservers[i]);
+ }
+ g_strfreev(dhcp->timeservers);
+ dhcp->timeservers = NULL;
+ }
+ if (dhcp->nameservers) {
+ for (i = 0; dhcp->nameservers[i]; i++) {
+#if defined TIZEN_EXT
+ __connman_service_nameserver_remove(service,
+ dhcp->nameservers[i], false,
+ CONNMAN_IPCONFIG_TYPE_IPV4);
+#else
+ __connman_service_nameserver_remove(service,
+ dhcp->nameservers[i], false);
+#endif
+ }
+ g_strfreev(dhcp->nameservers);
+ dhcp->nameservers = NULL;
+ }
+
+ return true;
}
/**
- * connman_dhcp_get_interface:
- * @dhcp: DHCP structure
+ * 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.
*
- * Get network interface of DHCP
+ * 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.
*/
-char *connman_dhcp_get_interface(struct connman_dhcp *dhcp)
+static void dhcp_invalidate(struct connman_dhcp *dhcp, bool callback)
{
- return connman_inet_ifname(dhcp->index);
+ DBG("dhcp %p callback %u", dhcp, callback);
+
+ if (!dhcp)
+ return;
+
+ __connman_6to4_remove(dhcp->ipconfig);
+
+ if (!apply_dhcp_invalidate_on_network(dhcp))
+ return;
+
+ __connman_ipconfig_set_dhcp_address(dhcp->ipconfig,
+ __connman_ipconfig_get_local(dhcp->ipconfig));
+ DBG("last address %s",
+ __connman_ipconfig_get_dhcp_address(dhcp->ipconfig));
+
+ __connman_ipconfig_address_remove(dhcp->ipconfig);
+
+ __connman_ipconfig_set_local(dhcp->ipconfig, NULL);
+ __connman_ipconfig_set_broadcast(dhcp->ipconfig, NULL);
+ __connman_ipconfig_set_gateway(dhcp->ipconfig, NULL);
+ __connman_ipconfig_set_prefixlen(dhcp->ipconfig, 0);
+
+ if (dhcp->callback && callback)
+ dhcp->callback(dhcp->ipconfig, dhcp->network,
+ false, dhcp->user_data);
}
-/**
- * connman_dhcp_set_value:
- * @dhcp: DHCP structure
- * @key: unique identifier
- * @value: string value
- *
- * Set string value for specific key
- */
-void connman_dhcp_set_value(struct connman_dhcp *dhcp,
- const char *key, const char *value)
+static void dhcp_valid(struct connman_dhcp *dhcp)
{
- char **nameservers;
+ if (dhcp->callback)
+ dhcp->callback(dhcp->ipconfig, dhcp->network,
+ true, dhcp->user_data);
+}
- if (g_strcmp0(key, "Address") == 0) {
- g_free(dhcp->element->ipv4.address);
- dhcp->element->ipv4.address = g_strdup(value);
- } else if (g_strcmp0(key, "Netmask") == 0) {
- g_free(dhcp->element->ipv4.netmask);
- dhcp->element->ipv4.netmask = g_strdup(value);
- } else if (g_strcmp0(key, "Gateway") == 0) {
- g_free(dhcp->element->ipv4.gateway);
- dhcp->element->ipv4.gateway = g_strdup(value);
- } else if (g_strcmp0(key, "Network") == 0) {
- g_free(dhcp->element->ipv4.network);
- dhcp->element->ipv4.network = g_strdup(value);
- } else if (g_strcmp0(key, "Broadcast") == 0) {
- g_free(dhcp->element->ipv4.broadcast);
- dhcp->element->ipv4.broadcast = g_strdup(value);
- } else if (g_strcmp0(key, "Nameserver") == 0) {
- g_free(dhcp->element->ipv4.nameserver);
- nameservers = g_strsplit_set(value, " ", 0);
- /* FIXME: The ipv4 structure can only hold one nameserver, so
- * we are only able to pass along the first nameserver sent by
- * the DHCP server. If this situation changes, we should
- * retain all of them.
- */
- dhcp->element->ipv4.nameserver = g_strdup(nameservers[0]);
- g_strfreev(nameservers);
- } else if (g_strcmp0(key, "Domainname") == 0) {
- g_free(dhcp->element->domainname);
- dhcp->element->domainname = g_strdup(value);
-
- __connman_utsname_set_domainname(value);
- } else if (g_strcmp0(key, "Hostname") == 0) {
- g_free(dhcp->element->hostname);
- dhcp->element->hostname = g_strdup(value);
-
- __connman_utsname_set_hostname(value);
- } else if (g_strcmp0(key, "Timeserver") == 0) {
- g_free(dhcp->element->ipv4.timeserver);
- dhcp->element->ipv4.timeserver = g_strdup(value);
- } else if (g_strcmp0(key, "MTU") == 0) {
- } else if (g_strcmp0(key, "PAC") == 0) {
- connman_info("PAC configuration %s", value);
-
- g_free(dhcp->element->ipv4.pac);
- dhcp->element->ipv4.pac = g_strdup(value);
+static void dhcp_debug(const char *str, void *data)
+{
+ connman_info("%s: %s", (const char *) data, str);
+}
+
+static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data);
+static void ipv4ll_available_cb(GDHCPClient *ipv4ll_client, gpointer user_data);
+
+static int ipv4ll_start_client(struct connman_dhcp *dhcp)
+{
+ GDHCPClient *ipv4ll_client;
+ GDHCPClientError error;
+ const char *hostname;
+ int index;
+ int err;
+
+#if defined TIZEN_EXT
+ DBG("dhcp %p", dhcp);
+#endif
+
+ if (dhcp->ipv4ll_client)
+ return -EALREADY;
+
+ index = __connman_ipconfig_get_index(dhcp->ipconfig);
+
+ ipv4ll_client = g_dhcp_client_new(G_DHCP_IPV4LL, index, &error);
+ if (error != G_DHCP_CLIENT_ERROR_NONE)
+ return -EINVAL;
+
+#if !defined TIZEN_EXT
+ if (getenv("CONNMAN_DHCP_DEBUG")) {
+#endif
+ dhcp->ipv4ll_debug_prefix = g_strdup_printf("IPv4LL index %d",
+ index);
+ g_dhcp_client_set_debug(ipv4ll_client, dhcp_debug,
+ dhcp->ipv4ll_debug_prefix);
+#if !defined TIZEN_EXT
+ }
+#endif
+
+ g_dhcp_client_set_id(ipv4ll_client);
+
+ if (dhcp->network) {
+ hostname = connman_utsname_get_hostname();
+ if (hostname)
+ g_dhcp_client_set_send(ipv4ll_client,
+ G_DHCP_HOST_NAME, hostname);
+ }
+
+ g_dhcp_client_register_event(ipv4ll_client,
+ G_DHCP_CLIENT_EVENT_IPV4LL_LOST, ipv4ll_lost_cb, dhcp);
+
+ g_dhcp_client_register_event(ipv4ll_client,
+ G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE,
+ ipv4ll_available_cb, dhcp);
+
+ dhcp->ipv4ll_client = ipv4ll_client;
+
+ err = g_dhcp_client_start(dhcp->ipv4ll_client, NULL);
+ if (err < 0) {
+ ipv4ll_stop_client(dhcp);
+ return err;
}
+
+ dhcp->ipv4ll_running = true;
+ return 0;
}
-/**
- * connman_dhcp_bound:
- * @dhcp: DHCP structure
- *
- * Report successful bound of the interface
- */
-void connman_dhcp_bound(struct connman_dhcp *dhcp)
+static gboolean dhcp_retry_cb(gpointer user_data)
{
- struct connman_element *element;
+ struct connman_dhcp *dhcp = user_data;
+
+ dhcp->timeout = 0;
+#if defined TIZEN_EXT
DBG("dhcp %p", dhcp);
+ DBG("dhcp->timeout %d", dhcp->timeout);
+#endif
+ g_dhcp_client_start(dhcp->dhcp_client,
+ __connman_ipconfig_get_dhcp_address(dhcp->ipconfig));
+
+ return FALSE;
+}
+
+static void no_lease_cb(GDHCPClient *dhcp_client, gpointer user_data)
+{
+ struct connman_dhcp *dhcp = user_data;
+ int err;
+
+ DBG("No lease available ipv4ll %d client %p", dhcp->ipv4ll_running,
+ dhcp->ipv4ll_client);
- element = connman_element_create(NULL);
- if (element == NULL)
+#if defined TIZEN_EXT
+ if (connman_setting_get_bool("EnableAutoIp") == false) {
+ DBG("link-local address autoconfiguration is disabled.");
+ if (dhcp->network)
+ __connman_network_disconnect(dhcp->network);
return;
+ }
+#endif
+ if (dhcp->timeout > 0)
+ g_source_remove(dhcp->timeout);
- element->type = CONNMAN_ELEMENT_TYPE_IPV4;
- element->index = dhcp->index;
+ dhcp->timeout = g_timeout_add_seconds(RATE_LIMIT_INTERVAL,
+ dhcp_retry_cb,
+ dhcp);
+ if (dhcp->ipv4ll_running)
+ return;
- connman_element_update(dhcp->element);
+ err = ipv4ll_start_client(dhcp);
+ if (err < 0)
+ DBG("Cannot start ipv4ll client (%d/%s)", err, strerror(-err));
- if (connman_element_register(element, dhcp->element) < 0)
- connman_element_unref(element);
+ /* Only notify upper layer if we have a problem */
+ dhcp_invalidate(dhcp, !dhcp->ipv4ll_running);
}
-/**
- * connman_dhcp_renew:
- * @dhcp: DHCP structure
- *
- * Report successful renew of the interface
- */
-void connman_dhcp_renew(struct connman_dhcp *dhcp)
+static void lease_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
{
- DBG("dhcp %p", dhcp);
+ struct connman_dhcp *dhcp = user_data;
+
+ DBG("Lease lost");
- connman_element_update(dhcp->element);
+ /* Upper layer will decide what to do, e.g. nothing or retry. */
+ dhcp_invalidate(dhcp, true);
}
-/**
- * connman_dhcp_fail:
- * @dhcp: DHCP structure
- *
- * Report DHCP failure of the interface
- */
-void connman_dhcp_fail(struct connman_dhcp *dhcp)
+static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
{
- DBG("dhcp %p", dhcp);
+ struct connman_dhcp *dhcp = user_data;
+
+ DBG("Lease lost");
+
+ ipv4ll_stop_client(dhcp);
- connman_element_set_error(dhcp->element,
- CONNMAN_ELEMENT_ERROR_FAILED);
+ /*
+ * Since we lost our IPv4LL configuration we might as notify
+ * the upper layers.
+ */
+ dhcp_invalidate(dhcp, true);
}
-/**
- * connman_dhcp_get_data:
- * @dhcp: DHCP structure
- *
- * Get private DHCP data pointer
- */
-void *connman_dhcp_get_data(struct connman_dhcp *dhcp)
+static bool compare_string_arrays(char **array_a, char **array_b)
{
- return dhcp->driver_data;
+ int i;
+
+ if (!array_a || !array_b)
+ return false;
+
+ if (g_strv_length(array_a) != g_strv_length(array_b))
+ return false;
+
+ for (i = 0; array_a[i] &&
+ array_b[i]; i++) {
+ if (g_strcmp0(array_a[i], array_b[i]) != 0)
+ return false;
+ }
+
+ return true;
}
-/**
- * connman_dhcp_set_data:
- * @dhcp: DHCP structure
- * @data: data pointer
- *
- * Set private DHCP data pointer
- */
-void connman_dhcp_set_data(struct connman_dhcp *dhcp, void *data)
+static bool apply_lease_available_on_network(GDHCPClient *dhcp_client,
+ struct connman_dhcp *dhcp)
{
- dhcp->driver_data = data;
+ char **nameservers, **timeservers, *pac = NULL;
+ struct connman_service *service;
+ GList *list, *option = NULL;
+ int ns_entries;
+ int i;
+
+ if (!dhcp->network)
+ return true;
+
+ service = connman_service_lookup_from_network(dhcp->network);
+ if (!service) {
+ connman_error("Can not lookup service");
+ return false;
+ }
+
+ option = g_dhcp_client_get_option(dhcp_client, G_DHCP_MTU);
+ if (option && option->data) {
+ int mtu, index, err;
+
+ mtu = atoi(option->data);
+
+ if (mtu >= IPV6_MIN_MTU && mtu <= ETH_DATA_LEN) {
+ index = __connman_ipconfig_get_index(dhcp->ipconfig);
+ err = connman_inet_set_mtu(index, mtu);
+
+ DBG("MTU %d index %d err %d", mtu, index, err);
+ }
+ }
+
+ option = g_dhcp_client_get_option(dhcp_client, 252);
+ if (option)
+ pac = g_strdup(option->data);
+
+ option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
+ ns_entries = g_list_length(option);
+ nameservers = g_try_new0(char *, ns_entries + 1);
+ if (nameservers) {
+ for (i = 0, list = option;list; list = list->next, i++)
+ nameservers[i] = g_strdup(list->data);
+ nameservers[ns_entries] = NULL;
+ }
+
+ option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
+ if (option)
+ __connman_service_set_domainname(service, option->data);
+
+ option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
+ if (option)
+ __connman_service_set_hostname(service, option->data);
+
+ option = g_dhcp_client_get_option(dhcp_client, G_DHCP_NTP_SERVER);
+ ns_entries = g_list_length(option);
+ timeservers = g_try_new0(char *, ns_entries + 1);
+ if (timeservers) {
+ for (i = 0, list = option; list; list = list->next, i++)
+ timeservers[i] = g_strdup(list->data);
+ timeservers[ns_entries] = NULL;
+ }
+
+ if (!compare_string_arrays(nameservers, dhcp->nameservers)) {
+ if (dhcp->nameservers) {
+#if defined TIZEN_EXT
+ for (i = 0; dhcp->nameservers[i] != NULL; i++) {
+ __connman_service_nameserver_remove(service,
+ dhcp->nameservers[i], false,
+ CONNMAN_IPCONFIG_TYPE_IPV4);
+ }
+#else
+ for (i = 0; dhcp->nameservers[i]; i++) {
+ __connman_service_nameserver_remove(service,
+ dhcp->nameservers[i], false);
+ }
+#endif
+ g_strfreev(dhcp->nameservers);
+ }
+
+ dhcp->nameservers = nameservers;
+
+ for (i = 0; dhcp->nameservers && dhcp->nameservers[i]; i++) {
+#if defined TIZEN_EXT
+ __connman_service_nameserver_append(service,
+ dhcp->nameservers[i], false,
+ CONNMAN_IPCONFIG_TYPE_IPV4);
+#else
+ __connman_service_nameserver_append(service,
+ dhcp->nameservers[i], false);
+#endif
+ }
+ } else {
+ g_strfreev(nameservers);
+ }
+
+ if (!compare_string_arrays(timeservers, dhcp->timeservers)) {
+ if (dhcp->timeservers) {
+ for (i = 0; dhcp->timeservers[i]; i++) {
+ __connman_service_timeserver_remove(service,
+ dhcp->timeservers[i]);
+ }
+ g_strfreev(dhcp->timeservers);
+ }
+
+ dhcp->timeservers = timeservers;
+
+ for (i = 0; dhcp->timeservers && dhcp->timeservers[i]; 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_ipconfig_set_proxy_autoconfig(dhcp->ipconfig,
+ dhcp->pac);
+ }
+
+ if (connman_setting_get_bool("Enable6to4"))
+ __connman_6to4_probe(service);
+
+ return true;
}
-static GSList *driver_list = NULL;
+static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
+{
+ struct connman_dhcp *dhcp = user_data;
+ GList *option = NULL;
+ enum connman_ipconfig_method old_method;
+ char *address, *netmask = NULL, *gateway = NULL;
+ const char *c_address, *c_gateway;
+ unsigned char prefixlen, c_prefixlen;
+ bool ip_change = false;
+
+ DBG("Lease available");
+
+ if (dhcp->ipv4ll_client) {
+ ipv4ll_stop_client(dhcp);
+ dhcp_invalidate(dhcp, false);
+ }
+
+ c_address = __connman_ipconfig_get_local(dhcp->ipconfig);
+ c_gateway = __connman_ipconfig_get_gateway(dhcp->ipconfig);
+ c_prefixlen = __connman_ipconfig_get_prefixlen(dhcp->ipconfig);
+
+ address = g_dhcp_client_get_address(dhcp_client);
+
+ __connman_ipconfig_set_dhcp_address(dhcp->ipconfig, address);
+ DBG("last address %s", address);
+
+#if defined TIZEN_EXT
+ int dhcp_lease_duration = g_dhcp_client_get_dhcp_lease_duration(dhcp_client);
+#endif
+
+ option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
+ if (option)
+ netmask = g_strdup(option->data);
+
+ option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
+ if (option)
+ gateway = g_strdup(option->data);
+
+ prefixlen = connman_ipaddress_calc_netmask_len(netmask);
+ if (prefixlen == 255)
+ connman_warn("netmask: %s is invalid", netmask);
+
+ DBG("c_address %s", c_address);
+
+ if (g_strcmp0(address, c_address)) {
+ ip_change = true;
+ if (c_address) {
+ /* Remove old ip address */
+ __connman_ipconfig_address_remove(dhcp->ipconfig);
+ }
+ }
+ if (g_strcmp0(gateway, c_gateway)) {
+ ip_change = true;
+ if (c_gateway) {
+ /* Remove gateway ip address */
+ __connman_ipconfig_gateway_remove(dhcp->ipconfig);
+ }
+ } else if (prefixlen != c_prefixlen)
+ ip_change = true;
+
+ old_method = __connman_ipconfig_get_method(dhcp->ipconfig);
+ __connman_ipconfig_set_method(dhcp->ipconfig,
+ CONNMAN_IPCONFIG_METHOD_DHCP);
+
+#if defined TIZEN_EXT
+ __connman_ipconfig_set_dhcp_lease_duration(dhcp->ipconfig, dhcp_lease_duration);
+#endif
+
+ /*
+ * Notify IPv4.Configuration's method moved back to DHCP.
+ *
+ * This is the case ConnMan initially set an address by using
+ * IPv4LL because DHCP failed but now we got an address from DHCP.
+ */
+ if (old_method == CONNMAN_IPCONFIG_METHOD_AUTO) {
+ struct connman_service *service =
+ connman_service_lookup_from_network(dhcp->network);
+
+ if (service)
+ __connman_service_notify_ipv4_configuration(service);
+ }
+
+ if (ip_change) {
+ __connman_ipconfig_set_local(dhcp->ipconfig, address);
+ __connman_ipconfig_set_prefixlen(dhcp->ipconfig, prefixlen);
+ __connman_ipconfig_set_gateway(dhcp->ipconfig, gateway);
+ }
+
+ if (!apply_lease_available_on_network(dhcp_client, dhcp))
+ goto done;
+
+ if (ip_change)
+ dhcp_valid(dhcp);
+
+done:
+ g_free(address);
+ g_free(netmask);
+ g_free(gateway);
+}
-static gint compare_priority(gconstpointer a, gconstpointer b)
+static void ipv4ll_available_cb(GDHCPClient *ipv4ll_client, gpointer user_data)
{
- const struct connman_dhcp_driver *driver1 = a;
- const struct connman_dhcp_driver *driver2 = b;
+ struct connman_dhcp *dhcp = user_data;
+ enum connman_ipconfig_method old_method;
+ char *address, *netmask;
+ unsigned char prefixlen;
+
+ DBG("IPV4LL available");
+
+ address = g_dhcp_client_get_address(ipv4ll_client);
+ netmask = g_dhcp_client_get_netmask(ipv4ll_client);
+
+ prefixlen = connman_ipaddress_calc_netmask_len(netmask);
+
+ old_method = __connman_ipconfig_get_method(dhcp->ipconfig);
+ __connman_ipconfig_set_method(dhcp->ipconfig,
+ CONNMAN_IPCONFIG_METHOD_AUTO);
+
+ /*
+ * Notify IPv4.Configuration's method is AUTO now.
+ *
+ * This is the case DHCP failed thus ConnMan used IPv4LL to get an
+ * address. Set IPv4.Configuration method to AUTO allows user to
+ * ask for a DHCP address by setting the method again to DHCP.
+ */
+ if (old_method == CONNMAN_IPCONFIG_METHOD_DHCP) {
+ struct connman_service *service =
+ connman_service_lookup_from_network(dhcp->network);
+
+ if (service)
+ __connman_service_notify_ipv4_configuration(service);
+ }
+
+ __connman_ipconfig_set_local(dhcp->ipconfig, address);
+ __connman_ipconfig_set_prefixlen(dhcp->ipconfig, prefixlen);
+ __connman_ipconfig_set_gateway(dhcp->ipconfig, NULL);
- return driver2->priority - driver1->priority;
+ dhcp_valid(dhcp);
+
+ g_free(address);
+ g_free(netmask);
}
-/**
- * connman_dhcp_driver_register:
- * @driver: DHCP driver definition
- *
- * Register a new DHCP driver
- *
- * Returns: %0 on success
- */
-int connman_dhcp_driver_register(struct connman_dhcp_driver *driver)
+static int dhcp_initialize(struct connman_dhcp *dhcp)
{
- DBG("driver %p name %s", driver, driver->name);
+ GDHCPClient *dhcp_client;
+ GDHCPClientError error;
+ int index;
+ const char *vendor_class_id;
- driver_list = g_slist_insert_sorted(driver_list, driver,
- compare_priority);
+ DBG("dhcp %p", dhcp);
+
+ index = __connman_ipconfig_get_index(dhcp->ipconfig);
+
+ dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
+ if (error != G_DHCP_CLIENT_ERROR_NONE)
+#if defined TIZEN_EXT
+ {
+ DBG("failed g_dhcp_client_new(%d), index(%d)", error, index);
+#endif
+ return -EINVAL;
+#if defined TIZEN_EXT
+ }
+#endif
+
+#if !defined TIZEN_EXT
+ if (getenv("CONNMAN_DHCP_DEBUG")) {
+#endif
+ dhcp->dhcp_debug_prefix = g_strdup_printf("DHCP index %d",
+ index);
+ g_dhcp_client_set_debug(dhcp_client, dhcp_debug,
+ dhcp->dhcp_debug_prefix);
+#if !defined TIZEN_EXT
+ }
+#endif
+
+ g_dhcp_client_set_id(dhcp_client);
+
+ if (dhcp->network) {
+ struct connman_service *service;
+ const char *hostname;
+
+ service = connman_service_lookup_from_network(dhcp->network);
+
+ hostname = __connman_service_get_hostname(service);
+ if (!hostname)
+ hostname = connman_utsname_get_hostname();
+
+ if (hostname)
+ g_dhcp_client_set_send(dhcp_client,
+ G_DHCP_HOST_NAME, hostname);
+
+ g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
+ g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
+ g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
+ g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
+ g_dhcp_client_set_request(dhcp_client, 252);
+ g_dhcp_client_set_request(dhcp_client, G_DHCP_MTU);
+ }
+
+ g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
+ g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
+
+ vendor_class_id = connman_option_get_string("VendorClassID");
+ if (vendor_class_id)
+ g_dhcp_client_set_send(dhcp_client, G_DHCP_VENDOR_CLASS_ID,
+ vendor_class_id);
+
+ g_dhcp_client_register_event(dhcp_client,
+ G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
+ lease_available_cb, dhcp);
+
+ g_dhcp_client_register_event(dhcp_client,
+ G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp);
+
+ g_dhcp_client_register_event(dhcp_client,
+ G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, dhcp);
+
+ dhcp->dhcp_client = dhcp_client;
return 0;
}
-/**
- * connman_dhcp_driver_unregister:
- * @driver: DHCP driver definition
- *
- * Remove a previously registered DHCP driver
- */
-void connman_dhcp_driver_unregister(struct connman_dhcp_driver *driver)
+static int dhcp_release(struct connman_dhcp *dhcp)
{
- DBG("driver %p name %s", driver, driver->name);
+ DBG("dhcp %p", dhcp);
+
+ if (dhcp->timeout > 0) {
+ g_source_remove(dhcp->timeout);
+ dhcp->timeout = 0;
+ }
+
+ if (dhcp->dhcp_client) {
+ g_dhcp_client_stop(dhcp->dhcp_client);
+ g_dhcp_client_unref(dhcp->dhcp_client);
+ }
+
+ dhcp->dhcp_client = NULL;
- driver_list = g_slist_remove(driver_list, driver);
+ g_free(dhcp->dhcp_debug_prefix);
+ dhcp->dhcp_debug_prefix = NULL;
+
+ ipv4ll_stop_client(dhcp);
+
+ return 0;
}
-static int dhcp_probe(struct connman_element *element)
+char *__connman_dhcp_get_server_address(struct connman_ipconfig *ipconfig)
{
struct connman_dhcp *dhcp;
- GSList *list;
- DBG("element %p name %s", element, element->name);
+ dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
+ if (!dhcp)
+ return NULL;
+
+ return g_dhcp_client_get_server_address(dhcp->dhcp_client);
+}
- dhcp = g_try_new0(struct connman_dhcp, 1);
- if (dhcp == NULL)
- return -ENOMEM;
+#if defined TIZEN_EXT_WIFI_MESH
+int __connman_mesh_dhcp_start(struct connman_ipconfig *ipconfig,
+ dhcp_cb callback, gpointer user_data)
+{
+ struct connman_dhcp *dhcp;
+ int err;
- dhcp->refcount = 1;
- dhcp->index = element->index;
- dhcp->state = CONNMAN_DHCP_STATE_IDLE;
+ DBG("");
- dhcp->element = element;
+ dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
+ if (!dhcp) {
- connman_element_set_data(element, dhcp);
+ dhcp = g_try_new0(struct connman_dhcp, 1);
+ if (!dhcp)
+ return -ENOMEM;
- for (list = driver_list; list; list = list->next) {
- struct connman_dhcp_driver *driver = list->data;
+ dhcp->ipconfig = ipconfig;
+ __connman_ipconfig_ref(ipconfig);
- DBG("driver %p name %s", driver, driver->name);
+ err = dhcp_initialize(dhcp);
- if (driver->request(dhcp) == 0) {
- dhcp->driver = driver;
- break;
+ if (err < 0) {
+ g_free(dhcp);
+ return err;
}
- }
- if (dhcp->driver == NULL) {
- connman_dhcp_unref(dhcp);
- return -ENOENT;
+ g_hash_table_insert(ipconfig_table, ipconfig, dhcp);
}
- return 0;
+ dhcp->callback = callback;
+ dhcp->user_data = user_data;
+ return g_dhcp_client_start(dhcp->dhcp_client, NULL);
}
+#endif
-static void dhcp_remove(struct connman_element *element)
+int __connman_dhcp_start(struct connman_ipconfig *ipconfig,
+ struct connman_network *network, dhcp_cb callback,
+ gpointer user_data)
{
- struct connman_dhcp *dhcp = connman_element_get_data(element);
+#if !defined TIZEN_EXT
+ const char *last_addr = NULL;
+#endif
+ struct connman_dhcp *dhcp;
+ int err;
- DBG("element %p name %s", element, element->name);
+ DBG("");
- connman_element_set_data(element, NULL);
+ if (network) {
+ struct connman_service *service;
- if (dhcp->driver) {
- dhcp->driver->release(dhcp);
- dhcp->driver = NULL;
+ service = connman_service_lookup_from_network(network);
+ if (!service)
+ return -EINVAL;
}
- connman_dhcp_unref(dhcp);
+#if !defined TIZEN_EXT
+ last_addr = __connman_ipconfig_get_dhcp_address(ipconfig);
+#endif
+
+ dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
+ if (!dhcp) {
+
+ dhcp = g_try_new0(struct connman_dhcp, 1);
+ if (!dhcp)
+ return -ENOMEM;
+
+ dhcp->ipconfig = ipconfig;
+ __connman_ipconfig_ref(ipconfig);
+
+ if (network) {
+ dhcp->network = network;
+ connman_network_ref(network);
+ }
+
+ err = dhcp_initialize(dhcp);
+
+ if (err < 0) {
+ if (network)
+ connman_network_unref(network);
+ g_free(dhcp);
+ return err;
+ }
+
+ g_hash_table_insert(ipconfig_table, ipconfig, dhcp);
+ }
+
+ dhcp->callback = callback;
+ dhcp->user_data = user_data;
+
+#if defined TIZEN_EXT
+ DBG("Start DHCP with DHCPDISCOVER request");
+
+ return g_dhcp_client_start(dhcp->dhcp_client, NULL);
+#else
+ return g_dhcp_client_start(dhcp->dhcp_client, last_addr);
+#endif
}
-static void dhcp_change(struct connman_element *element)
+void __connman_dhcp_stop(struct connman_ipconfig *ipconfig)
{
- DBG("element %p name %s", element, element->name);
+ struct connman_dhcp *dhcp;
- if (element->state == CONNMAN_ELEMENT_STATE_ERROR)
- connman_element_set_error(element->parent,
- CONNMAN_ELEMENT_ERROR_DHCP_FAILED);
+ DBG("ipconfig_table %p ipconfig %p", ipconfig_table, ipconfig);
+
+ if (!ipconfig_table)
+ return;
+
+ dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
+ if (dhcp) {
+ g_hash_table_remove(ipconfig_table, ipconfig);
+ __connman_ipconfig_unref(ipconfig);
+ if (dhcp->network)
+ connman_network_unref(dhcp->network);
+ dhcp_release(dhcp);
+ dhcp_invalidate(dhcp, false);
+ dhcp_free(dhcp);
+ }
}
-static struct connman_driver dhcp_driver = {
- .name = "dhcp",
- .type = CONNMAN_ELEMENT_TYPE_DHCP,
- .priority = CONNMAN_DRIVER_PRIORITY_LOW,
- .probe = dhcp_probe,
- .remove = dhcp_remove,
- .change = dhcp_change,
-};
+void __connman_dhcp_decline(struct connman_ipconfig *ipconfig)
+{
+ struct connman_dhcp *dhcp;
+ const char *address;
+ struct in_addr addr;
+
+ DBG("ipconfig_table %p ipconfig %p", ipconfig_table, ipconfig);
+
+ if (!ipconfig_table)
+ return;
+
+ dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
+ if (dhcp) {
+ address = __connman_ipconfig_get_local(ipconfig);
+ if (!address)
+ return;
+
+ if (inet_pton(AF_INET, address, &addr) != 1)
+ connman_error("Could not convert address %s", address);
+
+ g_dhcp_client_decline(dhcp->dhcp_client, htonl(addr.s_addr));
+ }
+}
int __connman_dhcp_init(void)
{
- return connman_driver_register(&dhcp_driver);
+ DBG("");
+
+ ipconfig_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
+ NULL, NULL);
+
+ return 0;
}
void __connman_dhcp_cleanup(void)
{
- connman_driver_unregister(&dhcp_driver);
+ DBG("");
+
+ g_hash_table_destroy(ipconfig_table);
+ ipconfig_table = NULL;
}