5 * Copyright (C) 2007-2010 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
34 static DBusConnection *connection = NULL;
36 static GHashTable *provider_hash = NULL;
38 static GSList *driver_list = NULL;
40 struct connman_route {
47 struct connman_provider {
49 struct connman_service *vpn_service;
58 struct connman_provider_driver *driver;
60 GHashTable *setting_strings;
61 GHashTable *user_routes;
62 gchar **user_networks;
63 gsize num_user_networks;
66 void __connman_provider_append_properties(struct connman_provider *provider,
67 DBusMessageIter *iter)
69 if (provider->host != NULL)
70 connman_dbus_dict_append_basic(iter, "Host",
71 DBUS_TYPE_STRING, &provider->host);
73 if (provider->domain != NULL)
74 connman_dbus_dict_append_basic(iter, "Domain",
75 DBUS_TYPE_STRING, &provider->domain);
77 if (provider->type != NULL)
78 connman_dbus_dict_append_basic(iter, "Type", DBUS_TYPE_STRING,
82 int __connman_provider_append_user_route(struct connman_provider *provider,
83 int family, const char *network, const char *netmask)
85 struct connman_route *route;
86 char *key = g_strdup_printf("%d/%s/%s", family, network, netmask);
88 DBG("family %d network %s netmask %s", family, network, netmask);
90 route = g_hash_table_lookup(provider->user_routes, key);
92 route = g_try_new0(struct connman_route, 1);
94 connman_error("out of memory");
98 route->family = family;
99 route->host = g_strdup(network);
100 route->netmask = g_strdup(netmask);
102 g_hash_table_replace(provider->user_routes, key, route);
109 static void set_user_networks(struct connman_provider *provider,
114 while (networks[i] != NULL) {
115 char **elems = g_strsplit(networks[i], "/", 0);
116 char *network, *netmask = NULL;
117 int family = PF_UNSPEC, ret;
123 if (elems[1] != NULL)
126 if (g_strrstr(network, ":") != NULL)
128 else if (g_strrstr(network, ".") != NULL) {
131 if (g_strrstr(netmask, ".") == NULL) {
132 /* We have netmask length */
134 struct in_addr netmask_in;
135 unsigned char prefix_len = atoi(netmask);
137 addr = 0xffffffff << (32 - prefix_len);
138 netmask_in.s_addr = htonl(addr);
139 netmask = inet_ntoa(netmask_in);
141 DBG("network %s netmask %s", network, netmask);
145 ret = __connman_provider_append_user_route(provider,
146 family, network, netmask);
156 static int provider_load_from_keyfile(struct connman_provider *provider,
164 settings = g_key_file_get_keys(keyfile, provider->identifier, &length,
166 if (settings == NULL) {
167 g_key_file_free(keyfile);
171 while (idx < length) {
174 if (g_str_equal(key, "Networks") == TRUE) {
175 g_strfreev(provider->user_networks);
176 provider->user_networks =
177 g_key_file_get_string_list(keyfile,
178 provider->identifier,
180 &provider->num_user_networks,
183 value = g_key_file_get_string(keyfile,
184 provider->identifier,
186 connman_provider_set_string(provider, key,
193 g_strfreev(settings);
195 if (provider->user_networks != NULL)
196 set_user_networks(provider, provider->user_networks);
201 static int connman_provider_load(struct connman_provider *provider)
205 DBG("provider %p", provider);
207 keyfile = __connman_storage_load_provider(provider->identifier);
211 provider_load_from_keyfile(provider, keyfile);
213 g_key_file_free(keyfile);
217 static int connman_provider_save(struct connman_provider *provider)
221 DBG("provider %p", provider);
223 keyfile = g_key_file_new();
227 g_key_file_set_string(keyfile, provider->identifier,
228 "Name", provider->name);
229 g_key_file_set_string(keyfile, provider->identifier,
230 "Type", provider->type);
231 g_key_file_set_string(keyfile, provider->identifier,
232 "Host", provider->host);
233 g_key_file_set_string(keyfile, provider->identifier,
234 "VPN.Domain", provider->domain);
235 if (provider->user_networks != NULL)
236 g_key_file_set_string_list(keyfile, provider->identifier,
238 (const gchar **)provider->user_networks,
239 provider->num_user_networks);
241 if (provider->driver != NULL && provider->driver->save != NULL)
242 provider->driver->save(provider, keyfile);
244 __connman_storage_save_provider(keyfile, provider->identifier);
245 g_key_file_free(keyfile);
250 static struct connman_provider *connman_provider_lookup(const char *identifier)
252 struct connman_provider *provider = NULL;
254 provider = g_hash_table_lookup(provider_hash, identifier);
259 static gboolean match_driver(struct connman_provider *provider,
260 struct connman_provider_driver *driver)
262 if (g_strcmp0(driver->name, provider->type) == 0)
268 static int provider_probe(struct connman_provider *provider)
272 DBG("provider %p name %s", provider, provider->name);
274 if (provider->driver != NULL)
277 for (list = driver_list; list; list = list->next) {
278 struct connman_provider_driver *driver = list->data;
280 if (match_driver(provider, driver) == FALSE)
283 DBG("driver %p name %s", driver, driver->name);
285 if (driver->probe != NULL && driver->probe(provider) == 0) {
286 provider->driver = driver;
291 if (provider->driver == NULL)
297 static void provider_remove(struct connman_provider *provider)
299 if (provider->driver != NULL) {
300 provider->driver->remove(provider);
301 provider->driver = NULL;
305 static int provider_register(struct connman_provider *provider)
307 return provider_probe(provider);
310 static void provider_unregister(struct connman_provider *provider)
312 provider_remove(provider);
315 struct connman_provider *
316 connman_provider_ref_debug(struct connman_provider *provider,
317 const char *file, int line, const char *caller)
319 DBG("%p ref %d by %s:%d:%s()", provider, provider->refcount + 1,
322 __sync_fetch_and_add(&provider->refcount, 1);
327 static void provider_destruct(struct connman_provider *provider)
329 DBG("provider %p", provider);
331 g_free(provider->name);
332 g_free(provider->type);
333 g_free(provider->host);
334 g_free(provider->domain);
335 g_free(provider->identifier);
336 g_strfreev(provider->user_networks);
337 g_hash_table_destroy(provider->routes);
338 g_hash_table_destroy(provider->user_routes);
339 g_hash_table_destroy(provider->setting_strings);
343 void connman_provider_unref_debug(struct connman_provider *provider,
344 const char *file, int line, const char *caller)
346 DBG("%p ref %d by %s:%d:%s()", provider, provider->refcount - 1,
349 if (__sync_fetch_and_sub(&provider->refcount, 1) != 1)
352 provider_remove(provider);
354 provider_destruct(provider);
357 static int provider_indicate_state(struct connman_provider *provider,
358 enum connman_service_state state)
360 DBG("state %d", state);
362 __connman_service_ipconfig_indicate_state(provider->vpn_service, state,
363 CONNMAN_IPCONFIG_TYPE_IPV4);
365 return __connman_service_ipconfig_indicate_state(provider->vpn_service,
366 state, CONNMAN_IPCONFIG_TYPE_IPV6);
369 int __connman_provider_disconnect(struct connman_provider *provider)
373 DBG("provider %p", provider);
375 if (provider->driver != NULL && provider->driver->disconnect != NULL)
376 err = provider->driver->disconnect(provider);
380 if (provider->vpn_service != NULL)
381 provider_indicate_state(provider,
382 CONNMAN_SERVICE_STATE_DISCONNECT);
385 if (err != -EINPROGRESS)
394 int __connman_provider_connect(struct connman_provider *provider)
398 DBG("provider %p", provider);
400 if (provider->driver != NULL && provider->driver->connect != NULL)
401 err = provider->driver->connect(provider);
406 if (err != -EINPROGRESS)
409 provider_indicate_state(provider,
410 CONNMAN_SERVICE_STATE_ASSOCIATION);
418 int __connman_provider_remove(const char *path)
420 struct connman_provider *provider;
424 DBG("path %s", path);
426 g_hash_table_iter_init(&iter, provider_hash);
427 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
428 const char *srv_path;
431 if (provider->vpn_service == NULL)
434 srv_path = __connman_service_get_path(provider->vpn_service);
436 if (g_strcmp0(srv_path, path) == 0) {
437 DBG("Removing VPN %s", provider->identifier);
439 provider_unregister(provider);
440 g_hash_table_remove(provider_hash,
441 provider->identifier);
449 static void provider_append_routes(gpointer key, gpointer value,
452 struct connman_route *route = value;
453 struct connman_provider *provider = user_data;
454 int index = provider->index;
456 if (route->family == AF_INET6) {
457 unsigned char prefix_len = atoi(route->netmask);
459 connman_inet_add_ipv6_network_route(index, route->host,
463 connman_inet_add_network_route(index, route->host,
469 static int set_connected(struct connman_provider *provider,
470 connman_bool_t connected)
472 struct connman_service *service = provider->vpn_service;
473 struct connman_ipconfig *ipconfig;
478 ipconfig = __connman_service_get_ipconfig(service, provider->family);
480 if (connected == TRUE) {
481 if (ipconfig == NULL) {
482 provider_indicate_state(provider,
483 CONNMAN_SERVICE_STATE_FAILURE);
487 __connman_ipconfig_address_add(ipconfig);
488 __connman_ipconfig_gateway_add(ipconfig);
490 provider_indicate_state(provider,
491 CONNMAN_SERVICE_STATE_READY);
493 g_hash_table_foreach(provider->routes, provider_append_routes,
496 g_hash_table_foreach(provider->user_routes, provider_append_routes,
500 if (ipconfig != NULL) {
501 provider_indicate_state(provider,
502 CONNMAN_SERVICE_STATE_DISCONNECT);
503 __connman_ipconfig_gateway_remove(ipconfig);
506 provider_indicate_state(provider,
507 CONNMAN_SERVICE_STATE_IDLE);
513 int connman_provider_set_state(struct connman_provider *provider,
514 enum connman_provider_state state)
516 if (provider == NULL || provider->vpn_service == NULL)
520 case CONNMAN_PROVIDER_STATE_UNKNOWN:
522 case CONNMAN_PROVIDER_STATE_IDLE:
523 return set_connected(provider, FALSE);
524 case CONNMAN_PROVIDER_STATE_CONNECT:
525 return provider_indicate_state(provider,
526 CONNMAN_SERVICE_STATE_ASSOCIATION);
527 case CONNMAN_PROVIDER_STATE_READY:
528 return set_connected(provider, TRUE);
529 case CONNMAN_PROVIDER_STATE_DISCONNECT:
530 return provider_indicate_state(provider,
531 CONNMAN_SERVICE_STATE_DISCONNECT);
532 case CONNMAN_PROVIDER_STATE_FAILURE:
533 return provider_indicate_state(provider,
534 CONNMAN_SERVICE_STATE_FAILURE);
540 int connman_provider_indicate_error(struct connman_provider *provider,
541 enum connman_provider_error error)
543 enum connman_service_error service_error;
546 case CONNMAN_PROVIDER_ERROR_LOGIN_FAILED:
547 service_error = CONNMAN_SERVICE_ERROR_LOGIN_FAILED;
549 case CONNMAN_PROVIDER_ERROR_AUTH_FAILED:
550 service_error = CONNMAN_SERVICE_ERROR_AUTH_FAILED;
552 case CONNMAN_PROVIDER_ERROR_CONNECT_FAILED:
553 service_error = CONNMAN_SERVICE_ERROR_CONNECT_FAILED;
556 service_error = CONNMAN_SERVICE_ERROR_UNKNOWN;
560 return __connman_service_indicate_error(provider->vpn_service,
564 static void unregister_provider(gpointer data)
566 struct connman_provider *provider = data;
568 DBG("provider %p service %p", provider, provider->vpn_service);
570 if (provider->vpn_service != NULL) {
571 connman_service_unref(provider->vpn_service);
572 provider->vpn_service = NULL;
575 connman_provider_unref(provider);
578 static void destroy_route(gpointer user_data)
580 struct connman_route *route = user_data;
583 g_free(route->netmask);
584 g_free(route->gateway);
588 static void provider_initialize(struct connman_provider *provider)
590 DBG("provider %p", provider);
593 provider->name = NULL;
594 provider->type = NULL;
595 provider->domain = NULL;
596 provider->identifier = NULL;
597 provider->user_networks = NULL;
598 provider->routes = g_hash_table_new_full(g_direct_hash, g_direct_equal,
599 NULL, destroy_route);
600 provider->user_routes = g_hash_table_new_full(g_str_hash, g_str_equal,
601 g_free, destroy_route);
602 provider->setting_strings = g_hash_table_new_full(g_str_hash, g_str_equal,
606 static struct connman_provider *connman_provider_new(void)
608 struct connman_provider *provider;
610 provider = g_try_new0(struct connman_provider, 1);
611 if (provider == NULL)
614 provider->refcount = 1;
616 DBG("provider %p", provider);
617 provider_initialize(provider);
622 static struct connman_provider *connman_provider_get(const char *identifier)
624 struct connman_provider *provider;
626 provider = g_hash_table_lookup(provider_hash, identifier);
627 if (provider != NULL)
630 provider = connman_provider_new();
631 if (provider == NULL)
634 DBG("provider %p", provider);
636 provider->identifier = g_strdup(identifier);
638 g_hash_table_insert(provider_hash, provider->identifier, provider);
643 static void provider_dbus_ident(char *ident)
645 int i, len = strlen(ident);
647 for (i = 0; i < len; i++) {
648 if (ident[i] >= '0' && ident[i] <= '9')
650 if (ident[i] >= 'a' && ident[i] <= 'z')
652 if (ident[i] >= 'A' && ident[i] <= 'Z')
658 static struct connman_provider *provider_create_from_keyfile(GKeyFile *keyfile,
661 struct connman_provider *provider;
663 if (keyfile == NULL || ident == NULL)
666 provider = connman_provider_lookup(ident);
667 if (provider == NULL) {
668 provider = connman_provider_get(ident);
669 if (provider == NULL) {
670 DBG("can not create provider");
674 provider_load_from_keyfile(provider, keyfile);
676 if (provider->name == NULL || provider->host == NULL ||
677 provider->domain == NULL) {
678 DBG("cannot get name, host or domain");
679 connman_provider_unref(provider);
683 provider_register(provider);
688 static int provider_create_service(struct connman_provider *provider)
690 if (provider->vpn_service != NULL)
693 provider->vpn_service =
694 __connman_service_create_from_provider(provider);
696 if (provider->vpn_service == NULL)
702 static void provider_create_all_from_type(const char *provider_type)
708 struct connman_provider *provider;
710 DBG("provider type %s", provider_type);
712 providers = __connman_storage_get_providers();
714 for (i = 0; providers[i] != NULL; i+=1) {
716 if (strncmp(providers[i], "provider_", 9) != 0)
719 id = providers[i] + 9;
720 keyfile = __connman_storage_load_provider(id);
725 type = g_key_file_get_string(keyfile, id, "Type", NULL);
727 DBG("keyfile %p id %s type %s", keyfile, id, type);
729 if (strcmp(provider_type, type) != 0) {
731 g_key_file_free(keyfile);
735 provider = provider_create_from_keyfile(keyfile, id);
736 if (provider != NULL) {
737 if (provider_create_service(provider) == -EOPNOTSUPP) {
738 DBG("could not create service");
739 connman_provider_unref(provider);
744 g_key_file_free(keyfile);
746 g_strfreev(providers);
749 static char **get_user_networks(DBusMessageIter *array, int *count)
751 DBusMessageIter entry;
752 char **networks = NULL;
753 GSList *list = NULL, *l;
756 dbus_message_iter_recurse(array, &entry);
758 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
760 dbus_message_iter_get_basic(&entry, &val);
762 list = g_slist_prepend(list, g_strdup(val));
763 dbus_message_iter_next(&entry);
766 len = g_slist_length(list);
770 networks = g_try_new(char *, len + 1);
771 if (networks == NULL)
777 for (l = list; l != NULL; l = g_slist_next(l))
778 networks[--len] = l->data;
786 int __connman_provider_create_and_connect(DBusMessage *msg)
788 struct connman_provider *provider;
789 DBusMessageIter iter, array;
790 const char *type = NULL, *name = NULL, *service_path;
791 const char *host = NULL, *domain = NULL;
792 char **networks = NULL;
796 dbus_message_iter_init(msg, &iter);
797 dbus_message_iter_recurse(&iter, &array);
799 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
800 DBusMessageIter entry, value;
803 dbus_message_iter_recurse(&array, &entry);
804 dbus_message_iter_get_basic(&entry, &key);
806 dbus_message_iter_next(&entry);
807 dbus_message_iter_recurse(&entry, &value);
809 switch (dbus_message_iter_get_arg_type(&value)) {
810 case DBUS_TYPE_STRING:
811 if (g_str_equal(key, "Type") == TRUE)
812 dbus_message_iter_get_basic(&value, &type);
813 else if (g_str_equal(key, "Name") == TRUE)
814 dbus_message_iter_get_basic(&value, &name);
815 else if (g_str_equal(key, "Host") == TRUE)
816 dbus_message_iter_get_basic(&value, &host);
817 else if (g_str_equal(key, "VPN.Domain") == TRUE)
818 dbus_message_iter_get_basic(&value, &domain);
820 case DBUS_TYPE_ARRAY:
821 if (g_str_equal(key, "Networks") == TRUE)
822 networks = get_user_networks(&value, &count);
826 dbus_message_iter_next(&array);
829 if (host == NULL || domain == NULL)
832 DBG("Type %s name %s networks %p", type, name, networks);
834 if (type == NULL || name == NULL)
837 ident = g_strdup_printf("%s_%s", host, domain);
838 provider_dbus_ident(ident);
840 DBG("ident %s", ident);
842 provider = connman_provider_lookup(ident);
843 if (provider == NULL) {
844 provider = connman_provider_get(ident);
845 if (provider == NULL) {
846 DBG("can not create provider");
851 provider->host = g_strdup(host);
852 provider->domain = g_strdup(domain);
853 provider->name = g_strdup(name);
854 provider->type = g_strdup(type);
856 if (provider_register(provider) == 0)
857 connman_provider_load(provider);
860 if (networks != NULL) {
861 g_strfreev(provider->user_networks);
862 provider->user_networks = networks;
863 provider->num_user_networks = count;
864 set_user_networks(provider, provider->user_networks);
867 dbus_message_iter_init(msg, &iter);
868 dbus_message_iter_recurse(&iter, &array);
870 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
871 DBusMessageIter entry, value;
872 const char *key, *str;
874 dbus_message_iter_recurse(&array, &entry);
875 dbus_message_iter_get_basic(&entry, &key);
877 dbus_message_iter_next(&entry);
878 dbus_message_iter_recurse(&entry, &value);
880 switch (dbus_message_iter_get_arg_type(&value)) {
881 case DBUS_TYPE_STRING:
882 dbus_message_iter_get_basic(&value, &str);
883 connman_provider_set_string(provider, key, str);
887 dbus_message_iter_next(&array);
892 err = provider_create_service(provider);
893 if (err == -EALREADY) {
894 DBG("provider already connected");
896 if (err == -EOPNOTSUPP) {
899 err = __connman_service_connect(provider->vpn_service);
901 if (err < 0 && err != -EINPROGRESS)
906 connman_provider_save(provider);
907 service_path = __connman_service_get_path(provider->vpn_service);
908 g_dbus_send_reply(connection, msg,
909 DBUS_TYPE_OBJECT_PATH, &service_path,
914 connman_service_unref(provider->vpn_service);
915 provider->vpn_service = NULL;
918 DBG("can not connect, delete provider");
920 g_hash_table_remove(provider_hash, provider->identifier);
925 const char * __connman_provider_get_ident(struct connman_provider *provider)
927 if (provider == NULL)
930 return provider->identifier;
933 int connman_provider_set_string(struct connman_provider *provider,
934 const char *key, const char *value)
936 DBG("provider %p key %s value %s", provider, key, value);
938 if (g_str_equal(key, "Type") == TRUE) {
939 g_free(provider->type);
940 provider->type = g_strdup(value);
941 } else if (g_str_equal(key, "Name") == TRUE) {
942 g_free(provider->name);
943 provider->name = g_strdup(value);
944 } else if (g_str_equal(key, "Host") == TRUE) {
945 g_free(provider->host);
946 provider->host = g_strdup(value);
947 } else if (g_str_equal(key, "VPN.Domain") == TRUE) {
948 g_free(provider->domain);
949 provider->domain = g_strdup(value);
951 g_hash_table_replace(provider->setting_strings,
952 g_strdup(key), g_strdup(value));
956 const char *connman_provider_get_string(struct connman_provider *provider,
959 DBG("provider %p key %s", provider, key);
961 if (g_str_equal(key, "Type") == TRUE)
962 return provider->type;
963 else if (g_str_equal(key, "Name") == TRUE)
964 return provider->name;
965 else if (g_str_equal(key, "Host") == TRUE)
966 return provider->host;
967 else if (g_str_equal(key, "VPN.Domain") == TRUE)
968 return provider->domain;
970 return g_hash_table_lookup(provider->setting_strings, key);
974 __connman_provider_check_routes(struct connman_provider *provider)
976 if (provider == NULL)
979 if (provider->user_routes != NULL &&
980 g_hash_table_size(provider->user_routes) > 0)
983 if (provider->routes != NULL &&
984 g_hash_table_size(provider->routes) > 0)
990 void *connman_provider_get_data(struct connman_provider *provider)
992 return provider->driver_data;
995 void connman_provider_set_data(struct connman_provider *provider, void *data)
997 provider->driver_data = data;
1000 void connman_provider_set_index(struct connman_provider *provider, int index)
1002 struct connman_service *service = provider->vpn_service;
1003 struct connman_ipconfig *ipconfig;
1007 if (service == NULL)
1010 ipconfig = __connman_service_get_ip4config(service);
1012 if (ipconfig == NULL) {
1013 __connman_service_create_ip4config(service, index);
1015 ipconfig = __connman_service_get_ip4config(service);
1016 if (ipconfig == NULL) {
1017 DBG("Couldnt create ipconfig");
1022 __connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_FIXED);
1023 __connman_ipconfig_set_index(ipconfig, index);
1026 ipconfig = __connman_service_get_ip6config(service);
1028 if (ipconfig == NULL) {
1029 __connman_service_create_ip6config(service, index);
1031 ipconfig = __connman_service_get_ip6config(service);
1032 if (ipconfig == NULL) {
1033 DBG("Couldnt create ipconfig for IPv6");
1038 __connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_OFF);
1039 __connman_ipconfig_set_index(ipconfig, index);
1042 provider->index = index;
1045 int connman_provider_get_index(struct connman_provider *provider)
1047 return provider->index;
1050 int connman_provider_set_ipaddress(struct connman_provider *provider,
1051 struct connman_ipaddress *ipaddress)
1053 struct connman_ipconfig *ipconfig = NULL;
1055 ipconfig = __connman_service_get_ipconfig(provider->vpn_service,
1057 if (ipconfig == NULL)
1060 provider->family = ipaddress->family;
1062 __connman_ipconfig_set_local(ipconfig, ipaddress->local);
1063 __connman_ipconfig_set_peer(ipconfig, ipaddress->peer);
1064 __connman_ipconfig_set_broadcast(ipconfig, ipaddress->broadcast);
1065 __connman_ipconfig_set_gateway(ipconfig, ipaddress->gateway);
1066 __connman_ipconfig_set_prefixlen(ipconfig, ipaddress->prefixlen);
1071 int connman_provider_set_pac(struct connman_provider *provider, const char *pac)
1073 DBG("provider %p pac %s", provider, pac);
1075 __connman_service_set_pac(provider->vpn_service, pac);
1081 int connman_provider_set_domain(struct connman_provider *provider,
1084 DBG("provider %p domain %s", provider, domain);
1086 g_free(provider->domain);
1087 provider->domain = g_strdup(domain);
1089 __connman_service_set_domainname(provider->vpn_service, domain);
1094 int connman_provider_set_nameservers(struct connman_provider *provider,
1095 const char *nameservers)
1098 char **nameservers_array = NULL;
1100 DBG("provider %p nameservers %s", provider, nameservers);
1102 __connman_service_nameserver_clear(provider->vpn_service);
1104 if (nameservers == NULL)
1107 nameservers_array = g_strsplit(nameservers, " ", 0);
1109 for (i = 0; nameservers_array[i] != NULL; i++) {
1110 __connman_service_nameserver_append(provider->vpn_service,
1111 nameservers_array[i], FALSE);
1114 g_strfreev(nameservers_array);
1119 enum provider_route_type {
1120 PROVIDER_ROUTE_TYPE_NONE = 0,
1121 PROVIDER_ROUTE_TYPE_MASK = 1,
1122 PROVIDER_ROUTE_TYPE_ADDR = 2,
1123 PROVIDER_ROUTE_TYPE_GW = 3,
1126 static int route_env_parse(struct connman_provider *provider, const char *key,
1127 int *family, unsigned long *idx,
1128 enum provider_route_type *type)
1133 DBG("name %s", provider->name);
1135 if (!strcmp(provider->type, "openvpn")) {
1136 if (g_str_has_prefix(key, "route_network_") == TRUE) {
1137 start = key + strlen("route_network_");
1138 *type = PROVIDER_ROUTE_TYPE_ADDR;
1139 } else if (g_str_has_prefix(key, "route_netmask_") == TRUE) {
1140 start = key + strlen("route_netmask_");
1141 *type = PROVIDER_ROUTE_TYPE_MASK;
1142 } else if (g_str_has_prefix(key, "route_gateway_") == TRUE) {
1143 start = key + strlen("route_gateway_");
1144 *type = PROVIDER_ROUTE_TYPE_GW;
1149 *idx = g_ascii_strtoull(start, &end, 10);
1151 } else if (!strcmp(provider->type, "openconnect")) {
1152 if (g_str_has_prefix(key, "CISCO_SPLIT_INC_") == TRUE) {
1154 start = key + strlen("CISCO_SPLIT_INC_");
1155 } else if (g_str_has_prefix(key, "CISCO_IPV6_SPLIT_INC_") == TRUE) {
1157 start = key + strlen("CISCO_IPV6_SPLIT_INC_");
1161 *idx = g_ascii_strtoull(start, &end, 10);
1163 if (strncmp(end, "_ADDR", 5) == 0)
1164 *type = PROVIDER_ROUTE_TYPE_ADDR;
1165 else if (strncmp(end, "_MASK", 5) == 0)
1166 *type = PROVIDER_ROUTE_TYPE_MASK;
1167 else if (strncmp(end, "_MASKLEN", 8) == 0 &&
1168 *family == AF_INET6) {
1169 *type = PROVIDER_ROUTE_TYPE_MASK;
1177 int connman_provider_append_route(struct connman_provider *provider,
1178 const char *key, const char *value)
1180 struct connman_route *route;
1181 int ret, family = 0;
1182 unsigned long idx = 0;
1183 enum provider_route_type type = PROVIDER_ROUTE_TYPE_NONE;
1185 DBG("key %s value %s", key, value);
1187 ret = route_env_parse(provider, key, &family, &idx, &type);
1191 DBG("idx %lu family %d type %d", idx, family, type);
1193 route = g_hash_table_lookup(provider->routes, GINT_TO_POINTER(idx));
1194 if (route == NULL) {
1195 route = g_try_new0(struct connman_route, 1);
1196 if (route == NULL) {
1197 connman_error("out of memory");
1201 route->family = family;
1203 g_hash_table_replace(provider->routes, GINT_TO_POINTER(idx),
1208 case PROVIDER_ROUTE_TYPE_NONE:
1210 case PROVIDER_ROUTE_TYPE_MASK:
1211 route->netmask = g_strdup(value);
1213 case PROVIDER_ROUTE_TYPE_ADDR:
1214 route->host = g_strdup(value);
1216 case PROVIDER_ROUTE_TYPE_GW:
1217 route->gateway = g_strdup(value);
1224 const char *connman_provider_get_driver_name(struct connman_provider *provider)
1226 if (provider->driver == NULL)
1229 return provider->driver->name;
1232 const char *connman_provider_get_save_group(struct connman_provider *provider)
1234 return provider->identifier;
1237 static gint compare_priority(gconstpointer a, gconstpointer b)
1242 static void clean_provider(gpointer key, gpointer value, gpointer user_data)
1244 struct connman_provider *provider = value;
1246 if (provider->driver != NULL && provider->driver->remove)
1247 provider->driver->remove(provider);
1250 int connman_provider_driver_register(struct connman_provider_driver *driver)
1252 DBG("driver %p name %s", driver, driver->name);
1254 driver_list = g_slist_insert_sorted(driver_list, driver,
1256 provider_create_all_from_type(driver->name);
1260 void connman_provider_driver_unregister(struct connman_provider_driver *driver)
1262 DBG("driver %p name %s", driver, driver->name);
1264 driver_list = g_slist_remove(driver_list, driver);
1267 static void provider_remove_all(gpointer key, gpointer value,
1270 struct connman_provider *provider = value;
1272 __connman_provider_remove(provider->identifier);
1275 static void provider_offline_mode(connman_bool_t enabled)
1277 DBG("enabled %d", enabled);
1279 if (enabled == TRUE)
1280 g_hash_table_foreach(provider_hash, provider_remove_all, NULL);
1284 static struct connman_provider *provider_get(int index)
1286 GHashTableIter iter;
1287 gpointer value, key;
1289 g_hash_table_iter_init(&iter, provider_hash);
1291 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1292 struct connman_provider *provider = value;
1294 if (provider->index == index)
1301 static void provider_service_changed(struct connman_service *service,
1302 enum connman_service_state state)
1304 struct connman_provider *provider;
1305 int vpn_index, service_index;
1307 if (service == NULL)
1311 case CONNMAN_SERVICE_STATE_UNKNOWN:
1312 case CONNMAN_SERVICE_STATE_IDLE:
1313 case CONNMAN_SERVICE_STATE_ASSOCIATION:
1314 case CONNMAN_SERVICE_STATE_CONFIGURATION:
1315 case CONNMAN_SERVICE_STATE_READY:
1316 case CONNMAN_SERVICE_STATE_ONLINE:
1318 case CONNMAN_SERVICE_STATE_DISCONNECT:
1319 case CONNMAN_SERVICE_STATE_FAILURE:
1323 service_index = __connman_service_get_index(service);
1325 vpn_index = __connman_connection_get_vpn_index(service_index);
1327 DBG("service %p %s state %d index %d/%d", service,
1328 __connman_service_get_ident(service),
1329 state, service_index, vpn_index);
1334 provider = provider_get(vpn_index);
1335 if (provider == NULL)
1338 DBG("disconnect %p index %d", provider, vpn_index);
1340 __connman_provider_disconnect(provider);
1345 static struct connman_notifier provider_notifier = {
1347 .offline_mode = provider_offline_mode,
1348 .service_state_changed = provider_service_changed,
1351 int __connman_provider_init(void)
1357 connection = connman_dbus_get_connection();
1359 provider_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1360 NULL, unregister_provider);
1362 err = connman_notifier_register(&provider_notifier);
1364 g_hash_table_destroy(provider_hash);
1365 dbus_connection_unref(connection);
1371 void __connman_provider_cleanup(void)
1375 connman_notifier_unregister(&provider_notifier);
1377 g_hash_table_foreach(provider_hash, clean_provider, NULL);
1379 g_hash_table_destroy(provider_hash);
1380 provider_hash = NULL;
1382 dbus_connection_unref(connection);