Update WISPr command line client to use GWeb
[framework/connectivity/connman.git] / plugins / dhclient.c
index cc5efd8..e63e1dc 100644 (file)
@@ -2,7 +2,7 @@
  *
  *  Connection Manager
  *
- *  Copyright (C) 2007  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 <stdio.h>
 #include <errno.h>
 #include <unistd.h>
-#include <stdlib.h>
-#include <signal.h>
-#include <string.h>
-#include <sys/ioctl.h>
-#include <sys/socket.h>
 #include <arpa/inet.h>
-#include <net/if.h>
 
 #include <glib.h>
-#include <gdbus.h>
 
+#define CONNMAN_API_SUBJECT_TO_CHANGE
 #include <connman/plugin.h>
+#include <connman/utsname.h>
 #include <connman/dhcp.h>
+#include <connman/task.h>
+#include <connman/log.h>
 
-static const char *busname;
+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;
 
-struct dhclient_task {
-       GPid pid;
-       int ifindex;
-       char *ifname;
-       struct connman_iface *iface;
-};
+       dbus_message_iter_init(msg, &iter);
 
-static GSList *tasks = NULL;
+       dbus_message_iter_get_basic(&iter, &pid);
+       dbus_message_iter_next(&iter);
 
-static struct dhclient_task *find_task_by_pid(GPid pid)
-{
-       GSList *list;
+       dbus_message_iter_get_basic(&iter, &text);
+       dbus_message_iter_next(&iter);
 
-       for (list = tasks; list; list = list->next) {
-               struct dhclient_task *task = list->data;
+       DBG("change %d to %s", pid, text);
 
-               if (task->pid == pid)
-                       return task;
-       }
+       dbus_message_iter_recurse(&iter, &dict);
 
-       return NULL;
-}
+       while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
+               DBusMessageIter entry;
 
-static struct dhclient_task *find_task_by_index(int index)
-{
-       GSList *list;
+               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);
 
-       for (list = tasks; list; list = list->next) {
-               struct dhclient_task *task = list->data;
+               DBG("%s = %s", key, value);
+
+               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)
-{
-       char pathname[PATH_MAX];
+struct dhclient_data {
+       struct connman_task *task;
+       struct connman_dhcp *dhcp;
+       char *ifname;
+};
 
-       kill(task->pid, SIGTERM);
+static void dhclient_unlink(const char *ifname)
+{
+       char *pathname;
 
-       snprintf(pathname, sizeof(pathname) - 1,
-                       "%s/dhclient.%s.pid", STATEDIR, task->ifname);
+       pathname = g_strdup_printf("%s/dhclient.%s.pid",
+                                               STATEDIR, ifname);
        unlink(pathname);
+       g_free(pathname);
 
-       snprintf(pathname, sizeof(pathname) - 1,
-                       "%s/dhclient.%s.leases", STATEDIR, task->ifname);
+       pathname = g_strdup_printf("%s/dhclient.%s.leases",
+                                               STATEDIR, ifname);
        unlink(pathname);
+       g_free(pathname);
+}
+
+static void dhclient_died(struct connman_task *task, void *user_data)
+{
+       struct dhclient_data *dhclient = user_data;
 
-       free(task->ifname);
+       connman_dhcp_set_data(dhclient->dhcp, NULL);
 
-       g_free(task);
+       connman_dhcp_unref(dhclient->dhcp);
+
+       connman_task_destroy(dhclient->task);
+       dhclient->task = NULL;
+
+       dhclient_unlink(dhclient->ifname);
+
+       g_free(dhclient->ifname);
+       g_free(dhclient);
 }
 
-static int dhclient_request(struct connman_iface *iface)
+static void dhclient_setup(struct connman_task *task, const char *ifname)
 {
-       struct ifreq ifr;
-       struct dhclient_task *task;
-       char *argv[16], address[128], pidfile[PATH_MAX];
-       char leases[PATH_MAX], config[PATH_MAX], script[PATH_MAX];
-       int sk, err;
-
-       sk = socket(PF_INET, SOCK_DGRAM, 0);
-       if (sk < 0)
-               return -EIO;
-
-       memset(&ifr, 0, sizeof(ifr));
-       ifr.ifr_ifindex = iface->index;
+       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);
+}
 
-       err = ioctl(sk, SIOCGIFNAME, &ifr);
+static int dhclient_request(struct connman_dhcp *dhcp)
+{
+       struct dhclient_data *dhclient;
 
-       close(sk);
+       DBG("dhcp %p", dhcp);
 
-       if (err < 0)
+       if (access(DHCLIENT, X_OK) < 0)
                return -EIO;
 
-       task = g_try_new0(struct dhclient_task, 1);
-       if (task == NULL)
+       dhclient = g_try_new0(struct dhclient_data, 1);
+       if (dhclient == NULL)
                return -ENOMEM;
 
-       task->ifindex = iface->index;
-       task->ifname = strdup(ifr.ifr_name);
-       task->iface = iface;
-
-       if (task->ifname == NULL) {
-               g_free(task);
+       dhclient->task = connman_task_create(DHCLIENT);
+       if (dhclient->task == NULL) {
+               g_free(dhclient);
                return -ENOMEM;
        }
 
-       printf("[DHCP] request %s\n", 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] = "/sbin/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;
-
-       if (g_spawn_async(NULL, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD,
-                               NULL, NULL, &task->pid, NULL) == FALSE) {
-               printf("Failed to spawn dhclient\n");
-               return -1;
-       }
+       dhclient->dhcp = connman_dhcp_ref(dhcp);
+       dhclient->ifname = connman_dhcp_get_interface(dhcp);
 
-       tasks = g_slist_append(tasks, task);
+       dhclient_setup(dhclient->task, dhclient->ifname);
 
-       printf("[DHCP] executed with pid %d\n", task->pid);
+       connman_dhcp_set_data(dhcp, dhclient);
+
+       connman_task_set_notify(dhclient->task, "Notify",
+                                               dhclient_notify, dhcp);
+
+       connman_task_run(dhclient->task, dhclient_died, dhclient,
+                                               NULL, NULL, NULL);
 
        return 0;
 }
 
-static int dhclient_release(struct connman_iface *iface)
+static int dhclient_release(struct connman_dhcp *dhcp)
 {
-       struct dhclient_task *task;
-
-       task = find_task_by_index(iface->index);
-       if (task == NULL)
-               return -ENODEV;
+       struct dhclient_data *dhclient = connman_dhcp_get_data(dhcp);
 
-       printf("[DHCP] release %s\n", task->ifname);
+       DBG("dhcp %p", dhcp);
 
-       tasks = g_slist_remove(tasks, task);
+       if (dhclient == NULL)
+               return -ESRCH;
 
-       kill_task(task);
+       connman_task_stop(dhclient->task);
 
        return 0;
 }
 
 static struct connman_dhcp_driver dhclient_driver = {
        .name           = "dhclient",
+       .priority       = CONNMAN_DHCP_PRIORITY_LOW,
        .request        = dhclient_request,
        .release        = dhclient_release,
 };
 
-static DBusMessage *notify_method(DBusConnection *conn,
-                                       DBusMessage *msg, void *data)
+static int dhclient_init(void)
 {
-       DBusMessageIter iter, dict;
-       dbus_uint32_t pid;
-       struct dhclient_task *task;
-       struct connman_ipv4 ipv4;
-       const char *text, *key, *value;
-
-       memset(&ipv4, 0, sizeof(ipv4));
-
-       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);
-
-       printf("[DHCP] change %d to %s\n", pid, text);
-
-       task = find_task_by_pid(pid);
-       if (task == NULL)
-               return NULL;
-
-       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);
-
-               printf("[DHCP] %s = %s\n", key, value);
-
-               if (strcmp(key, "new_ip_address") == 0)
-                       inet_aton(value, &ipv4.address);
-
-               if (strcmp(key, "new_subnet_mask") == 0)
-                       inet_aton(value, &ipv4.netmask);
-
-               if (strcmp(key, "new_routers") == 0)
-                       inet_aton(value, &ipv4.gateway);
-
-               if (strcmp(key, "new_network_number") == 0)
-                       inet_aton(value, &ipv4.network);
-
-               if (strcmp(key, "new_broadcast_address") == 0)
-                       inet_aton(value, &ipv4.broadcast);
-
-               if (strcmp(key, "new_domain_name_servers") == 0)
-                       inet_aton(value, &ipv4.nameserver);
-
-               dbus_message_iter_next(&dict);
-       }
-
-       if (strcmp(text, "PREINIT") == 0)
-               connman_dhcp_update(task->iface,
-                                       CONNMAN_DHCP_STATE_INIT, &ipv4);
-       else if (strcmp(text, "BOUND") == 0 || strcmp(text, "REBOOT") == 0)
-               connman_dhcp_update(task->iface,
-                                       CONNMAN_DHCP_STATE_BOUND, &ipv4);
-       else if (strcmp(text, "RENEW") == 0 || strcmp(text, "REBIND") == 0)
-               connman_dhcp_update(task->iface,
-                                       CONNMAN_DHCP_STATE_RENEW, &ipv4);
-       else
-               connman_dhcp_update(task->iface,
-                                       CONNMAN_DHCP_STATE_FAILED, NULL);
-
-       return NULL;
-}
-
-static GDBusMethodTable dhclient_methods[] = {
-       { "notify", "usa{ss}", "", notify_method, G_DBUS_METHOD_FLAG_NOREPLY },
-       { },
-};
-
-static DBusConnection *connection;
-
-static int plugin_init(void)
-{
-       connection = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL);
-
-       busname = dbus_bus_get_unique_name(connection);
-
-       g_dbus_register_object(connection, "/org/isc/dhclient", NULL, NULL);
-
-       g_dbus_register_interface(connection, "/org/isc/dhclient",
-                                       "org.isc.dhclient",
-                                       dhclient_methods, NULL, NULL);
-
-       connman_dhcp_register(&dhclient_driver);
-
-       return 0;
+       return connman_dhcp_driver_register(&dhclient_driver);
 }
 
-static void plugin_exit(void)
+static void dhclient_exit(void)
 {
-       GSList *list;
-
-       for (list = tasks; list; list = list->next) {
-               struct dhclient_task *task = list->data;
-
-               printf("[DHCP] killing process %d\n", task->pid);
-
-               kill_task(task);
-       }
-
-       g_slist_free(tasks);
-
-       connman_dhcp_unregister(&dhclient_driver);
-
-       g_dbus_cleanup_connection(connection);
+       connman_dhcp_driver_unregister(&dhclient_driver);
 }
 
-CONNMAN_PLUGIN_DEFINE("dhclient", "ISC DHCP client plugin", VERSION,
-                                               plugin_init, plugin_exit)
+CONNMAN_PLUGIN_DEFINE(dhclient, "ISC DHCP client plugin", VERSION,
+               CONNMAN_PLUGIN_PRIORITY_DEFAULT, dhclient_init, dhclient_exit)