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
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
34 #include <arpa/inet.h>
40 #include <connman/plugin.h>
41 #include <connman/dhcp.h>
43 static const char *busname;
45 struct dhclient_task {
49 struct connman_iface *iface;
52 static GSList *tasks = NULL;
54 static struct dhclient_task *find_task_by_pid(GPid pid)
58 for (list = tasks; list; list = list->next) {
59 struct dhclient_task *task = list->data;
68 static struct dhclient_task *find_task_by_index(int index)
72 for (list = tasks; list; list = list->next) {
73 struct dhclient_task *task = list->data;
75 if (task->ifindex == index)
82 static void kill_task(struct dhclient_task *task)
84 char pathname[PATH_MAX];
86 kill(task->pid, SIGTERM);
88 snprintf(pathname, sizeof(pathname) - 1,
89 "%s/dhclient.%s.pid", STATEDIR, task->ifname);
92 snprintf(pathname, sizeof(pathname) - 1,
93 "%s/dhclient.%s.leases", STATEDIR, task->ifname);
101 static int dhclient_request(struct connman_iface *iface)
104 struct dhclient_task *task;
105 char *argv[16], address[128], pidfile[PATH_MAX];
106 char leases[PATH_MAX], config[PATH_MAX], script[PATH_MAX];
109 sk = socket(PF_INET, SOCK_DGRAM, 0);
113 memset(&ifr, 0, sizeof(ifr));
114 ifr.ifr_ifindex = iface->index;
116 err = ioctl(sk, SIOCGIFNAME, &ifr);
123 task = g_try_new0(struct dhclient_task, 1);
127 task->ifindex = iface->index;
128 task->ifname = strdup(ifr.ifr_name);
131 if (task->ifname == NULL) {
136 printf("[DHCP] request %s\n", task->ifname);
138 snprintf(address, sizeof(address) - 1, "BUSNAME=%s", busname);
139 snprintf(pidfile, sizeof(pidfile) - 1,
140 "%s/dhclient.%s.pid", STATEDIR, task->ifname);
141 snprintf(leases, sizeof(leases) - 1,
142 "%s/dhclient.%s.leases", STATEDIR, task->ifname);
143 snprintf(config, sizeof(config) - 1, "%s/dhclient.conf", SCRIPTDIR);
144 snprintf(script, sizeof(script) - 1, "%s/dhclient-script", SCRIPTDIR);
146 argv[0] = "/sbin/dhclient";
160 argv[14] = task->ifname;
163 if (g_spawn_async(NULL, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD,
164 NULL, NULL, &task->pid, NULL) == FALSE) {
165 printf("Failed to spawn dhclient\n");
169 tasks = g_slist_append(tasks, task);
171 printf("[DHCP] executed with pid %d\n", task->pid);
176 static int dhclient_release(struct connman_iface *iface)
178 struct dhclient_task *task;
180 task = find_task_by_index(iface->index);
184 printf("[DHCP] release %s\n", task->ifname);
186 tasks = g_slist_remove(tasks, task);
193 static struct connman_dhcp_driver dhclient_driver = {
195 .request = dhclient_request,
196 .release = dhclient_release,
199 static DBusMessage *notify_method(DBusConnection *conn,
200 DBusMessage *msg, void *data)
202 DBusMessageIter iter, dict;
204 struct dhclient_task *task;
205 struct connman_ipv4 ipv4;
206 const char *text, *key, *value;
208 memset(&ipv4, 0, sizeof(ipv4));
210 dbus_message_iter_init(msg, &iter);
212 dbus_message_iter_get_basic(&iter, &pid);
213 dbus_message_iter_next(&iter);
215 dbus_message_iter_get_basic(&iter, &text);
216 dbus_message_iter_next(&iter);
218 printf("[DHCP] change %d to %s\n", pid, text);
220 task = find_task_by_pid(pid);
224 dbus_message_iter_recurse(&iter, &dict);
226 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
227 DBusMessageIter entry;
229 dbus_message_iter_recurse(&dict, &entry);
230 dbus_message_iter_get_basic(&entry, &key);
231 dbus_message_iter_next(&entry);
232 dbus_message_iter_get_basic(&entry, &value);
234 printf("[DHCP] %s = %s\n", key, value);
236 if (strcmp(key, "new_ip_address") == 0)
237 inet_aton(value, &ipv4.address);
239 if (strcmp(key, "new_subnet_mask") == 0)
240 inet_aton(value, &ipv4.netmask);
242 if (strcmp(key, "new_routers") == 0)
243 inet_aton(value, &ipv4.gateway);
245 if (strcmp(key, "new_network_number") == 0)
246 inet_aton(value, &ipv4.network);
248 if (strcmp(key, "new_broadcast_address") == 0)
249 inet_aton(value, &ipv4.broadcast);
251 if (strcmp(key, "new_domain_name_servers") == 0)
252 inet_aton(value, &ipv4.nameserver);
254 dbus_message_iter_next(&dict);
257 if (strcmp(text, "PREINIT") == 0)
258 connman_dhcp_update(task->iface,
259 CONNMAN_DHCP_STATE_INIT, &ipv4);
260 else if (strcmp(text, "BOUND") == 0 || strcmp(text, "REBOOT") == 0)
261 connman_dhcp_update(task->iface,
262 CONNMAN_DHCP_STATE_BOUND, &ipv4);
263 else if (strcmp(text, "RENEW") == 0 || strcmp(text, "REBIND") == 0)
264 connman_dhcp_update(task->iface,
265 CONNMAN_DHCP_STATE_RENEW, &ipv4);
267 connman_dhcp_update(task->iface,
268 CONNMAN_DHCP_STATE_FAILED, NULL);
273 static GDBusMethodTable dhclient_methods[] = {
274 { "notify", "usa{ss}", "", notify_method, G_DBUS_METHOD_FLAG_NOREPLY },
278 static DBusConnection *connection;
280 static int plugin_init(void)
282 connection = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL);
284 busname = dbus_bus_get_unique_name(connection);
286 g_dbus_register_object(connection, "/org/isc/dhclient", NULL, NULL);
288 g_dbus_register_interface(connection, "/org/isc/dhclient",
290 dhclient_methods, NULL, NULL);
292 connman_dhcp_register(&dhclient_driver);
297 static void plugin_exit(void)
301 for (list = tasks; list; list = list->next) {
302 struct dhclient_task *task = list->data;
304 printf("[DHCP] killing process %d\n", task->pid);
311 connman_dhcp_unregister(&dhclient_driver);
313 g_dbus_cleanup_connection(connection);
316 CONNMAN_PLUGIN_DEFINE("dhclient", "ISC DHCP client plugin", VERSION,
317 plugin_init, plugin_exit)