5 * Copyright (C) 2007 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>
44 #define DHCLIENT_INTF "org.isc.dhclient"
45 #define DHCLIENT_PATH "/org/isc/dhclient"
47 static const char *busname;
49 struct dhclient_task {
53 struct connman_iface *iface;
56 static GSList *tasks = NULL;
58 static struct dhclient_task *find_task_by_pid(GPid pid)
62 for (list = tasks; list; list = list->next) {
63 struct dhclient_task *task = list->data;
72 static struct dhclient_task *find_task_by_index(int index)
76 for (list = tasks; list; list = list->next) {
77 struct dhclient_task *task = list->data;
79 if (task->ifindex == index)
86 static void kill_task(struct dhclient_task *task)
88 char pathname[PATH_MAX];
91 kill(task->pid, SIGTERM);
93 snprintf(pathname, sizeof(pathname) - 1,
94 "%s/dhclient.%s.pid", STATEDIR, task->ifname);
97 snprintf(pathname, sizeof(pathname) - 1,
98 "%s/dhclient.%s.leases", STATEDIR, task->ifname);
106 static void task_died(GPid pid, gint status, gpointer data)
108 struct dhclient_task *task = data;
110 if (WIFEXITED(status))
111 printf("[DHCP] exit status %d for %s\n",
112 WEXITSTATUS(status), task->ifname);
114 printf("[DHCP] signal %d killed %s\n",
115 WTERMSIG(status), task->ifname);
117 g_spawn_close_pid(pid);
120 tasks = g_slist_remove(tasks, task);
125 static void task_setup(gpointer data)
127 struct dhclient_task *task = data;
129 printf("[DHCP] setup %s\n", task->ifname);
132 static int dhclient_request(struct connman_iface *iface)
135 struct dhclient_task *task;
136 char *argv[16], *envp[1], address[128], pidfile[PATH_MAX];
137 char leases[PATH_MAX], config[PATH_MAX], script[PATH_MAX];
140 sk = socket(PF_INET, SOCK_DGRAM, 0);
144 memset(&ifr, 0, sizeof(ifr));
145 ifr.ifr_ifindex = iface->index;
147 err = ioctl(sk, SIOCGIFNAME, &ifr);
154 task = g_try_new0(struct dhclient_task, 1);
158 task->ifindex = iface->index;
159 task->ifname = strdup(ifr.ifr_name);
162 if (task->ifname == NULL) {
167 printf("[DHCP] request %s\n", task->ifname);
169 snprintf(address, sizeof(address) - 1, "BUSNAME=%s", busname);
170 snprintf(pidfile, sizeof(pidfile) - 1,
171 "%s/dhclient.%s.pid", STATEDIR, task->ifname);
172 snprintf(leases, sizeof(leases) - 1,
173 "%s/dhclient.%s.leases", STATEDIR, task->ifname);
174 snprintf(config, sizeof(config) - 1, "%s/dhclient.conf", SCRIPTDIR);
175 snprintf(script, sizeof(script) - 1, "%s/dhclient-script", SCRIPTDIR);
191 argv[14] = task->ifname;
196 if (g_spawn_async(NULL, argv, envp, G_SPAWN_DO_NOT_REAP_CHILD,
197 task_setup, task, &task->pid, NULL) == FALSE) {
198 printf("Failed to spawn dhclient\n");
202 tasks = g_slist_append(tasks, task);
204 g_child_watch_add(task->pid, task_died, task);
206 printf("[DHCP] executed %s with pid %d\n", DHCLIENT, task->pid);
211 static int dhclient_release(struct connman_iface *iface)
213 struct dhclient_task *task;
215 task = find_task_by_index(iface->index);
219 printf("[DHCP] release %s\n", task->ifname);
221 tasks = g_slist_remove(tasks, task);
228 static struct connman_dhcp_driver dhclient_driver = {
230 .request = dhclient_request,
231 .release = dhclient_release,
234 static DBusHandlerResult dhclient_filter(DBusConnection *conn,
235 DBusMessage *msg, void *data)
237 DBusMessageIter iter, dict;
239 struct dhclient_task *task;
240 struct connman_ipv4 ipv4;
241 const char *text, *key, *value;
243 if (dbus_message_is_method_call(msg, DHCLIENT_INTF, "notify") == FALSE)
244 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
246 memset(&ipv4, 0, sizeof(ipv4));
248 dbus_message_iter_init(msg, &iter);
250 dbus_message_iter_get_basic(&iter, &pid);
251 dbus_message_iter_next(&iter);
253 dbus_message_iter_get_basic(&iter, &text);
254 dbus_message_iter_next(&iter);
256 printf("[DHCP] change %d to %s\n", pid, text);
258 task = find_task_by_pid(pid);
260 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
262 dbus_message_iter_recurse(&iter, &dict);
264 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
265 DBusMessageIter entry;
267 dbus_message_iter_recurse(&dict, &entry);
268 dbus_message_iter_get_basic(&entry, &key);
269 dbus_message_iter_next(&entry);
270 dbus_message_iter_get_basic(&entry, &value);
272 printf("[DHCP] %s = %s\n", key, value);
274 if (strcmp(key, "new_ip_address") == 0)
275 inet_aton(value, &ipv4.address);
277 if (strcmp(key, "new_subnet_mask") == 0)
278 inet_aton(value, &ipv4.netmask);
280 if (strcmp(key, "new_routers") == 0)
281 inet_aton(value, &ipv4.gateway);
283 if (strcmp(key, "new_network_number") == 0)
284 inet_aton(value, &ipv4.network);
286 if (strcmp(key, "new_broadcast_address") == 0)
287 inet_aton(value, &ipv4.broadcast);
289 if (strcmp(key, "new_domain_name_servers") == 0)
290 inet_aton(value, &ipv4.nameserver);
292 dbus_message_iter_next(&dict);
295 if (strcmp(text, "PREINIT") == 0)
296 connman_dhcp_update(task->iface,
297 CONNMAN_DHCP_STATE_INIT, &ipv4);
298 else if (strcmp(text, "BOUND") == 0 || strcmp(text, "REBOOT") == 0)
299 connman_dhcp_update(task->iface,
300 CONNMAN_DHCP_STATE_BOUND, &ipv4);
301 else if (strcmp(text, "RENEW") == 0 || strcmp(text, "REBIND") == 0)
302 connman_dhcp_update(task->iface,
303 CONNMAN_DHCP_STATE_RENEW, &ipv4);
305 connman_dhcp_update(task->iface,
306 CONNMAN_DHCP_STATE_FAILED, NULL);
308 return DBUS_HANDLER_RESULT_HANDLED;
311 static DBusConnection *connection;
313 static int plugin_init(void)
317 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
319 busname = dbus_bus_get_unique_name(connection);
321 busname = "org.freedesktop.connman";
323 dbus_connection_add_filter(connection, dhclient_filter, NULL, NULL);
325 filter = g_strdup_printf("interface=%s,path=%s",
326 DHCLIENT_INTF, DHCLIENT_PATH);
328 dbus_bus_add_match(connection, filter, NULL);
332 connman_dhcp_register(&dhclient_driver);
337 static void plugin_exit(void)
341 for (list = tasks; list; list = list->next) {
342 struct dhclient_task *task = list->data;
344 printf("[DHCP] killing process %d\n", task->pid);
351 connman_dhcp_unregister(&dhclient_driver);
353 dbus_connection_unref(connection);
356 CONNMAN_PLUGIN_DEFINE("dhclient", "ISC DHCP client plugin", VERSION,
357 plugin_init, plugin_exit)