5 * Copyright (C) 2007-2013 Intel Corporation. All rights reserved.
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.
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.
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
31 #include <gweb/gresolv.h>
35 static DBusConnection *connection = NULL;
37 static GHashTable *provider_hash = NULL;
39 static GSList *driver_list = NULL;
41 struct connman_provider {
44 struct connman_service *vpn_service;
48 struct connman_provider_driver *driver;
52 void __connman_provider_append_properties(struct connman_provider *provider,
53 DBusMessageIter *iter)
55 const char *host, *domain, *type;
57 if (!provider->driver || !provider->driver->get_property)
60 host = provider->driver->get_property(provider, "Host");
61 domain = provider->driver->get_property(provider, "Domain");
62 type = provider->driver->get_property(provider, "Type");
65 connman_dbus_dict_append_basic(iter, "Host",
66 DBUS_TYPE_STRING, &host);
69 connman_dbus_dict_append_basic(iter, "Domain",
70 DBUS_TYPE_STRING, &domain);
73 connman_dbus_dict_append_basic(iter, "Type", DBUS_TYPE_STRING,
77 struct connman_provider *
78 connman_provider_ref_debug(struct connman_provider *provider,
79 const char *file, int line, const char *caller)
81 DBG("%p ref %d by %s:%d:%s()", provider, provider->refcount + 1,
84 __sync_fetch_and_add(&provider->refcount, 1);
89 static void provider_remove(struct connman_provider *provider)
91 if (provider->driver) {
92 provider->driver->remove(provider);
93 provider->driver = NULL;
97 static void provider_destruct(struct connman_provider *provider)
99 DBG("provider %p", provider);
101 g_free(provider->identifier);
105 void connman_provider_unref_debug(struct connman_provider *provider,
106 const char *file, int line, const char *caller)
108 DBG("%p ref %d by %s:%d:%s()", provider, provider->refcount - 1,
111 if (__sync_fetch_and_sub(&provider->refcount, 1) != 1)
114 provider_destruct(provider);
117 static int provider_indicate_state(struct connman_provider *provider,
118 enum connman_service_state state)
120 DBG("state %d", state);
122 __connman_service_ipconfig_indicate_state(provider->vpn_service, state,
123 CONNMAN_IPCONFIG_TYPE_IPV4);
125 return __connman_service_ipconfig_indicate_state(provider->vpn_service,
126 state, CONNMAN_IPCONFIG_TYPE_IPV6);
129 int connman_provider_disconnect(struct connman_provider *provider)
133 DBG("provider %p", provider);
135 if (provider->driver && provider->driver->disconnect)
136 err = provider->driver->disconnect(provider);
140 if (provider->vpn_service)
141 provider_indicate_state(provider,
142 CONNMAN_SERVICE_STATE_DISCONNECT);
147 if (provider->vpn_service)
148 provider_indicate_state(provider,
149 CONNMAN_SERVICE_STATE_IDLE);
154 int connman_provider_remove(struct connman_provider *provider)
156 DBG("Removing VPN %s", provider->identifier);
158 provider_remove(provider);
160 connman_provider_set_state(provider, CONNMAN_PROVIDER_STATE_IDLE);
162 g_hash_table_remove(provider_hash, provider->identifier);
167 int __connman_provider_connect(struct connman_provider *provider,
168 const char *dbus_sender)
172 DBG("provider %p", provider);
174 if (provider->driver && provider->driver->connect)
175 err = provider->driver->connect(provider, dbus_sender);
180 if (err != -EINPROGRESS)
183 provider_indicate_state(provider,
184 CONNMAN_SERVICE_STATE_ASSOCIATION);
192 int __connman_provider_remove_by_path(const char *path)
194 struct connman_provider *provider;
198 DBG("path %s", path);
200 g_hash_table_iter_init(&iter, provider_hash);
201 while (g_hash_table_iter_next(&iter, &key, &value)) {
202 const char *srv_path;
205 if (!provider->vpn_service)
208 srv_path = __connman_service_get_path(provider->vpn_service);
210 if (g_strcmp0(srv_path, path) == 0) {
211 DBG("Removing VPN %s", provider->identifier);
213 provider_remove(provider);
215 connman_provider_set_state(provider,
216 CONNMAN_PROVIDER_STATE_IDLE);
218 g_hash_table_remove(provider_hash,
219 provider->identifier);
227 static int set_connected(struct connman_provider *provider,
230 struct connman_service *service = provider->vpn_service;
231 struct connman_ipconfig *ipconfig;
236 ipconfig = __connman_service_get_ipconfig(service, provider->family);
240 provider_indicate_state(provider,
241 CONNMAN_SERVICE_STATE_FAILURE);
245 __connman_ipconfig_address_add(ipconfig);
246 __connman_ipconfig_gateway_add(ipconfig);
248 provider_indicate_state(provider,
249 CONNMAN_SERVICE_STATE_READY);
251 if (provider->driver && provider->driver->set_routes)
252 provider->driver->set_routes(provider,
253 CONNMAN_PROVIDER_ROUTE_ALL);
257 provider_indicate_state(provider,
258 CONNMAN_SERVICE_STATE_DISCONNECT);
259 __connman_ipconfig_gateway_remove(ipconfig);
262 provider_indicate_state(provider,
263 CONNMAN_SERVICE_STATE_IDLE);
269 int connman_provider_set_state(struct connman_provider *provider,
270 enum connman_provider_state state)
272 if (!provider || !provider->vpn_service)
276 case CONNMAN_PROVIDER_STATE_UNKNOWN:
278 case CONNMAN_PROVIDER_STATE_IDLE:
279 return set_connected(provider, false);
280 case CONNMAN_PROVIDER_STATE_CONNECT:
281 return provider_indicate_state(provider,
282 CONNMAN_SERVICE_STATE_ASSOCIATION);
283 case CONNMAN_PROVIDER_STATE_READY:
284 return set_connected(provider, true);
285 case CONNMAN_PROVIDER_STATE_DISCONNECT:
286 return provider_indicate_state(provider,
287 CONNMAN_SERVICE_STATE_DISCONNECT);
288 case CONNMAN_PROVIDER_STATE_FAILURE:
289 return provider_indicate_state(provider,
290 CONNMAN_SERVICE_STATE_FAILURE);
296 int connman_provider_indicate_error(struct connman_provider *provider,
297 enum connman_provider_error error)
299 enum connman_service_error service_error;
302 case CONNMAN_PROVIDER_ERROR_LOGIN_FAILED:
303 service_error = CONNMAN_SERVICE_ERROR_LOGIN_FAILED;
305 case CONNMAN_PROVIDER_ERROR_AUTH_FAILED:
306 service_error = CONNMAN_SERVICE_ERROR_AUTH_FAILED;
308 case CONNMAN_PROVIDER_ERROR_CONNECT_FAILED:
309 service_error = CONNMAN_SERVICE_ERROR_CONNECT_FAILED;
312 service_error = CONNMAN_SERVICE_ERROR_UNKNOWN;
316 return __connman_service_indicate_error(provider->vpn_service,
320 int connman_provider_create_service(struct connman_provider *provider)
322 if (provider->vpn_service) {
325 connected = __connman_service_is_connected_state(
326 provider->vpn_service, CONNMAN_IPCONFIG_TYPE_IPV4);
330 connected = __connman_service_is_connected_state(
331 provider->vpn_service, CONNMAN_IPCONFIG_TYPE_IPV6);
338 provider->vpn_service =
339 __connman_service_create_from_provider(provider);
341 if (!provider->vpn_service) {
342 connman_warn("service creation failed for provider %s",
343 provider->identifier);
345 g_hash_table_remove(provider_hash, provider->identifier);
352 bool __connman_provider_is_immutable(struct connman_provider *provider)
356 return provider->immutable;
361 int connman_provider_set_immutable(struct connman_provider *provider,
367 provider->immutable = immutable;
372 static struct connman_provider *provider_lookup(const char *identifier)
374 return g_hash_table_lookup(provider_hash, identifier);
377 static void connection_ready(DBusMessage *msg, int error_code, void *user_data)
380 const char *identifier = user_data;
382 DBG("msg %p error %d", msg, error_code);
384 if (error_code != 0) {
385 reply = __connman_error_failed(msg, -error_code);
386 if (!g_dbus_send_message(connection, reply))
387 DBG("reply %p send failed", reply);
390 struct connman_provider *provider;
392 provider = provider_lookup(identifier);
394 reply = __connman_error_failed(msg, EINVAL);
395 g_dbus_send_message(connection, reply);
399 path = __connman_service_get_path(provider->vpn_service);
401 g_dbus_send_reply(connection, msg,
402 DBUS_TYPE_OBJECT_PATH, &path,
407 int __connman_provider_create_and_connect(DBusMessage *msg)
409 struct connman_provider_driver *driver;
414 driver = driver_list->data;
415 if (!driver || !driver->create)
420 return driver->create(msg, connection_ready);
423 const char *__connman_provider_get_ident(struct connman_provider *provider)
428 return provider->identifier;
431 int connman_provider_set_string(struct connman_provider *provider,
432 const char *key, const char *value)
434 if (provider->driver && provider->driver->set_property)
435 return provider->driver->set_property(provider, key, value);
440 const char *connman_provider_get_string(struct connman_provider *provider,
443 if (provider->driver && provider->driver->get_property)
444 return provider->driver->get_property(provider, key);
450 __connman_provider_check_routes(struct connman_provider *provider)
455 if (provider->driver && provider->driver->check_routes)
456 return provider->driver->check_routes(provider);
461 void *connman_provider_get_data(struct connman_provider *provider)
463 return provider->driver_data;
466 void connman_provider_set_data(struct connman_provider *provider, void *data)
468 provider->driver_data = data;
471 void connman_provider_set_index(struct connman_provider *provider, int index)
473 struct connman_service *service = provider->vpn_service;
474 struct connman_ipconfig *ipconfig;
481 ipconfig = __connman_service_get_ip4config(service);
484 connman_service_create_ip4config(service, index);
486 ipconfig = __connman_service_get_ip4config(service);
488 DBG("Couldnt create ipconfig");
493 __connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_OFF);
494 __connman_ipconfig_set_index(ipconfig, index);
496 ipconfig = __connman_service_get_ip6config(service);
499 connman_service_create_ip6config(service, index);
501 ipconfig = __connman_service_get_ip6config(service);
503 DBG("Couldnt create ipconfig for IPv6");
508 __connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_OFF);
509 __connman_ipconfig_set_index(ipconfig, index);
512 provider->index = index;
515 int connman_provider_get_index(struct connman_provider *provider)
517 return provider->index;
520 int connman_provider_set_ipaddress(struct connman_provider *provider,
521 struct connman_ipaddress *ipaddress)
523 struct connman_ipconfig *ipconfig = NULL;
525 ipconfig = __connman_service_get_ipconfig(provider->vpn_service,
530 provider->family = ipaddress->family;
532 __connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_FIXED);
534 __connman_ipconfig_set_local(ipconfig, ipaddress->local);
535 __connman_ipconfig_set_peer(ipconfig, ipaddress->peer);
536 __connman_ipconfig_set_broadcast(ipconfig, ipaddress->broadcast);
537 __connman_ipconfig_set_gateway(ipconfig, ipaddress->gateway);
538 __connman_ipconfig_set_prefixlen(ipconfig, ipaddress->prefixlen);
543 int connman_provider_set_pac(struct connman_provider *provider, const char *pac)
545 DBG("provider %p pac %s", provider, pac);
547 __connman_service_set_pac(provider->vpn_service, pac);
553 int connman_provider_set_domain(struct connman_provider *provider,
556 DBG("provider %p domain %s", provider, domain);
558 __connman_service_set_domainname(provider->vpn_service, domain);
563 int connman_provider_set_nameservers(struct connman_provider *provider,
564 char * const *nameservers)
568 DBG("provider %p nameservers %p", provider, nameservers);
570 __connman_service_nameserver_clear(provider->vpn_service);
575 for (i = 0; nameservers[i]; i++)
576 __connman_service_nameserver_append(provider->vpn_service,
577 nameservers[i], false);
582 static void unregister_provider(gpointer data)
584 struct connman_provider *provider = data;
586 DBG("provider %p service %p", provider, provider->vpn_service);
588 if (provider->vpn_service) {
589 connman_service_unref(provider->vpn_service);
590 provider->vpn_service = NULL;
593 connman_provider_unref(provider);
596 static gint compare_priority(gconstpointer a, gconstpointer b)
601 int connman_provider_driver_register(struct connman_provider_driver *driver)
603 DBG("driver %p name %s", driver, driver->name);
605 driver_list = g_slist_insert_sorted(driver_list, driver,
610 void connman_provider_driver_unregister(struct connman_provider_driver *driver)
612 DBG("driver %p name %s", driver, driver->name);
614 driver_list = g_slist_remove(driver_list, driver);
617 void connman_provider_set_driver(struct connman_provider *provider,
618 struct connman_provider_driver *driver)
620 provider->driver = driver;
623 static void provider_disconnect_all(gpointer key, gpointer value,
626 struct connman_provider *provider = value;
628 connman_provider_disconnect(provider);
631 static void provider_offline_mode(bool enabled)
633 DBG("enabled %d", enabled);
636 g_hash_table_foreach(provider_hash, provider_disconnect_all,
641 static void provider_initialize(struct connman_provider *provider)
643 DBG("provider %p", provider);
646 provider->identifier = NULL;
649 static struct connman_provider *provider_new(void)
651 struct connman_provider *provider;
653 provider = g_try_new0(struct connman_provider, 1);
657 provider->refcount = 1;
659 DBG("provider %p", provider);
660 provider_initialize(provider);
665 struct connman_provider *connman_provider_get(const char *identifier)
667 struct connman_provider *provider;
669 provider = g_hash_table_lookup(provider_hash, identifier);
673 provider = provider_new();
677 DBG("provider %p", provider);
679 provider->identifier = g_strdup(identifier);
681 g_hash_table_insert(provider_hash, provider->identifier, provider);
686 void connman_provider_put(struct connman_provider *provider)
688 g_hash_table_remove(provider_hash, provider->identifier);
691 static struct connman_provider *provider_get(int index)
696 g_hash_table_iter_init(&iter, provider_hash);
698 while (g_hash_table_iter_next(&iter, &key, &value)) {
699 struct connman_provider *provider = value;
701 if (provider->index == index)
708 static void provider_service_changed(struct connman_service *service,
709 enum connman_service_state state)
711 struct connman_provider *provider;
712 int vpn_index, service_index;
718 case CONNMAN_SERVICE_STATE_UNKNOWN:
719 case CONNMAN_SERVICE_STATE_IDLE:
720 case CONNMAN_SERVICE_STATE_ASSOCIATION:
721 case CONNMAN_SERVICE_STATE_CONFIGURATION:
722 case CONNMAN_SERVICE_STATE_READY:
723 case CONNMAN_SERVICE_STATE_ONLINE:
725 case CONNMAN_SERVICE_STATE_DISCONNECT:
726 case CONNMAN_SERVICE_STATE_FAILURE:
730 service_index = __connman_service_get_index(service);
732 vpn_index = __connman_connection_get_vpn_index(service_index);
734 DBG("service %p %s state %d index %d/%d", service,
735 connman_service_get_identifier(service),
736 state, service_index, vpn_index);
741 provider = provider_get(vpn_index);
745 DBG("disconnect %p index %d", provider, vpn_index);
747 connman_provider_disconnect(provider);
750 static const struct connman_notifier provider_notifier = {
752 .offline_mode = provider_offline_mode,
753 .service_state_changed = provider_service_changed,
756 int __connman_provider_init(void)
762 connection = connman_dbus_get_connection();
764 provider_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
765 NULL, unregister_provider);
767 err = connman_notifier_register(&provider_notifier);
769 g_hash_table_destroy(provider_hash);
770 dbus_connection_unref(connection);
776 void __connman_provider_cleanup(void)
780 connman_notifier_unregister(&provider_notifier);
782 g_hash_table_destroy(provider_hash);
783 provider_hash = NULL;
785 dbus_connection_unref(connection);