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
33 #include <sys/ioctl.h>
34 #include <sys/socket.h>
35 #include <arpa/inet.h>
41 #include <connman/plugin.h>
42 #include <connman/dhcp.h>
43 #include <connman/dbus.h>
45 #define DHCLIENT_INTF "org.isc.dhclient"
46 #define DHCLIENT_PATH "/org/isc/dhclient"
48 static const char *busname;
50 struct dhclient_task {
54 struct connman_iface *iface;
57 static GSList *tasks = NULL;
59 static struct dhclient_task *find_task_by_pid(GPid pid)
63 for (list = tasks; list; list = list->next) {
64 struct dhclient_task *task = list->data;
73 static struct dhclient_task *find_task_by_index(int index)
77 for (list = tasks; list; list = list->next) {
78 struct dhclient_task *task = list->data;
80 if (task->ifindex == index)
87 static void kill_task(struct dhclient_task *task)
90 kill(task->pid, SIGTERM);
93 static void task_died(GPid pid, gint status, gpointer data)
95 struct dhclient_task *task = data;
96 char pathname[PATH_MAX];
98 if (WIFEXITED(status))
99 printf("[DHCP] exit status %d for %s\n",
100 WEXITSTATUS(status), task->ifname);
102 printf("[DHCP] signal %d killed %s\n",
103 WTERMSIG(status), task->ifname);
105 g_spawn_close_pid(pid);
108 tasks = g_slist_remove(tasks, task);
110 snprintf(pathname, sizeof(pathname) - 1,
111 "%s/dhclient.%s.pid", STATEDIR, task->ifname);
114 snprintf(pathname, sizeof(pathname) - 1,
115 "%s/dhclient.%s.leases", STATEDIR, task->ifname);
123 static void task_setup(gpointer data)
125 struct dhclient_task *task = data;
127 printf("[DHCP] setup %s\n", task->ifname);
130 static int dhclient_request(struct connman_iface *iface)
133 struct dhclient_task *task;
134 char *argv[16], *envp[1], address[128], pidfile[PATH_MAX];
135 char leases[PATH_MAX], config[PATH_MAX], script[PATH_MAX];
138 sk = socket(PF_INET, SOCK_DGRAM, 0);
142 memset(&ifr, 0, sizeof(ifr));
143 ifr.ifr_ifindex = iface->index;
145 err = ioctl(sk, SIOCGIFNAME, &ifr);
152 task = g_try_new0(struct dhclient_task, 1);
156 task->ifindex = iface->index;
157 task->ifname = strdup(ifr.ifr_name);
160 if (task->ifname == NULL) {
165 printf("[DHCP] request %s\n", task->ifname);
167 snprintf(address, sizeof(address) - 1, "BUSNAME=%s", busname);
168 snprintf(pidfile, sizeof(pidfile) - 1,
169 "%s/dhclient.%s.pid", STATEDIR, task->ifname);
170 snprintf(leases, sizeof(leases) - 1,
171 "%s/dhclient.%s.leases", STATEDIR, task->ifname);
172 snprintf(config, sizeof(config) - 1, "%s/dhclient.conf", SCRIPTDIR);
173 snprintf(script, sizeof(script) - 1, "%s/dhclient-script", SCRIPTDIR);
189 argv[14] = task->ifname;
194 if (g_spawn_async(NULL, argv, envp, G_SPAWN_DO_NOT_REAP_CHILD,
195 task_setup, task, &task->pid, NULL) == FALSE) {
196 printf("Failed to spawn dhclient\n");
200 tasks = g_slist_append(tasks, task);
202 g_child_watch_add(task->pid, task_died, task);
204 printf("[DHCP] executed %s with pid %d\n", DHCLIENT, task->pid);
209 static int dhclient_release(struct connman_iface *iface)
211 struct dhclient_task *task;
213 task = find_task_by_index(iface->index);
217 printf("[DHCP] release %s\n", task->ifname);
219 tasks = g_slist_remove(tasks, task);
226 static struct connman_dhcp_driver dhclient_driver = {
228 .request = dhclient_request,
229 .release = dhclient_release,
232 static DBusHandlerResult dhclient_filter(DBusConnection *conn,
233 DBusMessage *msg, void *data)
235 DBusMessageIter iter, dict;
237 struct dhclient_task *task;
238 struct connman_ipv4 ipv4;
239 const char *text, *key, *value;
241 if (dbus_message_is_method_call(msg, DHCLIENT_INTF, "notify") == FALSE)
242 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
244 memset(&ipv4, 0, sizeof(ipv4));
246 dbus_message_iter_init(msg, &iter);
248 dbus_message_iter_get_basic(&iter, &pid);
249 dbus_message_iter_next(&iter);
251 dbus_message_iter_get_basic(&iter, &text);
252 dbus_message_iter_next(&iter);
254 printf("[DHCP] change %d to %s\n", pid, text);
256 task = find_task_by_pid(pid);
258 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
260 dbus_message_iter_recurse(&iter, &dict);
262 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
263 DBusMessageIter entry;
265 dbus_message_iter_recurse(&dict, &entry);
266 dbus_message_iter_get_basic(&entry, &key);
267 dbus_message_iter_next(&entry);
268 dbus_message_iter_get_basic(&entry, &value);
270 printf("[DHCP] %s = %s\n", key, value);
272 if (strcmp(key, "new_ip_address") == 0)
273 inet_aton(value, &ipv4.address);
275 if (strcmp(key, "new_subnet_mask") == 0)
276 inet_aton(value, &ipv4.netmask);
278 if (strcmp(key, "new_routers") == 0)
279 inet_aton(value, &ipv4.gateway);
281 if (strcmp(key, "new_network_number") == 0)
282 inet_aton(value, &ipv4.network);
284 if (strcmp(key, "new_broadcast_address") == 0)
285 inet_aton(value, &ipv4.broadcast);
287 if (strcmp(key, "new_domain_name_servers") == 0)
288 inet_aton(value, &ipv4.nameserver);
290 dbus_message_iter_next(&dict);
293 if (strcmp(text, "PREINIT") == 0)
294 connman_dhcp_update(task->iface,
295 CONNMAN_DHCP_STATE_INIT, &ipv4);
296 else if (strcmp(text, "BOUND") == 0 || strcmp(text, "REBOOT") == 0)
297 connman_dhcp_update(task->iface,
298 CONNMAN_DHCP_STATE_BOUND, &ipv4);
299 else if (strcmp(text, "RENEW") == 0 || strcmp(text, "REBIND") == 0)
300 connman_dhcp_update(task->iface,
301 CONNMAN_DHCP_STATE_RENEW, &ipv4);
303 connman_dhcp_update(task->iface,
304 CONNMAN_DHCP_STATE_FAILED, NULL);
306 return DBUS_HANDLER_RESULT_HANDLED;
309 static DBusConnection *connection;
311 static int plugin_init(void)
315 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
317 busname = dbus_bus_get_unique_name(connection);
318 busname = CONNMAN_SERVICE;
320 dbus_connection_add_filter(connection, dhclient_filter, NULL, NULL);
322 filter = g_strdup_printf("interface=%s,path=%s",
323 DHCLIENT_INTF, DHCLIENT_PATH);
325 dbus_bus_add_match(connection, filter, NULL);
329 connman_dhcp_register(&dhclient_driver);
334 static void plugin_exit(void)
338 for (list = tasks; list; list = list->next) {
339 struct dhclient_task *task = list->data;
341 printf("[DHCP] killing process %d\n", task->pid);
348 connman_dhcp_unregister(&dhclient_driver);
350 dbus_connection_unref(connection);
353 CONNMAN_PLUGIN_DEFINE("dhclient", "ISC DHCP client plugin", VERSION,
354 plugin_init, plugin_exit)