5 * Copyright (C) 2007-2012 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_route {
48 struct connman_provider {
50 struct connman_service *vpn_service;
59 struct connman_provider_driver *driver;
61 GHashTable *setting_strings;
62 GHashTable *user_routes;
63 gchar **user_networks;
64 gsize num_user_networks;
69 static void resolv_result(GResolvResultStatus status,
70 char **results, gpointer user_data)
72 struct connman_provider *provider = user_data;
74 DBG("status %d", status);
76 if (status == G_RESOLV_RESULT_STATUS_SUCCESS && results != NULL &&
77 g_strv_length(results) > 0)
78 provider->host_ip = g_strdupv(results);
80 connman_provider_unref(provider);
83 static void provider_resolv_host_addr(struct connman_provider *provider)
85 if (provider->host == NULL)
88 if (connman_inet_check_ipaddress(provider->host) > 0)
91 if (provider->host_ip != NULL)
95 * If the hostname is not numeric, try to resolv it. We do not wait
96 * the result as it might take some time. We will get the result
97 * before VPN will feed routes to us because VPN client will need
98 * the IP address also before VPN connection can be established.
100 provider->resolv = g_resolv_new(0);
101 if (provider->resolv == NULL) {
102 DBG("Cannot resolv %s", provider->host);
106 DBG("Trying to resolv %s", provider->host);
108 connman_provider_ref(provider);
110 g_resolv_lookup_hostname(provider->resolv, provider->host,
111 resolv_result, provider);
114 void __connman_provider_append_properties(struct connman_provider *provider,
115 DBusMessageIter *iter)
117 if (provider->host != NULL)
118 connman_dbus_dict_append_basic(iter, "Host",
119 DBUS_TYPE_STRING, &provider->host);
121 if (provider->domain != NULL)
122 connman_dbus_dict_append_basic(iter, "Domain",
123 DBUS_TYPE_STRING, &provider->domain);
125 if (provider->type != NULL)
126 connman_dbus_dict_append_basic(iter, "Type", DBUS_TYPE_STRING,
130 int __connman_provider_append_user_route(struct connman_provider *provider,
131 int family, const char *network, const char *netmask)
133 struct connman_route *route;
134 char *key = g_strdup_printf("%d/%s/%s", family, network, netmask);
136 DBG("family %d network %s netmask %s", family, network, netmask);
138 route = g_hash_table_lookup(provider->user_routes, key);
140 route = g_try_new0(struct connman_route, 1);
142 connman_error("out of memory");
146 route->family = family;
147 route->host = g_strdup(network);
148 route->netmask = g_strdup(netmask);
150 g_hash_table_replace(provider->user_routes, key, route);
157 static void set_user_networks(struct connman_provider *provider,
162 while (networks[i] != NULL) {
163 char **elems = g_strsplit(networks[i], "/", 0);
164 char *network, *netmask;
165 int family = PF_UNSPEC, ret;
171 if (network == NULL || *network == '\0') {
172 DBG("no network/netmask set");
178 if (netmask != NULL && *netmask == '\0') {
179 DBG("no netmask set");
184 if (g_strrstr(network, ":") != NULL)
186 else if (g_strrstr(network, ".") != NULL) {
189 if (g_strrstr(netmask, ".") == NULL) {
190 /* We have netmask length */
192 struct in_addr netmask_in;
193 unsigned char prefix_len = 32;
196 prefix_len = atoi(netmask);
198 addr = 0xffffffff << (32 - prefix_len);
199 netmask_in.s_addr = htonl(addr);
200 netmask = inet_ntoa(netmask_in);
202 DBG("network %s netmask %s", network, netmask);
206 ret = __connman_provider_append_user_route(provider,
207 family, network, netmask);
217 static int provider_load_from_keyfile(struct connman_provider *provider,
225 settings = g_key_file_get_keys(keyfile, provider->identifier, &length,
227 if (settings == NULL) {
228 g_key_file_free(keyfile);
232 while (idx < length) {
235 if (g_str_equal(key, "Networks") == TRUE) {
236 g_strfreev(provider->user_networks);
237 provider->user_networks =
238 g_key_file_get_string_list(keyfile,
239 provider->identifier,
241 &provider->num_user_networks,
244 value = g_key_file_get_string(keyfile,
245 provider->identifier,
247 connman_provider_set_string(provider, key,
254 g_strfreev(settings);
256 if (provider->user_networks != NULL)
257 set_user_networks(provider, provider->user_networks);
262 static int connman_provider_load(struct connman_provider *provider)
266 DBG("provider %p", provider);
268 keyfile = __connman_storage_load_provider(provider->identifier);
272 provider_load_from_keyfile(provider, keyfile);
274 g_key_file_free(keyfile);
278 static int connman_provider_save(struct connman_provider *provider)
282 DBG("provider %p", provider);
284 keyfile = g_key_file_new();
288 g_key_file_set_string(keyfile, provider->identifier,
289 "Name", provider->name);
290 g_key_file_set_string(keyfile, provider->identifier,
291 "Type", provider->type);
292 g_key_file_set_string(keyfile, provider->identifier,
293 "Host", provider->host);
294 g_key_file_set_string(keyfile, provider->identifier,
295 "VPN.Domain", provider->domain);
296 if (provider->user_networks != NULL)
297 g_key_file_set_string_list(keyfile, provider->identifier,
299 (const gchar **)provider->user_networks,
300 provider->num_user_networks);
302 if (provider->driver != NULL && provider->driver->save != NULL)
303 provider->driver->save(provider, keyfile);
305 __connman_storage_save_provider(keyfile, provider->identifier);
306 g_key_file_free(keyfile);
311 static struct connman_provider *connman_provider_lookup(const char *identifier)
313 struct connman_provider *provider = NULL;
315 provider = g_hash_table_lookup(provider_hash, identifier);
320 static gboolean match_driver(struct connman_provider *provider,
321 struct connman_provider_driver *driver)
323 if (g_strcmp0(driver->name, provider->type) == 0)
329 static int provider_probe(struct connman_provider *provider)
333 DBG("provider %p name %s", provider, provider->name);
335 if (provider->driver != NULL)
338 for (list = driver_list; list; list = list->next) {
339 struct connman_provider_driver *driver = list->data;
341 if (match_driver(provider, driver) == FALSE)
344 DBG("driver %p name %s", driver, driver->name);
346 if (driver->probe != NULL && driver->probe(provider) == 0) {
347 provider->driver = driver;
352 if (provider->driver == NULL)
358 static void provider_remove(struct connman_provider *provider)
360 if (provider->driver != NULL) {
361 provider->driver->remove(provider);
362 provider->driver = NULL;
366 static int provider_register(struct connman_provider *provider)
368 return provider_probe(provider);
371 static void provider_unregister(struct connman_provider *provider)
373 provider_remove(provider);
376 struct connman_provider *
377 connman_provider_ref_debug(struct connman_provider *provider,
378 const char *file, int line, const char *caller)
380 DBG("%p ref %d by %s:%d:%s()", provider, provider->refcount + 1,
383 __sync_fetch_and_add(&provider->refcount, 1);
388 static void provider_destruct(struct connman_provider *provider)
390 DBG("provider %p", provider);
392 g_free(provider->name);
393 g_free(provider->type);
394 g_free(provider->host);
395 g_free(provider->domain);
396 g_free(provider->identifier);
397 g_strfreev(provider->user_networks);
398 g_hash_table_destroy(provider->routes);
399 g_hash_table_destroy(provider->user_routes);
400 g_hash_table_destroy(provider->setting_strings);
401 if (provider->resolv != NULL) {
402 g_resolv_unref(provider->resolv);
403 provider->resolv = NULL;
405 g_strfreev(provider->host_ip);
409 void connman_provider_unref_debug(struct connman_provider *provider,
410 const char *file, int line, const char *caller)
412 DBG("%p ref %d by %s:%d:%s()", provider, provider->refcount - 1,
415 if (__sync_fetch_and_sub(&provider->refcount, 1) != 1)
418 provider_remove(provider);
420 provider_destruct(provider);
423 static int provider_indicate_state(struct connman_provider *provider,
424 enum connman_service_state state)
426 DBG("state %d", state);
428 __connman_service_ipconfig_indicate_state(provider->vpn_service, state,
429 CONNMAN_IPCONFIG_TYPE_IPV4);
431 return __connman_service_ipconfig_indicate_state(provider->vpn_service,
432 state, CONNMAN_IPCONFIG_TYPE_IPV6);
435 int __connman_provider_disconnect(struct connman_provider *provider)
439 DBG("provider %p", provider);
441 if (provider->driver != NULL && provider->driver->disconnect != NULL)
442 err = provider->driver->disconnect(provider);
446 if (provider->vpn_service != NULL)
447 provider_indicate_state(provider,
448 CONNMAN_SERVICE_STATE_DISCONNECT);
451 if (err != -EINPROGRESS)
460 int __connman_provider_connect(struct connman_provider *provider)
464 DBG("provider %p", provider);
466 if (provider->driver != NULL && provider->driver->connect != NULL) {
467 provider_resolv_host_addr(provider);
468 err = provider->driver->connect(provider);
473 if (err != -EINPROGRESS)
476 provider_indicate_state(provider,
477 CONNMAN_SERVICE_STATE_ASSOCIATION);
485 int __connman_provider_remove(const char *path)
487 struct connman_provider *provider;
491 DBG("path %s", path);
493 g_hash_table_iter_init(&iter, provider_hash);
494 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
495 const char *srv_path;
498 if (provider->vpn_service == NULL)
501 srv_path = __connman_service_get_path(provider->vpn_service);
503 if (g_strcmp0(srv_path, path) == 0) {
504 DBG("Removing VPN %s", provider->identifier);
506 provider_unregister(provider);
507 g_hash_table_remove(provider_hash,
508 provider->identifier);
516 static connman_bool_t check_host(char **hosts, char *host)
523 for (i = 0; hosts[i] != NULL; i++) {
524 if (g_strcmp0(hosts[i], host) == 0)
531 static void provider_append_routes(gpointer key, gpointer value,
534 struct connman_route *route = value;
535 struct connman_provider *provider = user_data;
536 int index = provider->index;
539 * If the VPN administrator/user has given a route to
540 * VPN server, then we must discard that because the
541 * server cannot be contacted via VPN tunnel.
543 if (check_host(provider->host_ip, route->host) == TRUE) {
544 DBG("Discarding VPN route to %s via %s at index %d",
545 route->host, route->gateway, index);
549 if (route->family == AF_INET6) {
550 unsigned char prefix_len = atoi(route->netmask);
552 connman_inet_add_ipv6_network_route(index, route->host,
556 connman_inet_add_network_route(index, route->host,
562 static int set_connected(struct connman_provider *provider,
563 connman_bool_t connected)
565 struct connman_service *service = provider->vpn_service;
566 struct connman_ipconfig *ipconfig;
571 ipconfig = __connman_service_get_ipconfig(service, provider->family);
573 if (connected == TRUE) {
574 if (ipconfig == NULL) {
575 provider_indicate_state(provider,
576 CONNMAN_SERVICE_STATE_FAILURE);
580 __connman_ipconfig_address_add(ipconfig);
581 __connman_ipconfig_gateway_add(ipconfig);
583 provider_indicate_state(provider,
584 CONNMAN_SERVICE_STATE_READY);
586 g_hash_table_foreach(provider->routes, provider_append_routes,
589 g_hash_table_foreach(provider->user_routes, provider_append_routes,
593 if (ipconfig != NULL) {
594 provider_indicate_state(provider,
595 CONNMAN_SERVICE_STATE_DISCONNECT);
596 __connman_ipconfig_gateway_remove(ipconfig);
599 provider_indicate_state(provider,
600 CONNMAN_SERVICE_STATE_IDLE);
606 int connman_provider_set_state(struct connman_provider *provider,
607 enum connman_provider_state state)
609 if (provider == NULL || provider->vpn_service == NULL)
613 case CONNMAN_PROVIDER_STATE_UNKNOWN:
615 case CONNMAN_PROVIDER_STATE_IDLE:
616 return set_connected(provider, FALSE);
617 case CONNMAN_PROVIDER_STATE_CONNECT:
618 return provider_indicate_state(provider,
619 CONNMAN_SERVICE_STATE_ASSOCIATION);
620 case CONNMAN_PROVIDER_STATE_READY:
621 return set_connected(provider, TRUE);
622 case CONNMAN_PROVIDER_STATE_DISCONNECT:
623 return provider_indicate_state(provider,
624 CONNMAN_SERVICE_STATE_DISCONNECT);
625 case CONNMAN_PROVIDER_STATE_FAILURE:
626 return provider_indicate_state(provider,
627 CONNMAN_SERVICE_STATE_FAILURE);
633 int connman_provider_indicate_error(struct connman_provider *provider,
634 enum connman_provider_error error)
636 enum connman_service_error service_error;
639 case CONNMAN_PROVIDER_ERROR_LOGIN_FAILED:
640 service_error = CONNMAN_SERVICE_ERROR_LOGIN_FAILED;
642 case CONNMAN_PROVIDER_ERROR_AUTH_FAILED:
643 service_error = CONNMAN_SERVICE_ERROR_AUTH_FAILED;
645 case CONNMAN_PROVIDER_ERROR_CONNECT_FAILED:
646 service_error = CONNMAN_SERVICE_ERROR_CONNECT_FAILED;
649 service_error = CONNMAN_SERVICE_ERROR_UNKNOWN;
653 return __connman_service_indicate_error(provider->vpn_service,
657 static void unregister_provider(gpointer data)
659 struct connman_provider *provider = data;
661 DBG("provider %p service %p", provider, provider->vpn_service);
663 if (provider->vpn_service != NULL) {
664 connman_service_unref(provider->vpn_service);
665 provider->vpn_service = NULL;
668 connman_provider_unref(provider);
671 static void destroy_route(gpointer user_data)
673 struct connman_route *route = user_data;
676 g_free(route->netmask);
677 g_free(route->gateway);
681 static void provider_initialize(struct connman_provider *provider)
683 DBG("provider %p", provider);
686 provider->name = NULL;
687 provider->type = NULL;
688 provider->domain = NULL;
689 provider->identifier = NULL;
690 provider->user_networks = NULL;
691 provider->routes = g_hash_table_new_full(g_direct_hash, g_direct_equal,
692 NULL, destroy_route);
693 provider->user_routes = g_hash_table_new_full(g_str_hash, g_str_equal,
694 g_free, destroy_route);
695 provider->setting_strings = g_hash_table_new_full(g_str_hash, g_str_equal,
699 static struct connman_provider *connman_provider_new(void)
701 struct connman_provider *provider;
703 provider = g_try_new0(struct connman_provider, 1);
704 if (provider == NULL)
707 provider->refcount = 1;
709 DBG("provider %p", provider);
710 provider_initialize(provider);
715 static struct connman_provider *connman_provider_get(const char *identifier)
717 struct connman_provider *provider;
719 provider = g_hash_table_lookup(provider_hash, identifier);
720 if (provider != NULL)
723 provider = connman_provider_new();
724 if (provider == NULL)
727 DBG("provider %p", provider);
729 provider->identifier = g_strdup(identifier);
731 g_hash_table_insert(provider_hash, provider->identifier, provider);
736 static void provider_dbus_ident(char *ident)
738 int i, len = strlen(ident);
740 for (i = 0; i < len; i++) {
741 if (ident[i] >= '0' && ident[i] <= '9')
743 if (ident[i] >= 'a' && ident[i] <= 'z')
745 if (ident[i] >= 'A' && ident[i] <= 'Z')
751 static struct connman_provider *provider_create_from_keyfile(GKeyFile *keyfile,
754 struct connman_provider *provider;
756 if (keyfile == NULL || ident == NULL)
759 provider = connman_provider_lookup(ident);
760 if (provider == NULL) {
761 provider = connman_provider_get(ident);
762 if (provider == NULL) {
763 DBG("can not create provider");
767 provider_load_from_keyfile(provider, keyfile);
769 if (provider->name == NULL || provider->host == NULL ||
770 provider->domain == NULL) {
771 DBG("cannot get name, host or domain");
772 connman_provider_unref(provider);
776 provider_register(provider);
781 static int provider_create_service(struct connman_provider *provider)
783 if (provider->vpn_service != NULL) {
784 connman_bool_t connected;
786 connected = __connman_service_is_connected_state(
787 provider->vpn_service, CONNMAN_IPCONFIG_TYPE_IPV4);
788 if (connected == TRUE)
791 connected = __connman_service_is_connected_state(
792 provider->vpn_service, CONNMAN_IPCONFIG_TYPE_IPV6);
793 if (connected == TRUE)
799 provider->vpn_service =
800 __connman_service_create_from_provider(provider);
802 if (provider->vpn_service == NULL)
808 static void provider_create_all_from_type(const char *provider_type)
814 struct connman_provider *provider;
816 DBG("provider type %s", provider_type);
818 providers = __connman_storage_get_providers();
820 for (i = 0; providers[i] != NULL; i+=1) {
822 if (strncmp(providers[i], "provider_", 9) != 0)
825 id = providers[i] + 9;
826 keyfile = __connman_storage_load_provider(id);
831 type = g_key_file_get_string(keyfile, id, "Type", NULL);
833 DBG("keyfile %p id %s type %s", keyfile, id, type);
835 if (strcmp(provider_type, type) != 0) {
837 g_key_file_free(keyfile);
841 provider = provider_create_from_keyfile(keyfile, id);
842 if (provider != NULL) {
843 if (provider_create_service(provider) == -EOPNOTSUPP) {
844 DBG("could not create service");
845 connman_provider_unref(provider);
850 g_key_file_free(keyfile);
852 g_strfreev(providers);
855 static char **get_user_networks(DBusMessageIter *array, int *count)
857 DBusMessageIter entry;
858 char **networks = NULL;
859 GSList *list = NULL, *l;
862 dbus_message_iter_recurse(array, &entry);
864 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
866 dbus_message_iter_get_basic(&entry, &val);
868 list = g_slist_prepend(list, g_strdup(val));
869 dbus_message_iter_next(&entry);
872 len = g_slist_length(list);
876 networks = g_try_new(char *, len + 1);
877 if (networks == NULL)
883 for (l = list; l != NULL; l = g_slist_next(l))
884 networks[--len] = l->data;
892 int __connman_provider_create_and_connect(DBusMessage *msg)
894 struct connman_provider *provider;
895 DBusMessageIter iter, array;
896 const char *type = NULL, *name = NULL, *service_path;
897 const char *host = NULL, *domain = NULL;
898 char **networks = NULL;
901 connman_bool_t connecting = FALSE;
903 dbus_message_iter_init(msg, &iter);
904 dbus_message_iter_recurse(&iter, &array);
906 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
907 DBusMessageIter entry, value;
910 dbus_message_iter_recurse(&array, &entry);
911 dbus_message_iter_get_basic(&entry, &key);
913 dbus_message_iter_next(&entry);
914 dbus_message_iter_recurse(&entry, &value);
916 switch (dbus_message_iter_get_arg_type(&value)) {
917 case DBUS_TYPE_STRING:
918 if (g_str_equal(key, "Type") == TRUE)
919 dbus_message_iter_get_basic(&value, &type);
920 else if (g_str_equal(key, "Name") == TRUE)
921 dbus_message_iter_get_basic(&value, &name);
922 else if (g_str_equal(key, "Host") == TRUE)
923 dbus_message_iter_get_basic(&value, &host);
924 else if (g_str_equal(key, "VPN.Domain") == TRUE)
925 dbus_message_iter_get_basic(&value, &domain);
927 case DBUS_TYPE_ARRAY:
928 if (g_str_equal(key, "Networks") == TRUE)
929 networks = get_user_networks(&value, &count);
933 dbus_message_iter_next(&array);
936 if (host == NULL || domain == NULL)
939 DBG("Type %s name %s networks %p", type, name, networks);
941 if (type == NULL || name == NULL)
944 ident = g_strdup_printf("%s_%s", host, domain);
945 provider_dbus_ident(ident);
947 DBG("ident %s", ident);
949 provider = connman_provider_lookup(ident);
950 if (provider == NULL) {
951 provider = connman_provider_get(ident);
952 if (provider == NULL) {
953 DBG("can not create provider");
958 provider->host = g_strdup(host);
959 provider->domain = g_strdup(domain);
960 provider->name = g_strdup(name);
961 provider->type = g_strdup(type);
963 if (provider_register(provider) == 0)
964 connman_provider_load(provider);
966 provider_resolv_host_addr(provider);
969 if (networks != NULL) {
970 g_strfreev(provider->user_networks);
971 provider->user_networks = networks;
972 provider->num_user_networks = count;
973 set_user_networks(provider, provider->user_networks);
976 dbus_message_iter_init(msg, &iter);
977 dbus_message_iter_recurse(&iter, &array);
979 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
980 DBusMessageIter entry, value;
981 const char *key, *str;
983 dbus_message_iter_recurse(&array, &entry);
984 dbus_message_iter_get_basic(&entry, &key);
986 dbus_message_iter_next(&entry);
987 dbus_message_iter_recurse(&entry, &value);
989 switch (dbus_message_iter_get_arg_type(&value)) {
990 case DBUS_TYPE_STRING:
991 dbus_message_iter_get_basic(&value, &str);
992 connman_provider_set_string(provider, key, str);
996 dbus_message_iter_next(&array);
1001 err = provider_create_service(provider);
1002 if (err == -EALREADY) {
1003 DBG("provider already connected");
1005 if (err == -EOPNOTSUPP) {
1008 if (__connman_service_is_provider_pending(
1009 provider->vpn_service) == TRUE) {
1010 DBG("Provider %p connection already pending",
1012 dbus_message_ref(msg);
1013 __connman_service_reply_dbus_pending(msg,
1018 err = __connman_service_connect(provider->vpn_service);
1020 if (err < 0 && err != -EINPROGRESS)
1023 if (err == -EINPROGRESS)
1028 if (connecting == TRUE) {
1030 * Save the dbus message so that we can reply the
1031 * caller when VPN connection is established.
1033 dbus_message_ref(msg);
1035 __connman_service_set_provider_pending(provider->vpn_service,
1037 connman_provider_save(provider);
1042 service_path = __connman_service_get_path(provider->vpn_service);
1043 g_dbus_send_reply(connection, msg,
1044 DBUS_TYPE_OBJECT_PATH, &service_path,
1049 connman_service_unref(provider->vpn_service);
1050 provider->vpn_service = NULL;
1053 DBG("can not connect, delete provider");
1055 g_hash_table_remove(provider_hash, provider->identifier);
1060 const char * __connman_provider_get_ident(struct connman_provider *provider)
1062 if (provider == NULL)
1065 return provider->identifier;
1068 int connman_provider_set_string(struct connman_provider *provider,
1069 const char *key, const char *value)
1071 DBG("provider %p key %s value %s", provider, key, value);
1073 if (g_str_equal(key, "Type") == TRUE) {
1074 g_free(provider->type);
1075 provider->type = g_strdup(value);
1076 } else if (g_str_equal(key, "Name") == TRUE) {
1077 g_free(provider->name);
1078 provider->name = g_strdup(value);
1079 } else if (g_str_equal(key, "Host") == TRUE) {
1080 g_free(provider->host);
1081 provider->host = g_strdup(value);
1082 } else if (g_str_equal(key, "VPN.Domain") == TRUE) {
1083 g_free(provider->domain);
1084 provider->domain = g_strdup(value);
1086 g_hash_table_replace(provider->setting_strings,
1087 g_strdup(key), g_strdup(value));
1091 const char *connman_provider_get_string(struct connman_provider *provider,
1094 DBG("provider %p key %s", provider, key);
1096 if (g_str_equal(key, "Type") == TRUE)
1097 return provider->type;
1098 else if (g_str_equal(key, "Name") == TRUE)
1099 return provider->name;
1100 else if (g_str_equal(key, "Host") == TRUE)
1101 return provider->host;
1102 else if (g_str_equal(key, "HostIP") == TRUE) {
1103 if (provider->host_ip == NULL ||
1104 provider->host_ip[0] == NULL)
1105 return provider->host;
1107 return provider->host_ip[0];
1108 } else if (g_str_equal(key, "VPN.Domain") == TRUE)
1109 return provider->domain;
1111 return g_hash_table_lookup(provider->setting_strings, key);
1115 __connman_provider_check_routes(struct connman_provider *provider)
1117 if (provider == NULL)
1120 if (provider->user_routes != NULL &&
1121 g_hash_table_size(provider->user_routes) > 0)
1124 if (provider->routes != NULL &&
1125 g_hash_table_size(provider->routes) > 0)
1131 void *connman_provider_get_data(struct connman_provider *provider)
1133 return provider->driver_data;
1136 void connman_provider_set_data(struct connman_provider *provider, void *data)
1138 provider->driver_data = data;
1141 void connman_provider_set_index(struct connman_provider *provider, int index)
1143 struct connman_service *service = provider->vpn_service;
1144 struct connman_ipconfig *ipconfig;
1148 if (service == NULL)
1151 ipconfig = __connman_service_get_ip4config(service);
1153 if (ipconfig == NULL) {
1154 connman_service_create_ip4config(service, index);
1156 ipconfig = __connman_service_get_ip4config(service);
1157 if (ipconfig == NULL) {
1158 DBG("Couldnt create ipconfig");
1163 __connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_FIXED);
1164 __connman_ipconfig_set_index(ipconfig, index);
1167 ipconfig = __connman_service_get_ip6config(service);
1169 if (ipconfig == NULL) {
1170 connman_service_create_ip6config(service, index);
1172 ipconfig = __connman_service_get_ip6config(service);
1173 if (ipconfig == NULL) {
1174 DBG("Couldnt create ipconfig for IPv6");
1179 __connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_OFF);
1180 __connman_ipconfig_set_index(ipconfig, index);
1183 provider->index = index;
1186 int connman_provider_get_index(struct connman_provider *provider)
1188 return provider->index;
1191 int connman_provider_set_ipaddress(struct connman_provider *provider,
1192 struct connman_ipaddress *ipaddress)
1194 struct connman_ipconfig *ipconfig = NULL;
1196 ipconfig = __connman_service_get_ipconfig(provider->vpn_service,
1198 if (ipconfig == NULL)
1201 provider->family = ipaddress->family;
1203 __connman_ipconfig_set_local(ipconfig, ipaddress->local);
1204 __connman_ipconfig_set_peer(ipconfig, ipaddress->peer);
1205 __connman_ipconfig_set_broadcast(ipconfig, ipaddress->broadcast);
1206 __connman_ipconfig_set_gateway(ipconfig, ipaddress->gateway);
1207 __connman_ipconfig_set_prefixlen(ipconfig, ipaddress->prefixlen);
1212 int connman_provider_set_pac(struct connman_provider *provider, const char *pac)
1214 DBG("provider %p pac %s", provider, pac);
1216 __connman_service_set_pac(provider->vpn_service, pac);
1222 int connman_provider_set_domain(struct connman_provider *provider,
1225 DBG("provider %p domain %s", provider, domain);
1227 g_free(provider->domain);
1228 provider->domain = g_strdup(domain);
1230 __connman_service_set_domainname(provider->vpn_service, domain);
1235 int connman_provider_set_nameservers(struct connman_provider *provider,
1236 const char *nameservers)
1239 char **nameservers_array = NULL;
1241 DBG("provider %p nameservers %s", provider, nameservers);
1243 __connman_service_nameserver_clear(provider->vpn_service);
1245 if (nameservers == NULL)
1248 nameservers_array = g_strsplit(nameservers, " ", 0);
1250 for (i = 0; nameservers_array[i] != NULL; i++) {
1251 __connman_service_nameserver_append(provider->vpn_service,
1252 nameservers_array[i], FALSE);
1255 g_strfreev(nameservers_array);
1260 enum provider_route_type {
1261 PROVIDER_ROUTE_TYPE_NONE = 0,
1262 PROVIDER_ROUTE_TYPE_MASK = 1,
1263 PROVIDER_ROUTE_TYPE_ADDR = 2,
1264 PROVIDER_ROUTE_TYPE_GW = 3,
1267 static int route_env_parse(struct connman_provider *provider, const char *key,
1268 int *family, unsigned long *idx,
1269 enum provider_route_type *type)
1274 DBG("name %s", provider->name);
1276 if (!strcmp(provider->type, "openvpn")) {
1277 if (g_str_has_prefix(key, "route_network_") == TRUE) {
1278 start = key + strlen("route_network_");
1279 *type = PROVIDER_ROUTE_TYPE_ADDR;
1280 } else if (g_str_has_prefix(key, "route_netmask_") == TRUE) {
1281 start = key + strlen("route_netmask_");
1282 *type = PROVIDER_ROUTE_TYPE_MASK;
1283 } else if (g_str_has_prefix(key, "route_gateway_") == TRUE) {
1284 start = key + strlen("route_gateway_");
1285 *type = PROVIDER_ROUTE_TYPE_GW;
1290 *idx = g_ascii_strtoull(start, &end, 10);
1292 } else if (!strcmp(provider->type, "openconnect")) {
1293 if (g_str_has_prefix(key, "CISCO_SPLIT_INC_") == TRUE) {
1295 start = key + strlen("CISCO_SPLIT_INC_");
1296 } else if (g_str_has_prefix(key, "CISCO_IPV6_SPLIT_INC_") == TRUE) {
1298 start = key + strlen("CISCO_IPV6_SPLIT_INC_");
1302 *idx = g_ascii_strtoull(start, &end, 10);
1304 if (strncmp(end, "_ADDR", 5) == 0)
1305 *type = PROVIDER_ROUTE_TYPE_ADDR;
1306 else if (strncmp(end, "_MASK", 5) == 0)
1307 *type = PROVIDER_ROUTE_TYPE_MASK;
1308 else if (strncmp(end, "_MASKLEN", 8) == 0 &&
1309 *family == AF_INET6) {
1310 *type = PROVIDER_ROUTE_TYPE_MASK;
1318 int connman_provider_append_route(struct connman_provider *provider,
1319 const char *key, const char *value)
1321 struct connman_route *route;
1322 int ret, family = 0;
1323 unsigned long idx = 0;
1324 enum provider_route_type type = PROVIDER_ROUTE_TYPE_NONE;
1326 DBG("key %s value %s", key, value);
1328 ret = route_env_parse(provider, key, &family, &idx, &type);
1332 DBG("idx %lu family %d type %d", idx, family, type);
1334 route = g_hash_table_lookup(provider->routes, GINT_TO_POINTER(idx));
1335 if (route == NULL) {
1336 route = g_try_new0(struct connman_route, 1);
1337 if (route == NULL) {
1338 connman_error("out of memory");
1342 route->family = family;
1344 g_hash_table_replace(provider->routes, GINT_TO_POINTER(idx),
1349 case PROVIDER_ROUTE_TYPE_NONE:
1351 case PROVIDER_ROUTE_TYPE_MASK:
1352 route->netmask = g_strdup(value);
1354 case PROVIDER_ROUTE_TYPE_ADDR:
1355 route->host = g_strdup(value);
1357 case PROVIDER_ROUTE_TYPE_GW:
1358 route->gateway = g_strdup(value);
1365 const char *connman_provider_get_driver_name(struct connman_provider *provider)
1367 if (provider->driver == NULL)
1370 return provider->driver->name;
1373 const char *connman_provider_get_save_group(struct connman_provider *provider)
1375 return provider->identifier;
1378 static gint compare_priority(gconstpointer a, gconstpointer b)
1383 static void clean_provider(gpointer key, gpointer value, gpointer user_data)
1385 struct connman_provider *provider = value;
1387 if (provider->driver != NULL && provider->driver->remove)
1388 provider->driver->remove(provider);
1391 int connman_provider_driver_register(struct connman_provider_driver *driver)
1393 DBG("driver %p name %s", driver, driver->name);
1395 driver_list = g_slist_insert_sorted(driver_list, driver,
1397 provider_create_all_from_type(driver->name);
1401 void connman_provider_driver_unregister(struct connman_provider_driver *driver)
1403 DBG("driver %p name %s", driver, driver->name);
1405 driver_list = g_slist_remove(driver_list, driver);
1408 static void provider_remove_all(gpointer key, gpointer value,
1411 struct connman_provider *provider = value;
1413 __connman_provider_remove(provider->identifier);
1416 static void provider_offline_mode(connman_bool_t enabled)
1418 DBG("enabled %d", enabled);
1420 if (enabled == TRUE)
1421 g_hash_table_foreach(provider_hash, provider_remove_all, NULL);
1425 static struct connman_provider *provider_get(int index)
1427 GHashTableIter iter;
1428 gpointer value, key;
1430 g_hash_table_iter_init(&iter, provider_hash);
1432 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1433 struct connman_provider *provider = value;
1435 if (provider->index == index)
1442 static void provider_service_changed(struct connman_service *service,
1443 enum connman_service_state state)
1445 struct connman_provider *provider;
1446 int vpn_index, service_index;
1448 if (service == NULL)
1452 case CONNMAN_SERVICE_STATE_UNKNOWN:
1453 case CONNMAN_SERVICE_STATE_IDLE:
1454 case CONNMAN_SERVICE_STATE_ASSOCIATION:
1455 case CONNMAN_SERVICE_STATE_CONFIGURATION:
1456 case CONNMAN_SERVICE_STATE_READY:
1457 case CONNMAN_SERVICE_STATE_ONLINE:
1459 case CONNMAN_SERVICE_STATE_DISCONNECT:
1460 case CONNMAN_SERVICE_STATE_FAILURE:
1464 service_index = __connman_service_get_index(service);
1466 vpn_index = __connman_connection_get_vpn_index(service_index);
1468 DBG("service %p %s state %d index %d/%d", service,
1469 __connman_service_get_ident(service),
1470 state, service_index, vpn_index);
1475 provider = provider_get(vpn_index);
1476 if (provider == NULL)
1479 DBG("disconnect %p index %d", provider, vpn_index);
1481 __connman_provider_disconnect(provider);
1486 static struct connman_notifier provider_notifier = {
1488 .offline_mode = provider_offline_mode,
1489 .service_state_changed = provider_service_changed,
1492 int __connman_provider_init(void)
1498 connection = connman_dbus_get_connection();
1500 provider_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1501 NULL, unregister_provider);
1503 err = connman_notifier_register(&provider_notifier);
1505 g_hash_table_destroy(provider_hash);
1506 dbus_connection_unref(connection);
1512 void __connman_provider_cleanup(void)
1516 connman_notifier_unregister(&provider_notifier);
1518 g_hash_table_foreach(provider_hash, clean_provider, NULL);
1520 g_hash_table_destroy(provider_hash);
1521 provider_hash = NULL;
1523 dbus_connection_unref(connection);