diff options
Diffstat (limited to 'src/dhcp.c')
-rw-r--r-- | src/dhcp.c | 316 |
1 files changed, 170 insertions, 146 deletions
@@ -27,100 +27,76 @@ #include <string.h> #include <stdlib.h> +#include <connman/ipconfig.h> + #include <gdhcp/gdhcp.h> #include <glib.h> #include "connman.h" -enum connman_dhcp_state { - CONNMAN_DHCP_STATE_UNKNOWN = 0, - CONNMAN_DHCP_STATE_IDLE = 1, - CONNMAN_DHCP_STATE_BOUND = 2, - CONNMAN_DHCP_STATE_RENEW = 3, - CONNMAN_DHCP_STATE_FAIL = 4, -}; - struct connman_dhcp { - GDHCPClient *dhcp_client; + struct connman_network *network; + dhcp_cb callback; - int index; - enum connman_dhcp_state state; + char **nameservers; + char *timeserver; + char *pac; - struct connman_element *element; + GDHCPClient *dhcp_client; }; -static void dhcp_set_value(struct connman_dhcp *dhcp, - const char *key, const char *value) +static GHashTable *network_table; + +static void dhcp_free(struct connman_dhcp *dhcp) { - char **nameservers; + g_strfreev(dhcp->nameservers); + g_free(dhcp->timeserver); + g_free(dhcp->pac); - 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) { - connman_info("Timeserver %s", value); - - 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); - } + dhcp->nameservers = NULL; + dhcp->timeserver = NULL; + dhcp->pac = NULL; } -static void dhcp_bound(struct connman_dhcp *dhcp) +static void dhcp_invalid(struct connman_dhcp *dhcp) { - struct connman_element *element; + struct connman_service *service; + struct connman_ipconfig *ipconfig; + int i; - DBG("dhcp %p", dhcp); + service = __connman_service_lookup_from_network(dhcp->network); + if (service == NULL) + return; - element = connman_element_create(NULL); - if (element == NULL) + ipconfig = __connman_service_get_ip4config(service); + if (ipconfig == NULL) return; - element->type = CONNMAN_ELEMENT_TYPE_IPV4; - element->index = dhcp->index; + __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); - connman_element_update(dhcp->element); + if (dhcp->callback != NULL) + dhcp->callback(dhcp->network, FALSE); - if (connman_element_register(element, dhcp->element) < 0) - connman_element_unref(element); + dhcp_free(dhcp); +} + +static void dhcp_valid(struct connman_dhcp *dhcp) +{ + if (dhcp->callback != NULL) + dhcp->callback(dhcp->network, TRUE); } static void no_lease_cb(GDHCPClient *dhcp_client, gpointer user_data) @@ -129,13 +105,16 @@ static void no_lease_cb(GDHCPClient *dhcp_client, gpointer user_data) DBG("No lease available"); - connman_element_set_error(dhcp->element, - CONNMAN_ELEMENT_ERROR_FAILED); + dhcp_invalid(dhcp); } static void lease_lost_cb(GDHCPClient *dhcp_client, gpointer user_data) { + struct connman_dhcp *dhcp = user_data; + DBG("Lease lost"); + + dhcp_invalid(dhcp); } static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data) @@ -144,85 +123,134 @@ static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data) DBG("Lease lost"); - connman_element_unregister_children(dhcp->element); + dhcp_invalid(dhcp); } static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data) { struct connman_dhcp *dhcp = user_data; GList *list, *option = NULL; - char *address, *nameservers; - size_t ns_strlen = 0; + char *address, *netmask = NULL, *gateway = NULL, *net = NULL; + char *domainname = NULL, *hostname = NULL; + int ns_entries; + struct connman_ipconfig *ipconfig; + struct connman_service *service; + unsigned char prefixlen; + int i; DBG("Lease available"); + service = __connman_service_lookup_from_network(dhcp->network); + if (service == NULL) { + connman_error("Can not lookup service"); + return; + } + + ipconfig = __connman_service_get_ip4config(service); + if (ipconfig == NULL) { + connman_error("Could not lookup ipconfig"); + return; + } + address = g_dhcp_client_get_address(dhcp_client); - if (address != NULL) - dhcp_set_value(dhcp, "Address", address); - g_free(address); option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET); if (option != NULL) - dhcp_set_value(dhcp, "Netmask", option->data); + netmask = g_strdup(option->data); + + option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER); + if (option != NULL) + gateway = g_strdup(option->data); option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER); - for (list = option; list; list = list->next) - ns_strlen += strlen((char *) list->data) + 2; - nameservers = g_try_malloc0(ns_strlen); - if (nameservers) { - char *ns_index = nameservers; - - for (list = option; list; list = list->next) { - sprintf(ns_index, "%s ", (char *) list->data); - ns_index += strlen((char *) list->data) + 1; - } - - dhcp_set_value(dhcp, "Nameserver", nameservers); + 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) { + for (i = 0, list = option; list; list = list->next) + dhcp->nameservers[i] = g_strdup(list->data); + dhcp->nameservers[ns_entries] = NULL; } - g_free(nameservers); option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME); if (option != NULL) - dhcp_set_value(dhcp, "Domainname", option->data); - - option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER); - if (option != NULL) - dhcp_set_value(dhcp, "Gateway", option->data); + domainname = g_strdup(option->data); option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME); if (option != NULL) - dhcp_set_value(dhcp, "Hostname", option->data); + hostname = g_strdup(option->data); option = g_dhcp_client_get_option(dhcp_client, G_DHCP_NTP_SERVER); if (option != NULL) - dhcp_set_value(dhcp, "Timeserver", option->data); + dhcp->timeserver = g_strdup(option->data); option = g_dhcp_client_get_option(dhcp_client, 252); if (option != NULL) - dhcp_set_value(dhcp, "PAC", option->data); + dhcp->pac = g_strdup(option->data); + + 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, gateway); + + for (i = 0; dhcp->nameservers[i] != NULL; i++) { + __connman_service_nameserver_append(service, + dhcp->nameservers[i]); + } + __connman_service_timeserver_append(service, dhcp->timeserver); + __connman_service_set_pac(service, dhcp->pac); + __connman_service_set_domainname(service, domainname); - dhcp_bound(dhcp); + if (domainname != NULL) + __connman_utsname_set_domainname(domainname); + + if (hostname != NULL) + __connman_utsname_set_hostname(hostname); + + dhcp_valid(dhcp); + + g_free(address); + g_free(netmask); + g_free(gateway); + g_free(net); + g_free(domainname); + g_free(hostname); } static void ipv4ll_available_cb(GDHCPClient *dhcp_client, gpointer user_data) { struct connman_dhcp *dhcp = user_data; char *address, *netmask; + struct connman_service *service; + struct connman_ipconfig *ipconfig; + unsigned char prefixlen; DBG("IPV4LL available"); - address = g_dhcp_client_get_address(dhcp_client); - if (address != NULL) - dhcp_set_value(dhcp, "Address", address); + 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); - if (netmask != NULL) - dhcp_set_value(dhcp, "Netmask", netmask); + + 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); - - dhcp_bound(dhcp); } static void dhcp_debug(const char *str, void *data) @@ -239,7 +267,7 @@ static int dhcp_request(struct connman_dhcp *dhcp) DBG("dhcp %p", dhcp); - index = dhcp->index; + index = connman_network_get_index(dhcp->network); dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error); if (error != G_DHCP_CLIENT_ERROR_NONE) @@ -292,66 +320,62 @@ static int dhcp_release(struct connman_dhcp *dhcp) return 0; } -static int dhcp_probe(struct connman_element *element) +static void remove_network(gpointer user_data) +{ + struct connman_dhcp *dhcp = user_data; + + DBG("dhcp %p", dhcp); + + dhcp_free(dhcp); + g_free(dhcp); +} + +int __connman_dhcp_start(struct connman_network *network, dhcp_cb callback) { struct connman_dhcp *dhcp; - DBG("element %p name %s", element, element->name); + DBG(""); dhcp = g_try_new0(struct connman_dhcp, 1); if (dhcp == NULL) return -ENOMEM; - dhcp->index = element->index; - dhcp->state = CONNMAN_DHCP_STATE_IDLE; - - dhcp->element = element; + dhcp->network = network; + dhcp->callback = callback; - connman_element_set_data(element, dhcp); + g_hash_table_replace(network_table, network, dhcp); - dhcp_request(dhcp); - - return 0; + return dhcp_request(dhcp); } -static void dhcp_remove(struct connman_element *element) +void __connman_dhcp_stop(struct connman_network *network) { - struct connman_dhcp *dhcp = connman_element_get_data(element); + struct connman_dhcp *dhcp; - DBG("element %p name %s", element, element->name); + DBG(""); - connman_element_set_data(element, NULL); + dhcp = g_hash_table_lookup(network_table, network); + if (dhcp == NULL) + return; dhcp_release(dhcp); - g_free(dhcp); - connman_element_unref(element); + g_hash_table_remove(network_table, network); } -static void dhcp_change(struct connman_element *element) +int __connman_dhcp_init(void) { - DBG("element %p name %s", element, element->name); + DBG(""); - if (element->state == CONNMAN_ELEMENT_STATE_ERROR) - connman_element_set_error(element->parent, - CONNMAN_ELEMENT_ERROR_DHCP_FAILED); -} - -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, -}; + network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal, + NULL, remove_network); -int __connman_dhcp_init(void) -{ - return connman_driver_register(&dhcp_driver); + return 0; } void __connman_dhcp_cleanup(void) { - connman_driver_unregister(&dhcp_driver); + DBG(""); + + g_hash_table_destroy(network_table); } |