Update DHCP plugin to use D-Bus low-level calls
[connman] / plugins / dhclient.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <signal.h>
31 #include <string.h>
32 #include <sys/wait.h>
33 #include <sys/ioctl.h>
34 #include <sys/socket.h>
35 #include <arpa/inet.h>
36 #include <net/if.h>
37
38 #include <glib.h>
39 #include <gdbus.h>
40
41 #include <connman/plugin.h>
42 #include <connman/dhcp.h>
43
44 #define DHCLIENT_INTF "org.isc.dhclient"
45 #define DHCLIENT_PATH "/org/isc/dhclient"
46
47 static const char *busname;
48
49 struct dhclient_task {
50         GPid pid;
51         int ifindex;
52         char *ifname;
53         struct connman_iface *iface;
54 };
55
56 static GSList *tasks = NULL;
57
58 static struct dhclient_task *find_task_by_pid(GPid pid)
59 {
60         GSList *list;
61
62         for (list = tasks; list; list = list->next) {
63                 struct dhclient_task *task = list->data;
64
65                 if (task->pid == pid)
66                         return task;
67         }
68
69         return NULL;
70 }
71
72 static struct dhclient_task *find_task_by_index(int index)
73 {
74         GSList *list;
75
76         for (list = tasks; list; list = list->next) {
77                 struct dhclient_task *task = list->data;
78
79                 if (task->ifindex == index)
80                         return task;
81         }
82
83         return NULL;
84 }
85
86 static void kill_task(struct dhclient_task *task)
87 {
88         char pathname[PATH_MAX];
89
90         if (task->pid > 0)
91                 kill(task->pid, SIGTERM);
92
93         snprintf(pathname, sizeof(pathname) - 1,
94                         "%s/dhclient.%s.pid", STATEDIR, task->ifname);
95         unlink(pathname);
96
97         snprintf(pathname, sizeof(pathname) - 1,
98                         "%s/dhclient.%s.leases", STATEDIR, task->ifname);
99         unlink(pathname);
100
101         free(task->ifname);
102
103         g_free(task);
104 }
105
106 static void task_died(GPid pid, gint status, gpointer data)
107 {
108         struct dhclient_task *task = data;
109
110         if (WIFEXITED(status))
111                 printf("[DHCP] exit status %d for %s\n",
112                                         WEXITSTATUS(status), task->ifname);
113         else
114                 printf("[DHCP] signal %d killed %s\n",
115                                         WTERMSIG(status), task->ifname);
116
117         g_spawn_close_pid(pid);
118         task->pid = 0;
119
120         tasks = g_slist_remove(tasks, task);
121
122         kill_task(task);
123 }
124
125 static void task_setup(gpointer data)
126 {
127         struct dhclient_task *task = data;
128
129         printf("[DHCP] setup %s\n", task->ifname);
130 }
131
132 static int dhclient_request(struct connman_iface *iface)
133 {
134         struct ifreq ifr;
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];
138         int sk, err;
139
140         sk = socket(PF_INET, SOCK_DGRAM, 0);
141         if (sk < 0)
142                 return -EIO;
143
144         memset(&ifr, 0, sizeof(ifr));
145         ifr.ifr_ifindex = iface->index;
146
147         err = ioctl(sk, SIOCGIFNAME, &ifr);
148
149         close(sk);
150
151         if (err < 0)
152                 return -EIO;
153
154         task = g_try_new0(struct dhclient_task, 1);
155         if (task == NULL)
156                 return -ENOMEM;
157
158         task->ifindex = iface->index;
159         task->ifname = strdup(ifr.ifr_name);
160         task->iface = iface;
161
162         if (task->ifname == NULL) {
163                 g_free(task);
164                 return -ENOMEM;
165         }
166
167         printf("[DHCP] request %s\n", task->ifname);
168
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);
176
177         argv[0] = DHCLIENT;
178         argv[1] = "-d";
179         argv[2] = "-q";
180         argv[3] = "-n";
181         argv[4] = "-e";
182         argv[5] = address;
183         argv[6] = "-pf";
184         argv[7] = pidfile;
185         argv[8] = "-lf";
186         argv[9] = leases;
187         argv[10] = "-cf";
188         argv[11] = config;
189         argv[12] = "-sf";
190         argv[13] = script;
191         argv[14] = task->ifname;
192         argv[15] = NULL;
193
194         envp[0] = NULL;
195
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");
199                 return -1;
200         }
201
202         tasks = g_slist_append(tasks, task);
203
204         g_child_watch_add(task->pid, task_died, task);
205
206         printf("[DHCP] executed %s with pid %d\n", DHCLIENT, task->pid);
207
208         return 0;
209 }
210
211 static int dhclient_release(struct connman_iface *iface)
212 {
213         struct dhclient_task *task;
214
215         task = find_task_by_index(iface->index);
216         if (task == NULL)
217                 return -ENODEV;
218
219         printf("[DHCP] release %s\n", task->ifname);
220
221         tasks = g_slist_remove(tasks, task);
222
223         kill_task(task);
224
225         return 0;
226 }
227
228 static struct connman_dhcp_driver dhclient_driver = {
229         .name           = "dhclient",
230         .request        = dhclient_request,
231         .release        = dhclient_release,
232 };
233
234 static DBusHandlerResult dhclient_filter(DBusConnection *conn,
235                                                 DBusMessage *msg, void *data)
236 {
237         DBusMessageIter iter, dict;
238         dbus_uint32_t pid;
239         struct dhclient_task *task;
240         struct connman_ipv4 ipv4;
241         const char *text, *key, *value;
242
243         if (dbus_message_is_method_call(msg, DHCLIENT_INTF, "notify") == FALSE)
244                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
245
246         memset(&ipv4, 0, sizeof(ipv4));
247
248         dbus_message_iter_init(msg, &iter);
249
250         dbus_message_iter_get_basic(&iter, &pid);
251         dbus_message_iter_next(&iter);
252
253         dbus_message_iter_get_basic(&iter, &text);
254         dbus_message_iter_next(&iter);
255
256         printf("[DHCP] change %d to %s\n", pid, text);
257
258         task = find_task_by_pid(pid);
259         if (task == NULL)
260                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
261
262         dbus_message_iter_recurse(&iter, &dict);
263
264         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
265                 DBusMessageIter entry;
266
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);
271
272                 printf("[DHCP] %s = %s\n", key, value);
273
274                 if (strcmp(key, "new_ip_address") == 0)
275                         inet_aton(value, &ipv4.address);
276
277                 if (strcmp(key, "new_subnet_mask") == 0)
278                         inet_aton(value, &ipv4.netmask);
279
280                 if (strcmp(key, "new_routers") == 0)
281                         inet_aton(value, &ipv4.gateway);
282
283                 if (strcmp(key, "new_network_number") == 0)
284                         inet_aton(value, &ipv4.network);
285
286                 if (strcmp(key, "new_broadcast_address") == 0)
287                         inet_aton(value, &ipv4.broadcast);
288
289                 if (strcmp(key, "new_domain_name_servers") == 0)
290                         inet_aton(value, &ipv4.nameserver);
291
292                 dbus_message_iter_next(&dict);
293         }
294
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);
304         else
305                 connman_dhcp_update(task->iface,
306                                         CONNMAN_DHCP_STATE_FAILED, NULL);
307
308         return DBUS_HANDLER_RESULT_HANDLED;
309 }
310
311 static DBusConnection *connection;
312
313 static int plugin_init(void)
314 {
315         gchar *filter;
316
317         connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
318
319         busname = dbus_bus_get_unique_name(connection);
320
321         busname = "org.freedesktop.connman";
322
323         dbus_connection_add_filter(connection, dhclient_filter, NULL, NULL);
324
325         filter = g_strdup_printf("interface=%s,path=%s",
326                                                 DHCLIENT_INTF, DHCLIENT_PATH);
327
328         dbus_bus_add_match(connection, filter, NULL);
329
330         g_free(filter);
331
332         connman_dhcp_register(&dhclient_driver);
333
334         return 0;
335 }
336
337 static void plugin_exit(void)
338 {
339         GSList *list;
340
341         for (list = tasks; list; list = list->next) {
342                 struct dhclient_task *task = list->data;
343
344                 printf("[DHCP] killing process %d\n", task->pid);
345
346                 kill_task(task);
347         }
348
349         g_slist_free(tasks);
350
351         connman_dhcp_unregister(&dhclient_driver);
352
353         dbus_connection_unref(connection);
354 }
355
356 CONNMAN_PLUGIN_DEFINE("dhclient", "ISC DHCP client plugin", VERSION,
357                                                 plugin_init, plugin_exit)