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)
89 kill(task->pid, SIGTERM);
92 static void task_died(GPid pid, gint status, gpointer data)
94 struct dhclient_task *task = data;
95 char pathname[PATH_MAX];
97 if (WIFEXITED(status))
98 printf("[DHCP] exit status %d for %s\n",
99 WEXITSTATUS(status), task->ifname);
101 printf("[DHCP] signal %d killed %s\n",
102 WTERMSIG(status), task->ifname);
104 g_spawn_close_pid(pid);
107 tasks = g_slist_remove(tasks, task);
109 snprintf(pathname, sizeof(pathname) - 1,
110 "%s/dhclient.%s.pid", STATEDIR, task->ifname);
113 snprintf(pathname, sizeof(pathname) - 1,
114 "%s/dhclient.%s.leases", STATEDIR, task->ifname);
122 static void task_setup(gpointer data)
124 struct dhclient_task *task = data;
126 printf("[DHCP] setup %s\n", task->ifname);
129 static int dhclient_request(struct connman_iface *iface)
132 struct dhclient_task *task;
133 char *argv[16], *envp[1], address[128], pidfile[PATH_MAX];
134 char leases[PATH_MAX], config[PATH_MAX], script[PATH_MAX];
137 sk = socket(PF_INET, SOCK_DGRAM, 0);
141 memset(&ifr, 0, sizeof(ifr));
142 ifr.ifr_ifindex = iface->index;
144 err = ioctl(sk, SIOCGIFNAME, &ifr);
151 task = g_try_new0(struct dhclient_task, 1);
155 task->ifindex = iface->index;
156 task->ifname = strdup(ifr.ifr_name);
159 if (task->ifname == NULL) {
164 printf("[DHCP] request %s\n", task->ifname);
166 snprintf(address, sizeof(address) - 1, "BUSNAME=%s", busname);
167 snprintf(pidfile, sizeof(pidfile) - 1,
168 "%s/dhclient.%s.pid", STATEDIR, task->ifname);
169 snprintf(leases, sizeof(leases) - 1,
170 "%s/dhclient.%s.leases", STATEDIR, task->ifname);
171 snprintf(config, sizeof(config) - 1, "%s/dhclient.conf", SCRIPTDIR);
172 snprintf(script, sizeof(script) - 1, "%s/dhclient-script", SCRIPTDIR);
188 argv[14] = task->ifname;
193 if (g_spawn_async(NULL, argv, envp, G_SPAWN_DO_NOT_REAP_CHILD,
194 task_setup, task, &task->pid, NULL) == FALSE) {
195 printf("Failed to spawn dhclient\n");
199 tasks = g_slist_append(tasks, task);
201 g_child_watch_add(task->pid, task_died, task);
203 printf("[DHCP] executed %s with pid %d\n", DHCLIENT, task->pid);
208 static int dhclient_release(struct connman_iface *iface)
210 struct dhclient_task *task;
212 task = find_task_by_index(iface->index);
216 printf("[DHCP] release %s\n", task->ifname);
218 tasks = g_slist_remove(tasks, task);
225 static struct connman_dhcp_driver dhclient_driver = {
227 .request = dhclient_request,
228 .release = dhclient_release,
231 static DBusHandlerResult dhclient_filter(DBusConnection *conn,
232 DBusMessage *msg, void *data)
234 DBusMessageIter iter, dict;
236 struct dhclient_task *task;
237 struct connman_ipv4 ipv4;
238 const char *text, *key, *value;
240 if (dbus_message_is_method_call(msg, DHCLIENT_INTF, "notify") == FALSE)
241 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
243 memset(&ipv4, 0, sizeof(ipv4));
245 dbus_message_iter_init(msg, &iter);
247 dbus_message_iter_get_basic(&iter, &pid);
248 dbus_message_iter_next(&iter);
250 dbus_message_iter_get_basic(&iter, &text);
251 dbus_message_iter_next(&iter);
253 printf("[DHCP] change %d to %s\n", pid, text);
255 task = find_task_by_pid(pid);
257 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
259 dbus_message_iter_recurse(&iter, &dict);
261 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
262 DBusMessageIter entry;
264 dbus_message_iter_recurse(&dict, &entry);
265 dbus_message_iter_get_basic(&entry, &key);
266 dbus_message_iter_next(&entry);
267 dbus_message_iter_get_basic(&entry, &value);
269 printf("[DHCP] %s = %s\n", key, value);
271 if (strcmp(key, "new_ip_address") == 0)
272 inet_aton(value, &ipv4.address);
274 if (strcmp(key, "new_subnet_mask") == 0)
275 inet_aton(value, &ipv4.netmask);
277 if (strcmp(key, "new_routers") == 0)
278 inet_aton(value, &ipv4.gateway);
280 if (strcmp(key, "new_network_number") == 0)
281 inet_aton(value, &ipv4.network);
283 if (strcmp(key, "new_broadcast_address") == 0)
284 inet_aton(value, &ipv4.broadcast);
286 if (strcmp(key, "new_domain_name_servers") == 0)
287 inet_aton(value, &ipv4.nameserver);
289 dbus_message_iter_next(&dict);
292 if (strcmp(text, "PREINIT") == 0)
293 connman_dhcp_update(task->iface,
294 CONNMAN_DHCP_STATE_INIT, &ipv4);
295 else if (strcmp(text, "BOUND") == 0 || strcmp(text, "REBOOT") == 0)
296 connman_dhcp_update(task->iface,
297 CONNMAN_DHCP_STATE_BOUND, &ipv4);
298 else if (strcmp(text, "RENEW") == 0 || strcmp(text, "REBIND") == 0)
299 connman_dhcp_update(task->iface,
300 CONNMAN_DHCP_STATE_RENEW, &ipv4);
302 connman_dhcp_update(task->iface,
303 CONNMAN_DHCP_STATE_FAILED, NULL);
305 return DBUS_HANDLER_RESULT_HANDLED;
308 static DBusConnection *connection;
310 static int plugin_init(void)
314 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
316 busname = dbus_bus_get_unique_name(connection);
318 busname = "org.freedesktop.connman";
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)