Update WISPr command line client to use GWeb
[framework/connectivity/connman.git] / plugins / dhclient.c
index 342e6d7..e63e1dc 100644 (file)
@@ -2,7 +2,7 @@
  *
  *  Connection Manager
  *
- *  Copyright (C) 2007-2008  Intel Corporation. All rights reserved.
+ *  Copyright (C) 2007-2010  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
 #include <config.h>
 #endif
 
-#include <sys/wait.h>
-#include <glib/gstdio.h>
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+#include <arpa/inet.h>
 
+#include <glib.h>
+
+#define CONNMAN_API_SUBJECT_TO_CHANGE
 #include <connman/plugin.h>
-#include <connman/driver.h>
-#include <connman/dbus.h>
+#include <connman/utsname.h>
+#include <connman/dhcp.h>
+#include <connman/task.h>
 #include <connman/log.h>
 
-#include "inet.h"
-
-#define DHCLIENT_INTF "org.isc.dhclient"
-#define DHCLIENT_PATH "/org/isc/dhclient"
+static void dhclient_notify(struct connman_task *task,
+                                       DBusMessage *msg, void *user_data)
+{
+       struct connman_dhcp *dhcp = user_data;
+       DBusMessageIter iter, dict;
+       dbus_uint32_t pid;
+       const char *text, *key, *value;
 
-static const char *busname;
+       dbus_message_iter_init(msg, &iter);
 
-struct dhclient_task {
-       GPid pid;
-       int ifindex;
-       gchar *ifname;
-       struct connman_element *element;
-};
+       dbus_message_iter_get_basic(&iter, &pid);
+       dbus_message_iter_next(&iter);
 
-static GStaticMutex task_mutex = G_STATIC_MUTEX_INIT;
-static GSList *task_list = NULL;
+       dbus_message_iter_get_basic(&iter, &text);
+       dbus_message_iter_next(&iter);
 
-static struct dhclient_task *find_task_by_pid(GPid pid)
-{
-       GSList *list;
+       DBG("change %d to %s", pid, text);
 
-       for (list = task_list; list; list = list->next) {
-               struct dhclient_task *task = list->data;
+       dbus_message_iter_recurse(&iter, &dict);
 
-               if (task->pid == pid)
-                       return task;
-       }
+       while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
+               DBusMessageIter entry;
 
-       return NULL;
-}
+               dbus_message_iter_recurse(&dict, &entry);
+               dbus_message_iter_get_basic(&entry, &key);
+               dbus_message_iter_next(&entry);
+               dbus_message_iter_get_basic(&entry, &value);
 
-static struct dhclient_task *find_task_by_index(int index)
-{
-       GSList *list;
+               DBG("%s = %s", key, value);
 
-       for (list = task_list; list; list = list->next) {
-               struct dhclient_task *task = list->data;
+               if (g_strcmp0(key, "new_ip_address") == 0) {
+                       connman_dhcp_set_value(dhcp, "Address", value);
+               } else if (g_strcmp0(key, "new_subnet_mask") == 0) {
+                       connman_dhcp_set_value(dhcp, "Netmask", value);
+               } else if (g_strcmp0(key, "new_routers") == 0) {
+                       connman_dhcp_set_value(dhcp, "Gateway", value);
+               } else if (g_strcmp0(key, "new_network_number") == 0) {
+                       connman_dhcp_set_value(dhcp, "Network", value);
+               } else if (g_strcmp0(key, "new_broadcast_address") == 0) {
+                       connman_dhcp_set_value(dhcp, "Broadcast", value);
+               } else if (g_strcmp0(key, "new_domain_name_servers") == 0) {
+                       connman_dhcp_set_value(dhcp, "Nameserver", value);
+               } else if (g_ascii_strcasecmp(key, "new_domain_name") == 0) {
+                       connman_dhcp_set_value(dhcp, "Domainname", value);
+               } else if (g_ascii_strcasecmp(key, "new_domain_search") == 0) {
+               } else if (g_ascii_strcasecmp(key, "new_host_name") == 0) {
+                       connman_dhcp_set_value(dhcp, "Hostname", value);
+               } else if (g_ascii_strcasecmp(key, "new_ntp_servers") == 0) {
+                       connman_dhcp_set_value(dhcp, "Timeserver", value);
+               } else if (g_ascii_strcasecmp(key, "new_interface_mtu") == 0) {
+                       connman_dhcp_set_value(dhcp, "MTU", value);
+               } else if (g_ascii_strcasecmp(key, "new_proxy_auto_config") == 0) {
+                       connman_dhcp_set_value(dhcp, "PAC", value);
+               }
 
-               if (task->ifindex == index)
-                       return task;
+               dbus_message_iter_next(&dict);
        }
 
-       return NULL;
+       if (g_strcmp0(text, "PREINIT") == 0) {
+       } else if (g_strcmp0(text, "BOUND") == 0 ||
+                               g_strcmp0(text, "REBOOT") == 0) {
+               connman_dhcp_bound(dhcp);
+       } else if (g_strcmp0(text, "RENEW") == 0 ||
+                               g_strcmp0(text, "REBIND") == 0) {
+               connman_dhcp_renew(dhcp);
+       } else if (g_strcmp0(text, "FAIL") == 0) {
+               connman_dhcp_fail(dhcp);
+       } else {
+       }
 }
 
-static void kill_task(struct dhclient_task *task)
-{
-       DBG("task %p name %s pid %d", task, task->ifname, task->pid);
-
-       if (task->pid > 0)
-               kill(task->pid, SIGTERM);
-}
+struct dhclient_data {
+       struct connman_task *task;
+       struct connman_dhcp *dhcp;
+       char *ifname;
+};
 
-static void unlink_task(struct dhclient_task *task)
+static void dhclient_unlink(const char *ifname)
 {
-       gchar *pathname;
-
-       DBG("task %p name %s pid %d", task, task->ifname, task->pid);
+       char *pathname;
 
        pathname = g_strdup_printf("%s/dhclient.%s.pid",
-                                               STATEDIR, task->ifname);
-       g_unlink(pathname);
+                                               STATEDIR, ifname);
+       unlink(pathname);
        g_free(pathname);
 
        pathname = g_strdup_printf("%s/dhclient.%s.leases",
-                                               STATEDIR, task->ifname);
-       g_unlink(pathname);
+                                               STATEDIR, ifname);
+       unlink(pathname);
        g_free(pathname);
 }
 
-static void task_died(GPid pid, gint status, gpointer data)
+static void dhclient_died(struct connman_task *task, void *user_data)
 {
-       struct dhclient_task *task = data;
+       struct dhclient_data *dhclient = user_data;
 
-       if (WIFEXITED(status))
-               DBG("exit status %d for %s", WEXITSTATUS(status), task->ifname);
-       else
-               DBG("signal %d killed %s", WTERMSIG(status), task->ifname);
+       connman_dhcp_set_data(dhclient->dhcp, NULL);
 
-       g_spawn_close_pid(pid);
-       task->pid = 0;
+       connman_dhcp_unref(dhclient->dhcp);
 
-       g_static_mutex_lock(&task_mutex);
-       task_list = g_slist_remove(task_list, task);
-       g_static_mutex_unlock(&task_mutex);
+       connman_task_destroy(dhclient->task);
+       dhclient->task = NULL;
 
-       unlink_task(task);
+       dhclient_unlink(dhclient->ifname);
 
-       g_free(task->ifname);
-       g_free(task);
+       g_free(dhclient->ifname);
+       g_free(dhclient);
 }
 
-static void task_setup(gpointer data)
+static void dhclient_setup(struct connman_task *task, const char *ifname)
 {
-       struct dhclient_task *task = data;
-
-       DBG("task %p name %s", task, task->ifname);
+       const char *path, *intf = "org.moblin.connman.Task";
+
+       path = connman_task_get_path(task);
+
+       connman_task_add_argument(task, "-d", NULL);
+       connman_task_add_argument(task, "-q", NULL);
+       connman_task_add_argument(task, "-e", "BUSNAME=org.moblin.connman");
+       connman_task_add_argument(task, "-e", "BUSINTF=%s", intf);
+       connman_task_add_argument(task, "-e", "BUSPATH=%s", path);
+       connman_task_add_argument(task, "-pf", "%s/dhclient.%s.pid",
+                                                       STATEDIR, ifname);
+       connman_task_add_argument(task, "-lf", "%s/dhclient.%s.leases",
+                                                       STATEDIR, ifname);
+       connman_task_add_argument(task, "-cf", "%s/dhclient.conf", SCRIPTDIR);
+       connman_task_add_argument(task, "-sf", "%s/dhclient-script", SCRIPTDIR);
+       connman_task_add_argument(task, ifname, NULL);
+       connman_task_add_argument(task, "-n", NULL);
 }
 
-static int dhclient_probe(struct connman_element *element)
+static int dhclient_request(struct connman_dhcp *dhcp)
 {
-       struct dhclient_task *task;
-       char *argv[16], *envp[1], address[128], pidfile[PATH_MAX];
-       char leases[PATH_MAX], config[PATH_MAX], script[PATH_MAX];
+       struct dhclient_data *dhclient;
 
-       DBG("element %p name %s", element, element->name);
+       DBG("dhcp %p", dhcp);
 
-       task = g_try_new0(struct dhclient_task, 1);
-       if (task == NULL)
-               return -ENOMEM;
+       if (access(DHCLIENT, X_OK) < 0)
+               return -EIO;
 
-       task->ifindex = element->index;
-       task->ifname = inet_index2name(element->index);
-       task->element = element;
+       dhclient = g_try_new0(struct dhclient_data, 1);
+       if (dhclient == NULL)
+               return -ENOMEM;
 
-       if (task->ifname == NULL) {
-               g_free(task);
+       dhclient->task = connman_task_create(DHCLIENT);
+       if (dhclient->task == NULL) {
+               g_free(dhclient);
                return -ENOMEM;
        }
 
-       DBG("request %s", task->ifname);
-
-       snprintf(address, sizeof(address) - 1, "BUSNAME=%s", busname);
-       snprintf(pidfile, sizeof(pidfile) - 1,
-                       "%s/dhclient.%s.pid", STATEDIR, task->ifname);
-       snprintf(leases, sizeof(leases) - 1,
-                       "%s/dhclient.%s.leases", STATEDIR, task->ifname);
-       snprintf(config, sizeof(config) - 1, "%s/dhclient.conf", SCRIPTDIR);
-       snprintf(script, sizeof(script) - 1, "%s/dhclient-script", SCRIPTDIR);
-
-       argv[0] = DHCLIENT;
-       argv[1] = "-d";
-       argv[2] = "-q";
-       argv[3] = "-n";
-       argv[4] = "-e";
-       argv[5] = address;
-       argv[6] = "-pf";
-       argv[7] = pidfile;
-       argv[8] = "-lf";
-       argv[9] = leases;
-       argv[10] = "-cf";
-       argv[11] = config;
-       argv[12] = "-sf";
-       argv[13] = script;
-       argv[14] = task->ifname;
-       argv[15] = NULL;
-
-       envp[0] = NULL;
-
-       if (g_spawn_async(NULL, argv, envp, G_SPAWN_DO_NOT_REAP_CHILD,
-                               task_setup, task, &task->pid, NULL) == FALSE) {
-               connman_error("Failed to spawn dhclient");
-               return -1;
-       }
+       dhclient->dhcp = connman_dhcp_ref(dhcp);
+       dhclient->ifname = connman_dhcp_get_interface(dhcp);
 
-       g_static_mutex_lock(&task_mutex);
-       task_list = g_slist_append(task_list, task);
-       g_static_mutex_unlock(&task_mutex);
+       dhclient_setup(dhclient->task, dhclient->ifname);
 
-       g_child_watch_add(task->pid, task_died, task);
+       connman_dhcp_set_data(dhcp, dhclient);
 
-       DBG("executed %s with pid %d", DHCLIENT, task->pid);
+       connman_task_set_notify(dhclient->task, "Notify",
+                                               dhclient_notify, dhcp);
+
+       connman_task_run(dhclient->task, dhclient_died, dhclient,
+                                               NULL, NULL, NULL);
 
        return 0;
 }
 
-static void dhclient_remove(struct connman_element *element)
+static int dhclient_release(struct connman_dhcp *dhcp)
 {
-       struct dhclient_task *task;
-
-       DBG("element %p name %s", element, element->name);
+       struct dhclient_data *dhclient = connman_dhcp_get_data(dhcp);
 
-       g_static_mutex_lock(&task_mutex);
+       DBG("dhcp %p", dhcp);
 
-       task = find_task_by_index(element->index);
-       if (task != NULL)
-               task_list = g_slist_remove(task_list, task);
+       if (dhclient == NULL)
+               return -ESRCH;
 
-       g_static_mutex_unlock(&task_mutex);
+       connman_task_stop(dhclient->task);
 
-       if (task == NULL)
-               return;
-
-       DBG("release %s", task->ifname);
-
-       kill_task(task);
+       return 0;
 }
 
-static struct connman_driver dhclient_driver = {
+static struct connman_dhcp_driver dhclient_driver = {
        .name           = "dhclient",
-       .type           = CONNMAN_ELEMENT_TYPE_DHCP,
-       .probe          = dhclient_probe,
-       .remove         = dhclient_remove,
+       .priority       = CONNMAN_DHCP_PRIORITY_LOW,
+       .request        = dhclient_request,
+       .release        = dhclient_release,
 };
 
-static DBusHandlerResult dhclient_filter(DBusConnection *conn,
-                                               DBusMessage *msg, void *data)
-{
-       DBusMessageIter iter, dict;
-       dbus_uint32_t pid;
-       struct dhclient_task *task;
-       const char *text, *key, *value;
-
-       if (dbus_message_is_method_call(msg, DHCLIENT_INTF, "notify") == FALSE)
-               return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
-
-       dbus_message_iter_init(msg, &iter);
-
-       dbus_message_iter_get_basic(&iter, &pid);
-       dbus_message_iter_next(&iter);
-
-       dbus_message_iter_get_basic(&iter, &text);
-       dbus_message_iter_next(&iter);
-
-       DBG("change %d to %s", pid, text);
-
-       g_static_mutex_lock(&task_mutex);
-       task = find_task_by_pid(pid);
-       g_static_mutex_unlock(&task_mutex);
-
-       if (task == NULL)
-               return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
-
-       dbus_message_iter_recurse(&iter, &dict);
-
-       while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
-               DBusMessageIter entry;
-
-               dbus_message_iter_recurse(&dict, &entry);
-               dbus_message_iter_get_basic(&entry, &key);
-               dbus_message_iter_next(&entry);
-               dbus_message_iter_get_basic(&entry, &value);
-
-               DBG("%s = %s", key, value);
-
-               if (g_ascii_strcasecmp(key, "new_ip_address") == 0) {
-                       g_free(task->element->ipv4.address);
-                       task->element->ipv4.address = g_strdup(value);
-               }
-
-               if (g_ascii_strcasecmp(key, "new_subnet_mask") == 0) {
-                       g_free(task->element->ipv4.netmask);
-                       task->element->ipv4.netmask = g_strdup(value);
-               }
-
-               if (g_ascii_strcasecmp(key, "new_routers") == 0) {
-                       g_free(task->element->ipv4.gateway);
-                       task->element->ipv4.gateway = g_strdup(value);
-               }
-
-               if (g_ascii_strcasecmp(key, "new_network_number") == 0) {
-                       g_free(task->element->ipv4.network);
-                       task->element->ipv4.network = g_strdup(value);
-               }
-
-               if (g_ascii_strcasecmp(key, "new_broadcast_address") == 0) {
-                       g_free(task->element->ipv4.broadcast);
-                       task->element->ipv4.broadcast = g_strdup(value);
-               }
-
-               if (g_ascii_strcasecmp(key, "new_domain_name_servers") == 0) {
-                       g_free(task->element->ipv4.nameserver);
-                       task->element->ipv4.nameserver = g_strdup(value);
-               }
-
-               dbus_message_iter_next(&dict);
-       }
-
-       if (g_ascii_strcasecmp(text, "PREINIT") == 0) {
-       } else if (g_ascii_strcasecmp(text, "BOUND") == 0 ||
-                               g_ascii_strcasecmp(text, "REBOOT") == 0) {
-               struct connman_element *element;
-               element = connman_element_create(NULL);
-               element->type = CONNMAN_ELEMENT_TYPE_IPV4;
-               element->index = task->ifindex;
-               connman_element_update(task->element);
-               connman_element_register(element, task->element);
-       } else if (g_ascii_strcasecmp(text, "RENEW") == 0 ||
-                               g_ascii_strcasecmp(text, "REBIND") == 0) {
-               connman_element_update(task->element);
-       } else {
-       }
-
-       return DBUS_HANDLER_RESULT_HANDLED;
-}
-
-static DBusConnection *connection;
-
 static int dhclient_init(void)
 {
-       gchar *filter;
-       int err;
-
-       connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
-
-       busname = dbus_bus_get_unique_name(connection);
-       busname = CONNMAN_SERVICE;
-
-       dbus_connection_add_filter(connection, dhclient_filter, NULL, NULL);
-
-       filter = g_strdup_printf("interface=%s,path=%s",
-                                               DHCLIENT_INTF, DHCLIENT_PATH);
-
-       dbus_bus_add_match(connection, filter, NULL);
-
-       g_free(filter);
-
-       err = connman_driver_register(&dhclient_driver);
-       if (err < 0) {
-               dbus_connection_unref(connection);
-               return err;
-       }
-
-       return 0;
+       return connman_dhcp_driver_register(&dhclient_driver);
 }
 
 static void dhclient_exit(void)
 {
-       GSList *list;
-
-       g_static_mutex_lock(&task_mutex);
-
-       for (list = task_list; list; list = list->next) {
-               struct dhclient_task *task = list->data;
-
-               DBG("killing process %d", task->pid);
-
-               kill_task(task);
-               unlink_task(task);
-       }
-
-       g_static_mutex_unlock(&task_mutex);
-
-       g_slist_free(task_list);
-
-       connman_driver_unregister(&dhclient_driver);
-
-       dbus_connection_unref(connection);
+       connman_dhcp_driver_unregister(&dhclient_driver);
 }
 
-CONNMAN_PLUGIN_DEFINE("dhclient", "ISC DHCP client plugin", VERSION,
-                                               dhclient_init, dhclient_exit)
+CONNMAN_PLUGIN_DEFINE(dhclient, "ISC DHCP client plugin", VERSION,
+               CONNMAN_PLUGIN_PRIORITY_DEFAULT, dhclient_init, dhclient_exit)