This email list is read-only.  Emails sent to this list will be discarded
----------------------------------
 plugins/Makefile.am     |    5 +-
 plugins/inet.c          |   25 ++++++++
 plugins/inet.h          |    2 +
 plugins/task.c          |  145 +++++++++++++++++++++++++++++++++++++++++++
 plugins/task.h          |   35 +++++++++++
 plugins/udhcp.c         |  157 ++++++++++++++++++++++++++++++++++++++++++++++-
 scripts/Makefile.am     |    4 +-
 scripts/udhcpc-script.c |   97 +++++++++++++++++++++++++++++
 8 files changed, 465 insertions(+), 5 deletions(-)

New commits:
commit 4d49d7072590206594f1f6e2e591b8bd9e25712e
Author: Marcel Holtmann <[email protected]>
Date:   Sun Dec 28 06:37:25 2008 +0100

    Add initial implementation for uDHCP support

commit e0a0733fe56afa691e93e9e396a7fbbed6ae1002
Author: Marcel Holtmann <[email protected]>
Date:   Sun Dec 28 06:30:56 2008 +0100

    Add helpers for task handling

commit 2e2686dc1b229252fae150bfe97d1c3010563eb9
Author: Marcel Holtmann <[email protected]>
Date:   Sun Dec 28 06:18:29 2008 +0100

    Add helper for getting index from interface names


Diff in this email is a maximum of 400 lines.
diff --git a/plugins/Makefile.am b/plugins/Makefile.am
index b71e6b9..f616c0d 100644
--- a/plugins/Makefile.am
+++ b/plugins/Makefile.am
@@ -19,8 +19,9 @@ bluetooth_la_LIBADD = @GDBUS_LIBS@
 
 hso_la_SOURCES = hso.c
 
-udhcp_la_SOURCES = udhcp.c
-udhcp_la_CFLAGS = @GLIB_CFLAGS@ -DUDHCPC=\"@udh...@\"
+udhcp_la_SOURCES = udhcp.c inet.h inet.c task.h task.c
+udhcp_la_CFLAGS = @GLIB_CFLAGS@ @GDBUS_CFLAGS@ -DUDHCPC=\"@udh...@\" \
+               -DSTATEDIR=\""$(statedir)"\" -DSCRIPTDIR=\""$(scriptdir)"\"
 
 dhclient_la_SOURCES = dhclient.c inet.h inet.c
 dhclient_la_CFLAGS = @GLIB_CFLAGS@ @GDBUS_CFLAGS@ -DDHCLIENT=\"@dhcli...@\" \
diff --git a/plugins/inet.c b/plugins/inet.c
index e3fedfb..97f338e 100644
--- a/plugins/inet.c
+++ b/plugins/inet.c
@@ -35,6 +35,31 @@
 
 #include "inet.h"
 
+int inet_name2index(const char *name)
+{
+       struct ifreq ifr;
+       int sk, err;
+
+       if (name == NULL)
+               return -1;
+
+       sk = socket(PF_INET, SOCK_DGRAM, 0);
+       if (sk < 0)
+               return -1;
+
+       memset(&ifr, 0, sizeof(ifr));
+       strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
+
+       err = ioctl(sk, SIOCGIFINDEX, &ifr);
+
+       close(sk);
+
+       if (err < 0)
+               return -1;
+
+       return ifr.ifr_ifindex;
+}
+
 char *inet_index2name(int index)
 {
        struct ifreq ifr;
diff --git a/plugins/inet.h b/plugins/inet.h
index 3246cc1..23437ac 100644
--- a/plugins/inet.h
+++ b/plugins/inet.h
@@ -19,6 +19,8 @@
  *
  */
 
+int inet_name2index(const char *name);
+
 char *inet_index2name(int index);
 char *inet_index2ident(int index, const char *prefix);
 
diff --git a/plugins/task.c b/plugins/task.c
new file mode 100644
index 0000000..dae54c7
--- /dev/null
+++ b/plugins/task.c
@@ -0,0 +1,145 @@
+/*
+ *
+ *  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 <unistd.h>
+#include <sys/wait.h>
+
+#include <glib.h>
+
+#include <connman/log.h>
+
+#include "task.h"
+
+struct task_data {
+       pid_t pid;
+       int index;
+       task_cb_t callback;
+       void *user_data;
+};
+
+static GSList *task_list = NULL;
+
+struct task_data *task_find_by_pid(pid_t pid)
+{
+       GSList *list;
+
+       for (list = task_list; list; list = list->next) {
+               struct task_data *task = list->data;
+
+               if (task->pid == pid)
+                       return task;
+       }
+
+       return NULL;
+}
+
+struct task_data *task_find_by_index(int index)
+{
+       GSList *list;
+
+       for (list = task_list; list; list = list->next) {
+               struct task_data *task = list->data;
+
+               if (task->index == index)
+                       return task;
+       }
+
+       return NULL;
+}
+
+static void task_died(GPid pid, gint status, gpointer user_data)
+{
+       struct task_data *task = user_data;
+
+       if (WIFEXITED(status))
+               DBG("task %p exit status %d", task, WEXITSTATUS(status));
+       else
+               DBG("task %p signal %d", task, WTERMSIG(status));
+
+       g_spawn_close_pid(pid);
+       task->pid = 0;
+
+       task_list = g_slist_remove(task_list, task);
+
+       if (task->callback)
+               task->callback(task->index, task->user_data);
+
+       g_free(task);
+}
+
+static void task_setup(gpointer user_data)
+{
+       struct task_data *task = user_data;
+
+       DBG("task %p", task);
+}
+
+struct task_data *task_spawn(int index, char **argv, char **envp,
+                                       task_cb_t callback, void *user_data)
+{
+       GSpawnFlags flags = G_SPAWN_DO_NOT_REAP_CHILD |
+                                               G_SPAWN_STDOUT_TO_DEV_NULL;
+       struct task_data *task;
+
+       DBG("index %d", index);
+
+       task = g_try_new0(struct task_data, 1);
+       if (task == NULL)
+               return NULL;
+
+       task->index = index;
+
+       task->callback  = callback;
+       task->user_data = user_data;
+
+       if (g_spawn_async(NULL, argv, envp, flags,
+                               task_setup, task, &task->pid, NULL) == FALSE) {
+               connman_error("Failed to spawn task");
+               return NULL;
+       }
+
+       task_list = g_slist_append(task_list, task);
+
+       g_child_watch_add(task->pid, task_died, task);
+
+       DBG("task %p pid %d", task, task->pid);
+
+       return task;
+}
+
+int task_kill(struct task_data *task)
+{
+       DBG("task %p", task);
+
+       if (task->pid > 0)
+               kill(task->pid, SIGTERM);
+
+       return 0;
+}
+
+void *task_get_data(struct task_data *task)
+{
+       return task->user_data;
+}
diff --git a/plugins/task.h b/plugins/task.h
new file mode 100644
index 0000000..0234d47
--- /dev/null
+++ b/plugins/task.h
@@ -0,0 +1,35 @@
+/*
+ *
+ *  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
+ *
+ */
+
+#include <sys/types.h>
+
+struct task_data;
+
+typedef void (* task_cb_t) (int index, void *user_data);
+
+struct task_data *task_find_by_pid(pid_t pid);
+struct task_data *task_find_by_index(int index);
+
+struct task_data *task_spawn(int index, char **argv, char **envp,
+                                       task_cb_t callback, void *user_data);
+int task_kill(struct task_data *task);
+
+void *task_get_data(struct task_data *task);
diff --git a/plugins/udhcp.c b/plugins/udhcp.c
index dd7dd8c..597488d 100644
--- a/plugins/udhcp.c
+++ b/plugins/udhcp.c
@@ -23,20 +23,73 @@
 #include <config.h>
 #endif
 
+#include <stdio.h>
+#include <unistd.h>
+
 #include <connman/plugin.h>
 #include <connman/driver.h>
+#include <connman/dbus.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)
 {
+       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);
 
-       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)
 {
+       struct task_data *task;
+
        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 = {
@@ -47,14 +100,114 @@ static struct connman_driver udhcp_driver = {
        .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);
+}
+
_______________________________________________
Commits mailing list
[email protected]
https://lists.moblin.org/mailman/listinfo/commits

Reply via email to