Switch dhclient handling to task infrastructure
[framework/connectivity/connman.git] / plugins / dhclient.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  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 <arpa/inet.h>
30
31 #include <glib.h>
32
33 #define CONNMAN_API_SUBJECT_TO_CHANGE
34 #include <connman/plugin.h>
35 #include <connman/dhcp.h>
36 #include <connman/task.h>
37 #include <connman/log.h>
38
39 #if 0
40 static unsigned char netmask2prefixlen(const char *netmask)
41 {
42         unsigned char bits = 0;
43         in_addr_t mask = inet_network(netmask);
44         in_addr_t host = ~mask;
45
46         /* a valid netmask must be 2^n - 1 */
47         if ((host & (host + 1)) != 0)
48                 return -1;
49
50         for (; mask; mask <<= 1)
51                 ++bits;
52
53         return bits;
54 }
55 #endif
56
57 static void dhclient_notify(struct connman_task *task,
58                                         DBusMessage *msg, void *user_data)
59 {
60         struct connman_dhcp *dhcp = user_data;
61         DBusMessageIter iter, dict;
62         dbus_uint32_t pid;
63         const char *text, *key, *value;
64
65         dbus_message_iter_init(msg, &iter);
66
67         dbus_message_iter_get_basic(&iter, &pid);
68         dbus_message_iter_next(&iter);
69
70         dbus_message_iter_get_basic(&iter, &text);
71         dbus_message_iter_next(&iter);
72
73         DBG("change %d to %s", pid, text);
74
75         dbus_message_iter_recurse(&iter, &dict);
76
77         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
78                 DBusMessageIter entry;
79
80                 dbus_message_iter_recurse(&dict, &entry);
81                 dbus_message_iter_get_basic(&entry, &key);
82                 dbus_message_iter_next(&entry);
83                 dbus_message_iter_get_basic(&entry, &value);
84
85                 DBG("%s = %s", key, value);
86
87                 if (g_strcmp0(key, "new_ip_address") == 0) {
88                         connman_dhcp_set_value(dhcp, "Address", value);
89                 } else if (g_strcmp0(key, "new_subnet_mask") == 0) {
90                         connman_dhcp_set_value(dhcp, "Netmask", value);
91                 } else if (g_strcmp0(key, "new_routers") == 0) {
92                         connman_dhcp_set_value(dhcp, "Gateway", value);
93                 } else if (g_strcmp0(key, "new_network_number") == 0) {
94                         connman_dhcp_set_value(dhcp, "Network", value);
95                 } else if (g_strcmp0(key, "new_broadcast_address") == 0) {
96                         connman_dhcp_set_value(dhcp, "Broadcast", value);
97                 } else if (g_strcmp0(key, "new_domain_name_servers") == 0) {
98                         connman_dhcp_set_value(dhcp, "Nameserver", value);
99                 } else if (g_ascii_strcasecmp(key, "new_domain_name") == 0) {
100                         connman_dhcp_set_value(dhcp, "Domainname", value);
101                 } else if (g_ascii_strcasecmp(key, "new_domain_search") == 0) {
102                 } else if (g_ascii_strcasecmp(key, "new_host_name") == 0) {
103                         connman_dhcp_set_value(dhcp, "Hostname", value);
104                 } else if (g_ascii_strcasecmp(key, "new_ntp_servers") == 0) {
105                         connman_dhcp_set_value(dhcp, "Timeserver", value);
106                 } else if (g_ascii_strcasecmp(key, "new_interface_mtu") == 0) {
107                         connman_dhcp_set_value(dhcp, "MTU", value);
108                 }
109
110                 dbus_message_iter_next(&dict);
111         }
112
113         if (g_strcmp0(text, "PREINIT") == 0) {
114         } else if (g_strcmp0(text, "BOUND") == 0 ||
115                                 g_strcmp0(text, "REBOOT") == 0) {
116                 connman_dhcp_bound(dhcp);
117         } else if (g_strcmp0(text, "RENEW") == 0 ||
118                                 g_strcmp0(text, "REBIND") == 0) {
119                 connman_dhcp_renew(dhcp);
120         } else if (g_strcmp0(text, "FAIL") == 0) {
121                 connman_dhcp_fail(dhcp);
122         } else {
123         }
124 }
125
126 struct dhclient_data {
127         struct connman_task *task;
128         struct connman_dhcp *dhcp;
129         char *ifname;
130 };
131
132 static void dhclient_died(struct connman_task *task, void *user_data)
133 {
134         struct dhclient_data *dhclient = user_data;
135
136         connman_dhcp_unref(dhclient->dhcp);
137
138         connman_task_destroy(dhclient->task);
139         dhclient->task = NULL;
140
141         g_free(dhclient->ifname);
142         g_free(dhclient);
143 }
144
145 static void dhclient_setup(struct connman_task *task, const char *ifname)
146 {
147         const char *path, *intf = "org.moblin.connman.Task";
148
149         path = connman_task_get_path(task);
150
151         connman_task_add_argument(task, "-d", NULL);
152         connman_task_add_argument(task, "-q", NULL);
153         connman_task_add_argument(task, "-e", "BUSNAME=org.moblin.connman");
154         connman_task_add_argument(task, "-e", "BUSINTF=%s", intf);
155         connman_task_add_argument(task, "-e", "BUSPATH=%s", path);
156         connman_task_add_argument(task, "-pf", "%s/dhclient.%s.pid",
157                                                         STATEDIR, ifname);
158         connman_task_add_argument(task, "-lf", "%s/dhclient.%s.leases",
159                                                         STATEDIR, ifname);
160         connman_task_add_argument(task, "-cf", "%s/dhclient.conf", SCRIPTDIR);
161         connman_task_add_argument(task, "-sf", "%s/dhclient-script", SCRIPTDIR);
162         connman_task_add_argument(task, ifname, NULL);
163         connman_task_add_argument(task, "-n", NULL);
164 }
165
166 static void dhclient_unlink(const char *ifname)
167 {
168         char *pathname;
169
170         pathname = g_strdup_printf("%s/dhclient.%s.pid",
171                                                 STATEDIR, ifname);
172         unlink(pathname);
173         g_free(pathname);
174
175         pathname = g_strdup_printf("%s/dhclient.%s.leases",
176                                                 STATEDIR, ifname);
177         unlink(pathname);
178         g_free(pathname);
179 }
180
181 static int dhclient_request(struct connman_dhcp *dhcp)
182 {
183         struct dhclient_data *dhclient;
184
185         DBG("dhcp %p", dhcp);
186
187         if (access(DHCLIENT, X_OK) < 0)
188                 return -EIO;
189
190         dhclient = g_try_new0(struct dhclient_data, 1);
191         if (dhclient == NULL)
192                 return -ENOMEM;
193
194         dhclient->task = connman_task_create(DHCLIENT);
195         if (dhclient->task == NULL) {
196                 g_free(dhclient);
197                 return -ENOMEM;
198         }
199
200         dhclient->dhcp = connman_dhcp_ref(dhcp);
201         dhclient->ifname = connman_dhcp_get_interface(dhcp);
202
203         dhclient_setup(dhclient->task, dhclient->ifname);
204
205         connman_dhcp_set_data(dhcp, dhclient);
206
207         connman_task_set_notify(dhclient->task, "Notify",
208                                                 dhclient_notify, dhcp);
209
210         connman_task_run(dhclient->task, dhclient_died, dhclient);
211
212         return 0;
213 }
214
215 static int dhclient_release(struct connman_dhcp *dhcp)
216 {
217         struct dhclient_data *dhclient = connman_dhcp_get_data(dhcp);
218
219         DBG("dhcp %p", dhcp);
220
221         if (dhclient->task != NULL)
222                 connman_task_stop(dhclient->task);
223
224         dhclient_unlink(dhclient->ifname);
225
226         return 0;
227 }
228
229 static struct connman_dhcp_driver dhclient_driver = {
230         .name           = "dhclient",
231         .request        = dhclient_request,
232         .release        = dhclient_release,
233 };
234
235 static int dhclient_init(void)
236 {
237         return connman_dhcp_driver_register(&dhclient_driver);
238 }
239
240 static void dhclient_exit(void)
241 {
242         connman_dhcp_driver_unregister(&dhclient_driver);
243 }
244
245 CONNMAN_PLUGIN_DEFINE(dhclient, "ISC DHCP client plugin", VERSION,
246                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, dhclient_init, dhclient_exit)