#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include <connman/ipconfig.h>
+
+#include <gdhcp/gdhcp.h>
#include <glib.h>
#include "connman.h"
struct connman_dhcp {
- gint refcount;
- int index;
- enum connman_dhcp_state state;
+ struct connman_network *network;
+ dhcp_cb callback;
- struct connman_element *element;
+ char **nameservers;
+ char *timeserver;
+ char *pac;
- struct connman_dhcp_driver *driver;
- void *driver_data;
+ GDHCPClient *dhcp_client;
};
-/**
- * connman_dhcp_ref:
- * @dhcp: DHCP structure
- *
- * Increase reference counter of DHCP
- */
-struct connman_dhcp *connman_dhcp_ref(struct connman_dhcp *dhcp)
+static GHashTable *network_table;
+
+static void dhcp_free(struct connman_dhcp *dhcp)
{
- g_atomic_int_inc(&dhcp->refcount);
+ g_strfreev(dhcp->nameservers);
+ g_free(dhcp->timeserver);
+ g_free(dhcp->pac);
- return dhcp;
+ dhcp->nameservers = NULL;
+ dhcp->timeserver = NULL;
+ dhcp->pac = NULL;
}
-/**
- * connman_dhcp_unref:
- * @dhcp: DHCP structure
- *
- * Decrease reference counter of DHCP
- */
-void connman_dhcp_unref(struct connman_dhcp *dhcp)
+static void dhcp_invalid(struct connman_dhcp *dhcp)
{
- if (g_atomic_int_dec_and_test(&dhcp->refcount) == TRUE)
- g_free(dhcp);
-}
+ struct connman_service *service;
+ struct connman_ipconfig *ipconfig;
+ int i;
-/**
- * connman_dhcp_get_index:
- * @dhcp: DHCP structure
- *
- * Get network index of DHCP
- */
-int connman_dhcp_get_index(struct connman_dhcp *dhcp)
-{
- return dhcp->index;
+ service = __connman_service_lookup_from_network(dhcp->network);
+ if (service == NULL)
+ return;
+
+ ipconfig = __connman_service_get_ip4config(service);
+ 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);
+
+ for (i = 0; dhcp->nameservers[i] != NULL; i++) {
+ __connman_service_nameserver_remove(service,
+ dhcp->nameservers[i]);
+ }
+
+ __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)
+ dhcp->callback(dhcp->network, FALSE);
+
+ dhcp_free(dhcp);
}
-/**
- * connman_dhcp_get_interface:
- * @dhcp: DHCP structure
- *
- * Get network interface of DHCP
- */
-char *connman_dhcp_get_interface(struct connman_dhcp *dhcp)
+static void dhcp_valid(struct connman_dhcp *dhcp)
{
- return connman_inet_ifname(dhcp->index);
+ if (dhcp->callback != NULL)
+ dhcp->callback(dhcp->network, TRUE);
}
-/**
- * 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 no_lease_cb(GDHCPClient *dhcp_client, gpointer user_data)
{
- char **nameservers;
+ struct connman_dhcp *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) {
- __connman_utsname_set_domainname(value);
- } else if (g_strcmp0(key, "Hostname") == 0) {
- __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);
- }
+ DBG("No lease available");
+
+ dhcp_invalid(dhcp);
}
-/**
- * connman_dhcp_bound:
- * @dhcp: DHCP structure
- *
- * Report successful bound of the interface
- */
-void connman_dhcp_bound(struct connman_dhcp *dhcp)
+static void lease_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
{
- struct connman_element *element;
+ struct connman_dhcp *dhcp = user_data;
- DBG("dhcp %p", dhcp);
+ DBG("Lease lost");
- element = connman_element_create(NULL);
- if (element == NULL)
- return;
+ dhcp_invalid(dhcp);
+}
- element->type = CONNMAN_ELEMENT_TYPE_IPV4;
- element->index = dhcp->index;
+static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
+{
+ struct connman_dhcp *dhcp = user_data;
- connman_element_update(dhcp->element);
+ DBG("Lease lost");
- if (connman_element_register(element, dhcp->element) < 0)
- connman_element_unref(element);
+ dhcp_invalid(dhcp);
}
-/**
- * connman_dhcp_renew:
- * @dhcp: DHCP structure
- *
- * Report successful renew of the interface
- */
-void connman_dhcp_renew(struct connman_dhcp *dhcp)
+
+static gboolean compare_string_arrays(char **array_a, char **array_b)
{
- DBG("dhcp %p", dhcp);
+ int i;
- connman_element_update(dhcp->element);
-}
+ if (array_a == NULL || array_b == NULL)
+ return FALSE;
-/**
- * connman_dhcp_fail:
- * @dhcp: DHCP structure
- *
- * Report DHCP failure of the interface
- */
-void connman_dhcp_fail(struct connman_dhcp *dhcp)
-{
- DBG("dhcp %p", dhcp);
+ if (g_strv_length(array_a) != g_strv_length(array_b))
+ return FALSE;
- connman_element_set_error(dhcp->element,
- CONNMAN_ELEMENT_ERROR_FAILED);
-}
+ for (i = 0; array_a[i] != NULL &&
+ array_b[i] != NULL; i++) {
+ if (g_strcmp0(array_a[i], array_b[i]) != 0)
+ return FALSE;
+ }
-/**
- * connman_dhcp_get_data:
- * @dhcp: DHCP structure
- *
- * Get private DHCP data pointer
- */
-void *connman_dhcp_get_data(struct connman_dhcp *dhcp)
-{
- return dhcp->driver_data;
+ 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 void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
{
- dhcp->driver_data = data;
-}
+ struct connman_dhcp *dhcp = user_data;
+ GList *list, *option = 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, c_prefixlen;
+ gboolean ip_change;
+ int i;
+
+ DBG("Lease available");
+
+ service = __connman_service_lookup_from_network(dhcp->network);
+ if (service == NULL) {
+ connman_error("Can not lookup service");
+ return;
+ }
-static GSList *driver_list = NULL;
+ ipconfig = __connman_service_get_ip4config(service);
+ if (ipconfig == NULL) {
+ connman_error("Could not lookup ipconfig");
+ return;
+ }
-static gint compare_priority(gconstpointer a, gconstpointer b)
-{
- const struct connman_dhcp_driver *driver1 = a;
- const struct connman_dhcp_driver *driver2 = b;
+ 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);
+ if (option != NULL)
+ netmask = g_strdup(option->data);
+
+ option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
+ 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;
+ nameservers = g_try_new0(char *, ns_entries + 1);
+ if (nameservers != NULL) {
+ 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 != NULL)
+ domainname = g_strdup(option->data);
+
+ option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
+ if (option != NULL)
+ 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);
- return driver2->priority - driver1->priority;
+ 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);
+
+ 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_set_domainname(service, domainname);
+
+ if (domainname != NULL)
+ __connman_utsname_set_domainname(domainname);
+
+ if (hostname != NULL)
+ __connman_utsname_set_hostname(hostname);
+
+ if (ip_change == TRUE)
+ dhcp_valid(dhcp);
+
+ __connman_6to4_probe(service);
+
+ g_free(address);
+ g_free(netmask);
+ g_free(gateway);
+ g_free(domainname);
+ g_free(hostname);
}
-/**
- * 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 void ipv4ll_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
{
- DBG("driver %p name %s", driver, driver->name);
+ struct connman_dhcp *dhcp = user_data;
+ char *address, *netmask;
+ struct connman_service *service;
+ struct connman_ipconfig *ipconfig;
+ unsigned char prefixlen;
- driver_list = g_slist_insert_sorted(driver_list, driver,
- compare_priority);
+ DBG("IPV4LL available");
- return 0;
+ service = __connman_service_lookup_from_network(dhcp->network);
+ if (service == NULL)
+ return;
+
+ ipconfig = __connman_service_get_ip4config(service);
+ if (ipconfig == NULL)
+ return;
+
+ address = g_dhcp_client_get_address(dhcp_client);
+ netmask = g_dhcp_client_get_netmask(dhcp_client);
+
+ prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
+
+ 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);
+
+ dhcp_valid(dhcp);
+
+ g_free(address);
+ g_free(netmask);
}
-/**
- * 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 void dhcp_debug(const char *str, void *data)
{
- DBG("driver %p name %s", driver, driver->name);
-
- driver_list = g_slist_remove(driver_list, driver);
+ connman_info("%s: %s\n", (const char *) data, str);
}
-static int dhcp_probe(struct connman_element *element)
+static int dhcp_request(struct connman_dhcp *dhcp)
{
- struct connman_dhcp *dhcp;
- GSList *list;
+ GDHCPClient *dhcp_client;
+ GDHCPClientError error;
+ const char *hostname;
+ int index;
- DBG("element %p name %s", element, element->name);
+ DBG("dhcp %p", dhcp);
- dhcp = g_try_new0(struct connman_dhcp, 1);
- if (dhcp == NULL)
- return -ENOMEM;
+ index = connman_network_get_index(dhcp->network);
- dhcp->refcount = 1;
- dhcp->index = element->index;
- dhcp->state = CONNMAN_DHCP_STATE_IDLE;
+ dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
+ if (error != G_DHCP_CLIENT_ERROR_NONE)
+ return -EINVAL;
- dhcp->element = element;
+ if (getenv("CONNMAN_DHCP_DEBUG"))
+ g_dhcp_client_set_debug(dhcp_client, dhcp_debug, "DHCP");
- connman_element_set_data(element, dhcp);
+ hostname = connman_utsname_get_hostname();
+ if (hostname != NULL)
+ g_dhcp_client_set_send(dhcp_client, G_DHCP_HOST_NAME, hostname);
- for (list = driver_list; list; list = list->next) {
- struct connman_dhcp_driver *driver = list->data;
+ g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
+ g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
+ 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, G_DHCP_ROUTER);
+ g_dhcp_client_set_request(dhcp_client, 252);
- DBG("driver %p name %s", driver, driver->name);
+ g_dhcp_client_register_event(dhcp_client,
+ G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
+ lease_available_cb, dhcp);
- if (driver->request(dhcp) == 0) {
- dhcp->driver = driver;
- break;
- }
- }
+ g_dhcp_client_register_event(dhcp_client,
+ G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE,
+ ipv4ll_available_cb, dhcp);
- if (dhcp->driver == NULL) {
- connman_dhcp_unref(dhcp);
- return -ENOENT;
- }
+ g_dhcp_client_register_event(dhcp_client,
+ G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp);
- return 0;
+ g_dhcp_client_register_event(dhcp_client,
+ G_DHCP_CLIENT_EVENT_IPV4LL_LOST, ipv4ll_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 g_dhcp_client_start(dhcp_client);
}
-static void dhcp_remove(struct connman_element *element)
+static int dhcp_release(struct connman_dhcp *dhcp)
{
- struct connman_dhcp *dhcp = connman_element_get_data(element);
+ DBG("dhcp %p", dhcp);
- DBG("element %p name %s", element, element->name);
+ if (dhcp->dhcp_client == NULL)
+ return 0;
- connman_element_set_data(element, NULL);
+ g_dhcp_client_stop(dhcp->dhcp_client);
+ g_dhcp_client_unref(dhcp->dhcp_client);
- if (dhcp->driver) {
- dhcp->driver->release(dhcp);
- dhcp->driver = NULL;
- }
+ dhcp->dhcp_client = NULL;
- connman_dhcp_unref(dhcp);
+ return 0;
}
-static void dhcp_change(struct connman_element *element)
+static void remove_network(gpointer user_data)
{
- DBG("element %p name %s", element, element->name);
+ struct connman_dhcp *dhcp = user_data;
+
+ DBG("dhcp %p", dhcp);
- if (element->state == CONNMAN_ELEMENT_STATE_ERROR)
- connman_element_set_error(element->parent,
- CONNMAN_ELEMENT_ERROR_DHCP_FAILED);
+ dhcp_release(dhcp);
+
+ dhcp_free(dhcp);
+ g_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,
-};
+int __connman_dhcp_start(struct connman_network *network, dhcp_cb callback)
+{
+ struct connman_dhcp *dhcp;
+
+ DBG("");
+
+ dhcp = g_try_new0(struct connman_dhcp, 1);
+ if (dhcp == NULL)
+ return -ENOMEM;
+
+ dhcp->network = network;
+ dhcp->callback = callback;
+
+ g_hash_table_replace(network_table, network, dhcp);
+
+ return dhcp_request(dhcp);
+}
+
+void __connman_dhcp_stop(struct connman_network *network)
+{
+ DBG("");
+
+ g_hash_table_remove(network_table, network);
+}
int __connman_dhcp_init(void)
{
- return connman_driver_register(&dhcp_driver);
+ DBG("");
+
+ network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
+ NULL, remove_network);
+
+ return 0;
}
void __connman_dhcp_cleanup(void)
{
- connman_driver_unregister(&dhcp_driver);
+ DBG("");
+
+ g_hash_table_destroy(network_table);
}