+struct connman_dhcp {
+ struct connman_network *network;
+ dhcp_cb callback;
+
+ char **nameservers;
+ char **timeservers;
+ char *pac;
+
+ GDHCPClient *dhcp_client;
+};
+
+static GHashTable *network_table;
+
+static void dhcp_free(struct connman_dhcp *dhcp)
+{
+ g_strfreev(dhcp->nameservers);
+ g_strfreev(dhcp->timeservers);
+ g_free(dhcp->pac);
+
+ dhcp->nameservers = NULL;
+ dhcp->timeservers = NULL;
+ dhcp->pac = NULL;
+}
+
+/**
+ * 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)
+ goto out;
+
+ ipconfig = __connman_service_get_ip4config(service);
+ if (ipconfig == NULL)
+ goto out;
+
+ __connman_6to4_remove(ipconfig);
+
+ __connman_service_set_domainname(service, NULL);
+ __connman_service_set_pac(service, NULL);
+
+ 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 && callback)
+ dhcp->callback(dhcp->network, FALSE);
+
+out:
+ 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)
+{
+ struct connman_dhcp *dhcp = user_data;
+
+ DBG("No lease available");
+
+ dhcp_invalidate(dhcp, TRUE);
+}
+
+static void lease_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
+{
+ struct connman_dhcp *dhcp = user_data;
+
+ DBG("Lease lost");
+
+ dhcp_invalidate(dhcp, TRUE);
+}
+
+static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
+{
+ struct connman_dhcp *dhcp = user_data;
+
+ DBG("Lease lost");
+
+ 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;
+ 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, 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;
+ }
+
+ ipconfig = __connman_service_get_ip4config(service);
+ if (ipconfig == NULL) {
+ connman_error("Could not lookup ipconfig");
+ 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);
+
+ 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);
+ 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++)
+ 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);