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;
80 connman_bool_t roaming;
81 connman_bool_t login_required;
82 connman_bool_t network_created;
83 struct connman_ipconfig *ipconfig_ipv4;
84 struct connman_ipconfig *ipconfig_ipv6;
85 struct connman_network *network;
86 struct connman_provider *provider;
88 char **nameservers_config;
92 /* 802.1x settings from the config files */
97 char *client_cert_file;
98 char *private_key_file;
99 char *private_key_passphrase;
101 DBusMessage *pending;
103 struct connman_location *location;
104 struct connman_stats stats;
105 struct connman_stats stats_roaming;
106 GHashTable *counter_table;
107 enum connman_service_proxy_method proxy;
108 enum connman_service_proxy_method proxy_config;
115 static void append_path(gpointer value, gpointer user_data)
117 struct connman_service *service = value;
118 DBusMessageIter *iter = user_data;
120 if (service->path == NULL || service->hidden == TRUE)
123 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
127 void __connman_service_list(DBusMessageIter *iter, void *user_data)
129 if (service_list == NULL)
132 g_sequence_foreach(service_list, append_path, iter);
137 struct connman_service *service;
140 static void compare_path(gpointer value, gpointer user_data)
142 struct connman_service *service = value;
143 struct find_data *data = user_data;
145 if (data->service != NULL)
148 if (g_strcmp0(service->path, data->path) == 0)
149 data->service = service;
152 static struct connman_service *find_service(const char *path)
154 struct find_data data = { .path = path, .service = NULL };
156 DBG("path %s", path);
158 g_sequence_foreach(service_list, compare_path, &data);
163 const char *__connman_service_type2string(enum connman_service_type type)
166 case CONNMAN_SERVICE_TYPE_UNKNOWN:
168 case CONNMAN_SERVICE_TYPE_SYSTEM:
170 case CONNMAN_SERVICE_TYPE_ETHERNET:
172 case CONNMAN_SERVICE_TYPE_WIFI:
174 case CONNMAN_SERVICE_TYPE_WIMAX:
176 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
178 case CONNMAN_SERVICE_TYPE_CELLULAR:
180 case CONNMAN_SERVICE_TYPE_GPS:
182 case CONNMAN_SERVICE_TYPE_VPN:
184 case CONNMAN_SERVICE_TYPE_GADGET:
191 static const char *security2string(enum connman_service_security security)
194 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
196 case CONNMAN_SERVICE_SECURITY_NONE:
198 case CONNMAN_SERVICE_SECURITY_WEP:
200 case CONNMAN_SERVICE_SECURITY_PSK:
202 case CONNMAN_SERVICE_SECURITY_8021X:
204 case CONNMAN_SERVICE_SECURITY_WPA:
206 case CONNMAN_SERVICE_SECURITY_RSN:
213 static const char *state2string(enum connman_service_state state)
216 case CONNMAN_SERVICE_STATE_UNKNOWN:
218 case CONNMAN_SERVICE_STATE_IDLE:
220 case CONNMAN_SERVICE_STATE_ASSOCIATION:
221 return "association";
222 case CONNMAN_SERVICE_STATE_CONFIGURATION:
223 return "configuration";
224 case CONNMAN_SERVICE_STATE_READY:
226 case CONNMAN_SERVICE_STATE_ONLINE:
228 case CONNMAN_SERVICE_STATE_DISCONNECT:
230 case CONNMAN_SERVICE_STATE_FAILURE:
237 static const char *error2string(enum connman_service_error error)
240 case CONNMAN_SERVICE_ERROR_UNKNOWN:
242 case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
243 return "out-of-range";
244 case CONNMAN_SERVICE_ERROR_PIN_MISSING:
245 return "pin-missing";
246 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
247 return "dhcp-failed";
248 case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
249 return "connect-failed";
250 case CONNMAN_SERVICE_ERROR_LOGIN_FAILED:
251 return "login-failed";
252 case CONNMAN_SERVICE_ERROR_AUTH_FAILED:
253 return "auth-failed";
254 case CONNMAN_SERVICE_ERROR_INVALID_KEY:
255 return "invalid-key";
261 static enum connman_service_error string2error(const char *error)
263 if (g_strcmp0(error, "dhcp-failed") == 0)
264 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
265 else if (g_strcmp0(error, "pin-missing") == 0)
266 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
267 else if (g_strcmp0(error, "invalid-key") == 0)
268 return CONNMAN_SERVICE_ERROR_INVALID_KEY;
270 return CONNMAN_SERVICE_ERROR_UNKNOWN;
273 static const char *proxymethod2string(enum connman_service_proxy_method method)
276 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
278 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
280 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
282 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
289 static enum connman_service_proxy_method string2proxymethod(const char *method)
291 if (g_strcmp0(method, "direct") == 0)
292 return CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
293 else if (g_strcmp0(method, "auto") == 0)
294 return CONNMAN_SERVICE_PROXY_METHOD_AUTO;
295 else if (g_strcmp0(method, "manual") == 0)
296 return CONNMAN_SERVICE_PROXY_METHOD_MANUAL;
298 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
301 static enum connman_service_state combine_state(
302 enum connman_service_state state_a,
303 enum connman_service_state state_b)
305 enum connman_service_state result;
307 if (state_a == state_b) {
312 if (state_a == CONNMAN_SERVICE_STATE_UNKNOWN) {
317 if (state_b == CONNMAN_SERVICE_STATE_UNKNOWN) {
322 if (state_a == CONNMAN_SERVICE_STATE_IDLE) {
327 if (state_b == CONNMAN_SERVICE_STATE_IDLE) {
332 if (state_a == CONNMAN_SERVICE_STATE_ASSOCIATION) {
333 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION ||
334 state_b == CONNMAN_SERVICE_STATE_ONLINE ||
335 state_b == CONNMAN_SERVICE_STATE_READY)
342 if (state_b == CONNMAN_SERVICE_STATE_ASSOCIATION) {
343 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION ||
344 state_a == CONNMAN_SERVICE_STATE_ONLINE ||
345 state_a == CONNMAN_SERVICE_STATE_READY)
352 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION) {
353 if (state_b == CONNMAN_SERVICE_STATE_ONLINE ||
354 state_b == CONNMAN_SERVICE_STATE_READY)
361 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION) {
362 if (state_a == CONNMAN_SERVICE_STATE_ONLINE ||
363 state_a == CONNMAN_SERVICE_STATE_READY)
370 if (state_a == CONNMAN_SERVICE_STATE_READY) {
371 if (state_b == CONNMAN_SERVICE_STATE_ONLINE ||
372 state_b == CONNMAN_SERVICE_STATE_DISCONNECT)
379 if (state_b == CONNMAN_SERVICE_STATE_READY) {
380 if (state_a == CONNMAN_SERVICE_STATE_ONLINE ||
381 state_a == CONNMAN_SERVICE_STATE_DISCONNECT)
388 if (state_a == CONNMAN_SERVICE_STATE_ONLINE) {
389 if (state_b == CONNMAN_SERVICE_STATE_DISCONNECT)
396 if (state_b == CONNMAN_SERVICE_STATE_ONLINE) {
397 if (state_a == CONNMAN_SERVICE_STATE_DISCONNECT)
404 if (state_a == CONNMAN_SERVICE_STATE_DISCONNECT) {
409 if (state_b == CONNMAN_SERVICE_STATE_DISCONNECT) {
414 result = CONNMAN_SERVICE_STATE_FAILURE;
420 static connman_bool_t is_connecting_state(struct connman_service *service,
421 enum connman_service_state state)
424 case CONNMAN_SERVICE_STATE_UNKNOWN:
425 case CONNMAN_SERVICE_STATE_IDLE:
426 case CONNMAN_SERVICE_STATE_FAILURE:
427 if (service->network != NULL)
428 return connman_network_get_connecting(service->network);
429 case CONNMAN_SERVICE_STATE_DISCONNECT:
430 case CONNMAN_SERVICE_STATE_READY:
431 case CONNMAN_SERVICE_STATE_ONLINE:
433 case CONNMAN_SERVICE_STATE_ASSOCIATION:
434 case CONNMAN_SERVICE_STATE_CONFIGURATION:
441 static connman_bool_t is_connected_state(const struct connman_service *service,
442 enum connman_service_state state)
445 case CONNMAN_SERVICE_STATE_UNKNOWN:
446 case CONNMAN_SERVICE_STATE_IDLE:
447 case CONNMAN_SERVICE_STATE_ASSOCIATION:
448 case CONNMAN_SERVICE_STATE_CONFIGURATION:
449 case CONNMAN_SERVICE_STATE_DISCONNECT:
450 case CONNMAN_SERVICE_STATE_FAILURE:
452 case CONNMAN_SERVICE_STATE_READY:
453 case CONNMAN_SERVICE_STATE_ONLINE:
460 static connman_bool_t is_connecting(struct connman_service *service)
462 return is_connecting_state(service, service->state);
465 static connman_bool_t is_connected(struct connman_service *service)
467 return is_connected_state(service, service->state);
470 static void update_nameservers(struct connman_service *service)
474 if (service->ipconfig_ipv4)
475 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv4);
476 else if (service->ipconfig_ipv6)
477 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv6);
484 switch (combine_state(service->state_ipv4, service->state_ipv6)) {
485 case CONNMAN_SERVICE_STATE_UNKNOWN:
486 case CONNMAN_SERVICE_STATE_IDLE:
487 case CONNMAN_SERVICE_STATE_ASSOCIATION:
488 case CONNMAN_SERVICE_STATE_CONFIGURATION:
490 case CONNMAN_SERVICE_STATE_FAILURE:
491 case CONNMAN_SERVICE_STATE_DISCONNECT:
492 connman_resolver_remove_all(ifname);
494 case CONNMAN_SERVICE_STATE_READY:
495 case CONNMAN_SERVICE_STATE_ONLINE:
499 connman_resolver_remove_all(ifname);
501 if (service->nameservers_config != NULL) {
504 for (i = 0; service->nameservers_config[i] != NULL; i++) {
505 connman_resolver_append(ifname, NULL,
506 service->nameservers_config[i]);
508 } else if (service->nameservers != NULL) {
511 for (i = 0; service->nameservers[i] != NULL; i++) {
512 connman_resolver_append(ifname, NULL,
513 service->nameservers[i]);
517 if (service->domains != NULL) {
520 for (i = 0; service->domains[i]; i++)
521 connman_resolver_append(ifname, service->domains[i],
523 } else if (service->domainname != NULL)
524 connman_resolver_append(ifname, service->domainname, NULL);
526 connman_resolver_flush();
529 int __connman_service_nameserver_append(struct connman_service *service,
530 const char *nameserver)
534 DBG("service %p nameserver %s", service, nameserver);
536 if (nameserver == NULL)
539 if (service->nameservers != NULL) {
542 for (i = 0; service->nameservers[i] != NULL; i++)
543 if (g_strcmp0(service->nameservers[i], nameserver) == 0)
546 len = g_strv_length(service->nameservers);
547 service->nameservers = g_try_renew(char *, service->nameservers,
551 service->nameservers = g_try_new0(char *, len + 2);
554 if (service->nameservers == NULL)
557 service->nameservers[len] = g_strdup(nameserver);
558 service->nameservers[len + 1] = NULL;
560 update_nameservers(service);
565 int __connman_service_nameserver_remove(struct connman_service *service,
566 const char *nameserver)
571 DBG("service %p nameserver %s", service, nameserver);
573 if (nameserver == NULL)
576 if (service->nameservers == NULL)
579 len = g_strv_length(service->nameservers);
581 if (g_strcmp0(service->nameservers[0], nameserver) != 0)
584 g_strfreev(service->nameservers);
585 service->nameservers = NULL;
590 servers = g_try_new0(char *, len);
594 for (i = 0, j = 0; i < len; i++) {
595 if (g_strcmp0(service->nameservers[i], nameserver) != 0) {
596 servers[j] = g_strdup(service->nameservers[i]);
600 servers[len - 1] = NULL;
602 g_strfreev(service->nameservers);
603 service->nameservers = servers;
605 update_nameservers(service);
610 void __connman_service_nameserver_clear(struct connman_service *service)
612 g_strfreev(service->nameservers);
613 service->nameservers = NULL;
615 update_nameservers(service);
618 static void nameserver_add_routes(int index, char **nameservers,
622 struct addrinfo hints;
623 struct addrinfo *addr;
625 for (i = 0; nameservers[i] != NULL; i++) {
626 memset(&hints, 0, sizeof(struct addrinfo));
627 hints.ai_flags = AI_NUMERICHOST;
630 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
631 if (ret == EAI_NONAME)
632 family = AF_INET; /* use the IPv4 as a default */
636 family = addr->ai_family;
638 if (family == AF_INET) {
639 if (connman_inet_compare_subnet(index,
640 nameservers[i]) != TRUE)
641 connman_inet_add_host_route(index,
643 } else if (family == AF_INET6)
644 connman_inet_add_ipv6_host_route(index,
651 static void nameserver_del_routes(int index, char **nameservers)
654 struct addrinfo hints;
655 struct addrinfo *addr;
657 for (i = 0; nameservers[i] != NULL; i++) {
658 memset(&hints, 0, sizeof(struct addrinfo));
659 hints.ai_flags = AI_NUMERICHOST;
662 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
663 if (ret == EAI_NONAME)
664 family = AF_INET; /* use the IPv4 as a default */
668 family = addr->ai_family;
670 if (family == AF_INET)
671 connman_inet_del_host_route(index, nameservers[i]);
672 else if (family == AF_INET6)
673 connman_inet_del_ipv6_host_route(index,
680 void __connman_service_nameserver_add_routes(struct connman_service *service,
688 if (service->network != NULL)
689 index = connman_network_get_index(service->network);
690 else if (service->provider != NULL)
691 index = connman_provider_get_index(service->provider);
693 if (service->nameservers_config != NULL) {
695 * Configured nameserver takes preference over the
696 * discoverd nameserver gathered from DHCP, VPN, etc.
698 nameserver_add_routes(index, service->nameservers_config, gw);
699 } else if (service->nameservers != NULL) {
701 * We add nameservers host routes for nameservers that
702 * are not on our subnet. For those who are, the subnet
703 * route will be installed by the time the dns proxy code
704 * tries to reach them. The subnet route is installed
705 * when setting the interface IP address.
707 nameserver_add_routes(index, service->nameservers, gw);
711 void __connman_service_nameserver_del_routes(struct connman_service *service)
718 if (service->network != NULL)
719 index = connman_network_get_index(service->network);
720 else if (service->provider != NULL)
721 index = connman_provider_get_index(service->provider);
723 if (service->nameservers_config != NULL)
724 nameserver_del_routes(index, service->nameservers_config);
725 else if (service->nameservers != NULL)
726 nameserver_del_routes(index, service->nameservers);
729 static struct connman_stats *stats_get(struct connman_service *service)
731 if (service->roaming == TRUE)
732 return &service->stats_roaming;
734 return &service->stats;
737 static connman_bool_t stats_enabled(struct connman_service *service)
739 struct connman_stats *stats = stats_get(service);
741 return stats->enabled;
744 static void stats_start(struct connman_service *service)
746 struct connman_stats *stats = stats_get(service);
748 DBG("service %p", service);
750 if (stats->timer == NULL)
753 stats->enabled = TRUE;
754 stats->data_last.time = stats->data.time;
756 g_timer_start(stats->timer);
759 static void stats_stop(struct connman_service *service)
761 struct connman_stats *stats = stats_get(service);
762 unsigned int seconds;
764 DBG("service %p", service);
766 if (stats->timer == NULL)
769 if (stats->enabled == FALSE)
772 g_timer_stop(stats->timer);
774 seconds = g_timer_elapsed(stats->timer, NULL);
775 stats->data.time = stats->data_last.time + seconds;
777 stats->enabled = FALSE;
780 static void reset_stats(struct connman_service *service)
782 DBG("service %p", service);
785 service->stats.valid = FALSE;
787 service->stats.data.rx_packets = 0;
788 service->stats.data.tx_packets = 0;
789 service->stats.data.rx_bytes = 0;
790 service->stats.data.tx_bytes = 0;
791 service->stats.data.rx_errors = 0;
792 service->stats.data.tx_errors = 0;
793 service->stats.data.rx_dropped = 0;
794 service->stats.data.tx_dropped = 0;
795 service->stats.data.time = 0;
796 service->stats.data_last.time = 0;
798 g_timer_reset(service->stats.timer);
801 service->stats_roaming.valid = FALSE;
803 service->stats_roaming.data.rx_packets = 0;
804 service->stats_roaming.data.tx_packets = 0;
805 service->stats_roaming.data.rx_bytes = 0;
806 service->stats_roaming.data.tx_bytes = 0;
807 service->stats_roaming.data.rx_errors = 0;
808 service->stats_roaming.data.tx_errors = 0;
809 service->stats_roaming.data.rx_dropped = 0;
810 service->stats_roaming.data.tx_dropped = 0;
811 service->stats_roaming.data.time = 0;
812 service->stats_roaming.data_last.time = 0;
814 g_timer_reset(service->stats_roaming.timer);
817 static struct connman_service *get_default(void)
819 struct connman_service *service;
822 iter = g_sequence_get_begin_iter(service_list);
824 if (g_sequence_iter_is_end(iter) == TRUE)
827 service = g_sequence_get(iter);
829 if (is_connected(service) == FALSE)
835 static void default_changed(void)
837 struct connman_service *service = get_default();
839 __connman_notifier_default_changed(service);
842 const char *__connman_service_default(void)
844 struct connman_service *service;
846 service = get_default();
850 return __connman_service_type2string(service->type);
853 static void state_changed(struct connman_service *service)
857 __connman_notifier_service_state_changed(service, service->state);
859 str = state2string(service->state);
863 connman_dbus_property_changed_basic(service->path,
864 CONNMAN_SERVICE_INTERFACE, "State",
865 DBUS_TYPE_STRING, &str);
868 static void strength_changed(struct connman_service *service)
870 if (service->strength == 0)
873 connman_dbus_property_changed_basic(service->path,
874 CONNMAN_SERVICE_INTERFACE, "Strength",
875 DBUS_TYPE_BYTE, &service->strength);
878 static void favorite_changed(struct connman_service *service)
880 if (service->path == NULL)
883 connman_dbus_property_changed_basic(service->path,
884 CONNMAN_SERVICE_INTERFACE, "Favorite",
885 DBUS_TYPE_BOOLEAN, &service->favorite);
888 static void immutable_changed(struct connman_service *service)
890 if (service->path == NULL)
893 connman_dbus_property_changed_basic(service->path,
894 CONNMAN_SERVICE_INTERFACE, "Immutable",
895 DBUS_TYPE_BOOLEAN, &service->immutable);
898 static void roaming_changed(struct connman_service *service)
900 if (service->path == NULL)
903 connman_dbus_property_changed_basic(service->path,
904 CONNMAN_SERVICE_INTERFACE, "Roaming",
905 DBUS_TYPE_BOOLEAN, &service->roaming);
908 static void autoconnect_changed(struct connman_service *service)
910 if (service->path == NULL)
913 connman_dbus_property_changed_basic(service->path,
914 CONNMAN_SERVICE_INTERFACE, "AutoConnect",
915 DBUS_TYPE_BOOLEAN, &service->autoconnect);
918 static void passphrase_changed(struct connman_service *service)
920 dbus_bool_t required;
922 switch (service->type) {
923 case CONNMAN_SERVICE_TYPE_UNKNOWN:
924 case CONNMAN_SERVICE_TYPE_SYSTEM:
925 case CONNMAN_SERVICE_TYPE_ETHERNET:
926 case CONNMAN_SERVICE_TYPE_WIMAX:
927 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
928 case CONNMAN_SERVICE_TYPE_CELLULAR:
929 case CONNMAN_SERVICE_TYPE_GPS:
930 case CONNMAN_SERVICE_TYPE_VPN:
931 case CONNMAN_SERVICE_TYPE_GADGET:
933 case CONNMAN_SERVICE_TYPE_WIFI:
936 switch (service->security) {
937 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
938 case CONNMAN_SERVICE_SECURITY_NONE:
940 case CONNMAN_SERVICE_SECURITY_WEP:
941 case CONNMAN_SERVICE_SECURITY_PSK:
942 case CONNMAN_SERVICE_SECURITY_WPA:
943 case CONNMAN_SERVICE_SECURITY_RSN:
944 if (service->passphrase == NULL)
947 case CONNMAN_SERVICE_SECURITY_8021X:
953 connman_dbus_property_changed_basic(service->path,
954 CONNMAN_SERVICE_INTERFACE, "PassphraseRequired",
955 DBUS_TYPE_BOOLEAN, &required);
958 static void login_changed(struct connman_service *service)
960 dbus_bool_t required = service->login_required;
962 if (service->path == NULL)
965 connman_dbus_property_changed_basic(service->path,
966 CONNMAN_SERVICE_INTERFACE, "LoginRequired",
967 DBUS_TYPE_BOOLEAN, &required);
970 static void append_security(DBusMessageIter *iter, void *user_data)
972 struct connman_service *service = user_data;
975 str = security2string(service->security);
977 dbus_message_iter_append_basic(iter,
978 DBUS_TYPE_STRING, &str);
981 if (service->wps == TRUE)
982 dbus_message_iter_append_basic(iter,
983 DBUS_TYPE_STRING, &str);
986 static void append_ethernet(DBusMessageIter *iter, void *user_data)
988 struct connman_service *service = user_data;
990 if (service->ipconfig_ipv4 != NULL)
991 __connman_ipconfig_append_ethernet(service->ipconfig_ipv4,
993 else if (service->ipconfig_ipv6 != NULL)
994 __connman_ipconfig_append_ethernet(service->ipconfig_ipv6,
998 static void append_ipv4(DBusMessageIter *iter, void *user_data)
1000 struct connman_service *service = user_data;
1002 DBG("ipv4 %p state %s", service->ipconfig_ipv4,
1003 state2string(service->state_ipv4));
1005 if (is_connected_state(service, service->state_ipv4) == FALSE)
1008 if (service->ipconfig_ipv4 != NULL)
1009 __connman_ipconfig_append_ipv4(service->ipconfig_ipv4, iter);
1012 static void append_ipv6(DBusMessageIter *iter, void *user_data)
1014 struct connman_service *service = user_data;
1016 DBG("ipv6 %p state %s", service->ipconfig_ipv6,
1017 state2string(service->state_ipv6));
1019 if (is_connected_state(service, service->state_ipv6) == FALSE)
1022 if (service->ipconfig_ipv6 != NULL)
1023 __connman_ipconfig_append_ipv6(service->ipconfig_ipv6, iter,
1024 service->ipconfig_ipv4);
1027 static void append_ipv4config(DBusMessageIter *iter, void *user_data)
1029 struct connman_service *service = user_data;
1031 if (service->ipconfig_ipv4 != NULL)
1032 __connman_ipconfig_append_ipv4config(service->ipconfig_ipv4,
1036 static void append_ipv6config(DBusMessageIter *iter, void *user_data)
1038 struct connman_service *service = user_data;
1040 if (service->ipconfig_ipv6 != NULL)
1041 __connman_ipconfig_append_ipv6config(service->ipconfig_ipv6,
1045 static void append_nameserver(DBusMessageIter *iter, char ***nameservers)
1050 servers = *nameservers;
1052 for (i = 0; servers[i] != NULL; i++) {
1053 dbus_message_iter_append_basic(iter,
1054 DBUS_TYPE_STRING, &servers[i]);
1058 static void append_dns(DBusMessageIter *iter, void *user_data)
1060 struct connman_service *service = user_data;
1062 if (is_connected(service) == FALSE)
1065 if (service->nameservers_config != NULL) {
1066 append_nameserver(iter, &service->nameservers_config);
1068 } else if (service->nameservers != NULL) {
1069 append_nameserver(iter, &service->nameservers);
1074 static void append_dnsconfig(DBusMessageIter *iter, void *user_data)
1076 struct connman_service *service = user_data;
1079 if (service->nameservers_config == NULL)
1082 for (i = 0; service->nameservers_config[i]; i++) {
1083 dbus_message_iter_append_basic(iter,
1085 &service->nameservers_config[i]);
1089 static void append_domain(DBusMessageIter *iter, void *user_data)
1091 struct connman_service *service = user_data;
1093 if (is_connected(service) == FALSE &&
1094 is_connecting(service) == FALSE)
1097 if (service->domainname == NULL)
1100 dbus_message_iter_append_basic(iter,
1101 DBUS_TYPE_STRING, &service->domainname);
1104 static void append_domainconfig(DBusMessageIter *iter, void *user_data)
1106 struct connman_service *service = user_data;
1109 if (service->domains == NULL)
1112 for (i = 0; service->domains[i]; i++)
1113 dbus_message_iter_append_basic(iter,
1114 DBUS_TYPE_STRING, &service->domains[i]);
1117 static void append_proxies(DBusMessageIter *iter, void *user_data)
1119 struct connman_service *service = user_data;
1122 if (service->proxies == NULL)
1125 for (i = 0; service->proxies[i]; i++)
1126 dbus_message_iter_append_basic(iter,
1127 DBUS_TYPE_STRING, &service->proxies[i]);
1130 static void append_excludes(DBusMessageIter *iter, void *user_data)
1132 struct connman_service *service = user_data;
1135 if (service->excludes == NULL)
1138 for (i = 0; service->excludes[i]; i++)
1139 dbus_message_iter_append_basic(iter,
1140 DBUS_TYPE_STRING, &service->excludes[i]);
1143 static void append_proxy(DBusMessageIter *iter, void *user_data)
1145 struct connman_service *service = user_data;
1146 enum connman_service_proxy_method proxy;
1147 const char *pac = NULL;
1148 const char *method = proxymethod2string(
1149 CONNMAN_SERVICE_PROXY_METHOD_DIRECT);
1153 if (is_connected(service) == FALSE)
1156 proxy = connman_service_get_proxy_method(service);
1159 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1161 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1163 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1164 connman_dbus_dict_append_array(iter, "Servers",
1165 DBUS_TYPE_STRING, append_proxies,
1168 connman_dbus_dict_append_array(iter, "Excludes",
1169 DBUS_TYPE_STRING, append_excludes,
1172 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1173 /* Maybe DHCP, or WPAD, has provided an url for a pac file */
1174 if (service->ipconfig_ipv4 != NULL)
1175 pac = __connman_ipconfig_get_proxy_autoconfig(
1176 service->ipconfig_ipv4);
1177 else if (service->ipconfig_ipv6 != NULL)
1178 pac = __connman_ipconfig_get_proxy_autoconfig(
1179 service->ipconfig_ipv6);
1181 if (service->pac == NULL && pac == NULL)
1184 if (service->pac != NULL)
1187 connman_dbus_dict_append_basic(iter, "URL",
1188 DBUS_TYPE_STRING, &pac);
1192 method = proxymethod2string(proxy);
1195 connman_dbus_dict_append_basic(iter, "Method",
1196 DBUS_TYPE_STRING, &method);
1199 static void append_proxyconfig(DBusMessageIter *iter, void *user_data)
1201 struct connman_service *service = user_data;
1204 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
1207 switch (service->proxy_config) {
1208 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1210 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1212 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1213 if (service->proxies != NULL)
1214 connman_dbus_dict_append_array(iter, "Servers",
1216 append_proxies, service);
1218 if (service->excludes != NULL)
1219 connman_dbus_dict_append_array(iter, "Excludes",
1221 append_excludes, service);
1223 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1224 if (service->pac != NULL)
1225 connman_dbus_dict_append_basic(iter, "URL",
1226 DBUS_TYPE_STRING, &service->pac);
1230 method = proxymethod2string(service->proxy_config);
1232 connman_dbus_dict_append_basic(iter, "Method",
1233 DBUS_TYPE_STRING, &method);
1236 static void append_provider(DBusMessageIter *iter, void *user_data)
1238 struct connman_service *service = user_data;
1240 DBG("%p %p", service, service->provider);
1242 if (is_connected(service) == FALSE)
1245 if (service->provider != NULL)
1246 __connman_provider_append_properties(service->provider, iter);
1250 static void settings_changed(struct connman_service *service,
1251 struct connman_ipconfig *ipconfig)
1253 connman_dbus_property_changed_dict(service->path,
1254 CONNMAN_SERVICE_INTERFACE, "IPv4",
1255 append_ipv4, service);
1257 connman_dbus_property_changed_dict(service->path,
1258 CONNMAN_SERVICE_INTERFACE, "IPv6",
1259 append_ipv6, service);
1261 __connman_notifier_ipconfig_changed(service, ipconfig);
1264 static void ipv4_configuration_changed(struct connman_service *service)
1266 connman_dbus_property_changed_dict(service->path,
1267 CONNMAN_SERVICE_INTERFACE,
1268 "IPv4.Configuration",
1273 static void ipv6_configuration_changed(struct connman_service *service)
1275 connman_dbus_property_changed_dict(service->path,
1276 CONNMAN_SERVICE_INTERFACE,
1277 "IPv6.Configuration",
1282 static void dns_changed(struct connman_service *service)
1284 connman_dbus_property_changed_array(service->path,
1285 CONNMAN_SERVICE_INTERFACE, "Nameservers",
1286 DBUS_TYPE_STRING, append_dns, service);
1289 static void dns_configuration_changed(struct connman_service *service)
1291 connman_dbus_property_changed_array(service->path,
1292 CONNMAN_SERVICE_INTERFACE,
1293 "Nameservers.Configuration",
1294 DBUS_TYPE_STRING, append_dnsconfig, service);
1296 dns_changed(service);
1299 static void domain_changed(struct connman_service *service)
1301 connman_dbus_property_changed_array(service->path,
1302 CONNMAN_SERVICE_INTERFACE, "Domains",
1303 DBUS_TYPE_STRING, append_domain, service);
1306 static void domain_configuration_changed(struct connman_service *service)
1308 connman_dbus_property_changed_array(service->path,
1309 CONNMAN_SERVICE_INTERFACE,
1310 "Domains.Configuration",
1311 DBUS_TYPE_STRING, append_domainconfig, service);
1314 static void proxy_changed(struct connman_service *service)
1316 connman_dbus_property_changed_dict(service->path,
1317 CONNMAN_SERVICE_INTERFACE, "Proxy",
1318 append_proxy, service);
1321 static void proxy_configuration_changed(struct connman_service *service)
1323 connman_dbus_property_changed_dict(service->path,
1324 CONNMAN_SERVICE_INTERFACE, "Proxy.Configuration",
1325 append_proxyconfig, service);
1327 proxy_changed(service);
1330 static void link_changed(struct connman_service *service)
1332 connman_dbus_property_changed_dict(service->path,
1333 CONNMAN_SERVICE_INTERFACE, "Ethernet",
1334 append_ethernet, service);
1337 static void stats_append_counters(DBusMessageIter *dict,
1338 struct connman_stats_data *stats,
1339 struct connman_stats_data *counters,
1340 connman_bool_t append_all)
1342 if (counters->rx_packets != stats->rx_packets || append_all) {
1343 counters->rx_packets = stats->rx_packets;
1344 connman_dbus_dict_append_basic(dict, "RX.Packets",
1345 DBUS_TYPE_UINT32, &stats->rx_packets);
1348 if (counters->tx_packets != stats->tx_packets || append_all) {
1349 counters->tx_packets = stats->tx_packets;
1350 connman_dbus_dict_append_basic(dict, "TX.Packets",
1351 DBUS_TYPE_UINT32, &stats->tx_packets);
1354 if (counters->rx_bytes != stats->rx_bytes || append_all) {
1355 counters->rx_bytes = stats->rx_bytes;
1356 connman_dbus_dict_append_basic(dict, "RX.Bytes",
1357 DBUS_TYPE_UINT32, &stats->rx_bytes);
1360 if (counters->tx_bytes != stats->tx_bytes || append_all) {
1361 counters->tx_bytes = stats->tx_bytes;
1362 connman_dbus_dict_append_basic(dict, "TX.Bytes",
1363 DBUS_TYPE_UINT32, &stats->tx_bytes);
1366 if (counters->rx_errors != stats->rx_errors || append_all) {
1367 counters->rx_errors = stats->rx_errors;
1368 connman_dbus_dict_append_basic(dict, "RX.Errors",
1369 DBUS_TYPE_UINT32, &stats->rx_errors);
1372 if (counters->tx_errors != stats->tx_errors || append_all) {
1373 counters->tx_errors = stats->tx_errors;
1374 connman_dbus_dict_append_basic(dict, "TX.Errors",
1375 DBUS_TYPE_UINT32, &stats->tx_errors);
1378 if (counters->rx_dropped != stats->rx_dropped || append_all) {
1379 counters->rx_dropped = stats->rx_dropped;
1380 connman_dbus_dict_append_basic(dict, "RX.Dropped",
1381 DBUS_TYPE_UINT32, &stats->rx_dropped);
1384 if (counters->tx_dropped != stats->tx_dropped || append_all) {
1385 counters->tx_dropped = stats->tx_dropped;
1386 connman_dbus_dict_append_basic(dict, "TX.Dropped",
1387 DBUS_TYPE_UINT32, &stats->tx_dropped);
1390 if (counters->time != stats->time || append_all) {
1391 counters->time = stats->time;
1392 connman_dbus_dict_append_basic(dict, "Time",
1393 DBUS_TYPE_UINT32, &stats->time);
1397 static void stats_append(struct connman_service *service,
1398 const char *counter,
1399 struct connman_stats_counter *counters,
1400 connman_bool_t append_all)
1402 DBusMessageIter array, dict;
1405 DBG("service %p counter %s", service, counter);
1407 msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1411 dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH,
1412 &service->path, DBUS_TYPE_INVALID);
1414 dbus_message_iter_init_append(msg, &array);
1417 connman_dbus_dict_open(&array, &dict);
1419 stats_append_counters(&dict, &service->stats.data,
1420 &counters->stats.data, append_all);
1422 connman_dbus_dict_close(&array, &dict);
1424 /* roaming counter */
1425 connman_dbus_dict_open(&array, &dict);
1427 stats_append_counters(&dict, &service->stats_roaming.data,
1428 &counters->stats_roaming.data, append_all);
1430 connman_dbus_dict_close(&array, &dict);
1432 __connman_counter_send_usage(counter, msg);
1435 static void stats_update(struct connman_service *service,
1436 unsigned int rx_packets, unsigned int tx_packets,
1437 unsigned int rx_bytes, unsigned int tx_bytes,
1438 unsigned int rx_errors, unsigned int tx_errors,
1439 unsigned int rx_dropped, unsigned int tx_dropped)
1441 struct connman_stats *stats = stats_get(service);
1442 struct connman_stats_data *data_last = &stats->data_last;
1443 struct connman_stats_data *data = &stats->data;
1444 unsigned int seconds;
1446 DBG("service %p", service);
1448 if (stats->valid == TRUE) {
1450 rx_packets - data_last->rx_packets;
1452 tx_packets - data_last->tx_packets;
1454 rx_bytes - data_last->rx_bytes;
1456 tx_bytes - data_last->tx_bytes;
1458 rx_errors - data_last->rx_errors;
1460 tx_errors - data_last->tx_errors;
1462 rx_dropped - data_last->rx_dropped;
1464 tx_dropped - data_last->tx_dropped;
1466 stats->valid = TRUE;
1469 data_last->rx_packets = rx_packets;
1470 data_last->tx_packets = tx_packets;
1471 data_last->rx_bytes = rx_bytes;
1472 data_last->tx_bytes = tx_bytes;
1473 data_last->rx_errors = rx_errors;
1474 data_last->tx_errors = tx_errors;
1475 data_last->rx_dropped = rx_dropped;
1476 data_last->tx_dropped = tx_dropped;
1478 seconds = g_timer_elapsed(stats->timer, NULL);
1479 stats->data.time = stats->data_last.time + seconds;
1482 static char *wifi_build_group_name(const unsigned char *ssid,
1483 unsigned int ssid_len,
1485 const char *security)
1490 /* the last 3 is for the 2 '_' and '\0' */
1491 str = g_string_sized_new((ssid_len * 2) + strlen(mode)
1492 + strlen(security) + 3);
1496 for (i = 0; i < ssid_len; i++)
1497 g_string_append_printf(str, "%02x", ssid[i]);
1499 g_string_append_printf(str, "_%s_%s", mode, security);
1501 return g_string_free(str, FALSE);
1504 void __connman_service_notify(struct connman_service *service,
1505 unsigned int rx_packets, unsigned int tx_packets,
1506 unsigned int rx_bytes, unsigned int tx_bytes,
1507 unsigned int rx_errors, unsigned int tx_errors,
1508 unsigned int rx_dropped, unsigned int tx_dropped)
1510 GHashTableIter iter;
1511 gpointer key, value;
1512 const char *counter;
1513 struct connman_stats_counter *counters;
1514 struct connman_stats_data *data;
1517 if (service == NULL)
1520 if (is_connected(service) == FALSE)
1523 stats_update(service,
1524 rx_packets, tx_packets,
1526 rx_errors, tx_errors,
1527 rx_dropped, tx_dropped);
1529 data = &stats_get(service)->data;
1530 err = __connman_stats_update(service, service->roaming, data);
1532 connman_error("Failed to store statistics for %s",
1533 service->identifier);
1535 g_hash_table_iter_init(&iter, service->counter_table);
1536 while (g_hash_table_iter_next(&iter, &key, &value)) {
1540 stats_append(service, counter, counters, counters->append_all);
1541 counters->append_all = FALSE;
1545 int __connman_service_counter_register(const char *counter)
1547 struct connman_service *service;
1548 GSequenceIter *iter;
1549 struct connman_stats_counter *counters;
1551 DBG("counter %s", counter);
1553 counter_list = g_slist_append(counter_list, (gpointer)counter);
1555 iter = g_sequence_get_begin_iter(service_list);
1557 while (g_sequence_iter_is_end(iter) == FALSE) {
1558 service = g_sequence_get(iter);
1560 counters = g_try_new0(struct connman_stats_counter, 1);
1561 if (counters == NULL)
1564 counters->append_all = TRUE;
1566 g_hash_table_replace(service->counter_table, (gpointer)counter,
1569 iter = g_sequence_iter_next(iter);
1575 void __connman_service_counter_unregister(const char *counter)
1577 struct connman_service *service;
1578 GSequenceIter *iter;
1580 DBG("counter %s", counter);
1582 iter = g_sequence_get_begin_iter(service_list);
1584 while (g_sequence_iter_is_end(iter) == FALSE) {
1585 service = g_sequence_get(iter);
1587 g_hash_table_remove(service->counter_table, counter);
1589 iter = g_sequence_iter_next(iter);
1592 counter_list = g_slist_remove(counter_list, counter);
1595 GSequence *__connman_service_get_list(struct connman_session *session,
1596 service_match_cb service_match,
1597 create_service_entry_cb create_service_entry,
1598 GDestroyNotify destroy_service_entry)
1601 GSequenceIter *iter;
1602 struct connman_service *service;
1603 struct service_entry *entry;
1605 list = g_sequence_new(destroy_service_entry);
1609 iter = g_sequence_get_begin_iter(service_list);
1611 while (g_sequence_iter_is_end(iter) == FALSE) {
1612 service = g_sequence_get(iter);
1614 if (service_match(session, service) == TRUE) {
1615 entry = create_service_entry(service, service->name,
1620 g_sequence_append(list, entry);
1623 iter = g_sequence_iter_next(iter);
1629 void __connman_service_session_inc(struct connman_service *service)
1631 DBG("service %p ref count %d", service,
1632 g_atomic_int_get(&service->session_usage_count) + 1);
1634 g_atomic_int_inc(&service->session_usage_count);
1637 connman_bool_t __connman_service_session_dec(struct connman_service *service)
1639 DBG("service %p ref count %d", service,
1640 g_atomic_int_get(&service->session_usage_count) - 1);
1642 if (g_atomic_int_dec_and_test(&service->session_usage_count) == FALSE)
1648 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
1649 struct connman_service *service)
1651 dbus_bool_t required;
1654 str = __connman_service_type2string(service->type);
1656 connman_dbus_dict_append_basic(dict, "Type",
1657 DBUS_TYPE_STRING, &str);
1659 connman_dbus_dict_append_array(dict, "Security",
1660 DBUS_TYPE_STRING, append_security, service);
1662 str = state2string(service->state);
1664 connman_dbus_dict_append_basic(dict, "State",
1665 DBUS_TYPE_STRING, &str);
1667 str = error2string(service->error);
1669 connman_dbus_dict_append_basic(dict, "Error",
1670 DBUS_TYPE_STRING, &str);
1672 if (service->strength > 0)
1673 connman_dbus_dict_append_basic(dict, "Strength",
1674 DBUS_TYPE_BYTE, &service->strength);
1676 connman_dbus_dict_append_basic(dict, "Favorite",
1677 DBUS_TYPE_BOOLEAN, &service->favorite);
1679 connman_dbus_dict_append_basic(dict, "Immutable",
1680 DBUS_TYPE_BOOLEAN, &service->immutable);
1682 if (service->favorite == TRUE)
1683 connman_dbus_dict_append_basic(dict, "AutoConnect",
1684 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1686 connman_dbus_dict_append_basic(dict, "AutoConnect",
1687 DBUS_TYPE_BOOLEAN, &service->favorite);
1689 if (service->name != NULL)
1690 connman_dbus_dict_append_basic(dict, "Name",
1691 DBUS_TYPE_STRING, &service->name);
1693 connman_dbus_dict_append_basic(dict, "LoginRequired",
1694 DBUS_TYPE_BOOLEAN, &service->login_required);
1696 switch (service->type) {
1697 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1698 case CONNMAN_SERVICE_TYPE_SYSTEM:
1699 case CONNMAN_SERVICE_TYPE_GPS:
1700 case CONNMAN_SERVICE_TYPE_VPN:
1701 case CONNMAN_SERVICE_TYPE_GADGET:
1703 case CONNMAN_SERVICE_TYPE_CELLULAR:
1704 connman_dbus_dict_append_basic(dict, "Roaming",
1705 DBUS_TYPE_BOOLEAN, &service->roaming);
1708 connman_dbus_dict_append_basic(dict, "SetupRequired",
1709 DBUS_TYPE_BOOLEAN, &required);
1710 connman_dbus_dict_append_dict(dict, "Ethernet",
1711 append_ethernet, service);
1713 case CONNMAN_SERVICE_TYPE_WIFI:
1714 if (service->passphrase != NULL && limited == FALSE)
1715 connman_dbus_dict_append_basic(dict, "Passphrase",
1716 DBUS_TYPE_STRING, &service->passphrase);
1720 switch (service->security) {
1721 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1722 case CONNMAN_SERVICE_SECURITY_NONE:
1724 case CONNMAN_SERVICE_SECURITY_WEP:
1725 case CONNMAN_SERVICE_SECURITY_PSK:
1726 case CONNMAN_SERVICE_SECURITY_WPA:
1727 case CONNMAN_SERVICE_SECURITY_RSN:
1728 if (service->passphrase == NULL)
1731 case CONNMAN_SERVICE_SECURITY_8021X:
1735 connman_dbus_dict_append_basic(dict, "PassphraseRequired",
1736 DBUS_TYPE_BOOLEAN, &required);
1739 case CONNMAN_SERVICE_TYPE_ETHERNET:
1740 case CONNMAN_SERVICE_TYPE_WIMAX:
1741 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1742 connman_dbus_dict_append_dict(dict, "Ethernet",
1743 append_ethernet, service);
1747 connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
1749 connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
1750 append_ipv4config, service);
1752 connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
1754 connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
1755 append_ipv6config, service);
1757 connman_dbus_dict_append_array(dict, "Nameservers",
1758 DBUS_TYPE_STRING, append_dns, service);
1760 connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
1761 DBUS_TYPE_STRING, append_dnsconfig, service);
1763 connman_dbus_dict_append_array(dict, "Domains",
1764 DBUS_TYPE_STRING, append_domain, service);
1766 connman_dbus_dict_append_array(dict, "Domains.Configuration",
1767 DBUS_TYPE_STRING, append_domainconfig, service);
1769 connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
1771 connman_dbus_dict_append_dict(dict, "Proxy.Configuration",
1772 append_proxyconfig, service);
1774 connman_dbus_dict_append_dict(dict, "Provider",
1775 append_provider, service);
1778 static void append_struct(gpointer value, gpointer user_data)
1780 struct connman_service *service = value;
1781 DBusMessageIter *iter = user_data;
1782 DBusMessageIter entry, dict;
1784 if (service->path == NULL || service->hidden == TRUE)
1787 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
1789 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1792 connman_dbus_dict_open(&entry, &dict);
1793 append_properties(&dict, TRUE, service);
1794 connman_dbus_dict_close(&entry, &dict);
1796 dbus_message_iter_close_container(iter, &entry);
1799 void __connman_service_list_struct(DBusMessageIter *iter)
1801 g_sequence_foreach(service_list, append_struct, iter);
1804 int __connman_service_get_index(struct connman_service *service)
1806 if (service == NULL)
1809 if (service->network != NULL)
1810 return connman_network_get_index(service->network);
1811 else if (service->provider != NULL)
1812 return connman_provider_get_index(service->provider);
1817 void __connman_service_set_domainname(struct connman_service *service,
1818 const char *domainname)
1820 if (service == NULL)
1823 g_free(service->domainname);
1824 service->domainname = g_strdup(domainname);
1826 domain_changed(service);
1829 const char *connman_service_get_domainname(struct connman_service *service)
1831 if (service == NULL)
1834 if (service->domains != NULL)
1835 return service->domains[0];
1837 return service->domainname;
1840 char **connman_service_get_nameservers(struct connman_service *service)
1842 if (service == NULL)
1845 if (service->nameservers_config != NULL)
1846 return service->nameservers_config;
1847 else if (service->nameservers != NULL)
1848 return service->nameservers;
1853 void connman_service_set_proxy_method(struct connman_service *service,
1854 enum connman_service_proxy_method method)
1856 if (service == NULL)
1859 service->proxy = method;
1861 proxy_changed(service);
1863 if (method != CONNMAN_SERVICE_PROXY_METHOD_AUTO)
1864 __connman_notifier_proxy_changed(service);
1867 enum connman_service_proxy_method connman_service_get_proxy_method(
1868 struct connman_service *service)
1870 if (service == NULL)
1871 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
1873 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN) {
1874 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
1875 service->pac == NULL)
1876 return service->proxy;
1878 return service->proxy_config;
1881 return service->proxy;
1884 char **connman_service_get_proxy_servers(struct connman_service *service)
1886 return g_strdupv(service->proxies);
1889 char **connman_service_get_proxy_excludes(struct connman_service *service)
1891 return g_strdupv(service->excludes);
1894 const char *connman_service_get_proxy_url(struct connman_service *service)
1896 if (service == NULL)
1899 return service->pac;
1902 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
1905 if (service == NULL)
1908 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_AUTO;
1910 if (service->ipconfig_ipv4) {
1911 if (__connman_ipconfig_set_proxy_autoconfig(
1912 service->ipconfig_ipv4, url) < 0)
1914 } else if (service->ipconfig_ipv6) {
1915 if (__connman_ipconfig_set_proxy_autoconfig(
1916 service->ipconfig_ipv6, url) < 0)
1921 proxy_changed(service);
1923 __connman_notifier_proxy_changed(service);
1926 const char *connman_service_get_proxy_autoconfig(struct connman_service *service)
1928 if (service == NULL)
1931 if (service->ipconfig_ipv4)
1932 return __connman_ipconfig_get_proxy_autoconfig(
1933 service->ipconfig_ipv4);
1934 else if (service->ipconfig_ipv6)
1935 return __connman_ipconfig_get_proxy_autoconfig(
1936 service->ipconfig_ipv6);
1940 static void update_timeservers(struct connman_service *service)
1944 if (service->timeservers == NULL)
1947 switch (service->state) {
1948 case CONNMAN_SERVICE_STATE_UNKNOWN:
1949 case CONNMAN_SERVICE_STATE_IDLE:
1950 case CONNMAN_SERVICE_STATE_ASSOCIATION:
1951 case CONNMAN_SERVICE_STATE_CONFIGURATION:
1953 case CONNMAN_SERVICE_STATE_FAILURE:
1954 case CONNMAN_SERVICE_STATE_DISCONNECT:
1955 for (i = 0; service->timeservers[i] != NULL; i++)
1956 connman_timeserver_remove(service->timeservers[i]);
1958 case CONNMAN_SERVICE_STATE_READY:
1959 case CONNMAN_SERVICE_STATE_ONLINE:
1963 for (i = 0; service->timeservers[i] != NULL; i++)
1964 connman_timeserver_append(service->timeservers[i]);
1967 int __connman_service_timeserver_append(struct connman_service *service,
1968 const char *timeserver)
1972 DBG("service %p timeserver %s", service, timeserver);
1974 if (timeserver == NULL)
1977 if (service->timeservers != NULL) {
1980 for (i = 0; service->timeservers[i] != NULL; i++)
1981 if (g_strcmp0(service->timeservers[i], timeserver) == 0)
1984 len = g_strv_length(service->timeservers);
1985 service->timeservers = g_try_renew(char *, service->timeservers,
1989 service->timeservers = g_try_new0(char *, len + 2);
1992 if (service->timeservers == NULL)
1995 service->timeservers[len] = g_strdup(timeserver);
1996 service->timeservers[len + 1] = NULL;
1998 update_timeservers(service);
2003 int __connman_service_timeserver_remove(struct connman_service *service,
2004 const char *timeserver)
2009 DBG("service %p timeserver %s", service, timeserver);
2011 if (timeserver == NULL)
2014 if (service->timeservers == NULL)
2017 len = g_strv_length(service->timeservers);
2019 if (g_strcmp0(service->timeservers[0], timeserver) != 0)
2022 g_strfreev(service->timeservers);
2023 service->timeservers = NULL;
2028 servers = g_try_new0(char *, len - 1);
2029 if (servers == NULL)
2032 for (i = 0, j = 0; i < len; i++) {
2033 if (g_strcmp0(service->timeservers[i], timeserver) != 0) {
2034 servers[j] = g_strdup(service->timeservers[i]);
2038 servers[len - 2] = NULL;
2040 g_strfreev(service->timeservers);
2041 service->timeservers = servers;
2043 update_timeservers(service);
2048 void __connman_service_set_pac(struct connman_service *service,
2051 g_free(service->pac);
2052 service->pac = g_strdup(pac);
2054 proxy_changed(service);
2057 void __connman_service_set_identity(struct connman_service *service,
2058 const char *identity)
2060 if (service->immutable)
2063 g_free(service->identity);
2064 service->identity = g_strdup(identity);
2066 if (service->network != NULL)
2067 connman_network_set_string(service->network,
2072 void __connman_service_set_agent_identity(struct connman_service *service,
2073 const char *agent_identity)
2075 g_free(service->agent_identity);
2076 service->agent_identity = g_strdup(agent_identity);
2078 if (service->network != NULL)
2079 connman_network_set_string(service->network,
2080 "WiFi.AgentIdentity",
2081 service->agent_identity);
2084 void __connman_service_set_passphrase(struct connman_service *service,
2085 const char* passphrase)
2087 if (service->immutable == TRUE)
2090 g_free(service->passphrase);
2091 service->passphrase = g_strdup(passphrase);
2093 passphrase_changed(service);
2095 if (service->network != NULL)
2096 connman_network_set_string(service->network,
2098 service->passphrase);
2100 __connman_storage_save_service(service);
2103 void __connman_service_set_agent_passphrase(struct connman_service *service,
2104 const char *agent_passphrase)
2106 g_free(service->agent_passphrase);
2107 service->agent_passphrase = g_strdup(agent_passphrase);
2109 if (service->network != NULL)
2110 connman_network_set_string(service->network,
2111 "WiFi.AgentPassphrase",
2112 service->agent_passphrase);
2115 static DBusMessage *get_properties(DBusConnection *conn,
2116 DBusMessage *msg, void *user_data)
2118 struct connman_service *service = user_data;
2120 DBusMessageIter array, dict;
2122 DBG("service %p", service);
2124 reply = dbus_message_new_method_return(msg);
2128 dbus_message_iter_init_append(reply, &array);
2130 connman_dbus_dict_open(&array, &dict);
2131 append_properties(&dict, FALSE, service);
2132 connman_dbus_dict_close(&array, &dict);
2137 static int update_proxy_configuration(struct connman_service *service,
2138 DBusMessageIter *array)
2140 DBusMessageIter dict;
2141 enum connman_service_proxy_method method;
2142 GString *servers_str = NULL;
2143 GString *excludes_str = NULL;
2144 const char *url = NULL;
2146 method = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2148 dbus_message_iter_recurse(array, &dict);
2150 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2151 DBusMessageIter entry, variant;
2155 dbus_message_iter_recurse(&dict, &entry);
2157 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2160 dbus_message_iter_get_basic(&entry, &key);
2161 dbus_message_iter_next(&entry);
2163 if (dbus_message_iter_get_arg_type(&entry) !=
2167 dbus_message_iter_recurse(&entry, &variant);
2168 type = dbus_message_iter_get_arg_type(&variant);
2170 if (g_str_equal(key, "Method") == TRUE) {
2173 if (type != DBUS_TYPE_STRING)
2176 dbus_message_iter_get_basic(&variant, &val);
2177 method = string2proxymethod(val);
2178 } else if (g_str_equal(key, "URL") == TRUE) {
2179 if (type != DBUS_TYPE_STRING)
2182 dbus_message_iter_get_basic(&variant, &url);
2183 } else if (g_str_equal(key, "Servers") == TRUE) {
2184 DBusMessageIter str_array;
2186 if (type != DBUS_TYPE_ARRAY)
2189 servers_str = g_string_new(NULL);
2190 if (servers_str == NULL)
2193 dbus_message_iter_recurse(&variant, &str_array);
2195 while (dbus_message_iter_get_arg_type(&str_array) ==
2199 dbus_message_iter_get_basic(&str_array, &val);
2201 if (servers_str->len > 0)
2202 g_string_append_printf(servers_str,
2205 g_string_append(servers_str, val);
2207 dbus_message_iter_next(&str_array);
2209 } else if (g_str_equal(key, "Excludes") == TRUE) {
2210 DBusMessageIter str_array;
2212 if (type != DBUS_TYPE_ARRAY)
2215 excludes_str = g_string_new(NULL);
2216 if (excludes_str == NULL)
2219 dbus_message_iter_recurse(&variant, &str_array);
2221 while (dbus_message_iter_get_arg_type(&str_array) ==
2225 dbus_message_iter_get_basic(&str_array, &val);
2227 if (excludes_str->len > 0)
2228 g_string_append_printf(excludes_str,
2231 g_string_append(excludes_str, val);
2233 dbus_message_iter_next(&str_array);
2237 dbus_message_iter_next(&dict);
2241 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
2243 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
2244 if (servers_str == NULL && service->proxies == NULL)
2247 if (servers_str != NULL) {
2248 g_strfreev(service->proxies);
2250 if (servers_str->len > 0)
2251 service->proxies = g_strsplit_set(
2252 servers_str->str, " ", 0);
2254 service->proxies = NULL;
2257 if (excludes_str != NULL) {
2258 g_strfreev(service->excludes);
2260 if (excludes_str->len > 0)
2261 service->excludes = g_strsplit_set(
2262 excludes_str->str, " ", 0);
2264 service->excludes = NULL;
2267 if (service->proxies == NULL)
2268 method = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
2271 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
2272 g_free(service->pac);
2274 if (url != NULL && strlen(url) > 0)
2275 service->pac = g_strdup(url);
2277 service->pac = NULL;
2279 /* if we are connected:
2280 - if service->pac == NULL
2281 - if __connman_ipconfig_get_proxy_autoconfig(
2282 service->ipconfig) == NULL
2283 --> We should start WPAD */
2286 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
2290 if (servers_str != NULL)
2291 g_string_free(servers_str, TRUE);
2293 if (excludes_str != NULL)
2294 g_string_free(excludes_str, TRUE);
2296 service->proxy_config = method;
2301 if (servers_str != NULL)
2302 g_string_free(servers_str, TRUE);
2304 if (excludes_str != NULL)
2305 g_string_free(excludes_str, TRUE);
2310 static int set_ipconfig(struct connman_service *service,
2311 struct connman_ipconfig *ipconfig,
2312 DBusMessageIter *array,
2313 enum connman_service_state state,
2314 enum connman_service_state *new_state)
2316 enum connman_ipconfig_method old_method;
2317 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2318 enum connman_ipconfig_type type;
2321 old_method = __connman_ipconfig_get_method(ipconfig);
2323 if (is_connecting_state(service, state) ||
2324 is_connected_state(service, state))
2325 __connman_network_clear_ipconfig(service->network, ipconfig);
2327 err = __connman_ipconfig_set_config(ipconfig, array);
2328 method = __connman_ipconfig_get_method(ipconfig);
2329 type = __connman_ipconfig_get_config_type(ipconfig);
2331 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
2332 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2333 method == CONNMAN_IPCONFIG_METHOD_DHCP) {
2334 *new_state = service->state_ipv4 =
2335 CONNMAN_SERVICE_STATE_CONFIGURATION;
2336 __connman_ipconfig_enable(ipconfig);
2339 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2340 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2341 method == CONNMAN_IPCONFIG_METHOD_AUTO) {
2342 *new_state = service->state_ipv6;
2343 __connman_ipconfig_enable(ipconfig);
2347 DBG("err %d ipconfig %p type %d method %d state %s", err, ipconfig,
2348 type, method, state2string(*new_state));
2353 static DBusMessage *set_property(DBusConnection *conn,
2354 DBusMessage *msg, void *user_data)
2356 struct connman_service *service = user_data;
2357 DBusMessageIter iter, value;
2361 DBG("service %p", service);
2363 if (dbus_message_iter_init(msg, &iter) == FALSE)
2364 return __connman_error_invalid_arguments(msg);
2366 dbus_message_iter_get_basic(&iter, &name);
2367 dbus_message_iter_next(&iter);
2368 dbus_message_iter_recurse(&iter, &value);
2370 type = dbus_message_iter_get_arg_type(&value);
2372 if (g_str_equal(name, "AutoConnect") == TRUE) {
2373 connman_bool_t autoconnect;
2375 if (type != DBUS_TYPE_BOOLEAN)
2376 return __connman_error_invalid_arguments(msg);
2378 if (service->favorite == FALSE)
2379 return __connman_error_invalid_service(msg);
2381 dbus_message_iter_get_basic(&value, &autoconnect);
2383 if (service->autoconnect == autoconnect)
2384 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2386 service->autoconnect = autoconnect;
2388 autoconnect_changed(service);
2390 __connman_storage_save_service(service);
2391 } else if (g_str_equal(name, "Passphrase") == TRUE) {
2392 const char *passphrase;
2394 if (type != DBUS_TYPE_STRING)
2395 return __connman_error_invalid_arguments(msg);
2397 if (service->immutable == TRUE)
2398 return __connman_error_not_supported(msg);
2400 dbus_message_iter_get_basic(&value, &passphrase);
2402 __connman_service_set_passphrase(service, passphrase);
2403 } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
2404 DBusMessageIter entry;
2409 if (type != DBUS_TYPE_ARRAY)
2410 return __connman_error_invalid_arguments(msg);
2412 str = g_string_new(NULL);
2414 return __connman_error_invalid_arguments(msg);
2416 index = connman_network_get_index(service->network);
2417 gw = __connman_ipconfig_get_gateway_from_index(index);
2419 if (gw && strlen(gw))
2420 __connman_service_nameserver_del_routes(service);
2422 dbus_message_iter_recurse(&value, &entry);
2424 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2426 dbus_message_iter_get_basic(&entry, &val);
2427 dbus_message_iter_next(&entry);
2429 g_string_append_printf(str, " %s", val);
2431 g_string_append(str, val);
2434 g_strfreev(service->nameservers_config);
2437 service->nameservers_config =
2438 g_strsplit_set(str->str, " ", 0);
2440 service->nameservers_config = NULL;
2443 g_string_free(str, TRUE);
2445 if (gw && strlen(gw))
2446 __connman_service_nameserver_add_routes(service, gw);
2448 update_nameservers(service);
2449 dns_configuration_changed(service);
2451 __connman_storage_save_service(service);
2452 } else if (g_str_equal(name, "Domains.Configuration") == TRUE) {
2453 DBusMessageIter entry;
2456 if (type != DBUS_TYPE_ARRAY)
2457 return __connman_error_invalid_arguments(msg);
2459 str = g_string_new(NULL);
2461 return __connman_error_invalid_arguments(msg);
2463 dbus_message_iter_recurse(&value, &entry);
2465 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2467 dbus_message_iter_get_basic(&entry, &val);
2468 dbus_message_iter_next(&entry);
2470 g_string_append_printf(str, " %s", val);
2472 g_string_append(str, val);
2475 g_strfreev(service->domains);
2478 service->domains = g_strsplit_set(str->str, " ", 0);
2480 service->domains = NULL;
2482 g_string_free(str, TRUE);
2484 update_nameservers(service);
2485 domain_configuration_changed(service);
2487 __connman_storage_save_service(service);
2488 } else if (g_str_equal(name, "Proxy.Configuration") == TRUE) {
2491 if (type != DBUS_TYPE_ARRAY)
2492 return __connman_error_invalid_arguments(msg);
2494 err = update_proxy_configuration(service, &value);
2497 return __connman_error_failed(msg, -err);
2499 proxy_configuration_changed(service);
2501 __connman_notifier_proxy_changed(service);
2503 __connman_storage_save_service(service);
2504 } else if (g_str_equal(name, "IPv4.Configuration") == TRUE ||
2505 g_str_equal(name, "IPv6.Configuration")) {
2507 struct connman_ipconfig *ipv4 = NULL, *ipv6 = NULL;
2508 enum connman_service_state state =
2509 CONNMAN_SERVICE_STATE_UNKNOWN;
2514 if (service->ipconfig_ipv4 == NULL &&
2515 service->ipconfig_ipv6 == NULL)
2516 return __connman_error_invalid_property(msg);
2518 if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
2519 ipv4 = service->ipconfig_ipv4;
2520 err = set_ipconfig(service, ipv4, &value,
2521 service->state_ipv4, &state);
2523 } else if (g_str_equal(name, "IPv6.Configuration") == TRUE) {
2524 ipv6 = service->ipconfig_ipv6;
2525 err = set_ipconfig(service, ipv6, &value,
2526 service->state_ipv6, &state);
2530 if (is_connected_state(service, state) ||
2531 is_connecting_state(service, state))
2532 __connman_network_set_ipconfig(service->network,
2534 return __connman_error_failed(msg, -err);
2538 ipv4_configuration_changed(service);
2540 ipv6_configuration_changed(service);
2542 if (is_connecting(service) || is_connected(service))
2543 __connman_network_set_ipconfig(service->network,
2546 __connman_storage_save_service(service);
2548 return __connman_error_invalid_property(msg);
2550 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2553 static void set_idle(struct connman_service *service)
2555 service->state = service->state_ipv4 = service->state_ipv6 =
2556 CONNMAN_SERVICE_STATE_IDLE;
2557 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
2558 state_changed(service);
2561 static DBusMessage *clear_property(DBusConnection *conn,
2562 DBusMessage *msg, void *user_data)
2564 struct connman_service *service = user_data;
2567 DBG("service %p", service);
2569 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
2572 if (g_str_equal(name, "Error") == TRUE) {
2575 g_get_current_time(&service->modified);
2576 __connman_storage_save_service(service);
2577 } else if (g_str_equal(name, "Passphrase") == TRUE) {
2578 if (service->immutable == TRUE)
2579 return __connman_error_not_supported(msg);
2581 g_free(service->passphrase);
2582 service->passphrase = NULL;
2584 passphrase_changed(service);
2586 __connman_storage_save_service(service);
2588 return __connman_error_invalid_property(msg);
2590 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2593 static connman_bool_t is_ignore(struct connman_service *service)
2595 if (service->autoconnect == FALSE)
2598 if (service->roaming == TRUE)
2601 if (service->ignore == TRUE)
2604 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
2610 void __connman_service_auto_connect(void)
2612 struct connman_service *service = NULL;
2613 GSequenceIter *iter;
2617 if (__connman_session_mode() == TRUE) {
2618 DBG("Session mode enabled: auto connect disabled");
2622 iter = g_sequence_get_begin_iter(service_list);
2624 while (g_sequence_iter_is_end(iter) == FALSE) {
2625 service = g_sequence_get(iter);
2627 if (service->pending != NULL)
2630 if (is_connecting(service) == TRUE)
2633 if (service->favorite == FALSE)
2636 if (is_connected(service) == TRUE)
2639 if (is_ignore(service) == FALSE && service->state ==
2640 CONNMAN_SERVICE_STATE_IDLE)
2645 iter = g_sequence_iter_next(iter);
2648 if (service != NULL) {
2649 service->userconnect = FALSE;
2650 __connman_service_connect(service);
2654 static void remove_timeout(struct connman_service *service)
2656 if (service->timeout > 0) {
2657 g_source_remove(service->timeout);
2658 service->timeout = 0;
2662 static void reply_pending(struct connman_service *service, int error)
2664 remove_timeout(service);
2666 if (service->pending != NULL) {
2670 reply = __connman_error_failed(service->pending,
2673 g_dbus_send_message(connection, reply);
2677 sender = dbus_message_get_interface(service->pending);
2679 DBG("sender %s", sender);
2681 if (g_strcmp0(sender, CONNMAN_MANAGER_INTERFACE) == 0)
2682 g_dbus_send_reply(connection, service->pending,
2683 DBUS_TYPE_OBJECT_PATH, &service->path,
2686 g_dbus_send_reply(connection, service->pending,
2690 dbus_message_unref(service->pending);
2691 service->pending = NULL;
2695 static gboolean connect_timeout(gpointer user_data)
2697 struct connman_service *service = user_data;
2698 connman_bool_t autoconnect = FALSE;
2700 DBG("service %p", service);
2702 service->timeout = 0;
2704 if (service->network != NULL)
2705 __connman_network_disconnect(service->network);
2707 __connman_ipconfig_disable(service->ipconfig_ipv4);
2708 __connman_ipconfig_disable(service->ipconfig_ipv6);
2710 __connman_stats_service_unregister(service);
2712 if (service->pending != NULL) {
2715 reply = __connman_error_operation_timeout(service->pending);
2717 g_dbus_send_message(connection, reply);
2719 dbus_message_unref(service->pending);
2720 service->pending = NULL;
2724 __connman_service_ipconfig_indicate_state(service,
2725 CONNMAN_SERVICE_STATE_FAILURE,
2726 CONNMAN_IPCONFIG_TYPE_IPV4);
2727 __connman_service_ipconfig_indicate_state(service,
2728 CONNMAN_SERVICE_STATE_FAILURE,
2729 CONNMAN_IPCONFIG_TYPE_IPV6);
2731 if (autoconnect == TRUE && service->userconnect == FALSE)
2732 __connman_service_auto_connect();
2737 static void set_reconnect_state(struct connman_service *service,
2738 connman_bool_t reconnect)
2740 struct connman_device *device;
2742 if (service->network == NULL)
2745 device = connman_network_get_device(service->network);
2749 __connman_device_set_reconnect(device, reconnect);
2752 static connman_bool_t get_reconnect_state(struct connman_service *service)
2754 struct connman_device *device;
2756 if (service->network == NULL)
2759 device = connman_network_get_device(service->network);
2763 return __connman_device_get_reconnect(device);
2766 static void request_input_cb (struct connman_service *service,
2767 const char *identity, const char *passphrase,
2770 DBG ("RequestInput return, %p", service);
2772 if (identity == NULL && passphrase == NULL && service->wps == FALSE)
2775 if (identity != NULL)
2776 __connman_service_set_agent_identity(service, identity);
2778 if (passphrase != NULL) {
2779 switch (service->security) {
2780 case CONNMAN_SERVICE_SECURITY_WEP:
2781 case CONNMAN_SERVICE_SECURITY_PSK:
2782 __connman_service_set_passphrase(service, passphrase);
2784 case CONNMAN_SERVICE_SECURITY_8021X:
2785 __connman_service_set_agent_passphrase(service,
2788 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
2789 case CONNMAN_SERVICE_SECURITY_NONE:
2790 case CONNMAN_SERVICE_SECURITY_WPA:
2791 case CONNMAN_SERVICE_SECURITY_RSN:
2792 DBG("service security '%s' not handled",
2793 security2string(service->security));
2798 __connman_service_connect(service);
2800 /* Never cache agent provided credentials */
2801 __connman_service_set_agent_identity(service, NULL);
2802 __connman_service_set_agent_passphrase(service, NULL);
2805 static DBusMessage *connect_service(DBusConnection *conn,
2806 DBusMessage *msg, void *user_data)
2808 struct connman_service *service = user_data;
2809 GSequenceIter *iter;
2812 DBG("service %p", service);
2814 if (service->pending != NULL)
2815 return __connman_error_in_progress(msg);
2817 iter = g_sequence_get_begin_iter(service_list);
2819 while (g_sequence_iter_is_end(iter) == FALSE) {
2820 struct connman_service *temp = g_sequence_get(iter);
2822 if (service->type == temp->type && is_connecting(temp) == TRUE)
2823 return __connman_error_in_progress(msg);
2825 iter = g_sequence_iter_next(iter);
2828 service->ignore = FALSE;
2830 service->userconnect = TRUE;
2832 service->pending = dbus_message_ref(msg);
2834 set_reconnect_state(service, FALSE);
2836 err = __connman_service_connect(service);
2838 if (service->pending == NULL)
2841 if (err != -EINPROGRESS) {
2842 dbus_message_unref(service->pending);
2843 service->pending = NULL;
2845 return __connman_error_failed(msg, -err);
2851 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2854 static DBusMessage *disconnect_service(DBusConnection *conn,
2855 DBusMessage *msg, void *user_data)
2857 struct connman_service *service = user_data;
2860 DBG("service %p", service);
2862 reply_pending(service, ECONNABORTED);
2864 service->ignore = TRUE;
2866 set_reconnect_state(service, FALSE);
2868 err = __connman_service_disconnect(service);
2870 if (err != -EINPROGRESS)
2871 return __connman_error_failed(msg, -err);
2876 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2879 static DBusMessage *remove_service(DBusConnection *conn,
2880 DBusMessage *msg, void *user_data)
2882 struct connman_service *service = user_data;
2884 DBG("service %p", service);
2886 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
2887 return __connman_error_not_supported(msg);
2889 if (service->immutable == TRUE)
2890 return __connman_error_not_supported(msg);
2892 if (service->favorite == FALSE && service->state !=
2893 CONNMAN_SERVICE_STATE_FAILURE)
2894 return __connman_error_not_supported(msg);
2896 if (service->network != NULL) {
2897 set_reconnect_state(service, FALSE);
2899 __connman_network_disconnect(service->network);
2902 g_free(service->passphrase);
2903 service->passphrase = NULL;
2905 passphrase_changed(service);
2909 __connman_service_set_favorite(service, FALSE);
2910 __connman_storage_save_service(service);
2912 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2915 static gboolean check_suitable_state(enum connman_service_state a,
2916 enum connman_service_state b)
2919 * Special check so that "ready" service can be moved before
2922 if ((a == CONNMAN_SERVICE_STATE_ONLINE &&
2923 b == CONNMAN_SERVICE_STATE_READY) ||
2924 (b == CONNMAN_SERVICE_STATE_ONLINE &&
2925 a == CONNMAN_SERVICE_STATE_READY))
2931 static DBusMessage *move_service(DBusConnection *conn,
2932 DBusMessage *msg, void *user_data,
2935 struct connman_service *service = user_data;
2936 struct connman_service *target;
2938 GSequenceIter *src, *dst;
2939 enum connman_ipconfig_method target4, target6;
2940 enum connman_ipconfig_method service4, service6;
2942 DBG("service %p", service);
2944 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
2947 if (service->favorite == FALSE)
2948 return __connman_error_not_supported(msg);
2950 target = find_service(path);
2951 if (target == NULL || target->favorite == FALSE || target == service)
2952 return __connman_error_invalid_service(msg);
2954 target4 = __connman_ipconfig_get_method(target->ipconfig_ipv4);
2955 target6 = __connman_ipconfig_get_method(target->ipconfig_ipv6);
2956 service4 = __connman_ipconfig_get_method(service->ipconfig_ipv4);
2957 service6 = __connman_ipconfig_get_method(service->ipconfig_ipv6);
2959 DBG("target %s method %d/%d state %d/%d", target->identifier,
2961 target->state_ipv4, target->state_ipv6);
2963 DBG("service %s method %d/%d state %d/%d", service->identifier,
2965 service->state_ipv4, service->state_ipv6);
2968 * If method is OFF, then we do not need to check the corresponding
2971 if (target4 == CONNMAN_IPCONFIG_METHOD_OFF) {
2972 if (service6 != CONNMAN_IPCONFIG_METHOD_OFF) {
2973 if (check_suitable_state(target->state_ipv6,
2974 service->state_ipv6) == FALSE)
2975 return __connman_error_invalid_service(msg);
2979 if (target6 == CONNMAN_IPCONFIG_METHOD_OFF) {
2980 if (service4 != CONNMAN_IPCONFIG_METHOD_OFF) {
2981 if (check_suitable_state(target->state_ipv4,
2982 service->state_ipv4) == FALSE)
2983 return __connman_error_invalid_service(msg);
2987 if (service4 == CONNMAN_IPCONFIG_METHOD_OFF) {
2988 if (target6 != CONNMAN_IPCONFIG_METHOD_OFF) {
2989 if (check_suitable_state(target->state_ipv6,
2990 service->state_ipv6) == FALSE)
2991 return __connman_error_invalid_service(msg);
2995 if (service6 == CONNMAN_IPCONFIG_METHOD_OFF) {
2996 if (target4 != CONNMAN_IPCONFIG_METHOD_OFF) {
2997 if (check_suitable_state(target->state_ipv4,
2998 service->state_ipv4) == FALSE)
2999 return __connman_error_invalid_service(msg);
3003 g_get_current_time(&service->modified);
3004 __connman_storage_save_service(service);
3006 src = g_hash_table_lookup(service_hash, service->identifier);
3007 dst = g_hash_table_lookup(service_hash, target->identifier);
3009 before ? g_sequence_move(src, dst) : g_sequence_move(dst, src);
3011 __connman_profile_changed(FALSE);
3013 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3016 static DBusMessage *move_before(DBusConnection *conn,
3017 DBusMessage *msg, void *user_data)
3019 return move_service(conn, msg, user_data, TRUE);
3022 static DBusMessage *move_after(DBusConnection *conn,
3023 DBusMessage *msg, void *user_data)
3025 return move_service(conn, msg, user_data, FALSE);
3028 static DBusMessage *reset_counters(DBusConnection *conn,
3029 DBusMessage *msg, void *user_data)
3031 struct connman_service *service = user_data;
3033 reset_stats(service);
3035 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3038 static GDBusMethodTable service_methods[] = {
3039 { "GetProperties", "", "a{sv}", get_properties },
3040 { "SetProperty", "sv", "", set_property },
3041 { "ClearProperty", "s", "", clear_property },
3042 { "Connect", "", "", connect_service,
3043 G_DBUS_METHOD_FLAG_ASYNC },
3044 { "Disconnect", "", "", disconnect_service },
3045 { "Remove", "", "", remove_service },
3046 { "MoveBefore", "o", "", move_before },
3047 { "MoveAfter", "o", "", move_after },
3048 { "ResetCounters", "", "", reset_counters },
3052 static GDBusSignalTable service_signals[] = {
3053 { "PropertyChanged", "sv" },
3057 static void service_free(gpointer user_data)
3059 struct connman_service *service = user_data;
3060 char *path = service->path;
3062 DBG("service %p", service);
3064 reply_pending(service, ENOENT);
3066 g_hash_table_remove(service_hash, service->identifier);
3068 __connman_notifier_service_remove(service);
3070 stats_stop(service);
3071 __connman_storage_save_service(service);
3073 service->path = NULL;
3076 __connman_profile_changed(FALSE);
3078 g_dbus_unregister_interface(connection, path,
3079 CONNMAN_SERVICE_INTERFACE);
3083 g_hash_table_destroy(service->counter_table);
3085 if (service->network != NULL) {
3086 if (service->network_created == TRUE)
3087 connman_network_unref(service->network);
3090 if (service->provider != NULL)
3091 connman_provider_unref(service->provider);
3093 if (service->ipconfig_ipv4 != NULL) {
3094 connman_ipconfig_set_ops(service->ipconfig_ipv4, NULL);
3095 connman_ipconfig_set_data(service->ipconfig_ipv4, NULL);
3096 connman_ipconfig_unref(service->ipconfig_ipv4);
3097 service->ipconfig_ipv4 = NULL;
3100 if (service->ipconfig_ipv6 != NULL) {
3101 connman_ipconfig_set_ops(service->ipconfig_ipv6, NULL);
3102 connman_ipconfig_set_data(service->ipconfig_ipv6, NULL);
3103 connman_ipconfig_unref(service->ipconfig_ipv6);
3104 service->ipconfig_ipv6 = NULL;
3107 if (service->location != NULL)
3108 connman_location_unref(service->location);
3110 g_strfreev(service->nameservers);
3111 g_strfreev(service->nameservers_config);
3112 g_strfreev(service->domains);
3113 g_strfreev(service->proxies);
3114 g_strfreev(service->excludes);
3116 g_free(service->domainname);
3117 g_free(service->pac);
3118 g_free(service->profile);
3119 g_free(service->name);
3120 g_free(service->passphrase);
3121 g_free(service->agent_passphrase);
3122 g_free(service->identifier);
3123 g_free(service->eap);
3124 g_free(service->identity);
3125 g_free(service->agent_identity);
3126 g_free(service->ca_cert_file);
3127 g_free(service->client_cert_file);
3128 g_free(service->private_key_file);
3129 g_free(service->private_key_passphrase);
3130 g_free(service->phase2);
3132 if (service->stats.timer != NULL)
3133 g_timer_destroy(service->stats.timer);
3134 if (service->stats_roaming.timer != NULL)
3135 g_timer_destroy(service->stats_roaming.timer);
3141 * __connman_service_put:
3142 * @service: service structure
3144 * Release service if no longer needed
3146 void __connman_service_put(struct connman_service *service)
3148 DBG("service %p", service);
3150 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
3151 GSequenceIter *iter;
3153 iter = g_hash_table_lookup(service_hash, service->identifier);
3155 reply_pending(service, ECONNABORTED);
3157 __connman_service_disconnect(service);
3159 g_sequence_remove(iter);
3161 service_free(service);
3165 static void stats_init(struct connman_service *service)
3168 service->stats.valid = FALSE;
3169 service->stats.enabled = FALSE;
3170 service->stats.timer = g_timer_new();
3173 service->stats_roaming.valid = FALSE;
3174 service->stats_roaming.enabled = FALSE;
3175 service->stats_roaming.timer = g_timer_new();
3178 static void service_initialize(struct connman_service *service)
3180 DBG("service %p", service);
3182 service->refcount = 1;
3183 service->session_usage_count = 0;
3185 service->network_created = FALSE;
3187 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
3188 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
3190 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
3191 service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
3192 service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
3194 service->favorite = FALSE;
3195 service->immutable = FALSE;
3196 service->hidden = FALSE;
3198 service->ignore = FALSE;
3200 service->userconnect = FALSE;
3204 stats_init(service);
3206 service->provider = NULL;
3208 service->wps = FALSE;
3212 * connman_service_create:
3214 * Allocate a new service.
3216 * Returns: a newly-allocated #connman_service structure
3218 struct connman_service *connman_service_create(void)
3221 struct connman_stats_counter *counters;
3222 const char *counter;
3224 struct connman_service *service;
3226 service = g_try_new0(struct connman_service, 1);
3227 if (service == NULL)
3230 DBG("service %p", service);
3232 service->counter_table = g_hash_table_new_full(g_str_hash,
3233 g_str_equal, NULL, g_free);
3235 for (list = counter_list; list; list = list->next) {
3236 counter = list->data;
3238 counters = g_try_new0(struct connman_stats_counter, 1);
3239 if (counters == NULL) {
3240 g_hash_table_destroy(service->counter_table);
3245 counters->append_all = TRUE;
3247 g_hash_table_replace(service->counter_table, (gpointer)counter,
3251 service_initialize(service);
3253 service->location = __connman_location_create(service);
3258 struct connman_location *__connman_service_get_location(struct connman_service *service)
3260 return service->location;
3264 * connman_service_ref:
3265 * @service: service structure
3267 * Increase reference counter of service
3269 struct connman_service *connman_service_ref(struct connman_service *service)
3273 g_atomic_int_inc(&service->refcount);
3279 * connman_service_unref:
3280 * @service: service structure
3282 * Decrease reference counter of service
3284 void connman_service_unref(struct connman_service *service)
3286 __connman_service_put(service);
3289 static gint service_compare(gconstpointer a, gconstpointer b,
3292 struct connman_service *service_a = (void *) a;
3293 struct connman_service *service_b = (void *) b;
3294 enum connman_service_state state_a, state_b;
3296 state_a = service_a->state;
3297 state_b = service_b->state;
3299 if (state_a != state_b) {
3300 gboolean a_connected = is_connected(service_a);
3301 gboolean b_connected = is_connected(service_b);
3303 if (a_connected == TRUE && b_connected == TRUE) {
3304 /* We prefer online over ready state */
3305 if (state_a == CONNMAN_SERVICE_STATE_ONLINE)
3308 if (state_b == CONNMAN_SERVICE_STATE_ONLINE)
3312 if (a_connected == TRUE)
3314 if (b_connected == TRUE)
3317 if (is_connecting(service_a) == TRUE)
3319 if (is_connecting(service_b) == TRUE)
3323 if (service_a->order > service_b->order)
3326 if (service_a->order < service_b->order)
3329 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
3332 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
3335 if (service_a->type != service_b->type) {
3336 switch (service_a->type) {
3337 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3338 case CONNMAN_SERVICE_TYPE_SYSTEM:
3339 case CONNMAN_SERVICE_TYPE_ETHERNET:
3340 case CONNMAN_SERVICE_TYPE_GPS:
3341 case CONNMAN_SERVICE_TYPE_VPN:
3342 case CONNMAN_SERVICE_TYPE_GADGET:
3344 case CONNMAN_SERVICE_TYPE_WIFI:
3346 case CONNMAN_SERVICE_TYPE_WIMAX:
3347 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3348 case CONNMAN_SERVICE_TYPE_CELLULAR:
3353 return (gint) service_b->strength - (gint) service_a->strength;
3357 * connman_service_get_type:
3358 * @service: service structure
3360 * Get the type of service
3362 enum connman_service_type connman_service_get_type(struct connman_service *service)
3364 if (service == NULL)
3365 return CONNMAN_SERVICE_TYPE_UNKNOWN;
3367 return service->type;
3371 * connman_service_get_interface:
3372 * @service: service structure
3374 * Get network interface of service
3376 char *connman_service_get_interface(struct connman_service *service)
3380 if (service == NULL)
3383 if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
3384 if (service->ipconfig_ipv4)
3385 index = connman_ipconfig_get_index(
3386 service->ipconfig_ipv4);
3387 else if (service->ipconfig_ipv6)
3388 index = connman_ipconfig_get_index(
3389 service->ipconfig_ipv6);
3393 return connman_inet_ifname(index);
3396 if (service->network == NULL)
3399 index = connman_network_get_index(service->network);
3401 return connman_inet_ifname(index);
3405 * connman_service_get_network:
3406 * @service: service structure
3408 * Get the service network
3410 struct connman_network *
3411 __connman_service_get_network(struct connman_service *service)
3413 if (service == NULL)
3416 return service->network;
3419 struct connman_ipconfig *
3420 __connman_service_get_ip4config(struct connman_service *service)
3422 if (service == NULL)
3425 return service->ipconfig_ipv4;
3428 struct connman_ipconfig *
3429 __connman_service_get_ip6config(struct connman_service *service)
3431 if (service == NULL)
3434 return service->ipconfig_ipv6;
3437 struct connman_ipconfig *
3438 __connman_service_get_ipconfig(struct connman_service *service, int family)
3440 if (family == AF_INET)
3441 return __connman_service_get_ip4config(service);
3442 else if (family == AF_INET6)
3443 return __connman_service_get_ip6config(service);
3449 enum connman_service_security __connman_service_get_security(struct connman_service *service)
3451 if (service == NULL)
3452 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3454 return service->security;
3457 const char *__connman_service_get_phase2(struct connman_service *service)
3459 if (service == NULL)
3462 return service->phase2;
3465 connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
3467 if (service == NULL)
3470 return service->wps;
3474 * __connman_service_set_favorite:
3475 * @service: service structure
3476 * @favorite: favorite value
3478 * Change the favorite setting of service
3480 int __connman_service_set_favorite(struct connman_service *service,
3481 connman_bool_t favorite)
3483 GSequenceIter *iter;
3485 iter = g_hash_table_lookup(service_hash, service->identifier);
3489 if (service->favorite == favorite)
3492 service->favorite = favorite;
3494 favorite_changed(service);
3496 g_sequence_sort_changed(iter, service_compare, NULL);
3498 __connman_profile_changed(FALSE);
3503 int __connman_service_set_immutable(struct connman_service *service,
3504 connman_bool_t immutable)
3506 service->immutable = immutable;
3508 immutable_changed(service);
3513 void __connman_service_set_string(struct connman_service *service,
3514 const char *key, const char *value)
3516 if (g_str_equal(key, "EAP") == TRUE) {
3517 g_free(service->eap);
3518 service->eap = g_strdup(value);
3519 } else if (g_str_equal(key, "Identity") == TRUE) {
3520 g_free(service->identity);
3521 service->identity = g_strdup(value);
3522 } else if (g_str_equal(key, "CACertFile") == TRUE) {
3523 g_free(service->ca_cert_file);
3524 service->ca_cert_file = g_strdup(value);
3525 } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
3526 g_free(service->client_cert_file);
3527 service->client_cert_file = g_strdup(value);
3528 } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
3529 g_free(service->private_key_file);
3530 service->private_key_file = g_strdup(value);
3531 } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
3532 g_free(service->private_key_passphrase);
3533 service->private_key_passphrase = g_strdup(value);
3534 } else if (g_str_equal(key, "Phase2") == TRUE) {
3535 g_free(service->phase2);
3536 service->phase2 = g_strdup(value);
3537 } else if (g_str_equal(key, "Passphrase") == TRUE) {
3538 g_free(service->passphrase);
3539 service->passphrase = g_strdup(value);
3543 static void service_complete(struct connman_service *service)
3545 reply_pending(service, EIO);
3547 if (service->userconnect == FALSE)
3548 __connman_service_auto_connect();
3550 g_get_current_time(&service->modified);
3551 __connman_storage_save_service(service);
3554 static void report_error_cb(struct connman_service *service,
3555 gboolean retry, void *user_data)
3558 __connman_service_connect(service);
3560 service_complete(service);
3561 __connman_profile_changed(FALSE);
3562 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3566 static int __connman_service_indicate_state(struct connman_service *service)
3568 enum connman_service_state old_state, new_state;
3569 GSequenceIter *iter;
3571 if (service == NULL)
3574 old_state = service->state;
3575 new_state = combine_state(service->state_ipv4, service->state_ipv6);
3577 if (old_state == new_state)
3580 DBG("service %p old %s - new %s/%s => %s",
3582 state2string(old_state),
3583 state2string(service->state_ipv4),
3584 state2string(service->state_ipv6),
3585 state2string(new_state));
3587 service->state = new_state;
3588 state_changed(service);
3590 if (new_state == CONNMAN_SERVICE_STATE_IDLE &&
3591 old_state != CONNMAN_SERVICE_STATE_DISCONNECT) {
3592 reply_pending(service, ECONNABORTED);
3594 __connman_service_disconnect(service);
3597 if (new_state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
3598 if (__connman_stats_service_register(service) == 0) {
3599 __connman_stats_get(service, FALSE,
3600 &service->stats.data);
3601 __connman_stats_get(service, TRUE,
3602 &service->stats_roaming.data);
3606 if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
3607 if (service->login_required == TRUE) {
3608 service->login_required = FALSE;
3609 login_changed(service);
3612 connman_timeserver_sync();
3615 if (new_state == CONNMAN_SERVICE_STATE_IDLE) {
3616 connman_bool_t reconnect;
3618 reconnect = get_reconnect_state(service);
3619 if (reconnect == TRUE)
3620 __connman_service_auto_connect();
3622 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3625 if (new_state == CONNMAN_SERVICE_STATE_READY) {
3626 enum connman_ipconfig_method method;
3628 set_reconnect_state(service, TRUE);
3630 __connman_service_set_favorite(service, TRUE);
3632 reply_pending(service, 0);
3634 service->userconnect = FALSE;
3636 g_get_current_time(&service->modified);
3637 __connman_storage_save_service(service);
3639 update_nameservers(service);
3640 dns_changed(service);
3641 domain_changed(service);
3643 __connman_notifier_connect(service->type);
3645 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
3646 connman_network_get_bool(service->network,
3647 "WiFi.UseWPS") == TRUE) {
3650 pass = connman_network_get_string(service->network,
3653 __connman_service_set_passphrase(service, pass);
3655 connman_network_set_bool(service->network,
3656 "WiFi.UseWPS", FALSE);
3661 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
3662 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
3663 __connman_ipconfig_disable_ipv6(
3664 service->ipconfig_ipv6);
3666 } else if (new_state == CONNMAN_SERVICE_STATE_DISCONNECT) {
3667 struct connman_service *def_service = get_default();
3669 if (__connman_notifier_count_connected() == 0 &&
3670 def_service != NULL &&
3671 def_service->provider != NULL)
3672 __connman_provider_disconnect(def_service->provider);
3674 __connman_location_finish(service);
3678 __connman_wpad_stop(service);
3680 update_nameservers(service);
3681 dns_changed(service);
3682 domain_changed(service);
3684 __connman_notifier_disconnect(service->type);
3687 if (new_state == CONNMAN_SERVICE_STATE_FAILURE) {
3688 if (service->userconnect == TRUE &&
3689 __connman_agent_report_error(service,
3690 error2string(service->error),
3691 report_error_cb, NULL) == -EIO)
3693 service_complete(service);
3695 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3697 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
3699 iter = g_hash_table_lookup(service_hash, service->identifier);
3701 g_sequence_sort_changed(iter, service_compare, NULL);
3703 __connman_profile_changed(FALSE);
3705 if (new_state == CONNMAN_SERVICE_STATE_ONLINE)
3711 int __connman_service_indicate_error(struct connman_service *service,
3712 enum connman_service_error error)
3714 DBG("service %p error %d", service, error);
3716 if (service == NULL)
3719 service->error = error;
3721 if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
3722 __connman_service_set_passphrase(service, NULL);
3724 __connman_service_ipconfig_indicate_state(service,
3725 CONNMAN_SERVICE_STATE_FAILURE,
3726 CONNMAN_IPCONFIG_TYPE_IPV4);
3727 __connman_service_ipconfig_indicate_state(service,
3728 CONNMAN_SERVICE_STATE_FAILURE,
3729 CONNMAN_IPCONFIG_TYPE_IPV6);
3733 int __connman_service_clear_error(struct connman_service *service)
3735 DBG("service %p", service);
3737 if (service == NULL)
3740 if (service->state != CONNMAN_SERVICE_STATE_FAILURE)
3743 service->state_ipv4 = service->state_ipv6 =
3744 CONNMAN_SERVICE_STATE_UNKNOWN;
3745 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;;
3747 if (service->favorite == TRUE)
3748 set_reconnect_state(service, TRUE);
3750 __connman_service_ipconfig_indicate_state(service,
3751 CONNMAN_SERVICE_STATE_IDLE,
3752 CONNMAN_IPCONFIG_TYPE_IPV6);
3755 * Toggling the IPv6 state to IDLE could trigger the auto connect
3756 * machinery and consequently the IPv4 state.
3758 if (service->state_ipv4 != CONNMAN_SERVICE_STATE_UNKNOWN &&
3759 service->state_ipv4 != CONNMAN_SERVICE_STATE_FAILURE)
3762 return __connman_service_ipconfig_indicate_state(service,
3763 CONNMAN_SERVICE_STATE_IDLE,
3764 CONNMAN_IPCONFIG_TYPE_IPV4);
3767 int __connman_service_indicate_default(struct connman_service *service)
3769 DBG("service %p", service);
3773 __connman_location_detect(service);
3778 static void check_proxy_setup(struct connman_service *service)
3781 * We start WPAD if we haven't got a PAC URL from DHCP and
3782 * if our proxy manual configuration is either empty or set
3783 * to AUTO with an empty URL.
3786 if (service->proxy != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
3789 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
3790 (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_AUTO ||
3791 service->pac != NULL))
3794 if (__connman_wpad_start(service) < 0) {
3795 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
3796 __connman_notifier_proxy_changed(service);
3800 int __connman_service_ipconfig_indicate_state(struct connman_service *service,
3801 enum connman_service_state new_state,
3802 enum connman_ipconfig_type type)
3804 struct connman_ipconfig *ipconfig = NULL;
3805 enum connman_service_state old_state;
3807 if (service == NULL)
3810 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
3811 old_state = service->state_ipv4;
3812 ipconfig = service->ipconfig_ipv4;
3813 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
3814 old_state = service->state_ipv6;
3815 ipconfig = service->ipconfig_ipv6;
3818 if (ipconfig == NULL)
3822 if (old_state == new_state)
3825 DBG("service %p (%s) state %d (%s) type %d (%s)",
3826 service, service ? service->identifier : NULL,
3827 new_state, state2string(new_state),
3828 type, __connman_ipconfig_type2string(type));
3830 switch (new_state) {
3831 case CONNMAN_SERVICE_STATE_UNKNOWN:
3832 case CONNMAN_SERVICE_STATE_IDLE:
3833 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
3836 case CONNMAN_SERVICE_STATE_ASSOCIATION:
3838 case CONNMAN_SERVICE_STATE_CONFIGURATION:
3839 __connman_ipconfig_enable(ipconfig);
3841 case CONNMAN_SERVICE_STATE_READY:
3842 update_nameservers(service);
3844 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
3845 check_proxy_setup(service);
3847 case CONNMAN_SERVICE_STATE_ONLINE:
3849 case CONNMAN_SERVICE_STATE_DISCONNECT:
3850 if (service->state == CONNMAN_SERVICE_STATE_IDLE)
3853 case CONNMAN_SERVICE_STATE_FAILURE:
3857 /* We keep that state */
3858 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
3859 service->state_ipv4 = new_state;
3860 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
3861 service->state_ipv6 = new_state;
3863 return __connman_service_indicate_state(service);
3866 int __connman_service_request_login(struct connman_service *service)
3868 DBG("service %p", service);
3870 if (service == NULL)
3873 service->login_required = TRUE;
3874 login_changed(service);
3879 static connman_bool_t prepare_network(struct connman_service *service)
3881 enum connman_network_type type;
3882 unsigned int ssid_len;
3884 type = connman_network_get_type(service->network);
3887 case CONNMAN_NETWORK_TYPE_UNKNOWN:
3888 case CONNMAN_NETWORK_TYPE_VENDOR:
3890 case CONNMAN_NETWORK_TYPE_WIFI:
3891 if (connman_network_get_blob(service->network, "WiFi.SSID",
3895 if (service->passphrase != NULL)
3896 connman_network_set_string(service->network,
3897 "WiFi.Passphrase", service->passphrase);
3899 case CONNMAN_NETWORK_TYPE_ETHERNET:
3900 case CONNMAN_NETWORK_TYPE_WIMAX:
3901 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
3902 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
3903 case CONNMAN_NETWORK_TYPE_CELLULAR:
3910 static void prepare_8021x(struct connman_service *service)
3912 if (service->eap != NULL)
3913 connman_network_set_string(service->network, "WiFi.EAP",
3916 if (service->identity != NULL)
3917 connman_network_set_string(service->network, "WiFi.Identity",
3920 if (service->ca_cert_file != NULL)
3921 connman_network_set_string(service->network, "WiFi.CACertFile",
3922 service->ca_cert_file);
3924 if (service->client_cert_file != NULL)
3925 connman_network_set_string(service->network,
3926 "WiFi.ClientCertFile",
3927 service->client_cert_file);
3929 if (service->private_key_file != NULL)
3930 connman_network_set_string(service->network,
3931 "WiFi.PrivateKeyFile",
3932 service->private_key_file);
3934 if (service->private_key_passphrase != NULL)
3935 connman_network_set_string(service->network,
3936 "WiFi.PrivateKeyPassphrase",
3937 service->private_key_passphrase);
3939 if (service->phase2 != NULL)
3940 connman_network_set_string(service->network, "WiFi.Phase2",
3944 static int service_connect(struct connman_service *service)
3948 switch (service->type) {
3949 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3950 case CONNMAN_SERVICE_TYPE_SYSTEM:
3951 case CONNMAN_SERVICE_TYPE_GPS:
3952 case CONNMAN_SERVICE_TYPE_GADGET:
3954 case CONNMAN_SERVICE_TYPE_ETHERNET:
3955 case CONNMAN_SERVICE_TYPE_WIMAX:
3956 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3957 case CONNMAN_SERVICE_TYPE_CELLULAR:
3958 case CONNMAN_SERVICE_TYPE_VPN:
3960 case CONNMAN_SERVICE_TYPE_WIFI:
3961 switch (service->security) {
3962 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3963 case CONNMAN_SERVICE_SECURITY_NONE:
3965 case CONNMAN_SERVICE_SECURITY_WEP:
3966 case CONNMAN_SERVICE_SECURITY_PSK:
3967 case CONNMAN_SERVICE_SECURITY_WPA:
3968 case CONNMAN_SERVICE_SECURITY_RSN:
3969 if (service->passphrase == NULL) {
3970 if (service->network == NULL)
3973 if (service->wps == FALSE ||
3974 connman_network_get_bool(
3976 "WiFi.UseWPS") == FALSE)
3980 case CONNMAN_SERVICE_SECURITY_8021X:
3981 if (service->eap == NULL)
3985 * never request credentials if using EAP-TLS
3986 * (EAP-TLS networks need to be fully provisioned)
3988 if (g_str_equal(service->eap, "tls") == TRUE)
3992 * Return -ENOKEY if either identity or passphrase is
3993 * missing. Agent provided credentials can be used as
3994 * fallback if needed.
3996 if ((service->identity == NULL &&
3997 service->agent_identity == NULL) ||
3998 (service->passphrase == NULL &&
3999 service->agent_passphrase == NULL))
4007 if (service->network != NULL) {
4008 if (prepare_network(service) == FALSE)
4011 switch (service->security) {
4012 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
4013 case CONNMAN_SERVICE_SECURITY_NONE:
4014 case CONNMAN_SERVICE_SECURITY_WEP:
4015 case CONNMAN_SERVICE_SECURITY_PSK:
4016 case CONNMAN_SERVICE_SECURITY_WPA:
4017 case CONNMAN_SERVICE_SECURITY_RSN:
4019 case CONNMAN_SERVICE_SECURITY_8021X:
4020 prepare_8021x(service);
4024 if (__connman_stats_service_register(service) == 0) {
4025 __connman_stats_get(service, FALSE,
4026 &service->stats.data);
4027 __connman_stats_get(service, TRUE,
4028 &service->stats_roaming.data);
4031 if (service->ipconfig_ipv4)
4032 __connman_ipconfig_enable(service->ipconfig_ipv4);
4033 if (service->ipconfig_ipv6)
4034 __connman_ipconfig_enable(service->ipconfig_ipv6);
4036 err = __connman_network_connect(service->network);
4037 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4038 service->provider != NULL)
4039 err = __connman_provider_connect(service->provider);
4044 if (err != -EINPROGRESS) {
4045 __connman_ipconfig_disable(service->ipconfig_ipv4);
4046 __connman_ipconfig_disable(service->ipconfig_ipv6);
4047 __connman_stats_service_unregister(service);
4055 int __connman_service_connect(struct connman_service *service)
4059 DBG("service %p state %s", service, state2string(service->state));
4061 if (is_connected(service) == TRUE)
4064 if (is_connecting(service) == TRUE)
4067 switch (service->type) {
4068 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4069 case CONNMAN_SERVICE_TYPE_SYSTEM:
4070 case CONNMAN_SERVICE_TYPE_GPS:
4071 case CONNMAN_SERVICE_TYPE_GADGET:
4074 err = service_connect(service);
4080 if (err == -EINPROGRESS) {
4081 if (service->timeout == 0)
4082 service->timeout = g_timeout_add_seconds(
4083 CONNECT_TIMEOUT, connect_timeout, service);
4085 return -EINPROGRESS;
4088 __connman_service_ipconfig_indicate_state(service,
4089 CONNMAN_SERVICE_STATE_FAILURE,
4090 CONNMAN_IPCONFIG_TYPE_IPV4);
4091 __connman_service_ipconfig_indicate_state(service,
4092 CONNMAN_SERVICE_STATE_FAILURE,
4093 CONNMAN_IPCONFIG_TYPE_IPV6);
4095 if (service->network != NULL)
4096 __connman_network_disconnect(service->network);
4097 else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4098 service->provider != NULL)
4099 __connman_provider_disconnect(service->provider);
4101 if (service->userconnect == TRUE) {
4102 if (err == -ENOKEY) {
4103 if (__connman_agent_request_input(service,
4106 return -EINPROGRESS;
4108 reply_pending(service, err);
4114 int __connman_service_disconnect(struct connman_service *service)
4118 DBG("service %p", service);
4120 if (service->network != NULL) {
4121 err = __connman_network_disconnect(service->network);
4122 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4123 service->provider != NULL)
4124 err = __connman_provider_disconnect(service->provider);
4128 if (err < 0 && err != -EINPROGRESS)
4131 __connman_6to4_remove(service->ipconfig_ipv4);
4133 if (service->ipconfig_ipv4)
4134 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
4137 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
4140 __connman_ipconfig_address_remove(service->ipconfig_ipv4);
4141 __connman_ipconfig_address_remove(service->ipconfig_ipv6);
4143 __connman_ipconfig_disable(service->ipconfig_ipv4);
4144 __connman_ipconfig_disable(service->ipconfig_ipv6);
4146 __connman_stats_service_unregister(service);
4151 int __connman_service_disconnect_all(void)
4153 GSequenceIter *iter;
4157 iter = g_sequence_get_begin_iter(service_list);
4159 while (g_sequence_iter_is_end(iter) == FALSE) {
4160 struct connman_service *service = g_sequence_get(iter);
4162 service->ignore = TRUE;
4164 set_reconnect_state(service, FALSE);
4166 __connman_service_disconnect(service);
4168 iter = g_sequence_iter_next(iter);
4176 * __connman_service_lookup:
4177 * @pattern: search pattern
4178 * @path: return object path
4180 * Look up a service path from a search pattern
4182 int __connman_service_lookup(const char *pattern, const char **path)
4184 GHashTableIter iter;
4185 gpointer key, value;
4187 g_hash_table_iter_init(&iter, service_hash);
4189 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
4190 GSequenceIter *iter = value;
4191 struct connman_service *service = g_sequence_get(iter);
4193 if (g_strcmp0(service->identifier, pattern) == 0 ||
4194 g_strcmp0(service->name, pattern) == 0) {
4195 *path = (const char *) service->path;
4204 * lookup_by_identifier:
4205 * @identifier: service identifier
4207 * Look up a service by identifier (reference count will not be increased)
4209 static struct connman_service *lookup_by_identifier(const char *identifier)
4211 GSequenceIter *iter;
4213 iter = g_hash_table_lookup(service_hash, identifier);
4215 return g_sequence_get(iter);
4220 static struct connman_network *create_hidden_wifi(struct connman_device *device,
4221 const char *ssid, const char *mode, const char *security,
4224 struct connman_network *network;
4227 unsigned int i, ssid_len;
4229 ssid_len = strlen(ssid);
4233 network = connman_network_create(group, CONNMAN_NETWORK_TYPE_WIFI);
4234 if (network == NULL)
4237 connman_network_set_blob(network, "WiFi.SSID",
4238 (unsigned char *) ssid, ssid_len);
4240 connman_network_set_string(network, "WiFi.Mode", mode);
4241 connman_network_set_string(network, "WiFi.Security", security);
4243 name = g_try_malloc0(ssid_len + 1);
4245 connman_network_unref(network);
4249 for (i = 0; i < ssid_len; i++) {
4250 if (g_ascii_isprint(ssid[i]))
4256 connman_network_set_name(network, name);
4260 index = connman_device_get_index(device);
4261 connman_network_set_index(network, index);
4263 if (connman_device_add_network(device, network) < 0) {
4264 connman_network_unref(network);
4268 connman_network_set_available(network, TRUE);
4273 int __connman_service_create_and_connect(DBusMessage *msg)
4275 struct connman_service *service;
4276 struct connman_network *network;
4277 struct connman_device *device;
4278 DBusMessageIter iter, array;
4279 const char *mode = "managed", *security = "none", *group_security;
4280 const char *type = NULL, *ssid = NULL, *passphrase = NULL;
4281 unsigned int ssid_len = 0;
4286 dbus_message_iter_init(msg, &iter);
4287 dbus_message_iter_recurse(&iter, &array);
4289 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
4290 DBusMessageIter entry, value;
4293 dbus_message_iter_recurse(&array, &entry);
4294 dbus_message_iter_get_basic(&entry, &key);
4296 dbus_message_iter_next(&entry);
4297 dbus_message_iter_recurse(&entry, &value);
4299 switch (dbus_message_iter_get_arg_type(&value)) {
4300 case DBUS_TYPE_STRING:
4301 if (g_str_equal(key, "Type") == TRUE)
4302 dbus_message_iter_get_basic(&value, &type);
4303 else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
4304 g_str_equal(key, "Mode") == TRUE)
4305 dbus_message_iter_get_basic(&value, &mode);
4306 else if (g_str_equal(key, "WiFi.Security") == TRUE ||
4307 g_str_equal(key, "Security") == TRUE)
4308 dbus_message_iter_get_basic(&value, &security);
4309 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
4310 g_str_equal(key, "Passphrase") == TRUE)
4311 dbus_message_iter_get_basic(&value, &passphrase);
4312 else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
4313 g_str_equal(key, "SSID") == TRUE)
4314 dbus_message_iter_get_basic(&value, &ssid);
4317 dbus_message_iter_next(&array);
4323 if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
4329 ssid_len = strlen(ssid);
4333 if (g_strcmp0(security, "none") != 0 &&
4334 g_strcmp0(security, "wep") != 0 &&
4335 g_strcmp0(security, "psk") != 0 &&
4336 g_strcmp0(security, "wpa") != 0 &&
4337 g_strcmp0(security, "rsn") != 0 &&
4338 g_strcmp0(security, "ieee8021x") != 0)
4341 device = __connman_device_find_device(CONNMAN_SERVICE_TYPE_WIFI);
4345 ident = connman_device_get_ident(device);
4350 if (!g_strcmp0(security, "wpa") ||
4351 !g_strcmp0(security, "rsn"))
4352 group_security = "psk";
4354 group_security = security;
4356 group = wifi_build_group_name((unsigned char *) ssid,
4357 ssid_len, mode, group_security);
4361 name = g_strdup_printf("%s_%s_%s", type, ident, group);
4363 service = lookup_by_identifier(name);
4365 if (service != NULL)
4368 network = create_hidden_wifi(device, ssid, mode, security, group);
4369 if (network != NULL)
4370 connman_network_set_group(network, group);
4372 service = lookup_by_identifier(name);
4378 if (service == NULL) {
4383 service->network_created = TRUE;
4385 if (is_connected(service) == TRUE) {
4390 if (is_connecting(service) == TRUE) {
4395 set_reconnect_state(service, FALSE);
4397 __connman_device_disconnect(device);
4399 if (passphrase != NULL) {
4400 g_free(service->passphrase);
4401 service->passphrase = g_strdup(passphrase);
4404 service->userconnect = TRUE;
4406 err = __connman_service_connect(service);
4407 if (err < 0 && err != -EINPROGRESS)
4410 service->pending = dbus_message_ref(msg);
4415 if (service != NULL && service->network_created == TRUE) {
4416 struct connman_network *network = service->network;
4418 if (network != NULL) {
4419 connman_network_set_available(network, FALSE);
4420 __connman_device_cleanup_networks(device);
4422 __connman_service_put(service);
4428 static void provision_changed(gpointer value, gpointer user_data)
4430 struct connman_service *service = value;
4431 char *path = user_data;
4433 __connman_config_provision_service_ident(service, path);
4436 void __connman_service_provision_changed(const char *ident)
4438 g_sequence_foreach(service_list, provision_changed, (void *)ident);
4441 int __connman_service_provision(DBusMessage *msg)
4443 GKeyFile *keyfile = NULL;
4444 const char *config_str = NULL;
4445 char *group = NULL, *ident = NULL;
4447 struct connman_service *service;
4451 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &config_str,
4454 if (config_str == NULL || strlen(config_str) == 0)
4457 keyfile = g_key_file_new();
4459 /* populate GKeyFile with config_str */
4460 if (g_key_file_load_from_data(keyfile, config_str,
4461 strlen(config_str), 0, NULL) == FALSE) {
4467 * read only one group of settings (only one service supported, no
4470 group = g_key_file_get_start_group(keyfile);
4472 if (group == NULL || g_str_has_prefix(group, "service_") == FALSE) {
4477 err = __connman_config_load_service(keyfile, group, TRUE);
4481 ident = group + strlen("service_");
4483 /* trigger service provisioning if service exists */
4484 service = lookup_by_identifier(ident);
4485 if (service != NULL)
4486 __connman_config_provision_service(service);
4488 g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
4494 if (keyfile != NULL)
4495 g_key_file_free(keyfile);
4501 * __connman_service_get:
4502 * @identifier: service identifier
4504 * Look up a service by identifier or create a new one if not found
4506 static struct connman_service *service_get(const char *identifier)
4508 struct connman_service *service;
4509 GSequenceIter *iter;
4511 iter = g_hash_table_lookup(service_hash, identifier);
4513 service = g_sequence_get(iter);
4514 if (service != NULL)
4515 connman_service_ref(service);
4519 service = connman_service_create();
4520 if (service == NULL)
4523 DBG("service %p", service);
4525 service->identifier = g_strdup(identifier);
4527 service->profile = g_strdup(__connman_profile_active_ident());
4529 iter = g_sequence_insert_sorted(service_list, service,
4530 service_compare, NULL);
4532 g_hash_table_insert(service_hash, service->identifier, iter);
4537 static int service_register(struct connman_service *service)
4539 const char *path = __connman_profile_active_path();
4540 GSequenceIter *iter;
4542 DBG("service %p", service);
4544 if (service->path != NULL)
4547 service->path = g_strdup_printf("%s/%s", path, service->identifier);
4549 DBG("path %s", service->path);
4551 __connman_config_provision_service(service);
4553 __connman_storage_load_service(service);
4555 g_dbus_register_interface(connection, service->path,
4556 CONNMAN_SERVICE_INTERFACE,
4557 service_methods, service_signals,
4558 NULL, service, NULL);
4560 iter = g_hash_table_lookup(service_hash, service->identifier);
4562 g_sequence_sort_changed(iter, service_compare, NULL);
4564 __connman_profile_changed(TRUE);
4569 static void service_up(struct connman_ipconfig *ipconfig)
4571 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4573 DBG("%s up", connman_ipconfig_get_ifname(ipconfig));
4575 link_changed(service);
4577 service->stats.valid = FALSE;
4578 service->stats_roaming.valid = FALSE;
4581 static void service_down(struct connman_ipconfig *ipconfig)
4583 DBG("%s down", connman_ipconfig_get_ifname(ipconfig));
4586 static void service_lower_up(struct connman_ipconfig *ipconfig)
4588 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4590 DBG("%s lower up", connman_ipconfig_get_ifname(ipconfig));
4592 stats_start(service);
4595 static void service_lower_down(struct connman_ipconfig *ipconfig)
4597 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4599 DBG("%s lower down", connman_ipconfig_get_ifname(ipconfig));
4601 stats_stop(service);
4602 __connman_storage_save_service(service);
4605 static void service_ip_bound(struct connman_ipconfig *ipconfig)
4607 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4608 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4609 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4611 DBG("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
4613 type = __connman_ipconfig_get_config_type(ipconfig);
4614 method = __connman_ipconfig_get_method(ipconfig);
4616 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4619 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4620 method == CONNMAN_IPCONFIG_METHOD_AUTO)
4621 __connman_service_ipconfig_indicate_state(service,
4622 CONNMAN_SERVICE_STATE_READY,
4623 CONNMAN_IPCONFIG_TYPE_IPV6);
4625 settings_changed(service, ipconfig);
4628 static void service_ip_release(struct connman_ipconfig *ipconfig)
4630 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4631 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4632 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4634 DBG("%s ip release", connman_ipconfig_get_ifname(ipconfig));
4636 type = __connman_ipconfig_get_config_type(ipconfig);
4637 method = __connman_ipconfig_get_method(ipconfig);
4639 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4642 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4643 method == CONNMAN_IPCONFIG_METHOD_OFF)
4644 __connman_service_ipconfig_indicate_state(service,
4645 CONNMAN_SERVICE_STATE_DISCONNECT,
4646 CONNMAN_IPCONFIG_TYPE_IPV6);
4648 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
4649 method == CONNMAN_IPCONFIG_METHOD_OFF)
4650 __connman_service_ipconfig_indicate_state(service,
4651 CONNMAN_SERVICE_STATE_DISCONNECT,
4652 CONNMAN_IPCONFIG_TYPE_IPV4);
4654 settings_changed(service, ipconfig);
4657 static const struct connman_ipconfig_ops service_ops = {
4659 .down = service_down,
4660 .lower_up = service_lower_up,
4661 .lower_down = service_lower_down,
4662 .ip_bound = service_ip_bound,
4663 .ip_release = service_ip_release,
4666 static void setup_ip4config(struct connman_service *service, int index,
4667 enum connman_ipconfig_method method)
4672 service->ipconfig_ipv4 = connman_ipconfig_create(index,
4673 CONNMAN_IPCONFIG_TYPE_IPV4);
4674 if (service->ipconfig_ipv4 == NULL)
4677 connman_ipconfig_set_method(service->ipconfig_ipv4, method);
4679 connman_ipconfig_set_data(service->ipconfig_ipv4, service);
4681 connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
4684 static void setup_ip6config(struct connman_service *service, int index)
4689 service->ipconfig_ipv6 = connman_ipconfig_create(index,
4690 CONNMAN_IPCONFIG_TYPE_IPV6);
4691 if (service->ipconfig_ipv6 == NULL)
4694 connman_ipconfig_set_data(service->ipconfig_ipv6, service);
4696 connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
4699 void __connman_service_read_ip4config(struct connman_service *service)
4701 const char *ident = service->profile;
4707 if (service->ipconfig_ipv4 == NULL)
4710 keyfile = __connman_storage_open_profile(ident);
4711 if (keyfile == NULL)
4714 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
4715 service->identifier, "IPv4.");
4717 g_key_file_free(keyfile);
4720 void __connman_service_create_ip4config(struct connman_service *service,
4723 DBG("ipv4 %p", service->ipconfig_ipv4);
4725 if (service->ipconfig_ipv4 != NULL)
4728 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
4729 __connman_service_read_ip4config(service);
4732 void __connman_service_read_ip6config(struct connman_service *service)
4734 const char *ident = service->profile;
4739 if (service->ipconfig_ipv6 == NULL)
4742 keyfile = __connman_storage_open_profile(ident);
4744 if (keyfile == NULL)
4747 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
4748 service->identifier, "IPv6.");
4750 g_key_file_free(keyfile);
4753 void __connman_service_create_ip6config(struct connman_service *service,
4756 DBG("ipv6 %p", service->ipconfig_ipv6);
4758 if (service->ipconfig_ipv6 != NULL)
4761 setup_ip6config(service, index);
4762 __connman_service_read_ip6config(service);
4766 * __connman_service_lookup_from_network:
4767 * @network: network structure
4769 * Look up a service by network (reference count will not be increased)
4771 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
4773 struct connman_service *service;
4774 const char *ident, *group;
4777 DBG("network %p", network);
4779 ident = __connman_network_get_ident(network);
4783 group = connman_network_get_group(network);
4787 name = g_strdup_printf("%s_%s_%s",
4788 __connman_network_get_type(network), ident, group);
4789 service = lookup_by_identifier(name);
4795 struct connman_service *__connman_service_lookup_from_index(int index)
4797 struct connman_service *service;
4798 GSequenceIter *iter;
4800 iter = g_sequence_get_begin_iter(service_list);
4802 while (g_sequence_iter_is_end(iter) == FALSE) {
4803 service = g_sequence_get(iter);
4805 if (connman_ipconfig_get_index(service->ipconfig_ipv4)
4809 if (connman_ipconfig_get_index(service->ipconfig_ipv6)
4813 iter = g_sequence_iter_next(iter);
4819 const char *__connman_service_get_ident(struct connman_service *service)
4821 return service->identifier;
4824 const char *__connman_service_get_path(struct connman_service *service)
4826 return service->path;
4829 unsigned int __connman_service_get_order(struct connman_service *service)
4831 GSequenceIter *iter;
4833 if (service == NULL)
4836 if (service->favorite == FALSE) {
4841 iter = g_hash_table_lookup(service_hash, service->identifier);
4843 if (g_sequence_iter_get_position(iter) == 0)
4845 else if (service->type == CONNMAN_SERVICE_TYPE_VPN)
4846 service->order = 10;
4852 return service->order;
4855 static enum connman_service_type convert_network_type(struct connman_network *network)
4857 enum connman_network_type type = connman_network_get_type(network);
4860 case CONNMAN_NETWORK_TYPE_UNKNOWN:
4861 case CONNMAN_NETWORK_TYPE_VENDOR:
4863 case CONNMAN_NETWORK_TYPE_ETHERNET:
4864 return CONNMAN_SERVICE_TYPE_ETHERNET;
4865 case CONNMAN_NETWORK_TYPE_WIFI:
4866 return CONNMAN_SERVICE_TYPE_WIFI;
4867 case CONNMAN_NETWORK_TYPE_WIMAX:
4868 return CONNMAN_SERVICE_TYPE_WIMAX;
4869 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
4870 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
4871 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
4872 case CONNMAN_NETWORK_TYPE_CELLULAR:
4873 return CONNMAN_SERVICE_TYPE_CELLULAR;
4876 return CONNMAN_SERVICE_TYPE_UNKNOWN;
4879 static enum connman_service_security convert_wifi_security(const char *security)
4881 if (security == NULL)
4882 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4883 else if (g_str_equal(security, "none") == TRUE)
4884 return CONNMAN_SERVICE_SECURITY_NONE;
4885 else if (g_str_equal(security, "wep") == TRUE)
4886 return CONNMAN_SERVICE_SECURITY_WEP;
4887 else if (g_str_equal(security, "psk") == TRUE)
4888 return CONNMAN_SERVICE_SECURITY_PSK;
4889 else if (g_str_equal(security, "ieee8021x") == TRUE)
4890 return CONNMAN_SERVICE_SECURITY_8021X;
4891 else if (g_str_equal(security, "wpa") == TRUE)
4892 return CONNMAN_SERVICE_SECURITY_WPA;
4893 else if (g_str_equal(security, "rsn") == TRUE)
4894 return CONNMAN_SERVICE_SECURITY_RSN;
4896 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4899 static void update_from_network(struct connman_service *service,
4900 struct connman_network *network)
4902 connman_uint8_t strength = service->strength;
4903 GSequenceIter *iter;
4906 DBG("service %p network %p", service, network);
4908 if (is_connected(service) == TRUE)
4911 if (is_connecting(service) == TRUE)
4914 str = connman_network_get_string(network, "Name");
4916 g_free(service->name);
4917 service->name = g_strdup(str);
4918 service->hidden = FALSE;
4920 g_free(service->name);
4921 service->name = NULL;
4922 service->hidden = TRUE;
4925 service->strength = connman_network_get_strength(network);
4926 service->roaming = connman_network_get_bool(network, "Roaming");
4928 if (service->strength == 0) {
4930 * Filter out 0-values; it's unclear what they mean
4931 * and they cause anomalous sorting of the priority list.
4933 service->strength = strength;
4936 str = connman_network_get_string(network, "WiFi.Security");
4937 service->security = convert_wifi_security(str);
4939 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
4940 service->wps = connman_network_get_bool(network, "WiFi.WPS");
4942 if (service->strength > strength && service->network != NULL) {
4943 service->network = network;
4945 strength_changed(service);
4948 if (service->network == NULL)
4949 service->network = network;
4951 iter = g_hash_table_lookup(service_hash, service->identifier);
4953 g_sequence_sort_changed(iter, service_compare, NULL);
4957 * __connman_service_create_from_network:
4958 * @network: network structure
4960 * Look up service by network and if not found, create one
4962 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
4964 struct connman_service *service;
4965 struct connman_device *device;
4966 const char *ident, *group;
4970 DBG("network %p", network);
4972 ident = __connman_network_get_ident(network);
4976 group = connman_network_get_group(network);
4980 name = g_strdup_printf("%s_%s_%s",
4981 __connman_network_get_type(network), ident, group);
4982 service = service_get(name);
4985 if (service == NULL)
4988 if (__connman_network_get_weakness(network) == TRUE)
4991 if (service->path != NULL) {
4992 update_from_network(service, network);
4993 __connman_profile_changed(TRUE);
4997 service->type = convert_network_type(network);
4999 switch (service->type) {
5000 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5001 case CONNMAN_SERVICE_TYPE_SYSTEM:
5002 case CONNMAN_SERVICE_TYPE_ETHERNET:
5003 case CONNMAN_SERVICE_TYPE_WIMAX:
5004 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5005 case CONNMAN_SERVICE_TYPE_GPS:
5006 case CONNMAN_SERVICE_TYPE_VPN:
5007 case CONNMAN_SERVICE_TYPE_GADGET:
5008 service->autoconnect = FALSE;
5010 case CONNMAN_SERVICE_TYPE_WIFI:
5011 case CONNMAN_SERVICE_TYPE_CELLULAR:
5012 service->autoconnect = TRUE;
5016 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5017 service->state = combine_state(service->state_ipv4, service->state_ipv6);
5019 update_from_network(service, network);
5021 index = connman_network_get_index(network);
5023 if (service->ipconfig_ipv4 == NULL)
5024 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
5026 if (service->ipconfig_ipv6 == NULL)
5027 setup_ip6config(service, index);
5029 service_register(service);
5031 if (service->favorite == TRUE) {
5032 device = connman_network_get_device(service->network);
5033 if (device && __connman_device_scanning(device) == FALSE)
5034 __connman_service_auto_connect();
5037 __connman_notifier_service_add(service, service->name);
5042 void __connman_service_update_from_network(struct connman_network *network)
5044 struct connman_service *service;
5045 connman_uint8_t strength;
5046 connman_bool_t roaming;
5047 GSequenceIter *iter;
5049 connman_bool_t stats_enable;
5051 DBG("network %p", network);
5053 service = __connman_service_lookup_from_network(network);
5054 if (service == NULL)
5057 if (service->network == NULL)
5060 name = connman_network_get_string(service->network, "Name");
5061 if (g_strcmp0(service->name, name) != 0) {
5062 g_free(service->name);
5063 service->name = g_strdup(name);
5064 connman_dbus_property_changed_basic(service->path,
5065 CONNMAN_SERVICE_INTERFACE, "Name",
5066 DBUS_TYPE_STRING, &service->name);
5069 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
5070 service->wps = connman_network_get_bool(network, "WiFi.WPS");
5072 strength = connman_network_get_strength(service->network);
5073 if (strength == service->strength)
5076 service->strength = strength;
5078 strength_changed(service);
5081 roaming = connman_network_get_bool(service->network, "Roaming");
5082 if (roaming == service->roaming)
5085 stats_enable = stats_enabled(service);
5086 if (stats_enable == TRUE)
5087 stats_stop(service);
5089 service->roaming = roaming;
5091 if (stats_enable == TRUE)
5092 stats_start(service);
5094 roaming_changed(service);
5096 iter = g_hash_table_lookup(service_hash, service->identifier);
5098 g_sequence_sort_changed(iter, service_compare, NULL);
5101 void __connman_service_remove_from_network(struct connman_network *network)
5103 struct connman_service *service;
5105 DBG("network %p", network);
5107 service = __connman_service_lookup_from_network(network);
5108 if (service == NULL)
5111 __connman_service_put(service);
5115 * __connman_service_create_from_provider:
5116 * @provider: provider structure
5118 * Look up service by provider and if not found, create one
5120 struct connman_service *
5121 __connman_service_create_from_provider(struct connman_provider *provider)
5123 struct connman_service *service;
5124 const char *ident, *str;
5126 int index = connman_provider_get_index(provider);
5128 DBG("provider %p", provider);
5130 ident = __connman_provider_get_ident(provider);
5134 name = g_strdup_printf("vpn_%s", ident);
5135 service = service_get(name);
5138 if (service == NULL)
5141 service->type = CONNMAN_SERVICE_TYPE_VPN;
5142 service->provider = connman_provider_ref(provider);
5143 service->autoconnect = FALSE;
5145 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5147 str = connman_provider_get_string(provider, "Name");
5149 g_free(service->name);
5150 service->name = g_strdup(str);
5151 service->hidden = FALSE;
5153 g_free(service->name);
5154 service->name = NULL;
5155 service->hidden = TRUE;
5158 service->strength = 0;
5160 if (service->ipconfig_ipv4 == NULL)
5161 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_MANUAL);
5163 if (service->ipconfig_ipv6 == NULL)
5164 setup_ip6config(service, index);
5166 service_register(service);
5168 __connman_notifier_service_add(service, service->name);
5173 void __connman_service_downgrade_state(struct connman_service *service)
5175 if (service == NULL)
5178 DBG("service %p state4 %d state6 %d", service, service->state_ipv4,
5179 service->state_ipv6);
5181 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_ONLINE)
5182 __connman_service_ipconfig_indicate_state(service,
5183 CONNMAN_SERVICE_STATE_READY,
5184 CONNMAN_IPCONFIG_TYPE_IPV4);
5186 if (service->state_ipv6 == CONNMAN_SERVICE_STATE_ONLINE)
5187 __connman_service_ipconfig_indicate_state(service,
5188 CONNMAN_SERVICE_STATE_READY,
5189 CONNMAN_IPCONFIG_TYPE_IPV6);
5192 static int service_load(struct connman_service *service)
5194 const char *ident = service->profile;
5196 GError *error = NULL;
5197 gchar *pathname, *data = NULL;
5200 connman_bool_t autoconnect;
5201 unsigned int ssid_len;
5204 DBG("service %p", service);
5209 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
5210 if (pathname == NULL)
5213 keyfile = g_key_file_new();
5215 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
5222 if (g_key_file_load_from_data(keyfile, data, length,
5223 0, NULL) == FALSE) {
5230 switch (service->type) {
5231 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5232 case CONNMAN_SERVICE_TYPE_SYSTEM:
5233 case CONNMAN_SERVICE_TYPE_ETHERNET:
5234 case CONNMAN_SERVICE_TYPE_GPS:
5235 case CONNMAN_SERVICE_TYPE_VPN:
5236 case CONNMAN_SERVICE_TYPE_GADGET:
5238 case CONNMAN_SERVICE_TYPE_WIFI:
5239 if (service->name == NULL) {
5242 name = g_key_file_get_string(keyfile,
5243 service->identifier, "Name", NULL);
5245 g_free(service->name);
5246 service->name = name;
5249 if (service->network != NULL)
5250 connman_network_set_name(service->network,
5254 if (service->network &&
5255 connman_network_get_blob(service->network,
5256 "WiFi.SSID", &ssid_len) == NULL) {
5259 hex_ssid = g_key_file_get_string(keyfile,
5260 service->identifier,
5263 if (hex_ssid != NULL) {
5265 unsigned int i, j = 0, hex;
5266 size_t hex_ssid_len = strlen(hex_ssid);
5268 ssid = g_try_malloc0(hex_ssid_len / 2);
5275 for (i = 0; i < hex_ssid_len; i += 2) {
5276 sscanf(hex_ssid + i, "%02x", &hex);
5280 connman_network_set_blob(service->network,
5281 "WiFi.SSID", ssid, hex_ssid_len / 2);
5288 case CONNMAN_SERVICE_TYPE_WIMAX:
5289 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5290 case CONNMAN_SERVICE_TYPE_CELLULAR:
5291 service->favorite = g_key_file_get_boolean(keyfile,
5292 service->identifier, "Favorite", NULL);
5294 autoconnect = g_key_file_get_boolean(keyfile,
5295 service->identifier, "AutoConnect", &error);
5297 service->autoconnect = autoconnect;
5298 g_clear_error(&error);
5300 str = g_key_file_get_string(keyfile,
5301 service->identifier, "Failure", NULL);
5303 if (service->favorite == FALSE)
5304 service->state_ipv4 = service->state_ipv6 =
5305 CONNMAN_SERVICE_STATE_FAILURE;
5306 service->error = string2error(str);
5311 str = g_key_file_get_string(keyfile,
5312 service->identifier, "Modified", NULL);
5314 g_time_val_from_iso8601(str, &service->modified);
5318 str = g_key_file_get_string(keyfile,
5319 service->identifier, "Passphrase", NULL);
5321 g_free(service->passphrase);
5322 service->passphrase = str;
5325 if (service->ipconfig_ipv4 != NULL)
5326 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
5327 service->identifier, "IPv4.");
5329 if (service->ipconfig_ipv6 != NULL)
5330 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
5331 service->identifier, "IPv6.");
5333 service->nameservers_config = g_key_file_get_string_list(keyfile,
5334 service->identifier, "Nameservers", &length, NULL);
5335 if (service->nameservers_config != NULL && length == 0) {
5336 g_strfreev(service->nameservers_config);
5337 service->nameservers_config = NULL;
5340 service->domains = g_key_file_get_string_list(keyfile,
5341 service->identifier, "Domains", &length, NULL);
5342 if (service->domains != NULL && length == 0) {
5343 g_strfreev(service->domains);
5344 service->domains = NULL;
5347 str = g_key_file_get_string(keyfile,
5348 service->identifier, "Proxy.Method", NULL);
5350 service->proxy_config = string2proxymethod(str);
5354 service->proxies = g_key_file_get_string_list(keyfile,
5355 service->identifier, "Proxy.Servers", &length, NULL);
5356 if (service->proxies != NULL && length == 0) {
5357 g_strfreev(service->proxies);
5358 service->proxies = NULL;
5361 service->excludes = g_key_file_get_string_list(keyfile,
5362 service->identifier, "Proxy.Excludes", &length, NULL);
5363 if (service->excludes != NULL && length == 0) {
5364 g_strfreev(service->excludes);
5365 service->excludes = NULL;
5368 str = g_key_file_get_string(keyfile,
5369 service->identifier, "Proxy.URL", NULL);
5371 g_free(service->pac);
5376 g_key_file_free(keyfile);
5381 static int service_save(struct connman_service *service)
5383 const char *ident = service->profile;
5385 gchar *pathname, *data = NULL;
5388 const char *cst_str = NULL;
5391 DBG("service %p", service);
5396 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
5397 if (pathname == NULL)
5400 keyfile = g_key_file_new();
5402 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
5406 if (g_key_file_load_from_data(keyfile, data, length,
5414 if (service->name != NULL)
5415 g_key_file_set_string(keyfile, service->identifier,
5416 "Name", service->name);
5418 switch (service->type) {
5419 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5420 case CONNMAN_SERVICE_TYPE_SYSTEM:
5421 case CONNMAN_SERVICE_TYPE_ETHERNET:
5422 case CONNMAN_SERVICE_TYPE_GPS:
5423 case CONNMAN_SERVICE_TYPE_VPN:
5424 case CONNMAN_SERVICE_TYPE_GADGET:
5426 case CONNMAN_SERVICE_TYPE_WIFI:
5427 if (service->network) {
5428 const unsigned char *ssid;
5429 unsigned int ssid_len = 0;
5431 ssid = connman_network_get_blob(service->network,
5432 "WiFi.SSID", &ssid_len);
5434 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
5435 char *identifier = service->identifier;
5439 str = g_string_sized_new(ssid_len * 2);
5445 for (i = 0; i < ssid_len; i++)
5446 g_string_append_printf(str,
5449 g_key_file_set_string(keyfile, identifier,
5452 g_string_free(str, TRUE);
5457 case CONNMAN_SERVICE_TYPE_WIMAX:
5458 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5459 case CONNMAN_SERVICE_TYPE_CELLULAR:
5460 g_key_file_set_boolean(keyfile, service->identifier,
5461 "Favorite", service->favorite);
5463 if (service->favorite == TRUE)
5464 g_key_file_set_boolean(keyfile, service->identifier,
5465 "AutoConnect", service->autoconnect);
5467 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_FAILURE ||
5468 service->state_ipv6 == CONNMAN_SERVICE_STATE_FAILURE) {
5469 const char *failure = error2string(service->error);
5470 if (failure != NULL)
5471 g_key_file_set_string(keyfile,
5472 service->identifier,
5473 "Failure", failure);
5475 g_key_file_remove_key(keyfile, service->identifier,
5481 str = g_time_val_to_iso8601(&service->modified);
5483 g_key_file_set_string(keyfile, service->identifier,
5488 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
5489 g_key_file_set_string(keyfile, service->identifier,
5490 "Passphrase", service->passphrase);
5492 g_key_file_remove_key(keyfile, service->identifier,
5493 "Passphrase", NULL);
5495 switch (service->state) {
5496 case CONNMAN_SERVICE_STATE_UNKNOWN:
5497 case CONNMAN_SERVICE_STATE_IDLE:
5498 case CONNMAN_SERVICE_STATE_ASSOCIATION:
5500 case CONNMAN_SERVICE_STATE_CONFIGURATION:
5501 case CONNMAN_SERVICE_STATE_READY:
5502 case CONNMAN_SERVICE_STATE_ONLINE:
5503 case CONNMAN_SERVICE_STATE_DISCONNECT:
5504 case CONNMAN_SERVICE_STATE_FAILURE:
5505 if (service->ipconfig_ipv4 != NULL)
5506 __connman_ipconfig_save(service->ipconfig_ipv4,
5507 keyfile, service->identifier,
5510 if (service->ipconfig_ipv6 != NULL)
5511 __connman_ipconfig_save(service->ipconfig_ipv6,
5512 keyfile, service->identifier,
5516 if (service->nameservers_config != NULL) {
5517 guint len = g_strv_length(service->nameservers_config);
5519 g_key_file_set_string_list(keyfile, service->identifier,
5521 (const gchar **) service->nameservers_config, len);
5523 g_key_file_remove_key(keyfile, service->identifier,
5524 "Nameservers", NULL);
5526 if (service->domains != NULL) {
5527 guint len = g_strv_length(service->domains);
5529 g_key_file_set_string_list(keyfile, service->identifier,
5531 (const gchar **) service->domains, len);
5533 g_key_file_remove_key(keyfile, service->identifier,
5536 cst_str = proxymethod2string(service->proxy_config);
5537 if (cst_str != NULL)
5538 g_key_file_set_string(keyfile, service->identifier,
5539 "Proxy.Method", cst_str);
5541 if (service->proxies != NULL) {
5542 guint len = g_strv_length(service->proxies);
5544 g_key_file_set_string_list(keyfile, service->identifier,
5546 (const gchar **) service->proxies, len);
5548 g_key_file_remove_key(keyfile, service->identifier,
5549 "Proxy.Servers", NULL);
5551 if (service->excludes != NULL) {
5552 guint len = g_strv_length(service->excludes);
5554 g_key_file_set_string_list(keyfile, service->identifier,
5556 (const gchar **) service->excludes, len);
5558 g_key_file_remove_key(keyfile, service->identifier,
5559 "Proxy.Excludes", NULL);
5561 if (service->pac != NULL && strlen(service->pac) > 0)
5562 g_key_file_set_string(keyfile, service->identifier,
5563 "Proxy.URL", service->pac);
5565 g_key_file_remove_key(keyfile, service->identifier,
5568 data = g_key_file_to_data(keyfile, &length, NULL);
5570 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
5571 connman_error("Failed to store service information");
5576 g_key_file_free(keyfile);
5583 static struct connman_storage service_storage = {
5585 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
5586 .service_load = service_load,
5587 .service_save = service_save,
5590 int __connman_service_init(void)
5594 connection = connman_dbus_get_connection();
5596 if (connman_storage_register(&service_storage) < 0)
5597 connman_error("Failed to register service storage");
5599 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
5602 service_list = g_sequence_new(service_free);
5607 void __connman_service_cleanup(void)
5613 list = service_list;
5614 service_list = NULL;
5615 g_sequence_free(list);
5617 g_hash_table_destroy(service_hash);
5618 service_hash = NULL;
5620 g_slist_free(counter_list);
5621 counter_list = NULL;
5623 connman_storage_unregister(&service_storage);
5625 dbus_connection_unref(connection);