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