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