211b84efad59a8cf1ee60bcef411b7b0367c8239
[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/ioctl.h>
33 #include <sys/socket.h>
34 #include <arpa/inet.h>
35 #include <net/if.h>
36
37 #include <glib.h>
38 #include <gdbus.h>
39
40 #include <connman/plugin.h>
41 #include <connman/dhcp.h>
42
43 static const char *busname;
44
45 struct dhclient_task {
46         GPid pid;
47         int ifindex;
48         char *ifname;
49         struct connman_iface *iface;
50 };
51
52 static GSList *tasks = NULL;
53
54 static struct dhclient_task *find_task_by_pid(GPid pid)
55 {
56         GSList *list;
57
58         for (list = tasks; list; list = list->next) {
59                 struct dhclient_task *task = list->data;
60
61                 if (task->pid == pid)
62                         return task;
63         }
64
65         return NULL;
66 }
67
68 static struct dhclient_task *find_task_by_index(int index)
69 {
70         GSList *list;
71
72         for (list = tasks; list; list = list->next) {
73                 struct dhclient_task *task = list->data;
74
75                 if (task->ifindex == index)
76                         return task;
77         }
78
79         return NULL;
80 }
81
82 static void kill_task(struct dhclient_task *task)
83 {
84         char pathname[PATH_MAX];
85
86         kill(task->pid, SIGTERM);
87
88         snprintf(pathname, sizeof(pathname) - 1,
89                         "%s/dhclient.%s.pid", STATEDIR, task->ifname);
90         unlink(pathname);
91
92         snprintf(pathname, sizeof(pathname) - 1,
93                         "%s/dhclient.%s.leases", STATEDIR, task->ifname);
94         unlink(pathname);
95
96         free(task->ifname);
97
98         g_free(task);
99 }
100
101 static int dhclient_request(struct connman_iface *iface)
102 {
103         struct ifreq ifr;
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];
107         int sk, err;
108
109         sk = socket(PF_INET, SOCK_DGRAM, 0);
110         if (sk < 0)
111                 return -EIO;
112
113         memset(&ifr, 0, sizeof(ifr));
114         ifr.ifr_ifindex = iface->index;
115
116         err = ioctl(sk, SIOCGIFNAME, &ifr);
117
118         close(sk);
119
120         if (err < 0)
121                 return -EIO;
122
123         task = g_try_new0(struct dhclient_task, 1);
124         if (task == NULL)
125                 return -ENOMEM;
126
127         task->ifindex = iface->index;
128         task->ifname = strdup(ifr.ifr_name);
129         task->iface = iface;
130
131         if (task->ifname == NULL) {
132                 g_free(task);
133                 return -ENOMEM;
134         }
135
136         printf("[DHCP] request %s\n", task->ifname);
137
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);
145
146         argv[0] = "/sbin/dhclient";
147         argv[1] = "-d";
148         argv[2] = "-q";
149         argv[3] = "-n";
150         argv[4] = "-e";
151         argv[5] = address;
152         argv[6] = "-pf";
153         argv[7] = pidfile;
154         argv[8] = "-lf";
155         argv[9] = leases;
156         argv[10] = "-cf";
157         argv[11] = config;
158         argv[12] = "-sf";
159         argv[13] = script;
160         argv[14] = task->ifname;
161         argv[15] = NULL;
162
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");
166                 return -1;
167         }
168
169         tasks = g_slist_append(tasks, task);
170
171         printf("[DHCP] executed with pid %d\n", task->pid);
172
173         return 0;
174 }
175
176 static int dhclient_release(struct connman_iface *iface)
177 {
178         struct dhclient_task *task;
179
180         task = find_task_by_index(iface->index);
181         if (task == NULL)
182                 return -ENODEV;
183
184         printf("[DHCP] release %s\n", task->ifname);
185
186         tasks = g_slist_remove(tasks, task);
187
188         kill_task(task);
189
190         return 0;
191 }
192
193 static struct connman_dhcp_driver dhclient_driver = {
194         .name           = "dhclient",
195         .request        = dhclient_request,
196         .release        = dhclient_release,
197 };
198
199 static DBusMessage *notify_method(DBusConnection *conn,
200                                         DBusMessage *msg, void *data)
201 {
202         DBusMessageIter iter, dict;
203         dbus_uint32_t pid;
204         struct dhclient_task *task;
205         struct connman_ipv4 ipv4;
206         const char *text, *key, *value;
207
208         memset(&ipv4, 0, sizeof(ipv4));
209
210         dbus_message_iter_init(msg, &iter);
211
212         dbus_message_iter_get_basic(&iter, &pid);
213         dbus_message_iter_next(&iter);
214
215         dbus_message_iter_get_basic(&iter, &text);
216         dbus_message_iter_next(&iter);
217
218         printf("[DHCP] change %d to %s\n", pid, text);
219
220         task = find_task_by_pid(pid);
221         if (task == NULL)
222                 return NULL;
223
224         dbus_message_iter_recurse(&iter, &dict);
225
226         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
227                 DBusMessageIter entry;
228
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);
233
234                 printf("[DHCP] %s = %s\n", key, value);
235
236                 if (strcmp(key, "new_ip_address") == 0)
237                         inet_aton(value, &ipv4.address);
238
239                 if (strcmp(key, "new_subnet_mask") == 0)
240                         inet_aton(value, &ipv4.netmask);
241
242                 if (strcmp(key, "new_routers") == 0)
243                         inet_aton(value, &ipv4.gateway);
244
245                 if (strcmp(key, "new_network_number") == 0)
246                         inet_aton(value, &ipv4.network);
247
248                 if (strcmp(key, "new_broadcast_address") == 0)
249                         inet_aton(value, &ipv4.broadcast);
250
251                 if (strcmp(key, "new_domain_name_servers") == 0)
252                         inet_aton(value, &ipv4.nameserver);
253
254                 dbus_message_iter_next(&dict);
255         }
256
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);
266         else
267                 connman_dhcp_update(task->iface,
268                                         CONNMAN_DHCP_STATE_FAILED, NULL);
269
270         return NULL;
271 }
272
273 static GDBusMethodTable dhclient_methods[] = {
274         { "notify", "usa{ss}", "", notify_method, G_DBUS_METHOD_FLAG_NOREPLY },
275         { },
276 };
277
278 static DBusConnection *connection;
279
280 static int plugin_init(void)
281 {
282         connection = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, NULL);
283
284         busname = dbus_bus_get_unique_name(connection);
285
286         g_dbus_register_object(connection, "/org/isc/dhclient", NULL, NULL);
287
288         g_dbus_register_interface(connection, "/org/isc/dhclient",
289                                         "org.isc.dhclient",
290                                         dhclient_methods, NULL, NULL);
291
292         connman_dhcp_register(&dhclient_driver);
293
294         return 0;
295 }
296
297 static void plugin_exit(void)
298 {
299         GSList *list;
300
301         for (list = tasks; list; list = list->next) {
302                 struct dhclient_task *task = list->data;
303
304                 printf("[DHCP] killing process %d\n", task->pid);
305
306                 kill_task(task);
307         }
308
309         g_slist_free(tasks);
310
311         connman_dhcp_unregister(&dhclient_driver);
312
313         g_dbus_cleanup_connection(connection);
314 }
315
316 CONNMAN_PLUGIN_DEFINE("dhclient", "ISC DHCP client plugin", VERSION,
317                                                 plugin_init, plugin_exit)