5 * Copyright (C) 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 <connman/log.h>
32 #include <gweb/gresolv.h>
35 #include "../src/connman.h"
36 #include "connman/vpn-dbus.h"
37 #include "vpn-provider.h"
41 USER_ROUTES_CHANGED = 0x01,
42 SERVER_ROUTES_CHANGED = 0x02,
45 static DBusConnection *connection;
46 static GHashTable *provider_hash;
47 static GSList *driver_list;
48 static int configuration_count;
49 static gboolean handle_routes;
62 enum vpn_provider_state state;
71 struct vpn_provider_driver *driver;
73 GHashTable *setting_strings;
74 GHashTable *user_routes;
75 GSList *user_networks;
78 struct vpn_ipconfig *ipconfig_ipv4;
79 struct vpn_ipconfig *ipconfig_ipv6;
85 static void free_route(gpointer data)
87 struct vpn_route *route = data;
89 g_free(route->network);
90 g_free(route->netmask);
91 g_free(route->gateway);
96 static void append_route(DBusMessageIter *iter, void *user_data)
98 struct vpn_route *route = user_data;
102 connman_dbus_dict_open(iter, &item);
107 if (route->family == AF_INET)
109 else if (route->family == AF_INET6)
113 connman_dbus_dict_append_basic(&item, "ProtocolFamily",
114 DBUS_TYPE_INT32, &family);
116 if (route->network != NULL)
117 connman_dbus_dict_append_basic(&item, "Network",
118 DBUS_TYPE_STRING, &route->network);
120 if (route->netmask != NULL)
121 connman_dbus_dict_append_basic(&item, "Netmask",
122 DBUS_TYPE_STRING, &route->netmask);
124 if (route->gateway != NULL)
125 connman_dbus_dict_append_basic(&item, "Gateway",
126 DBUS_TYPE_STRING, &route->gateway);
129 connman_dbus_dict_close(iter, &item);
132 static void append_routes(DBusMessageIter *iter, void *user_data)
134 GHashTable *routes = user_data;
138 if (routes == NULL) {
139 append_route(iter, NULL);
143 g_hash_table_iter_init(&hash, routes);
145 while (g_hash_table_iter_next(&hash, &key, &value) == TRUE) {
146 DBusMessageIter dict;
148 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL,
150 append_route(&dict, value);
151 dbus_message_iter_close_container(iter, &dict);
155 static void send_routes(struct vpn_provider *provider, GHashTable *routes,
158 connman_dbus_property_changed_array(provider->path,
159 VPN_CONNECTION_INTERFACE,
161 DBUS_TYPE_DICT_ENTRY,
166 static int provider_property_changed(struct vpn_provider *provider,
169 DBG("provider %p name %s", provider, name);
171 if (g_str_equal(name, "UserRoutes") == TRUE)
172 send_routes(provider, provider->user_routes, name);
173 else if (g_str_equal(name, "ServerRoutes") == TRUE)
174 send_routes(provider, provider->routes, name);
179 static GSList *read_route_dict(GSList *routes, DBusMessageIter *dicts)
181 DBusMessageIter dict, value, entry;
182 const char *network, *netmask, *gateway;
183 struct vpn_route *route;
187 dbus_message_iter_recurse(dicts, &entry);
189 network = netmask = gateway = NULL;
192 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_DICT_ENTRY) {
194 dbus_message_iter_recurse(&entry, &dict);
195 dbus_message_iter_get_basic(&dict, &key);
197 dbus_message_iter_next(&dict);
198 dbus_message_iter_recurse(&dict, &value);
200 type = dbus_message_iter_get_arg_type(&value);
203 case DBUS_TYPE_STRING:
204 if (g_str_equal(key, "ProtocolFamily") == TRUE)
205 dbus_message_iter_get_basic(&value, &family);
206 else if (g_str_equal(key, "Network") == TRUE)
207 dbus_message_iter_get_basic(&value, &network);
208 else if (g_str_equal(key, "Netmask") == TRUE)
209 dbus_message_iter_get_basic(&value, &netmask);
210 else if (g_str_equal(key, "Gateway") == TRUE)
211 dbus_message_iter_get_basic(&value, &gateway);
215 dbus_message_iter_next(&entry);
218 DBG("family %d network %s netmask %s gateway %s", family,
219 network, netmask, gateway);
221 if (network == NULL || netmask == NULL) {
222 DBG("Ignoring route as network/netmask is missing");
226 route = g_try_new(struct vpn_route, 1);
228 g_slist_free_full(routes, free_route);
232 if (family == PF_UNSPEC) {
233 family = connman_inet_check_ipaddress(network);
235 DBG("Cannot get address family of %s (%d/%s)", network,
236 family, gai_strerror(family));
237 if (strstr(network, ":") != NULL) {
238 DBG("Guessing it is IPv6");
241 DBG("Guessing it is IPv4");
259 route->family = family;
260 route->network = g_strdup(network);
261 route->netmask = g_strdup(netmask);
262 route->gateway = g_strdup(gateway);
264 routes = g_slist_prepend(routes, route);
268 static GSList *get_user_networks(DBusMessageIter *array)
270 DBusMessageIter entry;
273 while (dbus_message_iter_get_arg_type(array) == DBUS_TYPE_ARRAY) {
275 dbus_message_iter_recurse(array, &entry);
277 while (dbus_message_iter_get_arg_type(&entry) ==
279 DBusMessageIter dicts;
281 dbus_message_iter_recurse(&entry, &dicts);
283 while (dbus_message_iter_get_arg_type(&dicts) ==
286 list = read_route_dict(list, &dicts);
287 dbus_message_iter_next(&dicts);
290 dbus_message_iter_next(&entry);
293 dbus_message_iter_next(array);
299 static void set_user_networks(struct vpn_provider *provider, GSList *networks)
303 for (list = networks; list != NULL; list = g_slist_next(list)) {
304 struct vpn_route *route= list->data;
306 if (__vpn_provider_append_user_route(provider,
307 route->family, route->network,
308 route->netmask) != 0)
313 static void del_routes(struct vpn_provider *provider)
318 g_hash_table_iter_init(&hash, provider->user_routes);
319 while (handle_routes == TRUE && g_hash_table_iter_next(&hash,
320 &key, &value) == TRUE) {
321 struct vpn_route *route = value;
322 if (route->family == AF_INET6) {
323 unsigned char prefixlen = atoi(route->netmask);
324 connman_inet_del_ipv6_network_route(provider->index,
328 connman_inet_del_host_route(provider->index,
332 g_hash_table_remove_all(provider->user_routes);
333 g_slist_free_full(provider->user_networks, free_route);
334 provider->user_networks = NULL;
337 static gboolean provider_send_changed(gpointer data)
339 struct vpn_provider *provider = data;
341 if (provider->what_changed & USER_ROUTES_CHANGED)
342 provider_property_changed(provider, "UserRoutes");
344 if (provider->what_changed & SERVER_ROUTES_CHANGED)
345 provider_property_changed(provider, "ServerRoutes");
347 provider->what_changed = 0;
348 provider->notify_id = 0;
353 static void provider_schedule_changed(struct vpn_provider *provider, int flag)
355 if (provider->notify_id != 0)
356 g_source_remove(provider->notify_id);
358 provider->what_changed |= flag;
360 provider->notify_id = g_timeout_add(100, provider_send_changed,
364 static DBusMessage *set_property(DBusConnection *conn, DBusMessage *msg,
367 struct vpn_provider *provider = data;
368 DBusMessageIter iter, value;
372 DBG("conn %p", conn);
374 if (dbus_message_iter_init(msg, &iter) == FALSE)
375 return __connman_error_invalid_arguments(msg);
377 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
378 return __connman_error_invalid_arguments(msg);
380 dbus_message_iter_get_basic(&iter, &name);
381 dbus_message_iter_next(&iter);
383 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
384 return __connman_error_invalid_arguments(msg);
386 dbus_message_iter_recurse(&iter, &value);
388 type = dbus_message_iter_get_arg_type(&value);
390 if (g_str_equal(name, "UserRoutes") == TRUE) {
393 if (type != DBUS_TYPE_ARRAY)
394 return __connman_error_invalid_arguments(msg);
396 networks = get_user_networks(&value);
397 if (networks != NULL) {
398 del_routes(provider);
399 provider->user_networks = networks;
400 set_user_networks(provider, provider->user_networks);
402 if (handle_routes == FALSE)
403 provider_schedule_changed(provider,
404 USER_ROUTES_CHANGED);
407 return __connman_error_invalid_property(msg);
409 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
412 static DBusMessage *clear_property(DBusConnection *conn, DBusMessage *msg,
415 struct vpn_provider *provider = data;
418 DBG("conn %p", conn);
420 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
423 if (g_str_equal(name, "UserRoutes") == TRUE) {
424 del_routes(provider);
426 if (handle_routes == FALSE)
427 provider_property_changed(provider, name);
429 return __connman_error_invalid_property(msg);
432 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
435 static DBusMessage *do_connect(DBusConnection *conn, DBusMessage *msg,
438 struct vpn_provider *provider = data;
441 DBG("conn %p provider %p", conn, provider);
443 err = __vpn_provider_connect(provider);
445 return __connman_error_failed(msg, -err);
447 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
450 static DBusMessage *do_disconnect(DBusConnection *conn, DBusMessage *msg,
453 struct vpn_provider *provider = data;
456 DBG("conn %p provider %p", conn, provider);
458 err = __vpn_provider_disconnect(provider);
460 return __connman_error_failed(msg, -err);
462 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
465 static const GDBusMethodTable connection_methods[] = {
466 { GDBUS_METHOD("SetProperty",
467 GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
468 NULL, set_property) },
469 { GDBUS_METHOD("ClearProperty",
470 GDBUS_ARGS({ "name", "s" }), NULL,
472 { GDBUS_ASYNC_METHOD("Connect", NULL, NULL, do_connect) },
473 { GDBUS_METHOD("Disconnect", NULL, NULL, do_disconnect) },
477 static const GDBusSignalTable connection_signals[] = {
478 { GDBUS_SIGNAL("PropertyChanged",
479 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
483 static void resolv_result(GResolvResultStatus status,
484 char **results, gpointer user_data)
486 struct vpn_provider *provider = user_data;
488 DBG("status %d", status);
490 if (status == G_RESOLV_RESULT_STATUS_SUCCESS && results != NULL &&
491 g_strv_length(results) > 0)
492 provider->host_ip = g_strdupv(results);
494 vpn_provider_unref(provider);
497 static void provider_resolv_host_addr(struct vpn_provider *provider)
499 if (provider->host == NULL)
502 if (connman_inet_check_ipaddress(provider->host) > 0)
505 if (provider->host_ip != NULL)
509 * If the hostname is not numeric, try to resolv it. We do not wait
510 * the result as it might take some time. We will get the result
511 * before VPN will feed routes to us because VPN client will need
512 * the IP address also before VPN connection can be established.
514 provider->resolv = g_resolv_new(0);
515 if (provider->resolv == NULL) {
516 DBG("Cannot resolv %s", provider->host);
520 DBG("Trying to resolv %s", provider->host);
522 vpn_provider_ref(provider);
524 g_resolv_lookup_hostname(provider->resolv, provider->host,
525 resolv_result, provider);
528 void __vpn_provider_append_properties(struct vpn_provider *provider,
529 DBusMessageIter *iter)
531 if (provider->host != NULL)
532 connman_dbus_dict_append_basic(iter, "Host",
533 DBUS_TYPE_STRING, &provider->host);
535 if (provider->domain != NULL)
536 connman_dbus_dict_append_basic(iter, "Domain",
537 DBUS_TYPE_STRING, &provider->domain);
539 if (provider->type != NULL)
540 connman_dbus_dict_append_basic(iter, "Type", DBUS_TYPE_STRING,
544 int __vpn_provider_append_user_route(struct vpn_provider *provider,
545 int family, const char *network, const char *netmask)
547 struct vpn_route *route;
548 char *key = g_strdup_printf("%d/%s/%s", family, network, netmask);
550 DBG("family %d network %s netmask %s", family, network, netmask);
552 route = g_hash_table_lookup(provider->user_routes, key);
554 route = g_try_new0(struct vpn_route, 1);
556 connman_error("out of memory");
560 route->family = family;
561 route->network = g_strdup(network);
562 route->netmask = g_strdup(netmask);
564 g_hash_table_replace(provider->user_routes, key, route);
571 static struct vpn_route *get_route(char *route_str)
573 char **elems = g_strsplit(route_str, "/", 0);
574 char *network, *netmask, *gateway, *family_str;
575 int family = PF_UNSPEC;
576 struct vpn_route *route = NULL;
581 family_str = elems[0];
584 if (network == NULL || network[0] == '\0')
588 if (netmask == NULL || netmask[0] == '\0')
593 route = g_try_new0(struct vpn_route, 1);
597 if (family_str[0] == '\0' || atoi(family_str) == 0) {
600 switch (family_str[0]) {
610 if (g_strrstr(network, ":") != NULL) {
611 if (family != PF_UNSPEC && family != AF_INET6)
612 DBG("You have IPv6 address but you have non IPv6 route");
613 } else if (g_strrstr(network, ".") != NULL) {
614 if (family != PF_UNSPEC && family != AF_INET)
615 DBG("You have IPv4 address but you have non IPv4 route");
617 if (g_strrstr(netmask, ".") == NULL) {
618 /* We have netmask length */
620 struct in_addr netmask_in;
621 unsigned char prefix_len = 32;
623 if (netmask != NULL) {
625 long int value = strtol(netmask, &ptr, 10);
626 if (ptr != netmask && *ptr == '\0' &&
631 addr = 0xffffffff << (32 - prefix_len);
632 netmask_in.s_addr = htonl(addr);
633 netmask = inet_ntoa(netmask_in);
635 DBG("network %s netmask %s", network, netmask);
639 if (family == PF_UNSPEC) {
640 family = connman_inet_check_ipaddress(network);
641 if (family < 0 || family == PF_UNSPEC)
645 route->family = family;
646 route->network = g_strdup(network);
647 route->netmask = g_strdup(netmask);
648 route->gateway = g_strdup(gateway);
655 static GSList *get_routes(gchar **networks)
657 struct vpn_route *route;
658 GSList *routes = NULL;
661 for (i = 0; networks[i] != NULL; i++) {
662 route = get_route(networks[i]);
664 routes = g_slist_prepend(routes, route);
670 static int provider_load_from_keyfile(struct vpn_provider *provider,
676 gsize length, num_user_networks;
677 gchar **networks = NULL;
679 settings = g_key_file_get_keys(keyfile, provider->identifier, &length,
681 if (settings == NULL) {
682 g_key_file_free(keyfile);
686 while (idx < length) {
689 if (g_str_equal(key, "Networks") == TRUE) {
690 networks = g_key_file_get_string_list(keyfile,
691 provider->identifier,
695 provider->user_networks = get_routes(networks);
698 value = g_key_file_get_string(keyfile,
699 provider->identifier,
701 vpn_provider_set_string(provider, key,
708 g_strfreev(settings);
709 g_strfreev(networks);
711 if (provider->user_networks != NULL)
712 set_user_networks(provider, provider->user_networks);
718 static int vpn_provider_load(struct vpn_provider *provider)
722 DBG("provider %p", provider);
724 keyfile = __connman_storage_load_provider(provider->identifier);
728 provider_load_from_keyfile(provider, keyfile);
730 g_key_file_free(keyfile);
734 static gchar **create_network_list(GSList *networks, gsize *count)
737 gchar **result = NULL;
738 unsigned int num_elems = 0;
740 for (list = networks; list != NULL; list = g_slist_next(list)) {
741 struct vpn_route *route = list->data;
744 result = g_try_realloc(result,
745 (num_elems + 1) * sizeof(gchar *));
749 switch (route->family) {
761 result[num_elems] = g_strdup_printf("%d/%s/%s/%s",
762 family, route->network, route->netmask,
763 route->gateway == NULL ? "" : route->gateway);
768 result = g_try_realloc(result, (num_elems + 1) * sizeof(gchar *));
772 result[num_elems] = NULL;
777 static int vpn_provider_save(struct vpn_provider *provider)
781 DBG("provider %p", provider);
783 keyfile = g_key_file_new();
787 g_key_file_set_string(keyfile, provider->identifier,
788 "Name", provider->name);
789 g_key_file_set_string(keyfile, provider->identifier,
790 "Type", provider->type);
791 g_key_file_set_string(keyfile, provider->identifier,
792 "Host", provider->host);
793 g_key_file_set_string(keyfile, provider->identifier,
794 "VPN.Domain", provider->domain);
795 if (provider->user_networks != NULL) {
799 networks = create_network_list(provider->user_networks,
801 if (networks != NULL) {
802 g_key_file_set_string_list(keyfile,
803 provider->identifier,
805 (const gchar ** const)networks,
807 g_strfreev(networks);
811 if (provider->driver != NULL && provider->driver->save != NULL)
812 provider->driver->save(provider, keyfile);
814 __connman_storage_save_provider(keyfile, provider->identifier);
815 g_key_file_free(keyfile);
820 static struct vpn_provider *vpn_provider_lookup(const char *identifier)
822 struct vpn_provider *provider = NULL;
824 provider = g_hash_table_lookup(provider_hash, identifier);
829 static gboolean match_driver(struct vpn_provider *provider,
830 struct vpn_provider_driver *driver)
832 if (g_strcmp0(driver->name, provider->type) == 0)
838 static int provider_probe(struct vpn_provider *provider)
842 DBG("provider %p name %s", provider, provider->name);
844 if (provider->driver != NULL)
847 for (list = driver_list; list; list = list->next) {
848 struct vpn_provider_driver *driver = list->data;
850 if (match_driver(provider, driver) == FALSE)
853 DBG("driver %p name %s", driver, driver->name);
855 if (driver->probe != NULL && driver->probe(provider) == 0) {
856 provider->driver = driver;
861 if (provider->driver == NULL)
867 static void provider_remove(struct vpn_provider *provider)
869 if (provider->driver != NULL) {
870 provider->driver->remove(provider);
871 provider->driver = NULL;
875 static int provider_register(struct vpn_provider *provider)
877 return provider_probe(provider);
880 static void provider_unregister(struct vpn_provider *provider)
882 provider_remove(provider);
885 struct vpn_provider *
886 vpn_provider_ref_debug(struct vpn_provider *provider,
887 const char *file, int line, const char *caller)
889 DBG("%p ref %d by %s:%d:%s()", provider, provider->refcount + 1,
892 __sync_fetch_and_add(&provider->refcount, 1);
897 static void provider_destruct(struct vpn_provider *provider)
899 DBG("provider %p", provider);
901 if (provider->notify_id != 0)
902 g_source_remove(provider->notify_id);
904 g_free(provider->name);
905 g_free(provider->type);
906 g_free(provider->host);
907 g_free(provider->domain);
908 g_free(provider->identifier);
909 g_free(provider->path);
910 g_slist_free_full(provider->user_networks, free_route);
911 g_strfreev(provider->nameservers);
912 g_hash_table_destroy(provider->routes);
913 g_hash_table_destroy(provider->user_routes);
914 g_hash_table_destroy(provider->setting_strings);
915 if (provider->resolv != NULL) {
916 g_resolv_unref(provider->resolv);
917 provider->resolv = NULL;
919 __vpn_ipconfig_unref(provider->ipconfig_ipv4);
920 __vpn_ipconfig_unref(provider->ipconfig_ipv6);
922 g_strfreev(provider->host_ip);
926 void vpn_provider_unref_debug(struct vpn_provider *provider,
927 const char *file, int line, const char *caller)
929 DBG("%p ref %d by %s:%d:%s()", provider, provider->refcount - 1,
932 if (__sync_fetch_and_sub(&provider->refcount, 1) != 1)
935 provider_remove(provider);
937 provider_destruct(provider);
940 static void configuration_count_add(void)
942 DBG("count %d", configuration_count + 1);
944 __sync_fetch_and_add(&configuration_count, 1);
947 static void configuration_count_del(void)
949 DBG("count %d", configuration_count - 1);
951 if (__sync_fetch_and_sub(&configuration_count, 1) != 1)
957 int __vpn_provider_disconnect(struct vpn_provider *provider)
961 DBG("provider %p", provider);
963 if (provider->driver != NULL && provider->driver->disconnect != NULL)
964 err = provider->driver->disconnect(provider);
969 if (err != -EINPROGRESS)
978 int __vpn_provider_connect(struct vpn_provider *provider)
982 DBG("provider %p", provider);
984 if (provider->driver != NULL && provider->driver->connect != NULL)
985 err = provider->driver->connect(provider);
992 static void connection_removed_signal(struct vpn_provider *provider)
995 DBusMessageIter iter;
997 signal = dbus_message_new_signal(VPN_MANAGER_PATH,
998 VPN_MANAGER_INTERFACE, "ConnectionRemoved");
1002 dbus_message_iter_init_append(signal, &iter);
1003 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
1005 dbus_connection_send(connection, signal, NULL);
1006 dbus_message_unref(signal);
1009 static char *get_ident(const char *path)
1016 pos = strrchr(path, '/');
1023 int __vpn_provider_remove(const char *path)
1025 struct vpn_provider *provider;
1028 DBG("path %s", path);
1030 ident = get_ident(path);
1032 provider = vpn_provider_lookup(ident);
1033 if (provider != NULL) {
1034 DBG("Removing VPN %s", provider->identifier);
1036 connection_removed_signal(provider);
1038 provider_unregister(provider);
1039 g_hash_table_remove(provider_hash, provider->identifier);
1046 static void append_ipv4(DBusMessageIter *iter, void *user_data)
1048 struct vpn_provider *provider = user_data;
1049 const char *address, *gateway, *peer;
1051 address = __vpn_ipconfig_get_local(provider->ipconfig_ipv4);
1052 if (address != NULL) {
1054 struct in_addr netmask;
1058 prefixlen = __vpn_ipconfig_get_prefixlen(
1059 provider->ipconfig_ipv4);
1061 addr = 0xffffffff << (32 - prefixlen);
1062 netmask.s_addr = htonl(addr);
1063 mask = inet_ntoa(netmask);
1065 connman_dbus_dict_append_basic(iter, "Address",
1066 DBUS_TYPE_STRING, &address);
1068 connman_dbus_dict_append_basic(iter, "Netmask",
1069 DBUS_TYPE_STRING, &mask);
1072 gateway = __vpn_ipconfig_get_gateway(provider->ipconfig_ipv4);
1073 if (gateway != NULL)
1074 connman_dbus_dict_append_basic(iter, "Gateway",
1075 DBUS_TYPE_STRING, &gateway);
1077 peer = __vpn_ipconfig_get_peer(provider->ipconfig_ipv4);
1079 connman_dbus_dict_append_basic(iter, "Peer",
1080 DBUS_TYPE_STRING, &peer);
1083 static void append_ipv6(DBusMessageIter *iter, void *user_data)
1085 struct vpn_provider *provider = user_data;
1086 const char *address, *gateway, *peer;
1088 address = __vpn_ipconfig_get_local(provider->ipconfig_ipv6);
1089 if (address != NULL) {
1090 unsigned char prefixlen;
1092 connman_dbus_dict_append_basic(iter, "Address",
1093 DBUS_TYPE_STRING, &address);
1095 prefixlen = __vpn_ipconfig_get_prefixlen(
1096 provider->ipconfig_ipv6);
1098 connman_dbus_dict_append_basic(iter, "PrefixLength",
1099 DBUS_TYPE_BYTE, &prefixlen);
1102 gateway = __vpn_ipconfig_get_gateway(provider->ipconfig_ipv6);
1103 if (gateway != NULL)
1104 connman_dbus_dict_append_basic(iter, "Gateway",
1105 DBUS_TYPE_STRING, &gateway);
1107 peer = __vpn_ipconfig_get_peer(provider->ipconfig_ipv6);
1109 connman_dbus_dict_append_basic(iter, "Peer",
1110 DBUS_TYPE_STRING, &peer);
1113 static const char *state2string(enum vpn_provider_state state)
1116 case VPN_PROVIDER_STATE_UNKNOWN:
1118 case VPN_PROVIDER_STATE_IDLE:
1120 case VPN_PROVIDER_STATE_CONNECT:
1121 return "configuration";
1122 case VPN_PROVIDER_STATE_READY:
1124 case VPN_PROVIDER_STATE_DISCONNECT:
1125 return "disconnect";
1126 case VPN_PROVIDER_STATE_FAILURE:
1133 static int provider_indicate_state(struct vpn_provider *provider,
1134 enum vpn_provider_state state)
1138 DBG("provider %p state %d", provider, state);
1140 str = state2string(state);
1144 provider->state = state;
1146 if (state == VPN_PROVIDER_STATE_READY) {
1147 connman_dbus_property_changed_basic(provider->path,
1148 VPN_CONNECTION_INTERFACE, "Index",
1149 DBUS_TYPE_INT32, &provider->index);
1151 if (provider->family == AF_INET)
1152 connman_dbus_property_changed_dict(provider->path,
1153 VPN_CONNECTION_INTERFACE, "IPv4",
1154 append_ipv4, provider);
1155 else if (provider->family == AF_INET6)
1156 connman_dbus_property_changed_dict(provider->path,
1157 VPN_CONNECTION_INTERFACE, "IPv6",
1158 append_ipv6, provider);
1161 connman_dbus_property_changed_basic(provider->path,
1162 VPN_CONNECTION_INTERFACE, "State",
1163 DBUS_TYPE_STRING, &str);
1167 static void append_nameservers(DBusMessageIter *iter, char **servers)
1173 for (i = 0; servers[i] != NULL; i++) {
1174 DBG("servers[%d] %s", i, servers[i]);
1175 dbus_message_iter_append_basic(iter,
1176 DBUS_TYPE_STRING, &servers[i]);
1180 static void append_dns(DBusMessageIter *iter, void *user_data)
1182 struct vpn_provider *provider = user_data;
1184 if (provider->nameservers != NULL)
1185 append_nameservers(iter, provider->nameservers);
1188 static void append_state(DBusMessageIter *iter,
1189 struct vpn_provider *provider)
1193 switch (provider->state) {
1194 case VPN_PROVIDER_STATE_UNKNOWN:
1195 case VPN_PROVIDER_STATE_IDLE:
1198 case VPN_PROVIDER_STATE_CONNECT:
1199 str = "configuration";
1201 case VPN_PROVIDER_STATE_READY:
1204 case VPN_PROVIDER_STATE_DISCONNECT:
1207 case VPN_PROVIDER_STATE_FAILURE:
1212 connman_dbus_dict_append_basic(iter, "State",
1213 DBUS_TYPE_STRING, &str);
1216 static void append_properties(DBusMessageIter *iter,
1217 struct vpn_provider *provider)
1219 DBusMessageIter dict;
1221 connman_dbus_dict_open(iter, &dict);
1223 append_state(&dict, provider);
1225 if (provider->type != NULL)
1226 connman_dbus_dict_append_basic(&dict, "Type",
1227 DBUS_TYPE_STRING, &provider->type);
1229 if (provider->name != NULL)
1230 connman_dbus_dict_append_basic(&dict, "Name",
1231 DBUS_TYPE_STRING, &provider->name);
1233 if (provider->host != NULL)
1234 connman_dbus_dict_append_basic(&dict, "Host",
1235 DBUS_TYPE_STRING, &provider->host);
1236 if (provider->index >= 0)
1237 connman_dbus_dict_append_basic(&dict, "Index",
1238 DBUS_TYPE_INT32, &provider->index);
1239 if (provider->domain != NULL)
1240 connman_dbus_dict_append_basic(&dict, "Domain",
1241 DBUS_TYPE_STRING, &provider->domain);
1243 if (provider->family == AF_INET)
1244 connman_dbus_dict_append_dict(&dict, "IPv4", append_ipv4,
1246 else if (provider->family == AF_INET6)
1247 connman_dbus_dict_append_dict(&dict, "IPv6", append_ipv6,
1250 connman_dbus_dict_append_array(&dict, "Nameservers",
1251 DBUS_TYPE_STRING, append_dns, provider);
1253 connman_dbus_dict_append_array(&dict, "UserRoutes",
1254 DBUS_TYPE_DICT_ENTRY, append_routes,
1255 provider->user_routes);
1257 connman_dbus_dict_append_array(&dict, "ServerRoutes",
1258 DBUS_TYPE_DICT_ENTRY, append_routes,
1261 connman_dbus_dict_close(iter, &dict);
1264 static void connection_added_signal(struct vpn_provider *provider)
1266 DBusMessage *signal;
1267 DBusMessageIter iter;
1269 signal = dbus_message_new_signal(VPN_MANAGER_PATH,
1270 VPN_MANAGER_INTERFACE, "ConnectionAdded");
1274 dbus_message_iter_init_append(signal, &iter);
1275 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
1277 append_properties(&iter, provider);
1279 dbus_connection_send(connection, signal, NULL);
1280 dbus_message_unref(signal);
1283 static connman_bool_t check_host(char **hosts, char *host)
1290 for (i = 0; hosts[i] != NULL; i++) {
1291 if (g_strcmp0(hosts[i], host) == 0)
1298 static void provider_append_routes(gpointer key, gpointer value,
1301 struct vpn_route *route = value;
1302 struct vpn_provider *provider = user_data;
1303 int index = provider->index;
1305 if (handle_routes == FALSE)
1309 * If the VPN administrator/user has given a route to
1310 * VPN server, then we must discard that because the
1311 * server cannot be contacted via VPN tunnel.
1313 if (check_host(provider->host_ip, route->network) == TRUE) {
1314 DBG("Discarding VPN route to %s via %s at index %d",
1315 route->network, route->gateway, index);
1319 if (route->family == AF_INET6) {
1320 unsigned char prefix_len = atoi(route->netmask);
1322 connman_inet_add_ipv6_network_route(index, route->network,
1326 connman_inet_add_network_route(index, route->network,
1332 static int set_connected(struct vpn_provider *provider,
1333 connman_bool_t connected)
1335 struct vpn_ipconfig *ipconfig;
1337 DBG("provider %p id %s connected %d", provider,
1338 provider->identifier, connected);
1340 if (connected == TRUE) {
1341 if (provider->family == AF_INET6)
1342 ipconfig = provider->ipconfig_ipv6;
1344 ipconfig = provider->ipconfig_ipv4;
1346 __vpn_ipconfig_address_add(ipconfig, provider->family);
1348 if (handle_routes == TRUE)
1349 __vpn_ipconfig_gateway_add(ipconfig, provider->family);
1351 provider_indicate_state(provider,
1352 VPN_PROVIDER_STATE_READY);
1354 g_hash_table_foreach(provider->routes, provider_append_routes,
1357 g_hash_table_foreach(provider->user_routes,
1358 provider_append_routes, provider);
1361 provider_indicate_state(provider,
1362 VPN_PROVIDER_STATE_DISCONNECT);
1364 provider_indicate_state(provider,
1365 VPN_PROVIDER_STATE_IDLE);
1371 int vpn_provider_set_state(struct vpn_provider *provider,
1372 enum vpn_provider_state state)
1374 if (provider == NULL)
1378 case VPN_PROVIDER_STATE_UNKNOWN:
1380 case VPN_PROVIDER_STATE_IDLE:
1381 return set_connected(provider, FALSE);
1382 case VPN_PROVIDER_STATE_CONNECT:
1383 return provider_indicate_state(provider, state);
1384 case VPN_PROVIDER_STATE_READY:
1385 return set_connected(provider, TRUE);
1386 case VPN_PROVIDER_STATE_DISCONNECT:
1387 return provider_indicate_state(provider, state);
1388 case VPN_PROVIDER_STATE_FAILURE:
1389 return provider_indicate_state(provider, state);
1394 int vpn_provider_indicate_error(struct vpn_provider *provider,
1395 enum vpn_provider_error error)
1397 DBG("provider %p id %s error %d", provider, provider->identifier,
1401 case VPN_PROVIDER_ERROR_LOGIN_FAILED:
1403 case VPN_PROVIDER_ERROR_AUTH_FAILED:
1405 case VPN_PROVIDER_ERROR_CONNECT_FAILED:
1414 static void unregister_provider(gpointer data)
1416 struct vpn_provider *provider = data;
1418 configuration_count_del();
1420 vpn_provider_unref(provider);
1423 static void provider_initialize(struct vpn_provider *provider)
1425 DBG("provider %p", provider);
1427 provider->index = 0;
1429 provider->name = NULL;
1430 provider->type = NULL;
1431 provider->domain = NULL;
1432 provider->identifier = NULL;
1433 provider->user_networks = NULL;
1434 provider->routes = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1436 provider->user_routes = g_hash_table_new_full(g_str_hash, g_str_equal,
1437 g_free, free_route);
1438 provider->setting_strings = g_hash_table_new_full(g_str_hash,
1439 g_str_equal, g_free, g_free);
1442 static struct vpn_provider *vpn_provider_new(void)
1444 struct vpn_provider *provider;
1446 provider = g_try_new0(struct vpn_provider, 1);
1447 if (provider == NULL)
1450 provider->refcount = 1;
1452 DBG("provider %p", provider);
1453 provider_initialize(provider);
1458 static struct vpn_provider *vpn_provider_get(const char *identifier)
1460 struct vpn_provider *provider;
1462 provider = g_hash_table_lookup(provider_hash, identifier);
1463 if (provider != NULL)
1466 provider = vpn_provider_new();
1467 if (provider == NULL)
1470 DBG("provider %p", provider);
1472 provider->identifier = g_strdup(identifier);
1474 g_hash_table_insert(provider_hash, provider->identifier, provider);
1476 configuration_count_add();
1481 static void provider_dbus_ident(char *ident)
1483 int i, len = strlen(ident);
1485 for (i = 0; i < len; i++) {
1486 if (ident[i] >= '0' && ident[i] <= '9')
1488 if (ident[i] >= 'a' && ident[i] <= 'z')
1490 if (ident[i] >= 'A' && ident[i] <= 'Z')
1496 static int connection_unregister(struct vpn_provider *provider)
1498 if (provider->path == NULL)
1501 g_dbus_unregister_interface(connection, provider->path,
1502 VPN_CONNECTION_INTERFACE);
1504 g_free(provider->path);
1505 provider->path = NULL;
1510 static int connection_register(struct vpn_provider *provider)
1512 DBG("provider %p path %s", provider, provider->path);
1514 if (provider->path != NULL)
1517 provider->path = g_strdup_printf("%s/connection/%s", VPN_PATH,
1518 provider->identifier);
1520 g_dbus_register_interface(connection, provider->path,
1521 VPN_CONNECTION_INTERFACE,
1522 connection_methods, connection_signals,
1523 NULL, provider, NULL);
1528 static struct vpn_provider *provider_create_from_keyfile(GKeyFile *keyfile,
1531 struct vpn_provider *provider;
1533 if (keyfile == NULL || ident == NULL)
1536 provider = vpn_provider_lookup(ident);
1537 if (provider == NULL) {
1538 provider = vpn_provider_get(ident);
1539 if (provider == NULL) {
1540 DBG("can not create provider");
1544 provider_load_from_keyfile(provider, keyfile);
1546 if (provider->name == NULL || provider->host == NULL ||
1547 provider->domain == NULL) {
1548 DBG("cannot get name, host or domain");
1549 vpn_provider_unref(provider);
1553 if (provider_register(provider) == 0)
1554 connection_register(provider);
1559 static void provider_create_all_from_type(const char *provider_type)
1566 DBG("provider type %s", provider_type);
1568 providers = __connman_storage_get_providers();
1570 for (i = 0; providers[i] != NULL; i+=1) {
1572 if (strncmp(providers[i], "provider_", 9) != 0)
1575 id = providers[i] + 9;
1576 keyfile = __connman_storage_load_provider(id);
1578 if (keyfile == NULL)
1581 type = g_key_file_get_string(keyfile, id, "Type", NULL);
1583 DBG("keyfile %p id %s type %s", keyfile, id, type);
1585 if (strcmp(provider_type, type) != 0) {
1587 g_key_file_free(keyfile);
1591 if (provider_create_from_keyfile(keyfile, id) == NULL)
1592 DBG("could not create provider");
1595 g_key_file_free(keyfile);
1597 g_strfreev(providers);
1600 int __vpn_provider_create(DBusMessage *msg)
1602 struct vpn_provider *provider;
1603 DBusMessageIter iter, array;
1604 const char *type = NULL, *name = NULL;
1605 const char *host = NULL, *domain = NULL;
1606 GSList *networks = NULL;
1610 dbus_message_iter_init(msg, &iter);
1611 dbus_message_iter_recurse(&iter, &array);
1613 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1614 DBusMessageIter entry, value;
1617 dbus_message_iter_recurse(&array, &entry);
1618 dbus_message_iter_get_basic(&entry, &key);
1620 dbus_message_iter_next(&entry);
1621 dbus_message_iter_recurse(&entry, &value);
1623 switch (dbus_message_iter_get_arg_type(&value)) {
1624 case DBUS_TYPE_STRING:
1625 if (g_str_equal(key, "Type") == TRUE)
1626 dbus_message_iter_get_basic(&value, &type);
1627 else if (g_str_equal(key, "Name") == TRUE)
1628 dbus_message_iter_get_basic(&value, &name);
1629 else if (g_str_equal(key, "Host") == TRUE)
1630 dbus_message_iter_get_basic(&value, &host);
1631 else if (g_str_equal(key, "VPN.Domain") == TRUE)
1632 dbus_message_iter_get_basic(&value, &domain);
1634 case DBUS_TYPE_ARRAY:
1635 if (g_str_equal(key, "UserRoutes") == TRUE)
1636 networks = get_user_networks(&value);
1640 dbus_message_iter_next(&array);
1643 if (host == NULL || domain == NULL)
1646 DBG("Type %s name %s networks %p", type, name, networks);
1648 if (type == NULL || name == NULL)
1651 ident = g_strdup_printf("%s_%s", host, domain);
1652 provider_dbus_ident(ident);
1654 DBG("ident %s", ident);
1656 provider = vpn_provider_lookup(ident);
1657 if (provider == NULL) {
1658 provider = vpn_provider_get(ident);
1659 if (provider == NULL) {
1660 DBG("can not create provider");
1665 provider->host = g_strdup(host);
1666 provider->domain = g_strdup(domain);
1667 provider->name = g_strdup(name);
1668 provider->type = g_strdup(type);
1670 if (provider_register(provider) == 0)
1671 vpn_provider_load(provider);
1673 provider_resolv_host_addr(provider);
1676 if (networks != NULL) {
1677 g_slist_free_full(provider->user_networks, free_route);
1678 provider->user_networks = networks;
1679 set_user_networks(provider, provider->user_networks);
1682 dbus_message_iter_init(msg, &iter);
1683 dbus_message_iter_recurse(&iter, &array);
1685 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1686 DBusMessageIter entry, value;
1687 const char *key, *str;
1689 dbus_message_iter_recurse(&array, &entry);
1690 dbus_message_iter_get_basic(&entry, &key);
1692 dbus_message_iter_next(&entry);
1693 dbus_message_iter_recurse(&entry, &value);
1695 switch (dbus_message_iter_get_arg_type(&value)) {
1696 case DBUS_TYPE_STRING:
1697 dbus_message_iter_get_basic(&value, &str);
1698 vpn_provider_set_string(provider, key, str);
1702 dbus_message_iter_next(&array);
1707 vpn_provider_save(provider);
1709 err = provider_register(provider);
1710 if (err != 0 && err != -EALREADY)
1713 connection_register(provider);
1715 DBG("provider %p index %d path %s", provider, provider->index,
1718 g_dbus_send_reply(connection, msg,
1719 DBUS_TYPE_OBJECT_PATH, &provider->path,
1722 connection_added_signal(provider);
1727 static void append_connection_structs(DBusMessageIter *iter, void *user_data)
1729 DBusMessageIter entry;
1730 GHashTableIter hash;
1731 gpointer value, key;
1733 g_hash_table_iter_init(&hash, provider_hash);
1735 while (g_hash_table_iter_next(&hash, &key, &value) == TRUE) {
1736 struct vpn_provider *provider = value;
1738 DBG("path %s", provider->path);
1740 if (provider->identifier == NULL)
1743 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT,
1745 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1747 append_properties(&entry, provider);
1748 dbus_message_iter_close_container(iter, &entry);
1752 DBusMessage *__vpn_provider_get_connections(DBusMessage *msg)
1758 reply = dbus_message_new_method_return(msg);
1762 __connman_dbus_append_objpath_dict_array(reply,
1763 append_connection_structs, NULL);
1768 const char * __vpn_provider_get_ident(struct vpn_provider *provider)
1770 if (provider == NULL)
1773 return provider->identifier;
1776 int vpn_provider_set_string(struct vpn_provider *provider,
1777 const char *key, const char *value)
1779 DBG("provider %p key %s value %s", provider, key, value);
1781 if (g_str_equal(key, "Type") == TRUE) {
1782 g_free(provider->type);
1783 provider->type = g_strdup(value);
1784 } else if (g_str_equal(key, "Name") == TRUE) {
1785 g_free(provider->name);
1786 provider->name = g_strdup(value);
1787 } else if (g_str_equal(key, "Host") == TRUE) {
1788 g_free(provider->host);
1789 provider->host = g_strdup(value);
1790 } else if (g_str_equal(key, "VPN.Domain") == TRUE) {
1791 g_free(provider->domain);
1792 provider->domain = g_strdup(value);
1794 g_hash_table_replace(provider->setting_strings,
1795 g_strdup(key), g_strdup(value));
1799 const char *vpn_provider_get_string(struct vpn_provider *provider,
1802 DBG("provider %p key %s", provider, key);
1804 if (g_str_equal(key, "Type") == TRUE)
1805 return provider->type;
1806 else if (g_str_equal(key, "Name") == TRUE)
1807 return provider->name;
1808 else if (g_str_equal(key, "Host") == TRUE)
1809 return provider->host;
1810 else if (g_str_equal(key, "HostIP") == TRUE) {
1811 if (provider->host_ip == NULL ||
1812 provider->host_ip[0] == NULL)
1813 return provider->host;
1815 return provider->host_ip[0];
1816 } else if (g_str_equal(key, "VPN.Domain") == TRUE)
1817 return provider->domain;
1819 return g_hash_table_lookup(provider->setting_strings, key);
1822 connman_bool_t __vpn_provider_check_routes(struct vpn_provider *provider)
1824 if (provider == NULL)
1827 if (provider->user_routes != NULL &&
1828 g_hash_table_size(provider->user_routes) > 0)
1831 if (provider->routes != NULL &&
1832 g_hash_table_size(provider->routes) > 0)
1838 void *vpn_provider_get_data(struct vpn_provider *provider)
1840 return provider->driver_data;
1843 void vpn_provider_set_data(struct vpn_provider *provider, void *data)
1845 provider->driver_data = data;
1848 void vpn_provider_set_index(struct vpn_provider *provider, int index)
1850 DBG("index %d provider %p", index, provider);
1852 if (provider->ipconfig_ipv4 == NULL) {
1853 provider->ipconfig_ipv4 = __vpn_ipconfig_create(index,
1855 if (provider->ipconfig_ipv4 == NULL) {
1856 DBG("Couldnt create ipconfig for IPv4");
1861 __vpn_ipconfig_set_index(provider->ipconfig_ipv4, index);
1863 if (provider->ipconfig_ipv6 == NULL) {
1864 provider->ipconfig_ipv6 = __vpn_ipconfig_create(index,
1866 if (provider->ipconfig_ipv6 == NULL) {
1867 DBG("Couldnt create ipconfig for IPv6");
1872 __vpn_ipconfig_set_index(provider->ipconfig_ipv6, index);
1875 provider->index = index;
1878 int vpn_provider_get_index(struct vpn_provider *provider)
1880 return provider->index;
1883 int vpn_provider_set_ipaddress(struct vpn_provider *provider,
1884 struct connman_ipaddress *ipaddress)
1886 struct vpn_ipconfig *ipconfig = NULL;
1888 switch (ipaddress->family) {
1890 ipconfig = provider->ipconfig_ipv4;
1893 ipconfig = provider->ipconfig_ipv6;
1899 DBG("provider %p ipconfig %p family %d", provider, ipconfig,
1902 if (ipconfig == NULL)
1905 provider->family = ipaddress->family;
1907 __vpn_ipconfig_set_local(ipconfig, ipaddress->local);
1908 __vpn_ipconfig_set_peer(ipconfig, ipaddress->peer);
1909 __vpn_ipconfig_set_broadcast(ipconfig, ipaddress->broadcast);
1910 __vpn_ipconfig_set_gateway(ipconfig, ipaddress->gateway);
1911 __vpn_ipconfig_set_prefixlen(ipconfig, ipaddress->prefixlen);
1916 int vpn_provider_set_pac(struct vpn_provider *provider,
1919 DBG("provider %p pac %s", provider, pac);
1925 int vpn_provider_set_domain(struct vpn_provider *provider,
1928 DBG("provider %p domain %s", provider, domain);
1930 g_free(provider->domain);
1931 provider->domain = g_strdup(domain);
1936 int vpn_provider_set_nameservers(struct vpn_provider *provider,
1937 const char *nameservers)
1939 DBG("provider %p nameservers %s", provider, nameservers);
1941 g_strfreev(provider->nameservers);
1942 provider->nameservers = NULL;
1944 if (nameservers == NULL)
1947 provider->nameservers = g_strsplit(nameservers, " ", 0);
1952 enum provider_route_type {
1953 PROVIDER_ROUTE_TYPE_NONE = 0,
1954 PROVIDER_ROUTE_TYPE_MASK = 1,
1955 PROVIDER_ROUTE_TYPE_ADDR = 2,
1956 PROVIDER_ROUTE_TYPE_GW = 3,
1959 static int route_env_parse(struct vpn_provider *provider, const char *key,
1960 int *family, unsigned long *idx,
1961 enum provider_route_type *type)
1966 DBG("name %s", provider->name);
1968 if (!strcmp(provider->type, "openvpn")) {
1969 if (g_str_has_prefix(key, "route_network_") == TRUE) {
1970 start = key + strlen("route_network_");
1971 *type = PROVIDER_ROUTE_TYPE_ADDR;
1972 } else if (g_str_has_prefix(key, "route_netmask_") == TRUE) {
1973 start = key + strlen("route_netmask_");
1974 *type = PROVIDER_ROUTE_TYPE_MASK;
1975 } else if (g_str_has_prefix(key, "route_gateway_") == TRUE) {
1976 start = key + strlen("route_gateway_");
1977 *type = PROVIDER_ROUTE_TYPE_GW;
1982 *idx = g_ascii_strtoull(start, &end, 10);
1984 } else if (!strcmp(provider->type, "openconnect")) {
1985 if (g_str_has_prefix(key, "CISCO_SPLIT_INC_") == TRUE) {
1987 start = key + strlen("CISCO_SPLIT_INC_");
1988 } else if (g_str_has_prefix(key,
1989 "CISCO_IPV6_SPLIT_INC_") == TRUE) {
1991 start = key + strlen("CISCO_IPV6_SPLIT_INC_");
1995 *idx = g_ascii_strtoull(start, &end, 10);
1997 if (strncmp(end, "_ADDR", 5) == 0)
1998 *type = PROVIDER_ROUTE_TYPE_ADDR;
1999 else if (strncmp(end, "_MASK", 5) == 0)
2000 *type = PROVIDER_ROUTE_TYPE_MASK;
2001 else if (strncmp(end, "_MASKLEN", 8) == 0 &&
2002 *family == AF_INET6) {
2003 *type = PROVIDER_ROUTE_TYPE_MASK;
2011 int vpn_provider_append_route(struct vpn_provider *provider,
2012 const char *key, const char *value)
2014 struct vpn_route *route;
2015 int ret, family = 0;
2016 unsigned long idx = 0;
2017 enum provider_route_type type = PROVIDER_ROUTE_TYPE_NONE;
2019 DBG("key %s value %s", key, value);
2021 ret = route_env_parse(provider, key, &family, &idx, &type);
2025 DBG("idx %lu family %d type %d", idx, family, type);
2027 route = g_hash_table_lookup(provider->routes, GINT_TO_POINTER(idx));
2028 if (route == NULL) {
2029 route = g_try_new0(struct vpn_route, 1);
2030 if (route == NULL) {
2031 connman_error("out of memory");
2035 route->family = family;
2037 g_hash_table_replace(provider->routes, GINT_TO_POINTER(idx),
2042 case PROVIDER_ROUTE_TYPE_NONE:
2044 case PROVIDER_ROUTE_TYPE_MASK:
2045 route->netmask = g_strdup(value);
2047 case PROVIDER_ROUTE_TYPE_ADDR:
2048 route->network = g_strdup(value);
2050 case PROVIDER_ROUTE_TYPE_GW:
2051 route->gateway = g_strdup(value);
2055 if (handle_routes == FALSE) {
2056 if (route->netmask != NULL && route->gateway != NULL &&
2057 route->network != NULL)
2058 provider_schedule_changed(provider,
2059 SERVER_ROUTES_CHANGED);
2065 const char *vpn_provider_get_driver_name(struct vpn_provider *provider)
2067 if (provider->driver == NULL)
2070 return provider->driver->name;
2073 const char *vpn_provider_get_save_group(struct vpn_provider *provider)
2075 return provider->identifier;
2078 static gint compare_priority(gconstpointer a, gconstpointer b)
2083 static void clean_provider(gpointer key, gpointer value, gpointer user_data)
2085 struct vpn_provider *provider = value;
2087 if (provider->driver != NULL && provider->driver->remove)
2088 provider->driver->remove(provider);
2090 connection_unregister(provider);
2093 int vpn_provider_driver_register(struct vpn_provider_driver *driver)
2095 DBG("driver %p name %s", driver, driver->name);
2097 driver_list = g_slist_insert_sorted(driver_list, driver,
2099 provider_create_all_from_type(driver->name);
2103 void vpn_provider_driver_unregister(struct vpn_provider_driver *driver)
2105 DBG("driver %p name %s", driver, driver->name);
2107 driver_list = g_slist_remove(driver_list, driver);
2110 int __vpn_provider_init(gboolean do_routes)
2114 handle_routes = do_routes;
2116 connection = connman_dbus_get_connection();
2118 provider_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2119 NULL, unregister_provider);
2124 void __vpn_provider_cleanup(void)
2128 g_hash_table_foreach(provider_hash, clean_provider, NULL);
2130 g_hash_table_destroy(provider_hash);
2131 provider_hash = NULL;
2133 dbus_connection_unref(connection);