5 * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <connman/plugin.h>
30 #include <connman/driver.h>
31 #include <connman/dbus.h>
32 #include <connman/log.h>
37 #define UDHCPC_INTF "org.busybox.udhcpc"
38 #define UDHCPC_PATH "/org/busybox/udhcpc"
40 static int udhcp_probe(struct connman_element *element)
42 struct task_data *task;
43 char *argv[9], *envp[2], *ifname;
44 char pidfile[PATH_MAX], script[PATH_MAX];
46 DBG("element %p name %s", element, element->name);
48 if (access(UDHCPC, X_OK) < 0)
51 ifname = inet_index2name(element->index);
55 snprintf(pidfile, sizeof(pidfile) - 1,
56 "%s/udhcpc.%s.pid", STATEDIR, ifname);
57 snprintf(script, sizeof(script) - 1, "%s/udhcpc-script", SCRIPTDIR);
71 task = task_spawn(element->index, argv, envp, NULL, element);
82 static void udhcp_remove(struct connman_element *element)
84 struct task_data *task;
86 DBG("element %p name %s", element, element->name);
88 task = task_find_by_index(element->index);
95 static struct connman_driver udhcp_driver = {
97 .type = CONNMAN_ELEMENT_TYPE_DHCP,
98 .priority = CONNMAN_DRIVER_PRIORITY_HIGH,
100 .remove = udhcp_remove,
103 static void udhcp_bound(DBusMessage *msg, gboolean renew)
105 struct task_data *task;
106 struct connman_element *element, *parent;
107 const char *interface, *address, *netmask, *broadcast, *gateway, *dns;
110 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &interface,
111 DBUS_TYPE_STRING, &address,
112 DBUS_TYPE_STRING, &netmask,
113 DBUS_TYPE_STRING, &broadcast,
114 DBUS_TYPE_STRING, &gateway,
115 DBUS_TYPE_STRING, &dns,
118 DBG("%s ==> address %s gateway %s", interface, address, gateway);
120 index = inet_name2index(interface);
124 task = task_find_by_index(index);
128 parent = task_get_data(task);
132 g_free(parent->ipv4.address);
133 parent->ipv4.address = g_strdup(address);
135 g_free(parent->ipv4.netmask);
136 parent->ipv4.netmask = g_strdup(netmask);
138 g_free(parent->ipv4.broadcast);
139 parent->ipv4.broadcast = g_strdup(broadcast);
141 g_free(parent->ipv4.gateway);
142 parent->ipv4.gateway = g_strdup(gateway);
144 g_free(parent->ipv4.nameserver);
145 parent->ipv4.nameserver = g_strdup(dns);
147 connman_element_update(parent);
152 element = connman_element_create(NULL);
156 element->type = CONNMAN_ELEMENT_TYPE_IPV4;
157 element->index = index;
159 if (connman_element_register(element, parent) < 0)
160 connman_element_unref(element);
163 static DBusHandlerResult udhcp_filter(DBusConnection *conn,
164 DBusMessage *msg, void *data)
166 if (dbus_message_is_method_call(msg, UDHCPC_INTF, "bound") == TRUE) {
167 udhcp_bound(msg, FALSE);
168 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
171 if (dbus_message_is_method_call(msg, UDHCPC_INTF, "renew") == TRUE) {
172 udhcp_bound(msg, TRUE);
173 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
176 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
179 static DBusConnection *connection;
181 static const char *udhcp_rule = "path=" UDHCPC_PATH ",interface=" UDHCPC_INTF;
183 static int udhcp_init(void)
187 connection = connman_dbus_get_connection();
189 dbus_connection_add_filter(connection, udhcp_filter, NULL, NULL);
191 dbus_bus_add_match(connection, udhcp_rule, NULL);
193 err = connman_driver_register(&udhcp_driver);
195 dbus_connection_unref(connection);
202 static void udhcp_exit(void)
204 connman_driver_unregister(&udhcp_driver);
206 dbus_bus_remove_match(connection, udhcp_rule, NULL);
208 dbus_connection_remove_filter(connection, udhcp_filter, NULL);
210 dbus_connection_unref(connection);
213 CONNMAN_PLUGIN_DEFINE(dhclient, "uDHCP client plugin", VERSION,
214 udhcp_init, udhcp_exit)