Add initial implementation for uDHCP support
authorMarcel Holtmann <marcel@holtmann.org>
Sun, 28 Dec 2008 05:37:25 +0000 (06:37 +0100)
committerMarcel Holtmann <marcel@holtmann.org>
Sun, 28 Dec 2008 05:37:25 +0000 (06:37 +0100)
plugins/Makefile.am
plugins/udhcp.c
scripts/Makefile.am
scripts/udhcpc-script.c [new file with mode: 0644]

index b71e6b9..f616c0d 100644 (file)
@@ -19,8 +19,9 @@ bluetooth_la_LIBADD = @GDBUS_LIBS@
 
 hso_la_SOURCES = hso.c
 
 
 hso_la_SOURCES = hso.c
 
-udhcp_la_SOURCES = udhcp.c
-udhcp_la_CFLAGS = @GLIB_CFLAGS@ -DUDHCPC=\"@UDHCPC@\"
+udhcp_la_SOURCES = udhcp.c inet.h inet.c task.h task.c
+udhcp_la_CFLAGS = @GLIB_CFLAGS@ @GDBUS_CFLAGS@ -DUDHCPC=\"@UDHCPC@\" \
+               -DSTATEDIR=\""$(statedir)"\" -DSCRIPTDIR=\""$(scriptdir)"\"
 
 dhclient_la_SOURCES = dhclient.c inet.h inet.c
 dhclient_la_CFLAGS = @GLIB_CFLAGS@ @GDBUS_CFLAGS@ -DDHCLIENT=\"@DHCLIENT@\" \
 
 dhclient_la_SOURCES = dhclient.c inet.h inet.c
 dhclient_la_CFLAGS = @GLIB_CFLAGS@ @GDBUS_CFLAGS@ -DDHCLIENT=\"@DHCLIENT@\" \
index dd7dd8c..597488d 100644 (file)
 #include <config.h>
 #endif
 
 #include <config.h>
 #endif
 
+#include <stdio.h>
+#include <unistd.h>
+
 #include <connman/plugin.h>
 #include <connman/driver.h>
 #include <connman/plugin.h>
 #include <connman/driver.h>
+#include <connman/dbus.h>
 #include <connman/log.h>
 
 #include <connman/log.h>
 
+#include "inet.h"
+#include "task.h"
+
+#define UDHCPC_INTF  "org.busybox.udhcpc"
+#define UDHCPC_PATH  "/org/busybox/udhcpc"
+
 static int udhcp_probe(struct connman_element *element)
 {
 static int udhcp_probe(struct connman_element *element)
 {
+       struct task_data *task;
+       char *argv[9], *envp[2], *ifname;
+       char pidfile[PATH_MAX], script[PATH_MAX];
+
        DBG("element %p name %s", element, element->name);
 
        DBG("element %p name %s", element, element->name);
 
-       return -ENODEV;
+       if (access(UDHCPC, X_OK) < 0)
+               return -errno;
+
+       ifname = inet_index2name(element->index);
+       if (ifname == NULL)
+               return -ENOMEM;
+
+       snprintf(pidfile, sizeof(pidfile) - 1,
+                               "%s/udhcpc.%s.pid", STATEDIR, ifname);
+       snprintf(script, sizeof(script) - 1, "%s/udhcpc-script", SCRIPTDIR);
+
+       argv[0] = UDHCPC;
+       argv[1] = "-f";
+       argv[2] = "-i";
+       argv[3] = ifname;
+       argv[4] = "-p";
+       argv[5] = pidfile;
+       argv[6] = "-s";
+       argv[7] = script;
+       argv[8] = NULL;
+
+       envp[0] = NULL;
+
+       task = task_spawn(element->index, argv, envp, NULL, element);
+       if (task == NULL) {
+               g_free(ifname);
+               return -EIO;
+       }
+
+       g_free(ifname);
+
+       return 0;
 }
 
 static void udhcp_remove(struct connman_element *element)
 {
 }
 
 static void udhcp_remove(struct connman_element *element)
 {
+       struct task_data *task;
+
        DBG("element %p name %s", element, element->name);
        DBG("element %p name %s", element, element->name);
+
+       task = task_find_by_index(element->index);
+       if (task == NULL)
+               return;
+
+       task_kill(task);
 }
 
 static struct connman_driver udhcp_driver = {
 }
 
 static struct connman_driver udhcp_driver = {
@@ -47,14 +100,114 @@ static struct connman_driver udhcp_driver = {
        .remove         = udhcp_remove,
 };
 
        .remove         = udhcp_remove,
 };
 
+static void udhcp_bound(DBusMessage *msg, gboolean renew)
+{
+       struct task_data *task;
+       struct connman_element *element, *parent;
+       const char *interface, *address, *netmask, *broadcast, *gateway, *dns;
+       int index;
+
+       dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &interface,
+                                       DBUS_TYPE_STRING, &address,
+                                       DBUS_TYPE_STRING, &netmask,
+                                       DBUS_TYPE_STRING, &broadcast,
+                                       DBUS_TYPE_STRING, &gateway,
+                                       DBUS_TYPE_STRING, &dns,
+                                                       DBUS_TYPE_INVALID);
+
+       DBG("%s ==> address %s gateway %s", interface, address, gateway);
+
+       index = inet_name2index(interface);
+       if (index < 0)
+               return;
+
+       task = task_find_by_index(index);
+       if (task == NULL)
+               return;
+
+       parent = task_get_data(task);
+       if (parent == NULL)
+               return;
+
+       g_free(parent->ipv4.address);
+       parent->ipv4.address = g_strdup(address);
+
+       g_free(parent->ipv4.netmask);
+       parent->ipv4.netmask = g_strdup(netmask);
+
+       g_free(parent->ipv4.broadcast);
+       parent->ipv4.broadcast = g_strdup(broadcast);
+
+       g_free(parent->ipv4.gateway);
+       parent->ipv4.gateway = g_strdup(gateway);
+
+       g_free(parent->ipv4.nameserver);
+       parent->ipv4.nameserver = g_strdup(dns);
+
+       connman_element_update(parent);
+
+       if (renew == TRUE)
+               return;
+
+       element = connman_element_create(NULL);
+       if (element == NULL)
+               return;
+
+       element->type = CONNMAN_ELEMENT_TYPE_IPV4;
+       element->index = index;
+
+       if (connman_element_register(element, parent) < 0)
+               connman_element_unref(element);
+}
+
+static DBusHandlerResult udhcp_filter(DBusConnection *conn,
+                                               DBusMessage *msg, void *data)
+{
+       if (dbus_message_is_method_call(msg, UDHCPC_INTF, "bound") == TRUE) {
+               udhcp_bound(msg, FALSE);
+               return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+       }
+
+       if (dbus_message_is_method_call(msg, UDHCPC_INTF, "renew") == TRUE) {
+               udhcp_bound(msg, TRUE);
+               return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+       }
+
+       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+static DBusConnection *connection;
+
+static const char *udhcp_rule = "path=" UDHCPC_PATH ",interface=" UDHCPC_INTF;
+
 static int udhcp_init(void)
 {
 static int udhcp_init(void)
 {
-       return connman_driver_register(&udhcp_driver);
+       int err;
+
+       connection = connman_dbus_get_connection();
+
+       dbus_connection_add_filter(connection, udhcp_filter, NULL, NULL);
+
+       dbus_bus_add_match(connection, udhcp_rule, NULL);
+
+       err = connman_driver_register(&udhcp_driver);
+       if (err < 0) {
+               dbus_connection_unref(connection);
+               return err;
+       }
+
+       return 0;
 }
 
 static void udhcp_exit(void)
 {
        connman_driver_unregister(&udhcp_driver);
 }
 
 static void udhcp_exit(void)
 {
        connman_driver_unregister(&udhcp_driver);
+
+       dbus_bus_remove_match(connection, udhcp_rule, NULL);
+
+       dbus_connection_remove_filter(connection, udhcp_filter, NULL);
+
+       dbus_connection_unref(connection);
 }
 
 CONNMAN_PLUGIN_DEFINE(dhclient, "uDHCP client plugin", VERSION,
 }
 
 CONNMAN_PLUGIN_DEFINE(dhclient, "uDHCP client plugin", VERSION,
index 1247398..b28f64f 100644 (file)
@@ -9,7 +9,9 @@ scriptdir = $(libdir)/connman/scripts
 
 script_DATA = dhclient.conf
 
 
 script_DATA = dhclient.conf
 
-script_PROGRAMS = dhclient-script
+script_PROGRAMS = udhcpc-script dhclient-script
+
+udhcpc_script_LDADD = @DBUS_LIBS@
 
 dhclient_script_LDADD = @DBUS_LIBS@
 
 
 dhclient_script_LDADD = @DBUS_LIBS@
 
diff --git a/scripts/udhcpc-script.c b/scripts/udhcpc-script.c
new file mode 100644 (file)
index 0000000..c7a7ae2
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ *
+ *  Connection Manager
+ *
+ *  Copyright (C) 2007-2008  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
+ *  published by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <dbus/dbus.h>
+
+#define UDHCPC_INTF  "org.busybox.udhcpc"
+#define UDHCPC_PATH  "/org/busybox/udhcpc"
+
+int main(int argc, char *argv[])
+{
+       DBusConnection *conn;
+       DBusError error;
+       DBusMessage *msg;
+       char *busname, *interface, *address, *netmask, *broadcast;
+       char *gateway, *dns;
+
+       if (argc < 2)
+               return 0;
+
+       if (strcmp(argv[1], "bound") != 0 && strcmp(argv[1], "renew") != 0)
+               return 0;
+
+       busname = "org.moblin.connman";
+
+       interface = getenv("interface");
+
+       address = getenv("ip");
+       netmask = getenv("subnet");
+       broadcast = getenv("broadcast");
+       gateway = getenv("router");
+       dns = getenv("dns");
+
+       dbus_error_init(&error);
+
+       conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
+       if (conn == NULL) {
+               if (dbus_error_is_set(&error) == TRUE) {
+                       fprintf(stderr, "%s\n", error.message);
+                       dbus_error_free(&error);
+               } else
+                       fprintf(stderr, "Failed to get on system bus\n");
+               return 0;
+       }
+
+       msg = dbus_message_new_method_call(busname, UDHCPC_PATH,
+                                               UDHCPC_INTF, argv[1]);
+       if (msg == NULL) {
+               dbus_connection_unref(conn);
+               fprintf(stderr, "Failed to allocate method call\n");
+               return 0;
+       }
+
+       dbus_message_set_no_reply(msg, TRUE);
+
+       dbus_message_append_args(msg, DBUS_TYPE_STRING, &interface,
+                                       DBUS_TYPE_STRING, &address,
+                                       DBUS_TYPE_STRING, &netmask,
+                                       DBUS_TYPE_STRING, &broadcast,
+                                       DBUS_TYPE_STRING, &gateway,
+                                       DBUS_TYPE_STRING, &dns,
+                                                       DBUS_TYPE_INVALID);
+
+       if (dbus_connection_send(conn, msg, NULL) == FALSE)
+               fprintf(stderr, "Failed to send message\n");
+
+       dbus_message_unref(msg);
+
+       dbus_connection_unref(conn);
+
+       return 0;
+}