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 #define CONNECT_TIMEOUT 120
36 static DBusConnection *connection = NULL;
38 static GSequence *service_list = NULL;
39 static GHashTable *service_hash = NULL;
40 static GSList *counter_list = NULL;
42 struct connman_stats {
44 connman_bool_t enabled;
45 struct connman_stats_data data_last;
46 struct connman_stats_data data;
50 struct connman_stats_counter {
51 connman_bool_t append_all;
52 struct connman_stats stats;
53 struct connman_stats stats_roaming;
56 struct connman_service {
58 gint session_usage_count;
61 enum connman_service_type type;
62 enum connman_service_security security;
63 enum connman_service_state state;
64 enum connman_service_state state_ipv4;
65 enum connman_service_state state_ipv6;
66 enum connman_service_error error;
67 connman_uint8_t strength;
68 connman_bool_t favorite;
69 connman_bool_t immutable;
70 connman_bool_t hidden;
71 connman_bool_t ignore;
72 connman_bool_t autoconnect;
73 connman_bool_t userconnect;
78 char *agent_passphrase;
79 connman_bool_t roaming;
80 connman_bool_t login_required;
81 connman_bool_t network_created;
82 struct connman_ipconfig *ipconfig_ipv4;
83 struct connman_ipconfig *ipconfig_ipv6;
84 struct connman_network *network;
85 struct connman_provider *provider;
87 char **nameservers_config;
91 /* 802.1x settings from the config files */
96 char *client_cert_file;
97 char *private_key_file;
98 char *private_key_passphrase;
100 DBusMessage *pending;
102 struct connman_location *location;
103 struct connman_stats stats;
104 struct connman_stats stats_roaming;
105 GHashTable *counter_table;
106 enum connman_service_proxy_method proxy;
107 enum connman_service_proxy_method proxy_config;
114 static void append_path(gpointer value, gpointer user_data)
116 struct connman_service *service = value;
117 DBusMessageIter *iter = user_data;
119 if (service->path == NULL || service->hidden == TRUE)
122 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
126 void __connman_service_list(DBusMessageIter *iter, void *user_data)
128 if (service_list == NULL)
131 g_sequence_foreach(service_list, append_path, iter);
136 struct connman_service *service;
139 static void compare_path(gpointer value, gpointer user_data)
141 struct connman_service *service = value;
142 struct find_data *data = user_data;
144 if (data->service != NULL)
147 if (g_strcmp0(service->path, data->path) == 0)
148 data->service = service;
151 static struct connman_service *find_service(const char *path)
153 struct find_data data = { .path = path, .service = NULL };
155 DBG("path %s", path);
157 g_sequence_foreach(service_list, compare_path, &data);
162 const char *__connman_service_type2string(enum connman_service_type type)
165 case CONNMAN_SERVICE_TYPE_UNKNOWN:
167 case CONNMAN_SERVICE_TYPE_SYSTEM:
169 case CONNMAN_SERVICE_TYPE_ETHERNET:
171 case CONNMAN_SERVICE_TYPE_WIFI:
173 case CONNMAN_SERVICE_TYPE_WIMAX:
175 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
177 case CONNMAN_SERVICE_TYPE_CELLULAR:
179 case CONNMAN_SERVICE_TYPE_GPS:
181 case CONNMAN_SERVICE_TYPE_VPN:
183 case CONNMAN_SERVICE_TYPE_GADGET:
190 static const char *security2string(enum connman_service_security security)
193 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
195 case CONNMAN_SERVICE_SECURITY_NONE:
197 case CONNMAN_SERVICE_SECURITY_WEP:
199 case CONNMAN_SERVICE_SECURITY_PSK:
201 case CONNMAN_SERVICE_SECURITY_8021X:
203 case CONNMAN_SERVICE_SECURITY_WPA:
205 case CONNMAN_SERVICE_SECURITY_RSN:
212 static const char *state2string(enum connman_service_state state)
215 case CONNMAN_SERVICE_STATE_UNKNOWN:
217 case CONNMAN_SERVICE_STATE_IDLE:
219 case CONNMAN_SERVICE_STATE_ASSOCIATION:
220 return "association";
221 case CONNMAN_SERVICE_STATE_CONFIGURATION:
222 return "configuration";
223 case CONNMAN_SERVICE_STATE_READY:
225 case CONNMAN_SERVICE_STATE_ONLINE:
227 case CONNMAN_SERVICE_STATE_DISCONNECT:
229 case CONNMAN_SERVICE_STATE_FAILURE:
236 static const char *error2string(enum connman_service_error error)
239 case CONNMAN_SERVICE_ERROR_UNKNOWN:
241 case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
242 return "out-of-range";
243 case CONNMAN_SERVICE_ERROR_PIN_MISSING:
244 return "pin-missing";
245 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
246 return "dhcp-failed";
247 case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
248 return "connect-failed";
249 case CONNMAN_SERVICE_ERROR_LOGIN_FAILED:
250 return "login-failed";
251 case CONNMAN_SERVICE_ERROR_AUTH_FAILED:
252 return "auth-failed";
253 case CONNMAN_SERVICE_ERROR_INVALID_KEY:
254 return "invalid-key";
260 static enum connman_service_error string2error(const char *error)
262 if (g_strcmp0(error, "dhcp-failed") == 0)
263 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
264 else if (g_strcmp0(error, "pin-missing") == 0)
265 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
266 else if (g_strcmp0(error, "invalid-key") == 0)
267 return CONNMAN_SERVICE_ERROR_INVALID_KEY;
269 return CONNMAN_SERVICE_ERROR_UNKNOWN;
272 static const char *proxymethod2string(enum connman_service_proxy_method method)
275 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
277 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
279 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
281 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
288 static enum connman_service_proxy_method string2proxymethod(const char *method)
290 if (g_strcmp0(method, "direct") == 0)
291 return CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
292 else if (g_strcmp0(method, "auto") == 0)
293 return CONNMAN_SERVICE_PROXY_METHOD_AUTO;
294 else if (g_strcmp0(method, "manual") == 0)
295 return CONNMAN_SERVICE_PROXY_METHOD_MANUAL;
297 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
300 static guint changed_timeout = 0;
302 static gboolean notify_services_changed(gpointer user_data)
306 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
307 CONNMAN_MANAGER_INTERFACE, "Services",
308 DBUS_TYPE_OBJECT_PATH, __connman_service_list,
314 static void services_changed(gboolean delayed)
318 if (changed_timeout > 0) {
319 g_source_remove(changed_timeout);
323 if (__connman_connection_update_gateway() == TRUE) {
324 notify_services_changed(NULL);
328 if (delayed == FALSE) {
329 notify_services_changed(NULL);
333 changed_timeout = g_timeout_add_seconds(1, notify_services_changed,
337 static enum connman_service_state combine_state(
338 enum connman_service_state state_a,
339 enum connman_service_state state_b)
341 enum connman_service_state result;
343 if (state_a == state_b) {
348 if (state_a == CONNMAN_SERVICE_STATE_UNKNOWN) {
353 if (state_b == CONNMAN_SERVICE_STATE_UNKNOWN) {
358 if (state_a == CONNMAN_SERVICE_STATE_IDLE) {
363 if (state_b == CONNMAN_SERVICE_STATE_IDLE) {
368 if (state_a == CONNMAN_SERVICE_STATE_ASSOCIATION) {
369 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION ||
370 state_b == CONNMAN_SERVICE_STATE_ONLINE ||
371 state_b == CONNMAN_SERVICE_STATE_READY)
378 if (state_b == CONNMAN_SERVICE_STATE_ASSOCIATION) {
379 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION ||
380 state_a == CONNMAN_SERVICE_STATE_ONLINE ||
381 state_a == CONNMAN_SERVICE_STATE_READY)
388 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION) {
389 if (state_b == CONNMAN_SERVICE_STATE_ONLINE ||
390 state_b == CONNMAN_SERVICE_STATE_READY)
397 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION) {
398 if (state_a == CONNMAN_SERVICE_STATE_ONLINE ||
399 state_a == CONNMAN_SERVICE_STATE_READY)
406 if (state_a == CONNMAN_SERVICE_STATE_READY) {
407 if (state_b == CONNMAN_SERVICE_STATE_ONLINE ||
408 state_b == CONNMAN_SERVICE_STATE_DISCONNECT)
415 if (state_b == CONNMAN_SERVICE_STATE_READY) {
416 if (state_a == CONNMAN_SERVICE_STATE_ONLINE ||
417 state_a == CONNMAN_SERVICE_STATE_DISCONNECT)
424 if (state_a == CONNMAN_SERVICE_STATE_ONLINE) {
425 if (state_b == CONNMAN_SERVICE_STATE_DISCONNECT)
432 if (state_b == CONNMAN_SERVICE_STATE_ONLINE) {
433 if (state_a == CONNMAN_SERVICE_STATE_DISCONNECT)
440 if (state_a == CONNMAN_SERVICE_STATE_DISCONNECT) {
445 if (state_b == CONNMAN_SERVICE_STATE_DISCONNECT) {
450 result = CONNMAN_SERVICE_STATE_FAILURE;
456 static connman_bool_t is_connecting_state(struct connman_service *service,
457 enum connman_service_state state)
460 case CONNMAN_SERVICE_STATE_UNKNOWN:
461 case CONNMAN_SERVICE_STATE_IDLE:
462 case CONNMAN_SERVICE_STATE_FAILURE:
463 if (service->network != NULL)
464 return connman_network_get_connecting(service->network);
465 case CONNMAN_SERVICE_STATE_DISCONNECT:
466 case CONNMAN_SERVICE_STATE_READY:
467 case CONNMAN_SERVICE_STATE_ONLINE:
469 case CONNMAN_SERVICE_STATE_ASSOCIATION:
470 case CONNMAN_SERVICE_STATE_CONFIGURATION:
477 static connman_bool_t is_connected_state(const struct connman_service *service,
478 enum connman_service_state state)
481 case CONNMAN_SERVICE_STATE_UNKNOWN:
482 case CONNMAN_SERVICE_STATE_IDLE:
483 case CONNMAN_SERVICE_STATE_ASSOCIATION:
484 case CONNMAN_SERVICE_STATE_CONFIGURATION:
485 case CONNMAN_SERVICE_STATE_DISCONNECT:
486 case CONNMAN_SERVICE_STATE_FAILURE:
488 case CONNMAN_SERVICE_STATE_READY:
489 case CONNMAN_SERVICE_STATE_ONLINE:
496 static connman_bool_t is_connecting(struct connman_service *service)
498 return is_connecting_state(service, service->state);
501 static connman_bool_t is_connected(struct connman_service *service)
503 return is_connected_state(service, service->state);
506 static void update_nameservers(struct connman_service *service)
510 if (service->ipconfig_ipv4)
511 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv4);
512 else if (service->ipconfig_ipv6)
513 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv6);
520 switch (combine_state(service->state_ipv4, service->state_ipv6)) {
521 case CONNMAN_SERVICE_STATE_UNKNOWN:
522 case CONNMAN_SERVICE_STATE_IDLE:
523 case CONNMAN_SERVICE_STATE_ASSOCIATION:
524 case CONNMAN_SERVICE_STATE_CONFIGURATION:
526 case CONNMAN_SERVICE_STATE_FAILURE:
527 case CONNMAN_SERVICE_STATE_DISCONNECT:
528 connman_resolver_remove_all(ifname);
530 case CONNMAN_SERVICE_STATE_READY:
531 case CONNMAN_SERVICE_STATE_ONLINE:
535 connman_resolver_remove_all(ifname);
537 if (service->nameservers_config != NULL) {
540 for (i = 0; service->nameservers_config[i] != NULL; i++) {
541 connman_resolver_append(ifname, NULL,
542 service->nameservers_config[i]);
544 } else if (service->nameservers != NULL) {
547 for (i = 0; service->nameservers[i] != NULL; i++) {
548 connman_resolver_append(ifname, NULL,
549 service->nameservers[i]);
553 if (service->domains != NULL) {
556 for (i = 0; service->domains[i]; i++)
557 connman_resolver_append(ifname, service->domains[i],
559 } else if (service->domainname != NULL)
560 connman_resolver_append(ifname, service->domainname, NULL);
562 connman_resolver_flush();
565 int __connman_service_nameserver_append(struct connman_service *service,
566 const char *nameserver)
570 DBG("service %p nameserver %s", service, nameserver);
572 if (nameserver == NULL)
575 if (service->nameservers != NULL) {
578 for (i = 0; service->nameservers[i] != NULL; i++)
579 if (g_strcmp0(service->nameservers[i], nameserver) == 0)
582 len = g_strv_length(service->nameservers);
583 service->nameservers = g_try_renew(char *, service->nameservers,
587 service->nameservers = g_try_new0(char *, len + 2);
590 if (service->nameservers == NULL)
593 service->nameservers[len] = g_strdup(nameserver);
594 service->nameservers[len + 1] = NULL;
596 update_nameservers(service);
601 int __connman_service_nameserver_remove(struct connman_service *service,
602 const char *nameserver)
607 DBG("service %p nameserver %s", service, nameserver);
609 if (nameserver == NULL)
612 if (service->nameservers == NULL)
615 len = g_strv_length(service->nameservers);
617 if (g_strcmp0(service->nameservers[0], nameserver) != 0)
620 g_strfreev(service->nameservers);
621 service->nameservers = NULL;
626 servers = g_try_new0(char *, len);
630 for (i = 0, j = 0; i < len; i++) {
631 if (g_strcmp0(service->nameservers[i], nameserver) != 0) {
632 servers[j] = g_strdup(service->nameservers[i]);
636 servers[len - 1] = NULL;
638 g_strfreev(service->nameservers);
639 service->nameservers = servers;
641 update_nameservers(service);
646 void __connman_service_nameserver_clear(struct connman_service *service)
648 g_strfreev(service->nameservers);
649 service->nameservers = NULL;
651 update_nameservers(service);
654 static void nameserver_add_routes(int index, char **nameservers,
658 struct addrinfo hints;
659 struct addrinfo *addr;
661 for (i = 0; nameservers[i] != NULL; i++) {
662 memset(&hints, 0, sizeof(struct addrinfo));
663 hints.ai_flags = AI_NUMERICHOST;
666 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
667 if (ret == EAI_NONAME)
668 family = AF_INET; /* use the IPv4 as a default */
672 family = addr->ai_family;
674 if (family == AF_INET) {
675 if (connman_inet_compare_subnet(index,
676 nameservers[i]) != TRUE)
677 connman_inet_add_host_route(index,
679 } else if (family == AF_INET6)
680 connman_inet_add_ipv6_host_route(index,
687 static void nameserver_del_routes(int index, char **nameservers)
690 struct addrinfo hints;
691 struct addrinfo *addr;
693 for (i = 0; nameservers[i] != NULL; i++) {
694 memset(&hints, 0, sizeof(struct addrinfo));
695 hints.ai_flags = AI_NUMERICHOST;
698 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
699 if (ret == EAI_NONAME)
700 family = AF_INET; /* use the IPv4 as a default */
704 family = addr->ai_family;
706 if (family == AF_INET)
707 connman_inet_del_host_route(index, nameservers[i]);
708 else if (family == AF_INET6)
709 connman_inet_del_ipv6_host_route(index,
716 void __connman_service_nameserver_add_routes(struct connman_service *service,
724 if (service->network != NULL)
725 index = connman_network_get_index(service->network);
726 else if (service->provider != NULL)
727 index = connman_provider_get_index(service->provider);
729 if (service->nameservers_config != NULL) {
731 * Configured nameserver takes preference over the
732 * discoverd nameserver gathered from DHCP, VPN, etc.
734 nameserver_add_routes(index, service->nameservers_config, gw);
735 } else if (service->nameservers != NULL) {
737 * We add nameservers host routes for nameservers that
738 * are not on our subnet. For those who are, the subnet
739 * route will be installed by the time the dns proxy code
740 * tries to reach them. The subnet route is installed
741 * when setting the interface IP address.
743 nameserver_add_routes(index, service->nameservers, gw);
747 void __connman_service_nameserver_del_routes(struct connman_service *service)
754 if (service->network != NULL)
755 index = connman_network_get_index(service->network);
756 else if (service->provider != NULL)
757 index = connman_provider_get_index(service->provider);
759 if (service->nameservers_config != NULL)
760 nameserver_del_routes(index, service->nameservers_config);
761 else if (service->nameservers != NULL)
762 nameserver_del_routes(index, service->nameservers);
765 static struct connman_stats *stats_get(struct connman_service *service)
767 if (service->roaming == TRUE)
768 return &service->stats_roaming;
770 return &service->stats;
773 static connman_bool_t stats_enabled(struct connman_service *service)
775 struct connman_stats *stats = stats_get(service);
777 return stats->enabled;
780 static void stats_start(struct connman_service *service)
782 struct connman_stats *stats = stats_get(service);
784 DBG("service %p", service);
786 if (stats->timer == NULL)
789 stats->enabled = TRUE;
790 stats->data_last.time = stats->data.time;
792 g_timer_start(stats->timer);
795 static void stats_stop(struct connman_service *service)
797 struct connman_stats *stats = stats_get(service);
798 unsigned int seconds;
800 DBG("service %p", service);
802 if (stats->timer == NULL)
805 if (stats->enabled == FALSE)
808 g_timer_stop(stats->timer);
810 seconds = g_timer_elapsed(stats->timer, NULL);
811 stats->data.time = stats->data_last.time + seconds;
813 stats->enabled = FALSE;
816 static void reset_stats(struct connman_service *service)
818 DBG("service %p", service);
821 service->stats.valid = FALSE;
823 service->stats.data.rx_packets = 0;
824 service->stats.data.tx_packets = 0;
825 service->stats.data.rx_bytes = 0;
826 service->stats.data.tx_bytes = 0;
827 service->stats.data.rx_errors = 0;
828 service->stats.data.tx_errors = 0;
829 service->stats.data.rx_dropped = 0;
830 service->stats.data.tx_dropped = 0;
831 service->stats.data.time = 0;
832 service->stats.data_last.time = 0;
834 g_timer_reset(service->stats.timer);
837 service->stats_roaming.valid = FALSE;
839 service->stats_roaming.data.rx_packets = 0;
840 service->stats_roaming.data.tx_packets = 0;
841 service->stats_roaming.data.rx_bytes = 0;
842 service->stats_roaming.data.tx_bytes = 0;
843 service->stats_roaming.data.rx_errors = 0;
844 service->stats_roaming.data.tx_errors = 0;
845 service->stats_roaming.data.rx_dropped = 0;
846 service->stats_roaming.data.tx_dropped = 0;
847 service->stats_roaming.data.time = 0;
848 service->stats_roaming.data_last.time = 0;
850 g_timer_reset(service->stats_roaming.timer);
853 static struct connman_service *get_default(void)
855 struct connman_service *service;
858 iter = g_sequence_get_begin_iter(service_list);
860 if (g_sequence_iter_is_end(iter) == TRUE)
863 service = g_sequence_get(iter);
865 if (is_connected(service) == FALSE)
871 static void default_changed(void)
873 struct connman_service *service = get_default();
875 __connman_notifier_default_changed(service);
878 const char *__connman_service_default(void)
880 struct connman_service *service;
882 service = get_default();
886 return __connman_service_type2string(service->type);
889 static void state_changed(struct connman_service *service)
893 __connman_notifier_service_state_changed(service, service->state);
895 str = state2string(service->state);
899 connman_dbus_property_changed_basic(service->path,
900 CONNMAN_SERVICE_INTERFACE, "State",
901 DBUS_TYPE_STRING, &str);
904 static void strength_changed(struct connman_service *service)
906 if (service->strength == 0)
909 connman_dbus_property_changed_basic(service->path,
910 CONNMAN_SERVICE_INTERFACE, "Strength",
911 DBUS_TYPE_BYTE, &service->strength);
914 static void favorite_changed(struct connman_service *service)
916 if (service->path == NULL)
919 connman_dbus_property_changed_basic(service->path,
920 CONNMAN_SERVICE_INTERFACE, "Favorite",
921 DBUS_TYPE_BOOLEAN, &service->favorite);
924 static void immutable_changed(struct connman_service *service)
926 if (service->path == NULL)
929 connman_dbus_property_changed_basic(service->path,
930 CONNMAN_SERVICE_INTERFACE, "Immutable",
931 DBUS_TYPE_BOOLEAN, &service->immutable);
934 static void roaming_changed(struct connman_service *service)
936 if (service->path == NULL)
939 connman_dbus_property_changed_basic(service->path,
940 CONNMAN_SERVICE_INTERFACE, "Roaming",
941 DBUS_TYPE_BOOLEAN, &service->roaming);
944 static void autoconnect_changed(struct connman_service *service)
946 if (service->path == NULL)
949 connman_dbus_property_changed_basic(service->path,
950 CONNMAN_SERVICE_INTERFACE, "AutoConnect",
951 DBUS_TYPE_BOOLEAN, &service->autoconnect);
954 static void passphrase_changed(struct connman_service *service)
956 dbus_bool_t required;
958 switch (service->type) {
959 case CONNMAN_SERVICE_TYPE_UNKNOWN:
960 case CONNMAN_SERVICE_TYPE_SYSTEM:
961 case CONNMAN_SERVICE_TYPE_ETHERNET:
962 case CONNMAN_SERVICE_TYPE_WIMAX:
963 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
964 case CONNMAN_SERVICE_TYPE_CELLULAR:
965 case CONNMAN_SERVICE_TYPE_GPS:
966 case CONNMAN_SERVICE_TYPE_VPN:
967 case CONNMAN_SERVICE_TYPE_GADGET:
969 case CONNMAN_SERVICE_TYPE_WIFI:
972 switch (service->security) {
973 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
974 case CONNMAN_SERVICE_SECURITY_NONE:
976 case CONNMAN_SERVICE_SECURITY_WEP:
977 case CONNMAN_SERVICE_SECURITY_PSK:
978 case CONNMAN_SERVICE_SECURITY_WPA:
979 case CONNMAN_SERVICE_SECURITY_RSN:
980 if (service->passphrase == NULL)
983 case CONNMAN_SERVICE_SECURITY_8021X:
989 connman_dbus_property_changed_basic(service->path,
990 CONNMAN_SERVICE_INTERFACE, "PassphraseRequired",
991 DBUS_TYPE_BOOLEAN, &required);
994 static void login_changed(struct connman_service *service)
996 dbus_bool_t required = service->login_required;
998 if (service->path == NULL)
1001 connman_dbus_property_changed_basic(service->path,
1002 CONNMAN_SERVICE_INTERFACE, "LoginRequired",
1003 DBUS_TYPE_BOOLEAN, &required);
1006 static void append_security(DBusMessageIter *iter, void *user_data)
1008 struct connman_service *service = user_data;
1011 str = security2string(service->security);
1013 dbus_message_iter_append_basic(iter,
1014 DBUS_TYPE_STRING, &str);
1017 if (service->wps == TRUE)
1018 dbus_message_iter_append_basic(iter,
1019 DBUS_TYPE_STRING, &str);
1022 static void append_ethernet(DBusMessageIter *iter, void *user_data)
1024 struct connman_service *service = user_data;
1026 if (service->ipconfig_ipv4 != NULL)
1027 __connman_ipconfig_append_ethernet(service->ipconfig_ipv4,
1029 else if (service->ipconfig_ipv6 != NULL)
1030 __connman_ipconfig_append_ethernet(service->ipconfig_ipv6,
1034 static void append_ipv4(DBusMessageIter *iter, void *user_data)
1036 struct connman_service *service = user_data;
1038 DBG("ipv4 %p state %s", service->ipconfig_ipv4,
1039 state2string(service->state_ipv4));
1041 if (is_connected_state(service, service->state_ipv4) == FALSE)
1044 if (service->ipconfig_ipv4 != NULL)
1045 __connman_ipconfig_append_ipv4(service->ipconfig_ipv4, iter);
1048 static void append_ipv6(DBusMessageIter *iter, void *user_data)
1050 struct connman_service *service = user_data;
1052 DBG("ipv6 %p state %s", service->ipconfig_ipv6,
1053 state2string(service->state_ipv6));
1055 if (is_connected_state(service, service->state_ipv6) == FALSE)
1058 if (service->ipconfig_ipv6 != NULL)
1059 __connman_ipconfig_append_ipv6(service->ipconfig_ipv6, iter,
1060 service->ipconfig_ipv4);
1063 static void append_ipv4config(DBusMessageIter *iter, void *user_data)
1065 struct connman_service *service = user_data;
1067 if (service->ipconfig_ipv4 != NULL)
1068 __connman_ipconfig_append_ipv4config(service->ipconfig_ipv4,
1072 static void append_ipv6config(DBusMessageIter *iter, void *user_data)
1074 struct connman_service *service = user_data;
1076 if (service->ipconfig_ipv6 != NULL)
1077 __connman_ipconfig_append_ipv6config(service->ipconfig_ipv6,
1081 static void append_nameserver(DBusMessageIter *iter, char ***nameservers)
1086 servers = *nameservers;
1088 for (i = 0; servers[i] != NULL; i++) {
1089 dbus_message_iter_append_basic(iter,
1090 DBUS_TYPE_STRING, &servers[i]);
1094 static void append_dns(DBusMessageIter *iter, void *user_data)
1096 struct connman_service *service = user_data;
1098 if (is_connected(service) == FALSE)
1101 if (service->nameservers_config != NULL) {
1102 append_nameserver(iter, &service->nameservers_config);
1104 } else if (service->nameservers != NULL) {
1105 append_nameserver(iter, &service->nameservers);
1110 static void append_dnsconfig(DBusMessageIter *iter, void *user_data)
1112 struct connman_service *service = user_data;
1115 if (service->nameservers_config == NULL)
1118 for (i = 0; service->nameservers_config[i]; i++) {
1119 dbus_message_iter_append_basic(iter,
1121 &service->nameservers_config[i]);
1125 static void append_domain(DBusMessageIter *iter, void *user_data)
1127 struct connman_service *service = user_data;
1129 if (is_connected(service) == FALSE &&
1130 is_connecting(service) == FALSE)
1133 if (service->domainname == NULL)
1136 dbus_message_iter_append_basic(iter,
1137 DBUS_TYPE_STRING, &service->domainname);
1140 static void append_domainconfig(DBusMessageIter *iter, void *user_data)
1142 struct connman_service *service = user_data;
1145 if (service->domains == NULL)
1148 for (i = 0; service->domains[i]; i++)
1149 dbus_message_iter_append_basic(iter,
1150 DBUS_TYPE_STRING, &service->domains[i]);
1153 static void append_proxies(DBusMessageIter *iter, void *user_data)
1155 struct connman_service *service = user_data;
1158 if (service->proxies == NULL)
1161 for (i = 0; service->proxies[i]; i++)
1162 dbus_message_iter_append_basic(iter,
1163 DBUS_TYPE_STRING, &service->proxies[i]);
1166 static void append_excludes(DBusMessageIter *iter, void *user_data)
1168 struct connman_service *service = user_data;
1171 if (service->excludes == NULL)
1174 for (i = 0; service->excludes[i]; i++)
1175 dbus_message_iter_append_basic(iter,
1176 DBUS_TYPE_STRING, &service->excludes[i]);
1179 static void append_proxy(DBusMessageIter *iter, void *user_data)
1181 struct connman_service *service = user_data;
1182 enum connman_service_proxy_method proxy;
1183 const char *pac = NULL;
1184 const char *method = proxymethod2string(
1185 CONNMAN_SERVICE_PROXY_METHOD_DIRECT);
1189 if (is_connected(service) == FALSE)
1192 proxy = connman_service_get_proxy_method(service);
1195 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1197 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1199 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1200 connman_dbus_dict_append_array(iter, "Servers",
1201 DBUS_TYPE_STRING, append_proxies,
1204 connman_dbus_dict_append_array(iter, "Excludes",
1205 DBUS_TYPE_STRING, append_excludes,
1208 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1209 /* Maybe DHCP, or WPAD, has provided an url for a pac file */
1210 if (service->ipconfig_ipv4 != NULL)
1211 pac = __connman_ipconfig_get_proxy_autoconfig(
1212 service->ipconfig_ipv4);
1213 else if (service->ipconfig_ipv6 != NULL)
1214 pac = __connman_ipconfig_get_proxy_autoconfig(
1215 service->ipconfig_ipv6);
1217 if (service->pac == NULL && pac == NULL)
1220 if (service->pac != NULL)
1223 connman_dbus_dict_append_basic(iter, "URL",
1224 DBUS_TYPE_STRING, &pac);
1228 method = proxymethod2string(proxy);
1231 connman_dbus_dict_append_basic(iter, "Method",
1232 DBUS_TYPE_STRING, &method);
1235 static void append_proxyconfig(DBusMessageIter *iter, void *user_data)
1237 struct connman_service *service = user_data;
1240 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
1243 switch (service->proxy_config) {
1244 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1246 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1248 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1249 if (service->proxies != NULL)
1250 connman_dbus_dict_append_array(iter, "Servers",
1252 append_proxies, service);
1254 if (service->excludes != NULL)
1255 connman_dbus_dict_append_array(iter, "Excludes",
1257 append_excludes, service);
1259 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1260 if (service->pac != NULL)
1261 connman_dbus_dict_append_basic(iter, "URL",
1262 DBUS_TYPE_STRING, &service->pac);
1266 method = proxymethod2string(service->proxy_config);
1268 connman_dbus_dict_append_basic(iter, "Method",
1269 DBUS_TYPE_STRING, &method);
1272 static void append_provider(DBusMessageIter *iter, void *user_data)
1274 struct connman_service *service = user_data;
1276 DBG("%p %p", service, service->provider);
1278 if (is_connected(service) == FALSE)
1281 if (service->provider != NULL)
1282 __connman_provider_append_properties(service->provider, iter);
1286 static void settings_changed(struct connman_service *service,
1287 struct connman_ipconfig *ipconfig)
1289 connman_dbus_property_changed_dict(service->path,
1290 CONNMAN_SERVICE_INTERFACE, "IPv4",
1291 append_ipv4, service);
1293 connman_dbus_property_changed_dict(service->path,
1294 CONNMAN_SERVICE_INTERFACE, "IPv6",
1295 append_ipv6, service);
1297 __connman_notifier_ipconfig_changed(service, ipconfig);
1300 static void ipv4_configuration_changed(struct connman_service *service)
1302 connman_dbus_property_changed_dict(service->path,
1303 CONNMAN_SERVICE_INTERFACE,
1304 "IPv4.Configuration",
1309 static void ipv6_configuration_changed(struct connman_service *service)
1311 connman_dbus_property_changed_dict(service->path,
1312 CONNMAN_SERVICE_INTERFACE,
1313 "IPv6.Configuration",
1318 static void dns_changed(struct connman_service *service)
1320 connman_dbus_property_changed_array(service->path,
1321 CONNMAN_SERVICE_INTERFACE, "Nameservers",
1322 DBUS_TYPE_STRING, append_dns, service);
1325 static void dns_configuration_changed(struct connman_service *service)
1327 connman_dbus_property_changed_array(service->path,
1328 CONNMAN_SERVICE_INTERFACE,
1329 "Nameservers.Configuration",
1330 DBUS_TYPE_STRING, append_dnsconfig, service);
1332 dns_changed(service);
1335 static void domain_changed(struct connman_service *service)
1337 connman_dbus_property_changed_array(service->path,
1338 CONNMAN_SERVICE_INTERFACE, "Domains",
1339 DBUS_TYPE_STRING, append_domain, service);
1342 static void domain_configuration_changed(struct connman_service *service)
1344 connman_dbus_property_changed_array(service->path,
1345 CONNMAN_SERVICE_INTERFACE,
1346 "Domains.Configuration",
1347 DBUS_TYPE_STRING, append_domainconfig, service);
1350 static void proxy_changed(struct connman_service *service)
1352 connman_dbus_property_changed_dict(service->path,
1353 CONNMAN_SERVICE_INTERFACE, "Proxy",
1354 append_proxy, service);
1357 static void proxy_configuration_changed(struct connman_service *service)
1359 connman_dbus_property_changed_dict(service->path,
1360 CONNMAN_SERVICE_INTERFACE, "Proxy.Configuration",
1361 append_proxyconfig, service);
1363 proxy_changed(service);
1366 static void link_changed(struct connman_service *service)
1368 connman_dbus_property_changed_dict(service->path,
1369 CONNMAN_SERVICE_INTERFACE, "Ethernet",
1370 append_ethernet, service);
1373 static void stats_append_counters(DBusMessageIter *dict,
1374 struct connman_stats_data *stats,
1375 struct connman_stats_data *counters,
1376 connman_bool_t append_all)
1378 if (counters->rx_packets != stats->rx_packets || append_all) {
1379 counters->rx_packets = stats->rx_packets;
1380 connman_dbus_dict_append_basic(dict, "RX.Packets",
1381 DBUS_TYPE_UINT32, &stats->rx_packets);
1384 if (counters->tx_packets != stats->tx_packets || append_all) {
1385 counters->tx_packets = stats->tx_packets;
1386 connman_dbus_dict_append_basic(dict, "TX.Packets",
1387 DBUS_TYPE_UINT32, &stats->tx_packets);
1390 if (counters->rx_bytes != stats->rx_bytes || append_all) {
1391 counters->rx_bytes = stats->rx_bytes;
1392 connman_dbus_dict_append_basic(dict, "RX.Bytes",
1393 DBUS_TYPE_UINT32, &stats->rx_bytes);
1396 if (counters->tx_bytes != stats->tx_bytes || append_all) {
1397 counters->tx_bytes = stats->tx_bytes;
1398 connman_dbus_dict_append_basic(dict, "TX.Bytes",
1399 DBUS_TYPE_UINT32, &stats->tx_bytes);
1402 if (counters->rx_errors != stats->rx_errors || append_all) {
1403 counters->rx_errors = stats->rx_errors;
1404 connman_dbus_dict_append_basic(dict, "RX.Errors",
1405 DBUS_TYPE_UINT32, &stats->rx_errors);
1408 if (counters->tx_errors != stats->tx_errors || append_all) {
1409 counters->tx_errors = stats->tx_errors;
1410 connman_dbus_dict_append_basic(dict, "TX.Errors",
1411 DBUS_TYPE_UINT32, &stats->tx_errors);
1414 if (counters->rx_dropped != stats->rx_dropped || append_all) {
1415 counters->rx_dropped = stats->rx_dropped;
1416 connman_dbus_dict_append_basic(dict, "RX.Dropped",
1417 DBUS_TYPE_UINT32, &stats->rx_dropped);
1420 if (counters->tx_dropped != stats->tx_dropped || append_all) {
1421 counters->tx_dropped = stats->tx_dropped;
1422 connman_dbus_dict_append_basic(dict, "TX.Dropped",
1423 DBUS_TYPE_UINT32, &stats->tx_dropped);
1426 if (counters->time != stats->time || append_all) {
1427 counters->time = stats->time;
1428 connman_dbus_dict_append_basic(dict, "Time",
1429 DBUS_TYPE_UINT32, &stats->time);
1433 static void stats_append(struct connman_service *service,
1434 const char *counter,
1435 struct connman_stats_counter *counters,
1436 connman_bool_t append_all)
1438 DBusMessageIter array, dict;
1441 DBG("service %p counter %s", service, counter);
1443 msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1447 dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH,
1448 &service->path, DBUS_TYPE_INVALID);
1450 dbus_message_iter_init_append(msg, &array);
1453 connman_dbus_dict_open(&array, &dict);
1455 stats_append_counters(&dict, &service->stats.data,
1456 &counters->stats.data, append_all);
1458 connman_dbus_dict_close(&array, &dict);
1460 /* roaming counter */
1461 connman_dbus_dict_open(&array, &dict);
1463 stats_append_counters(&dict, &service->stats_roaming.data,
1464 &counters->stats_roaming.data, append_all);
1466 connman_dbus_dict_close(&array, &dict);
1468 __connman_counter_send_usage(counter, msg);
1471 static void stats_update(struct connman_service *service,
1472 unsigned int rx_packets, unsigned int tx_packets,
1473 unsigned int rx_bytes, unsigned int tx_bytes,
1474 unsigned int rx_errors, unsigned int tx_errors,
1475 unsigned int rx_dropped, unsigned int tx_dropped)
1477 struct connman_stats *stats = stats_get(service);
1478 struct connman_stats_data *data_last = &stats->data_last;
1479 struct connman_stats_data *data = &stats->data;
1480 unsigned int seconds;
1482 DBG("service %p", service);
1484 if (stats->valid == TRUE) {
1486 rx_packets - data_last->rx_packets;
1488 tx_packets - data_last->tx_packets;
1490 rx_bytes - data_last->rx_bytes;
1492 tx_bytes - data_last->tx_bytes;
1494 rx_errors - data_last->rx_errors;
1496 tx_errors - data_last->tx_errors;
1498 rx_dropped - data_last->rx_dropped;
1500 tx_dropped - data_last->tx_dropped;
1502 stats->valid = TRUE;
1505 data_last->rx_packets = rx_packets;
1506 data_last->tx_packets = tx_packets;
1507 data_last->rx_bytes = rx_bytes;
1508 data_last->tx_bytes = tx_bytes;
1509 data_last->rx_errors = rx_errors;
1510 data_last->tx_errors = tx_errors;
1511 data_last->rx_dropped = rx_dropped;
1512 data_last->tx_dropped = tx_dropped;
1514 seconds = g_timer_elapsed(stats->timer, NULL);
1515 stats->data.time = stats->data_last.time + seconds;
1518 static char *wifi_build_group_name(const unsigned char *ssid,
1519 unsigned int ssid_len,
1521 const char *security)
1526 /* the last 3 is for the 2 '_' and '\0' */
1527 str = g_string_sized_new((ssid_len * 2) + strlen(mode)
1528 + strlen(security) + 3);
1532 for (i = 0; i < ssid_len; i++)
1533 g_string_append_printf(str, "%02x", ssid[i]);
1535 g_string_append_printf(str, "_%s_%s", mode, security);
1537 return g_string_free(str, FALSE);
1540 void __connman_service_notify(struct connman_service *service,
1541 unsigned int rx_packets, unsigned int tx_packets,
1542 unsigned int rx_bytes, unsigned int tx_bytes,
1543 unsigned int rx_errors, unsigned int tx_errors,
1544 unsigned int rx_dropped, unsigned int tx_dropped)
1546 GHashTableIter iter;
1547 gpointer key, value;
1548 const char *counter;
1549 struct connman_stats_counter *counters;
1550 struct connman_stats_data *data;
1553 if (service == NULL)
1556 if (is_connected(service) == FALSE)
1559 stats_update(service,
1560 rx_packets, tx_packets,
1562 rx_errors, tx_errors,
1563 rx_dropped, tx_dropped);
1565 data = &stats_get(service)->data;
1566 err = __connman_stats_update(service, service->roaming, data);
1568 connman_error("Failed to store statistics for %s",
1569 service->identifier);
1571 g_hash_table_iter_init(&iter, service->counter_table);
1572 while (g_hash_table_iter_next(&iter, &key, &value)) {
1576 stats_append(service, counter, counters, counters->append_all);
1577 counters->append_all = FALSE;
1581 int __connman_service_counter_register(const char *counter)
1583 struct connman_service *service;
1584 GSequenceIter *iter;
1585 struct connman_stats_counter *counters;
1587 DBG("counter %s", counter);
1589 counter_list = g_slist_append(counter_list, (gpointer)counter);
1591 iter = g_sequence_get_begin_iter(service_list);
1593 while (g_sequence_iter_is_end(iter) == FALSE) {
1594 service = g_sequence_get(iter);
1596 counters = g_try_new0(struct connman_stats_counter, 1);
1597 if (counters == NULL)
1600 counters->append_all = TRUE;
1602 g_hash_table_replace(service->counter_table, (gpointer)counter,
1605 iter = g_sequence_iter_next(iter);
1611 void __connman_service_counter_unregister(const char *counter)
1613 struct connman_service *service;
1614 GSequenceIter *iter;
1616 DBG("counter %s", counter);
1618 iter = g_sequence_get_begin_iter(service_list);
1620 while (g_sequence_iter_is_end(iter) == FALSE) {
1621 service = g_sequence_get(iter);
1623 g_hash_table_remove(service->counter_table, counter);
1625 iter = g_sequence_iter_next(iter);
1628 counter_list = g_slist_remove(counter_list, counter);
1631 GSequence *__connman_service_get_list(struct connman_session *session,
1632 service_match_cb service_match,
1633 create_service_entry_cb create_service_entry,
1634 GDestroyNotify destroy_service_entry)
1637 GSequenceIter *iter;
1638 struct connman_service *service;
1639 struct service_entry *entry;
1641 list = g_sequence_new(destroy_service_entry);
1645 iter = g_sequence_get_begin_iter(service_list);
1647 while (g_sequence_iter_is_end(iter) == FALSE) {
1648 service = g_sequence_get(iter);
1650 if (service_match(session, service) == TRUE) {
1651 entry = create_service_entry(service, service->name,
1656 g_sequence_append(list, entry);
1659 iter = g_sequence_iter_next(iter);
1665 void __connman_service_session_inc(struct connman_service *service)
1667 DBG("service %p ref count %d", service,
1668 g_atomic_int_get(&service->session_usage_count) + 1);
1670 g_atomic_int_inc(&service->session_usage_count);
1673 connman_bool_t __connman_service_session_dec(struct connman_service *service)
1675 DBG("service %p ref count %d", service,
1676 g_atomic_int_get(&service->session_usage_count) - 1);
1678 if (g_atomic_int_dec_and_test(&service->session_usage_count) == FALSE)
1684 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
1685 struct connman_service *service)
1687 dbus_bool_t required;
1690 str = __connman_service_type2string(service->type);
1692 connman_dbus_dict_append_basic(dict, "Type",
1693 DBUS_TYPE_STRING, &str);
1695 connman_dbus_dict_append_array(dict, "Security",
1696 DBUS_TYPE_STRING, append_security, service);
1698 str = state2string(service->state);
1700 connman_dbus_dict_append_basic(dict, "State",
1701 DBUS_TYPE_STRING, &str);
1703 str = error2string(service->error);
1705 connman_dbus_dict_append_basic(dict, "Error",
1706 DBUS_TYPE_STRING, &str);
1708 if (service->strength > 0)
1709 connman_dbus_dict_append_basic(dict, "Strength",
1710 DBUS_TYPE_BYTE, &service->strength);
1712 connman_dbus_dict_append_basic(dict, "Favorite",
1713 DBUS_TYPE_BOOLEAN, &service->favorite);
1715 connman_dbus_dict_append_basic(dict, "Immutable",
1716 DBUS_TYPE_BOOLEAN, &service->immutable);
1718 if (service->favorite == TRUE)
1719 connman_dbus_dict_append_basic(dict, "AutoConnect",
1720 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1722 connman_dbus_dict_append_basic(dict, "AutoConnect",
1723 DBUS_TYPE_BOOLEAN, &service->favorite);
1725 if (service->name != NULL)
1726 connman_dbus_dict_append_basic(dict, "Name",
1727 DBUS_TYPE_STRING, &service->name);
1729 connman_dbus_dict_append_basic(dict, "LoginRequired",
1730 DBUS_TYPE_BOOLEAN, &service->login_required);
1732 switch (service->type) {
1733 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1734 case CONNMAN_SERVICE_TYPE_SYSTEM:
1735 case CONNMAN_SERVICE_TYPE_GPS:
1736 case CONNMAN_SERVICE_TYPE_VPN:
1737 case CONNMAN_SERVICE_TYPE_GADGET:
1739 case CONNMAN_SERVICE_TYPE_CELLULAR:
1740 connman_dbus_dict_append_basic(dict, "Roaming",
1741 DBUS_TYPE_BOOLEAN, &service->roaming);
1744 connman_dbus_dict_append_basic(dict, "SetupRequired",
1745 DBUS_TYPE_BOOLEAN, &required);
1746 connman_dbus_dict_append_dict(dict, "Ethernet",
1747 append_ethernet, service);
1749 case CONNMAN_SERVICE_TYPE_WIFI:
1750 if (service->passphrase != NULL && limited == FALSE)
1751 connman_dbus_dict_append_basic(dict, "Passphrase",
1752 DBUS_TYPE_STRING, &service->passphrase);
1756 switch (service->security) {
1757 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1758 case CONNMAN_SERVICE_SECURITY_NONE:
1760 case CONNMAN_SERVICE_SECURITY_WEP:
1761 case CONNMAN_SERVICE_SECURITY_PSK:
1762 case CONNMAN_SERVICE_SECURITY_WPA:
1763 case CONNMAN_SERVICE_SECURITY_RSN:
1764 if (service->passphrase == NULL)
1767 case CONNMAN_SERVICE_SECURITY_8021X:
1771 connman_dbus_dict_append_basic(dict, "PassphraseRequired",
1772 DBUS_TYPE_BOOLEAN, &required);
1775 case CONNMAN_SERVICE_TYPE_ETHERNET:
1776 case CONNMAN_SERVICE_TYPE_WIMAX:
1777 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1778 connman_dbus_dict_append_dict(dict, "Ethernet",
1779 append_ethernet, service);
1783 connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
1785 connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
1786 append_ipv4config, service);
1788 connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
1790 connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
1791 append_ipv6config, service);
1793 connman_dbus_dict_append_array(dict, "Nameservers",
1794 DBUS_TYPE_STRING, append_dns, service);
1796 connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
1797 DBUS_TYPE_STRING, append_dnsconfig, service);
1799 connman_dbus_dict_append_array(dict, "Domains",
1800 DBUS_TYPE_STRING, append_domain, service);
1802 connman_dbus_dict_append_array(dict, "Domains.Configuration",
1803 DBUS_TYPE_STRING, append_domainconfig, service);
1805 connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
1807 connman_dbus_dict_append_dict(dict, "Proxy.Configuration",
1808 append_proxyconfig, service);
1810 connman_dbus_dict_append_dict(dict, "Provider",
1811 append_provider, service);
1814 static void append_struct(gpointer value, gpointer user_data)
1816 struct connman_service *service = value;
1817 DBusMessageIter *iter = user_data;
1818 DBusMessageIter entry, dict;
1820 if (service->path == NULL || service->hidden == TRUE)
1823 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
1825 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1828 connman_dbus_dict_open(&entry, &dict);
1829 append_properties(&dict, TRUE, service);
1830 connman_dbus_dict_close(&entry, &dict);
1832 dbus_message_iter_close_container(iter, &entry);
1835 void __connman_service_list_struct(DBusMessageIter *iter)
1837 g_sequence_foreach(service_list, append_struct, iter);
1840 int __connman_service_get_index(struct connman_service *service)
1842 if (service == NULL)
1845 if (service->network != NULL)
1846 return connman_network_get_index(service->network);
1847 else if (service->provider != NULL)
1848 return connman_provider_get_index(service->provider);
1853 void __connman_service_set_domainname(struct connman_service *service,
1854 const char *domainname)
1856 if (service == NULL)
1859 g_free(service->domainname);
1860 service->domainname = g_strdup(domainname);
1862 domain_changed(service);
1865 const char *connman_service_get_domainname(struct connman_service *service)
1867 if (service == NULL)
1870 if (service->domains != NULL)
1871 return service->domains[0];
1873 return service->domainname;
1876 char **connman_service_get_nameservers(struct connman_service *service)
1878 if (service == NULL)
1881 if (service->nameservers_config != NULL)
1882 return service->nameservers_config;
1883 else if (service->nameservers != NULL)
1884 return service->nameservers;
1889 void connman_service_set_proxy_method(struct connman_service *service,
1890 enum connman_service_proxy_method method)
1892 if (service == NULL)
1895 service->proxy = method;
1897 proxy_changed(service);
1899 if (method != CONNMAN_SERVICE_PROXY_METHOD_AUTO)
1900 __connman_notifier_proxy_changed(service);
1903 enum connman_service_proxy_method connman_service_get_proxy_method(
1904 struct connman_service *service)
1906 if (service == NULL)
1907 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
1909 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN) {
1910 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
1911 service->pac == NULL)
1912 return service->proxy;
1914 return service->proxy_config;
1917 return service->proxy;
1920 char **connman_service_get_proxy_servers(struct connman_service *service)
1922 return g_strdupv(service->proxies);
1925 char **connman_service_get_proxy_excludes(struct connman_service *service)
1927 return g_strdupv(service->excludes);
1930 const char *connman_service_get_proxy_url(struct connman_service *service)
1932 if (service == NULL)
1935 return service->pac;
1938 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
1941 if (service == NULL)
1944 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_AUTO;
1946 if (service->ipconfig_ipv4) {
1947 if (__connman_ipconfig_set_proxy_autoconfig(
1948 service->ipconfig_ipv4, url) < 0)
1950 } else if (service->ipconfig_ipv6) {
1951 if (__connman_ipconfig_set_proxy_autoconfig(
1952 service->ipconfig_ipv6, url) < 0)
1957 proxy_changed(service);
1959 __connman_notifier_proxy_changed(service);
1962 const char *connman_service_get_proxy_autoconfig(struct connman_service *service)
1964 if (service == NULL)
1967 if (service->ipconfig_ipv4)
1968 return __connman_ipconfig_get_proxy_autoconfig(
1969 service->ipconfig_ipv4);
1970 else if (service->ipconfig_ipv6)
1971 return __connman_ipconfig_get_proxy_autoconfig(
1972 service->ipconfig_ipv6);
1976 static void update_timeservers(struct connman_service *service)
1980 if (service->timeservers == NULL)
1983 switch (service->state) {
1984 case CONNMAN_SERVICE_STATE_UNKNOWN:
1985 case CONNMAN_SERVICE_STATE_IDLE:
1986 case CONNMAN_SERVICE_STATE_ASSOCIATION:
1987 case CONNMAN_SERVICE_STATE_CONFIGURATION:
1989 case CONNMAN_SERVICE_STATE_FAILURE:
1990 case CONNMAN_SERVICE_STATE_DISCONNECT:
1991 for (i = 0; service->timeservers[i] != NULL; i++)
1992 connman_timeserver_remove(service->timeservers[i]);
1994 case CONNMAN_SERVICE_STATE_READY:
1995 case CONNMAN_SERVICE_STATE_ONLINE:
1999 for (i = 0; service->timeservers[i] != NULL; i++)
2000 connman_timeserver_append(service->timeservers[i]);
2003 int __connman_service_timeserver_append(struct connman_service *service,
2004 const char *timeserver)
2008 DBG("service %p timeserver %s", service, timeserver);
2010 if (timeserver == NULL)
2013 if (service->timeservers != NULL) {
2016 for (i = 0; service->timeservers[i] != NULL; i++)
2017 if (g_strcmp0(service->timeservers[i], timeserver) == 0)
2020 len = g_strv_length(service->timeservers);
2021 service->timeservers = g_try_renew(char *, service->timeservers,
2025 service->timeservers = g_try_new0(char *, len + 2);
2028 if (service->timeservers == NULL)
2031 service->timeservers[len] = g_strdup(timeserver);
2032 service->timeservers[len + 1] = NULL;
2034 update_timeservers(service);
2039 int __connman_service_timeserver_remove(struct connman_service *service,
2040 const char *timeserver)
2045 DBG("service %p timeserver %s", service, timeserver);
2047 if (timeserver == NULL)
2050 if (service->timeservers == NULL)
2053 len = g_strv_length(service->timeservers);
2055 if (g_strcmp0(service->timeservers[0], timeserver) != 0)
2058 g_strfreev(service->timeservers);
2059 service->timeservers = NULL;
2064 servers = g_try_new0(char *, len - 1);
2065 if (servers == NULL)
2068 for (i = 0, j = 0; i < len; i++) {
2069 if (g_strcmp0(service->timeservers[i], timeserver) != 0) {
2070 servers[j] = g_strdup(service->timeservers[i]);
2074 servers[len - 2] = NULL;
2076 g_strfreev(service->timeservers);
2077 service->timeservers = servers;
2079 update_timeservers(service);
2084 void __connman_service_set_pac(struct connman_service *service,
2087 g_free(service->pac);
2088 service->pac = g_strdup(pac);
2090 proxy_changed(service);
2093 void __connman_service_set_identity(struct connman_service *service,
2094 const char *identity)
2096 if (service->immutable)
2099 g_free(service->identity);
2100 service->identity = g_strdup(identity);
2102 if (service->network != NULL)
2103 connman_network_set_string(service->network,
2108 void __connman_service_set_agent_identity(struct connman_service *service,
2109 const char *agent_identity)
2111 g_free(service->agent_identity);
2112 service->agent_identity = g_strdup(agent_identity);
2114 if (service->network != NULL)
2115 connman_network_set_string(service->network,
2116 "WiFi.AgentIdentity",
2117 service->agent_identity);
2120 void __connman_service_set_passphrase(struct connman_service *service,
2121 const char* passphrase)
2123 if (service->immutable == TRUE)
2126 g_free(service->passphrase);
2127 service->passphrase = g_strdup(passphrase);
2129 passphrase_changed(service);
2131 if (service->network != NULL)
2132 connman_network_set_string(service->network,
2134 service->passphrase);
2136 __connman_storage_save_service(service);
2139 void __connman_service_set_agent_passphrase(struct connman_service *service,
2140 const char *agent_passphrase)
2142 g_free(service->agent_passphrase);
2143 service->agent_passphrase = g_strdup(agent_passphrase);
2145 if (service->network != NULL)
2146 connman_network_set_string(service->network,
2147 "WiFi.AgentPassphrase",
2148 service->agent_passphrase);
2151 static DBusMessage *get_properties(DBusConnection *conn,
2152 DBusMessage *msg, void *user_data)
2154 struct connman_service *service = user_data;
2156 DBusMessageIter array, dict;
2158 DBG("service %p", service);
2160 reply = dbus_message_new_method_return(msg);
2164 dbus_message_iter_init_append(reply, &array);
2166 connman_dbus_dict_open(&array, &dict);
2167 append_properties(&dict, FALSE, service);
2168 connman_dbus_dict_close(&array, &dict);
2173 static int update_proxy_configuration(struct connman_service *service,
2174 DBusMessageIter *array)
2176 DBusMessageIter dict;
2177 enum connman_service_proxy_method method;
2178 GString *servers_str = NULL;
2179 GString *excludes_str = NULL;
2180 const char *url = NULL;
2182 method = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2184 dbus_message_iter_recurse(array, &dict);
2186 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2187 DBusMessageIter entry, variant;
2191 dbus_message_iter_recurse(&dict, &entry);
2193 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2196 dbus_message_iter_get_basic(&entry, &key);
2197 dbus_message_iter_next(&entry);
2199 if (dbus_message_iter_get_arg_type(&entry) !=
2203 dbus_message_iter_recurse(&entry, &variant);
2204 type = dbus_message_iter_get_arg_type(&variant);
2206 if (g_str_equal(key, "Method") == TRUE) {
2209 if (type != DBUS_TYPE_STRING)
2212 dbus_message_iter_get_basic(&variant, &val);
2213 method = string2proxymethod(val);
2214 } else if (g_str_equal(key, "URL") == TRUE) {
2215 if (type != DBUS_TYPE_STRING)
2218 dbus_message_iter_get_basic(&variant, &url);
2219 } else if (g_str_equal(key, "Servers") == TRUE) {
2220 DBusMessageIter str_array;
2222 if (type != DBUS_TYPE_ARRAY)
2225 servers_str = g_string_new(NULL);
2226 if (servers_str == NULL)
2229 dbus_message_iter_recurse(&variant, &str_array);
2231 while (dbus_message_iter_get_arg_type(&str_array) ==
2235 dbus_message_iter_get_basic(&str_array, &val);
2237 if (servers_str->len > 0)
2238 g_string_append_printf(servers_str,
2241 g_string_append(servers_str, val);
2243 dbus_message_iter_next(&str_array);
2245 } else if (g_str_equal(key, "Excludes") == TRUE) {
2246 DBusMessageIter str_array;
2248 if (type != DBUS_TYPE_ARRAY)
2251 excludes_str = g_string_new(NULL);
2252 if (excludes_str == NULL)
2255 dbus_message_iter_recurse(&variant, &str_array);
2257 while (dbus_message_iter_get_arg_type(&str_array) ==
2261 dbus_message_iter_get_basic(&str_array, &val);
2263 if (excludes_str->len > 0)
2264 g_string_append_printf(excludes_str,
2267 g_string_append(excludes_str, val);
2269 dbus_message_iter_next(&str_array);
2273 dbus_message_iter_next(&dict);
2277 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
2279 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
2280 if (servers_str == NULL && service->proxies == NULL)
2283 if (servers_str != NULL) {
2284 g_strfreev(service->proxies);
2286 if (servers_str->len > 0)
2287 service->proxies = g_strsplit_set(
2288 servers_str->str, " ", 0);
2290 service->proxies = NULL;
2293 if (excludes_str != NULL) {
2294 g_strfreev(service->excludes);
2296 if (excludes_str->len > 0)
2297 service->excludes = g_strsplit_set(
2298 excludes_str->str, " ", 0);
2300 service->excludes = NULL;
2303 if (service->proxies == NULL)
2304 method = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
2307 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
2308 g_free(service->pac);
2310 if (url != NULL && strlen(url) > 0)
2311 service->pac = g_strdup(url);
2313 service->pac = NULL;
2315 /* if we are connected:
2316 - if service->pac == NULL
2317 - if __connman_ipconfig_get_proxy_autoconfig(
2318 service->ipconfig) == NULL
2319 --> We should start WPAD */
2322 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
2326 if (servers_str != NULL)
2327 g_string_free(servers_str, TRUE);
2329 if (excludes_str != NULL)
2330 g_string_free(excludes_str, TRUE);
2332 service->proxy_config = method;
2337 if (servers_str != NULL)
2338 g_string_free(servers_str, TRUE);
2340 if (excludes_str != NULL)
2341 g_string_free(excludes_str, TRUE);
2346 static int set_ipconfig(struct connman_service *service,
2347 struct connman_ipconfig *ipconfig,
2348 DBusMessageIter *array,
2349 enum connman_service_state state,
2350 enum connman_service_state *new_state)
2352 enum connman_ipconfig_method old_method;
2353 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2354 enum connman_ipconfig_type type;
2357 old_method = __connman_ipconfig_get_method(ipconfig);
2359 if (is_connecting_state(service, state) ||
2360 is_connected_state(service, state))
2361 __connman_network_clear_ipconfig(service->network, ipconfig);
2363 err = __connman_ipconfig_set_config(ipconfig, array);
2364 method = __connman_ipconfig_get_method(ipconfig);
2365 type = __connman_ipconfig_get_config_type(ipconfig);
2367 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
2368 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2369 method == CONNMAN_IPCONFIG_METHOD_DHCP) {
2370 *new_state = service->state_ipv4 =
2371 CONNMAN_SERVICE_STATE_CONFIGURATION;
2372 __connman_ipconfig_enable(ipconfig);
2375 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2376 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2377 method == CONNMAN_IPCONFIG_METHOD_AUTO) {
2378 *new_state = service->state_ipv6;
2379 __connman_ipconfig_enable(ipconfig);
2383 DBG("err %d ipconfig %p type %d method %d state %s", err, ipconfig,
2384 type, method, state2string(*new_state));
2389 static DBusMessage *set_property(DBusConnection *conn,
2390 DBusMessage *msg, void *user_data)
2392 struct connman_service *service = user_data;
2393 DBusMessageIter iter, value;
2397 DBG("service %p", service);
2399 if (dbus_message_iter_init(msg, &iter) == FALSE)
2400 return __connman_error_invalid_arguments(msg);
2402 dbus_message_iter_get_basic(&iter, &name);
2403 dbus_message_iter_next(&iter);
2404 dbus_message_iter_recurse(&iter, &value);
2406 type = dbus_message_iter_get_arg_type(&value);
2408 if (g_str_equal(name, "AutoConnect") == TRUE) {
2409 connman_bool_t autoconnect;
2411 if (type != DBUS_TYPE_BOOLEAN)
2412 return __connman_error_invalid_arguments(msg);
2414 if (service->favorite == FALSE)
2415 return __connman_error_invalid_service(msg);
2417 dbus_message_iter_get_basic(&value, &autoconnect);
2419 if (service->autoconnect == autoconnect)
2420 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2422 service->autoconnect = autoconnect;
2424 autoconnect_changed(service);
2426 __connman_storage_save_service(service);
2427 } else if (g_str_equal(name, "Passphrase") == TRUE) {
2428 const char *passphrase;
2430 if (type != DBUS_TYPE_STRING)
2431 return __connman_error_invalid_arguments(msg);
2433 if (service->immutable == TRUE)
2434 return __connman_error_not_supported(msg);
2436 dbus_message_iter_get_basic(&value, &passphrase);
2438 __connman_service_set_passphrase(service, passphrase);
2439 } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
2440 DBusMessageIter entry;
2445 if (type != DBUS_TYPE_ARRAY)
2446 return __connman_error_invalid_arguments(msg);
2448 str = g_string_new(NULL);
2450 return __connman_error_invalid_arguments(msg);
2452 index = connman_network_get_index(service->network);
2453 gw = __connman_ipconfig_get_gateway_from_index(index);
2455 if (gw && strlen(gw))
2456 __connman_service_nameserver_del_routes(service);
2458 dbus_message_iter_recurse(&value, &entry);
2460 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2462 dbus_message_iter_get_basic(&entry, &val);
2463 dbus_message_iter_next(&entry);
2465 g_string_append_printf(str, " %s", val);
2467 g_string_append(str, val);
2470 g_strfreev(service->nameservers_config);
2473 service->nameservers_config =
2474 g_strsplit_set(str->str, " ", 0);
2476 service->nameservers_config = NULL;
2479 g_string_free(str, TRUE);
2481 if (gw && strlen(gw))
2482 __connman_service_nameserver_add_routes(service, gw);
2484 update_nameservers(service);
2485 dns_configuration_changed(service);
2487 __connman_storage_save_service(service);
2488 } else if (g_str_equal(name, "Domains.Configuration") == TRUE) {
2489 DBusMessageIter entry;
2492 if (type != DBUS_TYPE_ARRAY)
2493 return __connman_error_invalid_arguments(msg);
2495 str = g_string_new(NULL);
2497 return __connman_error_invalid_arguments(msg);
2499 dbus_message_iter_recurse(&value, &entry);
2501 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2503 dbus_message_iter_get_basic(&entry, &val);
2504 dbus_message_iter_next(&entry);
2506 g_string_append_printf(str, " %s", val);
2508 g_string_append(str, val);
2511 g_strfreev(service->domains);
2514 service->domains = g_strsplit_set(str->str, " ", 0);
2516 service->domains = NULL;
2518 g_string_free(str, TRUE);
2520 update_nameservers(service);
2521 domain_configuration_changed(service);
2523 __connman_storage_save_service(service);
2524 } else if (g_str_equal(name, "Proxy.Configuration") == TRUE) {
2527 if (type != DBUS_TYPE_ARRAY)
2528 return __connman_error_invalid_arguments(msg);
2530 err = update_proxy_configuration(service, &value);
2533 return __connman_error_failed(msg, -err);
2535 proxy_configuration_changed(service);
2537 __connman_notifier_proxy_changed(service);
2539 __connman_storage_save_service(service);
2540 } else if (g_str_equal(name, "IPv4.Configuration") == TRUE ||
2541 g_str_equal(name, "IPv6.Configuration")) {
2543 struct connman_ipconfig *ipv4 = NULL, *ipv6 = NULL;
2544 enum connman_service_state state =
2545 CONNMAN_SERVICE_STATE_UNKNOWN;
2550 if (service->ipconfig_ipv4 == NULL &&
2551 service->ipconfig_ipv6 == NULL)
2552 return __connman_error_invalid_property(msg);
2554 if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
2555 ipv4 = service->ipconfig_ipv4;
2556 err = set_ipconfig(service, ipv4, &value,
2557 service->state_ipv4, &state);
2559 } else if (g_str_equal(name, "IPv6.Configuration") == TRUE) {
2560 ipv6 = service->ipconfig_ipv6;
2561 err = set_ipconfig(service, ipv6, &value,
2562 service->state_ipv6, &state);
2566 if (is_connected_state(service, state) ||
2567 is_connecting_state(service, state))
2568 __connman_network_set_ipconfig(service->network,
2570 return __connman_error_failed(msg, -err);
2574 ipv4_configuration_changed(service);
2576 ipv6_configuration_changed(service);
2578 if (is_connecting(service) || is_connected(service))
2579 __connman_network_set_ipconfig(service->network,
2582 __connman_storage_save_service(service);
2584 return __connman_error_invalid_property(msg);
2586 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2589 static void set_idle(struct connman_service *service)
2591 service->state = service->state_ipv4 = service->state_ipv6 =
2592 CONNMAN_SERVICE_STATE_IDLE;
2593 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
2594 state_changed(service);
2597 static DBusMessage *clear_property(DBusConnection *conn,
2598 DBusMessage *msg, void *user_data)
2600 struct connman_service *service = user_data;
2603 DBG("service %p", service);
2605 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
2608 if (g_str_equal(name, "Error") == TRUE) {
2611 g_get_current_time(&service->modified);
2612 __connman_storage_save_service(service);
2613 } else if (g_str_equal(name, "Passphrase") == TRUE) {
2614 if (service->immutable == TRUE)
2615 return __connman_error_not_supported(msg);
2617 g_free(service->passphrase);
2618 service->passphrase = NULL;
2620 passphrase_changed(service);
2622 __connman_storage_save_service(service);
2624 return __connman_error_invalid_property(msg);
2626 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2629 static connman_bool_t is_ignore(struct connman_service *service)
2631 if (service->autoconnect == FALSE)
2634 if (service->roaming == TRUE)
2637 if (service->ignore == TRUE)
2640 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
2646 void __connman_service_auto_connect(void)
2648 struct connman_service *service = NULL;
2649 GSequenceIter *iter;
2653 if (__connman_session_mode() == TRUE) {
2654 DBG("Session mode enabled: auto connect disabled");
2658 iter = g_sequence_get_begin_iter(service_list);
2660 while (g_sequence_iter_is_end(iter) == FALSE) {
2661 service = g_sequence_get(iter);
2663 if (service->pending != NULL)
2666 if (is_connecting(service) == TRUE)
2669 if (service->favorite == FALSE)
2672 if (is_connected(service) == TRUE)
2675 if (is_ignore(service) == FALSE && service->state ==
2676 CONNMAN_SERVICE_STATE_IDLE)
2681 iter = g_sequence_iter_next(iter);
2684 if (service != NULL) {
2685 service->userconnect = FALSE;
2686 __connman_service_connect(service);
2690 static void remove_timeout(struct connman_service *service)
2692 if (service->timeout > 0) {
2693 g_source_remove(service->timeout);
2694 service->timeout = 0;
2698 static void reply_pending(struct connman_service *service, int error)
2700 remove_timeout(service);
2702 if (service->pending != NULL) {
2706 reply = __connman_error_failed(service->pending,
2709 g_dbus_send_message(connection, reply);
2713 sender = dbus_message_get_interface(service->pending);
2715 DBG("sender %s", sender);
2717 if (g_strcmp0(sender, CONNMAN_MANAGER_INTERFACE) == 0)
2718 g_dbus_send_reply(connection, service->pending,
2719 DBUS_TYPE_OBJECT_PATH, &service->path,
2722 g_dbus_send_reply(connection, service->pending,
2726 dbus_message_unref(service->pending);
2727 service->pending = NULL;
2731 static gboolean connect_timeout(gpointer user_data)
2733 struct connman_service *service = user_data;
2734 connman_bool_t autoconnect = FALSE;
2736 DBG("service %p", service);
2738 service->timeout = 0;
2740 if (service->network != NULL)
2741 __connman_network_disconnect(service->network);
2743 __connman_ipconfig_disable(service->ipconfig_ipv4);
2744 __connman_ipconfig_disable(service->ipconfig_ipv6);
2746 __connman_stats_service_unregister(service);
2748 if (service->pending != NULL) {
2751 reply = __connman_error_operation_timeout(service->pending);
2753 g_dbus_send_message(connection, reply);
2755 dbus_message_unref(service->pending);
2756 service->pending = NULL;
2760 __connman_service_ipconfig_indicate_state(service,
2761 CONNMAN_SERVICE_STATE_FAILURE,
2762 CONNMAN_IPCONFIG_TYPE_IPV4);
2763 __connman_service_ipconfig_indicate_state(service,
2764 CONNMAN_SERVICE_STATE_FAILURE,
2765 CONNMAN_IPCONFIG_TYPE_IPV6);
2767 if (autoconnect == TRUE && service->userconnect == FALSE)
2768 __connman_service_auto_connect();
2773 static void set_reconnect_state(struct connman_service *service,
2774 connman_bool_t reconnect)
2776 struct connman_device *device;
2778 if (service->network == NULL)
2781 device = connman_network_get_device(service->network);
2785 __connman_device_set_reconnect(device, reconnect);
2788 static connman_bool_t get_reconnect_state(struct connman_service *service)
2790 struct connman_device *device;
2792 if (service->network == NULL)
2795 device = connman_network_get_device(service->network);
2799 return __connman_device_get_reconnect(device);
2802 static void request_input_cb (struct connman_service *service,
2803 const char *identity, const char *passphrase,
2806 DBG ("RequestInput return, %p", service);
2808 if (identity == NULL && passphrase == NULL && service->wps == FALSE)
2811 if (identity != NULL)
2812 __connman_service_set_agent_identity(service, identity);
2814 if (passphrase != NULL) {
2815 switch (service->security) {
2816 case CONNMAN_SERVICE_SECURITY_WEP:
2817 case CONNMAN_SERVICE_SECURITY_PSK:
2818 __connman_service_set_passphrase(service, passphrase);
2820 case CONNMAN_SERVICE_SECURITY_8021X:
2821 __connman_service_set_agent_passphrase(service,
2824 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
2825 case CONNMAN_SERVICE_SECURITY_NONE:
2826 case CONNMAN_SERVICE_SECURITY_WPA:
2827 case CONNMAN_SERVICE_SECURITY_RSN:
2828 DBG("service security '%s' not handled",
2829 security2string(service->security));
2834 __connman_service_connect(service);
2836 /* Never cache agent provided credentials */
2837 __connman_service_set_agent_identity(service, NULL);
2838 __connman_service_set_agent_passphrase(service, NULL);
2841 static DBusMessage *connect_service(DBusConnection *conn,
2842 DBusMessage *msg, void *user_data)
2844 struct connman_service *service = user_data;
2845 GSequenceIter *iter;
2848 DBG("service %p", service);
2850 if (service->pending != NULL)
2851 return __connman_error_in_progress(msg);
2853 iter = g_sequence_get_begin_iter(service_list);
2855 while (g_sequence_iter_is_end(iter) == FALSE) {
2856 struct connman_service *temp = g_sequence_get(iter);
2858 if (service->type == temp->type && is_connecting(temp) == TRUE)
2859 return __connman_error_in_progress(msg);
2861 iter = g_sequence_iter_next(iter);
2864 service->ignore = FALSE;
2866 service->userconnect = TRUE;
2868 service->pending = dbus_message_ref(msg);
2870 set_reconnect_state(service, FALSE);
2872 err = __connman_service_connect(service);
2874 if (service->pending == NULL)
2877 if (err != -EINPROGRESS) {
2878 dbus_message_unref(service->pending);
2879 service->pending = NULL;
2881 return __connman_error_failed(msg, -err);
2887 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2890 static DBusMessage *disconnect_service(DBusConnection *conn,
2891 DBusMessage *msg, void *user_data)
2893 struct connman_service *service = user_data;
2896 DBG("service %p", service);
2898 reply_pending(service, ECONNABORTED);
2900 service->ignore = TRUE;
2902 set_reconnect_state(service, FALSE);
2904 err = __connman_service_disconnect(service);
2906 if (err != -EINPROGRESS)
2907 return __connman_error_failed(msg, -err);
2912 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2915 static DBusMessage *remove_service(DBusConnection *conn,
2916 DBusMessage *msg, void *user_data)
2918 struct connman_service *service = user_data;
2920 DBG("service %p", service);
2922 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
2923 return __connman_error_not_supported(msg);
2925 if (service->immutable == TRUE)
2926 return __connman_error_not_supported(msg);
2928 if (service->favorite == FALSE && service->state !=
2929 CONNMAN_SERVICE_STATE_FAILURE)
2930 return __connman_error_not_supported(msg);
2932 if (service->network != NULL) {
2933 set_reconnect_state(service, FALSE);
2935 __connman_network_disconnect(service->network);
2938 g_free(service->passphrase);
2939 service->passphrase = NULL;
2941 passphrase_changed(service);
2945 __connman_service_set_favorite(service, FALSE);
2946 __connman_storage_save_service(service);
2948 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2951 static gboolean check_suitable_state(enum connman_service_state a,
2952 enum connman_service_state b)
2955 * Special check so that "ready" service can be moved before
2958 if ((a == CONNMAN_SERVICE_STATE_ONLINE &&
2959 b == CONNMAN_SERVICE_STATE_READY) ||
2960 (b == CONNMAN_SERVICE_STATE_ONLINE &&
2961 a == CONNMAN_SERVICE_STATE_READY))
2967 static DBusMessage *move_service(DBusConnection *conn,
2968 DBusMessage *msg, void *user_data,
2971 struct connman_service *service = user_data;
2972 struct connman_service *target;
2974 GSequenceIter *src, *dst;
2975 enum connman_ipconfig_method target4, target6;
2976 enum connman_ipconfig_method service4, service6;
2978 DBG("service %p", service);
2980 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
2983 if (service->favorite == FALSE)
2984 return __connman_error_not_supported(msg);
2986 target = find_service(path);
2987 if (target == NULL || target->favorite == FALSE || target == service)
2988 return __connman_error_invalid_service(msg);
2990 target4 = __connman_ipconfig_get_method(target->ipconfig_ipv4);
2991 target6 = __connman_ipconfig_get_method(target->ipconfig_ipv6);
2992 service4 = __connman_ipconfig_get_method(service->ipconfig_ipv4);
2993 service6 = __connman_ipconfig_get_method(service->ipconfig_ipv6);
2995 DBG("target %s method %d/%d state %d/%d", target->identifier,
2997 target->state_ipv4, target->state_ipv6);
2999 DBG("service %s method %d/%d state %d/%d", service->identifier,
3001 service->state_ipv4, service->state_ipv6);
3004 * If method is OFF, then we do not need to check the corresponding
3007 if (target4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3008 if (service6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3009 if (check_suitable_state(target->state_ipv6,
3010 service->state_ipv6) == FALSE)
3011 return __connman_error_invalid_service(msg);
3015 if (target6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3016 if (service4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3017 if (check_suitable_state(target->state_ipv4,
3018 service->state_ipv4) == FALSE)
3019 return __connman_error_invalid_service(msg);
3023 if (service4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3024 if (target6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3025 if (check_suitable_state(target->state_ipv6,
3026 service->state_ipv6) == FALSE)
3027 return __connman_error_invalid_service(msg);
3031 if (service6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3032 if (target4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3033 if (check_suitable_state(target->state_ipv4,
3034 service->state_ipv4) == FALSE)
3035 return __connman_error_invalid_service(msg);
3039 g_get_current_time(&service->modified);
3040 __connman_storage_save_service(service);
3042 src = g_hash_table_lookup(service_hash, service->identifier);
3043 dst = g_hash_table_lookup(service_hash, target->identifier);
3045 before ? g_sequence_move(src, dst) : g_sequence_move(dst, src);
3047 services_changed(FALSE);
3049 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3052 static DBusMessage *move_before(DBusConnection *conn,
3053 DBusMessage *msg, void *user_data)
3055 return move_service(conn, msg, user_data, TRUE);
3058 static DBusMessage *move_after(DBusConnection *conn,
3059 DBusMessage *msg, void *user_data)
3061 return move_service(conn, msg, user_data, FALSE);
3064 static DBusMessage *reset_counters(DBusConnection *conn,
3065 DBusMessage *msg, void *user_data)
3067 struct connman_service *service = user_data;
3069 reset_stats(service);
3071 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3074 static GDBusMethodTable service_methods[] = {
3075 { "GetProperties", "", "a{sv}", get_properties },
3076 { "SetProperty", "sv", "", set_property },
3077 { "ClearProperty", "s", "", clear_property },
3078 { "Connect", "", "", connect_service,
3079 G_DBUS_METHOD_FLAG_ASYNC },
3080 { "Disconnect", "", "", disconnect_service },
3081 { "Remove", "", "", remove_service },
3082 { "MoveBefore", "o", "", move_before },
3083 { "MoveAfter", "o", "", move_after },
3084 { "ResetCounters", "", "", reset_counters },
3088 static GDBusSignalTable service_signals[] = {
3089 { "PropertyChanged", "sv" },
3093 static void service_free(gpointer user_data)
3095 struct connman_service *service = user_data;
3096 char *path = service->path;
3098 DBG("service %p", service);
3100 reply_pending(service, ENOENT);
3102 g_hash_table_remove(service_hash, service->identifier);
3104 __connman_notifier_service_remove(service);
3106 stats_stop(service);
3107 __connman_storage_save_service(service);
3109 service->path = NULL;
3112 services_changed(FALSE);
3114 g_dbus_unregister_interface(connection, path,
3115 CONNMAN_SERVICE_INTERFACE);
3119 g_hash_table_destroy(service->counter_table);
3121 if (service->network != NULL) {
3122 if (service->network_created == TRUE)
3123 connman_network_unref(service->network);
3126 if (service->provider != NULL)
3127 connman_provider_unref(service->provider);
3129 if (service->ipconfig_ipv4 != NULL) {
3130 connman_ipconfig_set_ops(service->ipconfig_ipv4, NULL);
3131 connman_ipconfig_set_data(service->ipconfig_ipv4, NULL);
3132 connman_ipconfig_unref(service->ipconfig_ipv4);
3133 service->ipconfig_ipv4 = NULL;
3136 if (service->ipconfig_ipv6 != NULL) {
3137 connman_ipconfig_set_ops(service->ipconfig_ipv6, NULL);
3138 connman_ipconfig_set_data(service->ipconfig_ipv6, NULL);
3139 connman_ipconfig_unref(service->ipconfig_ipv6);
3140 service->ipconfig_ipv6 = NULL;
3143 if (service->location != NULL)
3144 connman_location_unref(service->location);
3146 g_strfreev(service->nameservers);
3147 g_strfreev(service->nameservers_config);
3148 g_strfreev(service->domains);
3149 g_strfreev(service->proxies);
3150 g_strfreev(service->excludes);
3152 g_free(service->domainname);
3153 g_free(service->pac);
3154 g_free(service->name);
3155 g_free(service->passphrase);
3156 g_free(service->agent_passphrase);
3157 g_free(service->identifier);
3158 g_free(service->eap);
3159 g_free(service->identity);
3160 g_free(service->agent_identity);
3161 g_free(service->ca_cert_file);
3162 g_free(service->client_cert_file);
3163 g_free(service->private_key_file);
3164 g_free(service->private_key_passphrase);
3165 g_free(service->phase2);
3167 if (service->stats.timer != NULL)
3168 g_timer_destroy(service->stats.timer);
3169 if (service->stats_roaming.timer != NULL)
3170 g_timer_destroy(service->stats_roaming.timer);
3176 * __connman_service_put:
3177 * @service: service structure
3179 * Release service if no longer needed
3181 void __connman_service_put(struct connman_service *service)
3183 DBG("service %p", service);
3185 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
3186 GSequenceIter *iter;
3188 iter = g_hash_table_lookup(service_hash, service->identifier);
3190 reply_pending(service, ECONNABORTED);
3192 __connman_service_disconnect(service);
3194 g_sequence_remove(iter);
3196 service_free(service);
3200 static void stats_init(struct connman_service *service)
3203 service->stats.valid = FALSE;
3204 service->stats.enabled = FALSE;
3205 service->stats.timer = g_timer_new();
3208 service->stats_roaming.valid = FALSE;
3209 service->stats_roaming.enabled = FALSE;
3210 service->stats_roaming.timer = g_timer_new();
3213 static void service_initialize(struct connman_service *service)
3215 DBG("service %p", service);
3217 service->refcount = 1;
3218 service->session_usage_count = 0;
3220 service->network_created = FALSE;
3222 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
3223 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
3225 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
3226 service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
3227 service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
3229 service->favorite = FALSE;
3230 service->immutable = FALSE;
3231 service->hidden = FALSE;
3233 service->ignore = FALSE;
3235 service->userconnect = FALSE;
3239 stats_init(service);
3241 service->provider = NULL;
3243 service->wps = FALSE;
3247 * connman_service_create:
3249 * Allocate a new service.
3251 * Returns: a newly-allocated #connman_service structure
3253 struct connman_service *connman_service_create(void)
3256 struct connman_stats_counter *counters;
3257 const char *counter;
3259 struct connman_service *service;
3261 service = g_try_new0(struct connman_service, 1);
3262 if (service == NULL)
3265 DBG("service %p", service);
3267 service->counter_table = g_hash_table_new_full(g_str_hash,
3268 g_str_equal, NULL, g_free);
3270 for (list = counter_list; list; list = list->next) {
3271 counter = list->data;
3273 counters = g_try_new0(struct connman_stats_counter, 1);
3274 if (counters == NULL) {
3275 g_hash_table_destroy(service->counter_table);
3280 counters->append_all = TRUE;
3282 g_hash_table_replace(service->counter_table, (gpointer)counter,
3286 service_initialize(service);
3288 service->location = __connman_location_create(service);
3293 struct connman_location *__connman_service_get_location(struct connman_service *service)
3295 return service->location;
3299 * connman_service_ref:
3300 * @service: service structure
3302 * Increase reference counter of service
3304 struct connman_service *connman_service_ref(struct connman_service *service)
3308 g_atomic_int_inc(&service->refcount);
3314 * connman_service_unref:
3315 * @service: service structure
3317 * Decrease reference counter of service
3319 void connman_service_unref(struct connman_service *service)
3321 __connman_service_put(service);
3324 static gint service_compare(gconstpointer a, gconstpointer b,
3327 struct connman_service *service_a = (void *) a;
3328 struct connman_service *service_b = (void *) b;
3329 enum connman_service_state state_a, state_b;
3331 state_a = service_a->state;
3332 state_b = service_b->state;
3334 if (state_a != state_b) {
3335 gboolean a_connected = is_connected(service_a);
3336 gboolean b_connected = is_connected(service_b);
3338 if (a_connected == TRUE && b_connected == TRUE) {
3339 /* We prefer online over ready state */
3340 if (state_a == CONNMAN_SERVICE_STATE_ONLINE)
3343 if (state_b == CONNMAN_SERVICE_STATE_ONLINE)
3347 if (a_connected == TRUE)
3349 if (b_connected == TRUE)
3352 if (is_connecting(service_a) == TRUE)
3354 if (is_connecting(service_b) == TRUE)
3358 if (service_a->order > service_b->order)
3361 if (service_a->order < service_b->order)
3364 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
3367 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
3370 if (service_a->type != service_b->type) {
3371 switch (service_a->type) {
3372 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3373 case CONNMAN_SERVICE_TYPE_SYSTEM:
3374 case CONNMAN_SERVICE_TYPE_ETHERNET:
3375 case CONNMAN_SERVICE_TYPE_GPS:
3376 case CONNMAN_SERVICE_TYPE_VPN:
3377 case CONNMAN_SERVICE_TYPE_GADGET:
3379 case CONNMAN_SERVICE_TYPE_WIFI:
3381 case CONNMAN_SERVICE_TYPE_WIMAX:
3382 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3383 case CONNMAN_SERVICE_TYPE_CELLULAR:
3388 return (gint) service_b->strength - (gint) service_a->strength;
3392 * connman_service_get_type:
3393 * @service: service structure
3395 * Get the type of service
3397 enum connman_service_type connman_service_get_type(struct connman_service *service)
3399 if (service == NULL)
3400 return CONNMAN_SERVICE_TYPE_UNKNOWN;
3402 return service->type;
3406 * connman_service_get_interface:
3407 * @service: service structure
3409 * Get network interface of service
3411 char *connman_service_get_interface(struct connman_service *service)
3415 if (service == NULL)
3418 if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
3419 if (service->ipconfig_ipv4)
3420 index = connman_ipconfig_get_index(
3421 service->ipconfig_ipv4);
3422 else if (service->ipconfig_ipv6)
3423 index = connman_ipconfig_get_index(
3424 service->ipconfig_ipv6);
3428 return connman_inet_ifname(index);
3431 if (service->network == NULL)
3434 index = connman_network_get_index(service->network);
3436 return connman_inet_ifname(index);
3440 * connman_service_get_network:
3441 * @service: service structure
3443 * Get the service network
3445 struct connman_network *
3446 __connman_service_get_network(struct connman_service *service)
3448 if (service == NULL)
3451 return service->network;
3454 struct connman_ipconfig *
3455 __connman_service_get_ip4config(struct connman_service *service)
3457 if (service == NULL)
3460 return service->ipconfig_ipv4;
3463 struct connman_ipconfig *
3464 __connman_service_get_ip6config(struct connman_service *service)
3466 if (service == NULL)
3469 return service->ipconfig_ipv6;
3472 struct connman_ipconfig *
3473 __connman_service_get_ipconfig(struct connman_service *service, int family)
3475 if (family == AF_INET)
3476 return __connman_service_get_ip4config(service);
3477 else if (family == AF_INET6)
3478 return __connman_service_get_ip6config(service);
3484 enum connman_service_security __connman_service_get_security(struct connman_service *service)
3486 if (service == NULL)
3487 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3489 return service->security;
3492 const char *__connman_service_get_phase2(struct connman_service *service)
3494 if (service == NULL)
3497 return service->phase2;
3500 connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
3502 if (service == NULL)
3505 return service->wps;
3509 * __connman_service_set_favorite:
3510 * @service: service structure
3511 * @favorite: favorite value
3513 * Change the favorite setting of service
3515 int __connman_service_set_favorite(struct connman_service *service,
3516 connman_bool_t favorite)
3518 GSequenceIter *iter;
3520 iter = g_hash_table_lookup(service_hash, service->identifier);
3524 if (service->favorite == favorite)
3527 service->favorite = favorite;
3529 favorite_changed(service);
3531 g_sequence_sort_changed(iter, service_compare, NULL);
3533 services_changed(FALSE);
3538 int __connman_service_set_immutable(struct connman_service *service,
3539 connman_bool_t immutable)
3541 service->immutable = immutable;
3543 immutable_changed(service);
3548 void __connman_service_set_string(struct connman_service *service,
3549 const char *key, const char *value)
3551 if (g_str_equal(key, "EAP") == TRUE) {
3552 g_free(service->eap);
3553 service->eap = g_strdup(value);
3554 } else if (g_str_equal(key, "Identity") == TRUE) {
3555 g_free(service->identity);
3556 service->identity = g_strdup(value);
3557 } else if (g_str_equal(key, "CACertFile") == TRUE) {
3558 g_free(service->ca_cert_file);
3559 service->ca_cert_file = g_strdup(value);
3560 } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
3561 g_free(service->client_cert_file);
3562 service->client_cert_file = g_strdup(value);
3563 } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
3564 g_free(service->private_key_file);
3565 service->private_key_file = g_strdup(value);
3566 } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
3567 g_free(service->private_key_passphrase);
3568 service->private_key_passphrase = g_strdup(value);
3569 } else if (g_str_equal(key, "Phase2") == TRUE) {
3570 g_free(service->phase2);
3571 service->phase2 = g_strdup(value);
3572 } else if (g_str_equal(key, "Passphrase") == TRUE) {
3573 g_free(service->passphrase);
3574 service->passphrase = g_strdup(value);
3578 static void service_complete(struct connman_service *service)
3580 reply_pending(service, EIO);
3582 if (service->userconnect == FALSE)
3583 __connman_service_auto_connect();
3585 g_get_current_time(&service->modified);
3586 __connman_storage_save_service(service);
3589 static void report_error_cb(struct connman_service *service,
3590 gboolean retry, void *user_data)
3593 __connman_service_connect(service);
3595 service_complete(service);
3596 services_changed(FALSE);
3597 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3601 static int __connman_service_indicate_state(struct connman_service *service)
3603 enum connman_service_state old_state, new_state;
3604 GSequenceIter *iter;
3606 if (service == NULL)
3609 old_state = service->state;
3610 new_state = combine_state(service->state_ipv4, service->state_ipv6);
3612 if (old_state == new_state)
3615 DBG("service %p old %s - new %s/%s => %s",
3617 state2string(old_state),
3618 state2string(service->state_ipv4),
3619 state2string(service->state_ipv6),
3620 state2string(new_state));
3622 service->state = new_state;
3623 state_changed(service);
3625 if (new_state == CONNMAN_SERVICE_STATE_IDLE &&
3626 old_state != CONNMAN_SERVICE_STATE_DISCONNECT) {
3627 reply_pending(service, ECONNABORTED);
3629 __connman_service_disconnect(service);
3632 if (new_state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
3633 if (__connman_stats_service_register(service) == 0) {
3634 __connman_stats_get(service, FALSE,
3635 &service->stats.data);
3636 __connman_stats_get(service, TRUE,
3637 &service->stats_roaming.data);
3641 if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
3642 if (service->login_required == TRUE) {
3643 service->login_required = FALSE;
3644 login_changed(service);
3647 connman_timeserver_sync();
3650 if (new_state == CONNMAN_SERVICE_STATE_IDLE) {
3651 connman_bool_t reconnect;
3653 reconnect = get_reconnect_state(service);
3654 if (reconnect == TRUE)
3655 __connman_service_auto_connect();
3657 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3660 if (new_state == CONNMAN_SERVICE_STATE_READY) {
3661 enum connman_ipconfig_method method;
3663 set_reconnect_state(service, TRUE);
3665 __connman_service_set_favorite(service, TRUE);
3667 reply_pending(service, 0);
3669 service->userconnect = FALSE;
3671 g_get_current_time(&service->modified);
3672 __connman_storage_save_service(service);
3674 update_nameservers(service);
3675 dns_changed(service);
3676 domain_changed(service);
3678 __connman_notifier_connect(service->type);
3680 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
3681 connman_network_get_bool(service->network,
3682 "WiFi.UseWPS") == TRUE) {
3685 pass = connman_network_get_string(service->network,
3688 __connman_service_set_passphrase(service, pass);
3690 connman_network_set_bool(service->network,
3691 "WiFi.UseWPS", FALSE);
3696 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
3697 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
3698 __connman_ipconfig_disable_ipv6(
3699 service->ipconfig_ipv6);
3701 } else if (new_state == CONNMAN_SERVICE_STATE_DISCONNECT) {
3702 struct connman_service *def_service = get_default();
3704 if (__connman_notifier_count_connected() == 0 &&
3705 def_service != NULL &&
3706 def_service->provider != NULL)
3707 __connman_provider_disconnect(def_service->provider);
3709 __connman_location_finish(service);
3713 __connman_wpad_stop(service);
3715 update_nameservers(service);
3716 dns_changed(service);
3717 domain_changed(service);
3719 __connman_notifier_disconnect(service->type);
3722 if (new_state == CONNMAN_SERVICE_STATE_FAILURE) {
3723 if (service->userconnect == TRUE &&
3724 __connman_agent_report_error(service,
3725 error2string(service->error),
3726 report_error_cb, NULL) == -EIO)
3728 service_complete(service);
3730 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3732 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
3734 iter = g_hash_table_lookup(service_hash, service->identifier);
3736 g_sequence_sort_changed(iter, service_compare, NULL);
3738 services_changed(FALSE);
3740 if (new_state == CONNMAN_SERVICE_STATE_ONLINE)
3746 int __connman_service_indicate_error(struct connman_service *service,
3747 enum connman_service_error error)
3749 DBG("service %p error %d", service, error);
3751 if (service == NULL)
3754 service->error = error;
3756 if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
3757 __connman_service_set_passphrase(service, NULL);
3759 __connman_service_ipconfig_indicate_state(service,
3760 CONNMAN_SERVICE_STATE_FAILURE,
3761 CONNMAN_IPCONFIG_TYPE_IPV4);
3762 __connman_service_ipconfig_indicate_state(service,
3763 CONNMAN_SERVICE_STATE_FAILURE,
3764 CONNMAN_IPCONFIG_TYPE_IPV6);
3768 int __connman_service_clear_error(struct connman_service *service)
3770 DBG("service %p", service);
3772 if (service == NULL)
3775 if (service->state != CONNMAN_SERVICE_STATE_FAILURE)
3778 service->state_ipv4 = service->state_ipv6 =
3779 CONNMAN_SERVICE_STATE_UNKNOWN;
3780 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;;
3782 if (service->favorite == TRUE)
3783 set_reconnect_state(service, TRUE);
3785 __connman_service_ipconfig_indicate_state(service,
3786 CONNMAN_SERVICE_STATE_IDLE,
3787 CONNMAN_IPCONFIG_TYPE_IPV6);
3790 * Toggling the IPv6 state to IDLE could trigger the auto connect
3791 * machinery and consequently the IPv4 state.
3793 if (service->state_ipv4 != CONNMAN_SERVICE_STATE_UNKNOWN &&
3794 service->state_ipv4 != CONNMAN_SERVICE_STATE_FAILURE)
3797 return __connman_service_ipconfig_indicate_state(service,
3798 CONNMAN_SERVICE_STATE_IDLE,
3799 CONNMAN_IPCONFIG_TYPE_IPV4);
3802 int __connman_service_indicate_default(struct connman_service *service)
3804 DBG("service %p", service);
3808 __connman_location_detect(service);
3813 static void check_proxy_setup(struct connman_service *service)
3816 * We start WPAD if we haven't got a PAC URL from DHCP and
3817 * if our proxy manual configuration is either empty or set
3818 * to AUTO with an empty URL.
3821 if (service->proxy != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
3824 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
3825 (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_AUTO ||
3826 service->pac != NULL))
3829 if (__connman_wpad_start(service) < 0) {
3830 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
3831 __connman_notifier_proxy_changed(service);
3835 int __connman_service_ipconfig_indicate_state(struct connman_service *service,
3836 enum connman_service_state new_state,
3837 enum connman_ipconfig_type type)
3839 struct connman_ipconfig *ipconfig = NULL;
3840 enum connman_service_state old_state;
3842 if (service == NULL)
3845 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
3846 old_state = service->state_ipv4;
3847 ipconfig = service->ipconfig_ipv4;
3848 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
3849 old_state = service->state_ipv6;
3850 ipconfig = service->ipconfig_ipv6;
3853 if (ipconfig == NULL)
3857 if (old_state == new_state)
3860 DBG("service %p (%s) state %d (%s) type %d (%s)",
3861 service, service ? service->identifier : NULL,
3862 new_state, state2string(new_state),
3863 type, __connman_ipconfig_type2string(type));
3865 switch (new_state) {
3866 case CONNMAN_SERVICE_STATE_UNKNOWN:
3867 case CONNMAN_SERVICE_STATE_IDLE:
3868 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
3871 case CONNMAN_SERVICE_STATE_ASSOCIATION:
3873 case CONNMAN_SERVICE_STATE_CONFIGURATION:
3874 __connman_ipconfig_enable(ipconfig);
3876 case CONNMAN_SERVICE_STATE_READY:
3877 update_nameservers(service);
3879 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
3880 check_proxy_setup(service);
3882 case CONNMAN_SERVICE_STATE_ONLINE:
3884 case CONNMAN_SERVICE_STATE_DISCONNECT:
3885 if (service->state == CONNMAN_SERVICE_STATE_IDLE)
3888 case CONNMAN_SERVICE_STATE_FAILURE:
3892 /* We keep that state */
3893 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
3894 service->state_ipv4 = new_state;
3895 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
3896 service->state_ipv6 = new_state;
3898 return __connman_service_indicate_state(service);
3901 int __connman_service_request_login(struct connman_service *service)
3903 DBG("service %p", service);
3905 if (service == NULL)
3908 service->login_required = TRUE;
3909 login_changed(service);
3914 static connman_bool_t prepare_network(struct connman_service *service)
3916 enum connman_network_type type;
3917 unsigned int ssid_len;
3919 type = connman_network_get_type(service->network);
3922 case CONNMAN_NETWORK_TYPE_UNKNOWN:
3923 case CONNMAN_NETWORK_TYPE_VENDOR:
3925 case CONNMAN_NETWORK_TYPE_WIFI:
3926 if (connman_network_get_blob(service->network, "WiFi.SSID",
3930 if (service->passphrase != NULL)
3931 connman_network_set_string(service->network,
3932 "WiFi.Passphrase", service->passphrase);
3934 case CONNMAN_NETWORK_TYPE_ETHERNET:
3935 case CONNMAN_NETWORK_TYPE_WIMAX:
3936 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
3937 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
3938 case CONNMAN_NETWORK_TYPE_CELLULAR:
3945 static void prepare_8021x(struct connman_service *service)
3947 if (service->eap != NULL)
3948 connman_network_set_string(service->network, "WiFi.EAP",
3951 if (service->identity != NULL)
3952 connman_network_set_string(service->network, "WiFi.Identity",
3955 if (service->ca_cert_file != NULL)
3956 connman_network_set_string(service->network, "WiFi.CACertFile",
3957 service->ca_cert_file);
3959 if (service->client_cert_file != NULL)
3960 connman_network_set_string(service->network,
3961 "WiFi.ClientCertFile",
3962 service->client_cert_file);
3964 if (service->private_key_file != NULL)
3965 connman_network_set_string(service->network,
3966 "WiFi.PrivateKeyFile",
3967 service->private_key_file);
3969 if (service->private_key_passphrase != NULL)
3970 connman_network_set_string(service->network,
3971 "WiFi.PrivateKeyPassphrase",
3972 service->private_key_passphrase);
3974 if (service->phase2 != NULL)
3975 connman_network_set_string(service->network, "WiFi.Phase2",
3979 static int service_connect(struct connman_service *service)
3983 switch (service->type) {
3984 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3985 case CONNMAN_SERVICE_TYPE_SYSTEM:
3986 case CONNMAN_SERVICE_TYPE_GPS:
3987 case CONNMAN_SERVICE_TYPE_GADGET:
3989 case CONNMAN_SERVICE_TYPE_ETHERNET:
3990 case CONNMAN_SERVICE_TYPE_WIMAX:
3991 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3992 case CONNMAN_SERVICE_TYPE_CELLULAR:
3993 case CONNMAN_SERVICE_TYPE_VPN:
3995 case CONNMAN_SERVICE_TYPE_WIFI:
3996 switch (service->security) {
3997 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3998 case CONNMAN_SERVICE_SECURITY_NONE:
4000 case CONNMAN_SERVICE_SECURITY_WEP:
4001 case CONNMAN_SERVICE_SECURITY_PSK:
4002 case CONNMAN_SERVICE_SECURITY_WPA:
4003 case CONNMAN_SERVICE_SECURITY_RSN:
4004 if (service->passphrase == NULL) {
4005 if (service->network == NULL)
4008 if (service->wps == FALSE ||
4009 connman_network_get_bool(
4011 "WiFi.UseWPS") == FALSE)
4015 case CONNMAN_SERVICE_SECURITY_8021X:
4016 if (service->eap == NULL)
4020 * never request credentials if using EAP-TLS
4021 * (EAP-TLS networks need to be fully provisioned)
4023 if (g_str_equal(service->eap, "tls") == TRUE)
4027 * Return -ENOKEY if either identity or passphrase is
4028 * missing. Agent provided credentials can be used as
4029 * fallback if needed.
4031 if ((service->identity == NULL &&
4032 service->agent_identity == NULL) ||
4033 (service->passphrase == NULL &&
4034 service->agent_passphrase == NULL))
4042 if (service->network != NULL) {
4043 if (prepare_network(service) == FALSE)
4046 switch (service->security) {
4047 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
4048 case CONNMAN_SERVICE_SECURITY_NONE:
4049 case CONNMAN_SERVICE_SECURITY_WEP:
4050 case CONNMAN_SERVICE_SECURITY_PSK:
4051 case CONNMAN_SERVICE_SECURITY_WPA:
4052 case CONNMAN_SERVICE_SECURITY_RSN:
4054 case CONNMAN_SERVICE_SECURITY_8021X:
4055 prepare_8021x(service);
4059 if (__connman_stats_service_register(service) == 0) {
4060 __connman_stats_get(service, FALSE,
4061 &service->stats.data);
4062 __connman_stats_get(service, TRUE,
4063 &service->stats_roaming.data);
4066 if (service->ipconfig_ipv4)
4067 __connman_ipconfig_enable(service->ipconfig_ipv4);
4068 if (service->ipconfig_ipv6)
4069 __connman_ipconfig_enable(service->ipconfig_ipv6);
4071 err = __connman_network_connect(service->network);
4072 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4073 service->provider != NULL)
4074 err = __connman_provider_connect(service->provider);
4079 if (err != -EINPROGRESS) {
4080 __connman_ipconfig_disable(service->ipconfig_ipv4);
4081 __connman_ipconfig_disable(service->ipconfig_ipv6);
4082 __connman_stats_service_unregister(service);
4090 int __connman_service_connect(struct connman_service *service)
4094 DBG("service %p state %s", service, state2string(service->state));
4096 if (is_connected(service) == TRUE)
4099 if (is_connecting(service) == TRUE)
4102 switch (service->type) {
4103 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4104 case CONNMAN_SERVICE_TYPE_SYSTEM:
4105 case CONNMAN_SERVICE_TYPE_GPS:
4106 case CONNMAN_SERVICE_TYPE_GADGET:
4109 err = service_connect(service);
4115 if (err == -EINPROGRESS) {
4116 if (service->timeout == 0)
4117 service->timeout = g_timeout_add_seconds(
4118 CONNECT_TIMEOUT, connect_timeout, service);
4120 return -EINPROGRESS;
4123 __connman_service_ipconfig_indicate_state(service,
4124 CONNMAN_SERVICE_STATE_FAILURE,
4125 CONNMAN_IPCONFIG_TYPE_IPV4);
4126 __connman_service_ipconfig_indicate_state(service,
4127 CONNMAN_SERVICE_STATE_FAILURE,
4128 CONNMAN_IPCONFIG_TYPE_IPV6);
4130 if (service->network != NULL)
4131 __connman_network_disconnect(service->network);
4132 else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4133 service->provider != NULL)
4134 __connman_provider_disconnect(service->provider);
4136 if (service->userconnect == TRUE) {
4137 if (err == -ENOKEY) {
4138 if (__connman_agent_request_input(service,
4141 return -EINPROGRESS;
4143 reply_pending(service, err);
4149 int __connman_service_disconnect(struct connman_service *service)
4153 DBG("service %p", service);
4155 if (service->network != NULL) {
4156 err = __connman_network_disconnect(service->network);
4157 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4158 service->provider != NULL)
4159 err = __connman_provider_disconnect(service->provider);
4163 if (err < 0 && err != -EINPROGRESS)
4166 __connman_6to4_remove(service->ipconfig_ipv4);
4168 if (service->ipconfig_ipv4)
4169 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
4172 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
4175 __connman_ipconfig_address_remove(service->ipconfig_ipv4);
4176 __connman_ipconfig_address_remove(service->ipconfig_ipv6);
4178 __connman_ipconfig_disable(service->ipconfig_ipv4);
4179 __connman_ipconfig_disable(service->ipconfig_ipv6);
4181 __connman_stats_service_unregister(service);
4186 int __connman_service_disconnect_all(void)
4188 GSequenceIter *iter;
4192 iter = g_sequence_get_begin_iter(service_list);
4194 while (g_sequence_iter_is_end(iter) == FALSE) {
4195 struct connman_service *service = g_sequence_get(iter);
4197 service->ignore = TRUE;
4199 set_reconnect_state(service, FALSE);
4201 __connman_service_disconnect(service);
4203 iter = g_sequence_iter_next(iter);
4211 * __connman_service_lookup:
4212 * @pattern: search pattern
4213 * @path: return object path
4215 * Look up a service path from a search pattern
4217 int __connman_service_lookup(const char *pattern, const char **path)
4219 GHashTableIter iter;
4220 gpointer key, value;
4222 g_hash_table_iter_init(&iter, service_hash);
4224 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
4225 GSequenceIter *iter = value;
4226 struct connman_service *service = g_sequence_get(iter);
4228 if (g_strcmp0(service->identifier, pattern) == 0 ||
4229 g_strcmp0(service->name, pattern) == 0) {
4230 *path = (const char *) service->path;
4239 * lookup_by_identifier:
4240 * @identifier: service identifier
4242 * Look up a service by identifier (reference count will not be increased)
4244 static struct connman_service *lookup_by_identifier(const char *identifier)
4246 GSequenceIter *iter;
4248 iter = g_hash_table_lookup(service_hash, identifier);
4250 return g_sequence_get(iter);
4255 static struct connman_network *create_hidden_wifi(struct connman_device *device,
4256 const char *ssid, const char *mode, const char *security,
4259 struct connman_network *network;
4262 unsigned int i, ssid_len;
4264 ssid_len = strlen(ssid);
4268 network = connman_network_create(group, CONNMAN_NETWORK_TYPE_WIFI);
4269 if (network == NULL)
4272 connman_network_set_blob(network, "WiFi.SSID",
4273 (unsigned char *) ssid, ssid_len);
4275 connman_network_set_string(network, "WiFi.Mode", mode);
4276 connman_network_set_string(network, "WiFi.Security", security);
4278 name = g_try_malloc0(ssid_len + 1);
4280 connman_network_unref(network);
4284 for (i = 0; i < ssid_len; i++) {
4285 if (g_ascii_isprint(ssid[i]))
4291 connman_network_set_name(network, name);
4295 index = connman_device_get_index(device);
4296 connman_network_set_index(network, index);
4298 if (connman_device_add_network(device, network) < 0) {
4299 connman_network_unref(network);
4303 connman_network_set_available(network, TRUE);
4308 int __connman_service_create_and_connect(DBusMessage *msg)
4310 struct connman_service *service;
4311 struct connman_network *network;
4312 struct connman_device *device;
4313 DBusMessageIter iter, array;
4314 const char *mode = "managed", *security = "none", *group_security;
4315 const char *type = NULL, *ssid = NULL, *passphrase = NULL;
4316 unsigned int ssid_len = 0;
4321 dbus_message_iter_init(msg, &iter);
4322 dbus_message_iter_recurse(&iter, &array);
4324 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
4325 DBusMessageIter entry, value;
4328 dbus_message_iter_recurse(&array, &entry);
4329 dbus_message_iter_get_basic(&entry, &key);
4331 dbus_message_iter_next(&entry);
4332 dbus_message_iter_recurse(&entry, &value);
4334 switch (dbus_message_iter_get_arg_type(&value)) {
4335 case DBUS_TYPE_STRING:
4336 if (g_str_equal(key, "Type") == TRUE)
4337 dbus_message_iter_get_basic(&value, &type);
4338 else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
4339 g_str_equal(key, "Mode") == TRUE)
4340 dbus_message_iter_get_basic(&value, &mode);
4341 else if (g_str_equal(key, "WiFi.Security") == TRUE ||
4342 g_str_equal(key, "Security") == TRUE)
4343 dbus_message_iter_get_basic(&value, &security);
4344 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
4345 g_str_equal(key, "Passphrase") == TRUE)
4346 dbus_message_iter_get_basic(&value, &passphrase);
4347 else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
4348 g_str_equal(key, "SSID") == TRUE)
4349 dbus_message_iter_get_basic(&value, &ssid);
4352 dbus_message_iter_next(&array);
4358 if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
4364 ssid_len = strlen(ssid);
4368 if (g_strcmp0(security, "none") != 0 &&
4369 g_strcmp0(security, "wep") != 0 &&
4370 g_strcmp0(security, "psk") != 0 &&
4371 g_strcmp0(security, "wpa") != 0 &&
4372 g_strcmp0(security, "rsn") != 0 &&
4373 g_strcmp0(security, "ieee8021x") != 0)
4376 device = __connman_device_find_device(CONNMAN_SERVICE_TYPE_WIFI);
4380 ident = connman_device_get_ident(device);
4385 if (!g_strcmp0(security, "wpa") ||
4386 !g_strcmp0(security, "rsn"))
4387 group_security = "psk";
4389 group_security = security;
4391 group = wifi_build_group_name((unsigned char *) ssid,
4392 ssid_len, mode, group_security);
4396 name = g_strdup_printf("%s_%s_%s", type, ident, group);
4398 service = lookup_by_identifier(name);
4400 if (service != NULL)
4403 network = create_hidden_wifi(device, ssid, mode, security, group);
4404 if (network != NULL)
4405 connman_network_set_group(network, group);
4407 service = lookup_by_identifier(name);
4413 if (service == NULL) {
4418 service->network_created = TRUE;
4420 if (is_connected(service) == TRUE) {
4425 if (is_connecting(service) == TRUE) {
4430 set_reconnect_state(service, FALSE);
4432 __connman_device_disconnect(device);
4434 if (passphrase != NULL) {
4435 g_free(service->passphrase);
4436 service->passphrase = g_strdup(passphrase);
4439 service->userconnect = TRUE;
4441 err = __connman_service_connect(service);
4442 if (err < 0 && err != -EINPROGRESS)
4445 service->pending = dbus_message_ref(msg);
4450 if (service != NULL && service->network_created == TRUE) {
4451 struct connman_network *network = service->network;
4453 if (network != NULL) {
4454 connman_network_set_available(network, FALSE);
4455 __connman_device_cleanup_networks(device);
4457 __connman_service_put(service);
4463 static void provision_changed(gpointer value, gpointer user_data)
4465 struct connman_service *service = value;
4466 char *path = user_data;
4468 __connman_config_provision_service_ident(service, path);
4471 void __connman_service_provision_changed(const char *ident)
4473 g_sequence_foreach(service_list, provision_changed, (void *)ident);
4476 int __connman_service_provision(DBusMessage *msg)
4478 GKeyFile *keyfile = NULL;
4479 const char *config_str = NULL;
4480 char *group = NULL, *ident = NULL;
4482 struct connman_service *service;
4486 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &config_str,
4489 if (config_str == NULL || strlen(config_str) == 0)
4492 keyfile = g_key_file_new();
4494 /* populate GKeyFile with config_str */
4495 if (g_key_file_load_from_data(keyfile, config_str,
4496 strlen(config_str), 0, NULL) == FALSE) {
4502 * read only one group of settings (only one service supported, no
4505 group = g_key_file_get_start_group(keyfile);
4507 if (group == NULL || g_str_has_prefix(group, "service_") == FALSE) {
4512 err = __connman_config_load_service(keyfile, group, TRUE);
4516 ident = group + strlen("service_");
4518 /* trigger service provisioning if service exists */
4519 service = lookup_by_identifier(ident);
4520 if (service != NULL)
4521 __connman_config_provision_service(service);
4523 g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
4529 if (keyfile != NULL)
4530 g_key_file_free(keyfile);
4536 * __connman_service_get:
4537 * @identifier: service identifier
4539 * Look up a service by identifier or create a new one if not found
4541 static struct connman_service *service_get(const char *identifier)
4543 struct connman_service *service;
4544 GSequenceIter *iter;
4546 iter = g_hash_table_lookup(service_hash, identifier);
4548 service = g_sequence_get(iter);
4549 if (service != NULL)
4550 connman_service_ref(service);
4554 service = connman_service_create();
4555 if (service == NULL)
4558 DBG("service %p", service);
4560 service->identifier = g_strdup(identifier);
4562 iter = g_sequence_insert_sorted(service_list, service,
4563 service_compare, NULL);
4565 g_hash_table_insert(service_hash, service->identifier, iter);
4570 static int service_register(struct connman_service *service)
4572 GSequenceIter *iter;
4574 DBG("service %p", service);
4576 if (service->path != NULL)
4579 service->path = g_strdup_printf("%s/service/%s", CONNMAN_PATH,
4580 service->identifier);
4582 DBG("path %s", service->path);
4584 __connman_config_provision_service(service);
4586 __connman_storage_load_service(service);
4588 g_dbus_register_interface(connection, service->path,
4589 CONNMAN_SERVICE_INTERFACE,
4590 service_methods, service_signals,
4591 NULL, service, NULL);
4593 iter = g_hash_table_lookup(service_hash, service->identifier);
4595 g_sequence_sort_changed(iter, service_compare, NULL);
4597 services_changed(TRUE);
4602 static void service_up(struct connman_ipconfig *ipconfig)
4604 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4606 DBG("%s up", connman_ipconfig_get_ifname(ipconfig));
4608 link_changed(service);
4610 service->stats.valid = FALSE;
4611 service->stats_roaming.valid = FALSE;
4614 static void service_down(struct connman_ipconfig *ipconfig)
4616 DBG("%s down", connman_ipconfig_get_ifname(ipconfig));
4619 static void service_lower_up(struct connman_ipconfig *ipconfig)
4621 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4623 DBG("%s lower up", connman_ipconfig_get_ifname(ipconfig));
4625 stats_start(service);
4628 static void service_lower_down(struct connman_ipconfig *ipconfig)
4630 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4632 DBG("%s lower down", connman_ipconfig_get_ifname(ipconfig));
4634 stats_stop(service);
4635 __connman_storage_save_service(service);
4638 static void service_ip_bound(struct connman_ipconfig *ipconfig)
4640 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4641 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4642 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4644 DBG("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
4646 type = __connman_ipconfig_get_config_type(ipconfig);
4647 method = __connman_ipconfig_get_method(ipconfig);
4649 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4652 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4653 method == CONNMAN_IPCONFIG_METHOD_AUTO)
4654 __connman_service_ipconfig_indicate_state(service,
4655 CONNMAN_SERVICE_STATE_READY,
4656 CONNMAN_IPCONFIG_TYPE_IPV6);
4658 settings_changed(service, ipconfig);
4661 static void service_ip_release(struct connman_ipconfig *ipconfig)
4663 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4664 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4665 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4667 DBG("%s ip release", connman_ipconfig_get_ifname(ipconfig));
4669 type = __connman_ipconfig_get_config_type(ipconfig);
4670 method = __connman_ipconfig_get_method(ipconfig);
4672 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4675 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4676 method == CONNMAN_IPCONFIG_METHOD_OFF)
4677 __connman_service_ipconfig_indicate_state(service,
4678 CONNMAN_SERVICE_STATE_DISCONNECT,
4679 CONNMAN_IPCONFIG_TYPE_IPV6);
4681 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
4682 method == CONNMAN_IPCONFIG_METHOD_OFF)
4683 __connman_service_ipconfig_indicate_state(service,
4684 CONNMAN_SERVICE_STATE_DISCONNECT,
4685 CONNMAN_IPCONFIG_TYPE_IPV4);
4687 settings_changed(service, ipconfig);
4690 static const struct connman_ipconfig_ops service_ops = {
4692 .down = service_down,
4693 .lower_up = service_lower_up,
4694 .lower_down = service_lower_down,
4695 .ip_bound = service_ip_bound,
4696 .ip_release = service_ip_release,
4699 static void setup_ip4config(struct connman_service *service, int index,
4700 enum connman_ipconfig_method method)
4705 service->ipconfig_ipv4 = connman_ipconfig_create(index,
4706 CONNMAN_IPCONFIG_TYPE_IPV4);
4707 if (service->ipconfig_ipv4 == NULL)
4710 connman_ipconfig_set_method(service->ipconfig_ipv4, method);
4712 connman_ipconfig_set_data(service->ipconfig_ipv4, service);
4714 connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
4717 static void setup_ip6config(struct connman_service *service, int index)
4722 service->ipconfig_ipv6 = connman_ipconfig_create(index,
4723 CONNMAN_IPCONFIG_TYPE_IPV6);
4724 if (service->ipconfig_ipv6 == NULL)
4727 connman_ipconfig_set_data(service->ipconfig_ipv6, service);
4729 connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
4732 void __connman_service_read_ip4config(struct connman_service *service)
4734 const char *ident = "default";
4740 if (service->ipconfig_ipv4 == NULL)
4743 keyfile = __connman_storage_open_profile(ident);
4744 if (keyfile == NULL)
4747 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
4748 service->identifier, "IPv4.");
4750 g_key_file_free(keyfile);
4753 void __connman_service_create_ip4config(struct connman_service *service,
4756 DBG("ipv4 %p", service->ipconfig_ipv4);
4758 if (service->ipconfig_ipv4 != NULL)
4761 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
4762 __connman_service_read_ip4config(service);
4765 void __connman_service_read_ip6config(struct connman_service *service)
4767 const char *ident = "default";
4772 if (service->ipconfig_ipv6 == NULL)
4775 keyfile = __connman_storage_open_profile(ident);
4777 if (keyfile == NULL)
4780 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
4781 service->identifier, "IPv6.");
4783 g_key_file_free(keyfile);
4786 void __connman_service_create_ip6config(struct connman_service *service,
4789 DBG("ipv6 %p", service->ipconfig_ipv6);
4791 if (service->ipconfig_ipv6 != NULL)
4794 setup_ip6config(service, index);
4795 __connman_service_read_ip6config(service);
4799 * __connman_service_lookup_from_network:
4800 * @network: network structure
4802 * Look up a service by network (reference count will not be increased)
4804 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
4806 struct connman_service *service;
4807 const char *ident, *group;
4810 DBG("network %p", network);
4812 ident = __connman_network_get_ident(network);
4816 group = connman_network_get_group(network);
4820 name = g_strdup_printf("%s_%s_%s",
4821 __connman_network_get_type(network), ident, group);
4822 service = lookup_by_identifier(name);
4828 struct connman_service *__connman_service_lookup_from_index(int index)
4830 struct connman_service *service;
4831 GSequenceIter *iter;
4833 iter = g_sequence_get_begin_iter(service_list);
4835 while (g_sequence_iter_is_end(iter) == FALSE) {
4836 service = g_sequence_get(iter);
4838 if (connman_ipconfig_get_index(service->ipconfig_ipv4)
4842 if (connman_ipconfig_get_index(service->ipconfig_ipv6)
4846 iter = g_sequence_iter_next(iter);
4852 const char *__connman_service_get_ident(struct connman_service *service)
4854 return service->identifier;
4857 const char *__connman_service_get_path(struct connman_service *service)
4859 return service->path;
4862 unsigned int __connman_service_get_order(struct connman_service *service)
4864 GSequenceIter *iter;
4866 if (service == NULL)
4869 if (service->favorite == FALSE) {
4874 iter = g_hash_table_lookup(service_hash, service->identifier);
4876 if (g_sequence_iter_get_position(iter) == 0)
4878 else if (service->type == CONNMAN_SERVICE_TYPE_VPN)
4879 service->order = 10;
4885 return service->order;
4888 static enum connman_service_type convert_network_type(struct connman_network *network)
4890 enum connman_network_type type = connman_network_get_type(network);
4893 case CONNMAN_NETWORK_TYPE_UNKNOWN:
4894 case CONNMAN_NETWORK_TYPE_VENDOR:
4896 case CONNMAN_NETWORK_TYPE_ETHERNET:
4897 return CONNMAN_SERVICE_TYPE_ETHERNET;
4898 case CONNMAN_NETWORK_TYPE_WIFI:
4899 return CONNMAN_SERVICE_TYPE_WIFI;
4900 case CONNMAN_NETWORK_TYPE_WIMAX:
4901 return CONNMAN_SERVICE_TYPE_WIMAX;
4902 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
4903 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
4904 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
4905 case CONNMAN_NETWORK_TYPE_CELLULAR:
4906 return CONNMAN_SERVICE_TYPE_CELLULAR;
4909 return CONNMAN_SERVICE_TYPE_UNKNOWN;
4912 static enum connman_service_security convert_wifi_security(const char *security)
4914 if (security == NULL)
4915 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4916 else if (g_str_equal(security, "none") == TRUE)
4917 return CONNMAN_SERVICE_SECURITY_NONE;
4918 else if (g_str_equal(security, "wep") == TRUE)
4919 return CONNMAN_SERVICE_SECURITY_WEP;
4920 else if (g_str_equal(security, "psk") == TRUE)
4921 return CONNMAN_SERVICE_SECURITY_PSK;
4922 else if (g_str_equal(security, "ieee8021x") == TRUE)
4923 return CONNMAN_SERVICE_SECURITY_8021X;
4924 else if (g_str_equal(security, "wpa") == TRUE)
4925 return CONNMAN_SERVICE_SECURITY_WPA;
4926 else if (g_str_equal(security, "rsn") == TRUE)
4927 return CONNMAN_SERVICE_SECURITY_RSN;
4929 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4932 static void update_from_network(struct connman_service *service,
4933 struct connman_network *network)
4935 connman_uint8_t strength = service->strength;
4936 GSequenceIter *iter;
4939 DBG("service %p network %p", service, network);
4941 if (is_connected(service) == TRUE)
4944 if (is_connecting(service) == TRUE)
4947 str = connman_network_get_string(network, "Name");
4949 g_free(service->name);
4950 service->name = g_strdup(str);
4951 service->hidden = FALSE;
4953 g_free(service->name);
4954 service->name = NULL;
4955 service->hidden = TRUE;
4958 service->strength = connman_network_get_strength(network);
4959 service->roaming = connman_network_get_bool(network, "Roaming");
4961 if (service->strength == 0) {
4963 * Filter out 0-values; it's unclear what they mean
4964 * and they cause anomalous sorting of the priority list.
4966 service->strength = strength;
4969 str = connman_network_get_string(network, "WiFi.Security");
4970 service->security = convert_wifi_security(str);
4972 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
4973 service->wps = connman_network_get_bool(network, "WiFi.WPS");
4975 if (service->strength > strength && service->network != NULL) {
4976 service->network = network;
4978 strength_changed(service);
4981 if (service->network == NULL)
4982 service->network = network;
4984 iter = g_hash_table_lookup(service_hash, service->identifier);
4986 g_sequence_sort_changed(iter, service_compare, NULL);
4990 * __connman_service_create_from_network:
4991 * @network: network structure
4993 * Look up service by network and if not found, create one
4995 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
4997 struct connman_service *service;
4998 struct connman_device *device;
4999 const char *ident, *group;
5003 DBG("network %p", network);
5005 ident = __connman_network_get_ident(network);
5009 group = connman_network_get_group(network);
5013 name = g_strdup_printf("%s_%s_%s",
5014 __connman_network_get_type(network), ident, group);
5015 service = service_get(name);
5018 if (service == NULL)
5021 if (__connman_network_get_weakness(network) == TRUE)
5024 if (service->path != NULL) {
5025 update_from_network(service, network);
5026 services_changed(TRUE);
5030 service->type = convert_network_type(network);
5032 switch (service->type) {
5033 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5034 case CONNMAN_SERVICE_TYPE_SYSTEM:
5035 case CONNMAN_SERVICE_TYPE_ETHERNET:
5036 case CONNMAN_SERVICE_TYPE_WIMAX:
5037 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5038 case CONNMAN_SERVICE_TYPE_GPS:
5039 case CONNMAN_SERVICE_TYPE_VPN:
5040 case CONNMAN_SERVICE_TYPE_GADGET:
5041 service->autoconnect = FALSE;
5043 case CONNMAN_SERVICE_TYPE_WIFI:
5044 case CONNMAN_SERVICE_TYPE_CELLULAR:
5045 service->autoconnect = TRUE;
5049 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5050 service->state = combine_state(service->state_ipv4, service->state_ipv6);
5052 update_from_network(service, network);
5054 index = connman_network_get_index(network);
5056 if (service->ipconfig_ipv4 == NULL)
5057 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
5059 if (service->ipconfig_ipv6 == NULL)
5060 setup_ip6config(service, index);
5062 service_register(service);
5064 if (service->favorite == TRUE) {
5065 device = connman_network_get_device(service->network);
5066 if (device && __connman_device_scanning(device) == FALSE)
5067 __connman_service_auto_connect();
5070 __connman_notifier_service_add(service, service->name);
5075 void __connman_service_update_from_network(struct connman_network *network)
5077 struct connman_service *service;
5078 connman_uint8_t strength;
5079 connman_bool_t roaming;
5080 GSequenceIter *iter;
5082 connman_bool_t stats_enable;
5084 DBG("network %p", network);
5086 service = __connman_service_lookup_from_network(network);
5087 if (service == NULL)
5090 if (service->network == NULL)
5093 name = connman_network_get_string(service->network, "Name");
5094 if (g_strcmp0(service->name, name) != 0) {
5095 g_free(service->name);
5096 service->name = g_strdup(name);
5097 connman_dbus_property_changed_basic(service->path,
5098 CONNMAN_SERVICE_INTERFACE, "Name",
5099 DBUS_TYPE_STRING, &service->name);
5102 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
5103 service->wps = connman_network_get_bool(network, "WiFi.WPS");
5105 strength = connman_network_get_strength(service->network);
5106 if (strength == service->strength)
5109 service->strength = strength;
5111 strength_changed(service);
5114 roaming = connman_network_get_bool(service->network, "Roaming");
5115 if (roaming == service->roaming)
5118 stats_enable = stats_enabled(service);
5119 if (stats_enable == TRUE)
5120 stats_stop(service);
5122 service->roaming = roaming;
5124 if (stats_enable == TRUE)
5125 stats_start(service);
5127 roaming_changed(service);
5129 iter = g_hash_table_lookup(service_hash, service->identifier);
5131 g_sequence_sort_changed(iter, service_compare, NULL);
5134 void __connman_service_remove_from_network(struct connman_network *network)
5136 struct connman_service *service;
5138 DBG("network %p", network);
5140 service = __connman_service_lookup_from_network(network);
5141 if (service == NULL)
5144 __connman_service_put(service);
5148 * __connman_service_create_from_provider:
5149 * @provider: provider structure
5151 * Look up service by provider and if not found, create one
5153 struct connman_service *
5154 __connman_service_create_from_provider(struct connman_provider *provider)
5156 struct connman_service *service;
5157 const char *ident, *str;
5159 int index = connman_provider_get_index(provider);
5161 DBG("provider %p", provider);
5163 ident = __connman_provider_get_ident(provider);
5167 name = g_strdup_printf("vpn_%s", ident);
5168 service = service_get(name);
5171 if (service == NULL)
5174 service->type = CONNMAN_SERVICE_TYPE_VPN;
5175 service->provider = connman_provider_ref(provider);
5176 service->autoconnect = FALSE;
5178 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5180 str = connman_provider_get_string(provider, "Name");
5182 g_free(service->name);
5183 service->name = g_strdup(str);
5184 service->hidden = FALSE;
5186 g_free(service->name);
5187 service->name = NULL;
5188 service->hidden = TRUE;
5191 service->strength = 0;
5193 if (service->ipconfig_ipv4 == NULL)
5194 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_MANUAL);
5196 if (service->ipconfig_ipv6 == NULL)
5197 setup_ip6config(service, index);
5199 service_register(service);
5201 __connman_notifier_service_add(service, service->name);
5206 void __connman_service_downgrade_state(struct connman_service *service)
5208 if (service == NULL)
5211 DBG("service %p state4 %d state6 %d", service, service->state_ipv4,
5212 service->state_ipv6);
5214 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_ONLINE)
5215 __connman_service_ipconfig_indicate_state(service,
5216 CONNMAN_SERVICE_STATE_READY,
5217 CONNMAN_IPCONFIG_TYPE_IPV4);
5219 if (service->state_ipv6 == CONNMAN_SERVICE_STATE_ONLINE)
5220 __connman_service_ipconfig_indicate_state(service,
5221 CONNMAN_SERVICE_STATE_READY,
5222 CONNMAN_IPCONFIG_TYPE_IPV6);
5225 static int service_load(struct connman_service *service)
5227 const char *ident = "default";
5229 GError *error = NULL;
5230 gchar *pathname, *data = NULL;
5233 connman_bool_t autoconnect;
5234 unsigned int ssid_len;
5237 DBG("service %p", service);
5242 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
5243 if (pathname == NULL)
5246 keyfile = g_key_file_new();
5248 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
5255 if (g_key_file_load_from_data(keyfile, data, length,
5256 0, NULL) == FALSE) {
5263 switch (service->type) {
5264 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5265 case CONNMAN_SERVICE_TYPE_SYSTEM:
5266 case CONNMAN_SERVICE_TYPE_ETHERNET:
5267 case CONNMAN_SERVICE_TYPE_GPS:
5268 case CONNMAN_SERVICE_TYPE_VPN:
5269 case CONNMAN_SERVICE_TYPE_GADGET:
5271 case CONNMAN_SERVICE_TYPE_WIFI:
5272 if (service->name == NULL) {
5275 name = g_key_file_get_string(keyfile,
5276 service->identifier, "Name", NULL);
5278 g_free(service->name);
5279 service->name = name;
5282 if (service->network != NULL)
5283 connman_network_set_name(service->network,
5287 if (service->network &&
5288 connman_network_get_blob(service->network,
5289 "WiFi.SSID", &ssid_len) == NULL) {
5292 hex_ssid = g_key_file_get_string(keyfile,
5293 service->identifier,
5296 if (hex_ssid != NULL) {
5298 unsigned int i, j = 0, hex;
5299 size_t hex_ssid_len = strlen(hex_ssid);
5301 ssid = g_try_malloc0(hex_ssid_len / 2);
5308 for (i = 0; i < hex_ssid_len; i += 2) {
5309 sscanf(hex_ssid + i, "%02x", &hex);
5313 connman_network_set_blob(service->network,
5314 "WiFi.SSID", ssid, hex_ssid_len / 2);
5321 case CONNMAN_SERVICE_TYPE_WIMAX:
5322 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5323 case CONNMAN_SERVICE_TYPE_CELLULAR:
5324 service->favorite = g_key_file_get_boolean(keyfile,
5325 service->identifier, "Favorite", NULL);
5327 autoconnect = g_key_file_get_boolean(keyfile,
5328 service->identifier, "AutoConnect", &error);
5330 service->autoconnect = autoconnect;
5331 g_clear_error(&error);
5333 str = g_key_file_get_string(keyfile,
5334 service->identifier, "Failure", NULL);
5336 if (service->favorite == FALSE)
5337 service->state_ipv4 = service->state_ipv6 =
5338 CONNMAN_SERVICE_STATE_FAILURE;
5339 service->error = string2error(str);
5344 str = g_key_file_get_string(keyfile,
5345 service->identifier, "Modified", NULL);
5347 g_time_val_from_iso8601(str, &service->modified);
5351 str = g_key_file_get_string(keyfile,
5352 service->identifier, "Passphrase", NULL);
5354 g_free(service->passphrase);
5355 service->passphrase = str;
5358 if (service->ipconfig_ipv4 != NULL)
5359 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
5360 service->identifier, "IPv4.");
5362 if (service->ipconfig_ipv6 != NULL)
5363 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
5364 service->identifier, "IPv6.");
5366 service->nameservers_config = g_key_file_get_string_list(keyfile,
5367 service->identifier, "Nameservers", &length, NULL);
5368 if (service->nameservers_config != NULL && length == 0) {
5369 g_strfreev(service->nameservers_config);
5370 service->nameservers_config = NULL;
5373 service->domains = g_key_file_get_string_list(keyfile,
5374 service->identifier, "Domains", &length, NULL);
5375 if (service->domains != NULL && length == 0) {
5376 g_strfreev(service->domains);
5377 service->domains = NULL;
5380 str = g_key_file_get_string(keyfile,
5381 service->identifier, "Proxy.Method", NULL);
5383 service->proxy_config = string2proxymethod(str);
5387 service->proxies = g_key_file_get_string_list(keyfile,
5388 service->identifier, "Proxy.Servers", &length, NULL);
5389 if (service->proxies != NULL && length == 0) {
5390 g_strfreev(service->proxies);
5391 service->proxies = NULL;
5394 service->excludes = g_key_file_get_string_list(keyfile,
5395 service->identifier, "Proxy.Excludes", &length, NULL);
5396 if (service->excludes != NULL && length == 0) {
5397 g_strfreev(service->excludes);
5398 service->excludes = NULL;
5401 str = g_key_file_get_string(keyfile,
5402 service->identifier, "Proxy.URL", NULL);
5404 g_free(service->pac);
5409 g_key_file_free(keyfile);
5414 static int service_save(struct connman_service *service)
5416 const char *ident = "default";
5418 gchar *pathname, *data = NULL;
5421 const char *cst_str = NULL;
5424 DBG("service %p", service);
5429 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
5430 if (pathname == NULL)
5433 keyfile = g_key_file_new();
5435 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
5439 if (g_key_file_load_from_data(keyfile, data, length,
5447 if (service->name != NULL)
5448 g_key_file_set_string(keyfile, service->identifier,
5449 "Name", service->name);
5451 switch (service->type) {
5452 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5453 case CONNMAN_SERVICE_TYPE_SYSTEM:
5454 case CONNMAN_SERVICE_TYPE_ETHERNET:
5455 case CONNMAN_SERVICE_TYPE_GPS:
5456 case CONNMAN_SERVICE_TYPE_VPN:
5457 case CONNMAN_SERVICE_TYPE_GADGET:
5459 case CONNMAN_SERVICE_TYPE_WIFI:
5460 if (service->network) {
5461 const unsigned char *ssid;
5462 unsigned int ssid_len = 0;
5464 ssid = connman_network_get_blob(service->network,
5465 "WiFi.SSID", &ssid_len);
5467 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
5468 char *identifier = service->identifier;
5472 str = g_string_sized_new(ssid_len * 2);
5478 for (i = 0; i < ssid_len; i++)
5479 g_string_append_printf(str,
5482 g_key_file_set_string(keyfile, identifier,
5485 g_string_free(str, TRUE);
5490 case CONNMAN_SERVICE_TYPE_WIMAX:
5491 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5492 case CONNMAN_SERVICE_TYPE_CELLULAR:
5493 g_key_file_set_boolean(keyfile, service->identifier,
5494 "Favorite", service->favorite);
5496 if (service->favorite == TRUE)
5497 g_key_file_set_boolean(keyfile, service->identifier,
5498 "AutoConnect", service->autoconnect);
5500 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_FAILURE ||
5501 service->state_ipv6 == CONNMAN_SERVICE_STATE_FAILURE) {
5502 const char *failure = error2string(service->error);
5503 if (failure != NULL)
5504 g_key_file_set_string(keyfile,
5505 service->identifier,
5506 "Failure", failure);
5508 g_key_file_remove_key(keyfile, service->identifier,
5514 str = g_time_val_to_iso8601(&service->modified);
5516 g_key_file_set_string(keyfile, service->identifier,
5521 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
5522 g_key_file_set_string(keyfile, service->identifier,
5523 "Passphrase", service->passphrase);
5525 g_key_file_remove_key(keyfile, service->identifier,
5526 "Passphrase", NULL);
5528 switch (service->state) {
5529 case CONNMAN_SERVICE_STATE_UNKNOWN:
5530 case CONNMAN_SERVICE_STATE_IDLE:
5531 case CONNMAN_SERVICE_STATE_ASSOCIATION:
5533 case CONNMAN_SERVICE_STATE_CONFIGURATION:
5534 case CONNMAN_SERVICE_STATE_READY:
5535 case CONNMAN_SERVICE_STATE_ONLINE:
5536 case CONNMAN_SERVICE_STATE_DISCONNECT:
5537 case CONNMAN_SERVICE_STATE_FAILURE:
5538 if (service->ipconfig_ipv4 != NULL)
5539 __connman_ipconfig_save(service->ipconfig_ipv4,
5540 keyfile, service->identifier,
5543 if (service->ipconfig_ipv6 != NULL)
5544 __connman_ipconfig_save(service->ipconfig_ipv6,
5545 keyfile, service->identifier,
5549 if (service->nameservers_config != NULL) {
5550 guint len = g_strv_length(service->nameservers_config);
5552 g_key_file_set_string_list(keyfile, service->identifier,
5554 (const gchar **) service->nameservers_config, len);
5556 g_key_file_remove_key(keyfile, service->identifier,
5557 "Nameservers", NULL);
5559 if (service->domains != NULL) {
5560 guint len = g_strv_length(service->domains);
5562 g_key_file_set_string_list(keyfile, service->identifier,
5564 (const gchar **) service->domains, len);
5566 g_key_file_remove_key(keyfile, service->identifier,
5569 cst_str = proxymethod2string(service->proxy_config);
5570 if (cst_str != NULL)
5571 g_key_file_set_string(keyfile, service->identifier,
5572 "Proxy.Method", cst_str);
5574 if (service->proxies != NULL) {
5575 guint len = g_strv_length(service->proxies);
5577 g_key_file_set_string_list(keyfile, service->identifier,
5579 (const gchar **) service->proxies, len);
5581 g_key_file_remove_key(keyfile, service->identifier,
5582 "Proxy.Servers", NULL);
5584 if (service->excludes != NULL) {
5585 guint len = g_strv_length(service->excludes);
5587 g_key_file_set_string_list(keyfile, service->identifier,
5589 (const gchar **) service->excludes, len);
5591 g_key_file_remove_key(keyfile, service->identifier,
5592 "Proxy.Excludes", NULL);
5594 if (service->pac != NULL && strlen(service->pac) > 0)
5595 g_key_file_set_string(keyfile, service->identifier,
5596 "Proxy.URL", service->pac);
5598 g_key_file_remove_key(keyfile, service->identifier,
5601 data = g_key_file_to_data(keyfile, &length, NULL);
5603 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
5604 connman_error("Failed to store service information");
5609 g_key_file_free(keyfile);
5616 static struct connman_storage service_storage = {
5618 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
5619 .service_load = service_load,
5620 .service_save = service_save,
5623 int __connman_service_init(void)
5627 connection = connman_dbus_get_connection();
5629 if (connman_storage_register(&service_storage) < 0)
5630 connman_error("Failed to register service storage");
5632 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
5635 service_list = g_sequence_new(service_free);
5640 void __connman_service_cleanup(void)
5646 list = service_list;
5647 service_list = NULL;
5648 g_sequence_free(list);
5650 g_hash_table_destroy(service_hash);
5651 service_hash = NULL;
5653 g_slist_free(counter_list);
5654 counter_list = NULL;
5656 connman_storage_unregister(&service_storage);
5658 dbus_connection_unref(connection);