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 #define CONNMAN_API_SUBJECT_TO_CHANGE
30 #include <connman/plugin.h>
31 #include <connman/driver.h>
32 #include <connman/dbus.h>
33 #include <connman/log.h>
38 #define UDHCPC_INTF "net.busybox.udhcpc"
39 #define UDHCPC_PATH "/net/busybox/udhcpc"
41 static int udhcp_probe(struct connman_element *element)
43 struct task_data *task;
44 char *argv[9], *envp[2], *ifname;
45 char pidfile[PATH_MAX], script[PATH_MAX];
47 DBG("element %p name %s", element, element->name);
49 if (access(UDHCPC, X_OK) < 0)
52 ifname = inet_index2name(element->index);
56 snprintf(pidfile, sizeof(pidfile) - 1,
57 "%s/udhcpc.%s.pid", STATEDIR, ifname);
58 snprintf(script, sizeof(script) - 1, "%s/udhcpc-script", SCRIPTDIR);
72 task = task_spawn(element->index, argv, envp, NULL, element);
83 static void udhcp_remove(struct connman_element *element)
85 struct task_data *task;
87 DBG("element %p name %s", element, element->name);
89 task = task_find_by_index(element->index);
96 static struct connman_driver udhcp_driver = {
98 .type = CONNMAN_ELEMENT_TYPE_DHCP,
99 .priority = CONNMAN_DRIVER_PRIORITY_HIGH,
100 .probe = udhcp_probe,
101 .remove = udhcp_remove,
104 static void udhcp_bound(DBusMessage *msg, gboolean renew)
106 struct task_data *task;
107 struct connman_element *element, *parent;
108 const char *interface, *address, *netmask, *broadcast, *gateway, *dns;
111 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &interface,
112 DBUS_TYPE_STRING, &address,
113 DBUS_TYPE_STRING, &netmask,
114 DBUS_TYPE_STRING, &broadcast,
115 DBUS_TYPE_STRING, &gateway,
116 DBUS_TYPE_STRING, &dns,
119 DBG("%s ==> address %s gateway %s", interface, address, gateway);
121 index = inet_name2index(interface);
125 task = task_find_by_index(index);
129 parent = task_get_data(task);
133 g_free(parent->ipv4.address);
134 parent->ipv4.address = g_strdup(address);
136 g_free(parent->ipv4.netmask);
137 parent->ipv4.netmask = g_strdup(netmask);
139 g_free(parent->ipv4.broadcast);
140 parent->ipv4.broadcast = g_strdup(broadcast);
142 g_free(parent->ipv4.gateway);
143 parent->ipv4.gateway = g_strdup(gateway);
145 g_free(parent->ipv4.nameserver);
146 parent->ipv4.nameserver = g_strdup(dns);
148 connman_element_update(parent);
153 element = connman_element_create(NULL);
157 element->type = CONNMAN_ELEMENT_TYPE_IPV4;
158 element->index = index;
160 if (connman_element_register(element, parent) < 0)
161 connman_element_unref(element);
164 static DBusHandlerResult udhcp_filter(DBusConnection *conn,
165 DBusMessage *msg, void *data)
167 if (dbus_message_is_method_call(msg, UDHCPC_INTF, "bound") == TRUE) {
168 udhcp_bound(msg, FALSE);
169 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
172 if (dbus_message_is_method_call(msg, UDHCPC_INTF, "renew") == TRUE) {
173 udhcp_bound(msg, TRUE);
174 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
177 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
180 static DBusConnection *connection;
182 static const char *udhcp_rule = "path=" UDHCPC_PATH ",interface=" UDHCPC_INTF;
184 static int udhcp_init(void)
188 connection = connman_dbus_get_connection();
190 dbus_connection_add_filter(connection, udhcp_filter, NULL, NULL);
192 dbus_bus_add_match(connection, udhcp_rule, NULL);
194 err = connman_driver_register(&udhcp_driver);
196 dbus_connection_unref(connection);
203 static void udhcp_exit(void)
205 connman_driver_unregister(&udhcp_driver);
207 dbus_bus_remove_match(connection, udhcp_rule, NULL);
209 dbus_connection_remove_filter(connection, udhcp_filter, NULL);
211 dbus_connection_unref(connection);
214 CONNMAN_PLUGIN_DEFINE(dhclient, "uDHCP client plugin", VERSION,
215 udhcp_init, udhcp_exit)