*
* Connection Manager
*
- * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
+ * Copyright (C) 2007-2013 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
#include <string.h>
#include <stdlib.h>
#include <gdbus.h>
+#include <gweb/gresolv.h>
#include "connman.h"
static GSList *driver_list = NULL;
-struct connman_route {
- int family;
- char *host;
- char *netmask;
- char *gateway;
-};
-
struct connman_provider {
- gint refcount;
+ int refcount;
+ bool immutable;
struct connman_service *vpn_service;
int index;
char *identifier;
- char *name;
- char *type;
- char *host;
- char *domain;
int family;
- GHashTable *routes;
struct connman_provider_driver *driver;
void *driver_data;
- GHashTable *setting_strings;
};
void __connman_provider_append_properties(struct connman_provider *provider,
DBusMessageIter *iter)
{
- if (provider->host != NULL)
+ const char *host, *domain, *type;
+
+ if (!provider->driver || !provider->driver->get_property)
+ return;
+
+ host = provider->driver->get_property(provider, "Host");
+ domain = provider->driver->get_property(provider, "Domain");
+ type = provider->driver->get_property(provider, "Type");
+
+ if (host)
connman_dbus_dict_append_basic(iter, "Host",
- DBUS_TYPE_STRING, &provider->host);
+ DBUS_TYPE_STRING, &host);
- if (provider->domain != NULL)
+ if (domain)
connman_dbus_dict_append_basic(iter, "Domain",
- DBUS_TYPE_STRING, &provider->domain);
+ DBUS_TYPE_STRING, &domain);
- if (provider->type != NULL)
+ if (type)
connman_dbus_dict_append_basic(iter, "Type", DBUS_TYPE_STRING,
- &provider->type);
+ &type);
}
-static struct connman_provider *connman_provider_lookup(const char *identifier)
+struct connman_provider *
+connman_provider_ref_debug(struct connman_provider *provider,
+ const char *file, int line, const char *caller)
{
- struct connman_provider *provider = NULL;
+ DBG("%p ref %d by %s:%d:%s()", provider, provider->refcount + 1,
+ file, line, caller);
- provider = g_hash_table_lookup(provider_hash, identifier);
+ __sync_fetch_and_add(&provider->refcount, 1);
return provider;
}
-static gboolean match_driver(struct connman_provider *provider,
- struct connman_provider_driver *driver)
-{
- if (g_strcmp0(driver->name, provider->type) == 0)
- return TRUE;
-
- return FALSE;
-}
-
-static int provider_probe(struct connman_provider *provider)
-{
- GSList *list;
-
- DBG("provider %p name %s", provider, provider->name);
-
- if (provider->driver != NULL)
- return -EALREADY;
-
- for (list = driver_list; list; list = list->next) {
- struct connman_provider_driver *driver = list->data;
-
- if (match_driver(provider, driver) == FALSE)
- continue;
-
- DBG("driver %p name %s", driver, driver->name);
-
- if (driver->probe != NULL && driver->probe(provider) == 0) {
- provider->driver = driver;
- break;
- }
- }
-
- if (provider->driver == NULL)
- return -ENODEV;
-
- return 0;
-}
-
static void provider_remove(struct connman_provider *provider)
{
- if (provider->driver != NULL) {
+ if (provider->driver) {
provider->driver->remove(provider);
provider->driver = NULL;
}
}
-static int provider_register(struct connman_provider *provider)
-{
- return provider_probe(provider);
-}
-
-static void provider_unregister(struct connman_provider *provider)
-{
- provider_remove(provider);
-}
-
-struct connman_provider *connman_provider_ref(struct connman_provider *provider)
-{
- DBG("provider %p refcount %d", provider, provider->refcount + 1);
-
- g_atomic_int_inc(&provider->refcount);
-
- return provider;
-}
-
static void provider_destruct(struct connman_provider *provider)
{
DBG("provider %p", provider);
- g_free(provider->name);
- g_free(provider->type);
- g_free(provider->host);
- g_free(provider->domain);
g_free(provider->identifier);
- g_hash_table_destroy(provider->routes);
- g_hash_table_destroy(provider->setting_strings);
+ g_free(provider);
}
-void connman_provider_unref(struct connman_provider *provider)
+void connman_provider_unref_debug(struct connman_provider *provider,
+ const char *file, int line, const char *caller)
{
- DBG("provider %p refcount %d", provider, provider->refcount - 1);
+ DBG("%p ref %d by %s:%d:%s()", provider, provider->refcount - 1,
+ file, line, caller);
- if (g_atomic_int_dec_and_test(&provider->refcount) == FALSE)
+ if (__sync_fetch_and_sub(&provider->refcount, 1) != 1)
return;
- provider_remove(provider);
-
provider_destruct(provider);
}
state, CONNMAN_IPCONFIG_TYPE_IPV6);
}
-int __connman_provider_disconnect(struct connman_provider *provider)
+int connman_provider_disconnect(struct connman_provider *provider)
{
int err;
DBG("provider %p", provider);
- if (provider->driver != NULL && provider->driver->disconnect != NULL)
+ if (provider->driver && provider->driver->disconnect)
err = provider->driver->disconnect(provider);
else
return -EOPNOTSUPP;
- if (provider->vpn_service != NULL)
+ if (provider->vpn_service)
provider_indicate_state(provider,
CONNMAN_SERVICE_STATE_DISCONNECT);
- if (err < 0) {
- if (err != -EINPROGRESS)
- return err;
+ if (err < 0)
+ return err;
- return -EINPROGRESS;
- }
+ if (provider->vpn_service)
+ provider_indicate_state(provider,
+ CONNMAN_SERVICE_STATE_IDLE);
+
+ return 0;
+}
+
+int connman_provider_remove(struct connman_provider *provider)
+{
+ DBG("Removing VPN %s", provider->identifier);
+
+ provider_remove(provider);
+
+ connman_provider_set_state(provider, CONNMAN_PROVIDER_STATE_IDLE);
+
+ g_hash_table_remove(provider_hash, provider->identifier);
return 0;
}
-int __connman_provider_connect(struct connman_provider *provider)
+int __connman_provider_connect(struct connman_provider *provider,
+ const char *dbus_sender)
{
int err;
DBG("provider %p", provider);
- if (provider->driver != NULL && provider->driver->connect != NULL)
- err = provider->driver->connect(provider);
+ if (provider->driver && provider->driver->connect)
+ err = provider->driver->connect(provider, dbus_sender);
else
return -EOPNOTSUPP;
return 0;
}
-int __connman_provider_remove(const char *path)
+int __connman_provider_remove_by_path(const char *path)
{
struct connman_provider *provider;
GHashTableIter iter;
DBG("path %s", path);
g_hash_table_iter_init(&iter, provider_hash);
- while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
+ while (g_hash_table_iter_next(&iter, &key, &value)) {
const char *srv_path;
provider = value;
- if (provider->vpn_service == NULL)
+ if (!provider->vpn_service)
continue;
srv_path = __connman_service_get_path(provider->vpn_service);
if (g_strcmp0(srv_path, path) == 0) {
DBG("Removing VPN %s", provider->identifier);
- provider_unregister(provider);
+ provider_remove(provider);
+
+ connman_provider_set_state(provider,
+ CONNMAN_PROVIDER_STATE_IDLE);
+
g_hash_table_remove(provider_hash,
provider->identifier);
return 0;
return -ENXIO;
}
-static void provider_append_routes(gpointer key, gpointer value,
- gpointer user_data)
-{
- struct connman_route *route = value;
- struct connman_provider *provider = user_data;
- int index = provider->index;
-
- if (route->family == AF_INET6) {
- unsigned char prefix_len = atoi(route->netmask);
-
- connman_inet_add_ipv6_network_route(index, route->host,
- route->gateway,
- prefix_len);
- } else {
- connman_inet_add_network_route(index, route->host,
- route->gateway,
- route->netmask);
- }
-}
-
static int set_connected(struct connman_provider *provider,
- connman_bool_t connected)
+ bool connected)
{
struct connman_service *service = provider->vpn_service;
struct connman_ipconfig *ipconfig;
- if (service == NULL)
+ if (!service)
return -ENODEV;
ipconfig = __connman_service_get_ipconfig(service, provider->family);
- if (connected == TRUE) {
- if (ipconfig == NULL) {
+ if (connected) {
+ if (!ipconfig) {
provider_indicate_state(provider,
CONNMAN_SERVICE_STATE_FAILURE);
return -EIO;
}
__connman_ipconfig_address_add(ipconfig);
+#if defined TIZEN_EXT
+ __connman_ipconfig_gateway_add(ipconfig, service);
+#else
__connman_ipconfig_gateway_add(ipconfig);
+#endif
provider_indicate_state(provider,
CONNMAN_SERVICE_STATE_READY);
- g_hash_table_foreach(provider->routes, provider_append_routes,
- provider);
+ if (provider->driver && provider->driver->set_routes)
+ provider->driver->set_routes(provider,
+ CONNMAN_PROVIDER_ROUTE_ALL);
} else {
- if (ipconfig != NULL) {
+ if (ipconfig) {
provider_indicate_state(provider,
CONNMAN_SERVICE_STATE_DISCONNECT);
__connman_ipconfig_gateway_remove(ipconfig);
int connman_provider_set_state(struct connman_provider *provider,
enum connman_provider_state state)
{
- if (provider == NULL || provider->vpn_service == NULL)
+ if (!provider || !provider->vpn_service)
return -EINVAL;
switch (state) {
case CONNMAN_PROVIDER_STATE_UNKNOWN:
return -EINVAL;
case CONNMAN_PROVIDER_STATE_IDLE:
- return set_connected(provider, FALSE);
+ return set_connected(provider, false);
case CONNMAN_PROVIDER_STATE_CONNECT:
return provider_indicate_state(provider,
CONNMAN_SERVICE_STATE_ASSOCIATION);
case CONNMAN_PROVIDER_STATE_READY:
- return set_connected(provider, TRUE);
+ return set_connected(provider, true);
case CONNMAN_PROVIDER_STATE_DISCONNECT:
return provider_indicate_state(provider,
CONNMAN_SERVICE_STATE_DISCONNECT);
service_error);
}
-static void unregister_provider(gpointer data)
+int connman_provider_create_service(struct connman_provider *provider)
{
- struct connman_provider *provider = data;
- struct connman_service *service = provider->vpn_service;
+ if (provider->vpn_service) {
+ bool connected;
- DBG("provider %p", provider);
+ connected = __connman_service_is_connected_state(
+ provider->vpn_service, CONNMAN_IPCONFIG_TYPE_IPV4);
+ if (connected)
+ return -EALREADY;
- provider->vpn_service = NULL;
- __connman_service_put(service);
+ connected = __connman_service_is_connected_state(
+ provider->vpn_service, CONNMAN_IPCONFIG_TYPE_IPV6);
+ if (connected)
+ return -EALREADY;
- connman_provider_unref(provider);
-}
+ return 0;
+ }
-static void destroy_route(gpointer user_data)
-{
- struct connman_route *route = user_data;
+ provider->vpn_service =
+ __connman_service_create_from_provider(provider);
- g_free(route->host);
- g_free(route->netmask);
- g_free(route->gateway);
- g_free(route);
-}
+ if (!provider->vpn_service) {
+ connman_warn("service creation failed for provider %s",
+ provider->identifier);
-static void provider_initialize(struct connman_provider *provider)
-{
- DBG("provider %p", provider);
+ g_hash_table_remove(provider_hash, provider->identifier);
+ return -EOPNOTSUPP;
+ }
- provider->index = 0;
- provider->name = NULL;
- provider->type = NULL;
- provider->domain = NULL;
- provider->identifier = NULL;
- provider->routes = g_hash_table_new_full(g_direct_hash, g_direct_equal,
- NULL, destroy_route);
- provider->setting_strings = g_hash_table_new_full(g_str_hash, g_str_equal,
- g_free, g_free);
+ return 0;
}
-static struct connman_provider *connman_provider_new(void)
-{
- struct connman_provider *provider;
+bool __connman_provider_is_immutable(struct connman_provider *provider)
- provider = g_try_new0(struct connman_provider, 1);
- if (provider == NULL)
- return NULL;
-
- provider->refcount = 1;
-
- DBG("provider %p", provider);
- provider_initialize(provider);
+{
+ if (provider)
+ return provider->immutable;
- return provider;
+ return false;
}
-static struct connman_provider *connman_provider_get(const char *identifier)
+int connman_provider_set_immutable(struct connman_provider *provider,
+ bool immutable)
{
- struct connman_provider *provider;
-
- provider = g_hash_table_lookup(provider_hash, identifier);
- if (provider != NULL)
- return provider;
-
- provider = connman_provider_new();
- if (provider == NULL)
- return NULL;
-
- DBG("provider %p", provider);
-
- provider->identifier = g_strdup(identifier);
-
- g_hash_table_insert(provider_hash, provider->identifier, provider);
+ if (!provider)
+ return -EINVAL;
- provider->name = g_strdup(identifier);
+ provider->immutable = immutable;
- return provider;
+ return 0;
}
-static void provider_dbus_ident(char *ident)
+static struct connman_provider *provider_lookup(const char *identifier)
{
- int i, len = strlen(ident);
-
- for (i = 0; i < len; i++) {
- if (ident[i] >= '0' && ident[i] <= '9')
- continue;
- if (ident[i] >= 'a' && ident[i] <= 'z')
- continue;
- if (ident[i] >= 'A' && ident[i] <= 'Z')
- continue;
- ident[i] = '_';
- }
+ return g_hash_table_lookup(provider_hash, identifier);
}
-int __connman_provider_create_and_connect(DBusMessage *msg)
+static void connection_ready(DBusMessage *msg, int error_code, void *user_data)
{
- struct connman_provider *provider;
- DBusMessageIter iter, array;
- const char *type = NULL, *name = NULL, *service_path;
- const char *host = NULL, *domain = NULL;
- char *ident;
- int err;
-
- dbus_message_iter_init(msg, &iter);
- dbus_message_iter_recurse(&iter, &array);
-
- while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
- DBusMessageIter entry, value;
- const char *key;
-
- dbus_message_iter_recurse(&array, &entry);
- dbus_message_iter_get_basic(&entry, &key);
-
- dbus_message_iter_next(&entry);
- dbus_message_iter_recurse(&entry, &value);
-
- switch (dbus_message_iter_get_arg_type(&value)) {
- case DBUS_TYPE_STRING:
- if (g_str_equal(key, "Type") == TRUE)
- dbus_message_iter_get_basic(&value, &type);
- else if (g_str_equal(key, "Name") == TRUE)
- dbus_message_iter_get_basic(&value, &name);
- else if (g_str_equal(key, "Host") == TRUE)
- dbus_message_iter_get_basic(&value, &host);
- else if (g_str_equal(key, "VPN.Domain") == TRUE)
- dbus_message_iter_get_basic(&value, &domain);
- break;
- }
-
- dbus_message_iter_next(&array);
- }
+ DBusMessage *reply;
+ const char *identifier = user_data;
- if (host == NULL || domain == NULL)
- return -EINVAL;
-
- DBG("Type %s name %s", type, name);
-
- if (type == NULL || name == NULL)
- return -EOPNOTSUPP;
+ DBG("msg %p error %d", msg, error_code);
- ident = g_strdup_printf("%s_%s", host, domain);
- provider_dbus_ident(ident);
-
- DBG("ident %s", ident);
-
- provider = connman_provider_lookup(ident);
- if (provider == NULL) {
- provider = connman_provider_get(ident);
- if (provider == NULL) {
- DBG("can not create provider");
- g_free(ident);
- return -EOPNOTSUPP;
+ if (error_code != 0) {
+ reply = __connman_error_failed(msg, -error_code);
+ if (!g_dbus_send_message(connection, reply))
+ DBG("reply %p send failed", reply);
+ } else {
+ const char *path;
+ struct connman_provider *provider;
+
+ provider = provider_lookup(identifier);
+ if (!provider) {
+ reply = __connman_error_failed(msg, EINVAL);
+ g_dbus_send_message(connection, reply);
+ return;
}
- provider->host = g_strdup(host);
- provider->domain = g_strdup(domain);
- g_free(provider->name);
- provider->name = g_strdup(name);
- provider->type = g_strdup(type);
-
- provider_register(provider);
- }
-
- dbus_message_iter_init(msg, &iter);
- dbus_message_iter_recurse(&iter, &array);
-
- while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
- DBusMessageIter entry, value;
- const char *key, *str;
-
- dbus_message_iter_recurse(&array, &entry);
- dbus_message_iter_get_basic(&entry, &key);
+ path = __connman_service_get_path(provider->vpn_service);
- dbus_message_iter_next(&entry);
- dbus_message_iter_recurse(&entry, &value);
-
- switch (dbus_message_iter_get_arg_type(&value)) {
- case DBUS_TYPE_STRING:
- dbus_message_iter_get_basic(&value, &str);
- connman_provider_set_string(provider, key, str);
- break;
- }
-
- dbus_message_iter_next(&array);
+ g_dbus_send_reply(connection, msg,
+ DBUS_TYPE_OBJECT_PATH, &path,
+ DBUS_TYPE_INVALID);
}
+}
- g_free(ident);
-
- if (provider->vpn_service == NULL) {
- provider->vpn_service =
- __connman_service_create_from_provider(provider);
- if (provider->vpn_service == NULL) {
- err = -EOPNOTSUPP;
- goto unref;
- }
-
- err = __connman_service_connect(provider->vpn_service);
- if (err < 0 && err != -EINPROGRESS)
- goto failed;
- } else
- DBG("provider already connected");
-
- service_path = __connman_service_get_path(provider->vpn_service);
- g_dbus_send_reply(connection, msg,
- DBUS_TYPE_OBJECT_PATH, &service_path,
- DBUS_TYPE_INVALID);
- return 0;
+int __connman_provider_create_and_connect(DBusMessage *msg)
+{
+ struct connman_provider_driver *driver;
-failed:
- __connman_service_put(provider->vpn_service);
- provider->vpn_service = NULL;
+ if (!driver_list)
+ return -EINVAL;
-unref:
- DBG("can not connect, delete provider");
+ driver = driver_list->data;
+ if (!driver || !driver->create)
+ return -EINVAL;
- connman_provider_unref(provider);
+ DBG("msg %p", msg);
- return err;
+ return driver->create(msg, connection_ready);
}
-const char * __connman_provider_get_ident(struct connman_provider *provider)
+const char *__connman_provider_get_ident(struct connman_provider *provider)
{
- if (provider == NULL)
+ if (!provider)
return NULL;
return provider->identifier;
int connman_provider_set_string(struct connman_provider *provider,
const char *key, const char *value)
{
- DBG("provider %p key %s value %s", provider, key, value);
-
- if (g_str_equal(key, "Type") == TRUE) {
- g_free(provider->type);
- provider->type = g_strdup(value);
- } else if (g_str_equal(key, "Name") == TRUE) {
- g_free(provider->name);
- provider->name = g_strdup(value);
- } else if (g_str_equal(key, "Host") == TRUE) {
- g_free(provider->host);
- provider->host = g_strdup(value);
- } else if (g_str_equal(key, "VPN.Domain") == TRUE) {
- g_free(provider->domain);
- provider->domain = g_strdup(value);
- } else
- g_hash_table_replace(provider->setting_strings,
- g_strdup(key), g_strdup(value));
+ if (provider->driver && provider->driver->set_property)
+ return provider->driver->set_property(provider, key, value);
+
return 0;
}
const char *connman_provider_get_string(struct connman_provider *provider,
const char *key)
{
- DBG("provider %p key %s", provider, key);
+ if (provider->driver && provider->driver->get_property)
+ return provider->driver->get_property(provider, key);
- if (g_str_equal(key, "Type") == TRUE)
- return provider->type;
- else if (g_str_equal(key, "Name") == TRUE)
- return provider->name;
- else if (g_str_equal(key, "Host") == TRUE)
- return provider->host;
- else if (g_str_equal(key, "VPN.Domain") == TRUE)
- return provider->domain;
+ return NULL;
+}
- return g_hash_table_lookup(provider->setting_strings, key);
+bool
+__connman_provider_check_routes(struct connman_provider *provider)
+{
+ if (!provider)
+ return false;
+
+ if (provider->driver && provider->driver->check_routes)
+ return provider->driver->check_routes(provider);
+
+ return false;
}
void *connman_provider_get_data(struct connman_provider *provider)
DBG("");
- if (service == NULL)
+ if (!service)
return;
ipconfig = __connman_service_get_ip4config(service);
- if (ipconfig == NULL) {
- __connman_service_create_ip4config(service, index);
+ if (!ipconfig) {
+ connman_service_create_ip4config(service, index);
ipconfig = __connman_service_get_ip4config(service);
- if (ipconfig == NULL) {
+ if (!ipconfig) {
DBG("Couldnt create ipconfig");
goto done;
}
}
- connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_FIXED);
+ __connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_OFF);
__connman_ipconfig_set_index(ipconfig, index);
-
ipconfig = __connman_service_get_ip6config(service);
- if (ipconfig == NULL) {
- __connman_service_create_ip6config(service, index);
+ if (!ipconfig) {
+ connman_service_create_ip6config(service, index);
ipconfig = __connman_service_get_ip6config(service);
- if (ipconfig == NULL) {
+ if (!ipconfig) {
DBG("Couldnt create ipconfig for IPv6");
goto done;
}
}
- connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_OFF);
+ __connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_OFF);
__connman_ipconfig_set_index(ipconfig, index);
done:
ipconfig = __connman_service_get_ipconfig(provider->vpn_service,
ipaddress->family);
- if (ipconfig == NULL)
+ if (!ipconfig)
return -EINVAL;
provider->family = ipaddress->family;
+ __connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_FIXED);
+
__connman_ipconfig_set_local(ipconfig, ipaddress->local);
__connman_ipconfig_set_peer(ipconfig, ipaddress->peer);
__connman_ipconfig_set_broadcast(ipconfig, ipaddress->broadcast);
{
DBG("provider %p domain %s", provider, domain);
- g_free(provider->domain);
- provider->domain = g_strdup(domain);
-
__connman_service_set_domainname(provider->vpn_service, domain);
return 0;
}
int connman_provider_set_nameservers(struct connman_provider *provider,
- const char *nameservers)
+ char * const *nameservers)
{
int i;
- char **nameservers_array = NULL;
- DBG("provider %p nameservers %s", provider, nameservers);
+ DBG("provider %p nameservers %p", provider, nameservers);
__connman_service_nameserver_clear(provider->vpn_service);
- if (nameservers == NULL)
+ if (!nameservers)
return 0;
- nameservers_array = g_strsplit(nameservers, " ", 0);
-
- for (i = 0; nameservers_array[i] != NULL; i++) {
+ for (i = 0; nameservers[i]; i++)
+#if defined TIZEN_EXT
__connman_service_nameserver_append(provider->vpn_service,
- nameservers_array[i]);
- }
-
- g_strfreev(nameservers_array);
+ nameservers[i], false,
+ CONNMAN_IPCONFIG_TYPE_ALL);
+#else
+ __connman_service_nameserver_append(provider->vpn_service,
+ nameservers[i], false);
+#endif
return 0;
}
-enum provider_route_type {
- PROVIDER_ROUTE_TYPE_NONE = 0,
- PROVIDER_ROUTE_TYPE_MASK = 1,
- PROVIDER_ROUTE_TYPE_ADDR = 2,
- PROVIDER_ROUTE_TYPE_GW = 3,
-};
+static void unregister_provider(gpointer data)
+{
+ struct connman_provider *provider = data;
-static int route_env_parse(struct connman_provider *provider, const char *key,
- int *family, unsigned long *idx,
- enum provider_route_type *type)
-{
- char *end;
- const char *start;
-
- DBG("name %s", provider->name);
-
- if (!strcmp(provider->type, "openvpn")) {
- if (g_str_has_prefix(key, "route_network_") == TRUE) {
- start = key + strlen("route_network_");
- *type = PROVIDER_ROUTE_TYPE_ADDR;
- } else if (g_str_has_prefix(key, "route_netmask_") == TRUE) {
- start = key + strlen("route_netmask_");
- *type = PROVIDER_ROUTE_TYPE_MASK;
- } else if (g_str_has_prefix(key, "route_gateway_") == TRUE) {
- start = key + strlen("route_gateway_");
- *type = PROVIDER_ROUTE_TYPE_GW;
- } else
- return -EINVAL;
-
- *family = AF_INET;
- *idx = g_ascii_strtoull(start, &end, 10);
-
- } else if (!strcmp(provider->type, "openconnect")) {
- if (g_str_has_prefix(key, "CISCO_SPLIT_INC_") == TRUE) {
- *family = AF_INET;
- start = key + strlen("CISCO_SPLIT_INC_");
- } else if (g_str_has_prefix(key, "CISCO_IPV6_SPLIT_INC_") == TRUE) {
- *family = AF_INET6;
- start = key + strlen("CISCO_IPV6_SPLIT_INC_");
- } else
- return -EINVAL;
-
- *idx = g_ascii_strtoull(start, &end, 10);
-
- if (strncmp(end, "_ADDR", 5) == 0)
- *type = PROVIDER_ROUTE_TYPE_ADDR;
- else if (strncmp(end, "_MASK", 5) == 0)
- *type = PROVIDER_ROUTE_TYPE_MASK;
- else if (strncmp(end, "_MASKLEN", 8) == 0 &&
- *family == AF_INET6) {
- *type = PROVIDER_ROUTE_TYPE_MASK;
- } else
- return -EINVAL;
+ DBG("provider %p service %p", provider, provider->vpn_service);
+
+ if (provider->vpn_service) {
+ connman_service_unref(provider->vpn_service);
+ provider->vpn_service = NULL;
}
- return 0;
+ connman_provider_unref(provider);
}
-int connman_provider_append_route(struct connman_provider *provider,
- const char *key, const char *value)
+static gint compare_priority(gconstpointer a, gconstpointer b)
{
- struct connman_route *route;
- int ret, family = 0;
- unsigned long idx = 0;
- enum provider_route_type type = PROVIDER_ROUTE_TYPE_NONE;
-
- DBG("key %s value %s", key, value);
+ return 0;
+}
- ret = route_env_parse(provider, key, &family, &idx, &type);
- if (ret < 0)
- return ret;
+int connman_provider_driver_register(struct connman_provider_driver *driver)
+{
+ DBG("driver %p name %s", driver, driver->name);
- DBG("idx %lu family %d type %d", idx, family, type);
+ driver_list = g_slist_insert_sorted(driver_list, driver,
+ compare_priority);
+ return 0;
+}
- route = g_hash_table_lookup(provider->routes, GINT_TO_POINTER(idx));
- if (route == NULL) {
- route = g_try_new0(struct connman_route, 1);
- if (route == NULL) {
- connman_error("out of memory");
- return -ENOMEM;
- }
+void connman_provider_driver_unregister(struct connman_provider_driver *driver)
+{
+ DBG("driver %p name %s", driver, driver->name);
- route->family = family;
+ driver_list = g_slist_remove(driver_list, driver);
+}
- g_hash_table_replace(provider->routes, GINT_TO_POINTER(idx),
- route);
- }
+void connman_provider_set_driver(struct connman_provider *provider,
+ struct connman_provider_driver *driver)
+{
+ provider->driver = driver;
+}
- switch (type) {
- case PROVIDER_ROUTE_TYPE_NONE:
- break;
- case PROVIDER_ROUTE_TYPE_MASK:
- route->netmask = g_strdup(value);
- break;
- case PROVIDER_ROUTE_TYPE_ADDR:
- route->host = g_strdup(value);
- break;
- case PROVIDER_ROUTE_TYPE_GW:
- route->gateway = g_strdup(value);
- break;
- }
+static void provider_disconnect_all(gpointer key, gpointer value,
+ gpointer user_data)
+{
+ struct connman_provider *provider = value;
- return 0;
+ connman_provider_disconnect(provider);
}
-const char *connman_provider_get_driver_name(struct connman_provider *provider)
+static void provider_offline_mode(bool enabled)
{
- if (provider->driver == NULL)
- return NULL;
+ DBG("enabled %d", enabled);
+
+ if (enabled)
+ g_hash_table_foreach(provider_hash, provider_disconnect_all,
+ NULL);
- return provider->driver->name;
}
-static gint compare_priority(gconstpointer a, gconstpointer b)
+static void provider_initialize(struct connman_provider *provider)
{
- return 0;
+ DBG("provider %p", provider);
+
+ provider->index = 0;
+ provider->identifier = NULL;
}
-static void clean_provider(gpointer key, gpointer value, gpointer user_data)
+static struct connman_provider *provider_new(void)
{
- struct connman_provider *provider = value;
+ struct connman_provider *provider;
- if (provider->driver != NULL && provider->driver->remove)
- provider->driver->remove(provider);
+ provider = g_try_new0(struct connman_provider, 1);
+ if (!provider)
+ return NULL;
+
+ provider->refcount = 1;
+
+ DBG("provider %p", provider);
+ provider_initialize(provider);
+
+ return provider;
}
-int connman_provider_driver_register(struct connman_provider_driver *driver)
+struct connman_provider *connman_provider_get(const char *identifier)
{
- DBG("driver %p name %s", driver, driver->name);
+ struct connman_provider *provider;
- driver_list = g_slist_insert_sorted(driver_list, driver,
- compare_priority);
- return 0;
+ provider = g_hash_table_lookup(provider_hash, identifier);
+ if (provider)
+ return provider;
+
+ provider = provider_new();
+ if (!provider)
+ return NULL;
+
+ DBG("provider %p", provider);
+
+ provider->identifier = g_strdup(identifier);
+
+ g_hash_table_insert(provider_hash, provider->identifier, provider);
+
+ return provider;
}
-void connman_provider_driver_unregister(struct connman_provider_driver *driver)
+void connman_provider_put(struct connman_provider *provider)
{
- DBG("driver %p name %s", driver, driver->name);
-
- driver_list = g_slist_remove(driver_list, driver);
+ g_hash_table_remove(provider_hash, provider->identifier);
}
-static void provider_remove_all(gpointer key, gpointer value,
- gpointer user_data)
+static struct connman_provider *provider_get(int index)
{
- struct connman_provider *provider = value;
+ GHashTableIter iter;
+ gpointer value, key;
+
+ g_hash_table_iter_init(&iter, provider_hash);
- __connman_provider_remove(provider->identifier);
+ while (g_hash_table_iter_next(&iter, &key, &value)) {
+ struct connman_provider *provider = value;
+
+ if (provider->index == index)
+ return provider;
+ }
+
+ return NULL;
}
-static void provider_offline_mode(connman_bool_t enabled)
+static void provider_service_changed(struct connman_service *service,
+ enum connman_service_state state)
{
- DBG("enabled %d", enabled);
+ struct connman_provider *provider;
+ int vpn_index, service_index;
+
+ if (!service)
+ return;
+
+ switch (state) {
+ case CONNMAN_SERVICE_STATE_UNKNOWN:
+ case CONNMAN_SERVICE_STATE_IDLE:
+ case CONNMAN_SERVICE_STATE_ASSOCIATION:
+ case CONNMAN_SERVICE_STATE_CONFIGURATION:
+ case CONNMAN_SERVICE_STATE_READY:
+ case CONNMAN_SERVICE_STATE_ONLINE:
+ return;
+ case CONNMAN_SERVICE_STATE_DISCONNECT:
+ case CONNMAN_SERVICE_STATE_FAILURE:
+ break;
+ }
+
+ service_index = __connman_service_get_index(service);
- if (enabled == TRUE)
- g_hash_table_foreach(provider_hash, provider_remove_all, NULL);
+ vpn_index = __connman_connection_get_vpn_index(service_index);
+ DBG("service %p %s state %d index %d/%d", service,
+ __connman_service_get_ident(service),
+ state, service_index, vpn_index);
+
+ if (vpn_index < 0)
+ return;
+
+ provider = provider_get(vpn_index);
+ if (!provider)
+ return;
+
+ DBG("disconnect %p index %d", provider, vpn_index);
+
+ connman_provider_disconnect(provider);
+
+ return;
}
static struct connman_notifier provider_notifier = {
.name = "provider",
.offline_mode = provider_offline_mode,
+ .service_state_changed = provider_service_changed,
};
int __connman_provider_init(void)
connman_notifier_unregister(&provider_notifier);
- g_hash_table_foreach(provider_hash, clean_provider, NULL);
-
g_hash_table_destroy(provider_hash);
provider_hash = NULL;