Add hooks for domain details in dhclient
[connman] / plugins / dhclient.c
index 7249773..10f32ff 100644 (file)
@@ -2,7 +2,7 @@
  *
  *  Connection Manager
  *
- *  Copyright (C) 2007-2008  Intel Corporation. All rights reserved.
+ *  Copyright (C) 2007-2009  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 <unistd.h>
 #include <sys/wait.h>
 #include <glib/gstdio.h>
 
+#define CONNMAN_API_SUBJECT_TO_CHANGE
 #include <connman/plugin.h>
 #include <connman/driver.h>
+#include <connman/inet.h>
 #include <connman/dbus.h>
 #include <connman/log.h>
 
-#include "inet.h"
-
 #define DHCLIENT_INTF "org.isc.dhclient"
 #define DHCLIENT_PATH "/org/isc/dhclient"
 
@@ -135,12 +136,15 @@ static int dhclient_probe(struct connman_element *element)
 
        DBG("element %p name %s", element, element->name);
 
+       if (access(DHCLIENT, X_OK) < 0)
+               return -errno;
+
        task = g_try_new0(struct dhclient_task, 1);
        if (task == NULL)
                return -ENOMEM;
 
        task->ifindex = element->index;
-       task->ifname = inet_index2name(element->index);
+       task->ifname = connman_inet_ifname(element->index);
        task->element = element;
 
        if (task->ifname == NULL) {
@@ -161,18 +165,18 @@ static int dhclient_probe(struct connman_element *element)
        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[3] = "-e";
+       argv[4] = address;
+       argv[5] = "-pf";
+       argv[6] = pidfile;
+       argv[7] = "-lf";
+       argv[8] = leases;
+       argv[9] = "-cf";
+       argv[10] = config;
+       argv[11] = "-sf";
+       argv[12] = script;
+       argv[13] = task->ifname;
+       argv[14] = "-n";
        argv[15] = NULL;
 
        envp[0] = NULL;
@@ -210,11 +214,21 @@ static void dhclient_remove(struct connman_element *element)
        kill_task(task);
 }
 
+static void dhclient_change(struct connman_element *element)
+{
+       DBG("element %p name %s", element, element->name);
+
+       if (element->state == CONNMAN_ELEMENT_STATE_ERROR)
+               connman_element_set_error(element->parent,
+                                       CONNMAN_ELEMENT_ERROR_DHCP_FAILED);
+}
+
 static struct connman_driver dhclient_driver = {
        .name           = "dhclient",
        .type           = CONNMAN_ELEMENT_TYPE_DHCP,
        .probe          = dhclient_probe,
        .remove         = dhclient_remove,
+       .change         = dhclient_change,
 };
 
 static DBusHandlerResult dhclient_filter(DBusConnection *conn,
@@ -285,6 +299,15 @@ static DBusHandlerResult dhclient_filter(DBusConnection *conn,
                        task->element->ipv4.nameserver = g_strdup(value);
                }
 
+               if (g_ascii_strcasecmp(key, "new_domain_name") == 0) {
+               }
+
+               if (g_ascii_strcasecmp(key, "new_domain_search") == 0) {
+               }
+
+               if (g_ascii_strcasecmp(key, "new_host_name") == 0) {
+               }
+
                dbus_message_iter_next(&dict);
        }
 
@@ -296,10 +319,14 @@ static DBusHandlerResult dhclient_filter(DBusConnection *conn,
                element->type = CONNMAN_ELEMENT_TYPE_IPV4;
                element->index = task->ifindex;
                connman_element_update(task->element);
-               connman_element_register(element, task->element);
+               if (connman_element_register(element, task->element) < 0)
+                       connman_element_unref(element);
        } else if (g_ascii_strcasecmp(text, "RENEW") == 0 ||
                                g_ascii_strcasecmp(text, "REBIND") == 0) {
                connman_element_update(task->element);
+       } else if (g_ascii_strcasecmp(text, "FAIL") == 0) {
+               connman_element_set_error(task->element,
+                                               CONNMAN_ELEMENT_ERROR_FAILED);
        } else {
        }
 
@@ -308,24 +335,21 @@ static DBusHandlerResult dhclient_filter(DBusConnection *conn,
 
 static DBusConnection *connection;
 
+static const char *dhclient_rule = "path=" DHCLIENT_PATH
+                                               ",interface=" DHCLIENT_INTF;
+
 static int dhclient_init(void)
 {
-       gchar *filter;
        int err;
 
-       connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
+       connection = connman_dbus_get_connection();
 
        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);
+       dbus_bus_add_match(connection, dhclient_rule, NULL);
 
        err = connman_driver_register(&dhclient_driver);
        if (err < 0) {
@@ -353,8 +377,12 @@ static void dhclient_exit(void)
 
        connman_driver_unregister(&dhclient_driver);
 
+       dbus_bus_remove_match(connection, dhclient_rule, NULL);
+
+       dbus_connection_remove_filter(connection, dhclient_filter, NULL);
+
        dbus_connection_unref(connection);
 }
 
-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)