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 int __connman_provider_create_and_connect(DBusMessage *msg)
751 struct connman_provider *provider;
752 DBusMessageIter iter, array;
753 const char *type = NULL, *name = NULL, *service_path;
754 const char *host = NULL, *domain = NULL;
758 dbus_message_iter_init(msg, &iter);
759 dbus_message_iter_recurse(&iter, &array);
761 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
762 DBusMessageIter entry, value;
765 dbus_message_iter_recurse(&array, &entry);
766 dbus_message_iter_get_basic(&entry, &key);
768 dbus_message_iter_next(&entry);
769 dbus_message_iter_recurse(&entry, &value);
771 switch (dbus_message_iter_get_arg_type(&value)) {
772 case DBUS_TYPE_STRING:
773 if (g_str_equal(key, "Type") == TRUE)
774 dbus_message_iter_get_basic(&value, &type);
775 else if (g_str_equal(key, "Name") == TRUE)
776 dbus_message_iter_get_basic(&value, &name);
777 else if (g_str_equal(key, "Host") == TRUE)
778 dbus_message_iter_get_basic(&value, &host);
779 else if (g_str_equal(key, "VPN.Domain") == TRUE)
780 dbus_message_iter_get_basic(&value, &domain);
784 dbus_message_iter_next(&array);
787 if (host == NULL || domain == NULL)
790 DBG("Type %s name %s", type, name);
792 if (type == NULL || name == NULL)
795 ident = g_strdup_printf("%s_%s", host, domain);
796 provider_dbus_ident(ident);
798 DBG("ident %s", ident);
800 provider = connman_provider_lookup(ident);
801 if (provider == NULL) {
802 provider = connman_provider_get(ident);
803 if (provider == NULL) {
804 DBG("can not create provider");
809 provider->host = g_strdup(host);
810 provider->domain = g_strdup(domain);
811 provider->name = g_strdup(name);
812 provider->type = g_strdup(type);
814 if (provider_register(provider) == 0)
815 connman_provider_load(provider);
818 dbus_message_iter_init(msg, &iter);
819 dbus_message_iter_recurse(&iter, &array);
821 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
822 DBusMessageIter entry, value;
823 const char *key, *str;
825 dbus_message_iter_recurse(&array, &entry);
826 dbus_message_iter_get_basic(&entry, &key);
828 dbus_message_iter_next(&entry);
829 dbus_message_iter_recurse(&entry, &value);
831 switch (dbus_message_iter_get_arg_type(&value)) {
832 case DBUS_TYPE_STRING:
833 dbus_message_iter_get_basic(&value, &str);
834 connman_provider_set_string(provider, key, str);
838 dbus_message_iter_next(&array);
843 err = provider_create_service(provider);
844 if (err == -EALREADY) {
845 DBG("provider already connected");
847 if (err == -EOPNOTSUPP) {
850 err = __connman_service_connect(provider->vpn_service);
852 if (err < 0 && err != -EINPROGRESS)
857 connman_provider_save(provider);
858 service_path = __connman_service_get_path(provider->vpn_service);
859 g_dbus_send_reply(connection, msg,
860 DBUS_TYPE_OBJECT_PATH, &service_path,
865 connman_service_unref(provider->vpn_service);
866 provider->vpn_service = NULL;
869 DBG("can not connect, delete provider");
871 g_hash_table_remove(provider_hash, provider->identifier);
876 const char * __connman_provider_get_ident(struct connman_provider *provider)
878 if (provider == NULL)
881 return provider->identifier;
884 int connman_provider_set_string(struct connman_provider *provider,
885 const char *key, const char *value)
887 DBG("provider %p key %s value %s", provider, key, value);
889 if (g_str_equal(key, "Type") == TRUE) {
890 g_free(provider->type);
891 provider->type = g_strdup(value);
892 } else if (g_str_equal(key, "Name") == TRUE) {
893 g_free(provider->name);
894 provider->name = g_strdup(value);
895 } else if (g_str_equal(key, "Host") == TRUE) {
896 g_free(provider->host);
897 provider->host = g_strdup(value);
898 } else if (g_str_equal(key, "VPN.Domain") == TRUE) {
899 g_free(provider->domain);
900 provider->domain = g_strdup(value);
902 g_hash_table_replace(provider->setting_strings,
903 g_strdup(key), g_strdup(value));
907 const char *connman_provider_get_string(struct connman_provider *provider,
910 DBG("provider %p key %s", provider, key);
912 if (g_str_equal(key, "Type") == TRUE)
913 return provider->type;
914 else if (g_str_equal(key, "Name") == TRUE)
915 return provider->name;
916 else if (g_str_equal(key, "Host") == TRUE)
917 return provider->host;
918 else if (g_str_equal(key, "VPN.Domain") == TRUE)
919 return provider->domain;
921 return g_hash_table_lookup(provider->setting_strings, key);
924 void *connman_provider_get_data(struct connman_provider *provider)
926 return provider->driver_data;
929 void connman_provider_set_data(struct connman_provider *provider, void *data)
931 provider->driver_data = data;
934 void connman_provider_set_index(struct connman_provider *provider, int index)
936 struct connman_service *service = provider->vpn_service;
937 struct connman_ipconfig *ipconfig;
944 ipconfig = __connman_service_get_ip4config(service);
946 if (ipconfig == NULL) {
947 __connman_service_create_ip4config(service, index);
949 ipconfig = __connman_service_get_ip4config(service);
950 if (ipconfig == NULL) {
951 DBG("Couldnt create ipconfig");
956 __connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_FIXED);
957 __connman_ipconfig_set_index(ipconfig, index);
960 ipconfig = __connman_service_get_ip6config(service);
962 if (ipconfig == NULL) {
963 __connman_service_create_ip6config(service, index);
965 ipconfig = __connman_service_get_ip6config(service);
966 if (ipconfig == NULL) {
967 DBG("Couldnt create ipconfig for IPv6");
972 __connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_OFF);
973 __connman_ipconfig_set_index(ipconfig, index);
976 provider->index = index;
979 int connman_provider_get_index(struct connman_provider *provider)
981 return provider->index;
984 int connman_provider_set_ipaddress(struct connman_provider *provider,
985 struct connman_ipaddress *ipaddress)
987 struct connman_ipconfig *ipconfig = NULL;
989 ipconfig = __connman_service_get_ipconfig(provider->vpn_service,
991 if (ipconfig == NULL)
994 provider->family = ipaddress->family;
996 __connman_ipconfig_set_local(ipconfig, ipaddress->local);
997 __connman_ipconfig_set_peer(ipconfig, ipaddress->peer);
998 __connman_ipconfig_set_broadcast(ipconfig, ipaddress->broadcast);
999 __connman_ipconfig_set_gateway(ipconfig, ipaddress->gateway);
1000 __connman_ipconfig_set_prefixlen(ipconfig, ipaddress->prefixlen);
1005 int connman_provider_set_pac(struct connman_provider *provider, const char *pac)
1007 DBG("provider %p pac %s", provider, pac);
1009 __connman_service_set_pac(provider->vpn_service, pac);
1015 int connman_provider_set_domain(struct connman_provider *provider,
1018 DBG("provider %p domain %s", provider, domain);
1020 g_free(provider->domain);
1021 provider->domain = g_strdup(domain);
1023 __connman_service_set_domainname(provider->vpn_service, domain);
1028 int connman_provider_set_nameservers(struct connman_provider *provider,
1029 const char *nameservers)
1032 char **nameservers_array = NULL;
1034 DBG("provider %p nameservers %s", provider, nameservers);
1036 __connman_service_nameserver_clear(provider->vpn_service);
1038 if (nameservers == NULL)
1041 nameservers_array = g_strsplit(nameservers, " ", 0);
1043 for (i = 0; nameservers_array[i] != NULL; i++) {
1044 __connman_service_nameserver_append(provider->vpn_service,
1045 nameservers_array[i], FALSE);
1048 g_strfreev(nameservers_array);
1053 enum provider_route_type {
1054 PROVIDER_ROUTE_TYPE_NONE = 0,
1055 PROVIDER_ROUTE_TYPE_MASK = 1,
1056 PROVIDER_ROUTE_TYPE_ADDR = 2,
1057 PROVIDER_ROUTE_TYPE_GW = 3,
1060 static int route_env_parse(struct connman_provider *provider, const char *key,
1061 int *family, unsigned long *idx,
1062 enum provider_route_type *type)
1067 DBG("name %s", provider->name);
1069 if (!strcmp(provider->type, "openvpn")) {
1070 if (g_str_has_prefix(key, "route_network_") == TRUE) {
1071 start = key + strlen("route_network_");
1072 *type = PROVIDER_ROUTE_TYPE_ADDR;
1073 } else if (g_str_has_prefix(key, "route_netmask_") == TRUE) {
1074 start = key + strlen("route_netmask_");
1075 *type = PROVIDER_ROUTE_TYPE_MASK;
1076 } else if (g_str_has_prefix(key, "route_gateway_") == TRUE) {
1077 start = key + strlen("route_gateway_");
1078 *type = PROVIDER_ROUTE_TYPE_GW;
1083 *idx = g_ascii_strtoull(start, &end, 10);
1085 } else if (!strcmp(provider->type, "openconnect")) {
1086 if (g_str_has_prefix(key, "CISCO_SPLIT_INC_") == TRUE) {
1088 start = key + strlen("CISCO_SPLIT_INC_");
1089 } else if (g_str_has_prefix(key, "CISCO_IPV6_SPLIT_INC_") == TRUE) {
1091 start = key + strlen("CISCO_IPV6_SPLIT_INC_");
1095 *idx = g_ascii_strtoull(start, &end, 10);
1097 if (strncmp(end, "_ADDR", 5) == 0)
1098 *type = PROVIDER_ROUTE_TYPE_ADDR;
1099 else if (strncmp(end, "_MASK", 5) == 0)
1100 *type = PROVIDER_ROUTE_TYPE_MASK;
1101 else if (strncmp(end, "_MASKLEN", 8) == 0 &&
1102 *family == AF_INET6) {
1103 *type = PROVIDER_ROUTE_TYPE_MASK;
1111 int connman_provider_append_route(struct connman_provider *provider,
1112 const char *key, const char *value)
1114 struct connman_route *route;
1115 int ret, family = 0;
1116 unsigned long idx = 0;
1117 enum provider_route_type type = PROVIDER_ROUTE_TYPE_NONE;
1119 DBG("key %s value %s", key, value);
1121 ret = route_env_parse(provider, key, &family, &idx, &type);
1125 DBG("idx %lu family %d type %d", idx, family, type);
1127 route = g_hash_table_lookup(provider->routes, GINT_TO_POINTER(idx));
1128 if (route == NULL) {
1129 route = g_try_new0(struct connman_route, 1);
1130 if (route == NULL) {
1131 connman_error("out of memory");
1135 route->family = family;
1137 g_hash_table_replace(provider->routes, GINT_TO_POINTER(idx),
1142 case PROVIDER_ROUTE_TYPE_NONE:
1144 case PROVIDER_ROUTE_TYPE_MASK:
1145 route->netmask = g_strdup(value);
1147 case PROVIDER_ROUTE_TYPE_ADDR:
1148 route->host = g_strdup(value);
1150 case PROVIDER_ROUTE_TYPE_GW:
1151 route->gateway = g_strdup(value);
1158 const char *connman_provider_get_driver_name(struct connman_provider *provider)
1160 if (provider->driver == NULL)
1163 return provider->driver->name;
1166 const char *connman_provider_get_save_group(struct connman_provider *provider)
1168 return provider->identifier;
1171 static gint compare_priority(gconstpointer a, gconstpointer b)
1176 static void clean_provider(gpointer key, gpointer value, gpointer user_data)
1178 struct connman_provider *provider = value;
1180 if (provider->driver != NULL && provider->driver->remove)
1181 provider->driver->remove(provider);
1184 int connman_provider_driver_register(struct connman_provider_driver *driver)
1186 DBG("driver %p name %s", driver, driver->name);
1188 driver_list = g_slist_insert_sorted(driver_list, driver,
1190 provider_create_all_from_type(driver->name);
1194 void connman_provider_driver_unregister(struct connman_provider_driver *driver)
1196 DBG("driver %p name %s", driver, driver->name);
1198 driver_list = g_slist_remove(driver_list, driver);
1201 static void provider_remove_all(gpointer key, gpointer value,
1204 struct connman_provider *provider = value;
1206 __connman_provider_remove(provider->identifier);
1209 static void provider_offline_mode(connman_bool_t enabled)
1211 DBG("enabled %d", enabled);
1213 if (enabled == TRUE)
1214 g_hash_table_foreach(provider_hash, provider_remove_all, NULL);
1218 static struct connman_notifier provider_notifier = {
1220 .offline_mode = provider_offline_mode,
1223 int __connman_provider_init(void)
1229 connection = connman_dbus_get_connection();
1231 provider_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1232 NULL, unregister_provider);
1234 err = connman_notifier_register(&provider_notifier);
1236 g_hash_table_destroy(provider_hash);
1237 dbus_connection_unref(connection);
1243 void __connman_provider_cleanup(void)
1247 connman_notifier_unregister(&provider_notifier);
1249 g_hash_table_foreach(provider_hash, clean_provider, NULL);
1251 g_hash_table_destroy(provider_hash);
1252 provider_hash = NULL;
1254 dbus_connection_unref(connection);