task: use g_hash_table_replace instead of insert function
[framework/connectivity/connman.git] / plugins / pacrunner.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 <errno.h>
27 #include <string.h>
28
29 #include <gdbus.h>
30
31 #define CONNMAN_API_SUBJECT_TO_CHANGE
32 #include <connman/plugin.h>
33 #include <connman/notifier.h>
34 #include <connman/dbus.h>
35 #include <connman/log.h>
36 #include <connman/proxy.h>
37
38 #define PACRUNNER_SERVICE       "org.pacrunner"
39 #define PACRUNNER_INTERFACE     "org.pacrunner.Manager"
40 #define PACRUNNER_PATH          "/org/pacrunner/manager"
41
42 #define PACRUNNER_CLIENT_INTERFACE      "org.pacrunner.Client"
43 #define PACRUNNER_CLIENT_PATH           "/org/pacrunner/client"
44
45 #define DBUS_TIMEOUT    5000
46
47 struct proxy_data {
48         struct connman_service *service;
49         char *url;
50 };
51
52 static DBusConnection *connection;
53 static dbus_bool_t daemon_running = FALSE;
54
55 static struct connman_service *default_service = NULL;
56 static char *current_config = NULL;
57
58 static void create_config_reply(DBusPendingCall *call, void *user_data)
59 {
60         DBusMessage *reply = dbus_pending_call_steal_reply(call);
61         const char *path;
62
63         DBG("");
64
65         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
66                 connman_error("Failed to create proxy configuration");
67                 goto done;
68         }
69
70         if (dbus_message_get_args(reply, NULL, DBUS_TYPE_OBJECT_PATH, &path,
71                                                 DBUS_TYPE_INVALID) == FALSE)
72                 goto done;
73
74         g_free(current_config);
75         current_config = g_strdup(path);
76
77 done:
78         dbus_message_unref(reply);
79 }
80
81 static void append_string(DBusMessageIter *iter, void *user_data)
82 {
83         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, user_data);
84 }
85
86 static void append_string_list(DBusMessageIter *iter, void *user_data)
87 {
88         char **list = user_data;
89         int i;
90
91         for (i = 0; list[i] != NULL; i++)
92                 dbus_message_iter_append_basic(iter,
93                                         DBUS_TYPE_STRING, &list[i]);
94 }
95
96 static void create_proxy_configuration(void)
97 {
98         DBusMessage *msg;
99         DBusMessageIter iter, dict;
100         DBusPendingCall *call;
101         dbus_bool_t result;
102         char *interface;
103         const char *method;
104         const char *str;
105         char **str_list;
106
107         if (default_service == NULL)
108                 return;
109
110         DBG("");
111
112         msg = dbus_message_new_method_call(PACRUNNER_SERVICE, PACRUNNER_PATH,
113                         PACRUNNER_INTERFACE, "CreateProxyConfiguration");
114         if (msg == NULL)
115                 return;
116
117         dbus_message_set_auto_start(msg, FALSE);
118
119         dbus_message_iter_init_append(msg, &iter);
120         connman_dbus_dict_open(&iter, &dict);
121
122         switch(connman_service_get_proxy_method(default_service)) {
123         case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
124                 goto done;
125         case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
126                 method= "direct";
127                 break;
128         case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
129                 method = "manual";
130
131                 str_list = connman_service_get_proxy_servers(default_service);
132                 if (str_list == NULL)
133                         goto done;
134
135                 connman_dbus_dict_append_array(&dict, "Servers",
136                                         DBUS_TYPE_STRING, append_string_list,
137                                         str_list);
138                 g_strfreev(str_list);
139
140                 str_list = connman_service_get_proxy_excludes(default_service);
141                 if (str_list == NULL)
142                         break;
143
144                 connman_dbus_dict_append_array(&dict, "Excludes",
145                                         DBUS_TYPE_STRING, append_string_list,
146                                         str_list);
147                 g_strfreev(str_list);
148
149                 break;
150         case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
151                 method = "auto";
152
153                 str = connman_service_get_proxy_url(default_service);
154                 if (str == NULL) {
155                         str = connman_service_get_proxy_autoconfig(
156                                                         default_service);
157                         if (str == NULL)
158                                 goto done;
159                 }
160
161                 connman_dbus_dict_append_basic(&dict, "URL",
162                                         DBUS_TYPE_STRING, &str);
163                 break;
164         }
165
166         connman_dbus_dict_append_basic(&dict, "Method",
167                                 DBUS_TYPE_STRING, &method);
168
169         interface = connman_service_get_interface(default_service);
170         if (interface != NULL) {
171                 connman_dbus_dict_append_basic(&dict, "Interface",
172                                                 DBUS_TYPE_STRING, &interface);
173                 g_free(interface);
174         }
175
176         str = connman_service_get_domainname(default_service);
177         if (str != NULL)
178                 connman_dbus_dict_append_array(&dict, "Domains",
179                                         DBUS_TYPE_STRING, append_string, &str);
180
181         str_list = connman_service_get_nameservers(default_service);
182         if (str_list != NULL)
183                 connman_dbus_dict_append_array(&dict, "Nameservers",
184                                         DBUS_TYPE_STRING, append_string_list,
185                                         str_list);
186         g_strfreev(str_list);
187
188         connman_dbus_dict_close(&iter, &dict);
189
190         result = dbus_connection_send_with_reply(connection, msg,
191                                                         &call, DBUS_TIMEOUT);
192
193         if (result == FALSE || call == NULL)
194                 goto done;
195
196         dbus_pending_call_set_notify(call, create_config_reply, NULL, NULL);
197
198         dbus_pending_call_unref(call);
199
200 done:
201         dbus_message_unref(msg);
202 }
203
204 static void destroy_config_reply(DBusPendingCall *call, void *user_data)
205 {
206         DBusMessage *reply = dbus_pending_call_steal_reply(call);
207
208         DBG("");
209
210         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
211                 connman_error("Failed to destoy proxy configuration");
212
213         dbus_message_unref(reply);
214 }
215
216 static void destroy_proxy_configuration(void)
217 {
218         DBusMessage *msg;
219         DBusPendingCall *call;
220         dbus_bool_t result;
221
222         if (current_config == NULL)
223                 return;
224
225         DBG("");
226
227         msg = dbus_message_new_method_call(PACRUNNER_SERVICE, PACRUNNER_PATH,
228                         PACRUNNER_INTERFACE, "DestroyProxyConfiguration");
229         if (msg == NULL)
230                 return;
231
232         dbus_message_set_auto_start(msg, FALSE);
233
234         dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH, &current_config,
235                                                         DBUS_TYPE_INVALID);
236
237         result = dbus_connection_send_with_reply(connection, msg,
238                                                         &call, DBUS_TIMEOUT);
239
240         dbus_message_unref(msg);
241
242         if (result == FALSE || call == NULL)
243                 return;
244
245         dbus_pending_call_set_notify(call, destroy_config_reply, NULL, NULL);
246
247         dbus_pending_call_unref(call);
248
249         g_free(current_config);
250         current_config = NULL;
251 }
252
253 static void default_service_changed(struct connman_service *service)
254 {
255         DBG("service %p", service);
256
257         if (service == default_service)
258                 return;
259
260         default_service = service;
261
262         if (daemon_running == FALSE)
263                 return;
264
265         destroy_proxy_configuration();
266
267         create_proxy_configuration();
268 }
269
270 static void proxy_changed(struct connman_service *service)
271 {
272         DBG("service %p", service);
273
274         if (service != default_service)
275                 return;
276
277         if (daemon_running == FALSE)
278                 return;
279
280         destroy_proxy_configuration();
281
282         create_proxy_configuration();
283 }
284
285 static struct connman_notifier pacrunner_notifier = {
286         .name                   = "pacrunner",
287         .default_changed        = default_service_changed,
288         .proxy_changed          = proxy_changed,
289 };
290
291 static void pacrunner_connect(DBusConnection *conn, void *user_data)
292 {
293         DBG("");
294
295         daemon_running = TRUE;
296
297         create_proxy_configuration();
298 }
299
300 static void pacrunner_disconnect(DBusConnection *conn, void *user_data)
301 {
302         DBG("");
303
304         daemon_running = FALSE;
305
306         g_free(current_config);
307         current_config = NULL;
308 }
309
310 static char * parse_url(const char *url)
311 {
312         char *scheme, *host, *path, *host_ret;
313
314         scheme = g_strdup(url);
315         if (scheme == NULL)
316                 return NULL;
317
318         host = strstr(scheme, "://");
319         if (host != NULL) {
320                 *host = '\0';
321                 host += 3;
322         } else
323                 host = scheme;
324
325         path = strchr(host, '/');
326         if (path != NULL)
327                 *(path++) = '\0';
328
329         host_ret = g_strdup(host);
330
331         g_free(scheme);
332
333         return host_ret;
334 }
335
336 static void request_lookup_reply(DBusPendingCall *call, void *user_data)
337 {
338         DBusMessage *reply = dbus_pending_call_steal_reply(call);
339         struct proxy_data *data = user_data;
340         const char *proxy;
341
342         DBG("");
343
344         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
345                 connman_error("Failed to find URL:%s", data->url);
346                 proxy = NULL;
347                 goto done;
348         }
349
350         if (dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &proxy,
351                                                 DBUS_TYPE_INVALID) == FALSE)
352                 proxy = NULL;
353
354 done:
355         connman_proxy_driver_lookup_notify(data->service, data->url, proxy);
356
357         connman_service_unref(data->service);
358
359         g_free(data->url);
360         g_free(data);
361
362         dbus_message_unref(reply);
363 }
364
365 static int request_lookup(struct connman_service *service, const char *url)
366 {
367         DBusMessage *msg;
368         DBusPendingCall *call;
369         dbus_bool_t result;
370         char *host;
371         struct proxy_data *data;
372
373         DBG("");
374
375         if (daemon_running == FALSE)
376                 return -EINVAL;
377
378         msg = dbus_message_new_method_call(PACRUNNER_SERVICE,
379                                                 PACRUNNER_CLIENT_PATH,
380                                                 PACRUNNER_CLIENT_INTERFACE,
381                                                 "FindProxyForURL");
382         if (msg == NULL)
383                 return -1;
384
385         host = parse_url(url);
386         if (host == NULL) {
387                 dbus_message_unref(msg);
388                 return -EINVAL;
389         }
390
391         data = g_try_new0(struct proxy_data, 1);
392         if (data == NULL) {
393                 dbus_message_unref(msg);
394                 g_free(host);
395                 return -ENOMEM;
396         }
397
398         data->url = g_strdup(url);
399         data->service = connman_service_ref(service);
400
401         dbus_message_set_auto_start(msg, FALSE);
402
403         dbus_message_append_args(msg, DBUS_TYPE_STRING, &url,
404                                         DBUS_TYPE_STRING, &host,
405                                         DBUS_TYPE_INVALID);
406
407         result = dbus_connection_send_with_reply(connection, msg,
408                                                         &call, DBUS_TIMEOUT);
409
410         dbus_message_unref(msg);
411
412         if (result == FALSE || call == NULL) {
413                 g_free(host);
414                 g_free(data->url);
415                 g_free(data);
416                 return -EINVAL;
417         }
418
419         dbus_pending_call_set_notify(call, request_lookup_reply,
420                                                         data, NULL);
421
422         dbus_pending_call_unref(call);
423         g_free(host);
424
425         return 0;
426 }
427
428 static void cancel_lookup(struct connman_service *service, const char *url)
429 {
430         DBG("");
431 }
432
433 static struct connman_proxy_driver pacrunner_proxy = {
434         .name           = "pacrunnerproxy",
435         .priority       = CONNMAN_PROXY_PRIORITY_HIGH,
436         .request_lookup = request_lookup,
437         .cancel_lookup  = cancel_lookup,
438 };
439
440 static guint pacrunner_watch;
441
442 static int pacrunner_init(void)
443 {
444         connection = connman_dbus_get_connection();
445         if (connection == NULL)
446                 return -EIO;
447
448         pacrunner_watch = g_dbus_add_service_watch(connection,
449                                         PACRUNNER_SERVICE, pacrunner_connect,
450                                         pacrunner_disconnect, NULL, NULL);
451         if (pacrunner_watch == 0) {
452                 dbus_connection_unref(connection);
453                 return -EIO;
454         }
455
456         connman_notifier_register(&pacrunner_notifier);
457
458         connman_proxy_driver_register(&pacrunner_proxy);
459
460         return 0;
461 }
462
463 static void pacrunner_exit(void)
464 {
465         connman_proxy_driver_unregister(&pacrunner_proxy);
466
467         connman_notifier_unregister(&pacrunner_notifier);
468
469         g_dbus_remove_watch(connection, pacrunner_watch);
470
471         destroy_proxy_configuration();
472
473         dbus_connection_unref(connection);
474 }
475
476 CONNMAN_PLUGIN_DEFINE(pacrunner, "PAC runner proxy plugin", VERSION,
477                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, pacrunner_init, pacrunner_exit)